# coding:utf-8
#tree-node type
class
node
:def
__init__
(self,freq)
: self.left =
none
self.right =
none
self.father =
none
self.freq = freq
defisleft
(self)
:return self.father.left == self
#create nodes建立葉子節點
defcreatenodes
(freqs)
:return
[node(freq)
for freq in freqs]
#create huffman-tree建立huffman樹
defcreatehuffmantree
(nodes)
: queue = nodes[:]
while
len(queue)
>1:
queue.sort(key=
lambda item:item.freq)
node_left = queue.pop(0)
node_right = queue.pop(0)
node_father = node(node_left.freq + node_right.freq)
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編碼
defhuffmanencoding
(nodes,root)
: codes =[''
]*len(nodes)
for i in
range
(len
(nodes)):
node_tmp = nodes[i]
while node_tmp != root:
if node_tmp.isleft():
codes[i]
='0'
+ codes[i]
else
: codes[i]
='1'
+ codes[i]
node_tmp = node_tmp.father
return codes
# 解壓縮huffman檔案
defdecode_huffman
(input_string, char_store, freq_store)
:#input_string 哈夫曼編碼
#char_store 字元集合
#freq_store 字元轉編碼01序列
encode =
'' decode =
''for index in
range
(len
(input_string)):
encode = encode + input_string[index]
for item in
zip(char_store, freq_store)
:if encode == item[1]
: decode = decode + item[0]
encode =
''return decode;
#獲取huffman編碼
defgethuffmancode
(string)
:
dict1 =
for i in string:
if i in dict1.keys():
dict1[i]+=1
else
: dict1[i]=1
#將字元根據頻次排序
chars_freqs =
sorted
(dict1.items(
), key =
lambda kv:
(kv[1]
, kv[0]
))#建立huffman節點樹
nodes = createnodes(
[item[1]
for item in chars_freqs]
) root = createhuffmantree(nodes)
#每個字元的huffman編碼
codes = huffmanencoding(nodes,root)
#print codes
dict2 =
for item in
zip(chars_freqs,codes)
:#print 'character:%s freq:%-2d encoding: %s' % (item[0][0],item[0][1],item[1])
dict2[item[0]
[0]]
= item[1]
str=
''for v in string:
str+= dict2[v]
return
[str
,dict2]
gethuffmancode(string):獲取string字串的01編碼及單個字元和相對於的01序列
decode_huffman(string,chars,freqs):解碼,引數為編碼的返回值
藍橋杯python Huffman樹 哈夫曼樹
基礎訓練basic 28 huffman樹 問題描述 huffman樹在編碼中有著廣泛的應用。在這裡,我們只關心huffman樹的構造過程。給出一列數 用這列數構造huffman樹的過程如下 1.找到中最小的兩個數,設為pa和pb,將pa和pb從中刪除掉,然後將它們的和加入到中。這個過程的費用記為p...
字元編碼 unicode編碼
1.ascii american standard code for information interchange 美國資訊交換標準 這是計算機上最早使用的通用的編碼方案。那個時候計算機還只是拉丁文本的專利,根本沒有想到現在計算機的發展勢頭,如果想到了,可能一開始就會使用unicode了。當時絕大...
信源編碼 huffman編碼
1.對omaha.img sensin.img以及sena.img三個檔案先使用matlab程式設計求相鄰畫素之差,對差值進行huffman編碼以及解碼。得到以下的的結果。可以看出,用huffman編碼進行壓縮時,符號概率分布越不均勻,信源壓縮效果越好。進行差分處理後,各個檔案的信源符號分布概率成拉...