1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| //O(log2 n)
element delete_max_heap(int *n){
//delete element with the highest key from the heap
int parent, child;
element item,temp;
if(HEAP_EMPTY(*n)){
exit(1);
}
// save value of the element with the highest key
item = heap[1];
//use last element in heap to adjust heap
temp = heap[(*n)--];
parent =1;
child = 2;
while(child <= *n){
//find the larger child of the current parent
if ((child < *n) && (heap[child].key < heap[child+1].key))
child++;
if(temp.key >= heap[child].key) break;
//move to the next lower level
heap[parent] = heap[child];
parent = child;
child *= 2;
}
return item;
}
|