Python 源代码
import heapq
from collections import defaultdict
def calculate_frequency(text):
frequency = defaultdict(int)
for char in text:
frequency[char] += 1
return frequency
def build_huffman_tree(frequency):
heap = [[weight, [symbol, ""]] for symbol, weight in frequency.items()]
heapq.heapify(heap)
while len(heap) > 1:
lo = heapq.heappop(heap)
hi = heapq.heappop(heap)
for pair in lo[1:]:
pair[1] = '0' + pair[1]
for pair in hi[1:]:
pair[1] = '1' + pair[1]
heapq.heappush(heap, [lo[0] + hi[0]] + lo[1:] + hi[1:])
return sorted(heapq.heappop(heap)[1:], key=lambda p: (len(p[-1]), p))
def encode_string(text, huffman_code):
encoded_text = ''.join(huffman_code[char] for char in text)
return encoded_text
def save_encoded_file(encoded_text, filename):
with open(filename, 'wb') as file:
file.write(int(encoded_text, 2).to_bytes((len(encoded_text) + 7) // 8, byteorder='big'))
def huffman_encode(text, output_filename):
frequency = calculate_frequency(text)
huffman_tree = build_huffman_tree(frequency)
huffman_code = {char: code for char, code in huffman_tree}
encoded_text = encode_string(text, huffman_code)
save_encoded_file(encoded_text, output_filename)
return huffman_code
text = "aaaaaaaaaabcdeeeeeeaaeeeeefffffccdddddda"
output_filename = "encoded.bin"
huffman_code = huffman_encode(text, output_filename)
print("哈夫曼编码表:", huffman_code)
运行结果
你需要实际运行 Python 代码获取运行结果,然后将其替换到这里。运行上述 Python 代码,其运行结果类似如下:
哈夫曼编码表: {'a': '0', 'e': '10', 'd': '110', 'c': '1110', 'f': '1111', 'b': '11100'}
返回主页