哈夫曼哈夫曼编/译码器
时间: 2023-12-15 11:03:28 浏览: 94
哈夫曼编码是一种可变长度编码,它将出现频率较高的字符用较短的编码表示,出现频率较低的字符用较长的编码表示,从而达到压缩数据的目的。哈夫曼编码的编码和解码过程都需要用到哈夫曼树,因此需要先构建哈夫曼树。
下面是一个Python实现的哈夫曼编/译码器的例子:
```python
import heapq
from collections import defaultdict
# 构建哈夫曼树
def build_huffman_tree(freq_dict):
heap = [[freq, [char, ""]] for char, freq in freq_dict.items()]
heapq.heapify(heap)
while len(heap) > 1:
left = heapq.heappop(heap)
right = heapq.heappop(heap)
for pair in left[1:]:
pair[1] = '0' + pair[1]
for pair in right[1:]:
pair[1] = '1' + pair[1]
heapq.heappush(heap, [left[0] + right[0]] + left[1:] + right[1:])
return sorted(heapq.heappop(heap)[1:], key=lambda x: (len(x[-1]), x))
# 编码
def encode(text):
freq_dict = defaultdict(int)
for char in text:
freq_dict[char] += 1
huffman_tree = build_huffman_tree(freq_dict)
code_dict = dict(huffman_tree)
encoded_text = ''.join([code_dict[char] for char in text])
return encoded_text, code_dict
# 解码
def decode(encoded_text, code_dict):
decoded_text = ''
code = ''
for bit in encoded_text:
code += bit
if code in code_dict:
decoded_text += code_dict[code]
code = ''
return decoded_text
```
阅读全文