|
| | ThreadedTree (void) |
| | the empty tree, the head node alone, its left link a thread to itself
|
| | ThreadedTree (const ThreadedTree &)=delete |
| | no copy
|
| ThreadedTree & | operator= (const ThreadedTree &)=delete |
| | no copy
|
| | ~ThreadedTree () |
| | frees the nodes
|
| void | insert (const K &k) |
| | inserts \( k \), if it is not there
|
| bool | contains (const K &k) const |
| | whether \( k \) is there, by the search of a binary search tree that stops at a thread
|
| std::size_t | size (void) const |
| | the number of keys
|
| template<class F> |
| void | forEach (F &&f) const |
| | f( k ) on the keys by increasing order, by the threads: no stack, no recursion
|
| std::vector< K > | reversed (void) const |
| | the keys by decreasing order, by the left threads, as forEach() with the predecessors
|
| const_iterator | begin (void) const |
| | the iterator on the least key, the successor of the head
|
| const_iterator | end (void) const |
| | the iterator past the largest key, the head
|
| const_iterator | cbegin (void) const |
| | as begin()
|
| const_iterator | cend (void) const |
| | as end()
|
template<std::totally_ordered K>
class ADS::ThreadedTree< K >
a binary search tree with threads in symmetric order
template<std::totally_ordered K>
inserts \( k \), if it is not there
In pseudocode (Knuth, section 2.3.1, the insertion into a threaded tree, beyond the book): the new leaf takes over the thread that the search ends on, and threads back to its parent on the other side:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{insert}( k ) \cr
2 & \quad x \gets \text{the root} \cr
3 & \quad \textbf{loop} \cr
4 & \qquad \textbf{if}\ k = x.\mathit{key}\ \textbf{then return} \cr
5 & \qquad \textbf{if}\ k < x.\mathit{key}\ \textbf{and}\ x.\mathit{left}
\text{ is a thread}\ \textbf{then} \cr
6 & \qquad\quad x.\mathit{left} \gets \text{a leaf } ( k ,\ \text{left
thread } x.\mathit{left} ,\ \text{right thread } x );
\ \textbf{return} \cr
7 & \qquad \textbf{if}\ k > x.\mathit{key}\ \textbf{and}
\ x.\mathit{right} \text{ is a thread}\ \textbf{then} \cr
8 & \qquad\quad x.\mathit{right} \gets \text{a leaf } ( k ,\ \text{left
thread } x ,\ \text{right thread } x.\mathit{right} );
\ \textbf{return} \cr
9 & \qquad x \gets x.\mathit{left} \text{ if } k < x.\mathit{key}
\text{, } x.\mathit{right} \text{ otherwise}
\end{array}}
\]
- Postcondition
- \( k \) is there