Tree TraversalHere, tree traversal means traversing or visiting each node of a tree. Linear data structures like Stack, Queue, linked list have only one way for traversing, whereas the tree has various ways to traverse or visit each node. The following are the three different ways of traversal:
Let's look at each traversal one by one. Inorder TraversalAn inorder traversal is a traversal technique that follows the policy, i.e., Left Root Right. Here, Left Root Right means that the left subtree of the root node is traversed first, then the root node, and then the right subtree of the root node is traversed. Here, inorder name itself suggests that the root node comes in between the left and the right subtrees. Let's understand the inorder traversal through an example. Consider the below tree for the inorder traversal. First, we will visit the left part, then root, and then the right part of performing the inorder traversal. In the above tree, A is a root node, so we move to the left of the A, i.e., B. As node B does not have any left child so B will be printed as shown below: After visiting node B, we move to the right child of node B, i.e., D. Since node D is a leaf node, so node D gets printed as shown below: The left part of node A is traversed. Now, we will visit the root node, i.e., A, and it gets printed as shown below: Once the traversing of left part and root node is completed, we move to the right part of the root node. We move to the right child of node A, i.e., C. The node C has also left child, i.e., E and E has also left child, i.e., G. Since G is a leaf node, so G gets printed as shown below: The root node of G is E, so it gets printed as shown below: Since E does not have any right child, so we move to the root of the E node, i.e., C. C gets printed as shown below: Once the left part of node C and the root node, i.e., C, are traversed, we move to the right part of Node C. We move to the node F and node F has a left child, i.e., H. Since H is a leaf node, so it gets printed as shown below: Now we move to the root node of H, i.e., F and it gets printed as shown below: After visiting the F node, we move to the right child of node F, i.e., I, and it gets printed as shown below: Therefore, the inorder traversal of the above tree is B, D, A, G, E, C, H, F, I. Preorder TraversalA preorder traversal is a traversal technique that follows the policy, i.e., Root Left Right. Here, Root Left Right means root node of the tree is traversed first, then the left subtree and finally the right subtree is traversed. Here, the Preorder name itself suggests that the root node would be traversed first. Let's understand the Preorder traversal through an example. Consider the below tree for the Preorder traversal. To perform the preorder traversal, we first visit the root node, then the left part, and then we traverse the right part of the root node. As node A is the root node in the above tree, so it gets printed as shown below: Once the root node is traversed, we move to the left subtree. In the left subtree, B is the root node for its right child, i.e., D. Therefore, B gets printed as shown below: Since node B does not have a left child, and it has only a right child; therefore, D gets printed as shown below: Once the left part of the root node A is traversed, we move to the right part of node A. The right child of node A is C. Since C is a root node for all the other nodes; therefore, C gets printed as shown below: Now we move to the left child, i.e., E of node C. Since node E is a root node for node G; therefore, E gets printed as shown below: The node E has a left child, i.e., G, and it gets printed as shown below: Since the left part of the node C is completed, so we move to the right part of the node C. The right child of node C is node F. The node F is a root node for the nodes H and I; therefore, the node F gets printed as shown below: Once the node F is visited, we will traverse the left child, i.e., H of node F as shown below: Now we will traverse the right child, i.e., I of node F, as shown below: Therefore, the preorder traversal of the above tree is A, B, D, C, E, G, F, H, I. Postorder TraversalA Postorder traversal is a traversal technique that follows the policy, i.e., Left Right Root. Here, Left Right Root means the left subtree of the root node is traversed first, then the right subtree, and finally, the root node is traversed. Here, the Postorder name itself suggests that the root node of the tree would be traversed at the last. Let's understand the Postorder traversal through an example. Consider the below tree for the Postorder traversal. To perform the postorder traversal, we first visit the left part, then the right part, and then we traverse the root node. In the above tree, we move to the left child, i.e., B of node A. Since B is a root node for the node D; therefore, the right child, i.e., D of node B, would be traversed first and then B as shown below: Once the traversing of the left subtree of node A is completed, then the right part of node A would be traversed. We move to the right child of node A, i.e., C. Since node C is a root node for the other nodes, so we move to the left child of node C, i.e., node E. The node E is a root node, and node G is a left child of node E; therefore, the node G is printed first and then E as shown below: Once the traversal of the left part of the node C is traversed, then we move to the right part of the node C. The right child of node C is node F. Since F is also a root node for the nodes H and I; therefore, the left child 'H' is traversed first and then the right child 'I' of node F as shown below: After traversing H and I, node F is traversed as shown below: Once the left part and the right part of node C are traversed, then the node C is traversed as shown below: In the above tree, the left subtree and the right subtree of root node A have been traversed, the node A would be traversed. Therefore, the Postorder traversal of the above tree is D, B, G, E, H, I, F, C, A.
Next TopicImplementation of Queue using Stacks
