Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we construct a full binary tree with only postorder traversal or preorder traversal?

For example, we are provided with only post order traversal array or only pre order traversal array. Can we reconstruct the binary tree back? If we know that the binary tree is full. Moreover, if it is not, is it possible to construct the full binary if know both preorder and post order at the same time?

like image 946
Makara Avatar asked Jan 22 '26 23:01

Makara


2 Answers

No, you can't from one list alone.

think of the postorder list: 4 5 2 3 1

    1         1   
   / \       / \
  2   3     4   3
 / \           / \
4   5         5   2

both trees are possible, but we don't know which one generated the list

Assuming every element in the tree is unique, we know that preorder is build like that:

[Node][     LeftTree     ][     RightTree     ]

and postorder like this:

[     LeftTree     ][     RightTree     ][Node]

if we have two lists, preorder 1 2 4 5 3 and postorder 4 5 2 3 1, we know that 1 is the root of the tree, because it is the first number of the preorder list (and the last number of the postorder list). Furthermore we know that 2 must be the root of the left tree and 3 the root of the right tree, because they are the first numbers after the root node which are roots of the left or right tree. With this in mind we can split the lists into this:

           [Root in preorder] [ LeftTree ] [RightTree] [Root in postorder]
preorder:        [1]             [2 4 5]      [3]     
postorder:                       [4 5 2]      [3]              [1]   

From here you can do this algorithm recursively with the left and right tree and in the end you get this:

    1     
   / \      
  2   3    
 / \       
4   5

Since every element is unique there is only one way to build the tree and therefore you can rebuild a tree from its postorder and preorder list.

In case you have elements that are the same you can't build a unique tree, example:

preorder:  1 X X 5 X
postorder: X 5 X X 1

from these lists you could create these two trees:

    1         1   
   / \       / \
  X   X     X   X
 / \           / \
X   5         5   X
like image 109
Absurd-Mind Avatar answered Jan 24 '26 18:01

Absurd-Mind


I was playing with the orders a bit to understand them better and here are my findings:

  • Post-order - from the sequence you can always tell what is the root and what is the right-most child (ex. is 1,2,3,4,5 - 5 is the root and 4 is the right-most child)
  • Pre-order - from the sequence you can always tell what is the root and what is the left-most child (ex. is 1,2,3,4,5 - 1 is the root and 2 is the left-most child)
  • In-order - given a root vertex, you can always tell what is on the left and what is on the right (ex. is 1,2,3,4,5 and for the root 3 - 1,2 are on the left, 3,4,5 are on the right )

Now you can play with it. Having an in-order with either post-order or pre-order, you can easily reconstruct the tree because you can find the root and recursively find it always for the left/right branch. In case of having pre-order and post-order together, you can find the root and left-most children & right-most children. The problem happens in case the root has only left/right children, because you cannot tell which one it is and as such, you cannot easily reconstruct the tree.

However, as being asked, having the "full" binary tree, where every vertex has either both children or any, you don't have the problem with pre/post order combination and therefore every pair of orders will help you to reconstruct the tree. However only having one of the orders, you cannot reconstruct the tree (for example knowing only left child is not enough, you don't have information about which is the right one)

like image 31
Matej Briškár Avatar answered Jan 24 '26 17:01

Matej Briškár



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!