算法导论哈夫曼编码
来源:互联网 发布: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;}
阅读全文
1 0
- 算法导论 哈夫曼编码
- 算法导论哈夫曼编码
- 算法导论之哈夫曼编码
- 算法导论之贪心算法:哈夫曼编码
- [算法导论读书笔记]huffman编码
- 算法导论之--------------Huffman编码
- 算法导论 ch16 贪心算法 霍夫曼编码
- 算法导论贪心算法-构造赫夫曼编码
- 【算法导论】贪心算法之赫夫曼编码
- 算法导论第十五章--霍夫曼编码
- 算法导论程序41--赫夫曼编码
- 算法导论之贪心算法(Huffman编码和拟阵)
- 《算法导论》实验四:哈夫曼(Huffman)编码问题(C++实现)
- 算法导论16.3_Huffman编码+优先队列(堆实现)
- 算法导论第16章 构造赫夫曼编码
- 基于visual Studio2013解决算法导论之047赫夫曼编码
- 《算法导论》笔记 第16章 16.3 赫夫曼编码
- 《算法导论》
- XSLT常见问题及解决(三)xsl中variable标签在什么情况下使用
- Java并发之HashMap内部结构解析
- Spring data jpa 多表查询(一:单表动态条件查询)
- 使用VS2013编写QT的环境配置
- UNIX编程专题-wait和waitpid详解
- 算法导论哈夫曼编码
- [RK3288][Android6.0] WiFi之无线网络配置的忘记(移除)过程
- Python 爬虫(以赛马数据为例)之总体架构
- day01 如有错误还望指教
- [Terminal Game Center]贪吃蛇
- Cocos2d-x 之调度器 Scheduler
- HDU1398(生成函数)
- Ubuntu 安装 RabbitMQ 和PHP扩展
- 动态代理实现方法以及对象HooK