鱼C论坛

 找回密码
 立即注册
查看: 3126|回复: 6

哈夫曼编码问题

[复制链接]
发表于 2021-12-17 14:10:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 看清之后才看轻 于 2021-12-17 15:28 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define n 7
  4. #define m 2 * n - 1
  5. #define maxval 100.0 //令maxval为最大值
  6. typedef struct
  7. {
  8.     float weight;
  9.     int parent, lchild, rchild;
  10. } hufmtree;
  11. typedef struct
  12. {
  13.     char bits[n];
  14.     int start;
  15.     char ch;
  16. } Codetype;
  17. Codetype code[n];
  18. hufmtree tree[2 * n - 1];
  19. void Huffman(hufmtree tree[])
  20. {
  21.     int i, j, p1, p2;
  22.     float small1, small2, f;
  23.     for (i = 0; i < m; i++)
  24.     {
  25.         tree[i].parent = 0; //初始化数组
  26.         tree[i].lchild = 0;
  27.         tree[i].rchild = 0;
  28.         tree[i].weight = 0.0;
  29.     }
  30.     printf("Input every character's weight!\n");
  31.     for (i = 0; i < n; i++)
  32.     {
  33.         scanf("%f", &f);
  34.         tree[i].weight = f;
  35.     }
  36.     for (i = n; i < m; i++)
  37.     {
  38.         p1 = p2 = 0;
  39.         small1 = small2 = maxval;
  40.         for (j = 0; j <= i - 1; j++)
  41.         {
  42.             if (tree[j].parent == 0)
  43.                 if (tree[j].weight < small1)
  44.                 {
  45.                     small2 = small1;
  46.                     small1 = tree[j].weight;
  47.                     p2 = p1;
  48.                     p1 = j;
  49.                     // printf("%d#",p1);
  50.                 }
  51.                 else if (tree[j].weight < small2)
  52.                 { //查找次小权,并用p2记录下标
  53.                     small2 = tree[j].weight;
  54.                     p2 = j;
  55.                     // printf("%d#",p2);
  56.                 }
  57.             tree[p1].parent = tree[p2].parent = i;
  58.             tree[i].weight = tree[p1].weight + tree[p2].weight;
  59.             tree[i].lchild = p1;
  60.             tree[i].rchild = p2;
  61.         }
  62.     }
  63. }
  64. void HuffmanCode(hufmtree tree[])
  65. {
  66.     int i, j, p;
  67.     for (i = 0; i < n; i++)
  68.     {
  69.         code[i].start = n - 1;
  70.         j = i;
  71.         p = tree[i].parent;
  72.         while (p != 0)
  73.         {
  74.             // printf("%d %d#\n",i,j);
  75.             if (tree[p].lchild == j)
  76.                 code[i].bits[code[i].start] = '0';
  77.             else
  78.                 code[i].bits[code[i].start] = '1';
  79.             code[i].start--;
  80.             j = p;
  81.             p = tree[p].parent;
  82.         }
  83.     }
  84. }
  85. void print_prefix_code(Codetype code[])
  86. {
  87.     for (int i = 0; i < n ; i++)
  88.     {
  89.         printf("%c's prefix code is ", code[i].ch);
  90.         for (int j = 0; j < n; j++)
  91.         {
  92.             printf("%c", code[i].bits[j]);
  93.         }
  94.         printf("\n");
  95.     }
  96. }
  97. int main()
  98. {
  99.     printf("Input code character!Maxsize is 7.\n");
  100.     for (int i = 0; i < n; i++)
  101.     {
  102.         scanf("%c", &code[i].ch);
  103.     }
  104.     Huffman(tree);
  105.     HuffmanCode(tree);
  106.     // for (int i = 0; i < n; i++)
  107.     // {
  108.     //     printf("%f ",tree[i].weight);
  109.     // }
  110.     print_prefix_code(code);

  111.     return 0;
  112. }
复制代码

请问这个代码为什么输入ABCDEFG和0.40 0.30 0.15 0.05 0.04 0.03 0.03输出的编码不是唯一的?
QQ图片20211217141018.png
QQ图片20211217141719.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-17 15:36:25 | 显示全部楼层
数据结构 上学期的 学得人麻了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-17 16:16:56 | 显示全部楼层

回帖奖励 +10 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-17 17:21:04 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-18 08:04:30 | 显示全部楼层

回帖奖励 +10 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-18 12:12:40 | 显示全部楼层

回帖奖励 +10 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-21 14:06:52 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 02:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表