-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman_encoding.cpp
104 lines (98 loc) · 2.38 KB
/
huffman_encoding.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
#include "bits/stdc++.h"
using namespace std;
// Node structure for Huffman Tree
struct MinHeap
{
char cur;
int freq;
MinHeap* left;
MinHeap* right;
MinHeap(char cur = '$',int freq = 0)
{
left = right = NULL;
this->cur = cur;
this->freq = freq;
}
};
// Comparator function for insertion in Huffman Tree
struct cmp
{
// As priority_queue is a max heap rather than min-heap,
// invert the meaning of the < operator,
// in order to get lower probabilities at the top
bool operator()(MinHeap *l,MinHeap *r)
{ return l->freq > r->freq; }
};
map<char, string> code_of_char;
map<string, char> char_from;
priority_queue<MinHeap*, vector<MinHeap*>, cmp> heap;
void Huffman(map<char, int> &frequency)
{
MinHeap *left,*right,*top;
for(auto i : frequency)
heap.push(new MinHeap(i.first,i.second));
while(heap.size() not_eq 1)
{
left = heap.top();
heap.pop();
if(heap.size())
{
right = heap.top();
heap.pop();
}
else right = NULL;
top = new MinHeap('$',left->freq + right->freq);
top->left = left;
top->right = right;
heap.push(top);
}
}
void Encode(MinHeap* node, string code)
{
if(not node)return;
if(node->cur != '$')
{
code_of_char[node->cur] = code;
char_from[code] = node->cur;
}
Encode(node->left,code+"0");
Encode(node->right,code+"1");
}
string Decode()
{
// Now that we've encoded string and now we got to take our string back, so we've their back code as well.
string till_now = "";
string decoded_string = "";
for(char cur : encoded_string)
{
till_now += cur;
if(char_from.find(till_now) != char_from.end())
{
decoded_string += char_from[till_now];
till_now = "";
}
}
return decoded_string;
}
int main()
{
string given_text;
cin>>given_text;
// Storing frequency of every character
map<char, int> frequency;
for(char cur : given_text)
frequency[cur]++;
// Creating Huffman Tree
Huffman(frequency);
// Huffman Tree is ready, now we need to find proper code for each character.
Encode(heap.top(),"");
// Till now we've code for every character in string, so we can convert the given string into their following Huffman Code
string encoded_string = "";
for(char cur : given_text)
encoded_string += code_of_char[cur];
cout<<encoded_string<<endl;
// Decoded string is finally obtained from the encoded string, which should be same from the given string.
string decoded_string = Decode();
cout<<decoded_string<<endl;
return 0;
}