-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
277 lines (222 loc) · 6.11 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "heap.h"
/*
* TODO use instead of SequenceT* data
*/
typedef struct {
SequenceT data;
int id;
} NodeT;
typedef struct {
size_t size;
size_t capacity;
SequenceT* data; /* NodeT* nodes */
} HeapImplT;
#define GROWTH_FACTOR 2
#define INITIAL_CAPACITY 10
#define INVALID_NODE -1 /* TODO use - infinity */
#define LEFT_CHILD(index) (index << 1) + 1
#define RIGHT_CHILD(index) LEFT_CHILD(index) + 1
#define PARENT(index) (index - 1) >> 1
inline static void swap(SequenceT* a, SequenceT* b) {
SequenceT c = *a;
*a = *b;
*b = c;
}
HeapT* create_heap_from(const SequenceT* arr, size_t size) {
if (arr == NULL) {
return NULL;
}
HeapImplT* heap = (HeapImplT*) malloc(sizeof(HeapImplT));
if (heap == NULL) {
return NULL;
}
heap->data = (SequenceT*) malloc(sizeof(SequenceT) * size);
if (heap->data == NULL) {
free(heap);
return NULL;
}
memcpy(heap->data, arr, size * sizeof(SequenceT));
heap->capacity = size;
heap->size = size;
return (HeapT*) heap;
}
HeapT* create_heap() {
HeapImplT* heap = (HeapImplT*) malloc(sizeof(HeapImplT));
if (heap == NULL) {
return NULL;
}
heap->size = 0;
heap->data = (SequenceT*) malloc(sizeof(SequenceT) * INITIAL_CAPACITY);
heap->capacity = INITIAL_CAPACITY;
if (heap->data == NULL) {
free(heap);
return NULL;
}
return (HeapT*) heap;
}
void destroy_heap(HeapT* h) {
if (h == NULL) {
return;
}
HeapImplT* heap = (HeapImplT*) h;
if (heap->data != NULL) {
free(heap->data);
}
free(heap);
}
int get_heap_size(HeapT* h) {
if (h == NULL) {
return - 1;
}
HeapImplT* heap = (HeapImplT*) h;
return heap->size;
}
SequenceT* get_heap_data(HeapT* h) {
if (h == NULL) {
return NULL;
}
HeapImplT* heap = (HeapImplT*) h;
return heap->data;
}
void max_heapify(HeapT* h, size_t index) {
assert(h != NULL);
HeapImplT* heap = (HeapImplT*) h;
size_t largest_index, left_index, right_index;
/* loop way */
while (1) {
left_index = LEFT_CHILD(index);
right_index = RIGHT_CHILD(index);
largest_index = index;
if (heap->size > left_index &&
heap->data[left_index] > heap->data[largest_index]
) {
largest_index = left_index;
}
if (heap->size > right_index &&
heap->data[right_index] > heap->data[largest_index]
) {
largest_index = right_index;
}
if (largest_index == index) {
break;
}
swap(&heap->data[largest_index], &heap->data[index]);
index = largest_index;
}
}
/* rec way */
void min_heapify(HeapT* h, size_t index) {
assert(h != NULL);
HeapImplT* heap = (HeapImplT*) h;
size_t left_index = LEFT_CHILD(index);
size_t right_index = RIGHT_CHILD(index);
size_t lowest_index = index;
if (heap->size > left_index &&
heap->data[left_index] < heap->data[lowest_index]
) {
lowest_index = left_index;
}
if (heap->size > right_index &&
heap->data[right_index] < heap->data[lowest_index]
) {
lowest_index = right_index;
}
if (lowest_index != index) {
swap(&heap->data[lowest_index], &heap->data[index]);
min_heapify(h, lowest_index);
}
}
void build_max_heap(HeapT* h) {
assert(h != NULL);
HeapImplT* heap = (HeapImplT*) h;
int index = PARENT(heap->size - 1);
for (; index >= 0; --index) {
max_heapify(h, index);
}
}
void build_min_heap(HeapT* h) {
assert(h != NULL);
HeapImplT* heap = (HeapImplT*) h;
int index = PARENT(heap->size - 1);
for (; index >= 0; --index) {
min_heapify(h, index);
}
}
void build_max_heap2(HeapT* h) {
assert(h != NULL);
HeapImplT* heap = (HeapImplT*) h;
size_t size = heap->size, index = 0;
heap->size = 0;
for (; index < size; ++index) {
max_heap_insert(h, heap->data[index]);
}
}
void heapsort(SequenceT* arr, size_t size, SortOrderT order) {
if (arr == NULL || size <= 1) {
return;
}
HeapT* h = create_heap_from(arr, size);
assert(h != NULL);
HeapImplT* heap = (HeapImplT*) h;
void (*heapify)(HeapT*, size_t);
if (order == DESC) {
build_max_heap(h);
heapify = max_heapify;
} else {
build_min_heap(h);
heapify = min_heapify;
}
size_t index = size - 1;
for (; index > 0; --index) {
swap(&heap->data[index], &heap->data[0]);
--heap->size;
heapify(h, 0);
}
memcpy(arr, heap->data, size * sizeof(SequenceT));
destroy_heap(h);
}
SequenceT heap_maximum(HeapT* h) {
HeapImplT* heap = (HeapImplT*) h;
assert(heap != NULL && heap->size != 0);
return heap->data[0];
}
SequenceT heap_extract_max(HeapT* h) {
HeapImplT* heap = (HeapImplT*) h;
assert(heap != NULL && heap->size != 0);
SequenceT max = heap_maximum(h);
heap->data[0] = heap->data[heap->size - 1];
--heap->size;
if (heap->capacity > INITIAL_CAPACITY &&
heap->capacity >= (heap->size * GROWTH_FACTOR * GROWTH_FACTOR)
) {
heap->capacity /= GROWTH_FACTOR;
heap->data = realloc(heap->data, sizeof(SequenceT) * heap->capacity);
}
max_heapify(h, 0);
return max;
}
void heap_increase_key(HeapT* h, size_t index, SequenceT key) {
HeapImplT* heap = (HeapImplT*) h;
assert(heap != NULL && heap->size > index);
assert(key > heap->data[index]);
/* 6_5_6 */
while (index > 0 && heap->data[PARENT(index)] < key) {
heap->data[index] = heap->data[PARENT(index)];
index = PARENT(index);
}
heap->data[index] = key;
}
void max_heap_insert(HeapT* h, SequenceT key) {
assert(h != NULL);
HeapImplT* heap = (HeapImplT*) h;
if (heap->size == heap->capacity) {
heap->capacity *= GROWTH_FACTOR;
heap->data = realloc(heap->data, sizeof(SequenceT) * heap->capacity);
}
++heap->size;
heap->data[heap->size - 1] = INVALID_NODE;
heap_increase_key(h, heap->size - 1, key);
}