-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffmen_encoding.cpp
331 lines (305 loc) · 5.54 KB
/
Huffmen_encoding.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Huffmen encoding is used to compress the text file size
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<iostream>
#define SIZE 2
template <class T>
class Stack{
T s[SIZE];
int indx;
public:
int read_ist;
Stack();
bool is_Full();
bool is_Empty();
void push(T val);
T read_from_start();
T pop();
T top();
};
template <class T>
Stack<T>::Stack()
{
indx = -1;
}
template <class T>
T Stack<T>::read_from_start()
{
T store = s[read_ist];
if(read_ist == indx)
read_ist = 0;
else
read_ist++;
return store;
}
template <class T>
T Stack<T>::top()
{
return s[indx];
}
template <class T>
void Stack<T>::push(T val)
{
if(is_Full())
{
std::cout << "Stack is full " << std::endl;
exit(1);
}
s[++indx] = val;
}
template <class T>
T Stack<T>::pop()
{
if(is_Empty())
{
std::cout << "Stack is Empty ";
exit(1);
}
return s[indx--];
}
template <class T>
bool Stack<T>::is_Full()
{
if(indx >= SIZE-1)
return true;
return false;
}
template <class T>
bool Stack<T>::is_Empty()
{
if(indx < 0)
return true;
return false;
}
struct find{
int freq;
char ch;
};
struct char_nemonic{
char c;
std::string nemonic;
};
class TreeNode{
public:
int val;
int freq;
TreeNode *left ,*right, *parent;
TreeNode(int val, int freq);
};
TreeNode::TreeNode(int val, int freq)
{
this->val = val;
this->freq = freq;
left = right = NULL;
}
class Tree{
TreeNode* root;
TreeNode* tmp;
std::string str;
std::string code;
std::string on, off;
int brk;
std::stack<char> chr;
std::stack<char> nemonic;
Stack<std::string> store;
std::stack<std::string> temp;
std::string rev;
bool cond;
char chh;
public:
static int indx;
Tree(std::string str = "");
void Huffmen_Encode();
void pre_order(char_nemonic* m,TreeNode* t = NULL);
void Huffmen_Decod(std::string bin_code = "", TreeNode* temp = NULL);
};
Tree::Tree(std::string str)
{
this->str = str;
root = NULL;
}
void Tree::Huffmen_Decod(std::string bin_code , TreeNode* temp)
{
temp = root;
char on = '1';
char off = '0';
for(int i = 0 ; i < bin_code.length(); i++)
{
if(bin_code[i] == off )
{
temp = temp->left;
}
else if(bin_code[i] == on)
{
temp = temp->right;
}
if(temp->right == NULL && temp->left == NULL)
{
std::cout << char(temp->val);
temp = root;
}
}
}
int Tree::indx = 0;
void Tree::pre_order(char_nemonic* m, TreeNode* t)
{
if(t == NULL)
{
code = "";
t = root;
}
if(t->right == NULL && t->left == NULL)
{
code = "";
tmp = t;
on = "1";
off = "0";
while(tmp->parent != NULL)
{
if(tmp == tmp->parent->left)
code += off;
else if(tmp == tmp->parent->right)
code+= on;
tmp = tmp->parent;
}
rev = code;
code = "";
int itr = 0;
for(int i = rev.length()-1; i >= 0; i-- )
{
code += rev[i];
}
m[indx].c = t->val;
m[indx].nemonic = code;
temp.push(code);
indx++;
}
if(t->left != NULL)
pre_order(m , t->left);
if(t->right != NULL)
pre_order( m, t->right);
//std::cout << "Ret \n";
}
void Tree::Huffmen_Encode()
{
std::string complete = "";
find f[str.length()];
find swap;
char c;
int count;
int indx = 0;
bool flag;
for(int i = 0 ; i < str.length(); i++)
{
flag = false;
count = 0;
for(int k = 0; k < complete.length(); k++)
{
if(str[i] == complete[k])
{
flag = true;
break;
}
}
if(flag)
continue;
for(int j = i; j < str.length(); j++)
{
if(str[i] == str[j])
count++;
}
complete += str[i];
f[indx].ch = str[i];
f[indx].freq = count;
indx++;
}
brk = indx-1;
for(int i = 0 ; i <= brk; i++)
{
for(int j = 0 ; j <= brk; j++)
{
if(f[i].freq < f[j].freq)
{
swap = f[i];
f[i] = f[j];
f[j] = swap;
}
}
}
for(int i = indx; i < str.length(); i++)
f[i].freq = 0;
for(int i = 0; i < brk+1; i++)
{
std::cout << "char is " << f[i].ch << " , and freq is " << f[i].freq << std::endl;
}
// Huffmen encoding starts here
std::queue<TreeNode*> q;
std::queue<TreeNode*> q2;
for(int i = 0; i <= brk; i++)
{
TreeNode* t = new TreeNode(f[i].ch, f[i].freq);
q.push(t);
}
TreeNode *child1, *child2, *hld;
flag = false;
while(!q.empty() || !q2.empty() )
{
flag = false;
root = child1 = q.front();
q.pop();
if(q.empty() && q2.empty())
{
break;
}
child2 = q.front();
q.pop();
if(q.front() == q.back())
flag = true;
TreeNode* parent = new TreeNode(0, (child1->freq + child2->freq));
parent->left = child1;
parent->right = child2;
child1->parent = child2->parent = parent;
if(q.front() == q.back())
{
q2.push(q.front());
q.pop();
}
q2.push(parent);
if(q.empty())
{
while(!q2.empty())
{
q.push(q2.front());
q2.pop();
}
}
}
root->parent = NULL;
char_nemonic m[brk+1];
pre_order(m);
for(int i = 0; i <= brk; i++)
{
std::cout << "character " << m[i].c << " has binary value " << m[i].nemonic << std::endl;
}
code = "";
for(int i = 0; i < str.length(); i++)
{
for(int j = 0; j <= brk; j++)
{
if(str[i] == m[j].c)
{
code += m[j].nemonic;
}
}
}
std::cout << "This is the list of binarycode of all string \n" << code << std::endl;
Huffmen_Decod(code);
}
int main()
{
std::string encode = "My name is Muhammad Aamir KHan " ;
Tree t(encode);
t.Huffmen_Encode();
}