Huffmann Algo
Huffmann Algo
Huffmann Algo
#include <string>
#include <queue>
#include <unordered_map>
using namespace std;
int total = 0;
// A Tree node
struct Node
{
char ch;
int freq;
Node *left, *right;
};
node->ch = ch;
node->freq = freq;
node->left = left;
node->right = right;
return node;
}
struct comp
{
bool operator()(Node* l, Node* r)
{
// highest priority item has lowest frequency
return l->freq > r->freq;
}
};
index++;
if (str[index] =='0')
decode(root->left, index, str);
else
decode(root->right, index, str);
}
}
for (auto pair: huffmanCode ) {
total = total+pair.second.length() * freq.at(pair.first);
cout << "\nOriginal string was :\n" << text << '\n';
int main()
{
string text = "Huffman coding is a data compr This is a big one. Depending upon where you place your comma, your sentence could
convey an entirely different meaning. Like in this sentenceession algorithm.";
cout<<"\n";
buildHuffmanTree(text);
cout<<"\n";
cout<<"Length of UnCompressed text:"<<text.length()*8<<endl;
cout<<"Length of Compressed text:"<<total<<endl;