-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuffmanCoding.hh
212 lines (195 loc) · 5.74 KB
/
HuffmanCoding.hh
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
// =========================================================
// Author: Anupam Samanta
// Create date: 08-09-2021
// Description: Huffman Coding Class
// =========================================================
#include <bits/stdc++.h>
using namespace std;
// DecodeTable class that can be used in other files to get decodeTable using HuffmanCoding::getDecodeTable()
class DecodeTable
{
private:
unordered_map<string, char> decodeTable;
DecodeTable(unordered_map<string, char> decodeTable)
{
this->decodeTable = decodeTable;
}
friend class HuffmanCoding;
// public:
// void operator=(DecodeTable &decodeTable)
// {
// }
};
// Main class for huffman coding
class HuffmanCoding
{
private:
// Node class for huffman coding
class HCNode
{
public:
char data;
int charCount;
HCNode *zero;
HCNode *one;
HCNode(char data, int count)
{
this->data = data;
charCount = count;
zero = NULL;
one = NULL;
}
};
unordered_map<char, string> encodeTable;
unordered_map<string, char> decodeTable;
// class to compare HCNode charCount in priority queue
class Compare
{
public:
bool operator()(HCNode *parent, HCNode *child)
{
return parent->charCount > child->charCount;
}
};
// function to delete the huffman tree or root
void deleteTree(HCNode *root)
{
if (root == NULL)
return;
queue<HCNode *> q;
q.push(root);
while (!q.empty())
{
HCNode *tempNode;
tempNode = q.front();
if (tempNode->zero)
q.push(tempNode->zero);
if (tempNode->one)
q.push(tempNode->one);
q.pop();
delete tempNode;
}
}
// function to join two HCNoe and will return a joint node of type HCNode
HCNode *joinNode(HCNode *zero, HCNode *one)
{
HCNode *node = new HCNode('\0', zero->charCount + one->charCount);
node->zero = zero;
node->one = one;
return node;
}
// temp function to generate encode and decode table
// on which recursion will be called
void generateEncodeDecodeTableTemp(HCNode *root, string encodedBit)
{
if (root == NULL)
return;
if (root->data != '\0')
{
encodeTable[root->data] = encodedBit;
decodeTable[encodedBit] = root->data;
return;
}
if (root->data == '\0')
{
generateEncodeDecodeTableTemp(root->zero, encodedBit + "0");
generateEncodeDecodeTableTemp(root->one, encodedBit + "1");
}
return;
}
// function to generate encode and decode table
void generateEncodeDecodeTable(HCNode *root)
{
generateEncodeDecodeTableTemp(root, "");
}
public:
// function to encode the given string
// return a string of 0's and 1's
string encode(string s)
{
clear();
string encodedString;
if (s.size() == 0)
return encodedString;
unordered_map<char, int> charCount;
for (int i = 0; i < s.size(); i++)
{
charCount[s[i]]++;
}
priority_queue<HCNode *, vector<HCNode *>, Compare> nodeQueue; // creating the min priority queue
for (auto i : charCount)
{
// datatype of i is pair
// i.first contains character
// i.second contains count of that character
nodeQueue.push(new HCNode(i.first, i.second));
}
// if the string has only one type of character then just return 0's according to the number of character
if (nodeQueue.size() == 1)
{
for (int i = 0; i < nodeQueue.top()->charCount; i++)
encodedString.append("0");
HCNode *temp = nodeQueue.top();
nodeQueue.pop();
delete temp;
}
// if there is multiple types of character then
// - create huffman tree
// - generate encode and decode table
else
{
while (nodeQueue.size() != 1)
{
HCNode *zero = nodeQueue.top();
nodeQueue.pop();
HCNode *one = nodeQueue.top();
nodeQueue.pop();
nodeQueue.push(joinNode(zero, one));
}
HCNode *root = nodeQueue.top();
nodeQueue.pop();
generateEncodeDecodeTable(root);
deleteTree(root);
for (int i = 0; i < s.size(); i++)
encodedString.append(encodeTable[s[i]]);
}
return encodedString;
}
// function to decode the given string using given decodeTable
// return the decoded string
string decode(string encodedString, DecodeTable decodeTable)
{
clear();
string decodedString;
int size = 1;
this->decodeTable = decodeTable.decodeTable;
while (encodedString.size() != 0)
{
string substr = encodedString.substr(0, size);
if (this->decodeTable.count(substr) > 0)
{
string s(1, this->decodeTable[substr]);
decodedString.append(s);
encodedString = encodedString.substr(size);
size = 1;
}
else
{
size++;
}
}
return decodedString;
}
// function to clear everything (ie. encodeTable, decodeTable)
void clear()
{
encodeTable.clear();
decodeTable.clear();
}
// function to get decodeTable
DecodeTable getDecodeTable()
{
DecodeTable temp(decodeTable);
return temp;
}
};