| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifndef TSTTREE_H |
| 21 | #define TSTTREE_H |
| 22 | |
| 23 | template < class T > |
| 24 | class TsTTree |
| 25 | { |
| 26 | private: // members |
| 27 | /// element in the tree node |
| 28 | T elem; |
| 29 | /// pointer to left subtree |
| 30 | TsTTree *left; |
| 31 | /// pointer to right subtree |
| 32 | TsTTree *right; |
| 33 | |
| 34 | public: // interface |
| 35 | /// default c'tor |
| 36 | explicit TsTTree ( const T& Init, TsTTree *l = nullptr, TsTTree *r = nullptr ) |
| 37 | : elem(Init) |
| 38 | , left(l) |
| 39 | , right(r) |
| 40 | {} |
| 41 | /// no copy c'tor |
| 42 | TsTTree ( const TsTTree& ) = delete; |
| 43 | /// no assignment |
| 44 | TsTTree& operator = ( const TsTTree& ) = delete; |
| 45 | /// d'tor |
| 46 | ~TsTTree() = default; |
| 47 | |
| 48 | // access to members |
| 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 | }; // TsTTree |
| 69 | |
| 70 | /// delete the whole tree |
| 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 |