Created
May 27, 2018 06:40
-
-
Save thmain/f7eb987bb01f92154d501f789f1d0e29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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