简体   繁体   English

迭代Java链表的n级

[英]Iterate n-levels of linked lists Java

I have a tree with nodes, and each node contains a list with nodes, and each node in those lists contain nodes... It can be 10 levels deep, with around 100 node objects. 我有一棵带有节点的树,每个节点包含一个带有节点的列表,并且这些列表中的每个节点都包含节点...它的深度可以是10层,大约有100个节点对象。

I've tried to iterate to find all individual node objects but most guides covers binary trees.. It's just overwhelming. 我试图迭代查找所有单个节点对象,但是大多数指南涵盖了二叉树。 If anyone has some pseudo code or can point me in the right direction I would be very glad. 如果有人有伪代码或可以指出正确的方向,我将非常高兴。 Classes below (sorry about the bad formatting): 下面的类(对错误的格式表示抱歉):

Node 节点

public class Node {
    public String text;
    public LinkedList<Node> nodes;

    public Node() {
        this.nodes = new LinkedList<Node>();
    }

    public Node(String data) {
        this();
        this.text = data;
    }
}

Tree

public class Tree {
    public LinkedList<Node> nodes;
    public String text;

    public Tree() {
        nodes = new LinkedList<Node>();
    }

    public Tree(String data) {
        this();
        this.text = data;
    }
}

Program 程序

public class Main {

    public static void main(String[] args) {

        LinkedList<Tree> trees = new LinkedList<Tree>();

        trees.add(new Tree("Root 1"));

        trees.get(0).nodes.add(new Node("Node 1"));
        trees.get(0).nodes.add(new Node("Node 2"));
        trees.get(0).nodes.add(new Node("Node 3"));

        trees.get(0).nodes.get(0).nodes.add(new Node("Node 1:Child 1"));
        trees.get(0).nodes.get(1).nodes.add(new Node("Node 2:Child 1"));
        trees.get(0).nodes.get(2).nodes.add(new Node("Node 3:Child 1"));

        for (Tree tree : trees) {
            tree.nodes.. // find all node-objects
        }
    }
}

This can be done by recursion. 这可以通过递归来完成。 Since you have maximum depth of 10 and max elements = 100, recursion should not give any performance issues. 由于您的最大深度为10,最大元素= 100,因此递归不会带来任何性能问题。 Sample code to do this: 执行此操作的示例代码:

List<Node> flattenTree(List<Node> nodes){
    List<Node> allNodes=new LinkedList<>(nodes) ;
    for(Node node:nodes){
        {
           if(node.getNodes()!=null)
           allNodes.addAll(flattenTree(node.getNodes()));
        } 
     return allNodes;
    }

Call this using: 使用以下命令进行调用:

List<Node> allNodes = flattenTree(tree.getNodes);

allNodes will contain all the nodes of this tree. allNodes将包含此树的所有节点。

You can do this by using recursion: 您可以使用递归来做到这一点:

interface TreeOp {
    void apply(Tree tree);
}

... ...

void traverse(Tree tree, TreeOp op) {
    op.apply(tree);
    for (Tree child: tree.nodes) {
        traverse(child, op);
    }
}

... ...

for (Tree tree : trees) {
    traverse(tree, (tree) -> {
       doSomethingWith(tree);
    });
}

UPDATE: 更新:

Actually, you can use the Consumer interface instead of defining a new one: 实际上,您可以使用Consumer接口而不是定义一个新接口:

void traverse(Tree tree, Consumer<Tree> op) {
    op.accept(tree);
    for (Tree child: tree.nodes) {
        traverse(child, op);
    }
}

UPDATE 2: 更新2:

And traverse could be a method of Tree: 遍历可能是Tree的一种方法:

void traverse(Consumer<Tree> op) {
    op.accept(this);
    for (Tree child: nodes) {
        traverse(child, op);
    }
}

Just out of curiosity, what kind of problem would require a data structure like this? 出于好奇,什么样的问题需要这样的数据结构?

Something like this in main should set you on the right track: main类似内容应使您走上正确的轨道:

// set up the master list of all nodes and the list of unvisited nodes
// with the base tree's node.
List<Node> allTheNodes = new LinkedList<>();
allTheNodes.add(tree.node);
List<Node> unvisited = new LinkedList<>();
unvisited.add(tree.node);

while (!unvisited.isEmpty())
{
    // while our unvisited list is not empty, visit the first node in the list
    // and add all of its subnodes to both the list of nodes to visit and the master list of nodes.
    Node currentNode = unvisited.get(0);
    allTheNodes.addAll(currentNode.nodes);
    unvisited.addAll(currentNode.nodes);
    unvisited.remove(currentNode);
}   

// now, allTheNodes should have all of your nodes in it.

You just need to track the depth of the nodes: 您只需要跟踪节点的深度:

LinkedList<Node> currentNodes = new LinkedList<Node>();
currentNodes.add(root);
int depth = 0;
while depth < 10 {
    depth++;
    int size = currentNodes.size();
    for (int i = 0; i < size; i++) {
        Node current = currentNodes.removeFirst();
        //Do something with current
        for (int j = 0; j < current.nodes.size(); j++) {
            currentNodes.addLast(current.nodes.get(j));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM