ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Tree.h

The ordered TREE of the book, realized with pointers to the parent, to the first child and to the next sibling, and its visits. More...

Files

file  ADS/tree/Tree.h
 The ordered TREE of the book, realized with pointers to the parent, to the first child and to the next sibling, and its visits.

Classes

class  ADS::Tree< T >
 an ordered tree, with pointers to parent, first child and next sibling More...

Functions

template<class T, class Pre, class Post>
void ADS::dfs (Tree< T > *t, Pre &&pre, Post &&post)
 the visit in depth: pre( u ) on each node before its subtrees are visited, post( u ) after; pre_visit() and post_visit() are dfs() with one of them.
template<class T, class F>
void ADS::pre_visit (Tree< T > *t, F &&f)
 the previsit: f( u ) on the root, then on the subtrees in order
template<class T, class F>
void ADS::post_visit (Tree< T > *t, F &&f)
 the postvisit: f( u ) on the subtrees in order, then on the root
template<class T, class F>
void ADS::in_visit (Tree< T > *t, int i, F &&f)
 the invisit: the first \( i \) subtrees, the root, the other subtrees
template<class T>
Tree< T > * ADS::copy (const Tree< T > *t)
 a deep copy of the subtree \( t \) and of its children, not of its siblings; the caller owns it
template<class T, class F>
void ADS::bfs (Tree< T > *t, F &&f)
 the visit by levels, with a queue of the nodes to examine
template<class T>
int ADS::maxWeightLevel (Tree< T > *t)
 the first level of largest weight, the weight of a level being the sum of the values of its nodes (the root at level 0), by a visit by levels
template<class T>
std::string ADS::to_list (const Tree< T > *t)
 the parenthesized list of \( t \), \( ( r \, T_1 \cdots T_k ) \) for the root \( r \) with the subtrees \( T_1 , \ldots , T_k \), e.g., ( a ( b ( e ) ( f ) ) ( c ( g ) ) ); the values are written with << and must contain no space nor parenthesis
template<class T>
Tree< T > * ADS::tree_from_list (std::string_view s)
 the tree of the parenthesized list \( s \), its values read with >>; the caller owns it
template<class T>
auto ADS::children (Tree< T > *t)
 the children of \( t \), from the leftmost, as a range
template<class T>
auto ADS::preorder (Tree< T > *t)
 the nodes of the subtree of \( t \) in preorder, \( t \) first, as a range by the links to the parents and the siblings, with no stack

Detailed description

The ordered TREE of the book, realized with pointers to the parent, to the first child and to the next sibling, and its visits.

The definitions, the invariants and the costs of the area are in Theory of trees.

Function documentation

◆ bfs()

template<class T, class F>
void ADS::bfs ( Tree< T > * t,
F && f )

the visit by levels, with a queue of the nodes to examine

In pseudocode (section 5.3):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{bfs}( t , f )\quad \triangleright \ \text{by levels, each from left to right} \cr 2 & \quad Q \gets \text{a queue holding only } t \cr 3 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 4 & \qquad u \gets \mathrm{dequeue}( Q );\ f( u ); \ \text{enqueue the children of } u \text{, from the leftmost} \end{array}} \]

Precondition
\( t \) is a node

◆ children()

template<class T>
auto ADS::children ( Tree< T > * t)

the children of \( t \), from the leftmost, as a range

◆ copy()

template<class T>
Tree< T > * ADS::copy ( const Tree< T > * t)

a deep copy of the subtree \( t \) and of its children, not of its siblings; the caller owns it

In pseudocode (section 5.3), a previsit that copies each node and then the subtrees of its children, in order:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{copy}( t ) \cr 2 & \quad c \gets \mathrm{Tree}( t.\mathrm{read}() );\ \ell \gets \textbf{nil}\quad \triangleright\ \ell \text{ the last child copied} \cr 3 & \quad \textbf{foreach}\ \text{child } u \text{ of } t \text{, from the leftmost}\ \textbf{do} \cr 4 & \qquad x \gets \mathrm{copy}( u ) \cr 5 & \qquad \textbf{if}\ \ell \ne \textbf{nil}\ \textbf{then} \ \ell.\mathrm{insertSibling}( x )\ \textbf{else} \ c.\mathrm{insertChild}( x ) \cr 6 & \qquad \ell \gets x \cr 7 & \quad \textbf{return}\ c \end{array}} \]

Precondition
\( t \) is a node
Postcondition
the copy is a root

◆ dfs()

template<class T, class Pre, class Post>
void ADS::dfs ( Tree< T > * t,
Pre && pre,
Post && post )

the visit in depth: pre( u ) on each node before its subtrees are visited, post( u ) after; pre_visit() and post_visit() are dfs() with one of them.

It and in_visit() run with a stack of their own, not by recursion, so that the height of the tree may be \( n \)

In pseudocode (section 5.3), recursive as in the book, while the code keeps on a stack each node with the child it is visiting:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{dfs}( t ) \cr 2 & \quad \mathrm{pre}( t )\quad \triangleright\ \text{on the way down} \cr 3 & \quad \textbf{for}\ c \gets t.\mathrm{leftmostChild}() , c.\mathrm{rightSibling}() , \ldots \text{ while } c \ne \textbf{nil} \ \textbf{do}\ \mathrm{dfs}( c ) \cr 4 & \quad \mathrm{post}( t )\quad \triangleright\ \text{on the way up} \end{array}} \]

Precondition
\( t \) is a node

