28 deleteAll(root->left);
29 deleteAll(root->right);
39 return l->freq > r->freq;
45void printCodes(
struct MinHeapNode* root,
const string& str) {
49 if (root->data !=
'$')
50 cout << root->data <<
": " << str <<
"\n";
52 printCodes(root->left, str +
"0");
53 printCodes(root->right, str +
"1");
58void HuffmanCodes(
const char data[],
const int freq[],
int size) {
62 priority_queue<MinHeapNode*, vector<MinHeapNode*>,
compare> minHeap;
64 for (
int i = 0; i < size; ++i)
68 while (minHeap.size() != 1) {
74 right = minHeap.top();
84 auto*
const top =
new MinHeapNode(
'$', left->freq + right->freq);
94 printCodes(minHeap.top(),
"");
95 deleteAll(minHeap.top());
100 char arr[] = {
'a',
'b',
'c',
'd',
'e',
'f'};
101 int freq[] = {5, 9, 12, 13, 16, 45};
103 int size =
sizeof(arr) /
sizeof(arr[0]);
105 HuffmanCodes(arr, freq, size);