理解从先序遍历构造树的伪代码
我需要做一些类似于这个问题中描述的任务:
用给定的前序遍历构造树
有一个非常有用的答案在这里,但我不完全理解伪代码,所以我想知道是否有人可以帮助向我描述发生了什么。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有循环。
k
是一个全局范围的变量,可以在Reconstruct(T)
中访问。它只是字符数组(输入字符串)的当前索引。正如您引用的问题中所解释的(使用前序遍历构造树),正确的算法是先处理节点的左子节点,然后再处理右子节点,这就是您在
if
的true
部分。如果当前节点恰好是叶子L
,则不要为其提供子节点并返回到调用函数。此函数的作用是沿着树的左边缘,向所有
N
节点添加子节点,并且在字符串结束之前不向所有L
节点(也称为叶子)添加子节点。编辑:当伪代码的作者说
T.left = T.right = null
时,这意味着此时当前节点没有左或右子节点(因为它是叶子)。这只是一个断言,不一定需要出现在代码中。There is no loop.
k
is a globally-scoped variable which can be accessed withinReconstruct(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 theif
. 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 allL
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.