计算机算法设计与分析 哈夫曼编码 实验报告

时间:2024.4.20

×××学院实验报告纸

计算机科学与工程学院( 院、系)网络工程 专业 071 班 组 计算机算法设计与分析 课 哈夫曼编码问题

1、 实验目的:

根据最优二叉树构造哈夫曼编码 利用哈夫曼树很容易求出给定字符集及其概率分布的最优前缀码。哈夫曼编码正是一种应用广泛且非常,本次试验通过设计一个算法,体会和掌握哈夫曼编码要点。

2、 算法分析:

给定字符集的哈夫曼树生成后,求哈夫曼编码的具体实现过程是:依次以叶子T[i]为出发点,向上回溯至根为止。

上溯时走左分支则生成代码0,走右分支则生成代码1。

3、 程序代码:

问题描述: 已知每一个字符出现的频率,构造哈夫曼树,并设计哈夫曼编码。

功能描述: 键入权值和相应字符 按照“树状结构”打印构造好的哈夫曼树,并打印出每一个字符对应的哈夫曼编码。

选作内容:已知一个字符串,将其进行编码;

已知一串编码,可以将其译码成字符串。

cprintf("请输入哈夫曼编码测试数据,在此建议为'this

programme is my favorite'");

printf("\n");

cprintf("注意小写,空格由大写字母T代替,并且字符数小于27.\n");

scanf("%s",sstring);

if(strlen(sstring)>=MAXNODE)

{printf("you input the data number >=MAXNODE.");

exit(1);

}

for(i=0;i<strlen(sstring);i++)

{

for(j=0;j<MAXBIT;j++)

if(sstring[i]==haffcode[j].data)

{

k=j;

break;

}

if(k<0||k>MAXNODE-1)

{printf("在系统中找不到与第个%d字符相匹配的编码\n",i+1);

continue;

}

newhaffcode[i].start=haffcode[k].start;

newhaffcode[i].weight=haffcode[k].weight;

newhaffcode[i].data=haffcode[k].data;

for(s=haffcode[k].start+1;s<MAXBIT;s++)

newhaffcode[i].bit[s]=haffcode[k].bit[s];

}

pprintf(newhaffcode,strlen(sstring));

}

void end()

4、 时间复杂度分析:

算法的时间复杂度分析:程序部分用双循环嵌套结构,时间复杂度为O(n2). 算法的空间复杂度分析:算法的空间复杂度为O(n)

5、 总结:

通过一个简单的例子,掌握了哈夫曼编码的算法思想和要点。

附代码:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<conio.h>a

#include<graphics.h>

#define MAXVALUE 200 /*权值的最大值*/

#define MAXBIT 30 /*最大的编码位数*/

#define MAXNODE 30 /*初始的最大的结点数*/

struct haffnode

{char data;

int weight;

int flag;

int parent; /*双亲结点的下标*/

int leftchild; /*左孩子下标*/

int rightchild; /*右孩子下标*/

};

struct haffcode

{int bit[MAXNODE];

int start; /*编码的起始下标*/

char data;

int weight; /*字符权值*/

};

/*函数说明*/

/************************************************************************/

void pprintf(struct haffcode haffcode[],int n);

/*输出函数*/

void haffmantree(int weight[],int n,struct haffnode hafftree[],char

data[]);

/*建立哈夫曼树*/

void haffmancode(struct haffnode hafftree[],int n,struct haffcode

haffcode[]);

/*求哈夫曼编码*/

void test(struct haffcode haffcode[],int n);

/*测试函数*/

void end();

/*结束界面函数*/

/************************************************************************/

void haffmantree(int weight[],int n,struct haffnode hafftree[],char data[])

/*建立叶结点个数为n,权值数组为weight[]的哈夫曼树*/ {int i,j,m1,m2,x1,x2;

/*哈夫曼树hafftree[]初始化,n个叶结点共有2n-1个结点*/ for(i=0;i<2*n-1;i++)

{if(i<n) {hafftree[i].data=data[i];

hafftree[i].weight=weight[i]; /*叶结点*/

}

else {hafftree[i].weight=0; /*非叶结点*/

hafftree[i].data='\0';

}

hafftree[i].parent=0;

/*初始化没有双亲结点*/

hafftree[i].flag=0;

hafftree[i].leftchild=-1;

hafftree[i].rightchild=-1;

}

for(i=0;i<n-1;i++)

/*构造哈夫曼树n-1个非叶结点*/

{m1=m2=MAXVALUE;

x1=x2=0;

for(j=0;j<n+i;j++)

{if(hafftree[j].weight<m1&&hafftree[j].flag==0)

{m2=m1;

x2=x1;

m1=hafftree[j].weight;

x1=j;

}

else if(hafftree[j].weight<m2&&hafftree[j].flag==0)

{m2=hafftree[j].weight;

x2=j;

}

}

hafftree[x1].parent=n+i;

hafftree[x2].parent=n+i;

hafftree[x1].flag=1;

hafftree[x2].flag=1;

hafftree[n+i].weight=hafftree[x1].weight+hafftree[x2].weight; hafftree[n+i].leftchild=x1;

hafftree[n+i].rightchild=x2;

}

}

