Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. struct treenode{
  8. int num;
  9. int freq;
  10. vector<int> code;
  11. string codeStr; // !
  12. struct treenode *parent;
  13. struct treenode *leftchild;
  14. struct treenode *rightchild;
  15. };
  16.  
  17. bool CompareFreq(treenode *a,treenode *b){return a->freq < b->freq;};
  18. void HuffmanCoding();
  19. void HuffmanEncode(int n , treenode *root);
  20. void BuildHuffmanCode(treenode *root, string parentCode );
  21. void PrintCode(treenode *root);
  22.  
  23. int main()
  24. {
  25. HuffmanCoding();
  26. }
  27.  
  28. void HuffmanCoding(){
  29. int n;
  30. cin >> n;
  31. vector<treenode *> vector;
  32. for(int i = 0 ; i < n ; i++){
  33. treenode *tempnode = new treenode;
  34. tempnode->num = i; // !
  35. tempnode->codeStr = "";
  36. tempnode->parent = NULL; // ! 後面有要用NULL判斷,初始化保險
  37. tempnode->leftchild = NULL; // ! 後面有要用NULL判斷,初始化保險
  38. tempnode->rightchild = NULL; // ! 後面有要用NULL判斷,初始化保險
  39. cin >> tempnode->freq;
  40. vector.push_back(tempnode);
  41. }
  42. sort(vector.begin(),vector.end(),CompareFreq);
  43.  
  44. int count = 0;
  45. while(count < n-1){
  46. // treenode *tempnode1 = new treenode; tempnode1 = vector[0]; // ! 浪費一格空間
  47. // treenode *tempnode2 = new treenode; tempnode2 = vector[1]; // ! 浪費一格空間
  48. treenode *tempnode1; tempnode1 = vector[0];
  49. treenode *tempnode2; tempnode2 = vector[1];
  50. treenode *insertnode = new treenode; // ! 沒有格子放資訊
  51. insertnode->num;
  52. insertnode->codeStr = ""; // !
  53. insertnode->freq = tempnode1->freq + tempnode2->freq;
  54. insertnode->leftchild = tempnode1;
  55. insertnode->rightchild = tempnode2;
  56. tempnode1->parent = insertnode ; tempnode2->parent = insertnode;
  57. vector[1] = insertnode;
  58. vector.erase(vector.begin());
  59. sort( vector.begin(), vector.end(), CompareFreq );
  60. count ++; // ! 原本沒有這行
  61.  
  62. }
  63.  
  64. // treenode *root = new treenode; root = vector[0]; // ! 浪費一格空間
  65. treenode *root = vector[0];
  66. HuffmanEncode(n , root);
  67.  
  68. }
  69.  
  70. void HuffmanEncode(int n , treenode *root){
  71. BuildHuffmanCode(root, root->codeStr);
  72. PrintCode(root);
  73. }
  74.  
  75. void BuildHuffmanCode(treenode *root, string parentCode ){
  76. // ! 條件怪怪的 root會進去1和2,code應該是跟著child,不是跟著parent
  77.  
  78. // treenode *root_parent = new treenode; root_parent = root->parent; // ! 浪費一格空間
  79. treenode *root_parent = root->parent;
  80. if(root->leftchild != NULL){
  81. root->leftchild->codeStr = root->codeStr + "0";
  82. BuildHuffmanCode(root->leftchild, root->codeStr );
  83. }
  84. if(root->rightchild != NULL){
  85. root->rightchild->codeStr = root->codeStr + "1";
  86. BuildHuffmanCode(root->rightchild, root->codeStr );
  87. }
  88. if(root->leftchild == NULL && root->rightchild == NULL){
  89. return;
  90. }
  91. }
  92.  
  93. void PrintCode(treenode *root){
  94. queue<treenode *> queue;
  95. queue.push(root);
  96. while (!queue.empty()){ // 若queue不是空的, 表示還有node沒有visiting
  97. treenode *current = queue.front(); // 取出先進入queue的node
  98. queue.pop();
  99. for(int i = 0 ; i < current->code.size() ; i++){
  100. for ( int k = 0; k < current->code.size(); k ++ ); // cout << current->code[k] << " "; // 進行visiting // !
  101. }
  102.  
  103. cout << current->freq << " " << current->codeStr << endl;
  104. if (current->leftchild != NULL){ // 若leftchild有資料, 將其推進queue
  105. queue.push(current->leftchild);
  106. }
  107. if (current->rightchild != NULL){ // 若rightchild有資料, 將其推進queue
  108. queue.push(current->rightchild);
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement