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

an ordered tree whose nodes keep the list of their children More...

#include <ADS/tree/ChildListTree.h>

Public types

using Item = T
 the type of the values

Public member functions

 ChildListTree (void)=default
 the empty tree
 ChildListTree (const T &v)
 a tree of one node of value \( v \)
 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, in \( O( n ) \)
 ChildListTree (ChildListTree &&o) noexcept=default
 the nodes of o, with their lists and cells
ChildListTree & operator= (const ChildListTree &o)
 a copy of o, as the copy constructor
ChildListTree & operator= (ChildListTree &&o) noexcept=default
 the nodes of o, with their lists and cells
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 parent (int n) const
 the parent of \( n \), NIL for the root
int leftmostChild (int n) const
 the first child of \( n \), the head of its list, NIL for a leaf
int rightSibling (int n) const
 the next sibling of \( n \), the next cell of the list of its parent, NIL if there is none
bool isLeaf (int n) const
 true if \( n \) has no child
bool isLastSibling (int n) const
 whether \( n \) is the last of its siblings
const List< int > & children (int n) const
 the list of the children of \( n \), in their order
const T & read (int n) const
 the value of \( n \)
void write (int n, const T &v)
 writes \( v \) in \( n \)
int size (void) const
 the number of nodes
int insertChild (int n, const T &v)
 a new first child of \( n \) of value \( v \), in \( O( 1 ) \)
int insertSibling (int n, const T &v)
 a new next sibling of \( n \), not the root, of value \( v \), in \( O( 1 ) \): as insertChild() on the list of the parent of \( n \), after the cell of \( n \)
int insertChildTree (int n, const ChildListTree &t, int s)
 a copy of the subtree of \( t \) in \( s \) as the first child of \( n \), in \( O( m ) \) for \( m \) the nodes of the subtree; \( t \) may be this tree, copied as it was before the call
int insertSiblingTree (int n, const ChildListTree &t, int s)
 a copy of the subtree of \( t \) in \( s \) as the next sibling of \( n \), as insertChildTree() after the cell of \( n \); \( t \) may be this tree, copied as it was before the call
void remove (int n)
 removes the subtree of \( n \), detached from its parent in \( O( 1 ) \) by the position of its cell, its places back to the free list
std::vector< int > previsit (void) const
 the nodes in previsit
std::vector< int > postvisit (void) const
 the nodes in postvisit
std::vector< int > invisit (int i) const
 the nodes in invisit with index \( i \), as CursorTree::invisit() on the lists of the children

Static public attributes

static constexpr int NIL = -1
 no node

Detailed description

template<class T>
class ADS::ChildListTree< T >

an ordered tree whose nodes keep the list of their children

Member typedef documentation

◆ Item

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

the type of the values

Constructor and destructor documentation

◆ ChildListTree() [1/4]

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

the empty tree

◆ ChildListTree() [2/4]

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

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

◆ ChildListTree() [3/4]

template<class T>
ADS::ChildListTree< T >::ChildListTree ( const ChildListTree< T > & o)

a copy of o, whose nodes keep the positions of their cells in the new lists, not in those of o, in \( O( n ) \)

◆ ChildListTree() [4/4]

template<class T>
ADS::ChildListTree< T >::ChildListTree ( ChildListTree< T > && o)
defaultnoexcept

the nodes of o, with their lists and cells

Member function documentation

◆ children()

template<class T>
const List< int > & ADS::ChildListTree< T >::children ( int n) const
nodiscard

the list of the children of \( n \), in their order

Precondition
\( n \) is a node

◆ empty()

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

true if the tree has no node

◆ insertChild()

template<class T>
int ADS::ChildListTree< T >::insertChild ( int n,
const T & v )

a new first child of \( n \) of value \( v \), in \( O( 1 ) \)

In pseudocode (Aho, Hopcroft and Ullman, section 3.3), \( N \) the vector of the nodes and \( C_n \) the list of the children of \( n \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insertChild}( n , v ) \cr 2 & \quad c \gets \text{a free place, or a new one, of value } v \text{ and parent } n \cr 3 & \quad N[ c ].\mathit{where} \gets C_n.\mathrm{insert}( C_n.\mathrm{head}() , c ) \cr 4 & \quad \textbf{return}\ c \end{array}} \]

Precondition
\( n \) is a node

◆ insertChildTree()

