home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / COMPILER / SAMPLE06 / TREENODE.HPP < prev   
Text File  |  1993-05-07  |  6KB  |  108 lines

  1. //+----------------------------------------------------------------------------+
  2. //| TREENODE.HPP                                                               |
  3. //|                                                                            |
  4. //| COPYRIGHT:                                                                 |
  5. //| ----------                                                                 |
  6. //|  Copyright (C) International Business Machines Corp., 1992,1993.           |
  7. //|                                                                            |
  8. //| DISCLAIMER OF WARRANTIES:                                                  |
  9. //| -------------------------                                                  |
  10. //|  The following [enclosed] code is sample code created by IBM Corporation.  |
  11. //|  This sample code is not part of any standard IBM product and is provided  |
  12. //|  to you solely for the purpose of assisting you in the development of      |
  13. //|  your applications.  The code is provided "AS IS", without warranty of     |
  14. //|  any kind.  IBM shall not be liable for any damages arising out of your    |
  15. //|  use of the sample code, even if they have been advised of the             |
  16. //|  possibility of such damages.                                              |
  17. //|                                                                            |
  18. //| REVISION LEVEL: 1.0                                                        |
  19. //| ---------------                                                            |
  20. //|                                                                            |
  21. //+----------------------------------------------------------------------------+
  22. //| Class Name  : TREENODE                                                     |
  23. //| Purpose     : This class encapulates the behaviour of a data structure     |
  24. //|               known as an n-ary tree.                                      |
  25. //| Author      : njC Sales                                                    |
  26. //| Date        : 27 October 1992                                              |
  27. //+----------------------------------------------------------------------------+
  28.  
  29. #ifndef TREENODE_HPP_INCLUDED
  30. #define TREENODE_HPP_INCLUDED
  31.  
  32. #include <os2.h>
  33. #include "treelink.hpp"
  34.  
  35. class TreeNode : public TreeLink
  36. {
  37. public:
  38.    enum TreeNodeLoc {Undefined, Leaf =Undefined, Internal, Top};
  39.    //+-------------------------------------------------------------------------+
  40.    //| Constructors                                                            |
  41.    //+-------------------------------------------------------------------------+
  42.    TreeNode() :
  43.           myState(Undefined),
  44.           TreeLink() {}
  45.  
  46.    TreeNode(TreeNode *pNode) :
  47.           myState(pNode->myState),
  48.           TreeLink(pNode){}
  49.  
  50.    TreeNode(const TreeNode&);
  51.  
  52.    virtual ~TreeNode() {}         // use default destructor
  53.  
  54.    //+-------------------------------------------------------------------------+
  55.    //| Assignment                                                              |
  56.    //+-------------------------------------------------------------------------+
  57.    TreeNode &operator= (const TreeNode &node);
  58.  
  59.    //+-------------------------------------------------------------------------+
  60.    //| Accessors                                                               |
  61.    //+-------------------------------------------------------------------------+
  62.    virtual TreeNodeLoc  getState() const  {return myState; }
  63.    virtual void setState(TreeNodeLoc newLoc)
  64.                         {myState= newLoc; }
  65.  
  66.    //+-------------------------------------------------------------------------+
  67.    //| Define state predicates.                                                |
  68.    //+-------------------------------------------------------------------------+
  69.    BOOL isUndefined() const {return myState == Undefined;}
  70.    BOOL isLeaf()      const {return myState == Leaf;}
  71.    BOOL isInternal()  const {return myState == Internal;}
  72.    BOOL isTop()       const {return myState == Top;}
  73.  
  74.    //+-------------------------------------------------------------------------+
  75.    //| Tree Navigation functions                                               |
  76.    //+-------------------------------------------------------------------------+
  77.    TreeNode *GetParent() {return (TreeNode *)getParent();}
  78.    TreeNode *GetChild()  {return (TreeNode *)getChild(); }
  79.    TreeNode *GetLeft()   {return (TreeNode *)getLeft();  }
  80.    TreeNode *GetRight()  {return (TreeNode *)getRight(); }
  81.  
  82.    //+-------------------------------------------------------------------------+
  83.    //| Tree Alteration functions                                               |
  84.    //+-------------------------------------------------------------------------+
  85.    TreeNode *addChild (TreeNode *node);
  86.    TreeNode *addChild (TreeNode &node);
  87.    TreeNode *addSister(TreeNode *node);
  88.    TreeNode *addSister(TreeNode &node);
  89.    TreeNode *remove();
  90.  
  91. protected:
  92.  
  93.    TreeNodeLoc    myState;
  94.  
  95.    //+-------------------------------------------------------------------------+
  96.    //| Tree Alteration Helper Functions                                        |
  97.    //|    Due to the fact that these functions have to operate on various      |
  98.    //|    other instances and not only 'this', they are declared as friends.   |
  99.    //+-------------------------------------------------------------------------+
  100.  
  101.    friend TreeNode *adopt   (TreeNode *parent,       TreeNode *child);
  102.    friend TreeNode *insert  (TreeNode *currentChild, TreeNode *newChild);
  103.    friend TreeNode *addfirst(TreeNode *parent,       TreeNode *newChild);
  104.    friend TreeNode *add     (TreeNode *currentChild, TreeNode *newChild);
  105.    friend TreeNode *delink  (TreeNode *currentNode);
  106. };
  107. #endif
  108.