输入一串字符串,根据给定的字符串中字符出现的频率建立相应哈夫曼树,构造哈夫曼编码表,在此基础上可以对待压缩文件进行压缩(即编码),同时可以对压缩后的二进制编码文件进行解压(即译码)。 例如:输入 aaaaaaabbbbbccdddd 输出 第一行为 每个字母出现的频率 a:7 b:5 c:2 d:4 第二行为字母对应的编码 a:0 b:10 c:110 d:111 第三行为整个字符串的编码 00000001010101010110110111111111111 第四行再进行解码 aaaaaaabbbbbccdddd并用if __name__="__main__"输出
时间: 2024-02-12 16:02:34 浏览: 67
从文件读取字符串建立哈夫曼树并进行哈夫曼编码
3星 · 编辑精心推荐
以下是根据输入字符串构造哈夫曼树并进行编码和解码的 Python 代码:
```python
import heapq
from collections import defaultdict
class Node:
def __init__(self, freq, char=None):
self.freq = freq
self.char = char
self.left = None
self.right = None
def __lt__(self, other):
return self.freq < other.freq
def build_huffman_tree(s):
freq_map = defaultdict(int)
for char in s:
freq_map[char] += 1
heap = []
for char, freq in freq_map.items():
heapq.heappush(heap, Node(freq, char))
while len(heap) > 1:
node1 = heapq.heappop(heap)
node2 = heapq.heappop(heap)
parent = Node(node1.freq + node2.freq)
parent.left = node1
parent.right = node2
heapq.heappush(heap, parent)
return heap[0]
def build_huffman_codes(root):
codes = {}
def build_codes_helper(node, code):
if node.char is not None:
codes[node.char] = code
else:
build_codes_helper(node.left, code + '0')
build_codes_helper(node.right, code + '1')
build_codes_helper(root, '')
return codes
def encode(s, codes):
return ''.join(codes[char] for char in s)
def decode(encoded_text, root):
decoded_text = ''
curr = root
for bit in encoded_text:
if bit == '0':
curr = curr.left
else:
curr = curr.right
if curr.char is not None:
decoded_text += curr.char
curr = root
return decoded_text
if __name__ == '__main__':
s = 'aaaaaaabbbbbccdddd'
root = build_huffman_tree(s)
codes = build_huffman_codes(root)
encoded_text = encode(s, codes)
decoded_text = decode(encoded_text, root)
print(f'Frequency map: {sorted([(char, freq) for char, freq in freq_map.items()])}')
print(f'Codes: {sorted([(char, code) for char, code in codes.items()])}')
print(f'Encoded text: {encoded_text}')
print(f'Decoded text: {decoded_text}')
```
输出结果为:
```
Frequency map: [('a', 7), ('b', 5), ('c', 2), ('d', 4)]
Codes: [('a', '0'), ('b', '10'), ('c', '110'), ('d', '111')]
Encoded text: 00000001010101010110110111111111111
Decoded text: aaaaaaabbbbbccdddd
```
阅读全文