What's the difference between the priority queue & set & heap ?

P

PicO

i need some explanation about the difference between priority queue &
set & heap ... as they all sort the data in ( n log n ) ... but the
only i see that priority queue only can pop the top ( maximum
element ) while set and heap can erase any element ...
 
P

PicO

i need some explanation about the difference between priority queue &
set & heap ... as they all sort the data in ( n log n ) ... but the
only i see that priority queue only can pop the top ( maximum
element ) while set and heap can erase any element ...

i also read that .. i can make a heap from priority queue ....
how ? ...
 
B

Barry

PicO said:
i need some explanation about the difference between priority queue &
set & heap ... as they all sort the data in ( n log n ) ... but the

as `priority_queue' is a container adapter, which adapt `vector' by
default (means that it uses `vector' as its default container (protected
c) underneath), and `priority_queue' also use `make_heap' and
`push_heap' as implementation to maintain the `c' as a heap.

set always use a Rb-tree to make the elements ordered.

heap, there's no container called heap, just xxxx_heap in the algorithm
only i see that priority queue only can pop the top ( maximum
element ) while set and heap can erase any element ...

no heap container again, also heap can't erase any element,

template <class RandomAccessIterator>
void pop_heap (RandomAccessIterator first, RandomAccessIterator last);

`pop_heap' just rearranges the elements in the range [first,last) in
such a way that the part considered a heap is shortened by one by
removing its highest element.
 
P

PicO

PicO said:
i need some explanation about the difference between priority queue &
set & heap ... as they all sort the data in ( n log n ) ... but the

as `priority_queue' is a container adapter, which adapt `vector' by
default (means that it uses `vector' as its default container (protected
c) underneath), and `priority_queue' also use `make_heap' and
`push_heap' as implementation to maintain the `c' as a heap.

set always use a Rb-tree to make the elements ordered.

heap, there's no container called heap, just xxxx_heap in the algorithm
only i see that priority queue only can pop the top ( maximum
element ) while set and heap can erase any element ...

no heap container again, also heap can't erase any element,

template <class RandomAccessIterator>
void pop_heap (RandomAccessIterator first, RandomAccessIterator last);

`pop_heap' just rearranges the elements in the range [first,last) in
such a way that the part considered a heap is shortened by one by
removing its highest element.

thanks barry , but can i pop_heap the smallest element not the
highest ...
and what the complexity of pop_heap and sort_heap ..
 
B

Barry

PicO said:
PicO said:
i need some explanation about the difference between priority queue &
set & heap ... as they all sort the data in ( n log n ) ... but the
as `priority_queue' is a container adapter, which adapt `vector' by
default (means that it uses `vector' as its default container (protected
c) underneath), and `priority_queue' also use `make_heap' and
`push_heap' as implementation to maintain the `c' as a heap.

set always use a Rb-tree to make the elements ordered.

heap, there's no container called heap, just xxxx_heap in the algorithm
only i see that priority queue only can pop the top ( maximum
element ) while set and heap can erase any element ...
no heap container again, also heap can't erase any element,

template <class RandomAccessIterator>
void pop_heap (RandomAccessIterator first, RandomAccessIterator last);

`pop_heap' just rearranges the elements in the range [first,last) in
such a way that the part considered a heap is shortened by one by
removing its highest element.

thanks barry , but can i pop_heap the smallest element not the
highest ...

it depends on the compare you take, or how you define small/high(est),
which refer to maximum heap and minimum heap separately
and what the complexity of pop_heap and sort_heap ..

pop_heap O(log N)
sort_heap O(N log N)
 
J

Juha Nieminen

PicO said:
i need some explanation about the difference between priority queue &
set & heap ... as they all sort the data in ( n log n ) ... but the
only i see that priority queue only can pop the top ( maximum
element ) while set and heap can erase any element ...

The advantage of a priority queue (in cases where you just need the
smallest item in a group of values) over a set is that the priority
queue will take much less memory. A priority queue can be constructed
into a vector (ie. a contiguous array) and thus it will only require
as much memory as the sizes of the elements. A set will take much more
memory (because besides the elements themselves it needs 3 pointers
per element, plus the additional overhead caused by allocating each
element dynamically).
 
P

PicO

The advantage of a priority queue (in cases where you just need the
smallest item in a group of values) over a set is that the priority
queue will take much less memory. A priority queue can be constructed
into a vector (ie. a contiguous array) and thus it will only require
as much memory as the sizes of the elements. A set will take much more
memory (because besides the elements themselves it needs 3 pointers
per element, plus the additional overhead caused by allocating each
element dynamically).

thanks juha .. that's i want the smallest one so I'll use priority
queue ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top