Inorder Traversal of Binary Tree
Last Updated :
28 Mar, 2023
Inorder traversal is defined as a type of tree traversal technique which follows the Left-Root-Right pattern, such that:
- The left subtree is traversed first
- Then the root node for that subtree is traversed
- Finally, the right subtree is traversed
Inorder traversal
Algorithm for Inorder Traversal of Binary Tree
The algorithm for inorder traversal is shown as follows:
Inorder(root):
- Follow step 2 to 4 until root != NULL
- Inorder (root -> left)
- Write root -> data
- Inorder (root -> right)
- End loop
How does Inorder Traversal of Binary Tree work?
Consider the following tree:
Example of Binary Tree
If we perform an inorder traversal in this binary tree, then the traversal will be as follows:
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its left subtree root, i.e., 4. Now 4 has no left subtree, so it will be visited. It also does not have any right subtree. So no more traversal from 4
Node 4 is visited
Step 2: As the left subtree of 2 is visited completely, now it read data of node 2 before moving to its right subtree.
Node 2 is visited
Step 3: Now the right subtree of 2 will be traversed i.e., move to node 5. For node 5 there is no left subtree, so it gets visited and after that, the traversal comes back because there is no right subtree of node 5.
Node 5 is visited
Step 4: As the left subtree of node 1 is, the root itself, i.e., node 1 will be visited.
Node 1 is visited
Step 5: Left subtree of node 1 and the node itself is visited. So now the right subtree of 1 will be traversed i.e., move to node 3. As node 3 has no left subtree so it gets visited.
Node 3 is visited
Step 6: The left subtree of node 3 and the node itself is visited. So traverse to the right subtree and visit node 6. Now the traversal ends as all the nodes are traversed.
The complete tree is traversed
So the order of traversal of nodes is 4 -> 2 -> 5 -> 1 -> 3 -> 6.
Program to implement Inorder Traversal of Binary Tree:
Below is the code implementation of the inorder traversal:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node( int v)
{
data = v;
left = right = NULL;
}
};
void printInorder( struct Node* node)
{
if (node == NULL)
return ;
printInorder(node->left);
cout << node->data << " " ;
printInorder(node->right);
}
int main()
{
struct Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->right = new Node(6);
cout << "Inorder traversal of binary tree is: \n" ;
printInorder(root);
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node left, right;
Node( int v)
{
data = v;
left = right = null ;
}
}
class GFG {
public static void printInorder(Node node)
{
if (node == null )
return ;
printInorder(node.left);
System.out.print(node.data + " " );
printInorder(node.right);
}
public static void main(String[] args)
{
Node root = new Node( 1 );
root.left = new Node( 2 );
root.right = new Node( 3 );
root.left.left = new Node( 4 );
root.left.right = new Node( 5 );
root.right.right = new Node( 6 );
System.out.println(
"Inorder traversal of binary tree is: " );
printInorder(root);
}
}
|
Python3
class Node:
def __init__( self , v):
self .data = v
self .left = None
self .right = None
def printInorder(node):
if node is None :
return
printInorder(node.left)
print (node.data, end = ' ' )
printInorder(node.right)
if __name__ = = '__main__' :
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.left = Node( 4 )
root.left.right = Node( 5 )
root.right.right = Node( 6 )
print ( "Inorder traversal of binary tree is:" )
printInorder(root)
|
C#
using System;
public class Node {
public int data;
public Node left, right;
public Node( int v)
{
data = v;
left = right = null ;
}
}
public class BinaryTree {
public static void printInorder(Node node)
{
if (node == null )
return ;
printInorder(node.left);
Console.Write(node.data + " " );
printInorder(node.right);
}
public static void Main()
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
Console.WriteLine(
"Inorder traversal of binary tree is: " );
printInorder(root);
}
}
|
Javascript
class Node {
constructor(v) {
this .data = v;
this .left = null ;
this .right = null ;
}
}
function printInorder(node) {
if (node === null ) {
return ;
}
printInorder(node.left);
console.log(node.data);
printInorder(node.right);
}
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
console.log( "Inorder traversal of binary tree is: " );
printInorder(root);
|
Output
Inorder traversal of binary tree is:
4 2 5 1 3 6
Explanation:
How inorder traversal works
Complexity Analysis:
Time Complexity: O(N) where N is the total number of nodes. Because it traverses all the nodes at least once.
Auxiliary Space: O(1) if no recursion stack space is considered. Otherwise, O(h) where h is the height of the tree
- In the worst case, h can be the same as N (when the tree is a skewed tree)
- In the best case, h can be the same as logN (when the tree is a complete tree)
Use cases of Inorder Traversal:
In the case of BST (Binary Search Tree), if any time there is a need to get the nodes in non-decreasing order, the best way is to implement an inorder traversal.
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...