理解从先序遍历构造树的伪代码

发布于 2024-11-05 00:09:17 字数 947 浏览 7 评论 0原文

我需要做一些类似于这个问题中描述的任务:
用给定的前序遍历构造树

有一个非常有用的答案在这里,但我不完全理解伪代码,所以我想知道是否有人可以帮助向我描述发生了什么。

k = 0 // Initialize
input = ... get preorder traversal vector from user ... // Get input
Reconstruct(T) // Reconstruct method with tree input
  if input[k] == N // If element of input is N
    T = new node with label N // Make a new node with label N in tree T
    k = k + 1  // Increment k for next loop (Is this whole thing a loop? or a method call?)
    Reconstruct(T.left) // ?????
    Reconstruct(T.right) // ?????
 else // If element of input is L
    T = new node with label L // Make a new node with label L in tree T
    T.left = T.right = null // ?????
    k = k + 1 // Increment k for next loop

我已经在评论中写下了我对事物的理解,如果有人能够检查我的理解是否正确以及问号位的作用,我将非常感激。另外,这个伪代码是否通过运行输入并在输入中遇到 L 时回溯来创建一棵新树?或者它正在重建现有的二叉树?

I need to do something similar to the task described in this question:
Construct tree with pre-order traversal given

There is a really helpful answer here, but I don't understand the pseudocode fully, so I was wondering if someone could help describe to me what is going on.

k = 0 // Initialize
input = ... get preorder traversal vector from user ... // Get input
Reconstruct(T) // Reconstruct method with tree input
  if input[k] == N // If element of input is N
    T = new node with label N // Make a new node with label N in tree T
    k = k + 1  // Increment k for next loop (Is this whole thing a loop? or a method call?)
    Reconstruct(T.left) // ?????
    Reconstruct(T.right) // ?????
 else // If element of input is L
    T = new node with label L // Make a new node with label L in tree T
    T.left = T.right = null // ?????
    k = k + 1 // Increment k for next loop

I have written my understanding of things in the comments and I'd really appreciate it if someone could check that my understanding is correct, and what the question mark bits are doing. Also, is this pseudocode making a new tree by running through the input and backtracking whenever an L is encountered in the input? Or is it reconstructing an existing binary tree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2024-11-12 00:09:17

没有循环。 k 是一个全局范围的变量,可以在 Reconstruct(T) 中访问。它只是字符数组(输入字符串)的当前索引。

正如您引用的问题中所解释的(使用前序遍历构造树),正确的算法是先处理节点的左子节点,然后再处理右子节点,这就是您在iftrue 部分。如果当前节点恰好是叶子L,则不要为其提供子节点并返回到调用函数。

此函数的作用是沿着树的左边缘,向所有 N 节点添加子节点,并且在字符串结束之前不向所有 L 节点(也称为叶子)添加子节点。

编辑:当伪代码的作者说 T.left = T.right = null 时,这意味着此时当前节点没有左或右子节点(因为它是叶子)。这只是一个断言,不一定需要出现在代码中。

There is no loop. k is a globally-scoped variable which can be accessed within Reconstruct(T). It is simply the current index of the character-array (the input string).

As explained in the question you referenced (Contsruct Tree with Pre-Order Traversal), The proper algorithm is to do the left-child of a node, then the right-child Which is what you see in the true section of the if. If the current node happens to be a leaf, L, then do not give it children and return to the calling function.

What this function does is follows the left edge of the tree, adding children to all N nodes and not adding children to all L nodes (aka leaves) until the string finishes.

Edit: When the author of the pseudocode says T.left = T.right = null, it means that at this point, the current node has no left or right child (because it is a leaf). This is just an assertion and does not necessarily need to be in the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文