Next: Code and demonstration
Up: Binary Search Trees
Previous: The toString method
  Index
You may find this Java implementation of binary search trees somewhat
complicated, specifically the use of an abstract SearchTree
class having concrete extensions EmptyTree and
NodeTree. A more common approach would be similar to the
treatment of the LinkedList class given previously. In that
case we should begin with the idea of a TreeNode class
class TreeNode {
Comparable data;
TreeNode left;
TreeNode right;
TreeNode(Comparable data,...) {
this.data = data;
.....
}
}
corresponding to the previous idea of a ListNode.
The SearchTree class would then be a concrete, fully implemented
class, having it own private TreeNode:
public class SearchTree {
private TreeNode root;
SearchTree() {
root = null;
}
.....
}
An empty SearchTree would be one whose private root was
equal to the null reference. However, when it comes to writing
recursive methods for this implementation of a SearchTree, you
will find there are complications which we have managed to avoid. The
reason, it can be argued, is that our implementation is algebraically
more correct. Furthermore it avoids the use of the null
reference as representative of an empty tree, which is a trick some
purists deplore. In any case, you may find the present implementation
simpler rather than more complicated. Finally, note that it is
possible, and arguably preferable, to give a corresponding
implementation to the LinkedList class, which would become an
abstract class, having two concrete extensions EmptyList and
NodeList. Enthusiasts may like to implement this for
themselves.
Next: Code and demonstration
Up: Binary Search Trees
Previous: The toString method
  Index
Peter Williams
2005-06-07