#include
#include
#include
#include
#define MAXNUM 60
typedef struct
{
char ch;
int weight; //权值,这个字符出现的频率
int parent;
int left;
int right;
}HuffNode;
typedef struct
{
char code[MAXNUM];
int start;
}HuffCode;
HuffNode ht[MAXNUM*2]; //存放哈夫曼树
HuffCode hcd[MAXNUM]; //存放ht数组中对应的字符的编码
int n; //字符的个数
//初始化哈夫曼树ht
void initHt()
{
FILE * fp;
char ch;
int i=0;
//从文件document/character.txt中读出要编码的字符和权值
if((fp=fopen("document/character.txt","r"))==NULL){
printf("can not open the file character.txt");
exit(0);
}
ht[i].left=ht[i].right=ht[i].parent=-1;
while((ch=fgetc(fp))!=EOF){
if(ch=='\n'){
i++;
ht[i].left=ht[i].right=ht[i].parent=-1;
}
else if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
ht[i].ch=ch;
else if(ch>='0'&&ch<='9')
ht[i].weight=ht[i].weight*10+ch-'0';