哈夫曼编码与译码文档格式.docx
《哈夫曼编码与译码文档格式.docx》由会员分享,可在线阅读,更多相关《哈夫曼编码与译码文档格式.docx(9页珍藏版)》请在冰豆网上搜索。
![哈夫曼编码与译码文档格式.docx](https://file1.bdocx.com/fileroot1/2022-12/12/ace680ba-74c4-45a7-9ef2-ac7152c1d2c7/ace680ba-74c4-45a7-9ef2-ac7152c1d2c71.gif)
{
charch;
intweight;
intparent;
intLChild;
intRChild;
}HTNode,HuffmanTree[MAXSIZE];
typedefstruct//定义结构体
intnumber;
}NUM[MAXSIZE];
typedefchar*HuffmanCode[N+1];
voidmenu(void);
voidgetchars(char*str,int*len);
//输入字符串
voidInit(intA[MAXSIZE],int*n);
//输入结点的权值(备用)
voidCrtHuffmanTree(HuffmanTreeht,NUMw,intn);
//创建哈夫曼树
voidselect(HuffmanTreeht,intpos,int*s1,int*s2);
voidCrtHuffmanCode(HuffmanTreeht,HuffmanCodehc,intn);
//创建哈夫曼编码
voidshowHuffmanCode(HuffmanCodehc,intn,NUMw,char*str,char*code,intlen);
//显示哈夫曼编码及各结点详细信息
voidtotal(charstr[MAXSIZE],NUMw,int*n,intlen);
voidCrtHuffmanDecode(HuffmanTreeht,char*code,intn);
//进行译码工作
voidCrtHuffmanDecode(HuffmanTreeht,char*code,intn)
charresult[MAXSIZE]={0};
introotIndex=n*2-1;
inti,j,root=rootIndex;
printf("
\n传过来的信息是:
\n"
);
puts(&
code[1]);
for(i=j=1;
i<
=(int)strlen(&
code[1])+1;
i++)
{
if(ht[root].RChild==0)
{
result[j++]=ht[root].ch;
root=rootIndex;
}
if(code[i]=='
1'
)
root=ht[root].RChild;
else
root=ht[root].LChild;
}
result[j]=0;
printf("
\n译码的结果是:
\n%s\n\n"
&
result[1]);
puts(&
}
voidtotal(charstr[MAXSIZE],NUMw,int*n,intlen)
inti,j=1,k=1;
for(i=1;
=len;
w[i].number=0;
w[k].ch=str[j];
w[k].number++;
k++;
for(j=2;
j<
len;
j++)
for(i=1;
k;
if(str[j]==w[i].ch)
{
w[i].number++;
break;
}
if(i==k)
w[k].ch=str[j];
w[k].number++;
k++;
*n=k-1;
voidshowHuffmanCode(HuffmanCodehc,intn,NUMw,char*str,char*code,intlen)
inti,j;
字符\t权值\t编码\n"
=n;
%c\t%d\t%s\n"
w[i].ch,w[i].number,hc[i]);
\n字符编码是:
for(j=1;
if(str[i]==w[j].ch)
//printf("
%s"
hc[j]);
strcat(&
code[1],hc[j]);
//break;
puts(&
voidCrtHuffmanCode(HuffmanTreeht,HuffmanCodehc,intn)
inti,c,start,p;
char*cd;
cd=(char*)malloc((n+1)*sizeof(char));
cd[n-1]='
\0'
;
start=n-1;
c=i;
p=ht[i].parent;
while(p!
=0)
--start;
if(ht[p].LChild==c)
cd[start]='
0'
else
c=p;
p=ht[p].parent;
hc[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(hc[i],&
cd[start]);
free(cd);
voidselect(HuffmanTreeht,intpos,int*s1,int*s2)
inti,m1,m2;
=pos;
if(ht[i].parent==0)
m1=i;
break;
if(ht[i].weight<
=ht[m1].weight&
&
ht[i].parent==0)
m1=i;
*s1=m1;
m2=m1;
for(i=1;
if(ht[i].parent==0&
i!
=m2)
{
m1=i;
break;
}
for(i=1;
if(ht[i].weight<
ht[i].parent==0&
m1=i;
*s2=m1;
voidCrtHuffmanTree(HuffmanTreeht,NUMw,intn)
//构造哈夫曼树ht[M+1],w[]存放n个权值
inti,m,s1,s2;
ht[i].ch=w[i].ch;
ht[i].weight=w[i].number;
ht[i].parent=0;
ht[i].LChild=0;
ht[i].RChild=0;
m=2*n-1;
for(i=n+1;
=m;
ht[i].ch='
'
ht[i].weight=0;
select(ht,i-1,&
s1,&
s2);
ht[i].weight=ht[s1].weight+ht[s2].weight;
ht[s1].parent=i;
ht[s2].parent=i;
ht[i].LChild=s1;
ht[i].RChild=s2;
voidgetchars(char*str,int*len)
请输入一行字符串:
gets(&
str[1]);
*len=strlen(str);
/*voidInit(intw[MAXSIZE],int*n)
intnum,i=1;
请输入一组权值(以0结束):
"
scanf("
%d"
num);
while(num)
w[i]=num;
i++;
scanf("
*n=i-1;
}*/
voidmain(void)
HuffmanTreeht;
HuffmanCodehc;
charcode[MAXSIZE]="
NUMw;
intn,len;
charstr[MAXSIZE];
getchars(str,&
len);
total(str,w,&
n,len);
//Init(w,&
n);
CrtHuffmanTree(ht,w,n);
CrtHuffmanCode(ht,hc,n);
showHuffmanCode(hc,n,w,str,code,len);
CrtHuffmanDecode(ht,code,n);
5.测试程序:
输入一段英文Iamsunyu,iamastudent.
显示结果如下:
运行成功!