-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
379 lines (312 loc) · 8.16 KB
/
main.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
#include <iostream>
#include <string>
#include <queue>
#include <exception>
#include <fstream>
#include <vector>
using namespace std;
static string st[256];
static int freq[256];
class Node;
class Huffman;
class Huffman
{
public:
static string compress(string message);
static string expand(string bit_input, Node *root);
};
class Node
{
public:
char ch; // usado só nas folhas
int freq; // usado só para construção da trie
Node *left, *right;
Node(char ch, int freq) {
this->ch = ch;
this->freq = freq;
this->left = nullptr;
this->right = nullptr;
}
Node(char ch, int freq, Node *left, Node *right) {
this->ch = ch;
this->freq = freq;
this->left = left;
this->right = right;
}
Node()
{
this->ch = '\0';
this->freq = 0;
this->left = nullptr;
this->right = nullptr;
}
bool isLeaf() {
return (left == nullptr && right == nullptr);
}
// int compareTo(Node that) {
// return this.freq - that.freq;
// }
};
struct comp
{
bool operator()(Node* l, Node* r)
{
// highest priority item has lowest frequency
return l->freq > r->freq;
}
};
void buildFreqTable(string message);
Node* buildTree(string message);
void buildCode(Node *x, string s);
Node* getNode(char ch, int freq, Node* left, Node* right);
void print2Leaf(const Node * cur);
string writeTree(Node *x, string s);
Node *readTree(string input, int& pos);
void print2Leaf_ch(const Node * cur);
bool writeMessage(string text, string fileName);
bool readMessage(string &message, string fileName);
bool putEncodedMessage(string message, string writtenTree, string fileName);
bool getEncodedMessage(string &message, string &writtenTree, string fileName);
int main()
{
//=====================================================
// DECODIFICATION
//=====================================================
string message = "";
readMessage(message, "baseMessage.txt");
Node* root = buildTree(message);
string writtenTree;
buildCode(root, "");
string codified_message = Huffman::compress(message);
writtenTree = writeTree(root, "");
putEncodedMessage(codified_message, writtenTree, "encodedMessage.txt");
//=====================================================
// DECODIFICATION
//=====================================================
string enc_writtenTree="";
string enc_codified_message="";
getEncodedMessage(enc_codified_message, enc_writtenTree, "encodedMessage.txt");
int pos = 0;
Node *root_dec = readTree(enc_writtenTree, pos);
string decodified_message = Huffman::expand(enc_codified_message, root_dec);
writeMessage(decodified_message, "decodifiedMessage.txt");
//=====================================================
// COMPARISON
//=====================================================
cout <<endl<< "ORIGINAL SIZE: " << 8*message.length() << endl;
cout << "COMPRESSED SIZE: " << codified_message.length() << endl;
cout << "DECOMPRESSED SIZE: " << 8*decodified_message.length() << endl;
cout << "COMPRESSIBILITY: -" << 100*(8*message.length() - codified_message.length())/(8.0*message.length())<< "%" << endl;
return 0;
}
bool putEncodedMessage(string message, string writtenTree, string fileName)
{
ofstream filestr;
filestr.open(fileName, ios::binary);
if(!filestr.is_open())
{
return false;
}
filestr << writtenTree << "\n" << message;
filestr.close();
return true;
}
bool getEncodedMessage(string &message, string &writtenTree, string fileName)
{
ifstream filestr;
filestr.open(fileName, ios::in);
if(!filestr.is_open())
{
return false;
}
while(!filestr.eof())
{
string line;
getline(filestr, line, '\n');
if(!filestr.eof())
{
writtenTree+=line;
writtenTree+="\n";
}
else
{
message+=line;
}
}
filestr.close();
return true;
}
bool writeMessage(string text, string fileName)
{
ofstream filestr;
filestr.open(fileName, ios::out);
if(!filestr.is_open())
{
return false;
}
filestr << text;
filestr.close();
return true;
}
bool readMessage(string &message, string fileName)
{
ifstream filestr;
filestr.open(fileName, ios::in);
if(!filestr.is_open())
{
return false;
}
while(!filestr.eof())
{
string line;
getline(filestr, line, '\n');
message+=line;
message+="\n";
}
filestr.close();
return true;
}
string writeTree(Node *x, string s)
{
//cout << s << endl;
if(x->isLeaf())
{
s+='1';
s+=x->ch;
return s;
}
s+='0';
s = writeTree(x->left, s);
s = writeTree(x->right, s);
return s;
}
Node *readTree(string input, int &pos)
{
if(pos == input.length())
{
cout << "Deu maior!" << endl;
return nullptr;
}
//cout << input[pos];
if (input[pos] == '1')
{
char c = input[++pos];
//cout << c;
pos++;
return new Node(c, 1, nullptr, nullptr);
}
pos++;
return new Node('\0', 0, readTree(input, pos), readTree(input, pos));
}
void buildCode(Node *x, string s)
{
if(x->isLeaf())
{
st[x->ch] = s;
//cout << x->ch << ": " << s <<endl;
return;
}
buildCode(x->left, s+'0');
buildCode(x->right, s+'1');
}
Node* getNode(char ch, int freq, Node* left, Node* right)
{
Node* node = new Node();
node->ch = ch;
node->freq = freq;
node->left = left;
node->right = right;
return node;
}
Node* buildTree(string message)
{
buildFreqTable(message);
priority_queue<Node*, vector<Node*>, comp> collection;
for (int i = 0; i< 256; i++) {
if(freq[i]>0)
{
collection.push(getNode(i, freq[i], nullptr, nullptr));
}
}
while (collection.size() > 1)
{
Node *left = collection.top();
collection.pop();
Node *right = collection.top();
collection.pop();
int sum = left->freq + right->freq;
collection.push(getNode('\0', sum, left, right));
}
Node *root = collection.top();
// cout <<"CODE TREE" << endl;
// print2Leaf(root);
// cout << endl;
return root;
}
void print2Leaf(const Node * cur)
{
if(!( cur->left ==nullptr && cur->right == nullptr))
{
cout << "(" << cur->freq << ")"<< " -> (" << cur->left->ch << ") ("<< cur->right->ch << ")"<< endl;
print2Leaf(cur->left);
print2Leaf(cur->right);
}
}
void print2Leaf_ch(const Node * cur)
{
if(!( cur->left ==nullptr && cur->right == nullptr))
{
cout << "(" << cur->freq << ")"<< " -> (" << cur->left->ch << ") ("<< cur->right->ch << ")"<< endl;
print2Leaf(cur->left);
print2Leaf(cur->right);
}
}
void buildFreqTable(string message)
{
for(int i=0; i<256; i++)
{
freq[i] = 0;
}
for(int j = 0; j<message.length(); j++)
{
freq[message[j]]++;
}
// cout << "FREQ TABLE"<< endl;
// for(int i=0; i<256; i++)
// {
// if(freq[i]>0) cout << (char)i << ": " << freq[i] << endl;
// }
// cout << endl;
}
string Huffman::compress(string message)
{
string bit_output = "";
const int size = message.length();
char input[size];
for(int i=0; i<size; i++) input[i] = message[i];
for(int i = 0; i < size; i++)
{
string code = st[input[i]];
bit_output.append(code);
}
return bit_output;
}
string Huffman::expand(string bit_input, Node *root)
{
string output ="";
int j = 0;
Node* x = root;
while(j <bit_input.length())
{
x = root;
while(!x->isLeaf())
{
if(bit_input[j] == '1') x = x->right;
else x = x->left;
++j;
}
//cout << x->ch;
output+={x->ch};
}
return output;
}