Tree implementation in Java (root, parents and children)

前端 未结 8 2173
挽巷
挽巷 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条回答
  •  萌比男神i
    2020-11-30 20:51

    Since @Jonathan's answer still consisted of some bugs, I made an improved version. I overwrote the toString() method for debugging purposes, be sure to change it accordingly to your data.

    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Provides an easy way to create a parent-->child tree while preserving their depth/history.
     * Original Author: Jonathan, https://stackoverflow.com/a/22419453/14720622
     */
    public class TreeNode {
        private final List> children;
        private TreeNode parent;
        private T data;
        private int depth;
    
        public TreeNode(T data) {
            // a fresh node, without a parent reference
            this.children = new ArrayList<>();
            this.parent = null;
            this.data = data;
            this.depth = 0; // 0 is the base level (only the root should be on there)
        }
    
        public TreeNode(T data, TreeNode parent) {
            // new node with a given parent
            this.children = new ArrayList<>();
            this.data = data;
            this.parent = parent;
            this.depth = (parent.getDepth() + 1);
            parent.addChild(this);
        }
    
        public int getDepth() {
            return this.depth;
        }
    
        public void setDepth(int depth) {
            this.depth = depth;
        }
    
        public List> getChildren() {
            return children;
        }
    
        public void setParent(TreeNode parent) {
            this.setDepth(parent.getDepth() + 1);
            parent.addChild(this);
            this.parent = parent;
        }
    
        public TreeNode getParent() {
            return this.parent;
        }
    
        public void addChild(T data) {
            TreeNode child = new TreeNode<>(data);
            this.children.add(child);
        }
    
        public void addChild(TreeNode child) {
            this.children.add(child);
        }
    
        public T getData() {
            return this.data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    
        public boolean isRootNode() {
            return (this.parent == null);
        }
    
        public boolean isLeafNode() {
            return (this.children.size() == 0);
        }
    
        public void removeParent() {
            this.parent = null;
        }
    
        @Override
        public String toString() {
            String out = "";
            out += "Node: " + this.getData().toString() + " | Depth: " + this.depth + " | Parent: " + (this.getParent() == null ? "None" : this.parent.getData().toString()) + " | Children: " + (this.getChildren().size() == 0 ? "None" : "");
            for(TreeNode child : this.getChildren()) {
                out += "\n\t" + child.getData().toString() + " | Parent: " + (child.getParent() == null ? "None" : child.getParent().getData());
            }
            return out;
        }
    }
    

    And for the visualization:

    import model.TreeNode;
    
    /**
     * Entrypoint
     */
    public class Main {
        public static void main(String[] args) {
            TreeNode rootNode = new TreeNode<>("Root");
            TreeNode firstNode = new TreeNode<>("Child 1 (under Root)", rootNode);
            TreeNode secondNode = new TreeNode<>("Child 2 (under Root)", rootNode);
            TreeNode thirdNode = new TreeNode<>("Child 3 (under Child 2)", secondNode);
            TreeNode fourthNode = new TreeNode<>("Child 4 (under Child 3)", thirdNode);
            TreeNode fifthNode = new TreeNode<>("Child 5 (under Root, but with a later call)");
            fifthNode.setParent(rootNode);
    
            System.out.println(rootNode.toString());
            System.out.println(firstNode.toString());
            System.out.println(secondNode.toString());
            System.out.println(thirdNode.toString());
            System.out.println(fourthNode.toString());
            System.out.println(fifthNode.toString());
            System.out.println("Is rootNode a root node? - " + rootNode.isRootNode());
            System.out.println("Is firstNode a root node? - " + firstNode.isRootNode());
            System.out.println("Is thirdNode a leaf node? - " + thirdNode.isLeafNode());
            System.out.println("Is fifthNode a leaf node? - " + fifthNode.isLeafNode());
        }
    }
    

    Example output:

    Node: Root | Depth: 0 | Parent: None | Children: 
        Child 1 (under Root) | Parent: Root
        Child 2 (under Root) | Parent: Root
        Child 5 (under Root, but with a later call) | Parent: Root
    Node: Child 1 (under Root) | Depth: 1 | Parent: Root | Children: None
    Node: Child 2 (under Root) | Depth: 1 | Parent: Root | Children: 
        Child 3 (under Child 2) | Parent: Child 2 (under Root)
    Node: Child 3 (under Child 2) | Depth: 2 | Parent: Child 2 (under Root) | Children: 
        Child 4 (under Child 3) | Parent: Child 3 (under Child 2)
    Node: Child 4 (under Child 3) | Depth: 3 | Parent: Child 3 (under Child 2) | Children: None
    Node: Child 5 (under Root, but with a later call) | Depth: 1 | Parent: Root | Children: None
    Is rootNode a root node? - true
    Is firstNode a root node? - false
    Is thirdNode a leaf node? - false
    Is fifthNode a leaf node? - true
    

提交回复
热议问题