Construct Tree from given Inorder and Preorder Traversals

Construct Tree From Given Inorder and Preorder Traversals in Java

There are three types of traversals in a tree: Inorder, Preorder and Postorder traversal. A tree can be formed with any two tree traversals in which one of them being the in order traversal.

construct a tree from preorder and inorder traversal

Given Inorder and Preorder Tree Traversals, construct the Tree.

Example

Given traversals:
Preorder: 50, 25, 12, 37, 30, 40, 75, 62, 60, 70, 87
Inorder:12, 25, 30, 37, 40, 50, 60, 62, 70, 75, 87
1. The last element in the preorder traversal is the root of the tree.
So, here 50 will be the root of the tree.
2. We will find the index of 50 in the inorder traversal.The index found is 5. Let this index is denoted by ‘pos’.
3. All the elements to the left of this index ( from 0 to 4 index) will be in the left subtree of 50.
4. And all the elements to the right of this index ( from 6 to 10) will be in the right subtree of 50.

structure of tree

Now, we will divide preorder and inorder array in two parts.
One is for the left subtree and other is for the right subtree.

Let

  • psi: starting index for preorder array
  • pei: ending index for preorder array
  • isi: starting index of inorder array
  • iei: ending index of preorder array
  • clc: Number of elements in the left subtree

Clearly, clc= pos – isi;

For left subtree:
Preorder array: from index psi + 1, psi + clc
Inorder array: from index isi, isi + clc -1

For right subtree:
Preorder array: from index psi + clc + 1, pei
Inorder array: from index isi + clc + 1, iei

Using the above arrays, all the steps are recursively repeated.

The following binary tree is obtained:

Construct Tree from given Inorder and Preorder traversals

Code For Binary Search Tree​

Run
public class Main
{

// Binary tree class
  public static class BinaryTree
  {
// Node class
    public class Node
    {
      int data;
      Node left;
      Node right;

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

    private Node root;
    private int size;

    public BinaryTree (int[]pre, int[]in)
    {
      this.root =
	this.construct (pre, 0, pre.length-1, in, 0, in.length-1);
    }

    private Node construct (int[]pre, int psi, int pei, int[]in, int isi,int iei)
    {

//Base case
      if (psi > pei)
	{
	  return null;
	}
      Node node = new Node (pre[psi]);
      node.left = null;
      node.right = null;
      this.size++;

//Finding pre[psi] in the inorder array
      int pos = -1;
      for (int i = isi; i <= iei; i++)
	{
	  if (in[i] == node.data)
	    {
	      pos = i;
	      break;
	    }
	}

//Number of elements in left subtree
      int clc = pos-isi;

//Left subtree
      node.left =
	this.construct (pre, psi + 1, psi + clc, in, isi, isi + clc-1);

//Right subtree
      node.right = this.construct (pre, psi + clc + 1, pei, in, isi + clc + 1,
				   iei);

      return node;
    }

// Preorder tree traversal
    public void preOrder ()
    {
      preOrder (this.root);
    }

    private void preOrder (Node node)
    {
      if (node == null)
	{
	  return;
	}

      System.out.print (node.data + " ");
      preOrder (node.left);
      preOrder (node.right);

    }
  }

  public static void main (String[]args) throws Exception
  {

// Construct binary tree

    int[] in = { 12, 25, 30, 37, 40, 50, 60, 62, 70, 75, 87 };
    int[] pre = { 50, 25, 12, 37, 30, 40, 75, 62, 60, 70, 87 };

    BinaryTree bt = new BinaryTree (pre, in);
    System.out.println("The new tree constructed is :");
    bt.preOrder ();

  }
}

Output :

The new tree constructed is :
50 25 12 37 30 40 75 62 60 70 87 

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