Binary Tree Traversal
Binary Tree Traversal
Many binary tree operations are done by
performing a traversal of the binary tree
In a traversal, each element of the binary tree is
visited exactly once
During the visit of an element, all action (make a
clone, display, evaluate the operator, etc.) with
respect to this element is taken
Traversing Binary Tree
In a linked list we can start from the head and
systematically go from node to node
With binary trees the concept is a bit different because
we have options. A binary tree has three components:
Root, Left Subtree and Right Subtree. Thus there are
six possible orderings of traversal. By convention, we
always traverse the left subtree before the right one.
That reduces the choices to three:
Preorder - Visit the root node first
Inorder - Visit the left subtree, then the root
Postorder - Visit the root node last
Preorder Traversal: J E A H T M Y
Visit first
tree
‘J’
‘E’ ‘T’
‘A’ ‘H’ ‘M’ ‘Y’
Visit left subtree second Visit right subtree last
4
Preorder Traversal
Visit the root of the tree first, then visit the
nodes in the left subtree, then visit the nodes in
the right subtree
Preorder(tree)
If tree is not NULL
Visit Info(tree)
Preorder(Left(tree))
Preorder(Right(tree))
Preorder Traversal
void preOrder(TREE t) a
{
if (t != null)
b a b c c
{
visit(t);
a
preOrder(t->left);
preOrder(t->right);
} b c
} f
d e
g h i j
a b d g h e i c f j
Traversing a Tree Preorder
B C
D E F G
H I
Traversing a Tree Preorder
B C
D E F G
H I
Result: A
Traversing a Tree Preorder
B C
D E F G
H I
Result: AB
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABD
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDE
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEH
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHC
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHCF
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHCFG
Traversing a Tree Preorder
B C
D E F G
H I
Result: ABDEHCFGI
Inorder Traversal: A E H J M T Y
Visit second
tree
‘J’
‘E’ ‘T’
‘A’ ‘H’ ‘M’ ‘Y’
Visit left subtree first Visit right subtree last
17
Inorder Traversal
Visit the nodes in the left subtree, then visit the
root of the tree, then visit the nodes in the right
subtree
Inorder(tree)
If tree is not NULL
Inorder(Left(tree))
Visit Info(tree)
Inorder(Right(tree))
Inorder Traversal
Void inOrder(Tree t) a
{
if (t != null) b b a c c
{
inOrder(t->left);
a
visit(t);
inOrder(t.rightChild); b c
}
f
} d e
g h i j
g d h b e i a f j c
Traversing a Tree Inorder
B C
D E F G
H I
Traversing a Tree Inorder
B C
D E F G
H I
Traversing a Tree Inorder
B C
D E F G
H I
Traversing a Tree Inorder
B C
D E F G
H I
Result: D
Traversing a Tree Inorder
B C
D E F G
H I
Result: DB
Traversing a Tree Inorder
B C
D E F G
H I
Result: DB
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBH
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHE
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEA
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEA
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEAF
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEAFC
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEAFCG
Traversing a Tree Inorder
B C
D E F G
H I
Result: DBHEAFCGI
Implementing InOrder without recursion
void inorder(TREE root)
{
TREE temp = root;
while(not empty stack) || temp)
{
if(temp){
push(temp);
temp = temp->left;
}
else {
temp = pop();
print temp->data;
temp = temp->right;
}
}
}
Postorder Traversal: A H E M Y TJ
Visit last
tree
‘J’
‘E’ ‘T’
‘A’ ‘H’ ‘M’ ‘Y’
Visit left subtree first Visit right subtree second
35
Postorder Traversal
Visit the nodes in the left subtree first, then
visit the nodes in the right subtree, then visit
the root of the tree
Postorder(tree)
If tree is not NULL
Postorder(Left(tree))
Postorder(Right(tree))
Visit Info(tree)
Postorder Traversal
void postOrder(Tree t) a
{
if (t != null)
{ b c
b c a
postOrder(t->left);
postOrder(t->rigt); a
visit(t);
}
} b c
f
d e
g h i j
g h d i e b j f c a
Traversing a Tree Postorder
B C
D E F G
H I
Traversing a Tree Postorder
B C
D E F G
H I
Result:
Traversing a Tree Postorder
B C
D E F G
H I
Result:
Traversing a Tree Postorder
B C
D E F G
H I
Result: D
Traversing a Tree Postorder
B C
D E F G
H I
Result: D
Traversing a Tree Postorder
B C
D E F G
H I
Result: DH
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHE
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEB
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEB
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBF
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBF
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBFI
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBFIG
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBFIGC
Traversing a Tree Postorder
B C
D E F G
H I
Result: DHEBFIGCA
Breadth-first traversal of a tree
B C
D E F G
H I
Breadth-first traversal of a tree
B C
D E F G
H I
Result: A
Breadth-first traversal of a tree
B C
D E F G
H I
Result: AB
Breadth-first traversal of a tree
B C
D E F G
H I
Result: ABC
Breadth-first traversal of a tree
B C
D E F G
H I
Result: ABCD
Breadth-first traversal of a tree
B C
D E F G
H I
Result: ABCDE
Breadth-first traversal of a tree
B C
D E F G
H I
Result: ABCDEF
Breadth-first traversal of a tree
B C
D E F G
H I
Result: ABCDEFG
Breadth-first traversal of a tree
B C
D E F G
H I
Result: ABCDEFGH
Breadth-first traversal of a tree
B C
D E F G
H I
Result: ABCDEFGHI
Binary Tree Construction
Suppose that the elements in a binary tree are
distinct
Can you construct the binary tree from which a
given traversal sequence came?
When a traversal sequence has more than one
element, the binary tree is not uniquely defined
Therefore, the tree from which the sequence was
obtained cannot be reconstructed uniquely
Some Examples
preorder a a
= ab b b
inorder b a
= ab a b
postorder b b
= ab a a
level order a a
= ab b b
Binary Tree Construction
Can you construct the binary tree, given two
traversal sequences?
Depends on which two sequences are given
Preorder And Postorder
preorder = ab a a
postorder = ba b b
Preorder and postorder do not uniquely define a
binary tree.
Nor do preorder and level order (same example)
Nor do postorder and level order (same example)
Inorder And Preorder
a
inorder = g d h b e i a f j c
preorder = a b d g h e i c f j gdhbei fjc
Scan the preorder left to right
using the inorder to separate a
left and right subtrees
1. a is the root of the tree; b fjc
gdhbei are in the left subtree; gdh ei
fjc are in the right subtree
a
2. b is the next root;
gdh are in the left subtree; b fjc
ei are in the right subtree
3. d is the next root; d ei
g is in the left subtree;
h is in the right subtree g h
Inorder And Postorder
Scan postorder from right to left using inorder to
separate left and right subtrees
inorder = g d h b e i a f j c
postorder = g h d i e b j f c a
Tree root is a;
gdhbei are in left subtree;
fjc are in right subtree
A few exercises
Construct the binary tree with following
traversals:
InOrder : BFGHPRSTWYZ
PreOrder : PFBHGSRYTWZ
Also what will be the PostOrder traversal of above
tree?
A few exercises
For a complete tree with inOrder traversal
GDEABCF, what is the postOrder traversal?
Assume a tree is containing integers 1,2,3. How
many trees have same inorder and postorder
traversal?
Assume a tree is containing integers 1,2,3…n.
How many trees have same inorder and preorder
traversal?
A few programming exercises
Write down the function to search for a value in a
binary tree.
int search_tree(TREE root, int val)
Write down the function to print all odd values
stored in a binary tree.
Write down the function to find the maximum
value stored in nodes of a binary tree.
Write down the function to check if a tree is
strictly binary tree or not. A tree is strictly binary
if every non-leaf has two children.
check_SBT(TREE root)
Some Interview Questions
Some Interview Questions
Some Interview Questions
1. Write a function to check if two binary trees have the same
elements.[Facebook Interview Question]
2. Write a code that can find the sum of values in the left-subtree
nodes after entering the root node of the tree.[Microsoft
Interview Question]
3. Write a program to count the number of leaf nodes in a binary
tree.[Adobe]
4. Write a program that takes a binary tree and prints out elements
of each level at the same line. [Facebook]
5. Given a binary tree, print the average of each level. [Facebook]
6. Check if any two nodes in a binary tree are cousins. [Amazon]
7. Given the root of a binary tree containing integers, print the
columns of the tree in order with the nodes in each column
printed top-to-bottom. [Facebook]
Sum of Nodes in left side in Binary Tree
Input Binary Tree
Answer : 6 + 9 + 8 + 7 = 30
Level wise Binary Tree Traversal
B C
D E F G
H I
Result: A B C D E F G H I
Check if two nodes are cousins in a Binary Tree
Two nodes are
said to be cousins
of each other if
they are at same
level of the Binary
Tree and have
different parents.
Print the binary tree column wise top to
bottom
Input Tree
Output : 9 5 3 2 6 1 7 4 8 0