-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffman.c
More file actions
180 lines (165 loc) · 3.02 KB
/
huffman.c
File metadata and controls
180 lines (165 loc) · 3.02 KB
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
# include <ctype.h> // isalnum
# include <unistd.h> // write
# include "huffman.h"
// Create a single node with symbols, count, and leaf or not
treeNode *newNode(uint8_t s, uint64_t c, bool l)
{
treeNode *t = (treeNode *) malloc(sizeof(treeNode));
if (t)
{
t->symbol = s;
t->count = c;
t->leaf = l;
t->left = NIL;
t->right = NIL;
return t;
}
return (treeNode *) 0;
}
// Delete a tree of nodes using post-order traversal.
void delTree(treeNode *t)
{
if (t == NIL)
{
return;
}
delTree(t->left);
delTree(t->right);
free(t);
}
/* dumpTree:
*
* Dump the nodes of a tree onto a file using post-order traversal.
* If the node is a leaf, write 'L' followed by its symbol.
* Else, it is an interior node, write 'I'.
*/
void dumpTree(treeNode *t, int fildes)
{
if (t->leaf)
{
write(fildes, "L", sizeof(char));
write(fildes, &(t->symbol), sizeof(t->symbol));
return;
}
else
{
dumpTree(t->left, fildes);
dumpTree(t->right, fildes);
write(fildes, "I", sizeof(char));
}
}
/* stepTree:
*
* Goes down the tree following the value of a bit 'code'.
* treeNode **t represents the current node after stepping.
* If the bit is 0, traverse left. Else, traverse right.
*
* If a leaf is found, return its symbol and reset the
* current node back to the root of the tree.
*
*/
int32_t stepTree(treeNode *root, treeNode **t, uint32_t code)
{
if (code == 0)
{
if ((*t)->left)
{
*t = (*t)->left;
}
}
else
{
if ((*t)->right)
{
*t = (*t)->right;
}
}
if ((*t)->leaf)
{
int32_t sym = (*t)->symbol;
*t = root;
return sym;
}
else
{
return -1;
}
}
/* buildCode:
*
* Performs a post-order traversal of the Huffman Tree
* using a stack, setting the bit paths of each symbol.
*
* When traversing left, push 0.
* When traversing right, push 1.
*
* Pop the bit after returning from either traversal.
*/
void buildCode(treeNode *t, code s, code table[256])
{
if (t->leaf)
{
table[t->symbol] = s;
return;
}
uint32_t tmp;
pushCode(&s, 0);
buildCode(t->left, s, table);
popCode(&s, &tmp);
pushCode(&s, 1);
buildCode(t->right, s, table);
popCode(&s, &tmp);
}
/* join:
*
* Joins two nodes under one parent node, with its count as
* the sum of the left and right childrens' counts.
*
* Returns the parent node.
*/
treeNode *join(treeNode *l, treeNode *r)
{
treeNode *j = newNode('$', l->count + r->count, false);
j->left = l;
j->right = r;
return j;
}
/* convert:
*
* Converts a treeNode to a pointer.
*/
treeNode *convert(treeNode t)
{
treeNode *p = newNode(t.symbol, t.count, t.leaf);
p->left = t.left;
p->right = t.right;
return p;
}
// credit: DDEL
void printTree(treeNode *t, int depth)
{
if (t)
{
printTree(t->left, depth + 1);
if (t->leaf)
{
if (isalnum(t->symbol))
{
spaces(4 * depth);
printf("'%c' (%lu)\n", t->symbol, t->count);
}
else
{
spaces(4 * depth);
printf("0x%X (%lu)\n", t->symbol, t->count);
}
}
else
{
spaces(4 * depth);
printf("$ (%lu)\n", t->count);
}
printTree(t->right, depth + 1);
}
return;
}