Inorder Postorder Preorder Tree Traversals in Binary Tree

Introduction about Inorder Postorder Preorder

There are three most popular ways in which we can traverse a binary tree which are –

  • Inorder
  • Postorder
  • PreOrder

Here we are explaining all these three traversal procedures in three different programming languages i.e; C, C++ and Java

TCS NQT Registration Steps Large

How each of the Postorder, Inorder, Preorder work?

Inorder

  • Traverse the left sub-tree, (recursively call inorder(root -> left).
  • Visit and print the root node.
  • Traverse the right sub-tree, (recursively call inorder(root -> right).

Tips to remember –

  • Direction : Clockwise direction
  • Rule : LCR i.e. Left Center(root) Right

Preorder

  • Visit and print the root node.
  • Traverse the left sub-tree, (recursively call Preorder(root -> left).
  • Traverse the right sub-tree, (recursively call Preorder(root -> right)

Tips to remember –

  • Direction : Anti-Clockwise direction
  • Rule : CLR i.e. Center(root) left Right

Postorder

  • Traverse the left sub-tree, (recursively call Postorder(root -> left).
  • Traverse the right sub-tree, (recursively call Postorder(root -> right).
  • Visit and print the root node

Tips to remember –

  • Direction : Anti-Clockwise direction
  • Rule : LRC i.e.  Left Right Center(root)

Following Traversals

  • Postorder
  • Inorder
  • Preorder
PreOrder Tree Traversal in Binary Tree in C – 1.3
Postorder Preorder Inorder Traversals of Binary Tree 1
PreOrder Tree Traversal in Binary Tree in C – 1.2

Code for Inorder PostOrder PreOrder in Binary Tree 

// Program for tree traversal inorder, postorder, preorder in Binary Tree/Binary Search Tree
#include
#include

// We are creating struct for the binary tree below
struct node
{
int data;
struct node *left, *right;
};

// newNode function for initialisation of the newly created node
struct node* newNode(int item)
{
struct node* temporary = (struct node *)malloc(sizeof(struct node));
temporary->data = item;
temporary->left = temporary->right = NULL;
return temporary;
}

// Here we print the postorder recursively
void postorder(struct node* root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
// Here we print the preorder recursively
void preorder(struct node* root)
{
if (root != NULL)
{
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
// Here we print the inorder recursively
void inorder(struct node* root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

// Basic Program to insert new node at the correct position in BST
struct node* insert(struct node* node, int data)
{
/* When there no node in the tree(subtree) then create and return new node using newNode function*/
if (node == NULL) return newNode(data);

/* If not then we recur down the tree to find correct position for insertion */
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);

return node;
}

int main()
{
/* What our binary search tree looks like really
9
/ \
7 14
/ \ / \
5 8 11 16 */
struct node* root = NULL;
root = insert(root, 9);
insert(root, 7);
insert(root, 5);
insert(root, 8);
insert(root, 14);
insert(root, 11);
insert(root, 16);

int choice;
printf("1. Postorder 2. PreOrder 3. Inorder\nEnter your choice:\n");
scanf("%d",&choice);

switch(choice){
case 1:
printf("The postorder is :\n");
postorder(root);
break;

case 2:
printf("The preorder is :\n");
preorder(root);
break;

case 3:
printf("The inorder is :\n");
inorder(root);
break;


default:
printf("Enter correct choice \n");
}

return 0;
}
// Program for tree traversal inorder, postorder, preorder in Binary Tree/Binary Search Tree
#include <iostream>
#include <stdlib.h>
using namespace std;

//Creating nodes for the binary tree below
struct node 
{ 
    int data; 
    struct node *left, *right; 
}; 
   
struct node* newNode(int item) //This function will initialize newly crated node.
{ 
    struct node* temporary =  (struct node *)malloc(sizeof(struct node)); 
    temporary->data = item; 
    temporary->left = temporary->right = NULL; 
    return temporary; 
} 
   

void postorder(struct node* root) // This function will print postorder recursively
{ 
    if (root != NULL) 
    { 
        postorder(root->left); 
        postorder(root->right);
        cout<< root->data<<" ";  
    } 
}

void inorder(struct node* root) // This function will print inorder recursively
{ 
    if (root != NULL) 
    { 
        inorder(root->left); 
        cout<< root->data<<" "; 
        inorder(root->right); 
    } 
} 

void preorder(struct node* root) // This function will print preorder recursively
{ 
    if (root != NULL) 
    { 
        cout<< root->data<<" "; 
        preorder(root->left); 
        preorder(root->right);
    } 
} 

   
// Inserting new node at the correct position in Binary Search Tree
struct node* insert(struct node* node, int data) 
{ 
    /* When there no node in the tree(subtree) then create and return new node using newNode function*/
    if (node == NULL) return newNode(data); 
  
    /* If not then we recur down the tree to find correct position for insertion */
    if (data < node->data) 
        node->left  = insert(node->left, data); 
    else if (data > node->data) 
        node->right = insert(node->right, data);    

    return node; 
} 
   
int main() 
{ 
   
    struct node* root = NULL; 
    root = insert(root, 11); 
    insert(root, 2); 
    insert(root, 1); 
    insert(root, 4); 
    insert(root, 7); 
    insert(root, 5); 
    insert(root, 10);
    
    int choice;
    cout<<"1. Postorder\n2. PreOrder\n3. Inorder\nEnter your choice:\n";
    cin>>choice;
    
    if(choice==1)
    {
        cout<<"The postorder is :\n";
        postorder(root); 
    }
        
    else if(choice==2) 
    {    
        cout<<"The preorder is :\n";
        preorder(root);
    }    
        
    else if(choice==3)
    {
        cout<<"The inorder is :\n";
        inorder(root); 
    }    
    else
    {
        cout<<"Wrong choice. Enter correct one \n";
    }
   
    return 0; 
}
import java.util.Scanner;
class Node
{
  int data;
  Node left, right;
  public Node (int item)
  {
    data = item;
    left = right = null;
  }
}

class BinaryTree
{
  Node root;
    BinaryTree ()
  {
    root = null;
  }
  void postOrder (Node node)
  {
    if (node == null)
      {
	return;
      }
    postOrder (node.left);
    postOrder (node.right);
    System.out.println (node.data + " ");
  }
  void inOrder (Node node)
  {
    if (node == null)
      {
	return;
      }
    inOrder (node.left);
    System.out.println (node.data + " ");
    inOrder (node.right);
  }
  void preOrder (Node node)
  {
    if (node == null)
      {
	return;
      }
    System.out.println (node.data + " ");
    preOrder (node.left);
    preOrder (node.right);

  }
  void postOrder ()
  {
    postOrder (root);
  }
  void inOrder ()
  {
    inOrder (root);
  }
  void preOrder ()
  {
    preOrder (root);
  }
}
public class Main
{
public static void main (String[]args)
  {
    BinaryTree tree = new BinaryTree ();
      tree.root = new Node (9);
      tree.root.left = new Node (7);
      tree.root.right = new Node (14);
      tree.root.left.left = new Node (5);
      tree.root.left.right = new Node (8);
      tree.root.right.left = new Node (11);
      tree.root.right.right = new Node (16);
      Scanner kb = new Scanner(System.in);
      
      int choice;
      System.out.println("1 PreOrder, 2 Inorder, 3 Postorder\nEnter your choice");
      choice =kb.nextInt();
      switch(choice)
      {
        case 1:
            {  
              System.out.println("Preorder");
              tree.preOrder();
              break;
                
            }
        case 2:
            {
              System.out.println("Inorder");
              tree.inOrder();
              break;
            }
        case 3:
            {
                System.out.println("postOrder");
                tree.postOrder();
                break;
            }
        default:
        {
            System.out.println("Enter choice");
        }
        
      }

  }

}