如题,通过python实现哈夫曼编码,代码如下:
哈夫曼编码的思想为:在节点中每次找出两个出线频次最低的组合在一起,当迭代到最后只剩下一个节点时,该节点就为根节点,所有节点构成了huffman树,通过对树的左右孩子的路径设置为0,1,实现每一个字符的huffman编码。
import random
# 统计字符出现频次,然后每次找出两个频次最低的,合为一个节点,最后只有一个节点时,就构成了树。然后对树进行编码,并进行输出即可
# 定义哈夫曼树节点的类:
class huffmannode(object):
def __init__(self):
self.parent = 0
self.weight = 0
self.lchild = 0
self.rchild = 0
# 原始字符集
str_ori_list = []
for i in range(0, 100):
str_ori_list.append(random.randint(0, 10))
# 统计字符出现频次
fre_list = {}
def fre_str(str_list):
str_unique_list = list(set(str_list))
fre_list = dict.fromkeys(str_unique_list, 0)
for s in str_list:
fre_list[s] = fre_list[s] + 1
return fre_list
# a = fre_str(str_ori_list)
# print(a)
# 在huffman树节点中选择权重最小的两个节点
def selectNode(huffmantree):
s1 = -1
s2 = -1
min1 = -1
min2 = -1
for node in huffmantree:
if node.parent == 0 and node.weight > 0:
if s1 < 0 or min1 > node.weight:
s2 = s1
min2 = min1
s1 = huffmantree.index(node)
min1 = node.weight
elif s2 < 0 or min2 > node.weight:
s2 = huffmantree.index(node)
min2 = node.weight
return s1, s2
# 哈夫曼编码
def huffman_code(fre_str_dict):
# 每个节点的权重赋值
n = len(fre_str_dict)
m = 2 * len(fre_str_dict) - 1
huffman_tree = []
for i in fre_str_dict:
temp_huffmancode = huffmannode()
temp_huffmancode.weight = fre_str_dict[i]
huffman_tree.append(temp_huffmancode)
# 选择权重最小的两个节点,建立树结构
for i in range(n, m):
# 在huffman树节点中选择权重最小的两个节点
s1, s2 = selectNode(huffman_tree)
huffman_tree[s1].parent = huffman_tree[s2].parent = i
temp_huffmancode1 = huffmannode()
temp_huffmancode1.lchild = s1
temp_huffmancode1.rchild = s2
temp_huffmancode1.weight = huffman_tree[s1].weight + huffman_tree[s2].weight
huffman_tree.append(temp_huffmancode1)
# 根据构建好的huffman树进行编码
str_list = list(set(str_ori_list))
codeing_dict = dict.fromkeys(str_list, None)
print(codeing_dict)
for i in range(0, n):
temp_list = []
node = i
parent = huffman_tree[i].parent
while parent != 0:
if huffman_tree[parent].lchild == node:
temp_list.append(0)
else:
temp_list.append(1)
node = parent
parent = huffman_tree[node].parent
codeing_dict[str_list[i]] = list(reversed(temp_list))
return codeing_dict
#调用统计频数函数,返回字典:{key(字符):value(频数)}
unique_dict = fre_str(str_ori_list)
#构建huffman树,并返回字符编码
code_list = huffman_code(unique_dict)