求大虾指导指导!
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define LEN 8
#define MAXLEAF 8 // 最大叶子结点数目
#define MAXNODE (MAXLEAF*2)-1
typedef int ElemType;
typedef struct /* this structure stores the information of code */
{
int start; /* 存放编码的起始位置右至左(高位至低位)*/
int bit[LEN]; /* 存放 huffman编码 */
}HCode;
typedef HCode HuffCode[MAXLEAF];
typedef struct /* huffman tree结点的结构 */
{
int parent;
int LChild;
int RChild;
ElemType weight;
}HNode;
typedef HNode Huffman[MAXLEAF*2-1];
void createHuffmanTree(Huffman h,int leaves,ElemType *weight)
{
int i,j;
for(i=0;i<leaves*2-1;i++) /* 初始化huffman tree */
{
(h+i)->parent=-1;
(h+i)->LChild=-1;
(h+i)->RChild=-1;
(h+i)->weight=0;
}
for(i=0;i<leaves;i++) /* 给叶子赋权重 */
{
(h+i)->weight=*(weight+i);
}
/* 上一个循环叶子已经带权,下面这个循环用来生成新根
* 新根数量为n-1
*/
for(i=0;i<leaves-1;i++)
{
ElemType m1, m2;
int m1_pos, m2_pos;
m1=m2=65536; /* m1存放最小的权值m2存放次小的权值 */
m1_pos=m2_pos=0; /* m1存放最小的权值对应下标m2存放次小的权值对应下标*/
for(j=0;j<leaves+i;j++)
{
if((h+j)->weight<m1&&(h+j)->parent==-1)
{
m2=m1;
m1=(h+j)->weight;
m2_pos=m1_pos;
m1_pos=j;
}
else if((h+j)->weight<m2&&(h+j)->parent==-1)
{
m2=(h+j)->weight;
m2_pos=j;
}
}
(h+leaves+i)->parent=-1; // 生成新根,无双亲-1
(h+leaves+i)->LChild=m1_pos; // 新根左孩子在数组中的下标
(h+leaves+i)->RChild=m2_pos; // 新根右孩子在数组中的下标
(h+m1_pos)->parent=leaves+i; // 原根的父亲位置
(h+m2_pos)->parent=leaves+i; // 原根的父亲位置
(h+leaves+i)->weight=m2+m1;
}
}
void huffmancode(Huffman h,HuffCode code,int leaves)
{
int i,j,p,c;
HCode hf;
/*从叶子结点开始向上回溯 从而计算出huffman code */
for(i=0;i<leaves;i++)
{
c=i;
p=h[i].parent;
hf.start=LEN-1;
while(p!=-1)
{
if(h[p].LChild==c)
{
hf.bit[hf.start]=1;
}
else
{
hf.bit[hf.start]=0;
}
--hf.start;
c=p;
p=h[c].parent;
}
for(j=hf.start+1;j<LEN;j++)
{
code[i].bit[j]=hf.bit[j];
}
code[i].start=hf.start+1;
}
printf( "%d",h[leaves-1].parent);
}
void printhuffcode(HuffCode hcode,char characters[],int weights[])
{
int i,j;
for(i=0;i<strlen(characters);i++)
{
// printf("%f的huffman编码:",characters[i]);
printf("第%d个字符 " ,i+1);
printf("频率为%d对应的Huffman编码为:",weights[i]);
for(j=hcode[i].start;j<LEN;j++)
{
printf("%d",hcode[i].bit[j]);
}
printf("\n");
}
}
void main()
{
Huffman h;
int i,q,k[6]={0},j,p=0;
HuffCode hcode;
char characters[]={"abcdef"},a[81],b[81]; /* 待编码的字符 */
int weights[6];/* 字符出现的频率 */
printf("请输入字符串:");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='a')k[0]++;
else if(a[i]=='b')k[1]++;
else if(a[i]=='c')k[2]++;
else if(a[i]=='d')k[3]++;
else if(a[i]=='e')k[4]++;
else if(a[i]=='f')k[5]++;
else q++;
}
// printf("请输入对应字符的发生频率:\n");
for (i=0;i<6;i++)
weights[i]=k[i];
createHuffmanTree(h,strlen(characters),weights);
huffmancode(h,hcode,sizeof(characters));
printhuffcode(hcode,characters,weights);
// system("pause");
printf("请输出压缩的编码:");
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='a')
for(j=hcode[0].start;j<LEN;j++)
{
printf("%d",hcode[0].bit[j]);
}
else if(a[i]=='b')
for(j=hcode[1].start;j<LEN;j++)
{
printf("%d",hcode[1].bit[j]);
}
else if(a[i]=='c')
for(j=hcode[2].start;j<LEN;j++)
{
printf("%d",hcode[2].bit[j]);
}
else if(a[i]=='d')
for(j=hcode[3].start;j<LEN;j++)
{
printf("%d",hcode[3].bit[j]);
}
else if(a[i]=='e')
for(j=hcode[4].start;j<LEN;j++)
{
printf("%d",hcode[4].bit[j]);
}
else if(a[i]=='f')
for(j=hcode[5].start;j<LEN;j++)
{
printf("%d",hcode[5].bit[j]);
}
else q++;
}
/* printf("请输入需要解压的的编码:");
gets(b);
for(i=0;b[i]!='\0';i++)p++;
for(i=p-1;i>=0;i--)
{*/
}
帮忙在后面写个解压编码的过程!这是哈夫曼编码的解压过程,比如输入010010101001110101010则输出abcdef中的字母