写点什么

赫夫曼树编码实验报告

  • 2022-11-29
    河南
  • 本文字数:2574 字

    阅读完需:约 8 分钟

一、 实验目的

了解赫夫曼树的结构,并实现赫夫曼树。


二、 实验仪器设备及实验环境:

实验仪器:笔记本

实验环境:DEV C++ 5.11

总的设计思想、实验原理:

1.构建赫夫曼树

2、输入权值的个数和权值的大小

3、从叶子到根逆向求每个字符的赫夫曼编码

4、输出对应的赫夫曼编码


三、 实验步骤设计:(可加附页)


  1. 构建赫夫曼树和赫夫曼编码的存储表示

typedef char** HuffmanCode;typedef  struct {	int parent;	int lch, rch,weight;}HuffmanNode, * HuffmanTree;
复制代码

2.构造赫夫曼树

void CreatHuffTree(HuffmanTree& HT, int n)
{
   int m = 2 * n - 1;
   HT = (HuffmanTree)malloc((m + 1) * sizeof(HuffmanNode));
   printf("请输入n个元素得权值\n");
   int sum;
   for (int i = 1; i <= n; i++)
   {
          scanf("%d", &sum);
          HT[i].weight = sum;
          HT[i].lch = 0; HT[i].parent = 0; HT[i].rch = 0;
   }
   for (int i = n + 1; i <= m; i++)
   {
          HT[i].lch = 0; HT[i].parent = 0; HT[i].rch = 0; HT[i].weight = 0;
   }
   int x1, x2;
   for (int i = n + 1; i <= m; i++)
   {
          Select(HT, i - 1, &x1, &x2);
          HT[x1].parent = i;
          HT[x2].parent = i;
          HT[i].lch = x1;
          HT[i].rch = x2;
          HT[i].weight = HT[x1].weight + HT[x2].weight;
   }
}
复制代码

3.对赫夫曼树进行赫夫曼编码

void CreatHuffmancode(HuffmanTree HT, HuffmanCode& HC, int n)
{
   HC = (HuffmanCode)malloc((n + 1) * sizeof(char*));
   char* cd;
   cd = (char*)malloc(n * sizeof(char));
   cd[n - 1] = '\0';
   int start; int c, f;
   for (int i = 1; i <= n; i++)
   {
          int start = n - 1;
          c = i;
          f = HT[i].parent;
          while (f != 0)
          {
                 --start;
                 if (HT[f].lch == c)
                        cd[start] = '0';
                 else
                        cd[start] = '1';
                 c = f;
                 f = HT[f].parent;
          }
          HC[i] = (char*)malloc((n - start) * sizeof(char));
          strcpy(HC[i], &cd[start]);
   }
   free(cd);
}
复制代码

 

四、 实验结果及分析:



五、自我评价与总结:

自我评价:

对赫夫曼编码编码过程理解,到但是进行上机实现时,自己对自己理解的内容还是不能熟练的进行,还需要在课余的时间,进行复习对代码进行再次的理解和加深记忆。


总结:

通过编写赫夫曼编码的实现这一实验,我将之前的遗漏点补足,学会运用所学知识与实际相结合。让我意识到数据结构这门课程没有想象中的那么简单,以后一定会继续学习这门课程,争取多练习,多学习,多动手实践。七、遇到的问题及创新之处:编写赫夫曼树的过程中会出现代码错误,有时找不到报错的原因,需要进行调试,必要的时候还会在网上进行查询。根据自己理解第一次写出的代码输入数据无输出结果,编码存不进 HuffmanCode 的二维数组,通过调试和对函数内增添打印信息,找出错误之处,得以解决。


源代码


#include <stdio.h>#include <malloc.h>#include <string.h>#define MAXINT  32767 typedef char** HuffmanCode;typedef  struct {	int parent;	int lch, rch,weight;}HuffmanNode, * HuffmanTree;
void Select(HuffmanTree ht, int n, int* s1, int* s2) { int i, min1 = MAXINT, min2 = MAXINT; *s1 = 0; *s2 = 0; for (i = 1; i <= n; i++) { if (ht[i].parent == 0) { if (ht[i].weight < min1) { min2 = min1; *s2 = *s1; min1 = ht[i].weight; *s1 = i; } else if (ht[i].weight < min2) { min2 = ht[i].weight; *s2 = i; } } }}void CreatHuffTree(HuffmanTree& HT, int n){ int m = 2 * n - 1; HT = (HuffmanTree)malloc((m + 1) * sizeof(HuffmanNode)); printf("请输入n个元素得权值"); int sum; for (int i = 1; i <= n; i++) { scanf("%d", &sum); HT[i].weight = sum; HT[i].lch = 0; HT[i].parent = 0; HT[i].rch = 0; } for (int i = n + 1; i <= m; i++) { HT[i].lch = 0; HT[i].parent = 0; HT[i].rch = 0; HT[i].weight = 0; } int x1, x2; for (int i = n + 1; i <= m; i++) { Select(HT, i - 1, &x1, &x2); HT[x1].parent = i; HT[x2].parent = i; HT[i].lch = x1; HT[i].rch = x2; HT[i].weight = HT[x1].weight + HT[x2].weight; }}void CreatHuffmancode(HuffmanTree HT, HuffmanCode& HC, int n){ HC = (HuffmanCode)malloc((n + 1) * sizeof(char*)); char* cd; cd = (char*)malloc(n * sizeof(char)); cd[n - 1] = '\0'; int start; int c, f; for (int i = 1; i <= n; i++) { int start = n - 1; c = i; f = HT[i].parent; while (f != 0) { --start; if (HT[f].lch == c) cd[start] = '0'; else cd[start] = '1'; c = f; f = HT[f].parent; } HC[i] = (char*)malloc((n - start) * sizeof(char)); strcpy(HC[i], &cd[start]); } free(cd);}int main(){ HuffmanTree HT; HuffmanCode HC; printf("请输入元素个数:"); int n; scanf("%d", &n); CreatHuffTree(HT, n); printf("Weigth Lchild Rchild Parent \n"); for (int i = 1; i <= 2 * n - 1; i++) { printf("%-7d%-7d%-7d%-20d", HT[i].weight, HT[i].lch, HT[i].rch, HT[i].parent); printf("\n"); } CreatHuffmancode(HT, HC, n); for (int i = 1; i <= n; i++) { printf("%c的编码为:", 'A' + i-1); printf("%s",HC[i]); printf("\n"); } return 0;}
复制代码


发布于: 刚刚阅读数: 4
用户头像

还未添加个人签名 2022-11-01 加入

还未添加个人简介

评论

发布
暂无评论
赫夫曼树编码实验报告_数据结构_我是一个茶壶_InfoQ写作社区