哈夫曼编/译码器问题:C语言版的数据结构,我急啊!那位朋友帮帮忙,结果必需是我的问题的结果,不能有错啊

作者&投稿:并哀 (若有异议请与网页底部的电邮联系)
谁能帮帮我,《数据结构》课程设计——哈夫曼编译码器设计(用C语言的)?!!!谢谢了!!!~

#include
#include
#include

typedef char* HuffmanCode;/*动态分配数组,存储哈夫曼编码*/

typedef struct
{
unsigned int weight ; /* 用来存放各个结点的权值*/
unsigned int parent, LChild,RChild ; /*指向双亲、孩子结点的指针*/
}HTNode, * 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 CrtHuffmanTree(HuffmanTree *ht , int *w, int n)
{ /* w存放已知的n个权值,构造哈夫曼树ht */
int m,i;
int s1,s2;
m=2*n-1;
*ht=(HuffmanTree)malloc((m+1)*sizeof(HTNode)); /*0号单元未使用*/
for(i=1;i<=n;i++)
{/*1-n号放叶子结点,初始化*/
(*ht)[i].weight = w[i];
(*ht)[i].LChild = 0;
(*ht)[i].parent = 0;
(*ht)[i].RChild = 0;
}
for(i=n+1;i<=m;i++)
{
(*ht)[i].weight = 0;
(*ht)[i].LChild = 0;
(*ht)[i].parent = 0;
(*ht)[i].RChild = 0;
} /*非叶子结点初始化*/
/* ------------初始化完毕!对应算法步骤1---------*/

for(i=n+1;i<=m;i++) /*创建非叶子结点,建哈夫曼树*/
{ /*在(*ht)[1]~(*ht)[i-1]的范围内选择两个parent为0且weight最小的结点,其序号分别赋值给s1、s2返回*/
select(ht,i-1,&s1,&s2);
(*ht)[s1].parent=i;
(*ht)[s2].parent=i;
(*ht)[i].LChild=s1;
(*ht)[i].RChild=s2;
(*ht)[i].weight=(*ht)[s1].weight+(*ht)[s2].weight;
}
}/*哈夫曼树建立完毕*/
void outputHuffman(HuffmanTree HT, int m)
{
if(m!=0)
{
printf("%d ", HT[m].weight);
outputHuffman(HT,HT[m].LChild);
outputHuffman(HT,HT[m].RChild);
}
}

