-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffmanCoding.cpp
304 lines (203 loc) · 8.26 KB
/
huffmanCoding.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
#include "huffman.h"
#include <iostream>
#include <sstream>
//QUESTION 1 - 0 credit
HuffmanTree::HuffmanHeap::HuffmanHeap(istream &instr) {
// start with an empty map
map<string, unsigned int> associate;
// ----
// use a the input stream with a string to read every word one by one
string g;
// read every word one by one
while(instr >> g) {
// if the word was already seen, increment the count. Otherwise, initialized it at 1
if (associate.find(g) != associate.end()) {
associate[g]++;
}else {
associate[g] = 1;
}
}
// insert every word in the heap
for(auto &ada : associate) {
auto tn = new TreeNode(ada.first);
this->push(tn, ada.second);
}
}
void HuffmanTree::HuffmanHeap::pop() {
if(content.size() <= 1) return;
auto newpriority = content[0]->priority;
auto tn1 = *(content[0]->data);
Heap::pop();
newpriority += content[0]->priority;
auto tn2 = *(content[0]->data);
Heap::pop();
auto tn = new TreeNode(tn1, tn2);
Heap::push(tn, newpriority);
}
// QUESTION 2
void HuffmanTree::dfs(HuffmanCode& hc, TreeNode *tn, string &s) {
//Map to keep track of visited
map<TreeNode*, int> vm; //VISITED MAP
string runningCode = s;
vm.insert(pair<TreeNode*, int>(tn, 1)); //Mark node as visited by adding it to the visitedmap
//cout << "DEBUG: (printing vm[tn]): " << vm[tn] << endl;
if(tn->word != nullptr){ //Check if leaf
hc.insert(pair<string, string>(*(tn->word),runningCode)); //Associate code
}
//-----DFS Recursion
for(auto i = 0; i <= 1; ++i){
if(tn->children[i] != nullptr){
if(vm[tn->children[i]] != 1){
runningCode = s + to_string(i); //append to string
dfs(hc, tn->children[i], runningCode); //recursive step
if(runningCode.size() > 0){ //erase last character of string
runningCode.resize(runningCode.size() - 1);
}
}
}
}
}
//QUESTION 3
HuffmanCode::HuffmanCode(istream &input) {
string getlineStr;
string inputWord;
string inputHuffCode;
string oneWord;
while(getline(input, getlineStr)){
//string into stringstream
istringstream stream1(getlineStr);
vector<string> vc;
unsigned int count = 1;
while(stream1 >> oneWord){
//-----input validation-------
//input validation (# of params)
vc.push_back(oneWord); //add to vector
if(count > 2){
cout << "Error 0: Too many params!" << endl;
throw 0; //exception too many params
}
count++;
}
//check HuffmanCoding formatting
string checkformat = vc.at(1); //Huffman code
for(auto j = 0; j < checkformat.length(); ++j){
if(checkformat[j] != '0' && checkformat[j] != '1'){
cout << "Error 0: Incorrect HuffmanCode format - something is not a 0 or a 1." << endl;
throw 0; //exception: formatting
}
}
//-----end input validation----
this->insert(pair<string, string>(vc.at(0),vc.at(1)));
}
}
//QUESTION 4
HuffmanTree::HuffmanTree(const HuffmanCode &hc) {
//example entry: <hello, 110>
HuffmanCode hcmap = hc; //Copy input map just in case
this->root = new TreeNode; //initialize root
//strategy: build entire tree first, and then go through and add shit.
for(auto entry : hcmap) {
resetIterator();
//cout << "debug hcmap contents: " << entry.first << entry.second << endl;
//cout << "second entry length: " << entry.second.length() << endl;
for(int i = 0; i < entry.second.length(); i++){
if(entry.second[i] == '1'){ //Huffcode bit is 1
if(iter->children[1] == nullptr){ //if there does not exist a child
if(i == entry.second.length() - 1){ //if exhausted code
if(iter->children[1] != nullptr){
cout << "Error 1: Is not a prefix code." << endl;
throw 1;
} else {
iter->children[1] = new TreeNode(entry.first);
}
} else {
TreeNode* node = new TreeNode(); //then create a tree node as child
iter->children[1] = node;
moveDownOnOne();
}
} else moveDownOnOne();
}
if(entry.second[i] == '0'){ //Huffcode bit is 0
if(iter->children[0] == nullptr){ //if there does not exist a child
if(i == entry.second.length() - 1){ //if exhausted code
if(iter->children[0] != nullptr){
cout << "Error 1: Is not a prefix code." << endl;
throw 1;
} else {
iter->children[0] = new TreeNode(entry.first);
}
} else {
TreeNode* node = new TreeNode(); //then create a tree node as child
iter->children[0] = node;
moveDownOnZero();
}
} else moveDownOnZero();
}
if(entry.second[i] != '0' && entry.second[i] != '1'){ //Huffcode bit is INVALID (i.e., not 1 or 0)
cout << "Error 2: A Huffman Code is invalid: contains x that is not 0 or 1" << endl;
throw 2;
}
}
//iter->children[i]= new TreeNode(entry.first);
//cout << entry.first << "%%" << entry.second << endl;
}
}
// QUESTION 5
void HuffmanEncoder::encode(istream &fin, ostream &fout) const {
string dict;
string ss;
while(getline(fin, ss)){
dict = dict + ss;
}
//cout << "DEBUG string: " << dict << endl;
istringstream stream1(dict);
HuffmanTree newTree(stream1); //Make Huffman tree out of input stream
HuffmanCode hc; //Map of codes
hc = newTree.getCode();
string word;
/* for(auto entry : hc) {
cout << "DEBUG: " << entry.first << " " << entry.second << endl;
} */
istringstream stream2(dict);
while(stream2 >> word){
if(hc.count(word) != 1){
cout << "Error 1: Word from istream not contained in dictionary.";
throw 1;
}
fout << hc[word];
}
}
//QUESTION 5
void HuffmanDecoder::push(istream &f) {
string str; //Store stream
HuffmanCode hc = this->getCode(); //Get Huffmancode Map
getline(f, str);
int position = 0;
int substrlen = 1;
int found = 0; //For error throw
//Sequence Validation
for (int i = 0; i < str.length(); i++) {
if (!(str[i] == '0') && !(str[i] == '1')) {
cout << "Error 0: Invalid sequence: contains an x that is neither 0 nor 1." << endl;
throw 0; //Error: Invalid sequence
}
}
while (position + substrlen <= str.length()) {
string segment = str.substr(position, substrlen);
map<string, string>::iterator it; //initialize map iterators (old school)
for (it = hc.begin(); it != hc.end(); ++it) {
if (it->second == segment) {
savedWords.push(&(it->first)); //Found match, push the first
position += substrlen; //Move cursor by length
substrlen = 0;
found++; //Up the found
break;
}
}
substrlen++;
}
if(found == 0){
cout << "Error 0: Word not fully completed." << endl;
throw 1; //Word not fully completed error.
}
}