数据结构课程设计 哈夫曼编译码3.docx
《数据结构课程设计 哈夫曼编译码3.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计 哈夫曼编译码3.docx(11页珍藏版)》请在冰豆网上搜索。
数据结构课程设计哈夫曼编译码3
数据结构课程设计
专业:
计算机科学与技术
姓名:
陈振辉
学号:
12224506
班级:
5班
课程设计三
哈夫曼编译码
简要:
利用赫夫曼编码进行通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本。
这要求在发送端通过一个编码系统对待传输数据预先编码,在接收端将传来的数据进行译码(复原)。
对于双工信道(即可以双向传输信息的信道),每端都需要一个完整的编/译码系统。
试为这样的信息收发站编写一个赫夫曼码的编/译码系统。
2.基本要求
一个完整的系统应具有以下功能:
(1)I:
初始化(Initialization)。
从终端读入字符集大小n,以及n个字符和n个权值,建立赫夫曼树,并将它存于文件hfmTree中。
(2)E:
编码(Encoding)。
利用已建好的赫夫曼树(如不在内存,则从文件hfmTree中读入),对文件ToBeTran中的正文进行编码,然后将结果存入文件CodeFile中。
(3)D:
译码(Decoding)。
利用已建好的赫夫曼树将文件CodeFile中的代码进行译码,结果存入文件Textfile中。
以下为选做:
(4)P:
印代码文件(Print)。
将文件CodeFile以紧凑格式显示在终端上,每行50个代码。
同时将此字符形式的编码文件写入文件CodePrin中。
(5)T:
印赫夫曼树(Treeprinting)。
将已在内存中的赫夫曼树以直观的方式(比如树)显示在终端上,同时将此字符形式的赫夫曼树写入文件TreePrint中。
3.测试要求
(1)已知某系统在通信联络中只可能出现八种字符,其频率分别为0.05,0.29,0.07,0.08,0.14,0.23,0.03,0.11,试设计赫夫曼编码。
(2)用下表给出的字符集和频度的实际统计数据建立赫夫曼树,并实现以下报文的编码和译码:
“THISPROGRAMEISMYFAVORITE”。
字符
A
B
C
D
E
F
G
H
I
J
K
L
M
频度
186
64
13
22
32
103
21
15
47
57
1
5
32
20
字符
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
频度
57
63
15
1
48
51
80
23
8
18
1
16
1
4.实现提示
(1)编码结果以文本方式存储在文件Codefile中。
(2)用户界面可以设计为“菜单”方式:
显示上述功能符号,再加上“Q”,表示退出运行Quit。
请用户键入一个选择功能符。
此功能执行完毕后再显示此菜单,直至某次用户选择了“Q”为止。
(3)在程序的一次执行过程中,第一次执行I,D或C命令之后,赫夫曼树已经在内存了,不必再读入。
每次执行中不一定执行I命令,因为文件hfmTree可能早已建好
1.哈夫曼树节点的数据类型定义为:
typedefstruct{//赫夫曼树的结构体
charch;
intweight;//权值
intparent,lchild,rchild;
}htnode,*hfmtree;
2)所实现的功能函数如下
1初始化(Initialization)。
编码(Encoding)译码(Decoding)、voidhfmcoding(hfmtree&HT,hfmcode&HC,intn)初始化哈夫曼树,处理InputHuffman(HuffmanHfm)函数得到的数据,按照哈夫曼规则建立2叉树。
此函数块调用了Select()函数。
voidSelect(hfmtree&HT,inta,int*p1,int*p2)//Select函数,选出HT树到a为止,权值最小且parent为0的2个节点
其代码如下:
#include
#include
#include
#include
#include
typedefstruct{//赫夫曼树的结构体
charch;
intweight;//权值
intparent,lchild,rchild;
}htnode,*hfmtree;
typedefchar**hfmcode;
voidSelect(hfmtree&HT,inta,int*p1,int*p2)//Select函数,选出HT树到a为止,权值最小且parent为0的2个节点
{
inti,j,x,y;//x是记录权值最小的节点的下标,y是记录第二个最小权值节点的下标
for(j=1;j<=a;++j){
if(HT[j].parent==0){
x=j;
break;
}
}
for(i=j+1;i<=a;++i){
if(HT[i].weight{
x=i;
}
}
for(j=1;j<=a;++j){
if(HT[j].parent==0&&x!
=j)
{
y=j;
break;
}
}
for(i=j+1;i<=a;++i)
{
if(HT[i].weight=i)
{
y=i;//选出次小的节点
}
}
if(x>y){//让p1指向下标最小的节点地址
*p1=y;
*p2=x;
}
else
{
*p1=x;
*p2=y;
}
}
voidhfmcoding(hfmtree&HT,hfmcode&HC,intn)//构建赫夫曼树HT,并求出n个字符的赫夫曼编码HC
{
inti,start,c,f,m,w;
intp1,p2;
char*cd,z;
if(n<=1){
cout<<"输入有错"<return;
}
m=2*n-1;
HT=(hfmtree)malloc((m+1)*sizeof(htnode));//分配2n+1个空间存储树节点
for(i=1;i<=n;++i)//初始化n个叶子结点
{
printf("请输入第%d字符和权值:
",i);
scanf("%c%d",&z,&w);
while(getchar()!
='\n')
{
continue;
}
HT[i].ch=z;
HT[i].weight=w;
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
for(;i<=m;++i)//初始化其余的结点
{
HT[i].ch='0';
HT[i].weight=0;
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
for(i=n+1;i<=m;++i)//建立赫夫曼树
{
Select(HT,i-1,&p1,&p2);
HT[p1].parent=i;HT[p2].parent=i;//让最小p1,p2的双亲相同都为i
HT[i].lchild=p1;HT[i].rchild=p2;//让p1,p2分别为i的左右孩子
HT[i].weight=HT[p1].weight+HT[p2].weight;//把i的权值编为最小二权值相加
}
HC=(hfmcode)malloc((n+1)*sizeof(char*));//分配n+1个空间存储编码
cd=(char*)malloc(n*sizeof(char));//分配n个空间分别存储左右孩子的0与1
cd[n-1]='\0';
for(i=1;i<=n;++i)//给n个字符编码
{
start=n-1;
for(c=i,f=HT[i].parent;f!
=0;c=f,f=HT[f].parent)//不是根节点左右分支分别赋值0,1
{
if(HT[f].lchild==c)
{
cd[--start]='0';
}
else
{
cd[--start]='1';
}
}
HC[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);//把数组cd中的内容0,1赋值给HC,HC为编码
}
free(cd);
}
voidInitiation(hfmtree&HT,hfmcode&HC,int&n)
{
ofstreamoutput_file;
cout<<"请输入字符个数:
";
cin>>n;
hfmcoding(HT,HC,n);
cout<for(inti=1;i<=n;++i)
{
cout<"<}
output_file.open("hfmTree.txt");//新建一个文件
if(!
output_file){
cout<<"can'toenfile!
"<}
for(i=1;i<=n;i++)
{
output_file<<"("<}
output_file.close();
cout<cout<<"哈夫曼树建完且放入hfmTree.txt文件中!
"<}
voidEncoding(hfmtree&HT,hfmcode&HC,intn)
{
charcode[100],str[100];
ofstreamoutput_file;
ifstreaminput_file;
printf("请输入字符:
");
gets(str);
output_file.open("ToBeTran.txt");
if(!
output_file)
{
cout<<"can'toenfile!
"<}
output_file<output_file.close();
output_file.open("CodeFile.txt");
if(!
output_file){
cout<<"can'toenfile!
"<}
for(inti=0;ifor(intj=0;j<=n;++j)
{
if(HT[j].ch==str[i])//判断输入的字符是否与编码的字符相同
{
output_file<break;
}
}
}
output_file.close();
cout<<"\n";
cout<<"编码完毕,并且已经存入CodeFile.txt文件!
\n";
input_file.open("CodeFile.txt");//从CodeFile.txt中读入编码,输出在终端,打开文件
if(!
input_file)
{
cout<<"can'toenfile!
"<}
input_file>>code;//从CodeFile.txt中读入编码,并存进code数组中
cout<<"编码码值为:
"<input_file.close();
}
voidDecodeing(hfmtree&HT,hfmcode&HC,char*h,intn)
{charhl[100];
intk;
ifstreaminput_file;//文件输入输出流
ofstreamoutput_file;
input_file.open("CodeFile.txt");//打开文件
if(!
input_file){
cout<<"can'toenfile!
"<}
input_file>>h;//将文件里面的编码存进数组h[]
input_file.close();
output_file.open("Textfile.txt");
if(!
output_file)
{
cout<<"can'toenfile!
"<}
k=0;
while(h[k]!
='\0')//先用编码中的前几个和字符的编码相比较,然后往后移
{
for(inti=1;i<=n;i++){
intl=k;
for(intj=0;jhl[j]=h[l];//把h[]中的编码按字符编码分别一个个存进数组h1[]中
}
hl[j]='\0';
if(strcmp(HC[i],hl)==0)//比较HC与h1中编码字符串的大小,假如相等
{
output_file<k=k+strlen(HC[i]);//移到下一个字符
break;
}
}
}
output_file.close();
input_file.open("Textfile.txt");
if(!
input_file){
cout<<"can'toenfile!
"<}
input_file>>h;
cout<输出编码
input_file.close();
cout<<"译码完成,信息存入Textfile.txt文件中!
"<}
voidmain(){
charh[100],choice;
intn;
hfmtreeHT;//里面存储字符与权值
hfmcodeHC;//里面存储编码,0与1
cout<cout<<"I.初始化"<<""<<"E.编码"<<""<<"D.译码"<<""<<"Q.停止"<while(true)//当choice的值不为q且不为Q时循环
{
cout<<"请选择I,E,D,Q,\n";
cin>>choice;
switch(choice)
{
case'I':
Initiation(HT,HC,n);break;
case'E':
Encoding(HT,HC,n);break;
case'D':
Decodeing(HT,HC,h,n);break;
case'Q':
exit(0);
default:
cout<<"您没有输入正确的步骤,请重新输入!
"<}
cout<}
}
总结:
经过这次课程设计,我重新又再学了一遍,让我更了解哈夫曼树的构建,其中在老师的验收中,老师又给我一些数据,让我重新构建一个权重和最小的哈夫曼树,在这次课程设计中,我学到了很多,懂得虚心请教同学,虚心接受他人意见,这样,使同学间友谊更深,同样,学习问题也解决了,知识也巩固了
。