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.

lastKey()<br />

last().getValue()<br />

get(lastKey())<br />

successors(k)<br />

tailMap(k).entrySet().iterator()<br />

predecessors(k)<br />

headMap(k).entrySet().iterator()<br />

9.5.3 Flight <strong>Data</strong>bases <strong>and</strong> Maxima Sets<br />

As we have mentioned <strong>in</strong> the preced<strong>in</strong>g sections, unordered <strong>and</strong> ordered<br />

dictionaries have many applications.<br />

In this section, we explore some specific applications of ordered dictionaries.<br />

Flight <strong>Data</strong>bases<br />

There are several web sites on the Internet that allow users to perform queries on<br />

flight databases to f<strong>in</strong>d flights between various cities, typically with the <strong>in</strong>tent to<br />

buy a ticket. To make a query, a user specifies orig<strong>in</strong> <strong>and</strong> dest<strong>in</strong>ation cities, a<br />

departure date, <strong>and</strong> a departure time. To support such queries, we can model the<br />

flight database as a dictionary, where keys are Flight objects that conta<strong>in</strong> fields<br />

correspond<strong>in</strong>g to these four parameters. That is, a key is a tuple<br />

k = (orig<strong>in</strong>, dest<strong>in</strong>ation, date, time).<br />

Additional <strong>in</strong>formation about a flight, such as the flight number, the number of<br />

seats still available <strong>in</strong> first (F) <strong>and</strong> coach (Y) class, the flight duration, <strong>and</strong> the<br />

fare, can be stored <strong>in</strong> the value object.<br />

F<strong>in</strong>d<strong>in</strong>g a requested flight is not simply a matter of f<strong>in</strong>d<strong>in</strong>g a key <strong>in</strong> the dictionary<br />

match<strong>in</strong>g the requested query, however. The ma<strong>in</strong> difficulty is that, although a<br />

user typically wants to exactly match the orig<strong>in</strong> <strong>and</strong> dest<strong>in</strong>ation cities, as well as<br />

the departure date, he or she will probably be content with any departure time that<br />

is close to his or her requested departure time. We can h<strong>and</strong>le such a query, of<br />

course, by order<strong>in</strong>g our keys lexicographically. Thus, given a user query key k,<br />

we can call successors(k) to return an iteration of all the flights between the<br />

desired cities on the desired date, with departure times <strong>in</strong> strictly <strong>in</strong>creas<strong>in</strong>g order<br />

from the requested departure time. A similar use of predecessors(k) would<br />

give us flights with times before the requested time. Therefore, an efficient<br />

571

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

Saved successfully!

Ooh no, something went wrong!