python 霍夫曼编码的代码,最好是可以import的
时间: 2024-09-18 18:02:13 浏览: 41
霍夫曼编码是一种基于字符频率自适应的前缀编码算法,常用于数据压缩。在Python中,我们可以使用`collections`模块结合字典和堆来实现霍夫曼树及其编码过程。下面是一个简单的霍夫曼编码生成器的例子:
```python
from heapq import heapify, heappush, heappop
import collections
class Node:
def __init__(self, char, freq):
self.char = char
self.freq = freq
self.left = None
self.right = None
def __lt__(self, other):
return self.freq < other.freq
def build_huffman_tree(freqs):
huffman_tree = []
for char, freq in freqs.items():
node = Node(char, freq)
heapify(huffman_tree)
heappush(huffman_tree, node)
while len(huffman_tree) > 1:
left = heappop(huffman_tree)
right = heappop(huffman_tree)
combined = Node(None, left.freq + right.freq)
combined.left, combined.right = left, right
heappush(huffman_tree, combined)
return heappop(huffman_tree)
def generate_code(node, code='', codes={}):
if node is None or node.char is not None:
codes[node.char] = code
return
generate_code(node.left, code + '0', codes)
generate_code(node.right, code + '1', codes)
def huffman_encoding(data, tree):
encoded_data = ''
for char in data:
encoded_data += tree[char]
return encoded_data, codes
# 示例用法
freq_dict = {'a': 45, 'b': 15, 'c': 20} # 假设这是输入文本的字符频率
huff_tree = build_huffman_tree(freq_dict)
encoded_data, codes = huffman_encoding('abcabcc', huff_tree)
print(f"Huffman Codes: {codes}")
print(f"Encoded Data: {encoded_data}")
阅读全文