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

a binary tree in a positional vector More...

#include <ADS/tree/ArrayBinaryTree.h>

Classes

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

Public types

using Item = T
 the type of the values

Public member functions

 ArrayBinaryTree (void)=default
 the empty tree
 ArrayBinaryTree (const T &v)
 a tree of one node of value \( v \)
 ArrayBinaryTree (const T &v, const ArrayBinaryTree &l, const ArrayBinaryTree &r)
 a new root of value \( v \) with copies of \( l \) and \( r \) as its subtrees
bool empty (void) const
 true if the tree has no node
long root (void) const
 the root, 0, or NIL if the tree is empty
long parent (long i) const
 the parent \( \lfloor ( i - 1 ) / 2 \rfloor \) of \( i \), NIL for the root
long left (long i) const
 the left child \( 2 i + 1 \) of \( i \), NIL if there is none
long right (long i) const
 the right child \( 2 i + 2 \) of \( i \), NIL if there is none
bool isLeaf (long i) const
 true if \( i \) has no child
const T & read (long i) const
 the value of \( i \)
void write (long i, const T &v)
 writes \( v \) in \( i \)
void setRoot (const T &v)
 the root of value \( v \) of the empty tree
void insertLeft (long i, const ArrayBinaryTree &t)
 a copy of \( t \) as the left subtree of \( i \), which must be missing; \( t \) may be this tree, copied as it was before the call
void insertRight (long i, const ArrayBinaryTree &t)
 a copy of \( t \) as the right subtree of \( i \), which must be missing; \( t \) may be this tree, copied as it was before the call: the copy of insertLeft() from \( k = 2 i + 2 \)
void remove (long i)
 empties the subtree of \( i \), a previsit of its places
std::vector< long > preorder (void) const
 the places of the nodes in preorder
auto nodes (void) const
 the nodes, in the order of their places, bidirectional

Static public attributes

static constexpr long NIL = -1
 no node

Detailed description

template<class T>
class ADS::ArrayBinaryTree< T >

a binary tree in a positional vector

Member typedef documentation

◆ Item

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

the type of the values

Constructor and destructor documentation

◆ ArrayBinaryTree() [1/3]

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

the empty tree

◆ ArrayBinaryTree() [2/3]

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

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

◆ ArrayBinaryTree() [3/3]

template<class T>
ADS::ArrayBinaryTree< T >::ArrayBinaryTree ( const T & v,
const ArrayBinaryTree< T > & l,
const ArrayBinaryTree< T > & r )

a new root of value \( v \) with copies of \( l \) and \( r \) as its subtrees

Member function documentation

◆ empty()

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

true if the tree has no node

◆ insertLeft()

template<class T>
void ADS::ArrayBinaryTree< T >::insertLeft ( long i,
const ArrayBinaryTree< T > & t )

a copy of \( t \) as the left subtree of \( i \), which must be missing; \( t \) may be this tree, copied as it was before the call

In pseudocode (beyond the book), the place \( j \) of \( t \) copied to the place \( k \), from \( j = 0 \) and \( k = 2 i + 1 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{copy}( k , t , j ) \cr 2 & \quad \textbf{if}\ t \text{ has no node in } j\ \textbf{then return} \cr 3 & \quad A[ k ] \gets t.A[ j ]\quad \triangleright\ \text{the vector } \text{grown to } k \text{ if needed} \cr 4 & \quad \mathrm{copy}( 2 k + 1 , t , 2 j + 1 );\ \mathrm{copy}( 2 k + 2 , t , 2 j + 2 ) \end{array}} \]

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

◆ insertRight()

template<class T>
void ADS::ArrayBinaryTree< T >::insertRight ( long i,
const ArrayBinaryTree< T > & t )

a copy of \( t \) as the right subtree of \( i \), which must be missing; \( t \) may be this tree, copied as it was before the call: the copy of insertLeft() from \( k = 2 i + 2 \)

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

◆ isLeaf()

template<class T>
bool ADS::ArrayBinaryTree< T >::isLeaf ( long i) const
nodiscard

true if \( i \) has no child

Precondition
\( i \) is a node

◆ left()

template<class T>
long ADS::ArrayBinaryTree< T >::left ( long i) const
nodiscard

the left child \( 2 i + 1 \) of \( i \), NIL if there is none

Precondition
\( i \) is a node

◆ nodes()

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

the nodes, in the order of their places, bidirectional

◆ parent()

template<class T>
long ADS::ArrayBinaryTree< T >::parent ( long i) const
nodiscard

the parent \( \lfloor ( i - 1 ) / 2 \rfloor \) of \( i \), NIL for the root

Precondition
\( i \) is a node

◆ preorder()

template<class T>
std::vector< long > ADS::ArrayBinaryTree< T >::preorder ( void ) const
nodiscard

the places of the nodes in preorder

◆ read()

template<class T>
const T & ADS::ArrayBinaryTree< T >::read ( long i) const
nodiscard

the value of \( i \)

Precondition
\( i \) is a node

◆ remove()

template<class T>
void ADS::ArrayBinaryTree< T >::remove ( long i)

empties the subtree of \( i \), a previsit of its places

Precondition
\( i \) is a node

◆ right()

template<class T>
long ADS::ArrayBinaryTree< T >::right ( long i) const
nodiscard

the right child \( 2 i + 2 \) of \( i \), NIL if there is none

Precondition
\( i \) is a node

◆ root()

template<class T>
long ADS::ArrayBinaryTree< T >::root ( void ) const
nodiscard

the root, 0, or NIL if the tree is empty

◆ setRoot()

template<class T>
void ADS::ArrayBinaryTree< T >::setRoot ( const T & v)

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

Precondition
the tree is empty

◆ write()

template<class T>
void ADS::ArrayBinaryTree< T >::write ( long i,
const T & v )

writes \( v \) in \( i \)

Precondition
\( i \) is a node

Member data documentation

◆ NIL

template<class T>
long ADS::ArrayBinaryTree< T >::NIL = -1
staticconstexpr

no node


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