3

For a Binary SEARCH Tree, a preorder or a postorder traversal is sufficient to reconstruct its original binary search tree unambigiously.

But I cannot find a proof or a explanation for why it is so.

For inorder traversal, it is trivial to come up with a counter-example to show that there may be many different BSTs correspond to a given inorder traversal.

Is there any proof or reference that a preorder or a postorder traversal is sufficient to reconstruct its original BST unambiguously?

This is for a BST, not for a general binary tree.

greybeard
  • 2,249
  • 8
  • 30
  • 66
SHH
  • 3,226
  • 1
  • 28
  • 41
  • 2
    Consider that the preorder traversal begins with the root, then the left subtree, then the right subtree. So the first element is definitely the root. Is there any possibility that there is more than one way that you could choose where amongst the rest of the traversal the left subtree ends and the right subtree begins. If there is no such ambiguity - there is a unique point that you can determine, then there is only one way to choose the root, left subtree nodes and right subtree nodes. Each subtree is a BST, so if you can uniquely split the entire tree like this, you can do it with subtrees. – moreON Mar 06 '17 at 02:12
  • 3
    moreON's argument is correct if we disallow duplicate elements. If we *don't* disallow duplicate elements, then the claim is false: `1 1 1` is a preorder traversal of 5 different BSTs. – ruakh Mar 06 '17 at 02:35
  • @ruakh - If you read carefully, you'll find that I proposed a method to reason about this, into which you can place your own rules and assumptions about BSTs that would lead to appropriate conclusions rather than constructing a specific argument for any particular conclusion. Note the use of "if ..." to allow one to come to their own conclusions. I will admit that with the conciseness required by comments it certainly does guide the reader toward one set of assumptions. – moreON Mar 06 '17 at 02:48
  • 1
    @moreON: My comment was not intended as a criticism of yours; I'm sorry if it came off as one. The OP is looking to prove a certain claim, and insofar as the claim is correct, your proof (= the proof that your comment is guiding the OP to) is the correct one. I'm simply pointing out an ambiguity in the claim, and clarifying that only under one interpretation is it actually correct. – ruakh Mar 06 '17 at 04:37
  • @ruakh Thanks for clarifying the problem. I should've mentioned that this was case for no duplicate elements :) – SHH Mar 06 '17 at 04:44
  • @moreON Thanks for the answer! Now I see the idea (the same logic applies to postorder). I would've defintely upvoted it an marked it as a correct answer if it wasn't a comment :) – SHH Mar 06 '17 at 04:46
  • Go ahead and convert it to something answer-worthy yourself and claim your correct answer bonus, I have no complaints. I think it's probably a little short of being an answer right now. – moreON Mar 06 '17 at 04:57
  • (You may answer your own question. Even if somebody else sketched the idea in a comment.) Please correct the title. – greybeard Mar 06 '17 at 10:43

1 Answers1

2

By the definition of pre-order traversal, it is guaranteed that root will be the first element.

Now, given the root, there is a unique set of elements that will come in the left half of the root - i.e. all elements having value less that root->val.

Likewise for the right subtree.

Now, we have to apply the above logic recursively for the left subtree and the right subtree.

Since, post-order traversal is just the reverse of pre-order traversal on the reverse tree, the same logic applies to post-order traversal too.