写一段哈夫曼树的python代码
时间: 2023-09-03 09:27:31 浏览: 193
### 回答1:
这是一段简单的哈夫曼树的 Python 代码:
```python
from collections import defaultdict
def huffman_tree(frequencies):
# 建立哈夫曼树
# frequencies 是一个字典,键为字符,值为频率
heap = [[weight, [symbol, '']] for symbol, weight in frequencies.items()]
heap.sort()
while len(heap) > 1:
low = heap.pop(0)
high = heap.pop(0)
for pair in low[1:]:
pair[1] = '0' + pair[1]
for pair in high[1:]:
pair[1] = '1' + pair[1]
heap.append([low[0] + high[0]] + low[1:] + high[1:])
heap.sort()
return sorted(heap[0][1:], key=lambda p: (len(p[-1]), p))
# 测试
frequencies = defaultdict(int)
string = "this is an example for huffman encoding"
for char in string:
frequencies[char] += 1
print(huffman_tree(frequencies))
```
这段代码会输出哈夫曼编码后的字符及其对应的编码,比如:
```
[[' ', '01'], ['a', '100'], ['c', '1010'], ['d', '1011'], ['e', '1100'], ['f', '1101'], ['g', '111'], ['h', '1000'], ['i', '101'], ['m', '1110'], ['n', '1111'], ['o', '001'], ['p', '0000'], ['r', '0001'], ['s', '0010'], ['t', '0011'], ['u', '010'], ['x', '011']]
```
上面的代码中,字符串 `string` 为待编码的字符串,字典 `frequencies` 用来统计字符串中每个字符的出现次数。然后调用 `huffman_tree` 函数,传入 `frequencies` 字典,得到哈夫曼编码后的字符及其对应的编码。
### 回答2:
哈夫曼树(Huffman Tree)是一种用于数据压缩的二叉树结构,根据数据出现的频率构建的树。这里提供一个用Python实现哈夫曼树的简单代码示例:
```python
class HuffmanNode:
def __init__(self, freq, data, left=None, right=None):
self.freq = freq
self.data = data
self.left = left
self.right = right
def build_huffman_tree(char_freq):
nodes = [HuffmanNode(freq, char) for char, freq in char_freq.items()]
while len(nodes) > 1:
nodes = sorted(nodes, key=lambda x: x.freq)
left = nodes.pop(0)
right = nodes.pop(0)
freq = left.freq + right.freq
parent = HuffmanNode(freq, None, left, right)
nodes.append(parent)
return nodes[0]
def huffman_encoding(root, code_dict, code=''):
if root.data is not None:
code_dict[root.data] = code
return
huffman_encoding(root.left, code_dict, code + '0')
huffman_encoding(root.right, code_dict, code + '1')
def huffman_decoding(root, code):
decoded = ''
current = root
for bit in code:
if bit == '0':
current = current.left
else:
current = current.right
if current.data is not None:
decoded += current.data
current = root
return decoded
if __name__ == '__main__':
char_freq = {'a': 5, 'b': 9, 'c': 12, 'd': 13, 'e': 16, 'f': 45}
huffman_tree = build_huffman_tree(char_freq)
code_dict = {}
huffman_encoding(huffman_tree, code_dict)
print('Huffman Codes:')
for char, code in code_dict.items():
print(char, code)
encoded_text = ''.join(code_dict[char] for char in input_text)
print('Encoded Text:', encoded_text)
decoded_text = huffman_decoding(huffman_tree, encoded_text)
print('Decoded Text:', decoded_text)
```
以上代码实现了哈夫曼树的构建、编码和解码功能。我们首先构建了哈夫曼树的节点类HuffmanNode,然后使用build_huffman_tree函数构建哈夫曼树。接着,使用huffman_encoding函数对哈夫曼树进行编码,生成字符与编码之间的映射关系。最后,使用huffman_decoding函数对编码进行解码,并输出结果。
这个示例代码中,我们假设输入的字符及其频率储存在char_freq字典中,可以根据实际情况进行修改。
### 回答3:
哈夫曼树是一种用于数据压缩的树形结构,其中出现频率越高的字符在树中的路径越短。下面是一个用Python实现的哈夫曼树代码示例。
```python
class TreeNode:
def __init__(self, value, freq):
self.value = value
self.freq = freq
self.left = None
self.right = None
def build_huffman_tree(freq_dict):
nodes = []
# 将每个字符和它的频率作为节点,放入列表中
for value, freq in freq_dict.items():
nodes.append(TreeNode(value, freq))
while len(nodes) > 1:
# 按照频率对节点列表进行排序
nodes = sorted(nodes, key=lambda x: x.freq)
# 取出频率最低的两个节点
left = nodes[0]
right = nodes[1]
# 构建一个新节点作为它们的父节点,频率为两个节点频率之和
parent = TreeNode(None, left.freq + right.freq)
parent.left = left
parent.right = right
# 将新节点加入节点列表中
nodes.append(parent)
# 从节点列表中移除已处理的两个节点
nodes = nodes[2:]
# 返回哈夫曼树的根节点
return nodes[0]
# 测试
freq_dict = {'A': 5, 'B': 9, 'C': 12, 'D': 13, 'E': 16, 'F': 45}
root = build_huffman_tree(freq_dict)
print(root.value) # 输出 None
```
以上代码中,首先定义了一个`TreeNode`类表示哈夫曼树中的节点。`build_huffman_tree`函数接受一个字符频率字典作为输入,根据频率构建哈夫曼树并返回根节点。在构建哈夫曼树的过程中,首先将每个字符的频率转化为节点,然后不断合并频率最低的两个节点,直到只剩下一个节点为止。最后通过`print(root.value)`输出根节点的值,即`None`。
阅读全文