How to implement a tree data-structure in Java?

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

    I wrote a small "TreeMap" class based on "HashMap" that supports adding paths:

    import java.util.HashMap;
    import java.util.LinkedList;
    
    public class TreeMap<T> extends LinkedHashMap<T, TreeMap<T>> {
    
        public void put(T[] path) {
            LinkedList<T> list = new LinkedList<>();
            for (T key : path) {
                list.add(key);
            }
            return put(list);
        }
    
        public void put(LinkedList<T> path) {
            if (path.isEmpty()) {
                return;
            }
            T key = path.removeFirst();
            TreeMap<T> val = get(key);
            if (val == null) {
                val = new TreeMap<>();
                put(key, val);
            }
            val.put(path);
        }
    
    }
    

    It can be use to store a Tree of things of type "T" (generic), but does not (yet) support storing extra data in it's nodes. If you have a file like this:

    root, child 1
    root, child 1, child 1a
    root, child 1, child 1b
    root, child 2
    root, child 3, child 3a
    

    Then you can make it a tree by executing:

    TreeMap<String> root = new TreeMap<>();
    Scanner scanner = new Scanner(new File("input.txt"));
    while (scanner.hasNextLine()) {
      root.put(scanner.nextLine().split(", "));
    }
    

    And you will get a nice tree. It should be easy to adapt to your needs.

    0 讨论(0)
  • 2020-11-22 00:49

    In the past I have just used a nested map for this. This is what I use today, it is very simple but it fits my needs. Maybe this will help another one.

    import com.fasterxml.jackson.annotation.JsonValue;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.TreeMap;
    
    /**
     * Created by kic on 16.07.15.
     */
    public class NestedMap<K, V> {
        private final Map root = new HashMap<>();
    
        public NestedMap<K, V> put(K key) {
            Object nested = root.get(key);
    
            if (nested == null || !(nested instanceof NestedMap)) root.put(key, nested = new NestedMap<>());
            return (NestedMap<K, V>) nested;
        }
    
        public Map.Entry<K,V > put(K key, V value) {
            root.put(key, value);
    
            return (Map.Entry<K, V>) root.entrySet().stream().filter(e -> ((Map.Entry) e).getKey().equals(key)).findFirst().get();
        }
    
        public NestedMap<K, V> get(K key) {
            return (NestedMap<K, V>) root.get(key);
        }
    
        public V getValue(K key) {
            return (V) root.get(key);
        }
    
        @JsonValue
        public Map getRoot() {
            return root;
        }
    
        public static void main(String[] args) throws Exception {
            NestedMap<String, Integer> test = new NestedMap<>();
            test.put("a").put("b").put("c", 12);
            Map.Entry<String, Integer> foo = test.put("a").put("b").put("d", 12);
            test.put("b", 14);
            ObjectMapper mapper = new ObjectMapper();
            System.out.println(mapper.writeValueAsString(test));
    
            foo.setValue(99);
            System.out.println(mapper.writeValueAsString(test));
    
            System.out.println(test.get("a").get("b").getValue("d"));
        }
    }
    
    0 讨论(0)
  • 2020-11-22 00:50

    There is actually a pretty good tree structure implemented in the JDK.

    Have a look at javax.swing.tree, TreeModel, and TreeNode. They are designed to be used with the JTreePanel but they are, in fact, a pretty good tree implementation and there is nothing stopping you from using it with out a swing interface.

    Note that as of Java 9 you may wish not to use these classes as they will not be present in the 'Compact profiles'.

    0 讨论(0)
  • 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 <T>
      *          Object's type in the tree.
    */
    public class Tree<T> {
    
      private T head;
    
      private ArrayList<Tree<T>> leafs = new ArrayList<Tree<T>>();
    
      private Tree<T> parent = null;
    
      private HashMap<T, Tree<T>> locate = new HashMap<T, Tree<T>>();
    
      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<T> addLeaf(T leaf) {
        Tree<T> t = new Tree<T>(leaf);
        leafs.add(t);
        t.parent = this;
        t.locate = this.locate;
        locate.put(leaf, t);
        return t;
      }
    
      public Tree<T> setAsParent(T parentRoot) {
        Tree<T> t = new Tree<T>(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<T> getTree(T element) {
        return locate.get(element);
      }
    
      public Tree<T> getParent() {
        return parent;
      }
    
      public Collection<T> getSuccessors(T root) {
        Collection<T> successors = new ArrayList<T>();
        Tree<T> tree = getTree(root);
        if (null != tree) {
          for (Tree<T> leaf : tree.leafs) {
            successors.add(leaf.head);
          }
        }
        return successors;
      }
    
      public Collection<Tree<T>> getSubTrees() {
        return leafs;
      }
    
      public static <T> Collection<T> getSuccessors(T of, Collection<Tree<T>> in) {
        for (Tree<T> tree : in) {
          if (tree.locate.containsKey(of)) {
            return tree.getSuccessors(of);
          }
        }
        return new ArrayList<T>();
      }
    
      @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<T> child : leafs) {
          s += "\n" + child.printTree(increment + indent);
        }
        return s;
      }
    }
    
    0 讨论(0)
  • 2020-11-22 00:52

    I wrote a little library that handles generic trees. It's much more lightweight than the swing stuff. I also have a maven project for it.

    0 讨论(0)
  • 2020-11-22 00:52

    For example :

    import java.util.ArrayList;
    import java.util.List;
    
    
    
    /**
     * 
     * @author X2
     *
     * @param <T>
     */
    public class HisTree<T> 
    {
        private Node<T> root;
    
        public HisTree(T rootData) 
        {
            root = new Node<T>();
            root.setData(rootData);
            root.setChildren(new ArrayList<Node<T>>());
        }
    
    }
    
    class Node<T> 
    {
    
        private T data;
        private Node<T> parent;
        private List<Node<T>> children;
    
        public T getData() {
            return data;
        }
        public void setData(T data) {
            this.data = data;
        }
        public Node<T> getParent() {
            return parent;
        }
        public void setParent(Node<T> parent) {
            this.parent = parent;
        }
        public List<Node<T>> getChildren() {
            return children;
        }
        public void setChildren(List<Node<T>> children) {
            this.children = children;
        }
    }
    
    0 讨论(0)
提交回复
热议问题