ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::CursorBinaryTree< T > Class Template Reference

a binary tree on a vector of nodes linked by their places More...

#include <ADS/tree/CursorBinaryTree.h>

Classes

struct  Used
 the places in use, for SlotIterator More...

Public types

using Item = T
 the type of the values

Public member functions

 CursorBinaryTree (void)=default
 the empty tree
 CursorBinaryTree (const T &v)
 a tree of one node of value \( v \)
bool empty (void) const
 true if the tree has no node
int root (void) const
 the place of the root, NIL if the tree is empty
int parent (int n) const
 the parent of \( n \), NIL for the root
int left (int n) const
 the left child of \( n \), NIL if missing
int right (int n) const
 the right child of \( n \), NIL if missing
bool isLeaf (int n) const
 true if \( n \) has no child
const T & read (int n) const
 the value of \( n \)
void write (int n, const T &v)
 writes \( v \) in \( n \)
int size (void) const
 the number of nodes
int setRoot (const T &v)
 the root of value \( v \) of the empty tree
int insertLeft (int n, const T &v)
 a new left child of \( n \) of value \( v \), which must be missing
int insertRight (int n, const T &v)
 a new right child of \( n \) of value \( v \), which must be missing, as insertLeft() on the right
int insertLeft (int n, const CursorBinaryTree &t, int s)
 a copy of the subtree of \( t \) in \( s \) as the left subtree of \( n \); \( t \) may be this tree, copied as it was before the call
int insertRight (int n, const CursorBinaryTree &t, int s)
 a copy of the subtree of \( t \) in \( s \) as the right subtree of \( n \), as the insertLeft() above on the right
void remove (int n)
 removes the subtree of \( n \), detached from its parent, its places back to the free list, in time linear in its size
std::vector< int > preorder (void) const
 the nodes in preorder
auto nodes (void) const
 the nodes, in the order of their places, bidirectional

Static public attributes

static constexpr int NIL = -1
 no node

Detailed description

template<class T>
class ADS::CursorBinaryTree< T >

a binary tree on a vector of nodes linked by their places

Member typedef documentation

◆ Item

template<class T>
using ADS::CursorBinaryTree< T >::Item = T

the type of the values

Constructor and destructor documentation

◆ CursorBinaryTree() [1/2]

template<class T>
ADS::CursorBinaryTree< T >::CursorBinaryTree ( void )
default

the empty tree

◆ CursorBinaryTree() [2/2]

template<class T>
ADS::CursorBinaryTree< T >::CursorBinaryTree ( const T & v)
explicit

a tree of one node of value \( v \)

Member function documentation

◆ empty()

template<class T>
bool ADS::CursorBinaryTree< T >::empty ( void ) const
nodiscard

true if the tree has no node

◆ insertLeft() [1/2]

template<class T>
int ADS::CursorBinaryTree< T >::insertLeft ( int n,
const CursorBinaryTree< T > & t,
int s )

a copy of the subtree of \( t \) in \( s \) as the left subtree of \( n \); \( t \) may be this tree, copied as it was before the call

In pseudocode (section 5.6), a previsit of \( t \) from \( s \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{copy}( t , s , p ) \cr 2 & \quad \textbf{if}\ s = \mathrm{NIL}\ \textbf{then return} \ \mathrm{NIL} \cr 3 & \quad c \gets \text{a new node under } p \text{, of value } t.\mathrm{read}( s ) \cr 4 & \quad N[ c ].\mathit{left} \gets \mathrm{copy}( t , t.\mathrm{left}( s ) , c ) \cr 5 & \quad N[ c ].\mathit{right} \gets \mathrm{copy}( t , t.\mathrm{right}( s ) , c );\ \textbf{return}\ c \end{array}} \]

Precondition
\( n \) is a node with no left child, \( s \) a node of \( t \)

◆ insertLeft() [2/2]

template<class T>
int ADS::CursorBinaryTree< T >::insertLeft ( int n,
const T & v )

a new left child of \( n \) of value \( v \), which must be missing

In pseudocode (section 5.5, with cursors), \( N \) the vector of the nodes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insertLeft}( n , v ) \cr 2 & \quad c \gets \text{a free place, or a new one};\ N[ c ] \gets ( v , n , \mathrm{NIL} , \mathrm{NIL} ) \cr 3 & \quad N[ n ].\mathit{left} \gets c;\ \textbf{return}\ c \end{array}} \]

Precondition
\( n \) is a node with no left child

◆ insertRight() [1/2]

template<class T>
int ADS::CursorBinaryTree< T >::insertRight ( int n,
const CursorBinaryTree< T > & t,
int s )

a copy of the subtree of \( t \) in \( s \) as the right subtree of \( n \), as the insertLeft() above on the right

Precondition
\( n \) is a node with no right child, \( s \) a node of \( t \)

◆ insertRight() [2/2]

template<class T>
int ADS::CursorBinaryTree< T >::insertRight ( int n,
const T & v )

a new right child of \( n \) of value \( v \), which must be missing, as insertLeft() on the right

Precondition
\( n \) is a node with no right child

◆ isLeaf()

template<class T>
bool ADS::CursorBinaryTree< T >::isLeaf ( int n) const
nodiscard

true if \( n \) has no child

Precondition
\( n \) is a node

◆ left()

template<class T>
int ADS::CursorBinaryTree< T >::left ( int n) const
nodiscard

the left child of \( n \), NIL if missing

Precondition
\( n \) is a node

◆ nodes()

template<class T>
auto ADS::CursorBinaryTree< T >::nodes ( void ) const
inlinenodiscard

the nodes, in the order of their places, bidirectional

◆ parent()

template<class T>
int ADS::CursorBinaryTree< T >::parent ( int n) const
nodiscard

the parent of \( n \), NIL for the root

Precondition
\( n \) is a node

◆ preorder()

template<class T>
std::vector< int > ADS::CursorBinaryTree< T >::preorder ( void ) const
nodiscard

the nodes in preorder

◆ read()

template<class T>
const T & ADS::CursorBinaryTree< T >::read ( int n) const
nodiscard

the value of \( n \)

Precondition
\( n \) is a node

◆ remove()

template<class T>
void ADS::CursorBinaryTree< T >::remove ( int n)

removes the subtree of \( n \), detached from its parent, its places back to the free list, in time linear in its size

Precondition
\( n \) is a node

◆ right()

template<class T>
int ADS::CursorBinaryTree< T >::right ( int n) const
nodiscard

the right child of \( n \), NIL if missing

Precondition
\( n \) is a node

◆ root()

template<class T>
int ADS::CursorBinaryTree< T >::root ( void ) const
nodiscard

the place of the root, NIL if the tree is empty

◆ setRoot()

template<class T>
int ADS::CursorBinaryTree< T >::setRoot ( const T & v)

the root of value \( v \) of the empty tree

Precondition
the tree is empty

◆ size()

template<class T>
int ADS::CursorBinaryTree< T >::size ( void ) const
nodiscard

the number of nodes

◆ write()

template<class T>
void ADS::CursorBinaryTree< T >::write ( int n,
const T & v )

writes \( v \) in \( n \)

Precondition
\( n \) is a node

Member data documentation

◆ NIL

template<class T>
int ADS::CursorBinaryTree< T >::NIL = -1
staticconstexpr

no node


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