-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuffmanNode.cpp
204 lines (172 loc) · 5.36 KB
/
HuffmanNode.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
#include "HuffmanNode.h"
#include <iostream>
#include <memory>
#include <string>
#include <sstream>
#include <fstream>
#include <typeinfo>
#include <ctype.h>
#include <tr1/unordered_map>
using namespace std::tr1;
using namespace std;
HuffmanNode::HuffmanNode(){
//default constructor
}
//Copy constructor
HuffmanNode::HuffmanNode(const shared_ptr<HuffmanNode> &other) {
key = other->key;
value = other->value;
if(other->hasLeft() && !other->hasRight()){
left = other->left;
}
if(other->hasRight() && !other->hasLeft()){
right = other->right;
}
if(other->hasRight() && other->hasLeft()){
right = other->right;
left = other-> left;
}
}
//move constructor
HuffmanNode::HuffmanNode(shared_ptr<HuffmanNode>&& other){
key = other->key;
value = other->value;
if(other->hasLeft() && !other->hasRight()){
left = other->left;
}
if(other->hasRight() && !other->hasLeft()){
right = other->right;
}
if(other->hasRight() && other->hasLeft()){
right = other->right;
left = other-> left;
}
other->key = '\0';
other->value = 0;
other->right = NULL;
other-> left = NULL;
}
//an assignment operator
HuffmanNode& HuffmanNode::operator=(const shared_ptr<HuffmanNode> & other)
{
key = other->key;
value = other->value;
if(other->hasLeft() && !other->hasRight()){
left = other->left;
}
if(other->hasRight() && !other->hasLeft()){
right = other->right;
}
if(other->hasRight() && other->hasLeft()){
right = other->right;
left = other-> left;
}
return *this;
}
//constructor that creates a HuffmanNode
HuffmanNode::HuffmanNode(char key, int value){
key = key;
value = value;
}
HuffmanNode::HuffmanNode(shared_ptr<HuffmanNode> &node1, shared_ptr<HuffmanNode> &node2){
left = node1;
right = node2;
}
//A method that sets the left child of some HuffmanNode
void HuffmanNode::setLeft(shared_ptr<HuffmanNode> &tree){
left = tree;
}
//A method that sets the right child of some HuffmanNode
void HuffmanNode::setRight(shared_ptr<HuffmanNode> &tree){
right = tree;
}
//A method that sets the key (character) of a HuffmanNode
void HuffmanNode::setKey(char k){
key = k;
}
//A method that sets the value (number of occurances in the text file) of a HuffmanNode
void HuffmanNode::setValue(int val){
value = val;
}
//returns the key of HuffmanNode
char HuffmanNode::getKey(){
return key;
}
//returns the frequency of a key of a HuffmanNode
int HuffmanNode::getValue(){
return value;
}
//Checks if a HuffmanNode has a left child
bool HuffmanNode::hasLeft(){
return left!=NULL;
}
//Checks if a HuffmanNode has a right child
bool HuffmanNode::hasRight(){
return right!=NULL;
}
//returns the left child of the HuffmanNode
shared_ptr<HuffmanNode> HuffmanNode::getLeft(){
return left;
}
//returns the right child of the HuffmanNode
shared_ptr<HuffmanNode> HuffmanNode::getRight(){
return right;
}
/*The method traverses down the tree (in order). It returns the character and its corresponding bitstream.
codeTable (unordered_map) is passed in to store the character and its corresponding bitstream.
*/
void HuffmanNode::traverse(string bitstream, std::unordered_map< char ,string >&codeTable, string &outputFile){
if(left == NULL && right == NULL){
tableWriter(key, bitstream, outputFile); //Append the character and its bitstream to the output file
codeTable.insert({key,bitstream}); //insert the character and its bitstream into the codeTable
}
if(left!= NULL){
left->traverse(bitstream + "0", codeTable, outputFile);
}
if(right!= NULL){
right->traverse(bitstream+"1", codeTable, outputFile);
}
}
//Writes the key(character) and its bitstream into the output file
void HuffmanNode::tableWriter(char key, string bitstream, string &outputFile){
std::ofstream fileobject;
fileobject.open(outputFile+".txt", std::ofstream::out | std::ofstream::app);
if(key == '\n'){ //newline character proved to be a special case
fileobject<< "\\n"<< " "<< bitstream <<endl;
}else{
fileobject<<key<<" = "<<bitstream<<endl;}
fileobject.close();
}
//For te sake of writing the number of different characters int the text file provided
void HuffmanNode::setSize(int size, string &outputFile){
std::ofstream fileobject;
fileobject.open(outputFile+".txt", std::ofstream::out | std::ofstream::app);
fileobject <<"Number of different characters: "<<size<<endl;
fileobject.close();
}
/*Reads the input file, takes each and every character (in the order they were written) and stores them into a vector
"characters"
*/
void HuffmanNode::readCharacters(string filename){
char ch;
fstream object(filename+".txt", fstream::in);
while (object >> noskipws >> ch){
characters.push_back(ch);
}
}
/*codeTable has a character and bitstream. The characters vector has all characters in order.
Find the corresponding bitstream for each character and write into a file.*/
void HuffmanNode::writeHeaderFile(std::unordered_map< char ,string > codeTable, string &outputFile){
std::ofstream fileobject;
fileobject.open(outputFile+".hdr", std::ofstream::out | std::ofstream::app);
std::unordered_map<char, string>::iterator it;
for(int i = 0; i < characters.size(); i++){
it = codeTable.find(characters[i]);
if(it != codeTable.end()){
fileobject<<it->second;
}
}
fileobject.close();
}
HuffmanNode::~HuffmanNode() {
}