-
Notifications
You must be signed in to change notification settings - Fork 19
/
Huffman.cpp
177 lines (150 loc) · 5.56 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
//
// Created by zawawy on 12/22/18.
//
#include "Huffman.h"
#define INTERNAL_NODE_CHARACTER char(128)
#define PSEUDO_EOF char(129)
#define CHARACTER_CODE_SEPERATOR char(130)
#define HEADER_ENTRY_SEPERATOR char(131)
#define HEADER_TEXT_SEPERATOR char(132)
int Huffman::myCompartor::operator()(Node *node1, Node *node2) {
return node1->getFrequency() > node2->getFrequency();
}
void Huffman::huffer(unordered_map<char, int> frequencyMap) {
priority_queue <Node *, vector<Node *>, myCompartor> HufferQueue;
string tempString;
Node *leftNode, *rightNode, *newNode;
for (const auto &item : frequencyMap)
HufferQueue.push(new Node(item.first, item.second));
HufferQueue.push(new Node(PSEUDO_EOF, 1));
while (HufferQueue.size() != 1) {
leftNode = HufferQueue.top();
HufferQueue.pop();
rightNode = HufferQueue.top();
HufferQueue.pop();
newNode = new Node(INTERNAL_NODE_CHARACTER, leftNode->getFrequency() + rightNode->getFrequency());
HufferQueue.push(newNode);
newNode->setLeft(leftNode);
newNode->setRight(rightNode);
}
encodeCharacters(HufferQueue.top(), tempString);
}
void Huffman::encodeCharacters(Node *rootNode, string codeString) {
if (!rootNode)
return;
if (rootNode->getCharacter() != INTERNAL_NODE_CHARACTER) {
codeMap[rootNode->getCharacter()] = codeString;
}
encodeCharacters(rootNode->getLeft(), codeString + "0");
encodeCharacters(rootNode->getRight(), codeString + "1");
}
void Huffman::compressTofile(string InputfileName ,string OutputfileName) {
char character;
string file;
ifstream inputStream;
ofstream outputStream;
outputStream.open(OutputfileName, ios::out);
inputStream.open(InputfileName, ios::in);
writeHeader(outputStream);
while (inputStream.get(character))
file += codeMap[character];
inputStream.close();
file += codeMap[PSEUDO_EOF];
unsigned long remainder = (file.size() - 1) % 8;
for (int i = 0; i < 8 - remainder; ++i)
file += '0';
stringstream stringStream(file);
while (stringStream.good()) {
bitset<8> bits;
stringStream >> bits;
char c = char(bits.to_ulong());
outputStream << c;
}
outputStream.flush();
outputStream.close();
}
void Huffman::writeHeader(ofstream &outputStream) {
for (const auto &item : codeMap)
outputStream << item.first << CHARACTER_CODE_SEPERATOR << item.second << HEADER_ENTRY_SEPERATOR;
outputStream << HEADER_TEXT_SEPERATOR;
}
void Huffman::deHuffer(string compressedFileName, string decompressedFileName) {
char character;
string codeString;
ifstream inputStream;
inputStream.open(compressedFileName, ios::in);
readHeader(inputStream);
while (inputStream.get(character)) {
bitset<8> bits(character);
codeString += bits.to_string();
}
Node *rootNode = buildDecodingTree(codeMap);
decompressToFile(codeString, rootNode, decompressedFileName);
}
void Huffman::readHeader(ifstream &inputStream) {
codeMap.clear();
char character;
inputStream.get(character);
char key = character;
while (character != HEADER_TEXT_SEPERATOR) {
if (character == CHARACTER_CODE_SEPERATOR) {
inputStream.get(character);
while (character != HEADER_ENTRY_SEPERATOR) {
codeMap[key] += character;
inputStream.get(character);
}
} else
key = character;
inputStream.get(character);
}
}
Node *Huffman::buildDecodingTree(unordered_map<char, string> encodingMap) {
Node *rootNode = new Node(INTERNAL_NODE_CHARACTER);
Node *previousNode;
for (const auto &item : encodingMap) {
previousNode = rootNode;
Node *newNode = new Node(item.first);
string characterCode = item.second;
for (int i = 0; i < characterCode.size(); ++i) {
if (characterCode[i] == '0') {
if (i == characterCode.size() - 1)
previousNode->setLeft(newNode);
else {
if (!previousNode->getLeft()) {
previousNode->setLeft(new Node(INTERNAL_NODE_CHARACTER));
previousNode = previousNode->getLeft();
} else previousNode = previousNode->getLeft();
}
} else {
if (i == characterCode.size() - 1)
previousNode->setRight(newNode);
else {
if (!previousNode->getRight()) {
previousNode->setRight(new Node(INTERNAL_NODE_CHARACTER));
previousNode = previousNode->getRight();
} else previousNode = previousNode->getRight();
}
}
}
}
return rootNode;
}
void Huffman::decompressToFile(string codeString, Node *rootNode, string decompressedFileName) {
ofstream outputStream;
outputStream.open(decompressedFileName, ios::out);
Node *traversingPointer = rootNode;
for (int i = 0; i < codeString.size() + 1; ++i) {
if (codeString[i] == '0')
traversingPointer = traversingPointer->getLeft();
else
traversingPointer = traversingPointer->getRight();
if (traversingPointer->getCharacter() != INTERNAL_NODE_CHARACTER) {
if (traversingPointer->getCharacter() == PSEUDO_EOF)
break;
outputStream << traversingPointer->getCharacter();
traversingPointer = rootNode;
}
}
outputStream.flush();
outputStream.close();
}