How does Java's PriorityQueue differ from a min-heap?
The default PriorityQueue is implemented with Min-Heap, that is the top element is the minimum one in the heap.
In order to implement a max-heap, you can create your own Comparator:
import java.util.Comparator;
public class MyComparator implements Comparator<Integer>
{
public int compare( Integer x, Integer y )
{
return y - x;
}
}
So, you can create a min-heap and max-heap in the following way:
PriorityQueue minHeap=new PriorityQueue();
PriorityQueue maxHeap=new PriorityQueue(size, new MyComparator());
For max-heap you can use:
PriorityQueue<Integer> queue = new PriorityQueue<>(10, Collections.reverseOrder());
Add() works like an insertWithPriority.
You can define priority for the type that you want using the constructor:
PriorityQueue(int, java.util.Comparator)
look under https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/PriorityQueue.html
The order the Comparator gives will represent the priority in the queue.