//----------------------------------------------------------------------------- #include #include //----For NULL #include "narytreeclass.h" //----------------------------------------------------------------------------- //----Public parts of treenode //----------------------------------------------------------------------------- //----Default constructor template treenode::treenode(void) { } //----------------------------------------------------------------------------- //----Initial value constructor template treenode::treenode(NodeType InitialValue, treenode *ParentNode) { int Index; Data = InitialValue; Parent = ParentNode; //----No offspring of new node for (Index = 0; Index < Degree; Index++) Offspring[Index] = NULL; } //----------------------------------------------------------------------------- //----Return the data in the node template NodeType treenode::GetData(void) { return(Data); } //----------------------------------------------------------------------------- //----Return the parent template treenode *treenode::GetParent(void) { return(Parent); } //----------------------------------------------------------------------------- //----Return an offspring template treenode *treenode::GetOffspring( int OffspringIndex) { if (OffspringIndex < Degree) return(Offspring[OffspringIndex]); else return(NULL); } //----------------------------------------------------------------------------- //----Add an offspring if one is not there already template boolean treenode::AddOffspring(int OffspringIndex, NodeType Value) { if (OffspringIndex < Degree && !OffspringExists(OffspringIndex)) { Offspring[OffspringIndex] = new treenode(Value,this); return(TRUE); } else return(FALSE); } //----------------------------------------------------------------------------- //----Add a tree as an offspring template boolean treenode::AddOffspringTree(int OffspringIndex, tree Subtree) { tree CopyTree(Subtree); if (OffspringIndex < Degree && !OffspringExists(OffspringIndex)) { Offspring[OffspringIndex] = CopyTree.Root; CopyTree.Root = NULL; } else return(FALSE); } //----------------------------------------------------------------------------- //----Check if a given offspring exists template boolean treenode::OffspringExists(int OffspringIndex) { return(OffspringIndex < Degree && Offspring[OffspringIndex] != NULL); } //----------------------------------------------------------------------------- //----Public parts of tree //----------------------------------------------------------------------------- //----Default constructor template tree::tree(void) { Root = NULL; } //----------------------------------------------------------------------------- //----Copy constructor template tree::tree(const tree &CopyThisTree) { Root = Copy(CopyThisTree.Root,NULL); } //----------------------------------------------------------------------------- //----Assignment operator //template //const tree &tree::operator=( //const tree AssignThisTree) { // //Destruct(Root); //Root = Copy(AssignThisTree.Root,NULL); //return(*this); //} //----------------------------------------------------------------------------- //----Workhorse for copy constructor template treenode* tree::Copy( treenode *CopyThisNode, treenode *TheParent) { treenode *NewNode; int Index; if (CopyThisNode != NULL) { NewNode = new treenode(CopyThisNode->Data,CopyThisNode); NewNode->Parent = TheParent; for (Index = 0; Index < Degree; Index++) NewNode->Offspring[Index] = Copy(CopyThisNode->Offspring[Index], NewNode); return(NewNode); } else return(NULL); } //----------------------------------------------------------------------------- //----Destructor template tree::~tree(void) { Destruct(Root); } //----------------------------------------------------------------------------- //----Workhorse for destructor template void tree::Destruct(treenode *Node) { int Index; if (Node != NULL) { for (Index = 0; Index < Degree; Index++) Destruct(Node->Offspring[Index]); delete Node; } } //----------------------------------------------------------------------------- //----Test if empty template boolean tree::Empty(void) { return(Root == NULL); } //----------------------------------------------------------------------------- //----Add a root node if not already there template boolean tree::AddRoot(NodeType InitialValue) { if (Empty()) { Root = new treenode(InitialValue,NULL); return(TRUE); } else return(FALSE); } //----------------------------------------------------------------------------- #include "preordertraversal.cpp" //----------------------------------------------------------------------------- //----Public parts of treeiterator //----------------------------------------------------------------------------- //----Initial value contructor template treeiterator::treeiterator( tree &TreeToIterate) { TheTree = &TreeToIterate; CurrentNodePointer = NULL; } //----------------------------------------------------------------------------- //----Check if pointing at a node template boolean treeiterator::NodeExists(void) { return(CurrentNodePointer != NULL); } //----------------------------------------------------------------------------- //----Check if parent exists template boolean treeiterator::ParentExists(void) { assert(NodeExists()); return(CurrentNodePointer->Parent != NULL); } //----------------------------------------------------------------------------- //----Check if offspring exists template boolean treeiterator::OffspringExists(int OffspringIndex) { assert(NodeExists()); return(CurrentNodePointer->OffspringExists(OffspringIndex)); } //----------------------------------------------------------------------------- //----Point to the root template void treeiterator::GoRoot(void) { CurrentNodePointer = TheTree->Root; } //----------------------------------------------------------------------------- //----Point to parent if any template void treeiterator::GoUp(void) { assert(NodeExists()); CurrentNodePointer = CurrentNodePointer->Parent; } //----------------------------------------------------------------------------- //----Point to child of exists template void treeiterator::GoDown(int OffspringIndex) { assert(NodeExists()); assert(OffspringIndex < Degree); CurrentNodePointer = CurrentNodePointer->Offspring[OffspringIndex]; } //----------------------------------------------------------------------------- //----Return current data template NodeType treeiterator::CurrentData(void) { assert(NodeExists()); return(CurrentNodePointer->Data); } //----------------------------------------------------------------------------- //----Add an offpring here if possible template boolean treeiterator::AddOffspring(int OffspringIndex, NodeType Value) { return(CurrentNodePointer->AddOffspring(OffspringIndex,Value)); } //----------------------------------------------------------------------------- //----Add a tree as an offspring template boolean treeiterator::AddOffspringTree(int OffspringIndex, tree Subtree) { return(CurrentNodePointer->AddOffspringTree(OffspringIndex,Subtree)); } //-----------------------------------------------------------------------------