ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BinaryTreeAlgorithms.h

The classic problems on binary trees and on binary search trees, written once for any representation of the nodes. More...

Files

file  ADS/tree/BinaryTreeAlgorithms.h
 The classic problems on binary trees and on binary search trees, written once for any representation of the nodes: a node is reached by a pointer, nil is nullptr, and its links and its key are read either by the member functions left(), right(), parent() and read() of BinaryTree [see BinaryTree.h] or by the fields left, right, parent and key of a plain struct, as in the pseudocode of the book.

Concepts

concept  ADS::BinaryNode
 a node of a binary tree: a pointer to a struct whose children are read by left() and right() or by the fields left and right
concept  ADS::ParentBinaryNode
 a node of a binary tree with the link to its parent
concept  ADS::KeyedBinaryNode
 a node of a binary tree with a key
concept  ADS::SizedBinaryNode
 a node of a binary tree with a key and the size of its subtree

Functions

template<ParentBinaryNode N>
N ADS::lowestCommonAncestor (N u, N v)
 the deepest node that is an ancestor of both \( u \) and \( v \), each node being an ancestor of itself; \( O( h ) \) time, \( O( 1 ) \) space
template<KeyedBinaryNode N, KeyedBinaryNode M, class Equal = std::equal_to<>>
bool ADS::equalTrees (N t, M s, Equal eq={})
 whether \( t \) and \( s \) have the same shape and eq( a , b ) holds on the keys \( a \) and \( b \) of each two nodes in the same place; \( O( n ) \) time, \( O( h ) \) space
template<KeyedBinaryNode N, class Compare = std::less<>>
bool ADS::isSearchTree (N t, Compare less={})
 whether the keys of \( t \) are strictly increasing in the invisit, by less; \( O( n ) \) time, \( O( h ) \) space
template<BinaryNode N, class Red>
bool ADS::isRedBlackTree (N t, Red red)
 whether the coloring of \( t \), red( u ) telling whether the node \( u \) is red, respects the constraints of a Red-Black tree: the root black, no red node with a red child, the same number of black nodes on every path from the root down to a leaf nil; \( O( n ) \) time, \( O( h ) \) space
template<KeyedBinaryNode N, class K, class F, class Compare = std::less<>>
std::size_t ADS::searchTreeRange (N t, const K &lo, const K &hi, F &&f, Compare less={})
 f( u ) on the nodes \( u \) of the binary search tree \( t \) whose keys are in \( [ \mathit{lo} , \mathit{hi} ] \), in increasing order of the keys; the number of the nodes examined, at most \( k + 2 ( h + 1 ) \) for \( k \) keys in the range; \( O( h + k ) \) time
template<KeyedBinaryNode N, class K, class Compare = std::less<>>
N ADS::searchTreeFind (N t, const K &k, Compare less={})
 the node of the key \( k \) in the binary search tree \( t \), nil if absent; \( O( h ) \) time
template<KeyedBinaryNode N, class K, class Compare = std::less<>>
auto ADS::searchTreeLookup (N t, const K &k, Compare less={})
 the value of the key \( k \) in the binary search tree \( t \), nothing if absent, by searchTreeFind(); \( O( h ) \) time
template<KeyedBinaryNode N, class K, class Compare = std::less<>>
N ADS::searchTreeLowerBound (N t, const K &k, Compare less={})
 the node of the smallest key not smaller than \( k \) in the binary search tree \( t \), nil if none (the find(x) of an SSet of Morin); \( O( h ) \) time
template<KeyedBinaryNode N, class K, class Compare = std::less<>>
auto ADS::searchTreeFindKey (N t, const K &k, Compare less={})
 the address of the key \( k \) in the binary search tree \( t \), nil if absent (the find(x) of a USet of Morin), by searchTreeLowerBound() and one more comparison; \( O( h ) \) time
template<KeyedBinaryNode N, class K, class Compare = std::less<>>
auto ADS::searchTreeLowerBoundKey (N t, const K &k, Compare less={})
 the address of the smallest key not smaller than \( k \) in the binary search tree \( t \), nil if none, by searchTreeLowerBound(); \( O( h ) \) time
template<KeyedBinaryNode N, class K, class Compare = std::less<>>
auto ADS::searchTreeLowerBoundEntry (N t, const K &k, Compare less={})
 the key and the value of the smallest key not smaller than \( k \) in the binary search tree \( t \) as a pair, nothing if none, by searchTreeLowerBound(); \( O( h ) \) time
template<SizedBinaryNode N>
N ADS::searchTreeSelect (N t, std::size_t i)
 the node of rank \( i \) of the binary search tree \( t \), i.e., with \( i \) smaller keys, by the sizes of the left subtrees; \( O( h ) \) time
template<SizedBinaryNode N, class K, class Compare = std::less<>>
std::size_t ADS::searchTreeRank (N t, const K &k, Compare less={})
 the number of the keys smaller than \( k \) in the binary search tree \( t \), the sizes of the left subtrees passed on the right along the search of \( k \), plus one for each node so passed; \( O( h ) \) time
template<class N, class K, class Compare = std::less<>>
requires KeyedBinaryNode< N * > && std::is_pointer_v< decltype( N::left ) >
N ** ADS::searchTreeLink (N **link, const K &k, std::vector< N * > &P, Compare less={})
 the link at which the key \( k \) is, or would be inserted, in the binary search tree whose root is *link, i.e., the address of the root or of the field left or right of the parent, the nodes passed above it pushed on \( P \) from the root down; the key is there if and only if the node at the link returned is not nil; \( O( h ) \) time
template<class N>
requires BinaryNode< N * > && std::is_pointer_v< decltype( N::left ) >
N * ADS::searchTreeSplice (N **link, std::vector< N * > &P)
 the node at link (as given by searchTreeLink()) taken out of its binary search tree: if it has two children, its key and its value are replaced by those of its successor, which is taken out instead, and the nodes down to the successor are pushed on \( P \); the node taken out is returned, to be freed, and its parent (the last node of \( P \)) links its only child; \( O( h ) \) time
template<ParentBinaryNode N>
N ADS::nextPreorder (N u)
 the node after \( u \) in the previsit of its tree, nil after the last; \( O( 1 ) \) amortized over a whole visit (exercise 6.8 of Morin)
template<ParentBinaryNode N>
N ADS::nextInorder (N u)
 the node after \( u \) in the invisit of its tree, nil after the last; \( O( 1 ) \) amortized over a whole visit (exercise 6.8 of Morin)
template<ParentBinaryNode N>
N ADS::nextPostorder (N u)
 the node after \( u \) in the postvisit of its tree, nil after the root; \( O( 1 ) \) amortized over a whole visit (exercise 6.8 of Morin)
template<BinaryNode N>
N ADS::firstInorder (N u)
 the first node of the invisit of the subtree of \( u \), its leftmost node, from which nextInorder() starts; \( O( h ) \) time
template<BinaryNode N>
N ADS::firstPostorder (N u)
 the first node of the postvisit of the subtree of \( u \), reached by going left when possible and right otherwise, from which nextPostorder() starts; \( O( h ) \) time (the first node of the previsit is \( u \) itself)
template<BinaryNode N>
long ADS::treeHeight (N t)
 the height of the tree \( t \), \( -1 \) if empty, by levels with no recursion: \( O( n ) \) time and \( O( w ) \) space for a tree of width \( w \), on a degenerate tree of any height
template<ParentBinaryNode N>
std::size_t ADS::subtreeSize (N u)
 the number of nodes of the subtree of \( u \), by a walk along the links to the parents that never leaves the subtree, with no recursion and no stack: \( O( n_u ) \) time and \( O( 1 ) \) space (the size2(u) of exercise 6.4 of Morin)
template<ParentBinaryNode N>
long ADS::subtreeHeight (N u)
 the height of the subtree of \( u \), \( -1 \) for nil, by the walk of subtreeSize() that keeps the depth below \( u \): \( O( n_u ) \) time and \( O( 1 ) \) space (the height2(u) of exercise 6.5 of Morin)
template<BinaryNode N>
bool ADS::isSizeBalanced (N t)
 whether every node \( u \) of \( t \) has subtrees whose sizes differ by 1 at most (exercise 6.6 of Morin), by a postvisit that returns the size of each subtree, or stops at the first that is not balanced: \( O( n ) \) time, against the \( \Theta( n \log n ) \) to \( \Theta( n^2 ) \) of a test that computes each size again
template<BinaryNode N, class F>
void ADS::orderNumbers (N t, F &&f)
 f( u , pre , in , post ) on each node \( u \) of \( t \) with its numbers in the previsit, in the invisit and in the postvisit, from 0, in one visit: \( O( n ) \) time, \( O( h ) \) space (exercise 6.7 of Morin).
template<BinaryNode N>
std::vector< bool > ADS::shapeCode (N t)
 the shape of \( t \) in \( 2 ( n - 1 ) \) bits, none for \( n \le 1 \) (exercise 6.11 of Morin): for each node in the previsit but the last, which is a leaf, whether it has a left and whether it has a right child; \( O( n ) \) time.
BinaryTree< std::size_t > * ADS::fromShapeCode (const std::vector< bool > &c, std::size_t n)
 the tree of the shape of shapeCode() above, its values the numbers of the previsit; the caller owns it, nil if \( n = 0 \); \( O( n ) \)
template<KeyedBinaryNode N, class K, class F, class Compare = std::less<>>
std::size_t ADS::lessOrEqual (N t, const K &x, F &&f, Compare less={})
 f( u ) on the nodes \( u \) of the binary search tree \( t \) whose keys are not larger than \( x \), in increasing order, and their number \( n' \); \( O( n' + h ) \) time (the getLE(x) of exercise 6.14 of Morin)
template<class T, class Make, class Compare = std::less<>>
auto ADS::minimumHeightSearchTree (const std::vector< T > &A, Make make, Compare less={}) -> std::invoke_result_t< Make &, const T &, std::nullptr_t, std::nullptr_t >
 the binary search tree of the keys of \( A \) of the least height, the lower middle key at the root of each subtree, whose nodes are made by make( v , l , r ) over the subtrees \( l \) and \( r \), nil if \( A \) is empty; \( O( n ) \) time
template<class T, class Compare = std::less<>>
requires std::predicate< Compare & , const T & , const T & >
BinaryTree< T > * ADS::minimumHeightSearchTree (const std::vector< T > &A, Compare less={})
 the tree of minimumHeightSearchTree() above as a BinaryTree, which the caller owns
template<class T>
BinaryTree< T > * ADS::fromPreorderInorder (const std::vector< T > &pre, const std::vector< T > &in)
 the binary tree whose previsit is pre and whose invisit is in, nil if they are empty; \( O( n ) \) time; the caller owns it
template<class T, class Compare = std::less< T >>
BinaryTree< T > * ADS::cartesianBinaryTree (const std::vector< T > &A, Compare less={})
 the Cartesian tree of \( A \), the leftmost minimum by less at the root, a heap whose invisit is \( A \), nil if \( A \) is empty; \( O( n ) \) time; the caller owns it

Detailed description

The classic problems on binary trees and on binary search trees, written once for any representation of the nodes.

The definitions, the invariants and the costs of the area are in Theory of trees.

Function documentation

◆ cartesianBinaryTree()

template<class T, class Compare = std::less< T >>
BinaryTree< T > * ADS::cartesianBinaryTree ( const std::vector< T > & A,
Compare less = {} )

the Cartesian tree of \( A \), the leftmost minimum by less at the root, a heap whose invisit is \( A \), nil if \( A \) is empty; \( O( n ) \) time; the caller owns it

In pseudocode (Vuillemin, beyond the book), \( p \) the parents that cartesianTree() gives by the stack of the right spine:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{cartesianBinaryTree}( A ) \cr 2 & \quad p \gets \mathrm{cartesianTree}( A );\ N[ i ] \gets \mathrm{BinaryTree}( A[ i ] ) \text{ for each } i \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad \textbf{if}\ i \text{ is the root}\ \textbf{then}\ r \gets N[ i ] \cr 5 & \qquad \textbf{else if}\ i < p[ i ]\ \textbf{then}\ N[ p[ i ] ].\mathrm{insertLeft}( N[ i ] ) \cr 6 & \qquad \textbf{else}\ N[ p[ i ] ].\mathrm{insertRight}( N[ i ] ) \cr 7 & \quad \textbf{return}\ r \end{array}} \]

Postcondition
the invisit is A (an audit)

◆ equalTrees()

template<KeyedBinaryNode N, KeyedBinaryNode M, class Equal = std::equal_to<>>
bool ADS::equalTrees ( N t,
M s,
Equal eq = {} )
nodiscard

whether \( t \) and \( s \) have the same shape and eq( a , b ) holds on the keys \( a \) and \( b \) of each two nodes in the same place; \( O( n ) \) time, \( O( h ) \) space

In pseudocode (section 5.6), a simultaneous previsit:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{equalTrees}( t , s ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{or}\ s = \textbf{nil} \ \textbf{then return}\ ( t = \textbf{nil}\ \textbf{and}\ s = \textbf{nil} ) \cr 3 & \quad \textbf{if}\ \textbf{not}\ \mathrm{eq}( t.\mathit{key} , s.\mathit{key} )\ \textbf{then return false} \cr 4 & \quad \textbf{return}\ \mathrm{equalTrees}( t.\mathit{left} , s.\mathit{left} )\ \textbf{and}\ \mathrm{equalTrees}( t.\mathit{right} , s.\mathit{right} ) \end{array}} \]

◆ firstInorder()

template<BinaryNode N>
N ADS::firstInorder ( N u)
nodiscard

the first node of the invisit of the subtree of \( u \), its leftmost node, from which nextInorder() starts; \( O( h ) \) time

Precondition
\( u \) is not nil

◆ firstPostorder()

template<BinaryNode N>
N ADS::firstPostorder ( N u)
nodiscard

the first node of the postvisit of the subtree of \( u \), reached by going left when possible and right otherwise, from which nextPostorder() starts; \( O( h ) \) time (the first node of the previsit is \( u \) itself)

Precondition
\( u \) is not nil

◆ fromPreorderInorder()

template<class T>
BinaryTree< T > * ADS::fromPreorderInorder ( const std::vector< T > & pre,
const std::vector< T > & in )

the binary tree whose previsit is pre and whose invisit is in, nil if they are empty; \( O( n ) \) time; the caller owns it

In pseudocode (section 5.6, beyond the book for the stack), \( S \) the nodes whose right subtree is still open and \( j \) the next place of in:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fromPreorderInorder}( P , I ) \cr 2 & \quad r \gets \mathrm{BinaryTree}( P[ 0 ] );\ S \gets ( r );\ j \gets 0 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad t \gets \mathrm{BinaryTree}( P[ i ] );\ \ell \gets \textbf{nil} \cr 5 & \qquad \textbf{while}\ S \ne \emptyset\ \textbf{and} \ \mathrm{top}( S ).\mathit{key} = I[ j ]\ \textbf{do}\ \ell \gets \mathrm{pop}( S );\ j \gets j + 1 \cr 6 & \qquad \textbf{if}\ \ell \ne \textbf{nil}\ \textbf{then} \ \ell.\mathrm{insertRight}( t )\ \textbf{else}\ \mathrm{top}( S ).\mathrm{insertLeft}( t ) \cr 7 & \qquad \mathrm{push}( S , t ) \cr 8 & \quad \textbf{return}\ r \end{array}} \]

Precondition
pre and in are the visits of a binary tree of distinct values

◆ fromShapeCode()

BinaryTree< std::size_t > * ADS::fromShapeCode ( const std::vector< bool > & c,
std::size_t n )
inline

the tree of the shape of shapeCode() above, its values the numbers of the previsit; the caller owns it, nil if \( n = 0 \); \( O( n ) \)

In pseudocode (exercise 6.11 of Morin), the previsit read back, the nodes whose right child is still due on a stack:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fromShapeCode}( c , n ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 3 & \qquad u \gets \text{a node } i;\ \text{link it as the child due} \text{ (the left of the last node, or the right of the top)} \cr 4 & \qquad ( \ell , r ) \gets \text{the next two bits of } c \text{, } ( 0 , 0 ) \text{ for the last node} \cr 5 & \qquad \textbf{if}\ r\ \textbf{then}\ \text{push } u \cr 6 & \qquad \textbf{if not}\ \ell\ \textbf{then}\ \text{the next is the right child of a popped node} \end{array}} \]

Precondition
\( c \) has \( 2 ( n - 1 ) \) bits, the code of a tree

◆ isRedBlackTree()

template<BinaryNode N, class Red>
bool ADS::isRedBlackTree ( N t,
Red red )
nodiscard

whether the coloring of \( t \), red( u ) telling whether the node \( u \) is red, respects the constraints of a Red-Black tree: the root black, no red node with a red child, the same number of black nodes on every path from the root down to a leaf nil; \( O( n ) \) time, \( O( h ) \) space

In pseudocode (section 6.2), a postvisit giving the black height of each subtree, the leaf nil counted, or \( -1 \) if it breaks a constraint:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bh}( t )\quad \triangleright \ \text{the black height counting the leaves nil, or } -1 \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return}\ 1 \cr 3 & \quad l \gets \mathrm{bh}( t.\mathit{left} );\ r \gets \mathrm{bh}( t.\mathit{right} ) \cr 4 & \quad \textbf{if}\ l < 0\ \textbf{or}\ r \ne l\ \textbf{or}\ ( t \text{ is red and has a red child} )\ \textbf{then return}\ -1 \cr 5 & \quad \textbf{if}\ t \text{ is red}\ \textbf{then return}\ l \ \textbf{else return}\ l + 1 \cr 6 & \textbf{function}\ \mathrm{isRedBlackTree}( t ) \cr 7 & \quad \textbf{return}\ \mathrm{bh}( t ) > 0\ \textbf{and}\ ( t = \textbf{nil}\ \textbf{or}\ t \text{ is black} ) \end{array}} \]

◆ isSearchTree()

template<KeyedBinaryNode N, class Compare = std::less<>>
bool ADS::isSearchTree ( N t,
Compare less = {} )
nodiscard

whether the keys of \( t \) are strictly increasing in the invisit, by less; \( O( n ) \) time, \( O( h ) \) space

In pseudocode (section 6.1), each key within the open interval \( ( a , b ) \) that its ancestors leave to it, \( a = -\infty \) and \( b = +\infty \) at the root:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{between}( t , a , b )\quad \triangleright \ \text{every key of } t \text{ strictly inside } ( a , b ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return true} \cr 3 & \quad \textbf{return}\ a < t.\mathit{key} < b\ \textbf{and} \ \mathrm{between}( t.\mathit{left} , a , t.\mathit{key} ) \ \textbf{and}\ \mathrm{between}( t.\mathit{right} , t.\mathit{key} , b ) \end{array}} \]

◆ isSizeBalanced()

template<BinaryNode N>
bool ADS::isSizeBalanced ( N t)
nodiscard

