|
| 1 | +import java.util.ArrayList; |
| 2 | +//Implemented a custom MinHeap using generics which supports operation to peek ,insert and remove |
| 3 | +public class CustomMinHeap <T extends Comparable<T>> { |
| 4 | + |
| 5 | + ArrayList<T> list; |
| 6 | + //constructor |
| 7 | + public CustomMinHeap(){ |
| 8 | + list = new ArrayList<>(); |
| 9 | + } |
| 10 | + //function to insert into min heap |
| 11 | + public void insert(T val){ |
| 12 | + list.add(val); |
| 13 | + upHeapify(list.size()-1); |
| 14 | + } |
| 15 | + //function to get the index of the parent |
| 16 | + public int getParentIndex(int index){ |
| 17 | + return (index -1)/2; |
| 18 | + } |
| 19 | + //function to get the index of the left child |
| 20 | + public int getLeftChildIndex(int index){ |
| 21 | + return 2*index + 1; |
| 22 | + } |
| 23 | + //function to return no of elements in the heap |
| 24 | + public int size(){ |
| 25 | + return list.size(); |
| 26 | + } |
| 27 | + //function to upheapify |
| 28 | + public void upHeapify(int index){ |
| 29 | + if(index==0){ |
| 30 | + return; |
| 31 | + } |
| 32 | + //getting parent index |
| 33 | + int parentindex = getParentIndex(index); |
| 34 | + //if the parent value is greater than current node value swap them since parent has to be minimum |
| 35 | + if(list.get(parentindex).compareTo(list.get(index)) > 0){ |
| 36 | + T temp = list.get(parentindex); |
| 37 | + list.set(parentindex,list.get(index)); |
| 38 | + list.set(index,temp); |
| 39 | + upHeapify(parentindex); |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + //function to remove the element from the heap |
| 45 | + public T remove(){ |
| 46 | + if(list.size()==0){return null;} |
| 47 | + if(list.size()==1){return list.remove(0);} |
| 48 | + T val = list.get(0); |
| 49 | + T temp = val; |
| 50 | + //swap root elment with the last element |
| 51 | + list.set(0,list.get(list.size()-1)); |
| 52 | + list.set(list.size()-1,temp); |
| 53 | + //remove the last element |
| 54 | + list.remove(list.size()-1); |
| 55 | + downHeapify(0); |
| 56 | + return val; |
| 57 | + } |
| 58 | + //function to downheapify |
| 59 | + public void downHeapify(int index){ |
| 60 | + int minimum = index; |
| 61 | + //getting left child index |
| 62 | + int left =getLeftChildIndex(index) ; |
| 63 | + //getting right child index |
| 64 | + int right =getRightChildIndex(index); |
| 65 | + //if left child value is smaller than current minimum index value set minimum to left child index |
| 66 | + if(left < list.size() && list.get(left).compareTo(list.get(minimum) ) < 0){ |
| 67 | + minimum = left ; |
| 68 | + } |
| 69 | + //if right child value is smaller than current minimum index value set minimum to right child index |
| 70 | + if(right< list.size() && list.get(right).compareTo(list.get(minimum) ) < 0){ |
| 71 | + minimum = right; |
| 72 | + } |
| 73 | + //swap minimum index value to current index value if minimum !=index |
| 74 | + if(minimum !=index){ |
| 75 | + T temp = list.get(minimum); |
| 76 | + list.set(minimum,list.get(index)); |
| 77 | + list.set(index,temp); |
| 78 | + downHeapify(minimum); |
| 79 | + } |
| 80 | + } |
| 81 | + //function to get the index of the right child |
| 82 | + public int getRightChildIndex(int index){ |
| 83 | + return 2*index + 2; |
| 84 | + } |
| 85 | + //function to peek into min heap and get the root |
| 86 | + public T peek() throws Exception { |
| 87 | + if(list.size()==0){ |
| 88 | + throw new Exception("Heap is empty.Cannot peek"); |
| 89 | + } |
| 90 | + return list.get(0); |
| 91 | + } |
| 92 | + @Override |
| 93 | + public String toString() { |
| 94 | + return list.toString(); |
| 95 | + } |
| 96 | + public static void main(String[] args) throws Exception { |
| 97 | + //Initialize the heap |
| 98 | + CustomMinHeap<Integer> minHeap = new CustomMinHeap<>(); |
| 99 | + //inserting into the heap |
| 100 | + minHeap.insert(5); |
| 101 | + minHeap.insert(500); |
| 102 | + minHeap.insert(10); |
| 103 | + minHeap.insert(100); |
| 104 | + minHeap.insert(15); |
| 105 | + minHeap.insert(20); |
| 106 | + //display the heap |
| 107 | + System.out.println(minHeap); |
| 108 | + //get the element at the top |
| 109 | + System.out.println(minHeap.peek()); |
| 110 | + //remove the min element and print it |
| 111 | + System.out.println(minHeap.remove()); |
| 112 | + //display the heap after removing min element |
| 113 | + System.out.println(minHeap); |
| 114 | + //display the size of the heap |
| 115 | + System.out.println(minHeap.size()); |
| 116 | + |
| 117 | + } |
| 118 | +} |
0 commit comments