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.

8.3.3 Implement<strong>in</strong>g a Priority Queue with a Heap<br />

We now discuss how to implement a priority queue us<strong>in</strong>g a heap. Our heap-based<br />

representation for a priority queue P consists of the follow<strong>in</strong>g (see Figure 8.6):<br />

• heap, a complete b<strong>in</strong>ary tree T whose <strong>in</strong>ternal nodes store entries so that<br />

the heap-order property is satisfied. We assume T is implemented us<strong>in</strong>g an array<br />

list, as described <strong>in</strong> Section 8.3.2. For each <strong>in</strong>ternal node v of T, we denote the key<br />

of the entry stored at v as k(v).<br />

• comp, a comparator that def<strong>in</strong>es the total order relation among the keys.<br />

With this data structure, methods size <strong>and</strong> isEmpty take O(1) time, as usual. In<br />

addition, method m<strong>in</strong> can also be easily performed <strong>in</strong> O(1) time by access<strong>in</strong>g the<br />

entry stored at the root of the heap (which is at <strong>in</strong>dex 1 <strong>in</strong> the array list).<br />

Insertion<br />

Let us consider how to perform <strong>in</strong>sert on a priority queue implemented with a<br />

heap T. To store a new entry (k,x) <strong>in</strong>to T we add a new node z to T with operation<br />

add so that this new node becomes the last node of T <strong>and</strong> stores entry (k,x).<br />

After this action, the tree T is complete, but it may violate the heap-order<br />

property. Hence, unless node z is the root of T (that is, the priority queue was<br />

empty before the <strong>in</strong>sertion), we compare key k(z) with the key k(u) stored at the<br />

parent u of z. If k(z) ≥ k(u), the heap-order property is satisfied <strong>and</strong> the algorithm<br />

term<strong>in</strong>ates. If <strong>in</strong>stead k(z) < k(u), then we need to restore the heap-order property,<br />

which can be locally achieved by swapp<strong>in</strong>g the entries stored at z <strong>and</strong> u. (See<br />

Figure 8.7c <strong>and</strong> d.) This swap causes the new entry (k,e) to move up one level.<br />

Aga<strong>in</strong>, the heap-order property may be violated, <strong>and</strong> we cont<strong>in</strong>ue swapp<strong>in</strong>g, go<strong>in</strong>g<br />

up <strong>in</strong> T until no violation of the heap-order property occurs. (See Figure 8.7e <strong>and</strong><br />

h.)<br />

Figure 8.6: Illustration of the heap-based<br />

implementation of a priority queue.<br />

479

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

Saved successfully!

Ooh no, something went wrong!