Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

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 Tree Traversal Without recursion

Example - Postorder Tree Traversal Using Two Stack

Example for Postorder tree travesal without recursion

Steps for postorder tree traversal using iteration

  • 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.

Algorithm

  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.

CODE FOR POSTORDER TRAVERSAL WITHOUT RECURSION

//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=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()){
           System.out.print(stack2.pop().value+" ");
        }
   }
   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

TIME AND SPACE COMPLEXITY OF THE ABOVE CODE

Time Complexity

O(n)

Space Complexity

O(n)