In the standard Java libraries, TreeNode is an Interface that tree nodes should implement for generic trees.

However, many programmers create a TreeNode class and use it as the underlying node class for binary trees. We will do as such with the following definition of the TreeNode class which has been provided by the University of San Francisco:

public class TreeNode<T> {
  private T item;
  private TreeNode<T> leftChild;
  private TreeNode<T> rightChild;

  public TreeNode(T newItem) {
  // Initializes tree node with item and no children.
    item = newItem;
    leftChild  = null;
    rightChild = null;
  }  // end constructor
  public TreeNode(T newItem, TreeNode<T> left, TreeNode<T> right) {
  // Initializes tree node with item and
  // the left and right children references.
    item = newItem;
    leftChild  = left;
    rightChild = right;
  }  // end constructor

  public T getItem() {
  // Returns the item field.
    return item;
  }  // end getItem

  public void setItem(T newItem) {
  // Sets the item field to the new value newItem.
    item  = newItem;
  }  // end setItem

  public TreeNode<T> getLeft() {
  // Returns the reference to the left child.
    return leftChild;
  }  // end getLeft

  public void setLeft(TreeNode<T> left) {
  // Sets the left child reference to left.
    leftChild  = left;
  }  // end setLeft

  public TreeNode<T> getRight() {
  // Returns the reference to the right child.
    return rightChild;
  }  // end getRight

  public void setRight(TreeNode<T> right) {
  // Sets the right child reference to right.
    rightChild  = right;
  }  // end setRight
}  // end TreeNode