-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.huffmanCoding.cpp
66 lines (61 loc) · 1.37 KB
/
2.huffmanCoding.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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
class Node{
public:
char data;
int freq;
Node *left,*right;
Node(char data,int freq){
this->data=data;
this->freq=freq;
left=right=NULL;
}
};
class cmp{
public:
bool operator()(Node* a,Node* b){
return a->freq>b->freq;
}
};
void printTree(Node *head,string str=""){
if(head==NULL)return;
if(head->left==NULL && head->right==NULL){
cout<<head->data<<" -> "<<str<<endl;
return;
}
printTree(head->left,str+"0");
printTree(head->right,str+"1");
}
Node* buildTree(vector<char>&arr,vector<int>&frequency){
priority_queue<Node*,vector<Node*>,cmp>pq;
for(int i=0;i<arr.size();i++){
pq.push(new Node(arr[i],frequency[i]));
}
while(pq.size()>1){
Node *a=pq.top();
pq.pop();
Node *b=pq.top();
pq.pop();
Node *c=new Node('$',a->freq+b->freq);
c->left=a;
c->right=b;
pq.push(c);
}
return pq.top();
}
int main(){
int n;
cin>>n;
vector<char>arr(n);
vector<int>frequency(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=0;i<n;i++){
cin>>frequency[i];
}
Node *head=buildTree(arr,frequency);
printTree(head);
return 0;
}