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 ...
随机推荐
- git安装和使用(二)
一.git安装 1.目的 通过git管理github托管项目代码 2.下载安装 Git - Downloading Packagewww.git-scm.com 点击桌面,右击鼠标,出现两个git单 ...
- mac 进程和线程工具
进程 查看端口进程 lsof lsof -i tcp:<port> 示例 $ lsof -i tcp:8082 COMMAND PID USER FD TYPE DEVICE SIZE/O ...
- centos7挂载新加4T硬盘到/home目录
以下操作均在root环境下运行. 1.查看硬盘 # fdisk -l 发现硬盘为/dev/sdb 大小4T 2.如果此硬盘以前有过分区,则先对磁盘格式化: # mkfs -t ext4 /dev/sd ...
- ubuntu下ldd,查看程序动态库信息
ldd list, dynamic, dependencies linux-vdso.so. => (0x00007ffe9d9b6000) libstdc++.so. => /usr/ ...
- cocos2d-x JS 纯代码渲染Lable描边
/** * Enables shadow style and sets color, offset and blur radius styles. * @param {cc.Color} shadow ...
- vue中使用base64和md5
1.在项目根目录下安装 cnpm install --save js-base64 cnpm install --save js-md5 2.在项目文件中引入 import md5 from 'js- ...
- RSA加解密 公钥加密私钥解密 公加私解 && C++ 调用openssl库 的代码实例
前提:秘钥长度=1024 ============================================== 对一片(117字节)明文加密 ========================= ...
- Sitecore CMS中如何管理默认字段值
在Sitecore CMS中管理默认字段值. 在创建内容时,自定义默认值对内容编辑者特别有用.通过指定良好的默认值,新创建的项目可以预先填充数据,以便内容编辑者不必浪费时间一遍又一遍地填充相同的字段. ...
- C# 队列(Queue)和 堆栈(Stack)
C# 队列(Queue)和 堆栈(Stack) C# 队列(Queue) 队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队 ...
- python 数据序列化(json、pickle、shelve)
本来要查一下json系列化自定义对象的一个问题,然后发现这篇博客(https://www.cnblogs.com/yyds/p/6563608.html)很全面,感谢作者,关于python序列化的知识 ...