ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CursorBinaryTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File CursorBinaryTree.h -------------------------*/
3/*--------------------------------------------------------------------------*/
29/*--------------------------------------------------------------------------*/
30/*----------------------------- DEFINITIONS --------------------------------*/
31/*--------------------------------------------------------------------------*/
32
33#ifndef __ADS_CursorBinaryTree
34 #define __ADS_CursorBinaryTree
35/* self-identification: #endif constant */
36
37/*--------------------------------------------------------------------------*/
38/*------------------------------ INCLUDES ----------------------------------*/
39/*--------------------------------------------------------------------------*/
40
41#include "ADS/Contract.h"
42#include "ADS/Iterators.h"
43
44#include <cstddef>
45#include <ranges>
46#include <stdexcept>
47#include <string>
48#include <utility>
49#include <vector>
50
51/*--------------------------------------------------------------------------*/
52/*--------------------------- NAMESPACE ------------------------------------*/
53/*--------------------------------------------------------------------------*/
54
55namespace ADS
56{
57
58/*--------------------------------------------------------------------------*/
59/*------------------------- CLASS CursorBinaryTree -------------------------*/
60/*--------------------------------------------------------------------------*/
61
63template< class T >
65{
66
67/*--------------------------------------------------------------------------*/
68 public:
69 using Item = T;
70 static constexpr int NIL = -1;
71
73 CursorBinaryTree( void ) = default;
74
76 explicit CursorBinaryTree( const T & v );
77
79 [[nodiscard]] bool empty( void ) const;
80
82 [[nodiscard]] int root( void ) const;
83
86 [[nodiscard]] int parent( int n ) const;
87
90 [[nodiscard]] int left( int n ) const;
91
94 [[nodiscard]] int right( int n ) const;
95
98 [[nodiscard]] bool isLeaf( int n ) const;
99
102 [[nodiscard]] const T & read( int n ) const;
103
106 void write( int n , const T & v );
107
109 [[nodiscard]] int size( void ) const;
110
113 int setRoot( const T & v );
114
129 int insertLeft( int n , const T & v );
130
134 int insertRight( int n , const T & v );
135
155 int insertLeft( int n , const CursorBinaryTree & t , int s );
156
161 int insertRight( int n , const CursorBinaryTree & t , int s );
162
166 void remove( int n );
167
169 [[nodiscard]] std::vector< int > preorder( void ) const;
170
171/*------------------------- THE STANDARD INTERFACE -------------------------*/
172
174 struct Used
175 {
177 static std::size_t size( const auto & o ) { return( o.f_N.size() ); }
179 static bool occupied( const auto & o , std::size_t i ) {
180 return( o.f_N[ i ].used );
181 }
182
183 static int read( const auto & , std::size_t i ) { return( int( i ) ); }
184 };
185
187 [[nodiscard]] auto nodes( void ) const {
189 return( std::ranges::subrange( It( this , 0 ) , It( this , f_N.size() ) ) );
190 }
191
192/*--------------------------------------------------------------------------*/
193 private:
195 struct Node
196 {
197 T value;
198 int parent ,
199 left ,
200 right;
201 bool used;
202 };
203
204 const Node & node( int n ) const;
205
206 Node & node( int n );
207
210 [[nodiscard]] bool linked( void ) const;
211
212 int make( const T & v , int p );
213
214 void release( int n );
215
216 int copy( const CursorBinaryTree & t , int s , int p );
217
218 std::vector< Node > f_N;
219 int f_root = NIL;
220 int f_free = NIL;
221 int f_size = 0;
222
223 }; // end( class CursorBinaryTree )
224
225/*--------------------------------------------------------------------------*/
226
227} // end( namespace ADS )
228
229/*--------------------------------------------------------------------------*/
230/*-------------------------- IMPLEMENTATION --------------------------------*/
231/*--------------------------------------------------------------------------*/
232
233#include "ADS/tree/impl/CursorBinaryTree.tpp"
234
235/*--------------------------------------------------------------------------*/
236
237#endif /* CursorBinaryTree.h included */
238
239/*--------------------------------------------------------------------------*/
240/*---------------------- End File CursorBinaryTree.h -----------------------*/
241/*--------------------------------------------------------------------------*/
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...
int setRoot(const T &v)
the root of value of the empty tree
void remove(int n)
removes the subtree of , detached from its parent, its places back to the free list,...
int insertRight(int n, const T &v)
a new right child of of value , which must be missing, as insertLeft() on the right
int insertRight(int n, const CursorBinaryTree &t, int s)
a copy of the subtree of in as the right subtree of , as the insertLeft() above on the right
int insertLeft(int n, const T &v)
a new left child of of value , which must be missing
CursorBinaryTree(const T &v)
a tree of one node of value
T Item
the type of the values
Definition CursorBinaryTree.h:69
int right(int n) const
the right child of , NIL if missing
bool empty(void) const
true if the tree has no node
int root(void) const
the place of the root, NIL if the tree is empty
int left(int n) const
the left child of , NIL if missing
int size(void) const
the number of nodes
std::vector< int > preorder(void) const
the nodes in preorder
const T & read(int n) const
the value of
void write(int n, const T &v)
writes in
int insertLeft(int n, const CursorBinaryTree &t, int s)
a copy of the subtree of in as the left subtree of ; may be this tree, copied as it was before the...
bool isLeaf(int n) const
true if has no child
int parent(int n) const
the parent of , NIL for the root
static constexpr int NIL
no node
Definition CursorBinaryTree.h:70
auto nodes(void) const
the nodes, in the order of their places, bidirectional
Definition CursorBinaryTree.h:187
CursorBinaryTree(void)=default
the empty tree
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
Definition ADT.h:96
the places in use, for SlotIterator
Definition CursorBinaryTree.h:175
static int read(const auto &, std::size_t i)
the node of the place , the place itself
Definition CursorBinaryTree.h:183
static std::size_t size(const auto &o)
the places
Definition CursorBinaryTree.h:177
static bool occupied(const auto &o, std::size_t i)
whether the place holds a node
Definition CursorBinaryTree.h:179