Given the binary tree below indicate the results of outputti
Given the binary tree below indicate the results of outputting all values stored in the tree during the three traversal of the tree:
a) Using a preorder traversal
b) Using an inorder traversal
c) Using a postorder traversal
Solution
a) Using a preorder traversal :
Preorder traversal starts by visiting the current node ( A here) —then its left child ( B here) and then its right child ( C here). Starting with the BST\'s root as A, this algorithm can be written out as:
Based on above algorithm, all the outputs of above tree can be written as :
A,B,D,H,I,M,E,J,C,F,K,N,G,L,O,P
B) Inorder traversal :
Inorder traversal starts by visiting the current node\'s left child, ( H here) then the current node, and then its right child. Starting with the BST\'s root as A, this algorithm can be written out as:
Based on above algorithm, all outputs of the above tree can be written as in this trasversal as :
H,D,I,M,D,B,J,E,A,N,K,F,C,O,G,L,P
C) Using a postorder traversal :
postorder traversal starts by visiting the current node\'s left child, then its right child, and finally the current node itself. Starting with the BST\'s root as c, this algorithm can be written out as:
So output for the above tree using this traversal is given as :
H,M.I.D.J,E,B,N,K,F,O,P,L,G,C,A

