AVL Trees
Note: Projects are to be completed by each student individually (not by groups of students).
Be sure to check out the TIPS page!
Introduction
This project requires you to implement part of the java.util.Set interface using AVL trees. Your implementation will be compared against the java.util.HashSet class in a number of test cases. Your AVL trees will also be tested to determine if they have the correct structure and balance.
Requirement #1: Implement the
java.util.Set InterfaceThe on-line documentation at java.sun.com defines the java.util.Set interface. You must implement the interface using AVL trees.
You must fully implement the following methods:
boolean add(Object o)
void clear()
boolean contains(Object o)
boolean isEmpty()
Iterator iterator()
boolean remove(Object o)
int size()
Object[] toArray()
Your implementation is allowed to throw UnsupportedOperationException for the following methods:
boolean addAll(Collection c)
boolean containsAll(Collection c)
boolean removeAll(Collection c)
boolean retainAll(Collection c)
Object[] toArray(Object[] array)
For example, you could implement addAll as follows:
boolean addAll(Collection c) {
throw new UnsupportedOperationException();
}
Your implementation must conform to the required behavior of each method as defined in the on-line documentation. In addition to the requirements given in the on-line documentation, the iterator and remove methods have additional requirements that are defined later in this document. Your implementation must use a binary search tree and your tree must use AVL balancing when adding and removing nodes. Put your implementation class in the cs235.avl package.
Requirement #2: Implement the
java.util.Iterator InterfaceThe java.util.Iterator interface is defined on-line at java.sun.com. You must implement an Iterator that will iterate over the items stored in your AVL tree. You could implement your Iterator using a class that is nested inside of the class you use to implement java.util.Set.
You must fully implement the following methods:
boolean hasNext()
Object next()
Your implementation is allowed to throw UnsupportedOperationException for the following method:
void remove()
Your implementation must conform to the required behavior of each method as defined in the on-line documentation. The definition of java.util.Set allows an Iterator to return the items in the set in any order. Your implementation must meet an additional requirement. You must return the items in the order given by a preorder traversal of the AVL tree that is implementing the set. The order in which items are returned from the Iterator will be used to determine if your tree is constructed and balanced correctly.
The iterator method in the class that implements java.util.Set should return an Iterator object as described in this section.
The toArray method in the class that implements java.util.Set must fill its array in the same order as described here for the Iterator. In fact, you could use an Iterator to help implement the toArray method.
The
remove Method in java.util.SetThe order of your Iterator could be affected by your implementation of the remove method in the class that implements java.util.Set. The remove method on an AVL tree has a case where two different choices can be made and both choices produce a correct tree. The case occurs when you are removing a node that has two non-null children. The node to be removed can be replaced with either the largest node in the left subtree or the smallest node in the right subtree. Your remove method is required to use the second choice, you must use the smallest node in the right subtree. If you do not use the required choice, the preorder output given by your Iterator will not produce the correct output and your trees will fail a number of test cases.
Requirement #3: Implement the method in the
cs235.avl.SetFactory class.The file SetFactory.java contains a class named SetFactory. SetFactory has one method named createSet. You must implement this method. The method creates a new instance of your AVL tree class and returns it.
Requirement #4: Test Your Code
The file TestDriver.java contains a test program you can use to help you test your code. A number of files must be in the current directory when you run the test driver. These files are packaged together in a zip archive avl.zip.
The test driver should not be viewed as a replacement for doing your own testing. If your code passes the test driver, it is still possible that it will fail at pass off because a different test program is used for pass off (i.e., the pass off driver). It is your responsibility to make sure that your code works.