#ifndef MNODE_H #define MNode_H /////////////////////////////////////////////////////////////////////////// // MNode.h - M-ary node class to use in M-ary tree // // ver 1.0 // // Language: Visual C++, 2008 // // Platform: Dell Precision T7400, Win7 Professional // // Application: CSE687 - Demo for Project #1, Spring 2010 // // Author: Jim Fawcett, Syracuse University, CST 4-187, // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////////// /* * Module Operations: * ================== * Provides a template-based M-ary Node class that holds a finite number * of child nodes. * * Required Files: * =============== * MNode.h, MNode.cpp * * Build Process: * ============== * cl /D:TEST_MNODE MNode.cpp * * Maintenance History: * ==================== * ver 1.0 : 27 Feb 10 * - first release */ #include #include namespace TMTree { ///////////////////////////////////////////////////////////////////////// // MNode class template class MNode { public: MNode(const T& val); MNode(const MNode& node); ~MNode(); MNode* clone(); MNode& operator=(const MNode& node); T& value(); void addChild(MNode* pNode); bool removeChild(MNode* pChild); size_t numChildren(); MNode* nextUnmarkedChild(); void clearMarks(); std::string ToString(); private: T val_; std::vector*> children; size_t visitIndex; }; //----< return text >---------------------------------------------------- template T& MNode::value() { return val_; } //----< promotion constructor >------------------------------------------ template MNode::MNode(const T& t) : val_(t), visitIndex(0) {} //----< destructor >------------------------------------------------------- template MNode::~MNode() { for(size_t i=0; i--------------------------- template MNode* MNode::clone() { MNode* pNode = new MNode(value()); for(size_t i=0; iaddChild(children[i]->clone()); return pNode; } //----< copy constructor >----------------------------------------------- template MNode::MNode(const MNode& node) : val_(node.val_) { for(size_t i=0; iclone()); } //----< assignment operator >---------------------------------------------- template MNode& MNode::operator =(const MNode& el) { if(&el == this) return *this; for(size_t i=0; iclone()); return *this; } //----< add child node >--------------------------------------------------- template void MNode::addChild(MNode* pMNode) { if(pMNode == this) throw std::exception("attempting to make node a child of itself!"); children.push_back(pMNode); } //----< return number of children >---------------------------------------- template size_t MNode::numChildren() { return children.size(); } //----< remove child node - returns false on failure, else true >---------- template bool MNode::removeChild(MNode* pMNode) { std::vector< MNode* >::iterator iter = children.begin(); for(iter=children.begin(); iter!=children.end(); ++iter) { if(pMNode == *iter) { delete pMNode; children.erase(iter); return true; } } return false; } //----< return pointer to next unvisited child or null >----------------- template MNode* MNode::nextUnmarkedChild() { if(children.size() == 0) return 0; if(visitIndex < children.size()) { return children[visitIndex++]; } return 0; } //----< "mark" all children as unvisited >------------------------------- template void MNode::clearMarks() { visitIndex = 0; } //----< serialize content to string representation of node >------------- template std::string MNode::ToString() { std::ostringstream temp; temp << "<" << value() << ">"; return temp.str(); } } #endif