Inorder traversal is a type of depth-first traversal used in binary trees which starts from root node and process all the nodes of the tree. In inorder traversal, the nodes are recursively visited and processed in the following order:
- Traverse the left subtree in inorder- First go to the left child node if it is present, apply the inorder traversal on the left child node.
- Process data at current node- Once inorder has been applied on left child node, process the data stored in the current node( In starting it is root node).
- Traverse the right subtree in inorder. - Once left subtree has been processed and data at the current node has been processed, go to the right child node and apply inorder traversal on the right child node.
5. Node 4 has left subtree so according to LRtR formula processing of left subtree of node 4 will start as given below:
6. Since node 6 has no left child so data at node 6 will be processed. Since node 6 is leaf node(no right child node) so first time the whole formula LRtR is processed at node 6. So node 6 is fully processed here and processing backtracks to its immediate partial processed node 4.
7. According to LRtR formula L part has already been processed at node 4 so next Rt part (means data present at node 4) is processed as given below:
8. R part of formula LRtR at node 4 is remaining, so processing of the right subtree of node 4 starts.
9. Since node 7 is leaf node the left subtree of node 7 is implicitly processed as no left child is present. Then the next part, data at node 7 is processed. Finally there is no right child here so the right subtree of node 7 is also implicitly processed and the process backtracks to its immediate partial processed parent node 4.
10. At node 4 whole part of LRtR is processed so node 4 is fully processed and process backtracks to node 2 where node 2 is also fully processed and the process again backtracks to node 1. At node 1 L part of LRtR is processed so next Rt(data at current node) part is processed.
14. Since node 8 is leaf node so all part of formula LRtR is applied here and this node is fully processed and control backtracks to node 5. Then node 5 is also fully processed and control backtracks to node 3.
15. At node 3 left subtree is completely processed. Next data at node 3 is processed and since no right child present here its right subtree is implicitly processed and control backtracks to node 1 where the right subtree of node 1 is also processed. Thus full processing of root node of given tree happens leaving complete inorder traversal of given tree as given below:
Method 1: Using STACK
inorderByStack(root)
- Let ST is a stack
- let current=root
- While ST is not empty or current is not not null:
- {
-
while current is not null
-
{
-
ST.push(current)
-
current=current.left
-
}
-
current=ST.pop()
-
Process current.data(print current.data)
-
current=current.right
- }
- In the starting at line no 3 current variable is not null but stack is empty. Since the current is not null it will go to while loop at line no 5 where left node is added to the stack recursively according to LRtR formula where L part is processed first.
- If any node is found where no left child is present, control goes out of while loop at line 5. Here next part of LRtR is processed by popping top item from the stack which is last node in the chain having no left child.
- After processing data at current node according to LRtR formula, right subtree must be processed so current is assigned to right child of current node
- If any node don't have right subtree then current will be null. In that case if stack has entry to process backtrack will happen.
Method 2: Using Recursion
From the above figure we can easily see inorder traversal can be implemented recursively. As we know from Recursion chapter, while designing recursive solution there are 3 steps to follow which are given below:
Hypothesis - In designing recursive solution we should define a function and its necessary parameter which will solve the given problem. In our case while applying inorder traversal to any node we need the left child, node data and the right child node which will be accessed from the given node itself. So only the current node is sufficient input parameter where processing will start from the root node. We are defining below function which will solve our problem.
Inorder(root)
Inductive Step - Here we need to define the problem in terms of the subproblems of itself. Here we know from the above mentioned formula inorder works on formula LRtR(Apply inorder on the left subtree, process the data on current node, apply inorder on the right subtree). So we can define inorder traversal as below in the inductive step.
Inorder(root)=Inorder(root.left)=>(process data at root)==>Inorder(root.right)
or
Inorder(root)=
Inorder(root.left)
(Process data at root)
Inorder(root.right)
Base Condition - Function body defined in the Inductive step will not work as after leaf node left or right child will be null so there must be some condition to handle this boundary which is called Base Condition. Here if current node is null means no subtree to process. So there must not be any processing when current node is null as given below:
Inorder(root)=
if root is null do nothing and return
Inorder(root.left)
(Process data at root)
Inorder(root.right)
So below will be inorder tree traversal algorithm for a given binary tree using recursion
{
- If the root node is null then do nothing and return
- Inorder(root.left)
- Process root.data(print root.data)
- Inorder(root.right)