67#ifndef __ADS_BinaryTree
68 #define __ADS_BinaryTree
127 [[nodiscard]]
const T &
read(
void )
const;
200 [[nodiscard]]
bool linked(
void )
const;
203 [[nodiscard]]
const BinaryTree * top(
void )
const;
232template<
class T ,
class Pre ,
class In ,
class Post >
236template<
class T ,
class F >
241template<
class T ,
class F >
245template<
class T ,
class F >
377 static constexpr std::nullptr_t
none =
nullptr;
379 static N * next(
const void * , N * n , N * root ) {
383 return( n->right() );
384 for( ; n != root ; n = n->parent() )
385 if( ( n == n->parent()->left() ) && n->parent()->right() )
386 return( n->parent()->right() );
394 static constexpr std::nullptr_t none =
nullptr;
396 static N * leftmost( N * n ) {
402 static N * next(
const void * , N * n , N * root ) {
404 return( leftmost( n->right() ) );
405 for( ; n != root ; n = n->parent() )
406 if( n == n->parent()->left() )
407 return( n->parent() );
413struct BinaryPostorder
415 static constexpr std::nullptr_t none =
nullptr;
417 static N * firstLeaf( N * n ) {
418 while( n->left() || n->right() )
419 n = n->left() ? n->left() : n->right();
423 static N * next(
const void * , N * n , N * root ) {
427 if( ( n == p->left() ) && p->right() )
428 return( firstLeaf( p->right() ) );
460 return( std::ranges::subrange( It(
nullptr , t , t ) , It() ) );
485 return( std::ranges::subrange(
486 It(
nullptr , detail::BinaryInorder::leftmost( t ) , t ) , It() ) );
510 return( std::ranges::subrange(
511 It(
nullptr , detail::BinaryPostorder::firstLeaf( t ) , t ) , It() ) );
522#include "ADS/tree/impl/BinaryTree.tpp"
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
The ordered TREE of the book, realized with pointers to the parent, to the first child and to the nex...
a binary tree with pointers to the parent and to the two children
Definition BinaryTree.h:104
BinaryTree * left(void) const
the left child; nil if there is none
BinaryTree * parent(void) const
the parent; nil if the node is a root
void insertRight(BinaryTree *t)
inserts the tree as the right child, which must be missing, as insertLeft() does on the left
BinaryTree * detachLeft(void)
detaches the left subtree without destroying it: it becomes a root, owned by the caller; nil if there...
~BinaryTree()
destroys the two subtrees, i.e., the whole tree, with a stack of its own rather than by recursion
T Item
the type of the values
Definition BinaryTree.h:108
void deleteLeft(void)
destroys the left subtree
void write(const T &v)
writes in the node
const T & read(void) const
the value of the node
void deleteRight(void)
destroys the right subtree
BinaryTree & operator=(const BinaryTree &)=delete
no copy, since a tree owns its subtrees [see copy()]
BinaryTree * detachRight(void)
detaches the right subtree without destroying it: it becomes a root, owned by the caller; nil if ther...
void insertLeft(BinaryTree *t)
inserts the tree as the left child, which must be missing
BinaryTree * right(void) const
the right child; nil if there is none
BinaryTree(const BinaryTree &)=delete
no copy, since a tree owns its subtrees [see copy()]
BinaryTree(const T &v)
a new tree, made of one node containing (the Tree( v ) of the book)
an ordered tree, with pointers to parent, first child and next sibling
Definition Tree.h:79
a forward iterator on the nodes H (pointers or indices) of the owner O, from a first node,...
Definition Iterators.h:1010
@ none
Definition Capabilities.h:553
void dfs(const G &g, int u, std::vector< bool > &visited, Pre &&pre, Edge &&edge, Post &&post)
the visit in depth from , calling pre( u ) in the previsit, edge( u , v ) on each edge and post( u ) ...
BinaryTree< T > * join(const T &v, const BinaryTree< T > *l, const BinaryTree< T > *r)
a new root of value with copies of and , each possibly nil, as its children; the caller owns it
void pre_visit(BinaryTree< T > *t, F &&f)
the previsit of a binary tree, dfs() with f as pre alone
BinaryTree< T > * from_list(std::string_view s)
the binary tree of the parenthesized list , nil for (); the caller owns it
BinaryTree< T > * to_binary(const Tree< T > *t)
the binary tree that represents the ordered tree (figure 5.9): the left child of a node is its first...
auto postorder(BinaryTree< T > *t)
the nodes of the subtree of in postorder, last, by the links to the parents
Definition BinaryTree.h:507
BinaryTree< T > * copy(const BinaryTree< T > *t)
a deep copy of the subtree , nil if is nil; the caller owns it
long dary_parent(long i, long d)
the place of the parent of the node in the place
std::string to_list(const BinaryTree< T > *t)
the parenthesized list of , () if is nil
long dary_child(long i, long d, long j)
the place of the -th child, , of the node in the place of a complete -ary tree stored in a vector
void in_visit(BinaryTree< T > *t, F &&f)
the invisit (symmetric visit) of a binary tree, dfs() with f as in alone
void post_visit(BinaryTree< T > *t, F &&f)
the postvisit of a binary tree, dfs() with f as post alone
auto inorder(BinaryTree< T > *t)
the nodes of the subtree of in symmetric order, by the links to the parents
Definition BinaryTree.h:482
auto preorder(BinaryTree< T > *t)
the nodes of the subtree of in preorder, first, by the links to the parents
Definition BinaryTree.h:457