|
ADS
Algorithms and Data Structures
|
a tree (a forest) of the nodes \( 0 , \ldots , n - 1 \) as a vector of parents More...
#include <ADS/tree/ParentTree.h>
Public member functions | |
| ParentTree (int n) | |
| a forest of \( n \) isolated nodes (the Tree( n ) of the book) | |
| int | parent (int i) const |
| the parent of the node \( i \); -1 if \( i \) is a root | |
| void | setParent (int i, int j) |
| makes the node \( i \) a child of the node \( j \), or a root if \( j = -1 \), in \( O( 1 ) \) (section 5.4, \( p[ i ] \gets j \)) | |
| int | size (void) const |
| the number of nodes | |
a tree (a forest) of the nodes \( 0 , \ldots , n - 1 \) as a vector of parents
|
explicit |
a forest of \( n \) isolated nodes (the Tree( n ) of the book)
|
nodiscard |
the parent of the node \( i \); -1 if \( i \) is a root
| void ADS::ParentTree::setParent | ( | int | i, |
| int | j ) |
makes the node \( i \) a child of the node \( j \), or a root if \( j = -1 \), in \( O( 1 ) \) (section 5.4, \( p[ i ] \gets j \))
In pseudocode (section 5.4), with the walk from \( j \) to its root that the audit of the contracts makes, in \( O( d( j ) ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{setParent}( i , j ) \cr 2 & \quad p[ i ] \gets j\quad \triangleright\ \text{audit: } \textbf{not}\ \mathrm{below}( j , i ) \cr 3 & \textbf{function}\ \mathrm{below}( j , i )\quad \triangleright \ \text{whether } j \text{ is in the subtree of } i \cr 4 & \quad \textbf{while}\ j \ne -1\ \textbf{do} \cr 5 & \qquad \textbf{if}\ j = i\ \textbf{then return}\ \textbf{true} \cr 6 & \qquad j \gets p[ j ] \cr 7 & \quad \textbf{return}\ \textbf{false} \end{array}} \]
|
nodiscard |
the number of nodes