-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
351 lines (321 loc) · 10.8 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
#include <iostream>
#include <map>
#include <queue>
#include <fstream>
#include <algorithm>
class huffman_tree {
private:
/*
* NODE
* ----
* Classification:
* Project spec.
*
* Description:
* in order to create the huffman tree it was needed to store our huffman code into
* a node that could be utilized when traversing the tree to find the appropriate
* huffman encoding of a charaacter. Within the node we define the frequency and
* char value of the node. A frequecny node for example is set to '-1' to avoid
* any errors when creating the huffman tree.
*/
struct node{
char c;
int f;
node* l = NULL;
node* r = NULL;
bool parent = true;
//Utilized when creating an empty node
node(){
}
//utilized when creating a huffman code containg a char and frequency
node(char c, int f){
this -> c = c;
this -> f = f;
parent = false;
}
//Utilized when creating a huffman code describing just a frequency
node(int f){
this -> f = f;
this -> c = '-1';
}
};
// The root belongs to the huffman tree class and cannot be accessed outside the tree
node* root;
//Creates the root of the huffman tree
huffman_tree(node* n){
this -> root = n;
}
public:
/*
Preconditions: input is a string of characters with ascii values 0-127
Postconditions: Reads the characters of input and constructs a
huffman tree based on the character frequencies of the file contents
*/
/*
* HUFFMAN_TREE
* ------------
* Classification:
* Project spec.
*
* Description:
* This method process both passed through strings and files by specifying
* a file starting the string with "/". Additionally to avoid opening a
* file when not wanted we check the size of the string to ensure the user
* is not just passing "/". After it is verified the type of data being
* passed we iterate through a couple loops using a sorted map to place
* each node in the queue and create the appropriate huffman tree.
*/
huffman_tree(const std::string& input) {
std::priority_queue<std::pair<int,node*>> q;
std::map<char,int> mappy;
if(input[0] == '/' && input.size() > 1){
try{
std::string file = input;
file.erase(0,1);
std::ifstream infile(file,std::ifstream::in);
char c = infile.get();
while(infile.good()){
if(mappy.find(c) != mappy.end()){
mappy.find(c) -> second += 1;
c = infile.get();
}else{
mappy[c] = 1;
c = infile.get();
}
}
for(auto it : mappy){
node* n = new node(it.first, it.second);
std::pair<int,node*> p;
p.first = it.second * -1;
p.second = n;
q.push(p);
}
while(q.size() != 1){
node* n = new node;
std::pair<int, node*> p;
n -> l = q.top().second;
q.pop();
n -> r = q.top().second;
q.pop();
n -> f = n -> l -> f + n -> r -> f;
p.first= n->f * -1;
p.second=n;
q.push(p);
}
if(mappy.size() == 1){
root = new node(1);
root -> l = q.top().second;
}else{
this -> root = q.top().second;
}
}catch(std::string e){
}
}else{
for(auto c : input){
if(mappy.find(c) != mappy.end()){
mappy.find(c) -> second += 1;
}else{
mappy[c] = 1;
}
}
for(auto it : mappy){
node* n = new node(it.first, it.second);
std::pair<int,node*> p;
p.first = it.second * -1;
p.second = n;
q.push(p);
}
while(q.size() != 1){
node* n = new node;
std::pair<int, node*> p;
n -> l = q.top().second;
q.pop();
n -> r = q.top().second;
q.pop();
n -> f = n -> l -> f + n -> r -> f;
p.first= n->f * -1;
p.second=n;
q.push(p);
}
if(mappy.size() == 1){
root = new node(1);
root -> l = q.top().second;
}else{
this -> root = q.top().second;
}
}
}
/*
Preconditions: input is a character with ascii value between 0-127
Postconditions: Returns the Huffman code for character if character is in the tree
and an empty string otherwise.
*/
/*
* SEARCH
* ------
* Classification:
* Helper method
*
* Description:
* This search method is used to return the huffman code of a given character, this is
* done recursively through this method. When it encounters and issue traversing one
* side of the tree it runs through the other side of the tree. That way the appropriate
* ascii code is utilized to define a char.
*/
std::string search(std::string code, node* n, char character) const{
std::string oCode = code;
if(n == NULL){
return "error";
} else{
if(n -> parent){
code.append("0");
code = search(code,n -> l,character);
if(code == "error"){
code = oCode;
code.append("1");
code = search(code, n -> r, character);
}
if(code == "error"){
return "error";
} else{
return code;
}
}
if (tolower(n -> c) == character) {
return code;
} else{
if(character < n -> c ){
code.append("0");
return search(code, n -> l, character);
} else{
code.append("1");
return search(code, n -> r, character);
}
}
}
}
/*
* GET_CHARACTER_CODE
* --------------------
* Classification:
* helper method
*
* DESCRIPTION:
* This class is a helper method used by encode to encode the appropriate
* huffman code to the chracter. This is done recursively through search
* for incresed performance eficiency.
*/
std::string get_character_code(char character) const {
character = tolower(character);
std::string code = "";
if(root == NULL){
return code;
}else{
code.append("0");
code = search(code,root -> l,character);
if(code == "error"){
code = "";
code.append("1");
code = search(code, root -> r, character);
}
if(code == "error"){
return "";
} else{
return code;
}
}
}
/*
Preconditions: input is a string of characters with ascii values 0-127
Postconditions: Returns the Huffman encoding for the contents of file_name
if file name exists and an empty string otherwise.
If the file contains letters not present in the huffman_tree,
return an empty string
*/
/*
* ENCODE
* --------
* Classification:
* Project spec.
*
* Description:
* This is part of the project spec its primary function is to encode the
* string passed to it or file. This done iteratively through a helper
* method get_character_code and its recursive helper method search.
*/
std::string encode(const std::string& input) const {
std::string result = "null";
if(input[0] == '/' && input.size() > 1){
try{
std::string file = input;
file.erase(0,1);
std::ifstream infile(file,std::ifstream::in);
char c = infile.get();
while(infile.good() && result != ""){
if(result == ""){
return result;
}else if(result == "null"){
result = "";
result += get_character_code(c);
}else{
result += get_character_code(c);
}
c = infile.get();
}
}catch(std::string e){
return"";
}
} else{
for(char c : input){
if(result == ""){
return result;
}else if(result == "null"){
result = "";
result += get_character_code(c);
}else{
result += get_character_code(c);
}
}
}
return result;
}
/*
Preconditions: string_to_decode is a string containing Huffman-encoded text
Postconditions: Returns the plaintext represented by the string if the string
is a valid Huffman encoding and an empty string otherwise
*/
/*
* DECODE
* ------
* Clasification:
* Project spec.
*
* Description:
* This method iteratively searches through the huffman tree, once it
* reaches a null chracter it returns the char character of the node.
* this allows the user to decode the message accurately after encoding.
*/
std::string decode(const std::string& string_to_decode) const {
std::string message = "";
node* n = root;
for(char c : string_to_decode){
if(c == '0' || c == '1'){
if(c == '0'){
n = n -> l;
if(n -> l == NULL || n -> r == NULL){
message += n -> c;
n = root;
}
}
if(c == '1'){
n = n -> r;
if(n -> l == NULL || n -> r == NULL){
message += n -> c;
n = root;
}
}
}
}
return message;
}
};