-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.c
More file actions
executable file
·237 lines (221 loc) · 5.33 KB
/
Heap.c
File metadata and controls
executable file
·237 lines (221 loc) · 5.33 KB
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
/*
* Heap.c
*
* Created on: Sep 22, 2014
* Author: Christopher McGrath
*/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include "Heap.h"
#include "ArrayInteger.h"
Heap* H_init(int type, size_t size){
Heap* newHeap = malloc(sizeof(Heap));
if (!newHeap){
printf("Error: H_init: unable to allocate memory.\n");
fflush(stdout);
return NULL;
}
newHeap->size = 0;
newHeap->capacity = size;
newHeap->array = malloc(sizeof(int) * newHeap->capacity);
newHeap->type = type;
return newHeap;
}
void H_destroy(Heap* heap){
if (heap != NULL){
free(heap->array);
free(heap);
}
}
bool H_isEmpty(Heap* heap){
if (heap == NULL){
printf("Error: H_isEmpty: heap points to NULL.\n");
fflush(stdout);
exit(EXIT_FAILURE);
}
return heap->size == 0;
}
void H_heapify(Heap* heap, int index){
if (heap == NULL){
printf("Error: H_heapify: heap points to NULL.\n");
fflush(stdout);
exit(EXIT_FAILURE);
}
int leftIndex = 2 * index + 1;
int rightIndex = 2 * index + 2;
if (leftIndex >= heap->size){
return;
}
if (rightIndex >= heap->size){
int temp = heap->array[index];
switch (heap->type){
case MAX:
if (temp < heap->array[leftIndex]){
heap->array[index] = heap->array[leftIndex];
heap->array[leftIndex] = temp;
H_heapify(heap, leftIndex);
}
break;
case MIN:
if (temp > heap->array[leftIndex]){
heap->array[index] = heap->array[leftIndex];
heap->array[leftIndex] = temp;
H_heapify(heap, leftIndex);
}
break;
}
} else {
int temp = heap->array[index];
switch (heap->type){
case MAX:
if (heap->array[leftIndex] > heap->array[rightIndex]){
if (temp < heap->array[leftIndex]){
heap->array[index] = heap->array[leftIndex];
heap->array[leftIndex] = temp;
H_heapify(heap, leftIndex);
}
} else if (temp < heap->array[rightIndex]){
heap->array[index] = heap->array[rightIndex];
heap->array[rightIndex] = temp;
H_heapify(heap, rightIndex);
}
break;
case MIN:
if (heap->array[leftIndex] < heap->array[rightIndex]){
if (temp > heap->array[leftIndex]){
heap->array[index] = heap->array[leftIndex];
heap->array[leftIndex] = temp;
H_heapify(heap, leftIndex);
}
} else if (temp > heap->array[rightIndex]){
heap->array[index] = heap->array[rightIndex];
heap->array[rightIndex] = temp;
H_heapify(heap, rightIndex);
}
break;
}
}
}
// Reallocates the Heap with twice its current capacity and returns it.
Heap* H_doubleCapacity(Heap* heap){
if (heap == NULL){
printf("Error: H_doubleCapacity: heap points to NULL.\n");
fflush(stdout);
exit(EXIT_FAILURE);
}
Heap* newHeap = H_init(heap->type, heap->capacity * 2);
if (!newHeap){
return NULL;
}
newHeap->size = heap->size;
int i;
for (i = 0; i < heap->size; i += 1){
newHeap->array[i] = heap->array[i];
}
H_destroy(heap);
return newHeap;
}
Heap* H_buildHeap(int type, int* array, int size){
Heap* heap = H_init(type, DEFAULT_SIZE);
if (heap == NULL){
printf("Error: H_buildHeap: heap points to NULL.\n");
fflush(stdout);
exit(EXIT_FAILURE);
}
int doubler = size >> 8;
while (doubler > 0){
heap = H_doubleCapacity(heap);
if (!heap){
return NULL;
}
doubler = doubler >> 1;
}
heap->size = size;
int i;
for (i = 0; i < size; i += 1){
heap->array[i] = array[i];
}
for (i = size / 2 - 1; i >= 0; i -= 1){
H_heapify(heap, i);
}
return heap;
}
bool H_insert(Heap* heap, int n){
if (heap == NULL){
printf("Error: H_insert: heap points to NULL.");
fflush(stdout);
exit(EXIT_FAILURE);
}
if (heap->capacity == heap->size){
heap = H_doubleCapacity(heap);
if (!heap){
printf("Error: H_insert: error doubling heap capacity.");
fflush(stdout);
exit(EXIT_FAILURE);
}
}
heap->array[heap->size] = n;
heap->size += 1;
int i;
for (i = heap->size / 2 - 1; i >= 0; i -= 1){
H_heapify(heap, i);
}
return true;
}
int H_peakFirst(Heap* heap){
if (heap == NULL){
printf("Error: H_peakFirst: heap points to NULL.");
fflush(stdout);
exit(EXIT_FAILURE);
}
if (heap->size > 0){
return heap->array[0];
} else {
return -1;
}
}
int H_extractFirst(Heap* heap){
if (heap == NULL){
printf("Error: H_extractFirst: heap points to NULL.");
fflush(stdout);
exit(EXIT_FAILURE);
}
if (heap->size == 0){
printf("Error: tried to extract from empty heap.");
fflush(stdout);
exit(EXIT_FAILURE);
}
int first = heap->array[0];
heap->size -= 1;
if (heap->size > 0){
heap->array[0] = heap->array[heap->size];
H_heapify(heap, 0);
}
return first;
}
void H_printHeapStats(Heap* heap){
if (heap == NULL){
printf("Error: H_printHeapStats: heap points to NULL.\n");
fflush(stdout);
return;
}
printf(" type = %s\n", (heap->type == MAX ? "max" : "min"));
printf(" size = %d\n", heap->size);
printf(" capacity = %d\n", heap->capacity);
}
void H_printHeap(Heap* heap){
printf("Heap: - - - - - - - - - -\n");
if (heap == NULL){
printf("Error: H_printHeap: heap points to NULL.\n");
fflush(stdout);
} else {
H_printHeapStats(heap);
printf(" array:");
int i;
for (i = 0; i < heap->size; i += 1){
printf(" %d", heap->array[i]);
}
}
printf("\n- - - - - - - - - - - - \n\n");
}