◆ in_visit()

template<class T, class F>
void ADS::in_visit ( Tree< T > * t,
int i,
F && f )

the invisit: the first \( i \) subtrees, the root, the other subtrees

In pseudocode (section 5.3), recursive as in the book, while the code keeps its own stack as dfs() does:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{in\_visit}( t , i , f )\quad \triangleright\ t \text{ after its first } i \text{ children} \cr 2 & \quad k \gets 0 \cr 3 & \quad \textbf{foreach}\ \text{child } c \text{ of } t \text{, from the leftmost}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ k = i\ \textbf{then}\ f( t ) \cr 5 & \qquad \mathrm{in\_visit}( c , i , f );\ k \gets k + 1 \cr 6 & \quad \textbf{if}\ k \le i\ \textbf{then}\ f( t )\quad \triangleright\ \text{at most } i \text{ children} \end{array}} \]

Precondition
\( t \) is a node and \( i \ge 0 \)

◆ maxWeightLevel()

template<class T>
int ADS::maxWeightLevel ( Tree< T > * t)

the first level of largest weight, the weight of a level being the sum of the values of its nodes (the root at level 0), by a visit by levels

In pseudocode (section 5.3), the levels kept one at a time:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxWeightLevel}( t ) \cr 2 & \quad L \gets ( t );\ \ell \gets 0;\ b \gets 0;\ W \gets -\infty\quad \triangleright\ L \text{ the nodes of level } \ell \cr 3 & \quad \textbf{while}\ L \ne \emptyset\ \textbf{do} \cr 4 & \qquad w \gets \text{the sum of the values of the nodes of } L \cr 5 & \qquad \textbf{if}\ w > W\ \textbf{then}\ ( b , W ) \gets ( \ell , w )\quad \triangleright\ \text{the first level of the largest weight} \cr 6 & \qquad L \gets \text{the children of the nodes of } L;\ \ell \gets \ell + 1 \cr 7 & \quad \textbf{return}\ b \end{array}} \]

Precondition
\( t \) is a node

◆ post_visit()

template<class T, class F>
void ADS::post_visit ( Tree< T > * t,
F && f )

the postvisit: f( u ) on the subtrees in order, then on the root

In pseudocode (section 5.3), dfs() with no previsit:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{post\_visit}( t , f ) \cr 2 & \quad \mathrm{dfs}( t ) \text{ with nothing as } \mathrm{pre} \text{ and } \mathrm{post} = f \end{array}} \]

◆ pre_visit()

template<class T, class F>
void ADS::pre_visit ( Tree< T > * t,
F && f )

the previsit: f( u ) on the root, then on the subtrees in order

In pseudocode (section 5.3), dfs() with no postvisit:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{pre\_visit}( t , f ) \cr 2 & \quad \mathrm{dfs}( t ) \text{ with } \mathrm{pre} = f \text{ and } \text{nothing as } \mathrm{post} \end{array}} \]

◆ preorder()

template<class T>
auto ADS::preorder ( Tree< T > * t)

the nodes of the subtree of \( t \) in preorder, \( t \) first, as a range by the links to the parents and the siblings, with no stack

In pseudocode (section 5.3), the step to the next node of the preorder:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{next}( u , t )\quad \triangleright \ \text{the node after } u \text{ in the previsit of } t \cr 2 & \quad \textbf{if}\ u.\mathrm{leftmostChild}() \ne \textbf{nil} \ \textbf{then return}\ u.\mathrm{leftmostChild}() \cr 3 & \quad \textbf{while}\ u \ne t \text{ and } u.\mathrm{rightSibling}() = \textbf{nil}\ \textbf{do}\ u \gets u.\mathrm{parent}() \cr 4 & \quad \textbf{if}\ u = t\ \textbf{then return nil} \ \textbf{else return}\ u.\mathrm{rightSibling}() \end{array}} \]

◆ to_list()

template<class T>
std::string ADS::to_list ( const Tree< T > * t)

the parenthesized list of \( t \), \( ( r \, T_1 \cdots T_k ) \) for the root \( r \) with the subtrees \( T_1 , \ldots , T_k \), e.g., ( a ( b ( e ) ( f ) ) ( c ( g ) ) ); the values are written with << and must contain no space nor parenthesis

In pseudocode (section 5.3), a previsit:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{to\_list}( t ) \cr 2 & \quad \textbf{return}\ \text{“(”, } t.\mathrm{read}() \text{, the } \mathrm{to\_list} \text{ of each child of } t \text{ and “)”, joined by single spaces} \end{array}} \]

Precondition
\( t \) is a node

◆ tree_from_list()

template<class T>
Tree< T > * ADS::tree_from_list ( std::string_view s)

the tree of the parenthesized list \( s \), its values read with >>; the caller owns it

In pseudocode (section 5.3), a descent by the grammar \( T \to ( \, v \, T^* \, ) \) on the tokens of \( s \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{readTree}()\quad \triangleright \ \text{the inverse of } \mathrm{to\_list} \cr 2 & \quad \text{read “(” and the value } v \text{ that follows};\ t \gets \mathrm{Tree}( v ) \cr 3 & \quad \textbf{while}\ \text{“(” follows}\ \textbf{do}\ \text{make } \mathrm{readTree}() \text{ the last child of } t \cr 4 & \quad \text{read “)”};\ \textbf{return}\ t \end{array}} \]

Precondition
\( s \) is the list of a tree, and >> reads each atom whole (e.g., 8x is not an int)