int main() { int tmp; while (scanf("%d", &n) != EOF) { bst.clear(); for (int i = 0; i < n; i++) { scanf("%d", &tmp); bst.insert(tmp); } bst.print(); } }
/** * A simple test driver for the BST class template. * P1 CSE 100 2013 * Author: P. Kube (c) 2013 */ int main() { /* Create an STL vector of some ints */ vector<int> v; v.push_back(3); v.push_back(4); v.push_back(1); v.push_back(33); v.push_back(66); v.push_back(144); v.push_back(1984); v.push_back(-92393); v.push_back(46); v.push_back(22); v.push_back(0); v.push_back(123445); v.push_back(100); v.push_back(-33); /* vector<string> v; v.push_back("hi"); v.push_back("hi no"); v.push_back("hi yes"); v.push_back("hi you"); v.push_back("hi fly"); v.push_back("hi w"); */ /* Create an instance of BST holding int */ BST<int> b; // BST<string> b; /* Insert a few data items. */ vector<int>::iterator vit = v.begin(); vector<int>::iterator ven = v.end(); for(; vit != ven; ++vit) { // all these inserts are unique, so should return a std::pair // with second part true std::pair<BST<int>::iterator,bool> pr = b.insert(*vit); // if(! pr.second ) { // cout << "Incorrect bool return value when inserting " << *vit << endl; // return -1; // } if(*(pr.first) != *vit) { cout << "Incorrect iterator return value when inserting " << *vit << endl; return -1; } } /* Test size. */ cout << "" << endl; cout << "Size is: " << b.size() << endl; cout << "" << endl; if(b.size() != 14) { cout << "... which is incorrect." << endl; return -1; } /* Test find return value. */ vit = v.begin(); for(; vit != ven; ++vit) { if(*(b.find(*vit)) != *vit) { cout << "Incorrect return value when finding " << *vit << endl; return -1; } } /* This is for testing find on something not in the BST * Causes a seg fault so it is taken out BST<int>::iterator it3 = b.find(5);; cout << *it3 << endl; */ /* Sort the vector, to compare with inorder iteration on the BST */ sort(v.begin(),v.end()); /* Test BST iterator; should iterate inorder */ cout << "traversal using iterator:" << endl; cout << "" << endl; vit = v.begin(); BST<int>::iterator en = b.end(); BST<int>::iterator it = b.begin(); for(; vit != ven; ++vit) { cout << *it << endl; if(*it != *vit) { cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl; return -1; } ++it; } cout << "" << endl; cout << "OK, order is correct" << endl; cout << "" << endl; /* Testing == and !=*/ cout << "Testing equals and not equals:" << endl; cout << "" << endl; BST<int>::iterator test1 = b.begin(); BST<int>::iterator test2 = b.begin(); if(*test1 != *test2) { cout << *test1 << " and " << *test2 << ": Should be equal." << endl; return -1; } cout << "Correct! " << *test1 << " and " << *test2 << ": Are equal." << endl; cout << "" << endl; ++test1; if(*test1 == *test2) { cout << *test1 << " and " << *test2 << ": Should NOT be equal." << endl; return -1; } cout << "Correct! " <<*test1 << " and " << *test2 << ": Are NOT equal." << endl; cout << "" << endl; cout << "== and != tests passed!" << endl; cout << "" << endl; cout << "Testing find...Printing all with find" << endl; cout << "" << endl; BST<int>::iterator test100 = b.find(3); cout << *test100 << endl; test100 = b.find(4); cout << *test100 << endl; test100 = b.find(1); cout << *test100 << endl; test100 = b.find(33); cout << *test100 << endl; test100 = b.find(66); cout << *test100 << endl; test100 = b.find(144); cout << *test100 << endl; test100 = b.find(1984); cout << *test100 << endl; test100 = b.find(-92393); cout << *test100 << endl; test100 = b.find(46); cout << *test100 << endl; test100 = b.find(22); cout << *test100 << endl; test100 = b.find(0); cout << *test100 << endl; test100 = b.find(123445); cout << *test100 << endl; test100 = b.find(100); cout << *test100 << endl; test100 = b.find(-33); cout << *test100 << endl; test100 = b.find(666); BST<int>::iterator test101 = b.end(); BST<int>::iterator test102 = b.begin(); if(test100 == test101) { cout << "666 returns null" << endl; cout << "" << endl; } if(test100 != test102) { cout << "Passes another check for !=" << endl; cout << "" << endl; } cout << "Testing Clear..." << endl; cout << "" << endl; b.clear(); cout << "Clear Ran, Now Testing Size." << endl; cout << "" << endl; /* Test size. */ cout << "Size is: " << b.size() << endl; cout << "" << endl; if(b.size() != 0) { cout << "BST WAS NOT CLEARED!!!!" << endl; return -1; } cout << "Correct! YAY! Super Great!" << endl; cout << "" << endl; /* Test BST iterator; should iterate inorder */ cout << "This should cause a seg fault if BST is empty:" << endl; cout << "" << endl; BST<int>::iterator it2 = b.begin(); cout << *it2 << endl; }
/** * A simple test driver for the BST class template. * P1 CSE 100 2013 * Author: P. Kube (c) 2013 */ int main() { /* Create an STL vector of some ints */ vector<int> v; v.push_back(3); v.push_back(4); v.push_back(1); v.push_back(100); v.push_back(-33); /* Create an instance of BST holding int */ BST<int> b; /* Insert a few data items. */ vector<int>::iterator vit = v.begin(); vector<int>::iterator ven = v.end(); for(; vit != ven; ++vit) { // all these inserts are unique, so should return a std::pair // with second part true std::pair<BST<int>::iterator,bool> pr = b.insert(*vit); cout << "vit = " << *vit << endl; if(! pr.second ) { cout << "Incorrect bool return value when inserting " << *vit << endl; return -1; } if(*(pr.first) != *vit) { cout << "Incorrect iterator return value when inserting " << *vit << endl; return -1; } } /* Test size. */ cout << "Size is: " << b.size() << endl; if(b.size() != v.size()) { cout<< "bsize is" << b.size() << endl; cout<< "vsize is" << v.size() << endl; cout << "... which is incorrect." << endl; return -1; } cout<<"simple test" << endl; for(BST<int>::iterator it = b.begin(); it != b.end(); ++it){ cout<<*it<< endl; } /* Test find return value. */ vit = v.begin(); for(; vit != ven; ++vit) { if(*(b.find(*vit)) != *vit) { cout << "Incorrect return value when finding " << *vit << endl; return -1; } } /* Sort the vector, to compare with inorder iteration on the BST */ sort(v.begin(),v.end()); /* Test BST iterator; should iterate inorder */ cout << "traversal using iterator:" << endl; vit = v.begin(); BST<int>::iterator en = b.end(); BST<int>::iterator it = b.begin(); for(; vit != ven; ++vit) { if(! (it != en) ) { cout << *it << "," << *vit << ": Early termination of BST iteration." << endl; return -1; } cout << *it << endl; if(*it != *vit) { cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl; return -1; } ++it; } cout << "OK." << endl; /* Testing clear and checking clear size afterwards*/ BST<int> c; vit = v.begin(); ven = v.end(); for(vit = v.begin(); vit != ven; ++vit) { // all these inserts are unique, so should return a std::pair // with second part true std::pair<BST<int>::iterator,bool> pr = c.insert(*vit); } c.clear(); cout<< "clear size is" << c.size() << endl; if(c.size() != 0){ cout << "fail" << endl; return -1; } /* Testing BST destructor */ BST<int>* d = new BST<int>(); d->insert(3); delete d; /*Test Clear then add and size after */ BST<int> e; vit = v.begin(); ven = v.end(); for(vit = v.begin(); vit != ven; ++vit) { // all these inserts are unique, so should return a std::pair // with second part true std::pair<BST<int>::iterator,bool> pr = e.insert(*vit); } e.clear(); e.insert(5); cout<< "e size is " << e.size() << endl; /* Test insert of same item */ BST<int> f; vit = v.begin(); ven = v.end(); for(; vit != ven; ++vit) { // all these inserts are unique, so should return a std::pair // with second part true std::pair<BST<int>::iterator,bool> pk = f.insert(*vit); } std::pair<BST<int>::iterator,bool> pi = f.insert(3); if(pi.second){ cout << "you f****d up" << endl; cout << "value is " << *(pi.first) << endl; return -1; } /* Clear an empty tree */ BST<int> q; q.clear(); }
// Warning: This test driver has horrible memory leaks. // Everytime the program does a remove to "it", the next time "it" // gets used, that previous Int object gets clobbered. int main() { BST<int, Int*> tree; Int* it; cout << "Size: " << tree.size() << "\n"; tree.insert(100, new Int(100)); tree.print(); cout << "Size: " << tree.size() << "\n"; it = tree.remove(10); tree.print(); cout << "Size: " << tree.size() << "\n"; tree.clear(); cout << "Cleared.\n"; cout << "Size: " << tree.size() << "\n"; tree.insert(15, new Int(15)); cout << "Size: " << tree.size() << "\n"; if ((it = tree.find(20)) != NULL) cout << "Found " << it << endl; else cout << "No key 20\n"; if ((it = tree.find(15)) != NULL) cout << "Found " << it << endl; else cout << "No key 15\n"; tree.print(); if ((it = tree.remove(20)) != NULL) cout << "Removed " << it << endl; else cout << "No key 20\n"; cout << "Now, insert 20\n"; tree.insert(20, new Int(20)); tree.print(); if ((it = tree.remove(20)) != NULL) cout << "Removed " << it << endl; else cout << "No key 20\n"; tree.print(); tree.insert(700, new Int(700)); cout << "Size: " << tree.size() << "\n"; tree.insert(350, new Int(350)); tree.insert(201, new Int(201)); tree.insert(170, new Int(170)); tree.insert(151, new Int(151)); tree.insert(190, new Int(190)); tree.insert(1000, new Int(1000)); tree.insert(900, new Int(900)); tree.insert(950, new Int(950)); tree.insert(10, new Int(10)); tree.print(); if ((it = tree.find(1000)) != NULL) cout << "Found " << it << endl; else cout << "No key 1000\n"; if ((it = tree.find(999)) != NULL) cout << "Found " << it << endl; else cout << "No key 999\n"; if ((it = tree.find(20)) != NULL) cout << "Found " << it << endl; else cout << "No key 20\n"; cout << "Now do some delete tests.\n"; tree.print(); if ((it = tree.remove(15)) != NULL) cout << "Removed " << it << endl; else cout << "No key 15\n"; tree.print(); if ((it = tree.remove(151)) != NULL) cout << "Removed " << it << endl; else cout << "No key 151\n"; tree.print(); if ((it = tree.remove(151)) != NULL) cout << "Removed " << it << endl; else cout << "No key 151\n"; if ((it = tree.remove(700)) != NULL) cout << "Removed " << it << endl; else cout << "No key 700\n"; tree.print(); tree.clear(); tree.print(); cout << "Size: " << tree.size() << "\n"; cout << "Finally, test iterator\n"; tree.insert(3500, new Int(3500)); tree.insert(2010, new Int(2010)); tree.insert(1700, new Int(1700)); tree.insert(1510, new Int(1510)); tree.insert(1900, new Int(1900)); tree.insert(10000, new Int(10000)); tree.insert(9000, new Int(9000)); tree.insert(9500, new Int(9500)); tree.insert(100, new Int(100)); tree.print(); cout << "Start:\n"; Int* temp; while (tree.size() > 0) { temp = tree.removeAny(); cout << temp << " "; } cout << "\n"; /*return 0;*/ //YaoC system("pause"); //YaoC }
/** * A simple test driver for the BST class template. * P1 CSE 100 2013 * Author: P. Kube (c) 2013 */ int main() { /* Create an STL vector of some ints */ vector<int> v; /* v.push_back(3); v.push_back(4); v.push_back(1); v.push_back(100); v.push_back(-33); v.push_back(-20); v.push_back(6); v.push_back(0); v.push_back(-1); v.push_back(-2); v.push_back(-3); v.push_back(-4); */ /* Create an instance of BST holding int */ BST<int> b; /* Insert a few data items. */ vector<int>::iterator vit = v.begin(); vector<int>::iterator ven = v.end(); for(; vit != ven; ++vit) { // all these inserts are unique, so should return a std::pair // with second part true std::pair<BST<int>::iterator,bool> pr = b.insert(*vit); if(! pr.second ) { cout << "Incorrect bool return value when inserting " << *vit << endl; return -1; } if(*(pr.first) != *vit) { cout << "Incorrect iterator return value when inserting " << *vit << endl; return -1; } } /* Test size. */ cout << "Size is: " << b.size() << endl; if(b.size() != v.size()) { cout << "... which is incorrect." << endl; return -1; } /* Test find return value. */ vit = v.begin(); for(; vit != ven; ++vit) { if(*(b.find(*vit)) != *vit) { cout << "Incorrect return value when finding " << *vit << endl; return -1; } } /* Sort the vector, to compare with inorder iteration on the BST */ sort(v.begin(),v.end()); /* Test BST iterator; should iterate inorder */ cout << "traversal using iterator:" << endl; vit = v.begin(); BST<int>::iterator en = b.end(); BST<int>::iterator it = b.begin(); for(; vit < ven; ++vit) { if(! (it != en) ) { cout << *it << "," << *vit << ": Early termination of BST iteration." << endl; return -1; } cout << *it << endl; if(*it != *vit) { cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl; return -1; } ++it; } /** Added a basic clear tests that checks for correct size after delete */ cout << "OK." << endl; b.clear(); cout << ((b.size() == 0) ? "PASS: clear() size of tree: " : "FAIL: clear() Size of tree: ") << b.size() << endl; /** ================================ Added test Cases =====================*/ /* Test fixtures */ vector<int> v2; srand(unsigned(time(NULL))); BST<int> b2; BST<int>* treeDestructorTest = new BST<int>(); unsigned int numInspected = 0; unsigned int sizeOfInput = 0; for(int i = 1000; i >= -1000; i--){ v2.push_back(i); sizeOfInput++; } /* randomize the ordering of -1000 - 1000 before inserting * so we don't just test a linked list */ std::random_shuffle(v2.begin(), v2.end()); vector<int>::iterator vit2 = v2.begin(); vector<int>::iterator ven2 = v2.end(); /* insert all the items in the randomized vector into the bst * and check the boolean return */ while( vit2 != ven2 ){ if( !(b2.insert(*vit2)).second ){ cout << "incorrect boolean return value of insert for: "<< *vit2 <<endl; return -1; } ++vit2; } cout << "PASS: insert() unique value, boolean return. " << endl; /* try to insert the values again. checking for correct return * boolean on duplicate insertion. */ for( vit2 = v2.begin(); vit2 != ven2; ++vit2 ){ if( (b2.insert(*vit2)).second ){ cout << "incorrect boolean return value of duplicate " << "insert for: "<< *vit2 << endl; return -1; } } cout << "PASS: insert() duplicate value, boolean return. " << endl; /** sort the vector for inorder traversal */ sort(v2.begin(), v2.end()); vector<int>::iterator vit3 = v2.begin(); vector<int>::iterator ven3 = v2.end(); BST<int>::iterator front = b2.begin(); BST<int>::iterator back = b2.end(); /* check that each returned element is the correct successor */ for( ; vit3 != ven3; ++front, ++vit3 ){ if( *front != *vit3 ){ cout << "FAIL: inorder traversal failed on : " << *front << " " << *vit3 << endl; return -1; } ++numInspected; } cout << "PASS: iterator correct element ordering in traversal. " << endl; /* check we traversed all the elements in the tree */ if( numInspected != sizeOfInput ){ cout << "FAIL: incomplete tree traversal. Read " << numInspected << " of " << sizeOfInput << " elements." << endl; return -1; }else{ cout << "PASS: iterator number of elements read Expected: " << sizeOfInput << " Result: " << numInspected << endl; } /* randomize the vector again */ std::random_shuffle(v2.begin(), v2.end()); /* test find on all the items in the vector & bst */ for(vit3 = v2.begin(); vit3 != ven3; ++vit3){ if( *(b2.find(*vit3)) != *vit3 ){ cout << "FAIL: find return value did not match: " << *vit3 << endl; return -1; } } cout << "PASS: find() returned all correct values." << endl; cout << ( (b2.size() == sizeOfInput) ? "PASS: size() returned expected tree size. " : "FAIL: size() returned non-matching tree size. " ) << endl; cout << ( (b2.empty() == false) ? "PASS: empty() returned false when not empty. " : "FAIL: empty() returned true when not empty. " ) << endl; b2.clear(); cout << ( (b2.empty() == true) ? "PASS: empty() returned true when empty. " : "FAIL: empty() returned false when empty. " ) << endl; /** Test for memory leaks when allocating a tree on the heap * using valgrind will tell you if this passes or not */ treeDestructorTest->insert(1); treeDestructorTest->insert(2); treeDestructorTest->insert(0); treeDestructorTest->clear(); treeDestructorTest->insert(3); treeDestructorTest->insert(5); treeDestructorTest->insert(0); delete treeDestructorTest; cout << "PASS: No errors when calling destructor after clear(). i" << endl; cout << " Run valgrind for memory leak analysis. " << endl; /*****************************START Testing for RST***********************/ RST<int> r; /* Insert a few data items. */ vit = v.begin(); //vector<int>::iterator ven = v.end(); for(; vit != ven; ++vit) { // all these inserts are unique, so should return a std::pair // with second part true std::pair<RST<int>::iterator,bool> pr = r.insert(*vit); if(! pr.second ) { cout << "Incorrect bool return value when inserting into RST " << *vit << endl; return -1; } if(*(pr.first) != *vit) { cout << "Incorrect iterator return value when inserting into RST " << *vit << endl; return -1; } } //PASS insert rst test cout << "PASS Insert into RST" << endl; /* Test size. */ cout << "Size is: " << r.size() << endl; if(r.size() != v.size()) { cout << "... which is incorrect." << endl; return -1; } /* Test find return value. */ vit = v.begin(); for(; vit != ven; ++vit) { if(*(r.find(*vit)) != *vit) { cout << "Incorrect return value when finding " << *vit << endl; return -1; } } /* Sort the vector, to compare with inorder iteration on the BST */ sort(v.begin(),v.end()); /* Test BST iterator; should iterate inorder */ cout << "traversal using iterator:" << endl; vit = v.begin(); //BST<int>::iterator en = r.end(); //BST<int>::iterator it = r.begin(); for(; vit < ven; ++vit) { if(! (it != en) ) { cout << *it << "," << *vit << ": Early termination of BST iteration." << endl; return -1; } cout << *it << endl; if(*it != *vit) { cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl; return -1; } ++it; } /** Added a basic clear tests that checks for correct size after delete */ cout << "OK." << endl; r.clear(); cout << ((r.size() == 0) ? "PASS: clear() size of tree: " : "FAIL: clear() Size of tree: ") << r.size() << endl; //END OF FILE }
int main(int argc, char * argv[]){ /* Variable Declarations */ double stdev = 0; double avgTotal =0; int maxSize = 0; //max size of tree int index = 1; int numRuns = 0; //Number of benchmark runs to preform int N = 1; int power = 1; bool shuffle = false; bool bst = false; bool rst = false; bool set1 = false; //Switch statement to parse user input while (argv[index] != NULL ){ //get argument //string word ( argv[index]); if( !strcmp(argv[index],"bst") ) { //Code to generate BST bst = true; } else if ( !strcmp (argv[index], "rst") ) { //Code to generate RST rst = true; } else if ( !strcmp(argv[index], "set" ) ) { //Code to generate c++ set set1 = true; } else if ( !strcmp(argv[index], "shuffled" ) ) { //Code to shuffle input shuffle = true; } index++; } //Get the max size as 3rd user argument maxSize = atoi( argv[3] ); //Get the number of benchmark runs to preform numRuns = atoi( argv[4] ) - 1; if (bst){ BST<countint> s; //cout messages to user cout << "# Benchmarking average number of comparisons for successful find" << endl; cout << "# Data Structure: " << argv[1] << endl; cout << "# Data: " << argv[2] << endl; cout << "# N is powers of 2, minus 1, from 1 to " << argv[3] << endl; cout << "# Averaging over 5 runs for each N" << endl; cout << "#" << endl; cout << "# N avgcomps stdev"<< endl; while ( N < maxSize ){ vector<countint> v; v.clear(); for(int i = N; 0 < i ; --i) { v.push_back(i); } vector<countint>::iterator vit = v.begin(); vector<countint>::iterator ven = v.end(); double totalSquared = 0; //For the number of runs passed in for ( int run = 0; run < numRuns; run++ ){ //if user wanted elements shuffeled if (shuffle){ std::random_shuffle( v.begin(),v.end() ); } //Insert into the vector vit = v.begin(); s.clear(); for(int i = 0; i < N ; i++ ){ //cerr << "variable being inserted: " << *vit << endl; s.insert(*vit); vit++; } //create avgComps variable double avgcomps = 0; //now start the count variable for find countint::clearcount(); for(vit = v.begin(); vit != ven; ++vit){ s.find(*vit); } //now get avg computations avgcomps = countint::getcount() / (double) N; //now calculate the standard deviation avgTotal = avgTotal + avgcomps; totalSquared = totalSquared + pow(avgcomps,TWO); } //average over the amount of runs avgTotal = (avgTotal / numRuns); //get the s variable t^2/R totalSquared = (totalSquared/numRuns); stdev = sqrt( (totalSquared) - (pow(avgTotal,TWO)) ); //print statements cout << setw(6) << N << setw(10) << avgTotal << setw(15) << stdev << endl; //reset avgTotal avgTotal = 0; stdev = 0; totalSquared = 0; power++; //increase N by power of 2 N = pow(TWO,power) -1; } } else if (rst){ RST<countint> s; //cout messages to user cout << "# Benchmarking average number of comparisons for successful find" << endl; cout << "# Data Structure: " << argv[1] << endl; cout << "# Data: " << argv[2] << endl; cout << "# N is powers of 2, minus 1, from 1 to " << argv[3] << endl; cout << "# Averaging over 5 runs for each N" << endl; cout << "#" << endl; cout << "# N avgcomps stdev"<< endl; while ( N < maxSize ){ vector<countint> v; v.clear(); for(int i = N; 0 < i ; --i) { v.push_back(i); } vector<countint>::iterator vit = v.begin(); vector<countint>::iterator ven = v.end(); double totalSquared = 0; //For the number of runs passed in for ( int run = 0; run < numRuns; run++ ){ //if user wanted elements shuffeled if (shuffle){ std::random_shuffle( v.begin(),v.end() ); } //Insert into the vector vit = v.begin(); s.clear(); for(int i = 0; i < N ; i++ ){ //cerr << "variable being inserted: " << *vit << endl; s.insert(*vit); vit++; } //create avgComps variable double avgcomps = 0; //now start the count variable for find countint::clearcount(); for(vit = v.begin(); vit != ven; ++vit){ s.find(*vit); } //now get avg computations avgcomps = countint::getcount() / (double) N; //now calculate the standard deviation avgTotal = avgTotal + avgcomps; totalSquared = totalSquared + pow(avgcomps,TWO); } //average over the amount of runs avgTotal = (avgTotal / numRuns); //get the s variable t^2/R totalSquared = (totalSquared/numRuns); stdev = sqrt( (totalSquared) - (pow(avgTotal,TWO)) ); //print statements cout << setw(6) << N << setw(10) << avgTotal << setw(15) << stdev << endl; //reset avgTotal avgTotal = 0; stdev = 0; totalSquared = 0; power++; //increase N by power of 2 N = pow(TWO,power) -1; } } if(set1){ set<countint> s; //cout messages to user cout << "# Benchmarking average number of comparisons for successful find" << endl; cout << "# Data Structure: " << argv[1] << endl; cout << "# Data: " << argv[2] << endl; cout << "# N is powers of 2, minus 1, from 1 to " << argv[3] << endl; cout << "# Averaging over 5 runs for each N" << endl; cout << "#" << endl; cout << "# N avgcomps stdev"<< endl; while ( N < maxSize ){ vector<countint> v; v.clear(); for(int i = N; 0 < i ; --i) { v.push_back(i); } vector<countint>::iterator vit = v.begin(); vector<countint>::iterator ven = v.end(); double totalSquared = 0; //For the number of runs passed in for ( int run = 0; run < numRuns; run++ ){ //if user wanted elements shuffeled if (shuffle){ std::random_shuffle( v.begin(),v.end() ); } //Insert into the vector vit = v.begin(); s.clear(); for(int i = 0; i < N ; i++ ){ //cerr << "variable being inserted: " << *vit << endl; s.insert(*vit); vit++; } //create avgComps variable double avgcomps = 0; //now start the count variable for find countint::clearcount(); for(vit = v.begin(); vit != ven; ++vit){ s.find(*vit); } //now get avg computations avgcomps = countint::getcount() / (double) N; //now calculate the standard deviation avgTotal = avgTotal + avgcomps; totalSquared = totalSquared + pow(avgcomps,TWO); } //average over the amount of runs avgTotal = (avgTotal / numRuns); //get the s variable t^2/R totalSquared = (totalSquared/numRuns); stdev = sqrt( (totalSquared) - (pow(avgTotal,TWO)) ); //print statements cout << setw(6) << N << setw(10) << avgTotal << setw(15) << stdev << endl; //reset avgTotal avgTotal = 0; stdev = 0; totalSquared = 0; power++; //increase N by power of 2 N = pow(TWO,power) -1; } } //endfile }
int main(int argc, char** argv) { if(argc != 5) return 1; string structure = argv[1]; //BST, RST, or std::set string order = argv[2]; //sorted or shuffled int maxSize = atoi(argv[3]); //Max size of tree int numRuns = atoi(argv[4]); //Number of runs cout << "# Benchmarking average number of comparisons for successful " << "find" << endl; cout << "# Data structure: " << structure << endl; cout << "# Data: " << order << endl; cout << "# N is powers of 2, minus 1, from 1 to 32768" << endl; cout << "# Averaging over " << numRuns << " runs for each N" << endl; cout << "# " << endl; cout << "# N\t avgComps\t stddev" << endl; if (structure == "bst") { for (int j = 1, N = 1; N <= maxSize; N = pow(2,++j)-1) { double avgComps = 0; double totalAvgNumRuns = 0; double standDev = 0; std::vector<countint> v; /* Create an empty instance of BST holding countint */ BST<countint>* b = new BST<countint>(); v.clear(); for (int i = 0; i < N ; ++i) { v.push_back(i); } if (order == "shuffled") { std::random_shuffle(v.begin(), v.end()); } std::vector<countint>::iterator vit = v.begin(); std::vector<countint>::iterator ven = v.end(); for (int f = 1; f <= numRuns; f++) { for(vit = v.begin(); vit != ven; ++vit) { std::pair<BST<countint>::iterator,bool> pr = b->insert(*vit); if(! pr.second ) { cout << "Incorrect bool return value when inserting " << *vit << endl; return -1; } } countint::clearcount(); for(vit = v.begin(); vit != ven; ++vit) { b->find(*vit); } //Compute Stuffs avgComps += countint::getcount()/(double)N; totalAvgNumRuns += pow(countint::getcount()/(double)N,2.0); countint::clearcount(); b->clear(); v.clear(); } //Compute More Stuffs avgComps = avgComps/(double)numRuns; totalAvgNumRuns = totalAvgNumRuns/(double)numRuns; standDev = sqrt(abs(totalAvgNumRuns - pow(avgComps,2))); //printing output cout << setw(6) << right << N << "\t" << setw(12) << right << avgComps << setw(16) << right << setiosflags(ios::fixed) << setprecision(8) << standDev << endl; } } else if (structure == "rst") { for (int j = 1, N = 1; N <= maxSize; N = pow(2,++j)-1) { double avgComps = 0; double totalAvgNumRuns = 0; double standDev = 0; for (int f = 0; f < numRuns; f++) { std::vector<countint> v; v.clear(); for (int i = 0; i < N ; ++i) { v.push_back(i); } if (order == "shuffled") { std::random_shuffle(v.begin(), v.end()); } std::vector<countint>::iterator vit = v.begin(); std::vector<countint>::iterator ven = v.end(); srand(time(0)); /* Create an empty instance of RST holding countint */ BST<countint>* r = new RST<countint>(); for(vit = v.begin(); vit != ven; ++vit) { std::pair<BST<countint>::iterator,bool> pr = r->insert(*vit); if(! pr.second ) { cout << "Incorrect bool return value when inserting " << *vit << endl; return -1; } } countint::clearcount(); for(vit = v.begin(); vit != ven; ++vit) { r->find(*vit); } //Compute Stuffs avgComps += countint::getcount()/(double)N; totalAvgNumRuns += pow(countint::getcount()/(double)N,2.0); countint::clearcount(); r->clear(); v.clear(); } //Compute More Stuffs avgComps = avgComps/(double)numRuns; totalAvgNumRuns = totalAvgNumRuns/(double)numRuns; standDev = sqrt(abs(totalAvgNumRuns - pow(avgComps,2))); //printing output cout << setw(6) << right << N << "\t" << setw(12) << right << avgComps << setw(16) << right << setiosflags(ios::fixed) << setprecision(8) << standDev << endl; } } else if (structure == "set") { for (int j = 1, N = 1; N <= maxSize; N = pow(2,++j)-1) { double avgComps = 0; double totalAvgNumRuns = 0; double standDev = 0; std::vector<countint> v; /* Create an empty instance of RST holding countint */ std::set<countint>* s = new std::set<countint> (); v.clear(); for (int i = 0; i < N ; ++i) { v.push_back(i); } if (order == "shuffled") { std::random_shuffle(v.begin(), v.end()); } std::vector<countint>::iterator vit = v.begin(); std::vector<countint>::iterator ven = v.end(); for (int f = 0; f < numRuns; f++) { for(vit = v.begin(); vit != ven; ++vit) { s->insert(*vit); } countint::clearcount(); for(vit = v.begin(); vit != ven; ++vit) { s->find(*vit); } //Compute Stuffs avgComps += countint::getcount()/(double)N; totalAvgNumRuns += pow(countint::getcount()/(double)N,2.0); countint::clearcount(); s->clear(); v.clear(); } //Compute More Stuffs avgComps = avgComps/(double)numRuns; totalAvgNumRuns = totalAvgNumRuns/(double)numRuns; standDev = sqrt(abs(totalAvgNumRuns - pow(avgComps,2))); //printing output cout << setw(6) << right << N << "\t" << setw(12) << right << avgComps << setw(16) << right << setiosflags(ios::fixed) << setprecision(8) << standDev << endl; } } return 0; }
int main () { vector<string> temp; BST<string> myBst; string usr_input, input = "q,w,e,r,t,a,s,d,f,g,z,x,c,v"; int tSize = tokenSize(input, del); cout << "Create default BinarySearchTree (BST): " << endl; temp.clear(); parse(input, temp, del); cout << "Display the binary search tree as it is being built:\n "; for (int i = 0; i < tSize; i++) { myBst.insert(temp[i]); cout << temp[i] << ( ( i < (tSize-1) ) ? "," : "\n" ); } myBst.print(); menu(); while (true) { cout << "\nTRACE(" << ( (trace==0) ? "OFF" : "ON" ) << "): "; getline(cin,usr_input); if (usr_input.length() > 1) cout << "\nSingle Character ONLY! " << endl; else { char in = tolower(usr_input[0]); if (in == 'p') { if (myBst.empty()) cout << "This BST is EMPTY!\n"; else myBst.print(); } else if (in == 'i') { cout << "Enter token: "; getline(cin, input); myBst.insert(input); if (trace > 0) { myBst.print(); cout << "____________________________________\n"; } cout << endl << input << " is inserted to the binary search tree.\n"; } else if (in == 'r') { cout << "Enter token: "; getline(cin, input); myBst.erase(input); } else if (in == 'b') { myBst.clear(); temp.clear(); string input; cout << "---- Enter a (\'" << del << "\' separated) string -> "; getline(cin, input); parse(input, temp, del); tSize = tokenSize(input,del); for (int i = 0; i < tSize; i++) { myBst.insert(temp[i]); if (trace > 1) { myBst.print(); cout << "____________________________________\n"; } } } else if (in == 'e') cout << "Binary search tree is: " << ((myBst.empty()) ? "EMPTY\n": "NOT EMPTY\n"); else if (in == 'c') myBst.clear(); else if (in == 't') trace = (trace + 1) % 2; else if (in == 'm') menu(); else if (in == 'q') exit(0); else cout << "Invalid Input ! " << endl; } } }