ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BinaryTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File BinaryTree.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
63/*--------------------------------------------------------------------------*/
64/*----------------------------- DEFINITIONS --------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#ifndef __ADS_BinaryTree
68 #define __ADS_BinaryTree
69/* self-identification: #endif constant */
70
71/*--------------------------------------------------------------------------*/
72/*------------------------------ INCLUDES ----------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#include "ADS/Contract.h"
76#include "ADS/Iterators.h"
77#include "ADS/tree/Tree.h"
78
79#include <cstddef>
80#include <limits>
81#include <memory>
82#include <ranges>
83#include <sstream>
84#include <stdexcept>
85#include <string>
86#include <string_view>
87#include <utility>
88#include <vector>
89
90/*--------------------------------------------------------------------------*/
91/*--------------------------- NAMESPACE ------------------------------------*/
92/*--------------------------------------------------------------------------*/
93
94namespace ADS
95{
96
97/*--------------------------------------------------------------------------*/
98/*-------------------------- CLASS BinaryTree ------------------------------*/
99/*--------------------------------------------------------------------------*/
100
102template< class T >
104{
105
106/*--------------------------------------------------------------------------*/
107 public:
108 using Item = T;
109
112 explicit BinaryTree( const T & v );
113
115 BinaryTree( const BinaryTree & ) = delete;
116
118 BinaryTree & operator=( const BinaryTree & ) = delete;
119
123
124/*--------------------------------------------------------------------------*/
125
127 [[nodiscard]] const T & read( void ) const;
128
130 void write( const T & v );
131
133 [[nodiscard]] BinaryTree * parent( void ) const;
134
136 [[nodiscard]] BinaryTree * left( void ) const;
137
139 [[nodiscard]] BinaryTree * right( void ) const;
140
154
161
163 void deleteLeft( void );
164
166 void deleteRight( void );
167
182 [[nodiscard]] BinaryTree * detachLeft( void );
183
188 [[nodiscard]] BinaryTree * detachRight( void );
189
190/*--------------------------------------------------------------------------*/
191 private:
193 void insert( BinaryTree *& child , BinaryTree * t );
194
196 BinaryTree * detach( BinaryTree *& child );
197
200 [[nodiscard]] bool linked( void ) const;
201
203 [[nodiscard]] const BinaryTree * top( void ) const;
204
205 T f_value;
206 BinaryTree * f_parent = nullptr;
207 BinaryTree * f_left = nullptr;
208 BinaryTree * f_right = nullptr;
209
210 }; // end( class BinaryTree )
211
212/*--------------------------------------------------------------------------*/
213/*------------------------------- VISITS -----------------------------------*/
214/*--------------------------------------------------------------------------*/
215
232template< class T , class Pre , class In , class Post >
233void dfs( BinaryTree< T > * t , Pre && pre , In && in , Post && post );
234
236template< class T , class F >
237void pre_visit( BinaryTree< T > * t , F && f );
238
241template< class T , class F >
242void in_visit( BinaryTree< T > * t , F && f );
243
245template< class T , class F >
246void post_visit( BinaryTree< T > * t , F && f );
247
263template< class T >
265
280template< class T >
281BinaryTree< T > * join( const T & v , const BinaryTree< T > * l ,
282 const BinaryTree< T > * r );
283
284/*--------------------------------------------------------------------------*/
285/*--------------------------- REPRESENTATIONS ------------------------------*/
286/*--------------------------------------------------------------------------*/
287
302template< class T >
303std::string to_list( const BinaryTree< T > * t );
304
326template< class T >
327BinaryTree< T > * from_list( std::string_view s );
328
347template< class T >
349
355inline long dary_child( long i , long d , long j );
356
360inline long dary_parent( long i , long d );
361
362/*--------------------------------------------------------------------------*/
363
364/*--------------------------------------------------------------------------*/
365/*------------------------- THE STANDARD INTERFACE -------------------------*/
366/*--------------------------------------------------------------------------*/
367
368// the nodes of a binary tree as ranges, forward, by the links to the
369// parents, with no stack; *it is a node
370
371namespace detail
372{
373
375struct BinaryPreorder
376{
377 static constexpr std::nullptr_t none = nullptr;
378 template< class N >
379 static N * next( const void * , N * n , N * root ) {
380 if( n->left() )
381 return( n->left() );
382 if( n->right() )
383 return( n->right() );
384 for( ; n != root ; n = n->parent() ) // up to a right sibling
385 if( ( n == n->parent()->left() ) && n->parent()->right() )
386 return( n->parent()->right() );
387 return( nullptr );
388 }
389 };
390
392struct BinaryInorder
393{
394 static constexpr std::nullptr_t none = nullptr;
395 template< class N >
396 static N * leftmost( N * n ) {
397 while( n->left() )
398 n = n->left();
399 return( n );
400 }
401 template< class N >
402 static N * next( const void * , N * n , N * root ) {
403 if( n->right() )
404 return( leftmost( n->right() ) );
405 for( ; n != root ; n = n->parent() ) // up from a left child
406 if( n == n->parent()->left() )
407 return( n->parent() );
408 return( nullptr );
409 }
410 };
411
413struct BinaryPostorder
414{
415 static constexpr std::nullptr_t none = nullptr;
416 template< class N >
417 static N * firstLeaf( N * n ) {
418 while( n->left() || n->right() )
419 n = n->left() ? n->left() : n->right();
420 return( n );
421 }
422 template< class N >
423 static N * next( const void * , N * n , N * root ) {
424 if( n == root )
425 return( nullptr );
426 N * p = n->parent();
427 if( ( n == p->left() ) && p->right() ) // down the right sibling
428 return( firstLeaf( p->right() ) );
429 return( p );
430 }
431 };
432
433} // end( namespace detail )
434
456template< class T >
458{
459 using It = WalkIterator< void , BinaryTree< T > * , detail::BinaryPreorder >;
460 return( std::ranges::subrange( It( nullptr , t , t ) , It() ) );
461 }
462
481template< class T >
483{
484 using It = WalkIterator< void , BinaryTree< T > * , detail::BinaryInorder >;
485 return( std::ranges::subrange(
486 It( nullptr , detail::BinaryInorder::leftmost( t ) , t ) , It() ) );
487 }
488
506template< class T >
508{
509 using It = WalkIterator< void , BinaryTree< T > * , detail::BinaryPostorder >;
510 return( std::ranges::subrange(
511 It( nullptr , detail::BinaryPostorder::firstLeaf( t ) , t ) , It() ) );
512 }
513
514/*--------------------------------------------------------------------------*/
515
516} // end( namespace ADS )
517
518/*--------------------------------------------------------------------------*/
519/*-------------------------- IMPLEMENTATION --------------------------------*/
520/*--------------------------------------------------------------------------*/
521
522#include "ADS/tree/impl/BinaryTree.tpp"
523
524/*--------------------------------------------------------------------------*/
525
526#endif /* BinaryTree.h included */
527
528/*--------------------------------------------------------------------------*/
529/*------------------------- End File BinaryTree.h --------------------------*/
530/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96