L09

Due by the end of this meeting

Starter Files:


SUBMIT   /   Submission Summary

Hash Tables

Activity 1
Download the program OpenLinear and run it. It shows:

Look at the code and familiarize yourself with the add method. We're going to add two more methods that are very similar.

Activity 2
Create two copies of the add method, named contains and delete.

Revise each to perform its task:

Delete the line labeled "DELETE THIS LINE" and fix any errors that happen to crop up. Then run the program and see the following extra output:

Looking for 'me' ...location 7 is in use... contains(me) is true Looking for 'Mark' ...location 7 is in use... ...location 10 is in use... contains(Mark) is true Looking for 'yo' contains(yo) is false Looking for 'swollen head' ...location 10 is in use... ...location 13 is in use... contains(swollen head) is true Press enter... Deleting 'me' ...location 7 is in use... i hashTable[i] - ------------ 0 'Young' 1 2 3 'myself' 4 'I' 5 6 'mine' 7 8 9 10 'Mark' 11 12 13 'swollen head' 14 15 16 'ego' 17 18 19 20 21 22 Press enter... Looking for 'me' contains(me) is false Looking for 'Mark' contains(Mark) is false Looking for 'yo' contains(yo) is false Looking for 'swollen head' ...location 10 is in use... ...location 13 is in use... contains(swollen head) is true Press enter...

Note that the program says that "Mark" is not in the hash table, even tho' it is clearly visible at location 10. The problem is that it was supposed to go into location 7, but collided with "me". Now that "me" has been deleted, your contains method notices the empty space at 7 and concludes that "Mark" cannot be there.

Activity 3
Create a private static class called Node.

Give it a single String instance variable.

Give it a constructor that sets the value of that IV.

Give it a toString method that returns the value of the IV.

Give it an equals(String) method. (NOTE: this does not @Override the version from Object!) Have it return true if the given String is equals to the Node's IV.

Back in OpenLinear (not in Node) create two methods isOpen and isUsed which each take a Node argument.

Change the hash table array's base type from String to Node. (That needs to happen in main and in any method that takes the hash table as an argument.)

Revise the add, contains and delete methods to make them work properly.

Run your program again and confirm that it finds "Mark" in the hash table.

Looking for 'me' ...location 7 is in use... ...location 10 is in use... ...location 13 is in use... ...location 16 is in use... contains(me) is false Looking for 'Mark' ...location 7 is in use... ...location 10 is in use... contains(Mark) is true Looking for 'yo' contains(yo) is false Looking for 'swollen head' ...location 10 is in use... ...location 13 is in use... contains(swollen head) is true Press enter...

Note also that the search for "me" is longer. Because 7 is used (even tho' it's open) the program has to keep looking at the following locations, just in case "me" was the "bumped" element.

Submit this/these files:


SUBMIT   /   Submission Summary