whether every node \( u \) of \( t \) has subtrees whose sizes differ by 1 at most (exercise 6.6 of Morin), by a postvisit that returns the size of each subtree, or stops at the first that is not balanced: \( O( n ) \) time, against the \( \Theta( n \log n ) \) to \( \Theta( n^2 ) \) of a test that computes each size again

In pseudocode (exercise 6.6 of Morin), with a stack of its own in the code since the height may be \( n \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sizeIfBalanced}( t ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return}\ 0 \cr 3 & \quad a \gets \mathrm{sizeIfBalanced}( t.\mathit{left} );\ b \gets \mathrm{sizeIfBalanced}( t.\mathit{right} ) \cr 4 & \quad \textbf{if}\ a = \bot\ \textbf{or}\ b = \bot\ \textbf{or} \ |a - b| > 1\ \textbf{then return}\ \bot \cr 5 & \quad \textbf{return}\ a + b + 1 \end{array}} \]

◆ lessOrEqual()

template<KeyedBinaryNode N, class K, class F, class Compare = std::less<>>
std::size_t ADS::lessOrEqual ( N t,
const K & x,
F && f,
Compare less = {} )

f( u ) on the nodes \( u \) of the binary search tree \( t \) whose keys are not larger than \( x \), in increasing order, and their number \( n' \); \( O( n' + h ) \) time (the getLE(x) of exercise 6.14 of Morin)

In pseudocode (exercise 6.14 of Morin), down the path of \( x \), every left subtree hanging from a node not larger than \( x \) visited whole:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{getLE}( t , x ) \cr 2 & \quad \textbf{while}\ t \ne \textbf{nil}\ \textbf{do} \cr 3 & \qquad \textbf{if}\ t.\mathit{key} \le x\ \textbf{then} \ \mathrm{invisit}( t.\mathit{left} , f );\ f( t );\ t \gets t.\mathit{right} \cr 4 & \qquad \textbf{else}\ t \gets t.\mathit{left} \end{array}} \]

◆ lowestCommonAncestor()

template<ParentBinaryNode N>
N ADS::lowestCommonAncestor ( N u,
N v )

the deepest node that is an ancestor of both \( u \) and \( v \), each node being an ancestor of itself; \( O( h ) \) time, \( O( 1 ) \) space

In pseudocode (section 5.5, beyond the book), \( d \) the depth of a node:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lowestCommonAncestor}( u , v ) \cr 2 & \quad \textbf{while}\ d( u ) > d( v )\ \textbf{do}\ u \gets u.\mathit{parent} \cr 3 & \quad \textbf{while}\ d( v ) > d( u )\ \textbf{do}\ v \gets v.\mathit{parent} \cr 4 & \quad \textbf{while}\ u \ne v\ \textbf{do}\ u \gets u.\mathit{parent};\ v \gets v.\mathit{parent} \cr 5 & \quad \textbf{return}\ u \end{array}} \]

Precondition
\( u \) and \( v \) are nodes of the same tree
Postcondition
an ancestor of \( u \) and of \( v \), none of whose children is one

◆ minimumHeightSearchTree() [1/2]

template<class T, class Compare = std::less<>>
requires std::predicate< Compare & , const T & , const T & >
BinaryTree< T > * ADS::minimumHeightSearchTree ( const std::vector< T > & A,
Compare less = {} )

the tree of minimumHeightSearchTree() above as a BinaryTree, which the caller owns

Precondition
\( A \) strictly increasing by less

◆ minimumHeightSearchTree() [2/2]

template<class T, class Make, class Compare = std::less<>>
auto ADS::minimumHeightSearchTree ( const std::vector< T > & A,
Make make,
Compare less = {} ) -> std::invoke_result_t< Make &, const T &, std::nullptr_t, std::nullptr_t >

the binary search tree of the keys of \( A \) of the least height, the lower middle key at the root of each subtree, whose nodes are made by make( v , l , r ) over the subtrees \( l \) and \( r \), nil if \( A \) is empty; \( O( n ) \) time

In pseudocode (section 6.1), on \( A[ i .. j ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{build}( A , i , j )\quad \triangleright \ \text{a tree of the sorted } A[ i .. j - 1 ] \cr 2 & \quad \textbf{if}\ j \le i\ \textbf{then return nil} \cr 3 & \quad m \gets i + \lfloor ( j - i - 1 ) / 2 \rfloor\quad \triangleright\ \text{the median, the lower one if two} \cr 4 & \quad \textbf{return}\ \mathrm{make}( A[ m ] , \mathrm{build}( A , i , m ) , \mathrm{build}( A , m + 1 , j ) ) \end{array}} \]

Precondition
\( A \) strictly increasing by less
Postcondition
the height is \( \lceil \log_2( n + 1 ) \rceil - 1 \) (an audit)

