Skip to content

Instantly share code, notes, and snippets.

@SumitJainUTD
Created May 10, 2015 05:11
Show Gist options
  • Save SumitJainUTD/3bf7c8b8129b46935ae2 to your computer and use it in GitHub Desktop.
Save SumitJainUTD/3bf7c8b8129b46935ae2 to your computer and use it in GitHub Desktop.
import java.util.PriorityQueue;
public class MinHeap_PQ {
PriorityQueue<Integer> pq;
public MinHeap_PQ() {
pq = new PriorityQueue<Integer>();
}
public void insert(int[] x) {
for (int i = 0; i < x.length; i++) {
pq.offer(x[i]);
}
}
public int peek() {
return pq.peek();
}
public int extractMin() {
return pq.poll();
}
public int getSize() {
return pq.size();
}
public void print() {
System.out.println(pq);
}
public static void main(String[] args) {
int[] arrA = { 1, 6, 2, 9, 4, 3, 8 };
MinHeap_PQ i = new MinHeap_PQ();
i.insert(arrA);
i.print();
System.out.println("Min Element in the Priority Queue: "
+ i.extractMin());
System.out.println("Min Element in the Priority Queue: "
+ i.extractMin());
System.out.println("Min Element in the Priority Queue: "
+ i.extractMin());
System.out.println("Priority Queue Size: " + i.getSize());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment