0

I came across a problem that really interested me, but I am not sure I fully understand how to complete the task at hand: Design an algorithm to construct a binary tree from two n-long sequences, known to be the output of in-order and post-order traversals of the same binary tree.

I've managed to complete that much so far. Below is my (relevant) code so far, however I also would like to be able to identify sequences for which no binary tree exists. I'm not sure how to check for this. Can someone give me a nudge in the right direction?

node* build_tree(int in[], int inStart, int inEnd,
                 int post[], int postStart, int postEnd) {
    if(inStart > inEnd || postStart > postEnd)
        return NULL;

    int rootValue = post[postEnd];
    node *tNode = new_node(rootValue);

    // find the index of this node in in-order traversal
    int inIndex = search(in, inStart, inEnd, rootValue);

    // Using index in in-order traversal, construct left and right subtrees
    tNode->left = build_tree(in, inStart, inIndex-1, post, postStart, postStart+inIndex-(inStart+1));
    tNode->right = build_tree(in, inIndex+1, inEnd, post, postStart + inIndex - inStart, postEnd - 1);

    return tNode;
}

// Function to find index of value in arr[start...end]
// The function assumes that value is present in in[]
int search(int arr[], int start, int end, int value) {
    int i;
    for(i = start; i < end; i++) {
        if(arr[i] == value)
            return i;
    }

    return i;
}

// function that allocates a new node with the
// given data and NULL left and right pointers
node* new_node(int data) {
    node* n = (node*)malloc(sizeof(node));
    n->data = data;
    n->left = NULL;
    n->right = NULL;

    return n;
}
5
  • Your description of the problem is fine. So take a shot at the algorithm so we can critique your approach. You'll be downvoted and/or closed if you don't.
    – Gene
    Nov 4, 2014 at 0:45
  • 1
    I added my code so far and I think it's close. I'm just not sure how to verify the output and prove that it's working. Nov 4, 2014 at 1:00
  • Just traverse the tree you're getting to produce inorder and postorder and compare them with the input!
    – Gene
    Nov 4, 2014 at 2:09
  • You can search SO for solution to the same problem and check yours against them. Most of them deal with inputs where the elements in the tree are unique, though.
    – nhahtdh
    Nov 4, 2014 at 3:27
  • How would I go about determining whether or not a given sequence can produce a binary tree? I got it working up to this point, but still no luck with that part... Nov 4, 2014 at 22:38

2 Answers 2

0

AFAIK a binary tree is not possible for the given sequences when ,

1) The two sequences are not of same length -- we can check the array length

2) The search of postorder value in inorder sequence fails -- search function should be modified to return negative value when search fails instead of returning i(after the for loop)

3) The given sequences do not represent postorder and inorder of the same tree -- as suggested by Gene traverse the tree and check the sequence

0

My Code for this problem :)

int searchelement( vector<int> &v , int start ,int end,int val)
{
    for(int i=start;i<=end;i++)
    {
    if(v[i]==val)
    return i;
    }
    return -1;
}
TreeNode * abc(vector<int> &postorder, vector<int> &inorder , int start ,int end , int &index)
{
    if(start>end)
    return NULL;
    int i = searchelement(inorder,start,end,postorder[index--]);
    TreeNode * temp = new TreeNode(inorder[i]);
    if(start==end)
    {
        return temp;
    }
    temp->right=abc(postorder,inorder,i+1,end,index);
    temp->left =abc(postorder,inorder,start,i-1,index);
    return temp;
}

main(){
    TreeNode * head =NULL;
    int start=0,end=inorder.size()-1,index=0;
    head= abc(preorder,inorder,start,end,index);
}
2
  • Thanks for your answer. Can you provide some more information, maybe some comments, about how your code works?
    – JAL
    Jun 9, 2015 at 18:16
  • @JAL 1) It just Picking elements from back of the postorder vector and then searching the element in inorder vector and find index. 2) And split the inorder vector on the basis of index.
    – krishana
    Jun 10, 2015 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.