chap6-solutions - Selected Solutions for Chapter 6:...

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

View Full Document Right Arrow Icon
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 N 1 elements (if it is complete) and at least 2 h N 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 N 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 MAX-HEAPIFY 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 MAX-HEAPIFY 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,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

chap6-solutions - Selected Solutions for Chapter 6:...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online