ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::PreorderArrayTree< T > Class Template Reference

an ordered tree in a vector of ( parent , value ) in preorder More...

#include <ADS/tree/PreorderArrayTree.h>

Public types

using Item = T
 the type of the values

Public member functions

 PreorderArrayTree (void)=default
 the empty tree
 PreorderArrayTree (const T &v)
 a tree of one node of value \( v \)
bool empty (void) const
 true if the tree has no node
int size (void) const
 the number of nodes, \( n \)
int root (void) const
 the root, 0, or NIL if the tree is empty
int parent (int i) const
 the parent of \( i \), NIL for the root
const T & read (int i) const
 the value of \( i \)
void write (int i, const T &v)
 writes \( v \) in \( i \)
int leftmostChild (int i) const
 the first child, \( i + 1 \) if its parent is \( i \), NIL otherwise
int rightSibling (int i) const
 the next sibling, the place after the subtree of \( i \) if it has the same parent, NIL otherwise
bool isLeaf (int i) const
 true if \( i \) has no child
int subtreeEnd (int i) const
 the place after the subtree of \( i \)
void insertChild (int i, const PreorderArrayTree &t)
 a copy of \( t \) as the first child of \( i \); \( t \) may be this tree, copied as it was before the call
void insertSibling (int i, const PreorderArrayTree &t)
 a copy of \( t \) as the next sibling of \( i \), not the root; \( t \) may be this tree, copied as it was before the call: the splice of insertChild() at the end of the subtree of \( i \), under the parent of \( i \)
void remove (int i)
 removes the subtree of \( i \), the places after it shifted back and their parents renumbered
auto values (void) const
 the values of the nodes in preorder, which is the order of the array, random access

Static public attributes

static constexpr int NIL = -1
 no node

Detailed description

template<class T>
class ADS::PreorderArrayTree< T >

an ordered tree in a vector of ( parent , value ) in preorder

Member typedef documentation

◆ Item

template<class T>
using ADS::PreorderArrayTree< T >::Item = T

the type of the values

Constructor and destructor documentation

◆ PreorderArrayTree() [1/2]

template<class T>
ADS::PreorderArrayTree< T >::PreorderArrayTree ( void )
default

the empty tree

◆ PreorderArrayTree() [2/2]

template<class T>
ADS::PreorderArrayTree< T >::PreorderArrayTree ( const T & v)
explicit

a tree of one node of value \( v \)

Member function documentation

◆ empty()

template<class T>
bool ADS::PreorderArrayTree< T >::empty ( void ) const
nodiscard

true if the tree has no node

◆ insertChild()

template<class T>
void ADS::PreorderArrayTree< T >::insertChild ( int i,
const PreorderArrayTree< T > & t )

a copy of \( t \) as the first child of \( i \); \( t \) may be this tree, copied as it was before the call

In pseudocode (beyond the book), the cells of \( t \), of \( k \) nodes, spliced in at the place \( i + 1 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insertChild}( i , t ) \cr 2 & \quad a \gets i + 1;\ C \gets \text{a copy of the cells of } t \cr 3 & \quad \textbf{for}\ j \gets a\ \textbf{to}\ n - 1\ \textbf{do} \ \textbf{if}\ p[ j ] \ge a\ \textbf{then}\ p[ j ] \gets p[ j ] + k \cr 4 & \quad C[ 0 ].\mathit{parent} \gets i;\ \textbf{for}\ j \gets 1 \ \textbf{to}\ k - 1\ \textbf{do}\ C[ j ].\mathit{parent} \gets C[ j ].\mathit{parent} + a \cr 5 & \quad \text{insert } C \text{ at the place } a \end{array}} \]

Precondition
\( i \) is a node

◆ insertSibling()

template<class T>
void ADS::PreorderArrayTree< T >::insertSibling ( int i,
const PreorderArrayTree< T > & t )

a copy of \( t \) as the next sibling of \( i \), not the root; \( t \) may be this tree, copied as it was before the call: the splice of insertChild() at the end of the subtree of \( i \), under the parent of \( i \)

Precondition
\( i \) is a node other than the root

◆ isLeaf()

template<class T>
bool ADS::PreorderArrayTree< T >::isLeaf ( int i) const
nodiscard

true if \( i \) has no child

Precondition
\( i \) is a node

◆ leftmostChild()

template<class T>
int ADS::PreorderArrayTree< T >::leftmostChild ( int i) const
nodiscard

the first child, \( i + 1 \) if its parent is \( i \), NIL otherwise

Precondition
\( i \) is a node

◆ parent()

template<class T>
int ADS::PreorderArrayTree< T >::parent ( int i) const
nodiscard

the parent of \( i \), NIL for the root

Precondition
\( i \) is a node

◆ read()

template<class T>
const T & ADS::PreorderArrayTree< T >::read ( int i) const
nodiscard

the value of \( i \)

Precondition
\( i \) is a node

◆ remove()

template<class T>
void ADS::PreorderArrayTree< T >::remove ( int i)

removes the subtree of \( i \), the places after it shifted back and their parents renumbered

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( i ) \cr 2 & \quad e \gets \mathrm{subtreeEnd}( i );\ k \gets e - i \cr 3 & \quad \text{erase the places } [ i , e ) \cr 4 & \quad \textbf{for}\ j \gets i\ \textbf{to}\ n - 1\ \textbf{do} \ \textbf{if}\ p[ j ] \ge e\ \textbf{then}\ p[ j ] \gets p[ j ] - k \end{array}} \]

Precondition
\( i \) is a node

◆ rightSibling()

template<class T>
int ADS::PreorderArrayTree< T >::rightSibling ( int i) const
nodiscard

the next sibling, the place after the subtree of \( i \) if it has the same parent, NIL otherwise

Precondition
\( i \) is a node

◆ root()

template<class T>
int ADS::PreorderArrayTree< T >::root ( void ) const
nodiscard

the root, 0, or NIL if the tree is empty

◆ size()

template<class T>
int ADS::PreorderArrayTree< T >::size ( void ) const
nodiscard

the number of nodes, \( n \)

◆ subtreeEnd()

template<class T>
int ADS::PreorderArrayTree< T >::subtreeEnd ( int i) const
nodiscard

the place after the subtree of \( i \)

In pseudocode (beyond the book), \( p \) the parents: the subtree goes on while the parents are in it, i.e., not before \( i \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{subtreeEnd}( i ) \cr 2 & \quad e \gets i + 1 \cr 3 & \quad \textbf{while}\ e < n\ \textbf{and}\ p[ e ] \ge i \ \textbf{do}\ e \gets e + 1 \cr 4 & \quad \textbf{return}\ e \end{array}} \]

Precondition
\( i \) is a node

◆ values()

template<class T>
auto ADS::PreorderArrayTree< T >::values ( void ) const
nodiscard

the values of the nodes in preorder, which is the order of the array, random access

◆ write()

template<class T>
void ADS::PreorderArrayTree< T >::write ( int i,
const T & v )

writes \( v \) in \( i \)

Precondition
\( i \) is a node

Member data documentation

◆ NIL

template<class T>
int ADS::PreorderArrayTree< T >::NIL = -1
staticconstexpr

no node


The documentation for this class was generated from the following file: