/////////////////////////////////////////////////////////////////////////// // Tree.cpp - demonstrates how to define a template m-ary tree class // // ver 1.2 // // Language: Visual C++, 2008 // // Platform: Dell Precision T7400, Vista Ultimate, SP1 // // Application: CSE687 - Demo for Project #1, Spring 2009 // // Author: Jim Fawcett, Syracuse University, CST 4-187, // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////////// #include #include "tree.h" //----< test stub >------------------------------------------------------ #ifdef TEST_TREE using namespace AbstractDataTypes; void main() { std::cout << "\n Tree class demonstration"; std::cout << "\n =============================\n"; // Here's how you create a node on the heap // MNode* pRoot = new MNode("root"); // Creating nodes on main's stack: MNode root("root"); MNode child1("child1"); MNode child2("child2"); MNode grandchild11("grandchild11"); MNode grandchild21("grandchild21"); MNode grandchild22("grandchild22"); MNode greatgrandchild211("greatgrandchild211"); MNode child3("child3"); child1.add(&grandchild11); grandchild21.add(&greatgrandchild211); child2.add(&grandchild21); child2.add(&grandchild22); root.add(&child1); root.add(&child2); root.add(&child3); class newOper : public Operation { public: void operator()(MNode* pNode) { std::cout << "\n -- " << pNode->value(); } }; Tree tree(new newOper); // Tree tree; tree.add(&root); tree.add(&child1); tree.add(&child2); tree.add(&child3); tree.add(&grandchild11); tree.add(&grandchild21); tree.add(&grandchild22); tree.add(&greatgrandchild211); tree.walk(&root); std::cout << "\n\n removing first child"; tree.remove(&child1); // remove from nodes collection root.remove(&child1); // unlink from tree structure std::cout << "\n unmarking all nodes\n"; tree.clearMarks(); tree.walk(&root); std::cout << "\n\n"; } #endif