Construct Tree From Given Inorder and Preorder traversals in C

Construct Tree From Given Inorder and Preorder Traversals

 

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.

Preorder Traversal: We first print the node, then move to the left subtree and the right subtree.
Inorder Traversal: We first move to the left subtree, then print the node and move to the right subtree.

inorder preorder traversal in C

Tree From Given Inorder and Preorder Traversel

Algorithm :

  • Pick an element from Preorder. Increment a Preorder Index Variable (preIndex in below code) to pick the next element in the next recursive call.
  • Create a new tree node tNode with the data as the picked element.
  • Find the picked element’s index in Inorder. Let the index be inIndex.
  • Call buildTree for elements before inIndex and make the built tree as a left subtree of tNode.
  • Call buildTree for elements after inIndex and make the built tree as a right subtree of tNode.
  • return tNode.
Tree From Given Inorder And Preorder Traversals

C Program based on above Algorithm

Run
#include<stdio.h>
#include<stdlib.h>
struct node
{
  char data;
  struct node *left;
  struct node *right;
};

int search (char arr[], int strt, int end, char value);
struct node *newNode (char data);

struct node *buildTree (char in[], char pre[], int inStrt, int inEnd)
{
  static int preIndex = 0;

  if (inStrt > inEnd)
    return NULL;

  struct node *tNode = newNode (pre[preIndex++]);

  if (inStrt == inEnd)
    return tNode;

  int inIndex = search (in, inStrt, inEnd, tNode->data);

  tNode->left = buildTree (in, pre, inStrt, inIndex - 1);
  tNode->right = buildTree (in, pre, inIndex + 1, inEnd);

  return tNode;
}

int search (char arr[], int strt, int end, char value)
{
  int i;
  for (i = strt; i <= end; i++)
    {
      if (arr[i] == value)
	return i;
    }
}

struct node *newNode (char data)
{
  struct node *node = (struct node *) malloc (sizeof (struct node));
  node->data = data;
  node->left = NULL;
  node->right = NULL;

  return (node);
}

void printInorder (struct node *node)
{
  if (node == NULL)
    return;

  printInorder (node->left);

  printf ("%c ", node->data);

  printInorder (node->right);
}

int main ()
{
  char in[] = { 'D', 'B', 'E', 'A', 'F', 'C' };
  char pre[] = { 'A', 'B', 'D', 'E', 'C', 'F' };
  int len = sizeof (in) / sizeof (in[0]);
  struct node *root = buildTree (in, pre, 0, len - 1);

  printf ("Inorder traversal of the constructed tree is \n");
  printInorder (root);
  getchar ();
}

Output:

Inorder traversal of the constructed tree is 
D B E A F C

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