-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoder.c
181 lines (123 loc) · 3.71 KB
/
Encoder.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
#include "Encoder.h"
char *code[256] = {0};
void free_codes(char *input, int size);
int *get_freq(char *in, char *distinct);
void print_codes(struct node *root, char out[], int top);
struct node *build_tree(char count_c[], int freq[], int size);
char *get_c(char *in, int size);
void free_tree(struct node *root);
void free_tree(struct node *root)
{
if(!is_leaf(root)){
free_tree(root->left);
free_tree(root->right);
}
else{
free(root);
}
}
void encode(char input[], int size)
{
int distinct_size = 0;
int *freq, top = 0;
char out[50];
char *char_in;
char_in = get_c(input, size);
distinct_size = strlen(char_in);
freq = get_freq(input, char_in);
struct node *root = build_tree(char_in, freq, distinct_size);
print_codes(root, out, top);
printf("\n");
//To exclude the null character
size--;
for(int i = 0; i < size - 1; i++){
printf("%s", code[(int)input[i]]);
}
printf("\n");
free_codes(char_in, distinct_size);
free(char_in);
free(freq);
free_tree(root);
}
struct node *build_tree(char char_in[], int freq[], int size)
{
struct node *left, *right, *top, *root;
struct minHeap *minH = create_dheap(char_in, freq, size);
while(minH->size != 1){
//Repeat until there is only one node left
left = MH_pop(minH);
right = MH_pop(minH);
top = create_node('$', left->freq + right->freq);
top->left = left;
top->right = right;
MH_push(minH, top);
}
//return the last node
root = MH_pop(minH);
destroy_mh(minH);
return root;
}
void free_codes(char *distinct, int size)
{
for(int i = 0; i < size; i++){
free(code[(int)distinct[i]]);
}
}
void print_codes(struct node *root, char out[], int top)
{
if(root->left){
out[top] = '0';
print_codes(root->left, out, top + 1);
}
if(root->right){
out[top] = '1';
print_codes(root->right, out, top + 1);
}
if(!(root->right) && !(root->left)){
printf("%c: ", root->c);
int len = strlen(out);
code[root->c] = malloc(len * sizeof(char));
if(code[root->c] == NULL){
fprintf(stderr, "Not enough memory\n");
}
strcpy(code[root->c], out);
printf("%s", out);
printf("\n");
}
}
char *get_c(char *in, int size)
{
int count = 0, top = 0, i;
char buf[256] = "", c, *out;
for(i = 0; i < size - 1; i++){
c = in[i];
buf[c] = c;
}
for(i = 0; i < 256; i++){
if(buf[i] != 0)
count++;
}
out = malloc(count * sizeof(char));
for(i = 0; i < 256; i++){
if(buf[i] != 0 && buf[i] != '\0'){
out[top] = buf[i];
top++;
}
}
out[top] = '\0';
return out;
}
int *get_freq(char *in, char *distinct)
{
//Get freq from string
int *out = malloc((sizeof(distinct) + 1) * sizeof(int));
char ch;
for(int i = 0; distinct[i] != '\0'; i++){
ch = distinct[i];
for(int j = 0; in[j] != '\0'; j++){
if(in[j] == ch)
out[i]++;
}
}
return out;
}