Inorder Traversal in Binary Tree without recursion in Java

Inorder Tree Traversal Without recursion 

In inorder tree traversal , left subtree is visited first , then the root and at last right subtree.Inorder Traversal of a binary search tree gives the sequence in non decreasing order In this article inorder traversal is performed using stack. And it is the obvious way to traverse tree without recursion.

inorder tree traversal using stack

Steps to find inorder traversal without recursion:

  • Step 1: Create an temporary variable and an empty stack of Node type with initial value null. Push the element value to stack and set  temp= temp.left until temp is null .Now stack become , Stack -> 2,3,5.
  • Step 2: pop the element from the stack and assign it to temp variable and print it i.e print 2.Now stack become Stack->3,5. pop again and print it i.e. 3.Now stack become , Stack->5.
  • step 3: push 4 to stack and make temp null. Stack-> 4,5.
  • Step 4: pop 4 from the stack and print it .Now Stack->5. pop 5 from the stack and print it, so stack become Stack->null.
  • Step 5: push 7 to stack and make temp null. So Stack become Stack->7.
  • Step 6: pop 7 and print it . Now traversal is complete as stack is empty and temp is null.
Therefore the sequence will be printed as 2,3,4,5,7.
 
inorder traversal using stack

Algorithm to find Inorder traversal of binary tree using stack

  1. If root is null , simply return .
  2. else , Create an empty stack  and initalize temp with root.
  3. Push the temp to stack and set  temp=temp.left until temp is null.
  4. Pop the top element from the stack and print it . Set temp=temp.right and goto step 3.
  5. Until current become null and stack become empty.Then, we are done with our traversal.
Run
//Inorder Traversal without Recursion


/*Node class containing left and right child of current nod e and key value.*/

import java.util.*;

class Node{

    int value;
    Node left,right;
    public Node(int value)
    {
        this.value=value;
        left=null;
        right=null;
    }
}

class Inorder{

     Node root; //root of the tree
     public Inorder(){
        root=null;
    }

/*function for Inorder traversal of the tree*/
    public void inorder()
    {
        if(root ==null)
        return ;
        Node temp=null;
        Stack stack=new Stack(); //Creating an empty Stack 
        for(temp=root; stack.size()>0 || temp!=null ;temp=temp.right)
        {
        /*loop until we are on the left most node of the current node */
         while(temp!=null)
         {
                 stack.push(temp);  //inserting an element into stack
                 temp=temp.left;
        }
        /* removing the top element from the stack */
        temp=(Node)stack.pop();
        System.out.print(temp.value+" ");
       }
    }
}
public class Main{
    public static void main(String[] args)
    {
            Inorder t=new Inorder();
            t.root=new Node(5);
            t.root.left=new Node(3);
            t.root.right=new Node(7);
            t.root.left.left=new Node(2);
            t.root.left.right=new Node(4);
            t.inorder();
     }

 }

Output :

2 3 4 5 7 

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

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java