Page 230, Exercise 5: Change Priority
void changePriority(int priority)
{ /*change priority of an item in the heap */
int position, newPriority, parent, child;
element item;
position = searchHeap(priority);
if (!(position)) {
printf("%d is not in the priority queue.\n", priority);
return;
}
printf("New Priority: ");
scanf("%d", &newPriority);
item.key = newPriority;
if (item.key > heap[position/2].key)
{/* new priority is higher than current priority */
while (1)
if ((position == 1) || (item.key <= heap[position / 2].key))
/* terminate when the root is reached or the element
is in its correct place */
break;
else {
/* check the next lower level of the heap */
heap[position] = heap[position/2];
position /= 2;
}
heap[position] = item;
}
else {/* new priority is lower, so go down the heap */
parent=position;
child=position *2;
while (child<=n) {
if (child |