Lab 04: Hash tables

advertisement
CmSc 250 Intro to Algorithms
Laboratory exercise 04
Download Lab04-HashTables.zip and unzip the folder. It contains a file WordCountOrwell.txt to
be hashed and a BlueJ project with two classes – HashOpenAddressing and HashTable.
The class HashOpenAddressing contains the main method that reads the text file and calls the
methods in HashTable.
The class HashTable creates two hash tables with size specified by the user, for a given
array of keys. The first hash table uses linear probing (referred below as “linear hash
table”), the second one – using quadratic probing (referred below as “quadratic hash
table”).
Methods of the class:
Implemented:
private int computeIndex (String key)
– computes the index in the hash table given the key. Use base 128.
private int insertLinear (String key)
– calls computeIndex to get the base index of the key and then searches the linear hash
table for available cell in linear probing, counting the number of collisions. Stores the
key in the first found available cell and returns the number of collisions.
public int
linearProbe(String [ ] keys, int numberOfKeys)
– in a loop inserts all keys in the linear hash table by calling insertLinear to store each
key, and returns the maximum number of collisions for a word.
To be implemented:
private int insertQuadratic (String key)
– calls computeIndex to get the base index of the key and then searches the quadratic
hash table for available cell in quadratic probing, counting the number of collisions.
Stores the key in the first found available cell and returns the number of collisions.
public int quadraticProbe (String [ ] keys, int numberOfKeys)
– in a loop inserts all words in the quadratic hash table by calling insertQuadratic to store
each key, and returns the maximum number of collisions for a word.
1
Your task is to implement the two methods above and find appropriate size for the hash
tables so that the maximum number of collisions per word is less than 10.
You may choose prime numbers for the size of the table using a prime number generator.
http://www.rsok.com/~jrm/printprimes.html
Make experiments with various sizes and with different base. Currently the program uses
base 128.
Record your observations, the chosen size and base, and justification of your choice in
the top comment area of the class HashTable. Is it always true that larger the table size
will give less collisions?
Complete program is due by Tuesday 11/01 uploaded on Scholar.
2
Download