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

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...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/sequence/List.h"
#include <cstddef>
#include <memory>
#include <ranges>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
#include "ADS/tree/impl/Tree.tpp"

Go to the source code of this file.

Classes

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

Namespaces

namespace  ADS

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.

A node and the subtree rooted in it are the same thing, a Tree: the constructor makes an isolated node, insertChild() and insertSibling() hang a whole tree under a node in \( O( 1 ) \) time, deleteChild() and deleteSibling() destroy a subtree in time linear in its size. A tree owns its subtrees: deleting the root of a tree destroys all of it; a tree whose insertion is refused (its contract is violated) stays the caller's. As in the book, nil is nullptr.

The visits examine each node once, in \( \Theta( n ) \) time: the previsit examines the root and then visits the subtrees \( T_0 , \ldots , T_{k-1} \) of its children in order, the postvisit visits the subtrees and then examines the root, the invisit with index \( i \) visits the first \( i \) subtrees, examines the root and visits the others, and the visit by levels (bfs()) examines all the nodes of level \( l \) before those of level \( l + 1 \), using a queue. copy() makes a deep copy of a subtree.

The contracts [see Contract.h]: the tree inserted is a root (the precondition of the book, t.parent() nil) other than the node itself, insertSibling() needs a node that is not a root, and the visits a node; the invariant is that each child of a node has that node as its parent, down the subtree. The operations are defined in Tree.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, sections 5.1, 5.2 and 5.3, https://algoritmicamente.it/materiali/libro/capitoli/cap05.pdf
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa