-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman.cpp
478 lines (380 loc) · 12.6 KB
/
Huffman.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: huffman.cpp
* Author: zain
*
* Created on October 16, 2016, 7:01 PM
*/
#include <stddef.h>
#include <stdio.h>
#include "Huffman.h"
#include "HuffmanNode.h"
Huffman::Huffman()
{
}
// compress file
void Huffman::compressFile(const string& fileName)
{
vector<int> numList;
file=new FileReader(fileName);
file->processFile();
numList=file->getNumList();
ifstream inputFile(fileName.c_str(),ios::in);
// incorrect file open checking
if(!inputFile)
{
cout<<"Invalid file or file not exist"<<endl;
return;
}
inputFile.close();
// remove .8b from last and add .out for output file
string outputFileName=fileName.substr(0,fileName.size()-3)+".zif";
ofstream outputFile; // output file adding header
outputFile.open(outputFileName.c_str(),ios::out);
int freqTab[256];
// zero all index of frequency table
for(int i=0;i<256;i++)
freqTab[i]=0;
// modify frequency table
for(int i=0;i<numList.size();i++)
{
freqTab[numList.at(i)]++;
}
Data_Freq df;
HuffmanNode *huffmanNode;
int checkFlag=0;
// variable if only one pattern present in file
int onlyOneItemData=0;
int onlyOneItemFreq=0;
// add non zero frequency items in dataFreq list
for(int i=0;i<256;i++)
{
// create nodes and push in queue
if(freqTab[i]>0)
{
df.setData(i);
df.setFreq(freqTab[i]);
// create huffman node
huffmanNode=new HuffmanNode;
huffmanNode->item=df;
huffmanNode->left=NULL;
huffmanNode->right=NULL;
// open compress file add data.frequency at first as header
if(checkFlag==0)
{
outputFile<<df.getData()<<"."<<df.getFreq()<<" ";
onlyOneItemData=df.getData();
onlyOneItemFreq=df.getFreq();
checkFlag++;
}
else if(checkFlag==1)
{
outputFile<<df.getData()<<"."<<df.getFreq();
checkFlag++;
}
else if(checkFlag>1)
{
outputFile<<" "<<df.getData()<<"."<<df.getFreq();
}
// and push in priority queue
minPriorityQ.push(huffmanNode);
}
}
outputFile<<endl;
// close file after adding header
outputFile.close();
// if only one pattern present in file
if(checkFlag==1)
{
outputFile.open(outputFileName.c_str());
outputFile<<onlyOneItemData<<"."<<onlyOneItemFreq;
outputFile<<endl;
outputFile.close();
}
// zero all index of frequency table
for(int i=0;i<256;i++)
freqTab[i]=0;
// run while loop until queue number of items in queue becomes 1
while(minPriorityQ.size()!=1)
{
// create internal heap node
HuffmanNode *heapInternalNode=new HuffmanNode;
// heapInternalNode->leftBranch='0'; // set node left branch tag = 0
heapInternalNode->left=minPriorityQ.top(); // attach node left with min queue item
heapInternalNode->item.setData(-1); // set node data = -1
heapInternalNode->item.setFreq(minPriorityQ.top()->item.getFreq()); // get frequency of first min queue item, set to node frequency
minPriorityQ.pop(); // pop item
// heapInternalNode->rightBranch='1'; // set node left branch tag = 0
heapInternalNode->right=minPriorityQ.top(); // attach node right with next min queue item
heapInternalNode->item.setFreq(heapInternalNode->item.getFreq()+minPriorityQ.top()->item.getFreq()); // get frequency of second min queue item and add with current node frequency, and set
minPriorityQ.pop();
minPriorityQ.push(heapInternalNode); // push node to queue again
}
HuffmanNode *heapRootNode=minPriorityQ.top(); // get last item of queue and it is a root node of MaxHeap
minPriorityQ.pop();
// print Max Heap
// printMaxHeap(heapRootNode);
string code;
// only one data pattern present in file
if(heapRootNode->left==NULL&&heapRootNode->right==NULL)
{
heapRootNode->item.setCode("0");
num_Key[heapRootNode->item.getData()]=heapRootNode->item.getCode();
}
else
generateHuffmanCode(heapRootNode,code);
// create compress file
createCompressFile(outputFileName);
// delete all heap nodes after compress file creation
// deleteMaxHeap(heapRootNode);
// delete node also
// delete huffmanNode;
}
// function compressFile function
void Huffman::createCompressFile(const string& fileName)
{
ifstream inputFile;
ofstream outputFile;
inputFile.open(file->getFileName().c_str(),ios::in);
if(!inputFile)
{
cout<<"Invalid file or file not exist"<<endl;
return;
}
outputFile.open(fileName.c_str(),ios::app); // append data after data.frequency header
FileReader *f=new FileReader;
char c;
string oneByte="";
int count=0;
while(inputFile.get(c))
{
oneByte=oneByte+c;
count++;
// get 1 byte binary pattern
if(count==8)
{
// convert binary number of input file convert into decimal, find number(key) from map and print its corresponding binary(value) in output file.
outputFile<<num_Key[f->binToDec(oneByte)];
count=0;
oneByte="";
}
}
outputFile.close();
inputFile.close();
}
// generate huffman code from huffman tree
void Huffman::generateHuffmanCode(HuffmanNode * tempRoot, string s)
{
HuffmanNode * dummyRoot = new HuffmanNode;
dummyRoot = tempRoot;
dummyRoot->item.setCode(s);
if (dummyRoot == NULL)
{
// return;
}
else if (dummyRoot->left == NULL && dummyRoot->right== NULL)
{
// get huffman code of digit.
num_Key[dummyRoot->item.getData()]=dummyRoot->item.getCode();
}
else
{
dummyRoot->left->item.setCode(s.append("0"));
s.erase(s.end() - 1);
dummyRoot->right->item.setCode(s.append("1"));
s.erase(s.end() - 1);
generateHuffmanCode(dummyRoot->left, s.append("0"));
s.erase(s.end() - 1);
generateHuffmanCode(dummyRoot->right, s.append("1"));
s.erase(s.end() - 1);
}
}
// print MAX Heap in infix order, edit to print branch pattern for each external node
void Huffman::printMaxHeap(HuffmanNode* root)
{
if(root!=NULL)
{
printMaxHeap(root->left);
if(root->item.getData()!=-1)
{
cout << endl;
cout << "Data : " << root->item.getData() << endl;
cout << "Frequency : " << root->item.getFreq() << endl;
cout << "Code : " << root->item.getCode() << endl;
}
printMaxHeap(root->right);
}
}
// delete heap tree after creating compress file
void Huffman::deleteMaxHeap(HuffmanNode* &root)
{
if(root==NULL)
{
return;
}
deleteMaxHeap(root->left);
deleteMaxHeap(root->right);
delete root;
root=NULL;
}
// decompress compressed file
void Huffman::decompressFile(const string& fileName)
{
vector<Data_Freq> DF;
DF.clear();
ifstream inputFile;
inputFile.open(fileName.c_str(),ios::in);
if(!inputFile)
{
cout<<"Invalid file or file not exist"<<endl;
return;
}
// decompress file
string outputFileName=fileName.substr(0,fileName.length()-4)+"_N.8b";
ofstream outputFile;
outputFile.open(outputFileName.c_str(),ios::out);
char ch='0';
string dataStr="";
string freqStr="";
int data=0;
int freq=0;
// read header tree from file
while(ch!='\n')
{
// read data.frequency from file
// read data
while(inputFile.get(ch)&&ch!='.')
{
dataStr=dataStr+ch;
}
// read frequency
while(inputFile.get(ch)&&ch!=' ')
{
if(ch==' '||ch=='\n')
{
break;
}
freqStr=freqStr+ch;
}
data=atoi(dataStr.c_str());
freq=atoi(freqStr.c_str());
DF.push_back(Data_Freq(data, freq));
if(ch=='\n')
{
break;
}
freq=0;
data=0;
freqStr="";
dataStr="";
}
// create huffman node
HuffmanNode *huffmanNode;
// create nodes which contain Data and Frequency and push in queue
for (int i = 0; i < DF.size(); i++)
{
// create huffman node
huffmanNode = new HuffmanNode;
huffmanNode->item = DF[i];
huffmanNode->left = NULL;
huffmanNode->right = NULL;
// and push in priority queue
minPriorityQ.push(huffmanNode);
}
// if than one nodes present in huffman tree
if (DF.size() > 1)
{
// run while loop until queue number of items in queue becomes 1
while (minPriorityQ.size() != 1)
{
// create internal heap node
HuffmanNode *heapInternalNode = new HuffmanNode;
heapInternalNode->left = minPriorityQ.top(); // attach node left with min queue item
heapInternalNode->item.setData(-1); // set node data = -1
heapInternalNode->item.setFreq(minPriorityQ.top()->item.getFreq()); // get frequency of first min queue item, set to node frequency
minPriorityQ.pop(); // pop item
heapInternalNode->right = minPriorityQ.top(); // attach node right with next min queue item
heapInternalNode->item.setFreq(heapInternalNode->item.getFreq() + minPriorityQ.top()->item.getFreq()); // get frequency of second min queue item and add with current node frequency, and set
minPriorityQ.pop();
minPriorityQ.push(heapInternalNode); // push node to queue again
}
}
HuffmanNode *heapRootNode=minPriorityQ.top(); // get last item of queue and it is a root node of MaxHeap
minPriorityQ.pop();
// print Max Heap
// printMaxHeap(heapRootNode);
// read compressed file, traverse in tree and get original code from leaf node
// if than one nodes present in huffman tree
if (DF.size() > 1)
{
HuffmanNode *current;
current=heapRootNode;
inputFile.get(ch);
while (!inputFile.eof())
{
if (ch == '0')
{
current = current->left;
}
else if (ch == '1')
{
current = current->right;
}
// if current point out leaf node
if (current->left == NULL && current->right == NULL)
{
outputFile << getBinary(current->item.getData()); // print 8-bit binary in file
current = heapRootNode; // reset current pointer to root
}
inputFile.get(ch);
}
}
// if only one nodes present in huffman tree
else if(DF.size()==1)
{
inputFile.get(ch);
while(!inputFile.eof())
{
outputFile<<getBinary(huffmanNode->item.getData());
inputFile.get(ch);
}
}
inputFile.close();
outputFile.close();
}
// get binary from decimalToBinary function and convert into 8-bit length
string Huffman::getBinary(int num)
{
stringstream binStr;
string binaryStr;
decimalToBinary(num,binStr);
// convert string stream into string
binaryStr=binStr.str();
// find difference convert binary into 8-bit binary by add zero at beginning
int strDiff=8-binaryStr.length();
for(int i=0;i<strDiff;i++)
{
binaryStr="0"+binaryStr;
}
return binaryStr;
}
// convert decimal number into binary number
void Huffman::decimalToBinary(int number, stringstream& numStr)
{
int remainder;
if(number <= 1) {
numStr << number;
return;
}
remainder = number%2;
decimalToBinary(number >> 1,numStr);
numStr << remainder;
}
// for decompression write huffman code table in output file than write compress string get pattern and match with table
Huffman::~Huffman()
{
}