ZBLOG

#include #include #define MAXSIZE 100 using namespace std; typedef struct {//哈夫曼树结点的形式 int weight; //结点的权值 int parent,lchild,rchild; //结点的双亲、左孩子、右孩子的下标 }HTNode,*HuffmanTree; //动态分配数组存储哈夫曼树 typedef char **...

以下是补充代码:

  1. int Search(char a[], char ch) {
  2. for(int i = 0; a[i] != '\0'; i++) {
  3. if(a[i] == ch)
  4. return i;
  5. }
  6. return -1;
  7. }
  8. void Sort(char a[], int b[], int len) {
  9. for(int i = 0; i < len - 1; i++) {
  10. for(int j = 0; j < len - 1 - i; j++) {
  11. if(a[j] > a[j+1]) {
  12. swap(a[j], a[j+1]);
  13. swap(b[j], b[j+1]);
  14. }
  15. }
  16. }
  17. }
  18. void Select_min(HuffmanTree HT, int n, int &s1, int &s2) {
  19. s1 = s2 = -1;
  20. for(int k = 0; k < n; k++) {
  21. if(HT[k].parent == -1 && (s1 == -1 || HT[k].weight < HT[s1].weight)) {
  22. s2 = s1;
  23. s1 = k;
  24. } else if (HT[k].parent == -1 && (s2 == -1 || HT[k].weight < HT[s2].weight)) {
  25. s2 = k;
  26. }
  27. }
  28. }
  29. void CreateHuffmanTree(HuffmanTree &HT, int n, int b[]) {
  30. m = 2 * n - 1;
  31. HT = new HTNode[m];
  32. for(int i = 0; i < m; i++) { // 初始化树的所有节点
  33. if(i < n) {
  34. HT[i].weight=b[i];
  35. } else {
  36. HT[i].weight=0;
  37. }
  38. HT[i].parent=-i-2;//初始设置所有结点的双亲为-2
  39. HT[i].lchild=-1;
  40. HT[i].rchild=-1;
  41. }
  42. for(int i = n; i < m; i++) { // 创建哈夫曼树
  43. int s1, s2;
  44. Select_min(HT, i, s1, s2); // 选择权值最小的两个节点
  45. HT[s1].parent = i;
  46. HT[s2].parent = i;
  47. HT[i].lchild = s1;
  48. HT[i].rchild = s2;
  49. HT[i].weight = HT[s1].weight + HT[s2].weight; // 合并节点,更新权值
  50. }
  51. }
  52. void CreateHuffmanCode(HuffmanTree HT, HuffmanCode &HC, int n) {
  53. HC = new char*[n];
  54. char *code = new char[n];
  55. code[n-1] = '\0';
  56. for(int i = 0; i < n; ++i) {
  57. int current = i;
  58. int parent = HT[current].parent;
  59. while(parent != -1) {
  60. if(current == HT[parent].lchild)
  61. code[--n] = '0';
  62. else
  63. code[--n] = '1';
  64. current=parent;//向上回溯
  65. parent=HT[current].parent;
  66. }
  67. HC[i]=new char[strlen(code+ n)];
  68. strcpy(HC[i],code+n);
  69. }
  70. delete[] code; //释放临时空间
  71. }
  72. void PrintHT(HuffmanTree HT) {
  73. cout << "存储结构:" << endl;
  74. for (int k=0;k<m;k++)
  75. {
  76. cout << setw(3) << k << " ";
  77. cout << setw(3) << HT[k].weight<< " ";
  78. cout << setw(3) << HT[k].parent<< " ";
  79. cout << setw(3) << HT[k].lchild<< " ";
  80. cout << setw(3) << HT[k].rchild<< endl;
  81. }
  82. }
  83. void PrintHC(HuffmanCode HC, char a[], int j) {
  84. cout<<"编码表:" << endl;
  85. for (int k=0;k<j;k++)
  86. {
  87. cout<<a[k]<<" : "<<HC[k]<<endl;
  88. }
  89. }
  90. void CharFrequency(char ch[], char a[], int b[], int &j){
  91. int len = strlen(ch);
  92. for(int i=0;i<len;++i)
  93. {
  94. if(Search(a,ch[i]) == -1)
  95. {
  96. a[j]=ch[i];
  97. b[j]++;
  98. ++j;
  99. }
  100. else
  101. {
  102. ++b[Search(a,ch[i])];
  103. }
  104. }
  105. }
  106. int main() {
  107. char ch[MAXSIZE];
  108. int i,j;
  109. while(cin>>ch)
  110. {
  111. if(ch[0]=='0') break;
  112. HuffmanTree HT;
  113. char a[MAXSIZE]={'\0'};
  114. int b[MAXSIZE]={0};
  115. j=0; //j统计不同字符的数量
  116. CharFrequency(ch,a,b,j); //统计词频
  117. Sort(a,b,j); //按ASCII码冒泡排序
  118. for(i=0;a[i]!='\0';i++) //输出统计出来的字符和出现频率
  119. {
  120. if(a[i+1]!='\0')
  121. cout<<a[i]<<":"<<b[i]<<" ";
  122. else
  123. cout<<a[i]<<":"<<b[i]<<endl;
  124. }
  125. //构造哈夫曼树
  126. CreateHuffmanTree(HT,i,b); //构造哈夫曼树HT
  127. PrintHT(HT); //输出哈夫曼树的存储结构的终态
  128. //哈夫曼编码
  129. HuffmanCode HC; //编码表HC
  130. CreateHuffmanCode(HT, HC, j);
  131. PrintHC(HC, a, j); //输出每个字符的哈夫曼编码
  132. int k;
  133. for(i=0;ch[i]!='\0';i++) //输出编码后的字符串
  134. {
  135. for(k=0;k<j;k++)
  136. {
  137. if(ch[i]==a[k])
  138. cout<<HC[k];
  139. }
  140. }
  141. cout << endl;
  142. cout << ch << endl;//输出解码后的字符串(与输入的字符串相同)
  143. }
  144. return 0;
  145. }

请注意,以上代码是基于给定的补充部分进行修正和完善,但仍然可能存在一些错误或逻辑缺陷。在实际应用中,请根据具体需求进行验证和调整。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9887

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?