|
优先级队列
0.1
数据结构_第7章
|
Priority queue. More...
#include <BinaryHeap.h>
Public Types | |
| typedef size_t | size_type |
| an unsigned integral type More... | |
| typedef Comparable | value_type |
| Type of the elements. More... | |
| typedef value_type & | reference |
Public Member Functions | |
| BinaryHeap (size_type capacity=100, const Compare &comp=Compare{}) | |
| Construct a new Binary Heap object. More... | |
| BinaryHeap (const List::seqList< value_type > &items, const Compare &comp=Compare{}) | |
| Construct a new Binary Heap object. More... | |
| ~BinaryHeap ()=default | |
| Destroy the Binary Heap object. More... | |
| bool | empty () const |
| 判队空 More... | |
| const_reference | top () const |
| 返回队头元素值 More... | |
| void | pop () |
| 队头元素出队 More... | |
| void | pop (Comparable *minItem) |
| 队头元素出队,保存在minItem指向的空间中 More... | |
| void | push (const_reference x) |
| 将x入队 More... | |
| void | clear () |
| 清空队列 More... | |
| size_type | size () const |
| Return size. More... | |
| void | print_heap (std::ostream &out) const |
| 将二叉堆中元素顺序输出. More... | |
Public Attributes | |
| const typedef value_type & | const_reference |
Protected Member Functions | |
| void | buildHeap () |
| Establish heap order property from an arbitrary arrangement of items. More... | |
| void | percolateDown (size_type hole) |
| Internal method to percolate down in the heap. More... | |
| void | percolateUp (size_type hole) |
| Internal method to percolate up in the heap. More... | |
Protected Attributes | |
| size_type | current_size_ |
| Number of elements in heap. More... | |
| List::seqList< value_type > | array_ |
| The heap array. More... | |
Static Protected Attributes | |
| static constexpr Compare | comp = Compare{} |
| function object of type Compare More... | |
Friends | |
| std::ostream & | operator<< (std::ostream &out, const BinaryHeap &heap) |
| 将二叉堆中元素顺序插到输出流对象out中. More... | |
Priority queue.
Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion.\ This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue).
| Comparable | Type of the elements.\ Aliased as member type priority_queue::value_type. |
| std::less<Comparable> | A binary predicate that takes two elements (of type T) as arguments and returns a bool.\ The expression comp(a,b), where comp is an object of this type and a and b are elements in the container, shall return true if a is considered to go before b in the strict weak ordering the function defines.\ The priority_queue uses this function to maintain the elements sorted in a way that preserves heap properties (i.e., that the element popped is the last according to this strict weak ordering).\ This can be a function pointer or a function object, and defaults to less<T>, which returns the same as applying the less-than operator (a<b).\ 即:comp(a, b)返回true,当且仅当b的优先级高于a. 故若comp为less,则为最大堆. |
Definition at line 40 of file BinaryHeap.h.
| typedef value_type& Queue::BinaryHeap< Comparable, Compare >::reference |
Definition at line 46 of file BinaryHeap.h.
| typedef size_t Queue::BinaryHeap< Comparable, Compare >::size_type |
an unsigned integral type
Definition at line 44 of file BinaryHeap.h.
| typedef Comparable Queue::BinaryHeap< Comparable, Compare >::value_type |
Type of the elements.
Definition at line 45 of file BinaryHeap.h.
|
explicit |
Construct a new Binary Heap object.
| capacity | 容器的初始容量. |
| comp | 函数对象(二元谓词) |
数据范围array[1] ~ array[capacity]
Definition at line 203 of file BinaryHeap.h.
|
explicit |
Construct a new Binary Heap object.
| items | 容器的初值来源 |
| comp | 函数对象(二元谓词) |
Definition at line 237 of file BinaryHeap.h.
|
default |
Destroy the Binary Heap object.
|
protected |
Establish heap order property from an arbitrary arrangement of items.
Definition at line 249 of file BinaryHeap.h.
| void Queue::BinaryHeap< Comparable, Compare >::clear |
清空队列
Definition at line 315 of file BinaryHeap.h.
| bool Queue::BinaryHeap< Comparable, Compare >::empty |
|
protected |
Internal method to percolate down in the heap.
| hole | the index at which the percolate begins. |
Definition at line 258 of file BinaryHeap.h.
|
protected |
Internal method to percolate up in the heap.
| hole | the index at which the percolate begins. |
Definition at line 283 of file BinaryHeap.h.
| void Queue::BinaryHeap< Comparable, Compare >::pop |
队头元素出队
Definition at line 298 of file BinaryHeap.h.
| void Queue::BinaryHeap< Comparable, Compare >::pop | ( | Comparable * | minItem | ) |
|
inline |
将二叉堆中元素顺序输出.
| out | 输出流对象,可以是std::cout,或fout等. |
Definition at line 181 of file BinaryHeap.h.
Referenced by main().
| void Queue::BinaryHeap< Comparable, Compare >::push | ( | const_reference | x | ) |
| BinaryHeap< Comparable, Compare >::size_type Queue::BinaryHeap< Comparable, Compare >::size |
Return size.
Returns the number of elements in the priority_queue.\ This member function effectively calls member size of the underlying container object.
Definition at line 322 of file BinaryHeap.h.
Referenced by main().
| BinaryHeap< Comparable, Compare >::const_reference Queue::BinaryHeap< Comparable, Compare >::top |
返回队头元素值
Definition at line 215 of file BinaryHeap.h.
Referenced by main().
|
friend |
将二叉堆中元素顺序插到输出流对象out中.
| out | 输出流对象,可以是std::cout,或fout等. |
| heap | 二叉堆对象 |
Definition at line 195 of file BinaryHeap.h.
|
protected |
The heap array.
数据范围[1, current_size]
Definition at line 62 of file BinaryHeap.h.
Referenced by Queue::BinaryHeap< int, std::greater< int > >::print_heap().
|
staticconstexprprotected |
function object of type Compare
A binary predicate that takes two elements (of type T) as arguments and returns a bool.\ The expression comp(a,b), where comp is an object of this type and a and b are elements in the container, shall return true if a is considered to go before b in the strict weak ordering the function defines.\ The priority_queue uses this function to maintain the elements sorted in a way that preserves heap properties (i.e., that the element popped is the last according to this strict weak ordering).\ This can be a function pointer or a function object, and defaults to less<T>, which returns the same as applying the less-than operator (a<b).
Definition at line 73 of file BinaryHeap.h.
| const typedef value_type& Queue::BinaryHeap< Comparable, Compare >::const_reference |
Definition at line 47 of file BinaryHeap.h.
|
protected |
Number of elements in heap.
Definition at line 55 of file BinaryHeap.h.
Referenced by Queue::BinaryHeap< int, std::greater< int > >::print_heap().