void haffmancode(struct haffnode hafftree[],int n,struct haffcode haffcode[])

{/*由n个结点的哈夫曼树hafftree[]构成的哈夫曼编码haffcode[]*/ int i,j,child,parent;

struct haffcode newcode;

struct haffcode *cd;

cd=&newcode;

for(i=0;i<n;i++)

/*求n个结点的哈夫曼编码*/

{cd->start=MAXBIT-1;

/*不等长编码的最后一位是n-1*/

cd->weight=hafftree[i].weight;

cd->data=hafftree[i].data; /*取得编码对应值的字符*/

child=i;

parent=hafftree[child].parent;

while(parent!=0)

{if(hafftree[parent].leftchild==child)

cd->bit[cd->start]=0;

/*左孩子编码为0*/

else

cd->bit[cd->start]=1; /*右孩子编码为1*/

cd->start--;

child=parent;

parent=hafftree[child].parent;

}

for(j=cd->start+1;j<MAXBIT;j++)

/*保存每个叶结点的编码和等长编码的起始位*/

haffcode[i].bit[j]=cd->bit[j];

haffcode[i].data=cd->data;

haffcode[i].start=cd->start;

haffcode[i].weight=cd->weight;

}

}

void pprintf(struct haffcode myhaffcode[],int n)

{int i,j,count=0;

clrscr();

for(i=0;i<n;i++)

{textcolor(YELLOW);

cprintf("字符=%c",myhaffcode[i].data);

printf(" ");

textcolor(YELLOW);

cprintf("weight=%3d",myhaffcode[i].weight);

printf(" ");

textcolor(YELLOW);

cprintf("haffcode=");

for(j=myhaffcode[i].start+1;j<MAXBIT;j++)

cprintf("%d",myhaffcode[i].bit[j]);

printf("\n");

count++;

if(count==21)

getch();

}

}

void test(struct haffcode haffcode[],int n)

