【问题标题】:Adding child to Tree using LinkedLists to store the value of Nodes使用 LinkedLists 将子节点添加到 Tree 以存储节点的值
【发布时间】:2020-06-07 15:24:26
【问题描述】:

好的,所以我想实现一棵树,其中每个节点的子节点的值都存储在 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) {

        }
}

我努力的地方是找到一种方法来将一个孩子添加到这个结构中。我已经使用类似的方式将每个节点的数据存储在 LinkedLists 中寻找网站,但我找不到任何东西。

【问题讨论】:

  • 为什么要把child存放到Node的LinkedList中?
  • 子结构不应该是Tree类型吗?这样您就可以创建递归树数据结构。
  • @sc0der 这是我的大学提供的一个练习,为什么你会像那个 idk 那样做。我还没有找到任何这样做的文章等。
  • @Sid 是的,这是有道理的,不幸的是我必须使用模板并且结构是这样给出的。
  • 那么每个节点都有N个其他节点?

标签: java data-structures tree


【解决方案1】:

给你:

 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();
            }
        }
    }

,来自main 函数的调用

    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();
    }

output

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

【讨论】:

    【解决方案2】:

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

    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);   
        }
    }
    

    函数将如下所示:

    public void addChild(Node parent, Node child) {
        parent.addChild(child);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      相关资源
      最近更新 更多