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的更多相关文章

  1. Tree Implementation with Python

    Tree Implementation with Python List of List 代码如下: def binary_tree(val): return [val, [], []] def in ...

  2. [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 ...

  3. naive cube implementation in python

    这篇论文中提到的naive cube算法的实现,python写出来真的就和伪代码差不多=.= 输入大约长这样,依次是 index userid country state city topic cat ...

  4. [Data Structure] Linked List Implementation in Python

    class Empty(Exception): pass class Linklist: class _Node: # Nonpublic class for storing a linked nod ...

  5. 【数据压缩】Huffman编码

    1. 压缩编码概述 数据压缩在日常生活极为常见,平常所用到jpg.mp3均采用数据压缩(采用Huffman编码)以减少占用空间.编码\(C\)是指从字符空间\(A\)到码字表\(X\)的映射.数据压缩 ...

  6. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  7. Awesome Python

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

  8. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  9. 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 ...

随机推荐

  1. 用于模型选择的AIC与BIC

    一.AIC(Akaike information Criterion)准则 二.BIC(Bayesian information Criterion)准则 参考文献: [1]AIC与BIC区别

  2. Hybrid设计--核心交互

    普通网页中跳转使用a标签,这里我们要对跳转进行更多的干预,所以将全站的跳转收口到框架层,用forward去实现.拒绝用a和window.location.如果我想对所有跳转做一个处理,开动画或者对跳转 ...

  3. usermod命令详解

    转载自:http://blog.51cto.com/urchin/987186 usermod - 修改用户帐户信息 modify a user account usermod [options] u ...

  4. iOS Xcode 用 GitHub 托管项目

    直接在xcode里用Github教程:http://jingyan.baidu.com/article/ab0b5630936ab6c15afa7d1c.html 经过本人尝试之后,发现,使用官网的客 ...

  5. Linux基础(四)网络设置

    环境: linux系统版本为:CentOS 7 虚拟机:vm12 共享本地计算机网络,达到我们linux系统上网的目的 虚拟机,添加网卡-设置 设置成,自定义vmnet8 NAT模式即可,不需要添加什 ...

  6. CentOS双机中Docker下安装Mysql并配置互为主从模式

    CentOS双机中Docker下安装Mysql并配置互为主从模式 目录 1.搜索镜像... 1 2.拉取镜像... 1 3.绑定端口: 1 4.配置文件(修改/etc/mysql/my.cnf文件): ...

  7. PGPDesktop在win7环境下的安装和使用

    PGPDesktop在win7环境下的安装和使用 PGP的简介 PGP(Pretty Good Privacy),是一个基于RSA公钥加密体系的邮件加密软件,它提供了非对称加密和数字签名,是目前非常流 ...

  8. Spark学习之路 (八)SparkCore的调优之开发调优

    摘抄自:https://tech.meituan.com/spark-tuning-basic.html 前言 在大数据计算领域,Spark已经成为了越来越流行.越来越受欢迎的计算平台之一.Spark ...

  9. Mysql相关技术细节整理

    一.错误日志相关 1.mysql错误日志所在位置 windows下,错误日志文件一般在安装目录下的data目录下.扩展名是.err的文件,也可以打开安装目录下的my.ini文件检查一下linux下,错 ...

  10. 把object转成JSONObject JSON.toJSON

    把object转成JSONObject JSON.toJSON public void onNext(Object o) { LogUtil.i("getFavorites", & ...