JAVA实现哈夫曼树

  
  
  1. **
  2. * 创建哈夫曼树,并打印。
  3. *
  4. * @author timmy1 1.创建树的节点:包括节点的数据,他指向的左节点和右节点 2.使用list集合保存所有的节点元素
  5. * 3.创建树:--》先对所有的节点进行排序,-->根据节点的数据大小从大到小排序-->每次取最小的两个元素,组合一个新的元素
  6. * --》将新元素放到集合中,最合的集合只剩一个元素,就是根元素
  7. */
  8. public class HuffmanTree {
  9. public static class Node<E> {
  10. E e;
  11. int weight;
  12. Node<E> leftChild;// 左节点
  13. Node<E> rightChild;// 右节点
  14. public Node(E e, int weight) {
  15. this.e = e;
  16. this.weight = weight;
  17. }
  18. public Node(E e, int weight, Node<E> leftChild, Node<E> rightChild) {
  19. this.e = e;
  20. this.weight = weight;
  21. this.leftChild = leftChild;
  22. this.rightChild = rightChild;
  23. }
  24. }
  25. /**
  26. * 创建哈夫曼树--并返回根节点
  27. *
  28. * @param nodeList
  29. * @return 先对所有的节点进行排序,-->根据节点的数据大小从大到小排序-->每次取最小的两个元素,组合一个新的元素
  30. * --》将新元素放到集合中,最合的集合只剩一个元素,就是根元素
  31. */
  32. private Node<String> createHuffmanTree(List<Node<String>> nodeList) {
  33. while (nodeList.size() != 1) {// 最后剩一个元素时退出循环
  34. // 根据元素的权重(weight)进行从大到小排序
  35. nodeListSort(nodeList);
  36. // 取最小的两个元素--》组成一个新的元素
  37. int size = nodeList.size();
  38. Node<String> left = nodeList.get(size - 1);
  39. Node<String> right = nodeList.get(size - 2);
  40. Node<String> newNode = new Node<String>("new", left.weight + right.weight, left, right);
  41. // 将最小的两个元素删除,并添加新生成的节点元素
  42. nodeList.remove(left);
  43. nodeList.remove(right);
  44. nodeList.add(newNode);
  45. }
  46. return nodeList.get(0);
  47. }
  48. /**
  49. * 使用插入排序按照元素的weight进行从大到小排序
  50. *
  51. * @param nodeList
  52. */
  53. private void nodeListSort(List<Node<String>> nodeList) {
  54. int size = nodeList.size();
  55. for (int i = 1; i < size; i++) {
  56. Node<String> node = nodeList.get(i);
  57. int j = i - 1;
  58. // 第0个比第1个小
  59. for (; j >= 0; j--) {
  60. if (nodeList.get(j).weight < node.weight) {
  61. swap(nodeList, j, j + 1);
  62. } else {
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * List中的位置i和位置j的元素进行交换
  70. *
  71. * @param nodeList
  72. * @param j
  73. * @param i
  74. */
  75. private void swap(List<Node<String>> nodeList, int j, int i) {
  76. Node<String> temp = nodeList.get(j);
  77. nodeList.set(j, nodeList.get(i));
  78. nodeList.set(i, temp);
  79. }
  80. public static void main(String[] args) {
  81. HuffmanTree hTree = new HuffmanTree();
  82. List<Node<String>> nodeList = new ArrayList<Node<String>>();
  83. nodeList.add(new Node<String>("A", 12));
  84. nodeList.add(new Node<String>("B", 2));
  85. nodeList.add(new Node<String>("C", 4));
  86. nodeList.add(new Node<String>("D", 7));
  87. nodeList.add(new Node<String>("E", 9));
  88. nodeList.add(new Node<String>("F", 15));
  89. Node<String> rootNode = hTree.createHuffmanTree(nodeList);
  90. // 中序遍历二叉树
  91. midTraversalTree(rootNode);
  92. }
  93. /**
  94. * 中序遍历二叉树
  95. *
  96. * @param rootNode
  97. */
  98. private static void midTraversalTree(Node<String> node) {
  99. if (node == null) {
  100. return;
  101. } else {
  102. midTraversalTree(node.leftChild);
  103. System.out.println("节点:" + node.e + " 权重:" + node.weight);
  104. midTraversalTree(node.rightChild);
  105. }
  106. }
  107. }

打印结果:

节点:E 权重:9

节点:new 权重:21

节点:A 权重:12

节点:new 权重:49

节点:B 权重:2

节点:new 权重:6

节点:C 权重:4

节点:new 权重:13

节点:D 权重:7

节点:new 权重:28

节点:F 权重:15


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值