技術部落格:
下面主要來看一下哈夫曼樹的 python 實現:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 統計字元出現頻率,生成對映表
defcount_frequency
(text)
: chars =
ret =
for char in text:
if char in chars:
continue
else
:(char, text.count(char)))
return ret
# 節點類
class
node
:def
__init__
(self, frequency)
: self.left =
none
self.right =
none
self.father =
none
self.frequency = frequency
defis_left
(self)
:return self.father.left == self
# 建立葉子節點
defcreate_nodes
(frequency_list)
:return
[node(frequency)
for frequency in frequency_list]
# 建立huffman樹
defcreate_huffman_tree
(nodes)
: queue = nodes[:]
while
len(queue)
>1:
queue.sort(key=
lambda item: item.frequency)
node_left = queue.pop(0)
node_right = queue.pop(0)
node_father = node(node_left.frequency + node_right.frequency)
node_father.left = node_left
node_father.right = node_right
node_left.father = node_father
node_right.father = node_father
queue[0]
.father =
none
return queue[0]
# huffman編碼
defhuffman_encoding
(nodes, root)
: huffman_code =[''
]*len(nodes)
for i in
range
(len
(nodes)):
node = nodes[i]
while node != root:
if node.is_left():
huffman_code[i]
='0'
+ huffman_code[i]
else
: huffman_code[i]
='1'
+ huffman_code[i]
node = node.father
return huffman_code
# 編碼整個字串
defencode_str
(text, char_frequency, codes)
: ret =
''for char in text:
i =0for item in char_frequency:
if char == item[0]
: ret += codes[i]
i +=
1return ret
# 解碼整個字串
defdecode_str
(huffman_str, char_frequency, codes)
: ret =
''while huffman_str !='':
i =0for item in codes:
if item in huffman_str and huffman_str.index(item)==0
: ret += char_frequency[i][0
] huffman_str = huffman_str[
len(item):]
i +=
1return ret
if __name__ ==
'__main__'
: text =
raw_input
('the text to encode:'
) char_frequency = count_frequency(text)
nodes = create_nodes(
[item[1]
for item in char_frequency]
) root = create_huffman_tree(nodes)
codes = huffman_encoding(nodes, root)
huffman_str = encode_str(text, char_frequency, codes)
origin_str = decode_str(huffman_str, char_frequency, codes)
print
'encode result:'
+ huffman_str
print
'decode result:'
+ origin_str
哈夫曼樹和哈夫曼編碼
1.帶權最短路徑wpl 2.哈夫曼樹是wpl最小的樹,但樹不一定唯一。左右子樹交換,大於二的同權重的樹的任意組合都會影響其唯一性。3.n個葉子節點的總節點數為2n 1。4.哈夫曼樹的建立,每次將兩個權重最小的樹組合乙個大樹 小樹消失,大樹插入。這裡的存在與否依據parent標記 直到還有一棵樹為止。...
哈夫曼樹和哈夫曼編碼
原文參照 1.基本概念 節點之間的路徑長度 在樹中從乙個結點到另乙個結點所經歷的分支,構成了這兩個結點間的路徑上的經過的分支數稱為它的路徑長度 樹的路徑長度 從樹的 根節點到樹中每一結點的 路徑長度之和。在結點數目相同的二叉樹中,完全二叉樹的路徑長度最短。結點的權 在一些應用中,賦予樹中結點的乙個有...
哈夫曼樹和哈夫曼編碼
在一般的資料結構的書中,樹的那章後面,著者一般都會介紹一下哈夫曼 huffman 樹和哈夫曼編碼。哈夫曼編碼是哈夫曼樹的乙個應用。哈夫曼編碼應用廣泛,如jpeg中就應用了哈夫曼編碼。首先介紹什麼是哈夫曼樹。哈夫曼樹又稱最優二叉樹,是一種帶權路徑長度最短的二叉樹。所謂樹的帶權路徑長度,就是樹中所有的葉...