This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: ECE608 Homework #5 Solution (1) CLR 6.16 No, ( 23 , 17 , 14 , 6 , 13 , 10 , 1 , 5 , 7 , 12 ) is not a heap because the heap property does not hold between the 4th element and its second child, the 9th element (i.e., 6 < 7). (2) CLR 6.17 Let i represent the index of a node in a heap. PROOF: Since a leaf in a heap is a node with no left son, for every leaf we should have; 2 i > n . That is; i = n/ 2 + 1 , n/ 2 + 2 , ..., n. (3) CLR 6.26 If you put a value at the root that is less than every value in the left and right subtrees, then MAXHEAPIFY 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 MAXHEAPIFY always recurse on the left child. It follows the left branch when the left child is the right child, so putting 0 at the root and 1 at all the other nodes, for example, will accomplish that. With such values, MAXHEAPIFY will be called h times (where h is the heap height, which is the number of edges in the longest path from the root to a leaf), so its running time will be ( h ) (since each call does (1) work), which is (lg n ). Since we have a case in which MAXHEAPIFY s running time is (lg n ), its worstcase running time is (lg n ). (4) CLR 6.33 Let H be the height of the heap. 1 Two subtleties to beware of: Be careful not to confuse the height of a node (longest distance from a leaf) with its depth (distance from the root). If the heap is not a complete binary tree (bottom level is not full), then the nodes at a given level (depth) dont all have the same height. For example, although all nodes at depth H have height 0, nodes at depth H 1 can have either height 0 or height 1. For a complete binary tree, its easy to show that there are ceilingleftbigg n 2 h +1 ceilingrightbigg nodes of height h . But the proof for an incomplete tree is tricky, and is not derived from the proof for a complete tree. The proof is by induction on h . Base case: Show that its true for h = 0 (i.e., that # leaves ceilingleftbigg n 2 h +1 ceilingrightbigg = ceilingleftbigg n 2 ceilingrightbigg ). The nodes at height 0 (i.e., the tree leaves) are at depths H and H 1. They consist of all nodes at depth H the nodes at depth H 1 that are not parents of depth H nodes Let x be the number of nodes at depth H that is, the number of nodes in the bottom (possibly incomplete) level. Note that n x is odd, because the n x nodes above the bottom level form a complete binary tree, and a complete binary tree has an odd number of nodes (1 less than a power of 2). Thus if n is odd, x is even, and if n is even, x is odd....
View Full
Document
 Fall '11
 PAI

Click to edit the document details