Skip to content

Instantly share code, notes, and snippets.

@thmain
Created May 27, 2018 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/f7eb987bb01f92154d501f789f1d0e29 to your computer and use it in GitHub Desktop.
Save thmain/f7eb987bb01f92154d501f789f1d0e29 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