user658266
user658266

Reputation: 2435

how to rebuild BST using {pre,in,post}order traversals results

We know the pre-order, in-order and post-order traversals. What algorithm will reconstruct the BST?

Upvotes: 3

Views: 7956

Answers (4)

fl00r
fl00r

Reputation: 83680

Here is a Ruby recursive solution

def rebuild(preorder, inorder)
  root = preorder.first
  root_inorder = inorder.index root
  return root unless root_inorder
  root.left = rebuild(preorder[1, root_inorder], inorder[0...root_inorder])
  root.right = rebuild(preorder[root_inorder+1..-1], inorder[root_inorder+1..-1])
  root
end

And an example

class Node
  attr_reader :val
  attr_accessor :left, :right

  def initialize(val)
    @val = val
  end

  def ==(node)
    node.val == val
  end

  def inspect
    "val: #{val}, left: #{left && left.val || "-"}, right: #{right && right.val || "-"}"
  end
end

inorder = [4, 7, 2, 5, 1, 3, 8, 6, 9].map{|v| Node.new v }
preorder = [1, 2, 4, 7, 5, 3, 6, 8, 9].map{|v| Node.new v }

tree = rebuild(preorder, inorder)
tree
# val: 1, left: 2, right: 3
tree.left
# val: 2, left: 4, right: 5
tree.left.left
# val: 4, left: -, right: 7

Upvotes: 0

codaddict
codaddict

Reputation: 454960

For reconstruction of a binary tree either preorder+inorder or postorder+inorder is needed. As already pointed out for a BST we can reconstruct using either preorder or postorder as sorting either of them will give us the inorder.

You can use the following function which is modification of the code given by @brainydexter to reconstruct the tree without using the static variable:

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

    // start index > end index..base condition return NULL.
    if(inStrt > inEnd)
        return NULL;

    // build the current node with the data at pre[preIndex].
    struct node *tNode = newNode(pre[preIndex]);

    // if all nodes are constructed 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,preIndex+1);
    tNode->right = buildTree(in, pre, inIndex+1, inEnd,preIndex+inIndex+1);

    return tNode;
}

Upvotes: 0

brainydexter
brainydexter

Reputation: 20346

I personally found Dante's answer a little hard to follow. I worked my way through the solution and found it to be similar to the one posted here http://geeksforgeeks.org/?p=6633

Complexity is O(N^2).

Here's another approach for building a tree using post-order traversal: http://www.technicallyidle.com/2011/02/15/build-binary-search-tree-using-post-order-traversal-trace/

Hope this helps

Upvotes: 0

Dante May Code
Dante May Code

Reputation: 11247

Because it is BST, in-order can be sorted from pre-order or post-order <1>. Actually, either pre-order or post-order is needed only....

<1> if you know what the comparison function is


From pre-order and in-order, to construct a binary tree

BT createBT(int* preOrder, int* inOrder, int len)
{
    int i;
    BT tree;
    if(len <= 0)
        return NULL;
    tree = new BTNode;
    t->data = *preOrder;
    for(i = 0; i < len; i++)
        if(*(inOrder + i) == *preOrder)
            break;
    tree->left = createBT(preOrder + 1, inOrder, i);
    tree->right = createBT(preOrder + i + 1, inOrder + i + 1, len - i - 1);
    return tree;
}

The rationale behind this:

In pre-order, the first node is the root. Find the root in the in-order. Then the tree can be divided into left and right. Do it recursively.

Similar for post-order and in-order.

Upvotes: 12

Related Questions