forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
115 lines (104 loc) · 2.78 KB
/
solution.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
#include<bits/stdc++.h>
using namespace std;
// to map each character its huffman value
map<char,string>codes;
// to store the frequency of character of the input data
map<char,int>freq;
// A Huffman tree node
struct node{
int freq; // Frequency of the character
char data; // One of the input characters
node *left,*right; // Left and right child
node(int freq,char data){
left=right=NULL;
this->data=data;
this->freq=freq;
}
node(int freq)
{
left=right=NULL;
this->data=NULL;
this->freq=freq;
}
};
// function to comapre the values when inserted in priority queue
struct compare
{
bool operator()(node *l,node *r)
{
return (l->freq>r->freq);
}
};
// function iterates through the encoded string s
// if s[i]=='1' then move to node->right
// if s[i]=='0' then move to node->left
// if leaf node append the node->data to our output string
void decode_huff(node * root, string s) {
struct node* curr=root;
for(int i=0; i<s.length(); i++){
if(s[i]=='0'){
curr=curr->left;
}else{
curr=curr->right;
}
// reached leaf node
if(curr->left==NULL&&curr->right==NULL){
cout<<curr->data;
curr=root;
}
}
}
// function to store characters along with there huffman value in a hash table, here we have C++ STL map
void storecodes(node * root,string s)
{
if(root==NULL){
return;
}
if(root->data!=NULL){
codes[root->data]=s;
}
storecodes(root->left,s+'0');
storecodes(root->right,s+'1');
}
// STL priority queue to store heap tree, with respect to their heap root node value
priority_queue<node *,vector<node *>,compare>pq_hft; //to store huffman tree
// Function to build the Huffman tree and store it in priority queue declared above
void huffmancodes(int size) // creates huffman tree
{
node *left,*right,*top;
map<char,int>::iterator v;
for(v=freq.begin(); v!=freq.end(); v++){
pq_hft.push(new node(v->second,v->first));
}
while(pq_hft.size()!=1)
{
left=pq_hft.top();
pq_hft.pop();
right=pq_hft.top();
pq_hft.pop();
top = new node(left->freq+right->freq);
top->left=left;
top->right=right;
pq_hft.push(top);
}
storecodes(pq_hft.top(),"");
}
// Function to store map each character with its frequency in input string
void calcfreq(string s,int n)
{
for(int i=0; i<s.size(); i++){
freq[s[i]]++;
}
}
// Driver program to test above functions
int main(){
string s;
cin>>s;
string encodedstring;
calcfreq(s,s.length());
huffmancodes(s.length());
for(auto i:s){
encodedstring+=codes[i];
}
decode_huff(pq_hft.top(),encodedstring);
}