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

an ordered tree, with pointers to parent, first child and next sibling More...

#include <ADS/tree/Tree.h>

Public types

using Item = T
 the type of the values

Public member functions

 Tree (const T &v)
 a new tree, made of one node containing \( v \) (the Tree( v ) of the book)
 Tree (const Tree &)=delete
 no copy, since a tree owns its subtrees [see copy()]
Tree & operator= (const Tree &)=delete
 no copy, since a tree owns its subtrees [see copy()]
 ~Tree ()
 destroys the subtrees of the children, i.e., the whole tree, with a stack of its own rather than by recursion
const T & read (void) const
 the value of the node
void write (const T &v)
 writes \( v \) in the node
Tree * parent (void) const
 the parent; nil if the node is a root
Tree * leftmostChild (void) const
 the first child; nil if the node is a leaf
Tree * rightSibling (void) const
 the next sibling; nil if there is none
void insertChild (Tree *t)
 inserts the tree \( t \) as the first child of this node, which then owns it, in \( O( 1 ) \)
void insertSibling (Tree *t)
 inserts the tree \( t \) as the next sibling of this node, which must not be a root; the parent of this node then owns \( t \)
bool isLeaf (void) const
 true if the node has no child, i.e., it is a leaf
bool isLastSibling (void) const
 true if the node has no next sibling
void deleteChild (void)
 destroys the subtree rooted in the first child, whose next sibling becomes the first child
void deleteSibling (void)
 destroys the subtree rooted in the next sibling, whose next sibling takes its place

Detailed description

template<class T>
class ADS::Tree< T >

an ordered tree, with pointers to parent, first child and next sibling

Member typedef documentation

◆ Item

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

the type of the values

Constructor and destructor documentation

◆ Tree() [1/2]

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

a new tree, made of one node containing \( v \) (the Tree( v ) of the book)

◆ Tree() [2/2]

template<class T>
ADS::Tree< T >::Tree ( const Tree< T > & )
delete

no copy, since a tree owns its subtrees [see copy()]

◆ ~Tree()

template<class T>
ADS::Tree< T >::~Tree ( )

destroys the subtrees of the children, i.e., the whole tree, with a stack of its own rather than by recursion

In pseudocode (section 5.2, with an explicit stack \( S \), as the height may be \( n \)):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{destroy}( t )\quad \triangleright \ \text{no recursion: the height may be } n \cr 2 & \quad S \gets \text{a stack of the children of } t \cr 3 & \quad \textbf{while}\ S \ne \emptyset\ \textbf{do}\ u \gets \mathrm{pop}( S );\ \text{push the children of } u;\ \text{free } u \end{array}} \]

Member function documentation

◆ deleteChild()

template<class T>
void ADS::Tree< T >::deleteChild ( void )

destroys the subtree rooted in the first child, whose next sibling becomes the first child

◆ deleteSibling()

template<class T>
void ADS::Tree< T >::deleteSibling ( void )

destroys the subtree rooted in the next sibling, whose next sibling takes its place

◆ insertChild()

template<class T>
void ADS::Tree< T >::insertChild ( Tree< T > * t)

inserts the tree \( t \) as the first child of this node, which then owns it, in \( O( 1 ) \)

In pseudocode (section 5.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insertChild}( t ) \cr 2 & \quad t.\mathit{parent} \gets \mathit{this};\ t.\mathit{sibling} \gets \mathit{child};\ \mathit{child} \gets t \end{array}} \]

Precondition
\( t \) is a root, not this node nor the root of its tree (which would make a cycle)
Postcondition
\( t \) is the first child

◆ insertSibling()

template<class T>
void ADS::Tree< T >::insertSibling ( Tree< T > * t)

inserts the tree \( t \) as the next sibling of this node, which must not be a root; the parent of this node then owns \( t \)

In pseudocode (section 5.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insertSibling}( t ) \cr 2 & \quad t.\mathit{parent} \gets \mathit{parent};\ t.\mathit{sibling} \gets \mathit{sibling};\ \mathit{sibling} \gets t \end{array}} \]

Precondition
\( t \) is a root, and this node is not; \( t \) is not the root of the tree of this node (which would make a cycle)
Postcondition
\( t \) is the next sibling

◆ isLastSibling()

template<class T>
bool ADS::Tree< T >::isLastSibling ( void ) const
nodiscard

true if the node has no next sibling

◆ isLeaf()

template<class T>
bool ADS::Tree< T >::isLeaf ( void ) const
nodiscard

true if the node has no child, i.e., it is a leaf

◆ leftmostChild()

template<class T>
Tree * ADS::Tree< T >::leftmostChild ( void ) const
nodiscard

the first child; nil if the node is a leaf

◆ operator=()

template<class T>
Tree & ADS::Tree< T >::operator= ( const Tree< T > & )
delete

no copy, since a tree owns its subtrees [see copy()]

◆ parent()

template<class T>
Tree * ADS::Tree< T >::parent ( void ) const
nodiscard

the parent; nil if the node is a root

◆ read()

template<class T>
const T & ADS::Tree< T >::read ( void ) const
nodiscard

the value of the node

◆ rightSibling()

template<class T>
Tree * ADS::Tree< T >::rightSibling ( void ) const
nodiscard

the next sibling; nil if there is none

◆ write()

template<class T>
void ADS::Tree< T >::write ( const T & v)

writes \( v \) in the node


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