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.6 Bottom-Up Heap Construction <br />

The analysis of the heap-sort algorithm shows that we can construct a heap stor<strong>in</strong>g<br />

n entries <strong>in</strong> O(nlogn) time, by means of n successive <strong>in</strong>sert operations, <strong>and</strong> then use<br />

that heap to extract the entries <strong>in</strong> order by nondecreas<strong>in</strong>g key. However, if all the n<br />

key-value pairs to be stored <strong>in</strong> the heap are given <strong>in</strong> advance, there is an al ternative<br />

bottom-up construction method that runs <strong>in</strong> O(n) time. We describe this method <strong>in</strong><br />

this section, observ<strong>in</strong>g that it could be <strong>in</strong>cluded as one of the constructors of a class<br />

implement<strong>in</strong>g a heap-based priority queue. For simplicity of exposition, we<br />

describe this bottom-up heap construction assum<strong>in</strong>g the number n of keys is an<br />

<strong>in</strong>teger of the type n = 2 h + 1 − 1. That is, the heap is a complete b<strong>in</strong>ary tree with<br />

every level be<strong>in</strong>g full, so the heap has height h = log(n+ 1) − 1. Viewed nonre<br />

491

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

Saved successfully!

Ooh no, something went wrong!