1package org.ahmadsoft.recursion; 2 3import java.util.ArrayList; 4 5/** 6 * A tree node that contains a value, a reference to 7 * its parent, and a reference to all its children. 8 * @author aahmad 9 */ 10public class TreeNode { 11 private ArrayList<TreeNode> children = new ArrayList<TreeNode>(); 12 private TreeNode parent; 13 private int value; 14 15 /** 16 * Construct a new tree node. 17 * @param parent 18 * @param value 19 */ 20 public TreeNode(TreeNode parent, int value) { 21 this.value = value; 22 this.parent = parent; 23 if (parent != null) { 24 parent.addChild(this); 25 } 26 } 27 28 /** 29 * Returns the value of the tree node. 30 * @return the value of the tree node. 31 */ 32 public int getValue() { 33 return value; 34 } 35 36 /** 37 * Returns the children of the tree node. 38 * @return the children of the tree node. 39 */ 40 public ArrayList<TreeNode> getChildren() { 41 return children; 42 } 43 44 /** 45 * Adds a child to this tree node. 46 * @param child 47 */ 48 private void addChild(TreeNode child) { 49 children.add(child); 50 } 51} 52