哈夫曼树实现
来源:互联网 发布:莱州网络销售招聘 编辑:程序博客网 时间:2024/06/05 02:21
#include <iostream>#include <cstdio>#define inf 1<<30using namespace std;//哈夫曼树实现//哈夫曼树的特征:如果有n个叶子节点的话,则总结点数为2*n-1。越频繁访问的编码越短struct node{int parent, lson, rson;int val; //访问次数};void createTree(node p[], int n, int total) //total=2*n-1{for(int t=0; t<total; ++t) //初始化{p[t].parent = p[t].lson = p[t].rson = -1;}for(int i=n; i<total; ++i){int min1, min2, l, r; //最大值和第二大值min1 = min2 = inf;l = r = -1;for(int t=0; t<i; ++t) //遍历寻找两个最小的点,也就是val值最小的{if(p[t].parent == -1){if(p[t].val<min1){r = l;l = t;min2 = min1;min1 = p[t].val;} else if(p[t].val<min2){r = t;min2 = p[t].val;}}}p[i].val = min1 + min2;p[l].parent = p[r].parent = i;p[i].lson = l;p[i].rson = r;}}struct code{int k;char c[100];};void huffmancode(node p[], int n, code c1[]){for(int t=0; t<n; ++t){int pre = p[t].parent;int g = t;c1[t].k = 0;while(pre != -1){if(p[pre].lson == g){c1[t].c[c1[t].k++] = '0';} else {c1[t].c[c1[t].k++] = '1';}g = pre;pre = p[pre].parent;}c1[t].k--;}}int main(){node p[4*2-1];for(int t=0; t<4; ++t){p[t].val = 2*t+1;}createTree(p, 4, 2*4-1);code c[4];huffmancode(p, 4, c);for(int t=0; t<4; ++t){int k = c[t].k;printf("%d:", p[t].val);for(int j=k; j>=0; --j){printf("%c", c[t].c[j]);}printf("\n");}return 0;}
运行结果:
0 0
- 哈夫曼树实现
- 哈夫曼树实现
- 实现哈夫曼树
- 实现哈夫曼树
- 哈夫曼树的实现
- 哈夫曼树的java实现
- 压缩解压哈夫曼树实现,
- 哈夫曼树实现示例
- java实现哈夫曼树模板
- 哈夫曼树的实现
- 哈夫曼树的代码实现
- 哈夫曼树的实现
- 哈夫曼树 C语言实现
- 哈夫曼树(Huffman Tree) 实现
- 哈夫曼树 C语言实现
- 哈夫曼树实现 python
- 哈夫曼树(java实现)
- 哈夫曼树 C语言实现
- eclipseIDE for javaee developers 开发环境搭建详解图文
- iOS static 全局变量和普通变量的区别、局部变量呢?
- Valid Anagram
- 百度技术沙龙-大规模分布式存储
- linux 动态库文件配置
- 哈夫曼树实现
- phpcms学习总结-图片上传不成功
- 黑马程序员----多线程
- Android 真机连接本地PC服务器
- HDU 1251 — 统计难题
- GYM 100285 D. This cheeseburger you don’t need(水~)
- strlen()和sizeof()函数
- STL 自定义结构体集合运算
- 贪心算法之Evil Straw Warts Live