ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PreorderArrayTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File PreorderArrayTree.h ------------------------*/
3/*--------------------------------------------------------------------------*/
31/*--------------------------------------------------------------------------*/
32/*----------------------------- DEFINITIONS --------------------------------*/
33/*--------------------------------------------------------------------------*/
34
35#ifndef __ADS_PreorderArrayTree
36 #define __ADS_PreorderArrayTree
37/* self-identification: #endif constant */
38
39/*--------------------------------------------------------------------------*/
40/*------------------------------ INCLUDES ----------------------------------*/
41/*--------------------------------------------------------------------------*/
42
43#include "ADS/Contract.h"
44
45#include <ranges>
46#include <stdexcept>
47#include <vector>
48
49/*--------------------------------------------------------------------------*/
50/*--------------------------- NAMESPACE ------------------------------------*/
51/*--------------------------------------------------------------------------*/
52
53namespace ADS
54{
55
56/*--------------------------------------------------------------------------*/
57/*------------------------ CLASS PreorderArrayTree -------------------------*/
58/*--------------------------------------------------------------------------*/
59
61template< class T >
63{
64
65/*--------------------------------------------------------------------------*/
66 public:
67 using Item = T;
68 static constexpr int NIL = -1;
69
71 PreorderArrayTree( void ) = default;
72
74 explicit PreorderArrayTree( const T & v );
75
77 [[nodiscard]] bool empty( void ) const;
78
80 [[nodiscard]] int size( void ) const;
81
83 [[nodiscard]] int root( void ) const;
84
87 [[nodiscard]] int parent( int i ) const;
88
91 [[nodiscard]] const T & read( int i ) const;
92
95 void write( int i , const T & v );
96
100 [[nodiscard]] int leftmostChild( int i ) const;
101
105 [[nodiscard]] int rightSibling( int i ) const;
106
109 [[nodiscard]] bool isLeaf( int i ) const;
110
125 [[nodiscard]] int subtreeEnd( int i ) const;
126
146 void insertChild( int i , const PreorderArrayTree & t );
147
153 void insertSibling( int i , const PreorderArrayTree & t );
154
169 void remove( int i );
170
171/*------------------------- THE STANDARD INTERFACE -------------------------*/
172
175 [[nodiscard]] auto values( void ) const;
176
177/*--------------------------------------------------------------------------*/
178 private:
180 struct Cell
181 {
182 int parent;
183 T value;
184 };
185
186 const Cell & cell( int i ) const;
187
189 [[nodiscard]] bool preordered( void ) const;
190
194 void splice( int at , int p , const PreorderArrayTree & t );
195
196 std::vector< Cell > f_A;
197
198 }; // end( class PreorderArrayTree )
199
200/*--------------------------------------------------------------------------*/
201
202} // end( namespace ADS )
203
204/*--------------------------------------------------------------------------*/
205/*-------------------------- IMPLEMENTATION --------------------------------*/
206/*--------------------------------------------------------------------------*/
207
208#include "ADS/tree/impl/PreorderArrayTree.tpp"
209
210/*--------------------------------------------------------------------------*/
211
212#endif /* PreorderArrayTree.h included */
213
214/*--------------------------------------------------------------------------*/
215/*---------------------- End File PreorderArrayTree.h ----------------------*/
216/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
an ordered tree in a vector of ( parent , value ) in preorder
Definition PreorderArrayTree.h:63
T Item
the type of the values
Definition PreorderArrayTree.h:67
int subtreeEnd(int i) const
the place after the subtree of
void write(int i, const T &v)
writes in
PreorderArrayTree(const T &v)
a tree of one node of value
bool empty(void) const
true if the tree has no node
auto values(void) const
the values of the nodes in preorder, which is the order of the array, random access
void insertChild(int i, const PreorderArrayTree &t)
a copy of as the first child of ; may be this tree, copied as it was before the call
void insertSibling(int i, const PreorderArrayTree &t)
a copy of as the next sibling of , not the root; may be this tree, copied as it was before the call...
static constexpr int NIL
no node
Definition PreorderArrayTree.h:68
int parent(int i) const
the parent of , NIL for the root
int root(void) const
the root, 0, or NIL if the tree is empty
int rightSibling(int i) const
the next sibling, the place after the subtree of if it has the same parent, NIL otherwise
int leftmostChild(int i) const
the first child, if its parent is , NIL otherwise
bool isLeaf(int i) const
true if has no child
void remove(int i)
removes the subtree of , the places after it shifted back and their parents renumbered
PreorderArrayTree(void)=default
the empty tree
const T & read(int i) const
the value of
int size(void) const
the number of nodes,
Definition ADT.h:96