TAHMID37

tahmids_tree

Feb 6th, 2021 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. /*  TAHMID RAHMAN
  2.     DAMIAN FOREVER
  3.      MATH LOVER
  4.     NEVER GIVE UP
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. #define pi acos(-1.0)
  9. #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  10. #define ll long long
  11. #define pb push_back
  12. #define fi first
  13. #define se second
  14. #define in insert
  15. #define mp make_pair
  16. #define GCD(a,b) __gcd(a,b);
  17. #define endl "\n"
  18. #define FRU freopen("out.txt","w",stdout)
  19. #define FRO freopen("in.txt","r",stdin)
  20. #define INFLL 9223372036854775807
  21. #define debug 0
  22. #define MAXN   100001
  23. #define ar array
  24. #define lb lower_bound
  25. #define ub upper_bound
  26. const int mxN=2e5;
  27. const int MOD=1e9+7;
  28. template<typename ForwardIterator, typename T>
  29. ForwardIterator first_less_than (ForwardIterator first, ForwardIterator last, T value)
  30. {auto it = std::lower_bound (first, last, value);
  31. return (it == first ? last : --it);}
  32. bool sortbysec(const pair<int,int> &a,const pair<int,int> &b)
  33. {
  34.     return (a.second < b.second);
  35. }
  36. #define debugxx(v) {for(auto x:v){cout<<x.fi<<" "<<x.se<<endl;}cout<<endl;}
  37. #define debugx(v){for(auto y:v) {cout<<y<<" ";}cout<<endl;}
  38.  
  39.  
  40. map<char,string>priff_codes;
  41.  
  42.  
  43. struct parent
  44. {
  45.     char ch;
  46.     double freq;
  47.  
  48.     parent *left;
  49.     parent *right;
  50.  
  51. };
  52.  
  53.  
  54.  
  55. string genarate_id(string s)
  56. {
  57.     string id="MYIDIS";
  58.     map<char,string>m;
  59.     m['0']="ZERO",m['1']="ONE",m['2']="TWO",m['3']="THREE",m['4']="FOUR",
  60.     m['5']="FIVE",m['6']="SIX",m['7']="SEVEN",m['8']="EIGHT",m['9']="NINE";
  61.     ll i;
  62.  
  63.     for(i=s.size()-4;i<s.size();i++)
  64.     {
  65.         id+=m[s[i]];
  66.     }
  67.  
  68.     return id;
  69.  
  70. }
  71.  
  72.  
  73. void traverse(struct parent *memo,string s)
  74. {
  75.  
  76.     if(memo->left==NULL&&memo->right==NULL)
  77.     {
  78.         //leaf node
  79.         priff_codes[memo->ch]=s;
  80.         return ;
  81.  
  82.     }
  83.  
  84.     //not a leaf node
  85.  
  86.  
  87.     //left_subtree <--
  88.     if(memo->left)
  89.         traverse(memo->left,s+'0');
  90.  
  91.  
  92.     //right_subtree -->
  93.     if(memo->right)
  94.         traverse(memo->right,s+'1');
  95.  
  96.  
  97. }
  98.  
  99.  
  100. bool sort_compo(struct parent *num1,struct parent *num2)
  101. {
  102.     if(num1->freq<num2->freq)
  103.         return true;
  104.     else
  105.         return false;
  106. }
  107.  
  108.  
  109. void priffix_code(string s)
  110. {
  111.     deque<struct parent*>nodes;
  112.  
  113.     map<char,ll>f;
  114.  
  115.     ll i;
  116.  
  117.     for(i=0;i<s.size();i++)
  118.     {
  119.         f[s[i]]++;
  120.     }
  121.  
  122.  
  123.     for(auto x:f)
  124.     {
  125.         struct parent *temp=new struct parent;
  126.         temp->ch=x.fi;
  127.         temp->freq=x.se*1.00/s.size()*1.00;
  128.         temp->left=NULL;
  129.         temp->right=NULL;
  130.         nodes.push_back(temp);
  131.     }
  132.  
  133.  
  134.  
  135.     sort(nodes.begin(),nodes.end(),sort_compo);
  136.  
  137.     /*for(i=0;i<nodes.size();i++)
  138.     {
  139.         cout<<nodes[i]->ch<<" "<<nodes[i]->freq<<endl;
  140.     }
  141.  
  142.     cout<<endl;*/
  143.  
  144.  
  145.     while(nodes.size()!=1)
  146.     {
  147.  
  148.         struct parent *temp1=new struct parent;
  149.         temp1->freq=nodes[0]->freq+nodes[1]->freq;
  150.         temp1->ch=max(nodes[0]->ch,nodes[1]->ch);
  151.         temp1->left=nodes[1];
  152.         temp1->right=nodes[0];
  153.  
  154.         nodes.pop_front();
  155.         nodes.pop_front();
  156.  
  157.         nodes.push_back(temp1);
  158.  
  159.         sort(nodes.begin(),nodes.end(),sort_compo);
  160.  
  161.     }
  162.  
  163.  
  164.     traverse(nodes[0],"");
  165.  
  166.  
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173. int main()
  174. {
  175.  
  176.     string stu_id;
  177.     cout<<"Step #1."<<"  "<<"Enter Student ID: ";
  178.     cin>>stu_id;
  179.     string id=genarate_id(stu_id);
  180.     priffix_code(id);
  181.     cout<<"Step #2."<<"  "<<"Generated String: "<<id<<endl;
  182.     cout<<"Step #3."<<"  "<<"Prefix Codes (GENERATED USING HUFFMAN CODING):"<<endl;
  183.     set<char>st;
  184.  
  185.     for(ll i=0;i<id.size();i++)
  186.     {
  187.         if(!st.count(id[i])){
  188.         cout<<"          "<<id[i]<<" :"<<"   "<<priff_codes[id[i]]<<endl;
  189.         st.in(id[i]);
  190.         }
  191.     }
  192.  
  193. }
  194.  
  195.  
  196.  
Add Comment
Please, Sign In to add comment