哈夫曼树是一种重要的数据结构,用于压缩和编码数据。它由经典的数学家和计算机科学家大卫哈夫曼在20世纪50年代发明。哈夫曼树的目的是为了在编码和解码数据中,尽可能地减少所需的比特数。换句话说,它可以将大量数据压缩为在传输过程中所需的最小比特数。
在NLP领域的词向量开篇制作Word2Vec中用到了一种softmax优化方法——层次softmax,就是将词频编码成哈夫曼树的形式,然后,(以skip-gram为例)在样本[v, w]
进入模型前,将周围词w
,基于哈夫曼树映射成从根到叶路径两个方向路径right_path=[p1, p2]; left_path=[n1, n2]
,最终组成[[v, p1], [v, p2]]; [[v, n1], [v, n2]
,最后以v, p, v, n
四个一维向量的方式进入模型。
哈夫曼树的构建过程非常简单,可以分为以下几个步骤:
借助优先队列headq
便于快速输出最小的2个节点
import heapq
class Node:
def __init__(self, idx, freq, char, left, right):
self.idx = idx
self.freq = freq
self.char = char
self.left = left
self.right = right
def __lt__(self, other):
return self.freq < other.freq
def __str__(self):
return f'Node([{self.idx}]({self.char}/{self.freq}), left={self.left}, right={self.right})'
def __repr__(self):
return str(self)
def build_huffman_tree(data=None, freq_map=None):
if freq_map is None:
freq_map = dict()
for char in data:
freq_map[char] = freq_map.get(char, 0) + 1
print(freq_map)
word_map = [Node(idx, freq, char, None, None) for idx, (char, freq) in enumerate(freq_map.items())]
min_node = min(word_map)
print(min_node)
print('len(word_map)=', len(word_map))
# 初始化 因为heappop 会先输出第一个元素
node_heap = [min_node] + [Node(idx, freq, char, None, None) for idx, (char, freq) in enumerate(freq_map.items()) if char != min_node.char]
# 构建哈夫曼树
while len(node_heap) > 1:
# 优先队列, 弹出最小的2个
left_node = heapq.heappop(node_heap)
right_node = heapq.heappop(node_heap)
freq_sum = left_node.freq + right_node.freq
idx = len(word_map)
# 左大右小
combined_node = Node(idx, freq_sum, None, right_node, left_node)
heapq.heappush(node_heap, combined_node)
word_map.append(combined_node)
# 哈夫曼树最后
print('len(word_map)=', len(word_map))
return node_heap[0], word_map
if __name__ == "__main__":
test_dict = {
'a': 6, 'b': 3,
'c': 4, 'd': 2
}
huffman_tree, total_word_map = build_huffman_tree(freq_map=test_dict)
print(huffman_tree)
结果
res = """
Node([6](None/15),
left=Node([5](None/9),
left=Node([4](None/5),
left=Node([1](b/3), left=None, right=None),
right=Node([3](d/2), left=None, right=None)
),
right=Node([2](c/4), left=None, right=None)
),
right=Node([0](a/6), left=None, right=None)
)
"""
哈夫曼树已经构成了,哈夫曼code生成就简单了
基本思路就是: 当前节点的code = 父节点code + [当前方向code]
用队列来做辅助:存储未处理的节点
import heapq
from collections import deque
class Node:
def __init__(self, idx, freq, char, left, right):
self.idx = idx
self.freq = freq
self.char = char
self.left = left
self.right = right
self.code = []
self.path = []
def __lt__(self, other):
return self.freq < other.freq
def __str__(self):
return f'Node([{self.idx}]({self.char}/{self.freq}), left={self.left}, right={self.right})'
def __repr__(self):
return str(self)
def build_huffman_tree(data=None, freq_map=None):
if freq_map is None:
freq_map = dict()
for char in data:
freq_map[char] = freq_map.get(char, 0) + 1
print(freq_map)
word_map = [Node(idx, freq, char, None, None) for idx, (char, freq) in enumerate(freq_map.items())]
min_node = min(word_map)
print(min_node)
print('len(word_map)=', len(word_map))
# 初始化 因为heappop 会先输出第一个元素
node_heap = [min_node] + [i for i in word_map if i.char != min_node.char]
# 构建哈夫曼树
while len(node_heap) > 1:
# 优先队列, 弹出最小的2个
left_node = heapq.heappop(node_heap)
right_node = heapq.heappop(node_heap)
freq_sum = left_node.freq + right_node.freq
idx = len(word_map)
# 左大右小
combined_node = Node(idx, freq_sum, None, right_node, left_node)
heapq.heappush(node_heap, combined_node)
word_map.append(combined_node)
# 哈夫曼树最后
print('len(word_map)=', len(word_map))
h_tree = complete_code_path(node_heap[0])
return h_tree, word_map
def complete_code_path(h_tree):
help_list = deque()
h_tree.path = [h_tree.idx]
h_tree.code = [-1]
help_list.append(h_tree)
while len(help_list):
n = help_list.popleft()
while n.left:
l = n.left
r = n.right
l.code = n.code + [1]
r.code = n.code + [0]
l.path = n.path + [l.idx]
r.path = n.path + [r.idx]
help_list.append(r)
n = l
return h_tree
if __name__ == '__main__':
test_dict = {
'a': 6, 'b': 3,
'c': 4, 'd': 2
}
h_tree, total_word_map = build_huffman_tree(freq_map=test_dict)
for i in total_word_map:
print('--'*25)
print(i)
print('code=', i.code)
print('path=', i.path)
结果如下
Node([3](d/2), left=None, right=None)
len(word_map)= 4
len(word_map)= 7
--------------------------------------------------
Node([0](a/6), left=None, right=None)
code= [-1, 0]
path= [6, 0]
--------------------------------------------------
Node([1](b/3), left=None, right=None)
code= [-1, 1, 1, 1]
path= [6, 5, 4, 1]
--------------------------------------------------
Node([2](c/4), left=None, right=None)
code= [-1, 1, 0]
path= [6, 5, 2]
--------------------------------------------------
Node([3](d/2), left=None, right=None)
code= [-1, 1, 1, 0]
path= [6, 5, 4, 3]
--------------------------------------------------
Node([4](None/5), left=Node([1](b/3), left=None, right=None), right=Node([3](d/2), left=None, right=None))
code= [-1, 1, 1]
path= [6, 5, 4]
--------------------------------------------------
Node([5](None/9), left=Node([4](None/5), left=Node([1](b/3), left=None, right=None), right=Node([3](d/2), left=None, right=None)), right=Node([2](c/4), left=None, right=None))
code= [-1, 1]
path= [6, 5]
--------------------------------------------------
Node([6](None/15), left=Node([5](None/9), left=Node([4](None/5), left=Node([1](b/3), left=None, right=None), right=Node([3](d/2), left=None, right=None)), right=Node([2](c/4), left=None, right=None)), right=Node([0](a/6), left=None, right=None))
code= [-1]
path= [6]