This repository was archived by the owner on Nov 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHuffman.cpp
116 lines (106 loc) · 3.14 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
//
// algorithm - some algorithms in "Introduction to Algorithms", third edition
// Copyright (C) 2018 lxylxy123456
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef MAIN
#define MAIN
#define MAIN_Huffman
#endif
#ifndef FUNC_Huffman
#define FUNC_Huffman
#define EVENTS std::vector<Event<T>>
#include "utils.h"
#include "printtree.h"
#include "MaxHeap.cpp"
template <typename T>
class CNode {
public:
CNode(T d, size_t f): disp(d), freq(f), left(nullptr), right(nullptr) {}
CNode(T d, size_t f, CNode<T>* l, CNode<T>* r): disp(d), freq(f),
left(l), right(r) {}
friend std::ostream& operator<<(std::ostream& o, const CNode<T>& r) {
return o << r.disp;
}
bool operator<(const CNode<T>& rhs) const { return freq > rhs.freq; }
bool operator>(const CNode<T>& rhs) const { return freq < rhs.freq; }
T disp;
size_t freq;
CNode<T> *left, *right;
};
template <typename T>
class CNodeDesc {
public:
CNodeDesc(CNode<T>* p): node(p) {}
bool isNull() { return !node; }
String key() {
std::ostringstream os;
if (node->disp)
os << *node;
else
os << node->freq;
std::string ans = os.str();
if (node->left)
return String(ans);
else {
String ret(ans);
ret.data[0] = "\033[31m" + ret.data[0];
ret.data[ret.size() - 1] += "\033[0m";
return ret;
}
}
CNodeDesc<T> left() { return CNodeDesc<T>(node->left); }
CNodeDesc<T> right() { return CNodeDesc<T>(node->right); }
CNode<T> *node;
};
template <typename T>
CNode<T>* Huffman(std::vector<CNode<T>> C) {
size_t n = C.size();
PriorityQueue<CNode<T>> P;
for (typename std::vector<CNode<T>>::iterator i = C.begin();
i != C.end(); i++) {
P.push_back(*i);
P.MaxHeapInsert(*i);
}
for (size_t i = 1; i < n; i++) {
CNode<T>* x = new CNode<T>(P.HeapExtractMax());
P.pop_back();
CNode<T>* y = new CNode<T>(P.HeapExtractMax());
P.pop_back();
CNode<T> z('\0', x->freq + y->freq, x, y);
P.push_back(z);
P.MaxHeapInsert(z);
}
return new CNode<T>(P.HeapMaximum());
}
#endif
#ifdef MAIN_Huffman
int main(int argc, char *argv[]) {
const size_t n = get_argv(argc, argv, 1, 6);
// prepare data
std::vector<CNode<char>> C;
std::vector<size_t> freq_list;
random_integers<size_t>(freq_list, 1, n, n);
for (size_t i = 0; i < n; i++)
C.push_back(CNode<char>('a' + i, freq_list[i]));
output_integers(C, "\t");
output_integers(freq_list, "\t");
std::cout << std::endl;
// call function
CNode<char>* B = Huffman(C);
printTree(CNodeDesc<char>(B));
return 0;
}
#endif