How to implement a tree data-structure? Provide some code.

Technology CommunityCategory: Binary TreeHow to implement a tree data-structure? Provide some code.
VietMX Staff asked 3 years ago

That is a basic (generic) tree structure that can be used for String or any other object:

Implementation
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;
    }
}