Advertisement
TAHMID37

sami_huffman

Feb 6th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. map<char, string> prefixCodes;
  5.  
  6. struct root {
  7.     double frequency;
  8.     char maxVal;
  9.     struct root* left;
  10.     struct root* right;
  11. };
  12.  
  13. bool rootComp(struct root* r1, struct root* r2){
  14.     /*   My bad sorting M < I in exam script  (*_ _)人 */
  15.     if(r1->maxVal == 'M' && r2->maxVal == 'I') return true;
  16.  
  17.     if(r1->frequency == r2->frequency) return r1->maxVal < r2->maxVal;
  18.     return r1->frequency < r2->frequency;
  19. }
  20.  
  21. void dfs(struct root *cur, string pCode){
  22.     if(!cur->left && !cur->right){
  23.         prefixCodes[cur->maxVal] = pCode;
  24.         return;
  25.     }
  26.     if(cur->left) dfs(cur->left, pCode + "0");
  27.     if(cur->right) dfs(cur->right, pCode + "1");
  28. }
  29.  
  30. string generateString(string id) {
  31.     stringstream ss;
  32.     ss<<"MYIDIS";
  33.     char digWord[10][6] =
  34.     {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};
  35.     for(int i = id.size() - 4; i < id.size(); i++) {
  36.         ss<<digWord[id[i]-'0'];
  37.     }
  38.     return ss.str();
  39. }
  40.  
  41. void generatePrefixCodes(string str) {
  42.     map<char, double> frequency;
  43.     vector<struct root*> roots;
  44.     for(auto c : str){
  45.         if(frequency.count(c)) frequency[c] += 1;
  46.         else frequency[c] = 1;
  47.     }
  48.     for(auto p : frequency){
  49.         struct root* newRoot = new struct root;
  50.         newRoot->frequency = p.second / str.size();
  51.         newRoot->maxVal = p.first;
  52.         newRoot->left = nullptr;
  53.         newRoot->right = nullptr;
  54.         roots.push_back(newRoot);
  55.     }
  56.  
  57.     sort(roots.begin(), roots.end(), rootComp);
  58.  
  59.     while(roots.size() > 1){
  60.         struct root* newRoot = new struct root;
  61.         newRoot->frequency = roots[0]->frequency + roots[1]->frequency;
  62.         newRoot->maxVal = max(roots[0]->maxVal, roots[1]->maxVal);
  63.  
  64.         if(rootComp(roots[0], roots[1])){
  65.             newRoot->left = roots[1];
  66.             newRoot->right = roots[0];
  67.         }
  68.         else{
  69.             newRoot->left = roots[0];
  70.             newRoot->right = roots[1];
  71.         }
  72.         roots.push_back(newRoot);
  73.         roots.erase(roots.begin());
  74.         roots.erase(roots.begin());
  75.         sort(roots.begin(), roots.end(), rootComp);
  76.     }
  77.     dfs(roots[0], "");
  78. }
  79.  
  80. int main() {
  81.     string id, str;
  82.     cout<<"Enter Student ID : ";
  83.     cin>>id;
  84.     str = generateString(id);
  85.     cout<<"Generated String: "<<str<<"\n";
  86.     generatePrefixCodes(str);
  87.     cout<<"Prefix Codes (Generated Using Huffman Coding):\n";
  88.     for(auto p : prefixCodes){
  89.         cout<<p.first<<" : "<<p.second<<"\n";
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement