哈夫曼树的编码与译码.docVIP

  • 12
  • 0
  • 约2.91千字
  • 约 7页
  • 2017-07-03 发布于湖北
  • 举报
哈夫曼树的编码与译码汇编

上机实验报告 学 院: 计算机与信息技术学院 专 业: 计算机科学与技术(师范) 课程名称: 数据结构 实验题目: 哈夫曼树编码与译码 班级序号: 师范1班 学 号: 201421012731 学生姓名: 邓雪 指导教师: 杨红颖 完成时间: 2015年12月25号 实验目的: 本实验的目的是通过对简单的哈夫曼编/译码系统的设计与实现来熟练掌握树形结构在实际问题中的应用。设计要求对输入的一串电文字符实现哈夫曼编码,再对哈夫曼编码生成的代码串进行译码,输出电文字符串。 二、实验环境: Windows 8.1 Microsoft Visual c++ 6.0 实验内容及要求: 本设计必须实现以下几个方面的功能:? 1)哈夫曼树的建立? 2)哈夫曼编码的生成? 3)编码文件的译码 四、概要设计: 创建哈夫曼树,定义二叉树结点值的类型为字符型。 建立编码文件的基本思想是:将要编码的字符串中的字符逐一与预先生成哈夫曼树时保存的字符编码对照表进行比较,找到以后,将字符的编码写入代码文件,直至所有的字符处理完为止。 测验数据:7 5 2 3 8 10 20 五、代码 #include stdio.h #include stdlib.h #define maxval 1024 #define n 5 #define m 2*n-1 typedef int datatype; //定义结构体 typedef struct { float weight; int lchild,rchild,parent; }hufmtree; //定义结构体 typedef struct { char bits[n]; int start; char ch; }codetype; hufmtree tree[m]; codetype code[n]; //建立哈夫曼树 void HUFFMAN(hufmtree tree[]) { int i,j,p1,p2; float small1,small2,f; for(i=0;im;i++) { tree[i].parent=0; tree[i].lchild=0; tree[i].rchild=0; tree[i].weight=0.0; } printf(\n\t\t\t\t哈夫曼编码与译码\n); printf(请输入叶子结点的权值:); for(i=0;in;i++) { scanf(%f,f); tree[i].weight=f; } for(i=n;im;i++) { p1=0; p2=0; small1=maxval; small2=maxval; for(j=0;j=i-1;j++) if(tree[j].parent==0) if(tree[j].weightsmall1) { small2=small1; small1=tree[j].weight; p2=p1; p1=j; } else if(tree[j].weightsmall2) { small2=tree[j].weight; p2=j; } tree[p1].parent=i+1; tree[p2].parent=i+1; tree[i].lchild=p1+1; tree[i].rchild=p2+1; tree[i].weight=tree[p1].weight+tree[p2].weight; } } //哈夫曼编码 void HUFFMANCODE(codetype code[],hufmtree tree[]) { int i,c,p; codetype cd; for(i=0;in;i++) { cd.start=n; scanf(%c,cd.ch); c=i+1; p=tree[i].parent; while(p!=0) { cd.start--; if(tree[p-1].lchild==c) cd.bits[cd.start]=0; else cd.bits[cd.start]=1; c=p; p=tree[p-1].parent; } code[i]=cd; } } //哈夫曼译码 void DECODE(codetype code[],hufmtree tree[]) { int i,j=0

文档评论(0)

1亿VIP精品文档

相关文档