|
| 1 | +/* |
| 2 | +Implement the class for Max Priority Queue which includes following functions - |
| 3 | +1. getSize - |
| 4 | +Return the size of priority queue i.e. number of elements present in the priority queue. |
| 5 | +2. isEmpty - |
| 6 | +Check if priority queue is empty or not. Return true or false accordingly. |
| 7 | +3. insert - |
| 8 | +Given an element, insert that element in the priority queue at the correct position. |
| 9 | +4. getMax - |
| 10 | +Return the maximum element present in the priority queue without deleting. Return -Infinity if priority queue is empty. |
| 11 | +5. removeMax - |
| 12 | +Delete and return the maximum element present in the priority queue. Return -Infinity if priority queue is empty. |
| 13 | +*/ |
| 14 | + |
| 15 | +import java.util.*; |
| 16 | + |
| 17 | +public class PQ { |
| 18 | + // Complete this class |
| 19 | + private ArrayList<Integer> heap = new ArrayList<Integer>(); |
| 20 | + boolean isEmpty() { |
| 21 | + // Implement the isEmpty() function here |
| 22 | + return heap.isEmpty(); |
| 23 | + } |
| 24 | + |
| 25 | + int getSize() { |
| 26 | + // Implement the getSize() function here |
| 27 | + return heap.size(); |
| 28 | + } |
| 29 | + |
| 30 | + int getMax() { |
| 31 | + // Implement the getMax() function here |
| 32 | + if(heap.isEmpty()) |
| 33 | + return Integer.MIN_VALUE; |
| 34 | + return heap.get(0); |
| 35 | + } |
| 36 | + |
| 37 | + void insert(int element) { |
| 38 | + // Implement the insert() function here |
| 39 | + //Insert new element at the end of the arraylist |
| 40 | + heap.add(element); |
| 41 | + int childIndex=heap.size()-1; |
| 42 | + int parentIndex=(childIndex-1)/2; |
| 43 | + |
| 44 | + while(parentIndex>=0) |
| 45 | + { |
| 46 | + //Check if parent has lesser priority than the child. If it does, swap them |
| 47 | + if(heap.get(childIndex)>heap.get(parentIndex)) |
| 48 | + { |
| 49 | + int temp=heap.get(childIndex); |
| 50 | + heap.set(childIndex, heap.get(parentIndex)); |
| 51 | + heap.set(parentIndex,temp); |
| 52 | + |
| 53 | + childIndex=parentIndex; |
| 54 | + parentIndex=(childIndex-1)/2; |
| 55 | + |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + return; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + int removeMax() { |
| 66 | + // Implement the removeMax() function here |
| 67 | + if(heap.isEmpty()) |
| 68 | + return Integer.MIN_VALUE; |
| 69 | + |
| 70 | + //Remove the Max element and store it |
| 71 | + int maxEle=heap.get(0); |
| 72 | + heap.set(0,heap.get(heap.size()-1)); |
| 73 | + heap.remove(heap.size()-1); |
| 74 | + |
| 75 | + //Start reinstating max. heap property from root to leaf |
| 76 | + int parentIndex=0; |
| 77 | + int leftChildIndex=2*parentIndex+1, rightChildIndex=2*parentIndex+2; |
| 78 | + |
| 79 | + while(leftChildIndex<heap.size()) |
| 80 | + { |
| 81 | + int maxIndex=parentIndex; |
| 82 | + if(heap.get(maxIndex)<heap.get(leftChildIndex)) |
| 83 | + { |
| 84 | + maxIndex=leftChildIndex; |
| 85 | + } |
| 86 | + |
| 87 | + if(rightChildIndex<heap.size()) |
| 88 | + { |
| 89 | + if(heap.get(maxIndex)<heap.get(rightChildIndex)) |
| 90 | + { |
| 91 | + maxIndex=rightChildIndex; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if(maxIndex==parentIndex) |
| 96 | + { |
| 97 | + return maxEle; |
| 98 | + } |
| 99 | + |
| 100 | + int temp=heap.get(maxIndex); |
| 101 | + heap.set(maxIndex,heap.get(parentIndex)); |
| 102 | + heap.set(parentIndex,temp); |
| 103 | + |
| 104 | + parentIndex=maxIndex; |
| 105 | + leftChildIndex=2*parentIndex+1; |
| 106 | + rightChildIndex=2*parentIndex+2; |
| 107 | + } |
| 108 | + |
| 109 | + return maxEle; |
| 110 | + } |
| 111 | +} |
0 commit comments