-
Notifications
You must be signed in to change notification settings - Fork 0
/
#261.cpp
68 lines (56 loc) · 1.81 KB
/
#261.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
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
struct Node {
char val;
Node* left;
Node* right;
Node(char c, Node* left = nullptr, Node* right = nullptr): val{c}, left{left}, right{right} {}
};
void printBT(const std::string& prefix, const Node* node, bool isLeft) {
if (node != nullptr) {
std::cout << prefix << (isLeft ? "├──" : "└──" ) << node->val << std::endl;
// enter the next tree level - left and right branch
printBT( prefix + (isLeft ? "│ " : " "), node->left, true);
printBT( prefix + (isLeft ? "│ " : " "), node->right, false);
}
}
void printBT(const Node* node) {
printBT("", node, false);
}
bool comparePair(const pair<int, Node*>& p1, const pair<int, Node*>& p2) {
return p1.first > p2.first;
}
template<typename T>
void popHeap(vector<T>& v) {
pop_heap(v.begin(), v.end(), comparePair);
v.pop_back();
}
template<typename T>
void pushHeap(vector<T>& v, T& t) {
v.push_back(t);
push_heap(v.begin(), v.end(), comparePair);
}
Node* huffmanEncoding(vector<char>& chars, vector<int>& freqs) {
vector<pair<int, Node*>> v;
for (int i = 0; i < chars.size(); i++) {
v.push_back(make_pair(freqs[i], new Node(chars[i])));
}
make_heap(v.begin(), v.end(), comparePair);
while (v.size() > 1) {
pair<int, Node*> p1 = v.front();
popHeap(v);
pair<int, Node*> p2 = v.front();
popHeap(v);
pair<int, Node*> newPair = make_pair(p1.first + p2.first, new Node('*', p1.second, p2.second));
pushHeap(v, newPair);
}
return v.front().second;
}
int main() {
vector<char> chars{'a', 'b', 'c', 'd', 'e'};
vector<int> freqs{1, 2, 3, 4, 5};
Node* HuffmanTree = huffmanEncoding(chars, freqs);
printBT(HuffmanTree);
}