Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-order to post-order traversal

Tags:

If the pre-order traversal of a binary search tree is 6, 2, 1, 4, 3, 7, 10, 9, 11, how to get the post-order traversal?

like image 902
Bobj-C Avatar asked Dec 27 '10 10:12

Bobj-C


People also ask

How do I convert pre-order to post order?

Pre-order = outputting the values of a binary tree in the order of the current node, then the left subtree, then the right subtree. Post-order = outputting the values of a binary tree in the order of the left subtree, then the right subtree, the the current node.

Where is Postorder traversal from preorder traversal?

In preorder traversal, the first element is always the root, and it will certainly lie in the initial range. So store the first element of the preorder array. In postorder traversal, first left and right subtrees are printed and then root data is printed.

Can we construct tree from pre-order and post order?

It is not possible to construct a general Binary Tree from preorder and postorder traversals (See this).

Can a preorder and postorder traversal be the same?

In order for them to produce the same output, "left" must be equals to "root", which just doesn't make sense. With the above example, pre-order will produce AB or AC respectively and post-order will produce BA and CA. Thank you, So just in one situation the pre-order and post-order are the same.


1 Answers

You are given the pre-order traversal of the tree, which is constructed by doing: output, traverse left, traverse right.

As the post-order traversal comes from a BST, you can deduce the in-order traversal (traverse left, output, traverse right) from the post-order traversal by sorting the numbers. In your example, the in-order traversal is 1, 2, 3, 4, 6, 7, 9, 10, 11.

From two traversals we can then construct the original tree. Let's use a simpler example for this:

  • Pre-order: 2, 1, 4, 3
  • In-order: 1, 2, 3, 4

The pre-order traversal gives us the root of the tree as 2. The in-order traversal tells us 1 falls into the left sub-tree and 3, 4 falls into the right sub-tree. The structure of the left sub-tree is trivial as it contains a single element. The right sub-tree's pre-order traversal is deduced by taking the order of the elements in this sub-tree from the original pre-order traversal: 4, 3. From this we know the root of the right sub-tree is 4 and from the in-order traversal (3, 4) we know that 3 falls into the left sub-tree. Our final tree looks like this:

  2  / \ 1   4    /   3 

With the tree structure, we can get the post-order traversal by walking the tree: traverse left, traverse right, output. For this example, the post-order traversal is 1, 3, 4, 2.

To generalise the algorithm:

  1. The first element in the pre-order traversal is the root of the tree. Elements less than the root form the left sub-tree. Elements greater than the root form the right sub-tree.
  2. Find the structure of the left and right sub-trees using step 1 with a pre-order traversal that consists of the elements we worked out to be in that sub-tree placed in the order they appear in the original pre-order traversal.
  3. Traverse the resulting tree in post-order to get the post-order traversal associated with the given pre-order traversal.

Using the above algorithm, the post-order traversal associated with the pre-order traversal in the question is: 1, 3, 4, 2, 9, 11, 10, 7, 6. Getting there is left as an exercise.

like image 115
moinudin Avatar answered Nov 11 '22 17:11

moinudin