|
| 1 | +package me.ramswaroop.common; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by IntelliJ IDEA. |
| 7 | + * <p/> |
| 8 | + * A HEAP is a specialized tree-based ABSTRACT DATA TYPE that satisfies the heap property: |
| 9 | + * min-heap: All non-leaf elements are either smaller than or equal to their left and right child. |
| 10 | + * max-heap: All non-leaf elements are either greater than or equal to their left and right child. |
| 11 | + * <p/> |
| 12 | + * Often implemented as an array, where the children of the element at index i are at index |
| 13 | + * 2i+1 (left child) and 2i+2 (right child). |
| 14 | + * <p/> |
| 15 | + * The first element (minimum or maximum, depending on chosen order) can be found in O(1). |
| 16 | + * Each successor can be found in O(log n). The algorithm in minHeapify() takes O(log n) time |
| 17 | + * Therefore, buildMinHeap() would take O(n log n) time BUT IF OBSERVED CAREFULLY IT TAKES 0(N) TIME. |
| 18 | + * <p/> |
| 19 | + * Used in the HeapSort algorithm. Also can be used to implement a PriorityQueue. |
| 20 | + * |
| 21 | + * @author: ramswaroop |
| 22 | + * @date: 8/2/15 |
| 23 | + * @time: 11:57 AM |
| 24 | + */ |
| 25 | +public class MinHeap { |
| 26 | + |
| 27 | + /** |
| 28 | + * Makes the array {@param a} satisfy the min heap property starting from |
| 29 | + * {@param index} till the end of array. |
| 30 | + * <p/> |
| 31 | + * Time complexity: O(log n). |
| 32 | + * |
| 33 | + * @param a |
| 34 | + * @param index |
| 35 | + */ |
| 36 | + public static void minHeapify(int[] a, int index) { |
| 37 | + int smallest = index; |
| 38 | + int leftIndex = 2 * index + 1; |
| 39 | + int rightIndex = 2 * index + 2; |
| 40 | + |
| 41 | + if (leftIndex < a.length && a[index] > a[leftIndex]) { |
| 42 | + smallest = leftIndex; |
| 43 | + } |
| 44 | + if (rightIndex < a.length && a[smallest] > a[rightIndex]) { |
| 45 | + smallest = rightIndex; |
| 46 | + } |
| 47 | + |
| 48 | + if (smallest != index) { |
| 49 | + swap(a, index, smallest); |
| 50 | + minHeapify(a, smallest); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Converts array {@param a} in to a min heap. |
| 56 | + * <p/> |
| 57 | + * Time complexity: O(n) and is not O(n log n). |
| 58 | + * |
| 59 | + * @param a |
| 60 | + */ |
| 61 | + public static void buildMinHeap(int[] a) { |
| 62 | + for (int i = a.length / 2 - 1; i >= 0; i--) { |
| 63 | + minHeapify(a, i); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public static void swap(int[] a, int firstIndex, int secondIndex) { |
| 68 | + a[firstIndex] = a[firstIndex] + a[secondIndex]; |
| 69 | + a[secondIndex] = a[firstIndex] - a[secondIndex]; |
| 70 | + a[firstIndex] = a[firstIndex] - a[secondIndex]; |
| 71 | + } |
| 72 | + |
| 73 | + // test cases |
| 74 | + public static void main(String[] args) { |
| 75 | + int[] a = new int[]{2, 4, 5, 1, 6, 7, 8}; |
| 76 | + System.out.println(Arrays.toString(a)); |
| 77 | + buildMinHeap(a); |
| 78 | + System.out.println(Arrays.toString(a)); |
| 79 | + } |
| 80 | +} |
0 commit comments