迭代后序遍历而不保留已访问标志
为什么需要为迭代后序遍历保留访问标志,而不是为中序或前序迭代遍历保留访问标志。
是否可以在不保留访问标志的情况下进行订单后遍历?
Why is it necessary to keep visited flag for iterative post order traversal and not for inorder or pre order iterative traversal.
Is it possible to do post order traversal without keeping visited flag ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
后序遍历迭代版本可以在不使用访问标志的情况下实现,只是更难实现。
请参阅此处,了解两种不使用任何访问标志的迭代后序遍历解决方案。
http://www.leetcode.com/2010/10 /binary-tree-post-order-traversal.html
Postorder traversal iterative version could be implemented without using visited flags, it is just more difficult to implement.
See here for two solutions for iterative postorder traversal without using any visited flags.
http://www.leetcode.com/2010/10/binary-tree-post-order-traversal.html
如果我们从简单的递归后序算法开始,
我们可以将函数分成“a”、“b”和“c”三部分,然后通过模拟虚拟调用堆栈将其天真地转换为迭代算法
:请注意,堆栈只能通过三种方式之一进行修改:
这意味着:
因此,我们实际上并不需要在堆栈内存储“a”——一个存储“a”的变量就足够了。这使我们能够将简单的迭代算法简化为更传统的形式:
堆栈上的布尔标志是“访问标志”。在此示例中,我们不将标志直接存储在节点上,而是存储在堆栈中,但最终效果是相同的。该算法的某些变体使用“最后访问的节点”变量,但这需要一种方法来比较节点的“身份”(指针/引用相等),我们在这里不假设。
该标志是算法的基本部分,因为正如我们之前的分析中所指出的,堆栈上的“b”和“c”条目可以完全任意的方式出现。我们需要某种信息来告诉我们是否应该向右遍历或调用
f
。If we start with the simple recursive postorder algorithm,
we can chop the function into three parts "a", "b", and "c", and then naively translate it into an iterative algorithm by emulating a virtual call stack:
From this we observe that the stack can only be modified in one of three ways:
This implies that:
Therefore, we don't really need to store "a" inside the stack – a single variable to store "a" would be enough. This allows us to simplify the naive iterative algorithm into the more conventional form:
The boolean flag on the stack is the “visited flag”. In this example, we don’t store the flag directly on the nodes, but within our stack, but the net effect is the same. Some variants of the algorithm use a “last visited node” variable instead, but that requires a way to compare nodes for “identity” (pointer/reference equality), which we do not assume here.
This flag is an essential part of the algorithm because, as noted in our analysis earlier, the "b" and "c" entries on the stack can appear in completely arbitrary ways. We need some kind of information to tell us whether we should traverse rightward or call
f
.这是订单后访问:
它不使用任何标志。您认为为什么需要一面旗帜?
也许我不明白“迭代后序遍历”这句话。
通过对称性,如果你认为“迭代前序遍历”不需要标志,
我认为你必须相信“迭代后序遍历”也是
无标志,反之亦然。
编辑:我的错,一定是深夜了。 “迭代”的意思是“不递归地实现”。
好的,所以您实现了自己的节点堆栈,并且您必须实现相当于返回地址堆栈的内容。我认为flag是模拟return的效果
地址知道接下来是去L1还是L2。由于您需要它来进行后购,因此根据对称性,您也需要它来进行预购。
2010 年 10 月编辑:我又错了,提供的算法不是后序的。修改。
Here's a post-order visit:
It doesn't use any flags. Why do you think a flag is necessary?
Maybe I don't understand the phrase, "iterative post order traversal".
By symmetry, if you think that "iterative preorder traversal" doesn't need a flag,
I contend you'd have to believe "iterative postorder traversal" is also
flag free, and vice-versa.
EDIT: My bad, must be late at night. "Iterative" meaning "implement without recursion".
OK, so you implement your own stack of nodes, and you have to implement what amounts to a stack of return addresses. I think the flag is simulating the effect of the return
address knowing whether to go to L1 or L2 next. And since you need this for post order, by symmetry you need it for pre-order.
EDIT October 2010: My bad again, the algorithm provided wasn't post-order. Revised.
我在用户1337c0d3r的解决方案中发现了一个问题:它只是逆序预购。我的解决方案使用“活动列表”来标记堆栈中的节点。
(您可以将标记标志存储在堆栈中。将单独发布该解决方案。)
I found a problem in the solution of user 1337c0d3r: it is simply a pre-order in reverse order. My solution uses an "active list" to mark the nodes in the stack.
(You could store the mark flag in the stack. Will post that solution separately. )
我相信之前发布的端口顺序遍历的算法是这样的,因为它在访问左子树之前“处理”节点。后序遍历本质上与逆波兰表示法相同,其中操作数(叶节点或子树)位于运算符(下一个更高的子树节点)之前。
正确的后序遍历算法是:
这将在访问子树的根之前访问叶节点或子树节点。
I believe the algorithm show in previous posting for port-order traversal is in as it "processes" the node before visiting the left sub-tree. Postorder Traversal is essentially the same as Reverse Polish Notation in which the operands (leaf nodes or sub-trees) precede the operator (the next higher sub-tree node).
A corrected postorder traversal algorith would be:
This would visit the leaf or sub-tree nodes before visiting the root of the sub-tree.
标志不是必需的,应该避免,因为读者不应该修改任何内容,并且任何修改都不允许并发。 这里是迭代后序的实现C 中的遍历作为宏。它适用于具有正确配置的任何树类型,并且还可以执行反向后序操作。整个库也实现了迭代前序遍历,位于此处。
它可以像这样使用。要获得反向后序,请将
W_REVERSED
重新定义为 1。要更改下一个字段获取操作,请重新定义W_TREE_NEXT(tree,ix)
,对于可变度数树,请重新定义W_TREE_GET_DEGREE(树)
。Flags are not necessary and should be avoided since a reader should not modify anything, and any modification would not allow concurrency, for example. Here is an implementation of an iterative post-order traversal in C as a macro. It works for any tree type with proper configuration, and can do also the reversed post-order. The whole library, which also implements an iterative pre-order traversal, is here.
It can be used like this. To get reversed post-order, redefine
W_REVERSED
to 1. To change the next field fetch operation, redefineW_TREE_NEXT(tree,ix)
and for variadic degree trees, redefineW_TREE_GET_DEGREE(tree)
.