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?
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.
You can use get(x) when you need a specific item.
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.
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 .
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 :-)
PriorityQueue doesn't support indexing. You can yourself associate an integer index to each item.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With