ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::TreePriorityQueue< T, P > Class Template Reference

a priority queue on a Red-Black tree of the pairs of a priority and of a number of insertion More...

#include <ADS/priority/TreePriorityQueue.h>

Public types

using Item = T
 the type of the items
using Priority = P
 the type of the priorities
using Handle = long
 the number of insertion of an item
using value_type = typename RedBlackTree< Key , T >::value_type
 the type of the items of the view
using const_iterator = typename RedBlackTree< Key , T >::const_iterator
 the iterator of the view, constant
using iterator = const_iterator
 the same as const_iterator

Public member functions

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, the leftmost node of the tree, in \( O( \log n ) \) time
T deleteMin (void)
 removes the item of smallest priority and returns it, in \( O( \log n ) \) time
Handle insert (const T &x, const P &p)
 inserts \( x \) with the priority \( p \) and gives its handle, in \( O( \log n ) \) time
void decrease (Handle y, const P &p)
 lowers to \( p \) the priority of the item of the handle \( y \), 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()

Detailed description

template<class T, std::totally_ordered P = int>
class ADS::TreePriorityQueue< T, P >

a priority queue on a Red-Black tree of the pairs of a priority and of a number of insertion

Member typedef documentation

◆ const_iterator

template<class T, std::totally_ordered P = int>
using ADS::TreePriorityQueue< T, P >::const_iterator = typename RedBlackTree< Key , T >::const_iterator

the iterator of the view, constant

◆ Handle

template<class T, std::totally_ordered P = int>
using ADS::TreePriorityQueue< T, P >::Handle = long

the number of insertion of an item

◆ Item

template<class T, std::totally_ordered P = int>
using ADS::TreePriorityQueue< T, P >::Item = T

the type of the items

◆ iterator

template<class T, std::totally_ordered P = int>
using ADS::TreePriorityQueue< T, P >::iterator = const_iterator

the same as const_iterator

◆ Priority

template<class T, std::totally_ordered P = int>
using ADS::TreePriorityQueue< T, P >::Priority = P

the type of the priorities

◆ value_type

template<class T, std::totally_ordered P = int>
using ADS::TreePriorityQueue< T, P >::value_type = typename RedBlackTree< Key , T >::value_type

the type of the items of the view

Member function documentation

◆ begin()

template<class T, std::totally_ordered P = int>
const_iterator ADS::TreePriorityQueue< T, P >::begin ( void ) const
inlinenodiscard

the first item of the view

◆ cbegin()

template<class T, std::totally_ordered P = int>
const_iterator ADS::TreePriorityQueue< T, P >::cbegin ( void ) const
inlinenodiscard

the same as begin()

◆ cend()

template<class T, std::totally_ordered P = int>
const_iterator ADS::TreePriorityQueue< T, P >::cend ( void ) const
inlinenodiscard

the same as end()

◆ decrease()

template<class T, std::totally_ordered P = int>
void ADS::TreePriorityQueue< T, P >::decrease ( Handle y,
const P & p )

lowers to \( p \) the priority of the item of the handle \( y \), in \( O( \log n ) \) time

In pseudocode (section 10.1.2), the item moved to its new key:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{decrease}( y , p ) \cr 2 & \quad k \gets ( \mathit{pr}[ y ] , y );\ x \gets \mathrm{lookup}( T , k );\ \mathrm{remove}( T , k ) \cr 3 & \quad \mathit{pr}[ y ] \gets p;\ \mathrm{insert}( T , ( p , y ) , x ) \end{array}} \]

Precondition
\( y \) is an item still in the queue and \( p \) is not larger than its priority

◆ deleteMin()

template<class T, std::totally_ordered P = int>
T ADS::TreePriorityQueue< T, P >::deleteMin ( void )

removes the item of smallest priority and returns it, in \( O( \log n ) \) time

In pseudocode (section 10.1.2), \( T \) the tree of the keys \( ( p , i ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{deleteMin}() \cr 2 & \quad ( k , x ) \gets \text{the key and the value of the leftmost node of } T\quad \triangleright\ \text{the least } ( p , i ) \cr 3 & \quad \mathrm{remove}( T , k );\ \textbf{return}\ x \end{array}} \]

Precondition
the queue is not empty
Postcondition
one item less

◆ empty()

template<class T, std::totally_ordered P = int>
bool ADS::TreePriorityQueue< T, P >::empty ( void ) const
nodiscard

whether the queue has no item

◆ end()

template<class T, std::totally_ordered P = int>
const_iterator ADS::TreePriorityQueue< T, P >::end ( void ) const
inlinenodiscard

the end of the view

◆ insert()

template<class T, std::totally_ordered P = int>
Handle ADS::TreePriorityQueue< T, P >::insert ( const T & x,
const P & p )

inserts \( x \) with the priority \( p \) and gives its handle, in \( O( \log n ) \) time

In pseudocode (section 10.1.2), \( \mathit{pr} \) the vector of the priorities by number of insertion:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( x , p ) \cr 2 & \quad i \gets |\mathit{pr}|\quad \triangleright \ \text{the handle, which breaks the ties} \cr 3 & \quad \mathrm{insert}( T , ( p , i ) , x );\ \text{append } p \text{ to } \mathit{pr};\ \textbf{return}\ i \end{array}} \]

Postcondition
one item more

◆ min()

template<class T, std::totally_ordered P = int>
const T & ADS::TreePriorityQueue< T, P >::min ( void ) const
nodiscard

the item of smallest priority, the leftmost node of the tree, in \( O( \log n ) \) time

Precondition
the queue is not empty

◆ size()

template<class T, std::totally_ordered P = int>
int ADS::TreePriorityQueue< T, P >::size ( void ) const
nodiscard

the number of items, \( n \)


The documentation for this class was generated from the following file: