00001 /*---------------------------------------------------------------------+ 00002 | Library QgarLib, graphics analysis and recognition | 00003 | Copyright (C) 2002 Qgar Project, LORIA | 00004 | | 00005 | This library is free software; you can redistribute it and/or | 00006 | modify it under the terms of the GNU Lesser General Public | 00007 | License version 2.1, as published by the Free Software Foundation. | 00008 | | 00009 | This library is distributed in the hope that it will be useful, | 00010 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00011 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 00012 | See the GNU Lesser General Public License for more details. | 00013 | | 00014 | The GNU Lesser General Public License is included in the file | 00015 | LICENSE.LGPL, in the root directory of the Qgar packaging. See | 00016 | http://www.gnu.org/licenses/lgpl.html for the terms of the licence. | 00017 | To receive a paper copy, write to the Free Software Foundation, | 00018 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. | 00019 | | 00020 | Contact Project Qgar for any information: | 00021 | LORIA - équipe Qgar | 00022 | B.P. 239, 54506 Vandoeuvre-lès-Nancy Cedex, France | 00023 | email: qgar-contact@loria.fr | 00024 | http://www.qgar.org/ | 00025 *---------------------------------------------------------------------*/ 00026 00027 00028 #ifndef __GENTREE_H_INCLUDED__ 00029 #define __GENTREE_H_INCLUDED__ 00030 00031 00032 /** 00033 * @file GenTree.H 00034 * @brief Header file of classes qgar::GenTreeNode and qgar::GenTree. 00035 * 00036 * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a> 00037 * @date Jul 3, 2001 16:23 00038 * @since Qgar 2.0 00039 */ 00040 00041 00042 // For RCS/CVS use: Do not delete 00043 /* $Id: GenTree.H,v 1.23 2005/09/15 18:19:35 masini Exp $ */ 00044 00045 00046 00047 // QGAR 00048 #include <qgarlib/QgarErrorUser.H> 00049 00050 00051 00052 namespace qgar 00053 { 00054 00055 00056 /*---------------------------------------------------------------------* 00057 | | 00058 | C L A S S G E N T R E E N O D E | 00059 | | 00060 *---------------------------------------------------------------------*/ 00061 00062 00063 /** 00064 * @class GenTreeNode GenTree.H 00065 * 00066 * @ingroup DS_GRAPH 00067 * 00068 * @brief Template class for a node of a Qgar Tree 00069 * (see class qgar::GenTree). 00070 * 00071 * A node is a container including: 00072 * - a link to its parent node in the tree, 00073 * - a link to its left sibling node in the tree, 00074 * - a link to its right sibling node in the tree, 00075 * - a link to its first (leftmost) child node in the tree, 00076 * - data of type <b>T</b>, representing the information 00077 * associated with the node. 00078 @verbatim 00079 o parent 00080 /|\ 00081 / | \ 00082 / / | \ \ 00083 / / | \ \ 00084 o ... o NODE o ... o 00085 ^ /|\ ^ 00086 left sibling / | \ right sibling 00087 / | \ 00088 / | \ \ 00089 first child > o o o ... o 00090 /|\ /|\ /|\ 00091 @endverbatim 00092 */ 00093 template <class T> class GenTreeNode 00094 { 00095 // ------------------------------------------------------------------- 00096 // P U B L I C M E M B E R S 00097 // ------------------------------------------------------------------- 00098 public: 00099 00100 /** @name Constructors */ 00101 // ============ 00102 //@{ 00103 00104 /** 00105 * @brief Default constructor. 00106 * 00107 * Create an empty node with no link and default data. 00108 */ 00109 GenTreeNode(); 00110 00111 /** 00112 * @brief Create a node with given data and no link. 00113 * @param aData data to be stored in the node 00114 */ 00115 GenTreeNode(const T& aData); 00116 00117 /** 00118 * @brief Create a node with given links and data. 00119 * 00120 * @param aPParent pointer to the parent in tree 00121 * @param aPLSibling pointer to the left sibling in tree 00122 * @param aPRSibling pointer to the right sibling in tree 00123 * @param aPFirstChild pointer to the FirstChild in tree 00124 * @param aData data stored in the node 00125 */ 00126 GenTreeNode(GenTreeNode<T>* aPParent, 00127 GenTreeNode<T>* aPLSibling, 00128 GenTreeNode<T>* aPRSibling, 00129 GenTreeNode<T>* aPFirstChild, 00130 const T& aData); 00131 00132 /** 00133 * @brief Copy constructor. 00134 * 00135 * @param aNode a node 00136 */ 00137 GenTreeNode(const GenTreeNode<T>& aNode); 00138 00139 //@} 00140 00141 00142 /** @name Destructor */ 00143 // ========== 00144 //@{ 00145 00146 /** 00147 * @brief Virtual destructor. 00148 */ 00149 virtual ~GenTreeNode(); 00150 00151 //@} 00152 00153 00154 /** @name Access to links */ 00155 // =============== 00156 //@{ 00157 00158 /** 00159 * @brief Get link to parent. 00160 */ 00161 inline GenTreeNode<T>* pParent() const; 00162 00163 /** 00164 * @brief Get link to left sibling. 00165 */ 00166 inline GenTreeNode<T>* pLSibling() const; 00167 00168 /** 00169 * @brief Get link to right sibling. 00170 */ 00171 inline GenTreeNode<T>* pRSibling() const; 00172 00173 /** 00174 * @brief Get link to first child. 00175 */ 00176 inline GenTreeNode<T>* pFirstChild() const; 00177 00178 //@} 00179 00180 00181 /** @name Set links */ 00182 // ========= 00183 //@{ 00184 00185 /** 00186 * @brief Set link to parent. 00187 * 00188 * @param aPNode a pointer to a node 00189 */ 00190 inline void setPParent(GenTreeNode<T>* aPNode); 00191 00192 /** 00193 * @brief Set link to left sibling. 00194 * 00195 * @param aPNode a pointer to a node 00196 */ 00197 inline void setPLSibling(GenTreeNode<T>* aPNode); 00198 00199 /** 00200 * @brief Set link to right sibling. 00201 * 00202 * @param aPNode a pointer to a node 00203 */ 00204 inline void setPRSibling(GenTreeNode<T>* aPNode); 00205 00206 /** 00207 * @brief Set link to first child. 00208 * 00209 * @param aPNode a pointer to a node 00210 */ 00211 inline void setPFirstChild(GenTreeNode<T>* aPNode); 00212 00213 //@} 00214 00215 00216 /** @name Access to data stored in nodes */ 00217 // ============================== 00218 //@{ 00219 00220 /** 00221 * @brief Get data. 00222 */ 00223 inline const T& accessData() const; 00224 00225 /** 00226 * @brief Get a copy of data. 00227 */ 00228 inline T data() const; 00229 00230 //@} 00231 00232 00233 /** @name Store data in nodes */ 00234 // =================== 00235 //@{ 00236 00237 /** 00238 * @brief Store data in current node. 00239 * 00240 * @param aData data to be stored 00241 */ 00242 inline void setData(const T& aData); 00243 00244 //@} 00245 00246 00247 /** @name Operators */ 00248 // ========= 00249 //@{ 00250 00251 /** 00252 * @brief Assignment. 00253 * 00254 * @param aNode a node 00255 */ 00256 GenTreeNode& operator=(const GenTreeNode<T>& aNode); 00257 00258 //@} 00259 00260 00261 // ------------------------------------------------------------------- 00262 // P R O T E C T E D M E M B E R S 00263 // ------------------------------------------------------------------- 00264 protected: 00265 00266 00267 /** @name Representation of a node */ 00268 // ======================== 00269 //@{ 00270 00271 /** 00272 * @brief Pointer to parent. 00273 */ 00274 GenTreeNode<T>* _pParent; 00275 00276 /** 00277 * @brief Pointer to left sibling. 00278 */ 00279 GenTreeNode<T>* _pLSibling; 00280 00281 /** 00282 * @brief Pointer to right sibling. 00283 */ 00284 GenTreeNode<T>* _pRSibling; 00285 00286 /** 00287 * @brief Pointer to first (leftmost) child. 00288 */ 00289 GenTreeNode<T>* _pFirstChild; 00290 00291 /** 00292 * @brief Data contained by node. 00293 */ 00294 T _data; 00295 00296 //@} 00297 00298 00299 // ------------------------------------------------------------------- 00300 00301 }; // Class GenTreeNode 00302 00303 00304 00305 00306 00307 /*---------------------------------------------------------------------* 00308 | | 00309 | C L A S S G E N T R E E | 00310 | | 00311 *---------------------------------------------------------------------*/ 00312 00313 00314 /** 00315 * @class GenTree GenTree.H "qgarlib/GenTree.H" 00316 * 00317 * @ingroup DS_GRAPH 00318 * 00319 * @brief A tree whose nodes contain data of type <b>T</b>. 00320 * 00321 * Such a tree is represented by: 00322 * - a pointer to the root node of the tree, 00323 * - a pointer to the current node, <i>i.e.</i> last accessed node. 00324 * 00325 * See class qgar::GenTreeNode for the way the inner nodes 00326 * are linked together. 00327 * 00328 * @warning 00329 * <ul> 00330 * 00331 * <li><p> 00332 * <b>For efficiency reasons, the validity of pointers given as 00333 * arguments to function members is not checked.</b> 00334 * In particular: 00335 * - pointers may be equal to <b>0</b>, which will undoubtedly be 00336 * the cause of a further program abortion, 00337 * - pointed nodes may not belong to the current graph, 00338 * with similar consequences. 00339 * </p></li> 00340 * 00341 * <li><p> 00342 * <b>The deletion of a tree causes the deletion of all its nodes.</b> 00343 * Consequently, a program abortion may occur when: 00344 * - an ill-formed tree contains multiple occurrences of 00345 * (pointers to) a same node, or contains null pointers, 00346 * - nodes are explicitely deleted, using operator <tt>delete</tt>, 00347 * before exiting the block of program containing the tree. 00348 * </p></li> 00349 * 00350 * </ul> 00351 * 00352 * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a> 00353 * @date Jul 3, 2001 16:23 00354 * @since Qgar 2.0 00355 */ 00356 template <class T> class GenTree 00357 { 00358 // ------------------------------------------------------------------- 00359 // T Y P E D E F I N I T I O N S 00360 // ------------------------------------------------------------------- 00361 public: 00362 00363 00364 /** @name Types */ 00365 // ===== 00366 //@{ 00367 00368 /** 00369 * @brief Type of the data stored in nodes. 00370 */ 00371 typedef T value_type; 00372 00373 /** 00374 * @brief Reference to qgar::GenTree::value_type. 00375 */ 00376 typedef value_type& reference; 00377 00378 /** 00379 * @brief Constant reference to qgar::GenTree::value_type. 00380 */ 00381 typedef const value_type& const_reference; 00382 00383 /** 00384 * @brief Pointer to qgar::GenTree::value_type. 00385 */ 00386 typedef value_type* pointer; 00387 00388 /** 00389 * @brief Constant pointer to qgar::GenTree::value_type. 00390 */ 00391 typedef const value_type* const_pointer; 00392 00393 //@} 00394 00395 00396 // ------------------------------------------------------------------- 00397 // P U B L I C M E M B E R S 00398 // ------------------------------------------------------------------- 00399 public: 00400 00401 00402 /** @name Constructors */ 00403 // ============ 00404 //@{ 00405 00406 /** 00407 * @brief Default constructor. 00408 * 00409 * Create an empty tree. 00410 */ 00411 GenTree(); 00412 00413 /** 00414 * @brief Copy constructor. 00415 * 00416 * The current node of the resulting tree is its root. 00417 * 00418 * @warning Perform a <b>deep copy</b>: 00419 * All the nodes of the given tree are duplicated. 00420 */ 00421 GenTree(const GenTree<value_type>& aTree); 00422 00423 /** 00424 * @brief Create a tree with a single node 00425 * containing the given data. 00426 * 00427 * This node becomes the current node. 00428 * 00429 * @param aData data to be stored in the node 00430 */ 00431 GenTree(const_reference aData); 00432 00433 /** 00434 * @brief Create a tree from a given node. 00435 * 00436 * If the pointer is <b>0</b>, create an empty tree. 00437 * Otherwise, the given node is considered as a root of a tree, 00438 * which is copied to become the current tree. 00439 * The current node is the root. 00440 * 00441 * @param aPNode a pointer to a node 00442 * 00443 * @warning <b>Perform a deep copy</b>. 00444 */ 00445 GenTree(GenTreeNode<value_type>* aPNode); 00446 00447 //@} 00448 00449 00450 /** @name Destructor */ 00451 // ========== 00452 //@{ 00453 00454 /** 00455 * @brief Virtual destructor. 00456 */ 00457 virtual ~GenTree(); 00458 00459 //@} 00460 00461 00462 /** @name Predicates */ 00463 // ========== 00464 //@{ 00465 00466 /** 00467 * @brief Is tree empty? 00468 */ 00469 bool empty() const; 00470 00471 //@} 00472 00473 00474 // ======================================================= 00475 /** @name Access to links 00476 <b>WARNING</b> 00477 The behavior of functions taking a pointer to a node 00478 as extra argument is undefined if: 00479 <ul> 00480 <li>The tree is empty,</li> 00481 <li>or the given node does not belong to the tree.</li> 00482 </ul> 00483 */ 00484 // ======================================================= 00485 //@{ 00486 00487 /** 00488 * @brief Get pointer to the root of the tree. 00489 */ 00490 inline GenTreeNode<value_type>* pRoot() const; 00491 00492 /** 00493 * @brief Get pointer to the current node. 00494 */ 00495 inline GenTreeNode<value_type>* pCurrent() const; 00496 00497 /** 00498 * @brief Get pointer to the parent of the current node. 00499 */ 00500 inline GenTreeNode<value_type>* pParent() const; 00501 00502 /** 00503 * @brief Get pointer to the parent of the given node. 00504 * 00505 * @param aPNode a pointer to a node 00506 */ 00507 inline GenTreeNode<value_type>* 00508 pParent(GenTreeNode<value_type>* aPNode) const; 00509 00510 /** 00511 * @brief Get pointer to the left sibling of the current node. 00512 */ 00513 inline GenTreeNode<value_type>* pLSibling() const; 00514 00515 /** 00516 * @brief Get pointer to the left sibling of the given node. 00517 * 00518 * @param aPNode a pointer to a node 00519 */ 00520 inline GenTreeNode<value_type>* 00521 pLSibling(GenTreeNode<value_type>* aPNode) const; 00522 00523 /** 00524 * @brief Get pointer to the right sibling of the current node. 00525 */ 00526 inline GenTreeNode<value_type>* pRSibling() const; 00527 00528 /** 00529 * @brief Get pointer to the right sibling of the given node. 00530 * 00531 * @param aPNode a pointer to a node 00532 */ 00533 inline GenTreeNode<value_type>* 00534 pRSibling(GenTreeNode<value_type>* aPNode) const; 00535 00536 /** 00537 * @brief Get pointer to the first child of the current node. 00538 */ 00539 inline GenTreeNode<value_type>* pFirstChild() const; 00540 00541 /** 00542 * @brief Get pointer to the first child of the given node. 00543 * 00544 * @param aPNode a pointer to a node 00545 */ 00546 inline GenTreeNode<value_type>* 00547 pFirstChild(GenTreeNode<value_type>* aPNode) const; 00548 00549 //@} 00550 00551 00552 // ======================================================= 00553 /** @name Access to data 00554 <b>WARNING</b> 00555 The behavior of functions taking a pointer to a node 00556 as extra extra argument is undefined if: 00557 <ul> 00558 <li>The tree is empty,</li> 00559 <li>or the given node does not belong to the tree.</li> 00560 </ul> 00561 */ 00562 // ======================================================= 00563 //@{ 00564 00565 /** 00566 * @brief Get data stored in current node. 00567 */ 00568 inline const_reference accessData() const; 00569 00570 /** 00571 * @brief Get data stored in given node. 00572 * 00573 * @param aPNode a pointer to a node 00574 */ 00575 inline const_reference accessData(GenTreeNode<value_type>* aPNode) const; 00576 00577 /** 00578 * @brief Get a copy of the data stored in current node. 00579 */ 00580 inline value_type data() const; 00581 00582 /** 00583 * @brief Get a copy of the data stored in current node. 00584 * 00585 * @param aPNode a pointer to a node 00586 */ 00587 inline value_type data(GenTreeNode<value_type>* aPNode) const; 00588 00589 //@} 00590 00591 00592 // =================================================================== 00593 /** @name Moving in the tree 00594 <b>WARNING</b> 00595 <ul> 00596 <li>If the destination node does not exist, the pointer 00597 to the current node is set to <i>0</i>.</li> 00598 <li>When a pointer to a node is given as argument, 00599 the corresponding node is supposed to belong to the tree!</li> 00600 </ul> 00601 */ 00602 // =================================================================== 00603 //@{ 00604 00605 /** 00606 * @brief The root becomes the current node. 00607 */ 00608 inline void gotoRoot(); 00609 00610 /** 00611 * @brief The parent of the current node becomes the current node. 00612 */ 00613 inline void gotoParent(); 00614 00615 /** 00616 * @brief The parent of the given node becomes the current node. 00617 * 00618 * @param aPNode a pointer to a node 00619 */ 00620 inline void gotoParent(GenTreeNode<value_type>* aPNode); 00621 00622 /** 00623 * @brief The left sibling of the current node becomes the current node. 00624 */ 00625 inline void gotoLSibling(); 00626 00627 /** 00628 * @brief The left sibling of the given node becomes the current node. 00629 * 00630 * @param aPNode a pointer to a node 00631 */ 00632 inline void gotoLSibling(GenTreeNode<value_type>* aPNode); 00633 00634 /** 00635 * @brief The right sibling of the current node becomes the current node. 00636 */ 00637 inline void gotoRSibling(); 00638 00639 /** 00640 * @brief The right sibling of the given node becomes the current node. 00641 * 00642 * @param aPNode a pointer to a node 00643 */ 00644 inline void gotoRSibling(GenTreeNode<value_type>* aPNode); 00645 00646 /** 00647 * @brief The first child of the current node becomes the current node. 00648 */ 00649 inline void gotoFirstChild(); 00650 00651 /** 00652 * @brief The first child of the given node becomes the current node. 00653 * 00654 * @param aPNode a pointer to a node 00655 */ 00656 inline void gotoFirstChild(GenTreeNode<value_type>* aPNode); 00657 //@} 00658 00659 00660 // ======================================================= 00661 /** @name Store data in nodes 00662 <b>WARNING</b> 00663 The behavior of functions taking a pointer to a node 00664 as extra extra argument is undefined if: 00665 <ul> 00666 <li>The tree is empty,</li> 00667 <li>or the given node does not belong to the tree.</li> 00668 </ul> 00669 */ 00670 // ======================================================= 00671 //@{ 00672 00673 /** 00674 * @brief Store data in current node. 00675 * 00676 * @param aData data to be stored in current node 00677 */ 00678 inline void setData(const_reference aData); 00679 00680 /** 00681 * @brief Store data in given node. 00682 * 00683 * @param aData data to be stored in current node 00684 * @param aPNode a pointer to a node 00685 */ 00686 inline void 00687 setData(const_reference aData, GenTreeNode<value_type>* aPNode); 00688 00689 //@} 00690 00691 00692 // ========================================================== 00693 /** @name Insert new nodes 00694 <b>WARNING</b> 00695 When a pointer to a node is given as argument, 00696 the corresponding node is supposed to belong to the tree! 00697 */ 00698 // ========================================================== 00699 //@{ 00700 00701 /** 00702 * @brief Insert a node containing given data 00703 * as parent of current root. 00704 * 00705 @verbatim 00706 NEW 00707 | 00708 | 00709 ROOT ===> ROOT 00710 /|\ /|\ 00711 @endverbatim 00712 * 00713 * If the tree is empty, the new node becomes the only node 00714 * of the tree, <i>i.e.</i> the root and the current node. 00715 * 00716 * Otherwise: 00717 * - The new node becomes the parent of the current root. 00718 * - The current root becomes the first child of the new node. 00719 * - The new node becomes the new root of the tree. 00720 * - The current node remains unchanged. 00721 * 00722 * @param aData data to be stored in the new node 00723 */ 00724 void insertParent(const_reference aData); 00725 00726 00727 /** 00728 * @brief Insert a node containing given data as left sibling 00729 * of current node, <b>which must not be the root</b>. 00730 * 00731 @verbatim 00732 p p 00733 /|\ /|\ 00734 / | \ / | \ 00735 / | \ ===> / | \ 00736 / | \ / | \ 00737 / | \ / | \ 00738 ->ls<-->CUR<-->rs<- ->ls<-->NEW<-->CUR<-->rs<- 00739 /|\ /|\ /|\ /|\ /|\ /|\ 00740 @endverbatim 00741 * 00742 * If the tree is empty, the new node becomes the only node 00743 * of the tree, <i>i.e.</i> the root and the current node. 00744 * 00745 * Otherwise: 00746 * - If the current node has a left sibling, the latter becomes the left 00747 * sibling of the new node; otherwise, the new node becomes the first 00748 * child of the parent node. 00749 * - The current node becomes the right sibling of the new node. 00750 * - The parent of the current node becomes the parent of the new node. 00751 * - The current node remains unchanged. 00752 * 00753 * @param aData data to be stored in the new node 00754 * 00755 * @exception qgar::QgarErrorUser (current node is the root) 00756 */ 00757 void insertLSibling(const_reference aData) throw(QgarErrorUser); 00758 00759 00760 /** 00761 * @brief Insert a node containing given data as left sibling 00762 * of given node, <b>which must not be the root</b>. 00763 * 00764 @verbatim 00765 p p 00766 /|\ /|\ 00767 / | \ / | \ 00768 / | \ ===> / | \ 00769 / | \ / | \ 00770 / | \ / | \ 00771 ->ls<-->GIV<-->rs<- ->ls<-->NEW<-->GIV<-->rs<- 00772 /|\ /|\ /|\ /|\ /|\ /|\ 00773 @endverbatim 00774 * 00775 * - If the given node has a left sibling, the latter becomes the left 00776 * sibling of the new node; otherwise, the new node becomes the first 00777 * child of the parent node. 00778 * - The given node becomes the right sibling of the new node. 00779 * - The parent of the given node becomes the parent of the new node. 00780 * - The given node becomes the current node 00781 * 00782 * @param aData data to be stored in the new node 00783 * @param aPNode a pointer to a node 00784 * 00785 * @exception qgar::QgarErrorUser (current node is the root) 00786 */ 00787 inline void insertLSibling(const_reference aData, 00788 GenTreeNode<value_type>* aPNode) 00789 throw(QgarErrorUser); 00790 00791 00792 /** 00793 * @brief Insert a node containing given data as right sibling 00794 * of current node, <b>which must not be the root</b>. 00795 * 00796 @verbatim 00797 p p 00798 /|\ /|\ 00799 / | \ / | \ 00800 / | \ ===> / | \ 00801 / | \ / | \ 00802 / | \ / | \ 00803 ->ls<-->CUR<-->rs<- ->ls<-->CUR<-->NEW<-->rs<- 00804 /|\ /|\ /|\ /|\ /|\ /|\ 00805 @endverbatim 00806 * 00807 * If the tree is empty, the new node becomes the only node 00808 * of the tree, <i>i.e.</i> the root and the current node. 00809 * 00810 * Otherwise: 00811 * - The right sibling of the current node becomes the right 00812 * sibling of the new node. 00813 * - The current node becomes the left sibling of the new node. 00814 * - The parent of the current node becomes the parent of the new node. 00815 * - The current node remains unchanged. 00816 * 00817 * @param aData data to be stored in the new node 00818 * 00819 * @exception qgar::QgarErrorUser (current node is the root) 00820 */ 00821 void insertRSibling(const_reference aData) throw(QgarErrorUser); 00822 00823 00824 /** 00825 * @brief Insert a node containing given data as right sibling 00826 * of given node, <b>which must not be the root</b>. 00827 * 00828 @verbatim 00829 p p 00830 /|\ /|\ 00831 / | \ / | \ 00832 / | \ ===> / | \ 00833 / | \ / | \ 00834 / | \ / | \ 00835 ->ls<-->GIV<-->rs<- ->ls<-->GIV<-->NEW<-->rs<- 00836 /|\ /|\ /|\ /|\ /|\ /|\ 00837 @endverbatim 00838 * 00839 * - The right sibling of the given node becomes the right 00840 * sibling of the new node. 00841 * - The given node becomes the left sibling of the new node. 00842 * - The parent of the given node becomes the parent of the new node. 00843 * - The given node becomes the current node. 00844 * 00845 * @param aData data to be stored in the new node 00846 * @param aPNode a pointer to a node 00847 * 00848 * @exception qgar::QgarErrorUser (current node is the root) 00849 */ 00850 inline void insertRSibling(const_reference aData, 00851 GenTreeNode<value_type>* aPNode) 00852 throw(QgarErrorUser); 00853 00854 00855 /** 00856 * @brief Insert a node containing given data as first child 00857 * of current node. 00858 * 00859 @verbatim 00860 CUR CUR 00861 |\ /|\ 00862 | \ / | \ 00863 | \ ===> / | \ 00864 | \ / | \ 00865 | \ / | \ 00866 fc<-->rs<- NEW<-->fc<-->rs<- 00867 /|\ /|\ /|\ /|\ 00868 @endverbatim 00869 * 00870 * If the tree is empty, the new node becomes the only node 00871 * of the tree, <i>i.e.</i> the root and the current node. 00872 * 00873 * Otherwise: 00874 * - The first child of the current node becomes the right sibling 00875 * of the given node. 00876 * - The new node becomes the first child of the current node. 00877 * - The current node remains unchanged. 00878 * 00879 * @param aData data to be stored in the new node 00880 */ 00881 void insertFirstChild(const_reference aData); 00882 00883 00884 /** 00885 * @brief Insert a node containing given data 00886 * as first child of given node. 00887 * 00888 @verbatim 00889 GIV GIV 00890 |\ /|\ 00891 | \ / | \ 00892 | \ ===> / | \ 00893 | \ / | \ 00894 | \ / | \ 00895 fc<-->rs<- NEW<-->fc<-->rs<- 00896 /|\ /|\ /|\ /|\ 00897 @endverbatim 00898 * 00899 * - The first child of the given node becomes the right sibling 00900 * of the given node. 00901 * - The new node becomes the first child of the given node. 00902 * - The given node becomes the current node. 00903 * 00904 * @param aData data to be stored in the new node 00905 * @param aPNode a pointer to a node 00906 */ 00907 inline void insertFirstChild(const_reference aData, 00908 GenTreeNode<value_type>* aPNode); 00909 00910 //@} 00911 00912 00913 // =================================================================== 00914 /** @name Merge 00915 <b>WARNING</b> 00916 <ul> 00917 <li>All the nodes of the given tree are duplicated.</li> 00918 <li>When a pointer to a node is given as argument, 00919 the corresponding node is supposed to belong to the tree!</li> 00920 </ul> 00921 */ 00922 // =================================================================== 00923 //@{ 00924 00925 /** 00926 * @brief Merge given tree as left sibling of current node, 00927 * <b>which must not be the root</b>. 00928 * 00929 @verbatim 00930 p p 00931 /|\ /|\ 00932 / | \ / | \ 00933 / | \ ===> / | \ 00934 / | \ / | \ 00935 / | \ / | \ 00936 ->ls<-->CUR<-->rs<- ->ls<-->GIV<-->CUR<-->rs<- 00937 /|\ /|\ /|\ /|\ /|\ /|\ /|\ 00938 / | \ 00939 c1 c2 c3 00940 GIV 00941 /|\ 00942 / | \ 00943 c1 c2 c3 00944 @endverbatim 00945 * 00946 * If the given tree is empty, the function has no effect. 00947 * 00948 * If the current tree is empty, the given tree becomes the current tree. 00949 * The new root becomes the current node. 00950 * 00951 * Otherwise: 00952 * - The left sibling of the current node becomes the left sibling 00953 * of the root of the given tree. 00954 * - The current node becomes the right sibling of the root of the given tree. 00955 * - The parent of the current node becomes the parent 00956 * of the root of the given tree. 00957 * - The current node remains unchanged. 00958 * 00959 * @param aTree a tree 00960 * 00961 * @exception qgar::QgarErrorUser (current node is the root) 00962 */ 00963 void mergeLSibling(const GenTree<value_type>& aTree) throw(QgarErrorUser); 00964 00965 00966 /** 00967 * @brief Merge given tree as left sibling of given node, 00968 * <b>which must not be the root</b>. 00969 * 00970 @verbatim 00971 p p 00972 /|\ /|\ 00973 / | \ / | \ 00974 / | \ ===> / | \ 00975 / | \ / | \ 00976 / | \ / | \ 00977 ->ls<-->GIV<-->rs<- ->ls<-->NEW<-->GIV<-->rs<- 00978 /|\ /|\ /|\ /|\ /|\ /|\ /|\ 00979 / | \ 00980 c1 c2 c3 00981 NEW 00982 /|\ 00983 / | \ 00984 c1 c2 c3 00985 @endverbatim 00986 * 00987 * If the given tree is empty, the function has no effect. 00988 * 00989 * Otherwise: 00990 * - The left sibling of the given node becomes the left sibling 00991 * of the root of the given tree. 00992 * - The given node becomes the right sibling of the root of the given tree. 00993 * - The parent of the given node becomes the parent 00994 * of the root of the given tree. 00995 * - The given node becomes the current node. 00996 * 00997 *.@warning The given node is supposed to belong 00998 * to the current tree! 00999 * 01000 * @param aTree a tree 01001 * @param aPNode a pointer to a node 01002 * 01003 * @exception qgar::QgarErrorUser (current node is the root) 01004 */ 01005 void mergeLSibling(const GenTree<value_type>& aTree, 01006 GenTreeNode<value_type>* aPNode) 01007 throw(QgarErrorUser); 01008 01009 01010 /** 01011 * @brief Merge given tree as right sibling of current node, 01012 * <b>which must not be the root</b>. 01013 * 01014 @verbatim 01015 p p 01016 /|\ /|\ 01017 / | \ / | \ 01018 / | \ ===> / | \ 01019 / | \ / | \ 01020 / | \ / | \ 01021 ->ls<-->CUR<-->rs<- ->ls<-->CUR<-->GIV<-->rs<- 01022 /|\ /|\ /|\ /|\ /|\ /|\ /|\ 01023 / | \ 01024 c1 c2 c3 01025 GIV 01026 /|\ 01027 / | \ 01028 c1 c2 c3 01029 @endverbatim 01030 * 01031 * If the given tree is empty, the function has no effect. 01032 * 01033 * If the current tree is empty, the given tree becomes the current tree. 01034 * The new root becomes the current node. 01035 * 01036 * Otherwise: 01037 * - The right sibling of the current node becomes the right 01038 * sibling of the root of the given tree. 01039 * - The current node becomes the left sibling of the root of the given tree. 01040 * - The parent of the current node becomes the parent 01041 * of the root of the given tree. 01042 * - The current node remains unchanged. 01043 * 01044 * @param aTree a tree 01045 * 01046 * @exception qgar::QgarErrorUser (current node is the root) 01047 */ 01048 void mergeRSibling(const GenTree<value_type>& aTree) throw(QgarErrorUser); 01049 01050 01051 /** 01052 * @brief Merge given tree as right sibling of current node, 01053 * <b>which must not be the root</b>. 01054 * 01055 @verbatim 01056 p p 01057 /|\ /|\ 01058 / | \ / | \ 01059 / | \ ===> / | \ 01060 / | \ / | \ 01061 / | \ / | \ 01062 ->ls<-->GIV<-->rs<- ->ls<-->GIV<-->NEW<-->rs<- 01063 /|\ /|\ /|\ /|\ /|\ /|\ /|\ 01064 / | \ 01065 c1 c2 c3 01066 NEW 01067 /|\ 01068 / | \ 01069 c1 c2 c3 01070 @endverbatim 01071 * 01072 * If the given tree is empty, the function has no effect. 01073 * 01074 * Otherwise: 01075 * - The right sibling of the given node becomes the right 01076 * sibling of the root of the given tree. 01077 * - The given node becomes the left sibling of the root of the given tree. 01078 * - The parent of the given node becomes the parent 01079 * of the root of the given tree. 01080 * - The given node becomes the current node. 01081 * 01082 * @param aTree a tree 01083 * @param aPNode a pointer to a node 01084 * 01085 * @warning The given node is supposed to belong 01086 * to the current tree! 01087 * 01088 * @exception qgar::QgarErrorUser (current node is the root) 01089 */ 01090 void mergeRSibling(const GenTree<value_type>& aTree, 01091 GenTreeNode<value_type>* aPNode) 01092 throw(QgarErrorUser); 01093 01094 01095 /** 01096 * @brief Merge given tree as first child of current node. 01097 * 01098 @verbatim 01099 CUR CUR 01100 |\ /|\ 01101 | \ / | \ 01102 | \ ===> / | \ 01103 | \ / | \ 01104 | \ / | \ 01105 fc<-->rs<- GIV<-->fc<-->rs<- 01106 /|\ /|\ /|\ 01107 / | \ 01108 c1 c2 c3 01109 GIV 01110 /|\ 01111 / | \ 01112 c1 c2 c3 01113 @endverbatim 01114 * 01115 * If the given tree is empty, the function has no effect. 01116 * 01117 * If the current tree is empty, the given tree becomes the current tree. 01118 * The new root becomes the current node. 01119 * 01120 * Otherwise: 01121 * - The first child of the current node becomes the right sibling 01122 * of the root of the given tree. 01123 * - The root of the given tree becomes the first child of the current node. 01124 * - The current node becomes the parent of the root of the given tree. 01125 * - The current node remains unchanged. 01126 * 01127 * @param aTree a tree 01128 */ 01129 void mergeFirstChild(const GenTree<value_type>& aTree); 01130 01131 01132 /** 01133 * @brief Merge given tree as first child of current node. 01134 * 01135 @verbatim 01136 GIV GIV 01137 |\ /|\ 01138 | \ / | \ 01139 | \ ===> / | \ 01140 | \ / | \ 01141 | \ / | \ 01142 fc<-->rs<- NEW<-->fc<-->rs<- 01143 /|\ /|\ /|\ 01144 / | \ 01145 c1 c2 c3 01146 NEW 01147 /|\ 01148 / | \ 01149 c1 c2 c3 01150 @endverbatim 01151 * 01152 * If the given tree is empty, the function has no effect. 01153 * 01154 * Otherwise: 01155 * - The first child of the given node becomes the right sibling 01156 * of the root of the given tree. 01157 * - The root of the given tree becomes the first child of the given node. 01158 * - The given node becomes the parent of the root of the given tree. 01159 * - The given node becomes the current node. 01160 * 01161 * @param aTree a tree 01162 * @param aPNode a pointer to a node 01163 * 01164 * @warning The given node is supposed to belong 01165 * to the current tree! 01166 */ 01167 void mergeFirstChild(const GenTree<value_type>& aTree, 01168 GenTreeNode<value_type>* aPNode); 01169 01170 //@} 01171 01172 01173 /** @name Remove nodes */ 01174 // ============ 01175 //@{ 01176 /** 01177 * @brief Remove current node and return it. 01178 * 01179 @verbatim 01180 p p 01181 /|\ /| 01182 / | \ / | 01183 / | \ ===> / | 01184 / | \ / | 01185 / | \ / | 01186 ->ls<-->CUR<-->rs<- ->ls<-->rs<- 01187 /|\ /|\ /|\ /|\ /|\ 01188 @endverbatim 01189 * 01190 * No effect if the tree is empty. 01191 * 01192 * @warning 01193 * - <b>The (sub)tree whose root is the initial current 01194 * node is not deleted: The deletion must be explicitely 01195 * performed by the user.</b> 01196 * - <b>The root becomes the current node after the removal.</b> 01197 */ 01198 GenTreeNode<value_type>* remove(); 01199 01200 /** 01201 * @brief Remove given node and return it. 01202 * 01203 @verbatim 01204 p p 01205 /|\ /| 01206 / | \ / | 01207 / | \ ===> / | 01208 / | \ / | 01209 / | \ / | 01210 ->ls<-->GIV<-->rs<- ->ls<-->rs<- 01211 /|\ /|\ /|\ /|\ /|\ 01212 @endverbatim 01213 * 01214 * @param aPNode a pointer to a node 01215 * 01216 * @warning 01217 * - <b>The (sub)tree whose root is the given node is not deleted: 01218 * The deletion must be explicitely performed by the user.</b> 01219 * - If the given node is the current node, the root becomes 01220 * the current node after the removal. Otherwise, the current node 01221 * remains unchanged. 01222 * - The given node is supposed to belong to the current tree! 01223 */ 01224 GenTreeNode<value_type>* remove(GenTreeNode<value_type>* aPNode); 01225 01226 //@} 01227 01228 01229 /** @name Operators */ 01230 // ========= 01231 //@{ 01232 01233 /** 01234 * @brief Assignment. 01235 * 01236 * The current node of the resulting tree is its root. 01237 * 01238 * @param aTree a tree 01239 * 01240 * @warning Perform a <b>deep copy</b>: 01241 * All the nodes of the given tree are duplicated. 01242 */ 01243 GenTree<value_type>& operator=(const GenTree<value_type>& aTree); 01244 01245 //@} 01246 01247 01248 // ------------------------------------------------------------------- 01249 // P R O T E C T E D M E M B E R S 01250 // ------------------------------------------------------------------- 01251 protected: 01252 01253 01254 /** @name Representation of a tree */ 01255 // ======================== 01256 //@{ 01257 01258 /** 01259 * @brief Pointer to the root of the tree. 01260 */ 01261 GenTreeNode<value_type>* _pRoot; 01262 01263 /** 01264 * @brief Pointer to the current node. 01265 */ 01266 GenTreeNode<value_type>* _pCurrent; 01267 01268 //@} 01269 01270 01271 // ------------------------------------------------------------------- 01272 // P R I V A T E M E M B E R S 01273 // ------------------------------------------------------------------- 01274 private: 01275 01276 01277 /** @name Auxiliaries */ 01278 // =========== 01279 //@{ 01280 01281 /** 01282 * @brief Perform a deep copy of a part of the tree 01283 * and return a pointer to the root of the copy. 01284 * 01285 * @param aPNode pointer a node, representing the root 01286 * of the subtree to be copied 01287 * @param aPFCopy pointer to link the copy of the given node 01288 * to its parent (default <b>0</b>) 01289 * @param aPLSCopy pointer to link the copy of the given node 01290 * to its left sibling (default <b>0</b>) 01291 */ 01292 GenTreeNode<value_type>* 01293 PRIVATEdeepCopy(GenTreeNode<value_type>* aPNode, 01294 GenTreeNode<value_type>* aPFCopy = 0, 01295 GenTreeNode<value_type>* aPLSCopy = 0) const; 01296 01297 /** 01298 * @brief Delete the (sub)tree of given root 01299 * 01300 * @param aPNode pointer to the root of the subtree to be deleted 01301 */ 01302 void PRIVATEdelete(GenTreeNode<value_type>* aPNode); 01303 01304 //@} 01305 01306 01307 // ------------------------------------------------------------------- 01308 01309 }; // class GenTree 01310 01311 01312 } // namespace qgar 01313 01314 01315 01316 01317 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 01318 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 01319 // I M P L E M E N T A T I O N S 01320 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 01321 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 01322 01323 #include <qgarlib/GenTree.TCC> 01324 01325 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 01326 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 01327 01328 01329 #endif /* __GENTREE_H_INCLUDED__ */