二叉树遍历时跳过节点

发布于 2024-12-12 08:41:10 字数 466 浏览 0 评论 0原文

我需要遍历二叉树,跳过满足条件的任何节点的子节点。

这实现了树引导的聚类方法;当子树的叶子共同满足条件时,它们被认为是一个簇。

似乎开始的地方是前序遍历,但我不确定如何修改算法以跳过当前节点的所有子节点。

更新

除了下面两个(正确的)答案之外,还可以使用以下 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 技术交流群。

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

发布评论

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

评论(2

套路撩心 2024-12-19 08:41:10

如果跳过所有子节点不仅意味着直系后代,还意味着它们的整个子树,则可以执行以下操作:

public void traverse(Node n){
    if (n==null)
        return;
    doSomethingWith(n);
    if (!conditionIsMet(n)){
        traverse(n.left);
        traverse(n.right);
    }
}

If by skipping all the children you mean not just direct descendants but their entire subtrees, you can do the following:

public void traverse(Node n){
    if (n==null)
        return;
    doSomethingWith(n);
    if (!conditionIsMet(n)){
        traverse(n.left);
        traverse(n.right);
    }
}
绝對不後悔。 2024-12-19 08:41:10

第一部分很简单:

class Tree {
    Tree(int data) {
        this.data = data;
    }
    Tree left, right;
    Object object;
    int data;
}
public class Main {
    static void myPreorder(Tree tree) {
        if (tree == null) return;
        if (tree.data > 2) {
            System.out.println("skipping " + tree.data);
            return;
        }
        System.out.println("visiting " + tree.data);
        myPreorder(tree.left);
        myPreorder(tree.right);
    }
    public static void main(String[] args) {
        Tree root = new Tree(1);
        root.left = new Tree(2);
        root.right = new Tree(3);
        root.right.left = new Tree(4);
        root.right.right = new Tree(5);
        myPreorder(root);
    }
}

第二部分:“......当子树的叶子共同满足条件时,它们被认为是一个簇......”很难。您需要检查节点的所有叶子节点以查看它们是否满足跳过条件。

the first part is easy:

class Tree {
    Tree(int data) {
        this.data = data;
    }
    Tree left, right;
    Object object;
    int data;
}
public class Main {
    static void myPreorder(Tree tree) {
        if (tree == null) return;
        if (tree.data > 2) {
            System.out.println("skipping " + tree.data);
            return;
        }
        System.out.println("visiting " + tree.data);
        myPreorder(tree.left);
        myPreorder(tree.right);
    }
    public static void main(String[] args) {
        Tree root = new Tree(1);
        root.left = new Tree(2);
        root.right = new Tree(3);
        root.right.left = new Tree(4);
        root.right.right = new Tree(5);
        myPreorder(root);
    }
}

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.

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