-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_queue.c
178 lines (147 loc) · 3.44 KB
/
priority_queue.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
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
#include "priority_queue.h"
#include "share.h"
#include "heap.h"
priority_queue *pq_create(int flag, void *t) {
priority_queue *pq = (priority_queue *)calloc(1, sizeof(priority_queue));
if (pq == NULL) {
return NULL;
}
pq->h = heap_create(0, flag, t);
if (pq->h == NULL) {
free(pq);
return NULL;
}
return pq;
}
void pq_release(priority_queue *pq) {
heap_release(pq->h);
free(pq);
return;
}
bool pq_insert(priority_queue *pq, void *val) {
return heap_insert(pq->h, val);
}
void *pq_max(priority_queue *pq) {
return heap_top(pq->h);
}
void *pq_extract_max(priority_queue *pq) {
return heap_pop(pq->h);
}
void *pq_increase_key(priority_queue *pq, void *val1, void *val2) {
}
void *pq_min(priority_queue *pq) {
return heap_top(pq->h);
}
void *pq_extract_min(priority_queue *pq) {
return heap_pop(pq->h);
}
void *pq_decrease_key(priority_queue *pq, void *val1, void *val2) {
}
#ifdef TEST_PQ
#include <stdio.h>
#include <stdlib.h>
//gcc -o priority_queue priority_queue.c heap.c -g -D TEST_PQ
typedef struct huffman {
struct huffman *left, *right;
float freq;
char value;
} huffman;
char code[100] = {'\0'};
void get_huffmap_code(huffman *h) {
static int idx = 0;
if (h->left == NULL && h->right == NULL) {
printf("%s\t%c\n", code, h->value);
}
if (h->left != NULL) {
code[idx] = '0';
idx ++;
get_huffmap_code(h->left);
code[idx] = '\0';
idx --;
}
if (h->right != NULL) {
code[idx] = '1';
idx ++;
get_huffmap_code(h->right);
code[idx] = '\0';
idx --;
}
}
int huffman_cmp(void *ptr1, void *ptr2) {
huffman *h1 = (huffman *)ptr1;
huffman *h2 = (huffman *)ptr2;
return h1->freq > h2->freq ? 1 : 0;
}
struct heap_type huffman_heap_type = {
.free = NULL,
.cmp = huffman_cmp,
.dup = NULL,
.match = NULL,
};
int main() {
struct huffman h[] = {
{
.left = NULL,
.right = NULL,
.freq = 0.45,
.value = 'a',
},
{
.left = NULL,
.right = NULL,
.freq = 0.13,
.value = 'b',
},
{
.left = NULL,
.right = NULL,
.freq = 0.12,
.value = 'c',
},
{
.left = NULL,
.right = NULL,
.freq = 0.16,
.value = 'd',
},
{
.left = NULL,
.right = NULL,
.freq = 0.09,
.value = 'e',
},
{
.left = NULL,
.right = NULL,
.freq = 0.05,
.value = 'f',
},
};
int i = 0;
huffman *p, *q;
priority_queue *pq = pq_create(MIN_HEAP, &huffman_heap_type);
if (pq == NULL) {
printf("pq_create failed\n");
return;
}
for (i = 0; i < sizeof(h)/sizeof(h[0]); i ++) {
pq_insert(pq, &h[i]);
}
while ((p = (huffman *)pq_extract_min(pq)) != NULL) {
q = (huffman *)pq_extract_min(pq);
if (q == NULL) {
break;
}
huffman *hp = malloc(sizeof(huffman));
hp->left = p;
hp->right = q;
hp->freq = p->freq + q->freq;
pq_insert(pq, hp);
}
if (p != NULL) {
//printf("%f\n", p->freq);
}
get_huffmap_code(p);
pq_release(pq);
}
#endif /*TEST_PQ*/