You are on page 1of 20

Tree Traversal Techniques; Heaps

• Tree Traversal Concept


• Tree Traversal Techniques: Preorder,
Inorder, Postorder
• Full Trees
• Almost Complete Trees
• Heaps
CS 103 1
Binary-Tree Related Definitions
• The children of any node in a binary tree are
ordered into a left child and a right child
1
• A node can have a left and
3 7
a right child, a left child
only, a right child only, 5 8 9
or no children
10
• The tree made up of a left 4 6

child (of a node x) and all its 11 12


descendents is called the left subtree of x
• Right subtrees are defined similarly

CS 103 2
Binary Tree Traversal
• Traversal is the process of visiting every node
once
• Visiting a node entails doing some processing
at that node, but when describing a traversal
strategy, we need not concern ourselves with
what that processing is

CS 103 3
Binary Tree Traversal Techniques
• Three recursive techniques for binary tree
traversal
• In each technique, the left subtree is traversed
recursively, the right subtree is traversed
recursively, and the root is visited
• What distinguishes the techniques from one
another is the order of those 3 tasks

CS 103 4
Preoder, Inorder, Postorder
• In Preorder, the root Preorder Traversal:
1. Visit the root
is visited before (pre) 2. Traverse left subtree
3. Traverse right subtree
the subtrees traversals
• In Inorder, the root is Inorder Traversal:
visited in-between left 1. Traverse left subtree
2. Visit the root
and right subtree traversal 3. Traverse right subtree

• In Preorder, the root


Postorder Traversal:
is visited after (pre) 1. Traverse left subtree
2. Traverse right subtree
the subtrees traversals 3. Visit the root

CS 103 5
Illustrations for Traversals
• Assume: visiting a node 1

is printing its label 3 7

• Preorder: 5 8 9
1 3 5 4 6 7 8 9 10 11 12 10
4 6
• Inorder:
11 12
4 5 6 3 1 8 7 9 11 10 12
• Postorder:
4 6 5 3 8 11 12 10 9 7 1

CS 103 6
Illustrations for Traversals (Contd.)
• Assume: visiting a node 15
8 20
is printing its data
• Preorder: 15 8 2 6 3 7 2 11 27

11 10 12 14 20 27 22 30 6 10 12 22 30

• Inorder: 2 3 6 7 8 10 11 3 7 14
12 14 15 20 22 27 30
• Postorder: 3 7 6 2 10 14
12 11 8 22 30 27 20 15

CS 103 7
Code for the Traversal Techniques
void preOrder(Tree *tree){
• The code for visit if (tree->isEmpty( )) return;
visit(tree->getRoot( ));
is up to you to preOrder(tree->getLeftSubtree());
preOrder(tree->getRightSubtree());
provide, depending }
void inOrder(Tree *tree){
on the application if (tree->isEmpty( )) return;
• A typical example inOrder(tree->getLeftSubtree( ));
visit(tree->getRoot( ));
for visit(…) is to }
inOrder(tree->getRightSubtree( ));

print out the data void postOrder(Tree *tree){


if (tree->isEmpty( )) return;
part of its input postOrder(tree->getLeftSubtree( ));
postOrder(tree->getRightSubtree( ));
node visit(tree->getRoot( ));
}
CS 103 8
Application of Traversal Sorting a
BST
• Observe the output of the inorder
traversal of the BST example two slides
earlier
• It is sorted
• This is no coincidence
• As a general rule, if you output the keys
(data) of the nodes of a BST using inorder
traversal, the data comes out sorted in
increasing order

CS 103 9
Other Kinds of Binary Trees
(Full Binary Trees)
• Full Binary Tree: A full binary tree is a
binary tree where all the leaves are on the
same level and every non-leaf has two
children
• The first four full binary trees are:

CS 103 10
Examples of Non-Full Binary Trees

• These trees are NOT full binary trees: (do


you know why?)

CS 103 11
Canonical Labeling of
Full Binary Trees
• Label the nodes from 1 to n from the top to
the bottom, left to right 1
1 1 3
2
2 3

4 5 6 7
1
2 3 Relationships between labels
5 6 7 of children and parent:
4
i
8 9 10 11 12 13 14 15 2i 2i+1

CS 103 12
Other Kinds of Binary Trees
(Almost Complete Binary trees)
• Almost Complete Binary Tree: An almost
complete binary tree of n nodes, for any
arbitrary nonnegative integer n, is the
binary tree made up of the first n nodes of
a canonically labeled full binary 1
1 1
1
1 2 3
2 2 3
2 1
1 4 5 6
4 2 3
2 3
4 5
4 5 6 7
CS 103 13
Depth/Height of Full Trees and Almost
Complete Trees
• The height (or depth ) h of such trees is O(log n)
• Proof: In the case of full trees,
– The number of nodes n is: n=1+2+22+23+…+2h=2h+1-1
– Therefore, 2h+1 = n+1, and thus, h=log(n+1)-1
– Hence, h=O(log n)
• For almost complete trees, the proof is left as an
exercise.

CS 103 14
Canonical Labeling of
Almost Complete Binary Trees
• Same labeling inherited from full binary
trees
• Same relationship holding between the
labels of children and parents:

Relationships between labels


of children and parent:
i
2i 2i+1

CS 103 15
Tree Traversal
• Used to print out the data in a tree in a certain
order
• Pre-order traversal
– Print the data at the root
– Recursively print out all data in the left subtree
– Recursively print out all data in the right subtree
Preorder, Postorder and Inorder
• Preorder traversal
– node, left, right
– prefix expression
• ++a*bc*+*defg
Preorder, Postorder and Inorder
• Postorder traversal • Inorder traversal
– left, right, node – left, node, right
– postfix expression – infix expression
• abc*+de*f+g*+ • a+b*c+d*e+f*g
Example: Unix Directory Traversal
PreOrder PostOrder
Preorder, Postorder and Inorder
Pseudo Code

You might also like