-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
87 lines (78 loc) · 1.95 KB
/
heap.c
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "heap.h"
/*
Based on the pseudo-code in CLRS Introduction to Algorithms book.
*/
Heap new_heap(unsigned int size){
// creates heap[size+1] first node keeps heap information
Heap h = calloc(size + 1, sizeof(Hnode));
h[0].id = size;
h[0].val = 0;
return h;
}
void free_heap(Heap h){
free(h);
}
void max_heapify(Heap h, unsigned int idx){
unsigned int l = 0 ,r = 0, sz = 0, largest = 0;
l = left_node(idx);
r = right_node(idx);
sz = heap_size(h);
largest = idx;
if (l <= sz && h[l].val > h[idx].val){
largest = l;
}
if (r <= sz && h[r].val > h[largest].val){
largest = r;
}
if (largest != idx){
Hnode temp;
temp = h[idx];
h[idx] = h[largest];
h[largest] = temp;
max_heapify(h, largest);
}
}
void heap_increase_key(Heap h, unsigned int idx, Hnode value){
g_assert(value.val > h[idx].val);
h[idx] = value;
while(idx > 1 && h[parent_node(idx)].val < h[idx].val){
Hnode temp;
temp = h[idx];
h[idx] = h[parent_node(idx)];
h[parent_node(idx)] = temp;
idx = parent_node(idx);
}
}
void max_heap_insert(Heap h, Hnode value){
h[0].val += 1;
h[heap_size(h)].val = INT_MIN;
heap_increase_key(h, heap_size(h), value);
}
Hnode heap_max(Heap h){
return h[1];
}
Hnode heap_extract_max(Heap h){
g_assert(heap_size(h) >= 1);
Hnode max = h[1];
h[1] = h[heap_size(h)];
h[0].val -= 1;
max_heapify(h,1);
return max;
}
void heap_sort(Heap h){
unsigned int sz = h[0].val;
for(int i = heap_size(h) ; i > 1; i--){
Hnode temp = h[i];
h[i] = h[1];
h[1] = temp;
h[0].val -= 1;
max_heapify(h,1);
}
h[0].val = sz;
}
void heap_print(Heap h){
for(int i = 1 ; i <= heap_size(h); i++){
printf("%d %g ",h[i].id, h[i].val);
}
printf("\n");
}