///////////////////////////////////////////////////////////////// // tree.cpp - generic m-ary tree // // ver 1.0 // // // // Language: Visual C++, ver 6.0, SP 5 // // Platform: Dell Dimension 8100, Win2000, SP 2 // // Application: CSE687 Project #1/#2 demonstration // // Author: Jim Fawcett, CST 2-187, Syracuse Univ. // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////// //----< test stub >-------------------------------------------- #ifdef TEST_TREE #include "tree.h" #include #include using namespace std; //----< make a set of title lines >---------------------------- string title(const string &str, const char underline) { string temp = string("\n ") + str + string("\n ") + string(str.size()+2,underline); return temp; } //----< entry point >------------------------------------------ void main() { //////////////////////////////////////// cout << title("Testing Node Class",'='); //////////////////////////////////////// node nd; cout << "\n value of new node is " << nd.value(); nd.value() = -1; cout << "\n value of node now is " << nd.value(); cout << "\n adding three child nodes:"; nd.addChild(node(-2)).addChild(node(-3)); nd.addChild(node(-4)); while(nd.mark() < nd.numChildren()) cout << "\n value of child is: " << nd.child(nd.next()-1).value(); /////////////////////////////////////////////// cout << endl << title("deleting 2nd node",'-'); /////////////////////////////////////////////// nd.delChild(1); nd.mark() = 0; while(nd.mark() < nd.numChildren()) cout << "\n value of child is: " << nd.child(nd.next()-1).value(); nd.mark() = 0; cout << "\n\n"; // //////////////////////////////////////// cout << title("Testing Tree Class",'='); //////////////////////////////////////// tree intTree; node root(4); intTree.setRoot(root); // add children to tree root for testing root.addChild(node(1)); root.addChild(node(2)); root.addChild(node(3)); root.child(0).addChild(node(5)).addChild(node(6)); root.child(1).addChild(node(7)); root.child(1).child(0).addChild(node(11)).addChild(node(12)); root.child(2).addChild(node(8)).addChild(node(9)); root.child(2).addChild(node(10)); root.child(2).child(1).addChild(node(13)); cout << "\n value of root is " << intTree.root().value(); cout << "\n\n"; ////////////////////////////////// cout << title("walking tree",'-'); ////////////////////////////////// intTree.walk(intTree.root()); //////////////////////////////////////////////////////// cout << '\f' << endl << title("deleting 2nd child of root",'-'); //////////////////////////////////////////////////////// intTree.root().delChild(1); intTree.walk(intTree.root()); ////////////////////////////////////////////////////////// cout << endl << title("searching for values in tree",'-'); ////////////////////////////////////////////////////////// searchForValue findInt8(8); if(intTree.find(findInt8,intTree.root())) cout << "\n found node with value " << findInt8.node().value(); else cout << "\n could not find value 8"; searchForValue findInt5(5); if(intTree.find(findInt5,intTree.root())) cout << "\n found node with value " << findInt5.node().value(); else cout << "\n could not find value 5"; searchForValue findInt12(12); if(intTree.find(findInt12, intTree.root())) cout << "\n found node with value " << findInt12.node().value(); else cout << "\n could not find value 12"; cout << "\n\n"; // ////////////////////////////////////////////////////////////////// cout << endl << title("searching for all nodes with a value",'-'); ////////////////////////////////////////////////////////////////// // create matching criteria functor collectValues collect8s(8); // add some duplicate nodes to tree root.child(1).child(0).addChild(node(8)); root.child(1).child(2).addChild(node(8)); // walk tree to show new structure intTree.walk(intTree.root()); // now find all nodes with a specific value intTree.findAll(collect8s, intTree.root()); vector*> nodesWith8 = collect8s.getNodes(); cout << "\n\n values collected: "; for(int i=0; ivalue() << ' '; cout << "\n\n"; } #endif