-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffmanEncoding.c
161 lines (145 loc) · 3.8 KB
/
HuffmanEncoding.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
#include "stdio.h"
#include "stdlib.h"
struct node{
int sum;
char c;
struct node* rnode;
struct node* lnode;
};
typedef struct node* nodeptr;
struct qu{
nodeptr data[100];
int last;
};
typedef struct qu* priorityQueue;
void reheapUp(priorityQueue q, int pos){
nodeptr temp;
if(pos <= 0){
return;
}
if(q->data[pos]->sum < q->data[(pos-1)/2]->sum){
temp = q->data[pos];
q->data[pos] = q->data[(pos-1)/2];
q->data[(pos-1)/2] = temp;
reheapUp(q, (pos-1)/2);
}
}
void reheapDown(priorityQueue q, int pos){
nodeptr temp;
if(pos*2+1 >= q->last){
return;
}
if(q->data[pos*2+1]->sum < q->data[pos*2+2]->sum){
if(q->data[pos*2+1]->sum < q->data[pos]->sum){
temp = q->data[pos];
q->data[pos] = q->data[pos*2+1];
q->data[pos*2+1] = temp;
reheapDown(q, pos*2+1);
}
}else{
if(q->data[pos*2+2]->sum < q->data[pos]->sum){
temp = q->data[pos];
q->data[pos] = q->data[pos*2+2];
q->data[pos*2+2] = temp;
reheapDown(q, pos*2+2);
}
}
}
void enqueue(priorityQueue q, nodeptr data){
q->data[q->last] = data;
reheapUp(q, q->last);
q->last++;
}
nodeptr dequeue(priorityQueue q){
nodeptr temp = q->data[0];
q->data[0] = q->data[q->last-1];
q->last--;
reheapDown(q, 0);
return temp;
}
int treeMapping(int map[], int count[], nodeptr root, int n, int d){
if(root == NULL){
return;
}
if(root->c != '\0'){
map[root->c] = n;
count[root->c] = d;
}else{
treeMapping(map,count,root->lnode, n << 1, d+1);
treeMapping(map,count,root->rnode, (n << 1)|0x01, d+1);
}
}
nodeptr encode(char filename[], char outname[]){
int i,j,k;
char t,c;
FILE* rawdata = fopen(filename, "r");
FILE* outdata;
int feq[256] = {};
int map[256] = {};
int count[256] = {};
char buffer[1024];
nodeptr temp,root;
priorityQueue queue = (priorityQueue) malloc(sizeof(struct qu));
queue->last = 0;
//make feq
while(!feof(rawdata)){
fgets(buffer, 1024, rawdata);
for(i = 0; buffer[i] != '\0'; i++){
feq[buffer[i]]++;
}
}
fclose(rawdata);
//make feq tree
for(i = 0; i < 256; i++){
if(feq[i]){
temp = (nodeptr)malloc(sizeof(struct node));
temp->sum = feq[i];
temp->c = i;
temp->rnode = NULL;
temp->lnode = NULL;
enqueue(queue, temp);
printf("%c %d\n", i, feq[i]);
}
}
for(i = 0; i < queue->last; i++){
printf("%d\n", queue->data[i]->sum);
}
//printf("%d\n", queue->last);
//return;
while(queue->last > 1){
//printf("f\n");
temp = (nodeptr)malloc(sizeof(struct node));
temp->c = '\0';
temp->lnode = dequeue(queue);
temp->rnode = dequeue(queue);
temp->sum = temp->lnode->sum + temp->rnode->sum;
enqueue(queue, temp);
//printf("%d %d %d\n", temp->lnode->sum,temp->rnode->sum, temp->sum);
}
root = dequeue(queue);
//mapping char
treeMapping(map, count, root, 0, 0);
//encode and output
i = 0;
t = 0;
for(i = 0; i < 256; i++){
if(feq[i] != 0) printf("%c %d %x %d\n", i, feq[i], map[i], count[i]);
}
outdata = fopen(outname, "w");
rawdata = fopen(filename, "r");
while(!feof(rawdata)){
//printf("loop");
c = fgetc(rawdata);//printf("count c = %d %d\n", c, count[c]);
if(c != EOF){
k = map[c];
for(j = count[c] - 1; j >= 0; j--, i++){
printf("%d", (k>>j)&0x01);
fputc(((k>>j)&0x01)+'0', outdata);
}
}
}
fclose(outdata);
}
int main(){
encode("war.txt", "encode.txt");
}