How to implement a tree data-structure in Java?

前端 未结 24 2492
鱼传尺愫
鱼传尺愫 2020-11-22 00:27

Is there any standard Java library class to represent a tree in Java?

Specifically I need to represent the following:

  • The sub-tree at any node can have
24条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 00:52

    For example :

    import java.util.ArrayList;
    import java.util.List;
    
    
    
    /**
     * 
     * @author X2
     *
     * @param 
     */
    public class HisTree 
    {
        private Node root;
    
        public HisTree(T rootData) 
        {
            root = new Node();
            root.setData(rootData);
            root.setChildren(new ArrayList>());
        }
    
    }
    
    class Node 
    {
    
        private T data;
        private Node parent;
        private List> children;
    
        public T getData() {
            return data;
        }
        public void setData(T data) {
            this.data = data;
        }
        public Node getParent() {
            return parent;
        }
        public void setParent(Node parent) {
            this.parent = parent;
        }
        public List> getChildren() {
            return children;
        }
        public void setChildren(List> children) {
            this.children = children;
        }
    }
    

提交回复
热议问题