ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ChildListTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File ChildListTree.h --------------------------*/
3/*--------------------------------------------------------------------------*/
35/*--------------------------------------------------------------------------*/
36/*----------------------------- DEFINITIONS --------------------------------*/
37/*--------------------------------------------------------------------------*/
38
39#ifndef __ADS_ChildListTree
40 #define __ADS_ChildListTree
41/* self-identification: #endif constant */
42
43/*--------------------------------------------------------------------------*/
44/*------------------------------ INCLUDES ----------------------------------*/
45/*--------------------------------------------------------------------------*/
46
47#include "ADS/Contract.h"
48#include "ADS/sequence/List.h"
49
50#include <utility>
51#include <vector>
52
53/*--------------------------------------------------------------------------*/
54/*--------------------------- NAMESPACE ------------------------------------*/
55/*--------------------------------------------------------------------------*/
56
57namespace ADS
58{
59
60/*--------------------------------------------------------------------------*/
61/*------------------------- CLASS ChildListTree ----------------------------*/
62/*--------------------------------------------------------------------------*/
63
65template< class T >
67{
68
69/*--------------------------------------------------------------------------*/
70 public:
71 using Item = T;
72 static constexpr int NIL = -1;
73
75 ChildListTree( void ) = default;
76
78 explicit ChildListTree( const T & v );
79
83
85 ChildListTree( ChildListTree && o ) noexcept = default;
86
89
91 ChildListTree & operator=( ChildListTree && o ) noexcept = default;
92
94 [[nodiscard]] bool empty( void ) const;
95
97 [[nodiscard]] int root( void ) const;
98
101 [[nodiscard]] int parent( int n ) const;
102
105 [[nodiscard]] int leftmostChild( int n ) const;
106
110 [[nodiscard]] int rightSibling( int n ) const;
111
114 [[nodiscard]] bool isLeaf( int n ) const;
115
118 [[nodiscard]] bool isLastSibling( int n ) const;
119
122 [[nodiscard]] const List< int > & children( int n ) const;
123
126 [[nodiscard]] const T & read( int n ) const;
127
130 void write( int n , const T & v );
131
133 [[nodiscard]] int size( void ) const;
134
151 int insertChild( int n , const T & v );
152
157 int insertSibling( int n , const T & v );
158
180 int insertChildTree( int n , const ChildListTree & t , int s );
181
187 int insertSiblingTree( int n , const ChildListTree & t , int s );
188
193 void remove( int n );
194
196 [[nodiscard]] std::vector< int > previsit( void ) const;
197
199 [[nodiscard]] std::vector< int > postvisit( void ) const;
200
203 [[nodiscard]] std::vector< int > invisit( int i ) const;
204
205/*--------------------------------------------------------------------------*/
206 private:
208 struct Node
209 {
210 T value;
211 int parent;
213 typename List< int >::Pos where;
214 bool used;
215 };
216
217 const Node & node( int n ) const;
218 Node & node( int n );
219
222 int make( const T & v , int p );
223
226 int copy( const ChildListTree & t , int s , int p );
227
229 void release( int n );
230
233 void visit( int n , int i , std::vector< int > & P , int kind ) const;
234
237 [[nodiscard]] bool linked( void ) const;
238
239 std::vector< Node > f_N;
240 int f_root = NIL;
241 int f_free = NIL;
242 int f_size = 0;
243
244 }; // end( class ChildListTree )
245
246/*--------------------------------------------------------------------------*/
247
248} // end( namespace ADS )
249
250/*--------------------------------------------------------------------------*/
251/*-------------------------- IMPLEMENTATION --------------------------------*/
252/*--------------------------------------------------------------------------*/
253
254#include "ADS/tree/impl/ChildListTree.tpp"
255
256/*--------------------------------------------------------------------------*/
257
258#endif /* ChildListTree.h included */
259
260/*--------------------------------------------------------------------------*/
261/*------------------------ End File ChildListTree.h ------------------------*/
262/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The LIST of the book: a SEQUENCE [see ADT.h] realized as a bidirectional circular list of cells with ...
an ordered tree whose nodes keep the list of their children
Definition ChildListTree.h:67
ChildListTree(void)=default
the empty tree
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
static constexpr int NIL
no node
Definition ChildListTree.h:72
int rightSibling(int n) const
the next sibling of , the next cell of the list of its parent, NIL if there is none
int insertSibling(int n, const T &v)
a new next sibling of , not the root, of value , in : as insertChild() on the list of the parent of ,...
int insertChildTree(int n, const ChildListTree &t, int s)
a copy of the subtree of in as the first child of , in for the nodes of the subtree; may be this...
std::vector< int > previsit(void) const
the nodes in previsit
void write(int n, const T &v)
writes in
ChildListTree & operator=(const ChildListTree &o)
a copy of o, as the copy constructor
bool isLeaf(int n) const
true if has no child
int leftmostChild(int n) const
the first child of , the head of its list, NIL for a leaf
int insertChild(int n, const T &v)
a new first child of of value , in
int insertSiblingTree(int n, const ChildListTree &t, int s)
a copy of the subtree of in as the next sibling of , as insertChildTree() after the cell of ; may ...
std::vector< int > invisit(int i) const
the nodes in invisit with index , as CursorTree::invisit() on the lists of the children
void remove(int n)
removes the subtree of , detached from its parent in by the position of its cell,...
ChildListTree & operator=(ChildListTree &&o) noexcept=default
the nodes of o, with their lists and cells
const List< int > & children(int n) const
the list of the children of , in their order
T Item
the type of the values
Definition ChildListTree.h:71
ChildListTree(const ChildListTree &o)
a copy of o, whose nodes keep the positions of their cells in the new lists, not in those of o,...
int parent(int n) const
the parent of , NIL for the root
bool empty(void) const
true if the tree has no node
int size(void) const
the number of nodes
std::vector< int > postvisit(void) const
the nodes in postvisit
const T & read(int n) const
the value of
ChildListTree(ChildListTree &&o) noexcept=default
the nodes of o, with their lists and cells
ChildListTree(const T &v)
a tree of one node of value
Cell * Pos
a position is the address of a cell
Definition List.h:132
constexpr int NIL
no node
Definition Visit.h:128
BinaryTree< T > * copy(const BinaryTree< T > *t)
a deep copy of the subtree , nil if is nil; the caller owns it
Definition ADT.h:96