|
ADS
Algorithms and Data Structures
|
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 | |
an ordered tree, with pointers to parent, first child and next sibling
| using ADS::Tree< T >::Item = T |
the type of the values
|
explicit |
a new tree, made of one node containing \( v \) (the Tree( v ) of the book)
|
delete |
no copy, since a tree owns its subtrees [see copy()]
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}} \]
| void ADS::Tree< T >::deleteChild | ( | void | ) |
destroys the subtree rooted in the first child, whose next sibling becomes the first child
| void ADS::Tree< T >::deleteSibling | ( | void | ) |
destroys the subtree rooted in the next sibling, whose next sibling takes its place
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}} \]
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}} \]
|
nodiscard |
true if the node has no next sibling
|
nodiscard |
true if the node has no child, i.e., it is a leaf
the first child; nil if the node is a leaf
no copy, since a tree owns its subtrees [see copy()]
the parent; nil if the node is a root
|
nodiscard |
the value of the node
the next sibling; nil if there is none
| void ADS::Tree< T >::write | ( | const T & | v | ) |
writes \( v \) in the node