简体   繁体   English

当给出的唯一信息是后序遍历时,如何构建严格的二叉树?

[英]How can I construct a strict binary tree when the only information given is post order traversal?

Previously I asked how to get the pre-order of a tree when I am only given post-order traversal.以前我问过当我只获得后序遍历时如何获得树的前序。 However, now I'm curious as to how one would build a strict binary tree (strict binary tree meaning a node either has two children or no children) when only given post order traversal.但是,现在我很好奇在仅给出后序遍历时如何构建严格二叉树(严格二叉树意味着一个节点有两个孩子或没有孩子)。 Some example data I am dealing with :我正在处理的一些示例数据:

7 8 2 // 8,2 are dimensions stored in the node, likewise for other nodes)
6 4 8
3 1 5
A
B

I know what the tree is supposed to look like:我知道这棵树应该是什么样子:

               B
            /     \
           7       A
                 /    \
                6      3

But I am lost on how to write a function that would actually build this tree (and store in the information in the nodes) from the information given (post order traveral.) How can I do this?但是我不知道如何根据给定的信息(后序遍历)编写一个函数来实际构建这棵树(并存储在节点中的信息中)。我该怎么做?

Creating a full binary tree from the postorder is like creating a syntax tree from Reverse Polish Notation with only binary operators.从后序创建一个完整的二叉树就像从逆波兰表示法创建一个只有二元运算符的语法树。 The branches are the operators, the leaves are the values.分支是操作符,叶子是值。 (You must be able to tell which is which. In your axample, brenches are letters, leaves are numbers.) (你必须能够分辨哪个是哪个。在你的例子中,brenches 是字母,叶子是数字。)

You can process the data as it comes.您可以随时处理数据。 If it is a leaf, create a node and push it on the stack.如果是叶子,则创建一个节点并将其压入堆栈。 It is is a branch, create a node, pop its right and left branches off the stack and push the new node.它是一个分支,创建一个节点,将其左右分支从堆栈中弹出并推送新节点。 If the received data is valid, you should have a single node on the stack.如果接收到的数据有效,则堆栈上应该只有一个节点。 That's the root of your tree.那是你树的根。

So, in pseudo-ish C code:因此,在伪 ish C 代码中:

Node *head = NULL;

while (!stream_empty()) {
    int data = stream_get();

    if (is_leaf(data)) {
        if (nstack == MAX) fatal("Stack overflow!");

        stack_push(make_node(data));
    } else {
        if (stack_size < 2) fatal("Stack underflow!");

        Node *node = make_node(data);

        node->right = stack_pop();
        node->left = stack_pop();
        stack_push(node);
    }
}

if (nstack != 1) fatal("Ill-formed full binary tree!");

head = stack[0];

Stack overflow occurs when the tree is deeper than the stack size.当树比堆栈大小更深时,就会发生堆栈溢出。 Stack underflow or leftover nodes at the end occur when the input data is ill-formed.当输入数据格式错误时,最后会出现堆栈下溢或剩余节点。

Here's a full working example on ideone .这是关于 ideone的完整工作示例

[ Note : I've completely rewritten my answer, because the OP has specified new requirements. [注意:我已经完全重写了我的答案,因为 OP 指定了新的要求。 I had also based my original answer of a answer I gave to OP's previous question.我还基于我对 OP 上一个问题给出的答案的原始答案。 I think that the present approach is more elegant.我认为目前的方法更优雅。 Whatever that means.不管是什么意思。 :) ] :) ]

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

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