Huffman哈夫曼树编码字符,binarytree,Python

x33g5p2x  于2021-12-15 转载在 Python  
字(1.9k)|赞(0)|评价(0)|浏览(321)

Huffman哈夫曼树(霍夫曼树,赫夫曼树)在通信领域最主要的应用是数据编码。假设现在有A、B、C、D、E五个字符,它们出现的概率或者权值不同,从A到E,权值依次降低,那么就可以用哈夫曼最优二叉树对其进行编码。左边为0,右边为1,通信网络电气链路的传输数据是0或者1。

  1. from binarytree import Node, get_parent
  2. def app():
  3. data = [(1, 'E'), (3, 'D'), (7, 'C'), (9, 'B'), (11, 'A')]
  4. huffman_tree = build_huffman_tree(data)
  5. print('-----')
  6. print('最终的Huffman树')
  7. root = huffman_tree.pop()
  8. print(root)
  9. print('---')
  10. print(data)
  11. travle(root, data)
  12. def travle(root, data):
  13. for leave in root.leaves:
  14. print('==')
  15. path = find_path(root, leave, data)
  16. d = find_data(data, leave.value)
  17. print('节点', d, '路径 ->', path)
  18. def find_data(data, v):
  19. for d in data:
  20. if d[0] == v:
  21. return d
  22. def build_huffman_tree(data):
  23. nodes = []
  24. for i in data:
  25. nodes.append(Node(i[0]))
  26. print('nodes', nodes)
  27. print('开始构建Huffman树')
  28. while True:
  29. nodes = sorted(nodes, key=lambda x: x.value)
  30. # print('nodes', nodes)
  31. if len(nodes) == 1:
  32. # print('仅剩一个节点,建树结束')
  33. break
  34. left = nodes.pop(0)
  35. right = nodes.pop(0)
  36. # print('选取节点', left.value, right.value)
  37. root = Node(left.value + right.value)
  38. root.left = left
  39. root.right = right
  40. nodes.append(root)
  41. return nodes
  42. def find_path(root, node, data):
  43. root_value = root.value
  44. path = [node]
  45. temp = node.value
  46. codes = []
  47. while True:
  48. p = get_parent(root, node)
  49. path.append(p)
  50. if p.left.value == node.value:
  51. codes.append(0)
  52. elif p.right.value == node.value:
  53. codes.append(1)
  54. if p.value == root_value:
  55. break
  56. else:
  57. node = p
  58. list.reverse(codes)
  59. s = ''.join(str(item) for item in codes)
  60. print(find_data(data, temp), '字符', find_data(data, temp)[1], '编码:', s)
  61. list.reverse(path)
  62. return path
  63. if __name__ == '__main__':
  64. app()

输出:

  1. nodes [Node(1), Node(3), Node(7), Node(9), Node(11)]
  2. 开始构建Huffman
  3. -----
  4. 最终的Huffman
  5. ___31__
  6. / \
  7. __11 20
  8. / \ / \
  9. 4 7 9 11
  10. / \
  11. 1 3
  12. ---
  13. [(1, 'E'), (3, 'D'), (7, 'C'), (9, 'B'), (11, 'A')]
  14. ==
  15. (7, 'C') 字符 C 编码: 01
  16. 节点 (7, 'C') 路径 -> [Node(31), Node(11), Node(7)]
  17. ==
  18. (9, 'B') 字符 B 编码: 10
  19. 节点 (9, 'B') 路径 -> [Node(31), Node(20), Node(9)]
  20. ==
  21. (11, 'A') 字符 A 编码: 11
  22. 节点 (11, 'A') 路径 -> [Node(31), Node(20), Node(11)]
  23. ==
  24. (1, 'E') 字符 E 编码: 000
  25. 节点 (1, 'E') 路径 -> [Node(31), Node(11), Node(4), Node(1)]
  26. ==
  27. (3, 'D') 字符 D 编码: 001
  28. 节点 (3, 'D') 路径 -> [Node(31), Node(11), Node(4), Node(3)]

相关文章