0% found this document useful (0 votes)
63 views

Huffman Code

This document contains C++ code to implement a Huffman coding tree algorithm. It defines a HuffmanNode struct to represent nodes in the tree, with character, frequency, and left/right child pointers. It also defines functions to build the Huffman tree from a string by calculating character frequencies, and to generate Huffman codes by traversing the tree. The main function calls these to build the tree for a sample string and output the generated codes.

Uploaded by

fuyf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Huffman Code

This document contains C++ code to implement a Huffman coding tree algorithm. It defines a HuffmanNode struct to represent nodes in the tree, with character, frequency, and left/right child pointers. It also defines functions to build the Huffman tree from a string by calculating character frequencies, and to generate Huffman codes by traversing the tree. The main function calls these to build the tree for a sample string and output the generated codes.

Uploaded by

fuyf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <queue>
#include <unordered_map>
#include <vector>

using namespace std;

// A Huffman tree node


struct HuffmanNode {
char character;
int frequency;
HuffmanNode *left, *right;
HuffmanNode(char c, int f) : character(c), frequency(f),
left(nullptr), right(nullptr) {}
};

// Compare two Huffman nodes based on their frequency


struct CompareHuffmanNodes {
bool operator()(HuffmanNode* a, HuffmanNode* b) {
return a->frequency > b->frequency;
}
};

// Traverse the Huffman tree and generate Huffman codes


void generateHuffmanCodes(HuffmanNode* root, string code,
unordered_map<char, string>& huffmanCodes) {
if (root == nullptr) {
return;
}
if (root->character != '\0') {
huffmanCodes[root->character] = code;
}
generateHuffmanCodes(root->left, code + "0", huffmanCodes);
generateHuffmanCodes(root->right, code + "1", huffmanCodes);
}

// Build Huffman tree for given string


HuffmanNode* buildHuffmanTree(string input) {
// Calculate frequency of each character
unordered_map<char, int> charFreq;
for (char c : input) {
charFreq[c]++;
}
// Create min heap of Huffman nodes
priority_queue<HuffmanNode*, vector<HuffmanNode*>,
CompareHuffmanNodes> minHeap;
for (auto& p : charFreq) {
minHeap.push(new HuffmanNode(p.first, p.second));
}
// Build Huffman tree from min heap
while (minHeap.size() > 1) {
HuffmanNode *left = minHeap.top(); minHeap.pop();
HuffmanNode *right = minHeap.top(); minHeap.pop();
HuffmanNode *parent = new HuffmanNode('\0', left->frequency +
right->frequency);
parent->left = left;
parent->right = right;
minHeap.push(parent);
}
return minHeap.top();
}

// Driver code
int main() {
string input = "ACCEBFFFFAAXXBLKE";
HuffmanNode* root = buildHuffmanTree(input);
unordered_map<char, string> huffmanCodes;
generateHuffmanCodes(root, "", huffmanCodes);
for (auto& p : huffmanCodes) {
cout << p.first << ": " << p.second << endl;
}
return 0;
}

You might also like