-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHuffman_Coding_And_Decoding_Algorithm.cpp
485 lines (419 loc) · 10.2 KB
/
Huffman_Coding_And_Decoding_Algorithm.cpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/**
* @file: Huffman_Coding_And_Decoding_Algorithm.cpp
* @author: Avinash Yadav
* @brief: This file contains the implementation of Huffman coding and decoding algorithm.
* @date: 07-05-2024
*/
#include <bits/stdc++.h>
using namespace std;
struct node
{
char info;
int freq;
char *code;
node *Llink;
node *Rlink;
};
// Coding Tree
class BinaryTree
{
private:
node *root;
public:
BinaryTree() { root = NULL; }
void print();
void assign_code(int i);
void print_code(char c);
void encode(const char str[]);
void print_symbol(char cd[], int &f, int length);
void decode(char cd[], int size);
friend class minHeap;
friend class HuffmanCode;
};
class minHeap
{
private:
// Array of Binary Trees
BinaryTree *T;
// Number of symbols
int n;
public:
minHeap();
void heapify(int i);
// Returns the first Binary Tree of the min heap and
// then heapify the array of Binary trees in order of the
// frequencies of their root nodes.
BinaryTree dequeue();
// To insert another Binary tree
// and then heapify the array of Binary trees
void enqueue(BinaryTree b);
void print();
friend class HuffmanCode;
};
class HuffmanCode
{
private:
// (A minimum weighted external path length tree)
BinaryTree HuffmanTree;
public:
HuffmanCode();
};
HuffmanCode::HuffmanCode()
{
minHeap Heap;
// Huffman Tree is build from bottom to top.
// The symbols with lowest frequency are at the bottom of the tree
// that leads to longer codes for lower frequency symbols and hence
// shorter codes for higher frequency symbol giving OPTIMAL code length.
while (Heap.T[0].root->freq > 1)
{
// The first two trees with min. priority (i.e. frequency) are taken and
BinaryTree l = Heap.dequeue();
cout << "\nAfter dequeueing " << l.root->freq << endl;
Heap.print();
BinaryTree r = Heap.dequeue();
cout << "\nAfter dequeueing " << r.root->freq << endl;
Heap.print();
// a new tree is constructed taking the above trees as left and right sub-trees
// with the frequency of root node as the sum of frequencies of left & right child.
HuffmanTree.root = new node;
HuffmanTree.root->info = '\0';
HuffmanTree.root->freq = l.root->freq + r.root->freq;
HuffmanTree.root->Llink = l.root;
HuffmanTree.root->Rlink = r.root;
// then it is inserted in the array and array is heapified again.
// Deletion and Insertion at an intermediate step is facilitated in heap-sort.
Heap.enqueue(HuffmanTree);
cout << "\nAfter enqueueing " << l.root->freq << " + " << r.root->freq << " = " << HuffmanTree.root->freq << endl;
Heap.print();
}
// The process continues till only one tree is left in the array of heap.
cout << "\nThe process is completed and Huffman Tree is obtained\n";
HuffmanTree = Heap.T[1]; // This tree is our HuffmanTree used for coding
delete[] Heap.T;
cout << "Traversal of Huffman Tree\n\n";
HuffmanTree.print();
cout << "\nThe symbols with their codes are as follows\n";
HuffmanTree.assign_code(0); // Codes are assigned to the symbols
cout << "Enter the string to be encoded by Huffman Coding: ";
char *str;
str = new char[30];
cin >> str;
HuffmanTree.encode(str);
cout << "\nEnter the code to be decoded by Huffman Coding: ";
char *cd;
cd = new char[100];
cin >> cd;
// cout << "Enter its code length: ";
// cin >> length;
int length = strlen(cd);
cout << "The length of the coded string is: " << length << endl;
HuffmanTree.decode(cd, length);
delete[] cd;
delete[] str;
}
minHeap::minHeap()
{
cout << "Enter Total Number Of Symbols In The String: ";
cin >> n;
cout << "Enter Each Character Of The String and Their Frequency One At A Time" << endl;
cout << "Enter Characters Only In Small Letter...";
cout << endl;
T = new BinaryTree[n + 1];
T[0].root = new node;
T[0].root->freq = n; // Number of elements in min. Heap at any time is stored in the
// zeroth element of the heap
for (int i = 1; i <= n; i++)
{
T[i].root = new node;
cout << "\nEnter characters of string :- ";
cin >> T[i].root->info;
cout << "and its frequency of occurence in the string:- ";
cin >> T[i].root->freq;
T[i].root->code = NULL;
T[i].root->Llink = NULL;
T[i].root->Rlink = NULL;
// Initially, all the nodes are leaf nodes and stored as an array of trees.
}
cout << endl;
// Heapification will be started from the PARENT element of
int i = (int)(n / 2);
// the last ( 'n th' ) element in the heap.
cout << "\nAs Per The Entered Elements\n";
print();
while (i > 0)
{
heapify(i);
i--;
}
cout << "\nAfter heapification \n";
print();
}
int min(node *a, node *b)
{
if (a->freq <= b->freq)
return a->freq;
else
return b->freq;
}
void swap(BinaryTree &a, BinaryTree &b)
{
BinaryTree c = a;
a = b;
b = c;
}
void minHeap::heapify(int i)
{
while (1)
{
if (2 * i > T[0].root->freq)
return;
if (2 * i + 1 > T[0].root->freq)
{
if (T[2 * i].root->freq <= T[i].root->freq)
swap(T[2 * i], T[i]);
return;
}
int m = min(T[2 * i].root, T[2 * i + 1].root);
if (T[i].root->freq <= m)
return;
if (T[2 * i].root->freq <= T[2 * i + 1].root->freq)
{
swap(T[2 * i], T[i]);
i = 2 * i;
}
else
{
swap(T[2 * i + 1], T[i]);
i = 2 * i + 1;
}
}
}
BinaryTree minHeap::dequeue()
{
BinaryTree b = T[1];
T[1] = T[T[0].root->freq];
T[0].root->freq--;
if (T[0].root->freq != 1)
heapify(1);
return b;
}
void minHeap::enqueue(BinaryTree b)
{
T[0].root->freq++;
T[T[0].root->freq] = b;
int i = (int)(T[0].root->freq / 2);
while (i > 0)
{
heapify(i);
i = (int)(i / 2);
}
}
int isleaf(node *nd)
{
if (nd->info == '\0')
return 0;
else
return 1;
}
void BinaryTree::assign_code(int i)
{
if (root == NULL)
return;
if (isleaf(root))
{
root->code[i] = '\0';
cout << root->info << "\t" << root->code << "\n";
return;
}
BinaryTree l, r;
l.root = root->Llink;
r.root = root->Rlink;
l.root->code = new char[i + 1];
r.root->code = new char[i + 1];
for (int k = 0; k < i; k++)
{
l.root->code[k] = root->code[k];
r.root->code[k] = root->code[k];
}
l.root->code[i] = '0';
r.root->code[i] = '1';
i++;
l.assign_code(i);
r.assign_code(i);
}
void BinaryTree::encode(const char str[])
{
if (root == NULL)
return;
int i = 0;
cout << "\nEncoded code for the input string '" << str << "' is\n";
while (1)
{
if (str[i] == '\0')
{
cout << endl;
return;
}
print_code(str[i]);
i++;
}
}
void BinaryTree::print_code(char c)
{
int f = 0;
if (isleaf(root))
{
if (c == root->info)
{
f = 1;
cout << root->code;
}
return;
}
BinaryTree l, r;
l.root = root->Llink;
if (f != 1)
l.print_code(c);
r.root = root->Rlink;
if (f != 1)
r.print_code(c);
}
int isequal(const char a[], const char b[], int length)
{
int i = 0;
while (i < length)
{
if (b[i] != a[i])
return 0;
i++;
}
if (a[i] != '\0')
return 0;
return 1;
}
void BinaryTree::decode(char cd[], int size)
{
if (root == NULL)
return;
int i = 0;
int length = 0;
int f;
char *s;
cout << "\nDecoded string for the input code '" << cd << "' is\n";
while (i < size)
{
f = 0;
s = &cd[i];
while (f == 0)
{
length++;
print_symbol(s, f, length);
}
i = i + length;
length = 0;
}
cout << endl;
}
void BinaryTree::print_symbol(char cd[], int &f, int length)
{
if (isleaf(root))
{
if (isequal(root->code, cd, length))
{
f = 1;
cout << root->info;
}
return;
}
BinaryTree l, r;
l.root = root->Llink;
if (f != 1)
l.print_symbol(cd, f, length);
r.root = root->Rlink;
if (f != 1)
r.print_symbol(cd, f, length);
}
void BinaryTree::print()
{
if (root == NULL)
return;
cout << root->info << "\t" << root->freq << "\n";
if (isleaf(root))
return;
BinaryTree l, r;
l.root = root->Llink;
r.root = root->Rlink;
l.print();
r.print();
}
int power(int i, int j)
{
int n = 1;
for (int k = 1; k <= j; k++)
n = n * i;
return n;
}
int ispowerof2(int i)
{
if (i == 1)
return 0;
if (i == 0)
return 1;
while (i > 2)
{
if (i % 2 != 0)
return 0;
i = i / 2;
}
return 1;
}
int fn(int l)
{
if (l == 1 || l == 0)
return 0;
return 2 * fn(l - 1) + 1;
}
void minHeap::print()
{
cout << "The Heap showing the root frequencies of the Binary Trees are:\n";
if (T[0].root->freq == 0)
{
cout << endl;
return;
}
int level = 1;
// 2^n-1 is the max. no. of nodes in a complete tree of n levels
while (T[0].root->freq >= power(2, level))
level++;
if (level == 1)
{
cout << T[1].root->freq << endl;
return;
}
for (int i = 1; i <= T[0].root->freq; i++)
{
if (ispowerof2(i))
{
cout << endl;
level--;
}
for (int k = 1; k <= fn(level); k++)
cout << " ";
cout << T[i].root->freq << " ";
for (int k = 1; k <= fn(level); k++)
cout << " ";
}
cout << endl;
}
int main()
{
HuffmanCode c;
cout << endl;
cout << endl;
cout << endl;
cout << "<----------------------...END OF THE CODE...---------------------->" << endl;
cout << endl;
system("pause");
return 0;
}