# Homework 4

## Due: Thursday, November 10

**Turn in this assignment on paper in class.**

- 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.
- 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.

- 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.

- We insert the numbers 1, 2, 3, 4, ..., 2
^{k}- 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
2
^{k}- 1, 2^{k}- 2, 2^{k}- 3, ..., 3, 2, 1). What is the shape of the resulting Leftist Heap?

*k*.