import java.util.*; class GenericTreeClass { public static void main(String[] args){ TNode t = new TNode<>("Roo"); t.add("Left"); t.add("Middle"); t.add("Right"); t.getChild(0).add("aaa"); t.getChild(0).add("bbb"); t.traverse(); } } class TNode { private T value; private ArrayList> children = new ArrayList<>(); TNode(T v) { this.value = v; } public T getValue() { return this.value; } public void add(T v) { TNode child = new TNode<>(v); this.children.add(child); } public TNode getChild(int i) { if ((i < 0) || (i > this.children.size())) return null; return (TNode)this.children.get(i); } public void traverse() { System.out.println(this.value); for (TNode child : this.children) child.traverse(); } }