Recitation #2

advertisement

COP 3503 Recitation #2 Problem:

Hash Table of Famous People

Assigned 9/8/2010 - Due 9/15/2010 over Webcourses

Problem

You are compiling a list of famous people and so far you have 936. You want to keep adding to your list but would like a fast insert, delete and search time. So you decide to store your famous people in a hash table. To handle collisions your hash table will be an array of linked lists. (Please use Java's LinkedList class.) After your program has read in the list of famous people into the hash table, you will present the user with statistics about the hash table. (These are specified below.) Then, your program will prompt the user for a person to search. You will then determine whether or not the person entered by the user is in the hash table or not and print out a message to that effect. You should allow the user to continue checking people until he/she wants to quit. Sample output showing roughly how your program should run is included below (Program output in italics, User input in normal font):

Welcome to the Famous People List!

Enter the name of your people file. famous_people.txt

Great, your list has been stored in memory.

There are 8012 empty entries.

The longest entry is length 1.

The average length of an entry is 1.0.

What person would you like to search for?

Alicia_Silverstone

Alicia_Silverstone is a valid famous person, found in entry 8202.

Would you like to check another famous person(yes=1,no=0)?

1

What person would you like to search for?

Vincent_Van_Gogh

Vincent_Van_Gogh is a valid famous person, found in entry 4233.

Would you like to check another word(yes=1,no=0)?

1

What person would you like to search for?

Sigmund_Freudian

Sigmund_Freudian is not a valid famous person.

Would you like to check another word(yes=1,no=0)?

0

Thank you for using the Famous People List!

Note: This sample is NOT accurate. It is just included to indicate the desired

FORMAT for the I/O.

HashTable class

Essentially, a HashTable object will store an array of linked lists. Through the hashing function, the unique hash location for any particular famous person can be determined.

Thus, when inserting a famous person into the hash table, you must compute its hash table location. Then, insert this famous person into the appropriate linked list. When searching for a famous person, simply compute the hash location for that famous person, and search through the linked list at that location for the famous person. If it isn't there, the famous person isn't in the list.

(Note: Please use Java’s LinkedList class.)

Here is a skeleton for the HashTable class: public class HashTable {

public final static int TABLE_SIZE = 9109;

public final static int NOT_FOUND = -1;

private LinkedList[] table; // Stores words.

public HashTable() {//insert code}

public void insert(String w) {//insert code}

private int hashFunction(String w) {//insert code}

// If w is in the hash table, the integer returned is

// the location of w in the hash table. If w is NOT in

// the table, then NOT_FOUND is returned.

public int search(String w) {//insert code}

public static void main(String[] args) throws

IOException {//insert code}

}

Here is the hash function I would like you to implement:

Let the famous person to be hashed be w

1 w

2

...w

n

where each w i

1  i  n, is a single character. Also, let val(x) be the numerical equivalent in between 0 to 25 of the letter x.

In particular, A = 0, B = 1, ... , Z = 25. Assume that all people are formed with lower or upper case letters only, thus ignore all underscores “_”. f(w

1 w

2

...w

n

) = [val(w

1

)*26 n-1 + val(w

2

)*26 n-2 +...+ val(w n-1

)*26 1 +val(w n

)*26 0 ] mod 9109.

As the function indicates, your array size for your hash table should also be 9109. Here is one small example: f("dog")=(val('d')*26 2 +val('o')*26 1 +val('g')*26 0 )%

9109

=(3x26 2 +14x26+6)%9109=2398.

So, dog would be stored in the linked list with index 2398.

main method

In main you will take care of prompting the user for the name of the famous person file.

In order to test your program, you must copy the famous person file posted on the web page to your local directory and read from that file to fill up your hash table, or just create your own famous person file

. (Note: your main should contain a single HashTable object.) Once you have finished that, print out the following statistics: (a) number of empty hash table locations, (b) the length of the longest hash table entry, (c) the average length of all non-empty hash table entries. Then, go ahead and prompt the user as designated in the sample output. Make all the necessary HashTable method calls to complete your task.

For curiosity

Experiment with hash functions and try to come up with one that minimizes the length of the longest list as well as the average length of a non-empty list. If you find one that performs better than the given one, put it in your code as hashFunction2, and have your code call that function instead. Please leave hashFunction in your code since it will be graded based on whether or not you followed the specification.

What to turn in

Turn in the file HashTable.java over WebCourses. Please follow all the specifications

(names of classes and methods given above.) Although you should work on the assignment together in recitation, please finish the assignment up on your own and submit it on your own. Note who you worked with in your header comment.

Download