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.

O(n)<br />

O(logn) (expected)<br />

9.5.2 The Ordered Dictionary ADT<br />

In an ordered dictionary, we want to perform the usual dictionary operations, but<br />

also ma<strong>in</strong>ta<strong>in</strong> an order relation for the keys <strong>in</strong> our dictionary. We can use a<br />

comparator to provide the order relation among keys, as we did for the ordered<br />

search table <strong>and</strong> skip list dictionary implementations described above. Indeed, all of<br />

the dictionary implementations discussed <strong>in</strong> Chapter 10 use a comparator to store<br />

the dictionary <strong>in</strong> nondecreas<strong>in</strong>g key order.<br />

When the entries of a dictionary are stored <strong>in</strong> order, we can provide efficient<br />

implementations for additional methods <strong>in</strong> the dictionary ADT. For example, we<br />

could consider add<strong>in</strong>g the follow<strong>in</strong>g methods to the dictionary ADT so as to def<strong>in</strong>e<br />

the ordered dictionary ADT.<br />

first(): Return an entry with smallest key.<br />

last(): Return an entry with largest key.<br />

successors(k): Return an iterator of the entries with keys greater than or<br />

equal to k, <strong>in</strong> nondecreas<strong>in</strong>g order.<br />

predecessors(k): Return an iterator of the entries with keys less than or equal to<br />

k, <strong>in</strong> non<strong>in</strong>creas<strong>in</strong>g order.<br />

Implement<strong>in</strong>g an Ordered Dictionary<br />

The ordered nature of the operations above makes the use of an unordered list or a<br />

hash table <strong>in</strong>appropriate for implement<strong>in</strong>g the dictionary, because neither of these<br />

data structures ma<strong>in</strong>ta<strong>in</strong>s any order<strong>in</strong>g <strong>in</strong>formation for the keys <strong>in</strong> the dictionary.<br />

Indeed, hash tables achieve their best search speeds when their keys are<br />

distributed almost at r<strong>and</strong>om. Thus, we should consider an ordered search table or<br />

skip list (or a data structure from Chapter 10) when deal<strong>in</strong>g with ordered<br />

dictionaries.<br />

for example, us<strong>in</strong>g a skip list to implement an ordered dictionary, we can<br />

implement methods first() <strong>and</strong> last() <strong>in</strong> O(1) time by access<strong>in</strong>g the second<br />

<strong>and</strong> second to last positions of the bottom list. Also methods successors(k)<br />

<strong>and</strong> predecessors(k) can be implemented to run <strong>in</strong> O(logn) expected time.<br />

Moreover, the iterators returned by the successors(k) <strong>and</strong><br />

predecessors(k) methods could be implemented us<strong>in</strong>g a reference to a current<br />

569

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

Saved successfully!

Ooh no, something went wrong!