-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffman.cpp
More file actions
374 lines (305 loc) · 9.67 KB
/
Huffman.cpp
File metadata and controls
374 lines (305 loc) · 9.67 KB
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
#include "Huffman.h"
Huffman::Huffman()
{
text_to_encode = "";
text_to_decode = "";
compressed_text = "";
file_storage = new Storage();
}
unordered_map<char, int> Huffman::getFrequency(string file_to_compress)
{
//Creating a map data type to get frequency
unordered_map<char, int> frequency_map;
//Opening the file
fstream file_to_open;
file_to_open.open(file_to_compress);
if(file_to_open.fail())
{
exit(7);
}
string text_line = "";
//Using a while loop to get each line of the file
while(getline(file_to_open, text_line))
{
for(int i = 0; i < text_line.length(); i++)
{
char character = text_line[i];
//Checks if the character is already in the map
if(frequency_map.find(character) != frequency_map.end())
{
frequency_map[character] += 1;
}
//Else we add it to frequency map with a value of 1
else
{
frequency_map[character] = 1;
}
}
}
//Adding the extra character stoppers
//so there are no unnecessary characters at the end
char extra_character_stopper = 3;
frequency_map[extra_character_stopper] = 1;
char new_line = 10;
frequency_map[new_line] = 1;
file_to_open.close();
return frequency_map;
}
Node* Huffman::buildTree(string file_to_compress)
{
//Getting the frequency map
unordered_map<char, int> frequency_map = getFrequency(file_to_compress);
//Making a queue of nodes of the characters
priority_queue<Node*, vector<Node*>, compareWeights> *nodes = new priority_queue<Node*, vector<Node*>, compareWeights>;
//Iterate through the map and putting it into the queue
for(const auto& node : frequency_map)
{
nodes->push(new Node(node.first,node.second));
}
//Queueing the nodes
while(nodes->size() > 1)
{
//Get the left and right node
Node *left = nodes->top();
nodes->pop();
Node *right = nodes->top();
nodes->pop();
//Creating a new combined node
nodes->push(new Node('\0', left->weight + right->weight, left, right));
}
return nodes->top();
}
Node* Huffman::rebuildTree(string tree_to_rebuild)
{
stringstream ss;
ss << tree_to_rebuild;
string current_cipher = "";
char separator = 30;
Node *rebuilt_huffman_tree = new Node('\0', 0, nullptr, nullptr);
Node *current = rebuilt_huffman_tree;
//Using a while loop reading up until the separator
while(getline(ss, current_cipher, separator))
{
//The first character is always the needed character
char current_character = current_cipher[0];
for(int i = 1; i < current_cipher.size(); i++)
{
if(current_cipher[i] == '1')
{
//If the node doesn't exist, we make one
if(current->one == nullptr)
{
current->one = new Node('\0', 0, nullptr, nullptr);
}
current = current->one;
}
else
{
//If the node doesn't exist, we make one
if(current->zero == nullptr)
{
current->zero = new Node('\0', 0, nullptr, nullptr);
}
current = current->zero;
}
}
//Setting the current letter to the character and resetting the tree
current->letter = current_character;
current = rebuilt_huffman_tree;
}
return rebuilt_huffman_tree;
}
void Huffman::compressGetCodes(Node *root, string code, unordered_map<char, string> &codes)
{
//If we reached a leaf, we set the letter to the code
if(root->zero == nullptr && root->one == nullptr)
{
codes[root->letter] = code;
}
//Else we recursively call
else
{
compressGetCodes(root->zero, code + "0", codes);
compressGetCodes(root->one, code + "1", codes);
}
}
void Huffman::compress(string file_to_compress, string output_file)
{
//Build the tree with the file name
Node *cipher = buildTree(file_to_compress);
//Getting the codes to encode the tree
unordered_map<char, string> codes;
compressGetCodes(cipher, "", codes);
string header = "";
char separator = 30;
//Creating the header with the codes and separator
for(const auto& code : codes)
{
header += code.first + code.second + separator;
}
//Opening the file to write into it
file_storage->open(output_file, "write");
file_storage->setHeader(header);
//Opening the file to read
fstream file_to_open;
file_to_open.open(file_to_compress);
if(file_to_open.fail())
{
exit(75);
}
string text_line = "";
string line_to_input = "";
char new_line = 10;
//Reading in the file to encode the text
while(getline(file_to_open, text_line))
{
//Checks if the line is empty
if(text_line == "")
{
file_storage->insert(codes[new_line]);
}
else
{
//Adding the character's code to the line to input
//and a new line every time the line is done
for (int i = 0; i < text_line.size(); i++) {
line_to_input += codes[text_line[i]];
}
file_storage->insert(line_to_input);
file_storage->insert(codes[new_line]);
line_to_input = "";
}
}
//Adding the extra character separator at the end
char extra_character_separator = 3;
file_storage->insert(codes[extra_character_separator]);
file_storage->close();
}
void Huffman::decompress(string file_to_decompress, string output_file)
{
//Opening the file to read in the header
file_storage->open(file_to_decompress, "read");
string header = file_storage->getHeader();
//Rebuilding the tree
Node *cipher = rebuildTree(header);
//Opening the file to write
fstream file_to_output;
file_to_output.open(output_file);
compressed_key = cipher;
string text_line = "";
string character_code = "";
char character_code_ender = 3;
//While loop to extract from the file
while(file_storage->extract(text_line))
{
string char_to_input = "";
//For loop to look through the text line
for(int i = 0; i < text_line.size(); i++)
{
//If we reached a leaf
if(compressed_key->zero == nullptr && compressed_key->one == nullptr)
{
//If the leaf is the code ender, we break
if(compressed_key->letter == character_code_ender)
{
break;
}
//Else we add the character to the character to input
char_to_input += compressed_key->letter;
compressed_key = cipher;
}
//If the character is zero, we go to the node's zero
if(text_line[i] == '0')
{
compressed_key = compressed_key->zero;
}
//If the character is zero, we go to the node's one
if(text_line[i] == '1')
{
compressed_key = compressed_key->one;
}
}
//Writing the character to the file
file_to_output << char_to_input;
}
file_to_output.close();
}
//Driver functions
void Huffman::setText(const string text_line)
{
this->text_to_encode = text_line;
}
void Huffman::setEncodedText(const string text_line)
{
this->text_to_decode = text_line;
}
string Huffman::encode()
{
map<char, int> frequency_map;
for(int i = 0; i < text_to_encode.length(); i++)
{
char character = text_to_encode[i];
//Checks if the character is already in the map
if(frequency_map.find(character) != frequency_map.end())
{
frequency_map[character] += 1;
}
//Else we add it to frequency map with a value of 1
else
{
frequency_map[character] = 1;
}
}
//Making a queue of nodes of the characters
queue<Node*> *nodes = new queue<Node*>;
//Iterate through the map and putting it into the queue
for(const auto& node : frequency_map)
{
nodes->push(new Node(node.first,node.second));
}
//Queueing the nodes
while(nodes->size() > 1)
{
//Get the left and right node
Node *left = nodes->front();
nodes->pop();
Node *right = nodes->front();
nodes->pop();
//Creating a new combined node
int frequency_total = left->weight + right->weight;
Node *new_node = new Node('\0', frequency_total, left, right);
nodes->push(new_node);
}
compressed_key = nodes->front();
unordered_map<char, string> codes;
compressGetCodes(compressed_key, "", codes);
//Get the compressed result
string compressed = "";
for(int i = 0; i < text_to_encode.size(); i++)
{
compressed_text += codes[text_to_encode[i]];
}
return compressed_text;
}
string Huffman::decode()
{
Node *decompressed_node = compressed_key;
string decompressed_result = "";
for(int i = 0; i < compressed_text.size(); i++)
{
if(compressed_text[i] == '0')
{
decompressed_node = decompressed_node->zero;
}
else if(compressed_text[i] == '1')
{
decompressed_node = decompressed_node->one;
}
if(decompressed_node->zero == nullptr && decompressed_node->one == nullptr)
{
decompressed_result += decompressed_node->letter;
decompressed_node = compressed_key;
}
}
return decompressed_result;
}