template<class T>
int ADS::ChildListTree< T >::insertChildTree ( int n,
const ChildListTree< T > & t,
int s )

a copy of the subtree of \( t \) in \( s \) as the first child of \( n \), in \( O( m ) \) for \( m \) the nodes of the subtree; \( t \) may be this tree, copied as it was before the call

In pseudocode (section 5.3), the copy a previsit, then linked as in insertChild():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{copy}( t , s , p )\quad \triangleright \ \text{the subtree of } s \text{ in } t \text{, hung under } p \cr 2 & \quad c \gets \text{a new node under } p \text{, of value } t.\mathrm{read}( s ) \cr 3 & \quad \textbf{foreach}\ \text{child } u \text{ of } s \text{ in } t \text{, in order}\ \textbf{do} \cr 4 & \qquad x \gets \mathrm{copy}( t , u , c );\ \text{append } x \text{ to } C_c \text{ and keep its cell in } N[ x ].\mathit{where} \cr 5 & \quad \textbf{return}\ c \end{array}} \]

Precondition
\( n \) is a node, \( s \) a node of \( t \)

◆ insertSibling()

template<class T>
int ADS::ChildListTree< T >::insertSibling ( int n,
const T & v )

a new next sibling of \( n \), not the root, of value \( v \), in \( O( 1 ) \): as insertChild() on the list of the parent of \( n \), after the cell of \( n \)

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

◆ insertSiblingTree()

template<class T>
int ADS::ChildListTree< T >::insertSiblingTree ( int n,
const ChildListTree< T > & t,
int s )

a copy of the subtree of \( t \) in \( s \) as the next sibling of \( n \), as insertChildTree() after the cell of \( n \); \( t \) may be this tree, copied as it was before the call

Precondition
\( n \) is a node other than the root, \( s \) a node of \( t \)

◆ invisit()

template<class T>
std::vector< int > ADS::ChildListTree< T >::invisit ( int i) const
nodiscard

the nodes in invisit with index \( i \), as CursorTree::invisit() on the lists of the children

◆ isLastSibling()

template<class T>
bool ADS::ChildListTree< T >::isLastSibling ( int n) const
nodiscard

whether \( n \) is the last of its siblings

Precondition
\( n \) is a node

◆ isLeaf()

template<class T>
bool ADS::ChildListTree< T >::isLeaf ( int n) const
nodiscard

true if \( n \) has no child

Precondition
\( n \) is a node

◆ leftmostChild()

template<class T>
int ADS::ChildListTree< T >::leftmostChild ( int n) const
nodiscard

the first child of \( n \), the head of its list, NIL for a leaf

Precondition
\( n \) is a node

◆ operator=() [1/2]

template<class T>
ChildListTree & ADS::ChildListTree< T >::operator= ( ChildListTree< T > && o)
defaultnoexcept

the nodes of o, with their lists and cells

◆ operator=() [2/2]

template<class T>
ChildListTree & ADS::ChildListTree< T >::operator= ( const ChildListTree< T > & o)

a copy of o, as the copy constructor

◆ parent()

template<class T>
int ADS::ChildListTree< T >::parent ( int n) const
nodiscard

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

Precondition
\( n \) is a node

◆ postvisit()

template<class T>
std::vector< int > ADS::ChildListTree< T >::postvisit ( void ) const
nodiscard

the nodes in postvisit

◆ previsit()

template<class T>
std::vector< int > ADS::ChildListTree< T >::previsit ( void ) const
nodiscard

the nodes in previsit

◆ read()

template<class T>
const T & ADS::ChildListTree< T >::read ( int n) const
nodiscard

the value of \( n \)

Precondition
\( n \) is a node

◆ remove()

template<class T>
void ADS::ChildListTree< T >::remove ( int n)

removes the subtree of \( n \), detached from its parent in \( O( 1 ) \) by the position of its cell, its places back to the free list

Precondition
\( n \) is a node

◆ rightSibling()

template<class T>
int ADS::ChildListTree< T >::rightSibling ( int n) const
nodiscard

the next sibling of \( n \), the next cell of the list of its parent, NIL if there is none

Precondition
\( n \) is a node

◆ root()

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

the place of the root, NIL if the tree is empty

◆ size()

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

the number of nodes

◆ write()

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

writes \( v \) in \( n \)

Precondition
\( n \) is a node

Member data documentation

◆ NIL

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

no node


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