Heap: PriorityQueue

Java: The default PriorityQueue is implemented with Min-Heap, that is the top element is the minimum one in the heap.
poll() Retrieves and removes the head

C++: std::priority_queue default: max heap,
java/python: priorityqueue: min-heap

min-heap:用来contains所有排出小到大,或过滤过k个最大
max-heap:用来contains所有排出大到小,或过滤过k个最小

Java 用priorityqueue实现的话:
int
(1)min-heap:
//PriorityQueue pq = new PriorityQueue<>((x, y) -> x - y);
PriorityQueue pq = new PriorityQueue<>();
(2)max-heap:
PriorityQueue pq = new PriorityQueue<>((x, y) -> y - x);

node
(1)min-heap:
PriorityQueue queue = new PriorityQueue<>((x, y) -> x.val - y.val);
(2)max-heap:
PriorityQueue queue = new PriorityQueue<>((x, y) -> y.val - x.val);

pq.offer(val);
pq.poll();
pq.peek();
pq.size();

Example:
http://www.jianshu.com/p/cbeeddb3cd1a

你可能感兴趣的:(Heap: PriorityQueue)