二叉树遍历时跳过节点
我需要遍历二叉树,跳过满足条件的任何节点的子节点。
这实现了树引导的聚类方法;当子树的叶子共同满足条件时,它们被认为是一个簇。
似乎开始的地方是前序遍历,但我不确定如何修改算法以跳过当前节点的所有子节点。
更新
除了下面两个(正确的)答案之外,还可以使用以下 Java 库:
- MyArch TreeIter - 通用(带有适配器类)树遍历,具有子跳过和动态最大遍历深度
- Phylosoft Forester - 使用 getAllExternalDescendants 和 Newick 到 XML 转换器的树实现
I need to traverse a binary tree, skipping the children of any node for which a condition is met.
This implements a tree-guided clustering approach; the leaves of a subtree are considered a cluster when they collectively meet the condition.
It seems like the place to start would be pre-order traversal, but I'm not sure how to modify the algorithm to skip all the children of the current node.
Update
In addition to the two (correct) answers below, one can use the following Java libraries:
- MyArch TreeIter - Generic (with adapter class) tree traversal, with child skipping and dynamic maximum traversal depth
- Phylosoft Forester - Tree implementation with
getAllExternalDescendants
and a Newick-to-XML converter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果跳过所有子节点不仅意味着直系后代,还意味着它们的整个子树,则可以执行以下操作:
If by skipping all the children you mean not just direct descendants but their entire subtrees, you can do the following:
第一部分很简单:
第二部分:“......当子树的叶子共同满足条件时,它们被认为是一个簇......”很难。您需要检查节点的所有叶子节点以查看它们是否满足跳过条件。
the first part is easy:
the second part: " ... the leaves of a subtree are considered a cluster when they collectively meet the condition. ..." is hard. you would need to examine all of the leaves of a node to see that they met the skip condition.