构建Huffman哈夫曼最优二叉树,binarytree,Python

x33g5p2x  于2021-12-13 转载在 Python  
字(2.0k)|赞(0)|评价(0)|浏览(350)

Huffman Tree,哈夫曼树(又被称为霍夫曼树、赫夫曼树),是一种基于贪心算法思想构建的二叉树,贪心算法寻求在建树过程中局部最优,最终迭代达到全局最优,从中可以看出,Huffman树的构建也体现了动态规划思想。Huffman Tree的贪心算法思想,寻找一种二叉树,使得WPL带路权的最小二叉树,因此,哈夫曼树也称为最优二叉树。

现在基于binarytree,用Python构建哈夫曼树:

  1. import random
  2. from binarytree import Node
  3. def app():
  4. data = []
  5. # 生成随机测试数据。
  6. for i in range(5):
  7. data.append(random.randint(1, 50))
  8. random.shuffle(data)
  9. print(data)
  10. huffman_tree = build_huffman_tree(data)
  11. print('-----')
  12. print('最终的Huffman树')
  13. print(huffman_tree.pop())
  14. print('---')
  15. print(data)
  16. def build_huffman_tree(data):
  17. nodes = []
  18. for i in data:
  19. nodes.append(Node(i))
  20. print('nodes', nodes)
  21. print('开始构建Huffman树')
  22. while True:
  23. print('-')
  24. nodes = sorted(nodes, key=lambda x: x.value)
  25. print('nodes', nodes)
  26. if len(nodes) == 1:
  27. print('仅剩一个节点,建树结束')
  28. break
  29. left = nodes.pop(0)
  30. right = nodes.pop(0)
  31. print('选取节点', left.value, right.value)
  32. root = Node(left.value + right.value)
  33. root.left = left
  34. root.right = right
  35. nodes.append(root)
  36. return nodes
  37. if __name__ == '__main__':
  38. app()

测试几轮算法日志输出:

  1. [22, 26, 29, 10, 24]
  2. nodes [Node(22), Node(26), Node(29), Node(10), Node(24)]
  3. 开始构建Huffman
  4. -
  5. nodes [Node(10), Node(22), Node(24), Node(26), Node(29)]
  6. 选取节点 10 22
  7. -
  8. nodes [Node(24), Node(26), Node(29), Node(32)]
  9. 选取节点 24 26
  10. -
  11. nodes [Node(29), Node(32), Node(50)]
  12. 选取节点 29 32
  13. -
  14. nodes [Node(50), Node(61)]
  15. 选取节点 50 61
  16. -
  17. nodes [Node(111)]
  18. 仅剩一个节点,建树结束
  19. -----
  20. 最终的Huffman
  21. ____111___
  22. / \
  23. _50 _61___
  24. / \ / \
  25. 24 26 29 _32
  26. / \
  27. 10 22
  28. ---
  29. [22, 26, 29, 10, 24]
  1. [21, 38, 18, 27, 5]
  2. nodes [Node(21), Node(38), Node(18), Node(27), Node(5)]
  3. 开始构建Huffman
  4. -
  5. nodes [Node(5), Node(18), Node(21), Node(27), Node(38)]
  6. 选取节点 5 18
  7. -
  8. nodes [Node(21), Node(23), Node(27), Node(38)]
  9. 选取节点 21 23
  10. -
  11. nodes [Node(27), Node(38), Node(44)]
  12. 选取节点 27 38
  13. -
  14. nodes [Node(44), Node(65)]
  15. 选取节点 44 65
  16. -
  17. nodes [Node(109)]
  18. 仅剩一个节点,建树结束
  19. -----
  20. 最终的Huffman
  21. _________109___
  22. / \
  23. _44__ _65
  24. / \ / \
  25. 21 23 27 38
  26. / \
  27. 5 18
  28. ---
  29. [21, 38, 18, 27, 5]
  1. [15, 2, 13, 31, 44]
  2. nodes [Node(15), Node(2), Node(13), Node(31), Node(44)]
  3. 开始构建Huffman
  4. -
  5. nodes [Node(2), Node(13), Node(15), Node(31), Node(44)]
  6. 选取节点 2 13
  7. -
  8. nodes [Node(15), Node(15), Node(31), Node(44)]
  9. 选取节点 15 15
  10. -
  11. nodes [Node(30), Node(31), Node(44)]
  12. 选取节点 30 31
  13. -
  14. nodes [Node(44), Node(61)]
  15. 选取节点 44 61
  16. -
  17. nodes [Node(105)]
  18. 仅剩一个节点,建树结束
  19. -----
  20. 最终的Huffman
  21. _105______________
  22. / \
  23. 44 _________61
  24. / \
  25. _30__ 31
  26. / \
  27. 15 15
  28. / \
  29. 2 13
  30. ---
  31. [15, 2, 13, 31, 44]

相关文章