-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.cpp
120 lines (105 loc) · 1.64 KB
/
huffman.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
#include <iostream>
#include <vector>
#define N 5
using namespace std;
struct node{
int val;
int freq;
node *left;
node *right;
};
class queue{
public:
vector < node * > arr;
int size;
queue(){
size=0;
}
void heapify (int i){
int min = i;
if(i*2+1 < size && arr[i*2+1]->freq < arr[i]->freq)
min = i*2+1;
if(i*2+2 < size && arr[i*2+2]->freq < arr[min]->freq)
min = i*2+2;
if(i!=min){
node *tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
heapify(min);
}
}
void minheap(){
for(int i=(size-1)/2; i>=0; i--){
heapify(i);
}
}
node* min(){
minheap();
return arr[0];
}
void pop(){
arr.erase(arr.begin());
size--;
}
void push(node *l, node *r){
node *n = new node();
n->left = l;
n->right = r;
n->freq = l->freq + r->freq;
arr.push_back(n);
size++;
}
void insert(int num){
node *n = new node();
n->left = NULL;
n->right = NULL;
n->freq = num;
arr.push_back(n);
size++;
}
int length(){
return size;
}
void printarr(){
for(int i=0; i<size; i++){
cout<<arr[i]->freq<<" ";
}
cout<<endl;
}
void huffman(){
while(size > 1){
node *l = min();
pop();
node *r = min();
pop();
push(l, r);
}
}
};
void print(node* root, string &s){
if(root!=NULL){
if(root->left == NULL && root->right == NULL)
cout<<root->freq<<" "<<s<<endl;
s += "0";
print(root->left, s);
s.erase(s.length()-1);
s += "1";
print(root->right, s);
s.erase(s.length() - 1);
}
}
int main()
{
queue q;
string s("");
int freq;
for(int i=0; i<N; i++){
cin>>freq;
q.insert(freq);
}
q.huffman();
node *r = q.arr[0];
print(r, s);
cout<<endl;
return 0;
}