-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpqueue.cpp
More file actions
76 lines (63 loc) · 1.45 KB
/
pqueue.cpp
File metadata and controls
76 lines (63 loc) · 1.45 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
#include "pqueue.h"
#include <stdlib.h>
#include <assert.h>
#define INIT_SZ 8
PQueue *newPQueue() {
PQueue *new_pq = (PQueue*) malloc(sizeof (PQueue));
new_pq->N = 0;
new_pq->keys_len = INIT_SZ;
new_pq->keys = (int*) malloc(sizeof (int) * INIT_SZ);
}
void PQInsert(PQueue *pq, int k) {
if (pq->N > pq->keys_len) {
pq = PQResize(pq, pq->keys_len * 2);
}
pq->keys[pq->N] = k;
pq->N++;
PQSwim(pq, pq->N);
}
int PQDelete(PQueue *pq) {
int k = pq->keys[pq->N];
PQExch(pq, 1, pq->N--);
PQSink(pq, 1);
return k;
}
int PQPeek(PQueue *pq) {
assert !PQEmpty(pq);
return pq->keys[0];
}
int PQSize(PQueue *pq) {
return pq->N;
}
bool PQEmpty(PQueue *pq) {
return pq->N == 0;
}
void PQSwim(PQueue *pq, int i) {
while (!PQOrdered(pq, i)) {
PQExch(pq, i, i / 2);
i = i / 2;
}
}
void PQSink(PQueue *pq, int i) {
while (2 * i <= pq->N && (!PQOrdered(pq, 2 * i) || !PQOrdered(pq, 2 * i + 1))) {
int j = PQCompare(pq, 2 * i, 2 * i + 1);
PQExch(pq, i, j);
i = j;
}
}
void PQExch(PQueue *pq, int i, int j) {
int tmp = pq->keys[i - 1];
pq->keys[i - 1] = pq->keys[j - 1];
pq->keys[j - 1] = tmp;
}
bool PQOrdered(PQueue *pq, int i) {
if ((i <= 1) || (i > pq->N)) {
return true;
}
int p = PQParent(pq, i);
return p >= pq->keys[i];
}
int PQParent(PQueue *pq, int i) {
return pq->keys[i / 2 - 1];
}
PQueue *PQResize