Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹定義與使用方法示例
本文實(shí)例講述了Python數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹定義與使用方法。分享給大家供大家參考,具體如下:
HaffMan.py
#coding=utf-8
#考慮權(quán)值的haff曼樹查找效率并非最高,但可以用于編碼等使用場(chǎng)景下
class TreeNode:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
self.parent=None
class HaffTree:
def __init__(self):
self.root=None
def set_root(self,rootNode):
self.root=rootNode
def run(self,lis):
i=0
lis=[[lis[j][0],lis[j][1],TreeNode(lis[j][1])]for j in range(len(lis))]
while len(lis)>1:
i+=1
lis=sorted(lis)
name='N'+str(i)
temp=TreeNode(name)
#結(jié)果與大話數(shù)據(jù)結(jié)構(gòu)書上略有不同 因?yàn)閘is[0][2]=lis[1][2] 無影響
#這里使用parent 替代深度優(yōu)先/廣度優(yōu)先 算法
temp.left=lis[0][2]
temp.right=lis[1][2]
lis[0][2].parent=temp
lis[1][2].parent=temp
#print lis[0][0],lis[1][0],len(lis)
value=lis[0][0]+lis[1][0]
lis=lis[1:]
lis[0]=[value,name,temp]
#print temp.data,temp.left.data,temp.right.data
self.set_root(temp)
def code(self,lis):
self.codeList=[]
stack=[]
Node=self.root
stack.append(Node)
res=[]
while(stack):
node=stack.pop()
res.append(node)
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
for li in lis:
codeL=[]
for re in res:
if re.data==li[1]:
parent=re
print '\n',parent.data,
codeL.append(parent)
while parent.parent:
parent=parent.parent
print parent.data,
codeL.append(parent)
codeLL=[int(codeL[len(codeL)-2-i]==codeL[len(codeL)-1-i].right) for i in range(len(codeL)-1)]
self.codeList.append([li[1],codeLL])
return self.codeList
def list_all(self,method):
lis=[]
res=[]
if method=='before':
Node=self.root
lis.append(Node)
while(lis):
node=lis[-1]
lis=lis[:-1]
if node:
res.append(node.data)
if node.right:
lis.append(node.right)
if node.left:
lis.append(node.left)
elif method=='mid':
node = self.root
while lis or node:
while node:
lis.append(node)
node = node.left
if len(lis)>0:
node = lis[-1]
lis=lis[:-1]
if node:
res.append(node.data)
node= node.right
else:
pass
return res
HaffMantest.py
#coding=utf-8
from HaffMan import HaffTree
tree=HaffTree()
lis=[
[5,'A'],
[15,'B'],
[40,'C'],
[30,'D'],
[10,'E'],
]
print lis[2:]
print sorted(lis)
tree.run(lis)
print tree.list_all('before')
#應(yīng)用 HaffMan編碼,比如字母分布不均勻的情況下比較適合,可減少傳輸?shù)男畔⒘浚ǘM(jìn)制),不會(huì)出現(xiàn)干涉。:
tree=HaffTree()
lis2=[
[27,'A'],
[8,'B'],
[15,'C'],
[15,'D'],
[30,'E'],
[5,'F'],
]
tree.run(lis2)
print tree.code(lis2)
運(yùn)行結(jié)果:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python 數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解
- Python數(shù)據(jù)結(jié)構(gòu)之二叉排序樹的定義、查找、插入、構(gòu)造、刪除
- Python描述數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之哈夫曼樹篇
- Python數(shù)據(jù)結(jié)構(gòu)之棧、隊(duì)列及二叉樹定義與用法淺析
- Python數(shù)據(jù)結(jié)構(gòu)與算法之字典樹實(shí)現(xiàn)方法示例
- Python數(shù)據(jù)結(jié)構(gòu)與算法之完全樹與最小堆實(shí)例
- Python數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹結(jié)構(gòu)定義與遍歷方法詳解
- Python數(shù)據(jù)結(jié)構(gòu)之樹的全面解讀
相關(guān)文章
Django Sitemap 站點(diǎn)地圖的實(shí)現(xiàn)方法
這篇文章主要介紹了Django Sitemap 站點(diǎn)地圖的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Python實(shí)現(xiàn)數(shù)據(jù)地址實(shí)體抽取
大家好,本篇文章主要講的是Python實(shí)現(xiàn)數(shù)據(jù)地址實(shí)體抽取,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Python中dilb和face_recognition第三方包安裝失敗的解決
本文主要介紹了Python中dilb和face_recognition第三方包安裝失敗的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
python初學(xué)之用戶登錄的實(shí)現(xiàn)過程(實(shí)例講解)
下面小編就為大家分享一篇python初學(xué)之用戶登錄的實(shí)現(xiàn)過程(實(shí)例講解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
numpy.transpose對(duì)三維數(shù)組的轉(zhuǎn)置方法
下面小編就為大家分享一篇numpy.transpose對(duì)三維數(shù)組的轉(zhuǎn)置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04

