|
| 1 | +import java.util.ArrayList; |
| 2 | + |
| 3 | +//Implemented a custom MaxHeap using generics which supports operation to peek ,insert and remove |
| 4 | + |
| 5 | +public class CustomMaxHeap <T extends Comparable<T>> { |
| 6 | + ArrayList<T> list; |
| 7 | + public CustomMaxHeap(){ |
| 8 | + list = new ArrayList<>(); |
| 9 | + } |
| 10 | + public void insert(T val){ |
| 11 | + list.add(val); |
| 12 | + upHeapify(list.size()-1); |
| 13 | + } |
| 14 | + public T peek() throws Exception { |
| 15 | + if(list.size()==0){ |
| 16 | + throw new Exception("Heap is empty.Cannot peek"); |
| 17 | + |
| 18 | + } |
| 19 | + return list.get(0); |
| 20 | + } |
| 21 | + @Override |
| 22 | + public String toString() { |
| 23 | + return list.toString(); |
| 24 | + } |
| 25 | + public int size(){ |
| 26 | + return list.size(); |
| 27 | + } |
| 28 | + public T remove(){ |
| 29 | + if(list.size()==0){return null;} |
| 30 | + if(list.size()==1){return list.remove(0);} |
| 31 | + T val = list.get(0); |
| 32 | + T temp = val; |
| 33 | + list.set(0,list.get(list.size()-1)); |
| 34 | + list.set(list.size()-1,temp); |
| 35 | + list.remove(list.size()-1); |
| 36 | + downHeapify(0); |
| 37 | + return val; |
| 38 | + } |
| 39 | + public void downHeapify(int index){ |
| 40 | + int maximum = index; |
| 41 | + int left =getLeftChildIndex(index) ; |
| 42 | + int right =getRightChildIndex(index); |
| 43 | + |
| 44 | + if(left < list.size() && list.get(left).compareTo(list.get(maximum) ) > 0){ |
| 45 | + maximum = left ; |
| 46 | + } |
| 47 | + if(right< list.size() && list.get(right).compareTo(list.get(maximum) ) > 0){ |
| 48 | + maximum = right; |
| 49 | + } |
| 50 | + if(maximum !=index){ |
| 51 | + T temp = list.get(maximum); |
| 52 | + list.set(maximum,list.get(index)); |
| 53 | + list.set(index,temp); |
| 54 | + downHeapify(maximum); |
| 55 | + } |
| 56 | + } |
| 57 | + public void upHeapify(int index){ |
| 58 | + if(index==0){ |
| 59 | + return; |
| 60 | + } |
| 61 | + int parentindex = getParentIndex(index); |
| 62 | + if(list.get(parentindex).compareTo(list.get(index)) < 0){ |
| 63 | + T temp = list.get(parentindex); |
| 64 | + list.set(parentindex,list.get(index)); |
| 65 | + list.set(index,temp); |
| 66 | + upHeapify(parentindex); |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + public int getParentIndex(int index){ |
| 71 | + return (index -1)/2; |
| 72 | + } |
| 73 | + public int getLeftChildIndex(int index){ |
| 74 | + return 2*index + 1; |
| 75 | + } |
| 76 | + |
| 77 | + public int getRightChildIndex(int index){ |
| 78 | + return 2*index + 2; |
| 79 | + } |
| 80 | + public static void main(String[] args) throws Exception { |
| 81 | + CustomMaxHeap<Integer> maxHeap = new CustomMaxHeap<>(); |
| 82 | + maxHeap.insert(5); |
| 83 | + maxHeap.insert(500); |
| 84 | + maxHeap.insert(10); |
| 85 | + maxHeap.insert(100); |
| 86 | + maxHeap.insert(15); |
| 87 | + maxHeap.insert(20); |
| 88 | + System.out.println(maxHeap); |
| 89 | + System.out.println(maxHeap.peek()); |
| 90 | + System.out.println(maxHeap.remove()); |
| 91 | + System.out.println(maxHeap); |
| 92 | + System.out.println(maxHeap.size()); |
| 93 | + } |
| 94 | +} |
0 commit comments