ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BinaryTreeAlgorithms.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------- File BinaryTreeAlgorithms.h -----------------------*/
3/*--------------------------------------------------------------------------*/
124/*--------------------------------------------------------------------------*/
125/*----------------------------- DEFINITIONS --------------------------------*/
126/*--------------------------------------------------------------------------*/
127
128#ifndef __ADS_BinaryTreeAlgorithms
129 #define __ADS_BinaryTreeAlgorithms
130/* self-identification: #endif constant */
131
132/*--------------------------------------------------------------------------*/
133/*------------------------------ INCLUDES ----------------------------------*/
134/*--------------------------------------------------------------------------*/
135
136#include "ADS/Contract.h"
138#include "ADS/tree/BinaryTree.h"
139
140#include <algorithm>
141#include <bit>
142#include <concepts>
143#include <cstddef>
144#include <cstdint>
145#include <functional>
146#include <memory>
147#include <optional>
148#include <type_traits>
149#include <utility>
150#include <vector>
151
152/*--------------------------------------------------------------------------*/
153/*--------------------------- NAMESPACE ------------------------------------*/
154/*--------------------------------------------------------------------------*/
155
156namespace ADS
157{
158
159/*--------------------------------------------------------------------------*/
160/*---------------------------- THE NODES -----------------------------------*/
161/*--------------------------------------------------------------------------*/
162
163namespace detail
164{
165
168template< class N >
169auto nodeLeft( N * n )
170{
171 if constexpr( requires { n->left(); } )
172 return( n->left() );
173 else if constexpr( requires { n->left.get(); } )
174 return( n->left.get() );
175 else
176 return( n->left );
177 }
178
181template< class N >
182auto nodeRight( N * n )
183{
184 if constexpr( requires { n->right(); } )
185 return( n->right() );
186 else if constexpr( requires { n->right.get(); } )
187 return( n->right.get() );
188 else
189 return( n->right );
190 }
191
193template< class N >
194auto nodeParent( N * n )
195{
196 if constexpr( requires { n->parent(); } )
197 return( n->parent() );
198 else
199 return( n->parent );
200 }
201
203template< class N >
204decltype( auto ) nodeKey( N * n )
205{
206 if constexpr( requires { n->read(); } )
207 return( n->read() );
208 else
209 return( ( n->key ) );
210 }
211
213template< class N >
214decltype( auto ) nodeValue( N * n )
215{
216 return( ( n->value ) );
217 }
218
221template< class N >
222concept SizedByS = requires( N n )
223{ std::size_t( n->s ); };
224
227template< class N >
228concept SizedBySize = requires( N n )
229{ std::size_t( n->size ); };
230
232template< class N >
233concept SizedNode = SizedByS< N > || SizedBySize< N >;
234
236template< SizedNode N >
237std::size_t nodeSize( N n )
238{
239 if( ! n )
240 return( 0 );
241 if constexpr( SizedByS< N > )
242 return( n->s );
243 else
244 return( n->size );
245 }
246
247} // end( namespace detail )
248
251template< class N >
252concept BinaryNode = std::is_pointer_v< N > && requires( N n )
253{
254 detail::nodeLeft( n );
255 detail::nodeRight( n );
256 };
257
259template< class N >
261 BinaryNode< N > && requires( N n )
262{ detail::nodeParent( n ); };
263
265template< class N >
267 BinaryNode< N > && requires( N n )
268{ detail::nodeKey( n ); };
269
271template< class N >
272concept SizedBinaryNode = KeyedBinaryNode< N > && detail::SizedNode< N >;
273
274/*--------------------------------------------------------------------------*/
275/*---------------------------- THE ANCESTORS -------------------------------*/
276/*--------------------------------------------------------------------------*/
277
299template< ParentBinaryNode N >
300N lowestCommonAncestor( N u , N v );
301
302/*--------------------------------------------------------------------------*/
303/*--------------------------- THE COMPARISONS ------------------------------*/
304/*--------------------------------------------------------------------------*/
305
324template< KeyedBinaryNode N , KeyedBinaryNode M ,
325 class Equal = std::equal_to<> >
326[[nodiscard]] bool equalTrees( N t , M s , Equal eq = {} );
327
345template< KeyedBinaryNode N , class Compare = std::less<> >
346[[nodiscard]] bool isSearchTree( N t , Compare less = {} );
347
373template< BinaryNode N , class Red >
374[[nodiscard]] bool isRedBlackTree( N t , Red red );
375
376/*--------------------------------------------------------------------------*/
377/*------------------------------ THE SEARCHES ------------------------------*/
378/*--------------------------------------------------------------------------*/
379
403template< KeyedBinaryNode N , class K , class F , class Compare = std::less<> >
404std::size_t searchTreeRange( N t , const K & lo , const K & hi , F && f ,
405 Compare less = {} );
406
421template< KeyedBinaryNode N , class K , class Compare = std::less<> >
422[[nodiscard]] N searchTreeFind( N t , const K & k , Compare less = {} );
423
426template< KeyedBinaryNode N , class K , class Compare = std::less<> >
427[[nodiscard]] auto searchTreeLookup( N t , const K & k , Compare less = {} );
428
448template< KeyedBinaryNode N , class K , class Compare = std::less<> >
449[[nodiscard]] N searchTreeLowerBound( N t , const K & k , Compare less = {} );
450
454template< KeyedBinaryNode N , class K , class Compare = std::less<> >
455[[nodiscard]] auto searchTreeFindKey( N t , const K & k , Compare less = {} );
456
460template< KeyedBinaryNode N , class K , class Compare = std::less<> >
461[[nodiscard]] auto searchTreeLowerBoundKey( N t , const K & k ,
462 Compare less = {} );
463
467template< KeyedBinaryNode N , class K , class Compare = std::less<> >
468[[nodiscard]] auto searchTreeLowerBoundEntry( N t , const K & k ,
469 Compare less = {} );
470
489template< SizedBinaryNode N >
490[[nodiscard]] N searchTreeSelect( N t , std::size_t i );
491
496template< SizedBinaryNode N , class K , class Compare = std::less<> >
497[[nodiscard]] std::size_t searchTreeRank( N t , const K & k ,
498 Compare less = {} );
499
505template< class N , class K , class Compare = std::less<> >
506 requires KeyedBinaryNode< N * > && std::is_pointer_v< decltype( N::left ) >
507[[nodiscard]] N ** searchTreeLink( N ** link , const K & k ,
508 std::vector< N * > & P ,
509 Compare less = {} );
510
532template< class N >
533 requires BinaryNode< N * > && std::is_pointer_v< decltype( N::left ) >
534[[nodiscard]] N * searchTreeSplice( N ** link , std::vector< N * > & P );
535
536/*--------------------------------------------------------------------------*/
537/*--------------------- THE TRAVERSALS WITH NO STACK -----------------------*/
538/*--------------------------------------------------------------------------*/
539
562template< ParentBinaryNode N >
563[[nodiscard]] N nextPreorder( N u );
564
580template< ParentBinaryNode N >
581[[nodiscard]] N nextInorder( N u );
582
602template< ParentBinaryNode N >
603[[nodiscard]] N nextPostorder( N u );
604
608template< BinaryNode N >
609[[nodiscard]] N firstInorder( N u );
610
616template< BinaryNode N >
617[[nodiscard]] N firstPostorder( N u );
618
622template< BinaryNode N >
623[[nodiscard]] long treeHeight( N t );
624
650template< ParentBinaryNode N >
651[[nodiscard]] std::size_t subtreeSize( N u );
652
657template< ParentBinaryNode N >
658[[nodiscard]] long subtreeHeight( N u );
659
679template< BinaryNode N >
680[[nodiscard]] bool isSizeBalanced( N t );
681
703template< BinaryNode N , class F >
704void orderNumbers( N t , F && f );
705
712template< BinaryNode N >
713[[nodiscard]] std::vector< bool > shapeCode( N t );
714
735fromShapeCode( const std::vector< bool > & c , std::size_t n );
736
755template< KeyedBinaryNode N , class K , class F , class Compare = std::less<> >
756std::size_t lessOrEqual( N t , const K & x , F && f , Compare less = {} );
757
758/*--------------------------------------------------------------------------*/
759/*------------------------------ THE BUILDERS ------------------------------*/
760/*--------------------------------------------------------------------------*/
761
782template< class T , class Make , class Compare = std::less<> >
783auto minimumHeightSearchTree( const std::vector< T > & A , Make make ,
784 Compare less = {} )
785 -> std::invoke_result_t< Make & , const T & , std::nullptr_t ,
786 std::nullptr_t >;
787
791template< class T , class Compare = std::less<> >
792 requires std::predicate< Compare & , const T & , const T & >
793BinaryTree< T > * minimumHeightSearchTree( const std::vector< T > & A ,
794 Compare less = {} );
795
821template< class T >
822BinaryTree< T > * fromPreorderInorder( const std::vector< T > & pre ,
823 const std::vector< T > & in );
824
846template< class T , class Compare = std::less< T > >
847BinaryTree< T > * cartesianBinaryTree( const std::vector< T > & A ,
848 Compare less = {} );
849
850/*--------------------------------------------------------------------------*/
851
852} // end( namespace ADS )
853
854/*--------------------------------------------------------------------------*/
855/*-------------------------- IMPLEMENTATION --------------------------------*/
856/*--------------------------------------------------------------------------*/
857
858#include "ADS/tree/impl/BinaryTreeAlgorithms.tpp"
859
860/*--------------------------------------------------------------------------*/
861
862#endif /* BinaryTreeAlgorithms.h included */
863
864/*--------------------------------------------------------------------------*/
865/*-------------------- End File BinaryTreeAlgorithms.h ---------------------*/
866/*--------------------------------------------------------------------------*/
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; ...
Definition ADT.h:96