Pre-order to post-order traversal

2019-01-31 13:27发布

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?

9条回答
迷人小祖宗
2楼-- · 2019-01-31 13:41

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.

查看更多
ら.Afraid
3楼-- · 2019-01-31 13:43

If you have been given preorder and you want to convert it into postorder. Then you should remember that in a BST in order always give numbers in ascending order.Thus you have both Inorder as well as the preorder to construct a tree.

preorder: 6, 2, 1, 4, 3, 7, 10, 9, 11

inorder: 1, 2, 3, 4, 6, 7, 9, 10, 11

And its postorder: 1 3 4 2 9 11 10 7 6

查看更多
闹够了就滚
4楼-- · 2019-01-31 13:50

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.

In a binary search tree, the values of all nodes in the left subtree are less than the value of the current node; and alike for the right subtree. Hence if you know the start of a pre-order dump of a binary search tree (i.e. its root node's value), you can easily decompose the whole dump into the root node value, the values of the left subtree's nodes, and the values of the right subtree's nodes.

To output the tree in post-order, recursion and output reordering is applied. This task is left upon the reader.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-31 13:50

I know this is old but there is a better solution.

We don't have to reconstruct a BST to get the post-order from the pre-order.

Here is a simple python code that does it recursively:

import itertools

def postorder(preorder):
    if not preorder:
        return []
    else:
        root = preorder[0]
        left = list(itertools.takewhile(lambda x: x < root, preorder[1:]))
        right = preorder[len(left) + 1:]
        return postorder(left) + postorder(right) + [root]

if __name__ == '__main__':
    preorder = [20, 10, 6, 15, 30, 35]
    print(postorder(preorder))

Output:

 [6, 15, 10, 35, 30, 20]

Explanation:

We know that we are in pre-order. This means that the root is at the index 0 of the list of the values in the BST. And we know that the elements following the root are:

  • first: the elements less than the root, which belong to the left subtree of the root
  • second: the elements greater than the root, which belong to the right subtree of the root

We then just call recursively the function on both subtrees (which still are in pre-order) and then chain left + right + root (which is the post-order).

查看更多
▲ chillily
6楼-- · 2019-01-31 13:51

This is the code of preorder to postorder traversal in python. I am constructing a tree so you can find any type of traversal

def postorder(root):
    if root==None:
        return
    postorder(root.left)
    print(root.data,end=" ")
    postorder(root.right)

def preordertoposorder(a,n):
    root=Node(a[0])
    top=Node(0)
    temp=Node(0)
    temp=None
    stack=[]
    stack.append(root)
    for i in range(1,len(a)):
        while len(stack)!=0 and a[i]>stack[-1].data:
            temp=stack.pop()
        if temp!=None:
            temp.right=Node(a[i])
            stack.append(temp.right)
        else:
            stack[-1].left=Node(a[i])
            stack.append(stack[-1].left)
    return root
class Node:
    def __init__(self,data):
        self.data=data
        self.left=None
        self.right=None  
a=[40,30,35,80,100]
n=5
root=preordertoposorder(a,n)
postorder(root)
# print(root.data)
# print(root.left.data)
# print(root.right.data)
# print(root.left.right.data)
# print(root.right.right.data)
查看更多
The star\"
7楼-- · 2019-01-31 13:57

Based on Ondrej Tucny's answer. Valid for BST only
example:

     20  
    /  \  
   10  30  
   /\    \  
  6  15   35  

Preorder = 20 10 6 15 30 35
Post = 6 15 10 35 30 20

For a BST, In Preorder traversal; first element of array is 20. This is the root of our tree. All numbers in array which are lesser than 20 form its left subtree and greater numbers form right subtree.

//N = number of nodes in BST (size of traversal array)
int post[N] = {0}; 
int i =0;

void PretoPost(int pre[],int l,int r){
  if(l==r){post[i++] = pre[l]; return;}
  //pre[l] is root
  //Divide array in lesser numbers and greater numbers and then call this function on them recursively  
  for(int j=l+1;j<=r;j++) 
      if(pre[j]>pre[l])
          break;
  PretoPost(a,l+1,j-1); // add left node
  PretoPost(a,j,r); //add right node
  //root should go in the end
  post[i++] = pre[l]; 
  return;
 }

Please correct me if there is any mistake.

查看更多
登录 后发表回答