#include<bits/stdc++.h>
using namespace std;
struct data
{
int cou;
};
data store[129];
struct node
{
string s;
int frq;
node *address;
node *left,*right;
bool operator<(const node& target) const
{
return frq >target.frq;
}
};
node arr[100];
priority_queue<node>q;
int n;
int string_match(string ch,string str)
{
int len = str.length(),p=0;
for(int i=0; i<len; i++)
{
if(ch[0]==str[i])
{
p=1;
}
}
return p;
}
node *generate_huffman_tree(string line)
{
string left_str,right_str;
int left_fr,right_fr;
for(int i=0; i<n-1; i++)
{
node *z,current,next;
z = new node();
current = q.top();
q.pop();
left_str = current.s;
left_fr = current.frq;
z->left = current.address;
next = q.top();
q.pop();
z->right = next.address;
right_str=next.s;
right_fr =next.frq;
z->s = left_str +right_str;
z->frq = left_fr+right_fr;
z->address = z;
q.push(*z);
//cout << z->s << ' ' << z->frq <<' '<< z->address <<' ' << endl;
}
node * root,x;
x= q.top();
root = x.address;
return root;
}
void generate_code_word(node *root)
{
cout << "code word" << endl <<endl;
for(int i=0; i<n; i++)
{
node *parent,*leftt,*rightt;
int m;
string ch = arr[i].s;
string bit="" ,str;
parent = root;
for (int j=0; j<2*n-1; j++)
{
str= parent->s;
if(str==ch)
{
cout << ch <<' ' << bit <<endl;
break;
}
leftt = parent->left;
str = leftt->s;
m=string_match(ch,str);
if(m==0)
{
bit+="1";
parent = parent->right;
}
else
{
bit+="0";
parent=leftt;
}
}
}
}
string generate_encodedstring(node *root,string line)
{
cout <<endl<< "show encoded string :" << endl;
int length = line.length();
string encoded_str="";
for(int i=0; i<length; i++)
{
node *parent,*leftt,*rightt;
int m;
string ch ="";
ch +=line[i];
string bit="" ,str;
parent= root;
for (int j=0; j<2*n-1; j++)
{
str= parent->s;
if(str==ch)
{
encoded_str+=bit ;
break;
}
leftt = parent->left;
str = leftt->s;
m=string_match(ch,str);
if(m==0)
{
bit+="1";
parent = parent->right;
}
else
{
bit+="0";
parent=leftt;
}
}
}
return encoded_str;
}
void huffman_decoding(node *root,string str)
{
cout << endl <<"Decoded String : " << endl;
int len = str.length();
node *parent;
parent = root;
for(int i=0; i<len; i++)
{
if(str[i]=='0')
{
parent = parent->left;
}
else if(str[i]=='1')
{
parent = parent->right;
}
if(parent->left ==NULL)
{
cout << parent->s ;
parent=root;
}
}
cout << endl;
}
int main()
{
string line;
getline(cin ,line);
int length = line.length();
for(int i=0; i<129; i++)
{
store[i].cou=0;
}
for(int i=0; i<length; i++)
{
int l = line[i];
store[l].cou++;
}
n =0;
for(int i=0; i<129; i++)
{
if(store[i].cou !=0)
{
string temp = "";
char c = i;
temp+=c;
arr[n].s = temp;
arr[n].frq=store[i].cou;
arr[n].left =NULL;
arr[n].right=NULL;
arr[n].address = &arr[n];
q.push(arr[n]);
n++;
}
}
node *result;
result = generate_huffman_tree(line);
generate_code_word(result);
string str = generate_encodedstring(result,line);
cout << str << endl;
huffman_decoding(result,str);
}
INPUT:
Hey ! Are you OK ?
OUTPUT:
code word
11
! 0110
? 0111
A 0000
H 0001
K 0101
O 0100
e 001
o 1010
r 1001
u 1000
y 1011
show encoded string :
000100110111101101100001001001111011101010001101000101110111
Decoded String :
Hey ! Are you OK ?
using namespace std;
struct data
{
int cou;
};
data store[129];
struct node
{
string s;
int frq;
node *address;
node *left,*right;
bool operator<(const node& target) const
{
return frq >target.frq;
}
};
node arr[100];
priority_queue<node>q;
int n;
int string_match(string ch,string str)
{
int len = str.length(),p=0;
for(int i=0; i<len; i++)
{
if(ch[0]==str[i])
{
p=1;
}
}
return p;
}
node *generate_huffman_tree(string line)
{
string left_str,right_str;
int left_fr,right_fr;
for(int i=0; i<n-1; i++)
{
node *z,current,next;
z = new node();
current = q.top();
q.pop();
left_str = current.s;
left_fr = current.frq;
z->left = current.address;
next = q.top();
q.pop();
z->right = next.address;
right_str=next.s;
right_fr =next.frq;
z->s = left_str +right_str;
z->frq = left_fr+right_fr;
z->address = z;
q.push(*z);
//cout << z->s << ' ' << z->frq <<' '<< z->address <<' ' << endl;
}
node * root,x;
x= q.top();
root = x.address;
return root;
}
void generate_code_word(node *root)
{
cout << "code word" << endl <<endl;
for(int i=0; i<n; i++)
{
node *parent,*leftt,*rightt;
int m;
string ch = arr[i].s;
string bit="" ,str;
parent = root;
for (int j=0; j<2*n-1; j++)
{
str= parent->s;
if(str==ch)
{
cout << ch <<' ' << bit <<endl;
break;
}
leftt = parent->left;
str = leftt->s;
m=string_match(ch,str);
if(m==0)
{
bit+="1";
parent = parent->right;
}
else
{
bit+="0";
parent=leftt;
}
}
}
}
string generate_encodedstring(node *root,string line)
{
cout <<endl<< "show encoded string :" << endl;
int length = line.length();
string encoded_str="";
for(int i=0; i<length; i++)
{
node *parent,*leftt,*rightt;
int m;
string ch ="";
ch +=line[i];
string bit="" ,str;
parent= root;
for (int j=0; j<2*n-1; j++)
{
str= parent->s;
if(str==ch)
{
encoded_str+=bit ;
break;
}
leftt = parent->left;
str = leftt->s;
m=string_match(ch,str);
if(m==0)
{
bit+="1";
parent = parent->right;
}
else
{
bit+="0";
parent=leftt;
}
}
}
return encoded_str;
}
void huffman_decoding(node *root,string str)
{
cout << endl <<"Decoded String : " << endl;
int len = str.length();
node *parent;
parent = root;
for(int i=0; i<len; i++)
{
if(str[i]=='0')
{
parent = parent->left;
}
else if(str[i]=='1')
{
parent = parent->right;
}
if(parent->left ==NULL)
{
cout << parent->s ;
parent=root;
}
}
cout << endl;
}
int main()
{
string line;
getline(cin ,line);
int length = line.length();
for(int i=0; i<129; i++)
{
store[i].cou=0;
}
for(int i=0; i<length; i++)
{
int l = line[i];
store[l].cou++;
}
n =0;
for(int i=0; i<129; i++)
{
if(store[i].cou !=0)
{
string temp = "";
char c = i;
temp+=c;
arr[n].s = temp;
arr[n].frq=store[i].cou;
arr[n].left =NULL;
arr[n].right=NULL;
arr[n].address = &arr[n];
q.push(arr[n]);
n++;
}
}
node *result;
result = generate_huffman_tree(line);
generate_code_word(result);
string str = generate_encodedstring(result,line);
cout << str << endl;
huffman_decoding(result,str);
}
INPUT:
Hey ! Are you OK ?
OUTPUT:
code word
11
! 0110
? 0111
A 0000
H 0001
K 0101
O 0100
e 001
o 1010
r 1001
u 1000
y 1011
show encoded string :
000100110111101101100001001001111011101010001101000101110111
Decoded String :
Hey ! Are you OK ?
No comments:
Post a Comment