Tree implementation in Java (root, parents and children)

前端 未结 8 2161
挽巷
挽巷 2020-11-30 20:05

I need to create a tree structure similar as the attached image in Java. I\'ve found some questions related to this one but I haven\'t found a convincing and well explained

8条回答
  •  [愿得一人]
    2020-11-30 20:27

    In the accepted answer

    public Node(T data, Node parent) {
        this.data = data;
        this.parent = parent;
    }
    

    should be

    public Node(T data, Node parent) {
        this.data = data;
        this.setParent(parent);
    }
    

    otherwise the parent does not have the child in its children list

提交回复
热议问题