急求哈夫曼编码/译码器课程设计

作者&投稿:印宽 (若有异议请与网页底部的电邮联系)
急求哈夫曼编码/译码器(80)课程设计,望高手帮忙~

我给你个差不多的,你自己修改一下就可以用了
/************Huffman编码和译码****************/


#include
#include
#include
#include
typedef struct
{
int weight;
char ch;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef struct
{
char ch;
char *chs;
}HuffmanCode;
typedef struct
{
char ch;
int weight;
}sw;
typedef struct
{
HuffmanTree HT;
HuffmanCode *HC;
}huf;
void select(HTNode * HT,int n,int *n1,int *n2)
{
int i=1; int n3;
while(HT[i].parent!=0)
i++;
*n1=i;
i++;
while(HT[i].parent!=0) i++;
*n2=i;
if(HT[*n1].weight<HT[*n2].weight)
{ n3=*n1;*n1=*n2;*n2=n3;}
for(i++;i<=n;i++)
{
if(HT[i].parent==0)
{ if(HT[i].weight<HT[*n1].weight)
*n1=i;
else if(HT[i].weight<HT[*n2].weight)
*n2=i;
}
}
}

huf * HuffmanCoding(HuffmanTree HT,HuffmanCode *HC,sw *w,int n,huf *HUF)
{int m,i,s1,s2,start,c,f;
HuffmanTree p;
char *cd;

if(n<=1) return 0;
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(p=HT+1,i=1;i<=n;i++,p++,w++)
{p->weight=w->weight;p->ch=w->ch;p->parent=0;p->lchild=0;p->rchild=0;}
for(;i<=m;i++,p++)
{p->weight=0;p->ch='^';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;
}
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].ch=HT[i].ch;
HC[i].chs=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i].chs,&cd[start]);
printf("%c %-10s
",HC[i].ch,HC[i].chs);
}

HUF->HT=HT;
HUF->HC=HC;
return HUF;
}
char * convert(char *chars,char *chars1,HuffmanCode *hc,int n)
{
char *p=chars; int i;
strcpy(chars1,"");

while(*p)
{
i=1; while(hc[i].ch!=*p&&i<=n) i++;
strcat(chars1,hc[i].chs); p++;
}

printf("the chars translate are:%s
",chars1);
return chars1;
}
void transcode(HuffmanTree ht,char *chars2,char*chars3)
{
int i=1,p; char *q=chars2;char *r=chars3;

while(ht[i].parent!=0) i++;
p=i;

while(*q)
{
while(ht[p].lchild!=0 && *q)
{
if(*q=='0')
p=ht[p].lchild;
else p=ht[p].rchild;
q++;
}
if(ht[p].lchild==0)
{*r=ht[p].ch;r++;}
p=i;

}

*r='\0';
printf("the chars are:");
puts(chars3);

}


void input(int *n,sw *w)
{
int i;
printf("input the mount of char:");
scanf("%d",n);

for(i=1;i<=*n;i++,w++)
{printf("input the %dth char and weight:",i);
fflush(stdin);
scanf("%c%d",&w->ch,&w->weight);
}

}
void main(void)
{HTNode HT;
HuffmanCode HC,*hc;
HuffmanTree ht;
huf *HUF,huf2;
int n;
sw w[40];
char ch,inchar[500],outchar[1000];
char *abc;
char *p=inchar;
input(&n,w);
HUF=HuffmanCoding(&HT,&HC,w,n,&huf2);
printf("input chars to translate,ends of '#':");
fflush(stdin);//清除流,解决输入干扰
ch=getchar();
while(ch!='#')
{*p=ch;
p++;
ch=getchar();
}
*p='\0';
hc=HUF->HC;
ht=HUF->HT;
abc=convert(inchar,outchar,hc,n);
transcode(ht,abc,outchar);
}

同学要自己做!!!

我给你个差不多的,你自己修改一下就可以用了
/************Huffman编码和译码****************/

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
int weight;
char ch;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef struct
{
char ch;
char *chs;
}HuffmanCode;
typedef struct
{
char ch;
int weight;
}sw;
typedef struct
{
HuffmanTree HT;
HuffmanCode *HC;
}huf;
void select(HTNode * HT,int n,int *n1,int *n2)
{
int i=1; int n3;
while(HT[i].parent!=0)
i++;
*n1=i;
i++;
while(HT[i].parent!=0) i++;
*n2=i;
if(HT[*n1].weight<HT[*n2].weight)

for(i++;i<=n;i++)
{
if(HT[i].parent==0)
{ if(HT[i].weight<HT[*n1].weight)
*n1=i;
else if(HT[i].weight<HT[*n2].weight)
*n2=i;
}
}
}

huf * HuffmanCoding(HuffmanTree HT,HuffmanCode *HC,sw *w,int n,huf *HUF)
{int m,i,s1,s2,start,c,f;
HuffmanTree p;
char *cd;

if(n<=1) return 0;
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(p=HT+1,i=1;i<=n;i++,p++,w++)

for(;i<=m;i++,p++)

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;
}
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].ch=HT[i].ch;
HC[i].chs=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i].chs,&cd[start]);
printf("%c %-10s\n",HC[i].ch,HC[i].chs);
}

