简体   繁体   English

从Pre和Order构造二叉树

[英]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 预购:3,9,2,6,1,1,1,4
In-Order: 2,9,3,1,1,1,6,4 依序: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: 这是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 } 按顺序= {2,9,3,1,1,1,6,4}

Pre-order= { 3,9,2,6,1,1,1,4} 预购= {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). 例如:使用-1来指定no-child(假设我的节点值在任何情况下都不为-1)。

Fig 1: 图。1:

In-order: {-1,2,-1,9,-1,3,-1,1,-1,1,-1,1,-1,6,-1,4,-1} 依序:{-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} 预购:{3,9,2,-1,-1,-1,6,1,1,-1,-1,1,-1,-1,4,-1,-1}

Fig 2: 图2:

In-order: {-1,2,-1,9,-1,3,-1,1,-1,1,-1,1,-1,6,-1,4,-1} 依序:{-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} 预订:{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. 为将要构建的节点定义一个占位符(0或-1),并使用USE进行表示以显示列表的结构。

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))). 预购:3(9(2)(0))(6(1(1)(1))(4))有括号,占位符为零,没有括号,相同的数字序列可以用“括号”表示为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. 因此,元素列表必须将树“填充”到其叶子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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