-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffmanCoding.cpp
496 lines (409 loc) · 12.7 KB
/
HuffmanCoding.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
486
487
488
489
490
491
492
493
494
495
496
#include <iostream>
#include <fstream>
#include <cstdint>
#include <filesystem>
#include <sstream>
#include <bitset>
#include <string>
#include <unordered_map>
#include <queue>
#include <vector>
using namespace std;
const char CHAR_EOF = -1;
//Class HUFFMAN TREE
class HuffmanTree {
public:
//Struct HUFFMAN NODE
struct HuffmanNode {
char ch;
int freq;
struct HuffmanNode* left;
struct HuffmanNode* right;
//Default constructor
HuffmanNode()
: ch('\0'), freq(0), left(nullptr), right(nullptr)
{
}
//Paramerterized constructor
HuffmanNode(char ch, int freq, HuffmanNode* left, HuffmanNode* right)
: ch(ch), freq(freq), left(left), right(right)
{
}
}*root;
//Struct COMPARE FREQUENCY
//Used for making min priority queue
struct CompareFrequency {
bool operator()(HuffmanNode* const& n1, HuffmanNode* const& n2)
{
return n1->freq > n2->freq;
}
};
//Default constructor
HuffmanTree() {
root = nullptr;
}
//Function Headers
void preorderTraversal(HuffmanNode* root);
void preorderHuffmanTree(HuffmanNode* root, string& traversal);
void createEncodedPreorderTraversal(HuffmanNode* root, string& traversal);
void createTreeFromPreorder(HuffmanNode* root, int& index, string& traversal);
};
//Class HUFFMAN ENCODING
class HuffmanEncoding {
public:
//Function Headers
unordered_map<int, int> countFrequencies(ifstream& infile);
HuffmanTree::HuffmanNode* buildEncodingTree(unordered_map<int, int>& freq);
void buildEncodingMap(HuffmanTree::HuffmanNode* root, string code, unordered_map<int, string>& codes);
void compressFile(ifstream& infile, HuffmanTree::HuffmanNode* root, unordered_map<int, string>& encodingMap, ofstream& outfile);
void decompressFile(ifstream& infile, ofstream& outfile);
};
//Functions
/*----------------
HUFFMAN TREE
----------------*/
//PREORDER TRAVERSAL
void HuffmanTree::preorderTraversal(HuffmanNode* root) {
if (root != nullptr) {
cout << root->ch << " ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
//PREORDER HUFFMAN TREE
void HuffmanTree::preorderHuffmanTree(HuffmanNode* root, string& traversal) {
if (root != nullptr) {
//If leaf node, append 1 and the charcater stored on the leaf
if (!(root->left) && !(root->right)) {
traversal += '1';
traversal += root->ch;
}
//If non-leaf node, append 0
else if ((root->left) && (root->right)) {
traversal += '0';
}
preorderHuffmanTree(root->left, traversal);
preorderHuffmanTree(root->right, traversal);
}
}
//CERATE ENCODED PREORDER TRAVERSAL
void HuffmanTree::createEncodedPreorderTraversal(HuffmanNode* root, string& traversal) {
if (root != nullptr) {
//If leaf node, append 1
if (!(root->left) && !(root->right)) {
traversal += '1';
//Append encoding for CHAR_EOF
if ((int)root->ch == -1) {
traversal += "11111111";
}
//Append the charcater stored on the leaf
else {
traversal += (bitset<8>(root->ch).to_string());
}
}
//If non-leaf node, append 0
else if ((root->left) && (root->right)) {
traversal += '0';
}
createEncodedPreorderTraversal(root->left, traversal);
createEncodedPreorderTraversal(root->right, traversal);
}
}
//CREATE TREE FROM PREORDER
void HuffmanTree::createTreeFromPreorder(HuffmanNode* root, int& index, string& traversal) {
if (root != nullptr) {
//If 1, make a leaf node and store the charcater
if (traversal[index] == '1') {
index++;
root->ch = traversal[index];
return;
}
root->left = new HuffmanNode;
createTreeFromPreorder(root->left, ++index, traversal);
root->right = new HuffmanNode;
createTreeFromPreorder(root->right, ++index, traversal);
}
}
/*--------------------
HUFFMAN ENCODING
--------------------*/
//COUNT FREQUENCIES
unordered_map<int, int> HuffmanEncoding::countFrequencies(ifstream& infile){
unordered_map<int, int> freq;
string fileText;
stringstream buffer;
//Read data from file
buffer << infile.rdbuf();
fileText = buffer.str();
//Reset the input stream to the start of file
infile.clear();
infile.seekg(0);
//Count frequencies for every characters
for (char ch : fileText) {
freq[ch]++;
}
//CHAR_EOF occurs only once
freq[CHAR_EOF]++;
return freq;
}
//BUILD ENCODING TREE
HuffmanTree::HuffmanNode* HuffmanEncoding::buildEncodingTree(unordered_map<int, int> &freq) {
HuffmanTree t;
priority_queue<HuffmanTree::HuffmanNode*, vector<HuffmanTree::HuffmanNode*>, HuffmanTree::CompareFrequency> pq;
//Make a min priority queue of Huffman nodes (containing character with frequency)
for (auto& x : freq) {
pq.push(new HuffmanTree::HuffmanNode(x.first, x.second, nullptr, nullptr));
}
//Repeatedly remove two nodes from the the queue and join them into a new node whose frequency is their sum
//The two nodes are positioned as children of the new node and the new node is re-inserted into the queue in sorted order
while (pq.size() != 1) {
HuffmanTree::HuffmanNode* n1 = pq.top();
pq.pop();
HuffmanTree::HuffmanNode* n2 = pq.top();
pq.pop();
pq.push(new HuffmanTree::HuffmanNode('\0', n1->freq + n2->freq, n1, n2));
}
HuffmanTree::HuffmanNode* root = pq.top();
cout << "\n\nPreorder Traversal of Huffman Tree:\n" << endl;
t.preorderTraversal(root);
cout << endl;
return root;
}
//BUILD ENCODING MAP
void HuffmanEncoding::buildEncodingMap(HuffmanTree::HuffmanNode* root, string code, unordered_map<int, string> &codes) {
if (root == nullptr) {
return;
}
//Stop on the leaf node and assign the code to the character stored on the leaf
if(!(root->left) && !(root->right)) {
codes[root->ch] = code;
}
//For Huffman codes, each left branch is considered 0 and each right branch 1
buildEncodingMap(root->left, code + "0", codes);
buildEncodingMap(root->right, code + "1", codes);
}
//COMPRESS FILE
void HuffmanEncoding::compressFile(ifstream& infile, HuffmanTree::HuffmanNode* root, unordered_map<int, string> &encodingMap, ofstream &outfile) {
HuffmanTree t;
string encoding = "", traversal = "";
int surplusBits = 0;
char ch;
t.preorderHuffmanTree(root, traversal);
cout << "\n\nTree Information To Be Used During Decompression:\n" << endl;
cout << traversal << endl;
//Create encoding for preorder traversal of Huffman tree to store it in file header
traversal = "";
t.createEncodedPreorderTraversal(root, traversal);
traversal += '1';
traversal += "11111110"; //Header separation from the encoded data
encoding += traversal;
//Check if streams are open
if (infile.is_open() && outfile.is_open()) {
//Encode each character of the input file
while (infile.get(ch)) {
encoding += (encodingMap.at((int)ch));
}
//Append encoding for CHAR_EOF
encoding += (encodingMap.at(-1));
//Check if encoding length is exact multiple of 8 (Data can be written to a file only as bytes)
surplusBits = encoding.length() % 8;
if (surplusBits != 0) {
for (int x = 0; x < (8 - surplusBits); x ++) {
encoding += "0";
}
}
//Append zeroes to the end of encoding if required
int pos = 0;
for (int y = 0; y < (encoding.length() / 8); y++) {
bitset<8> b(encoding.substr(pos, 8));
outfile << static_cast<uint_fast8_t>(b.to_ulong());
pos += 8;
}
}
if (!infile.eof() && infile.fail()) {
cout << "Error reading the file." << endl;
}
}
//DECOMPRESS FILE
void HuffmanEncoding::decompressFile(ifstream& infile, ofstream& outfile) {
HuffmanTree t;
string decoding = "", headerInfo = "";
int j = 0;
bool headerEnded = false;
//Read the encoded file
stringstream buffer;
buffer << infile.rdbuf();
string encodedData = "";
//Convert to binary form
for (std::size_t i = 0; i < (buffer.str()).size(); i++)
{
encodedData += (bitset<8>((buffer.str()).c_str()[i])).to_string();
}
//Extract tree information (preorder traversal) from file header
while (headerEnded == false && j < encodedData.size()) {
headerInfo;
//If 1, append 1 and extract the character (1 is for leaf node)
if (encodedData[j] == '1') {
//If character is CHAR_EOF
if (encodedData.substr(j + 1, 8) == "11111111") {
headerInfo += '1';
headerInfo += CHAR_EOF;
}
//If character is header separation character, loop ends
else if (encodedData.substr(j + 1, 8) == "11111110") {
headerEnded = true;
}
//Extract character
else {
headerInfo += '1';
headerInfo += (bitset<8>(encodedData.substr(j + 1, 8)).to_ulong());
}
j += 8;
}
//If 0, append 0 (0 is for non-leaf node)
else if (encodedData[j] == '0') {
headerInfo += '0';
}
j++;
}
cout << "\n\nTree Information Extracted from File Header:\n" << endl;
cout << headerInfo << endl;
HuffmanTree::HuffmanNode* root = new HuffmanTree::HuffmanNode;
int index = 0;
//Create tree from the extracted information of preorder
t.createTreeFromPreorder(root, index, headerInfo);
cout << "\n\nTree Created From Preorder Traversal:\n" << endl;
t.preorderTraversal(root);
bool fileEnded = false;
HuffmanTree::HuffmanNode* temp = root;
//Decode the encoded data
while (fileEnded == false && j < encodedData.size()) {
//If 1, move right in the tree
if (encodedData[j] == '1') {
temp = temp->right;
}
//If 0, move left in th tree
else {
temp = temp->left;
}
//If leaf node is reached
if (!(temp->left) && !(temp->right)) {
//If CHAR_EOF is reached, loop ends
if ((int)(temp->ch) == -1) {
fileEnded = true;
}
//Write the character to the output file
else {
decoding += temp->ch;
outfile << temp->ch;
temp = root;
}
}
j ++;
}
}
/*--------------------
MAIN FUNCTION
--------------------*/
int main() {
HuffmanEncoding h;
HuffmanTree::HuffmanNode* r;
ifstream infile1, infile2;
ofstream outfile1, outfile2;
unordered_map<int, int> frequencies;
unordered_map<int, string> codes;
string fileName, fileName_;
std::uintmax_t size1, size2;
size_t lastIndex;
int userChoice, i, count1, j, count2;
//Display menu
cout << "\n--------------------" << endl;
cout << "Huffman Coding";
cout << "\n--------------------\n" << endl;
cout << "1. Compress a File" << endl;
cout << "2. Decompress a File" << endl;
cout << "\nEnter your choice : ";
cin >> userChoice;
cin.ignore();
switch (userChoice) {
//COMPRESS FILE
case 1:
//Take user input
cout << "Enter name/location of file to encode: ";
getline(cin, fileName);
lastIndex = fileName.find_last_of(".");
fileName_ = fileName.substr(0, lastIndex);
//Open file streams
infile1.open(fileName);
outfile1.open(fileName_ + "_encoded.huf", ios::out | ios::binary);
//Count frequencies
frequencies = h.countFrequencies(infile1);
//Display frequency map
cout << "\n\nFrequency Map:\n" << endl;
i = 1;
count1 = frequencies.size();
cout << "{";
for (auto& x : frequencies) {
if (i != count1) {
cout << (char)x.first << ":" << x.second << ", ";
}
else {
cout << (char)x.first << ":" << x.second;
}
i++;
}
cout << "}" << endl;
//Build Huffman tree
r = h.buildEncodingTree(frequencies);
//Build encoding map
h.buildEncodingMap(r, "", codes);
//Display encoding map
cout << "\n\nEncoding Map:\n" << endl;
j = 1;
count2 = codes.size();
cout << "{";
for (auto& y : codes) {
if (j != count2) {
cout << (char)y.first << ":" << y.second << ", ";
}
else {
cout << (char)y.first << ":" << y.second;
}
j++;
}
cout << "}" << endl;
//Compress file
h.compressFile(infile1, r, codes, outfile1);
//Close streams
infile1.close();
outfile1.close();
//Display sizes and compression ratio
size1 = std::filesystem::file_size(fileName);
size2 = std::filesystem::file_size(fileName_ + "_encoded.huf");
cout << "\n\nSize of the original file is " << size1 << " bytes" << endl;
cout << "Size of the encoded file is " << size2 << " bytes" << endl;
cout << "Compression ratio is " << (float)size1/size2 << ":1" << endl;
cout << "Compression percentage is " << 100 - ((float)size2 / size1) * 100 << "%" << endl;
break;
//DECOMPRESS FILE
case 2:
//Take user input
cout << "Enter name/location of file to decode: ";
getline(cin, fileName);
lastIndex = fileName.find("_encoded.huf");
fileName_ = fileName.substr(0, lastIndex);
//Open file streams
infile2.open(fileName, ios::binary | ios::in);
outfile2.open(fileName_ + "_decoded.txt");
//Decompress file
h.decompressFile(infile2, outfile2);
//Close streams
infile2.close();
outfile2.close();
cout << endl;
break;
default:
cout << "Wrong choice" << endl;
}
return 0;
}