| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef TSTTREE_H |
| 21 |
#define TSTTREE_H |
| 22 |
|
| 23 |
template < class T > |
| 24 |
class TsTTree |
| 25 |
{ |
| 26 |
private: |
| 27 |
|
| 28 |
T elem; |
| 29 |
|
| 30 |
TsTTree *left; |
| 31 |
|
| 32 |
TsTTree *right; |
| 33 |
|
| 34 |
public: |
| 35 |
|
| 36 |
explicit TsTTree ( const T& Init, TsTTree *l = nullptr, TsTTree *r = nullptr ) |
| 37 |
: elem(Init) |
| 38 |
, left(l) |
| 39 |
, right(r) |
| 40 |
{} |
| 41 |
|
| 42 |
TsTTree ( const TsTTree& ) = delete; |
| 43 |
|
| 44 |
TsTTree& operator = ( const TsTTree& ) = delete; |
| 45 |
|
| 46 |
~TsTTree() = default; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
T& Element ( void ) { return elem; } |
| 51 |
const T& Element ( void ) const { return elem; } |
| 52 |
|
| 53 |
TsTTree* Left ( void ) const { return left; } |
| 54 |
TsTTree* Right ( void ) const { return right; } |
| 55 |
|
| 56 |
void SetLeft ( TsTTree *l ) { left = l; } |
| 57 |
void SetRight ( TsTTree *r ) { right = r; } |
| 58 |
|
| 59 |
TsTTree* clone ( void ) const |
| 60 |
{ |
| 61 |
TsTTree* p = new TsTTree(Element()); |
| 62 |
if ( left ) |
| 63 |
p->SetLeft(left->clone()); |
| 64 |
if ( right ) |
| 65 |
p->SetRight(right->clone()); |
| 66 |
return p; |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
template <typename T> |
| 72 |
void deleteTree ( TsTTree<T>* t ) |
| 73 |
{ |
| 74 |
if ( t ) |
| 75 |
{ |
| 76 |
deleteTree(t->Left()); |
| 77 |
deleteTree(t->Right()); |
| 78 |
delete t; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
#endif |