forked from Srikant797/Cplusplus-for-Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman_coding.cpp
160 lines (129 loc) · 3.12 KB
/
huffman_coding.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
//Implementation of Huffman Coding, Das here :D
#include <bits/stdc++.h>
using namespace std;
class huffmanNode{
public:
char data;
int freq;
huffmanNode * left;
huffmanNode * right;
huffmanNode(char data,int freq){
this->data=data;
this->freq=freq;
this->left=NULL;
this->right=NULL;
}
};
struct helper {
bool operator()(huffmanNode* h1, huffmanNode* h2)
{
return (h1->freq > h2->freq);
}
};
priority_queue<huffmanNode*, vector<huffmanNode*>, helper> pq;
map <char,int> freq;
void readinput(string inp){
for(int i=0;i<inp.size();i++){
freq[inp[i]]+=1;
}
cout<<"Frequency of characters are:- "<<endl;
for(auto z:freq){
cout<<z.first<<" "<<z.second<<endl;
}
}
void pushnodes(){
for(auto z:freq){
huffmanNode* a=new huffmanNode(z.first,z.second);
pq.push(a);
}
}
void maketree(){
while(pq.size()!=1){
huffmanNode * left=pq.top();
pq.pop();
huffmanNode * right=pq.top();
pq.pop();
huffmanNode * parent=new huffmanNode('#',left->freq+right->freq);
parent->left=left;
parent->right=right;
pq.push(parent);
}
}
map<char,string> codes;
void getcodes(huffmanNode * root,string str){
if(root==NULL){
return;
}
if(root->data!='#'){
codes[root->data]=str;
}
getcodes(root->left,str+"0");
getcodes(root->right,str+"1");
}
string output(string inp,string out){
cout<<"The codes for the characters are:- "<<endl;
for(auto z:codes){
cout<<z.first<<" "<<z.second<<endl;
}
cout<<endl;
for(int i=0;i<inp.size();i++){
out+=codes[inp[i]];
}
cout<<"Encrypted code will be: ";
cout<<out<<endl;
return out;
}
string decode(huffmanNode * root,string out){
string ans="";
huffmanNode * curr=root;
for(int i=0;i<out.size();i++){
if(out[i]=='0'){
curr=curr->left;
}else{
curr=curr->right;
}
if(curr->left==NULL && curr->right==NULL){
ans+=curr->data;
curr=root;
}
}
return ans;
}
void level_wise_print(){
queue <huffmanNode *> pendingNodes;
pendingNodes.push(pq.top());
while (pendingNodes.size() != 0) {
int n=pendingNodes.size();
while(n>0){
huffmanNode* front = pendingNodes.front();
pendingNodes.pop();
cout<<front->data<<":"<<front->freq<<" ";
if (front->left !=NULL) {
pendingNodes.push(front->left);
}
if (front->right !=NULL) {
pendingNodes.push(front->right);
}
n--;
}
cout<<endl;
}
return ;
}
int main(){
string inp;
cout<<"Enter string: ";
cin>>inp;
string out="";
readinput(inp);
pushnodes();
maketree();
getcodes(pq.top(),"");
out=output(inp,out);
string ans=decode(pq.top(),out);
cout<<"Decoded string is: ";
cout<<ans<<endl;
cout<<"\nLevel wise print is:\n"<<endl;
level_wise_print();
return 0;
}