◆ nextInorder()

template<ParentBinaryNode N>
N ADS::nextInorder ( N u)
nodiscard

the node after \( u \) in the invisit of its tree, nil after the last; \( O( 1 ) \) amortized over a whole visit (exercise 6.8 of Morin)

In pseudocode (exercise 6.8 of Morin, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{nextInorder}( u ) \cr 2 & \quad \textbf{if}\ u.\mathit{right} \ne \textbf{nil}\ \textbf{then return}\ \text{the leftmost node below } u.\mathit{right} \cr 3 & \quad \textbf{while}\ u.\mathit{parent} \ne \textbf{nil}\ \textbf{and} \ u = u.\mathit{parent}.\mathit{right}\ \textbf{do}\ u \gets u.\mathit{parent} \cr 4 & \quad \textbf{return}\ u.\mathit{parent} \end{array}} \]

◆ nextPostorder()

template<ParentBinaryNode N>
N ADS::nextPostorder ( N u)
nodiscard

the node after \( u \) in the postvisit of its tree, nil after the root; \( O( 1 ) \) amortized over a whole visit (exercise 6.8 of Morin)

In pseudocode (exercise 6.8 of Morin, beyond the book), the first node of the postvisit of a subtree reached by going left when possible and right otherwise:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{nextPostorder}( u ) \cr 2 & \quad p \gets u.\mathit{parent} \cr 3 & \quad \textbf{if}\ p = \textbf{nil}\ \textbf{or}\ u = p.\mathit{right}\ \textbf{or}\ p.\mathit{right} = \textbf{nil} \ \textbf{then return}\ p \cr 4 & \quad v \gets p.\mathit{right} \cr 5 & \quad \textbf{while}\ v \text{ has a child}\ \textbf{do}\ v \gets \text{its left child if any, its right one otherwise} \cr 6 & \quad \textbf{return}\ v \end{array}} \]

◆ nextPreorder()

template<ParentBinaryNode N>
N ADS::nextPreorder ( N u)
nodiscard

the node after \( u \) in the previsit of its tree, nil after the last; \( O( 1 ) \) amortized over a whole visit (exercise 6.8 of Morin)

In pseudocode (exercise 6.8 of Morin, beyond the book), by the links to the parents:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{nextPreorder}( u ) \cr 2 & \quad \textbf{if}\ u.\mathit{left} \ne \textbf{nil}\ \textbf{then return}\ u.\mathit{left} \cr 3 & \quad \textbf{if}\ u.\mathit{right} \ne \textbf{nil}\ \textbf{then return}\ u.\mathit{right} \cr 4 & \quad \textbf{while}\ u.\mathit{parent} \ne \textbf{nil}\ \textbf{do} \cr 5 & \qquad p \gets u.\mathit{parent} \cr 6 & \qquad \textbf{if}\ u = p.\mathit{left}\ \textbf{and} \ p.\mathit{right} \ne \textbf{nil}\ \textbf{then return} \ p.\mathit{right} \cr 7 & \qquad u \gets p \cr 8 & \quad \textbf{return nil} \end{array}} \]

◆ orderNumbers()

template<BinaryNode N, class F>
void ADS::orderNumbers ( N t,
F && f )

f( u , pre , in , post ) on each node \( u \) of \( t \) with its numbers in the previsit, in the invisit and in the postvisit, from 0, in one visit: \( O( n ) \) time, \( O( h ) \) space (exercise 6.7 of Morin).

By them \( u \) is an ancestor of \( w \) if and only if \( \mathrm{pre}( u ) \le \mathrm{pre}( w ) \) and \( \mathrm{post}( w ) \le \mathrm{post}( u ) \), and \( \mathrm{post}( u ) - \mathrm{pre}( u ) = n_u - 1 - d( u ) \) (exercise 6.9, see The trees of Open Data Structures)

In pseudocode (exercise 6.7 of Morin), three counters:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{number}( t ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \cr 3 & \quad \mathrm{pre}( t ) \gets a;\ a \gets a + 1 \cr 4 & \quad \mathrm{number}( t.\mathit{left} );\ \mathrm{in}( t ) \gets b;\ b \gets b + 1 \cr 5 & \quad \mathrm{number}( t.\mathit{right} );\ \mathrm{post}( t ) \gets c;\ c \gets c + 1 \end{array}} \]

◆ searchTreeFind()

template<KeyedBinaryNode N, class K, class Compare = std::less<>>
N ADS::searchTreeFind ( N t,
const K & k,
Compare less = {} )
nodiscard

the node of the key \( k \) in the binary search tree \( t \), nil if absent; \( O( h ) \) time

