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.

Proposition 8.5: A heap T stor<strong>in</strong>g n entries has height<br />

h = logn.<br />

Justification: From the fact that T is complete, we know that the number<br />

of nodes of T is at least<br />

1 + 2 + 4 + … + 2 h−1 + 1 = 2 h − 1 + 1<br />

= 2 h .<br />

This lower bound is achieved when there is only one node on level h. In addition,<br />

also follow<strong>in</strong>g from T be<strong>in</strong>g complete, we have that the number of nodes of T is at<br />

most<br />

1 + 2 + 4 + … + 2 h = 2 h + 1 − 1.<br />

This upper bound is achieved when level h has 2h nodes. S<strong>in</strong>ce the number of<br />

nodes is equal to the number n of entries, we obta<strong>in</strong><br />

2 h ≤ n<br />

<strong>and</strong><br />

n ≤ 2 h+1 − 1.<br />

Thus, by tak<strong>in</strong>g logarithms of both sides of these two <strong>in</strong>equalities, we see that<br />

h ≤ log n<br />

<strong>and</strong><br />

log(n + 1) − 1 ≤ h.<br />

S<strong>in</strong>ce h is an <strong>in</strong>teger, the two <strong>in</strong>equalities above imply that<br />

h = logn.<br />

Proposition 8.5 has an important consequence, for it implies that if we can<br />

perform update operations on a heap <strong>in</strong> time proportional to its height, then those<br />

operations will run <strong>in</strong> logarithmic time. Let us therefore turn to the problem of<br />

how to efficiently perform various priority queue methods us<strong>in</strong>g a heap.<br />

8.3.2 Complete B<strong>in</strong>ary Trees <strong>and</strong> Their Representation<br />

Let us discuss more about complete b<strong>in</strong>ary trees <strong>and</strong> how they are represented.<br />

471

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

Saved successfully!

Ooh no, something went wrong!