## CSci 2101 Problem set 6.

### Due Friday June 24 at 6pm

You may work individually or in pairs.

### 47 points

#### Problem 1 (5 points): Written part.

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.

#### Problem 2 (30 points). Implementing open addressing hash tables.

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)
• abstract class `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.

• class `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.
• class `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.
• class `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.

#### Problem 3 (12 points). Comparing open addressing hash tables.

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:

• Add a collision counter (a private variable) in each of the three hash tables classes. Add a `getCollisionCounter` method that returns the counter.
• Add a line of code in `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).
• Write a simulation class (with main) that generates M random pairs of keys and values (both Integer) such that the key is in the range from 0 to 10000 (where M <= N). After a pair is generated, it is added to each of the three tables.
• After all the elements have been added, print out (and record) the values of collision counters.
• If one of the tables does not allow you to add an element, record that information. Your program should stop at that point.
• Run these tests with the following load factors M/N: 50%, 75%, 90%, 100%. Write down your results and observations about the tables' behavior.

Submit the java file(s) with your testing code by e-mail to me. Make sure to CC your group partner (if any).

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.