next up previous index
Next: EmptyTree and NodeTree implementations Up: Binary Search Trees Previous: Discussion   Index

The abstract SearchTree class

We now fill in the details of the full abstract SearchTree class whose methods are shown in Figure 8.1.

Figure 8.1: The abstract SearchTree class.
public abstract class SearchTree {
    public static SearchTree empty() {
        return new EmptyTree();
    }
    public abstract boolean isEmpty();
    public abstract int nrNodes();
    public abstract boolean contains(Comparable item);
    public abstract SearchTree add(Comparable item);
    public abstract SearchTree remove(Comparable item);
    public abstract Enumeration elementsInOrder();
    public abstract Enumeration elementsLevelOrder();
    public abstract String toString(); 
}

This is effectively an interface for the SearchTree class. All the methods are declared to be abstract, except the first, and therefore must be implemented appropriately by both the EmptyTree and NodeTree subclasses.

We have already discussed the first two methods. The next method nrNodes returns the number of nodes in the tree. There are then three methods contains, add and remove which have an obvious meaning. Each of them takes an item of type Comparable as parameter. The first checks whether the item is present, returning true or false as the case may be. The second inserts the item in its correct place in the tree, and the third removes it. Both the add and remove methods must, of course, ensure that the ordering property (S) is preserved.

Note especially that both the add and remove methods return a SearchTree, namely the tree obtained by inserting or removing the item. Simple code for insertion therefore looks like

t = t.add(item);
assuming that t is properly declared and initialised. But beware! Java will allow you to write the line
t.add(item);
and much work may take place when executing this line of code. But, at the end, t will still reference the original tree before the insertion took place, unless the explicit reassignment to t is made.

The are then two methods, elementsInOrder and elementsLevelOrder, which return an Enumeration. Briefly, the in-order enumeration enumerates the data elements of a binary search tree in increasing order. Thus both of the trees


\begin{picture}(200,190)(-70,0)
\put(0,180){\makebox(0,0){21}}
\put(-60,120){\...
...){20}}
\put(-12,12){\line(2,3){20}}
\put(52,12){\line(-2,3){20}}
\end{picture}

\begin{picture}(200,135)(-100,0)
\put(0,120){\makebox(0,0){31}}
\put(-60,60){\...
...{20}}
\put(-28,12){\line(-2,3){20}}
\put(92,12){\line(-2,3){20}}
\end{picture}
shown previously would have in-order enumerations as
10, 21, 22, 31, 35, 40, 42
The level-order enumeration enumerates the elements of each level in turn. Thus the first tree is enumerated in level-order as
21, 10, 40, 31, 42, 22, 35
while the second tree is enumerated as
31, 21, 40, 10, 22, 35, 42
It would be simple to include pre-order and post-order enumerations, but we have omitted them for brevity.

The final method is the toString method which returns a string that represents the tree. This will override the Object.toString method.


next up previous index
Next: EmptyTree and NodeTree implementations Up: Binary Search Trees Previous: Discussion   Index
Peter Williams 2005-06-07