返回节点列表 Java-Parent 中的树可以有多个子节点

发布于 2024-12-13 04:07:01 字数 756 浏览 1 评论 0原文

我正在尝试编写java代码来返回树中的节点列表。 树看起来像 this

Node 类是

class Node{
 String label;
 List<Node> children;
}

我正在尝试这种方式。但无法理解如何编写循环来遍历。

    public List<Node> returnAllNodes(Node node){
    List<Node> listOfNodes = 
        new ArrayList<Node>();
    boolean iterationCompleted = false;
    if(node==null){
        return null;
    }
    while(!iterationCompleted){
    if(node.getChildren()==null){
        listOfNodes.add(node);
                    break;    
    }
            else{
               //
            }
    }
    return null;
    //return traverseAndReturnAllNodes(node.getChildren().get(0));
}

请帮忙。

I am trying to write java code to return list of Nodes in a tree.
The tree looks like this

Node class is

class Node{
 String label;
 List<Node> children;
}

I am trying this way. But not able to understand how to write a loop to traverse.

    public List<Node> returnAllNodes(Node node){
    List<Node> listOfNodes = 
        new ArrayList<Node>();
    boolean iterationCompleted = false;
    if(node==null){
        return null;
    }
    while(!iterationCompleted){
    if(node.getChildren()==null){
        listOfNodes.add(node);
                    break;    
    }
            else{
               //
            }
    }
    return null;
    //return traverseAndReturnAllNodes(node.getChildren().get(0));
}

Please help.

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

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

发布评论

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

评论(3

白馒头 2024-12-20 04:07:01

如果您确定该结构是一棵树(一个节点不能有多个父级),这将以深度优先顺序列出节点:

public static List<Node> returnAllNodes(Node node){
    List<Node> listOfNodes = new ArrayList<Node>();
    addAllNodes(node, listOfNodes);
    return listOfNodes;
}

private static void addAllNodes(Node node, List<Node> listOfNodes) {
    if (node != null) {
        listOfNodes.add(node);
        List<Node> children = node.getChildren();
        if (children != null) {
            for (Node child: children) {
                addAllNodes(child, listOfNodes);
            }
        }
    }
}

的第一行更改为:

    if (node != null && !listOfNodes.contains(node)) {

如果节点可以有多个父级,请将addAllNodes -第一个算法是这样的:

public static List<Node> returnAllNodes(Node node){
    List<Node> listOfNodes = new ArrayList<Node>();
    if (node != null) {
        listOfNodes.add(node);
        for(int i = 0; i < listOfNodes.size(); ++i) {
            Node n = listOfNodes.get(i);
            List<Node> children = n.getChildren();
            if (children != null) {
                for (Node child: children) {
                    if (!listOfNodes.contains(child)) {
                        listOfNodes.add(child);
                    }
                }
            }
        }
    }
    return listOfNodes;
}

If you're certain that the structure is a tree (a node cannot have more than one parent), this will list the nodes in depth-first order:

public static List<Node> returnAllNodes(Node node){
    List<Node> listOfNodes = new ArrayList<Node>();
    addAllNodes(node, listOfNodes);
    return listOfNodes;
}

private static void addAllNodes(Node node, List<Node> listOfNodes) {
    if (node != null) {
        listOfNodes.add(node);
        List<Node> children = node.getChildren();
        if (children != null) {
            for (Node child: children) {
                addAllNodes(child, listOfNodes);
            }
        }
    }
}

If nodes can have several parents, change the first line of addAllNodes to:

    if (node != null && !listOfNodes.contains(node)) {

The breadth-first algorithm goes like this:

public static List<Node> returnAllNodes(Node node){
    List<Node> listOfNodes = new ArrayList<Node>();
    if (node != null) {
        listOfNodes.add(node);
        for(int i = 0; i < listOfNodes.size(); ++i) {
            Node n = listOfNodes.get(i);
            List<Node> children = n.getChildren();
            if (children != null) {
                for (Node child: children) {
                    if (!listOfNodes.contains(child)) {
                        listOfNodes.add(child);
                    }
                }
            }
        }
    }
    return listOfNodes;
}
三寸金莲 2024-12-20 04:07:01

作为 Maurice Perry 对基于 DFS 的方法的答案的增强。如果单独需要子节点(因为父节点已知),则不必总是将节点添加到 listOfNodes 中,而是可以单独将子节点添加为

public static List<Node> returnAllNodes(Node node){
    List<Node> listOfNodes = new ArrayList<Node>();
    addAllNodes(node, listOfNodes);
    return listOfNodes;
}

private static void addAllNodes(Node node, List<Node> listOfNodes) {
    if (node != null) {
        List<Node> children = node.getChildren();
        if (children != null) {
            for (Node child: children) {
                listOfNodes.add(child);
                addAllNodes(child, listOfNodes);
            }
        }
    }
}

As a enhancement to Maurice Perry's answer for DFS based approach. In case child nodes are alone needed (as parent node is already known), then instead of adding the node always to listOfNodes, child node can alone be added as

public static List<Node> returnAllNodes(Node node){
    List<Node> listOfNodes = new ArrayList<Node>();
    addAllNodes(node, listOfNodes);
    return listOfNodes;
}

private static void addAllNodes(Node node, List<Node> listOfNodes) {
    if (node != null) {
        List<Node> children = node.getChildren();
        if (children != null) {
            for (Node child: children) {
                listOfNodes.add(child);
                addAllNodes(child, listOfNodes);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文