ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CursorTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File CursorTree.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
28/*--------------------------------------------------------------------------*/
29/*----------------------------- DEFINITIONS --------------------------------*/
30/*--------------------------------------------------------------------------*/
31
32#ifndef __ADS_CursorTree
33 #define __ADS_CursorTree
34/* self-identification: #endif constant */
35
36/*--------------------------------------------------------------------------*/
37/*------------------------------ INCLUDES ----------------------------------*/
38/*--------------------------------------------------------------------------*/
39
40#include "ADS/Contract.h"
41#include "ADS/Iterators.h"
42
43#include <cstddef>
44#include <ranges>
45#include <stdexcept>
46#include <string>
47#include <utility>
48#include <vector>
49
50/*--------------------------------------------------------------------------*/
51/*--------------------------- NAMESPACE ------------------------------------*/
52/*--------------------------------------------------------------------------*/
53
54namespace ADS
55{
56
57/*--------------------------------------------------------------------------*/
58/*---------------------------- CLASS CursorTree ----------------------------*/
59/*--------------------------------------------------------------------------*/
60
62template< class T >
64{
65
66/*--------------------------------------------------------------------------*/
67 public:
68 using Item = T;
69 static constexpr int NIL = -1;
70
72 CursorTree( void ) = default;
73
75 explicit CursorTree( const T & v );
76
78 [[nodiscard]] bool empty( void ) const;
79
81 [[nodiscard]] int root( void ) const;
82
85 [[nodiscard]] int parent( int n ) const;
86
89 [[nodiscard]] int leftmostChild( int n ) const;
90
93 [[nodiscard]] int rightSibling( int n ) const;
94
97 [[nodiscard]] bool isLeaf( int n ) const;
98
101 [[nodiscard]] bool isLastSibling( int n ) const;
102
105 [[nodiscard]] const T & read( int n ) const;
106
109 void write( int n , const T & v );
110
112 [[nodiscard]] int size( void ) const;
113
129 int insertChild( int n , const T & v );
130
135 int insertSibling( int n , const T & v );
136
159 int insertChildTree( int n , const CursorTree & t , int s );
160
165 int insertSiblingTree( int n , const CursorTree & t , int s );
166
185 void remove( int n );
186
188 [[nodiscard]] std::vector< int > previsit( void ) const;
189
191 [[nodiscard]] std::vector< int > postvisit( void ) const;
192
210 [[nodiscard]] std::vector< int > invisit( int i ) const;
211
212/*------------------------- THE STANDARD INTERFACE -------------------------*/
213
215 struct Used
216 {
218 static std::size_t size( const auto & o ) { return( o.f_N.size() ); }
220 static bool occupied( const auto & o , std::size_t i ) {
221 return( o.f_N[ i ].used );
222 }
223
224 static int read( const auto & , std::size_t i ) { return( int( i ) ); }
225 };
226
228 [[nodiscard]] auto nodes( void ) const {
230 return( std::ranges::subrange( It( this , 0 ) , It( this , f_N.size() ) ) );
231 }
232
234 struct Siblings
235 {
236 static constexpr int none = NIL;
238 static int next( const CursorTree * o , int n , int ) {
239 return( o->rightSibling( n ) );
240 }
241 };
242
245 [[nodiscard]] auto children( int n ) const {
247 return( std::ranges::subrange( It( this , leftmostChild( n ) , n ) ,
248 It() ) );
249 }
250
251/*--------------------------------------------------------------------------*/
252 private:
254 struct Node
255 {
256 T value;
257 int parent ,
258 child ,
259 sibling;
260 bool used;
261 };
262
263 const Node & node( int n ) const;
264
265 Node & node( int n );
266
269 [[nodiscard]] bool linked( void ) const;
270
271 int make( const T & v , int p );
272
273 void free_node( int n );
274
275 void release_children( int n );
276
279 int copy( const CursorTree & t , int s , int p );
280
283 void visit( int n , int i , std::vector< int > & P , int kind ) const;
284
285 std::vector< Node > f_N;
286 int f_root = NIL;
287 int f_free = NIL;
288 int f_size = 0;
289
290 }; // end( class CursorTree )
291
292/*--------------------------------------------------------------------------*/
293
294} // end( namespace ADS )
295
296/*--------------------------------------------------------------------------*/
297/*-------------------------- IMPLEMENTATION --------------------------------*/
298/*--------------------------------------------------------------------------*/
299
300#include "ADS/tree/impl/CursorTree.tpp"
301
302/*--------------------------------------------------------------------------*/
303
304#endif /* CursorTree.h included */
305
306/*--------------------------------------------------------------------------*/
307/*------------------------- End File CursorTree.h --------------------------*/
308/*--------------------------------------------------------------------------*/
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...
static constexpr int NIL
no node
Definition CursorTree.h:69
CursorTree(void)=default
the empty tree
CursorTree(const T &v)
a tree of one node of value
int insertSiblingTree(int n, const CursorTree &t, int s)
a copy of the subtree of in as the next sibling of ; may be this tree, copied as it was before the...
auto nodes(void) const
the nodes, in the order of their places, bidirectional
Definition CursorTree.h:228
bool isLeaf(int n) const
true if has no child
int insertSibling(int n, const T &v)
a new next sibling of , not the root, of value , in , as insertChild() with the sibling of in place ...
int size(void) const
the number of nodes
std::vector< int > invisit(int i) const
the nodes in invisit with index , each node after its first subtrees
std::vector< int > previsit(void) const
the nodes in previsit
bool empty(void) const
true if the tree has no node
int rightSibling(int n) const
the next sibling of , NIL if there is none
void remove(int n)
removes the subtree of , detached from its parent, its places back to the free list
auto children(int n) const
the children of , from the leftmost, forward
Definition CursorTree.h:245
int parent(int n) const
the parent of , NIL for the root
int insertChildTree(int n, const CursorTree &t, int s)
a copy of the subtree of in as the first child of ; may be this tree, copied as it was before the ...
void write(int n, const T &v)
writes in
std::vector< int > postvisit(void) const
the nodes in postvisit
T Item
the type of the values
Definition CursorTree.h:68
const T & read(int n) const
the value of
int root(void) const
the place of the root, NIL if the tree is empty
bool isLastSibling(int n) const
whether is the last of its siblings
int insertChild(int n, const T &v)
a new first child of of value , in
int leftmostChild(int n) const
the first child of , NIL for a leaf
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
a forward iterator on the nodes H (pointers or indices) of the owner O, from a first node,...
Definition Iterators.h:1010
Definition ADT.h:96
the walk of the children of a node
Definition CursorTree.h:235
static constexpr int none
the end
Definition CursorTree.h:236
static int next(const CursorTree *o, int n, int)
the next sibling of
Definition CursorTree.h:238
the places in use, for SlotIterator
Definition CursorTree.h:216
static int read(const auto &, std::size_t i)
the node of the place , the place itself
Definition CursorTree.h:224
static bool occupied(const auto &o, std::size_t i)
whether the place holds a node
Definition CursorTree.h:220
static std::size_t size(const auto &o)
the places
Definition CursorTree.h:218