-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman Encoder
222 lines (215 loc) · 4.72 KB
/
Huffman Encoder
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
//This is the c++ code for Huffman Compression by M.Zubair BESE-23C MCS, NUST.
#include<iostream>
#include<string>
#include<map>
using namespace std;
struct TreeNode{
int value;
string edge;
string s;
TreeNode *left,*right;
};
struct listnode{
TreeNode * ptr;
listnode * next;
};
class huffman{
public:
TreeNode* root;
listnode* head;
huffman(){
head=NULL;
root=NULL;
}
void listinsert(string a, int f){
TreeNode *tnode = new TreeNode;
tnode->left=NULL;
tnode->right=NULL;
tnode->s=a;
tnode->value=f;
listnode *lnode = new listnode;
lnode->next=NULL;
lnode->ptr=tnode;
listnode *lpos, *ltemp = head;
if(ltemp==NULL)
head=lnode;
else{
while(ltemp!=NULL){
if(f<=ltemp->ptr->value&<emp==head){
lnode->next=ltemp;
head=lnode;
break;
}
if(f>=ltemp->ptr->value&<emp->next==NULL){
ltemp->next=lnode;
break;
}
if(f>=ltemp->ptr->value&&f<ltemp->next->ptr->value){
lnode->next=ltemp->next;
ltemp->next=lnode;
break;
}
lpos=ltemp;
ltemp=ltemp->next;
}
}
}
void treemaker(){
TreeNode *temp1, *temp2;
string str;
int num;
while(head->next!=NULL){
temp1=head->ptr;
head=head->next;
temp2=head->ptr;
head=head->next;
num=temp1->value+temp2->value;
str=temp1->s+temp2->s;
listinsert(str,num);
treeinsert(temp1,temp2,str,num);
}
if(head->next==NULL)
treeshow(head->ptr,0,'@');
}
void treeinsert(TreeNode *t1, TreeNode *t2,string key, int val){
listnode *temp=head;
while(temp!=NULL){
if(temp->ptr->s==key&&temp->ptr->value==val){
if(t1->value<=t2->value){
temp->ptr->left=t1;
temp->ptr->right=t2;
}
else{
temp->ptr->left=t2;
temp->ptr->right=t1;
}
}
temp=temp->next;
}
}
void huffmanencoder(TreeNode *root,string str){
if(root->left==NULL&&root->right==NULL){
root->edge=str;
return;
}
huffmanencoder(root->left,"0");
root->edge=str;
huffmanencoder(root->right,"1");
root->edge=str;
}
void huffmancodegenerator(TreeNode *root, string str){
if(root->left==NULL&&root->right==NULL){
cout<<root->s<<"(";
root->s="";
root->s=str+root->edge;
cout<<root->s<<")\t";
return;
}
if(str!="-1"){
root->s="";
root->s=str+root->edge;
}
else
root->s=root->edge;
huffmancodegenerator(root->left,root->s);
if(str!="-1"){
root->s="";
root->s=str+root->edge;
}
else
root->s=root->edge;
huffmancodegenerator(root->right,root->s);
}
void listshow(){
listnode *ltemp = head;
if(ltemp==NULL)
cout<<"Sorry\n";
else{
while(ltemp!=NULL){
cout<<ltemp->ptr->s<<"="<<ltemp->ptr->value<<"\t";
ltemp=ltemp->next;
}
cout<<endl;
}
}
void treeshow(TreeNode *root, int space , char a)
{
if (root == NULL)
return;
space++;
treeshow(root->right, space, '/');
cout<<"\n";
if(root!=head->ptr)
for (int i =0; i < space; i++){
cout<<" ";
if(i==space-1)
cout<<a<<"--"<<root->edge<<"--";
}
if(root->left==NULL&&root->right==NULL)
cout<<root->s;
cout<<"("<<root->value<<")";
treeshow(root->left, space, '\\');
space--;
}
};
int main(){
string s="EIEIOEIEIOEEIOPPEEEEPPSSTTEEEPPPPTTSSEIEIOEIEIOEEIOPPEEEEEEEPPPPTTSS";
huffman hm;
map<char, int> charstrng;
map<char,int>::iterator itr;
for(int i=0; s[i]!='\0'; i++){
if(charstrng.count(s[i])>0)
{
itr=charstrng.find(s[i]);
itr->second=itr->second+1;
}
else{
charstrng.insert({ s[i], 1 });
}
}
cout<<"\t\t!!!!----Objectives of assignment----!!!!\n\n1.Count the frequency of occurrence for the letters in the sequence:-\n\n"<<s<<"\n\n";
for (itr = charstrng.begin(); itr != charstrng.end(); itr++) {
cout << itr->first
<< "=" << itr->second << '\t';
}
cout<<endl;
itr=charstrng.begin();
int min1=itr->second;
char s1=itr->first;
while(!charstrng.empty()){
itr=charstrng.begin();
min1=itr->second;
s1=itr->first;
for(itr=charstrng.begin(); itr!=charstrng.end(); itr++){
if(charstrng.size()==1){
min1=itr->second;
s1=itr->first;
break;
}
if(min1>itr->second){
min1=itr->second;
s1=itr->first;
}
}
if(charstrng.size()==1){
string str(1,s1);
hm.listinsert(str,min1);
charstrng.erase(s1);
break;
}
string str(1,s1);
hm.listinsert(str,min1);
charstrng.erase(s1);
}
if(charstrng.empty())
cout<<"\n2. Sort the frequencies into increasing order:-\n\n";
hm.listshow();
cout<<"\n3. Build the Huffman coding tree:\n\n";
hm.treemaker();
cout<<endl;
hm.huffmanencoder(hm.head->ptr,"-1");
cout<<"\n4.1_Assign a 0 to left branch and a 1 to the right branch\n\n";
hm.treeshow(hm.head->ptr,0,'@');
cout<<"\n\n4.2_Huffman code for each character.\n";
hm.huffmancodegenerator(hm.head->ptr,"-1");
}