Haffman Code
来源:互联网 发布:对刀点对编程的影响 编辑:程序博客网 时间:2024/05/08 03:58
Haffman Code
什么是Haffman编码?以后再说,嘿嘿先上实现。
Python 实现:
在这个实现当中,我们并没有统计文本的字符出现频率,仅仅是为Haffman Coding的一个demo.
"""Code writer : EOFCode file : haffman.pyCode date : 2015.01.28e-mail : jasonleaster@gmail.comCode description: Here is a implementation of Haffman-Codein Python."""class node() : def __init__(self,num) : self.right = None self.left = None self.key = numclass haffman_tree(): def __init__(self, array) : self.forest = [] for i in range(0, len(array)) : self.forest += [node(array[i])] self.build() def small_tree(self) : smallest_tree = self.forest[0] location = 0 for i in range(1, len(self.forest)) : if self.forest[i].key < smallest_tree.key : location = i return location def delete_root(self, x) : for i in range(0, len(self.forest)) : if x.key is self.forest[i].key : del self.forest[i] return def build(self) : while len(self.forest) is not 1 : small_tree_1 = self.forest[self.small_tree()] self.delete_root(small_tree_1) small_tree_2 = self.forest[self.small_tree()] self.delete_root(small_tree_2) tmp_node = node(small_tree_1.key + small_tree_2.key) tmp_node.right = small_tree_1 tmp_node.left = small_tree_2 self.forest += [tmp_node] self.root = self.forest[0] self.coding(self.root,'') def coding(self, node, path) : if node.right is None and node.left is None : print path, "is the haffman code of", node.key return else : self.coding(node.right, path + '1') self.coding(node.left, path + '0') #------------------------------------------------------tree = haffman_tree(['a','b','c','d','e','f','g','h'])
在Haffman树(就是个二叉树)当中, 1代表右拐,0代表左拐。
后续会更新,把Haffman code 的原理和用处讲清楚。
0 0
- Haffman Code
- Haffman应用
- haffman树
- Haffman编码
- Haffman编码
- Haffman 编码
- haffman树的遍历
- haffman部分报告
- NYOJ 801 Haffman编码
- 数据结构:haffman树
- erl_tree-haffman Tree 哈夫曼树
- nyoj 801 Haffman编码
- nyoj 801 Haffman编码
- Haffman Encoding and Decoding
- 数据结构笔记--haffman树与haffman编码分析
- Arithmetic coding and haffman coding
- haffman哈夫曼编码的实现
- 哈夫曼树(haffman tree)的某题
- Git常用命令
- mysqlbinlog
- 在Linux下安装Python
- java多线程-阻塞队列
- easyui的datagrid单击某一栏同时触发onClickRow和其中一栏的自定义事件怎么办?
- Haffman Code
- 工作日志
- [C++]LeetCode: 128 Largest Number (自定义比较函数排序)
- 关于namespace的文件组织
- gsoap生成webservice调用客户端接口
- 学习笔记——cad.net 创建文字样式
- 修改UITableViewCell中UIImageView的位置和大小
- Gamma校正及其实现
- oracle创建表空间和用户