I wrote a simple tree data type in Java consisting of nodes which are themselves trees, and where each tree has its own list of children (not a binary tree). I plan on eventually using it in a minimax algorithm where I keep track of future game states in a board game by holding different board states in nodes and holding moves that lead to each state as edges in the tree. Here's my code so far — any suggestions (whether it be conceptual or performance related, or other features I should add) are appreciated:
public class Tree<V, E> {
private V data;
//here the edgeVal is the edge leading from the node's parent to the node itself,
//not from the node to any of its children
private E edgeVal;
private Tree<V, E> parent;
private ArrayList<Tree<V, E>> children;
public Tree(V data, Tree<V, E> parent, E edgeVal) {
this.data = data;
this.edgeVal = edgeVal;
this.parent = parent;
this.children = new ArrayList<Tree<V, E>>();
}
public void addChild(V childData, E edgeVal) {
Tree<V, E> child = new Tree<V, E>(childData, this, edgeVal);
this.children.add(child);
}
public Tree<V, E> getParent() {
return this.parent;
}
public ArrayList<Tree<V, E>> getChildren() {
return this.children;
}
public V getData() {
return this.data;
}
public E getEdgeVal() {
return this.edgeVal;
}
public boolean isRoot() {
return this.parent == null;
}
public boolean isLeaf() {
return this.children.size() == 0;
}
public String toString() {
//assuming type of data has a nicely defined toString() method
return this.data.toString();
}
}