贪心法求解哈夫曼编码问题。 设要编码的字符集为{d1, d2, …, dn},它们出现的频率为{w1, w2, …, wn},应用哈夫曼树构造最优的不等长的由0、1构成的编码方案。python
时间: 2023-10-04 14:08:27 浏览: 47
【贪心算法】哈夫曼编码.doc
以下是python代码实现:
```python
class Node:
def __init__(self, freq, char=None):
self.freq = freq
self.char = char
self.left = None
self.right = None
def huffman_encoding(data):
if not data:
return None, None
freq = {}
for char in data:
freq[char] = freq.get(char, 0) + 1
nodes = [Node(freq[char], char) for char in freq]
while len(nodes) > 1:
nodes = sorted(nodes, key=lambda x: x.freq)
left = nodes.pop(0)
right = nodes.pop(0)
parent = Node(left.freq + right.freq)
parent.left = left
parent.right = right
nodes.append(parent)
root = nodes[0]
codes = {}
stack = [(root, '')]
while stack:
node, code = stack.pop()
if node.char:
codes[node.char] = code
if node.left:
stack.append((node.left, code + '0'))
if node.right:
stack.append((node.right, code + '1'))
encoded_data = ''
for char in data:
encoded_data += codes[char]
return encoded_data, codes
def huffman_decoding(encoded_data, codes):
if not encoded_data or not codes:
return None
reversed_codes = {v: k for k, v in codes.items()}
decoded_data = ''
i = 0
while i < len(encoded_data):
j = i + 1
while encoded_data[i:j] not in reversed_codes:
j += 1
decoded_data += reversed_codes[encoded_data[i:j]]
i = j
return decoded_data
```
我们可以用以下代码测试:
```python
data = 'aabbccdd'
encoded_data, codes = huffman_encoding(data)
print('Encoded data:', encoded_data)
print('Codes:', codes)
decoded_data = huffman_decoding(encoded_data, codes)
print('Decoded data:', decoded_data)
```
输出结果为:
```
Encoded data: 00110011001010010110111100
Codes: {'a': '001', 'c': '011', 'b': '000', 'd': '1'}
Decoded data: aabbccdd
```
可以看到,我们成功实现了哈夫曼编码和解码的功能。
阅读全文