|
ADS
Algorithms and Data Structures
|
Algorithms that use a STACK only through its operations, beyond the book. More...
Files | |
| file | ADS/stack/StackAlgorithms.h |
| Algorithms that use a STACK [see ADT.h] only through its operations, beyond the book: | |
Functions | |
| bool | ADS::isPalindrome (const std::string &s) |
| whether \( s \) reads the same from both ends | |
| template<Stack S> | |
| void | ADS::sortStack (S &s) |
| sorts \( s \) with the least item on top | |
| template<Stack S> | |
| void | ADS::removeGreater (S &s, const typename S::Item &k) |
| removes the items larger than \( k \), the others in their order | |
| template<Stack S, class F> | |
| void | ADS::forEachBottomUp (S &s, F &&f) |
| f( x ) on each item \( x \), from the bottom to the top | |
| bool | ADS::isDyckWord (std::span< const int > w) |
| whether the sequence \( w \) of \( +1 \) and \( -1 \) is a Dyck word, i.e., has no negative prefix sum | |
| bool | ADS::isMatched (std::string_view s) |
| whether \( s \), a string of the brackets (), [] and {}, is matched, in \( O( n ) \) | |
| template<Stack S, Queue Q = LinkedQueue< typename S::Item >> | |
| void | ADS::reverseWithQueue (S &s) |
| the order of the items of \( s \) reversed by a QUEUE, the only operations of \( s \) being push() and pop() | |
Algorithms that use a STACK only through its operations, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of stacks.
| void ADS::forEachBottomUp | ( | S & | s, |
| F && | f ) |
f( x ) on each item \( x \), from the bottom to the top
In pseudocode (section 4.3.1), the stack of the calls holding the items popped:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{forEachBottomUp}( s , f ) \cr 2 & \quad \textbf{if}\ \textbf{not}\ \mathrm{empty}( s )\ \textbf{then} \cr 3 & \qquad x \gets \mathrm{pop}( s );\ \mathrm{forEachBottomUp}( s , f )\quad \triangleright\ \text{the items below } x \text{ first} \cr 4 & \qquad f( x );\ \mathrm{push}( s , x ) \end{array}} \]
|
inline |
whether the sequence \( w \) of \( +1 \) and \( -1 \) is a Dyck word, i.e., has no negative prefix sum
In pseudocode (Morin, exercise 1.2), the stack of items all alike kept as its size \( h \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isDyckWord}( w ) \cr 2 & \quad h \gets 0 \cr 3 & \quad \textbf{foreach}\ c \in w\ \textbf{do} \cr 4 & \qquad \textbf{if}\ c = +1\ \textbf{then}\ h \gets h + 1 \quad \triangleright\ \mathrm{push} \cr 5 & \qquad \textbf{else if}\ h = 0\ \textbf{then return false} \ \textbf{else}\ h \gets h - 1\quad \triangleright\ \mathrm{pop} \cr 6 & \quad \textbf{return true} \end{array}} \]
|
inline |
whether \( s \), a string of the brackets (), [] and {}, is matched, in \( O( n ) \)
In pseudocode (Morin, exercise 1.3):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isMatched}( s ) \cr 2 & \quad S \gets \text{an empty stack} \cr 3 & \quad \textbf{foreach}\ \text{character } c \text{ of } s \ \textbf{do} \cr 4 & \qquad \textbf{if}\ c \text{ opens}\ \textbf{then}\ \mathrm{push}( S , c ) \cr 5 & \qquad \textbf{else if}\ \mathrm{empty}( S )\ \textbf{or} \ \mathrm{pop}( S ) \text{ does not open } c \ \textbf{then return false} \cr 6 & \quad \textbf{return}\ \mathrm{empty}( S ) \end{array}} \]
|
inline |
whether \( s \) reads the same from both ends
In pseudocode (section 4.3.1), the stack reversing the string:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isPalindrome}( s ) \cr 2 & \quad S \gets \text{an empty stack};\ \textbf{foreach} \ \text{character } c \text{ of } s\ \textbf{do}\ \mathrm{push}( S , c ) \cr 3 & \quad \textbf{foreach}\ \text{character } c \text{ of } s \ \textbf{do}\quad \triangleright\ S \text{ gives } s \text{ backwards} \cr 4 & \qquad \textbf{if}\ c \ne \mathrm{pop}( S ) \ \textbf{then return false} \cr 5 & \quad \textbf{return true} \end{array}} \]
| void ADS::removeGreater | ( | S & | s, |
| const typename S::Item & | k ) |
removes the items larger than \( k \), the others in their order
In pseudocode (section 4.3.1), the auxiliary stack reversing the order twice:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{removeGreater}( s , k ) \cr 2 & \quad A \gets \text{an empty stack}\quad \triangleright \ \text{the kept items, reversed} \cr 3 & \quad \textbf{while not}\ \mathrm{empty}( s )\ \textbf{do} \cr 4 & \qquad x \gets \mathrm{pop}( s );\ \textbf{if}\ \textbf{not}\ x > k \ \textbf{then}\ \mathrm{push}( A , x ) \cr 5 & \quad \textbf{while not}\ \mathrm{empty}( A )\ \textbf{do} \ \mathrm{push}( s , \mathrm{pop}( A ) )\quad \triangleright \ \text{the order restored} \end{array}} \]
| void ADS::reverseWithQueue | ( | S & | s | ) |
the order of the items of \( s \) reversed by a QUEUE, the only operations of \( s \) being push() and pop()
In pseudocode (Morin, exercise 1.4):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{reverseWithQueue}( s ) \cr 2 & \quad Q \gets \text{an empty queue} \cr 3 & \quad \textbf{while not}\ \mathrm{empty}( s )\ \textbf{do} \ \mathrm{enqueue}( Q , \mathrm{pop}( s ) ) \cr 4 & \quad \textbf{while not}\ \mathrm{empty}( Q )\ \textbf{do} \ \mathrm{push}( s , \mathrm{dequeue}( Q ) ) \end{array}} \]
| void ADS::sortStack | ( | S & | s | ) |
sorts \( s \) with the least item on top
In pseudocode (section 4.3.1, the bubble sort on a stack), \( n - 1 \) passes, each carrying the largest item it meets to the bottom:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{pass}( s )\quad \triangleright \ \text{the largest item to the bottom} \cr 2 & \quad \textbf{if}\ \mathrm{empty}( s )\ \textbf{then return} \cr 3 & \quad a \gets \mathrm{pop}( s ) \cr 4 & \quad \textbf{if}\ \textbf{not}\ \mathrm{empty}( s )\ \textbf{then} \cr 5 & \qquad b \gets \mathrm{pop}( s );\ \mathrm{push}( s , \max( a , b ) );\ \mathrm{pass}( s );\ a \gets \min( a , b ) \cr 6 & \quad \mathrm{push}( s , a ) \cr 7 & \textbf{procedure}\ \mathrm{sortStack}( s ) \cr 8 & \quad \mathrm{pass}( s ) \text{, } n - 1 \text{ times} \end{array}} \]