Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over a PriorityQueue?

for (Event e : pq) 

doesn't iterate in the priority order.

while(!pq.isEmpty()){   Event e = pq.poll(); } 

This works but empties the queue.

like image 493
simpatico Avatar asked Nov 14 '11 22:11

simpatico


People also ask

Can you iterate over a PriorityQueue?

You can't traverse a Priority Queue in that order because of the underlying implementation (I think it's min-heap in Java). It's not a sorted array, so that you can just go from one element to the one with the lesser priority.

How do you peek a PriorityQueue?

PriorityQueue peek() Method in JavaPriorityQueue. peek() method in Java is used to retrieve or fetch the first element of the Queue or the element present at the head of the Queue. The element retrieved does not get deleted or removed from the Queue. Parameters: The method does not take any parameters.

Can we iterate over PriorityQueue C++?

C++ priority_queue does not offer a . begin() pointer (like vector would do) that you can use to iterate over it. If you want to iterate over the priority queue to search for whether it contains a value then maybe create a wrapper priority queue and use a hash set to keep track of what you have in the queue.

Can you iterate through a PriorityQueue in python?

The PriorityQueue is implemented as binary heap, which is implemented using a list (array) in python. To iterate over the queue you need to know the rules about where children are stored in the list.


2 Answers

From the Javadocs

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

There are probably other equivalent mechanisms.

like image 177
Oliver Charlesworth Avatar answered Sep 17 '22 00:09

Oliver Charlesworth


You can't traverse a Priority Queue in that order because of the underlying implementation (I think it's min-heap in Java).

It's not a sorted array, so that you can just go from one element to the one with the lesser priority.

Peeking (read the top element heap in the heap) is constant time O(1) because it looks at the smallest element.

To get the second next one you must dequeue the smallest top element, that's how it works.
Dequeing (re-heapify = O(log n) time) isn't just a matter of taking that element out, the underlying structure rearranges itself in order to bring the element with the least priority first.

Also, to go through the entire priority queue to read all items in the sorted order, it is an O(n log(n)) operation.
So you may as well just grab all the elements in the queue and sort them (also O(n log (n)) )and then you can go through them as you wish. The only disadvantage is that you're holding an extra-copy of the queue.

Nonetheless, if you need to traverse the data in this way a priority queue may not be the right data structure for your needs.

like image 38
Alex Florescu Avatar answered Sep 20 '22 00:09

Alex Florescu