数据结构 二叉树应用:赫夫曼编码二

举报
悦来客栈的老板 发表于 2020/12/29 00:37:14 2020/12/29
【摘要】 #include <stdio.h>#include <stdlib.h>#include <string.h>#include <malloc.h> #define swap(a,b) {int t; t=a; a=b;b=t;} typedef struct { unsigned int weight; unsigned i...

  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5. #define swap(a,b) {int t; t=a; a=b;b=t;}
  6. typedef struct
  7. {
  8. unsigned int weight;
  9. unsigned int parent, lchild, rchild;
  10. }HTNode, *HuffmanTree;
  11. typedef char** HuffmanCode;
  12. void Select(HuffmanTree HT,int n,int &s1,int &s2)
  13. {
  14. int i,k;
  15. int j = 0;
  16. int *a = (int*)malloc(n * sizeof(int));
  17. for (i=1; i<=n; ++i)
  18. {
  19. if (HT[i].parent == 0)
  20. {
  21. a[j++] = HT[i].weight;
  22. }
  23. }
  24. for (i=0; i<j-1; i++)
  25. {
  26. for (k=j-2; k>=i; k--)
  27. {
  28. if (a[k] > a[k+1])
  29. {
  30. swap(a[k],a[k+1]);
  31. }
  32. }
  33. }
  34. s1 = a[0];
  35. s2 = a[1];
  36. for (i=1; i<=n; ++i)
  37. {
  38. if (s1 ==(int) HT[i].weight && HT[i].parent == 0)
  39. {
  40. s1 = i;
  41. break;
  42. }
  43. }
  44. for (i=1; i<=n; ++i)
  45. {
  46. if (s2 == (int) HT[i].weight && HT[i].parent == 0)
  47. {
  48. if (i != s1)
  49. {
  50. s2 = i;
  51. break;
  52. }
  53. else
  54. {
  55. continue;
  56. }
  57. }
  58. }
  59. free(a);
  60. }
  61. void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n)
  62. {
  63. HuffmanTree p;
  64. int i,m;
  65. int s1,s2;
  66. char *cd;
  67. if (n<=1)
  68. {
  69. return;
  70. }
  71. m = 2*n -1;/*n个字符,其赫夫曼树的结点个数为2*n -1*/
  72. HT = (HuffmanTree)malloc((m+1) * sizeof(HTNode)); //0号单元未用
  73. for (p = HT+1 , i=1; i<=n; ++i,++p,++w)
  74. {
  75. p->weight = *w;
  76. p->lchild = p->parent = p->rchild = 0;
  77. }
  78. for (;i<=m; ++i,++p)
  79. {
  80. p->lchild = p->parent = p->rchild = p->weight = 0;
  81. }
  82. for (i=n+1; i<=m; ++i)
  83. {
  84. Select(HT,i-1,s1,s2);
  85. HT[s1].parent = i;
  86. HT[s2].parent = i;
  87. HT[i].lchild = s1;
  88. HT[i].rchild = s2;
  89. HT[i].weight = HT[s1].weight + HT[s2].weight;
  90. }
  91. HC = (HuffmanCode)malloc((n+1) * sizeof(char *));
  92. cd = (char*)malloc(n*sizeof(char));
  93. int q = m,cdlen = 0;
  94. for (i=1; i<=m; i++)
  95. {
  96. HT[i].weight = 0;
  97. }
  98. while (q)
  99. {
  100. if (HT[q].weight == 0)
  101. {
  102. HT[q].weight = 1;
  103. if (HT[q].lchild != 0)
  104. {
  105. q = HT[q].lchild;
  106. cd[cdlen++] = '0';
  107. }
  108. else if (HT[q].rchild == 0)
  109. {
  110. HC[q] = (char*)malloc((cdlen+1) * sizeof(char));
  111. cd[cdlen] = '\0';
  112. strcpy(HC[q],cd);
  113. }
  114. }
  115. else if (HT[q].weight == 1)
  116. {
  117. HT[q].weight = 2;
  118. if (HT[q].rchild != 0)
  119. {
  120. q = HT[q].rchild;
  121. cd[cdlen++] = '1';
  122. }
  123. }
  124. else
  125. {
  126. HT[q].weight = 0;
  127. q = HT[q].parent ;
  128. --cdlen;
  129. }
  130. }
  131. free(cd);
  132. }
  133. int main()
  134. {
  135. HuffmanTree HT;
  136. HuffmanCode HC;
  137. int i;
  138. //int a[] = {5,29,7,8,14,23,3,11};
  139. //int a[] = {27,8,15,15,30,5};
  140. int a[] = {45,13,12,16,9,5};
  141. int n = sizeof(a) / sizeof(a[0]);
  142. HuffmanCoding(HT,HC,a,n);
  143. for (i=1; i<=n; i++)
  144. {
  145. printf("%d:%s\n",a[i-1],HC[i]);
  146. }
  147. return 0;
  148. }

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/17268171

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。