-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_heap.c
132 lines (97 loc) · 2.8 KB
/
binary_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "binary_heap.h"
int getLeftChildIndex(int parentIndex){ return 2 * parentIndex + 1; }
int getRightChildIndex(int parentIndex){ return 2 * parentIndex + 2; }
int getParentIndex(int childIndex){ return (childIndex - 1) / 2; }
bool hasLeftChild(heap *h, int index){ return getLeftChildIndex(index) < h->capacity; }
bool hasRightChild(heap *h, int index){ return getRightChildIndex(index) < h->capacity; }
bool hasParent(int index){ return getParentIndex(index) >= 0; }
int leftChild(heap *h, int index){ return h->h[getLeftChildIndex(index)]; }
int rightChild(heap *h, int index){ return h->h[getRightChildIndex(index)]; }
int parent(heap *h, int index){ return h->h[getParentIndex(index)]; }
void swap(int *heap, int indexOne, int indexTwo){
int tmp = heap[indexOne];
heap[indexOne] = heap[indexTwo];
heap[indexTwo] = tmp;
}
heap *increseHeapSize(heap *h){
h->capacity += 10;
int *items = realloc(h->h, sizeof(int) * (h->capacity));
h->h = items;
return h;
}
void heapifyDown(heap *h) {
int index = 0;
while(hasLeftChild(h, index)){
int smallerChildIndex = getLeftChildIndex(index);
if(hasRightChild(h, index) && rightChild(h, index) < leftChild(h, index))
smallerChildIndex = getRightChildIndex(index);
if(h->h[index] < h->h[smallerChildIndex])
break;
else
swap(h->h, index, smallerChildIndex);
index = smallerChildIndex;
}
}
void heapifyUp(heap *h) {
int index = h->size-1;
while(hasParent(index) && parent(h, index) > h->h[index]){
swap(h->h, getParentIndex(index), index);
index = getParentIndex(index);
}
}
heap *create_heap(int capacity){
heap *h = malloc(sizeof(heap));
assert(h != NULL);
h->h = malloc(sizeof(int) * capacity);
assert(h->h != NULL);
h->size = 0;
h->capacity = capacity;
return h;
}
int minimun(heap *h){
assert(h->size > 0);
return h->h[0];
}
int extract_min(heap *h){
assert(h->size > 0);
int item = h->h[0];
h->h[0] = h->h[h->size-1];
h->size--;
heapifyDown(h);
return item;
}
void add(heap *h, int item){
if(h->size == h->capacity)
h = increseHeapSize(h);
h->h[h->size] = item;
h->size++;
heapifyUp(h);
}
void print_heap(heap *h){
for(int i = 0; i < h->size; i++)
printf("%d ", h->h[i]);
printf("\n");
}
void decrease_key(heap *h, int i, int key){
h->h[i] = key;
while(hasParent(i) && parent(h, i) > h->h[i]){
swap(h->h, getParentIndex(i), i);
i = getParentIndex(i);
}
}
void destroyHeap(heap *h){
free(h->h);
free(h);
}
int main(){
heap *h = create_heap(2);
add(h, 2);
add(h, 3);
add(h, 6);
add(h, 4);
print_heap(h);
add(h, 1);
print_heap(h);
extract_min(h);
print_heap(h);
}