Page 230, Exercise 6: Remove Priority
void removePriority(int priority)
{/*Remove an arbitrary item from the priority queue */
int position, newPriority, parent, child;
element item;
position = searchHeap(priority);
if (!(position)) {
printf("%d is not in the priority queue.\n", priority);
return;
}
item = topPriority();
item.key++;
/* new priority is higher than top priority
sift heap upward*/
while(1)
if ((position == 1) || (item.key <= heap[position / 2].key))
break;
else {
heap[position] = heap[position/2];
position /= 2;
}
heap[position] = item;
/* remove it from the heap, since it's now at the top */
item = DeleteMaxHeap();
} |