HUF->HT=HT;
HUF->HC=HC;
return HUF;
}
char * convert(char *chars,char *chars1,HuffmanCode *hc,int n)
{
char *p=chars; int i;
strcpy(chars1,"");

while(*p)
{
i=1; while(hc[i].ch!=*p&&i<=n) i++;
strcat(chars1,hc[i].chs); p++;
}

printf("the chars translate are:%s\n",chars1);
return chars1;
}
void transcode(HuffmanTree ht,char *chars2,char*chars3)
{
int i=1,p; char *q=chars2;char *r=chars3;

while(ht[i].parent!=0) i++;
p=i;

while(*q)
{
while(ht[p].lchild!=0 && *q)
{
if(*q=='0')
p=ht[p].lchild;
else p=ht[p].rchild;
q++;
}
if(ht[p].lchild==0)

p=i;

}

*r='\0';
printf("the chars are:");
puts(chars3);

}

void input(int *n,sw *w)
{
int i;
printf("input the mount of char:");
scanf("%d",n);

for(i=1;i<=*n;i++,w++)
{printf("input the %dth char and weight:",i);
fflush(stdin);
scanf("%c%d",&w->ch,&w->weight);
}

}
void main(void)
{HTNode HT;
HuffmanCode HC,*hc;
HuffmanTree ht;
huf *HUF,huf2;
int n;
sw w[40];
char ch,inchar[500],outchar[1000];
char *abc;
char *p=inchar;
input(&n,w);
HUF=HuffmanCoding(&HT,&HC,w,n,&huf2);
printf("input chars to translate,ends of '#':");
fflush(stdin);//清除流,解决输入干扰
ch=getchar();
while(ch!='#')
{*p=ch;
p++;
ch=getchar();
}
*p='\0';
hc=HUF->HC;
ht=HUF->HT;
abc=convert(inchar,outchar,hc,n);
transcode(ht,abc,outchar);
}


我有实验报告,全套都有,我做的就是这个课程设计,不懂的地方可以问我啊,呵呵,希望可以帮到你啊!
下边的是我程序的源代码。如果需要流程图报告的话可以找我要,我的qq451543190

