|
| | SearchTree (void) |
| | an empty tree
|
| | SearchTree (const SearchTree &o) |
| | a copy, node by node with the same shape, in \( O( n ) \) time
|
| | SearchTree (SearchTree &&o) noexcept |
| | the nodes of o, which is left empty, in \( O( 1 ) \) time
|
| SearchTree & | operator= (SearchTree o) noexcept |
| | the nodes of o, by copy and swap
|
| | ~SearchTree () |
| | frees the nodes
|
| void | swap (SearchTree &o) noexcept |
| | exchanges the nodes with o
|
| std::optional< V > | lookup (const K &k) const |
| | the value of the key \( k \), or nothing
|
| void | insert (const K &k, const V &v) |
| | associates the value \( v \) to the key \( k \), in \( O( h ) \) time
|
| void | remove (const K &k) |
| | removes the key \( k \), if present, in \( O( h ) \) time
|
| bool | contains (const K &k) const |
| | true if the key \( k \) is present
|
| std::optional< std::pair< K, V > > | lowerBound (const K &k) const |
| | the smallest key not smaller than \( k \) and its value, or nothing (the find( x ) of an SSet of Morin), by lowerBoundNode() in \( O( h ) \) time
|
| const K * | findKey (const K &k) const |
| | the address of the key \( k \) in the tree, nil if absent, in \( O( h ) \) time: the find( x ) of a USet of Morin, which makes DictionarySet a USet [see SetInterfaces.h]; valid until the key is removed
|
| const K * | lowerBoundKey (const K &k) const |
| | the address of the smallest key not smaller than \( k \), nil if none, by lowerBoundNode() in \( O( h ) \) time: the find( x ) of an SSet of Morin, which makes DictionarySet an SSet; valid until the key is removed
|
| std::size_t | size (void) const |
| | the number \( n \) of keys
|
| bool | empty (void) const |
| | true if there is no key
|
| void | clear (void) |
| | removes all the nodes
|
| Link * | root (void) const |
| | the root, from which the nodes can be walked as in the book, along the fields parent, left and right of a Link, until the leaf, isNil(); they must not be changed
|
| bool | isNil (const Link *u) const |
| | true if \( u \) is the sentinel leaf (or the parent of the root)
|
| Link * | lookupNode (const K &x) const |
| | the node of the key \( x \), nil if absent, in \( O( h ) \) time
|
| Link * | lowerBoundNode (const K &x) const |
| | the node of the smallest key not smaller than \( x \), nil if none, in \( O( h ) \) time (the successor search of the find( x ) of an SSet of Morin)
|
| Link * | min (void) const |
| | the node of the smallest key, nil if the tree is empty, by the left path from the root
|
| Link * | max (void) const |
| | the node of the largest key, nil if the tree is empty, by the right path from the root
|
| Link * | successorNode (Link *t) const |
| | the node of the next key, nil after the largest one and from nil, in \( O( h ) \) time
|
| template<class F> |
| void | forEach (F &&f) const |
| | the foreach of the book: f( k , v ) on the pairs in increasing order of the keys, \( O( n ) \) time, by the visit in symmetric order
|
| Link * | predecessorNode (Link *t) const |
| | the node of the previous key, nil before the first one and from nil, successorNode() with left and right swapped
|
| const_iterator | begin (void) const |
| | the iterator on the smallest key
|
| const_iterator | end (void) const |
| | the iterator past the largest key, the sentinel
|
| const_iterator | cbegin (void) const |
| | as begin()
|
| const_iterator | cend (void) const |
| | as end()
|
template<std::totally_ordered K, class V, bool RB>
class ADS::SearchTree< K, V, RB >
a binary search tree, balanced as a Red-Black tree if RB, and then with the operations based on join [see Joinable]
template<std::totally_ordered K, class V, bool RB>
associates the value \( v \) to the key \( k \), in \( O( h ) \) time
In pseudocode (sections 6.1 and 6.2), the insertion of the binary search tree, balanceInsert() for a Red-Black tree, and the rotation of figure 6.4 (rotateRight() its mirror):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{insertNode}( T , x , v ) \cr
2 & \quad p \gets \textbf{nil};\ u \gets T\quad \triangleright\ p
\text{ is the parent of } u \cr
3 & \quad \textbf{while}\ u \ne \textbf{nil}\ \textbf{and}
\ u.\mathit{key} \ne x\ \textbf{do}\ ( p , u ) \gets ( u ,
\text{the child of } u \text{ on the side of } x ) \cr
4 & \quad \textbf{if}\ u \ne \textbf{nil}\ \textbf{then}
\ u.\mathit{value} \gets v;\ \textbf{return}\ T\quad \triangleright
\ x \text{ was there} \cr
5 & \quad n \gets \text{a new node of } ( x , v ) \text{, hung under } p
\text{ on the side of } x \cr
6 & \quad \text{add } 1 \text{ to the size of } p
\text{ and of each node above it} \cr
7 & \quad \textbf{if}\ \mathit{RB}\ \textbf{then}
\ \mathrm{balanceInsert}( n );\ \textbf{return}
\ \text{the root above } n \cr
8 & \quad \textbf{return}\ n \text{ if } p = \textbf{nil} \text{, else }
T \cr
9 & \textbf{procedure}\ \mathrm{balanceInsert}( t )\quad \triangleright
\ \text{only } t \text{ and its parent may be both red} \cr
10 & \quad t.\mathit{color} \gets \text{red} \cr
11 & \quad \textbf{while}\ t \text{ is not the root and its parent } p
\text{ is red}\ \textbf{do}\quad \triangleright
\ \text{none if (2), } p \text{ black} \cr
12 & \qquad n \gets p.\mathit{parent};\ z \gets \text{the child of } n
\text{ other than } p\quad \triangleright\ n \text{ is black} \cr
13 & \qquad \textbf{if}\ z \text{ is red}\ \textbf{then}
\ p.\mathit{color} \gets z.\mathit{color} \gets \text{black};
\ n.\mathit{color} \gets \text{red};\ t \gets n\quad \triangleright
\ \text{(3)} \cr
14 & \qquad \textbf{else} \cr
15 & \qquad\quad \textbf{if}\ t \text{ and } p
\text{ hang on opposite sides}\ \textbf{then}\quad \triangleright
\ \text{(4a), (4b)} \cr
16 & \qquad\qquad \text{rotate at } p \text{, so that } t
\text{ takes its place};\ ( t , p ) \gets ( p , t ) \cr
17 & \qquad\quad \text{rotate at } n \text{, so that } p
\text{ takes its place};\ p.\mathit{color} \gets \text{black};
\ n.\mathit{color} \gets \text{red}\quad \triangleright
\ \text{(5a), (5b)} \cr
18 & \quad \text{color the root black}\quad \triangleright
\ \text{(1) if } t \text{ is the root} \cr
19 & \textbf{function}\ \mathrm{rotateLeft}( x )\quad \triangleright
\ \text{the right child } y \text{ of } x \text{ goes up} \cr
20 & \quad y \gets x.\mathit{right};\ x.\mathit{right} \gets
y.\mathit{left};\ y.\mathit{left} \gets x \cr
21 & \quad y \text{ replaces } x
\text{ as the child of the old parent of } x;
\ \text{fix the parents} \cr
22 & \quad \text{recompute the size of } x \text{, then that of } y;
\ \textbf{return}\ y
\end{array}}
\]
template<std::totally_ordered K, class V, bool RB>
the node of the key \( x \), nil if absent, in \( O( h ) \) time
In pseudocode (section 6.1):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{lookupNode}( T , x ) \cr
2 & \quad \textbf{while}\ T \ne \textbf{nil}\ \textbf{and}
\ T.\mathit{key} \ne x\ \textbf{do}\ T \gets \text{the child of } T
\text{ on the side of } x \cr
3 & \quad \textbf{return}\ T\quad \triangleright\ \textbf{nil}
\text{ if } x \text{ is not in the tree}
\end{array}}
\]
template<std::totally_ordered K, class V, bool RB>
the node of the smallest key not smaller than \( x \), nil if none, in \( O( h ) \) time (the successor search of the find( x ) of an SSet of Morin)
In pseudocode (section 6.2.1 of Morin, beyond the book), the last node at which the path turns left being the best so far, and the search stopping at a key equal to \( x \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{lowerBoundNode}( T , x ) \cr
2 & \quad b \gets \textbf{nil} \cr
3 & \quad \textbf{while}\ T \ne \textbf{nil}\ \textbf{do} \cr
4 & \qquad \textbf{if}\ T.\mathit{key} < x\ \textbf{then}\ T \gets
T.\mathit{right} \cr
5 & \qquad \textbf{else if}\ x < T.\mathit{key}\ \textbf{then}\ b
\gets T;\ T \gets T.\mathit{left} \cr
6 & \qquad \textbf{else return}\ T \cr
7 & \quad \textbf{return}\ b
\end{array}}
\]
template<std::totally_ordered K, class V, bool RB>
removes the key \( k \), if present, in \( O( h ) \) time
In pseudocode (sections 6.1 and 6.2), the removal of the binary search tree and balanceDelete() for a Red-Black tree, its cases (1) to (4) written once for the two sides of \( p \), \( \mathit{near} \) the side of \( t \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{removeNode}( T , x ) \cr
2 & \quad u \gets \mathrm{lookupNode}( T , x );\ \textbf{if}\ u =
\textbf{nil}\ \textbf{then return}\ T \cr
3 & \quad \textbf{if}\ u \text{ has two children}\ \textbf{then}\quad
\triangleright\ \text{the node to cut has one child at most} \cr
4 & \qquad s \gets \mathrm{min}( u.\mathit{right} );
\ \text{copy the key and the value of } s \text{ into } u;\ u \gets
s \cr
5 & \quad t \gets \text{the child of } u \text{ if it has one, else }
\textbf{nil} \cr
6 & \quad \text{subtract } 1 \text{ from the size of each node above }
u;\ \text{put } t \text{ in the place of } u \cr
7 & \quad \textbf{if}\ \mathit{RB}\ \textbf{and}\ u \text{ is black}
\ \textbf{then}\ \mathrm{balanceDelete}( t , u.\mathit{parent} ) \cr
8 & \quad \text{free } u;\ \textbf{return}\ \text{the root} \cr
9 & \textbf{procedure}\ \mathrm{balanceDelete}( t , p )\quad
\triangleright\ \text{a black node is missing below } p
\text{ on the side of } t \cr
10 & \quad \textbf{while}\ p \ne \textbf{nil}\ \textbf{and}\ t
\text{ is black}\ \textbf{do} \cr
11 & \qquad \mathit{near} \gets \text{the side of } p
\text{ that holds } t;\ \mathit{far} \gets \text{the other side} \cr
12 & \qquad f \gets p.\mathit{far};\ a \gets f.\mathit{near};\ b \gets
f.\mathit{far}\quad \triangleright
\ \text{the sibling and the nephews} \cr
13 & \qquad \textbf{if}\ f \text{ is red}\ \textbf{then}
\ p.\mathit{color} \gets \text{red};\ f.\mathit{color} \gets
\text{black};\ \text{rotate } p \text{ down to } \mathit{near}\quad
\triangleright\ \text{(1)} \cr
14 & \qquad \textbf{else if}\ a \text{ and } b \text{ are black}
\ \textbf{then}\ f.\mathit{color} \gets \text{red};\ ( t , p ) \gets
( p , p.\mathit{parent} )\quad \triangleright\ \text{(2)} \cr
15 & \qquad \textbf{else if}\ b \text{ is black}\ \textbf{then}
\ a.\mathit{color} \gets \text{black};\ f.\mathit{color} \gets
\text{red};\ \text{rotate } f \text{ down to } \mathit{far}\quad
\triangleright\ \text{(3)} \cr
16 & \qquad \textbf{else}\quad \triangleright\ \text{(4)} \cr
17 & \qquad\quad f.\mathit{color} \gets p.\mathit{color};
\ p.\mathit{color} \gets b.\mathit{color} \gets \text{black};
\ \text{rotate } p \text{ down to } \mathit{near} \cr
18 & \qquad\quad ( t , p ) \gets ( \text{the root} , \textbf{nil} )\quad
\triangleright\ \text{the missing black is restored} \cr
19 & \quad \textbf{if}\ t \ne \textbf{nil}\ \textbf{then}
\ t.\mathit{color} \gets \text{black}
\end{array}}
\]
template<std::totally_ordered K, class V, bool RB>
the node of the next key, nil after the largest one and from nil, in \( O( h ) \) time
In pseudocode (section 6.1), by the links to the parents:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{successorNode}( t ) \cr
2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return}
\ \textbf{nil} \cr
3 & \quad \textbf{if}\ t.\mathit{right} \ne \textbf{nil}
\ \textbf{then return}\ \mathrm{min}( t.\mathit{right} ) \cr
4 & \quad \textbf{while}\ t \text{ is the right child of its parent}
\ \textbf{do}\ t \gets t.\mathit{parent} \cr
5 & \quad \textbf{return}\ t.\mathit{parent}\quad \triangleright
\ \text{the first ancestor with } t \text{ on its left, or }
\textbf{nil}
\end{array}}
\]
- Precondition
- \( t \) is not a null pointer