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.

8.5. Exercises<br />

For source code <strong>and</strong> help with exercises, please visit<br />

java.datastructures.net.<br />

Re<strong>in</strong>forcement<br />

R-8.1<br />

Suppose you label each node v of a b<strong>in</strong>ary tree T with a key equal to the<br />

preorder rank of v. Under what circumstances is T a heap?<br />

R-8.2<br />

What is the output from the follow<strong>in</strong>g sequence of priority queue ADT<br />

methods: <strong>in</strong>sert(5,A), <strong>in</strong>sert(4,B), <strong>in</strong>sert(7,I), <strong>in</strong>sert(1,D),<br />

removeM<strong>in</strong>(), <strong>in</strong>sert(3,J), <strong>in</strong>sert(6,L), removeM<strong>in</strong>(), removeM<strong>in</strong>(),<br />

<strong>in</strong>sert(8,G), remove M<strong>in</strong>(), <strong>in</strong>sert(2,H), removeM<strong>in</strong>(),<br />

removeM<strong>in</strong>() ?<br />

R-8.3<br />

An airport is develop<strong>in</strong>g a computer simulation of air-traffic control that h<strong>and</strong>les<br />

events such as l<strong>and</strong><strong>in</strong>gs <strong>and</strong> takeoffs. Each event has a time-stamp that denotes<br />

the time when the event occurs. The simulation program needs to efficiently<br />

perform the follow<strong>in</strong>g two fundamental operations:<br />

• Insert an event with a given time-stamp (that is, add a future event).<br />

• Extract the event with smallest time-stamp (that is, determ<strong>in</strong>e the next<br />

event to process).<br />

Which data structure should be used for the above operations? Why?<br />

R-8.4<br />

Although it is correct to use a "reverse" comparator with the priority queue<br />

ADT so that we retrieve <strong>and</strong> remove an entry with the maximum key each time,<br />

it is confus<strong>in</strong>g to have an entry with maximum key returned by a method named<br />

"removeM<strong>in</strong>." Write a short adapter class that can take any priority queue P<br />

<strong>and</strong> an associated comparator C <strong>and</strong> implement a priority queue that<br />

concentrates on the element with maximum key, us<strong>in</strong>g methods with names like<br />

removeMax.<br />

R-8.5<br />

504

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

Saved successfully!

Ooh no, something went wrong!