I have the code here from Skiena's manual:
int heap_compare(priority_queue *q, int i, int count, int x)
{
if ((count <= 0) || (i > q->n)) return(count);
if (q->q[i] < x) {
count = heap_compare(q, pq_young_child(i), count-1, x);
count = heap_compare(q, pq_young_child(i)+1, count, x);
}
return(count);
}
I don't understand why the count is not being decremented for the right child of the node?
The count does not decrease as when you go towards the right subtree the current node is counted as one of the "k-1" smaller elements and when we move towards the left the current node is not included in the "k" smallest elements.