-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhuffman_coding.cpp
More file actions
131 lines (113 loc) · 2.56 KB
/
Copy pathhuffman_coding.cpp
File metadata and controls
131 lines (113 loc) · 2.56 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
#include <iostream>
#include <stdlib.h>
/*
*STL headers
*/
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>
using namespace std;
#define PN cout<<'\n'
#define PI 3.1415926535
#define MOD 1000000007
struct node
{
struct node *right;
struct node *left;
//struct node *parent = NULL;
int freq;
char ch;
}*root;
struct comp_func{
bool operator()(const struct node& a,const struct node& b) const{
return a.freq > b.freq;
}
};
map<char, int> freq_table;
vector<struct node> x, y;
struct node extract_min(vector<struct node> &v){
struct node temp(v.front());
pop_heap (v.begin(),v.end(), comp_func());
v.pop_back();
cout<<"Min Heap Top Element: ---"<<temp.ch<<" "<<temp.freq<<"\n";;
return temp;
}
void construct_tree(){
int n = freq_table.size();
vector<struct node> V;
for (map<char, int>::iterator i = freq_table.begin(); i != freq_table.end(); ++i)
{
struct node *N =(struct node*) malloc(sizeof(struct node) );
N->freq = i->second;
N->ch = i->first;
N->left = NULL;
N->right = NULL;
V.push_back(*N);
// cout<<"s\n";
}
x.resize(n);
y.resize(n);
make_heap(V.begin(), V.end(), comp_func());
for(unsigned i = 0; i < n - 1; ++i) {
struct node *N =(struct node*) malloc(sizeof(struct node) );
x[i] = extract_min(V);
N->left = &x[i];
y[i] = extract_min(V);
N->right = &y[i];
N->freq = x[i].freq + y[i].freq;
N->ch = '#';
if(V.size() == 0) {
root = N;
return;
}
V.push_back(*N);
push_heap (V.begin(),V.end(), comp_func());
// cout<<"d\n";
}
}
void decode(struct node *ptr, vector<int> &v, int top){
if(!ptr->left && !ptr->right){
cout<<"Code for \'"<<ptr->ch<<"\': ";
copy(v.begin(), v.begin()+top,
ostream_iterator<int>(cout," "));
cout<<"\n";
return;
}
// cout<<ptr->freq;cout<<"\n";
if(ptr->left) {
v[top] = 0;
// int t; cin>>t;
// cout<<'\n'<<ptr->ch<<" --- "<<ptr->freq<<'\n';
decode(ptr->left, v, top+1);
}
if(ptr->right) {
v[top] = 1;
// int t; cin>>t;
// cout<<'\n'<<ptr->ch<<" -*-*- "<<ptr->freq<<'\n';
decode(ptr->right, v, top+1);
}
}
int main()
{
int max_height = 20;
vector<int> arr(20);
string s;
cin>>s;
for(unsigned i = 0; i < s.size(); ++i) {
if(freq_table.find(s[i]) == freq_table.end())
freq_table.insert(make_pair(s[i], 1));
else
freq_table[s[i]]++;
// cout<<"sd\n";
}
cout<<"\n\tFreq Table: \n";
for (map<char, int>::iterator i = freq_table.begin(); i != freq_table.end(); ++i){
cout<<"\t"<<i->first<<" --- "<<i->second<<'\n';
}
cout<<"\n";
construct_tree();
cout<<'\n';
decode(root, arr, 0);
return 0;
}