Tree Traversal
Here, 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:
o Inorder traversal
o Preorder traversal
o Postorder traversal
Let's look at each traversal one by one.
Inorder Traversal
An 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.
17.5M
338
Prime Ministers of India | List of Prime Minister of India (1947-2020)
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 Traversal
A 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 Traversal
A 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.
Data Structure & Algorithms - Tree Traversal
Advertisements
Previous Page
Next Page
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all
nodes are connected via edges (links) we always start from the root (head) node. That is, we
cannot randomly access a node in a tree. There are three ways which we use to traverse a tree
−
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the
values it contains.
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree.
We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending
order.
We start from A, and following in-order traversal, we move to its left subtree B. B is also
traversed in-order. The process goes on until all the nodes are visited. The output of inorder
traversal of this tree will be −
D→B→E→A→F→C→G
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree.
We start from A, and following pre-order traversal, we first visit A itself and then move to its left
subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited.
The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left
subtree, then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. The output of post-
order traversal of this tree will be −
D→E→B→F→G→C→A
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
To check the C implementation of tree traversing, please click here.