c语言哈夫曼编码

该博客介绍了如何使用C语言实现哈夫曼编码。首先,通过`getCharCount`函数统计字符串中各字符出现的频率,并进行排序。然后,`proTree`函数构建哈夫曼树,`decode`函数生成编码,`afterDecode`函数将原始字符串编码为哈夫曼编码。最后,`putcode`和`afterDecode`函数用于输出编码后的字符串。
摘要由CSDN通过智能技术生成
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct character
{
char str;
int  count;
int index;
 } character;
 //一棵树需要左孩子,右孩子,非叶子节点上的数字 
 typedef struct Tree
 {
 
  int left;
  int right;
  int count;
  int index;
  char str;
  char code[256];
 }Tree;
//统计字符串的各字符的频率,以及记录不同字符的个数,排序
character *getCharCount(int *count,char *String);
//输出统计后各个字符,频率 
void showChar(Tree *tree,int count);
//形成一个二叉树 
void proTree(Tree *tree,int count,character *chars);
//形成编码
void decode(Tree *tree,int root,int index,char *code);
//编码
void afterDecode(Tree *tree,char *String,int count,char *afterString); 
//编码后输出
void putcode(char *String); 
void putcode(char *String)
{
puts(String);
}
void afterDecode(Tree *tree,char *String,int count,char *afterString)
{
int i,j;
for(i=0;String[i];i++)
{
for(j=0;j<count;j++)
{
if(tree[j].str==String[i])
{
strcat(afterString,tree[j].code);
}
}
}
strcpy(String,afterString);
}
void decode(Tree *tree,int root,int index,char *code) 
{
if(tree[root].left==-1)
{
code[index]=0;
strcpy(tree[root].code,code);
}
else    
{
code[index]='1';
decode(tree,tree[root].left,index+1,code);
code[index]='0';
decode(tree,tree[root].right,index+1,code);
}
}
void proTree(Tree *tree,int count,character *chars)
{
int i;
int j;
for(i=0;i<count;i++)
{
tree[i].index=i;
tree[i].left=-1;
tree[i].right=-1;
tree[i].count=chars[i].count;
tree[i].str=chars[i].str; 
}

for(i=count,j=i-2;i<2*count-1;i++)
{
tree[i].index=i;
tree[i].left=tree[i-1].index;
tree[i].right=tree[j].index;
tree[i].count=tree[i-1].count+tree[j--].count;
tree[i].str=' '; 
}

}
void showChar(Tree *tree,int count) 
{
int i;
printf("下标  字符   频率     编码    左孩子    右孩子\n");
for(i=0;i<2*count-1;i++)
{
printf("%2d  %5c  %4d   %7s   %5d    %6d\n",tree[i].index,tree[i].str,tree[i].count,tree[i].code,tree[i].left,tree[i].right);
}
 } 
character *getCharCount(int *count,char *String)
{
character *p=NULL;
character t;
int i,j=0;
int k=0;
int chars[256]={0};
for(i=0;String[i];i++)
chars[String[i]]++;
for(i=0;i<256;i++)
{
if(chars[i])
{
k++;
}
}
*count=k;

p=(character *)(calloc(sizeof(character),k));

for(i=0;i<256;i++)
{
if(chars[i])
{
p[j].str=i;
p[j++].count=chars[i];
}
}
for(i=0;i<k;i++)
for(j=0;j<k-i-1;j++)
if(p[j].count<p[j+1].count)
{
t=p[j];
p[j]=p[j+1];
p[j+1]=t; 
}
for(i=0;i<k;i++)
p[i].index=i;
return p;

}


int main()
{
char String[256];
char afterString[256]={0};
character *chars;
Tree *tree;
int count;
char code[256];
printf("请输入一串字符串:\n");
gets(String);
chars=getCharCount(&count,String);
tree=(Tree *)(calloc(sizeof(Tree),2*count-1));
proTree(tree,count,chars);
decode(tree,2*count-2,0,code);
showChar(tree,count);
afterDecode(tree,String,count,afterString);
putcode(String);
free(chars);
free(tree);


}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值