|
| 1 | +package DataStructure; |
| 2 | + |
| 3 | +// https://www.coursera.org/learn/algorithms-part2/lecture/e3UfD/shortest-paths-apis |
| 4 | +// https://github.com/yennanliu/CS_basics/blob/master/data_structure/java/DirectedEdge.java |
| 5 | + |
| 6 | +public class DirectedEdge { |
| 7 | + private final int v, w; |
| 8 | + private final double weight; |
| 9 | + |
| 10 | + public DirectedEdge(int v, int w, double weight) { |
| 11 | + this.v = v; |
| 12 | + this.w = w; |
| 13 | + this.weight = weight; |
| 14 | + } |
| 15 | + |
| 16 | + public int from() { |
| 17 | + return v; |
| 18 | + } |
| 19 | + |
| 20 | + public int to() { |
| 21 | + return w; |
| 22 | + } |
| 23 | + |
| 24 | + public int weight() { |
| 25 | + return (int) weight; |
| 26 | + } |
| 27 | + |
| 28 | + // Implementing "Min Heap" |
| 29 | + public static class MinHeap { |
| 30 | + // Create a complete binary tree using an array |
| 31 | + // Then use the binary tree to construct a Heap |
| 32 | + int[] minHeap; |
| 33 | + // the number of elements is needed when instantiating an array |
| 34 | + // heapSize records the size of the array |
| 35 | + int heapSize; |
| 36 | + // realSize records the number of elements in the Heap |
| 37 | + int realSize = 0; |
| 38 | + |
| 39 | + public MinHeap(int heapSize) { |
| 40 | + this.heapSize = heapSize; |
| 41 | + minHeap = new int[heapSize + 1]; |
| 42 | + // To better track the indices of the binary tree, |
| 43 | + // we will not use the 0-th element in the array |
| 44 | + // You can fill it with any value |
| 45 | + minHeap[0] = 0; |
| 46 | + } |
| 47 | + |
| 48 | + // Function to add an element |
| 49 | + public void add(int element) { |
| 50 | + realSize++; |
| 51 | + // If the number of elements in the Heap exceeds the preset heapSize |
| 52 | + // print "Added too many elements" and return |
| 53 | + if (realSize > heapSize) { |
| 54 | + System.out.println("Added too many elements!"); |
| 55 | + realSize--; |
| 56 | + return; |
| 57 | + } |
| 58 | + // Add the element into the array |
| 59 | + minHeap[realSize] = element; |
| 60 | + // Index of the newly added element |
| 61 | + int index = realSize; |
| 62 | + // Parent node of the newly added element |
| 63 | + // Note if we use an array to represent the complete binary tree |
| 64 | + // and store the root node at index 1 |
| 65 | + // index of the parent node of any node is [index of the node / 2] |
| 66 | + // index of the left child node is [index of the node * 2] |
| 67 | + // index of the right child node is [index of the node * 2 + 1] |
| 68 | + int parent = index / 2; |
| 69 | + // If the newly added element is smaller than its parent node, |
| 70 | + // its value will be exchanged with that of the parent node |
| 71 | + while ( minHeap[index] < minHeap[parent] && index > 1 ) { |
| 72 | + int temp = minHeap[index]; |
| 73 | + minHeap[index] = minHeap[parent]; |
| 74 | + minHeap[parent] = temp; |
| 75 | + index = parent; |
| 76 | + parent = index / 2; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Get the top element of the Heap |
| 81 | + public int peek() { |
| 82 | + return minHeap[1]; |
| 83 | + } |
| 84 | + |
| 85 | + // Delete the top element of the Heap |
| 86 | + public int pop() { |
| 87 | + // If the number of elements in the current Heap is 0, |
| 88 | + // print "Don't have any elements" and return a default value |
| 89 | + if (realSize < 1) { |
| 90 | + System.out.println("Don't have any element!"); |
| 91 | + return Integer.MAX_VALUE; |
| 92 | + } else { |
| 93 | + // When there are still elements in the Heap |
| 94 | + // realSize >= 1 |
| 95 | + int removeElement = minHeap[1]; |
| 96 | + // Put the last element in the Heap to the top of Heap |
| 97 | + minHeap[1] = minHeap[realSize]; |
| 98 | + realSize--; |
| 99 | + int index = 1; |
| 100 | + // When the deleted element is not a leaf node |
| 101 | + while (index <= realSize / 2) { |
| 102 | + // the left child of the deleted element |
| 103 | + int left = index * 2; |
| 104 | + // the right child of the deleted element |
| 105 | + int right = (index * 2) + 1; |
| 106 | + // If the deleted element is larger than the left or right child |
| 107 | + // its value needs to be exchanged with the smaller value |
| 108 | + // of the left and right child |
| 109 | + if (minHeap[index] > minHeap[left] || minHeap[index] > minHeap[right]) { |
| 110 | + if (minHeap[left] < minHeap[right]) { |
| 111 | + int temp = minHeap[left]; |
| 112 | + minHeap[left] = minHeap[index]; |
| 113 | + minHeap[index] = temp; |
| 114 | + index = left; |
| 115 | + } else { |
| 116 | + // maxHeap[left] >= maxHeap[right] |
| 117 | + int temp = minHeap[right]; |
| 118 | + minHeap[right] = minHeap[index]; |
| 119 | + minHeap[index] = temp; |
| 120 | + index = right; |
| 121 | + } |
| 122 | + } else { |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + return removeElement; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // return the number of elements in the Heap |
| 131 | + public int size() { |
| 132 | + return realSize; |
| 133 | + } |
| 134 | + |
| 135 | + public String toString() { |
| 136 | + if (realSize == 0) { |
| 137 | + return "No element!"; |
| 138 | + } else { |
| 139 | + StringBuilder sb = new StringBuilder(); |
| 140 | + sb.append('['); |
| 141 | + for (int i = 1; i <= realSize; i++) { |
| 142 | + sb.append(minHeap[i]); |
| 143 | + sb.append(','); |
| 144 | + } |
| 145 | + sb.deleteCharAt(sb.length() - 1); |
| 146 | + sb.append(']'); |
| 147 | + return sb.toString(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public static void main(String[] args) { |
| 152 | + // Test case |
| 153 | + MinHeap minHeap = new MinHeap(3); |
| 154 | + minHeap.add(3); |
| 155 | + minHeap.add(1); |
| 156 | + minHeap.add(2); |
| 157 | + // [1,3,2] |
| 158 | + System.out.println(minHeap.toString()); |
| 159 | + // 1 |
| 160 | + System.out.println(minHeap.peek()); |
| 161 | + // 1 |
| 162 | + System.out.println(minHeap.pop()); |
| 163 | + // [2, 3] |
| 164 | + System.out.println(minHeap.toString()); |
| 165 | + minHeap.add(4); |
| 166 | + // Add too many elements |
| 167 | + minHeap.add(5); |
| 168 | + // [2,3,4] |
| 169 | + System.out.println(minHeap.toString()); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments