/
huffman.c
167 lines (137 loc) · 2.98 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
#include "huffman.h"
#include <stdio.h>
#include <stdlib.h>
static uint64_t
find_smallest (struct huffman_node **nodes, uint64_t count, int different)
{
// 'real' weights will always be positive.
int smallest = -1;
int i;
for (i = 0; i < count; i++) {
if (i == different) {
continue;
}
if (smallest == -1 ||
nodes[i]->weight < nodes[smallest]->weight) {
smallest = i;
}
}
return smallest;
}
static void
shift_nodes (struct huffman_node **nodes, uint64_t count, uint64_t start)
{
uint64_t i;
for (i = start; i + 1 < count; i++) {
nodes[i] = nodes[i + 1];
}
nodes[i] = NULL;
}
struct huffman_node *
huffman_build_tree(void **values, uint64_t count)
{
int i;
struct huffman_node **nodes;
nodes = malloc (sizeof (struct huffman_node *) * count);
for (i = 0; i < count; i++) {
struct huffman_node *node =
malloc (sizeof (struct huffman_node));
node->value = values[i];
node->weight = i + 1;
node->left = NULL;
node->right = NULL;
nodes[i] = node;
}
int tree1;
int tree2;
for (; count > 1; count--) {
struct huffman_node *tmp;
tree1 = find_smallest (nodes, count, -1);
tree2 = find_smallest (nodes, count, tree1);
tmp = malloc (sizeof (struct huffman_node));
tmp->weight = nodes[tree1]->weight + nodes[tree2]->weight;
tmp->value = NULL;
tmp->left = nodes[tree1];
tmp->right = nodes[tree2];
if (tree1 > tree2) {
shift_nodes (nodes, count, tree1);
shift_nodes (nodes, count, tree2);
} else {
shift_nodes (nodes, count, tree2);
shift_nodes (nodes, count, tree1);
}
nodes[count - 2] = tmp;
}
return nodes[0];
}
void *
huffman_lookup (struct huffman_node *tree, unsigned char *bits, int *bits_read,
bool print)
{
struct huffman_node *node = tree;
while (true) {
if (node == NULL) {
return NULL;
}
if (node->value != NULL) {
return node->value;
}
if ((bits[0] << *bits_read % 8 & 0x80) == 0) {
node = node->left;
if (print) {
putchar ('0');
}
} else {
node = node->right;
if (print) {
putchar ('1');
}
}
(*bits_read)++;
if (*bits_read % 8 == 0) {
bits++;
}
}
}
struct stack {
struct stack *next;
bool val;
};
static void
huffman_lookup_driver (struct huffman_node *tree, void *value,
struct stack *head, struct stack *cur)
{
if (tree->value == value) {
struct stack *x = head->next;
while (x != NULL) {
if (x->val) {
putchar ('1');
} else {
putchar ('0');
}
x = x->next;
}
return;
}
struct stack *next = malloc (sizeof (struct stack));
next->next = NULL;
cur->next = next;
if (tree->left != NULL) {
next->val = false;
huffman_lookup_driver (tree->left, value, head, next);
}
if (tree->right != NULL) {
next->val = true;
huffman_lookup_driver (tree->right, value, head, next);
}
cur->next = NULL;
free (next);
}
// given a value, print its code to stdout.
void
huffman_reverse_lookup (struct huffman_node *tree, void *value)
{
struct stack head;
head.next = NULL;
huffman_lookup_driver (tree, value, &head, &head);
}