Mahmoud_Hawara

Huffman Code

Dec 4th, 2025
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Node {
  5.     int freq;
  6.     char c;
  7.     Node* left;
  8.     Node* right;
  9.  
  10.     Node(int freq) {
  11.         this->freq = freq;
  12.         this->left = nullptr;
  13.         this->right = nullptr;  
  14.     }
  15.  
  16.     Node(char c, int freq) {
  17.         this->c = c;
  18.         this->freq = freq;
  19.         this->left = nullptr;
  20.         this->right = nullptr;  
  21.     }
  22. };
  23.  
  24. struct comp {
  25.     bool operator()(Node* a, Node* b) {
  26.         return a->freq > b->freq;
  27.     }
  28. };
  29.  
  30. bool comp2(pair<char, string> a, pair<char, string> b) {
  31.     if (a.second.size() == b.second.size()) {
  32.         return a.second < b.second;
  33.     }
  34.     return a.second.size() < b.second.size();
  35. }
  36.  
  37. void dfs(Node* curr, string code, vector<pair<char, string>>& ans) {
  38.     // if the current node is leaf
  39.     if (curr->left == nullptr && curr->right == nullptr) {
  40.         ans.push_back({curr->c, code});
  41.         return;
  42.     }
  43.     dfs(curr->left, code + "0", ans);
  44.     dfs(curr->right, code + "1", ans);    
  45. }
  46.  
  47. // assume string include lowercase characters only
  48. vector<pair<char, string>> huffmanCode(string s) {
  49.     int freq[26] = {};
  50.     for (int i = 0; s[i]; i++) {
  51.         freq[s[i] - 'a']++;
  52.     }  
  53.     priority_queue<Node*, vector<Node*>, comp> pq;
  54.     for (int i = 0; i < 26; i++) {
  55.         if (freq[i]) {
  56.             Node* newNode = new Node('a' + i, freq[i]);
  57.             pq.push(newNode);
  58.         }
  59.     }
  60.     while (pq.size() >= 2) {
  61.         auto fst = pq.top();
  62.         pq.pop();
  63.         auto snd = pq.top();
  64.         pq.pop();
  65.         Node* newNode = new Node(fst->freq + snd->freq);
  66.         newNode->left = fst;
  67.         newNode->right = snd;
  68.         pq.push(newNode);
  69.     }
  70.     Node* root = pq.top();
  71.     vector<pair<char, string>> ans;
  72.     dfs(root, "", ans);
  73.     sort(ans.begin(), ans.end(), comp2);
  74.     return ans;
  75. }
  76.  
  77. void solve()
  78. {
  79.     string s = "aaabbbccccccccccccccccccccccccccccccccccccc";
  80.     auto prefixCodes = huffmanCode(s);
  81.     string code[26];
  82.     map<string, char> decode;
  83.     for (auto [c, cooode] : prefixCodes) {
  84.         code[c - 'a'] = cooode;
  85.         decode[cooode] = c;
  86.     }
  87.     string compressed;
  88.     for (auto c : s) {
  89.         compressed += code[c - 'a'];
  90.     }
  91.     cout << "=> Compressed String: " << compressed << '\n';
  92.     string original;
  93.     string temp;
  94.     for (auto c : compressed) {
  95.         temp.push_back(c);
  96.         if (decode.find(temp) != decode.end()) {
  97.             original.push_back(decode[temp]);
  98.             temp = "";
  99.         }
  100.     }
  101.     cout << "=> Original String: " << original << '\n';
  102. }
  103.  
  104. int main()
  105. {
  106.     solve();
  107.     return 0;
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment