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

an ordered tree on a vector of nodes linked by their places More...

#include <ADS/tree/CursorTree.h>

Classes

struct  Used
 the places in use, for SlotIterator More...
struct  Siblings
 the walk of the children of a node More...

Public types

using Item = T
 the type of the values

Public member functions

 CursorTree (void)=default
 the empty tree
 CursorTree (const T &v)
 a tree of one node of value \( v \)
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 \), NIL for a leaf
int rightSibling (int n) const
 the next sibling of \( n \), 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 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() with the sibling of \( n \) in place of its child
int insertChildTree (int n, const CursorTree &t, int s)
 a copy of the subtree of \( t \) in \( s \) as the first child of \( n \); \( t \) may be this tree, copied as it was before the call
int insertSiblingTree (int n, const CursorTree &t, int s)
 a copy of the subtree of \( t \) in \( s \) as the next sibling 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, 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 \), each node after its first \( i \) subtrees
auto nodes (void) const
 the nodes, in the order of their places, bidirectional
auto children (int n) const
 the children of \( n \), from the leftmost, forward

Static public attributes

static constexpr int NIL = -1
 no node

Detailed description

template<class T>
class ADS::CursorTree< T >

an ordered tree on a vector of nodes linked by their places

Member typedef documentation

◆ Item

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

the type of the values

Constructor and destructor documentation

◆ CursorTree() [1/2]

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

the empty tree

◆ CursorTree() [2/2]

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

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

Member function documentation

◆ children()

template<class T>
auto ADS::CursorTree< T >::children ( int n) const
inlinenodiscard

the children of \( n \), from the leftmost, forward

Precondition
\( n \) is a node

◆ empty()

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

true if the tree has no node

◆ insertChild()

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

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

In pseudocode (section 5.2, with cursors), \( N \) the vector of the nodes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insertChild}( n , v )\quad \triangleright\ \text{the new leftmost child of } n \cr 2 & \quad c \gets \text{a place from the free list, or a new one} \cr 3 & \quad N[ c ] \gets ( v , n , \mathrm{NIL} , N[ n ].\mathit{child} )\quad \triangleright\ \text{value, parent, child, sibling} \cr 4 & \quad N[ n ].\mathit{child} \gets c;\ \textbf{return}\ c \end{array}} \]

Precondition
\( n \) is a node

◆ insertChildTree()

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

a copy of the subtree of \( t \) in \( s \) as the first child of \( n \); \( 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 ) \cr 2 & \quad c \gets \text{a new node under } p \text{, of value } t.\mathrm{read}( s );\ \ell \gets \mathrm{NIL}\quad \triangleright \ \ell \text{ the last copied child} \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 ) \cr 5 & \qquad \textbf{if}\ \ell \ne \mathrm{NIL}\ \textbf{then}\ N[ \ell ].\mathit{sibling} \gets x\ \textbf{else}\ N[ c ].\mathit{child} \gets x \cr 6 & \qquad \ell \gets x \cr 7 & \quad \textbf{return}\ c \end{array}} \]

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

◆ insertSibling()

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

a new next sibling of \( n \), not the root, of value \( v \), in \( O( 1 ) \), as insertChild() with the sibling of \( n \) in place of its child

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

◆ insertSiblingTree()

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

a copy of the subtree of \( t \) in \( s \) as the next sibling 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::CursorTree< T >::invisit ( int i) const
nodiscard

the nodes in invisit with index \( i \), each node after its first \( i \) subtrees

In pseudocode (section 5.3), the visit of Tree.h on the places, the previsit and the postvisit being the same with the node first or last:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{invisit}( n , i )\quad \triangleright\ n \text{ after its first } i \text{ subtrees} \cr 2 & \quad k \gets 0 \cr 3 & \quad \textbf{foreach}\ \text{child } u \text{ of } n \text{, in order}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ k = i\ \textbf{then}\ \text{append } n \cr 5 & \qquad \mathrm{invisit}( u , i );\ k \gets k + 1 \cr 6 & \quad \textbf{if}\ k \le i\ \textbf{then}\ \text{append } n \end{array}} \]

◆ isLastSibling()

template<class T>
bool ADS::CursorTree< 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::CursorTree< T >::isLeaf ( int n) const
nodiscard

true if \( n \) has no child

Precondition
\( n \) is a node

◆ leftmostChild()

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

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

Precondition
\( n \) is a node

◆ nodes()

template<class T>
auto ADS::CursorTree< T >::nodes ( void ) const
inlinenodiscard

the nodes, in the order of their places, bidirectional

◆ parent()

template<class T>
int ADS::CursorTree< 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::CursorTree< T >::postvisit ( void ) const
nodiscard

the nodes in postvisit

◆ previsit()

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

the nodes in previsit

◆ read()

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

the value of \( n \)

Precondition
\( n \) is a node

◆ remove()

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

removes the subtree of \( n \), detached from its parent, its places back to the free list

In pseudocode (section 5.2, with cursors), \( O( d + m ) \) for \( d \) the siblings before \( n \) and \( m \) the nodes of its subtree:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( n ) \cr 2 & \quad p \gets N[ n ].\mathit{parent} \cr 3 & \quad \textbf{if}\ p \ne \mathrm{NIL}\ \textbf{then}\ \text{find } n \text{ among the children of } p \text{ and unlink it} \ \textbf{else}\ \mathit{root} \gets \mathrm{NIL} \cr 4 & \quad \text{put the places of the nodes of the subtree of } n \text{ on the free list, children before parents} \end{array}} \]

Precondition
\( n \) is a node

◆ rightSibling()

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

the next sibling of \( n \), NIL if there is none

Precondition
\( n \) is a node

◆ root()

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

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

◆ size()

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

the number of nodes

◆ write()

template<class T>
void ADS::CursorTree< 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::CursorTree< T >::NIL = -1
staticconstexpr

no node


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