Preorder Of Expression Tree
* +
e f
+ -
a b c d
/ * +a b - c d +e f
Gives prefix form of expression!
Also called as Normal polish Notation
Inorder Of Expression Tree
/
* +
e f
+ -
a b c d
a + b * c - d/ e + f
Gives infix form of expression!
Postorder Of Expression Tree
/
* +
e f
+ -
a b c d
a b +c d - * e f + /
Gives postfix form of expression!
Also called as Reverse Polish Notation
Traversal Applications
a
b c
f
d e
g h i j
• Make a clone.
• Determine height.
•Determine number of nodes.
Level Order
Let ptr be a pointer to the tree root.
while (ptr != NULL)
{
visit node pointed at by ptr and put its children on
a FIFO queue;
if FIFO queue is empty, set ptr = NULL;
otherwise, delete a node from the FIFO queue and
call it ptr;
}
Level-Order Example (Visit = print)
a
b c
f
d e
g h i j
abcdef ghi j
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
• inorder = g d h b e i a f j c
• preorder = a b d g h e i c f j
• Scan the preorder left to right using the
inorder to separate left and right subtrees.
• a is the root of the tree; gdhbei are in the left
subtree; fjc are in the right subtree.
a
gdhbei fjc
Inorder And Preorder
a
gdhbei fjc
• preorder = a b d g h e i c f j
• b is the next root; gdh are in the left
subtree; ei are in the right subtree.
a
b fjc
gdh ei
Inorder And Preorder
a
b fjc
gdh ei
• preorder = a b d g h e i c f j
• d is the next root; g is in the left
subtree; h is in the right subtree.
a
b fjc
d ei
g h
Inorder And Preorder
a
b fjc
gdh ei
• preorder = a b d g h e i c f j
• e is the next root; i is in the right
subtree.
a
b fjc
d e
g h i
Inorder And Preorder
a
b fjc
gdh ei
• preorder = a b d g h e i c f j
• c is the next root; fj is in the left
subtree;
a
b c
d e fj
g h i
Inorder And Preorder
a
b fjc
gdh ei
• preorder = a b d g h e i c f j
• f is the next root; j is in the right
subtree.
a
b c
d e f
g h i j
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.
Inorder And Level Order
• Scan level order from left to right using
inorder to separate left and right subtrees.
• inorder = g d h b e i a f j c
• level order = a b c d e f g h i j
• Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
Operations on Binary Tree
• Count the number of nodes in a binary tree
• Count the number of leaf nodes in a binary
tree
• Find the degree of all nodes in a binary tree
• Find the degree of a binary tree
• Find the height of the binary tree
• Copy the tree
• Compare the tree
Count the number of nodes in a binary tree
int count(struct node *ptr)
{
int lc,rc;
if(ptr == NULL)
return 0;
else
{
lc= count(ptr -> left)
rc= count(ptr -> right)
return(lc+rc+1);
}
}
Count the number of leaf nodes in a binary tree
int leafcount(struct node *ptr)
{
int lc,rc;
if(ptr !== NULL)
if(ptr ->left == NULL && ptr ->right == NULL)
return 1;
else
{
lc= leafcount(ptr -> left)
rc= leafcount(ptr -> right)
return(lc+rc);
}
}
Find the degree of all nodes in a binary tree
int degree_nodes(struct node *ptr)
{ int d;
if(ptr !== NULL)
if(ptr ->left == NULL && ptr ->right == NULL)
d=0;
else {
if( ptr -> left != NULL && ptr -> right != NULL)
d = 2;
else
d = 1;
Printf (“ \n Degree of Node %d = %d “, ptr-> data, d);
degree_nodes(ptr -> left);
degree_nodes(ptr -> right);
}