-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinimumPriorityQueue.c
63 lines (50 loc) · 1.27 KB
/
MinimumPriorityQueue.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
// Minimum priority queue implementation for Huffman codes
#include "MinimumPriorityQueue.h"
enum { PQ_SIZE = 256 };
static int n = 0; // Number of elements in the priority queue
static trie_node *pq[PQ_SIZE + 1];
// checks if a trie node is greater than other
static int is_greater(int i, int j) {
if (pq[i]->freq > pq[j]->freq)
return 1;
return 0;
}
// swaps two nodes of the priority queue at given index
static void swap(int i, int j) {
trie_node *t = pq[i];
pq[i] = pq[j];
pq[j] = t;
}
// moves value at 'k' up to maintain heap invariant
static void swim(int k) {
while (k > 1 && is_greater(k / 2, k)) {
swap(k / 2, k);
k = k / 2;
}
}
// moves value at 'k' down to maintain heap invariant
static void sink(int k) {
while (2 * k <= n) {
int i = 2 * k;
if (i < n && is_greater(i, i + 1))
i++;
swap(k, i);
k = i;
}
}
// returns number of elements in the priority queue
int pq_size(void) {
return n;
}
// inserts a trie node pointer in the priority queue
void pq_insert(trie_node *t) {
pq[++n] = t;
swim(n);
}
// deletes and returns the node with minimum value
trie_node *pq_delete_min(void) {
trie_node *min = pq[1];
swap(1, n--);
sink(1);
return min;
}