Postorder Tree Traversal without recursion in Java

Postorder Traversal without recursion

In postorder traversal , first we traverse the left subtree, then the right subtree and finally the root node. Postorder traversal is also used to get the postfix expression of an expression given.In this post , iterative postorder traversal is discussed using two stacks.

postorder traversal in java

Steps to find preorder traversal using stack:

  • Step 1: Create two stack as stack1,stack2.
  • Step 2:  push root which is  5 to stack1. i.e. stack1->5  , stack2->Empty.
  • Step 3:  pop 5 from stack1 and push it into stack2.Also push right and left child of 5 to stack1. i.e. stack1->7,3  stack2->5.
  • Step 4: pop 7 from the stack1 and push it into stack2. Also push left and right child of 7 to stack1. i.e. stack1->8,6,3 and stack2->7,5.
  • Step 5:  pop 8 from stack1 and push it into stack2. i.e. stack1-> 6,3 and stack2->8,7,5.
  • step 6: pop 6 from stack1 and push it into stack2. i.e. stack1->3 and stack2->6,8,7,5.
  • step 7: pop 3 from stack1 and push it into stack2 and push left and right child of 3 into stack1. i.e. stack1->4,1 and stack2->3,6,8,7,5.
  • step 8: pop 4 from  stack1 and push it into stack2.i.e. stack1->1 and stack2->4,3,6,8,7,5.
  • step 9: pop 1 from stack1 and push it into stack2. i.e. stack1->Empty and stack2->1,4,3,6,8,7,5.
  • step 10: As stack1 is empty so stop and pop all the element from stack2 one by one an print it.
Therefore the sequence will be printed as 1,4,3,6,8,7,5.
Post order traversal using stacks

Algorithm to find Preorder traversal of binary tree using stack

  1. if root is null, simply return.
  2. else , create stack1 and stack2 and push the root element to stack1.
  3. pop an element from stack1 and push it into stack2 .
  4. push left and right child of popped element to stack1.
  5. Continue step 3 and 4 until stack1 becomes empty.
  6. Pop All the elements from stack2 one by one and print them.
Run
//Postorder Traversal without recursion

/*Node class containing left and right child of current node 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 Postorder{
    Node root; //root of the tree
    public Postorder(){
    root=null;
    }
    /* function for Postorder traversal of biary tree*/
    public void postorder()
    {
        if(root ==null)
        return ;
        Stack stack1=new Stack(); //createing stack1 Object
        Stack stack2=new Stack(); //creating stack2 Objec
        stack1.push(root); // inserting root element to stack1
        while(!stack1.isEmpty()){
            Node ptr=(Node)stack1.pop(); //removing element from stack1
            stack2.push(ptr); //inserting ptr to stack2
            /*inserting left and right child of ptr */  
            if(ptr.left!=null)
            stack1.push(ptr.left);
            if(ptr.right!=null)
            stack1.push(ptr.right);
         }
         while(!stack2.isEmpty()){
             Node temp=(Node)stack2.pop(); 
            System.out.print(temp.value+" ");
            
         }
    }
}
public class Main{
    public static void main(String[] args)
    {
        Postorder t=new Postorder();
        t.root=new Node(5);
        t.root.left=new Node(3);
        t.root.right=new Node(7);
        t.root.left.left=new Node(1);
        t.root.left.right=new Node(4);
        t.root.right.left=new Node(6);
        t.root.right.right=new Node(8);
        t.postorder();
    }
}

Output :

1 4 3 6 8 7 5 

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