Explain about Tree Traversal in Data Structure

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:

  • Inorder traversal
  • Preorder traversal
  • Postorder traversal

Inorder Traversal

An inorder traversal is a traversal technique that follows the policy, Left Root Right.

Let’s understand the inorder traversal through an example.

Consider the below tree for the inorder traversal.

Tree 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.

Tree Traversal

After visiting node B, we move to the right child of node B, i.e., D.

Tree Traversal
Tree Traversal

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.

Tree Traversal

The root node of G is E.

Tree Traversal

Since E does not have any right child, so we move to the root of the E node, i.e., C.

Tree Traversal

We move to the node F and node F has a left child, i.e., H.

Tree Traversal

Now we move to the root node of H, i.e., F

Tree Traversal

After visiting the F node, we move to the right child of node F, i.e., I,

Tree Traversal

Therefore, the in order traversal of the above tree is B, D, A, G, E, C, H, F, I.

Preorder Traversal

A preorder traversal is a traversal technique that follows the policy, i.e., Root Left Right. 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.

Tree Traversal

To perform the preorder traversal

Tree Traversal

In the left subtree, B is the root node for its right child, i.e., D.

Tree Traversal

Since node B does not have a left child, and it has only a right child;

Tree Traversal

The right child of node A is C. Since C is a root node for all the other nodes;

Tree Traversal

Now we move to the left child, i.e., E of node C. Since node E is a root node for node G;

Tree Traversal

The node E has a left child, i.e., G,

Tree Traversal

The right child of node C is node F. The node F is a root node for the nodes H and I;

Tree Traversal

we will traverse the left child, i.e., H of node F

Tree Traversal

Now we will traverse the right child, i.e., I of node F, as shown below:

Tree Traversal

Therefore, the preorder traversal of the above tree is A, B, D, C, E, G, F, H, I.

Postorder Traversal

A Postorder traversal is a traversal technique that follows the policy, i.e., Left Right Root. 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.

Tree Traversal

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;

Tree Traversal

We move to the right child of node A, i.e., C. The node E is a root node, and node G is a left child of node E;

Tree Traversal

The right child of node C is node F. Since F is also a root node for the nodes H and I;

Tree Traversal

After traversing H and I, node F

Tree Traversal

Once the right part of node C

Tree Traversal
Tree Traversal

Therefore, the Postorder traversal of the above tree is D, B, G, E, H, I, F, C, A.