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.

An efficient realization of a priority queue uses a data structure called a heap. This<br />

data structure allows us to perform both <strong>in</strong>sertions <strong>and</strong> removals <strong>in</strong> logarithmic time,<br />

which is a significant improvement over the list-based implementations discussed <strong>in</strong><br />

Section 8.2. The fundamental way the heap achieves this improvement is to ab<strong>and</strong>on<br />

the idea of stor<strong>in</strong>g entries <strong>in</strong> a list <strong>and</strong> take the approach of stor<strong>in</strong>g entries <strong>in</strong> a b<strong>in</strong>ary<br />

tree <strong>in</strong>stead.<br />

8.3.1 The Heap <strong>Data</strong> Structure<br />

A heap (see Figure 8.3) is a b<strong>in</strong>ary tree T that stores a collection of entries at its<br />

nodes <strong>and</strong> that satisfies two additional properties: a relational property def<strong>in</strong>ed <strong>in</strong><br />

terms of the way keys are stored <strong>in</strong> T <strong>and</strong> a structural property def<strong>in</strong>ed <strong>in</strong> terms of<br />

the nodes of T itself. We assume that a total order relation on the keys is given, for<br />

example, by a comparator.<br />

The relational property of T, def<strong>in</strong>ed <strong>in</strong> terms of the way keys are stored, is the<br />

follow<strong>in</strong>g:<br />

Heap-Order Property: In a heap T, for every node v other than the root, the key<br />

stored at v is greater than or equal to the key stored at v's parent.<br />

As a consequence of the heap-order property, the keys encountered on a path from<br />

the root to an external node of T are <strong>in</strong> nondecreas<strong>in</strong>g order. Also, a m<strong>in</strong>imum key<br />

is always stored at the root of T. This is the most important key <strong>and</strong> is <strong>in</strong>formally<br />

said to be "at the top of the heap"; hence, the name "heap" for the data structure. By<br />

the way, the heap data structure def<strong>in</strong>ed here has noth<strong>in</strong>g to do with the memory<br />

heap (Section 14.1.2) used <strong>in</strong> the run-time environment support<strong>in</strong>g a programm<strong>in</strong>g<br />

language like <strong>Java</strong>.<br />

If we def<strong>in</strong>e our comparator to <strong>in</strong>dicate the opposite of the st<strong>and</strong>ard total order<br />

relation between keys (so that, for example, compare(3,2) > 0), then the root of the<br />

heap stores the largest key. This versatility comes essentially "for free" from our<br />

use of the comparator pattern. By def<strong>in</strong><strong>in</strong>g the m<strong>in</strong>imum key <strong>in</strong> terms of the<br />

comparator, the "m<strong>in</strong>imum" key with a "reverse" comparator is <strong>in</strong> fact the largest.<br />

Figure 8.3: Example of a heap stor<strong>in</strong>g 13 entries<br />

with <strong>in</strong>teger keys. The last node is the one stor<strong>in</strong>g entry<br />

(8, W).<br />

469

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

Saved successfully!

Ooh no, something went wrong!