-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreelib.cpp
More file actions
218 lines (201 loc) · 5.28 KB
/
treelib.cpp
File metadata and controls
218 lines (201 loc) · 5.28 KB
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
#include "treelib.h"
#include <algorithm>
void deleteHelper(Node* n)
{
if (!n)
return;
if (n->l)
deleteHelper(n->l);
if (n->r)
deleteHelper(n->r);
delete n;
}
Tree::Tree() : root(nullptr) {}
Tree::~Tree()
{
deleteHelper(root);
}
//checks if a node is a leaf
bool isleaf(Node* node)
{
return !(node->l && node->r);
}
void Tree::icHelper(Node* n, std::string s)
{
if (isleaf(n))
{
hcodes[n->symbol] = s;
}
if (n->l)
icHelper(n->l, s + "0");
if (n->r)
icHelper(n->r, s + "1");
}
void Tree::initCodes()
{
icHelper(root, "");
}
void Tree::initSymbs()
{
initCodes();
for (auto it = hcodes.begin(); it != hcodes.end(); it++)
{
symbs[it->second] = it->first;
}
}
void Tree::codeslong()
{
codeslong(root);
}
void Tree::codeslong(Node* node)
{
if (isleaf(node))
{
std::cout << "Leaf (value " << char(node->symbol) << ", count " << node->count << ")" << std::endl;
return;
}
std::cout << "Internal node with value " << node->count << " has: ";
if (node->l)
{
std::cout << "left child ";
if (isleaf(node->l))
std::cout << "Leaf (value " << char(node->l->symbol) << ", count " << node->l->count << ")" << std::endl;
else
std::cout << "Internal node with value " << node->l->count << std::endl;
}
if (node->r)
{
std::cout << "right child ";
if (isleaf(node->r))
std::cout << "Leaf (value " << char(node->r->symbol) << ", count " << node->r->count << ")" << std::endl;
else
std::cout << "Internal node with value " << node->r->count << std::endl;
}
if (node->l)
codeslong(node->l);
if (node->r)
codeslong(node->r);
}
//recursive huffman tree generator
Node* treegen(std::priority_queue<Node*, std::vector<Node*>, NodeComp> pqueue)
{
if (pqueue.size() == 1)
{
Node* out = pqueue.top();
pqueue.pop();
return out;
}
Node* le = pqueue.top();
pqueue.pop();
Node* ri = pqueue.top();
pqueue.pop();
Node* i = new Node(le->count + ri->count, 0);
i->l = le;
i->r = ri;
le->p = i;
ri->p = i;
pqueue.push(i);
return treegen(pqueue);
}
void Tree::build(std::vector<std::pair<long unsigned int, unsigned char>>& freqs)
{
//convert all pairs to hcnodes and store in vector
std::vector<Node*> nodes;
for (auto it = freqs.begin(); it != freqs.end(); it++)
nodes.push_back(new Node(it->first, it->second));
//create priority_queue from nodes
std::priority_queue<Node*, std::vector<Node*>, NodeComp> minpqueue(nodes.begin(), nodes.end());
//generate huffman tree
root = treegen(minpqueue);
}
//helper method for serialize
std::vector<short> serializeHelper(Node* cur)
{
std::vector<short> out;
//if current node is null
if (!cur)
{
//push 0x0100 to distinguish from null leaf
//and internal character
out.push_back(0x0100);
}
else
{
//push current node
//if current node is internal, push 0x0200
//to distinguish from null leaf and null node
if (!isleaf(cur))
out.push_back(0x0200);
else
out.push_back(short(cur->symbol));
//call on left
std::vector<short> le = serializeHelper(cur->l);
//call on right
std::vector<short> ri = serializeHelper(cur->r);
//merge l, then r to out
for (auto it = le.begin(); it != le.end(); it++)
out.push_back(*it);
for (auto it = ri.begin(); it != ri.end(); it++)
out.push_back(*it);
}
return out;
}
std::vector<short> Tree::serialize()
{
return serializeHelper(root);
}
void Tree::encode(unsigned char symbol, OStreamWrapper & out)
{
std::string hcode = hcodes[symbol];
for (unsigned int i = 0; i < hcode.length(); i++)
{
char c = hcode[i];
if (c == '1')
out.writeBit(1);
else
out.writeBit(0);
}
}
void Tree::build(std::vector<short>& encNodes)
{
std::vector<short>::iterator begin = encNodes.begin();
root = dcBuildHelper(&begin, encNodes.end());
}
Node* Tree::dcBuildHelper(std::vector<short>::iterator* nit, std::vector<short>::iterator nodesEnd)
{
if (*nit == nodesEnd)
return nullptr;
short cur = *(*nit);
(*nit)++;
if (cur == 0x0100)
return nullptr;
Node* ro = new Node(0, char(cur & 0x00FF));
Node* lchild = dcBuildHelper(nit, nodesEnd);
Node* rchild = dcBuildHelper(nit, nodesEnd);
ro->l = lchild;
ro->r = rchild;
if (lchild)
lchild->p = ro;
if (rchild)
rchild->p = ro;
return ro;
}
void Tree::decode(IStreamWrapper& input, OStreamWrapper& out, unsigned long int filesize)
{
char cur;
long unsigned int numDecoded = 0;
std::string curstring = "";
while (((cur = input.readBit()) != -1) && numDecoded < filesize)
{
//update current string
curstring += cur ? "1" : "0";
//check if it exists in the symbs dictionary
if (symbs.find(curstring) != symbs.end())
{
//if it is, write it to the output file
out.writeByte(char(symbs[curstring] & 0x000000FF));
numDecoded++;
curstring = "";
}
}
}