|
ADS
Algorithms and Data Structures
|
The binary TREE of the book. More...
Files | |
| file | ADS/tree/BinaryTree.h |
| The binary TREE of the book: an ordered tree where each node has at most two children, told apart as the left and the right one, realized with pointers to the parent and to the two children. | |
Classes | |
| class | ADS::BinaryTree< T > |
| a binary tree with pointers to the parent and to the two children More... | |
Functions | |
| template<class T, class Pre, class In, class Post> | |
| void | ADS::dfs (BinaryTree< T > *t, Pre &&pre, In &&in, Post &&post) |
| the visit in depth: pre( t ) before the subtrees, in( t ) between them, post( t ) after them; nothing on an empty tree. | |
| template<class T, class F> | |
| void | ADS::pre_visit (BinaryTree< T > *t, F &&f) |
| the previsit of a binary tree, dfs() with f as pre alone | |
| template<class T, class F> | |
| void | ADS::in_visit (BinaryTree< T > *t, F &&f) |
| the invisit (symmetric visit) of a binary tree, dfs() with f as in alone | |
| template<class T, class F> | |
| void | ADS::post_visit (BinaryTree< T > *t, F &&f) |
| the postvisit of a binary tree, dfs() with f as post alone | |
| template<class T> | |
| BinaryTree< T > * | ADS::copy (const BinaryTree< T > *t) |
| a deep copy of the subtree \( t \), nil if \( t \) is nil; the caller owns it | |
| template<class T> | |
| BinaryTree< T > * | ADS::join (const T &v, const BinaryTree< T > *l, const BinaryTree< T > *r) |
| a new root of value \( v \) with copies of \( l \) and \( r \), each possibly nil, as its children; the caller owns it | |
| template<class T> | |
| std::string | ADS::to_list (const BinaryTree< T > *t) |
| the parenthesized list of \( t \), () if \( t \) is nil | |
| template<class T> | |
| BinaryTree< T > * | ADS::from_list (std::string_view s) |
| the binary tree of the parenthesized list \( s \), nil for (); the caller owns it | |
| template<class T> | |
| BinaryTree< T > * | ADS::to_binary (const Tree< T > *t) |
| the binary tree that represents the ordered tree \( t \) (figure 5.9): the left child of a node is its first child in \( t \), the right one its next sibling; the caller owns the result | |
| long | ADS::dary_child (long i, long d, long j) |
| the place \( d i + j \) of the \( j \)-th child, \( 1 \le j \le d \), of the node in the place \( i \) of a complete \( d \)-ary tree stored in a vector | |
| long | ADS::dary_parent (long i, long d) |
| the place \( \lfloor ( i - 1 ) / d \rfloor \) of the parent of the node in the place \( i > 0 \) | |
| template<class T> | |
| auto | ADS::preorder (BinaryTree< T > *t) |
| the nodes of the subtree of \( t \) in preorder, \( t \) first, by the links to the parents | |
| template<class T> | |
| auto | ADS::inorder (BinaryTree< T > *t) |
| the nodes of the subtree of \( t \) in symmetric order, by the links to the parents | |
| template<class T> | |
| auto | ADS::postorder (BinaryTree< T > *t) |
| the nodes of the subtree of \( t \) in postorder, \( t \) last, by the links to the parents | |
The binary TREE of the book.
The definitions, the invariants and the costs of the area are in Theory of trees.
| BinaryTree< T > * ADS::copy | ( | const BinaryTree< T > * | t | ) |
a deep copy of the subtree \( t \), nil if \( t \) is nil; the caller owns it
In pseudocode (section 5.6), a previsit:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{copy}( t )\quad \triangleright \ \text{a deep copy} \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return nil} \cr 3 & \quad c \gets \mathrm{BinaryTree}( t.\mathrm{read}() ) \cr 4 & \quad c.\mathrm{insertLeft}( \mathrm{copy}( t.\mathrm{left}() ) ); \ c.\mathrm{insertRight}( \mathrm{copy}( t.\mathrm{right}() ) ) \cr 5 & \quad \textbf{return}\ c \end{array}} \]
|
inline |
the place \( d i + j \) of the \( j \)-th child, \( 1 \le j \le d \), of the node in the place \( i \) of a complete \( d \)-ary tree stored in a vector
|
inline |
the place \( \lfloor ( i - 1 ) / d \rfloor \) of the parent of the node in the place \( i > 0 \)
| void ADS::dfs | ( | BinaryTree< T > * | t, |
| Pre && | pre, | ||
| In && | in, | ||
| Post && | post ) |
the visit in depth: pre( t ) before the subtrees, in( t ) between them, post( t ) after them; nothing on an empty tree.
It runs with a stack of its own, not by recursion, so that the height of the tree may be \( n \)
In pseudocode (section 5.6), recursive as in the book:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{dfs}( t ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \cr 3 & \quad \mathrm{pre}( t );\ \mathrm{dfs}( t.\mathrm{left}() )\quad \triangleright\ \text{then the node in the middle} \cr 4 & \quad \mathrm{in}( t );\ \mathrm{dfs}( t.\mathrm{right}() ); \ \mathrm{post}( t ) \end{array}} \]
| BinaryTree< T > * ADS::from_list | ( | std::string_view | s | ) |
the binary tree of the parenthesized list \( s \), nil for (); the caller owns it
In pseudocode (section 5.6), a descent by the grammar \( B \to ( \, ) \mid ( \, v \, B \, B \, ) \) 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 “(”};\ \textbf{if}\ \text{“)” follows} \ \textbf{then}\ \text{read it};\ \textbf{return nil}\quad \triangleright\ \text{the empty tree} \cr 3 & \quad t \gets \mathrm{BinaryTree}( \text{the value that follows} ) \cr 4 & \quad t.\mathrm{insertLeft}( \mathrm{readTree}() ); \ t.\mathrm{insertRight}( \mathrm{readTree}() ) \cr 5 & \quad \text{read “)”};\ \textbf{return}\ t \end{array}} \]
| void ADS::in_visit | ( | BinaryTree< T > * | t, |
| F && | f ) |
the invisit (symmetric visit) of a binary tree, dfs() with f as in alone
| auto ADS::inorder | ( | BinaryTree< T > * | t | ) |
the nodes of the subtree of \( t \) in symmetric order, by the links to the parents
In pseudocode (section 5.6, beyond the book for the walk with no stack), the first node the leftmost one, and the step to the next node:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{next}( u , t ) \cr 2 & \quad \textbf{if}\ u.\mathit{right} \ne \textbf{nil} \ \textbf{then return}\ \text{the leftmost node below } u.\mathit{right} \cr 3 & \quad \textbf{while}\ u \ne t\ \textbf{do} \cr 4 & \qquad \textbf{if}\ u = u.\mathit{parent}.\mathit{left} \ \textbf{then return}\ u.\mathit{parent} \cr 5 & \qquad u \gets u.\mathit{parent} \cr 6 & \quad \textbf{return nil} \end{array}} \]
| BinaryTree< T > * ADS::join | ( | const T & | v, |
| const BinaryTree< T > * | l, | ||
| const BinaryTree< T > * | r ) |
a new root of value \( v \) with copies of \( l \) and \( r \), each possibly nil, as its children; the caller owns it
In pseudocode (section 5.6):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{join}( v , l , r )\quad \triangleright\ l \text{ and } r \text{ are copied} \cr 2 & \quad t \gets \mathrm{BinaryTree}( v );\ t.\mathrm{insertLeft}( \mathrm{copy}( l ) );\ t.\mathrm{insertRight}( \mathrm{copy}( r ) ) \cr 3 & \quad \textbf{return}\ t \end{array}} \]
| void ADS::post_visit | ( | BinaryTree< T > * | t, |
| F && | f ) |
the postvisit of a binary tree, dfs() with f as post alone
| auto ADS::postorder | ( | BinaryTree< T > * | t | ) |
the nodes of the subtree of \( t \) in postorder, \( t \) last, by the links to the parents
In pseudocode (section 5.6, beyond the book for the walk with no stack), the first node the first leaf, down the left children when there are, and the step to the next node:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{next}( u , t ) \cr 2 & \quad \textbf{if}\ u = t\ \textbf{then return nil} \cr 3 & \quad p \gets u.\mathit{parent} \cr 4 & \quad \textbf{if}\ u = p.\mathit{left}\ \textbf{and} \ p.\mathit{right} \ne \textbf{nil}\ \textbf{then return} \ \text{the first leaf below } p.\mathit{right} \cr 5 & \quad \textbf{return}\ p \end{array}} \]
| void ADS::pre_visit | ( | BinaryTree< T > * | t, |
| F && | f ) |
the previsit of a binary tree, dfs() with f as pre alone
| auto ADS::preorder | ( | BinaryTree< T > * | t | ) |
the nodes of the subtree of \( t \) in preorder, \( t \) first, by the links to the parents
In pseudocode (section 5.6, beyond the book for the walk with no stack), the step to the next node:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{next}( u , t ) \cr 2 & \quad \textbf{if}\ u.\mathit{left} \ne \textbf{nil}\ \textbf{then return}\ u.\mathit{left} \cr 3 & \quad \textbf{if}\ u.\mathit{right} \ne \textbf{nil} \ \textbf{then return}\ u.\mathit{right} \cr 4 & \quad \textbf{while}\ u \ne t\ \textbf{do} \cr 5 & \qquad p \gets u.\mathit{parent} \cr 6 & \qquad \textbf{if}\ u = p.\mathit{left}\ \textbf{and} \ p.\mathit{right} \ne \textbf{nil}\ \textbf{then return} \ p.\mathit{right} \cr 7 & \qquad u \gets p \cr 8 & \quad \textbf{return nil} \end{array}} \]
| BinaryTree< T > * ADS::to_binary | ( | const Tree< T > * | t | ) |
the binary tree that represents the ordered tree \( t \) (figure 5.9): the left child of a node is its first child in \( t \), the right one its next sibling; the caller owns the result
In pseudocode (section 5.5, figure 5.9):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{to\_binary}( t )\quad \triangleright \ \text{left: the first child, right: the next sibling} \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return nil} \cr 3 & \quad b \gets \mathrm{BinaryTree}( t.\mathrm{read}() ) \cr 4 & \quad b.\mathrm{insertLeft}( \mathrm{to\_binary}( t.\mathrm{leftmostChild}() ) );\ b.\mathrm{insertRight}( \mathrm{to\_binary}( t.\mathrm{rightSibling}() ) ) \cr 5 & \quad \textbf{return}\ b \end{array}} \]
| std::string ADS::to_list | ( | const BinaryTree< T > * | t | ) |
the parenthesized list of \( t \), () if \( t \) is nil
In pseudocode (section 5.6), a previsit:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{to\_list}( t ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \ \text{“()”} \cr 3 & \quad \textbf{return}\ \text{the words “(”, } t.\mathrm{read}() \text{, } \mathrm{to\_list}( t.\mathrm{left}() ) \text{, } \mathrm{to\_list}( t.\mathrm{right}() ) \text{, “)”} \cr & \qquad \text{joined by single spaces} \end{array}} \]