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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

of entries <strong>in</strong> the dictionary at the time the operation is<br />

performed with n, <strong>and</strong> the size of the collection<br />

returned by operation f<strong>in</strong>dAll with s. The expected<br />

space requirement is O(n).<br />

Operation<br />

Time<br />

size, isEmpty<br />

O(1)<br />

entries<br />

O(n)<br />

f<strong>in</strong>d, <strong>in</strong>sert, remove<br />

O(logn) (expected)<br />

f<strong>in</strong>dAll<br />

O(logn + s) (expected)<br />

9.5 Extensions <strong>and</strong> Applications of Dictionaries<br />

In this section, we explore several extensions <strong>and</strong> applications of dictionaries.<br />

9.5.1 Support<strong>in</strong>g Location-Aware Dictionary Entries<br />

As we did for priority queues (Section 8.4.2), we can also use location-aware<br />

entries to speed up the runn<strong>in</strong>g time for some operations <strong>in</strong> a dictionary. In<br />

particular, a location-aware entry can greatly speed up entry removal <strong>in</strong> a<br />

dictionary. For <strong>in</strong> remov<strong>in</strong>g a location-aware entry e, we can simply go directly to<br />

the place <strong>in</strong> our data structure where we are stor<strong>in</strong>g e <strong>and</strong> remove it. We could<br />

implement a location-aware entry, for example, by augment<strong>in</strong>g our entry class with<br />

a private location variable <strong>and</strong> protected methods, location() <strong>and</strong><br />

setLocation(p), which return <strong>and</strong> set this variable respectively. We then require<br />

that the location variable for an entry e, always refer to e's position or <strong>in</strong>dex <strong>in</strong><br />

the data structure implement<strong>in</strong>g our dictionary. We would, of course, have to update<br />

this variable any time we moved an entry, so it would probably make the most<br />

sense for this entry class to be closely related to the class implement<strong>in</strong>g the<br />

dictionary (the location-aware entry class could even be nested <strong>in</strong>side the dictionary<br />

567

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

Saved successfully!

Ooh no, something went wrong!