Advertisement
TAHMID37

saim_s_huffman

Feb 7th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <queue>
  5. #include <unordered_map>
  6. using namespace std;
  7. long long int i=0;
  8.  
  9. map<char,string>huffmanCode;
  10. string s;
  11.  
  12. string generate_id(string s)
  13. {
  14.     string id="MYIDIS";
  15.     map<char,string>mp;
  16.     mp['0']="ZERO",mp['1']="ONE",mp['2']="TWO",mp['3']="THREE",mp['4']="FOUR",mp['5']="FIVE",mp['6']="SIX",mp['7']="SEVEN",
  17.                                          mp['8']="EIGHT",mp['9']="NINE";
  18.  
  19.  
  20.     for(i=s.size()-4; i<s.size(); i++)
  21.     {
  22.         id+=mp[s[i]];
  23.     }
  24.     return id;
  25. }
  26.  
  27.  
  28. struct Node
  29. {
  30.     char ch;
  31.     int freq;
  32.     Node *left, *right;
  33. };
  34.  
  35. // Function to allocate a new tree node
  36. Node* getNode(char ch, int freq, Node* left, Node* right)
  37. {
  38.     Node* node = new Node();
  39.  
  40.     node->ch = ch;
  41.     node->freq = freq;
  42.     node->left = left;
  43.     node->right = right;
  44.  
  45.     return node;
  46. }
  47.  
  48.  
  49.  
  50. struct comp
  51. {
  52.     bool operator()(Node* l, Node* r)
  53.     {
  54.         return l->freq > r->freq;
  55.     }
  56. };
  57.  
  58.  
  59.  
  60.  
  61. void  encode(Node* root, string str)
  62. {
  63.     if (root ==NULL)
  64.         return;
  65.  
  66.     if (!root->left && !root->right)
  67.     {
  68.         huffmanCode[root->ch] = str;
  69.     }
  70.  
  71.     encode(root->left, str + "0");
  72.     encode(root->right, str + "1");
  73. }
  74.  
  75.  
  76.  
  77. void buildHuffmanTree(string text)
  78. {
  79.     map<char, int> freq;
  80.     for (char ch: text)
  81.     {
  82.         freq[ch]++;
  83.     }
  84.  
  85.     priority_queue<Node*, vector<Node*>, comp> pq;
  86.  
  87.     for (auto pair: freq)
  88.     {
  89.         pq.push(getNode(pair.first, pair.second, NULL, NULL));
  90.     }
  91.  
  92.     while (pq.size() != 1)
  93.     {
  94.         Node *left = pq.top();
  95.         pq.pop();
  96.         Node *right = pq.top();
  97.         pq.pop();
  98.         int sum = left->freq + right->freq;
  99.         pq.push(getNode('\0', sum, left, right));
  100.     }
  101.  
  102.     Node* root = pq.top();
  103.  
  104.     encode(root, "");
  105.  
  106.     int frequ[26]={0};
  107.  
  108.     string st=generate_id(s);
  109.     string temp;
  110.     for(i=0;i<st.size();i++)
  111.     {
  112.         if(!frequ[st[i]-'A'])
  113.         {
  114.             temp+=st[i];
  115.             frequ[st[i]-'A']++;
  116.         }
  117.     }
  118.  
  119.     cout<<endl;
  120.  
  121.  
  122.     for(auto x:temp)
  123.     {
  124.         cout<<"        "<<x<<":"<<"  "<<huffmanCode[x]<<endl;
  125.     }
  126.  
  127. }
  128.  
  129. int main()
  130. {
  131.  
  132.     cout<<"Step #1.Enter Student ID:";
  133.     cin>>s;
  134.     cout<<"Step #2.Generated String:"<<generate_id(s)<<endl;
  135.     cout<<"Step #3.Prefix Codes(Generated Using Huffman Coding):";
  136.     buildHuffmanTree(generate_id(s));
  137.     return 0;
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement