简体   繁体   中英

Building Binary Tree from Pre and In order

I'm trying to build a binary tree from In-order and Pre-order. Each node holds an integer value for data. I ran into a problem when having these arrays:

Pre-order: 3,9,2,6,1,1,1,4
In-Order: 2,9,3,1,1,1,6,4

This is the original tree from which the traversals were extracted from:

    3
   / \
  9   6
 /   / \
2   1   4
   / \
  1   1

The problem is that the function I wrote can't distinguish the consecutive equal numbers.

This is the function in C:

TREE createTreeFromPreAndIn(int pre[], int in[], int n){
    TREE res;
    res.root = createTreeFromPreAndInHelper(pre, in, n);
    return res;
}

TNODE* createTreeFromPreAndInHelper(int pre[], int in[], int n){
    int index;
    TNODE* rootL, *rootR, *root;

    if (n == 0)
        return NULL;
    else {
        index = findIndex(in, n, pre[0]); //returns the index of the first appearance of pre[0] in 'in'
        rootL = createTreeFromPreAndInHelper(pre+1, in, index);
        rootR = createTreeFromPreAndInHelper(pre+1+index, in+index+1, n-index-1);
        root = createNewTreeNode(pre[0], rootL, rootR);
        return root;
    }
}

Thanks in advance

You dont have enough requirement to identify the exact image. Your tree above can also be expressed as

              Fig 1                       Fig 2                      Fig 3

                3                          3                           3   
               / \                        / \                         / \
              9   6                      9   6                       9   6
             /   / \                    /   / \                     /   / \
            2   1   4                  2   1   4                   2   1   4
               / \                        /                             \
              1   1                      1                               1 
                                        /                               / 
                                       1                               1

All the above tree gives the same In-order and Pre-order Sets as per your initiation.

In-order= { 2,9,3,1,1,1,6,4 }

Pre-order= { 3,9,2,6,1,1,1,4}

There is an ambiguity. So you cant identify the exact tree with this information. You have to specify additional informations to work with this problem.

If you want to recreate it, you can possibly try including boundaries in the array.

For ex: Use -1 to specify no-child(Assuming my node values will not be -1 at any case).

Fig 1:

In-order: {-1,2,-1,9,-1,3,-1,1,-1,1,-1,1,-1,6,-1,4,-1}

Pre-order: {3,9,2,-1,-1,-1,6,1,1,-1,-1,1,-1,-1,4,-1,-1}

Fig 2:

In-order: {-1,2,-1,9,-1,3,-1,1,-1,1,-1,1,-1,6,-1,4,-1}

Pre-order: {3,9,2,-1,-1,-1,6,1,1,1,-1,-1,-1,-1,4,-1,-1}

Obviously Pre-order will change and it can help you to avoid ambiguity and recreate the required structure.

The ambiguity arises from the fact that the leaf nodes are not fully defined. Define a placeholder (0 or -1) for nodes not to be built, and USE parens to show the list's structure.

Pre-order: 3(9(2)(0))(6 (1(1)(1)) (4)) with parens and a placeholder zero without the parens the same sequence of numbers could be "parenthesized" as 3(9(2)(0))(6(1)(1(1)(4))).

The duplicate numbers have nothing to do with the ambiguity. A number does not define its place in the structure. Rather, the fact that this is a pre-order binary tree where each node (ie parent) has, by definition left and right children, but each child can be a parent with two children! Thus the list of elements has to "fill-out" the tree down to its leafs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM