-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathheap.c
62 lines (47 loc) · 1.1 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
#include "heap.h"
#include <stdlib.h>
#include <string.h>
heap_p create_heap(heapcmpfunc cmpfunc){
heap_p hp = (heap_p) malloc(sizeof(struct heap));
hp->vec = create_vector();
hp->cmpfunc = cmpfunc;
return hp;
}
void destroy_heap(heap_p hp){
destroy_vector(hp->vec);
free(hp);
}
void heapify(heap_p hp, int i){
int largest;
int l = LEFT(i);
int r = RIGHT(i);
if(l < hp->vec->length && hp->cmpfunc(hp->vec, l, i) > 0)
largest = l;
else largest = i;
if(r < hp->vec->length && hp->cmpfunc(hp->vec, r, largest) > 0)
largest = r;
if(largest != i){
vector_swap(hp->vec, i, largest);
heapify(hp, largest);
}
}
void build_heap(heap_p hp){
int i;
for(i=PARENT(hp->vec->length-1); i>=0; i--){
heapify(hp, i);
}
}
void heap_remove(heap_p hp){
vector_swap(hp->vec, 0, hp->vec->length-1);
vector_remove(hp->vec, hp->vec->length-1);
heapify(hp, 0);
}
void heap_insert(heap_p hp, void * val, int size){
int i;
vector_add(hp->vec, val, size);
for(i=hp->vec->length-1; i>0; i=PARENT(i)){
if(hp->cmpfunc(hp->vec, PARENT(i), i) > 0)
break;
vector_swap(hp->vec, PARENT(i), i);
}
}