|
| 1 | +/** |
| 2 | + * http://algs4.cs.princeton.edu/24pq/ |
| 3 | + */ |
| 4 | +public class MinHeap { |
| 5 | + private int maxSize; |
| 6 | + private int current; |
| 7 | + private int[] heap; |
| 8 | + |
| 9 | + public MinHeap(int max) { |
| 10 | + maxSize = max; |
| 11 | + heap = new int[maxSize + 1]; |
| 12 | + current = 0; |
| 13 | + } |
| 14 | + |
| 15 | + public void insert(int value) { |
| 16 | + if (current == heap.length - 1) { |
| 17 | + throw new RuntimeException("Heap is full!"); |
| 18 | + } |
| 19 | + heap[++current] = value; |
| 20 | + shiftUp(current); |
| 21 | + } |
| 22 | + |
| 23 | + public boolean isEmpty() { |
| 24 | + return current == 0; |
| 25 | + } |
| 26 | + |
| 27 | + public int min() { |
| 28 | + if (isEmpty()) |
| 29 | + throw new RuntimeException("heap is empty"); |
| 30 | + return heap[1]; |
| 31 | + } |
| 32 | + |
| 33 | + public int removeMin() { |
| 34 | + if (isEmpty()) |
| 35 | + throw new RuntimeException("heap is empty"); |
| 36 | + |
| 37 | + int min = heap[1]; |
| 38 | + swap(1, current); |
| 39 | + // heap[current] = Integer.MAX_VALUE; |
| 40 | + current--; |
| 41 | + if(current != 0 ) |
| 42 | + shiftDown(1); |
| 43 | + return min; |
| 44 | + } |
| 45 | + |
| 46 | + private void shiftUp(int k) { |
| 47 | + // k/2 is parent |
| 48 | + while (k > 1 && heap[k / 2] > heap[k]) { |
| 49 | + swap(k / 2, k); |
| 50 | + k = k / 2; |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + private void shiftDown(int k) { |
| 56 | + while (2 * k <= current) { |
| 57 | + int i = 2 * k; // left child |
| 58 | + int j = i + 1; // right child |
| 59 | + //find the smallest of left and right |
| 60 | + if (i < current && heap[i] > heap[j]) { |
| 61 | + i++; |
| 62 | + } |
| 63 | + if (heap[k] < heap[i]) { |
| 64 | + break; |
| 65 | + } |
| 66 | + swap(k, i); |
| 67 | + k = i; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + private void swap(int i, int j) { |
| 73 | + int tmp = heap[i]; |
| 74 | + heap[i] = heap[j]; |
| 75 | + heap[j] = tmp; |
| 76 | + } |
| 77 | + |
| 78 | + // is pq[1..N] a min heap? |
| 79 | + private boolean isMinHeap() { |
| 80 | + return isMinHeap(1); |
| 81 | + } |
| 82 | + |
| 83 | + // is subtree of pq[1..N] rooted at k a min heap? |
| 84 | + private boolean isMinHeap(int k) { |
| 85 | + if (k > current) return true; |
| 86 | + int left = 2 * k, right = 2 * k + 1; |
| 87 | + if (left <= current && heap[k] > heap[left]) return false; |
| 88 | + if (right <= current && heap[k] > heap[right]) return false; |
| 89 | + return isMinHeap(left) && isMinHeap(right); |
| 90 | + } |
| 91 | + |
| 92 | + public void printHeap() { |
| 93 | + System.out.print("heap: "); |
| 94 | + for (int i = 1; i <= current; i++) { |
| 95 | + System.out.print(heap[i] + " "); |
| 96 | + } |
| 97 | + System.out.println(); |
| 98 | + } |
| 99 | + |
| 100 | + public static void main(String[] args) { |
| 101 | + int[] a = new int[]{5, 3, 1, 0, 6, 7, 2}; |
| 102 | + MinHeap minHeap = new MinHeap(a.length); |
| 103 | + for (int i = 0; i < a.length; i++) { |
| 104 | + minHeap.insert(a[i]); |
| 105 | + } |
| 106 | + |
| 107 | + System.out.println(minHeap.isMinHeap()); |
| 108 | + minHeap.printHeap(); |
| 109 | + System.out.println(minHeap.removeMin()); |
| 110 | + System.out.println(minHeap.removeMin()); |
| 111 | + System.out.println(minHeap.isMinHeap()); |
| 112 | + minHeap.printHeap(); |
| 113 | + System.out.println(minHeap.removeMin()); |
| 114 | + System.out.println(minHeap.removeMin()); |
| 115 | + System.out.println(minHeap.removeMin()); |
| 116 | + minHeap.printHeap(); |
| 117 | + |
| 118 | + } |
| 119 | +} |
0 commit comments