Project: Open addressing with linear probing

1. Introduction

The goal of this project is to understand hashing and hash tables by implenting the "open addressing with linear probing" strategy. (Since open addressing with linear probing is a bit long, we'll pretty much use both open addressing and linear probing to mean the same thing, though strictkly speaking there are other probing sub-strategies for open addressing besides linear probing.)

This project and the next form a pair analogous to the projects on AVL trees, traditional red-black trees, and left-leaning red-black trees. There is one code base for this and the next project. Also, like the previous, one missing part to these projects is an experimental section.

2. Set up

Find the project code for this and the next project at /homes/tvandrun/Public/cs345/hash. As usual, the code is organized into the packages adt, impl and test.

In addition to the classes you need to modify I am giving you the "basic" (separate chaining) hash map from class (and from CSCI 245), for comparison. Also included are the hash function factory, the prime source, and an implementation of a set using a list (since you may find a set to be of use at some points). Every implementation of every ADT has a JUnit test case for it.

3. Implementing linear probing

Your task in this project is to complete LinProbHashMap.java. I have already provided the instance variables (I suppose you may add to these, but I don't think you'll need to). You need to complete the constructor, the helper function findIndex(), put(), remove(), and iterator(). For my implementation of remove(), I used a helper method compareIdealPlace(), for which I've provided a stub (and thorough documentation) for in case you find it useful, but you don't need to do it that way.

You could organize the methods this way:

Test using test.LPHMTest.

I recommend that you develop this incrementally, saving iterator() and remove() for the end. You can test, for example, that your implementation of findIndex() and put() work before starting on the harder ones by running the JUnit test cases and looking only at the ones that rely only on what you've done so far. (I think every test case that relies on the iterator and remove() actually have "iterator" and/or "remove" in the name.)

For the harder ones, I highly recommend you think through the problem using loop invariants. What do your variables mean, how does the loop change them, and how can they be described in a way that is true for all iterations of the loop? For the iterator, don't try writing the code until you can articulate how the instance variables capture the internal state of the iterator. Also remember that next() should not only return the next element in the iteration but also modify the state of the iterator. hasNext() should only report on the state, not change it.

4. Turn in

Copy the file you modified (LinProbHashMap) to your turn-in folder /cslab/class/cs345/(your id)/linearprob .

To keep up with the course, this should be finished by April 20.


Thomas VanDrunen
Last modified: Fri Apr 7 10:46:09 CDT 2017