Guest User

Untitled

a guest
Dec 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. int val;
  5. char ch;
  6. node *left, *right;
  7. };
  8. node* create_n(){
  9. node* ptr= new node;
  10. ptr->val= 0;
  11. ptr->ch= '@';
  12. ptr->left= NULL;
  13. ptr->right= NULL;
  14. return ptr;
  15. }
  16. node* create_s(char ch){
  17. node* r= create_n();
  18. r->val= 1;
  19. r->left= create_n();
  20. r->right= create_n();
  21. r->right->val= 1;
  22. r->right->ch= ch;
  23. return r;
  24. }
  25. void update_count(node* r){
  26. if (r->left== NULL || r->right== NULL){
  27. return;
  28. }
  29. update_count(r->left);
  30. update_count(r->right);
  31. r->val= r->left->val+ r->right->val;
  32. }
  33.  
  34. vector<vector<node*> > lot(node* r){
  35. if (r== NULL){
  36. vector< vector< node*> > v;
  37. return v;
  38. }
  39. vector<vector< node*> > v;
  40. queue<node*> q;
  41. q.push(r);
  42. while(!q.empty()){
  43. int nodecount= q.size();
  44. vector<node*> v1;
  45. while (nodecount--){
  46. node* ptr= q.front();
  47. q.pop();
  48. v1.push_back(ptr);
  49. if (ptr->left!= NULL){
  50. q.push(ptr->left);
  51. }
  52. if(ptr->right!= NULL){
  53. q.push(ptr->right);
  54. }
  55. }
  56. v.push_back(v1);
  57. }
  58. reverse(v.begin(), v.end());
  59. return v;
  60. }
  61. bool chk_ascending(node* r){
  62. vector<vector<node*> > l= lot(r);
  63. vector<node* > li;
  64. for(int i=0; i<l.size(); i++){
  65. for(int j=0; j< l[i].size(); j++){
  66. li.push_back(l[i][j]);
  67. }
  68. }
  69. for(int i=1; i<li.size(); i++){
  70. if (li[i-1]->val> li[i]->val){
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. node* swapping_helper(node* r, node* nts1, node* nts2){
  77. if (r== NULL){
  78. return NULL;
  79. }
  80. if (r== nts1){
  81. return nts2;
  82. }
  83. if(r== nts2){
  84. return nts1;
  85. }
  86. r->left= swapping_helper(r->left, nts1, nts2);
  87. r->right= swapping_helper(r->right, nts1, nts2);
  88. return r;
  89. }
  90. node* swapping(node* r){
  91. vector<vector<node*> > l= lot(r);
  92. vector<node* > li;
  93. for(int i=0; i<l.size(); i++){
  94. for(int j=0; j< l[i].size(); j++){
  95. li.push_back(l[i][j]);
  96. }
  97. }
  98. for(int i=1; i<li.size(); i++){
  99. if (li[i-1]->val> li[i]->val){
  100. node* nts1= li[i-1];
  101. node* nts2= NULL;
  102. int j=i;
  103. while(j<li.size()){
  104. if (li[j]->val== li[i-1]->val -1){
  105. nts2= li[j];
  106. }
  107. j++;
  108. }
  109. node* ro= swapping_helper(r, nts1, nts2);
  110. return ro;
  111. }
  112. }
  113. return r;
  114. }
  115. void codewords(node* r, string code){
  116. if(r== NULL){
  117. return;
  118. }
  119. if(r->ch!='@'){
  120. cout<<r->ch<<" "<<code<<endl;
  121. return ;
  122. }
  123. codewords(r->left, code+"0");
  124. codewords(r->right, code+"1");
  125. }
  126.  
  127. int main(){
  128. node* root= NULL;
  129. node* zero_parent= NULL;
  130. string s;
  131. cout<<"Enter text: ";
  132. getline(cin, s);
  133. unordered_map<char, node*> ar;
  134. unordered_map<char, int> ar1;
  135. for(int i=0; i<s.length(); i++){
  136. if (root== NULL){
  137. root= create_s(s[i]);
  138. zero_parent= root;
  139. ar[s[i]]= zero_parent->right;
  140. ar1[s[i]]= 1;
  141. cout<<"After adding "<<s[i]<<"\n";
  142. codewords(root, "");
  143. cout<<endl;
  144. continue;
  145. }
  146. if(ar1.find(s[i])!= ar1.end()){
  147. ar[s[i]]->val+= 1;
  148. update_count(root);
  149. }else{
  150. zero_parent->left= create_s(s[i]);
  151. zero_parent= zero_parent->left;
  152. ar1[s[i]]= 1;
  153. ar[s[i]]= zero_parent->right;
  154.  
  155. update_count(root);
  156. }
  157. while (chk_ascending(root)){
  158. root= swapping(root);
  159. update_count(root);
  160. }
  161. cout<<"After adding "<<s[i]<<"\n";
  162. codewords(root, "");
  163. cout<<endl;
  164. }
  165. cout<<"Codewords: \n";
  166. codewords(root, "");
  167. return 0;
  168. }
Add Comment
Please, Sign In to add comment