How to implement a tree data-structure in Java?

前端 未结 24 2520
鱼传尺愫
鱼传尺愫 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:50

    What about this?

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashMap;
    
    /**
      * @author ycoppel@google.com (Yohann Coppel)
      * 
      * @param 
      *          Object's type in the tree.
    */
    public class Tree {
    
      private T head;
    
      private ArrayList> leafs = new ArrayList>();
    
      private Tree parent = null;
    
      private HashMap> locate = new HashMap>();
    
      public Tree(T head) {
        this.head = head;
        locate.put(head, this);
      }
    
      public void addLeaf(T root, T leaf) {
        if (locate.containsKey(root)) {
          locate.get(root).addLeaf(leaf);
        } else {
          addLeaf(root).addLeaf(leaf);
        }
      }
    
      public Tree addLeaf(T leaf) {
        Tree t = new Tree(leaf);
        leafs.add(t);
        t.parent = this;
        t.locate = this.locate;
        locate.put(leaf, t);
        return t;
      }
    
      public Tree setAsParent(T parentRoot) {
        Tree t = new Tree(parentRoot);
        t.leafs.add(this);
        this.parent = t;
        t.locate = this.locate;
        t.locate.put(head, this);
        t.locate.put(parentRoot, t);
        return t;
      }
    
      public T getHead() {
        return head;
      }
    
      public Tree getTree(T element) {
        return locate.get(element);
      }
    
      public Tree getParent() {
        return parent;
      }
    
      public Collection getSuccessors(T root) {
        Collection successors = new ArrayList();
        Tree tree = getTree(root);
        if (null != tree) {
          for (Tree leaf : tree.leafs) {
            successors.add(leaf.head);
          }
        }
        return successors;
      }
    
      public Collection> getSubTrees() {
        return leafs;
      }
    
      public static  Collection getSuccessors(T of, Collection> in) {
        for (Tree tree : in) {
          if (tree.locate.containsKey(of)) {
            return tree.getSuccessors(of);
          }
        }
        return new ArrayList();
      }
    
      @Override
      public String toString() {
        return printTree(0);
      }
    
      private static final int indent = 2;
    
      private String printTree(int increment) {
        String s = "";
        String inc = "";
        for (int i = 0; i < increment; ++i) {
          inc = inc + " ";
        }
        s = inc + head;
        for (Tree child : leafs) {
          s += "\n" + child.printTree(increment + indent);
        }
        return s;
      }
    }
    

提交回复
热议问题