If you are given two traversal sequences, can you construct the binary tree?
August 2, 2009
It depends on what traversals are given. If one of the traversal methods is Inorder then the tree can be constructed, otherwise not.
Therefore, following combination can uniquely identify a tree.
Inorder and Preorder.
Inorder and Postorder.
Inorder and Level-order.
And following do not.
Postorder and Preorder.
Preorder and Level-order.
Postorder and Level-order.
For example, Preorder, Level-order and Postorder traversals are same for the trees given in above diagram.
Preorder Traversal = AB
Postorder Traversal = BA
Level-Order Traversal = AB
So, even if three of them (Pre, Post and Level) are given, the tree can not be constructed.
@geeksforgeeks team if we know any one of the traversal except inorder we can construct the unique BST . because we can ourself find the inorder traversal by sort the given traversal sequence.
@Rahul Singh
We are talking about Binary tree not BST
#include
#include
#include
using namespace std;
struct node
{
int data;
node *left;
node *right;
};
node* Newnode(int data)
{
node * curr;
curr = (node *)malloc(sizeof(node));
curr->data = data;
curr->left = curr->right = NULL;
return(curr);
}
int search(int inorder[],int start,int end,int data)
{
for(int i=start;i end)
return NULL;
data=postorder[post_index--];
node *root=Newnode(data);
if(start == end)
return root;
in_pos=search(inorder,start,end,data);
root->right=createTree(postorder,inorder,in_pos+1,end);
root->left=createTree(postorder,inorder,start,in_pos-1);
return root;
}
void display_inOrder(struct node* node)
{
if (node == NULL)
return;
display_inOrder(node->left);
printf("%d ", node->data);
display_inOrder(node->right);
}
int main()
{
int n,in[100000],post[100000],i;
node *root;
cout<>n;
cout<<"Enter postorder sequence = ";
for(i=0;i>post[i];
cout<<"Enter inorder sequence = ";
for(i=0;i>in[i];
root=createTree(post,in,0,n-1);
cout<<"output inorder sequence = ";
display_inOrder(root);
return 0;
}
Here is a an algorithm from the URL http://stackoverflow.com/questions/4575719/binary-tree-from-in-order-and-level-order-traversals that mentions how to construct a BST given inorder and level order.
f(inorder, levelorder):
if length(levelorder) == 0:
return None
root = levelorder[0]#set root to first element in levelorder
subIn1, subIn2 = partition(inorder, levelorder[0]) #partition inorder based on root
subLevel1 = extract(levelOrder, subIn1)#remove elements in level order not in subIn1
subLevel2 = extract(levelOrder, subIn2)#remove elements in level order not in subIn2
root->left = f(subIn1, subLevel1)
root->right = f(subIn2, subLevel2)
return root
/* Paste your code here (You may delete these lines if not writing code) */ Construct Tree from given Inorder and Preorder traversals April 16, 2010 Let us consider the below traversals: Inorder sequence: D B E A F C Preorder sequence: A B D E C F BuildTree(inorder[],preorder[],start,end) { static int preindex=0; If start>end return NULL; struct node *newnode=new(preorder(preindex)); preindex=preindex+1; If start==end return node; int searchind=search(inorder,start,end,node->data); node->left=BuildTree(inorder,preorder,start,searchind-1); node->right=BuildTree(inorder,preorder,searchind+1,end); return newnode; } ========================= Given inorder and postorder traversals construct a binary tree Inorder sequence: D B E A F C Postorder sequence: D E B F C A struct node *BuidTree(int postorder[], int inorder[], int start, int end) { static int postindex=n-1; int searchindex; If start>end return NULL; struct node *newNode=new(postorder[postindex--]); If (start==end) return newNode; searchindex=search(inorder,start,end); newNode->right=BuildTree(postorder, inorder, searchindex+1,end); newNode->left=BuildTree(postorder,inorder,start,searchindex-1); return newNode; }Let us consider the below traversals:
Inorder sequence: D B E A F C
Preorder sequence: A B D E C F
BuildTree(inorder[],preorder[],start,end)
{
static int preindex=0;
If start>end return NULL;
struct node *newnode=new(preorder(preindex));
preindex=preindex+1;
If start==end return node;
int searchind=search(inorder,start,end,node->data);
node->left=BuildTree(inorder,preorder,start,searchind-1);
node->right=BuildTree(inorder,preorder,searchind+1,end);
return newnode;
}
=========================
Given inorder and postorder traversals construct a binary tree
Inorder sequence: D B E A F C
Postorder sequence: D E B F C A
struct node *BuidTree(int postorder[], int inorder[], int start, int end)
{
static int postindex=n-1;
int searchindex;
If start>end return NULL;
struct node *newNode=new(postorder[postindex--]);
If (start==end) return newNode;
searchindex=search(inorder,start,end);
newNode->right=BuildTree(postorder, inorder, searchindex+1,end);
newNode->left=BuildTree(postorder,inorder,start,searchindex-1);
return newNode;
}
Inorder of a tree is must as from other traversal we are getting the root node of that tree and from inorder we get the child nodes which are in left subtree and right subtree as nodes which are in left subtree appears before root node in inorder traversal and the ones which are in right subtree appears after root.
Hey can u xplain how to create a tree from inorder and level order !! I tried but can't figure out how we will get the knowledge of which child to attach to which root.
Given a post order and pre order traversal you can still construct a unique tree provide each internal nodes has two childern's
anandtechblog.blogspot.com/2011/06/construct-given-tree-from-pre-order-and.html
anandtechblog.blogspot.com/2011/06/construct-given-tree-from-inorder-and.html
Let I(n) = i0,i1,i2,i3....in be elements of a inorder traversal of a binary tree.
Simillarly let Pre(n) = p0,p1,p2......pn be the elements of a preorder traversal of a binary tree.
Now If we know that ik is root of binary tree , we can be sure that elements i0..ik-1 are in left subtree of tree and elements from 1k+1..in are in right-subtree rooted at ik(we can prove this by contradiction).
Now , if we fix ik to be root. We inturn fix its left subtree (i0..ik-1), and its right right-subtree(ik+1...n), thus in essence we fix the tree (we can apply induction on n to mathematically prove this).
Inorder to fix the root , we can use either pre-order traversal.In any preorder traversal Pre(n) ,p0 is root of the the binarytree. We can also use Postorder traversal Post(n) say q0..qn in which qn is always the root of the tree.
So this proves that inorder combined with either post-order or preorder uniquely determine a tree.
Similarly, for trees like :
A A / and \ B Bboth have preorder(and level-order) AB and postorder BA
consider Two Binary Trees
For tree1 :
Root = A
Left Chid = B
Preorder: A,B
Postorder: B,A
and for tree 2:
Root = A
Right Child = B
Preorder: A,B
Postorder: B,A
For given preorder and postorder two different binary trees can be formed
Forget about binary tree. What about a BST with just pre-order or a post-order?
It is not very clear why InOrder is a must to recreate the tree.
Can you please provide more details regarding the same?
It is not very clear why InOrder is a must to recreate the tree.
Can you please provide more details regarding the same?
//preIndex is global
node* BST::buildTree(int in[],int inStrt,int inEnd,int len,int pre[]) { if(preIndex >= len || inStrt > inEnd) return NULL; node *retNode = makeNode(pre[preIndex++]); if(inStrt == inEnd) return retNode; int inIndex = findNodeIn(in,inStrt, inEnd, retNode->data); retNode->left = buildTree(in, inStrt,inIndex-1,len,pre); retNode->right = buildTree(in,inIndex+1,inEnd,len,pre); return retNode; } int BST::findNodeIn(int in[],int inStrt, int inEnd,int value) { int i = inStrt ; for(;i<=inEnd;i++) if(in[i] == value) return i; } //test code int preIndex; int main() { preIndex = 0; int in[] = {1,2,3,4,5,7,8}; int pre[] = {4,2,1,3,7,5,8}; root = buildTree(in,0,6,7,pre); }@Rohini: Thanks for providing the code. We have published it here.