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 the keys, <strong>and</strong> we provide additional methods that refer to this order<strong>in</strong>g (see<br />

Section 9.5.2). In an unordered dictionary, however, no order relation is assumed on<br />

the keys; hence, only equality test<strong>in</strong>g between keys is used.<br />

As an ADT, an (unordered) dictionary D supports the follow<strong>in</strong>g methods:<br />

size(): Return the number of entries <strong>in</strong> D.<br />

isEmpty(): Test whether D is empty.<br />

f<strong>in</strong>d(k): If D conta<strong>in</strong>s an entry with key equal to k, then return such an<br />

entry, else return null.<br />

f<strong>in</strong>dAll(k): Return an iterable collection conta<strong>in</strong><strong>in</strong>g all entries with key<br />

equal to k.<br />

<strong>in</strong>sert(k,v): Insert an entry with key k <strong>and</strong> value v <strong>in</strong>to D, return<strong>in</strong>g the<br />

entry created.<br />

remove(e): Remove from D an entry e, return<strong>in</strong>g the removed entry or<br />

null if e was not <strong>in</strong> D.<br />

entries(): Return an iterable collection of the key-value entries <strong>in</strong> D.<br />

Notice that our dictionary operations use entries, which are the key-value pairs stored<br />

<strong>in</strong> the dictionary. We assume each entry comes equipped with getKey() <strong>and</strong><br />

getValue() methods to access its key <strong>and</strong> value components respectively.<br />

When the method f<strong>in</strong>d(k) is unsuccessful (that is, there is no entry with key equal to<br />

k), we use the convention of return<strong>in</strong>g a sent<strong>in</strong>el null. Another choice, of course,<br />

would be to throw an exception for an unsuccessful f<strong>in</strong>d(k), but that would probably<br />

not be an appropriate use of an exception, s<strong>in</strong>ce it is normal to ask for a key that<br />

might not be <strong>in</strong> our dictionary. Moreover, throw<strong>in</strong>g <strong>and</strong> catch<strong>in</strong>g an exception is<br />

typically slower than a test aga<strong>in</strong>st a sent<strong>in</strong>el; hence, us<strong>in</strong>g a sent<strong>in</strong>el is more efficient.<br />

Note that, as we have def<strong>in</strong>ed it, a dictionary D can conta<strong>in</strong> different entries with<br />

equal keys. In this case, operation f<strong>in</strong>d(k) returns an arbitrary entry (k,v), whose<br />

key is equal to k. We mention, <strong>in</strong> pass<strong>in</strong>g, that our dictionary ADT should not be<br />

confused with the abstract class java.util. Dictionary, which actually<br />

corresponds to the map ADT given above <strong>and</strong> is now considered obsolete.<br />

Example 9.2: In the follow<strong>in</strong>g, we show a series of operations on an <strong>in</strong>itially<br />

empty dictionary stor<strong>in</strong>g entries with <strong>in</strong>teger keys <strong>and</strong> character values.<br />

Operation<br />

Output<br />

545

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

Saved successfully!

Ooh no, something went wrong!