23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

9.2.6 A <strong>Java</strong> Hash Table Implementation<br />

In Code Fragments 9.3–9.5, we show class, HashTableMap, which implements<br />

the map ADT us<strong>in</strong>g a hash table with l<strong>in</strong>ear prob<strong>in</strong>g to resolve collisions. These<br />

code fragments <strong>in</strong>clude the entire implementation of the map ADT, except for the<br />

methods values() <strong>and</strong> entries(), which we leave as an Exercise (R-9.10).<br />

The ma<strong>in</strong> design elements of the <strong>Java</strong> class HashTableMap are as follows:<br />

• We ma<strong>in</strong>ta<strong>in</strong>, <strong>in</strong> <strong>in</strong>stance variables, the size, n, of the map, the bucket<br />

array, A, <strong>and</strong> the capacity, N, of A.<br />

• We use method hash Value to compute the hash function of a key by<br />

means of the built-<strong>in</strong> hashCode method <strong>and</strong> the multiply-add-<strong>and</strong>-divide (MAD)<br />

compression function.<br />

• We def<strong>in</strong>e a sent<strong>in</strong>el, AVAILABLE, as a marker for deactivated entries.<br />

• We provide an optional constructor that allows us to specify the <strong>in</strong>itial<br />

capac ity of the bucket array.<br />

• If the current bucket array is full <strong>and</strong> one tries to <strong>in</strong>sert a new entry, we<br />

rehash the entire contents <strong>in</strong>to a new array that is twice the size as the old version.<br />

• The follow<strong>in</strong>g (protected) auxiliary methods are used:<br />

checkKey(k), which checks if the key k is valid. This method currently<br />

just checks that k is not null, but a class that extends HashTableMap can<br />

override this method with a more elaborate test.<br />

rehash(), which computes a new MAD hash function with r<strong>and</strong>om pa<br />

rameters <strong>and</strong> rehashes the entries <strong>in</strong>to a new array with double capacity.<br />

f<strong>in</strong>dEntry(k), which looks for an entry with key equal to k, start<strong>in</strong>g at<br />

the <strong>in</strong>dex A[h(k)] <strong>and</strong> go<strong>in</strong>g through the array <strong>in</strong> a circular fashion. If the method<br />

f<strong>in</strong>ds a cell with such an entry, then it returns the <strong>in</strong>dex i of this cell. Otherwise, it<br />

returns -i-1, where i is the <strong>in</strong>dex of the last empty or available cell encountered.<br />

Code Fragment 9.3: Class HashTableMap<br />

implement<strong>in</strong>g the map ADT us<strong>in</strong>g a hash table with<br />

l<strong>in</strong>ear prob<strong>in</strong>g. (Cont<strong>in</strong>ues <strong>in</strong> Code Fragment 9.4.)<br />

536

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!