ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ArrayBinaryTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File ArrayBinaryTree.h -------------------------*/
3/*--------------------------------------------------------------------------*/
30/*--------------------------------------------------------------------------*/
31/*----------------------------- DEFINITIONS --------------------------------*/
32/*--------------------------------------------------------------------------*/
33
34#ifndef __ADS_ArrayBinaryTree
35 #define __ADS_ArrayBinaryTree
36/* self-identification: #endif constant */
37
38/*--------------------------------------------------------------------------*/
39/*------------------------------ INCLUDES ----------------------------------*/
40/*--------------------------------------------------------------------------*/
41
42#include "ADS/Contract.h"
43#include "ADS/Iterators.h"
45#include "ADS/tree/BinaryTree.h"
46
47#include <cstddef>
48#include <optional>
49#include <ranges>
50#include <stdexcept>
51#include <string>
52#include <vector>
53
54/*--------------------------------------------------------------------------*/
55/*--------------------------- NAMESPACE ------------------------------------*/
56/*--------------------------------------------------------------------------*/
57
58namespace ADS
59{
60
61/*--------------------------------------------------------------------------*/
62/*------------------------- CLASS ArrayBinaryTree --------------------------*/
63/*--------------------------------------------------------------------------*/
64
66template< class T >
68{
69
70/*--------------------------------------------------------------------------*/
71 public:
72 using Item = T;
73 static constexpr long NIL = -1;
74
76 ArrayBinaryTree( void ) = default;
77
79 explicit ArrayBinaryTree( const T & v );
80
83 ArrayBinaryTree( const T & v , const ArrayBinaryTree & l ,
84 const ArrayBinaryTree & r );
85
87 [[nodiscard]] bool empty( void ) const;
88
90 [[nodiscard]] long root( void ) const;
91
95 [[nodiscard]] long parent( long i ) const;
96
99 [[nodiscard]] long left( long i ) const;
100
103 [[nodiscard]] long right( long i ) const;
104
107 [[nodiscard]] bool isLeaf( long i ) const;
108
111 [[nodiscard]] const T & read( long i ) const;
112
115 void write( long i , const T & v );
116
119 void setRoot( const T & v );
120
138 void insertLeft( long i , const ArrayBinaryTree & t );
139
144 void insertRight( long i , const ArrayBinaryTree & t );
145
148 void remove( long i );
149
151 [[nodiscard]] std::vector< long > preorder( void ) const;
152
153/*------------------------- THE STANDARD INTERFACE -------------------------*/
154
156 struct Used
157 {
159 static std::size_t size( const auto & o ) { return( o.f_A.size() ); }
161 static bool occupied( const auto & o , std::size_t i ) {
162 return( o.f_A[ i ].has_value() );
163 }
164
165 static long read( const auto & , std::size_t i ) { return( long( i ) ); }
166 };
167
169 [[nodiscard]] auto nodes( void ) const {
171 return( std::ranges::subrange( It( this , 0 ) , It( this , f_A.size() ) ) );
172 }
173
174/*--------------------------------------------------------------------------*/
175 private:
176 [[nodiscard]] bool has( long i ) const;
177
178 std::optional< T > & place( long i );
179
180 const std::optional< T > & at( long i ) const;
181
182 std::optional< T > & at( long i );
183
185 void clear( long i );
186
189 [[nodiscard]] bool rooted( void ) const;
190
193 void copy( long k , const ArrayBinaryTree & t , long j );
194
195 void graft( long i , long c , const ArrayBinaryTree & t );
196
197 std::vector< std::optional< T > > f_A;
198
199 }; // end( class ArrayBinaryTree )
200
201/*--------------------------------------------------------------------------*/
202
203} // end( namespace ADS )
204
205/*--------------------------------------------------------------------------*/
206/*-------------------------- IMPLEMENTATION --------------------------------*/
207/*--------------------------------------------------------------------------*/
208
209#include "ADS/tree/impl/ArrayBinaryTree.tpp"
210
211/*--------------------------------------------------------------------------*/
212
213#endif /* ArrayBinaryTree.h included */
214
215/*--------------------------------------------------------------------------*/
216/*----------------------- End File ArrayBinaryTree.h -----------------------*/
217/*--------------------------------------------------------------------------*/
The QUEUE of the book [see ADT.h] realized with a circular vector of places, the place 0 following t...
The binary TREE of the book: an ordered tree where each node has at most two children,...
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...
a binary tree in a positional vector
Definition ArrayBinaryTree.h:68
long root(void) const
the root, 0, or NIL if the tree is empty
void remove(long i)
empties the subtree of , a previsit of its places
T Item
the type of the values
Definition ArrayBinaryTree.h:72
long parent(long i) const
the parent of , NIL for the root
void insertRight(long i, const ArrayBinaryTree &t)
a copy of as the right subtree of , which must be missing; may be this tree, copied as it was befor...
ArrayBinaryTree(void)=default
the empty tree
ArrayBinaryTree(const T &v, const ArrayBinaryTree &l, const ArrayBinaryTree &r)
a new root of value with copies of and as its subtrees
auto nodes(void) const
the nodes, in the order of their places, bidirectional
Definition ArrayBinaryTree.h:169
std::vector< long > preorder(void) const
the places of the nodes in preorder
static constexpr long NIL
no node
Definition ArrayBinaryTree.h:73
const T & read(long i) const
the value of
void insertLeft(long i, const ArrayBinaryTree &t)
a copy of as the left subtree of , which must be missing; may be this tree, copied as it was before...
long right(long i) const
the right child of , NIL if there is none
bool empty(void) const
true if the tree has no node
void setRoot(const T &v)
the root of value of the empty tree
ArrayBinaryTree(const T &v)
a tree of one node of value
long left(long i) const
the left child of , NIL if there is none
bool isLeaf(long i) const
true if has no child
void write(long i, const T &v)
writes in
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
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
the places in use, for SlotIterator
Definition ArrayBinaryTree.h:157
static long read(const auto &, std::size_t i)
the node of the place , the place itself
Definition ArrayBinaryTree.h:165
static bool occupied(const auto &o, std::size_t i)
whether the place holds a node
Definition ArrayBinaryTree.h:161
static std::size_t size(const auto &o)
the places
Definition ArrayBinaryTree.h:159