Selected Solutions for Chapter 6:
Heapsort
Solution to Exercise 6.11
Since a heap is an almostcomplete binary tree (complete at all levels except pos
sibly the lowest), it has at most
2
h
C
1
NUL
1
elements (if it is complete) and at least
2
h
NUL
1
C
1
D
2
h
elements (if the lowest level has just 1 element and the other levels
are complete).
Solution to Exercise 6.12
Given an
n
element heap of height
h
, we know from Exercise 6.11 that
2
h
±
n
±
2
h
C
1
NUL
1 < 2
h
C
1
:
Thus,
h
±
lg
n < h
C
1
. Since
h
is an integer,
h
D b
lg
n
c
(by definition of
b c
).
Solution to Exercise 6.26
If you put a value at the root that is less than every value in the left and right
subtrees, then M
AX
H
EAPIFY
will be called recursively until a leaf is reached. To
make the recursive calls traverse the longest path to a leaf, choose values that make
M
AX
H
EAPIFY
always recurse on the left child. It follows the left branch when
the left child is greater than or equal to the right child, so putting 0 at the root
and 1 at all the other nodes, for example, will accomplish that. With such values,
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '10
 .
 Analysis of algorithms, AX H, UILD M AX

Click to edit the document details