• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

priority queue doubt

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic