Inorder Traversal in Binary Tree in Java

 What is Inorder Traversal?

Inorder traversal is one of the method of traversing  a binary tree. In inorder traversal, the left subtree is visited first, then the root and later the right sub-tree.In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order.  In this article inorder is performed using recursion. Other ways of traversing a tree are -:

inorder tree traversal

Steps to find inorder Traversal in Binary tree

Here are some of the steps for tree traversal :-
  • Step 1: Traverse the left most child which is 4 in above example and print it.
  • Step 2: Then print it’s parent which is 2 and look for the right child.
  • Step3: Then move to right child and print it i.e. print 5
  • Step 4: Print 1 which is the root  and move to it’s right subtree.
  • Step 5: At last print 3 and as all the nodes get visited , so stop. Therefore ,  the printing sequence will be 4 2 5 1 3 .
inorder tree traversal in java

Example

Algorithm to Find Inorder Traversal using recursion

   Algorithm inorder(tree)

  1.   Recursively traverse left subtree.
  2.   Visit root node.
  3.   Recursively traverse right subtree.

Java code for Inorder traversal in binary tree in java

Run
//Inorder Traversal in java

/* Node class with left and right child and  current node and key value*/
class Node
{
    Node left ,right;
    int value;
    Node(int value) 
    {
        this.value=value;
        left=null;
        right=null;
    }
}
class Inorder
{
    Node root;  //root of the binary tree
    Inorder()
    {
        root=null;
    }
    /*inorder traversal of binary tree */
    public void inorder(Node ptr)
    {
        if(ptr==null)
        return ;
        /*first traverse left child */
        inorder(ptr.left);
        /*then print the value of node */
        System.out.print(ptr.value);
        /* now traverse the right child */
        inorder(ptr.right);
    }
}
public class Main{
    public static void main(String[] args)
    {
        Inorder t=new Inorder();
        t.root=new Node(1);
        t.root.left=new Node(2);
        t.root.right=new Node(3);
        t.inorder(t.root);
    }
}

Output :

213

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java