-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffmantree.cpp
184 lines (148 loc) · 4.26 KB
/
Huffmantree.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include<vector>
#include<string>
using namespace std;
struct output{
char name;
string value;
output(){
name = ' ';
value = "";
}
};
struct huffmannode{
int freq;
char name;
huffmannode * left;
huffmannode * right;
huffmannode(){
name = ' ';
freq = 10;
left = nullptr;
right = nullptr;
}
huffmannode(int data){
name = ' ';
freq = data;
left = nullptr;
right = nullptr;
}
};
void printvec(huffmannode * vec, int size){
for (int i = 0; i<size; i++){
cout<<vec[i].freq<<";";
}
cout<<endl;
}
void swap(huffmannode* vec, int pos, int pos2){
huffmannode temp = vec[pos];
vec[pos] = vec[pos2];
vec[pos2] = temp;
}
void heapify(huffmannode* vec, int index, int size){
int max = index;
int left = (index*2)+1;
int right = (index*2)+2;
if(left<size && vec[left].freq < vec[max].freq){
max = left;
}
if(right<size && vec[right].freq < vec[max].freq){
max = right;
}
if(max!= index){
swap(vec, max, index);
heapify(vec, max, size);
}
}
huffmannode* minheapify(huffmannode* vec, int size){
for(int i = size/2-1; i>=0; i--){//heapify every thing so it is a maxheap. i start at size/2 -1
heapify(vec, i , size);//anything under that is leaf nodes
}
return vec;
}
void heapinsert(huffmannode* vec,huffmannode key, int i){
vec[i]= key;
while(vec[i/2].freq > vec[i].freq){
swap(vec, i/2, i);
i = i/2;
}
}
huffmannode* heapextract(huffmannode* vec,int i){
huffmannode* data = new huffmannode[1];
data->freq = vec[0].freq;
data->right = vec[0].right;
data->left = vec[0].left;
data->name = vec[0].name;
swap(vec, i, 0);
heapify(vec, 0, i);
return data;
}
huffmannode huffmancode(huffmannode* vec,int size){
for(int i = 1; i<size; i++){
//we have to use malloc becuase if we have local variable,
// in the next itration the node might be overwriten
//then the pointer are no longer pointeing at the right thing
huffmannode *z, *x, *y;
x = heapextract(vec, size-i);
y = heapextract(vec, size-i-1);
z = new huffmannode[1];
z->freq = x->freq+y->freq;
z->left= x;
z->right = y;
heapinsert(vec, *z, size-i-1);
}
return vec[0];
}
void generate_output(huffmannode* node, vector<output>& outvec, char* code, int len) {
if (node->left == nullptr && node->right == nullptr) {
// leaf node, print or store the code
output a;
a.name = node->name;
for (int i = 0; i < len; i++) {
a.value += code[i];
}
outvec.push_back(a);
} else {
// non-leaf node, recursively traverse its children
code[len] = '0';
generate_output(node->left, outvec, code, len + 1);
code[len] = '1';
generate_output(node->right, outvec, code, len + 1);
}
}
void sort_output(vector<output>& outvec) {
// Sort using bubble sort algorithm
int n = outvec.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (outvec[j].name > outvec[j + 1].name) {
output temp = outvec[j];
outvec[j] = outvec[j + 1];
outvec[j + 1] = temp;
}
}
}
}
int main(int argc, char** argv) {
// getting the input
int size = 6;
huffmannode* freq = new huffmannode[6];
char names[] = {'A', 'B', 'C', 'D', 'E', 'F'};
for (int i = 0; i < size; i++) {
huffmannode data;
cin >> data.freq;
data.name = names[i];
freq[i] = data;
}
freq = minheapify(freq, size);
huffmannode tree = huffmancode(freq, size);
char* code = new char[size];
vector<output> outvec;
generate_output(&tree, outvec, code, 0);
sort_output(outvec);
for (int i = 0; i < 6; i++) {
output k = outvec[i];
cout << k.name << ":" << k.value << endl;
}
return 0;
}