-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhufmann.cpp
More file actions
167 lines (141 loc) · 3.8 KB
/
hufmann.cpp
File metadata and controls
167 lines (141 loc) · 3.8 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
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
///////////////////////////////////////////////////////////
// STRUCTURE NODE
///////////////////////////////////////////////////////////
typedef struct node{
char c;
int freq;
node* left;
node* right;
node(): c('\0'){
left = NULL;
right=NULL;
}
}node;
///////////////////////////////////////////////////////////
// GLOBAL VARIABLES
///////////////////////////////////////////////////////////
vector <node *> Arr;
vector <int> pst;
///////////////////////////////////////////////////////////
// FUNCTION TO DECREASE VALUE
//////////////////////////////////////////////////////////
void decrease_val( int i, node* val){
Arr[ i ] = val;
while( i > 0 and Arr[ i/2 ]->freq > Arr[ i ]->freq)
{
swap(Arr[ i/2 ], Arr[ i ]);
i = i/2;
}
}
///////////////////////////////////////////////////////////
// FUNCTION TO INSERT VALUE
//////////////////////////////////////////////////////////
void insert_val(node* val){
node *tmp=new node;
tmp->freq = 99999999;
//tmp->next = NULL;
Arr.push_back(tmp);
decrease_val(Arr.size() - 1, val);
}
///////////////////////////////////////////////////////////
// MIN HEAPIFY
///////////////////////////////////////////////////////////
void min_heapify(int i){
int N=Arr.size()-1;
int left = 2*i;
int right = 2*i+1;
int smallest;
if(left<= N and Arr[left]->freq < Arr[i]->freq )
smallest = left;
else
smallest = i;
if(right <= N and Arr[right]->freq < Arr[smallest]->freq )
smallest = right;
if(smallest != i )
{
swap (Arr[i] , Arr[smallest]);
min_heapify (smallest);
}
}
///////////////////////////////////////////////////////////
// EXTRACT MINIMUM
///////////////////////////////////////////////////////////
node* extract_minimum ()
{
int length = Arr.size()-1;
node* min = Arr[0];
//Arr.remove(min);
Arr[0] = Arr[length];
Arr.pop_back();
length = length -1;
min_heapify(0);
return min;
}
///////////////////////////////////////////////////////////
// HUFFMANN
///////////////////////////////////////////////////////////
node* huffmann(char alpha[],int freq[],int n){
node* tmp=new node;
tmp->c = alpha[0];
tmp->freq = freq[0];
Arr.push_back(tmp);
//delete [] tmp;
for(int i=1;i<n;i++){
node* tmp=new node;
tmp->c = alpha[i];
tmp->freq = freq[i];
insert_val(tmp);
//delete [] tmp;
}
for(int i=1;i<=n-1;i++){
node *tmp = new node;
tmp->left = extract_minimum();
//cout<<"left = "<<tmp->left->c<<" "<<tmp->left->freq<<endl;
tmp->right = extract_minimum();
//cout<<"right = "<<tmp->right->c<<" "<<tmp->right->freq<<endl;
tmp->freq = tmp->left->freq+tmp->right->freq;
insert_val(tmp);
}
return extract_minimum();
}
///////////////////////////////////////////////////////////
// POST ORDER ROUTINE
///////////////////////////////////////////////////////////
void post(node *root){
if(root->left){
pst.push_back(0);
post(root->left);
}
if(root->right){
pst.push_back(1);
post(root->right);
}
if(root->c=='\0'){
//pst.pop_back();
}
else{
cout<<root->c<<" = ";
for(int i=0;i<pst.size();i++)cout<<pst[i];
cout<<endl;
}
pst.pop_back();
return;
}
//////////////////////////////////////////////////////
// DRIVER FUNCTION
/////////////////////////////////////////////////////
int main(void){
int n;
cout<<"Enter the no. of variables\n";
cin>>n;
char s[n];
int freq[n];
scanf("%s",s);
for(int i=0;i<n;i++)cin>>freq[i];
node* root= huffmann(s,freq,n);
post(root);
}