ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::ParentTree Class Reference

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

Detailed description

a tree (a forest) of the nodes \( 0 , \ldots , n - 1 \) as a vector of parents

Constructor and destructor documentation

◆ ParentTree()

ADS::ParentTree::ParentTree ( int n)
explicit

a forest of \( n \) isolated nodes (the Tree( n ) of the book)

Precondition
\( n \ge 0 \)

Member function documentation

◆ parent()

int ADS::ParentTree::parent ( int i) const
nodiscard

the parent of the node \( i \); -1 if \( i \) is a root

Precondition
\( i \) is a node

◆ setParent()

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}} \]

Precondition
\( i \) and \( j \) are nodes, or \( j = -1 \), and \( i \ne j \); \( j \) is not in the subtree of \( i \) (which would make a cycle)

◆ size()

int ADS::ParentTree::size ( void ) const
nodiscard

the number of nodes


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