-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman_code.cpp
468 lines (387 loc) · 11.2 KB
/
huffman_code.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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct node
{
char data;
int frequency;
node* left;
node* right;
};
struct queue_node
{
node* data;
queue_node* next;
};
class queue
{
private:
queue_node* front;
queue_node* rear;
public:
queue()
{
front = NULL;
rear = NULL;
}
~queue()
{
while (!is_empty())
{
dequeue();
}
}
void enqueue(node* n)
{
queue_node* new_node = new queue_node;
new_node->data = n;
new_node->next = NULL;
if (is_empty())
{
front = rear = new_node;
}
else if (!is_empty())
{
rear->next = new_node;
rear = new_node;
}
}
node* dequeue()
{
if (is_empty())
{
return NULL;
}
else if (front->next == NULL)
{
queue_node* temp = front;
node* data = temp->data;
front = rear = NULL;
delete temp;
return data;
}
else
{
queue_node* temp = front;
node* data = temp->data;
front = front->next;
delete temp;
return data;
}
}
bool is_empty() const
{
if (front == NULL)
{
return true;
}
else if (front->next == NULL)
{
return false;
}
else
{
return false;
}
}
queue_node* get_front() const
{
if (front == NULL)
{
return NULL;
}
else if (front->next == NULL)
{
return front;
}
else
{
return front;
}
}
};
class huffman_tree
{
private:
node* root;
std::string codes[256];
node* create_node(char data, int frequency)
{
node* new_node = new node;
new_node->data = data;
new_node->frequency = frequency;
new_node->left = new_node->right = nullptr;
return new_node;
}
void delete_node(node* n)
{
if (n)
{
delete_node(n->left);
delete_node(n->right);
delete n;
}
}
void build_tree(const std::string& input_filename)
{
const int char_range = 256;
int frequencies[char_range] = {0};
ifstream input_file(input_filename);
if (input_file.is_open())
{
char c;
do
{
while (input_file.get(c))
{
frequencies[static_cast<unsigned char>(c)]++;
}
} while (false);
input_file.close();
}
else if (!input_file.is_open())
{
cout << "Unable to open file" << endl;
return;
}
queue q;
int i = 0;
do {
if (frequencies[i] > 0)
{
node* new_node = create_node(static_cast<char>(i), frequencies[i]);
q.enqueue(new_node);
}
++i;
} while (i < char_range);
do {
if (!q.is_empty() && q.get_front()->next != NULL)
{
queue_node* front_node = q.get_front();
node* new_node = create_node('\0', 0);
new_node->left = q.dequeue();
new_node->right = q.dequeue();
new_node->frequency = new_node->left->frequency + new_node->right->frequency;
q.enqueue(new_node);
}
} while (!q.is_empty() && q.get_front()->next != NULL);
root = q.dequeue();
}
void assign_codes(node* n, const std::string& code)
{
if (n)
{
if (n->data != '\0')
{
cout << n->data << ": " << code <<endl;
codes[static_cast<unsigned char>(n->data)] = code;
}
assign_codes(n->left, code + "0");
assign_codes(n->right, code + "1");
}
}
void write_codes_to_file(const std::string& output_filename)
{
ofstream codes_file(output_filename);
if (codes_file.is_open())
{
int i = 0;
do {
if (!codes[i].empty())
{
codes_file << static_cast<char>(i) << " " << codes[i] << endl;
}
++i;
} while (i < 256);
codes_file.close();
}
else
{
cout << "Unable to open codes file" << endl;
}
}
public :
huffman_tree()
{
root = nullptr;
}
~huffman_tree()
{
delete_node(root);
}
node* get_root() const
{
return root;
}
void build_and_assign_codes(const std::string& input_filename, const std::string& codes_filename)
{
build_tree(input_filename);
assign_codes(root, "");
write_codes_to_file(codes_filename);
}
void encode_text(const std::string& input_filename)
{
ifstream input_file(input_filename);
ofstream encoded_file("encoded.txt", ios::binary);
if (input_file.is_open())
{
if (encoded_file.is_open())
{
char c;
do
{
while (input_file.get(c)
) {
encoded_file << codes[static_cast<unsigned char>(c)];
}
input_file.close();
encoded_file.close();
} while (false);
}
else if (!encoded_file.is_open())
{
cout << "error!!! Unable to open encoded file. " << endl;
}
} else {
std::cout << "error!!! Unable to open input file." << endl;
}
}
void read_codes_from_file(const std::string& codes_filename)
{
ifstream codes_file(codes_filename);
bool file_open = codes_file.is_open();
do {
if (!file_open)
{
cout << "Error!!! Unable to open codes file" << endl;
break;
}
char character;
string code;
while (codes_file >> character >> code)
{
codes[static_cast<unsigned char>(character)] = code;
}
} while (false);
codes_file.close();
}
void decode_text(const std::string& input_filename, const std::string& output_filename)
{
read_codes_from_file("codes.txt");
ifstream input_file(input_filename);
ofstream decoded_file(output_filename);
bool files_open = input_file.is_open() && decoded_file.is_open();
do {
if (!files_open)
{
cout << "Unable to open files" << endl;
break;
}
string encoded_text;
input_file >> encoded_text;
string decoded_text;
string current_code;
char bit;
size_t i = 0;
while (i < encoded_text.size())
{
current_code += encoded_text[i];
size_t j = 0;
while (j < 256)
{
if (codes[j] == current_code)
{
decoded_text += static_cast<char>(j);
current_code = "";
break;
}
++j;
}
++i;
}
decoded_file << decoded_text;
cout << "Decoded text: " << decoded_text << endl;
} while (false);
input_file.close();
decoded_file.close();
}
void decode_text_and_show(const string& input_filename, const string& output_filename)
{
read_codes_from_file("codes.txt");
ifstream input_file(input_filename);
ofstream decoded_file(output_filename);
if (input_file.is_open() && decoded_file.is_open())
{
string encoded_text;
input_file >> encoded_text;
string decoded_text;
string current_code;
cout << "Code\t = \tLetter" << endl;
char bit;
size_t i = 0;
do {
current_code += encoded_text[i];
size_t j = 0;
while (j < 256)
{
if (codes[j] == current_code)
{
decoded_text += static_cast<char>(j);
cout << current_code << "\t\t" << static_cast<char>(j) << std::endl;
current_code = "";
break;
}
++j;
}
++i;
} while (i < encoded_text.size());
decoded_file << decoded_text;
cout << "\nDecoded text: " << decoded_text << endl;
input_file.close();
decoded_file.close();
}
else
{
cout << "Unable to open files" << std::endl;
}
}
void draw_tree(node* n, int depth = 0, char branch = ' ')
{
const int indentation = 4;
if (n)
{
cout << std::setw(indentation * depth) << branch << "[" << n->data;
if (n->data != '\0')
{
cout << " (" << codes[static_cast<unsigned char>(n->data)] << ")";
}
cout << "]" << std::endl;
draw_tree(n->left, depth + 1, ' ');
draw_tree(n->right, depth + 1, ' ');
}
}
};
int main() {
cout << "*****------------------------------*****" << endl;
cout << " Huffman Coding " << endl;
cout << "*****------------------------------*****" << endl;
huffman_tree ht;
// Sender program
ht.build_and_assign_codes("input.txt", "codes.txt");
cout << "----------------------------------------" << endl;
// Draw the Huffman tree
cout << "Huffman Tree:" << endl;
ht.draw_tree(ht.get_root());
cout << endl;
cout << "-------------------------------------------------" << endl;
cout << " Encoded file has been generated successfully!!" << endl;
cout << "-------------------------------------------------" << endl;
ht.encode_text("input.txt");
// Receiver program
cout << "\nDecoding..." << endl;
cout << "----------------------------------------" << endl;
ht.decode_text_and_show("encoded.txt", "decoded.txt");
return 0;
}