Binary Tree Traversals and Building Binary Tree given the Traversal data
There are three common types of binary tree traversals: in-order, pre-order, and post-order. These traversals define the order in which the nodes of the binary tree are visited.
In in-order traversal, we visit the left subtree, then the current node, and finally the right subtree.
Algorithm:
- Traverse the left subtree in-order.
- Visit the current node.
- Traverse the right subtree in-order.
Pseudocode In-order Traversal
Pre-Order Traversal:
Algorithm:
- Visit the current node.
- Traverse the left subtree pre-order.
- Traverse the right subtree pre-order.
In post-order traversal, we visit the left subtree, then the right subtree, and finally the current node.
Algorithm:
- Traverse the left subtree post-order.
- Traverse the right subtree post-order.
- Visit the current node.
C Implementation- Binary Tree Traversals
Enqueue the root node into a queue.
While the queue is not empty:
Enqueue its left child (if it exists).
Enqueue its right child (if it exists).
Level Order Traversal: 1 2 3 4 5
Constructing the Tree from given Inorder and Preorder Traversals
Algorithm
Pick the Root:
- The first element in the pre-order traversal is the root of the tree.
Locate the Root in In-Order Traversal:
- Find the index of the root element in the in-order traversal. The elements before this index represent the left subtree, and elements after represent the right subtree.
Recursively Build Left and Right Subtrees:
- Recursively construct the left subtree using the elements from the left side of the root in the in-order traversal.
- Recursively construct the right subtree using the elements from the right side of the root in the in-order traversal.
Preorder sequence: A B D E C F
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
char data;
struct node* left;
struct node* right;
};
/* Prototypes for utility functions */
int search(char arr[], int strt, int end, char value);
struct node* newNode(char data);
/* Recursive function to construct binary of size len from
Inorder traversal in[] and Preorder traversal pre[]. Initial values
of inStrt and inEnd should be 0 and len -1. The function doesn't
do any error checking for cases where inorder and preorder
do not form a tree */
struct node* buildTree(char in[], char pre[], int inStrt, int inEnd)
{
static int preIndex = 0;
if (inStrt > inEnd)
return NULL;
/* Pick current node from Preorder traversal using preIndex
and increment preIndex */
struct node* tNode = newNode(pre[preIndex++]);
/* If this node has no children then return */
if (inStrt == inEnd)
return tNode;
/* Else find the index of this node in Inorder traversal */
int inIndex = search(in, inStrt, inEnd, tNode->data);
/* Using index in Inorder traversal, construct left and
right subtress */
tNode->left = buildTree(in, pre, inStrt, inIndex - 1);
tNode->right = buildTree(in, pre, inIndex + 1, inEnd);
return tNode;
}
/* UTILITY FUNCTIONS */
/* Function to find index of value in arr[start...end]
The function assumes that value is present in in[] */
int search(char arr[], int strt, int end, char value)
{
int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
return i;
}
}
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
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);
}
/* This function is here just to test buildTree() */
void printInorder(struct node* node)
{
if (node == NULL)
return;
/* first recur on left child */
printInorder(node->left);
/* then print the data of node */
printf("%c ", node->data);
/* now recur on right child */
printInorder(node->right);
}
/* Driver program to test above functions */
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);
/* Let us test the built tree by printing Inorder traversal */
printf("Inorder traversal of the constructed tree is \n");
printInorder(root);
return 0;
}
✅ Algorithm: Build a BST from Preorder Traversal
Input: Preorder traversal array.
Output: Root of the reconstructed BST.
Idea
-
In preorder: Root → Left → Right.
-
The first element of preorder is always the root.
-
All elements smaller than root go to the left subtree.
-
All elements greater than root go to the right subtree.
-
Recursively apply this rule.
Example
Preorder = [40, 30, 20, 35, 50, 45, 60]
-
Root = 40
-
Left subtree = [30, 20, 35] (all < 40)
-
Right subtree = [50, 45, 60] (all > 40)
-
-
Left part [30, 20, 35]
-
Root = 30
-
Left subtree = [20], Right subtree = [35]
-
-
Right part [50, 45, 60]
-
Root = 50
-
Left subtree = [45], Right subtree = [60]
-
👉 Final BST:





Comments
Post a Comment