21

I am very confused by a number of articles at different sites regarding constructing a Binary Search Tree from any one traversal (pre,post or in-order), or a combination of any two of them. For example, at this page, it says that given the pre,post or level order traversal, along with the in-order traversal, one can construct the BST. But here and there, they show us to construct a BST from pre-order alone. Also, here they show us how to construct the BST from given pre and post-order traversals. In some other site, I found a solution for constructing a BST from the post-order traversal only.

Now I know that given the inorder and pre-order traversals, it is possible to uniquely form a BST. As regards the first link I provided, although they say that we can't construct the BST from pre-order and post-order, can't I just sort the post-order array to get its inorder traversal, and then use that and the pre-order array to form the BST? Will that be same as the solution in the 4th link, or different? And given pre-order only, I can sort that to get the in-order, then use that and the pre-order to get the BST. Again, does that have to be different from the solution at links 2 and 3?

Specifically, what is sufficient to uniquely generate the BST? If uniquement is not required, then I can simply sort it to get the in-order traversal, and build one of the N possible BSTs from it recursively.

SexyBeast
  • 7,913
  • 28
  • 108
  • 196

2 Answers2

26

To construct a BST you need only one (not in-order) traversal.

In general, to build a binary tree you are going to need two traversals, in order and pre-order for example. However, for the special case of BST - the in-order traversal is always the sorted array containing the elements, so you can always reconstruct it and use an algorithm to reconstruct a generic tree from pre-order and in-order traversals.

So, the information that the tree is a BST, along with the elements in it (even unordered) are equivalent to an in-order traversal.

Bonus: why is one traversal not enough for a general tree, (without the information it is a BST)?
Answer: Let's assume we have n distinct elements. There are n! possible lists to these n elements, however - the possible number of trees is much larger (2 * n! possible trees for the n elements are all decayed trees, such that node.right = null in every node, thus the tree is actually a list to the right. There are n! such trees, and another n! trees where always node.left = null ) Thus, from pigeon hole principle - there is at least one list that generates 2 trees, thus we cannot reconstruct the tree from a single traversal. (QED)

amit
  • 175,853
  • 27
  • 231
  • 333
  • So the solutions at 2nd and 3rd links generate the only possible `BST`? – SexyBeast Oct 14 '12 at 09:27
  • @Cupidvogel: I did not follow the code there - but if it is correct then yes. There is only one BST for each pre-order traversal. – amit Oct 14 '12 at 09:29
  • Then see the article at http://www.geeksforgeeks.org/archives/6633. They construct a `BST` from `pre` and `in-order` traversals. But considering that the tree can be constructed from the `pre-order` alone, don't you think that the binary search needed in this code to find the position makes it less efficient? Won't it be more efficient to not use the `inorder` at all, and simply use the `pre-order` to construct the tree? However, will the two solutions necessarily yield the same tree? – SexyBeast Oct 14 '12 at 09:34
  • @Cupidvogel: The in-order + pre-order traversal solution is constructing a *tree*, it does NOT have to be a *BST*. BST is a specific case where the in-order is implicitly known and can be used. Assuming the two solutions are correct - getting the pre order and in order, and invoking the last algorithm will yield the same tree as invoking the first algorothm (pre-order alone). – amit Oct 14 '12 at 09:39
  • By the way, can you give me an idea or link to a resource on how to construct the `BST` from the `post-order` array only? – SexyBeast Oct 14 '12 at 09:43
  • Sorry, I am not aware of one (It really does not thing exist, it just mean I never researched about it)/ – amit Oct 14 '12 at 09:44
  • Can you please elaborate on this a bit more: *Thus, from pigeon hole principle - there is at least one list that generates 2 trees, thus we cannot reconstruct the tree from a single traversal*? And how does pigeon hole principle apply here? – Nawaz Jul 23 '18 at 16:24
  • 1
    @Nawaz pigeonholes: permutations. Each permutation is a pigeonhole. There are n! of those. pigeons: all distinct trees with n unique elements (same `n`). There are `X > n!` such trees. Let `f:trees->permutations` be any mapping function. Since `|trees| > |permutations|`, there are some `x!=y` such that `f(x) = f(y)` (pigeonhole principle). That means, given only `f(x)`, you cannot construct uniquely the tree led to `f(x)`, because it could be either `x` or `y`. I hope that's clear, let me know if this explanation is missing any critical details. – amit Jul 24 '18 at 08:28
  • Got it now. Thanks for an awesome explanation. :-) – Nawaz Jul 24 '18 at 08:59
0

If the values for the nodes of the BST are given then only one traversal is enough because the rest of the data is provided by the values of the nodes. But if the values are unknown then, as per my understanding, constructing a unique BST from a single traversal is not possible. However, I am open to suggestions.