Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GenTree.TCC

Go to the documentation of this file.
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 /**
00029  * @file  GenTree.TCC
00030  * @brief Implementation of function members
00031  *   of classes qgar::GenTreeNode and qgar::GenTree.
00032  *
00033  * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a>
00034  * @date  January 27, 2005  22:25
00035  * @since Qgar 2.2
00036  */
00037 
00038 
00039 
00040 // STD
00041 #include <string>
00042 // QGAR
00043 #include <qgarlib/QgarErrorUser.H>
00044 
00045 
00046 
00047 namespace qgar
00048 {
00049 
00050 
00051 /*---------------------------------------------------------------------*
00052  |                                                                     |
00053  |         C  L  A  S  S      G  E  N  T  R  E  E  N  O  D  E          |
00054  |                                                                     |
00055  *---------------------------------------------------------------------*/
00056 
00057 
00058 // ============
00059 // CONSTRUCTORS
00060 // ============
00061 
00062 
00063 // DEFAULT CONSTRUCTOR
00064 
00065 template <class T>
00066 GenTreeNode<T>::GenTreeNode()
00067 
00068   : _pParent(0),
00069     _pLSibling(0),
00070     _pRSibling(0),
00071     _pFirstChild(0),
00072     _data()
00073 
00074 {
00075   // VOID
00076 }
00077 
00078 
00079 // CREATE A NODE WITH GIVEN DATA AND NO LINK
00080 
00081 template <class T>
00082 GenTreeNode<T>::GenTreeNode(const T& aData)
00083 
00084   : _pParent(0),
00085     _pLSibling(0),
00086     _pRSibling(0),
00087     _pFirstChild(0),
00088     _data(aData)
00089 
00090 {
00091   // VOID
00092 }
00093 
00094 
00095 // CREATE A NODE WITH GIVEN DATA AND LINKS
00096 
00097 template <class T>
00098 GenTreeNode<T>::GenTreeNode(GenTreeNode<T>* aPParent,
00099                             GenTreeNode<T>* aPLSibling,
00100                             GenTreeNode<T>* aPRSibling,
00101                             GenTreeNode<T>* aPFirstChild,
00102                             const T& aData)
00103 
00104   : _pParent(aPParent),
00105     _pLSibling(aPLSibling),
00106     _pRSibling(aPRSibling),
00107     _pFirstChild(aPFirstChild),
00108     _data(aData)
00109 
00110 {
00111   // VOID
00112 }
00113 
00114 // Copy-constructor
00115 template <class T>
00116 GenTreeNode<T>::GenTreeNode(const GenTreeNode<T>& aNode)
00117   : _pParent(aNode._pParent),
00118     _pLSibling(aNode._pLSibling),
00119     _pRSibling(aNode._pRSibling),
00120     _pFirstChild(aNode._pFirstChild),
00121     _data(aNode._data)
00122 {
00123   // VOID
00124 }
00125 
00126 
00127 // ==========
00128 // DESTRUCTOR
00129 // ==========
00130 
00131 
00132 template <class T>
00133 GenTreeNode<T>::~GenTreeNode()
00134 {
00135   // VOID
00136 }
00137 
00138 
00139 // ===============
00140 // ACCESS TO LINKS
00141 // ===============
00142 
00143 
00144 // GET POINTER TO PARENT
00145 
00146 template <class T>
00147 inline GenTreeNode<T>* GenTreeNode<T>::pParent() const
00148 {
00149   return _pParent;
00150 }
00151 
00152 
00153 // GET POINTER TO LEFT SIBLING
00154 
00155 template <class T>
00156 inline GenTreeNode<T>* GenTreeNode<T>::pLSibling() const
00157 {
00158   return _pLSibling;
00159 }
00160 
00161 
00162 // GET POINTER TO RIGHT SIBLING
00163 
00164 template <class T>
00165 inline GenTreeNode<T>* GenTreeNode<T>::pRSibling() const
00166 {
00167   return _pRSibling;
00168 }
00169 
00170 
00171 // GET POINTER TO FIRST CHILD
00172 
00173 template <class T>
00174 inline GenTreeNode<T>* GenTreeNode<T>::pFirstChild() const
00175 {
00176   return _pFirstChild;
00177 }
00178 
00179 
00180 // ====================
00181 // SIMPLE LINK SETTINGS
00182 // ====================
00183 
00184 
00185 // SET POINTER TO PARENT
00186 
00187 template <class T>
00188 inline void GenTreeNode<T>::setPParent(GenTreeNode<T>* aPNode)
00189 {
00190   _pParent = aPNode;
00191 }
00192 
00193 
00194 // SET POINTER TO LEFT SIBLING
00195 
00196 template <class T>
00197 inline void GenTreeNode<T>::setPLSibling(GenTreeNode<T>* aPNode)
00198 {
00199   _pLSibling = aPNode;
00200 }
00201 
00202 
00203 // SET POINTER TO RIGHT SIBLING
00204 
00205 template <class T>
00206 inline void GenTreeNode<T>::setPRSibling(GenTreeNode<T>* aPNode)
00207 {
00208   _pRSibling = aPNode;
00209 }
00210 
00211 
00212 // SET POINTER TO FIRST CHILD
00213 
00214 template <class T>
00215 inline void GenTreeNode<T>::setPFirstChild(GenTreeNode<T>* aPNode)
00216 {
00217   _pFirstChild = aPNode;
00218 }
00219 
00220 
00221 // ==============================
00222 // ACCESS TO DATA STORED IN NODES
00223 // ==============================
00224 
00225 
00226 // GET DATA CONTAINED BY CURRENT NODE
00227 
00228 template <class T>
00229 inline const T& GenTreeNode<T>::accessData() const
00230 {
00231   return _data;
00232 }
00233 
00234 
00235 // GET A COPY OF THE DATA CONTAINED BY CURRENT NODE
00236 
00237 template <class T>
00238 inline T GenTreeNode<T>::data() const 
00239 {
00240   return _data;
00241 }
00242 
00243 
00244 // ===================
00245 // STORE DATA IN NODES
00246 // ===================
00247 
00248 
00249 // SET NEW DATA TO CURRENT NODE
00250 
00251 template <class T>
00252 inline void GenTreeNode<T>::setData(const T& aData)
00253 {
00254   _data = aData;
00255 }
00256 
00257 
00258 // =========
00259 // OPERATORS
00260 // =========
00261 
00262 
00263 // ASSIGNMENT
00264 
00265 template <class T>
00266 GenTreeNode<T>& GenTreeNode<T>::operator=(const GenTreeNode<T>& aNode)
00267 {
00268   // Are left hand side and right hand side different objects?
00269   if (this != &aNode)
00270     {
00271       _pParent     = aNode.pParent();
00272       _pLSibling   = aNode.pLSibling();
00273       _pRSibling   = aNode.pRSibling();
00274       _pFirstChild = aNode.pFirstChild();
00275       _data        = aNode.data();
00276     }
00277 
00278   return *this;
00279 }
00280 
00281 
00282 /*---------------------------------------------------------------------*
00283  |                                                                     |
00284  *---------------------------------------------------------------------*/
00285 
00286 
00287 
00288 
00289 
00290 /*---------------------------------------------------------------------*
00291  |                                                                     |
00292  |               C  L  A  S  S      G  E  N  T  R  E  E                |
00293  |                                                                     |
00294  *---------------------------------------------------------------------*/
00295 
00296 
00297 // ============
00298 // CONSTRUCTORS
00299 // ============
00300 
00301 
00302 // DEFAULT CONSTRUCTOR: CREATE AN EMPTY TREE
00303 
00304 template <class T>
00305 GenTree<T>::GenTree()
00306 
00307   : _pRoot(0),
00308     _pCurrent(0)
00309 
00310 {
00311   // VOID
00312 }
00313 
00314 
00315 // CREATE A TREE WITH A SINGLE NODE CONTAINING GIVEN DATA
00316 
00317 template <class T>
00318 GenTree<T>::GenTree(const T& aData)
00319 
00320   : _pRoot(new GenTreeNode<T>(aData))
00321 
00322 {
00323   _pCurrent = _pRoot;
00324 }
00325 
00326 
00327 // COPY-CONSTRUCTOR
00328 // Warning: perform a deep copy
00329 
00330 template <class T>
00331 GenTree<T>::GenTree(const GenTree<T>& aTree)
00332 
00333   : _pRoot(PRIVATEdeepCopy(aTree._pRoot))
00334 
00335 {
00336   _pCurrent = _pRoot;
00337 }
00338 
00339 
00340 // CREATE A TREE FROM A GIVEN NODE.
00341 // WARNING: Perform a deep copy
00342 
00343 template <class T>
00344 GenTree<T>::GenTree(GenTreeNode<T>* aPNode)
00345 {
00346   if (aPNode == 0)
00347     {
00348       // Create an empty tree
00349       _pRoot    = 0;
00350       _pCurrent = 0;
00351     }
00352   else
00353     {
00354       // Create a temporary root
00355       // and link it to the descendants of the given node
00356       GenTreeNode<T>* proot =
00357         new GenTreeNode<T>(0,                      // No parent
00358                            0,                      // No left sibling
00359                            0,                      // No right sibling
00360                            aPNode->pFirstChild(),  // Same first child as given node
00361                            aPNode->data());
00362 
00363       // Copy the corresponding tree
00364       _pRoot    = PRIVATEdeepCopy(proot);
00365       _pCurrent = _pRoot;
00366 
00367       // Delete useless temporary node
00368       proot->setPFirstChild(0);
00369       delete proot;
00370     }
00371 }
00372 
00373 
00374 // ==========
00375 // DESTRUCTOR
00376 // ==========
00377 
00378 
00379 template <class T>
00380 GenTree<T>::~GenTree()
00381 {
00382   PRIVATEdelete(_pRoot);
00383 }
00384 
00385 
00386 // ==========
00387 // PREDICATES
00388 // ==========
00389 
00390 
00391 // IS TREE EMPTY?
00392 
00393 template <class T>
00394 bool GenTree<T>::empty() const
00395 {
00396   return _pRoot == 0;
00397 }
00398 
00399 
00400 // ===============
00401 // ACCESS TO LINKS
00402 // ===============
00403 
00404 
00405 // GET POINTER TO ROOT
00406 
00407 template <class T>
00408 inline GenTreeNode<T>*
00409 GenTree<T>::pRoot() const
00410 {
00411   return _pRoot;
00412 }
00413 
00414 
00415 // GET POINTER TO CURRENT NODE
00416 
00417 template <class T>
00418 inline GenTreeNode<T>*
00419 GenTree<T>::pCurrent() const
00420 {
00421   return _pCurrent;
00422 }
00423 
00424 
00425 // GET POINTER TO PARENT OF CURRENT NODE
00426 
00427 template <class T>
00428 inline GenTreeNode<T>*
00429 GenTree<T>::pParent() const
00430 {
00431   return _pCurrent->pParent();
00432 }
00433 
00434 
00435 // GET POINTER TO PARENT OF GIVEN NODE
00436 
00437 template <class T>
00438 inline GenTreeNode<T>*
00439 GenTree<T>::pParent(GenTreeNode<T>* aPNode) const
00440 {
00441   return aPNode->pParent();
00442 }
00443 
00444 
00445 // GET POINTER TO LEFT SIBLING OF CURRENT NODE
00446 
00447 template <class T>
00448 inline GenTreeNode<T>*
00449 GenTree<T>::pLSibling() const
00450 {
00451   return _pCurrent->pLSibling();
00452 }
00453 
00454   
00455 // GET POINTER TO LEFT SIBLING OF GIVEN NODE
00456 
00457 template <class T>
00458 inline GenTreeNode<T>*
00459 GenTree<T>::pLSibling(GenTreeNode<T>* aPNode) const
00460 {
00461   return aPNode->pLSibling();
00462 }
00463 
00464   
00465 // GET POINTER TO RIGHT SIBLING OF CURRENT NODE
00466 
00467 template <class T>
00468 inline GenTreeNode<T>*
00469 GenTree<T>::pRSibling() const
00470 {
00471   return _pCurrent->pRSibling();
00472 }
00473 
00474   
00475 // GET POINTER TO RIGHT SIBLING OF GIVEN NODE
00476 
00477 template <class T>
00478 inline GenTreeNode<T>* 
00479 GenTree<T>::pRSibling(GenTreeNode<T>* aPNode) const
00480 {
00481   return aPNode->pRSibling();
00482 }
00483   
00484 
00485 // GET POINTER TO FIRST CHILD OF CURRENT NODE
00486 
00487 template <class T>
00488 inline GenTreeNode<T>*
00489 GenTree<T>::pFirstChild() const
00490 {
00491   return _pCurrent->pFirstChild();
00492 }
00493 
00494 
00495 // GET POINTER TO FIRST CHILD OF GIVEN NODE
00496 
00497 template <class T>
00498 inline GenTreeNode<T>*
00499 GenTree<T>::pFirstChild(GenTreeNode<T>* aPNode) const
00500 {
00501   return aPNode->pFirstChild();
00502 }
00503 
00504 
00505 // ==============
00506 // ACCESS TO DATA
00507 // ==============
00508 
00509 
00510 // GET THE DATA STORED IN CURRENT NODE
00511 
00512 template <class T>
00513 inline const T&
00514 GenTree<T>::accessData() const
00515 {
00516   return _pCurrent->accessData();
00517 }
00518 
00519 
00520 // GET THE DATA STORED IN GIVEN NODE
00521 
00522 template <class T>
00523 inline const T&
00524 GenTree<T>::accessData(GenTreeNode<T>* aPNode) const
00525 {
00526   return aPNode->accessData();
00527 }
00528 
00529 
00530 // GET A COPY OF THE DATA STORED IN CURRENT NODE
00531 
00532 template <class T>
00533 inline T
00534 GenTree<T>::data() const
00535 {
00536   return _pCurrent->data();
00537 }
00538 
00539 
00540 // GET A COPY OF THE DATA STORED IN GIVEN NODE
00541 
00542 template <class T>
00543 inline T
00544 GenTree<T>::data(GenTreeNode<T>* aPNode) const
00545 {
00546   return aPNode->data();
00547 }
00548 
00549 
00550 // ==================
00551 // MOVING IN THE TREE
00552 // ==================
00553 
00554 
00555 // THE ROOT BECOMES THE CURRENT NODE
00556 
00557 template <class T>
00558 inline void
00559 GenTree<T>::gotoRoot() 
00560 {
00561   _pCurrent = _pRoot;
00562 }
00563 
00564 
00565 // THE PARENT OF THE CURRENT NODE BECOMES THE CURRENT NODE
00566 
00567 template <class T>
00568 inline void
00569 GenTree<T>::gotoParent()
00570 {
00571   _pCurrent = _pCurrent->pParent();
00572 }
00573 
00574 
00575 // THE PARENT OF THE GIVEN NODE BECOMES THE CURRENT NODE
00576 
00577 template <class T>
00578 inline void
00579 GenTree<T>::gotoParent(GenTreeNode<T>* aPNode)
00580 {
00581   _pCurrent = aPNode->pParent();
00582 }
00583 
00584 // THE LEFT SIBLING OF THE CURRENT NODE BECOMES THE CURRENT NODE
00585 
00586 template <class T>
00587 inline void
00588 GenTree<T>::gotoLSibling()
00589 {
00590   _pCurrent = _pCurrent->pLSibling();
00591 }
00592 
00593 
00594 // THE LEFT SIBLING OF THE GIVEN NODE BECOMES THE CURRENT NODE
00595 
00596 template <class T>
00597 inline void
00598 GenTree<T>::gotoLSibling(GenTreeNode<T>* aPNode)
00599 {
00600   _pCurrent = aPNode->pLSibling();
00601 }
00602 
00603 
00604 // THE RIGHT SIBLING OF THE CURRENT NODE BECOMES THE CURRENT NODE
00605 
00606 template <class T>
00607 inline void
00608 GenTree<T>::gotoRSibling()
00609 {
00610   _pCurrent = _pCurrent->pRSibling();
00611 }
00612 
00613 
00614 // THE RIGHT SIBLING OF THE GIVEN NODE BECOMES THE CURRENT NODE
00615 
00616 template <class T>
00617 inline void
00618 GenTree<T>::gotoRSibling(GenTreeNode<T>* aPNode)
00619 {
00620   _pCurrent = aPNode->pRSibling();
00621 }
00622 
00623 
00624 // THE FIRST CHILD OF THE CURRENT NODE BECOMES THE CURRENT NODE
00625 
00626 template <class T>
00627 inline void
00628 GenTree<T>::gotoFirstChild()
00629 {
00630   _pCurrent = _pCurrent->pFirstChild();
00631 }
00632 
00633 
00634 // THE FIRST CHILD OF THE GIVEN NODE BECOMES THE CURRENT NODE
00635 
00636 template <class T>
00637 inline void
00638 GenTree<T>::gotoFirstChild(GenTreeNode<T>* aPNode)
00639 {
00640   _pCurrent = aPNode->pFirstChild();
00641 }
00642 
00643 
00644 // ==========
00645 // STORE DATA
00646 // ==========
00647 
00648 
00649 // SET DATA TO CURRENT NODE
00650 
00651 template <class T>
00652 inline void
00653 GenTree<T>::setData(const T& aData)
00654 {
00655   _pCurrent->setData(aData);
00656 }
00657 
00658 
00659 // SET DATA TO GIVEN NODE
00660 
00661 template <class T>
00662 inline void
00663 GenTree<T>::setData(const T& aData, GenTreeNode<T>* aPNode)
00664 {
00665   aPNode->setData(aData);
00666 }
00667 
00668 
00669 // ================
00670 // INSERT NEW NODES
00671 // ================
00672 
00673 
00674 /*
00675 INSERT A NODE CONTAINING GIVEN DATA AS PARENT OF CURRENT ROOT
00676 ----------------------------------------------------------------
00677                                       pn
00678                                       |                                             
00679                                       |                                             
00680             ROOT        ===>         ROOT
00681             /|\                      /|\
00682            / | \                    / | \
00683 ----------------------------------------------------------------
00684 */
00685 template <class T>
00686 void
00687 GenTree<T>::insertParent(const T& aData)
00688 {
00689   if (empty())
00690     {
00691       // Empty tree
00692       _pRoot    = new GenTreeNode<T>(aData);
00693       _pCurrent = _pRoot;
00694     }
00695   else
00696     {
00697       // Tree is not empty
00698       // Link a new node to present root
00699       _pRoot->setPParent(new GenTreeNode<T>(0,      // parent
00700                                             0,      // left sibling
00701                                             0,      // right sibling
00702                                             _pRoot, // first son
00703                                             aData));
00704       // Update root
00705       _pRoot = _pRoot->pParent();
00706     }
00707 }
00708 
00709 
00710 /*
00711 SET A NEW NODE CONTAINING GIVEN DATA AS LEFT SIBLING OF CURRENT NODE
00712 ----------------------------------------------------------------
00713            p                          p
00714           /|\                        /|\
00715          / | \                      / | \
00716         /  |  \        ===>        /  |  \
00717        /   |   \                  /   |   \
00718       /    |    \                /    |    \
00719   ->ls<-->CUR<-->rs<-        ->ls<-->NEW<-->CUR<-->rs<-
00720    /|\    /|\   /|\           /|\           /|\   /|\
00721 ----------------------------------------------------------------
00722 */
00723 template <class T>
00724 void
00725 GenTree<T>::insertLSibling(const T& aData)
00726 
00727   throw(QgarErrorUser)
00728 
00729 {
00730   if (empty())
00731     {
00732       // Empty tree
00733       _pRoot    = new GenTreeNode<T>(aData);
00734       _pCurrent = _pRoot;
00735     }
00736   else if (_pCurrent->pParent() == 0)
00737     {
00738       // Inserting siblings to root is forbidden
00739       throw QgarErrorUser(__FILE__, __LINE__,
00740                           "template <class T> void qgar::GenTree<T>::insertLSibling(const T&)",                   
00741                           "A root of a tree cannot be provided with left siblings");
00742     }
00743   else
00744     {
00745       // Insert a new node in the tree
00746       GenTreeNode<T>* pNew
00747         = new GenTreeNode<T>(_pCurrent->pParent(),    // parent
00748                              _pCurrent->pLSibling(),  // left sibling
00749                              _pCurrent,               // right sibling
00750                              0,                       // first son
00751                              aData);
00752 
00753       // Update left sibling
00754       if (_pCurrent->pLSibling() == 0)
00755         {
00756           // No left sibling: New node becomes first son
00757           ( _pCurrent->pParent() )->setPFirstChild(pNew);
00758         }
00759       else
00760         {
00761           // New node becomes right sibling of left sibling
00762           ( _pCurrent->pLSibling() )->setPRSibling(pNew);
00763         }
00764 
00765       // Update current node: New node becomes its left sibling
00766       _pCurrent->setPLSibling(pNew);
00767     }
00768 }
00769 
00770 
00771 /*
00772 SET A NEW NODE CONTAINING GIVEN DATA AS LEFT SIBLING OF GIVEN NODE
00773 ----------------------------------------------------------------
00774            p                          p
00775           /|\                        /|\
00776          / | \                      / | \
00777         /  |  \        ===>        /  |  \
00778        /   |   \                  /   |   \
00779       /    |    \                /    |    \
00780   ->ls<-->GIV<-->rs<-        ->ls<-->NEW<-->GIV<-->rs<-
00781    /|\    /|\   /|\           /|\           /|\   /|\
00782 ----------------------------------------------------------------
00783 */
00784 template <class T>
00785 inline void
00786 GenTree<T>::insertLSibling(const T& aData, GenTreeNode<T>* aPNode)
00787 
00788   throw(QgarErrorUser)
00789 
00790 {
00791   _pCurrent = aPNode;
00792   insertLSibling(aData);
00793 }
00794 
00795 
00796 /*
00797 SET A NEW NODE CONTAINING GIVEN DATA AS RIGHT SIBLING OF CURRENT NODE
00798 ----------------------------------------------------------------
00799            p                                 p
00800           /|\                               /|\
00801          / | \                             / | \
00802         /  |  \        ===>               /  |  \
00803        /   |   \                         /   |   \
00804       /    |    \                       /    |    \
00805   ->ls<-->CUR<-->rs<-        ->ls<-->CUR<-->NEW<-->rs<-
00806    /|\    /|\    /|\          /|\    /|\           /|\
00807 ----------------------------------------------------------------
00808 */
00809 template <class T>
00810 void
00811 GenTree<T>::insertRSibling(const T& aData)
00812 
00813   throw(QgarErrorUser)
00814 
00815 {
00816   if (empty())
00817     {
00818       // Empty tree
00819       _pRoot    = new GenTreeNode<T>(aData);
00820       _pCurrent = _pRoot;
00821     }
00822   else if (_pCurrent->pParent() == 0)
00823     {
00824       // Inserting siblings to root is forbidden
00825       throw QgarErrorUser(__FILE__, __LINE__,
00826                           "template <class T> void qgar::GenTree<T>::insertRSibling(const T&)",                   
00827                           "A root of a tree cannot be provided with right siblings");
00828     }
00829   else
00830     {
00831      // Insert a new node in the tree
00832       GenTreeNode<T>* pNew
00833         = new GenTreeNode<T>(_pCurrent->pParent(),    // parent
00834                              _pCurrent,               // left sibling
00835                              _pCurrent->pRSibling(),  // right sibling
00836                              0,                       // first son
00837                              aData);
00838 
00839       // Update right sibling
00840       if (_pCurrent->pRSibling() != 0)
00841         {
00842           // New node becomes left sibling of right sibling
00843           ( _pCurrent->pRSibling() )->setPLSibling(pNew);
00844         }
00845 
00846       // Update current node: New node becomes its right sibling
00847       _pCurrent->setPRSibling(pNew);
00848     }
00849 }
00850 
00851 
00852 /*
00853 SET A NEW NODE CONTAINING GIVEN DATA AS RIGHT SIBLING OF GIVEN NODE
00854 ----------------------------------------------------------------
00855            p                                 p
00856           /|\                               /|\
00857          / | \                             / | \
00858         /  |  \        ===>               /  |  \
00859        /   |   \                         /   |   \
00860       /    |    \                       /    |    \
00861   ->ls<-->GIV<-->rs<-        ->ls<-->GIV<-->NEW<-->rs<-
00862    /|\    /|\    /|\          /|\    /|\           /|\
00863 ----------------------------------------------------------------
00864 */
00865 template <class T>
00866 inline void
00867 GenTree<T>::insertRSibling(const T& aData, GenTreeNode<T>* aPNode)
00868 
00869   throw(QgarErrorUser)
00870 
00871 {
00872   _pCurrent = aPNode;
00873   insertRSibling(aData);
00874 }
00875 
00876 
00877 /*
00878 SET A NEW NODE CONTAINING GIVEN DATA AS FIRST CHILD OF CURRENT NODE
00879 ----------------------------------------------------------------
00880      CUR                         CUR
00881       |\                         /|\
00882       | \                       / | \
00883       |  \         ===>        /  |  \
00884       |   \                   /   |   \
00885       |    \                 /    |    \
00886      fc<-->rs<-            NEW<-->fc<-->rs<-
00887     /|\   /|\                    /|\   /|\
00888 ----------------------------------------------------------------
00889 */
00890 template <class T>
00891 void
00892 GenTree<T>::insertFirstChild(const T& aData)
00893 {
00894   if (empty())
00895     {
00896       // Empty tree
00897       _pRoot = new GenTreeNode<T>(aData);
00898       _pCurrent = _pRoot;
00899     }
00900   else
00901     {
00902       // Tree is not empty: Insert a new node in the tree
00903       GenTreeNode<T>* pNew
00904         = new GenTreeNode<T>(_pCurrent,                 // parent
00905                              0,                         // left sibling
00906                              _pCurrent->pFirstChild(),  // right sibling
00907                              0,                         // first son
00908                              aData);
00909 
00910       // Update first child, if any
00911       GenTreeNode<T>* pfc = _pCurrent->pFirstChild();
00912       if (pfc != 0)
00913         {
00914           pfc->setPLSibling(pNew);
00915         }
00916 
00917       // Update current node
00918       _pCurrent->setPFirstChild(pNew);
00919     }
00920 }
00921 
00922 
00923 /*
00924 SET A NEW NODE CONTAINING GIVEN DATA AS FIRST CHILD OF CURRENT NODE
00925 ----------------------------------------------------------------
00926      GIV                         GIV
00927       |\                         /|\
00928       | \                       / | \
00929       |  \         ===>        /  |  \
00930       |   \                   /   |   \
00931       |    \                 /    |    \
00932      fc<-->rs<-            NEW<-->fc<-->rs<-
00933     /|\   /|\                    /|\   /|\
00934 ----------------------------------------------------------------
00935 */
00936 template <class T>
00937 inline void
00938 GenTree<T>::insertFirstChild(const T& aData, GenTreeNode<T>* aPNode)
00939 {
00940   _pCurrent = aPNode;
00941   insertFirstChild(aData);
00942 }
00943 
00944 
00945 // =====
00946 // MERGE
00947 // =====
00948 
00949 
00950 /*
00951 MERGE GIVEN TREE AS LEFT SIBLING OF CURRENT NODE
00952 ----------------------------------------------------------------
00953            p                          p
00954           /|\                        /|\
00955          / | \                      / | \
00956         /  |  \        ===>        /  |  \
00957        /   |   \                  /   |   \
00958       /    |    \                /    |    \
00959   ->ls<-->CUR<-->rs<-        ->ls<-->GIV<-->CUR<-->rs<-
00960    /|\    /|\    /|\          /|\    /|\    /|\    /|\
00961                                     / | \
00962                                   c1  c2 c3
00963           GIV
00964           /|\
00965          / | \
00966        c1  c2 c3
00967 ----------------------------------------------------------------
00968 */
00969 template <class T>
00970 void
00971 GenTree<T>::mergeLSibling(const GenTree<T>& aTree)
00972 
00973      throw(QgarErrorUser)
00974 
00975 {
00976   // Merging an empty tree has no effect
00977   if (aTree.empty())
00978     {
00979       return;
00980     }
00981 
00982   if (empty())
00983     {
00984       // Current tree is empty
00985       _pRoot    = PRIVATEdeepCopy(aTree.pRoot());
00986       _pCurrent = _pRoot;
00987     }
00988   else
00989     {
00990       // Current tree is not empty
00991 
00992       if (_pCurrent == _pRoot)
00993         {
00994           // Inserting siblings to root is forbidden
00995           throw QgarErrorUser(__FILE__, __LINE__,
00996                               "template <class T> void qgar::GenTree<T>::mergeLSibling(const qgar::GenTree<T>&)",
00997                               "A root of a tree cannot be provided with left siblings");
00998         }
00999 
01000       // Copy the given tree
01001       GenTreeNode<T>* pRootCopy = PRIVATEdeepCopy(aTree.pRoot());
01002 
01003       pRootCopy->setPParent(_pCurrent->pParent());
01004       pRootCopy->setPLSibling(_pCurrent->pLSibling());
01005       pRootCopy->setPRSibling(_pCurrent);
01006 
01007       // Update left sibling of current node
01008       if (_pCurrent->pLSibling() == 0)
01009         {
01010           // Current node has no left sibling, i.e. is first child:
01011           // The root of the copy becomes first child instead
01012           (_pCurrent->pParent())->setPFirstChild(pRootCopy);
01013         }
01014       else
01015         {
01016           // Current node has a left sibling:
01017           // The root of the copy becomes its right sibling
01018           (_pCurrent->pLSibling())->setPRSibling(pRootCopy);
01019         }
01020 
01021       // Update left sibling of current node
01022       _pCurrent->setPLSibling(pRootCopy);
01023     }
01024 }
01025 
01026 
01027 /*
01028 MERGE GIVEN TREE AS LEFT SIBLING OF GIVEN NODE
01029 ----------------------------------------------------------------
01030            p                          p
01031           /|\                        /|\
01032          / | \                      / | \
01033         /  |  \        ===>        /  |  \
01034        /   |   \                  /   |   \
01035       /    |    \                /    |    \
01036   ->ls<-->GIV<-->rs<-        ->ls<-->NEW<-->GIV<-->rs<-
01037    /|\    /|\    /|\          /|\    /|\    /|\    /|\
01038                                     / | \
01039                                   c1  c2 c3
01040           NEW
01041           /|\
01042          / | \
01043        c1  c2 c3
01044 ----------------------------------------------------------------
01045 */
01046 template <class T>
01047 void
01048  GenTree<T>::mergeLSibling(const GenTree<T>& aTree,
01049                            GenTreeNode<T>* aPNode)
01050 
01051      throw(QgarErrorUser)
01052 
01053 {
01054   _pCurrent = aPNode;
01055   mergeLSibling(aTree);
01056 }
01057 
01058 
01059 /*
01060 MERGE GIVEN TREE AS RIGHT SIBLING OF CURRENT NODE
01061 ----------------------------------------------------------------
01062            p                                 p
01063           /|\                               /|\
01064          / | \                             / | \
01065         /  |  \        ===>               /  |  \
01066        /   |   \                         /   |   \
01067       /    |    \                       /    |    \
01068   ->ls<-->CUR<-->rs<-        ->ls<-->CUR<-->GIV<-->rs<-
01069    /|\    /|\   /|\           /|\    /|\    /|\   /|\
01070                                            / | \
01071                                          c1  c2 c3
01072                  GIV
01073                  /|\
01074                 / | \
01075               c1  c2 c3
01076 ----------------------------------------------------------------
01077 */
01078 template <class T>
01079 void
01080 GenTree<T>::mergeRSibling(const GenTree<T>& aTree)
01081 
01082   throw(QgarErrorUser)
01083 
01084 {
01085   // Merging an empty tree has no effect
01086   if (aTree.empty())
01087     {
01088       return;
01089     }
01090 
01091   if (empty())
01092     {
01093       // Current tree is empty
01094       _pRoot = PRIVATEdeepCopy(aTree.pRoot());
01095       _pCurrent = _pRoot;
01096     }
01097   else
01098     {
01099       // Current tree is not empty
01100 
01101       if (_pCurrent == _pRoot)
01102         {
01103           // Inserting siblings to root is forbidden
01104           throw QgarErrorUser(__FILE__, __LINE__,
01105                               "template <class T> void qgar::GenTree<T>::mergeRSibling(const qgar::GenTree<T>&)",
01106                               "A root of a tree cannot be provided with right siblings");
01107         }
01108 
01109       // Copy the given tree
01110       GenTreeNode<T>* pRootCopy = PRIVATEdeepCopy(aTree.pRoot());
01111 
01112       pRootCopy->setPParent(_pCurrent->pParent());
01113       pRootCopy->setPLSibling(_pCurrent);
01114       pRootCopy->setPRSibling(_pCurrent->pRSibling());
01115 
01116      // Update right sibling of current node
01117       if (_pCurrent->pRSibling() != 0)
01118         {
01119           // The root of the copy becomes its left sibling
01120           (_pCurrent->pRSibling())->setPLSibling(pRootCopy);
01121         }
01122 
01123       // Update right sibling of current node
01124       _pCurrent->setPRSibling(pRootCopy);
01125     }
01126 }
01127 
01128 
01129 /*
01130 MERGE GIVEN TREE AS RIGHT SIBLING OF GIVEN NODE
01131 ----------------------------------------------------------------
01132            p                                 p
01133           /|\                               /|\
01134          / | \                             / | \
01135         /  |  \        ===>               /  |  \
01136        /   |   \                         /   |   \
01137       /    |    \                       /    |    \
01138   ->ls<-->GIV<-->rs<-        ->ls<-->GIV<-->NEW<-->rs<-
01139    /|\    /|\   /|\           /|\    /|\    /|\   /|\
01140                                            / | \
01141                                          c1  c2 c3
01142                  NEW
01143                  /|\
01144                 / | \
01145               c1  c2 c3
01146 ----------------------------------------------------------------
01147 */
01148 template <class T>
01149 void
01150 GenTree<T>::mergeRSibling(const GenTree<T>& aTree,
01151                           GenTreeNode<T>* aPNode)
01152 
01153   throw(QgarErrorUser)
01154 
01155 {
01156   _pCurrent = aPNode;
01157   mergeRSibling(aTree);
01158 }
01159 
01160 
01161 /*
01162 MERGE GIVEN TREE AS FIRST CHILD OF CURRENT NODE
01163 ----------------------------------------------------------------
01164      CUR                        CUR
01165       |\                        /|\
01166       | \                      / | \
01167       |  \         ===>       /  |  \
01168       |   \                  /   |   \
01169       |    \                /    |    \
01170      fc<-->rs<-          GIV<-->fc<-->rs<-
01171      /|\                 /|\    /|\
01172                         / | \
01173                       c1  c2 c3
01174      GIV
01175      /|\
01176     / | \
01177   c1  c2 c3
01178 ----------------------------------------------------------------
01179 */
01180 template <class T>
01181 void
01182 GenTree<T>::mergeFirstChild(const GenTree<T>& aTree)
01183 {
01184   // Merging an empty tree has no effect
01185   if (aTree.empty())
01186     {
01187       return;
01188     }
01189 
01190   // Copy the given tree
01191   GenTreeNode<T>* pRootCopy = PRIVATEdeepCopy(aTree.pRoot());
01192 
01193   if (empty())
01194     {
01195       // Current tree is empty
01196       _pRoot = pRootCopy;
01197       _pCurrent = _pRoot;
01198     }
01199   else
01200     {
01201       // Current tree is not empty
01202 
01203       // Update root of the copy, if non-empty
01204       if (pRootCopy != 0)
01205         {
01206           pRootCopy->setPParent(_pCurrent);
01207           pRootCopy->setPLSibling(0);
01208           pRootCopy->setPRSibling(_pCurrent->pFirstChild());
01209         }
01210 
01211      // Update right sibling of first child
01212       if (_pCurrent->pFirstChild() != 0)
01213         {
01214           // The root of the copy becomes its left sibling
01215           (_pCurrent->pFirstChild())->setPLSibling(pRootCopy);
01216         }
01217 
01218       // Update current node
01219       _pCurrent->setPFirstChild(pRootCopy);
01220     }
01221 }
01222 
01223 
01224 
01225 /*
01226 MERGE GIVEN TREE AS FIRST CHILD OF GIVEN NODE
01227 ----------------------------------------------------------------
01228      GIV                        GIV
01229       |\                        /|\
01230       | \                      / | \
01231       |  \         ===>       /  |  \
01232       |   \                  /   |   \
01233       |    \                /    |    \
01234      fc<-->rs<-          NEW<-->fc<-->rs<-
01235      /|\                 /|\    /|\
01236                         / | \
01237                       c1  c2 c3
01238      NEW
01239      /|\
01240     / | \
01241   c1  c2 c3
01242 ----------------------------------------------------------------
01243 */
01244 template <class T>
01245 void
01246 GenTree<T>::mergeFirstChild(const GenTree<T>& aTree,
01247                             GenTreeNode<T>* aPNode)
01248 {
01249   _pCurrent = aPNode;
01250   mergeFirstChild(aTree);
01251 }
01252 
01253 
01254 // ============
01255 // REMOVE NODES
01256 // ============
01257 
01258 
01259 /*
01260 REMOVE CURRENT NODE AND RETURN IT
01261 ----------------------------------------------------------------
01262            p                          p
01263           /|\                        /|
01264          / | \                      / |
01265         /  |  \         ===>       /  |
01266        /   |   \                  /   |
01267       /    |    \                /    |
01268   ->ls<-->CUR<-->rs<-        ->ls<-->rs<-
01269    /|\    /|\   /|\           /|\   /|\
01270 ----------------------------------------------------------------
01271 */
01272 template <class T>
01273 GenTreeNode<T>*
01274 GenTree<T>::remove()
01275 {
01276   if (empty())
01277     {
01278       // Current tree is empty
01279       return _pRoot;
01280     }
01281 
01282   if (_pCurrent == _pRoot)
01283     {
01284       // Root is being removed
01285 
01286       // Update root and current node
01287       GenTreeNode<T>* p = _pCurrent;
01288       _pRoot = 0;
01289       _pCurrent = 0;
01290 
01291       return p;
01292     }
01293 
01294   // Current node links
01295   GenTreeNode<T>* pp  = _pCurrent->pParent();   // parent (P)
01296   GenTreeNode<T>* prs = _pCurrent->pRSibling(); // right sibling (RS)
01297   GenTreeNode<T>* pls = _pCurrent->pLSibling(); // left sibling (LS)
01298 
01299   // Update RS, if any
01300   if (prs != 0)
01301     {
01302       prs->setPLSibling(pls);
01303     }
01304 
01305   // Update LS
01306   if (pls == 0)
01307     {
01308       // No LS: RS becomes first child of P
01309       pp->setPFirstChild(prs);
01310     }
01311   else
01312     {
01313       // RS becomes right sibling of LS
01314       pls->setPRSibling(prs);
01315     }
01316 
01317   // Update current node
01318   GenTreeNode<T>* p = _pCurrent;
01319   _pCurrent = _pRoot;
01320 
01321   return p;
01322 }
01323 
01324 
01325 /*
01326 REMOVE GIVEN NODE AND RETURN IT
01327 ----------------------------------------------------------------
01328            p                          p
01329           /|\                        /|
01330          / | \                      / |
01331         /  |  \         ===>       /  |
01332        /   |   \                  /   |
01333       /    |    \                /    |
01334   ->ls<-->GIV<-->rs<-        ->ls<-->rs<-
01335    /|\    /|\   /|\           /|\   /|\
01336 ----------------------------------------------------------------
01337 */
01338 template <class T>
01339 GenTreeNode<T>*
01340 GenTree<T>::remove(GenTreeNode<T>* aPNode)
01341 {
01342   if (aPNode ==  _pCurrent)
01343     {
01344       return remove();
01345     }
01346   else
01347     {
01348       GenTreeNode<T>* pSaveCurr = _pCurrent;
01349       _pCurrent = aPNode;
01350       remove();
01351       _pCurrent = pSaveCurr;
01352       return aPNode;
01353     }
01354 }
01355 
01356 
01357 // =========
01358 // OPERATORS
01359 // =========
01360 
01361 
01362 // ASSIGNMENT
01363 // WARNING: Perform a deep copy
01364 
01365 template <class T>
01366 GenTree<T>&
01367 GenTree<T>::operator=(const GenTree<T>& aTree)
01368 {
01369   // Are left hand side and right hand side different objects?
01370   if (this != &aTree)
01371     {
01372       _pRoot = PRIVATEdeepCopy(aTree._pRoot);
01373       _pCurrent = _pRoot;
01374     }
01375 
01376   return *this;
01377 }
01378 
01379 
01380 // ===========
01381 // AUXILIARIES
01382 // ===========
01383 
01384 
01385 // PERFORM A DEEP COPY OF A PART OF THE TREE
01386 // AND RETURN A POINTER TO THE ROOT OF THE COPY
01387 
01388 template <class T>
01389 GenTreeNode<T>*
01390 GenTree<T>::PRIVATEdeepCopy(GenTreeNode<T>* aPNode,
01391                             GenTreeNode<T>* aPFCopy,
01392                             GenTreeNode<T>* aPLSCopy)
01393   const
01394 
01395 {
01396   if (aPNode == 0)
01397     {
01398       // Return null pointer
01399       return aPNode;
01400     }
01401   else
01402     {
01403       // Copy  given node
01404       GenTreeNode<T>* copy = new GenTreeNode<T>(aPNode->data());
01405 
01406       // Link given node to its parent and FirstChild
01407       copy->setPParent(aPFCopy);
01408       copy->setPLSibling(aPLSCopy);      
01409 
01410       // Copy first child and link it to the copy
01411       copy->setPFirstChild(PRIVATEdeepCopy(aPNode->pFirstChild(), copy, 0));
01412 
01413       // Copy right sibling and link it to the copy
01414       copy->setPRSibling(PRIVATEdeepCopy(aPNode->pRSibling(),
01415                                          aPFCopy,
01416                                          copy));
01417 
01418       // Return pointer to the copy
01419       return copy;
01420     }
01421 }
01422 
01423 
01424 // DELETE THE SUBTREE OF GIVEN ROOT BY DEPTH-FIRST TRAVERSAL
01425 
01426 template <class T>
01427 void
01428 GenTree<T>::PRIVATEdelete(GenTreeNode<T>* aPNode)
01429 {
01430   if (aPNode != 0)
01431     {
01432       // Delete first child
01433       PRIVATEdelete(aPNode->pFirstChild());
01434       // Delete siblings
01435       PRIVATEdelete(aPNode->pRSibling());
01436 
01437       // Delete given node
01438       delete aPNode;
01439     }
01440 }
01441 
01442 
01443 /*---------------------------------------------------------------------*
01444  |                                                                     |
01445  *---------------------------------------------------------------------*/
01446 
01447 
01448 } // namespace qgar