Question

As we know to get a exact binary tree we need at-least two traversals (in-order and pre-order / in-order and post-order) to get back the original tree. But is it mandatory to have two traversals (in-order and pre-order / in-order and post-order) to get the original tree if the tree is a red-black tree? Thanks.

Was it helpful?

Solution

It is easy to show that in-order traversal is not sufficient, by providing a counter-example:

     5B
  3R     10B
1B  4B

      3B
    1B   5R
        4B  10B

These are both valid red-black trees whose in-order traversal order is (1,3,4,5,10). Thus, it is impossible to reconstruct a red-black tree by only an in-order traversal of its nodes.

OTHER TIPS

For reference, "Either pre-order or post-order paired with in-order is sufficient to describe the tree uniquely, while pre-order with post-order leaves some ambiguity in the tree structure." In particular, this answer shows by counter-example that in-order traversal alone is not sufficient, and this Q&A shows that pre-order & post-order together are not sufficient. Because "a red–black tree is a special type of binary tree," the answer is yes: At least two traversals, one of which is in-order, are required.

No, a pre-order or a post-order traversal is sufficient.

Since the red-black tree is a binary search tree, the search tree property implies that the in-order traversal is simply the sorted list of all elements, and thus superfluous.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top