-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpq.c
137 lines (115 loc) · 2.85 KB
/
pq.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
#include "pq.h"
#include "huffman.h"
#include "node.h"
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
struct PriorityQueue {
uint32_t size;
uint32_t capacity;
Node **node;
};
static inline void swap(PriorityQueue *q, uint32_t i, uint32_t j) {
Node *temp = q->node[i];
q->node[i] = q->node[j];
q->node[j] = temp;
return;
}
static inline uint32_t parent(uint32_t i) {
return ((i - 1) / 2);
}
static inline uint32_t right(uint32_t i) {
return ((2 * i) + 2);
}
static inline uint32_t left(uint32_t i) {
return ((2 * i) + 1);
}
static inline void heapify(PriorityQueue *q, uint32_t pos) {
uint32_t l = left(pos);
uint32_t r = right(pos);
uint32_t smallest = pos;
if (l < q->size && q->node[smallest]->frequency > q->node[l]->frequency) {
smallest = l;
}
if (r < q->size && q->node[smallest]->frequency > q->node[r]->frequency) {
smallest = r;
}
if (pos != smallest) {
swap(q, smallest, pos);
heapify(q, smallest);
}
return;
}
PriorityQueue *pq_create(uint32_t capacity) {
PriorityQueue *q = (PriorityQueue *) malloc(sizeof(PriorityQueue));
assert(q);
q->size = 0;
q->capacity = capacity;
q->node = (Node **) calloc(capacity, sizeof(Node *));
assert(q->node);
return q;
}
void pq_delete(PriorityQueue **q) {
if (*q) {
for (uint32_t i = 0; i < (*q)->size; i++) {
delete_tree(&(*q)->node[i]);
}
free((*q)->node);
free(*q);
*q = NULL;
}
return;
}
bool pq_empty(PriorityQueue *q) {
return q->size <= 0;
}
bool pq_full(PriorityQueue *q) {
return q->size == q->capacity;
}
uint32_t pq_size(PriorityQueue *q) {
return q->size;
}
bool enqueue(PriorityQueue *q, Node *n) {
if (q) {
if (pq_full(q)) {
return false;
}
q->size++;
uint32_t t = q->size - 1;
q->node[t] = n;
while (t != 0 && q->node[parent(t)]->frequency > q->node[t]->frequency) {
swap(q, t, parent(t));
t = parent(t);
}
return true;
}
return false;
}
bool dequeue(PriorityQueue *q, Node **n) {
if (q) {
if (pq_empty(q)) {
return false;
}
if (q->size == 1) {
q->size--;
*n = q->node[0];
return true;
}
*n = q->node[0];
q->node[0] = q->node[q->size - 1];
q->size--;
heapify(q, 0);
return true;
}
return false;
}
void pq_print(PriorityQueue *q) {
for (uint32_t i = 0; i < q->size / 2; i++) {
printf("Parent: %" PRId64 "", q->node[i]->frequency);
printf(" Left Child: %" PRId64 "", q->node[left(i)]->frequency);
printf(" Right Child: %" PRId64 "\n", q->node[right(i)]->frequency);
}
}