Class BinarySearchTree

java.lang.Object
  |
  +--BinarySearchTree

public class BinarySearchTree
extends java.lang.Object

Class BinarySearchTree defines a binary search tree data structure with Object-valued nodes. The class provides insert, remove, and find methods. See also the BinaryTree class. The difference between this BinarySearchTree class versus plain BinaryTree is that this class maintains the nodes in sorted order, per the standard binary search tree property.

This BinaryTree class uses the public class BinaryTreeNode as the definition of a tree node.


Constructor Summary
BinarySearchTree(BinaryTreeNode root)
          Construct this with the given node as the root.
 
Method Summary
 BinaryTreeNode find(java.lang.Object value)
          Return a pointer to the node containing the given value, using the O(log N) binary search tree algorithm.
protected  BinaryTreeNode find(java.lang.Object value, BinaryTreeNode t)
          Recursive work doer for find(Object).
protected  BinaryTreeNode findSmallest(BinaryTreeNode t)
          Find the node with smallest value in the given subtree t.
 BinarySearchTree insert(java.lang.Object value)
          Insert a new node containing the given value in the proper order position.
protected  BinaryTreeNode insert(java.lang.Object value, BinaryTreeNode t)
          Recursive work doer for insert(Object).
 BinarySearchTree remove(java.lang.Object value)
          Remove the node containing the given value, if any, retaining the proper binary search tree structure.
protected  BinaryTreeNode remove(java.lang.Object value, BinaryTreeNode t)
          Recursive work doer for remove(Object).
 java.lang.String toString()
          Convert this to a preorder string of the form: root left subtree ...
protected  java.lang.String toString(BinaryTreeNode t, int indent)
          Recursive work doer for toString(), converting the given node value to a string, preceded by indent spacing, followed by the recursive toString of the left and right subtrees.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BinarySearchTree

public BinarySearchTree(BinaryTreeNode root)
Construct this with the given node as the root.
Method Detail

insert

public BinarySearchTree insert(java.lang.Object value)
Insert a new node containing the given value in the proper order position.

insert

protected BinaryTreeNode insert(java.lang.Object value,
                                BinaryTreeNode t)
Recursive work doer for insert(Object).

remove

public BinarySearchTree remove(java.lang.Object value)
Remove the node containing the given value, if any, retaining the proper binary search tree structure.

remove

protected BinaryTreeNode remove(java.lang.Object value,
                                BinaryTreeNode t)
Recursive work doer for remove(Object).

findSmallest

protected BinaryTreeNode findSmallest(BinaryTreeNode t)
Find the node with smallest value in the given subtree t.

find

public BinaryTreeNode find(java.lang.Object value)
Return a pointer to the node containing the given value, using the O(log N) binary search tree algorithm. Assume no duplicate node values.

find

protected BinaryTreeNode find(java.lang.Object value,
                              BinaryTreeNode t)
Recursive work doer for find(Object).

toString

public java.lang.String toString()
Convert this to a preorder string of the form: root left subtree ... right subtree ... I.e., the root is on the first line, subtrees are on subsequent lines, each indented two spaces for each level of the tree.
Overrides:
toString in class java.lang.Object

toString

protected java.lang.String toString(BinaryTreeNode t,
                                    int indent)
Recursive work doer for toString(), converting the given node value to a string, preceded by indent spacing, followed by the recursive toString of the left and right subtrees.