a priority queue on a heap with pointers, which keeps a pointer to the last leaf if L
More...
|
| | LinkedHeapPriorityQueue (void)=default |
| | the empty queue
|
| | LinkedHeapPriorityQueue (const LinkedHeapPriorityQueue &)=delete |
| | no copy: the nodes belong to the queue
|
| LinkedHeapPriorityQueue & | operator= (const LinkedHeapPriorityQueue &)=delete |
| | no copy: the nodes belong to the queue
|
| | LinkedHeapPriorityQueue (LinkedHeapPriorityQueue &&other) noexcept |
| | the items of other, which is left empty
|
| LinkedHeapPriorityQueue & | operator= (LinkedHeapPriorityQueue &&other) noexcept |
| | the items of other, which is left empty, in place of these
|
| | ~LinkedHeapPriorityQueue () |
| | the queue and its nodes destroyed
|
| bool | empty (void) const |
| | whether the queue has no item
|
| int | size (void) const |
| | the number of items, \( n \)
|
| const T & | min (void) const |
| | the item of smallest priority, at the root
|
| const P & | minPriority (void) const |
| | the smallest priority
|
| void | insert (const T &x, const P &p) |
| | inserts \( x \) with the priority \( p \), a new last leaf moved up while smaller than its parent, in \( O( \log n ) \) time
|
| T | deleteMin (void) |
| | removes the item of smallest priority and returns it, the last leaf moved into the root and down while larger than a child, in \( O( \log n ) \) time
|
| const_iterator | begin (void) const |
| | the first item of the view
|
| const_iterator | end (void) const |
| | the end of the view
|
| const_iterator | cbegin (void) const |
| | the same as begin()
|
| const_iterator | cend (void) const |
| | the same as end()
|
template<class T, std::totally_ordered P = int, bool L = false>
class ADS::LinkedHeapPriorityQueue< T, P, L >
a priority queue on a heap with pointers, which keeps a pointer to the last leaf if L
template<class T, std::totally_ordered P = int, bool L = false>
removes the item of smallest priority and returns it, the last leaf moved into the root and down while larger than a child, in \( O( \log n ) \) time
In pseudocode (the heap of section 10.1.3 on pointers, beyond the book), \( \mathit{last} \) found as by insert(), or by the walk symmetric to \( \mathrm{nextParent}() \) if L:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{deleteMin}() \cr
2 & \quad x \gets \mathit{root}.\mathit{value} \cr
3 & \quad \textbf{if}\ n = 1\ \textbf{then}\ \mathit{root} \gets
\textbf{nil};\ n \gets 0;\ \textbf{return}\ x \cr
4 & \quad \ell \gets \mathit{last}\ \text{if } L \text{, else }
\mathrm{at}( n ) \cr
5 & \quad \text{move the contents of } \ell \text{ to }
\mathit{root};\ \text{detach } \ell \text{ from its parent};
\ n \gets n - 1 \cr
6 & \quad v \gets \mathit{root} \cr
7 & \quad \textbf{loop} \cr
8 & \qquad m \gets \text{the child of } v \text{ of least priority,}
\text{ if smaller than } v \text{, else } v \cr
9 & \qquad \textbf{if}\ m = v\ \textbf{then return}\ x \cr
10 & \qquad \text{swap the contents of } v \text{ and } m;\ v \gets m
\end{array}}
\]
- Precondition
- the queue is not empty
- Postcondition
- one item less, none of smaller priority than the one removed
template<class T, std::totally_ordered P = int, bool L = false>
inserts \( x \) with the priority \( p \), a new last leaf moved up while smaller than its parent, in \( O( \log n ) \) time
In pseudocode (the heap of section 10.1.3 on pointers, beyond the book), \( \mathrm{at}( k ) \) the node \( k \) of the levels, by the bits of \( k \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{insert}( x , p ) \cr
2 & \quad n \gets n + 1 \cr
3 & \quad \textbf{if}\ n = 1\ \textbf{then}\ \mathit{root} \gets
\text{a node } ( p , x );\ \textbf{return} \cr
4 & \quad u \gets \mathrm{nextParent}()\ \text{if } L \text{, else }
\mathrm{at}( \lfloor n / 2 \rfloor ) \cr
5 & \quad v \gets \text{a node } ( p , x ) \text{ as the left child of }
u \text{, or the right one if the left is taken} \cr
6 & \quad \textbf{while}\ v \ne \mathit{root}\ \textbf{and}
\ v.\mathit{priority} < v.\mathit{parent}.\mathit{priority}
\ \textbf{do} \cr
7 & \qquad \text{swap the contents of } v \text{ and }
v.\mathit{parent};\ v \gets v.\mathit{parent} \cr
8 & \textbf{function}\ \mathrm{at}( k ) \cr
9 & \quad u \gets \mathit{root} \cr
10 & \quad \textbf{foreach}\ \text{bit } b \text{ of } k
\text{ after the leading one, from the highest}\ \textbf{do} \cr
11 & \qquad u \gets u.\mathit{left}\ \text{if } b = 0 \text{, else }
u.\mathit{right} \cr
12 & \quad \textbf{return}\ u \cr
13 & \textbf{function}\ \mathrm{nextParent}()\quad \triangleright
\ \text{the node whose child the next leaf is} \cr
14 & \quad u \gets \mathit{last} \cr
15 & \quad \textbf{if}\ u = \mathit{root}\ \textbf{then return}\ u \cr
16 & \quad \textbf{if}\ u \text{ is a left child}\ \textbf{then}
\ \textbf{return}\ u.\mathit{parent} \cr
17 & \quad \textbf{while}\ u \ne \mathit{root}\ \textbf{and}\ u
\text{ is a right child}\ \textbf{do}\ u \gets u.\mathit{parent}
\cr
18 & \quad \textbf{if}\ u \ne \mathit{root}\ \textbf{then}\ u \gets
u.\mathit{parent}.\mathit{right} \cr
19 & \quad \textbf{while}\ u.\mathit{left} \ne \textbf{nil}
\ \textbf{do}\ u \gets u.\mathit{left} \cr
20 & \quad \textbf{return}\ u
\end{array}}
\]
- Postcondition
- one item more