Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

priority queue doubt

 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Priority queue




Adding
0
2
1
5
3
4
after polling
0
1
2
3
4
5
why the output for adding is 0 2 1 5 3 4?(what order it is?).
I guess while adding/offering it has to be in sort order.

or poll() does the sort order and removes the highest priority element.
correct me!

Thanks
samura

 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


public PriorityQueue(int initialCapacity,
Comparator<? super E> comparator)

what is the initialCapacity?
The API says it's the initialCapacity for the pq1(reference of
priorityqueue)
priorityQueue pq1 = new PriorityQueue(10,pqs)
what it does here? by giving the value 10..


I am little confused with this


 
Prasanna Rajaperumal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Good Reason to be confused. Priority queue is implemented as Heap data structure. I do not know the exact details of the implementation. But with a min heap structure only one thing is assured, root has the minimum.

So after you poll the order you get 1, 2, 3, 4, 5 is pure coincidence. It could have been in any order with 1 as the root (first element).

Still confusing
1. read about heaps
2. Read how heaps are impemented in a queue
3. Look at priorityqueue fixup method.

0 will be polled out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic