算法导论哈夫曼编码

来源:互联网 发布:qq空间 for mac客户端 编辑:程序博客网 时间:2024/06/14 08:36

算法导论哈夫曼编码

#include <stdio.h>#include <stdlib.h>int m_length=0;typedef struct Huff{    char word;    int  weight;    struct Huff *left;    struct Huff *right;}HuffNode;typedef struct minHeap{    int len;    HuffNode **ptHuffNode;}MinHeap;void myswap(HuffNode **a,HuffNode **b){    HuffNode *ptemp=*a;    *a=*b;    *b=ptemp;}void minheapify(HuffNode **ptNode,int i,int length){    int left=2*i+1;    int right=2*i+2;    int smallest=0;    if(left<=length-1 && ptNode[left]->weight < ptNode[i]->weight)    {        smallest=left;    }    else    {        smallest=i;    }    if(right <=length-1 && ptNode[right]->weight<ptNode[smallest]->weight)        smallest=right;    if(smallest!=i)    {        myswap(&ptNode[smallest],&ptNode[i]);        minheapify(ptNode,smallest,length);    }}MinHeap* buildminheap(char a[],int b[],int length){    MinHeap *ptMinHeap=(MinHeap*)malloc(sizeof(MinHeap));    ptMinHeap->len=length;    ptMinHeap->ptHuffNode=(HuffNode**)malloc(length*sizeof(HuffNode*));    int i=0;    for(i=0;i<length;++i)    {        ptMinHeap->ptHuffNode[i]=(HuffNode*)malloc(sizeof(HuffNode));        ptMinHeap->ptHuffNode[i]->word=a[i];        ptMinHeap->ptHuffNode[i]->weight=b[i];        ptMinHeap->ptHuffNode[i]->left=NULL;        ptMinHeap->ptHuffNode[i]->right=NULL;    }    for(i=length/2-1;i>=0;--i)    {        minheapify(ptMinHeap->ptHuffNode,i,length);    }    return ptMinHeap;}HuffNode *heapextractmin(MinHeap *ptHeap){    if(ptHeap->len<1)    {        return NULL;    }    HuffNode *ptminNode=ptHeap->ptHuffNode[0];    myswap(&ptHeap->ptHuffNode[0],&ptHeap->ptHuffNode[ptHeap->len-1]);    ptHeap->len--;    minheapify(ptHeap->ptHuffNode,0,ptHeap->len);    return ptminNode;}void heapincreasekey(MinHeap *ptHeap,int i,HuffNode *pthuffNode){    if(pthuffNode->weight > ptHeap->ptHuffNode[i]->weight)        printf("new key is larger than current key\n");    ptHeap->ptHuffNode[i]=pthuffNode;    while(i>0 && ptHeap->ptHuffNode[i/2]->weight>ptHeap->ptHuffNode[i]->weight )    {        myswap(&ptHeap->ptHuffNode[i/2],&ptHeap->ptHuffNode[i]);        i=i/2;    }}void minheapinsert(MinHeap *ptHeap,HuffNode *pthuffNode){    ptHeap->len=ptHeap->len+1;    ptHeap->ptHuffNode[ptHeap->len-1]->weight=0x7FFFFFFF;    heapincreasekey(ptHeap,ptHeap->len-1,pthuffNode);}HuffNode*  huffman(char a[],int b[],int length){    MinHeap *ptMinHeap=buildminheap(a,b,length);    int i=0;    HuffNode *ptleft=NULL;    HuffNode *ptright=NULL;    HuffNode *ptz=NULL;    for(i=0;i<length-1;++i)    {        ptleft=heapextractmin(ptMinHeap);        ptright=heapextractmin(ptMinHeap);        ptz=(HuffNode*)malloc(sizeof(HuffNode));        ptz->left=ptleft;        ptz->right=ptright;        ptz->word='*';        ptz->weight=ptleft->weight+ptright->weight;        minheapinsert(ptMinHeap,ptz);    }    return ptMinHeap->ptHuffNode[0];}void PrintHuffcode(char s[],int i,char c,HuffNode *ptHuff){    if(NULL==ptHuff)        return;    if(ptHuff->word==c)    {        s[i]='\0';        printf("%s\n",s);        return;    }    s[i]='0';    PrintHuffcode(s,i+1,c,ptHuff->left);    s[i]='1';    PrintHuffcode(s,i+1,c,ptHuff->right);}int main(){    char a[]={'f','e','c','b','d','a'};    int b[]={5,9,12,13,16,45};    HuffNode *ptNode=huffman(a,b,6);    char *s=(char *)malloc(7);    PrintHuffcode(s,0,'e',ptNode);    return 0;}
原创粉丝点击