How to implement a tree data-structure in Java?

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

    There are a couple of tree data structures in Java, such as DefaultMutableTreeNode in JDK Swing, Tree in Stanford parser package, and other toy codes. But none of these are sufficient yet small enough for general purpose.

    Java-tree project attempts to provide another general-purpose tree data structure in Java. The difference between this and others are

    • Totally free. You can use it anywhere (except in your homework :P)
    • Small but general enough. I put everything of the data structure in one class file, so it would be easy to copy/paste.
    • Not just a toys. I am aware of dozens of Java tree codes that can only handle binary trees or limited operations. This TreeNode is much more than that. It provides different ways of visiting nodes, such as preorder, postorder, breadthfirst, leaves, path to root, etc. Moreover, iterators are provided too for the sufficiency.
    • More utils will be added. I am willing to add more operations to make this project comprehensive, especially if you send a request through github.
    0 讨论(0)
  • 2020-11-22 00:53

    I wrote a tree library that plays nicely with Java8 and that has no other dependencies. It also provides a loose interpretation of some ideas from functional programming and lets you map/filter/prune/search the entire tree or subtrees.

    https://github.com/RutledgePaulV/prune

    The implementation doesn't do anything special with indexing and I didn't stray away from recursion, so it's possible that with large trees performance will degrade and you could blow the stack. But if all you need is a straightforward tree of small to moderate depth, I think it works well enough. It provides a sane (value based) definition of equality and it also has a toString implementation that lets you visualize the tree!

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

    You can use TreeSet class in java.util.*. It is working like Binary search tree, so it is already sorted. TreeSet class implements Iterable, Collection and Set interfaces. You can traverse through the tree with iterator like a set.

    TreeSet<String> treeSet = new TreeSet<String>();
    Iterator<String> it  = treeSet.Iterator();
    while(it.hasNext()){
    ...
    }
    

    You can check, Java Doc and some other .

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

    Since the question asks for an available data structure, a tree can be constructed from lists or arrays:

    Object[] tree = new Object[2];
    tree[0] = "Hello";
    {
      Object[] subtree = new Object[2];
      subtree[0] = "Goodbye";
      subtree[1] = "";
      tree[1] = subtree;
    }
    

    instanceof can be used to determine whether an element is a subtree or a terminal node.

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

    Here:

    public class Tree<T> {
        private Node<T> root;
    
        public Tree(T rootData) {
            root = new Node<T>();
            root.data = rootData;
            root.children = new ArrayList<Node<T>>();
        }
    
        public static class Node<T> {
            private T data;
            private Node<T> parent;
            private List<Node<T>> children;
        }
    }
    

    That is a basic tree structure that can be used for String or any other object. It is fairly easy to implement simple trees to do what you need.

    All you need to add are methods for add to, removing from, traversing, and constructors. The Node is the basic building block of the Tree.

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

    Yet another tree structure:

    public class TreeNode<T> implements Iterable<TreeNode<T>> {
    
        T data;
        TreeNode<T> parent;
        List<TreeNode<T>> children;
    
        public TreeNode(T data) {
            this.data = data;
            this.children = new LinkedList<TreeNode<T>>();
        }
    
        public TreeNode<T> addChild(T child) {
            TreeNode<T> childNode = new TreeNode<T>(child);
            childNode.parent = this;
            this.children.add(childNode);
            return childNode;
        }
    
        // other features ...
    
    }
    

    Sample usage:

    TreeNode<String> root = new TreeNode<String>("root");
    {
        TreeNode<String> node0 = root.addChild("node0");
        TreeNode<String> node1 = root.addChild("node1");
        TreeNode<String> node2 = root.addChild("node2");
        {
            TreeNode<String> node20 = node2.addChild(null);
            TreeNode<String> node21 = node2.addChild("node21");
            {
                TreeNode<String> node210 = node20.addChild("node210");
            }
        }
    }
    

    BONUS
    See fully-fledged tree with:

    • iterator
    • searching
    • Java/C#

    https://github.com/gt4dev/yet-another-tree-structure

    0 讨论(0)
提交回复
热议问题