Huffman Implementation with Python
Huffman Implementation with Python
码表
| Token | Frequency |
|---|---|
| a | 10 |
| e | 15 |
| i | 12 |
| s | 3 |
| t | 4 |
| space | 13 |
| n | 1 |
生成 Huffman 编码
根据上面的码表,先生成 Huffman 树,然后生成 Huffman 编码。代码如下:
def binary_tree(val=None):
return [val, [], []]
def insert_left(root, branch):
root[1] = branch
def insert_right(root, branch):
root[2] = branch
def get_root_val(root):
return root[0]
def set_root_val(root, val):
root[0] = val
def get_left_child(root):
return root[1]
def get_right_child(root):
return root[2]
def is_leaf(root):
if len(get_left_child(root)) == 0 and len(get_right_child(root)) == 0:
return True
return False
def huffman(data):
while len(data) > 1:
data = sorted(data, key=lambda e: e[1])
root = binary_tree()
left = data.pop(0)
right = data.pop(0)
insert_left(root, left[0])
insert_right(root, right[0])
data.append((root, left[1] + right[1]))
return data[0][0]
def tree2code(root, code, plan):
if is_leaf(root):
plan[get_root_val(root)] = code
else:
tree2code(get_left_child(root), code+'0', plan)
tree2code(get_right_child(root), code+'1', plan)
def build_data(d):
l = list()
for pair in d.items():
root = binary_tree(pair[0])
l.append((root, pair[1]))
return l
if __name__ == '__main__':
d = {'a':10, 'e':15, 'i':12, 's':3, 't':4, 'space':13, 'n':1}
l = build_data(d)
tree = huffman(l)
plan = dict()
tree2code(tree, str(), plan)
print(plan)
运行结果得到
{'i': '00', 'space': '01', 'e': '10', 't': '1100', 'n': '11010', 's': '11011', 'a': '111'}
结果分析
根据文献[1],可以知道当前的解是最好结果。
Bibliography
[1] 《数据结构与算法分析——C语言描述》 机械工业出版社
Huffman Implementation with Python的更多相关文章
- Tree Implementation with Python
Tree Implementation with Python List of List 代码如下: def binary_tree(val): return [val, [], []] def in ...
- [Data Structure] Stack Implementation in Python
We can realize a Stack as an adaptation of a Python List. S.push(e)=L.append(e) S.pop()=L.pop() S.to ...
- naive cube implementation in python
这篇论文中提到的naive cube算法的实现,python写出来真的就和伪代码差不多=.= 输入大约长这样,依次是 index userid country state city topic cat ...
- [Data Structure] Linked List Implementation in Python
class Empty(Exception): pass class Linklist: class _Node: # Nonpublic class for storing a linked nod ...
- 【数据压缩】Huffman编码
1. 压缩编码概述 数据压缩在日常生活极为常见,平常所用到jpg.mp3均采用数据压缩(采用Huffman编码)以减少占用空间.编码\(C\)是指从字符空间\(A\)到码字表\(X\)的映射.数据压缩 ...
- Python框架、库以及软件资源汇总
转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...
- Awesome Python
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- Understanding Asynchronous IO With Python 3.4's Asyncio And Node.js
[转自]http://sahandsaba.com/understanding-asyncio-node-js-python-3-4.html Introduction I spent this su ...
随机推荐
- VirtualBox修改现有VDI虚拟磁盘大小
之前创建的虚拟磁盘10G,现需要更大的空间,而又不想使用新增虚拟磁盘的方法. 通过查看资料发现VirtulBox提供的VBoxManage modifyhd命令可以解决,该命令的具体用法: VBoxM ...
- 回车和刷新以及Ctr+F5的区别
回车(url跳转)主要是判断本地缓存文件的Expires的有效时间,如果有效则直接使用客户端缓存 不在提交到HTTP服务器 F5 Expires设置不再起效果,只有Last-Modified/ETag ...
- WebSocket.之.基础入门-断开连接处理
ebSocket.之.基础入门-断开连接处理 在<WebSocket.之.基础入门-后端响应消息>的代码基础之上,继续更新代码.代码只改动了:TestSocket.java 和 index ...
- winform下picturebox控件显示图片问题
viewData_pictureBox.SizeMode=PictureBoxSizeMode.StretchImage;图片会自动按照比例缩放来完全显示在你的PictureBox中.
- 本地计算机上的SQLServer(MSSQLSERVER)服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止
SQLServer的服务启动问题: 本地计算机上的SQLServer(MSSQLSERVER)服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止 出现这个问题导致无法启动SQLServer服 ...
- binTreepreorderTraversal二叉树前序遍历
原题 Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binar ...
- 11.match
(我对部分段落进行翻译) A match statement is used to branch execution of a program. It’s the equivalent of the ...
- vs远程调试 转http://www.cnblogs.com/magicchaiy/archive/2013/05/28/3088274.html
远程调试应用场景 部署环境:ASP.NET(C#)+IIS+Win7 64 bit 很多公司的开发模式都是将开发机器和服务器分开,也就是开发一台机,服务器一台机.而测试人员会在服务器上录入测试数据,此 ...
- C# vs2015单元测试测试资源管理器不显示测试方法
问题描述:在用VS2015用测试框架NUnit单元测试的时候,测试资源管理器死活不出现测试方法,无法运行单元测试模块 现象如下图: 原因:nunit版本不对应 解决方案:下载nunit3.0及往上的版 ...
- QPushButton 控制两种状态
[1]Custom.cpp #include "CustomButton.h" CustomButton::CustomButton(QWidget* parent) : QPus ...