/
correct-huffman.c
306 lines (241 loc) · 7.9 KB
/
correct-huffman.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Program Statement : Implementation of Huffman Tree
Description : By Designing the Huffman Tree it is possible to encode the given sentence based on the occurrence of the character in the
string
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"characterFrequency.h"
#define MAXSIZE 15
//structure declaraction to store the code word generated from the huffman tree
struct Code
{
char ch; //to store the character
char code[MAXSIZE]; //TO store the code word for respective character
};
typedef struct Code CODE;
//structure declaraction to store the tree datastructure
struct Tree
{
char character; //variable to hold the character
float prob; //To store the probability of occurrence of the respective character in the given text
struct Tree *left;
struct Tree *right;
};
typedef struct Tree TREE;
//structure to store the information about the collection of data and intermediate trees
struct Node
{
TREE *root; //pointer variable to hold the root of the tree
struct Node *link; //link to the next node
};
typedef struct Node NODE;
//set of global variables
CODE code[MAXSIZE];
int pointer; //variable to hold the count of how many code word has been generated for particular character
FREQUENCY fre[MAXSIZE]; //array of structure declared in user defined header file which contins the information about frequency and
// probability of occurrence of characters in the given text.
NODE *n=NULL,*backup; //n is a pointer to hold the address of first node
//Function to generate the node by allocating the memory for each node.
NODE *getNode()
{
NODE *temp;
temp = (NODE *)malloc(sizeof(NODE *));
temp->root = (TREE *) malloc(sizeof(TREE *));
temp->link=NULL;
temp->root->left = NULL;
temp->root->right = NULL;
return temp;
}
void insert(char,float); //function to insert character and its probability into linked list
void constructTree(NODE *,NODE *); //function to construct the huffman tree
void insertTree(NODE *); //function to insert intermediate tree into the list
void inorder(TREE *); //Process the inorder traversal of huffman tree
void display();
void encode(TREE *,int [],int); //encode the set of character based on the generated huffman tree
void diplayEncodedString(char []); //display the encoded string
int main()
{
int num,i;
char c;
char sentence[MAXSIZE];
int top=0,arr[MAXSIZE];
NODE *temp1,*temp2;
printf("Enter the sentence to be encoded : ");
scanf("%[^\n]",sentence);
num = calculateFre(fre,sentence); //calculate the frequency of occurrence
displayFre(fre); //displayFre() function present in the header file.
for(i=0;i<num;i++)
{
insert(fre[i].ch,fre[i].prob); //insert every character into linked list
}
//display();
while(n->link != NULL)
{
temp1=n;
temp2 = n->link;
n=n->link->link;
constructTree(temp1,temp2);
}
encode(n->root,arr,top);
diplayEncodedString(sentence);
//inorder(n->root);
}
void insert(char ch,float prob)
{
NODE *prev,*cur,*temp;
prev=NULL;
temp = getNode();
temp->root->prob = prob;
temp->root->character = ch;
if(n == NULL)
{
n=temp;
}
else if((n->root->prob) > prob)
{
temp->link = n;
n=temp;
}
else if(n->link == NULL)
{
cur = n;
cur->link = temp;
}
else
{
cur = n;
while(cur != NULL)
{
if((cur->root->prob) > prob)
{
temp->link = prev -> link;
prev->link = temp;
break;
}
prev = cur;
cur = cur->link;
}
if(cur==NULL)
{
prev->link=temp;
}
}
}
void constructTree(NODE *temp1,NODE *temp2)
{
NODE *temp;
temp = getNode();
temp->root->left=temp1->root;
temp->root->right=temp2->root;
temp->root->prob = temp1->root->prob + temp2->root->prob;
insertTree(temp);
}
void insertTree(NODE *temp)
{
NODE *cur,*prev;
if(n==NULL)
{
n=temp;
return;
}
else if(n->root->prob > temp->root->prob)
{
temp->link = n;
n=temp;
return;
}
else if(n->link == NULL)
{
n->link = temp;
}
else
{
prev=NULL;
cur = n;
while(cur!=NULL)
{
if(temp->root->prob < cur->root->prob)
{
temp->link = prev->link;
prev->link = temp;
break;
}
prev = cur;
cur = cur->link;
}
if(cur == NULL)
{
prev->link = temp;
}
}
}
void inorder(TREE *root)
{
if(root!=NULL)
{
inorder(root->left);
//printf("%f------%c\n",root->prob,root->character);
inorder(root->right);
}
}
void display()
{
NODE *test;
test = n;
while(test!=NULL)
{
printf("%f------%c\n",test->root->prob,test->root->character);
test = test->link;
}
}
void encode(TREE *head,int arr[],int top)
{
char temp[MAXSIZE];
if(head->left != NULL)
{
arr[top] = 0;
encode(head->left,arr,top+1);
}
if(head->right != NULL)
{
arr[top] = 1;
encode(head->right,arr,top+1);
}
if(head->left==NULL && head->right==NULL)
{
int i;
for(i=0;i<top;i++)
{
temp[i] = arr[i]+48;
}
temp[i] = '\0';
strcpy(code[pointer].code,temp);
code[pointer].ch = head->character;
//printf("%c %s\n",head->character,code[pointer].code);
pointer++;
}
}
void diplayEncodedString(char string[])
{
char codeWord[100];
int i,j;
codeWord[0]='\0';
printf("\nCHARACTER\tCODEWORD\n");
for(i=0;i<pointer;i++)
{
j=0;
while(j<pointer)
{
if(string[i] == code[j].ch)
{
strcat(codeWord,code[j].code);
break;
}
j++;
}
printf("\n%c\t\t%s\n",code[i].ch,code[i].code);
}
printf("Encoded string for the given string %s is \n%s\n",string,codeWord);
}