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

a binary tree with pointers to the parent and to the two children More...

#include <ADS/tree/BinaryTree.h>

Public types

using Item = T
 the type of the values

Public member functions

 BinaryTree (const T &v)
 a new tree, made of one node containing \( v \) (the Tree( v ) of the book)
 BinaryTree (const BinaryTree &)=delete
 no copy, since a tree owns its subtrees [see copy()]
BinaryTree & operator= (const BinaryTree &)=delete
 no copy, since a tree owns its subtrees [see copy()]
 ~BinaryTree ()
 destroys the two subtrees, i.e., the whole tree, with a stack of its own rather than by recursion
const T & read (void) const
 the value of the node
void write (const T &v)
 writes \( v \) in the node
BinaryTree * parent (void) const
 the parent; nil if the node is a root
BinaryTree * left (void) const
 the left child; nil if there is none
BinaryTree * right (void) const
 the right child; nil if there is none
void insertLeft (BinaryTree *t)
 inserts the tree \( t \) as the left child, which must be missing
void insertRight (BinaryTree *t)
 inserts the tree \( t \) as the right child, which must be missing, as insertLeft() does on the left
void deleteLeft (void)
 destroys the left subtree
void deleteRight (void)
 destroys the right subtree
BinaryTree * detachLeft (void)
 detaches the left subtree without destroying it: it becomes a root, owned by the caller; nil if there is none
BinaryTree * detachRight (void)
 detaches the right subtree without destroying it: it becomes a root, owned by the caller; nil if there is none, as detachLeft() does on the left

Detailed description

template<class T>
class ADS::BinaryTree< T >

a binary tree with pointers to the parent and to the two children

Member typedef documentation

◆ Item

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

the type of the values

Constructor and destructor documentation

◆ BinaryTree() [1/2]

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

a new tree, made of one node containing \( v \) (the Tree( v ) of the book)

◆ BinaryTree() [2/2]

template<class T>
ADS::BinaryTree< T >::BinaryTree ( const BinaryTree< T > & )
delete

no copy, since a tree owns its subtrees [see copy()]

◆ ~BinaryTree()

template<class T>
ADS::BinaryTree< T >::~BinaryTree ( )

destroys the two subtrees, i.e., the whole tree, with a stack of its own rather than by recursion

Member function documentation

◆ deleteLeft()

template<class T>
void ADS::BinaryTree< T >::deleteLeft ( void )

destroys the left subtree

◆ deleteRight()

template<class T>
void ADS::BinaryTree< T >::deleteRight ( void )

destroys the right subtree

◆ detachLeft()

template<class T>
BinaryTree * ADS::BinaryTree< T >::detachLeft ( void )
nodiscard

detaches the left subtree without destroying it: it becomes a root, owned by the caller; nil if there is none

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{detachLeft}() \cr 2 & \quad t \gets \mathit{left};\ \mathit{left} \gets \textbf{nil} \cr 3 & \quad \textbf{if}\ t \ne \textbf{nil}\ \textbf{then} \ t.\mathit{parent} \gets \textbf{nil} \cr 4 & \quad \textbf{return}\ t \end{array}} \]

Postcondition
the left child is missing and the result is nil or a root

◆ detachRight()

template<class T>
BinaryTree * ADS::BinaryTree< T >::detachRight ( void )
nodiscard

detaches the right subtree without destroying it: it becomes a root, owned by the caller; nil if there is none, as detachLeft() does on the left

Postcondition
the right child is missing and the result is nil or a root

◆ insertLeft()

template<class T>
void ADS::BinaryTree< T >::insertLeft ( BinaryTree< T > * t)

inserts the tree \( t \) as the left child, which must be missing

In pseudocode (section 5.5):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insertLeft}( t ) \cr 2 & \quad t.\mathit{parent} \gets \mathit{this};\ \mathit{left} \gets t \end{array}} \]

Precondition
\( t \) is a root, not this node nor the root of its tree (which would make a cycle), and the left child is missing
Postcondition
\( t \) is the left child

◆ insertRight()

template<class T>
void ADS::BinaryTree< T >::insertRight ( BinaryTree< T > * t)

inserts the tree \( t \) as the right child, which must be missing, as insertLeft() does on the left

Precondition
\( t \) is a root, not this node nor the root of its tree (which would make a cycle), and the right child is missing
Postcondition
\( t \) is the right child

◆ left()

template<class T>
BinaryTree * ADS::BinaryTree< T >::left ( void ) const
nodiscard

the left child; nil if there is none

◆ operator=()

template<class T>
BinaryTree & ADS::BinaryTree< T >::operator= ( const BinaryTree< T > & )
delete

no copy, since a tree owns its subtrees [see copy()]

◆ parent()

template<class T>
BinaryTree * ADS::BinaryTree< T >::parent ( void ) const
nodiscard

the parent; nil if the node is a root

◆ read()

template<class T>
const T & ADS::BinaryTree< T >::read ( void ) const
nodiscard

the value of the node

◆ right()

template<class T>
BinaryTree * ADS::BinaryTree< T >::right ( void ) const
nodiscard

the right child; nil if there is none

◆ write()

template<class T>
void ADS::BinaryTree< T >::write ( const T & v)

writes \( v \) in the node


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