In pseudocode (section 6.1):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( t , k ) \cr 2 & \quad \textbf{while}\ t \ne \textbf{nil}\ \textbf{and}\ k \ne t.\mathit{key}\ \textbf{do} \cr 3 & \qquad \textbf{if}\ k < t.\mathit{key}\ \textbf{then}\ t \gets t.\mathit{left}\ \textbf{else}\ t \gets t.\mathit{right} \cr 4 & \quad \textbf{return}\ t \end{array}} \]

◆ searchTreeFindKey()

template<KeyedBinaryNode N, class K, class Compare = std::less<>>
auto ADS::searchTreeFindKey ( N t,
const K & k,
Compare less = {} )
nodiscard

the address of the key \( k \) in the binary search tree \( t \), nil if absent (the find(x) of a USet of Morin), by searchTreeLowerBound() and one more comparison; \( O( h ) \) time

◆ searchTreeLink()

template<class N, class K, class Compare = std::less<>>
requires KeyedBinaryNode< N * > && std::is_pointer_v< decltype( N::left ) >
N ** ADS::searchTreeLink ( N ** link,
const K & k,
std::vector< N * > & P,
Compare less = {} )
nodiscard

the link at which the key \( k \) is, or would be inserted, in the binary search tree whose root is *link, i.e., the address of the root or of the field left or right of the parent, the nodes passed above it pushed on \( P \) from the root down; the key is there if and only if the node at the link returned is not nil; \( O( h ) \) time

◆ searchTreeLookup()

template<KeyedBinaryNode N, class K, class Compare = std::less<>>
auto ADS::searchTreeLookup ( N t,
const K & k,
Compare less = {} )
nodiscard

the value of the key \( k \) in the binary search tree \( t \), nothing if absent, by searchTreeFind(); \( O( h ) \) time

◆ searchTreeLowerBound()

template<KeyedBinaryNode N, class K, class Compare = std::less<>>
N ADS::searchTreeLowerBound ( N t,
const K & k,
Compare less = {} )
nodiscard

the node of the smallest key not smaller than \( k \) in the binary search tree \( t \), nil if none (the find(x) of an SSet of Morin); \( O( h ) \) time

In pseudocode (section 6.2.2 of Morin), \( b \) the last node passed on the left, the search stopping at \( k \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lowerBound}( t , k ) \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} < k\ \textbf{then}\ t \gets t.\mathit{right} \cr 5 & \qquad \textbf{else if}\ k < 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}} \]

◆ searchTreeLowerBoundEntry()

template<KeyedBinaryNode N, class K, class Compare = std::less<>>
auto ADS::searchTreeLowerBoundEntry ( N t,
const K & k,
Compare less = {} )
nodiscard

the key and the value of the smallest key not smaller than \( k \) in the binary search tree \( t \) as a pair, nothing if none, by searchTreeLowerBound(); \( O( h ) \) time

◆ searchTreeLowerBoundKey()

template<KeyedBinaryNode N, class K, class Compare = std::less<>>
auto ADS::searchTreeLowerBoundKey ( N t,
const K & k,
Compare less = {} )
nodiscard

the address of the smallest key not smaller than \( k \) in the binary search tree \( t \), nil if none, by searchTreeLowerBound(); \( O( h ) \) time

◆ searchTreeRange()

template<KeyedBinaryNode N, class K, class F, class Compare = std::less<>>
std::size_t ADS::searchTreeRange ( N t,
const K & lo,
const K & hi,
F && f,
Compare less = {} )

f( u ) on the nodes \( u \) of the binary search tree \( t \) whose keys are in \( [ \mathit{lo} , \mathit{hi} ] \), in increasing order of the keys; the number of the nodes examined, at most \( k + 2 ( h + 1 ) \) for \( k \) keys in the range; \( O( h + k ) \) time

In pseudocode (section 6.1), an invisit pruned out of the range:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{range}( t , \mathit{lo} , \mathit{hi} )\quad \triangleright\ f \text{ on the keys in } [ \mathit{lo} , \mathit{hi} ] \text{, in order} \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \cr 3 & \quad \textbf{if}\ \mathit{lo} \le t.\mathit{key}\ \textbf{then} \ \mathrm{range}( t.\mathit{left} , \mathit{lo} , \mathit{hi} )\quad \triangleright\ \text{else no key on the left is in range} \cr 4 & \quad \textbf{if}\ \mathit{lo} \le t.\mathit{key} \le \mathit{hi} \ \textbf{then}\ f( t ) \cr 5 & \quad \textbf{if}\ t.\mathit{key} \le \mathit{hi}\ \textbf{then} \ \mathrm{range}( t.\mathit{right} , \mathit{lo} , \mathit{hi} ) \end{array}} \]

Precondition
not less( hi , lo )

◆ searchTreeRank()

template<SizedBinaryNode N, class K, class Compare = std::less<>>
std::size_t ADS::searchTreeRank ( N t,
const K & k,
Compare less = {} )
nodiscard

the number of the keys smaller than \( k \) in the binary search tree \( t \), the sizes of the left subtrees passed on the right along the search of \( k \), plus one for each node so passed; \( O( h ) \) time

