ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Tree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Tree.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
40/*--------------------------------------------------------------------------*/
41/*----------------------------- DEFINITIONS --------------------------------*/
42/*--------------------------------------------------------------------------*/
43
44#ifndef __ADS_Tree
45 #define __ADS_Tree
46/* self-identification: #endif constant */
47
48/*--------------------------------------------------------------------------*/
49/*------------------------------ INCLUDES ----------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52#include "ADS/Contract.h"
53#include "ADS/Iterators.h"
54#include "ADS/sequence/List.h"
55
56#include <cstddef>
57#include <memory>
58#include <ranges>
59#include <sstream>
60#include <stdexcept>
61#include <string>
62#include <string_view>
63#include <vector>
64
65/*--------------------------------------------------------------------------*/
66/*--------------------------- NAMESPACE ------------------------------------*/
67/*--------------------------------------------------------------------------*/
68
69namespace ADS
70{
71
72/*--------------------------------------------------------------------------*/
73/*----------------------------- CLASS Tree ---------------------------------*/
74/*--------------------------------------------------------------------------*/
75
77template< class T >
78class Tree
79{
80
81/*--------------------------------------------------------------------------*/
82 public:
83 using Item = T;
84
87 explicit Tree( const T & v );
88
90 Tree( const Tree & ) = delete;
91
93 Tree & operator=( const Tree & ) = delete;
94
110
111/*--------------------------------------------------------------------------*/
112
114 [[nodiscard]] const T & read( void ) const;
115
117 void write( const T & v );
118
120 [[nodiscard]] Tree * parent( void ) const;
121
123 [[nodiscard]] Tree * leftmostChild( void ) const;
124
126 [[nodiscard]] Tree * rightSibling( void ) const;
127
142 void insertChild( Tree * t );
143
158 void insertSibling( Tree * t );
159
161 [[nodiscard]] bool isLeaf( void ) const;
162
164 [[nodiscard]] bool isLastSibling( void ) const;
165
168 void deleteChild( void );
169
172 void deleteSibling( void );
173
174/*--------------------------------------------------------------------------*/
175 private:
178 [[nodiscard]] bool linked( void ) const;
179
181 [[nodiscard]] const Tree * top( void ) const;
182
183 T f_value;
184 Tree * f_parent = nullptr;
185 Tree * f_child = nullptr;
186 Tree * f_sibling = nullptr;
187
188 }; // end( class Tree )
189
190/*--------------------------------------------------------------------------*/
191/*------------------------------- VISITS -----------------------------------*/
192/*--------------------------------------------------------------------------*/
193
213template< class T , class Pre , class Post >
214void dfs( Tree< T > * t , Pre && pre , Post && post );
215
226template< class T , class F >
227void pre_visit( Tree< T > * t , F && f );
228
239template< class T , class F >
240void post_visit( Tree< T > * t , F && f );
241
260template< class T , class F >
261void in_visit( Tree< T > * t , int i , F && f );
262
286template< class T >
287Tree< T > * copy( const Tree< T > * t );
288
303template< class T , class F >
304void bfs( Tree< T > * t , F && f );
305
326template< class T >
328
344template< class T >
345std::string to_list( const Tree< T > * t );
346
365template< class T >
366Tree< T > * tree_from_list( std::string_view s );
367
368/*--------------------------------------------------------------------------*/
369
370/*--------------------------------------------------------------------------*/
371/*------------------------- THE STANDARD INTERFACE -------------------------*/
372/*--------------------------------------------------------------------------*/
373
374// the nodes of a tree as ranges, forward, by the links to the parents and
375// the siblings, with no stack; *it is a node
376
377namespace detail
378{
379
381struct TreeChildren
382{
383 static constexpr std::nullptr_t none = nullptr;
384 template< class N >
385 static N * next( const void * , N * n , N * ) {
386 return( n->rightSibling() );
387 }
388 };
389
391struct TreePreorder
392{
393 static constexpr std::nullptr_t none = nullptr;
394 template< class N >
395 static N * next( const void * , N * n , N * root ) {
396 if( n->leftmostChild() )
397 return( n->leftmostChild() );
398 for( ; n != root ; n = n->parent() ) // up to a right sibling
399 if( n->rightSibling() )
400 return( n->rightSibling() );
401 return( nullptr );
402 }
403 };
404
405} // end( namespace detail )
406
408template< class T >
410{
411 using It = WalkIterator< void , Tree< T > * , detail::TreeChildren >;
412 return(
413 std::ranges::subrange( It( nullptr , t->leftmostChild() , t ) , It() ) );
414 }
415
432template< class T >
434{
435 using It = WalkIterator< void , Tree< T > * , detail::TreePreorder >;
436 return( std::ranges::subrange( It( nullptr , t , t ) , It() ) );
437 }
438
439/*--------------------------------------------------------------------------*/
440
441} // end( namespace ADS )
442
443/*--------------------------------------------------------------------------*/
444/*-------------------------- IMPLEMENTATION --------------------------------*/
445/*--------------------------------------------------------------------------*/
446
447#include "ADS/tree/impl/Tree.tpp"
448
449/*--------------------------------------------------------------------------*/
450
451#endif /* Tree.h included */
452
453/*--------------------------------------------------------------------------*/
454/*---------------------------- End File Tree.h -----------------------------*/
455/*--------------------------------------------------------------------------*/
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 LIST of the book: a SEQUENCE [see ADT.h] realized as a bidirectional circular list of cells with ...
an ordered tree, with pointers to parent, first child and next sibling
Definition Tree.h:79
void insertSibling(Tree *t)
inserts the tree as the next sibling of this node, which must not be a root; the parent of this node...
Tree * rightSibling(void) const
the next sibling; nil if there is none
Tree * leftmostChild(void) const
the first child; nil if the node is a leaf
void deleteSibling(void)
destroys the subtree rooted in the next sibling, whose next sibling takes its place
void write(const T &v)
writes in the node
void insertChild(Tree *t)
inserts the tree as the first child of this node, which then owns it, in
const T & read(void) const
the value of the node
Tree * parent(void) const
the parent; nil if the node is a root
void deleteChild(void)
destroys the subtree rooted in the first child, whose next sibling becomes the first child
bool isLastSibling(void) const
true if the node has no next sibling
Tree & operator=(const Tree &)=delete
no copy, since a tree owns its subtrees [see copy()]
T Item
the type of the values
Definition Tree.h:83
Tree(const Tree &)=delete
no copy, since a tree owns its subtrees [see copy()]
Tree(const T &v)
a new tree, made of one node containing (the Tree( v ) of the book)
bool isLeaf(void) const
true if the node has no child, i.e., it is a leaf
~Tree()
destroys the subtrees of the children, i.e., the whole tree, with a stack of its own rather than by r...
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 ) ...
void bfs(const G &g, int r, FN &&node, FE &&edge)
the visit in breadth from , calling node( u ) when is examined and edge( u , v ) when is examined
void pre_visit(BinaryTree< T > *t, F &&f)
the previsit of a binary tree, dfs() with f as pre alone
BinaryTree< T > * copy(const BinaryTree< T > *t)
a deep copy of the subtree , nil if is nil; the caller owns it
std::string to_list(const BinaryTree< T > *t)
the parenthesized list of , () if is nil
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 preorder(BinaryTree< T > *t)
the nodes of the subtree of in preorder, first, by the links to the parents
Definition BinaryTree.h:457
int maxWeightLevel(Tree< T > *t)
the first level of largest weight, the weight of a level being the sum of the values of its nodes (th...
auto children(Tree< T > *t)
the children of , from the leftmost, as a range
Definition Tree.h:409
Tree< T > * tree_from_list(std::string_view s)
the tree of the parenthesized list , its values read with >>; the caller owns it
Definition ADT.h:96