Homework 5 - Hash Tables

Due: Wednesday May 19, 2010 at 5:00pm

The purpose of this assignment is to implement an integer array-based hash table which uses double hashing for collision resolution.

You will be writing a C++ class called HashTable for this assignment. The header file for this class is hash.h. You will need to write the implementation and main files to complete this assignment. You may also alter hash.h to add any defines or exception classes you may wish to add to the class. Exception handling is not required for this assignment however.

Notice that the header file uses the sentinel values EMPTY_VALUE and DELETED_VALUE to differentiate between the end of a collision chain (EMPTY_VALUE) and a value deleted out of the middle of a collision chain (DELETED_VALUE). You should not allow either sentinel value to be inserted into the hash table. You may opt to restrict values in the hash table to positive numbers or you may opt to allow all numbers except the two sentinel values. Either approach will be valid, just make sure that the sentinel values cannot be inserted into the hash table.

Make sure that insert and remove appropriately adjust the count variable as that is used to see if the hash table is full or empty. On insert, you will use double hashing to find the first array slot that contains either EMPTY_VALUE or DELETED_VALUE, both of which indicate that the slot is available. On remove, find the value to be deleted and replace it with DELETED_VALUE. If the value is not found in the table, remove should either issue an exception or print an error message to the screen. When searching for values in the hash table on both remove and search, skip over any slot that contains DELETED_VALUE and do not consider the value "not found" until you reach a slot that contains EMPTY_VALUE.

Your primary hash function (hash) should be the modulo hash function. Your secondary hash function (hash2) should be the prime-based modulo hash function, which is:

hash2(value) = R - (value % R)
where R is a prime number that is smaller than MAX_CAPACITY in hash.h. If you wish, you may add a new define to hash.h for this prime number.

To test your hash table class, write the following menu as your main function:

          Hash Table Menu
  ===================================
  1. Insert an integer into the table
  2. Delete an integer from the table
  3. Search the table for an integer
  4. Print the primary and secondary 
     keys for an integer

  0. Exit
  ===================================
Option 1 should check if the hash table is full. If it is, tell the user that the table is full. Otherwise, prompt the user for the value they wish to insert and then call the insert function in the hash table class to insert that value.

Option 2 should check if the hash table is empty. If it is, tell the user that the table is empty. Otherwise, prompt the user for the value they wish to delete and then call the remove function in the hash table class to remove the value.

Option 3 should also check if the hash table is empty and tell the user that, like Option 2 does. Otherwise, it should prompt the user for a value and call the search function in the hash table class. If search returns true, say the value was found. If search returns false, say the value was not found.

Option 4 should prompt the user for a value. It will then call hash in the hash table class and print out the integer hash returns. It will then call hash2 and print out the integer it returns.

Email me all the source code files for this assignment, including hash.h if you altered it.