Hashing (continued)

Closed hashing, as discussed in the previous page, has very serious problems if the number of elements hashed gets to be near the size of the hash array - soon all the slots get filled and find, for example, needs to look through the entire array to find a particular value. (insert and delete have similar problems.) In fact, the hash function becomes nearly useless because you almost never find what you are looking for at the place the hash function takes you to.

An alternative is what is called open hashing. In open hashing the array is not an array of data items but, instead, an array of linked lists each of which has its own insert, delete, and find functions. Now when you do an insert, the hash function simply tells you which linked list to use and, then, you insert into that linked list with a linked list insert. What you insert will, in general, be an ordered pair - the key data item and an index of some sort pointing to where the actual data record is. Generally, you must start with a new class called, for example, datapair which consists of an item of the type of the key combined with an index into the data. For example, suppose the data is in an array as follows:
 
D dataforD
K dataforK
A dataforA
Z dataforZ
M dataforM

Then if our hash function were the ascii value of the character mod 3, then an open hash table for this data might look as follows:

Notice that the linked list at position 0 of the array contains K and Z because the ASCII values of K and Z are 0 when modded by 3 - namely, 75 (=25*3) and 90 (=30*3). Similarly, the linked list at position 2 contains D, A, and M since their ASCII values are 65 (=21*3+2), 68 (=22*3+2), and 77 (=25*3+2). Each pair in the linked lists contains the data value (a character in this case) together with an integer indicating where in the data array the key value is located. (Thus, if you get a pair from the hash table, you can use that index into the data array to get the particular record you want. For example, if you did find('K') you would call the linked lists find function at position f('K') where f is our hash function and it would do a list find operation to find 'K'. In this case, the result would be that the list's current pointer would point at K.

As an example, the code for the insert function could be essentially one line:

HashArray[f(data)].insert(DataPair(data,position));

where DataPair is the class consisting of the pairs of data and position that occur in the lists that make up our hash table and where DataPair(data,position) is a call to the constructor for DataPair that creates a new object of type DataPair with data value data and position value position. The insert would, then, be the insert for the linked list at position f(data) in the HashArray.

Next

Lynn Ziegler, lziegler@csbsju.edu

W3C Wilbur Checked! Another HTML Validation Site