Construct Tree from given Postorder and Preorder traversals
Construct Tree from given Postorder and Preorder traversal
There are three types of traversals in a tree: Inorder, Preorder and Postorder Traversal. In this article we will discuss how to construct tree from given postorder and preorder traversal .
Preorder Traversal – We first print the node,then move to the left subtree and finally to the right subtree.
Postorder Traversal – Left and right subtree is visited first and then the node is printed
Here, we construct Tree from given Postorder and Preorder traversal
Tree From Given Postorder and Preorder Traversal
Given traversals:
Preorder: 50, 25, 12, 37, 30, 40, 75, 62, 60, 70, 87
Postorder: 12, 30, 40, 37, 25, 60, 70, 62, 87, 75, 50
- The first element in the preorder traversal is the root of the tree.
So, here 50 will be the root of the tree. - We will find the index of element next to 50 i.e 25 in the postorder traversal.The index found is 4. Let this index is denoted by ‘pos’.
- All the elements to the left of this index and element at this index( i.e from 0 to 4 index) will be in the left subtree of 50.
- And all the elements to the right of this index ( from 6 to 10) will be in the right subtree of 50.
Now, we will divide preorder and postorder array in two parts.
One is for the left subtree and other is for the right subtree.
Let
- presi: starting index for preorder array
- preei: ending index for preorder array
- postsi: starting index of postorder array
- postei: ending index of postorder array
- clc: Number of elements in the left subtree
Clearly, clc= pos – postsi + 1;
For left subtree:
Preorder array: from index presi + 1, presi + clc
Postorder array: from index postsi, pos.
For right subtree:
Preorder array: from index presi + clc + 1, preei
Postorder array: from index pos + 1, postei -1
Using the above arrays, all the steps are recursively repeated.
Java Program to Construct Tree from given Postorder and Preorder Traversals
import java.util.*; 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[]post) { this.root = this.construct (pre, 0, pre.length-1, post, 0, post.length-1); } private Node construct (int[]pre, int presi, int preei, int[]post, int postsi, int postei) { // this case occurs when a node has only one child if (presi > preei) { return null; } Node node = new Node (pre[presi]); node.left = null; node.right = null; this.size++; if (presi == preei) { return node; } //Searching pre[presi + 1] in postorder array int pos = -1; for (int i = postsi; i <= postei; i++) { if (post[i] == pre[presi + 1]) { pos = i; break; } } //Number of elements in left subtree int clc = pos-postsi + 1; //Left subtree node.left = this.construct (pre, presi + 1, presi + clc, post, postsi, pos); //Right subtree node.right = this.construct (pre, presi + clc + 1, preei, post, pos + 1, postei-1); return node; } // Postorder tree traversal public void inOrder () { inOrder (this.root); } private void inOrder (Node node) { if (node == null) { return; } System.out.print (node.data + " "); inOrder (node.left); inOrder (node.right); } } public static void main (String[]args) throws Exception { // Construct binary tree int[] pre = { 50, 25, 12, 37, 30, 40, 75, 62, 60, 70, 87 }; int[] post = { 12, 30, 40, 37, 25, 60, 70, 62, 87, 75, 50 }; BinaryTree bt = new BinaryTree (pre, post); System.out.println("The new tree constructed is : "); bt.inOrder (); } }
Output:
Inorder traversal of the constructed tree: 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
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
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
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- C| C++| Java
- Spiral Order traversal of Tree- C | C++| 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.- C| C++| 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- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
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
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- C| C++| Java
- Spiral Order traversal of Tree- C | C++| 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.- C| C++| 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- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java