-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhuffman_final.cpp
More file actions
executable file
·229 lines (218 loc) · 3.98 KB
/
huffman_final.cpp
File metadata and controls
executable file
·229 lines (218 loc) · 3.98 KB
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
//Hrishi
// Huffman Tree using min Heap + Encoding + Decoding
#include<iostream>
#include<cstdio>
#define asize 100
using namespace std;
struct node
{
struct node *lptr;
struct node *rptr;
int f;
char c;
};
class Heap
{
public:
Heap();
struct node B[100];
int hsize;
void HeapInsert(struct node &T);
void HeapDisplay();
struct node* Delmin();
}H;
Heap::Heap()
{
for(int i=0;i<100;i++)
B[i].lptr=B[i].rptr=NULL;
}
struct node *hptr;
void swap(struct node &a,struct node &b);
void HUFF_TREE();
struct node* huff_tree_insert(struct node *TEMP1,struct node *TEMP2);
void huff_tree_inorder(struct node *ptr);
void getcode(int A[],char letter,struct node *ptr,int prev_val);
void getstring(char C[]);
int main()
{
H.hsize=0;
int A[20],N,num,freq;
char C[50],letter;
struct node *temp=new struct node;
cout<<"\n Enter the no.of distinct letters: ";
cin>>N;
for(int i=0;i<N;i++)
{
cout<<"\n "<<i+1<<" th letter: ";
cin>>temp->c;
cout<<" frequency: ";
cin>>temp->f;
H.HeapInsert(*temp);
}
H.HeapDisplay();
HUFF_TREE();
cout<<endl<<"\nHuffman Tree(Inorder):\n";
huff_tree_inorder(hptr);
cout<<endl;
do{
cout<<"\n\n 1.Get string for the given Hoffman code(Decoding):\n 2.Get Hoffman code for the given string (Encoding):\n 3.Exit\n Enter your choice: ";
cin>>num;
if(num==1)
{
cout<<"\n Enter the huffman code to identify string: ";
cin>>C;
getstring(C);
}
if(num==2)
{
cout<<"\n Enter the string to find hoffman code: ";
cin>>C;
for(int i=0;C[i]!='\0';i++)
getcode(A,C[i],hptr,0);
}
}while(num!=3);
cout<<endl;
return 0;
}
void Heap::HeapInsert(struct node &T)
{
hsize++;
B[hsize]=T;
int i=hsize;
while(i>1&& B[i].f<B[i/2].f)
{
swap (B[i],B[i/2]);
i=i/2;
}
}
struct node * Heap::Delmin()
{
struct node *TEMP=new struct node;
TEMP->lptr=B[1].lptr;
TEMP->rptr=B[1].rptr;
TEMP->c=B[1].c;
TEMP->f=B[1].f;
B[1]=B[hsize--];
cout<<"\n Minimum element: "<<TEMP->f<<": "<<TEMP->c<<" got deleted";
int i=1; int minI;
while(2*i<=hsize)
{
if((2*i+1)>hsize)
minI=2*i;
else
{
if(B[2*i].f<B[2*i+1].f)
minI=2*i;
else
minI=2*i+1;
}
if(B[minI].f<B[i].f)
{
swap (B[i],B[minI]);
i=minI;
}
else
break;
}
return TEMP;
}
void Heap::HeapDisplay()
{
cout<<"\nHeap Display:\n";
struct node *temp;
Heap H1=H;
for(int i=1;i<=H1.hsize;)
temp=H1.Delmin();
cout<<endl;
}
void HUFF_TREE()
{
cout<<"\n Heap with sum reinsertion:\n";
struct node *TEMP1,*TEMP2;
cout<<H.hsize;
while(H.hsize>1)
{
TEMP1=H.Delmin();
TEMP2=H.Delmin();
struct node *TEMP=huff_tree_insert(TEMP1,TEMP2);
H.HeapInsert(*TEMP);
cout<<endl;
}
hptr=H.Delmin();
}
struct node* huff_tree_insert(struct node *TEMP1,struct node *TEMP2)
{
struct node *thptr=new struct node;
thptr->lptr=TEMP1; //always least freq will be coming to TEMP1!
thptr->rptr=TEMP2;
thptr->f=TEMP1->f+TEMP2->f;
thptr->c='@';
return(thptr);
}
void huff_tree_inorder(struct node *ptr)
{
if(ptr!=NULL)
{
huff_tree_inorder(ptr->lptr);
cout<<"\n freq: "<<ptr->f<<" char: "<<ptr->c;
huff_tree_inorder(ptr->rptr);
}
}
void getstring(char C[])
{
struct node *thptr=hptr;
int i=0;
cout<<"\nThe string for given code is: ";
while(C[i]!='\0')
{
if(thptr->lptr==NULL&& thptr->rptr==NULL)
{
cout<<thptr->c;
thptr=hptr;
continue;
}
if(C[i]=='0')
thptr=thptr->lptr;
else if(C[i]=='1')
thptr=thptr->rptr;
else
{
cout<<"\n Invalid code";
break;
}
i++;
}
if(thptr->lptr==NULL&& thptr->rptr==NULL)
cout<<thptr->c;
else
cout<<"\n invalid Code";
}
void getcode(int A[],char letter,struct node *ptr,int prev_val)
{
if(ptr->lptr!=NULL)
{
A[prev_val]=0;
getcode(A,letter,ptr->lptr,prev_val+1);
}
if(ptr->rptr!=NULL)
{
A[prev_val]=1;
getcode(A,letter,ptr->rptr,prev_val+1);
}
if(ptr->lptr==NULL&& ptr->rptr==NULL)
{
if(ptr->c==letter)
{
cout<<"\n code for "<<letter<<" is: ";
for(int i=0;i<prev_val;i++)
cout<<A[i];
cout<<endl;
}
}
}
void swap(struct node &a,struct node &b)
{
struct node temp=a;
a=b;
b=temp;
}