-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffman_code.h
More file actions
102 lines (87 loc) · 2.54 KB
/
Copy pathhuffman_code.h
File metadata and controls
102 lines (87 loc) · 2.54 KB
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
#ifndef CPP_ALGORITHM_HUFFMAN_CODE_H
#define CPP_ALGORITHM_HUFFMAN_CODE_H
#include <map>
#include <queue>
#include <string>
namespace Huffman
{
/**
* \brief Node in full binary tree for Huffman code.
*/
struct HuffmanNode
{
HuffmanNode* left;
HuffmanNode* right;
int key;
char ch;
int freq;
HuffmanNode(const int key, const char ch, const int freq)
: left(nullptr), right(nullptr), key(key), ch(ch), freq(freq)
{
}
};
/**
* \brief Comparator for minimal heap builds
*/
class MinComparator
{
public:
bool operator()(const HuffmanNode* l, const HuffmanNode* r) const { return (l->freq > r->freq); }
};
/**
* \brief Huffman code algorithm.
* \param freq_map a map of char and frequency pairs
* \return root node
*/
HuffmanNode* HuffmanCode(const std::map<int, char>& freq_map);
/**
* \brief Tree traversal to get the Huffman code
* \param root root node that starts the tree traversal
* \param code prefix code
* \param result result of tree traversal
*/
void TraversalHuffmanCode(
const HuffmanNode* root,
const std::string& code,
std::map<char, std::string>& result);
}
// ----------------------------------------------------------------------------
inline Huffman::HuffmanNode* Huffman::HuffmanCode(const std::map<int, char>& freq_map)
{
std::priority_queue<HuffmanNode*, std::vector<HuffmanNode*>, MinComparator> min_queue;
for (auto [freq, ch] : freq_map)
{
min_queue.push(new HuffmanNode{freq, ch, freq});
}
while (static_cast<int>(min_queue.size()) != 1)
{
const auto left = min_queue.top();
min_queue.pop();
const auto right = min_queue.top();
min_queue.pop();
const int key = left->key + right->key;
auto node = new HuffmanNode{key, '-', left->freq + right->freq};
node->left = left;
node->right = right;
min_queue.push(node);
}
return min_queue.top();
}
// ----------------------------------------------------------------------------
inline void Huffman::TraversalHuffmanCode(
const HuffmanNode* root,
const std::string& code,
std::map<char, std::string>& result)
{
if (root == nullptr)
{
return;
}
if (root->ch != '-')
{
result.insert(std::pair(root->ch, code));
}
TraversalHuffmanCode(root->left, code + "0", result);
TraversalHuffmanCode(root->right, code + "1", result);
}
#endif