c语言实现赫夫曼信源编码,用C语言编无失真信源编码(哈夫曼编码)

#include

#include

#include

typedef unsigned char U8;

typedef unsigned short U16;

typedef unsigned long U32;

typedef struct HuffmanNode

{

double prob;

struct HuffmanNode *left;

struct HuffmanNode *right;

}huffmancode;

void Init ( U32*, double** );

huffmancode* Encode ( double*, U32 );

huffmancode* CreateBinaryTree ( huffmancode**, int );

void Print ( huffmancode*, char* );

void DeleteBinaryTree ( huffmancode* );

void main()

{

U32 n;

double *p;

huffmancode *pHuffman;

Init ( &n, &p );

pHuffman = Encode ( p, n );

Print ( pHuffman, "" );

DeleteBinaryTree ( pHuffman );

}

void DeleteBinaryTree ( huffmancode *pHuffman )

{

if ( pHuffman->left != NULL )

DeleteBinaryTree ( pHuffman->left );

if ( pHuffman->right != NULL )

DeleteBinaryTree ( pHuffman->right );

free ( pHuffman );

return;

}

void Print ( huffmancode *pHuffman, char *code )

{

int numChild = 0;

int strLen = strlen(code);

char *nextCode = (char*) malloc ( strLen + 2 );

if ( pHuffman->left != NULL )

{

numChild++;

strcpy ( nextCode, code );

nextCode[strLen] = '0';

nextCode[strLen+1] = '\0';

Print ( pHuffman->left, nextCode );

}

if ( pHuffman->right != NULL )

{

numChild++;

strcpy ( nextCode, code );

nextCode[strLen] = '1';

nextCode[strLen+1] = '\0';

Print ( pHuffman->right, nextCode );

}

if ( numChild == 0 )

{

printf ( "%lf\t==>\t%s\n", pHuffman->prob, code );

}

free ( nextCode );

return;

}

huffmancode* Encode ( double *p, U32 n )

{

U32 i;

huffmancode *pRes;

huffmancode **ppHuffman;

ppHuffman = (huffmancode**) malloc ( sizeof(huffmancode*) * n );

for ( i = 0; i < n; i++ )

{

ppHuffman[i] = (huffmancode*) malloc ( sizeof(huffmancode) );

ppHuffman[i]->prob = p[i];

ppHuffman[i]->left = NULL;

ppHuffman[i]->right = NULL;

}

pRes = CreateBinaryTree ( ppHuffman, n );

free ( ppHuffman );

return pRes;

}

huffmancode* CreateBinaryTree ( huffmancode **ppHuffman, int n )

{

int i;

int min1 = 0, min2 = 1;  /* min1 < min2 */

huffmancode *tmp, *pRes;

pRes = (huffmancode*) malloc ( sizeof ( huffmancode ) );

if ( ppHuffman[0]->prob > ppHuffman[1]->prob )

min1 = 1, min2 = 0;

for ( i = 2; i < n; i++ )

{

if ( ppHuffman[i]->prob < ppHuffman[min2]->prob )

{

if ( ppHuffman[i]->prob < ppHuffman[min1]->prob )

{

min2 = min1;

min1 = i;

}

else

{

min2 = i;

}

}

}

pRes->prob = ppHuffman[min1]->prob + ppHuffman[min2]->prob;

pRes->left = ppHuffman[min1];

pRes->right = ppHuffman[min2];

if ( min1 == n - 1 )

ppHuffman[min2] = pRes;

else if ( min2 == n -1 )

ppHuffman[min1] = pRes;

else

{

ppHuffman[min1] = pRes;

ppHuffman[min2] = ppHuffman[n-1];

}

if ( n != 2 )

pRes = CreateBinaryTree ( ppHuffman, n-1 );

return pRes;

}

void Init ( U32 *n, double **pp )

{

*n = 6;

*pp = (double*) malloc ( sizeof(double) * (*n) );

(*pp)[0] = 0.1;

(*pp)[1] = 0.2;

(*pp)[2] = 0.3;

(*pp)[3] = 0.15;

(*pp)[4] = 0.04;

(*pp)[5] = 0.21;

/* (*pp)[0] = 0.1;

(*pp)[1] = 0.1;

(*pp)[2] = 0.1;

(*pp)[3] = 0.1;

(*pp)[4] = 0.1;

(*pp)[5] = 0.1;

(*pp)[6] = 0.1;

(*pp)[7] = 0.1;*/

return;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值