完整版实验报告模版 二叉树基本操作与哈夫曼编码译码系统的设计.docx
《完整版实验报告模版 二叉树基本操作与哈夫曼编码译码系统的设计.docx》由会员分享,可在线阅读,更多相关《完整版实验报告模版 二叉树基本操作与哈夫曼编码译码系统的设计.docx(21页珍藏版)》请在冰豆网上搜索。
![完整版实验报告模版 二叉树基本操作与哈夫曼编码译码系统的设计.docx](https://file1.bdocx.com/fileroot1/2022-10/10/64a90129-1e54-4d3b-aa6b-c7cdedd15907/64a90129-1e54-4d3b-aa6b-c7cdedd159071.gif)
完整版实验报告模版二叉树基本操作与哈夫曼编码译码系统的设计
课程设计报告
(2013/2014学年第2学期)
题目:
二叉树基本操作与哈夫曼编码译码系统的设计
专业:
学生姓名:
班级学号:
指导教师
指导单位
日期
评分细则
评分项
成绩
遵守机房规章制度(5分)
上机时的表现(5分)
学习态度(5分)
程序准备情况(5分)
程序设计能力(10分)
团队合作精神(5分)
课题功能实现情况(10分)
算法设计合理性(10分)
用户界面设计(10分)
报告书写认真程度(5分)
内容详实程度(10分)
文字表达熟练程度(10分)
回答问题准确度(10分)
简短评语
教师签名:
年月日
评分等级
备注
评分等级有五种:
优秀、良好、中等、及格、不及格
课题题目
二叉树基本操作与哈夫曼编码译码系统的设计
一、课题内容和要求
创建一棵二叉树,分别实现先序、中序和后序遍历一棵二叉树,计算二叉树结点个数等操作。
设计哈夫曼编码/译码系统。
能成功演示二叉树的有关运算,运算完毕后能成功释放二叉树所有结点占用的系统内存。
二、需求分析
我们根据需求得到以上的菜单,以链表方式存储二叉树,以插入二叉搜索树的方式,将数据一个一个地插入二叉树,以递归的方式分别实现先、中、后三种方式的遍历和计算二叉树的高度,删除二叉树时,先搜索找到那个节点,若有两个子节点,查找中序遍历直接后继节点,之后常规的替代删除操作,最后是哈夫曼树的实现,从文件读取字符串的时候,用while循环来得到每个字母的出现次数,得到权值,之后实现哈夫曼树,通过译码函数输出译码序列。
三、概要设计
typedefcharEtype;
typedefstructbtnode
{
Etypedata;
structbtnode*lch,*rch;
intweight;
}btnode;
typedefstructbtree
{
structbtnode*root;
}btree;
typedefstruct{
intweight;
intparent,lchild,rchild;
}HTNode,*HuffmanTree;
typedefchar**HuffmanCode;
其他的就是各类函数,见下文。
四、详细设计
#include
#include
#include
#include
#include
#include
typedefcharEtype;
typedefstructbtnode
{
Etypedata;
structbtnode*lch,*rch;
intweight;
}btnode;
typedefstructbtree
{
structbtnode*root;
}btree;
btnode*newnode()
{
btnode*p=(btnode*)malloc(sizeof(btnode));
returnp;
}
btnode*newnode(Etypee)
{
btnode*p=(btnode*)malloc(sizeof(btnode));
p->data=e;
p->lch=NULL;
p->rch=NULL;
returnp;
}
voidMAKEBTREE(btree*bt,intx,btree*lt,btree*rt)
{
btnode*p=newnode();
p->weight=x;
p->lch=lt->root;
p->rch=rt->root;
lt->root=NULL;
rt->root=NULL;
bt->root=p;
}
voidCREATEBTREE(btree*bt)/*构造一颗空二叉数*/
{
bt->root=NULL;
}
//模仿先序递归遍历方法,建立二叉树
btnode*creat_bt2()
{
btnode*t;
Etypee;
scanf("%c",&e);
if(e=='#')t=NULL;//对于'#'值,不分配新结点
else{
t=(btnode*)malloc(sizeof(btnode));
t->data=e;
t->lch=creat_bt2();//左孩子获得新指针值
t->rch=creat_bt2();//右孩子获得新指针值
}
returnt;
}//creat_bt2
voidpreorder(btnode*p)//前序遍历
{
if(p){
printf("%3c",p->data);
preorder(p->lch);
preorder(p->rch);
}
}//preorder
//中序递归遍历二叉树
voidinorder(btnode*p)
{
if(p){
inorder(p->lch);
cout<data<inorder(p->rch);
}
}//inorder
//后序递归遍历二叉树
voidpostorder(btnode*p)
{
if(p){postorder(p->lch);
postorder(p->rch);
cout<data<}
}//postorder
intDepth(btnode*p)
{
if(!
p)
return0;
else
return1+((Depth(p->lch)>Depth(p->rch))?
Depth(p->lch):
Depth(p->rch));
}
intleafcount(btnode*bt)//输入btree的root
{//计算叶结点数
intcount=0;
if(bt!
=NULL)
{
leafcount(bt->lch);
leafcount(bt->rch);
}
if((bt->lch==NULL)&&(bt->rch==NULL))
count++;
returncount;
}
intremove(btree*bt)//输入那个节点的值
{
btnode*p=bt->root;
btnode*c,*r,*s,*q;
Etypex,e;
cout<<"请输入要删除的节点的值"<cin>>e;
while(p&&p->data!
=e)
{
q=p;
if(edata)p=p->lch;
elsep=p->rch;
}
if(!
p)
{
cout<<"不存在"<return0;
}
x=p->data;
if(p->lch&&p->rch)
{
s=p->rch;
r=p;
while(s->lch)
{
r=s;
s=s->lch;
}
p->data=s->data;
p=s;
q=r;
}
if(p->lch)c=p->lch;
elsec=p->rch;
if(p==bt->root)bt->root=c;
elseif(p==q->lch)q->lch=c;
elseq->rch=c;
free(p);
return1;
}
intinsert(btree*btr,Etypeet)//二叉搜索树的插入函数
{
btnode*r,*p=btr->root,*q;
while(p)
{
q=p;
if(etdata)p=p->lch;
elseif(et>p->data)p=p->rch;
else{
cout<<"duplicate"<return0;
}
}
r=newnode(et);
if(btr->root)
if(etdata)q->lch=r;
elseq->rch=r;
elsebtr->root=r;
return1;
}
voidmycreate(btreebt)//创建二叉搜索树
{
intx=1;
Etypec;
cout<<"第一个输入的值为根的值,请输入根值"<cin>>c;
btnodebtn;
btn.lch=NULL;
btn.rch=NULL;
btn.data=c;
bt.root->data=btn.data;
bt.root->lch=btn.lch;
bt.root->rch=btn.rch;
c=getchar();
cout<<"其他节点的值"<while((c=getchar())!
='\n'&&x)
{
x=insert(&bt,c);
}
}
voidFmin(btreeht[],int*k1,int*k2,intk)
{
inta,b,c,d;
a=ht[0].root->weight;
b=ht[0].root->weight;
*k1=0;
*k2=0;
for(c=0;c{
if(a>=ht[c].root->weight)
{
a=ht[c].root->weight;
*k1=c;
}
}
for(d=0;d{
if(d==*k1)
continue;
if(b>=ht[d].root->weight)
{
b=ht[d].root->weight;
*k2=d;
}
}
}
btreecreatehfmtree()//生成哈弗曼树
{
btreezero,ht[26];
inti,k,k1,k2;
intw[26];
for(i=0;i<26;i++)
w[i]=0;
CREATEBTREE(&zero);
FILE*fp;
fp=fopen("c:
\\test.txt","r+");
while(!
feof(fp))
{
w[fgetc(fp)-97]++;
}
for(i=0;i<26;i++)
{
MAKEBTREE(&ht[i],w[i],&zero,&zero);
ht[i].root->data=97+i;
printf("%3d",ht[i].root->data);
}
for(k=25;k>0;k--)
{
Fmin(ht,&k1,&k2,k);
MAKEBTREE(&ht[k1],ht[k1].root->weight+ht[k2].root->weight,&ht[k1],&ht[k2]);
ht[k1].root->data='!
';