Skip to content

Commit 3b0bcb8

Browse files
committed
huffman coding
1 parent 216ef4e commit 3b0bcb8

File tree

1 file changed

+253
-0
lines changed

1 file changed

+253
-0
lines changed

HuffmanCoding/huffman_coding.cpp

+253
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
#include <bits/stdc++.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
using namespace std;
5+
6+
ifstream input;
7+
ofstream output;
8+
9+
struct Node{
10+
char *symbol;
11+
int frequency;
12+
Node *next;
13+
Node *left;
14+
Node *right;
15+
};
16+
17+
void TreeTraverse(string *H,Node *A,string C)
18+
{
19+
output<<A->symbol<<" ";
20+
if(A->left == NULL && A->right==NULL)
21+
{
22+
if(*(A->symbol) >= 'a' && *(A->symbol)<='z')
23+
{
24+
H[(int)(*(A->symbol) - 'a')] = C;
25+
}
26+
else
27+
{
28+
H[(int)(*(A->symbol)-'0')+26] = C;
29+
}
30+
}
31+
else
32+
{
33+
string t;
34+
t = C;
35+
C.append("0");
36+
t.append("1");
37+
TreeTraverse(H,A->left,C);
38+
TreeTraverse(H,A->right,t);
39+
}
40+
}
41+
42+
int main()
43+
{
44+
45+
input.open("log.txt");
46+
output.open("18CS10034_A5_output.txt");
47+
48+
int n;
49+
input>>n;
50+
int freq[36];
51+
52+
for(int i=0;i<36;i++)
53+
{
54+
freq[i]=0;
55+
}
56+
57+
for(int i=0;i<n;i++)
58+
{
59+
string s;
60+
getline(input,s);
61+
62+
for(int j=0;j<s.size();j++)
63+
{
64+
if((int)s[j] >='a' && (int)s[j] <='z')
65+
{
66+
freq[(int)s[j] - 'a']++;
67+
}
68+
else
69+
{
70+
freq[(int)s[j] - '0' + 26]++;
71+
}
72+
}
73+
}
74+
75+
for(int i=0;i<36;i++)
76+
{
77+
if(i==0)
78+
{
79+
output<<(char)('a'+i)<<"="<<freq[i];
80+
}
81+
else if(i<=25)
82+
{
83+
output<<","<<(char)('a'+i)<<"="<<freq[i];
84+
}
85+
else
86+
{
87+
output<<","<<(char)('0'+i-26)<<"="<<freq[i];
88+
}
89+
}
90+
output<<"\n";
91+
92+
int temp[36];
93+
for(int i=0;i<36;i++)
94+
temp[i] = freq[i];
95+
int max=INT_MIN;
96+
int index;
97+
98+
Node *list;
99+
list = NULL;
100+
101+
for(int i=0;i<36;i++)
102+
{
103+
max = INT_MIN;
104+
for(int j=0;j<36;j++)
105+
{
106+
if(temp[j]>=max)
107+
{
108+
max = temp[j];
109+
index = j;
110+
}
111+
}
112+
113+
char *s;
114+
s = new char;
115+
if(index>=0 && index<=25)
116+
{
117+
*s = char(index + 'a');
118+
}
119+
else
120+
*s = char(index + '0' -26);
121+
122+
Node *tem;
123+
tem = new Node;
124+
tem->frequency = max;
125+
tem->symbol = s;
126+
tem->left = NULL;
127+
tem->right = NULL;
128+
tem->next = NULL;
129+
130+
temp[index] = INT_MIN;
131+
132+
if(list == NULL)
133+
{
134+
list = tem;
135+
}
136+
else
137+
{
138+
tem->next = list;
139+
list = tem;
140+
}
141+
142+
}
143+
144+
int count=1;
145+
146+
while(list->next !=NULL)
147+
{
148+
149+
Node *tr;
150+
tr = list;
151+
while(tr!=NULL)
152+
{
153+
cout<<tr->symbol<<" ";
154+
tr = tr->next;
155+
}
156+
cout<<endl;
157+
158+
159+
char *a;
160+
if(count>9)
161+
{
162+
a = new char[3];
163+
a[0] = 'N';
164+
a[1] = (count/10) + '0';
165+
a[2] = (count%10) + '0';
166+
}
167+
else
168+
{
169+
a = new char[2];
170+
a[0] = 'N';
171+
a[1] = count + '0';
172+
}
173+
174+
Node *temp;
175+
temp = new Node;
176+
temp->symbol = a;
177+
temp->frequency = list->frequency + list->next->frequency;
178+
temp->left = list;
179+
temp->right = list->next;
180+
temp->next = NULL;
181+
182+
cout<<"a"<<endl;
183+
list = list->next->next;
184+
if(list == NULL)
185+
{
186+
list = temp;
187+
break;
188+
}
189+
190+
191+
Node *temp1;
192+
temp1 = list;
193+
194+
if(list->frequency >= temp->frequency)
195+
{
196+
temp->next = list;
197+
list = temp;
198+
}
199+
else
200+
{
201+
while(list->next!=NULL)
202+
{
203+
if(list->next->frequency >= temp->frequency)
204+
{
205+
temp->next = list->next;
206+
list->next = temp;
207+
break;
208+
}
209+
list = list->next;
210+
}
211+
if(list->next==NULL)
212+
{
213+
list->next = temp;
214+
}
215+
list = temp1;
216+
217+
}
218+
219+
count++;
220+
221+
}
222+
223+
cout<<list->symbol<<endl;
224+
225+
string H[36];
226+
string C = "";
227+
228+
TreeTraverse(H,list,C);
229+
230+
output<<"\n";
231+
232+
for(int i=0;i<36;i++)
233+
{
234+
cout<<H[i]<<endl;
235+
}
236+
237+
for(int i=0;i<36;i++)
238+
{
239+
if(i<=25)
240+
{
241+
output<<(char)('a'+i)<<" "<<H[i]<<endl;
242+
}
243+
else
244+
{
245+
output<<(char)('0'+i-26)<<" "<<H[i]<<endl;
246+
}
247+
}
248+
249+
250+
251+
252+
253+
}

0 commit comments

Comments
 (0)