|
ADS
Algorithms and Data Structures
|
The QUEUE with the minimum of exercise 3.15 of Morin's book, beyond the book [see ADT.h]: enqueue(), dequeue(), top(), size() and min(), all in \( O( 1 ) \) amortized time, on two stacks with the minimum [see MinStack.h]: the items enter the stack \( I \) and leave from the stack \( O \), which, when empty, takes all the items of \( I \) in the reverse order, the front on top. More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/stack/MinStack.h"#include <cstddef>#include <functional>#include <utility>#include "ADS/queue/impl/MinQueue.tpp"Go to the source code of this file.
Classes | |
| class | ADS::MinQueue< T, C > |
| a QUEUE with its least item in \( O( 1 ) \) amortized time More... | |
Namespaces | |
| namespace | ADS |
The QUEUE with the minimum of exercise 3.15 of Morin's book, beyond the book [see ADT.h]: enqueue(), dequeue(), top(), size() and min(), all in \( O( 1 ) \) amortized time, on two stacks with the minimum [see MinStack.h]: the items enter the stack \( I \) and leave from the stack \( O \), which, when empty, takes all the items of \( I \) in the reverse order, the front on top.
Each item is pushed and popped twice at most, so that a sequence of \( m \) operations costs \( O( m ) \), and the least item is the lesser of the minima of the two stacks. RealTimeMinQueue [see RealTimeMinQueue.h] spreads the move of the stack \( I \) over the operations, for \( O( 1 ) \) in the worst case.
The contracts [see Contract.h]: top(), dequeue() and min() need a queue that is not empty; enqueue() ensures one item more, dequeue() one less. The operations are defined in MinQueue.tpp.