-
Notifications
You must be signed in to change notification settings - Fork 3
/
HuffmanDecoder.c
194 lines (170 loc) · 5.91 KB
/
HuffmanDecoder.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
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*******************************************************************************
List structs - Maintains character and its frequency in a structure
*******************************************************************************/
struct Node {
char character;
int frequency;
struct Node * left;
struct Node * right;
} * temp = NULL, * node1 = NULL, * node2 = NULL;
/* declare the temp, node1 ,node2 so that in lower function,
just have to initialize*/
/*******************************************************************************
newNode
Function to create a new node without a child
*******************************************************************************/
struct Node * newNode(char character, int frequency)
{
struct Node * temp = (struct Node * ) malloc(sizeof(struct Node));
temp -> left = NULL;
temp -> right = NULL;
temp -> character = character;
temp -> frequency = frequency;
return temp;
}
/*******************************************************************************
newNodeWithChilds
Function to create a new node with children
*******************************************************************************/
struct Node * newNodeWithChilds(char character,
int frequency, struct Node * leftNode, struct Node * rightNode)
{
struct Node * temp = (struct Node * ) malloc(sizeof(struct Node));
temp -> left = leftNode;
temp -> right = rightNode;
temp -> character = character;
temp -> frequency = frequency;
return temp;
}
/*******************************************************************************
isLeaf
Check if given node is leaf of the tree
*******************************************************************************/
int isLeaf(struct Node * root)
{
return !(root -> left) && !(root -> right);
}
/*******************************************************************************
printCodes
generates the individual binary codes of characters from the huffman tree
*******************************************************************************/
void printCodes(struct Node * root, int array[], int top, int * * characters)
{
/*if node has left node */
if (root -> left != NULL)
{
array[top] = 0;
printCodes(root -> left, array, top + 1, characters);
}
/*if node has right node */
if (root -> right != NULL)
{
array[top] = 1;
printCodes(root -> right, array, top + 1, characters);
}
/*if node is a leaf, then its a character */
if (isLeaf(root))
{
int index = (int) root -> character;
int i = 0;
for (i = 0; i < top; i++)
{
characters[index][i] = array[i];
}
printf("\n%c -> ", (char) index);
for (i = 0; i < top; i++)
{
printf("%d", characters[index][i]);
}
characters[index][top] = -1;
printf("\n");
}
}
/*******************************************************************************
decode
Function to decode the encoded characters
*******************************************************************************/
void decode(struct Node * root, char * encodeString) {
int i;
struct Node * temp = root;
FILE *fp;
char output[]="DecodedOutput.txt";
fp=fopen(output,"w");
for (i = 0; encodeString[i] != '\0'; i++) {
if (encodeString[i] == '1') {
temp = temp -> right;
} else {
temp = temp -> left;
}
if (isLeaf(temp)) {
printf("%c", temp -> character);
fprintf(fp,"%c",temp -> character);
temp = root;
}
}
fclose(fp);
}
/*******************************************************************************
readFile
Function to read input file
*******************************************************************************/
char * readFile(char * inputFile) {
FILE * file = fopen(inputFile, "r");
char * characters;
size_t n = 0;
int c;
if (file == NULL)
return NULL;
characters = malloc(32000);
while ((c = fgetc(file)) != EOF) {
characters[n++] = (char) c;
}
characters[n] = '\0';
return characters;
}
/*******************************************************************************
readTree
Function to read the Huffman tree for decoding
*******************************************************************************/
struct Node * readTree(FILE * fp){
int c=fgetc(fp);
if(c==EOF)
return NULL;
if(((char)c)=='1'){
c=fgetc(fp);
return newNode(((char)c),0);
}
else
{
struct Node *leftChild=readTree(fp);
struct Node *rightChild=readTree(fp);
return newNodeWithChilds('\0',0,leftChild,rightChild);
}
}
/*******************************************************************************
Main
*******************************************************************************/
int main()
{
FILE *fp1;
char output1[]="HuffManTree.txt";
fp1=fopen(output1,"r");
struct Node *root=readTree(fp1);
fclose(fp1);
int path[1000];
int * encoded[256];
int i;
for (i = 0; i < 256; i++)
encoded[i] = (int * ) malloc(100 * sizeof(int));
printCodes(root, path, 0, encoded);
char * characters;
characters = readFile("EncodedOutput.txt");
printf("\nRead from file : %s\n\n", characters);
printf("\nDecoded string: ");
decode(root, characters);
printf("\n");
return 0;
}