typedef struct
{
int x;
char c;
}Trans,*pTrans;
int storeHuffman(Trans *a,pHuff root,int i)
{
int j,k;
if(root->left && root->right)
{
a[i].x=root->freq;
a[i].c=root->c;
j=storeHuffman(a,root->left,i+1);
k=storeHuffman(a,root->right,j);
return k;
}
else
{
a[i].x=root->freq;
a[i].c=root->c;
return i+1;
}
}
int decodeHuffman(Trans *a,int i,pHuff ph,char di)
{
pHuff ph2;
if(di!='M')
{
ph2=(pHuff)malloc(sizeof(Huff));
ph2->freq=a[i].x;
ph2->c=a[i].c;
ph2->left=ph2->right=NULL;
}
else
{
ph->freq=a[i].x;
ph->c=a[i].c;
}
if(a[i].c!='*')
{
if(di=='L')
{
ph->left=ph2;
}
else if(di=='R')
{
ph->right=ph2;
}
return i+1;
}
else
{
if(di=='L')
{
ph->left=ph2;
}
else if(di=='R')
{
ph->right=ph2;
}
else
{
ph2=ph;
}
int k,j;
j=decodeHuffman(a,i+1,ph2,'L');
k=decodeHuffman(a,j,ph2,'R');
return k;
}
}
void main()
{
Trans a2[2*N-1];
storeHuffman(a2,ph,0);
printA(a2,2*N-1);
pHuff ph2=(pHuff)malloc(sizeof(Huff));
ph2->left=ph2->right=NULL;
decodeHuffman(a2,0,ph2,'M');
getCode(ph2,s,'d',0);
printf("\n");
getchar();
}
算法导论 哈夫曼树的传输
最新推荐文章于 2025-04-08 16:23:55 发布