Java 使用LinkedList将子节点添加到树以存储节点的值

Java 使用LinkedList将子节点添加到树以存储节点的值,java,data-structures,tree,Java,Data Structures,Tree,好的,我想实现一个树,其中每个节点的子节点的值存储在LinkedList中。到目前为止,我有以下代码: class Tree { class Node { int value; LinkedList<Node> children = null; Node(int value) { this.value = value; }

好的,我想实现一个树,其中每个节点的子节点的值存储在LinkedList中。到目前为止,我有以下代码:

class Tree {

        class Node {
            int value;
            LinkedList<Node> children = null;

              Node(int value) {
                  this.value = value;
              }
        }

        public Node root = null;

        public void setRoot(Node root) {
            root = this.root;
        }

        public Node getRoot() {
            return root;
        }

        public void addChild(Node parent, Node child) {

        }
}
类树{
类节点{
int值;
LinkedList子项=null;
节点(int值){
这个值=值;
}
}
公共节点根=空;
公共void setRoot(节点根){
root=this.root;
}
公共节点getRoot(){
返回根;
}
public void addChild(节点父节点、节点子节点){
}
}

我挣扎的地方是找到一种方法,在这个结构中增加一个孩子。我在LinkedList中查找了使用类似方法存储每个节点数据的站点,但找不到任何内容。

我认为这段代码应该可以工作:

class Node {
    int value;
    LinkedList<Node> children = null;

    Node(int value) {
        this.value = value;
        this.children = new LinkedList<Node>();
    }

    public void addChild(Node child) {
        this.children.add(child);   
    }
}
给你:

 static class Tree {
        static class Node {

            int value;
            LinkedList<Node> children;

            Node(int value) {
                this.value = value;
                this.children = new LinkedList<>();
            }

            // Override equals to detect node equality based on the value
            @Override
            public boolean equals(Object o) {
                if (this == o) {
                    return true;
                }
                if (o == null || getClass() != o.getClass()) {
                    return false;
                }

                Node node = (Node) o;

                return value == node.value;
            }

            @Override
            public int hashCode() {
                return value;
            }

            @Override
            public String toString() {
                return value + "";
            }
        }

        public Node root = null;

        // Assign the root like this not like yours which was not correct
        public void setRoot(Node root) {
            this.root = root;
        }

        public Node getRoot() {
            return root;
        }

        public void addChild(Node parent, Node child) {
            // Check if the root is null and parent not null then add child to the root
            if (root == null && parent != null) {
                root = parent;
                root.children.add(child);
                return;
            }

            // if the parent equals root then add to the root's child
            if (parent.equals(root)) {
                root.children.add(child);
                return;
            }

            // add recusively
            addRecur(root, parent, child);
        }

        private void addRecur(Node parent, Node p, Node child) {
            // base condition to the recursion
            if (parent == null) {
                return;
            }

            // if the parent equals to p then add to child
            if (parent.equals(p)) {
                parent.children.add(child);
                return;
            }

            // loop over every child and check if equals p if not do recursion
            for (Node node : parent.children) {
                if (node.equals(p)) {
                    node.children.add(child);
                    return;
                }
                addRecur(node, p, child);
            }
        }

        // print the tree
        public void print() {
            ArrayDeque<Node> queue = new ArrayDeque<>();
            queue.add(root);
            while (!queue.isEmpty()) {
                Node current = queue.poll();
                if (!current.children.isEmpty())
                    System.out.print("Parent: " + current + ", child: ");

                for (Node node : current.children) {
                    if (!queue.contains(node)) {
                        System.out.print(node + " ");
                        queue.add(node);
                    }
                }

                if (!current.children.isEmpty())
                    System.out.println();
            }
        }
    }
输出

Parent: 1, child: 2 3 4 5
Parent: 5, child: 6 7
Parent: 6, child: 8 9 15
Parent: 9, child: 11 10 12

为什么要将子节点存储到节点的LinkedList中?子结构不应该是
树的类型吗?这样您就可以创建一个递归树数据结构了。@SC0这是我所在大学的一个练习,您为什么要这样做呢。我还没有找到任何文章等这样做。@Sid是的,这是有意义的,不幸的是,我必须使用tempelate,结构是这样给出的。那么每个节点都有N个其他节点?
    static public void main(String[] args) {
        Tree tree = new Tree();
        Tree.Node root = new Tree.Node(1);
        tree.addChild(root, new Tree.Node(2));
        tree.addChild(root, new Tree.Node(3));
        tree.addChild(root, new Tree.Node(4));
        tree.addChild(root, new Tree.Node(5));

        tree.addChild(new Tree.Node(5), new Tree.Node(6));
        tree.addChild(new Tree.Node(5), new Tree.Node(7));

        tree.addChild(new Tree.Node(6), new Tree.Node(8));
        tree.addChild(new Tree.Node(6), new Tree.Node(9));
        tree.addChild(new Tree.Node(6), new Tree.Node(15));

        tree.addChild(new Tree.Node(9), new Tree.Node(11));
        tree.addChild(new Tree.Node(9), new Tree.Node(10));
        tree.addChild(new Tree.Node(9), new Tree.Node(12));

        tree.print();
    }
Parent: 1, child: 2 3 4 5
Parent: 5, child: 6 7
Parent: 6, child: 8 9 15
Parent: 9, child: 11 10 12