算法导论哈夫曼编码

算法导论哈夫曼编码

#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值