-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1AHTB.c
91 lines (77 loc) · 1.7 KB
/
1AHTB.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
#include "1AHTB.h"
#include <stdlib.h>
#include <stdio.h>
#define BITTYPE long int
HuffNode* create_huffman_tree(CharOcc** o, int s) {
if(s == 1) // base case: only one character
return o[0];
int ts, te, ns; // Tree Start, Tree End, Node Start
int i, j;
HuffNode* hn; // Hufman Node
// combine first two
hn = malloc(sizeof(HuffNode));
hn->left = o[0];
hn->right = o[1];
hn->o = o[0]->o + o[1]->o;
o[0] = hn;
// set variables
ts = 0;
te = 1;
ns = 2;
// loop for non trees
while (ns < s)
{ // while there are nodes left
//find smallest 2
if(o[ts]->o < o[ns]->o) {
i = ts++;
j = ns>=s || o[i]->o < o[ns]->o ? ts++ : ns++;
} else {
i = ns++;
j = ns<s && o[i]->o < o[ts]->o ? ns++ : ts++;
}
hn = malloc(sizeof(HuffNode));
hn->left = o[i];
hn->right = o[j];
hn->o = o[i]->o + o[j]->o;
hn->c = 0;
o[te++] = hn;
}
// loop for trees
while(ts < te - 1)
{ //While there are more than 1 tree
hn = malloc(sizeof(HuffNode));
hn->o = 0;
hn->left = o[ts];
hn->o += o[ts++]->o;
hn->right = o[ts];
hn->o += o[ts++]->o;
hn->c = 0;
o[te++] = hn;
}
// return the last tree
return o[te - 1];
}
void print_huffman_internal(HuffNode* t, BITTYPE prefix, unsigned int depth) {
if( t->c != 0 )
{ // node is a leaf
int f=0, c;
for(int i=sizeof(BITTYPE) - 1;i>=0;i--) {
c = (prefix >> i) & 1;
f = f || c == 1;
if(i+1 <= depth)
printf("%i", c);
else
printf(" ");
}
printf("\t%c\n", t->c, depth);
} else {
prefix = (prefix << 1) & ~1;
depth++;
print_huffman_internal(t->left, prefix, depth);
prefix = prefix | 1;
print_huffman_internal(t->right, prefix, depth);
}
}
void print_huffman(HuffNode* t) {
print_huffman_internal(t, 0, 0);
}