哈夫曼树,哈夫曼编码,解码

zeus~ 2010-12-07 06:14:55

#include<stdio.h>

FILE *hfmTree,*CodeFile;

typedef struct
{
unsigned int weight ;
unsigned int parent,LChild,RChild;
}HuffmanTree;

void select(HuffmanTree *ht,int n,int *s1,int *s2)
{ int i; int min;
for(i=1; i<=n; i++)
{
if(ht[i].parent == 0)
{
min = i; i = n+1;
}
}
for(i=1; i<=n; i++)
{
if(ht[i].parent == 0)
{
if(ht[i].weight < ht[min].weight)
min = i;
}
}
*s1 = min;
for(i=1; i<=n; i++)
{
if(ht[i].parent == 0 && i!=(*s1))
{
min = i; i = n+1;
}
}
for(i=1; i<=n; i++)
{
if(ht[i].parent == 0 && i!=(*s1))
{
if(ht[i].weight < ht[min].weight)
min = i;
}
}
*s2 = min;
}

void CreatHuffmanTree(HuffmanTree *ht,int *w, int n)
{
int m=2*n-1;
int i;
int s1,s2;
for(i=1;i<=m;i++)
{
if(i<=n)
{
ht[i].weight=w[i-1];
}
else
{
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,&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;
}
}


void outputHuffman(HuffmanTree *HT, int m)
{
if(m!=0)
{
printf("%d ", HT[m].weight);
fprintf(hfmTree,"%d ",HT[m].weight);
outputHuffman(HT,HT[m].LChild);
outputHuffman(HT,HT[m].RChild);
}
}

void search(int a,int m,HuffmanTree *t,char *s,int collation[100][100]){
int i,b=a,coding[100],j;
for(i=0;b!=m;i++){
if(t[t[b].parent].RChild==b)
coding[i]=1;
else
coding[i]=0;
b=t[b].parent;}
printf("letters:%c,weight:%d编码为",s[a-1],t[a].weight);
for(j=0,i--;i>=0;i--,j++){
printf("%d",coding[i]);
collation[a-1][j]=coding[i]+1;}
printf("\n");
}

void output(char *cod,char *s,HuffmanTree *t,int n){
int i;
HuffmanTree p;
p=t[2*n-1];
for(i=0;cod[i]!='\0';i++){
if(cod[i]=='0')
p=t[p.LChild];
else
p=t[p.RChild];
if(p.LChild==0&&p.RChild==0){
if(cod[i]=='1')
printf("%c",s[t[p.parent].RChild-1]);
else
printf("%c",s[t[p.parent].LChild-1]);
p=t[2*n-1];}
}
}

int main(){
hfmTree=fopen("D:\\我的文档\\hfmTree.txt","w");
CodeFile=fopen("D:\\我的文档\\CodeFile.txt","w");
int a[100],collation[100][100]={},n,i,j,k;
HuffmanTree t[100];
char s[100],cod[100],word[100];
while(printf("输入元素个数:(输入0结束)\n"),scanf("%d",&n),n){
printf("依次输入元素(字符串)\n");
getchar();
for(i=0;i<n;i++)
scanf("%c",&s[i]);
printf("依次输入元素的权值\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
CreatHuffmanTree(t,a,n);
outputHuffman(t,2*n-1);
printf("\n");
for(i=1;i<=n;i++)
search(i,2*n-1,t,s,collation);
printf("\n输入原文:\n");
getchar();
gets(word);
printf("%s\n",word);
for(i=0;word[i]!='\0';i++){
for(j=0;j<n;j++){
if(word[i]==s[j]){
for(k=0;collation[j][k]!=0;k++){
printf("%d",collation[j][k]-1);
fprintf(CodeFile,"%d",collation[j][k]-1);}
break;}
}
}
printf("\n输入哈夫曼编码:\n");
scanf("%s",cod);
output(cod,s,t,n);
printf("\n\n");
}
fclose(hfmTree);
fclose(CodeFile);
return 0;
}



输入:

27
ABCDEFGHIJKLMNOPQRSTUVWXYZ
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 168
THIS PROGRAM IS MY FAVORITE
11010001011000111111000100010100110000
100101010110010111011000111111100101000
11111110011101011000001001001001101101010
(注意:这段编码是连在一起的,去掉回车再输入(论坛说影响效果只能这样))

输出有点问题:不能正常输出THIS PROGRAM IS MY FAVORITE


问题应该出在 void output()上,知道的朋友帮帮忙。
...全文
578 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zeus~ 2010-12-10
  • 打赏
  • 举报
回复
11#,高人,谢谢你了。
logiciel 2010-12-10
  • 打赏
  • 举报
回复
printf("\n输入哈夫曼编码:\n");
scanf("%s",cod);
cod的长度只定义了100,而在输入例子时,输入的哈夫曼编码长度超过100,超过部分被放到变量s中,覆盖了s原来的内容.修改如下:

char s[100],cod[200],word[100]; // char s[100],cod[100],word[100];

lt114896 2010-12-09
  • 打赏
  • 举报
回复
未初始化
把这四个数组初始化一下
memset(collation,0,sizeof(collation));
memset(s,0,sizeof(s));
memset(cod,0,sizeof(cod));
memset(word,0,sizeof(word));

加个头文件
#include "windows.h"
zeus~ 2010-12-09
  • 打赏
  • 举报
回复
不行,还是老样子。

随着cod[](别的数组)的输入改变而改变,

少写了输入二字。

另 s[]是用gets()输入的。
zeus~ 2010-12-08
  • 打赏
  • 举报
回复
问题的解决方法我是找到了,定义一个char 数组cpoy[100],复制s[100];

output(cod,cpoy,t,n)能正常输出,但s[]数组怎么会随着cod[](别的数组)改变而改变,

望高手解释。
zeus~ 2010-12-08
  • 打赏
  • 举报
回复
另外的文件操作不用管(实验要求的)
zeus~ 2010-12-08
  • 打赏
  • 举报
回复
建立哈夫曼树

对给定的信息进行编码

再对编码进行解码
yskcg 2010-12-07
  • 打赏
  • 举报
回复
你程序要实现什么功能???
zeus~ 2010-12-07
  • 打赏
  • 举报
回复
注意:第二行输入Z之后有一个空格。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