{int i,j,k,s;

char sstring[MAXNODE];

struct haffcode newhaffcode[MAXNODE];

j=0;

clrscr();

textcolor(YELLOW);

cprintf("请输入哈夫曼编码测试数据,在此建议为'this

programme is my favorite'");

printf("\n");

cprintf("注意小写,空格由大写字母T代替,并且字符数小于27.\n"); scanf("%s",sstring);

if(strlen(sstring)>=MAXNODE)

{printf("you input the data number >=MAXNODE.");

exit(1);

}

for(i=0;i<strlen(sstring);i++)

{

for(j=0;j<MAXBIT;j++)

if(sstring[i]==haffcode[j].data)

{

k=j;

break;

}

if(k<0||k>MAXNODE-1)

{printf("在系统中找不到与第个%d字符相匹配的编码\n",i+1); continue;

}

newhaffcode[i].start=haffcode[k].start;

newhaffcode[i].weight=haffcode[k].weight;

newhaffcode[i].data=haffcode[k].data;

for(s=haffcode[k].start+1;s<MAXBIT;s++)

newhaffcode[i].bit[s]=haffcode[k].bit[s];

}

pprintf(newhaffcode,strlen(sstring));

}

void end()

{int driver,mode;

driver=VGA;

mode=VGAHI;

initgraph(&driver,&mode," ");

setlinestyle(0,0,2);

setfillstyle(1,9);

bar(120,60,520,80);

setfillstyle(1,9);

bar(90,100,550,350);

moveto(121,65);

settextstyle(5,0,6);

setcolor(7);

outtext("This programme is designed by Dou Zheren"); settextstyle(3,0,3);

setcolor(7);

moveto(150,200);

outtext("thank you use this programme.");

moveto(100,300);

settextstyle(3,0,2);

setcolor(7);

outtext("please press anykey to end this programme."); }

void main()

{

int i,j,n=27;

int driver=VGA,mode=VGAHI;

char ch;

int weight[27]={186,64,13,22,32,103,21,15,47,

57,1,5,32,20,57,63,15,1,48,

51,80,23,8,18,1,16,1};

char data[28]={'T','a','b','c','d','e','f','g','h',

'i','j','k','l','m','n','o','p','q',

'r','s','t','u','v','w','x','y','z'};

struct haffnode newhaffnode[2*MAXNODE-1];

struct haffcode newcode[MAXNODE];

struct haffnode *myhafftree=newhaffnode;

struct haffcode *myhaffcode=newcode;

if(n>MAXNODE)

{printf("you input the haffnode > MAXNODE,so you input the data is

wrong");

printf("\n");

exit(1);

}

clrscr();

textcolor(YELLOW);

cprintf("WELCOME!这是一个求哈夫曼编码的问题");

printf("\n");

cprintf("即对所有的字母进行编码后,在根据用户的需要,对用户的要求进行编码。");

printf("\n");

cprintf("注意:本程序只支持小写字母,空格用大写字母T代替!

");

printf("\n");

getch();

textcolor(YELLOW);

cprintf("Ready?Enter,if you want to begin!\n");

printf("\n");

getch();

cprintf("Now,开始演示哈夫曼编码.");

getch();

haffmantree(weight,n,myhafftree,data);

haffmancode(myhafftree,n,myhaffcode);

pprintf(myhaffcode,n);

clrscr();

printf("若执行自定义编译,请输入y继续。否则程序将结束.");

if((ch=getch())=='y'||ch=='Y')

test(myhaffcode,n);

getch();

clrscr();

end();

getch(); exit(1); }

更多相关推荐:
哈夫曼编码实验报告

哈夫曼编码器实验报告学院:计算机学院班级:计科0801班姓名:***学号:***一.实验目的练习树和哈夫曼树的有关操作,和各个算法程序,理解哈夫曼树的编码和译码二.实验环境Microsoftvisualc++三…

C语言-哈夫曼编码实验报告

福建工程学院课程设计课程数据结构题目哈夫曼编码和译码专业信息管理信息系统班级座号15号姓名林左权20xx年6月27日1实验题目哈夫曼编码和译码一要解决的问题利用哈夫曼编码进行信息通信可以大大提高信道利用率缩短信...

哈夫曼编码实验报告

数据结构作业报告哈夫曼编码实验报告姓名班级学号摘要本程序能执行让一段字符以哈夫曼编码的形式被转存或将一段哈夫曼编码翻译成字符通过字符的频率统计哈夫曼树的建立递归求字符的哈夫曼编码完成这个程序让我熟悉哈夫曼编码的...

哈夫曼编码实验报告

中南大学数据结构课程姓名刘阳班级信息0703学号0903070312实验时间081114指导老师赵颖

哈夫曼编码实验报告

北京邮电大学信息与通信工程学院数据结构实验报告实验名称实验三哈夫曼编码学生姓名牛佳宁班级20xx211107班内序号27学号10210213日期20xx年12月10日1实验要求利用二叉树结构实现赫夫曼编解码器1...

数据结构+哈夫曼编码+实验报告

实验报告一实验目的掌握哈夫曼树及其应用二实验内容利用哈夫曼算法构造最优二叉树然后对构造好的二叉树的叶子结点进行前缀编码三实验步骤1审清题意分析并理出解决问题的基本思路2根据基本思路设计好程序的算法3根据算法编写...

哈夫曼编码实验报告

数据结构实验班级软件C092姓名孙琼芳学号098586一实验目的利用哈夫曼编码进行通信可以大大提高信道利用率缩短信息传输时间降低传输成本但是这要求在发送端通过一个编码系统对待传数据预先编码在接收端将传来的数据进...

哈夫曼编码 译码器 实验报告

数据结构课程设计报告QFileInfohufFileInfoHuffView负责具体画图classHuffViewpublicQGraphicsViewQOBJECTpublicexplicitHuffView...

哈夫曼编码课程设计报告

数据结构课程设计报告基于哈夫曼树的文件压缩解压程序专业班级信科2班姓名徐爱娟谢静学号20xx161431005120xx161431005020xx1231一需求分析1课题要求实现文件的压缩与解压并计算压缩率A...

哈夫曼编码算法实验报告_昆明理工大学

昆明理工大学信息工程与自动化学院学生实验报告20xx20xx学年第1学期课程名称算法设计与分析开课实验室信自楼44220xx年11月8日一上机目的及内容1上机内容设需要编码的字符集为d1d2dn它们出现的频率为...

实验报告模版 二叉树基本操作与哈夫曼编码译码系统的设计

课程设计报告20xx20xx学年第2学期题目二叉树基本操作与哈夫曼编码译码系统的设计专业学生姓名班级学号指导教师指导单位日期课题题目二叉树基本操作与哈夫曼编码译码系统的设计一课题内容和要求创建一棵二叉树分别实现...

哈夫曼编码与多叉路口交通灯管理课程设计报告

目录一哈夫曼编码译码器21需求分析22详细设计221哈夫曼树节点的数据类型定义为222所实现的功能函数如下223流程图33调试分析34用户手册35测试结果46附录4二多叉路口交通灯管理111需求分析112详细设...

哈夫曼编码实验报告(37篇)