package org.ahmadsoft.recursion; import java.util.ArrayList; /** * A tree node that contains a value, a reference to * its parent, and a reference to all its children. * @author aahmad */ public class TreeNode { private ArrayList children = new ArrayList(); private TreeNode parent; private int value; /** * Construct a new tree node. * @param parent * @param value */ public TreeNode(TreeNode parent, int value) { this.value = value; this.parent = parent; if (parent != null) { parent.addChild(this); } } /** * Returns the value of the tree node. * @return the value of the tree node. */ public int getValue() { return value; } /** * Returns the children of the tree node. * @return the children of the tree node. */ public ArrayList getChildren() { return children; } /** * Adds a child to this tree node. * @param child */ private void addChild(TreeNode child) { children.add(child); } }