-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpqheap.c
More file actions
executable file
·242 lines (177 loc) · 5.63 KB
/
Copy pathpqheap.c
File metadata and controls
executable file
·242 lines (177 loc) · 5.63 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
238
239
240
#include <assert.h>
#include <stdlib.h>
#include "pqheap.h"
/* setting this enables printing pq debug info */
#define PQ_DEBUG if(1)
//#define PQ_DEBUG if(0)
//initial size of PQ. Gets doubled every time an insert finds the
//array is full.
const int PQINITSIZE = 256;
/* arrays in C start at 0. For simplicity the heap structure is slightly modified as:
0
|
1
/\
2 3
/\ /\
4 5 6 7
*/
/* The children of an element of the heap. */
static inline unsigned int heap_lchild(unsigned int index) {
return 2 * index;
}
static inline unsigned int heap_rchild(unsigned int index) {
return 2 * index + 1;
}
/* The parent of an element. */
static inline unsigned int heap_parent(unsigned int index) {
return index >> 1;
}
/* return minimum of two integers */
static unsigned int mymin(unsigned int a, unsigned int b) {
return (a<=b)? a:b;
}
static void heapify(PQueue* pq, unsigned int root) {
unsigned int min_index = root;
unsigned int lc = heap_lchild(root);
unsigned int rc = heap_rchild(root);
assert(pq && pq->elements);
if ((lc < pq->cursize) &&
(getPriority(pq->elements[lc]) < getPriority(pq->elements[min_index]))) {
min_index = lc;
}
if ((rc < pq->cursize) &&
(getPriority(pq->elements[rc]) < getPriority(pq->elements[min_index]))) {
min_index = rc;
}
if (min_index != root) {
elemType tmp_q = pq->elements[min_index];
pq->elements[min_index] = pq->elements[root];
pq->elements[root] = tmp_q;
heapify(pq, min_index);
}
}
static void PQ_grow(PQueue* pq) {
elemType* elements;
unsigned int i;
printf("PQ: doubling size to %d\n",pq->maxsize*2); fflush(stdout);
assert(pq && pq->elements);
pq->maxsize *= 2;
elements = (elemType*)malloc(pq->maxsize*sizeof(elemType));
if (!elements) {
printf("PQ_grow: could not reallocate priority queue: insufficient memory..\n");
exit(1);
}
/* should use realloc..*/
for (i=0; i<pq->cursize; i++) {
elements[i] = pq->elements[i];
}
free(pq->elements);
pq->elements = elements;
}
/**************************************************************/
/* create and initialize a pqueue and return it */
PQueue* PQ_initialize() {
PQueue *pq;
printf("PQ-initialize: initializing heap\n"); fflush(stdout);
pq = (PQueue*)malloc(sizeof(PQueue));
assert(pq);
pq->elements = (elemType*)malloc(PQINITSIZE*sizeof(elemType));
if (!pq->elements) {
printf("PQ_initialize: could not allocate priority queue: insufficient memory..\n");
exit(1);
}
assert(pq->elements);
pq->maxsize = PQINITSIZE;
pq->cursize = 0;
return pq;
}
/************************************************************/
/* delete the pqueue and free its space */
void PQ_delete(PQueue* pq) {
printf("PQ-delete: deleting heap\n"); fflush(stdout);
assert(pq && pq->elements);
if (pq->elements) free(pq->elements);
}
/************************************************************/
/* Is it empty? */
int PQ_isEmpty(PQueue* pq) {
assert(pq && pq->elements);
return (pq->cursize == 0);
}
/************************************************************/
/* Return the nb of elements currently in the queue */
unsigned int PQ_size(PQueue* pq) {
assert(pq && pq->elements);
return pq->cursize;
}
/************************************************************/
/* Set *elt to the min element in the queue;
return value: 1 if exists a min, 0 if not */
int PQ_min(PQueue* pq, elemType* elt) {
assert(pq && pq->elements);
if (!pq->cursize) {
return 0;
}
*elt = pq->elements[0];
return 1;
}
/************************************************************/
/* Set *elt to the min element in the queue and delete it from queue;
return value: 1 if exists a min, 0 if not */
int PQ_extractMin(PQueue* pq, elemType* elt) {
assert(pq && pq->elements);
if (!pq->cursize) {
return 0;
}
*elt = pq->elements[0];
pq->elements[0] = pq->elements[--pq->cursize];
heapify(pq, 0);
PQ_DEBUG {printf("PQ_extractMin: "); printElem(*elt); fflush(stdout);}
return 1;
}
/************************************************************/
/* Delete the min element; same as PQ_extractMin, but ignore the value extracted;
return value: 1 if exists a min, 0 if not */
int PQ_deleteMin(PQueue* pq) {
assert(pq && pq->elements);
elemType dummy;
return PQ_extractMin(pq, &dummy);
}
/************************************************************/
/* Insert */
void PQ_insert(PQueue* pq, elemType elt) {
unsigned int ii;
assert(pq && pq->elements);
PQ_DEBUG {printf("PQ_insert: "); printElem(elt); fflush(stdout);}
if (pq->cursize==pq->maxsize) {
PQ_grow(pq);
}
assert(pq->cursize < pq->maxsize);
for (ii = pq->cursize++;
ii && (getPriority(pq->elements[heap_parent(ii)]) > getPriority(elt));
ii = heap_parent(ii)) {
pq->elements[ii] = pq->elements[heap_parent(ii)];
}
pq->elements[ii] = elt;
}
/************************************************************/
/* Delete the min element and insert the new item x; by doing a delete
and an insert together you can save a heapify() call */
void PQ_deleteMinAndInsert(PQueue* pq, elemType elt) {
assert(pq && pq->elements);
PQ_DEBUG {printf("PQ_deleteMinAndinsert: "); printElem(elt); printf("\n"); fflush(stdout);}
pq->elements[0] = elt;
heapify(pq, 0);
}
/************************************************************/
/* print the elements in the queue */
void PQ_print(PQueue* pq) {
printf("PQ:\n "); fflush(stdout);
unsigned int i;
for (i=0; i< mymin(10, pq->cursize); i++) {
printf("\t");
printElem(pq->elements[i]);
}
printf("\n");fflush(stdout);
}