01-04-2012, 10:23 PM
When we call pre-order function first the root node is printed and after that left node is printed and then right node is printed. In post-order traversal first left node is printed then right node is printed and then root node is printed.
10
/ \
/ \
5 12
/ \
/ \
3 6
When we call pre-order function on this tree. First pre-order call on node 10. Node 10 is printed. Then we pre-order call on 5 as 5 has both left and right child so 5 is printed. Then pre-order call on node 3. 3 is printed. It check whether 3 has any left child. As 3 has not any left child so it check whether 3 has right child which is not so pre-order function call on 6 . 6 is printed. We check its left child which is null and then its right child which is also null after that pre-order call on node 12 node 12 is print after that we check its left child as left child is null we check right child which is also null.
When we apply Post-order function on this tree. First node 10 is traverse, function check its left node which is 5. 5 also have left node which is 3 as 3 have not any left child neither right child so 3 is printed. Then go to the 5. As 5 have right node so 6 is printed. Then 5 is printed which is the root of both 3 and 6. Then we traverse 10. As 10 has right child so which is 12. we traverse 12 . As 12 have not any left child and right child so 12 is printed and then we traverse 10. In the end 10 is printed.
To see animation of tree traversal, see this link.
Binary Tree Traversals
10
/ \
/ \
5 12
/ \
/ \
3 6
When we call pre-order function on this tree. First pre-order call on node 10. Node 10 is printed. Then we pre-order call on 5 as 5 has both left and right child so 5 is printed. Then pre-order call on node 3. 3 is printed. It check whether 3 has any left child. As 3 has not any left child so it check whether 3 has right child which is not so pre-order function call on 6 . 6 is printed. We check its left child which is null and then its right child which is also null after that pre-order call on node 12 node 12 is print after that we check its left child as left child is null we check right child which is also null.
When we apply Post-order function on this tree. First node 10 is traverse, function check its left node which is 5. 5 also have left node which is 3 as 3 have not any left child neither right child so 3 is printed. Then go to the 5. As 5 have right node so 6 is printed. Then 5 is printed which is the root of both 3 and 6. Then we traverse 10. As 10 has right child so which is 12. we traverse 12 . As 12 have not any left child and right child so 12 is printed and then we traverse 10. In the end 10 is printed.
To see animation of tree traversal, see this link.
Binary Tree Traversals
0300-9520262
Get solution in 24 hour |cpmputer & internet
mileys81:[SIZE="4"][SIZE="3"]....[/SIZE][/SIZE]
