Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the index of an item in a PriorityQueue? (Java)

I was wondering if it was possible for me to find the index of a value in a PriorityQueue . Just to see what number it is "in line". Does anyone know?

like image 761
AndrewP Avatar asked May 15 '12 02:05

AndrewP


People also ask

Does priority queue have an index?

Priority queue is a data structure in which data is stored on basis of its priority. In an Indexed Priority Queue, data is stored just like standard priority queue and along with this, the value of a data can be updated using its key.

How do you find the nth element of a queue?

You can use get(x) when you need a specific item.

How do you find the middle element of a priority queue?

If there are odd elements then the middle element is the median but if there are an even number of elements, the smaller element in the median pair will be the one which we give priority to in our median priority queue.

How do I find the queue element?

It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue . Contains(T) Method is used to check whether an element is in the Queue .


2 Answers

There is an index priority queue written by Princeton.

algs4.cs.princeton.edu/24pq/IndexMinPQ.java.html

The key idea is to build two index maps between the item and its position in priority queue.

When you are updating the priority queue, you also need to update those two index maps.

Hope this solves your problem :-)

like image 110
Meng Zhang Avatar answered Oct 25 '22 10:10

Meng Zhang


PriorityQueue doesn't support indexing. You can yourself associate an integer index to each item.

like image 44
Piyush Mattoo Avatar answered Oct 25 '22 09:10

Piyush Mattoo