欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 实现最大堆

程序员文章站 2024-02-13 14:08:40
...

最大堆的特点是父元素比子元素大,并且是一棵完全二叉树。

堆接口:

  1. /**
  2. * Create by zxb on 2017/9/10
  3. */
  4. public interface IHeap<T extends Comparable<T>> {
  5. void display();
  6. void initOriginList(List<T> orginList);
  7. void makeHeap(int first, int last);
  8. void popHeap(int first, int last);
  9. void pushHeap(int first, int last);
  10. List<T> getHeap();
  11. }
堆实现,这里采用的是动态数组来做存储

  1. /**
  2. * Create by zxb on 2017/9/10
  3. */
  4. public class HeapImpl<T extends Comparable<T>> implements IHeap<T> {
  5. private List<T> heap;
  6. private List<T> orginList;
  7. public void initOriginList(List<T> orginList) {
  8. this.orginList = orginList;
  9. }
  10. public List<T> getHeap() {
  11. return heap;
  12. }
  13. public HeapImpl() {
  14. this.heap = new ArrayList<>();
  15. }
  16. /**
  17. * 插入对应上浮
  18. *
  19. * @param start
  20. */
  21. protected void adjustUp(int start) {
  22. int currentIndex = start;
  23. int parentIndex = (currentIndex - 1) / 2;
  24. T tmp = heap.get(currentIndex);
  25. while (currentIndex > 0) {
  26. int cmp = heap.get(parentIndex).compareTo(tmp);
  27. if (cmp >= 0) {
  28. break;
  29. } else {
  30. heap.set(currentIndex, heap.get(parentIndex));
  31. currentIndex = parentIndex;
  32. parentIndex = (parentIndex - 1) / 2;
  33. }
  34. }
  35. heap.set(currentIndex, tmp);
  36. }
  37. public void insert(T data) {
  38. int size = heap.size();
  39. heap.add(data); // 将"数组"插在表尾
  40. adjustUp(size); // 向上调整堆
  41. }
  42. public void remove(int index) {
  43. int size = heap.size();
  44. heap.set(index, heap.get(size - 1));
  45. heap.remove(size - 1);
  46. adjustDown(index);
  47. }
  48. /**
  49. * 删除对应下沉
  50. *
  51. * @param index
  52. */
  53. private void adjustDown(int index) {
  54. int currentIndex = index;
  55. int leftChildIndex = index * 2 + 1;
  56. int rightChildIndex = index * 2 + 2;
  57. T tmp = heap.get(currentIndex);
  58. int size = heap.size();
  59. while (leftChildIndex < size) {
  60. T left = null;
  61. T right = null;
  62. if (leftChildIndex < size) {
  63. left = heap.get(leftChildIndex);
  64. }
  65. if (rightChildIndex < size) {
  66. right = heap.get(rightChildIndex);
  67. }
  68. int maxIndex = right == null ? leftChildIndex : (left.compareTo(right) >= 0 ? leftChildIndex : rightChildIndex);
  69. T max = heap.get(maxIndex);
  70. if(tmp.compareTo(max)>=0){
  71. break;
  72. }else{
  73. heap.set(currentIndex, max);
  74. heap.set(maxIndex, tmp);
  75. leftChildIndex = maxIndex * 2 +1;
  76. rightChildIndex = maxIndex +1;
  77. }
  78. }
  79. }
  80. public void makeHeap(int first, int last) {
  81. for (int i = first; i < last; i++) {
  82. insert(orginList.get(i));
  83. }
  84. }
  85. public void popHeap(int first, int last) {
  86. remove(first);
  87. }
  88. public void pushHeap(int first, int last) {
  89. adjustUp(last - 1);
  90. }
  91. public void display() {
  92. System.out.println(heap);
  93. }
  94. public static void main(String[] args) {
  95. IHeap<Integer> heap = new HeapImpl<>();
  96. heap.initOriginList(Arrays.asList(10, 20, 30, 5, 15));
  97. System.out.println("初始构建堆:");
  98. heap.makeHeap(0, 5);
  99. heap.display();
  100. System.out.println("弹出堆顶:");
  101. heap.popHeap(0, 5);
  102. heap.display();
  103. System.out.println("弹出堆顶:");
  104. heap.popHeap(0, 4);
  105. heap.display();
  106. System.out.println("插入堆尾:");
  107. heap.getHeap().add(90);
  108. heap.display();
  109. System.out.println("重新排列:");
  110. heap.pushHeap(0, 4);
  111. heap.display();
  112. }
  113. }
运行结果:

Java 实现最大堆

相关标签: java