Java 實現最大堆

最大堆的特點是父元素比子元素大,並且是一棵完全二叉樹。

堆接口:

  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. }
運行結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章