Homework 3 Solution Guide
1)
Idea
The important thing to note is that we need to find the kth smallest number in
O(klog
k
) not O(klogn) time. Because of this, we can never perform a removemin
operation on the original binary search tree, because this would be an O(log n) time
operation.
However, the heap property can be exploited to access the k smallest elements in
the heap. Notice that in a min heap, the smallest element is at the top of the heap, and the
next smallest element must be one of the children of this node. This is because the heap
property requires that any child of a node be greater than its parent.
To solve this problem, we will create another min heap. The entries in this heap
will be stored based on the same key, however an additional value will be stored that
indicates the index in the original array where the key was stored. Note that the children
of any node in an array based heap can be found in O(1) time from section 4.3.2 of
Manber on heaps.
The following procedure can be used. Take the first item in the original heap and
add it to the new heap. This initializes the new heap. Next, remove the top item from the
new heap. This is the minimum element not yet considered. Take the index into the
original array, and use this to calculate the children of this node in constant time. Add
those children to the new heap, there will only be two of them. Removing the next item
from the new heap finds the next smallest node. Repeat this process until the kth item has
been reached.
The second heap used is the same as a normal heap, except a second item is stored
with each key. Specifically each item contains two integers {key, index}. The second
heap maintains the heap property on the key, and the index is the position in the original
array for the original heap where that key came from.
It is assumed that K<=N.
PseudoCode
find_kth()
{
int K = desired value;
int original_heap[N];
Heap second_heap;//heap per pg 76 of Manber,
int count =1;
Item last_value = original_heap[0];
//lowest is stored at original_heap[0]
add_children(0);
while(count<K)
{
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document//remove 1 item from second_heap
{next_value, next_index} = second_heap.remove_min();
//add 2 items to second_heap
add_children(next_index);
count++;
}
return last_value;
}
void add_children(int location)
{
//note that the left child is at 2i and the right at 2i+1
//if the index of the array is 1 based
int left = 2i+1//formula for left child
int right = 2i+2//formula for right child
if(left>=0 && left<N)
second_heap.add({original_heap[left],left});
if(right>=0 && left<N)
second_heap.add({original_heap[right],right});
}
Complexity
Notice that every time this procedure is used, the new heaps size increases by one.
Running this procedure k times finds the kth smallest node, with the new heap size being
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '11
 游啟璋
 Manber

Click to edit the document details