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.

emoveM<strong>in</strong> operations play an important role. We consider such an application<br />

<strong>in</strong> the example that follows.<br />

Example 8.1: Suppose a certa<strong>in</strong> flight is fully booked an hour prior to<br />

departure. Because of the possibility of cancellations, the airl<strong>in</strong>e ma<strong>in</strong>ta<strong>in</strong>s a<br />

priority queue of st<strong>and</strong>by passengers hop<strong>in</strong>g to get a seat. The priority of each<br />

st<strong>and</strong>by passenger is determ<strong>in</strong>ed by the airl<strong>in</strong>e tak<strong>in</strong>g <strong>in</strong>to account the fare paid,<br />

the frequent-flyer status, <strong>and</strong> the time that the passenger is <strong>in</strong>serted <strong>in</strong>to the<br />

priority queue. A st<strong>and</strong>by passenger reference is <strong>in</strong>serted <strong>in</strong>to the priority queue<br />

with an <strong>in</strong>sert operation as soon as he or she requests to fly st<strong>and</strong>by. Shortly<br />

before the flight departure, if seats become available (for example, due to noshows<br />

or last-m<strong>in</strong>ute cancellations), the airl<strong>in</strong>e removes a st<strong>and</strong>by passenger with<br />

first priority from the priority queue, us<strong>in</strong>g aremoveM<strong>in</strong> operation <strong>and</strong> lets this<br />

person board. This process is then repeated until all available seats have been<br />

filled or the priority queue becomes empty.<br />

8.1.2 Entries <strong>and</strong> Comparators<br />

There are still two important issues that we have left undeterm<strong>in</strong>ed to this po<strong>in</strong>t:<br />

• How do we keep track of the associations between keys <strong>and</strong> values?<br />

• How do we compare keys so as to determ<strong>in</strong>e a smallest key?<br />

Answer<strong>in</strong>g these questions <strong>in</strong>volves the use of two <strong>in</strong>terest<strong>in</strong>g design patterns.<br />

The def<strong>in</strong>ition of a priority queue implicitly makes use of two special k<strong>in</strong>ds of<br />

objects that answer the above questions, the entry <strong>and</strong> comparator, which we<br />

discuss <strong>in</strong> this subsection.<br />

Entries<br />

An entry is an association between a key k <strong>and</strong> a value x, that is, an entry is<br />

simply a key-value pair. We use entries <strong>in</strong> a priority queue Q to keep track of the<br />

way Q is associat<strong>in</strong>g keys <strong>and</strong> their correspond<strong>in</strong>g values.<br />

An entry is actually an example of a more general object-oriented design pattern,<br />

the composition pattern, which def<strong>in</strong>es a s<strong>in</strong>gle object that is composed of other<br />

objects. We use this pattern <strong>in</strong> a priority queue when we def<strong>in</strong>e the entries be<strong>in</strong>g<br />

stored <strong>in</strong> the priority queue to be pairs consist<strong>in</strong>g of a key k <strong>and</strong> a value x. A pair<br />

is the simplest composition, for it comb<strong>in</strong>es two objects <strong>in</strong>to a s<strong>in</strong>gle pair object.<br />

To implement this concept, we def<strong>in</strong>e a class that stores two objects <strong>in</strong> its first <strong>and</strong><br />

second <strong>in</strong>stance variables, respectively, <strong>and</strong> provides methods to access <strong>and</strong><br />

update these variables. In Code Fragment 8.1, we show an implementation of the<br />

composition pattern for entries stor<strong>in</strong>g key-value pairs <strong>in</strong> a priority queue. We<br />

realize this composition with an <strong>in</strong>terface called Entry (the java.util<br />

452

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

Saved successfully!

Ooh no, something went wrong!