python实现Huffman编码
一、问题
利用二叉树的结构对Huffman树进行编码,实现最短编码
二、解决
# 构建节点类
class TreeNode:
def __init__(self, data):
"""
:data is a tuple the first element is value and the second is priority
:param data:
"""
self.value = data[0]
self.priority = data[1]
self.left_child = None
self.right_child = None
self.code = "" # 创建树节点队列的函数
def create_node_queue(codes):
queue = []
for code in codes:
queue.append(TreeNode(code))
return queue # 在队列中间添加新的节点元素并保证优先度从大到小排列
def add_queue(queue, node_new):
if len(queue) == 0:
return [node_new]
for i in range(len(queue)):
if queue[i].priority >= node_new.priority:
return queue[:i] + [node_new] + queue[i:]
return queue + [node_new] # 节点队列类
class NodeQueue:
def __init__(self, code):
self.queue = create_node_queue(code)
self.size = len(self.queue) def add_node(self, node):
self.queue = add_queue(self.queue, node)
self.size += 1 def pop_node(self):
self.size -= 1
return self.queue.pop(0) # 各个字符在字符串中出现的次数 即计算优先度
def frequent_char(string_s):
store_d = {}
for c in string_s:
if c not in store_d:
store_d[c] = 1
else:
store_d[c] += 1
return sorted(store_d.items(), key=lambda x: x[1]) # 创建Huffman树
def create_huffman_tree(node_queue):
while node_queue.size != 1:
node1 = node_queue.pop_node()
node2 = node_queue.pop_node()
r_1 = TreeNode([None, node1.priority + node2.priority])
r_1.left_child = node1
r_1.right_child = node2
node_queue.add_node(r_1)
return node_queue.pop_node() code_dict1 = {}
code_dict2 = {} # 由Huffman树得到的Huffman编码表
def huffman_code_dict(head, x):
# global code_dict, code_list
if head:
huffman_code_dict(head.left_child, x + "")
head.code += x
if head.value:
code_dict2[head.code] = head.value
code_dict1[head.value] = head.code
huffman_code_dict(head.right_child, x + "") # 字符串编码
def trans_encode(string_s):
# global code_dict1
trans_code = ""
for c in string_s:
trans_code += code_dict1[c]
return trans_code # 字符串解码
def trans_decode(string_s):
# global code_dict1
code = ""
answer = ""
for c in string_s:
code += c
if code in code_dict2:
answer += code_dict2[code]
code = ""
return answer
三、总结
利用Huffman树的编码形式可以进行数据的压缩,因此Huffman的应用也很广泛。在此记录一下方便以后查看。
python实现Huffman编码的更多相关文章
- 【数据压缩】Huffman编码
1. 压缩编码概述 数据压缩在日常生活极为常见,平常所用到jpg.mp3均采用数据压缩(采用Huffman编码)以减少占用空间.编码\(C\)是指从字符空间\(A\)到码字表\(X\)的映射.数据压缩 ...
- [老文章搬家] 关于 Huffman 编码
按:去年接手一个项目,涉及到一个一个叫做Mxpeg的非主流视频编码格式,编解码器是厂商以源代码形式提供的,但是可能代码写的不算健壮,以至于我们tcp直连设备很正常,但是经过一个UDP数据分发服务器之后 ...
- python基础之编码问题
python基础之编码问题 本节内容 字符串编码问题由来 字符串编码解决方案 1.字符串编码问题由来 由于字符串编码是从ascii--->unicode--->utf-8(utf-16和u ...
- Python基础-字符编码与转码
***了解计算机的底层原理*** Python全栈开发之Python基础-字符编码与转码 需知: 1.在python2默认编码是ASCII, python3里默认是utf-8 2.unicode 分为 ...
- Huffman编码
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <cstri ...
- python中的编码问题:以ascii和unicode为主线
1.unicode.gbk.gb2312.utf-8的关系 http://www.pythonclub.org/python-basic/encode-detail 这篇文章写的比较好,utf-8 ...
- 优先队列求解Huffman编码 c++
优先队列小析 优先队列的模板: template <class T, class Container = vector<T>,class Compare = less< ...
- python与字符集编码
讲的比较明白的博客:http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html 以上面博文的汉为例子,汉字的GBK编码是baba, UNIC ...
- 第三篇:python基础之编码问题
python基础之编码问题 python基础之编码问题 本节内容 字符串编码问题由来 字符串编码解决方案 1.字符串编码问题由来 由于字符串编码是从ascii--->unicode---&g ...
随机推荐
- JVM问题排查工具:Serviceability-Agent介绍
本文首发于微信公众号:javaadu 简单介绍 构建高性能的Java应用过程中,必然会遇到各种各样的问题,像CPU飙高.内存泄漏.应用奔溃,以及其他疑难杂症,这时可以使用Serviceability ...
- SQLServer2017自动备份数据库
本文截图来源: 地址一:https://blog.csdn.net/u010944388/article/details/86674078 地址二:https://www.cnblogs.com/ja ...
- elastalert docker安装
基于对elasticsearch中数据监控需要,我尝试了sentinl和elastalert两款工具.虽然elastalert是纯文本,但易配置管理.elk自带的watch需要付费才可使用. 6.2x ...
- 大众点评评论数据抓取 反爬虫措施有css文字映射和字体库反爬虫
大众点评评论数据抓取 反爬虫措施有css文字映射和字体库反爬虫 大众点评的反爬虫手段有那些: 封ip,封账号,字体库反爬虫,css文字映射,图形滑动验证码 这个图片是滑动验证码,访问频率高的话,会出 ...
- 微信公众平台iPhone版内测开始了
5月9日晚些时候自媒体人收到了微信公众平台iPhone版的内测邀请,微信公众平台iPhone版可在手机上写图文,快速查看并回复粉丝消息.留言和赞赏,新建群发.查看群发历史和今日数据,这些功能很实用了, ...
- 10-赵志勇机器学习-meanshift
(草稿) meanshift 也是一种聚类方法. 优点在于:不需要提前指定类型数. 缺点就是计算量大 过程:(最一般的做法,没有使用核函数) 1. 逐点迭代,设置为位置中心 2. 计算所有点到位置中心 ...
- 《浅谈我眼中的express、koa和koa2》好文留存+笔记
原文 :三英战豪强,思绪走四方.浅谈我眼中的express.koa和koa2 一.回调大坑怎么解决呢? 1.es5可以利用一下第三方库,例如 async 库, 2.或者单纯使用 connect中间件 ...
- ajax post 提交无法进入controller 请求200
最近写js遇到个问题: 用ajax的post方式给后台提交数据,页面200,但是不进入controller 断点,我以为我post参数不对. 网上查的: 1.说路径不对,但是我通过get方式是可以进入 ...
- 微信小程序跳转函数总结
微信小程序跳转函数总结 笔者在微信小程序前端的开发过程中,在不同的情况下遇到了需要使用不同的页面跳转逻辑的情况,以下是我对这些函数的使用场景的一个总结介绍. wx.navigateTo 这是最常用 ...
- Gamma阶段第九次scrum meeting
每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #91 用户体验与优化https://github.com/rRetr0Git/rateMyCourse/issues/91(持续完成) # ...