|
| 1 | +package me.ramswaroop.arrays; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.NoSuchElementException; |
| 5 | + |
| 6 | +/** |
| 7 | + * Created by IntelliJ IDEA. |
| 8 | + * |
| 9 | + * @author: ramswaroop |
| 10 | + * @date: 9/12/15 |
| 11 | + * @time: 11:19 PM |
| 12 | + */ |
| 13 | +public class MedianOfStream { |
| 14 | + |
| 15 | + public static int getMedianOfStream(int med, int elem, MaxHeap maxHeap, MinHeap minHeap) { |
| 16 | + switch (compare(maxHeap.getSize(), minHeap.getSize())) { |
| 17 | + case 0: |
| 18 | + if (elem < med) { |
| 19 | + maxHeap.insert(elem); |
| 20 | + med = maxHeap.findMax(); |
| 21 | + } else { |
| 22 | + minHeap.insert(elem); |
| 23 | + med = minHeap.findMin(); |
| 24 | + } |
| 25 | + break; |
| 26 | + case 1: |
| 27 | + if (elem < med) { |
| 28 | + minHeap.insert(maxHeap.deleteMax()); |
| 29 | + maxHeap.insert(elem); |
| 30 | + } else { |
| 31 | + minHeap.insert(elem); |
| 32 | + } |
| 33 | + med = (maxHeap.findMax() + minHeap.findMin())/2; |
| 34 | + break; |
| 35 | + case -1: |
| 36 | + if (elem < med) { |
| 37 | + maxHeap.insert(elem); |
| 38 | + } else { |
| 39 | + maxHeap.insert(minHeap.deleteMin()); |
| 40 | + minHeap.insert(elem); |
| 41 | + } |
| 42 | + med = (maxHeap.findMax() + minHeap.findMin())/2; |
| 43 | + break; |
| 44 | + } |
| 45 | + return med; |
| 46 | + } |
| 47 | + |
| 48 | + static void printMedianOfStream(int[] a) { |
| 49 | + int m = 0; |
| 50 | + MaxHeap maxHeap = new MaxHeap(128); |
| 51 | + MinHeap minHeap = new MinHeap(128); |
| 52 | + for (int i = 0; i < a.length; i++) { |
| 53 | + m = getMedianOfStream(m, a[i], maxHeap, minHeap); |
| 54 | + } |
| 55 | + System.out.println(m); |
| 56 | + } |
| 57 | + |
| 58 | + static int compare(int a, int b) { |
| 59 | + if (a == b) { |
| 60 | + return 0; |
| 61 | + } else { |
| 62 | + return a < b ? -1 : 1; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String a[]) { |
| 67 | + printMedianOfStream(new int[]{5, 15, 1, 3, 2, 8, 7, 9, 10, 6, 11, 4}); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +class MinHeap { |
| 72 | + /** |
| 73 | + * The number of children each node has * |
| 74 | + */ |
| 75 | + private static final int d = 2; |
| 76 | + private int size; |
| 77 | + private int[] heap; |
| 78 | + |
| 79 | + /** |
| 80 | + * Constructor * |
| 81 | + */ |
| 82 | + public MinHeap(int capacity) { |
| 83 | + size = 0; |
| 84 | + heap = new int[capacity + 1]; |
| 85 | + Arrays.fill(heap, -1); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Function to check if heap is empty * |
| 90 | + */ |
| 91 | + public boolean isEmpty() { |
| 92 | + return size == 0; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Check if heap is full * |
| 97 | + */ |
| 98 | + public boolean isFull() { |
| 99 | + return size == heap.length; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Clear heap |
| 104 | + */ |
| 105 | + public void makeEmpty() { |
| 106 | + size = 0; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Function to get index parent of i * |
| 111 | + */ |
| 112 | + private int parent(int i) { |
| 113 | + return (i - 1) / d; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Function to get index of k th child of i * |
| 118 | + */ |
| 119 | + private int kthChild(int i, int k) { |
| 120 | + return d * i + k; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Function to insert element |
| 125 | + */ |
| 126 | + public void insert(int x) { |
| 127 | + if (isFull()) |
| 128 | + throw new NoSuchElementException("Overflow Exception"); |
| 129 | + /** Percolate up **/ |
| 130 | + heap[size++] = x; |
| 131 | + heapifyUp(size - 1); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Function to find least element * |
| 136 | + */ |
| 137 | + public int findMin() { |
| 138 | + if (isEmpty()) |
| 139 | + throw new NoSuchElementException("Underflow Exception"); |
| 140 | + return heap[0]; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Function to delete min element * |
| 145 | + */ |
| 146 | + public int deleteMin() { |
| 147 | + int keyItem = heap[0]; |
| 148 | + delete(0); |
| 149 | + return keyItem; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Function to delete element at an index * |
| 154 | + */ |
| 155 | + public int delete(int ind) { |
| 156 | + if (isEmpty()) |
| 157 | + throw new NoSuchElementException("Underflow Exception"); |
| 158 | + int keyItem = heap[ind]; |
| 159 | + heap[ind] = heap[size - 1]; |
| 160 | + size--; |
| 161 | + heapifyDown(ind); |
| 162 | + return keyItem; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Function heapifyUp * |
| 167 | + */ |
| 168 | + private void heapifyUp(int childInd) { |
| 169 | + int tmp = heap[childInd]; |
| 170 | + while (childInd > 0 && tmp < heap[parent(childInd)]) { |
| 171 | + heap[childInd] = heap[parent(childInd)]; |
| 172 | + childInd = parent(childInd); |
| 173 | + } |
| 174 | + heap[childInd] = tmp; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Function heapifyDown * |
| 179 | + */ |
| 180 | + private void heapifyDown(int ind) { |
| 181 | + int child; |
| 182 | + int tmp = heap[ind]; |
| 183 | + while (kthChild(ind, 1) < size) { |
| 184 | + child = minChild(ind); |
| 185 | + if (heap[child] < tmp) |
| 186 | + heap[ind] = heap[child]; |
| 187 | + else |
| 188 | + break; |
| 189 | + ind = child; |
| 190 | + } |
| 191 | + heap[ind] = tmp; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Function to get smallest child * |
| 196 | + */ |
| 197 | + private int minChild(int ind) { |
| 198 | + int bestChild = kthChild(ind, 1); |
| 199 | + int k = 2; |
| 200 | + int pos = kthChild(ind, k); |
| 201 | + while ((k <= d) && (pos < size)) { |
| 202 | + if (heap[pos] < heap[bestChild]) |
| 203 | + bestChild = pos; |
| 204 | + pos = kthChild(ind, k++); |
| 205 | + } |
| 206 | + return bestChild; |
| 207 | + } |
| 208 | + |
| 209 | + public int getSize() { |
| 210 | + return size; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Function to print heap * |
| 215 | + */ |
| 216 | + public void printHeap() { |
| 217 | + System.out.print("\nHeap = "); |
| 218 | + for (int i = 0; i < size; i++) |
| 219 | + System.out.print(heap[i] + " "); |
| 220 | + System.out.println(); |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +class MaxHeap { |
| 225 | + private int[] heap; |
| 226 | + private int size; |
| 227 | + private int maxsize; |
| 228 | + |
| 229 | + private static final int FRONT = 1; |
| 230 | + |
| 231 | + public MaxHeap(int maxsize) { |
| 232 | + this.maxsize = maxsize; |
| 233 | + this.size = 0; |
| 234 | + heap = new int[this.maxsize + 1]; |
| 235 | + heap[0] = Integer.MAX_VALUE; |
| 236 | + } |
| 237 | + |
| 238 | + public int findMax() { |
| 239 | + return heap[0]; |
| 240 | + } |
| 241 | + |
| 242 | + public int parent(int pos) { |
| 243 | + return pos / 2; |
| 244 | + } |
| 245 | + |
| 246 | + private int leftChild(int pos) { |
| 247 | + return (2 * pos); |
| 248 | + } |
| 249 | + |
| 250 | + private int rightChild(int pos) { |
| 251 | + return (2 * pos) + 1; |
| 252 | + } |
| 253 | + |
| 254 | + private boolean isLeaf(int pos) { |
| 255 | + if (pos >= (size / 2) && pos <= size) { |
| 256 | + return true; |
| 257 | + } |
| 258 | + return false; |
| 259 | + } |
| 260 | + |
| 261 | + private void swap(int fpos, int spos) { |
| 262 | + int tmp; |
| 263 | + tmp = heap[fpos]; |
| 264 | + heap[fpos] = heap[spos]; |
| 265 | + heap[spos] = tmp; |
| 266 | + } |
| 267 | + |
| 268 | + private void maxHeapify(int pos) { |
| 269 | + if (!isLeaf(pos)) { |
| 270 | + if (heap[pos] < heap[leftChild(pos)] || heap[pos] < heap[rightChild(pos)]) { |
| 271 | + if (heap[leftChild(pos)] > heap[rightChild(pos)]) { |
| 272 | + swap(pos, leftChild(pos)); |
| 273 | + maxHeapify(leftChild(pos)); |
| 274 | + } else { |
| 275 | + swap(pos, rightChild(pos)); |
| 276 | + maxHeapify(rightChild(pos)); |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + public void insert(int element) { |
| 283 | + heap[++size] = element; |
| 284 | + int current = size; |
| 285 | + |
| 286 | + while (heap[current] > heap[parent(current)]) { |
| 287 | + swap(current, parent(current)); |
| 288 | + current = parent(current); |
| 289 | + } |
| 290 | + } |
| 291 | + |
| 292 | + public int getSize() { |
| 293 | + return size; |
| 294 | + } |
| 295 | + |
| 296 | + public void print() { |
| 297 | + for (int i = 1; i <= size / 2; i++) { |
| 298 | + System.out.print(" PARENT : " + heap[i] + " LEFT CHILD : " + heap[2 * i] |
| 299 | + + " RIGHT CHILD :" + heap[2 * i + 1]); |
| 300 | + System.out.println(); |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + public void maxHeap() { |
| 305 | + for (int pos = (size / 2); pos >= 1; pos--) { |
| 306 | + maxHeapify(pos); |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + public int deleteMax() { |
| 311 | + int popped = heap[FRONT]; |
| 312 | + heap[FRONT] = heap[size--]; |
| 313 | + maxHeapify(FRONT); |
| 314 | + return popped; |
| 315 | + } |
| 316 | +} |
0 commit comments