Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions MaxPriorityQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package priorityQueues;
import java.util.*;

public class MaxPriorityQueue {

ArrayList<Integer> heap;
MaxPriorityQueue(){
heap=new ArrayList<>();
}

int getSize(){
return heap.size();
}

boolean isEmpty(){
return (heap.size()==0);
}

int getMax(){
if(isEmpty()){
return Integer.MIN_VALUE;
}
return heap.get(0);
}

void insert(int element){
heap.add(element);
int childIndex = heap.size() - 1;
int parentIndex = (childIndex - 1) / 2;

while(childIndex > 0){
if(heap.get(childIndex) > heap.get(parentIndex)){
int temp = heap.get(childIndex);
heap.set(childIndex, heap.get(parentIndex));
heap.set(parentIndex, temp);
childIndex = parentIndex;
parentIndex = (childIndex - 1) / 2;
}else{
return;
}
}
}

int removeMax(){
int maxValue=heap.get(0);
int lastIndex=heap.size()-1;
heap.set(0,heap.get(lastIndex));
heap.remove(heap.size()-1);

//down-Heapify
int index=0;
int maxIndex=index;
int leftchildIndex=1;
int rightchildIndex=2;
while(leftchildIndex<heap.size()){

if(heap.get(leftchildIndex)>heap.get(maxIndex)){
maxIndex=leftchildIndex;
}
if(rightchildIndex<heap.size() && heap.get(rightchildIndex)>heap.get(maxIndex)){
maxIndex=rightchildIndex;
}
if(maxIndex==index){
break;
}
int temp=heap.get(index);
heap.set(index,heap.get(maxIndex));
heap.set(maxIndex,temp);
index=maxIndex;
leftchildIndex=2*index+1;
rightchildIndex=2*index+2;

}

return maxValue;
}
}
81 changes: 81 additions & 0 deletions MinPriorityQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package priorityQueues;
import java.util.*;

public class MinPriorityQueue {

private ArrayList<Integer> heap;

public MinPriorityQueue() {
heap = new ArrayList<Integer>();
}

boolean isEmpty(){
return heap.size() == 0;
}

int size(){
return heap.size();
}

int getMin() throws PriorityQueueException{
if(isEmpty()){
// Throw an exception
throw new PriorityQueueException();
}
return heap.get(0);
}

void insert(int element){
heap.add(element);
int childIndex = heap.size() - 1;
int parentIndex = (childIndex - 1) / 2;

//up-heapify
while(childIndex > 0){
if(heap.get(childIndex) < heap.get(parentIndex)){
int temp = heap.get(childIndex);
heap.set(childIndex, heap.get(parentIndex));
heap.set(parentIndex, temp);
childIndex = parentIndex;
parentIndex = (childIndex - 1) / 2;
}else{
return;
}
}
}

int removeMin() {
int minValue=heap.get(0);
int lastIndex=heap.size()-1;
heap.set(0,heap.get(lastIndex));
heap.remove(heap.size()-1);

//down-Heapify
int index=0;
int minIndex=index;
int leftchildIndex=1;
int rightchildIndex=2;
while(leftchildIndex<heap.size()){

if(heap.get(leftchildIndex)<heap.get(minIndex)){
minIndex=leftchildIndex;
}
if(rightchildIndex<heap.size() && heap.get(rightchildIndex)<heap.get(minIndex)){
minIndex=rightchildIndex;
}
if(minIndex==index){
break;
}
int temp=heap.get(index);
heap.set(index,heap.get(minIndex));
heap.set(minIndex,temp);
index=minIndex;
leftchildIndex=2*index+1;
rightchildIndex=2*index+2;

}

return minValue;
}

}
10 changes: 10 additions & 0 deletions PriorityQueueException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package priorityQueues;

public class PriorityQueueException extends Exception{

/**
*
*/
private static final long serialVersionUID = 1L;

}
31 changes: 31 additions & 0 deletions PriorityQueueUse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package priorityQueues;

public class PriorityQueueUse {

public static void main(String[] args) {
// TODO Auto-generated method stub

MaxPriorityQueue maxpq=new MaxPriorityQueue();
int arr[]= {5,6,9,10,83,65,1};
for(int i=0;i<arr.length;i++) {
maxpq.insert(arr[i]);
}

while(!maxpq.isEmpty()) {
System.out.print(maxpq.removeMax()+" ");
}

System.out.println();

MinPriorityQueue minpq=new MinPriorityQueue();
int a[]= {5,6,9,10,83,65,1};
for(int i=0;i<a.length;i++) {
minpq.insert(a[i]);
}

while(!minpq.isEmpty()) {
System.out.print(minpq.removeMin()+" ");
}
}

}