电文的编码和译码 任务:从键盘接收一串电文字符,输出对应的哈夫曼编码。同时,能翻译由哈夫曼编码生成的代码串,输出对应的电文字符串。 ①构造一棵哈夫曼树;②实现哈夫曼编码,并用哈夫曼编码生成的代码串进行译码;③程序中字符和权值是可变的,实现程序的灵活性。
时间: 2023-05-27 08:03:07 浏览: 233
实现代码如下:
①哈夫曼树的构造:
```python
class Node:
def __init__(self, value, freq):
self.value = value
self.freq = freq
self.left = None
self.right = None
def construct_huffman_tree(freq_dict):
nodes = [Node(value, freq) for value, freq in freq_dict.items()]
while len(nodes) > 1:
nodes = sorted(nodes, key=lambda x: x.freq)
left_node, right_node = nodes.pop(0), nodes.pop(0)
parent_node = Node(None, left_node.freq + right_node.freq)
parent_node.left, parent_node.right = left_node, right_node
nodes.append(parent_node)
return nodes[0]
```
其中,freq_dict是一个字典类型,记录每个字符出现的频率。
②哈夫曼编码和译码:
```python
class HuffmanCode:
def __init__(self, freq_dict):
self.freq_dict = freq_dict
self.root = construct_huffman_tree(freq_dict)
self.codes_dict = self._generate_codes_dict()
def _generate_codes_dict(self):
def dfs(node, path):
if not node:
return
if node.value is not None:
self.codes_dict[node.value] = path
if node.left:
dfs(node.left, path + "0")
if node.right:
dfs(node.right, path + "1")
self.codes_dict = {}
dfs(self.root, "")
return self.codes_dict
def encode(self, text):
result = ""
for ch in text:
result += self.codes_dict[ch]
return result
def decode(self, code):
result = ""
node = self.root
for bit in code:
node = node.left if bit == "0" else node.right
if node.value is not None:
result += node.value
node = self.root
return result
```
其中,_generate_codes_dict()函数通过遍历哈夫曼树,生成每个字符对应的哈夫曼编码。
③完整代码:
```python
class Node:
def __init__(self, value, freq):
self.value = value
self.freq = freq
self.left = None
self.right = None
def construct_huffman_tree(freq_dict):
nodes = [Node(value, freq) for value, freq in freq_dict.items()]
while len(nodes) > 1:
nodes = sorted(nodes, key=lambda x: x.freq)
left_node, right_node = nodes.pop(0), nodes.pop(0)
parent_node = Node(None, left_node.freq + right_node.freq)
parent_node.left, parent_node.right = left_node, right_node
nodes.append(parent_node)
return nodes[0]
class HuffmanCode:
def __init__(self, freq_dict):
self.freq_dict = freq_dict
self.root = construct_huffman_tree(freq_dict)
self.codes_dict = self._generate_codes_dict()
def _generate_codes_dict(self):
def dfs(node, path):
if not node:
return
if node.value is not None:
self.codes_dict[node.value] = path
if node.left:
dfs(node.left, path + "0")
if node.right:
dfs(node.right, path + "1")
self.codes_dict = {}
dfs(self.root, "")
return self.codes_dict
def encode(self, text):
result = ""
for ch in text:
result += self.codes_dict[ch]
return result
def decode(self, code):
result = ""
node = self.root
for bit in code:
node = node.left if bit == "0" else node.right
if node.value is not None:
result += node.value
node = self.root
return result
freq_dict = {}
text = input("请输入电文字符串:")
for ch in text:
freq_dict[ch] = freq_dict.get(ch, 0) + 1
huffman_code = HuffmanCode(freq_dict)
code = huffman_code.encode(text)
print("哈夫曼编码:", code)
text_decode = huffman_code.decode(code)
print("电文字符串:", text_decode)
```
阅读全文