◆ searchTreeSelect()

template<SizedBinaryNode N>
N ADS::searchTreeSelect ( N t,
std::size_t i )
nodiscard

the node of rank \( i \) of the binary search tree \( t \), i.e., with \( i \) smaller keys, by the sizes of the left subtrees; \( O( h ) \) time

In pseudocode (exercise 7.10 of Morin), \( s \) the sizes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{select}( t , i ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad \textbf{if}\ i < s( t.\mathit{left} )\ \textbf{then}\ t \gets t.\mathit{left} \cr 4 & \qquad \textbf{else if}\ i = s( t.\mathit{left} )\ \textbf{then return}\ t \cr 5 & \qquad \textbf{else}\ i \gets i - s( t.\mathit{left} ) - 1;\ t \gets t.\mathit{right} \end{array}} \]

Precondition
\( i \) is smaller than the size of \( t \)

◆ searchTreeSplice()

template<class N>
requires BinaryNode< N * > && std::is_pointer_v< decltype( N::left ) >
N * ADS::searchTreeSplice ( N ** link,
std::vector< N * > & P )
nodiscard

the node at link (as given by searchTreeLink()) taken out of its binary search tree: if it has two children, its key and its value are replaced by those of its successor, which is taken out instead, and the nodes down to the successor are pushed on \( P \); the node taken out is returned, to be freed, and its parent (the last node of \( P \)) links its only child; \( O( h ) \) time

In pseudocode (section 6.2.3 of Morin), \( u \) the node at the link:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{splice}( u ) \cr 2 & \quad \textbf{if}\ u.\mathit{left} \ne \textbf{nil}\ \textbf{and} \ u.\mathit{right} \ne \textbf{nil}\ \textbf{then} \cr 3 & \qquad w \gets \text{the leftmost node of } u.\mathit{right}; \ u.\mathit{key} \gets w.\mathit{key};\ u \gets w \cr 4 & \quad \text{the link to } u \gets \text{its only child, or} \ \textbf{nil} \cr 5 & \quad \textbf{return}\ u \end{array}} \]

Precondition
the node at link is not nil

◆ shapeCode()

template<BinaryNode N>
std::vector< bool > ADS::shapeCode ( N t)
nodiscard

the shape of \( t \) in \( 2 ( n - 1 ) \) bits, none for \( n \le 1 \) (exercise 6.11 of Morin): for each node in the previsit but the last, which is a leaf, whether it has a left and whether it has a right child; \( O( n ) \) time.

The \( C_n \) shapes of \( n \) nodes need \( \log_2 C_n = 2 n - O( \log n ) \) bits, so the code is within \( O( \log n ) \) bits of the least

◆ subtreeHeight()

template<ParentBinaryNode N>
long ADS::subtreeHeight ( N u)
nodiscard

the height of the subtree of \( u \), \( -1 \) for nil, by the walk of subtreeSize() that keeps the depth below \( u \): \( O( n_u ) \) time and \( O( 1 ) \) space (the height2(u) of exercise 6.5 of Morin)

◆ subtreeSize()

template<ParentBinaryNode N>
std::size_t ADS::subtreeSize ( N u)
nodiscard

the number of nodes of the subtree of \( u \), by a walk along the links to the parents that never leaves the subtree, with no recursion and no stack: \( O( n_u ) \) time and \( O( 1 ) \) space (the size2(u) of exercise 6.4 of Morin)

In pseudocode (section 6.1.2 and exercise 6.4 of Morin), \( p \) the node the walk comes from, the walk entering a node from its parent, from its left child or from its right child:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{size2}( u ) \cr 2 & \quad r \gets u;\ p \gets r.\mathit{parent};\ s \gets 0 \cr 3 & \quad \textbf{while}\ u \ne r.\mathit{parent}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ p = u.\mathit{parent}\ \textbf{then}\ s \gets s + 1;\ \textit{next} \gets u.\mathit{left}, \text{ or } u.\mathit{right}, \text{ or } u.\mathit{parent} \cr 5 & \qquad \textbf{else if}\ p = u.\mathit{left}\ \textbf{then} \ \mathit{next} \gets u.\mathit{right}, \text{ or } u.\mathit{parent} \cr 6 & \qquad \textbf{else}\ \mathit{next} \gets u.\mathit{parent} \cr 7 & \qquad p \gets u;\ u \gets \mathit{next} \cr 8 & \quad \textbf{return}\ s \end{array}} \]

where "or" takes the first that is not nil

◆ treeHeight()

template<BinaryNode N>
long ADS::treeHeight ( N t)
nodiscard

the height of the tree \( t \), \( -1 \) if empty, by levels with no recursion: \( O( n ) \) time and \( O( w ) \) space for a tree of width \( w \), on a degenerate tree of any height