目录
实验内容
哈夫曼编码是一种以哈夫曼树(最优二叉树,带权路径长度最小的二叉树) 为基础
变长编码
方法。其
基本思想
是:将使用次数多的代码转换成长度较短的编码,而使用次数少的采用较长的编码,并且保持编码的唯一可解性。在计算机信息处理中,经常应用于
数据压缩
。是一种一致性编码法(又称
"
熵编码
法
"
),用于数据的
无损压缩
。要求实现一个完整的哈夫曼
编码
与
译码
系统。
实验要求
1.
从文件中读入任意一篇英文文本文件,分别统计英文文本文件中各字符(包 括标点符号和空格)的使用频率;
2.
根据已统计的字符使用频率构造哈夫曼编码树,并给出每个字符的哈夫曼编 码(字符集的哈夫曼编码表);
3.
将文本文件利用哈夫曼树进行编码,存储成压缩文件(哈夫曼编码文件);
4.
计算哈夫曼编码文件的压缩率;
5.
将哈夫曼编码文件译码为文本文件,并与原文件进行比较。
测试结果
完整代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node
{
char data;
int weight;
int lchild;
int rchild;
int parent;
}Node;
typedef struct Code
{
char data;
char bit[66];
}Code;
//0-9为数字;10-35为小写字母;36-61为大写字母;62-64为特殊字符
void Initial_Hoffman_tree(Node t[])
{
int i;
char num='0';
char charcter_U='A';
char charcter_N='a';
for(i=0;i<129;i++)
{
t[i].lchild=t[i].rchild=t[i].parent=-1;
t[i].weight=0;
if(i>=0&&i<=9)
{
t[i].data=num;
num++;
}
else if(i>=10&&i<=35)
{
t[i].data=charcter_N;
charcter_N++;
}
else if(i>=36&&i<=61)
{
t[i].data=charcter_U;
charcter_U++;
}
else if(i==62)
{
t[i].data=',';
}
else if(i==63)
{
t[i].data='.';
}
else if(i==64)
{
t[i].data=' ';
}
}
}
void Select_Min(Node hoffman[],int len,int *p1,int *p2)
{
int i=0;
int min1,min2;//min1最小,min2第二小
for(i=0;i<len;i++)//给min1和min2第一个未加入树的初值
{
if(hoffman[i].parent == -1)
{
min1=i;
break;
}
}
for(i=min1+1;i<len;i++)
{
if(hoffman[i].parent == -1)
{
min2=i;
break;
}
}
int temp;
if(hoffman[min1].weight>hoffman[min2].weight)//使初始化的min1<min2
{
temp=min1;
min2=min1;
min1=temp;
}
i=min2+1;
while(i<len)
{
if(hoffman[i].parent == -1)
{
if(hoffman[i].weight<=hoffman[min1].weight)
{
min2=min1;
min1=i;
}
else if(hoffman[i].weight>=hoffman[min1].weight&&hoffman[i].weight<=hoffman[min2].weight)
{
min2=i;
}
}
i++;
}
*p1=min1;
*p2=min2;
}
void Create(Node hoffman[])
{
int i=65;
int p1,p2;
for(i=65;i<129;i++)
{
Select_Min(hoffman,i,&p1,&p2);
hoffman[p1].parent=i;
hoffman[p2].parent=i;
hoffman[i].lchild=p1;
hoffman[i].rchild=p2;
hoffman[i].weight=hoffman[p1].weight+hoffman[p2].weight;
}
printf("Creating finished!\n");
}
void HoffmanCode(Node hoffman[],Code T[])//左孩子为0,右孩子为1
{
int i=0;
int p=0;
int temp;//用于指向移动前的位置,判断为左还是右孩子
char c[66];//临时存放编码
int start=65;
c[65]='\0';
for(i=0;i<65;i++)
{
T[i].data=hoffman[i].data;
p=i;
temp=i;
start=64;
while(hoffman[p].parent != -1)//回溯至根
{
p=hoffman[p].parent;
if(hoffman[p].lchild==temp)
{
c[start]='0';
start--;
}
else
{
c[start]='1';
start--;
}
temp=p;
}
strcpy(T[i].bit,&c[start+1]);
}
printf("Coding finished!\n");
}
void Print_Compress(Code T[])
{
FILE *fp;
FILE *fp2;
fp=fopen("Input.txt","r+");
if(fp==NULL)
{
printf("Fail!");
exit(0);
}
fp2=fopen("Compress.txt","r+");
if(fp2==NULL)
{
printf("Fail!");
exit(0);
}
int i=0;
char ch=fgetc(fp);
while(ch != EOF)
{
for(i=0;i<65;i++)
{
if(T[i].data==ch)
{
fprintf(fp2,"%s",T[i].bit);
break;
}
}
ch=fgetc(fp);
}
fclose(fp);
fclose(fp2);
}
void Compress_Rate(Code T[],Node hoffman[])
{
float WPL=0;
int i;
float sum=0;
for(i=0;i<65;i++)
{
WPL+=(strlen(T[i].bit))*hoffman[i].weight;
sum+=8.0*hoffman[i].weight;
}
printf("Compress rate is %f",WPL/sum);
}
void Decoding(Node hoffman[])
{
FILE *fp1,*fp2;
fp1=fopen("Compress.txt","r+");
fp2=fopen("Output.txt","r+");
if(fp1 == NULL)
{
printf("Fail!");
exit(0);
}
if(fp2 == NULL)
{
printf("Fail!");
exit(0);
}
char ch=fgetc(fp1);
int root=128,i;
for(i=65;i<129;i++)//找到根节点,其实就是最后一个结点
{
if(hoffman[i].parent==-1)
{
root=i;
break;
}
}
int c=root;
while(ch != EOF)
{
c=root;
while(hoffman[c].lchild != -1||hoffman[c].rchild != -1)
{
if(ch == '0')
{
c=hoffman[c].lchild;
}
else if(ch == '1')
{
c=hoffman[c].rchild;
}
ch=fgetc(fp1);
if(ch == EOF)
{
break;
}
}
fprintf(fp2,"%c",hoffman[c].data);
}
fclose(fp1);
fclose(fp2);
}
int main()
{
Node hoffman[129];
Code T[65];
Initial_Hoffman_tree(hoffman);
char c;
int i;
FILE *fp;
fp=fopen("Input.txt","r+");
if(fp==NULL)
{
printf("Fail!");
}
else
{
c=fgetc(fp);
while(c != EOF)
{
for(i=0;i<65;i++)
{
if(hoffman[i].data==c)
{
hoffman[i].weight++;
}
}
c=fgetc(fp);
}
fclose(fp);
}
Create(hoffman);
HoffmanCode(hoffman,T);
for(i=0;i<65;i++)
{
printf("Weight of %c is %3d. It's code is %s\n",
hoffman[i].data,
hoffman[i].weight,
T[i].bit);
}
Print_Compress(T);
Decoding(hoffman);
Compress_Rate(T,hoffman);
return 0;
}
总结反思
- 在代码实现过程中,要对第一个和最后一个位置进行严谨地分析,如在编码和译码的最后到根节点处时怎样设置循环停止的条件。
- 对每个字母进行编码时采用了回溯的方式,得到的0,1序列也就恰好相反。
- 要好好写注释。