Implementing a
Binary Search Tree
BinSearchTree
Another implement of a BST
Java Collections Framework
Notice that BinSearchTree is derived from AbstractSet
AbstractSet is a member of the Collection Framework in the
Java Library
The BinSearchTree class implements the Collection interface and extends
one of its members
But it is not
really part of the Collection Framework
It is less
complicated, and less powerful, than real Collection classes
Well look at the BinSearchTree class, then briefly look at some of the features of
Javas Collection Framework
Link to
the code
Embedded classes in BinSearchTree
class Entry
Contains a reference variable to the parent as well as
the left and right child
Only one constructor
It
sets the element to be created in the tree as well as the parent
reference variable
When you implement the driver, this element object
must be Comparable
It is cast to type Comparable in several of the methods
in BinarySearchTree
Mistake on your handout for this class
what is it?
Embedded classes in BinSearchTree
class TreeIterator implements Iterator
What are the data members (the attributes)?
Look at the constructor for this class; why is it
written that way?
The Iterator interface
required that three methods be implemented
hasNext( )
next( )
remove( )
The methods of BinSearchTree
public Iterator iterator()
Calls the
constructor for a TreeIterator
Returns an iterator positioned at the first entry in
the tree
Question: What
is the first entry?
Note that the
method iterator( ) is different than the class Iterator
When you want an
iterator, you call this method with an object of type BinSearchTree
Iterator itr = tree.iterator();
\
The methods of BinSearchTree
public boolean
add (Object o)
Returns true if the item is successfully added;
returns false if the item is already in the tree
The average time is O(log n)
Note that you cannot do adds to a BST without doing
comparisons
The object to be
added is of type Object (to be
compatible with the signature in AbstractSet) so it must be cast to type Comparable
comp = ((Compare)o).compareTo (temp.element);
The methods of BinSearchTree
public boolean
contains (Object o)
Returns true if there is an element o in the tree, otherwise returns false
Average time is O(log n)
Note that o must be cast to type Comparable
The methods of BinSearchTree
public boolean
remove(Object elem)
This public
method does its work by calling two private methods
It
calls getEntry( ) to get the pointer to
the node to be deleted
It calls deleteEntry( ) to delete the node
The argument is
the object to be deleted, not a reference variable pointing to the node
containing the object
private void deleteEntry
(Entry p)
This method
considers the three cases we talked about when talking about deleting
from a BST
It calls successor( ) if the node to be deleted has two children
Will this method work in the code you are writing for
the out lab? Why?
The methods of BinSearchTree
private Entry getEntry (Object elem)
Returns a reference variable pointing to the node
containing elem
If elem does not exist in the tree, null is returned
Again, elem must be cast to type Comparable
private Entry successor (Entry
e)
Returns a
reference variable pointing to the inorder sucessor of the node pointed to by e
This is called by
deleteEntry( ) if the node to be removed has two children
It is also called
by next( ) in the Iterator class
Javas Collection Framework
BinSearchTree is a minor
shadow of the TreeSet
class implemented in the Collection Framework.
Its interface is almost the same, but functionality
much less
Well look now at how the elements of the Collection Framework fit together.
The Collection Framework consists of:
Collection
Interfaces
these interfaces form the basis of the framework
They specify
methods that must be implemented in classes that implement the interface
Abstract
Implementations
Partial implementations to make custom collections
easier to implement
General-purpose
Implementations
Classes like ArrayList, LinkedList, HashMap and TreeSet
Algorithms
Static methods to
perform useful functions on collections, such as sorting a list
BinSearchTree extends AbstractSet
The code I handed out does not contain implementations
of all the methods specified in the Collection
interface
Some of them have
been implemented in the class AbstractSet
The class AbstractSet implements the Set interface (at least part of it.)
The interface Set extends the Collection interface
Javas predefined Collection interfaces
Each of these interfaces has an abstract class that
implements some of its methods

Implementations in the Collection Framework
This chart shows the data structures that are used in
the classes that are implemented in the Collection Framework.

How Java iterators work
By repeatedly calling the next method, you can visit the elements in the collection one
by one
But you must make
sure not to run off the end of the collection by first calling hasNext
Think of Java iterators as being between elements
When you call next, the iterator jumps over the next element, and returns
a reference to the element it just passed
The only way to look up an element is to call next, and that lookup advances the position
i.e. a call to next returns the item just jumped over (The returned item's
type will be Object)
