哈弗曼编码译码,数组版

本文详细介绍了哈弗曼编码译码的过程,并提供了一个数组版的C语言实现。包括读取文件、编码、创建哈弗曼树、生成哈弗曼编码、总编码、解码和保存文件等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

哈弗曼编码译码,数组版

#include
#include
#include
#define N 120
#define M 2*N-1
int w[120];
char str[N];
int m;
typedef struct
{
 int weight;
 int parent;
 int lchild;
 int rchild;
 char ch;
}HTNode,HafumanTree[M+1];
typedef char* HafumanCode[N+1];
void Du(char s[]){
 FILE *fp;
 char name[25];
 int i=0;
 printf("输入文件路径\n");
 gets(name);
 if((fp=fopen(name,"r"))==NULL)
 {
  printf("打开失败!\n");
  exit(0);
 }
 s[i++]=fgetc(fp);
 while(s[i-1]!=EOF){
  s[i++]=fgetc(fp);
 }
 s[i]='\0';
 fclose(fp);

}
void Save(char s[]){
 FILE *fp;
 char name[25];
 int i=0;
 printf("输入保存的文件路径\n");
 gets(name);
 if((fp=fopen(name,"w"))==NULL)
 {
  printf("保存失败\n");
   exit(0);
 }
 fputs(s,fp);
 printf("保存完毕\n");

}
void Weight(char s[],char str[]){
 int i,j,k=0,x=strlen(s)-1;
 for(i=0;i
  w[i]=0;
 for(i=0;i
 {
  for(j=0;j
   if(str[j]==s[i])
   {
    w[j]++;
    break;
   }
   if(j==k){
    str[k]=s[i];
       w[k++]++;
   }
 }
 str[k]='\0';
  m=k;
}
void  select(HafumanTree ht,int n,int *p,int *q){
 int min1=32767,min2=32767;
 for(int i=0;i
  if(ht[i].parent==0)
  {
   if(min1>ht[i].weight){
   min1=ht[i].weight;
   *p=i;
   }
  }
  for(i=0;i
  if(ht[i].parent==0&&i!=*p)
  {
             if(min2>ht[i].weight&&i!=*p){
        min2=ht[i].weight;
          *q=i;
    }
  }
}
void CHafumanTree(HafumanTree ht,int n,char str[]){
 int g,i,s1,s2;
  g=2*n-1;
  for(i=0;i
   ht[i].weight=w[i];
   ht[i].ch=str[i];
   ht[i].parent=0;
   ht[i].rchild=0;
   ht[i].lchild=0;
  }
  for(i=n;i
   ht[i].weight=0;
   ht[i].parent=0;
   ht[i].rchild=0;
   ht[i].lchild=0;
   ht[i].ch='0';
  }
 for(i=n;i
  select(ht,i,&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 CHafumanCode(HafumanTree ht,HafumanCode hc,int n)
{
 int i,c,p,start;
char *cd;
cd=(char*)malloc((n+1)*sizeof(char));
cd[n]='\0';
for(i=0;i
{
 start=n;
  c=i;
p=ht[i].parent;
while(p!=0)
{--start;
if(ht[p].lchild==c)
cd[start]='0';
else
cd[start]='1';
c=p;
p=ht[p].parent;
}
hc[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(hc[i],&cd[start]);
}
free(cd);
}
char* TotalCoding(HafumanTree ht,HafumanCode hc,int m,char s[]){
 char Code[300005];
 Code[0]='\0';
 for(int i=0;s[i]!='\0';i++)
  for(int j=0;j
   if(s[i]==ht[j].ch)
    strcat(Code,hc[j]);
  printf("编码完毕:%s\n",Code);
  return Code;

}
void DeCoding(HafumanTree ht,HafumanCode hc,int m,char s[]){
 int p,i=0,j;
 p=2*m-2;
 while(s[i]!='\0'){
  if(s[i]=='0')
   p=ht[p].lchild;
  else
   p=ht[p].rchild;
  if(ht[p].lchild==0&&ht[p].rchild==0){
   for(j=0;j
    if(ht[p].weight==ht[j].weight){
     printf("%c",ht[j].ch);
     p=2*m-2;
        break;
    }
  }
  i++;

 }
     
}
void main(){
 HafumanTree ht;
 HafumanCode hc;
 char s[2005];
 char g[3005];
 printf("输入文件路径,进行编码\n");
 Du(s);
 Weight(s,str);
 CHafumanTree(ht,m,str);
 CHafumanCode(ht,hc,m);
 printf("哈弗曼树已创建完毕、n");
 printf("编码开始\n");
 TotalCoding(ht,hc,m,s);
 gets(g);
 DeCoding(ht,hc,m,g);
 Save(g);

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值