ccbeginner

HW_3_2

May 7th, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string code[26];
  5. struct tree_node{
  6. char ch;
  7. int val;
  8. tree_node *lch, *rch;
  9. tree_node(int v, char c){
  10. ch = c;
  11. val = v;
  12. lch = rch = nullptr;
  13. }
  14. tree_node(int v, char c, tree_node *l, tree_node *r){
  15. ch = c;
  16. val = v;
  17. lch = l;
  18. rch = r;
  19. }
  20. void preorder(){
  21. cout << ' ' << val;
  22. if(lch != nullptr)lch->preorder();
  23. if(rch != nullptr)rch->preorder();
  24. }
  25. void inorder(){
  26. if(lch != nullptr)lch->inorder();
  27. cout << ' ' << val;
  28. if(rch != nullptr)rch->inorder();
  29. }
  30. int level(){
  31. int lv = 0;
  32. if(lch != nullptr)lv = max(lv, lch->level());
  33. if(rch != nullptr)lv = max(lv, rch->level());
  34. return lv+1;
  35. }
  36. void build_code(string s=""){
  37. if(lch == nullptr && rch == nullptr){
  38. code[ch-'a'] = s;
  39. }else{// two children
  40. lch->build_code(s+'0');
  41. rch->build_code(s+'1');
  42. }
  43. }
  44. char decode(string &s){
  45. if(lch == nullptr && rch == nullptr){
  46. return ch;
  47. }else if(s[0] == '0'){
  48. s.erase(0, 1);
  49. lch->decode(s);
  50. }else if(s[0] == '1'){
  51. s.erase(0, 1);
  52. rch->decode(s);
  53. }
  54. }
  55. };
  56.  
  57. struct heap_node{
  58. tree_node *tree;
  59. int val;
  60. heap_node *lch, *rch;
  61. heap_node(int v, tree_node *T){
  62. tree = T;
  63. val = v;
  64. lch = rch = nullptr;
  65. }
  66. };
  67.  
  68. // heap start from 1
  69. heap_node *heap[100];
  70. int heap_size = 0;
  71.  
  72. void Insert(heap_node *N){
  73. heap[++heap_size] = N;
  74. int cur = heap_size;
  75. while(cur > 1 && heap[cur]->val < heap[cur/2]->val){
  76. swap(heap[cur], heap[cur/2]);
  77. cur /= 2;
  78. }
  79. }
  80.  
  81. heap_node *Pop(){
  82. heap_node *ret = heap[1];
  83. heap[1] = heap[heap_size];
  84. heap[heap_size--] = nullptr;
  85. int cur = 1;
  86. while(heap[cur*2] != nullptr){
  87. if(heap[cur*2+1] == nullptr){
  88. if(heap[cur]->val <= heap[cur*2]->val){
  89. break;
  90. }else{
  91. swap(heap[cur], heap[cur*2]);
  92. cur = cur*2;
  93. }
  94. }else if(heap[cur*2+1] != nullptr){
  95. if(heap[cur]->val <= heap[cur*2]->val
  96. && heap[cur]->val <= heap[cur*2+1]->val){
  97. break;
  98. }else if(heap[cur*2]->val <= heap[cur*2+1]->val){
  99. swap(heap[cur], heap[cur*2]);
  100. cur = cur*2;
  101. }else if(heap[cur*2]->val > heap[cur*2+1]->val){
  102. swap(heap[cur], heap[cur*2+1]);
  103. cur = cur*2+1;
  104. }
  105. }
  106. }
  107. return ret;
  108. }
  109.  
  110.  
  111. int main(){
  112. string s, cipher;
  113. cin >> s >> cipher;
  114. int cnt[26] = {0};
  115. for(int i = 0; i < s.size(); ++i){
  116. ++cnt[s[i]-'a'];
  117. }
  118. cout << "Symbol:";
  119. for(int i = 0; i < 26; ++i){
  120. if(cnt[i] != 0){
  121. cout << ' ' << (char)('a'+i);
  122. }
  123. }
  124. cout << "\nFrequency:";
  125. for(int i = 0; i < 26; ++i){
  126. if(cnt[i] != 0){
  127. cout << ' ' << cnt[i];
  128. }
  129. }
  130. cout << endl;
  131. // heap-ing
  132. for(int i = 0; i < 26; ++i){
  133. if(cnt[i] != 0){
  134. tree_node *T = new tree_node(cnt[i], 'a'+i);
  135. heap_node *H = new heap_node(cnt[i], T);
  136. Insert(H);
  137. }
  138. }
  139. while(heap_size > 1){
  140. heap_node *H1 = Pop();
  141. heap_node *H2 = Pop();
  142. tree_node *newT = new tree_node(H1->val+H2->val, 0, H1->tree, H2->tree);
  143. heap_node *newH = new heap_node(H1->val+H2->val, newT);
  144. Insert(newH);
  145. }
  146. tree_node *tree = heap[1]->tree;
  147. cout << "Huffman Tree:"<<endl;
  148. cout << "Preorder:";
  149. tree->preorder();
  150. cout << "\nInorder:";
  151. tree->inorder();
  152. cout << "\nLevel:";
  153. cout << tree->level() << endl;
  154. cout << "Huffman Code:" << endl;
  155. tree->build_code();
  156. for(int i = 0; i < 26; ++i){
  157. if(cnt[i] != 0){
  158. cout << (char)('a'+i) << " : " << code[i] << endl;
  159. }
  160. }
  161. cout << "Decode Result:\n";
  162. while(!cipher.empty()){
  163. cout << tree->decode(cipher);
  164. }
  165. cout << endl;
  166. return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment