You may work individually or in pairs.
The following nodes are being put into an AVL tree (in this order):
30, 10, 20, 35, 15, 12, 2, 11, 5
Show step by step how the tree is being constructed and the final tree. You must show the tree before and after every rotation (if any are needed). For every rotation show the three nodes that are being rotated and mark it as LL, RR, LR, or RL.
Implement three open addressing hash table classes. Note that most of the
code in the three tables is the same. The only difference is the collision
resolution mechanism. Therefore it makes sense to implement most of the
functionality in an abstract class that provides code for all methods except
getIndexIfCollision
method that provides a next index if there is
a collision. The getIndexIfCollision
method should be declared
abstract and overwritten in the subclasses.
The class structure should be as follows:
OurHashtable<K,V>
interface (given)OpenAddressHashtable<K,V>
that provides
the array of elements and implements all of the methods of the interface.
The table size is N = 101 (a prime number). The table must be implemented
as an array (not an array list since the array size is known and fixed)
of pairs Pair<K,V>. The unfilled slots must be null. It helps to recall
that arrays
of objects are initialized to all nulls.
The class declares an abstract protected method
abstract protected int getIndexIfCollision(K key, int probeNumber);
The method should return -1 if the location is already occupied. The
method then should be called again with the next value of i.
If the method is called N times
and doesn't find an empty location in the table, throw
a HashTableFullException
(write a class for it and make
it extend RuntimeException).
Updated 6/2: alternatively instead of the method getIndexIfCollision
you could have a getNextIndex
method which will return the next index given the key and the probe number. The abstract class then will have to check if that spot is available and if not then call the method again. The advantage of this approach is that getNextIndex
can be used for both putting the element in and looking it up, whereas getIndexIfCollision
cannot be used for lookup.
LinearProbingHashtable<K,V>
that extends
OpenAddressHashtable<K,V>
and resolves collisions according
to the linear probing (with c = 1): h(k,i) = (h(k) + i) % N,
where h(k) is the hashcode
of the key, i is the probe number, and N is the table size.QuadraticProbingHashtable<K,V>
that extends
OpenAddressHashtable<K,V>
and resolves collisions according
to the quadratic probing (with c1 = c2 = 1): h(k,i) = (h(k) + i + i*i) % N,
where h(k) is the hashcode
of the key, i is the probe number, and N is the table size.DoubleHashingHashtable<K,V>
that extends
OpenAddressHashtable<K,V>
and resolves collisions according
to the double-hashing formula: h(k,i) = (h(k) + i * g(k)) % N,
where h(k) is the hashcode
of the key, g(k) = h(k) % 7 + 1 (1 is added to guarantee that it's never 0),
i is the probe number, and N is the table size.Test your classes well. You might want to write a method that returns the contents of the array (as an array or array list). Note that some elements are null and should be preserved in the output since they indicate an empty slot.
The easiest way to test hash tables is by using Integer
for
both the key and the value.
You need to compare the number of collisions when putting randomly
generated keys (and the corresponding values) into the three hash
tables. In order to measure collisions you need to add a counter
that counts the number of times the
method getIndexIfCollision
is called in each of the
classes. At the end the counters are printed and compared.
Specifically you need to do the following:
getCollisionCounter
method
that returns the counter.getIndexIfCollision
to
increment the counter every time the method is called (i.e. every
time the collision occurs). Write a test to check that the counters
work as expected (you might want to write the test first, and then the
actual code that works with the variable).Submit the java file(s) with your testing code by e-mail to me. Make sure to CC your group partner (if any).