# Homework 4

## Due: Thursday, November 10

Turn in this assignment on paper in class.

1. Question R-8.24 of textbook (p. 362): Draw an example of a heap whose keys are all the odd numbers from 1 to 59 (without repetition), such that inserting 32 into the heap will result in 32 bubbling up to a position as one of the children of the root. Draw the initial heap and the final heap. Then mark the path taken by 32 bubbling up in the final heap.

2. Question C-8.14 of textbook (p. 364): Given a min heap T and a key k, describe an algorithm that prints out the entries of T that are less than or equal to k (in any order). Your algorithm should run in time proportional to the number of items printed out.

Describe your algorithm in English. Pseudo-code is optional and does not replace an English description. Briefly justify the running time of your algorithm.

3. Suppose that we have a max heap stored as an array. You are given an index i for an item to be removed from the heap. Describe how this can be accomplished in O(log n) time.

Again, describe your algorithm in English. Pseudo-code is optional and does not replace an English description. Briefly justify the running time of your algorithm.

4. We insert the numbers 1, 2, 3, 4, ..., 2k - 1 in a Leftist Heap. Let's make that a min heap.
• Suppose that we insert the numbers in increasing order. What is the shape of the resulting Leftist Heap?
• Suppose that we insert the numbers in decreasing order (i.e., insert 2k - 1, 2k - 2, 2k - 3, ..., 3, 2, 1). What is the shape of the resulting Leftist Heap?
(Hint: use the visualization tool.) Argue that you are correct for any value of k.