哈夫曼树的构建.docx
《哈夫曼树的构建.docx》由会员分享,可在线阅读,更多相关《哈夫曼树的构建.docx(15页珍藏版)》请在冰豆网上搜索。
![哈夫曼树的构建.docx](https://file1.bdocx.com/fileroot1/2023-2/13/408d2871-701e-427e-89ca-b42d85b545c4/408d2871-701e-427e-89ca-b42d85b545c41.gif)
哈夫曼树的构建
#include
#include
#include
#include
#include
#include
usingnamespacestd;
constintuint_max=10000;
typedefstruct
{
intweight;
intparent,lchild,rchild;
}HTNode,*HuffmanTree;//动态分配数组存储赫夫曼树
typedefchar**HuffmanCode;//动态分配数组存储赫夫曼编码表
//--------------------全局变量-----------------------
HuffmanTreeHT;HuffmanCodeHC;
int*w,i,j;
constintn=26;
char*z;
intflag=0;
intnumb=0;
//-----------------求赫夫曼编码---------------------
intmin(HuffmanTreet,inti)
{//此函数将要被voidselect()调用
intj,flag;
intk=uint_max;//取k为不小于可能的值
for(j=1;j<=i;j++)
if(t[j].weightk=t[j].weight;
flag=j;
t[flag].parent=1;
returnflag;
}
//--------------------slect函数----------------------
voidselect(HuffmanTreet,inti,int&s1,int&s2)//s1为最小的两个值中序号小的那个
{
intj;
s1=min(t,i);
s2=min(t,i);
if(s1>s2)
{
j=s1;
s1=s2;
s2=j;
}
}
//-------------------参考课本算法6.12-------------------
voidHuffmanCoding(HuffmanTree&HT,HuffmanCode&HC,int*w,intn)
{//w存放n个字符的权值(均>0),构造赫夫曼树HT,并求出n个字符的赫夫曼编码HC
intm,i,s1,s2,start;
intc,f;
HuffmanTreep;
char*cd;
if(n<=1)
return;//检测结点数是否可以构成树
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));//0号单元未用
for(p=HT+1,i=1;i<=n;++i,++p,++w)
{
p->weight=*w;
p->parent=0;
p->lchild=0;
p->rchild=0;
}
for(;i<=m;++i,++p)
p->parent=0;
for(i=n+1;i<=m;++i)//建赫夫曼树
{//在HT[1~i-1]中选择parent=0且weight最小的两个结点,其序号分别为s1和s2
select(HT,i-1,s1,s2);
HT[s1].parent=HT[s2].parent=i;
HT[i].lchild=s1;
HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
//从叶子到根逆向求每个字符的赫夫曼编码
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));//分配n个字符编码的头指针向量([0]不用)
cd=(char*)malloc(n*sizeof(char));//分配求编码的工作空间
cd[n-1]='\0';//编码结束符
for(i=1;i<=n;i++)
{//逐个字符求赫夫曼编码
start=n-1;//编码结束符位置
for(c=i,f=HT[i].parent;f!
=0;c=f,f=HT[f].parent)//从叶子到根逆向求编码
if(HT[f].lchild==c)
cd[--start]='0';
else
cd[--start]='1';
HC[i]=(char*)malloc((n-start)*sizeof(char));//为第i个字符编码分配空间
strcpy(HC[i],&cd[start]);//从cd复制编码(串)到HC
}
free(cd);//释放工作空间
}
//----------------------初始化赫夫曼链表-------------------------
voidInitialization()
{
flag=1;
intnum2;
cout<<"下面初始化赫夫曼链表"<w=(int*)malloc(n*sizeof(int));//为第26个字符权值分配空间
z=(char*)malloc(n*sizeof(char));//为第26个字符分配空间
cout<<"\n依次显示"<charbase[2];//?
ifstreamfin("abc.txt");
for(i=0;i{
fin>>base;
*(z+i)=*base;//?
fin>>num2;//上面123行
*(w+i)=num2;
}
HuffmanCoding(HT,HC,w,n);
//----------------------------------打印编码---------------------------------------
cout<<"字符"<for(i=1;i<=n;i++)
{
cout<cout<}
//--------------------------将赫夫曼编码写入文件----------------------------
cout<<"下面将赫夫曼编码写入文件"<FILE*htmTree;
charr[]={'','\0'};
if((htmTree=fopen("htmTree.txt","w"))==NULL)
{
cout<<"不能打开文件"<return;
}
for(i=0;i{
fputc(*(z+i),htmTree);
fputs(r,htmTree);
}
for(i=0;i{
fprintf(htmTree,"%6d",*(w+i));
fputs(r,htmTree);
}
for(i=1;i<=n;i++)
{
fputs(HC[i],htmTree);
fputs(r,htmTree);
}
fclose(htmTree);
cout<<"已将字符与对应编码写入根目录下文件htmTree.txt中"<}
//--------------------------获取报文并写入文件---------------------------
voidInputCode()
{
FILE*tobetran;
charstr[100];
if((tobetran=fopen("tobetran.txt","w"))==NULL)
{
cout<<"不能打开文件"<return;
}
cout<<"请输入你想要编码的字符"<gets(str);
fputs(str,tobetran);
cout<<"获取报文成功"<fclose(tobetran);
cout<<"...................."<}
//---------------------------------编码函数---------------------------------
voidEncoding()
{
cout<<"下面对目录下文件tobetran.txt中的字符进行编码"<FILE*tobetran,*codefile;
if((tobetran=fopen("tobetran.txt","rb"))==NULL)
{
cout<<"不能打开文件"<}
if((codefile=fopen("codefile.txt","wb"))==NULL)
{
cout<<"不能打开文件"<}
char*tran;
i=99;
tran=(char*)malloc(100*sizeof(char));
while(i==99)
{
if(fgets(tran,100,tobetran)==NULL)
{
cout<<"不能打开文件"<break;
}
for(i=0;*(tran+i)!
='\0';i++)
{
for(j=0;j<=n;j++)
{
if(*(z+j-1)==*(tran+i))
{
fputs(HC[j],codefile);
if(j>n)
{
cout<<"字符错误,无法编码!
"<break;
}
}
}
}
}
cout<<"„„„„编码完成„„„„"<cout<<"编码写入目录下的codefile.txt中"<fclose(tobetran);
fclose(codefile);
free(tran);
}
//-------------------------译码函数---------------------------
voidDecoding()
{
cout<<"下面对根目录下文件codefile.txt中的字符进行译码"<FILE*codef,*txtfile;
if((txtfile=fopen("\\Textfile.txt","w"))==NULL)
{
cout<<"不能打开文件"<}
txtfile=fopen("Textfile.txt","w");
if((codef=fopen("codefile.txt","r"))==NULL)
{
cout<<"不能打开文件"<}
codef=fopen("codefile.txt","r");
char*work,*work2,i2;
inti4=0,i,i3;
unsignedlonglength=10000;
work=(char*)malloc(length*sizeof(char));
fgets(work,length,codef);
work2=(char*)malloc(length*sizeof(char));
i3=2*n-1;
for(i=0;*(work+i-1)!
='\0';i++)
{
i2=*(work+i);
if(HT[i3].lchild==0)
{
*(work2+i4)=*(z+i3-1);
i4++;
i3=2*n-1;
i--;
}
elseif(i2=='0')i3=HT[i3].lchild;
elseif(i2=='1')i3=HT[i3].rchild;
}
*(work2+i4)='\0';
fputs(work2,txtfile);
cout<<"„„„„译码完成„„„„"<cout<<"内容写入根目录下的文件textfile.txt中"<free(work);//释放工作区
free(work2);//释放工作区
fclose(txtfile);//关闭文件txtfile.txt;
fclose(codef);//关闭文件codef.txt
}
//-----------------------打印编码的函数----------------------
voidCode_printing()
{
cout<<"下面打印根目录下文件CodePrin.txt中编码字符"<FILE*CodePrin,*codefile;
if((CodePrin=fopen("CodePrin.txt","w"))==NULL)
{
cout<<"不能打开文件"<return;
}
if((codefile=fopen("codefile.txt","r"))==NULL)
{
cout<<"不能打开文件"<return;
}
char*work3;
work3=(char*)malloc(51*sizeof(char));
if(fgets(work3,51,codefile)==NULL)
{
cout<<"不能读取文件"<}
else
do
{
fputs(work3,CodePrin);
puts(work3);
}while(strlen(work3)==50&&fgets(work3,51,codefile)!
=NULL);
free(work3);
cout<<"打印结束"<fclose(CodePrin);
fclose(codefile);
}
//------------------------打印赫夫曼树的函数-----------------------
voidcoprint(HuffmanTreestart,HuffmanTreeHT)//start=ht+26这是一个递归算法
{
if(start!
=HT)
{
FILE*TreePrint;
if((TreePrint=fopen("TreePrint.txt","a"))==NULL)
{
cout<<"创建文件失败"<return;
}
numb++;//number=0该变量为已被声明为全局变量
coprint(HT+start->rchild,HT);//递归先序遍历
cout<weight<fprintf(TreePrint,"%d\n",start->weight);
coprint(HT+start->lchild,HT);
numb--;
fclose(TreePrint);
}
}
voidTree_printing(HuffmanTreeHT,intw)
{
HuffmanTreep;
p=HT+w;//p=HT+26
cout<<"下面打印赫夫曼树"<coprint(p,HT);//p=HT+26
cout<<"打印工作结束"<}
//----------------------------------主函数-------------------------------------
intmain()
{
charchoice;
while(choice!
='q')
{
cout<<"(i)初始化哈夫曼表"<cout<<"(w)输入待编码的字符"<cout<<"(e)进行编码、译码、打印编码"<cout<<"(t)打印哈夫曼树"<cout<<"(q)离开"<if(flag==0)
{
cout<<"\n请先初始化哈夫曼链表,输入'i'"<cout<<"(程序将从根目录下的abc.txt文件中读出26个字母及其权值并对字母进行编码)"<}
cin>>choice;
switch(choice)
{
case'i':
Initialization();//初始化赫夫曼表
break;
case'w':
InputCode();//输入待编码的字符
break;
case'e':
Encoding();//进行编码
Decoding();//进行译码
Code_printing();//打印编码
break;
case't':
Tree_printing(HT,2*n-1);//打印26个字母权值形成的哈夫曼树
break;
case'q':
//退出程序
break;
default:
cout<<"输入命令错误"<}
}
free(z);//释放字母所占内存空间
free(w);//释放权值所占内存空间
free(HT);//释放HT结构体所占内存空间
}