-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.cpp
286 lines (246 loc) · 7.44 KB
/
PriorityQueue.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
#ifndef PriorityQueue_cpp
#define PriorityQueue_cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <utility>
#include <vector>
typedef std::pair<unsigned char, std::string> HuffmanCode;
struct NodeType
{
unsigned char ascii;
unsigned int weight;
NodeType *next, *zero, *one;
NodeType() : ascii{'*'} {}
};
template <typename NodeType>
class PriorityQueue
{
private:
NodeType *root;
NodeType **envPtr;
unsigned int nodeCount;
void altClear(NodeType *);
void altPrint(NodeType *, unsigned int);
// Private Huffman Tree Methods //
void altEncode(NodeType *, std::string, HuffmanCode *);
void altDecode(NodeType *, std::string, std::basic_string<char>::size_type);
public:
PriorityQueue();
~PriorityQueue();
void clear();
bool isThere(NodeType *);
void push(NodeType *);
void pop();
void print();
unsigned int size();
// Public Huffman Tree Methods //
void buildHuffTree();
void printHuffTree(NodeType *, int);
void encode(unsigned char);
void decode(std::string);
std::vector<std::string> encodings;
};
template <typename NodeType>
void PriorityQueue<NodeType>::altClear(NodeType *node)
{
if ( node )
{
// Pointer to node's next node (possibly null)
NodeType *nextNode = node->next;
// Break link to next node
node->next = nullptr;
// Destroy root node
delete node;
// Attempt delete of next node
altClear(nextNode);
}
}
template <typename NodeType>
void PriorityQueue<NodeType>::altPrint(NodeType *node, unsigned int index)
{
if ( node )
{
std::cout << '\t' << index << '\t' << *node << std::endl;
altPrint(node->next, index+1);
}
}
template <typename NodeType>
PriorityQueue<NodeType>::PriorityQueue()
{
this->root = nullptr;
this->envPtr = nullptr;
this->nodeCount = 0;
clear();
}
template <typename NodeType>
PriorityQueue<NodeType>::~PriorityQueue()
{
altClear(this->root); // Deletes existing list
}
template <typename NodeType>
void PriorityQueue<NodeType>::clear()
{
altClear(this->root);
this->root = nullptr;
this->encodings.clear();
}
template <typename NodeType>
bool PriorityQueue<NodeType>::isThere(NodeType *unit)
{
this->envPtr = &this->root;
while( *this->envPtr && (*this->envPtr)->weight <= unit->weight )
this->envPtr = &(*this->envPtr)->next;
return *this->envPtr && (*this->envPtr)->ascii == unit->ascii;
}
template <typename NodeType>
void PriorityQueue<NodeType>::push(NodeType *unit)
{
if ( !isThere(unit) )
{
try
{
NodeType *p = *this->envPtr;
*this->envPtr = new NodeType;
(*this->envPtr)->ascii = unit->ascii;
(*this->envPtr)->weight = unit->weight;
(*this->envPtr)->zero = unit->zero;
(*this->envPtr)->one = unit->one;
(*this->envPtr)->next = p;
this->nodeCount++;
}
catch(const std::bad_alloc& excpt)
{
std::cout << "std::bad_alloc::what(): " << excpt.what() << std::endl;
}
}
else
{
std::cout << "Unit: <" << unit << "> already exists in PriorityQueue" << std::endl;
}
}
template <typename NodeType>
void PriorityQueue<NodeType>::pop()
{
if ( this->nodeCount )
{
this->envPtr = &this->root;
NodeType *p = *this->envPtr;
this->envPtr = &(*this->envPtr)->next;
this->root = *this->envPtr;
p->next = nullptr;
delete p;
this->nodeCount--;
}
else
{
std::cout << "*** This PriorityQueue is empty ***" << std::endl;
}
}
template <typename NodeType>
void PriorityQueue<NodeType>::print()
{
std::cout << "*** Priority Queue Linked List Contents ***" << std::endl;
std::cout << '#' << std::setw(100) << std::setfill('#') << '#' << std::endl;
std::cout << " INDEX ASCII COUNT" << std::endl;
std::cout << "*---------------------------------------*" << std::endl;
if ( this->root )
altPrint(this->root, 0);
else
std::cout << "*** This PriorityQueue is empty ***" << std::endl;
std::cout << "*---------------------------------------*" << std::endl;
std::cout << " Node Count: " << this->nodeCount << std::endl;
std::cout << '#' << std::setw(100) << std::setfill('#') << '#' << std::endl;
}
template <typename NodeType>
unsigned int PriorityQueue<NodeType>::size()
{
return this->nodeCount;
}
template <typename NodeType>
void PriorityQueue<NodeType>::buildHuffTree()
{
while( this->root && this->root->next )
{
NodeType *p = new NodeType;
p->zero = this->root;
p->one = this->root->next;
p->weight = p->zero->weight + p->one->weight;
this->root = this->root->next->next;
push( p );
}
std::cout << "*** Huffman Tree Contents ***\t ('*' represents an internal node)" << std::endl;
std::cout << '#' << std::setw(100) << std::setfill('#') << '#' << std::endl;
std::cout << " ASCII WEIGHT LEVEL" << std::endl;
std::cout << "*---------------------------------------*" << std::endl;
printHuffTree(this->root, 0);
std::cout << "*---------------------------------------*" << std::endl;
std::cout << '#' << std::setw(100) << std::setfill('#') << '#' << std::endl;
}
template <typename NodeType>
void PriorityQueue<NodeType>::printHuffTree(NodeType *p, int level)
{
if ( p )
{
std::cout << '\t' << p->ascii << '\t' << p->weight << '\t' << level << std::endl;
printHuffTree(p->zero, level+1);
printHuffTree(p->one, level+1);
}
}
template <typename NodeType>
void PriorityQueue<NodeType>::altEncode(NodeType* parent, std::string encoding, HuffmanCode *huffCode)
{
// Node is a nullptr OR leaf node
if ( !parent || (!parent->zero && !parent->one) )
{
return;
}
// Found ASCII character at node's left child
else if( parent->zero->ascii == huffCode->first )
{
encoding += "0";
huffCode->second = encoding;
}
// Found ASCII character at node's right child
else if( parent->one->ascii == huffCode->first )
{
encoding += "1";
huffCode->second = encoding;
}
else
{
// Recursively search huffman tree for requested ASCII character value
altEncode(parent->zero, encoding + "0", huffCode);
altEncode(parent->one, encoding + "1", huffCode);
}
}
template <typename NodeType>
void PriorityQueue<NodeType>::encode(unsigned char ch)
{
HuffmanCode enc = std::make_pair(ch, "");
altEncode(this->root, "", &enc);
std::cout << '\t' << enc.first << '\t' << enc.second << std::endl;
this->encodings.push_back( enc.second );
}
template <typename NodeType>
void PriorityQueue<NodeType>::altDecode(NodeType *parent, std::string encoding, std::basic_string<char>::size_type index)
{
if ( index < encoding.size() && encoding[index] == '0' )
{
altDecode(parent->zero, encoding, index+1);
}
else if ( index < encoding.size() && encoding[index] == '1' )
{
altDecode(parent->one, encoding, index+1);
}
else
{
std::cout << "Decoded( \"" << encoding << "\" )\t->\t\'" << parent->ascii << '\'' << std::endl;
}
}
template <typename NodeType>
void PriorityQueue<NodeType>::decode(std::string encoding)
{
altDecode(this->root, encoding, 0);
}
#endif // PriorityQueue_cpp //