|
ADS
Algorithms and Data Structures
|
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. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/tree/Tree.h"#include <cstddef>#include <limits>#include <memory>#include <ranges>#include <sstream>#include <stdexcept>#include <string>#include <string_view>#include <utility>#include <vector>#include "ADS/tree/impl/BinaryTree.tpp"Go to the source code of this file.
Classes | |
| class | ADS::BinaryTree< T > |
| a binary tree with pointers to the parent and to the two children More... | |
Namespaces | |
| namespace | ADS |
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: 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.
Moving along the tree, reading, writing and inserting take \( O( 1 ) \) time, deleting a subtree time linear in its size. As for Tree, a node and its subtree are the same thing, a tree owns its subtrees (a tree whose insertion is refused stays the caller's), and nil is nullptr.
The visit dfs() examines a node before its subtrees (previsit), between them (invisit, or symmetric visit) or after them (postvisit). An ordered tree \( T \) is represented by the binary tree \( B \) whose left child of a node is its first child in \( T \) and whose right child is its next sibling in \( T \) [see to_binary()]: the previsits of \( T \) and of \( B \) are the same, and the postvisit of \( T \) is the invisit of \( B \).
detachLeft() and detachRight() cut a subtree off its parent without destroying it, in \( O( 1 ) \) time: the subtree becomes a root owned by the caller, ready to be inserted elsewhere, which moves subtrees around (a mirror, a rotation) with no copy.
copy() makes a deep copy of a subtree and join() a new root with the copies of two trees as children, in time linear in their size.
The ranges preorder(), inorder() and postorder() walk the nodes of a subtree by the links to the parents, with no stack and no recursion, in \( O( n ) \) time overall and \( O( 1 ) \) extra space: each node is left at most once from each of its three visits.
to_list() writes a binary tree as a parenthesized list, () for the empty tree and \( ( v \, L \, R ) \) for the root \( v \) with the subtrees \( L \) and \( R \), e.g., ( 8 () ( 5 ( 27 () () ) ( 16 () () ) ) ), and from_list() reads it back; the values are written with << and read with >>, and must contain no space nor parenthesis.
A complete \( d \)-ary tree, whose internal nodes have \( d \) children, is stored in a vector, the root in the place 0 and the children of the node \( i \) in the places \( d i + 1 , \ldots , d i + d \) [see dary_child()].
The contracts [see Contract.h]: the tree inserted is a root other than the node itself and goes where no child is; the subtree detached is a root and its place is empty; the invariant is that each child has its parent as parent, down the subtree; the places of a \( d \)-ary tree need \( d \geq 1 \), a child \( 1 \leq j \leq d \) and a place \( i > 0 \) to have a parent. The operations are defined in BinaryTree.tpp.