Traversals in Binary tree
- The traversal operation is frquently used operation on a binary tree.
- This operation is used to visit each node in the tree exactly once.
- There are 6(six) possible ways a tree can be traversed and they are as follows:
- R TL TY
- TL R TR
- TL TR R
- TR TL R
- TR R TL
- R TR TL
where, TL is left sub-treesof the node R.
TR is right sub-trees of the node R.
- Out of these six possible traversals, only three are fundamental , they are categorized as given below:
- R TL TR (preorder)
- TL R TR (inorder)
- TL TR R (post order)
1) The given binary tree is
Find out the inorder, preorder and postorder traversals.
Inorder:Left root right
Step:1 LC A RC
Step:2 _B_A RC
- if(ptr≠NULL) then
- end if
Preoder:Root LC RC
Step:1 A LC RC
Step:2 A B_ C _
Step:3 ABDEH CFIJG
- if(ptr≠NULL) then
- end if
Postorder : LC RC Root
Step:1 LC RC A
Step:2 DHEB IJEGC A
- if (ptr≠NULL) then
- end if
2) To construct inorder, preoder and post order traversals for below binary tree:
Inorder: left root right
Step:1 LC n6 RC
Step:2 n1 n2 n3 n4 n5 n6 n7 n8 n9
Step:3 n1 n2 n3 n4 n5 n6 n7 n8 n9
Preoder: Root LC RC
Step:1 n6 LC RC
Step:2 n6 n2 n1 n4 n3 n5 n9 n7 n8
Step:3 n6 n2 n1 n4 n3 n5 n9 n7 n8
Postorder:LC RC root
Step1: LC RC n6
Step:2 n1 n3 n5 n4 n2 n8 n7 n9 n6
Step:3 n1 n3 n5 n4 n2 n8 n7 n9 n6
Formation of Binary tree from its traversals
- From a single traversal, it is not possible to construct a unqiue binary tree.
- But, if two traversals are known then, we can construct a unique binary tree.
- Out of two traversals, one should be inorder traversal and another preorder or postorder.
- If the two traversals are preorder and postorder then, a binary tree cannot be constructed uniquely.
Construct the binary tree from the inorder and preorder traversals of a binary tree as given below:
Inorder:D B H E A I F J C G
Preoder:A B D E H C F I J G
The following steps need to be followed:
- From the preoder traversal, it is evident that A is the root node.
- In inorder traversal, all the nodes which are on the left side of A belong to the left sub tree and those which are on right side of A belong to the right subtree.
- Now the problem reduces to form subtreees and the same procedure can be applied repeatedly.
(i) In left subtree , B will be the root(B D E H)
(ii) In right subtree, C will be the root(C F I J G)
(i) In right subtree of B, E is the root from the preoder E H.
From inorder, H will be left subtree of E.
(ii) From left subtree of C, F is the root from the preorder F I J
From inorder, I is left of F and J is right of F. So,
So, the final binary tree from inorder and preorder is
Construct a binary tree from the inorder and postorder traversals given below:
Inorder: n1 n2 n3 n4 n5 n6 n7 n8 n9
Postorder: n1 n3 n5 n4 n2 n8 n7 n9 n6
n6 is the root from postorder.
Left subtree: n1 n2 n3 n4 n5
Right subtree: n7 n8 n9
From postorder : n2 is the root of left subtree.
From postorder: n9 is the root for right subtree.
n4 is the root of n2 right subtree.
n7 is the root of n9 left subtree.
Merging two binary trees together
- Merge operation is applicable to trees which are reprsented by using a linked structure .
- There are two ways that this operation can be carried out.
Suppose T1 and T2 are two binary trees. T2 can be merged with T1 if all the nodes from T2, one by one , are inserted into the binary tree T1
When the entire tree T2 can be include as a subtree of T1 To achieve this, we need that in either tree . There must be atleast one null subtree.
(i) Before performing merging, we have to test for compatibility. If in both trees the root node has both the left and right subtrees, then the merge will be fail.
(ii) If T1 has left subtree(right subtree) empty. Then T2 will be added to the left subtree(the right subtree) of the T1 and vice-versa.
(iii) If T1 is a tree with n1 nodes and T2 is a tree with n2 nodes. Then the resultant tree T after merging is
T(n1+n2) =T1(n1) +T2(n2)
If the binary tree contains an arithmetic expression,
- Inorder will give infix expression
- Preorder will give prefix expression
- Postorder will give postfix expression.