#include<iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
# define MaxN 100//初始设定的最大结点数
# define MaxC 1000//最大编码长度
# define ImpossibleWeight 10000//结点不可能达到的权值
# define n 26//字符集的个数
//-----------哈夫曼树的结点结构类型定义-----------
typedef struct //定义哈夫曼树各结点
{
int weight;//权值
int parent;//双亲结点下标
int lchild;//左孩子结点下标
int rchild;//右孩子结点下标
}HTNode,*HuffmanTree;//动态分配数组存储哈夫曼树
typedef char**HuffmanCode;//动态分配数组存储哈夫曼编码表
//-------全局变量--------
HuffmanTree HT;
HuffmanCode HC;
int *w;//权值数组
//const int n=26;//字符集的个数
char *info;//字符值数组
int flag=0;//初始化标记
//**********************************************************************
//初始化函数
//函数功能: 从终端读入字符集大小n , 以及n个字符和n个权值,建立哈夫曼树,并将它存于文件hfmTree中
//函数参数:
//向量HT的前n个分量表示叶子结点,最后一个分量表示根结点,各字符的编码长度不等,所以按实际长度动态分配空间
void Select(HuffmanTree t,int i,int &s1,int &s2)
{ //s1为最小的两个值中序号最小的那个
int j;

int k=ImpossibleWeight;//k的初值为不可能达到的最大权值
for(j=1;j<=i;j++)
{
if(t[j].weight<k&&t[j].parent==0)
{k=t[j].weight;
s1=j;}
}
t[s1].parent=1;
k=ImpossibleWeight;
for(j=1;j<=i;j++)
{
if(t[j].weight<k&&t[j].parent==0)
{k=t[j].weight;
s2=j;}
}
t[s2].parent=1;
}
void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int num)//w存放n个字符的权值(均>0),构造哈夫曼树HT,并求出n个字符的哈弗曼编码HC
{
int i,m,c,s1,s2,start,f;
HuffmanTree p;
char* cd;
if(num<=1) return;
m=2*num-1;//m为结点数,一棵有n个叶子结点的哈夫曼树共有2n-1个结点,可以存储在一个大小为2n-1的一维数组中
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));//0号单元未用
//--------初始化哈弗曼树-------
for(p=HT+1,i=1;i<=num;i++,p++,w++)
{
p->weight=*w;
p->parent=0;
p->lchild=0;
p->rchild=0;
}
for(i=num+1;i<=m;i++,p++)
{
p->weight=0;
p->parent=0;
p->lchild=0;
p->rchild=0;
}
//--------建哈夫曼树-------------
for(i=num+1;i<=m;i++)
{
Select(HT,i-1,s1,s2);//在HT[1...i-1]选择parent为0且weight最小的两个结点,其序号分别为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;
}
//-------从叶子到根逆向求每个字符的哈弗曼编码--------
HC=(HuffmanCode)malloc((num+1)*sizeof(char *));//指针数组:分配n个字符编码的头指针向量
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';//判断是左孩子还是右孩子(左为0右为1)
else cd[--start]='1';
HC[i]=(char*)malloc((num-start)*sizeof(char*));//按所需长度分配空间
int j,h;
strcpy(HC[i],&cd[start]);
}
free(cd);
}
//****************初始化函数******************
void Initialization()
{
flag=1;//标记为已初始化
int i;
w=(int*)malloc(n*sizeof(int));//为26个字符权值分配空间
info=(char*)malloc(n*sizeof(char));//为26个字符分配空间
ifstream infile("ABC.txt",ios::in);
if(!infile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
for(i=0;i<n;i++)
{
infile>>info[i];
infile>>w[i];
}
infile.close();
cout<<"读入字符成功!"<<endl;
HuffmanCoding(HT,HC,w,n);
//------------打印编码-----------
cout<<"依次显示各个字符的值,权值或频度,编码如下"<<endl;
cout<<"字符"<<setw(6)<<"权值"<<setw(11)<<"编码"<<endl;
for(i=0;i<n;i++)
{
cout<<setw(3)<<info[i];
cout<<setw(6)<<w[i]<<setw(12)<<HC[i+1]<<endl;
}
//---------将建好的哈夫曼树写入文件------------
cout<<"下面将哈夫曼树写入文件"<<endl;
ofstream outfile("hfmTree.txt",ios::out);
if(!outfile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
for(i=0;i<n;i++,w++)
{
outfile<<info[i]<<" ";
outfile<<w[i]<<" ";
outfile<<HC[i+1]<<" ";
}
outfile.close();
cout<<"已经将字符与对应的权值,编码写入根目录下文件hfmTree.txt"<<endl;
}
//*****************输入待编码字符函数*************************
void Input()
{
char string[100];
ofstream outfile("ToBeTran.txt",ios::out);
if(!outfile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
cout<<"请输入你想要编码的字符串(字符个数应小于100),以#结束"<<endl;
cin>>string;
for(int i=0;string[i]!='\0';i++)
{
if(string[i]=='\0') break;
outfile<<string[i];
}
cout<<"获取报文成功"<<endl;
outfile.close();
cout<<"------"<<"已经将报文存入根目录下的ToBeTran.txt文件"<<endl;
}
//******************编码函数****************
void Encoding()
{
int i,j;
char*string;
string=(char*)malloc(MaxN*sizeof(char));
cout<<"下面对根目录下的ToBeTran.txt文件中的字符进行编码"<<endl;
ifstream infile("ToBeTran.txt",ios::in);
if(!infile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
for(i=0;i<100;i++)
{

infile>>string[i];

}
for(i=0;i<100;i++)
if(string[i]!='#')
cout<<string[i];
else break;
infile.close();
ofstream outfile("CodeFile.txt",ios::out);
if(!outfile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
for(i=0;string[i]!='#';i++)
{
for(j=0;j<n;j++)
{
if(string[i]==info[j])
outfile<<HC[j+1];
}
}
outfile<<'#';
outfile.close();
free(string);
cout<<"编码完成------";
cout<<"编码已写入根目录下的文件CodeFile.txt中"<<endl;
}
//******************译码函数****************
void Decoding()
{
int j=0,i;
char *code;
code=(char*)malloc(MaxC*sizeof(char));
char*string;
string=(char*)malloc(MaxN*sizeof(char));
cout<<"下面对根目录下的CodeFile.txt文件中的代码进行译码"<<endl;
ifstream infile("CodeFile.txt",ios::in);
if(!infile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
for( i=0;i<MaxC;i++)
{ infile>>code[i];
if(code[i]!='#')
{
cout<<code[i];
}
else break;
}
infile.close();
int m=2*n-1;
for(i=0;code[i-1]!='#';i++)
{
if(HT[m].lchild==0)
{
string[j]=info[m-1];
j++;
m=2*n-1;
i--;
}
else
if(code[i]=='1')
m=HT[m].rchild;
else
if(code[i]=='0')
m=HT[m].lchild;
}
string[j]='#';
ofstream outfile("TextFile.txt",ios::out);
if(!outfile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
cout<<"的译码为------"<<endl;
for( i=0;string[i]!='#';i++)
{
outfile<<string[i];
cout<<string[i];
}
outfile<<'#';
outfile.close();
cout<<"------译码完成------"<<endl;
cout<<"译码结果已写入根目录下的文件TextFile.txt中"<<endl;
free(code);
free(string);
}
//*************打印编码函数****************
void Code_printing()
{
int i;
char *code;
code=(char*)malloc(MaxC*sizeof(char));
cout<<"下面打印根目录下文件CodeFile.txt中的编码"<<endl;
ifstream infile("CodeFile.txt",ios::in);
if(!infile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
for( i=0;i<MaxC;i++)
{
infile>>code[i];
if(code[i]!='#')
cout<<code[i];
else break;
}
infile.close();
cout<<endl;
ofstream outfile("CodePrin.txt",ios::out);
if(!outfile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
for(i=0;code[i]!='#';i++)
{
outfile<<code[i];
}
outfile.close();
free(code);
cout<<"------打印结束------"<<endl;
cout<<"该字符形式的编码文件已写入文件CodePrin.txt中"<<endl;
}
//*************打印哈夫曼树函数****************
int numb=0;
void coprint(HuffmanTree start,HuffmanTree HT) //start=ht+26这是一个递归算法
{
if(start!=HT)
{
ofstream outfile("TreePrint.txt",ios::out);
if(!outfile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
numb++; //number=0 该变量为已被声明为全局变量
coprint(HT+start->rchild,HT); //递归先序遍历
cout<<setw(5*numb)<<start->weight<<endl;
//if(start->rchild==0) cout<<info[start-HT-1]<<endl;
outfile<<start->weight;
coprint(HT+start->lchild,HT);
numb--;
outfile.close();
}
}
void Tree_printing(HuffmanTree HT,int num)
{
HuffmanTree p;
p=HT+2*num-1; //p=HT+26
cout<<"下面打印赫夫曼树"<<endl;
coprint(p,HT); //p=HT+26
cout<<"打印工作结束"<<endl;
}
//*************主函数**************************
int main()
{
char choice;
do{
cout<<"************哈弗曼编/译码器系统***************"<<endl;
cout<<"请选择您所需功能:"<<endl;
cout<<"<I>:初始化哈弗曼树"<<endl;
cout<<"<W>:输入待编码字符串"<<endl;
cout<<"<E>:利用已建好的哈夫曼树进行编码"<<endl;
cout<<"<D>:利用已建好的哈夫曼树进行译码"<<endl;
cout<<"<P>:打印代码文件"<<endl;
cout<<"<T>:打印哈夫曼树"<<endl;
cout<<"<Q>:退出"<<endl;
if(flag==0)
{
cout<<"请先初始化哈夫曼树,输入I"<<endl;
cout<<"<系统将从根目录下的文件ABC.txt中读出26个字母并对字母进行编码>"<<endl;
}
cin>>choice;
switch(choice)
{
case 'I':Initialization();break;
case 'W':Input();break;
case 'E':Encoding();break;
case 'D':Decoding();break;
case 'P':Code_printing();break;
case 'T':Tree_printing(HT,n);break;
case 'Q':;break;
default:cout<<"输入的命令出错,请重新输入!"<<endl;
}
}while(choice!='Q');
free(w);
free(info);
free(HT);
free(HC);
system("pause");
return 0;
}


求哈夫曼编码
void Init(); \/\/输入n个字符及其对应的权值,根据权值建立哈夫曼树void Coding(); \/\/编码void Decoding(); \/\/译码void Print_code(); \/\/打印译码好的代码文件void Print_tree(); \/\/以凹凸表形式打印哈夫曼树int Read_tree(HuffmanTree &); \/\/从文件中读入赫夫曼树void find(HuffmanTree &HT,char *code...

哈夫曼编码码长怎么算
设某信源产生有五种符号u1、u2、u3、u4和u5,对应概率P1=0.4,P2=0.1,P3=P4=0.2,P5=0.1。霍夫曼编码是变长编码,思路:对概率大的编的码字短,概率小的编的码字长,这样一来所编的总码长就小,这样编码效率就高。上面那样求是不对的,除非你这6个码字是等概率的,各占1\/6。应该用...

计算哈夫曼编码
计算哈夫曼编码假设字符a、b、c、d、e、f的使用频度分别为0.04,0.06,0.13,0.25,0.28,0.33,写出a、b、c、d、e、f的Huffman(哈夫曼... 计算哈夫曼编码假设字符a、b、c、d、e、f的使用频度分别为0.04,0.06,0.13,0.25,0.28,0.33,写出a、b、c、d、e、f的Huffman(哈夫曼)编码和该哈夫曼树的带权路径长度。

如何计算Huffman编码的编码效率和压缩比?
哈夫曼编码进行压缩的压缩率是根据平均码长来计算的,压缩率比较低。例如:用三位二进行数进行的等长编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%,所以...

画出哈夫曼树,并求出每个字符的哈夫曼编码
42 32 \/ \\ \/ \\ 23 19 12 20 \/ \\ \/ \\ 15 8 9 10 \/ \\ 8 7 \/ \\ 3 5 编码:A(010)B(00000)C(00001)D(001)E(10)F(11)G(0001)H(011)带权路径长度值为:(3+5)*5+7*4+(8+9+10)*3+(12+20)*2=213...

哈夫曼编码的扩展操作码是怎么算的
哈夫曼编码 根据上面可得编码表: a:1001 b:01 c:10111 d:1010 e:11 f:10110 g:00 h:1000 用三位二进行数进行的等长编码平均长度为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=...

哈夫曼编码码长怎么算
解: (1)哈夫曼编码 根据上图可得编码表: a:1001 b:01 c:10111 d:1010 e:11 f:10110 g:00 h:1000 (2)用三位二进行数进行的等长编码平均长度为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...

一道关于求哈夫曼编码的数据结构题,求解答
哈夫曼编码首先要构造哈夫曼树,其构造规则是从概率这个序列中选择两个最小结点的值构造一颗树,新的树根的权值为两个子树的概率权值和。如题中,首先选择0.02 和 0.03构造一颗树,将权值之和放回序列中,为:0.07 0.19 0.10 0.32 0.21 0.06 0.05 继续上述过程只剩下一颗树为止。最终哈...

哈夫曼编码 急需!满意即追加分 谢谢了
哈夫曼编码步骤:一、对给定的n个权值{W1,W2,W3,...,Wi,...,Wn}构成n棵二叉树的初始集合F= {T1,T2,T3,...,Ti,...,Tn},其中每棵二叉树Ti中只有一个权值为Wi的根结点,它的左右子树均为空。(为方便在计算机上实现算 法,一般还要求以Ti的权值Wi的升序排列。)二、在F中选取两棵根...

求有关哈夫曼编码的问题?
如下: ( 5364 )\/ \\ (2251) ( 3113 )\/ \\ \/ \\ (1059) 1192 ( 1327 )

连山区18592259051: 急!!高分求关于 哈夫曼编码 课程设计程序 -
钱骆瑞高: #include<string.h> #include<stdlib.h> #include<stdio.h> int m,s1,s2; typedef struct { unsigned int weight; unsigned int parent,lchild,rchild; }HTNode,*HuffmanTree; //动态分配数组存储哈夫曼树 typedef char *HuffmanCode; //动态分配数组存储哈夫...

连山区18592259051: 求一个<哈夫曼编码>数据结构课程设计(C语言版) -
钱骆瑞高: 我帮你测试了,这个可以满足你的要求! #include #include #define max 50 struct a { int weight; int parent,lchild,rchild; }; struct b { char cd[max]; int start; }; void main() { struct a ht[2*max]; struct b hcd[max],d; int i,k,n,c,s1,s2,m1,m2,f; printf("输入n:"); scanf("%d",&n); for(i=1;i

连山区18592259051: 我们有个数据结构的哈夫曼编码解码的课程设计,你能帮帮我吗 -
钱骆瑞高: 树和哈夫曼树实验报告一.实验目的练习树和哈夫曼树的有关操作,和各个算法程序,理解哈夫曼树的编码和译码二.实验环境 Microsoft visual c++三.实

连山区18592259051: 哈夫曼编码/译码器编程 -
钱骆瑞高: #include #include #define M 10000 //定义字符串最大长度#define N 128 //定义叶子节点个数 typedef struct node //定义哈夫曼树节点结构体 { int weight; struct node *LChild,*RChild,*Parent; //分别指向该节点的左孩子,右孩子,和双亲节点 struct ...

连山区18592259051: C++课程设计:哈夫曼编码器
钱骆瑞高: #include <iostream.h>#include <iomanip.h>#include <string.h>#include <malloc.h>#include <stdio.h>//typedef int TElemType;const int UINT_MAX = 1000;typedef struct{ int weight; int parent, lchild, rchild;} HTNode, *HuffmanTree;typedef char **...

连山区18592259051: 设计和编制哈夫曼编译码器. -
钱骆瑞高: #include<stdio.h>#include<iostream.h>#include<windows.h>#include<stdlib.h>#include<string.h>#define N 100 typedef char* *huffmancode; //动态分配数组存放哈弗曼编码表 typedef struct { char index; int weight; int lchild; int rchild; int parent; }...

连山区18592259051: 谁有哈夫曼编码译码程序啊
钱骆瑞高: #include<stdio.h> #include<malloc.h> #include<string.h> typedef struct{ char name; int weight; int parent,lchild,rchild; }HTNode,*HuffmanTree; typedef char ** HuffmanCode; //查找权值最小的两个结点 void Select(HuffmanTree &HT,int i,int & m,int ...

连山区18592259051: 求高手写个哈夫曼编码和译码的程序啊
钱骆瑞高: /*有bug可给我留言*/ #include &lt;stdio.h&gt; #define MAXBIT 10 /*定义哈夫曼编码的最大长度*/ #define MAXVALUE 10000 /*定义最大权值*/ #define MAXLEAF 30 /*定义哈夫曼树中最多叶子节点个数*/ #define MAXNODE MAXLEAF*2-1 /*哈夫...

连山区18592259051: 哈夫曼编码/译码器 -
钱骆瑞高: 生成哈夫曼树的代码如下:#define INT_MAX 10000#define ENCODING_LENGTH 1000#include "stdio.h"#include "string.h"#include "malloc.h" typedef enum{none,left_child,right_child} Which;//标记是左孩子还是右孩子 typedef char ...

连山区18592259051: 1、二叉树的应用 - 哈夫曼树(电文的编码和译码)哈夫曼编码/译码器 问题描述:设计一个哈夫曼编码/译码系统,对字符串进行编码/译码 基本要求: (1)... -
钱骆瑞高:[答案] #include int n; int m=2*n-1; struct tree { float weight; int parent; int lch,rch; }; struct codetype { int bits[100]; int start; char ch; }; tree hftree[100]; codetype code[99]; void creathuffmantree(int n,int m) { int i,j ,p1,p2; float s1,s2; for(i=1;i {hftree[i].parent=0; hftree[...

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