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.

e 4<br />

{(9,C)}<br />

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

e 2<br />

{}<br />

The java.util.PriorityQueue Class<br />

There is no priority queue <strong>in</strong>terface built <strong>in</strong>to <strong>Java</strong>, but <strong>Java</strong> does <strong>in</strong>clude a class,<br />

java.util.PriorityQueue, which implements the java.util.Queue<br />

<strong>in</strong>terface. Instead of add<strong>in</strong>g <strong>and</strong> remov<strong>in</strong>g elements accord<strong>in</strong>g to the FIFO policy,<br />

however, which is the st<strong>and</strong>ard queue policy, the<br />

java.util.PriorityQueue class processes its entries accord<strong>in</strong>g to a<br />

priority. This priority is def<strong>in</strong>ed by a given comparator object, which is passed to<br />

the queue <strong>in</strong> a constructor, or it is def<strong>in</strong>ed by the natural order<strong>in</strong>g of the elements<br />

be<strong>in</strong>g stored <strong>in</strong> the queue. Even though the java.util.PriorityQueue is<br />

based on the java.util.Queue <strong>in</strong>terface, we can def<strong>in</strong>e a simple<br />

correspondence between the methods of this class <strong>and</strong> our priority queue ADT, as<br />

shown <strong>in</strong> Table 8.1, assum<strong>in</strong>g we have a class, PQEntry, which implements the<br />

Entry <strong>in</strong>terface.<br />

Table 8.1: Methods of our priority queue ADT <strong>and</strong><br />

correspond<strong>in</strong>g methods of class<br />

java.util.PriorityQueue. We assume that the<br />

comparator for PQEntry objects is essentially the<br />

same as the comparator for the keys of the priority<br />

queue. Note that java.util.PriorityQueue has a<br />

pair of methods for its ma<strong>in</strong> operation. The two<br />

methods have similar functionality, with m<strong>in</strong>or<br />

differences <strong>in</strong> the way they deal with boundary<br />

conditions (e.g., try<strong>in</strong>g to remove from an empty<br />

priority queue).<br />

Priority Queue ADT<br />

Classjava.util.PriorityQueue<br />

458

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

Saved successfully!

Ooh no, something went wrong!