home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / compiler / authors / treenode.hpp < prev   
Encoding:
C/C++ Source or Header  |  1996-02-20  |  5.8 KB  |  109 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. #define NULL 0
  33. typedef unsigned long BOOL;
  34. #include "treelink.hpp"
  35.  
  36. class TreeNode : public TreeLink
  37. {
  38. public:
  39.    enum TreeNodeLoc {Undefined, Leaf =Undefined, Internal, Top};
  40.    //+-------------------------------------------------------------------------+
  41.    //| Constructors                                                            |
  42.    //+-------------------------------------------------------------------------+
  43.    TreeNode() :
  44.           myState(Undefined),
  45.           TreeLink() {}
  46.  
  47.    TreeNode(TreeNode *pNode) :
  48.           myState(pNode->myState),
  49.           TreeLink(pNode){}
  50.  
  51.    TreeNode(const TreeNode&);
  52.  
  53.    virtual ~TreeNode() {}         // use default destructor
  54.  
  55.    //+-------------------------------------------------------------------------+
  56.    //| Assignment                                                              |
  57.    //+-------------------------------------------------------------------------+
  58.    TreeNode &operator= (const TreeNode &node);
  59.  
  60.    //+-------------------------------------------------------------------------+
  61.    //| Accessors                                                               |
  62.    //+-------------------------------------------------------------------------+
  63.    virtual TreeNodeLoc  getState() const  {return myState; }
  64.    virtual void setState(TreeNodeLoc newLoc)
  65.                         {myState= newLoc; }
  66.  
  67.    //+-------------------------------------------------------------------------+
  68.    //| Define state predicates.                                                |
  69.    //+-------------------------------------------------------------------------+
  70.    BOOL isUndefined() const {return myState == Undefined;}
  71.    BOOL isLeaf()      const {return myState == Leaf;}
  72.    BOOL isInternal()  const {return myState == Internal;}
  73.    BOOL isTop()       const {return myState == Top;}
  74.  
  75.    //+-------------------------------------------------------------------------+
  76.    //| Tree Navigation functions                                               |
  77.    //+-------------------------------------------------------------------------+
  78.    TreeNode *GetParent() {return (TreeNode *)getParent();}
  79.    TreeNode *GetChild()  {return (TreeNode *)getChild(); }
  80.    TreeNode *GetLeft()   {return (TreeNode *)getLeft();  }
  81.    TreeNode *GetRight()  {return (TreeNode *)getRight(); }
  82.  
  83.    //+-------------------------------------------------------------------------+
  84.    //| Tree Alteration functions                                               |
  85.    //+-------------------------------------------------------------------------+
  86.    TreeNode *addChild (TreeNode *node);
  87.    TreeNode *addChild (TreeNode &node);
  88.    TreeNode *addSister(TreeNode *node);
  89.    TreeNode *addSister(TreeNode &node);
  90.    TreeNode *remove();
  91.  
  92. protected:
  93.  
  94.    TreeNodeLoc    myState;
  95.  
  96.    //+-------------------------------------------------------------------------+
  97.    //| Tree Alteration Helper Functions                                        |
  98.    //|    Due to the fact that these functions have to operate on various      |
  99.    //|    other instances and not only 'this', they are declared as friends.   |
  100.    //+-------------------------------------------------------------------------+
  101.  
  102.    friend TreeNode *adopt   (TreeNode *parent,       TreeNode *child);
  103.    friend TreeNode *insert  (TreeNode *currentChild, TreeNode *newChild);
  104.    friend TreeNode *addfirst(TreeNode *parent,       TreeNode *newChild);
  105.    friend TreeNode *add     (TreeNode *currentChild, TreeNode *newChild);
  106.    friend TreeNode *delink  (TreeNode *currentNode);
  107. };
  108. #endif
  109.