void CrtHuffmanCode(HuffmanTree *ht, HuffmanCode *hc, int n)
/*从叶子结点到根,逆向求每个叶子结点对应的哈夫曼编码*/
{
char *cd;
int i;
unsigned int c;
int start;
int p;
hc=(HuffmanCode *)malloc((n+1)*sizeof(char *)); /*分配n个编码的头指针*/
cd=(char * )malloc(n * sizeof(char )); /*分配求当前编码的工作空间*/
cd[n-1]='\0'; /*从右向左逐位存放编码,首先存放编码结束符*/
for(i=1;i<=n;i++) /*求n个叶子结点对应的哈夫曼编码*/
{
start=n-1; /*初始化编码起始指针*/
for(c=i,p=(*ht)[i].parent; p!=0; c=p,p=(*ht)[p].parent) /*从叶子到根结点求编码*/
if( (*ht)[p].LChild == c)
cd[--start]='0'; /*左分支标0*/
else
cd[--start]='1'; /*右分支标1*/
hc[i]=(char *)malloc((n-start)*sizeof(char)); /*为第i个编码分配空间*/
strcpy(hc[i],&cd[start]);
}
free(cd);
for(i=1;i<=n;i++)
printf("%d编码为%s
",(*ht)[i].weight,hc[i]);
}


void main()
{
HuffmanTree HT;
HuffmanCode HC;
int *w;
int i,n; // the number of elements;
int wei; // the weight of a element;
int m;

printf("input the total number of the Huffman Tree:" );
scanf("%d",&n);
w=(int *)malloc((n+1)*sizeof(int));
for(i=1;i<=n;i++)
{
printf("input the %d element's weight:",i);
fflush(stdin);
scanf("%d",&wei);
w[i]=wei;
}

CrtHuffmanTree(&HT,w,n);
m = 2*n-1;
outputHuffman(HT,m);
printf("
");
CrtHuffmanCode(&HT,&HC,n);

}

#include
#include
typedef struct
{
char character;
unsigned int weight;
unsigned int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char **HuffmanCode;
char *c;
HuffmanTree HT;
HuffmanCode HC;
void menu() //定义菜单函数
{
printf("pleace make the choice:
");
cout<<"***** I: 初始化 *****"<<endl;
cout<<"***** E:编码 *****"<<endl;
cout<<"***** D:译码 *****"<<endl;
cout<<"***** P:印代码文件 *****"<<endl;
cout<<"***** T: 印赫夫曼树 *****"<<endl;
cout<<"***** Q: 退出 *****"<<endl;
}

void Select(HuffmanTree &HT,int end,int &s1,int &s2) //选择权值最小的两个结点
{
int i;
int weight1,weight2;
weight1=1000;

for(i=1;i<=end;i++)
{
if(HT[i].parent==0&&HT[i].weight<weight1)
{
weight1 = HT[i].weight;
s1 = i;
}
}
for(i=1;i<=end;i++)
{
if(HT[i].parent==0&&HT[i].weight<weight2&&(i!=s1))
{
weight2 = HT[i].weight;
s2 = i;
}
}
}
void Initialization(HuffmanTree &HT,HuffmanCode &HC) //建立赫夫曼树
{
FILE *fp,*fp1;
fp=fopen("hfmTree.txt","w+");
fp1=fopen("ht.txt","w+");
int m,n,i,s1,s2,start,f,b;
int *w;
char *cd;
bool flag = true;
printf("please input n:
");
scanf("%d",&n);
fprintf(fp,"%d
",n);
fprintf(fp1,"%d
",n);
if(n<=1)return;
w=(int *)malloc((n+1)*sizeof(int)); //动态申请数组空间存储结点的权值
c=(char *)malloc((n+1)*sizeof(char)); //动态申请数组空间存储结点字符
printf("please input character and weight:
");
for(i=1;i<=n;i++)
{
getchar();
cout<<"请输入第"<<i<<"个字符及其权值:";
scanf("%c",&c[i]);
scanf("%d",&w[i]);
}
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(i=1;i<=n;i++) //初始化赫夫曼树
{
HT[i].character=c[i];
HT[i].weight=w[i];
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
for(i=n+1;i<=m;++i)
{
HT[i].character=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,s1,s2);
HT[s1].parent=i;
HT[s2].parent=i;
HT[i].lchild=s1;HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}
for(i=n+1;i<2*n;i++)
{
fprintf(fp1,"%d ",HT[i].lchild);
fprintf(fp1,"%d
",HT[i].rchild);
}
//---从叶子到根逆向求每个字符的赫夫曼编码
HC = (HuffmanCode)malloc((n+1)*sizeof(char*));//分配n个字符编码的头指针向量
HC[0] = NULL;
cd = (char *)malloc(n*sizeof(char));//分配求编码的工作空间
cd[n-1] = '\0'; //编码结束符
for(i=1;i<=n;i++)//逐个字符求赫夫曼编码
{
start = n-1; //编码结束符位置
for(b=i,f=HT[i].parent;f!=0;b=f,f=HT[f].parent)//从叶子到根逆向求编码
{
if(HT[f].lchild == b)
cd[--start] = '0';
else
cd[--start] = '1';
}
HC[i] = (char *)malloc((n-start)*sizeof(char)); //为第i个字符分配存储空间
strcpy(HC[i],&cd[start]);
printf("字符%c的编码是:",c[i]);
printf("%s
",HC[i]);
fputc(HT[i].character,fp); //将编码写入文件hfmTree中
fputs(HC[i],fp);
fputc('
',fp);
}
free(cd);
fclose(fp);
fclose(fp1);
}
void Encoding(HuffmanTree &HT,HuffmanCode &HC) //利用已建好的赫夫曼树,对文件ToBeTran中的正文进行编码
{
int choice,i,n;
char ch;
ifstream inf("hfmTree.txt",ios::in); //将文件hfmTree.txt中的赫夫曼树读入内存
if(!inf)
{
cerr<<"open hmfTree failed!"<<endl;
exit(1);
}
inf>>n;
inf.get();

HC = (HuffmanCode)malloc((n+1)*sizeof(char*));
c = (char *)malloc((n+1)*sizeof(char));
for(i=1;i<=n;i++)
{
HC[i] = (char *)malloc((n-1)*sizeof(char));
inf.get(c[i]);
inf>>HC[i];
inf.get();
}
inf.close();
getchar();
printf("是否要改写要译码的正文?请选择Y或N
");//选择译文,Y重新创建ToBeTran,N文件ToBeTran已存在
scanf("%c",&ch);
getchar();
if(ch=='Y') //重新创建文件ToBeTran.txt
{
ofstream outfile("ToBeTran.txt",ios::out);
if(!outfile)
{
cerr<<"create file failed!"<<endl;
exit(1);
}
outfile<<"THIS PROGRAME IS MY FAVORITE";
outfile.close();
}

ifstream infile("ToBeTran.txt",ios::in);
if(!infile)
{
cerr<<"open ToBeTran.txt failed!"<<endl;
exit(1);
}
ofstream outfile("CodeFile.txt",ios::out);
if(!outfile)
{
cerr<<"open CodeFile.txt failed!"<<endl;
exit(1);
}
while(true) //讲正文进行译码,写入文件CodeFile.txt
{
infile.get(ch);
if(infile.eof()) break;
for(i=1;i<=n;i++)
{
if(ch == c[i])
{
outfile<<HC[i];
cout<<HC[i]<<" ";
break;
}
}
cout<<endl;
if(i==n+1)
{
cout<<"error!"<<endl;
exit(1);
}
}
infile.close();
outfile.close();
cout<<"sucess!"<<endl;
}
void Decoding(HuffmanTree &HT,HuffmanCode &HC) //译码
{
int i,n;
char ht;
ifstream infile("ht.txt",ios::in);
if(!infile)
{
cerr<<"open ht.txt failed!"<<endl;
exit(1);
}
infile>>n;
infile.get();
HT = (HuffmanTree)malloc((2*n)*sizeof(HTNode));
for(i=0;i<=n;i++)
{
HT[i].lchild=HT[i].rchild=HT[i].parent=0;
}
for(i=n+1;i<=2*n;i++)
{
infile>>HT[i].lchild>>HT[i].rchild;
infile.get();
}
infile.close();
ifstream inf1("hfmTree.txt",ios::in);
if(!inf1)
{
cerr<<"open hmfTree.txt failed!"<<endl;
exit(1);
}
inf1>>n;
inf1.get();
HC = (HuffmanCode)malloc((n+1)*sizeof(char*));
c = (char *)malloc(n*sizeof(char));
for(i=1;i<=n;i++) //从文件hmfTree.txt中读出所有字符及其编码,存入HC[i]和c[i]中
{
HC[i] = (char *)malloc((n-1)*sizeof(char));
inf1.get(c[i]);
inf1>>HC[i];
inf1.get();
}
inf1.close();
ifstream inf("CodeFile.txt",ios::in);
if(!inf)
{
cerr<<"open CodeFile.txt failed!"<<endl;
exit(1);
}
ofstream outfile("TextFile.txt",ios::out);
if(!outfile)
{
cerr<<"open TextFile.txt failed!"<<endl;
exit(1);
}
inf.get(ht);
while(!inf.eof())
{
if(inf.eof()) break;
for(i=2*n-1;HT[i].lchild>0||HT[i].rchild>0;)
{
if(ht=='0')
i = HT[i].lchild;
else if(ht=='1')
i = HT[i].rchild;
inf.get(ht);
}
outfile<<c[i];
}
inf.close();
outfile.close();
cout<<"译码成功!"<<endl;
}
int main()
{
char ch;
menu();
scanf("%c",&ch);
while(true)
{
switch(ch)
{
case 'I':
Initialization(HT,HC);
menu();
cout<<"请输入选项:";
getchar();
scanf("%c",&ch);
break;
case 'E':
Encoding(HT,HC);
menu();
cout<<"请输入选项:";
getchar();
scanf("%c",&ch);
break;
case 'D':
Decoding(HT,HC);
menu();
cout<<"请输入选项:";
getchar();
scanf("%c",&ch);
break;
case 'Q':
exit(0);
break;
default:
cout<<"error!"<<endl;
scanf("%c",&ch);
break;
}
}
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//////////////////////////////////////////////////////////////////////////////
/*定义赫夫曼树结点的结构体变量,存放结点的权值、字符、双亲、坐孩子和右孩子*/
typedef struct{
int weight;
char ch; //增加一个域用于存放该节点的字符
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char **HuffmanCode; //指向赫夫曼编码的指针

//////////////////////////////////////////////////////////////////////////////
/*本程序用到的函数原型*/
void welcome(); //打印操作选择界面
void HuffmanCoding(HuffmanTree &,char *,int *,int);//建立赫夫曼树的算法
void select(HuffmanTree HT,int j,int *s1,int *s2); //从目前已建好的赫夫曼树中选择parent为0且weight最小的两个结点
void Init(); //输入n个字符及其对应的权值,根据权值建立哈夫曼树
void Coding(); //编码
void Decoding(); //译码
void Print_code(); //打印译码好的代码文件
void Print_tree(); //以凹凸表形式打印哈夫曼树
int Read_tree(HuffmanTree &); //从文件中读入赫夫曼树
void find(HuffmanTree &HT,char *code,char *text,int i,int m);//译码时根据01字符串寻找相应叶子节点的递归算法
void Convert_tree(unsigned char T[100][100],int s,int *i,int j);//将内存中的赫夫曼树转换成凹凸表形式的赫夫曼树

HuffmanTree HT; //全局变量,指向存放赫夫曼树的存储空间
int n=0; //全局变量,存放赫夫曼树叶子结点的数目

int main()
{
char select;

while(1)
{
welcome();
scanf("%c",&select);
switch(select)
{
case 'i':
case 'I':Init();break;
case 'c':
case 'C':Coding();break;
case 'd':
case 'D':Decoding();break;
case 'p':
case 'P':Print_code();break;
case 't':
case 'T':Print_tree();break;
case 'e':
case 'E':exit(1);
default :printf("Input error!\n");
}
getchar();
}
return 0;
}

void welcome() //打印操作选择界面
{
printf("*-----------------------------------------------------*\n");
printf("| What do you want to do? |\n");
printf("|-----------------------------------------------------|\n");
printf("| |\n");
printf("| I--------------------------Init the Huffman tree. |\n");
printf("| C--------------------------Code your file. |\n");
printf("| D--------------------------Decode the code. |\n");
printf("| P--------------------------Print the codefile. |\n");
printf("| T--------------------------Print the Huffman tree. |\n");
printf("| |\n");
printf("*-----------------------------------------------------*\n");
}

//////////////////////////////////////////////////////////////////////////////////////
/*初始化函数,输入n个字符及其对应的权值,根据权值建立哈夫曼树,并将其存于文件hfmtree中*/
void Init()
{
FILE *fp;
int i,n,w[52]; //w数组存放n个字符的权值
char character[52]; //存放n个字符
printf("\n输入字符个数 n:");
scanf("%d",&n); //输入字符集大小
printf("输入%d个字符及其对应的权值:\n",n);
for (i=0;i<n;i++)
{
char b=getchar();
scanf("%c",&character[i]);
scanf("%d",&w[i]); //输入n个字符和对应的权值
}
HuffmanCoding(HT,character,w,n); //建立赫夫曼树

if((fp=fopen("hfmtree.txt","w"))==NULL)
printf("Open file hfmtree.txt error!\n");
for (i=1;i<=2*n-1;i++)
{
if(fwrite(&HT[i],sizeof(HTNode),1,fp)!=1) //将建立的赫夫曼树存入文件hfmtree.txt中
printf("File write error!\n");
}
printf("\n建立赫夫曼树成功,已将其存于文件hfmtree.txt中\n");
fclose(fp);

}

///////////////////////////////////////////////////////////////////////////////////
//////建立赫夫曼树的算法///////////////////////////////////////////////////////////
void HuffmanCoding(HuffmanTree &HT,char *character,int *w,int n)
{
int m,i,s1,s2;
HuffmanTree p;
if(n<=1) return;
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(p=HT+1,i=1;i<=n;++i,++p,++character,++w)
{p->ch=*character;p->weight=*w;p->parent=0;p->lchild=0;p->rchild=0;}
for(;i<=m;++i,++p) {p->ch=0;p->weight=0;p->parent=0;p->lchild=0;p->rchild=0;}
for(i=n+1;i<=m;++i)
{
select(HT,i-1,&s1,&s2);
HT[s1].parent=i;HT[s2].parent=i;
HT[i].lchild=s1;HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;
}

}

///////////////////////////////////////////////////////////////////////////////
/*从HT[1]到HT[j]中选择parent为0且weight最小的两个结点,用s1和s2返回其序号*/
void select(HuffmanTree HT,int j,int *s1,int *s2)
{
int i;
//找weight最小的结点
for (i=1;i<=j;i++)
if (HT[i].parent==0)
{*s1=i;break;}
for (;i<=j;i++)
if ((HT[i].parent==0)&&(HT[i].weight<HT[*s1].weight))
*s1=i;
HT[*s1].parent=1;
//找weight次小的结点
for (i=1;i<=j;i++)
if (HT[i].parent==0)
{*s2=i;break;}
for (;i<=j;i++)
if ((HT[i].parent==0)&&(i!=*s1)&&(HT[i].weight<HT[*s2].weight))
*s2=i;

}

///////////////////////////////////////////////////////////////////////////////
/*对文件tobetrans中的正文进行编码,然后将结果存入文件codefile中*/
void Coding()
{
FILE *fp,*fw;
int i,f,c,start;
char *cd;
HuffmanCode HC;
if(n==0)
n=Read_tree(HT);//从文件hfmtree.txt中读入赫夫曼树,返回叶子结点数

/////以下程序段求赫夫曼树中各叶子节点的字符对应的的编码,并存于HC指向的空间中
{
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
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));
strcpy(HC[i],&cd[start]);
}
free(cd);
}
/////////////////////////////////////////////////////////////////////////////////////

if((fp=fopen("tobetrans.txt","rb"))==NULL)
printf("Open file tobetrans.txt error!\n");
if((fw=fopen("codefile.txt","wb+"))==NULL)
printf("Open file codefile.txt error!\n");
char temp;
fscanf(fp,"%c",&temp); //从文件读入第一个字符
while(!feof(fp))
{
for(i=1;i<=n;i++)
if(HT[i].ch==temp) break; //在赫夫曼树中查找字符所在的位置
for(int r=0;HC[i][r]!='\0';r++) //将字符对应的编码存入文件
fputc(HC[i][r],fw);
fscanf(fp,"%c",&temp); //从文件读入下一个字符

}
fclose(fw);
fclose(fp);
printf("\n对文件hfmtree.txt编码成功,结果已存入codefile.txt中。\n\n");
}

/////////////////////////////////////////////////////////////////////////
/*将文件codefile中的代码进行译码,结果存入文件textfile中*/
void Decoding()
{
FILE *fp,*fw;
int m,i;
char *code,*text,*p;

if(n==0)
n=Read_tree(HT);//从文件hfmtree.txt中读入赫夫曼树,返回叶子结点数

if((fp=fopen("codefile.txt","rb"))==NULL)
printf("Open file codefile.txt error!\n");
if((fw=fopen("textfile.txt","wb+"))==NULL)
printf("Open file textfile.txt error!\n");
code=(char *)malloc(sizeof(char));
fscanf(fp,"%c",code); //从文件读入一个字符
for(i=1;!feof(fp);i++)
{
code=(char *)realloc(code,(i+1)*sizeof(char)); //增加空间
fscanf(fp,"%c",&code[i]); //从文件读入下一个字符
}
code[i-1]='\0';
/////////到此codefile.txt文件中的字符已全部读入,存放在code数组中
text=(char *)malloc(100*sizeof(char));
p=text;
m=2*n-1;
if(*code=='0')
find(HT,code,text,HT[m].lchild,m); //从根节点的左子树去找
else
find(HT,code,text,HT[m].rchild,m); //从根节点的右子树去找

for(i=0;p[i]!='\0';i++) //把译码好的字符存入文件textfile.txt中
fputc(p[i],fw);

fclose(fp);
fclose(fw);

printf("\n对codefile.txt文件译码成功,结果已存入textfile.txt文件。\n\n");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
/*将文件codefi1e以紧凑格式显示在终端上,每行50个代码。同时将此字符形式的编码文件写入文件codeprint中。*/
void Print_code()
{
FILE *fp,*fw;
char temp;
int i;

if((fp=fopen("codefile.txt","rb"))==NULL)
printf("Open file codefile.txt error!\n");
if((fw=fopen("codeprint.txt","wb+"))==NULL)
printf("Open file codeprint.txt error!\n");

printf("\n文件codefi1e以紧凑格式显示如下:\n");
fscanf(fp,"%c",&temp); //从文件读入一个字符
for (i=1;!feof(fp);i++)
{
printf("%c",temp);
if(i%50==0) printf("\n");
fputc(temp,fw); //将该字符存入文件codeprint.txt中
fscanf(fp,"%c",&temp); //从文件读入一个字符
}
printf("\n\n此字符形式的编码已写入文件codeprint.txt中.\n\n");

fclose(fp);
fclose(fw);

}

//////////////////////////////////////////////////////////////////////////////////////////////////
/*将已在内存中的哈夫曼树以凹凸表形式显示在屏幕上,同时将此字符形式的哈夫曼树写入文件treeprint中。*/
void Print_tree()
{
unsigned char T[100][100];
int i,j,m=0;
FILE *fp;
if(n==0)
n=Read_tree(HT);//从文件hfmtree.txt中读入赫夫曼树,返回叶子结点数

Convert_tree(T,0,&m,2*n-1); //将内存中的赫夫曼树转换成凹凸表形式的树,存于数组T中

if((fp=fopen("treeprint.txt","wb+"))==NULL)
printf("Open file treeprint.txt error!\n");
printf("\n以凹凸表形式打印已建好的赫夫曼树:\n");
for(i=1;i<=2*n-1;i++)
{
for (j=0;T[i][j]!=0;j++)
{
if(T[i][j]==' ') {printf(" ");fputc(T[i][j],fp);}
else
{printf("%d",T[i][j]);fprintf(fp,"%d\n",T[i][j]);}
}
printf("\n");
}
fclose(fp);
printf("\n此字符形式的哈夫曼树已写入文件treeprint.txt中.\n\n");

}

//////////////////////////////////////////////////////////////////////////////////
/*从文件hfmtree.txt中读入赫夫曼树,返回叶子节点数*/
int Read_tree(HuffmanTree &HT)
{
FILE *fp;
int i,n;
HT=(HuffmanTree)malloc(sizeof(HTNode));
if((fp=fopen("hfmtree.txt","r"))==NULL)
printf("Open file hfmtree.txt error!\n");
for (i=1;!feof(fp);i++)
{
HT=(HuffmanTree)realloc(HT,(i+1)*sizeof(HTNode)); //增加空间
fread(&HT[i],sizeof(HTNode),1,fp); //读入一个节点信息
}
fclose(fp);
n=(i-1)/2;
return n;
}

////////////////////////////////////////////////////////////////
/*译码时根据01字符串寻找相应叶子节点的递归算法*/
void find(HuffmanTree &HT,char *code,char *text,int i,int m)
{

if(*code!='\0') //若译码未结束
{
code++;
if(HT[i].lchild==0&&HT[i].rchild==0) //若找到叶子节点
{

*text=HT[i].ch; //将叶子节点的字符存入text中

text++;
if((*code=='0'))
find(HT,code,text,HT[m].lchild,m); //继续从根节点的左子树找
else
find(HT,code,text,HT[m].rchild,m); //继续从根节点的右子树找
}
else //如果不是叶子节点
if(*code=='0')
find(HT,code,text,HT[i].lchild,m); //从该节点的左子树去找
else
find(HT,code,text,HT[i].rchild,m); //从该节点的右子树去找

}
else
*text='\0'; //译码结束

}

////////////////////////////////////////////////////////////////////////
/*将内存中的赫夫曼树转换成凹凸表形式的赫夫曼树*/
void Convert_tree(unsigned char T[100][100],int s,int *i,int j)
{
int k,l;
l=++(*i);
for(k=0;k<s;k++)
T[l][k]=' ';
T[l][k]=HT[j].weight;
if(HT[j].lchild)
Convert_tree(T,s+1,i,HT[j].lchild);
if(HT[j].rchild)
Convert_tree(T,s+1,i,HT[j].rchild);
T[l][++k]='\0';
}

为了您的安全,请只打开来源可靠的网址
打开网站 取消
来自: http://hi.baidu.com/laizhij/blog/item/4f3edefb6008321e6c22eb34.html

同样期待高手的解答

你哭吧.........


霍夫曼编码思想是什么?
霍夫曼编码是变长编码,思路:对概率大的编的码字短,概率小的编的码字长,这样一来所编的总码长就小,这样编码效率就高。上面那样求是不对的,除非你这6个码字是等概率的,各占1\/6。应该用对应的概率*其对应得码长,再求和。

赫夫曼树编码是怎样编写的?
哈夫曼编码进行压缩的压缩率是根据平均码长来计算的,压缩率比较低。例如:用三位二进行数进行的等长编dao码平均长度为3,而根据哈夫曼树编码的平均码长为:4*0.07+2*0.19+5*0.02+4*0.06+2*0.32+5*0.03+2*0.21+4*0.10=2.61 2.61\/3=0.87=87 其平均码长是等长码的87%,所以...

哈夫曼编码规则
哈夫曼编码是一种将字符编码为可变长度二进制数的压缩算法,由David A. Huffman在1952年提出。哈夫曼编码是一种可变长度编码,它能够将字符集中出现频率较高的字符用较短的编码表示,从而实现对数据的压缩。相对于固定长度编码(如 ASCII 编码),哈夫曼编码能够更好地适应数据的特点,从而实现更高效的压...

霍夫曼(Huffman)编码背景及国内外研究现状
哈夫曼编码(Huffman Coding)是一种编码方式,以哈夫曼树—即最优二叉树,带权路径长度最小的二叉树,经常应用于数据压缩。 在计算机信息处理中,“哈夫曼编码”是一种一致性编码法(又称"熵编码法"),用于数据的无损耗压缩。这一术语是指使用一张特殊的编码表将源字符(例如某文件中的一个符号)进...

哈夫曼的编码
它是一种变长的编码。在编码中,若各码字长度严格按照码字所对应符号出现概率的大小的逆序排列,则编码的平均长度是最小的。(注:码字即为符号经哈夫曼编码后得到的编码,其长度是因符号出现的概率而不同,所以说哈夫曼编码是变长的编码。) 而且哈夫曼编码是按照子树到父亲,而其读码则是完全相反的...

哈夫曼编\/译码器
char ctemp,result[100]="\\0";\/*存放编\/译码结果*\/ for(i=0;i<n;i++){ ctemp=str[i]; for(j=1;j<=n;j++) if(str[i]==HT[j].data) strcat(result,HC[j]); } printf("所求哈夫曼编码为:\\n"); puts(result);}\/\/translate_to_huffmancode void main(){ char ch[N+1];\/*读入字...

huffman编码问题(请用c语言编写)急!!!
sum=0,m=0,j;printf(" 输出哈夫曼编码:\\n"); \/*输出哈夫曼编码*\/ for (i=0;i<n;i++){ j=0;printf(" %s:\\t",ht[i].data);for (k=hcd[i].start;k<=n;k++){ printf("%c",hcd[i].cd[k]);j++;} m+=ht[i].weight;sum+=ht[i].weight*j;printf("\\n");} ...

哈夫曼树霍夫曼树平均码率是什么意思
根据查询百度百科得知,哈夫曼编码(Huffman Coding),又称霍夫曼编码,是一种编码方式,哈夫曼编码是可变字长编码(VLC)的一种。Huffman于1952年提出一种编码方法,该方法完全依据字符出现概率来构造异字头的平均长度最短的码字,有时称之为最佳编码,一般就叫做Huffman编码(有时也称为霍夫曼编码)。平均...

音频数据压缩编码方法中的huffman编码属于无损压缩吗
霍夫曼编码具有一些明显的特点:1) 编出来的码都是异字头码,保证了码的唯一可译性。2) 由于编码长度可变。因此译码时间较长,使得霍夫曼编码的压缩与还原相当费时。3) 编码长度不统一,硬件实现有难度。4) 对不同信号源的编码效率不同,当信号源的符号概率为2的负幂次方时,达到100%的编码效率;...

哈夫曼树和二进制编码有什么不同点?
哈夫曼编码是依据字符出现概率来构造异字头的平均长度最短的码字,比较精准,二进制编码是用预先规定的方法将文字、数字或其他对象编成二进制的数码,或将信息、数据转换成规定的二进制电脉冲信号。二进制是最基础的编码。赫夫曼编码方法:先按出现的概率大小排队,把两个最小的概率相加,作为新的概率 和...

平武县18373993974: 简单的Huffman霍夫曼编码.用C语言实现. -
蓍服海正: #include<stdio.h>#include<conio.h>#include<iostream.h>#include<string.h>#include<stdlib.h>#define MAXVALUE 10000 /*权值最大值*/#define MAXLEAF 30 /*叶子最多个数*/#define MAXNODE MAXLEAF*2-1 /* 结点数的个数*/#define MAXBIT...

平武县18373993974: 哈夫曼编码译码C语言编写
蓍服海正: #include<stdio.h> #include<stdlib.h> #include<string.h> typedef char ElemType; typedef struct{ ElemType elem; unsigned int weight; unsigned int parent,lchild,rchild; }HTNode,*HuffmanTree; typedef char** HuffmanCode; typedef int Status; typedef ...

平武县18373993974: 哈夫曼编码译码C语言编写 -
蓍服海正: #include #include #define null 0#define ok 1#define error 0#define overflow -2#define max_num 10000#define max 60 typedef int status; typedef char **huffmancode; typedef struct{ unsigned int weight; unsigned int parent,lchild,rchild; }htnode,*...

平武县18373993974: 用c语言完成:1.哈夫曼编码/译码器2.内部排序算法的性能分析 -
蓍服海正: 我把网上的程序修改了一下,并整合了,你看看

平武县18373993974: huffman编码、译码 (c语言版) -
蓍服海正: 呵呵,你所有的要求我都有,但是是C++版本的~~没法帮你了 输入字符串:OHOLE!!LOEH 字符频率统计及相应的哈夫曼编码:O: 3: 10 H: 2: 00 L: 2: 111 E: 2: 110!: 2: 01 哈夫曼树打印:------------2------4------------211------------3------7------------------2------------4------------------2 输入电码:(注意此处请输入标准01电码,不作其他检验)001101111111001 译码结果:HELLO!请按任意键继续. . .

平武县18373993974: 对N个字符,N个权值进行哈夫曼编码/译码.用C语言. -
蓍服海正: #include #include #define NULL 0 int size;...

平武县18373993974: 哈夫曼编/译码器的问题:请求高手帮帮忙,课程设计用的,自己写的错误太多了,万分感谢,c描述.拜托了
蓍服海正: 以下程序在c-free下测试成功.算法参考 数据结构(C语言版)——清华大学出版社 请根据题目具体要求进行修改!有问题找我……#include"stdio.h" #include"stdlib.h"#include"string.h" typedef char ElemType; typedef char** ...

平武县18373993974: 霍夫曼编码 用c语言实现 -
蓍服海正: 以前写的,证明最优子结构,随便一本算法书上就有. #include<stdio.h>#include<stdlib.h>#define NIL -2#define Size_Max_bm 30#define left(i) (2*(i)+1)#define right(i) (2*(i)+2)#define swap(a,b) {cjys t;t=(a);(a)=(b);(b)=t;}#define parent(i) ((i)%2?((i)-...

平武县18373993974: 哈夫曼编/译码器 数据结构实践题 -
蓍服海正: 展开全部#include#include typedef struct { char character; unsigned int weight; unsigned int parent,lchild,rchild; }HTNode,*HuffmanTree; typedef char **HuffmanCode; char *c; HuffmanTree HT; HuffmanCode HC; void menu() //定义菜单函数 { printf...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网