//----------------------------------------------------------------------------- #ifndef NARYTREECLASS_H #define NARYTREECLASS_H //----------------------------------------------------------------------------- #include "boolean.h" //----------------------------------------------------------------------------- //----Forward declaration of tree and treeiterator classes template class tree; template class treeiterator; //----------------------------------------------------------------------------- template class treenode { //----The tree and the iterator are friends of the node friend class tree; friend class treeiterator; public: //----Default constructor treenode(void); //----Initial value constructor treenode(NodeType InitialValue,treenode *ParentNode); //----Return the data in the node NodeType GetData(void); //----Return the parent treenode *GetParent(void); //----Return an offspring treenode *GetOffspring(int OffspringIndex); //----Add an offspring if one is not there already boolean AddOffspring(int OffspringIndex,NodeType Value); //----Add a tree as an offspring boolean AddOffspringTree(int OffspringIndex, tree Subtree); //----Check if a given offspring exists boolean OffspringExists(int OffspringIndex); protected: NodeType Data; treenode *Parent; treenode *Offspring[Degree]; }; //----------------------------------------------------------------------------- template class tree { //----The tree iterator is a friend friend class treenode; friend class treeiterator; public: //----Default constructor tree(void); //----Copy constructor tree(const tree &CopyThisTree); //----Destructor ~tree(void); //----Test if empty boolean Empty(void); //----Add a root node if not already there boolean AddRoot(NodeType InitialValue); //----Preorder traversal void PreorderTaverse(void); //----Protected so specialised trees can inherit protected: treenode *Root; private: //----Workhorse for copy constructor treenode* Copy( treenode* CopyThisNode, treenode* TheParent); //----Workhorse for destructor void Destruct(treenode *Node); //----Workhorse for PreorderTraverse void DoPreorderTaverse(treenode *Node); }; //----------------------------------------------------------------------------- template class treeiterator { public: //----Initial value contructor treeiterator(tree &TreeToIterate); //----Check if pointing at a node boolean NodeExists(void); //----Check if parent exists boolean ParentExists(void); //----Check if offspring exists boolean OffspringExists(int OffspringIndex); //----Point to the root void GoRoot(void); //----Point to parent if any void GoUp(void); //----Point to child of exists void GoDown(int OffspringIndex); //----Return current data NodeType CurrentData(void); //----Add an offpring here if possible boolean AddOffspring(int OffspringIndex, NodeType Value); //----Add a tree as an offspring boolean AddOffspringTree(int OffspringIndex,tree Subtree); protected: tree *TheTree; treenode *CurrentNodePointer; }; //----------------------------------------------------------------------------- #endif //-----------------------------------------------------------------------------