{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chap6-solutions

# chap6-solutions - Selected Solutions for Chapter 6 Heapsort...

This preview shows pages 1–2. Sign up to view the full content.

Selected Solutions for Chapter 6: Heapsort Solution to Exercise 6.1-1 Since a heap is an almost-complete 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.1-2 Given an n -element heap of height h , we know from Exercise 6.1-1 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.2-6 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.

{[ snackBarMessage ]}