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.

for entries with duplicate keys. Assum<strong>in</strong>g that the load factor of our hash table is<br />

kept below 1, our hash function spreads entries fairly uniformly, <strong>and</strong> we use<br />

separate cha<strong>in</strong><strong>in</strong>g to resolve collisions, then we can achieve O(1)-time performance<br />

for the f<strong>in</strong>d, remove, <strong>and</strong> <strong>in</strong>sert methods <strong>and</strong> O(1 + m)-time performance<br />

for the f<strong>in</strong>dAll method, where m is the number of entries returned.<br />

In addition, we can simplify the algorithms for implement<strong>in</strong>g this dictionary, if we<br />

assume we have a list-based dictionary stor<strong>in</strong>g the entries at each cell <strong>in</strong> the bucket<br />

array A. Such an assumption would be <strong>in</strong> keep<strong>in</strong>g with our use of separate cha<strong>in</strong><strong>in</strong>g,<br />

s<strong>in</strong>ce each cell would be a list. This approach allows us to implement the ma<strong>in</strong><br />

dictionary methods as shown <strong>in</strong> Code Fragment 9.8.<br />

Code Fragment 9.8: Some of the ma<strong>in</strong> methods<br />

for a dictionary D, implemented with a hash table that<br />

uses a bucket array, A, <strong>and</strong> an unordered list for each<br />

cell <strong>in</strong> A. We use n to denote the number of entries <strong>in</strong><br />

D, N to denote the capacity of A, <strong>and</strong> λ to denote the<br />

maximum load factor for the hash table.<br />

551

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

Saved successfully!

Ooh no, something went wrong!