128#ifndef __ADS_BinaryTreeAlgorithms
129 #define __ADS_BinaryTreeAlgorithms
148#include <type_traits>
169auto nodeLeft( N * n )
171 if constexpr(
requires { n->left(); } )
173 else if constexpr(
requires { n->left.get(); } )
174 return( n->left.get() );
182auto nodeRight( N * n )
184 if constexpr(
requires { n->right(); } )
185 return( n->right() );
186 else if constexpr(
requires { n->right.get(); } )
187 return( n->right.get() );
194auto nodeParent( N * n )
196 if constexpr(
requires { n->parent(); } )
197 return( n->parent() );
204decltype( auto ) nodeKey( N * n )
206 if constexpr(
requires { n->read(); } )
209 return( ( n->key ) );
214decltype( auto ) nodeValue( N * n )
216 return( ( n->value ) );
222concept SizedByS =
requires( N n )
223{ std::size_t( n->s ); };
228concept SizedBySize =
requires( N n )
229{ std::size_t( n->size ); };
233concept SizedNode = SizedByS< N > || SizedBySize< N >;
236template< SizedNode N >
237std::size_t nodeSize( N n )
241 if constexpr( SizedByS< N > )
252concept BinaryNode = std::is_pointer_v< N > &&
requires( N n )
254 detail::nodeLeft( n );
255 detail::nodeRight( n );
262{ detail::nodeParent( n ); };
268{ detail::nodeKey( n ); };
299template< ParentBinaryNode N >
325 class Equal = std::equal_to<> >
345template< KeyedBinaryNode N ,
class Compare = std::less<> >
373template< BinaryNode N ,
class Red >
403template< KeyedBinaryNode N ,
class K ,
class F ,
class Compare = std::less<> >
421template< KeyedBinaryNode N ,
class K ,
class Compare = std::less<> >
426template< KeyedBinaryNode N ,
class K ,
class Compare = std::less<> >
448template< KeyedBinaryNode N ,
class K ,
class Compare = std::less<> >
454template< KeyedBinaryNode N ,
class K ,
class Compare = std::less<> >
460template< KeyedBinaryNode N ,
class K ,
class Compare = std::less<> >
467template< KeyedBinaryNode N ,
class K ,
class Compare = std::less<> >
489template< SizedBinaryNode N >
496template< SizedBinaryNode N ,
class K ,
class Compare = std::less<> >
505template<
class N ,
class K ,
class Compare = std::less<> >
506 requires KeyedBinaryNode< N * > && std::is_pointer_v<
decltype( N::left ) >
508 std::vector< N * > & P ,
533 requires BinaryNode< N * > && std::is_pointer_v<
decltype( N::left ) >
562template< ParentBinaryNode N >
580template< ParentBinaryNode N >
602template< ParentBinaryNode N >
608template< BinaryNode N >
616template< BinaryNode N >
622template< BinaryNode N >
650template< ParentBinaryNode N >
657template< ParentBinaryNode N >
679template< BinaryNode N >
703template< BinaryNode N ,
class F >
712template< BinaryNode N >
755template< KeyedBinaryNode N ,
class K ,
class F ,
class Compare = std::less<> >
756std::size_t
lessOrEqual( N t ,
const K & x , F && f , Compare less = {} );
782template<
class T ,
class Make ,
class Compare = std::less<> >
785 -> std::invoke_result_t< Make & ,
const T & , std::nullptr_t ,
791template<
class T ,
class Compare = std::less<> >
792 requires std::predicate< Compare & , const T & , const T & >
823 const std::vector< T > & in );
846template<
class T ,
class Compare = std::less< T > >
858#include "ADS/tree/impl/BinaryTreeAlgorithms.tpp"
The binary TREE of the book: an ordered tree where each node has at most two children,...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The range minimum query, beyond the book: given an array of items of an ordered universe,...
a binary tree with pointers to the parent and to the two children
Definition BinaryTree.h:104
a node of a binary tree: a pointer to a struct whose children are read by left() and right() or by th...
Definition BinaryTreeAlgorithms.h:252
a node of a binary tree with a key
Definition BinaryTreeAlgorithms.h:266
a node of a binary tree with the link to its parent
Definition BinaryTreeAlgorithms.h:260
a node of a binary tree with a key and the size of its subtree
Definition BinaryTreeAlgorithms.h:272
bool equalTrees(N t, M s, Equal eq={})
whether and have the same shape and eq( a , b ) holds on the keys and of each two nodes in the sa...
BinaryTree< T > * cartesianBinaryTree(const std::vector< T > &A, Compare less={})
the Cartesian tree of , the leftmost minimum by less at the root, a heap whose invisit is ,...
N searchTreeFind(N t, const K &k, Compare less={})
the node of the key in the binary search tree , nil if absent; time
auto 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 of the least height, the lower middle key at the root of each ...
N ** searchTreeLink(N **link, const K &k, std::vector< N * > &P, Compare less={})
the link at which the key is, or would be inserted, in the binary search tree whose root is *link,...
bool isSizeBalanced(N t)
whether every node of has subtrees whose sizes differ by 1 at most (exercise 6.6 of Morin),...
auto searchTreeLowerBoundKey(N t, const K &k, Compare less={})
the address of the smallest key not smaller than in the binary search tree , nil if none,...
auto searchTreeLowerBoundEntry(N t, const K &k, Compare less={})
the key and the value of the smallest key not smaller than in the binary search tree as a pair,...
long treeHeight(N t)
the height of the tree , if empty, by levels with no recursion: time and space for a tree of width...
BinaryTree< T > * 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; time; the call...
auto searchTreeLookup(N t, const K &k, Compare less={})
the value of the key in the binary search tree , nothing if absent, by searchTreeFind(); time
std::size_t subtreeSize(N u)
the number of nodes of the subtree of , by a walk along the links to the parents that never leaves th...
N nextInorder(N u)
the node after in the invisit of its tree, nil after the last; amortized over a whole visit (exerci...
N nextPostorder(N u)
the node after in the postvisit of its tree, nil after the root; amortized over a whole visit (exer...
N nextPreorder(N u)
the node after in the previsit of its tree, nil after the last; amortized over a whole visit (exerc...
N searchTreeSelect(N t, std::size_t i)
the node of rank of the binary search tree , i.e., with smaller keys, by the sizes of the left subt...
N firstInorder(N u)
the first node of the invisit of the subtree of , its leftmost node, from which nextInorder() starts;...
std::vector< bool > shapeCode(N t)
the shape of in bits, none for (exercise 6.11 of Morin): for each node in the previsit but the las...
N firstPostorder(N u)
the first node of the postvisit of the subtree of , reached by going left when possible and right oth...
std::size_t searchTreeRank(N t, const K &k, Compare less={})
the number of the keys smaller than in the binary search tree , the sizes of the left subtrees passe...
void orderNumbers(N t, F &&f)
f( u , pre , in , post ) on each node of with its numbers in the previsit, in the invisit and in th...
bool isRedBlackTree(N t, Red red)
whether the coloring of , red( u ) telling whether the node is red, respects the constraints of a Re...
long subtreeHeight(N u)
the height of the subtree of , for nil, by the walk of subtreeSize() that keeps the depth below : t...
N * 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 ch...
N searchTreeLowerBound(N t, const K &k, Compare less={})
the node of the smallest key not smaller than in the binary search tree , nil if none (the find(x) o...
auto searchTreeFindKey(N t, const K &k, Compare less={})
the address of the key in the binary search tree , nil if absent (the find(x) of a USet of Morin),...
bool isSearchTree(N t, Compare less={})
whether the keys of are strictly increasing in the invisit, by less; time, space
N lowestCommonAncestor(N u, N v)
the deepest node that is an ancestor of both and , each node being an ancestor of itself; time,...
std::size_t lessOrEqual(N t, const K &x, F &&f, Compare less={})
f( u ) on the nodes of the binary search tree whose keys are not larger than , in increasing order,...
BinaryTree< std::size_t > * 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 i...
std::size_t searchTreeRange(N t, const K &lo, const K &hi, F &&f, Compare less={})
f( u ) on the nodes of the binary search tree whose keys are in , in increasing order of the keys; ...