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.

wait<strong>in</strong>g list. Thus, we would like to remove from the priority queue the entry<br />

associated with this passenger. Operation removeM<strong>in</strong> is not suitable for this<br />

purpose s<strong>in</strong>ce the passenger leav<strong>in</strong>g is unlikely to have first priority. Instead, we<br />

would like to have a new operation remove (e) that removes an arbitrary entry e.<br />

• Another st<strong>and</strong>by passenger f<strong>in</strong>ds her gold frequent-flyer card <strong>and</strong> shows it to the<br />

agent. Thus, her priority has to be modified accord<strong>in</strong>gly. To achieve this change of<br />

priority, we would like to have a new operation replaceKey(e,k) that replaces<br />

with k the key of entry e <strong>in</strong> the priority queue.<br />

• F<strong>in</strong>ally, a third st<strong>and</strong>by passenger notices her name is misspelled on the ticket <strong>and</strong><br />

asks it to be corrected. To perform the change, we need to up date the passenger's<br />

record. Hence, we would like to have a new operation replaceValue(e,x) that<br />

replaces with x the value of entry e <strong>in</strong> the priority queue.<br />

8.4.1 Methods of the Adaptable Priority Queue ADT<br />

The above scenarios motivate the def<strong>in</strong>ition of a new ADT that extends the prior ity<br />

queue ADT with methods remove, replaceKey, <strong>and</strong> replaceValue.<br />

Namely, an adaptable priority queue P supports the follow<strong>in</strong>g methods <strong>in</strong> addition<br />

to those of the priority queue ADT:<br />

remove(e): Remove from P <strong>and</strong> return entry e.<br />

replaceKey(e,k): Replace with k <strong>and</strong> return the key of entry e of P; an<br />

be<br />

error condition occurs if k is <strong>in</strong>valid (that is, k cannot<br />

compared with other keys).<br />

replaceValue(e,x): Replace with x <strong>and</strong> return the value of entry e of P.<br />

Example 8.8: The follow<strong>in</strong>g table shows a series of operations <strong>and</strong> their effects<br />

on an <strong>in</strong>itially empty adaptable priority queue P.<br />

Operation<br />

Output<br />

P<br />

<strong>in</strong>sert(5,A)<br />

e 1<br />

{(5,A)}<br />

496

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

Saved successfully!

Ooh no, something went wrong!