This assignment covers the basics of trees and, in particular, binary search trees (BSTs). We'll get to balanced trees later in the course; work the problems on this homework using unbalanced binary trees.
As usual, your code should not only work properly, but it also should be high-quality. In particular:
Answer these questions on a separate sheet of paper. We suggest you do these problems early as part of your preparation for the programming part of the assignment.
One of the applications of BSTs is to implement ordered sets. A set is a container that contains a collection of items with no duplicates; attempting to add a duplicate item to a set has no effect. An ordered set is a set with the additional property that when we iterate through the set we retrieve the elements in order as defined by the comparison function for those elements. Not visible to the user, but useful in the implementation, is that we can take advantage of the binary search tree to speed up various operations, including searches and modifications of the set.
For this assignment, provide Java interfaces for ordered sets and their iterators and an implementation of those interfaces using a binary search tree as the underlying data structure. Specifically, implement the following two interfaces:
BasicSet
. This is the interface describing the set
operations. Include the following:
add(item)
Add the specified item to the set if it is not already present.
Return true if the item was added; false if not.contains(item)
Return true if the item is an element of the
set, false if not.size()
Return the number of elements in the set.remove(item)
Remove item from the set if present. Return true if the item
was removed; false if not.iterator()
Return an iterator for this set.BasicSetIterator
. This interface describes the
supported iterator operations for a BasicSet
.
hasNext()
Return true if there is another available element in the iteration;
false if not.next()
Return the next item in the iteration. Throw a
NoSuchElementException
if there is no next item in the iteration.
Successive calls to next()
should return the items in the
set in order.Because this is a sorted set, the items in the set must have type Comparable
,
meaning that whatever kind of objects they are, they implement the Comparable
interface,
which includes the compareTo()
method. The ordering specified
by compareTo()
is the one that determines the order in which the
items are returned from the set by successive calls to an iterator's next()
method.
After defining the interfaces, create an implementation of BasicSet
in
a class named BasicOrderedTreeSet
. That class should contain a
nested class BasicOrderedTreeSetIterator
that implements the BasicSetIterator
interface.
BasicOrderedTreeSet
must use an unbalanced
binary tree to store the set items in the order resulting from
the add()
and remove()
operations
used to create the set, using the compareTo()
method for
the set items..BasicOrderedTreeSet
.
A tree node should only contain a reference to the item stored at that node
and single links to the node's left and right children. These links should
be
null if the node has no children.BasicOrderedTreeSet
to refer to the root node of the
tree. This should be null for an empty tree.contains()
should only search
the left or right subtree of a node when looking for a particular item.previous()
, hasPrevious()
or remove()
for
the
BasicSetIterator
. Also, you do not need to check for or signal
a ConcurrentModificationException
in the iterator.Implementation of the BST used for the set should be relatively straightforward. The trickiest part is being sure that remove properly deletes items and that the resulting tree is a proper binary tree.
The iterator is probably the trickiest part since it is basically doing an
inorder traversal of the tree, but one node at a time each time that next()
is
called. To make this work, the iterator needs to keep track of the current
node in the tree reached by the iteration and also all of the anscestors
of that node. After visiting a node and all of its children, the iteration
needs to back up to the parent of the node. If the original node was a left
child of the parent, the iteration next needs to visit the parent and then
its right children. You can keep track of this information
by keeping a list (actually a stack) of the anscestor nodes along with information
needed to remember whether each anscestor and/or its children have been visited
yet. This information should be private data in the iterator class.
Turn in your programming assignment online using the turnin form linked from the course web site (when available). You should include the Java source code for both the interfaces and implementations of the tree set.
Turn in your written questions at the beginning of class, Friday, 1/27.