forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.c
265 lines (216 loc) · 6.27 KB
/
huffman.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Made by Guston and edited by Gathros
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct tree {
struct tree* left;
struct tree* right;
int count;
char value;
};
struct bitstring_builder {
char str[257];
int next_index;
};
struct codebook {
char* codes[256];
};
struct heap {
struct tree** data;
int length;
int capacity;
};
bool is_leaf(const struct tree* t) {
return !t->left && !t->right;
}
void swap(struct tree** lhs, struct tree** rhs) {
struct tree* tmp = *lhs;
*lhs = *rhs;
*rhs = tmp;
}
/* The two concat functions are horribly inefficient */
void concat(char** dst, const char* src) {
int dst_len = strlen(*dst);
int src_len = strlen(src);
*dst = realloc(*dst, src_len + dst_len + 1);
strcat(*dst, src);
}
void concat_char(char** dst, char c) {
int len = strlen(*dst);
*dst = realloc(*dst, len + 2);
(*dst)[len] = c;
(*dst)[len + 1] = '\0';
}
char* duplicate(const char* src) {
int length = strlen(src);
char* dst = malloc(length + 1);
memcpy(dst, src, length + 1);
return dst;
}
void heap_push(struct heap* heap, struct tree* value) {
if (heap->capacity == heap->length) {
heap->capacity = heap->capacity == 0 ? 4 : heap->capacity * 2;
heap->data = realloc(heap->data, heap->capacity * sizeof(struct tree*));
}
heap->data[heap->length++] = value;
int index = heap->length - 1;
while (index) {
int parent_index = (index - 1) / 2;
if (heap->data[parent_index]->count <= heap->data[index]->count) {
break;
}
swap(&heap->data[parent_index], &heap->data[index]);
index = parent_index;
}
}
struct tree* heap_pop(struct heap* heap) {
if (!heap->length) {
return NULL;
}
struct tree* result = heap->data[0];
swap(&heap->data[0], &heap->data[--heap->length]);
int index = 0;
for (;;) {
int target = index;
int left = 2 * index + 1;
int right = left + 1;
if (left < heap->length &&
heap->data[left]->count < heap->data[target]->count) {
target = left;
}
if (right < heap->length &&
heap->data[right]->count < heap->data[target]->count) {
target = right;
}
if (target == index) {
break;
}
swap(&heap->data[index], &heap->data[target]);
index = target;
}
return result;
}
void heap_free(struct heap* heap) {
free(heap->data);
}
struct tree* generate_tree(const char* str) {
int counts[256] = { 0 };
for (; *str != '\0'; ++str) {
counts[(unsigned char)*str] += 1;
}
struct heap heap = { 0 };
for (int i = 0; i < sizeof(counts) / sizeof(int); ++i) {
if (counts[i]) {
struct tree* tree = calloc(1, sizeof(struct tree));
tree->value = (char)i;
tree->count = counts[i];
heap_push(&heap, tree);
}
}
while (heap.length > 1) {
struct tree* left = heap_pop(&heap);
struct tree* right = heap_pop(&heap);
struct tree* branch = calloc(1, sizeof(struct tree));
branch->count = left->count + right->count;
branch->left = left;
branch->right = right;
heap_push(&heap, branch);
}
struct tree* root = heap_pop(&heap);
heap_free(&heap);
return root;
}
void tree_free(struct tree* tree) {
if (!tree) return;
tree_free(tree->left);
tree_free(tree->right);
free(tree);
}
void codebook_recurse(const struct tree* tree,
struct bitstring_builder* builder,
struct codebook* codebook) {
if (!tree) {
return;
}
if (is_leaf(tree)) {
builder->str[builder->next_index] = '\0';
codebook->codes[(unsigned char)tree->value] = duplicate(builder->str);
return;
}
builder->str[builder->next_index++] = '0';
codebook_recurse(tree->left, builder, codebook);
builder->next_index -= 1;
builder->str[builder->next_index++] = '1';
codebook_recurse(tree->right, builder, codebook);
builder->next_index -= 1;
}
struct codebook generate_codebook(const struct tree* tree) {
struct codebook codebook = { .codes = { 0 } };
struct bitstring_builder builder = { .str = { 0 }, .next_index = 0 };
codebook_recurse(tree, &builder, &codebook);
return codebook;
}
void codebook_free(struct codebook* codebook) {
int size = sizeof(codebook->codes) / sizeof(codebook->codes[0]);
for (int i = 0; i < size; ++i) {
free(codebook->codes[i]);
}
}
const char* get_code(const struct codebook* codebook, char c) {
return codebook->codes[(unsigned char)c];
}
char* encode(const char* input, struct tree** huffman_tree,
struct codebook* codebook) {
*huffman_tree = generate_tree(input);
*codebook = generate_codebook(*huffman_tree);
char* result = duplicate(get_code(codebook, *input));
int result_length = strlen(result);
int result_capacity = result_length;
input += 1;
for (; *input; ++input) {
concat(&result, get_code(codebook, *input));
}
return result;
}
const char* decode_recurse(const char* input, const struct tree* tree,
char** result) {
if (!tree) {
return input;
}
if (is_leaf(tree)) {
concat_char(result, tree->value);
return input;
}
if (*input == '0') {
return decode_recurse(input + 1, tree->left, result);
} else {
return decode_recurse(input + 1, tree->right, result);
}
}
char* decode(const char* input, const struct tree* tree) {
char* result = calloc(1, 1);
do {
input = decode_recurse(input, tree, &result);
} while (*input);
return result;
}
int main() {
struct tree* tree;
struct codebook codebook;
char* encoded = encode("bibbity bobbity", &tree, &codebook);
char* decoded = decode(encoded, tree);
printf("Codebook:\n");
for (int i = 0; i < 256; ++i) {
if (codebook.codes[i]) {
printf("%c %s\n", (char)i, codebook.codes[i]);
}
}
printf("%s\n", encoded);
printf("%s\n", decoded);
tree_free(tree);
codebook_free(&codebook);
free(encoded);
free(decoded);
return 0;
}