void DestructorTest() { cout << "--------------------------DestructorTest---------------------------" << endl; RedBlackTree<int> tree; cout << "Create Tree..." << endl; tree.Insert(6); tree.Insert(7); tree.Insert(8); tree.Insert(9); cout << "Remove All of Tree..." << endl << endl; tree.RemoveAll(); RedBlackTree<int> tree2; cout << "Create Tree2..." << endl << endl; tree2.Insert(0); tree2.Insert(1); tree2.Insert(2); tree2.Insert(3); RedBlackTree<int> tree3(tree2); cout << "Create Tree3 (Copy of Tree2)..." << endl; cout << "Insert into Tree3..." << endl << endl; tree3.Insert(10); tree3.Insert(55); tree3.Insert(96); tree3.Insert(777); cout << "Destory Empty Tree1..." << endl; cout << "Destory Tree2..." << endl; cout << "Destory Copy Tree3..." << endl << endl; }
TEST_F(TestRBTree, RedBlackTree) { RedBlackTree<int32> tree; tree.insert(1); tree.insert(2); tree.insert(3); tree.dump(); }
int main() { RedBlackTree t; for (unsigned i = 1000; i > 0; --i) { int k = static_cast<int>(rand()); cout << "inserting " << k << endl; t.insert(k); } }
void CopyTest() { cout << "--------------------------CopyTest---------------------------" << endl; RedBlackTree<int> tree; cout << "Create Tree..." << endl; tree.Insert(6); tree.Insert(77); tree.Insert(1); tree.Insert(9); tree.Insert(11); tree.Insert(5); cout << endl << "Verify Red Black Tree..." << endl; tree.verify(tree); cout << "Size: " << tree.Size() << endl; RedBlackTree<int> treeC(tree); cout << "Perform Copy Tree..." << endl; cout << "Size of Copy: " << treeC.Size() << endl; cout << endl << "Verify Copy Red Black Tree..." << endl; treeC.verify(treeC); cout << "Remove 2 Elements of Copy" << endl; treeC.Remove(5); treeC.Remove(1); cout << "Size of Copy: " << treeC.Size() << endl; //Verify order of Tree cout << endl << "Verify Copy Red Black Tree..." << endl; treeC.verify(treeC); cout << endl; }
int main(){ int i; RedBlackTree t; RedBlackNode * ptr; RedBlackNode * tpr; i = 3; ptr = t.BSTinsert(i); cout << "****insert " << i << "************************************" << endl; cout << t; i = 22; tpr = t.BSTinsert(i); cout << "****insert " << i << "************************************" << endl; cout << t; i = 15; t.BSTinsert(i); cout << "****insert " << i << "************************************" << endl; cout << t; i = 2; t.BSTinsert(i); cout << "****insert " << i << "************************************" << endl; cout << t; i = 12; t.BSTinsert(i); cout << "****insert " << i << "************************************" << endl; cout << t; i = 25; t.BSTinsert(i); cout << "****insert " << i << "************************************" << endl; cout << t; cout << "****right rotate at 3************************************" << endl; t.rightrotate(ptr); cout << t; cout<< "****left rotate at 22*************************************" <<endl; t.leftrotate(tpr); cout << t; cout<< "****right rotate at 22*************************************" <<endl; t.rightrotate(tpr); cout << t; }
void InsertTestB() { cout << "--------------------------Insert TestB---------------------------" << endl; RedBlackTree<int> tree; tree.Insert(20); tree.Insert(3); tree.Insert(7); tree.Insert(5); tree.Insert(6); tree.Insert(10); tree.Insert(21); tree.Insert(23); cout << "New Tree..." << endl; cout << "Verify Red Black Tree..." << endl; tree.verify(tree); RedBlackTree<int> treeC(tree); cout << "Copy And Insert..." << endl; treeC.Insert(55); treeC.Insert(9); treeC.Insert(11); treeC.Insert(777); cout << "Verify Copy and Inserted Red Black Tree..." << endl; treeC.verify(treeC); cout << endl; }
bool test_redblack() { RedBlackTree<int> rbt; int lim = 1000; srand(time(NULL)); for(int i = 0; i < lim; i++) { rbt.insert((i + lim ) % lim); } int *sorted = rbt.inorder(); printf("\n"); for(int i = 0; i < 15; i++) { printf("%d", sorted[i]); } printf("\n"); return true; }
int main() { srand(time(0)); BinarySearchTree BST; RedBlackTree RBT; cout << "Inserting 1000 ordered elements into a Binary Search Tree and a Red Black Tree:" << endl << endl; for (int i = 0; i < 1000; i++) { BST.insert(i); RBT.insert(i); } cout << "Height of the Binary Search Tree: " << BST.height() << endl; cout << "Height of the Red Black Tree: " << RBT.height() << endl; cout << endl; BinarySearchTree BST2; RedBlackTree RBT2; cout << "Inserting 1000 random elements into a Binary Search Tree and a Red Black Tree:" << endl << endl; int r; for (int i = 0; i < 1000; i++) { r = rand() % 1000; BST2.insert(r); RBT2.insert(r); } cout << "Height of the Binary Search Tree: " << BST2.height() << endl; cout << "Height of the Red Black Tree: " << RBT2.height() << endl; cout << endl; }
int main(int argc, char **argv) { RedBlackTree *rbt = new RedBlackTree(1); rbt->Insert(1); int numElements = (argc > 1 ? std::atoi(argv[1]) : 1000000); std::clock_t start = std::clock(); for (int i = 2; i <= numElements; i++) { rbt->Insert(std::rand() % (numElements * 10)); std::cout << rbt->ToString() << std::endl; std::cout << "---------------------------" << std::endl; } // double duration = (std::clock() - start) / (double)CLOCKS_PER_SEC; // std::cout << "Done inserting " << numElements << " elements. Insertion took // " // << duration << " seconds. That is equal to " // << numElements / duration << " insertions per second.\n" // << std::endl; // // bool foundAll = true; // start = std::clock(); // for (int i = 2; i <= numElements; i++) { // foundAll = foundAll && (rbt->Search(i) != nullptr); // } // duration = (std::clock() - start) / (double)CLOCKS_PER_SEC; // // std::cout << "Done searching " << numElements << " elements, " // << (foundAll ? "found all" : "did not find all") // << " elements. Search took " << duration // << " seconds. That is equal to " << numElements / duration // << " searches per second.\n" << std::endl; std::cout << rbt->Deepest() << std::endl; std::cout << rbt->Shallowest() << std::endl; }
int main(int argc, char * argv[]){ if(argc != 2){ cout << "USAGE ./trees <input-file>" << endl; return 1; } string file(argv[1]); ifstream infile(file); string line; RedBlackTree RBT; while(getline(infile, line)){ stringstream ss(line); int input; ss >> input; RBT.insertNode(input); } RBT.printContents(); RBT.showTree(); infile.close(); // RBT.test_search(file); return 0; }
int main(int argc, char const *argv[]) { RedBlackTree<int, string> sample; sample.insert(make_pair(10,"haha")); sample.insert(make_pair(20,"haha")); sample.insert(make_pair(30,"haha")); sample.insert(make_pair(15,"haha")); sample.insert(make_pair(25,"haha")); sample.insert(make_pair(12,"haha")); sample.insert(make_pair(5,"haha")); sample.insert(make_pair(3,"haha")); sample.insert(make_pair(8,"haha")); sample.insert(make_pair(27,"haha")); sample.insert(make_pair(40,"haha")); sample.insert(make_pair(50,"haha")); sample.insert(make_pair(45,"haha")); sample.insert(make_pair(9,"haha")); sample.print(); return 0; }
int main() { RedBlackTree rb; srand(time(NULL)); /* insert delete는 매번 호출 후에 redblack tree가 유지되고 있는지 검사합니다. */ //insert 임의의 숫자를 insert. std::cout << "insert start\n" << std::endl; for (int i = 0; i < 20; i++) { rb.Insert(rand() %20); } //search. delete안에서 매번 호출하기 때문에 10만 search해봤습니다. std::cout << "\n\n\nsearch start\n" << std::endl; Node* result = rb.SearchData(10); if(result) std::cout << "search 10: " << (result->data) << std::endl; else std::cout << "No Such Data" << std::endl; //delete 임의의 숫자를 delete. 없는 경우 no matching data가 뜹니다. std::cout << "\n\n\ndelete start" << std::endl; for (int i = 0; i < 20; i++) { rb.Delete(rand()%20); } getchar(); }
void AssignmentTest() { cout << "--------------------------AssignmentTest---------------------------" << endl; cout << endl << "Basic Tree Size of Four" << endl; cout << "Insert.." << endl; RedBlackTree<int> tree; tree.Insert(5); tree.Insert(4); tree.Insert(3); tree.Insert(2); cout << "Verify Red Black Tree 1..." << endl; tree.verify(tree); RedBlackTree<int> tree2; cout << "Assign Tree 1 to Tree 2..." << endl << endl; tree2 = tree; cout << "Verify Red Black Tree 2..." << endl; tree2.verify(tree2); cout << "Creat Blank Tree..." << endl ; RedBlackTree<int> treeEmpty; cout << "Size: " << treeEmpty.Size() << endl << endl; RedBlackTree<int> tree3(tree); cout << "Creat Tree3 Copy of Tree1..." << endl ; cout << "Insert..." << endl; tree3.Insert(3); tree3.Insert(2); cout << "Verify Red Black Tree 3..." << endl; tree3.verify(tree3); cout << "Assign Tree3 to Empty Tree..." << endl << endl; cout << "Verify Red Black Tree 3..." << endl; tree3 = treeEmpty; tree3.verify(tree3); cout << endl; }
int TestRedBlackTree_String() { RedBlackTree<std::string, std::string> *rbtree = new RedBlackTree<std::string, std::string>(); TEST_ASSERT(rbtree->size() == 0); rbtree->insert("first", "one"); rbtree->insert("second", "two"); rbtree->insert("third", "three"); rbtree->insert("fourth", "four"); std::string res; TEST_ASSERT(rbtree->size() == 4); TEST_ASSERT(rbtree->find("first", res)); TEST_ASSERT(res == "one"); TEST_ASSERT(!rbtree->exists("fifth")); TEST_ASSERT(rbtree->exists("second")); TEST_ASSERT(rbtree->find("second", res)); TEST_ASSERT(res == "two"); TEST_ASSERT(!rbtree->erase("fifth")); TEST_ASSERT(rbtree->size() == 4); TEST_ASSERT(rbtree->exists("first")); TEST_ASSERT(rbtree->erase("first")); TEST_ASSERT(rbtree->size() == 3); TEST_ASSERT(rbtree->exists("second")); TEST_ASSERT(rbtree->erase("second")); TEST_ASSERT(rbtree->size() == 2); TEST_ASSERT(rbtree->exists("third")); TEST_ASSERT(rbtree->erase("third")); TEST_ASSERT(rbtree->size() == 1); TEST_ASSERT(rbtree->exists("fourth")); TEST_ASSERT(rbtree->erase("fourth")); TEST_ASSERT(rbtree->size() == 0); delete rbtree; return 0; }
int main() { cout << "\n-------------------------------------------------------------------\n"; srand(unsigned(time(0))); vector<int> myvector; cout << "Initializing a random vector with 1 million values"; cout << endl << "Values are unique and inclue 1 and 1 million" << endl; // set some values: for (int i=1; i<=1000000; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 // using built-in random generator: random_shuffle ( myvector.begin(), myvector.end() ); // using myrandom: random_shuffle ( myvector.begin(), myvector.end(), myrandom); cout << '\n'; //initializing two clock variables to measure execution time. clock_t t1,t2; cout << "---------------------------------------------------------------\n"; /* For 2-3 B Tree */ cout << "\nFor a 2-3 B Tree\n\n"; cout << "Inserting unique values from 1 to 1,000,000 randomly\n"; // start clock t1 = clock(); BTree Bt(3); for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) { Bt.insert(*it); } t2 = clock(); // end clock float time_diff = ((float)t2-(float)t1); // converting the time to seconds before console printing // CLOCKS_PER_SEC is a macro from the time library cout << "Net time taken for insert:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl << endl; // deleting values in the same order: cout << "\nDeleting values from 1 to 1,000,000 serially in ascending order\n"; t1 = clock(); for (int i = 1; i <= 1000000; i++) { Bt.remove(i); } t2 = clock(); // end clock time_diff = ((float)t2-(float)t1); // converting the time to seconds before console printing // CLOCKS_PER_SEC is a macro from the time library cout << "net time taken for delete:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl; cout << "**********************************************************************\n"; /* For BinarySearch Tree */ cout << "For a BinarySearch Tree\n\n"; cout << "Inserting unique values from 1 to 1,000,000 randomly\n"; // start clock t1 = clock(); BinarySearchTree bst; for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) { bst.insert(*it); } t2 = clock(); // end clock time_diff = ((float)t2-(float)t1); // converting the time to seconds before console printing // CLOCKS_PER_SEC is a macro from the time library cout << "Net time taken for insert:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl; // deleting values in the same order: cout << "\nDeleting values from 1 to 1,000,000 serially in ascending order\n"; t1 = clock(); for (int i = 1; i <= 1000000; i++ ){ bst.remove(i); } t2 = clock(); // end clock time_diff = ((float)t2-(float)t1); // converting the time to seconds before console printing // CLOCKS_PER_SEC is a macro from the time library cout << "Net time taken for delete:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl; cout << "\n**********************************************************************\n"; /* For an AVL Tree */ cout << "For a Red Black Tree\n\n"; cout << "Inserting unique values from 1 to 1,000,000 randomly\n"; // start clock t1 = clock(); RedBlackTree<int> rbt; for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) { rbt.insert_key(*it); } t2 = clock(); // end clock time_diff = ((float)t2-(float)t1); // converting the time to seconds before console printing // CLOCKS_PER_SEC is a macro from the time library cout << "Net time taken for insert:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl; // deleting values in the same order: cout << "\nDeleting values from 1 to 1,000,000 serially in ascending order\n"; t1 = clock(); for (int i = 1; i <= 1000000; i++ ){ rbt.delete_key(i); } t2 = clock(); // end clock time_diff = ((float)t2-(float)t1); // converting the time to seconds before console printing // CLOCKS_PER_SEC is a macro from the time library cout << "Net time taken for delete:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl; cout << "\n**********************************************************************\n"; /* For an AVL Tree */ cout << "For an AVL Tree\n\n"; cout << "Inserting unique values from 1 to 1,000,000 randomly\n"; // start clock t1 = clock(); AVLTree avlt; for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) { avlt.InsertNode(*it); } t2 = clock(); // end clock time_diff = ((float)t2-(float)t1); // converting the time to seconds before console printing // CLOCKS_PER_SEC is a macro from the time library cout << "Net time taken for insert:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl; // deleting values in the same order: cout << "\nDeleting values from 1 to 1,000,000 serially in ascending order\n"; t1 = clock(); for (int i = 1; i <= 1000000; i++ ){ avlt.RemoveNode(i); } t2 = clock(); // end clock time_diff = ((float)t2-(float)t1); // converting the time to seconds before console printing // CLOCKS_PER_SEC is a macro from the time library cout << "Net time taken for delete:(in seconds) " << time_diff / CLOCKS_PER_SEC << endl; cout << "\n**********************************************************************\n"; return 0; }
void ParseMemoryLeakFile(const char *_inputFilename, const char *_outputFilename) { /* */ /* Start up */ /* */ RedBlackTree<char *, int> combined; RedBlackTree<char *, int> frequency; int unrecognised = 0; /* */ /* Open the file and start parsing */ /* */ FILE *memoryfile = fopen(_inputFilename, "rb"); while (memoryfile && !feof(memoryfile)) { char thisline[1024]; fgets(thisline, 1024, memoryfile); if (!strncmp(thisline, " Data:", 6) == 0) { /* This line is a data line - useless to us */ if (strchr(thisline, ':')) { /* This line does not have a source file location - useless to us */ /* Get the size */ char *lastcomma = strrchr(thisline, ','); if (lastcomma == 0) continue; char *ssize = lastcomma + 2; int size; char unused[32]; sscanf(ssize, "%d %s", &size, unused); /* Get the source file name */ char *sourcelocation = thisline; char *colon = strrchr(thisline, ':'); *(colon - 1) = '\x0'; /* Put the result into our BTree */ int result = 0; bool found = combined.find(sourcelocation, result); if (found) combined.replace(sourcelocation, result + size); else combined.insert(sourcelocation, size); found = frequency.find(sourcelocation, result); if (frequency.exists(sourcelocation)) frequency.replace(sourcelocation, result + size); else frequency.insert(sourcelocation, 1); } else { char *lastcomma = strrchr(thisline, ','); if (lastcomma) { char *ssize = lastcomma + 2; int size; char unused[32]; sscanf(ssize, "%d %s", &size, unused); unrecognised += size; } } } } fclose(memoryfile); /* */ /* Sort the results into a list */ /* */ LList<char *> sorted; DArray<char *> *dataI = combined.ConvertIndexToDArray(); DArray<int> *dataD = combined.ConvertToDArray(); int totalsize = 0; for (size_t i = 0; i < dataI->size(); i++) { if (dataI->valid(i)) { char *newsource = dataI->get(i); int newsize = dataD->get(i); totalsize += newsize; bool inserted = false; for (size_t i = 0; i < sorted.size(); i++) { char *existingsource = sorted.get(i); int existingsize; combined.find(existingsource, existingsize); if (newsize <= existingsize) { sorted.insert_at(newsource, i); inserted = true; break; } } if (!inserted) sorted.insert(newsource); } } delete dataI; delete dataD; /* */ /* Open the output file */ /* */ if (sorted.size()) { FILE *output = fopen(_outputFilename, "wt"); /* */ /* Print out our sorted list */ /* */ fprintf(output, "Total recognised memory leaks : %d Kbytes\n", int( totalsize / 1024 )); fprintf(output, "Total unrecognised memory leaks : %d Kbytes\n\n", int( unrecognised / 1024 )); for (int k = (int)sorted.size() - 1; k >= 0 && k < (int)sorted.size(); k--) { char *source = sorted.get(k); CoreAssert(source); int size; combined.find(source, size); int freq; frequency.find(source, freq); if (size > 1048576) { fprintf(output, "%-95s (%d MB in %d leaks)\n", source, int( size / 1048576 ), freq); } else if (size > 2048) { fprintf(output, "%-95s (%d KB in %d leaks)\n", source, int( size / 1024 ), freq); } else { fprintf(output, "%-95s (%d bytes in %d leaks)\n", source, size, freq); } } /* */ /* Clean up */ fclose(output); } }
RedBlackTree<T>::RedBlackTree(const RedBlackTree& rbtree) : root(NULL), size(0) { CopyTree(GetRoot(), rbtree.GetRoot(), rbtree.GetRoot()); size = rbtree.Size(); }
int main() { cout<< endl << "reb black tree test :"<<endl; RedBlackTree p; cout<<p.Find(4)<<endl; p.Insert(9); p.Insert(37); p.Insert(4); p.Insert(53); p.Insert(6); p.Insert(45); p.Insert(1); p.InOrderTraverse(); p.Delete(9); p.InOrderTraverse(); cout<<p.Find(4)<<endl; p.Delete(4); cout<<p.Find(4)<<endl; p.InOrderTraverse(); p.Insert(9); p.Insert(37); p.InOrderTraverse(); return(0); }
void Display::ShowDataStructItems() { ArrayStack<int> dsArrayStack; ListStack<int> lsListStack; ArrayQueue<int> aqArrayQueue; CycleDoublyLinkList<int> dcllCycleDoublyLinkList; BinarySearchTree<int> bstBinarySearchTree; RedBlackTree<int> rbtRedBlackTree; MergeFindSet<int> mfsMergeFindSet; HashTable<int> htHashTable; cout<<WELCOME_STRING<<endl; cout<<"This is the Implement of Data Structs"<<endl; cout<<"Please Select the Item you are Interest in:"<<endl; cout<<"1. Stack(Implement of Array);"<<endl; cout<<"2. Stack(Implement of List);"<<endl; cout<<"3. Queue(Implement of Array);"<<endl; cout<<"4. Cycle Doubly Linked List"<<endl; cout<<"5. Binary Search Tree"<<endl; cout<<"6. Red Black Tree"<<endl; cout<<"7. Merge Find Set"<<endl; cout<<"8. Hash Table"<<endl; cout<<"98. Up Layer;"<<endl; cout<<"99. Quit."<<endl; cout<<STAR_STRING<<endl; cout<<endl; int nSelect; while(1) { cin>>nSelect; if(cin.fail()) { ClearScreen(); cout<<"Input Error! Please Select Again!"<<endl; cin.clear(); cin.sync(); ShowDataStructItems(); } ClearScreen(); switch(nSelect) { case 1: cout<<dsArrayStack.GetTitle().c_str()<<endl; dsArrayStack.Description(); dsArrayStack.Test(); ShowDataStructItems(); break; case 2: cout<<lsListStack.GetTitle().c_str()<<endl; lsListStack.Description(); lsListStack.Test(); ShowDataStructItems(); break; case 3: cout<<aqArrayQueue.GetTitle().c_str()<<endl; aqArrayQueue.Description(); aqArrayQueue.Test(); ShowDataStructItems(); break; case 4: cout<<dcllCycleDoublyLinkList.GetTitle().c_str()<<endl; dcllCycleDoublyLinkList.Description(); dcllCycleDoublyLinkList.Test(); ShowDataStructItems(); break; case 5: cout<<bstBinarySearchTree.GetTitle().c_str()<<endl; bstBinarySearchTree.Description(); bstBinarySearchTree.Test(); ShowDataStructItems(); break; case 6: cout<<rbtRedBlackTree.GetTitle().c_str()<<endl; rbtRedBlackTree.Description(); rbtRedBlackTree.Test(); ShowDataStructItems(); break; case 7: cout<<mfsMergeFindSet.GetTitle().c_str()<<endl; mfsMergeFindSet.Description(); mfsMergeFindSet.Test(); ShowDataStructItems(); break; case 8: cout<<htHashTable.GetTitle().c_str()<<endl; htHashTable.Description(); htHashTable.Test(); ShowDataStructItems(); break; case 98: goto ShowWelcome; break; case 99: exit(0); break; default: ClearScreen(); cout<<"Select Error! Please Select Again!"<<endl; cin.clear(); cin.sync(); ShowDataStructItems(); break; } } ShowWelcome: Show(); return; }
void RemoveTest2() { cout << "--------------------------RemoveTest2---------------------------" << endl; RedBlackTree<int> tree; tree.Insert(20); tree.Insert(3); tree.Insert(7); tree.Insert(5); tree.Insert(6); tree.Insert(10); tree.Insert(21); tree.Insert(23); cout << endl << "New Tree..." << endl; cout << "Verify Red Black Tree..." << endl; tree.verify(tree); cout << "Remove Root..." << endl; tree.Remove(7); cout << "Verify Red Black Tree..." << endl; tree.verify(tree); cout << "New Tree2..." << endl; RedBlackTree<int> tree2; tree2.Insert(5); tree2.Insert(6); tree2.Insert(3); tree2.Insert(1); tree2.Insert(8); tree2.Insert(23); tree2.Insert(11); tree2.Insert(24); tree2.Insert(10); tree2.Insert(4); tree2.Insert(2); tree2.Insert(25); cout << endl << "Verify Red Black Tree2..." << endl; tree2.verify(tree2); cout << "Remove 5 Elements (6 24 11 3 8)..." << endl; tree2.Remove(6); cout << endl << "Verify Red Black Tree2..." << endl; tree2.verify(tree2); tree2.Remove(24); cout << endl << "Verify Red Black Tree2..." << endl; tree2.verify(tree2); tree2.Remove(11); cout << endl << "Verify Red Black Tree2..." << endl; tree2.verify(tree2); tree2.Remove(3); cout << endl << "Verify Red Black Tree2..." << endl; tree2.verify(tree2); tree2.Remove(8); cout << endl << "Verify Red Black Tree2..." << endl; tree2.verify(tree2); cout << "Create Tree3..." << endl; RedBlackTree<int> tree3; tree3.Insert(5); tree3.Insert(6); tree3.Insert(3); tree3.Insert(1); tree3.Insert(8); tree3.Insert(23); tree3.Insert(11); tree3.Insert(24); tree3.Insert(10); tree3.Insert(4); tree3.Insert(2); tree3.Insert(25); tree3.Remove(6); tree3.Remove(24); tree3.Remove(11); tree3.Remove(3); tree3.Insert(6); cout << "Verify Red Black Tree3..." << endl; tree3.verify(tree3); cout << "Remove Root..." << endl; tree3.Remove(8); cout << "Verify Red Black Tree3..." << endl; tree3.verify(tree3); cout << endl; }
void TestRedBlackTree() { int *ptr; int *val, *ret; bool IsInsert; RedBlackTree<int, int>* RBTree; SimpleCompareNodesAlgorithm<int, int> CompareAlgorithm; /********************** * Example ***********************/ RBTree = new RedBlackTree<int, int>(&CompareAlgorithm); //initialize randomizer srand( (unsigned)time( NULL ) ); for (int i = 0; i < 12; i++) { ptr = new int; *ptr = i;//rand()&0xff; val = RBTree->Search(ptr, &IsInsert); if(val != NULL) { cout << "ERROR" << endl; } } //display all values for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBNEXT, val)) cout << *val << endl; //delete tree for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBFIRST, val)) { ret = RBTree->Delete(val); delete (int*) ret; } delete RBTree; /********************** * Example 1 ***********************/ RBTree = new RedBlackTree<int, int>(&CompareAlgorithm); for (int j = 200; j > 0; j--) { ptr = new int; *ptr = j; val = RBTree->Search(ptr, &IsInsert); if(val != NULL) { cout << "ERROR" << endl; } } //display tree for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBNEXT, val)) cout << *val << endl; //delete tree for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBFIRST, val)) { ret = RBTree->Delete(val); delete (int*) ret; } delete RBTree; /********************** * Example 2 ***********************/ RBTree = new RedBlackTree<int, int>(&CompareAlgorithm); for (int k = 400; k > 0; k--) { ptr = new int; *ptr = k; val = RBTree->Search(ptr, &IsInsert); if(val != NULL) { cout << "ERROR" << endl; } } /* printf("Minimum branch length: %d\n", minleaf); printf("Maximum branch length: %d\n", maxleaf); */ for (int k1 = 400; k1 > 0; k1--) { val = RBTree->Delete(&k1); if(val == NULL) { cout << "ERROR" << endl; } delete (int*) val; } delete RBTree; /********************** * Test Find ***********************/ RBTree = new RedBlackTree<int, int>(&CompareAlgorithm); for (int m = 200; m > 0; m--) { ptr = new int; *ptr = m; val = RBTree->Find(ptr); if(val != NULL) { cout << "ERROR" << endl; } //ptr shouldn't be added to the tree, so just delete it delete ptr; } //display tree (shouldn't be one) for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBFIRST, val)) { cout << *val << endl; ret = RBTree->Delete(val); delete (int*) ret; } delete RBTree; }
void HeightTest() { cout << "--------------------------Height Test---------------------------" << endl; RedBlackTree<int> tree; cout<< "Creat Tree..." << endl; cout << "Height: " << tree.Height() << endl; cout << "Insert 1" << endl; tree.Insert(1); cout << "Height: " << tree.Height() << endl <<endl ; cout << "Insert 5 Item" << endl; tree.Insert(20); tree.Insert(3); tree.Insert(7); tree.Insert(5); tree.Insert(6); tree.Insert(10); tree.Insert(21); tree.Insert(23); cout << "Height: " << tree.Height() << endl; cout << "Verify Red Black Tree..." << endl; tree.verify(tree); cout << "Remove 1" << endl; tree.Remove(1); cout << "Height: " << tree.Height() << endl <<endl ; cout << "Remove All..." << endl; tree.RemoveAll(); cout << "Height: " << tree.Height() << endl; cout << "Verify Red Black Tree..." << endl; tree.verify(tree); cout << endl; }
int main(int argc, const char * argv[]) { /***************************** Test 1: increasing input ***************************/ RedBlackTree<int> mytree; for(int k = 0; k < 100000; k++){ mytree.insert(k, k); } //checks if(mytree.testBlackHeight()){ std::cout << "Passed 1st black height test\n"; } else{ std::cout << "Failed 1st black height test\n"; } int height = mytree.getHeight(); std::cout << "Height: " << height << std::endl; //The claim: Every Red Black Tree with n nodes has height <= 2 * Log_2(n+1) if(mytree.heightVSsizeTest()){ std::cout << "passed 1st height vs size test\n"; } else{ std::cout << "Failed 1st height vs size test\n"; } /***************************** Test 2: decreasing input ***************************/ RedBlackTree<int> mytree2; for(int k = 100000; k > 0; k--){ mytree2.insert(k, k); } //checks if(mytree2.testBlackHeight()){ std::cout << "Passed 2nd black height test\n"; } else{ std::cout << "Failed 2nd black height test\n"; } int height2 = mytree2.getHeight(); std::cout << "Height: " << height2 << std::endl; //The claim: Every Red Black Tree with n nodes has height <= 2 * Log_2(n+1) if(mytree2.heightVSsizeTest()){ std::cout << "passed 2nd height vs size test\n"; } else{ std::cout << "Failed 2nd height vs size test\n"; } /***************************** Test 3: 100 random trees with 100,000 inputs ***************************/ srand(time(0)); bool blackTest = true; bool hVsTest = true; double avgheight = 0; for(int j = 0; j < 100; j++){ RedBlackTree<int> mytree4; for(int k = 0; k < 100000; k++){ int x = rand()%100000; mytree4.insert(x, k); } //checks if(!mytree4.testBlackHeight()){ blackTest = false; } if(!mytree4.heightVSsizeTest()){ hVsTest = false; } avgheight = avgheight + mytree4.getHeight(); } avgheight = avgheight/100; std::cout << "Average height: " << avgheight << std::endl; if(mytree2.testBlackHeight()){ std::cout << "Passed multi-black height test\n"; } else{ std::cout << "Failed multi-black height test\n"; } if(mytree2.heightVSsizeTest()){ std::cout << "passed multi-height vs size test\n"; } else{ std::cout << "Failed multi-height vs size test\n"; } /***************************** Test 4: testing ordering ***************************/ RedBlackTree<int> mytree6; for(int k = 0; k < 100; k++){ int x = rand()%100; mytree6.insert(x, x); } std::cout << "Inorder traversal:\n"; mytree6.inorder(); /************************** More random testing *************************/ RedBlackTree<int> mytree8; //test case that was previously breaking the tree mytree8.insert(36, 36); mytree8.insert(58, 58); mytree8.insert(40, 40); mytree8.insert(71, 71); mytree8.insert(56, 56); std::cout << std::endl; mytree8.inorder(); std::cout << std::endl; return 0; }
int main(int argc,char**argv) { RedBlackTree p; cout<<p.Find(4)<<endl; p.Insert(9); p.Insert(37); p.Insert(4); p.Insert(53); p.Insert(6); p.Insert(45); p.Insert(1); p.InOrderTraverse(); p.Delete(9); p.InOrderTraverse(); cout<<p.Find(4)<<endl; p.Delete(4); cout<<p.Find(4)<<endl; p.InOrderTraverse(); p.Insert(9); p.Insert(37); p.InOrderTraverse(); return 0; }
int main(){ RedBlackTree<int, char>* tree = new RedBlackTree<int,char>(); cout << "Constructed Tree" << endl; cout << "Insert 10, A" << endl; pair<int, char> p1(10,'a'); tree->insert(p1); tree->print2(); cout << endl; cout << "Insert Duplicate 10, X" << endl; pair<int, char> p123(10,'x'); tree->insert(p123); tree->print2(); cout << endl; cout << "Insert 20, B" << endl; pair<int, char> p2(20,'b'); tree->insert(p2); tree->print2(); cout << endl; cout << "Insert 30, C" << endl; pair<int, char> p3(30,'c'); tree->insert(p3); tree->print2(); cout << endl; cout << "Insert 15, D" << endl; pair<int, char> p4(15,'d'); tree->insert(p4); tree->print2(); cout << endl; cout << "Insert 25, E" << endl; pair<int, char> p5(25,'e'); tree->insert(p5); tree->print2(); cout << endl; cout << "Insert 12, F" << endl; pair<int, char> p6(12,'f'); tree->insert(p6); tree->print2(); cout << endl; cout << "Insert 5, G" << endl; pair<int, char> p7(5,'g'); tree->insert(p7); tree->print2(); cout << endl; cout << "Insert 3, H" << endl; pair<int, char> p8(3,'h'); tree->insert(p8); tree->print2(); cout << endl; cout << "Insert 8, I" << endl; pair<int, char> p9(8,'i'); tree->insert(p9); tree->print2(); cout << endl; cout << "Insert 27, j" << endl; pair<int, char> p10(27,'j'); tree->insert(p10); tree->print2(); cout << endl; cout << "Insert 40, k" << endl; pair<int, char> p11(40,'k'); tree->insert(p11); tree->print2(); cout << endl; cout << "Insert 50, z" << endl; pair<int, char> p14(50,'z'); tree->insert(p14); tree->print2(); cout << endl; cout << "Insert 45, l" << endl; pair<int, char> p12(45,'l'); tree->insert(p12); tree->print2(); cout << endl; cout << "Insert 9, m" << endl; pair<int, char> p13(9,'m'); tree->insert(p13); tree->print2(); cout << endl; cout << "Testing Iteration:" << endl; for(RedBlackTree<int,char>::iterator it = tree->begin(); it!=tree->end(); ++it){ cout << (*it).first << " "; } cout << endl; return 0; }
int TestRedBlackTree_CString() { RedBlackTree<const char *, const char *> *rbtree = new RedBlackTree<const char *, const char *>(); char *tmp; TEST_ASSERT(rbtree->size() == 0); /* If the tree is properly encapsulated, this won't cause an error on test #1. */ tmp = cc_strdup("first"); rbtree->insert(tmp, "one"); free(tmp); tmp = NULL; TEST_ASSERT(rbtree->size() == 1); rbtree->insert("second", "two"); rbtree->insert("third", "three"); rbtree->insert("fourth", "four"); TEST_ASSERT(rbtree->size() == 4); const char *res = NULL; TEST_ASSERT(rbtree->find("first", res)); const char *tmp1 = "one"; TEST_ASSERT(Compare(res, tmp1) == 0); TEST_ASSERT(!rbtree->exists("fifth")); TEST_ASSERT(rbtree->exists("second")); TEST_ASSERT(rbtree->find("second", res)); tmp1 = "two"; TEST_ASSERT(Compare(res, tmp1) == 0); TEST_ASSERT(!rbtree->erase("fifth")); TEST_ASSERT(rbtree->size() == 4); TEST_ASSERT(rbtree->exists("first")); TEST_ASSERT(rbtree->erase("first")); TEST_ASSERT(rbtree->size() == 3); TEST_ASSERT(rbtree->exists("second")); TEST_ASSERT(rbtree->erase("second")); TEST_ASSERT(rbtree->size() == 2); TEST_ASSERT(rbtree->exists("third")); TEST_ASSERT(rbtree->erase("third")); TEST_ASSERT(rbtree->size() == 1); TEST_ASSERT(rbtree->exists("fourth")); TEST_ASSERT(rbtree->erase("fourth")); TEST_ASSERT(rbtree->size() == 0); delete rbtree; return 0; }
int main() { RedBlackTree<int>* tree = new RedBlackTree<int>(); tree->Insert(41); cout << "root " << tree->GetRoot()->data << endl; tree->Insert(32); cout << "root " << tree->GetRoot()->data << endl; tree->Insert(71); cout << "root " << tree->GetRoot()->data << endl; tree->Insert(65); cout << "root " << tree->GetRoot()->data << endl; tree->Insert(51); cout << "root " << tree->GetRoot()->data << endl; tree->Insert(87); cout << "root " << tree->GetRoot()->data << endl; tree->Insert(82); cout << "root " << tree->GetRoot()->data << endl; tree->Insert(93); cout << "root " << tree->GetRoot()->data << endl; /* cout << "root " << tree->GetRoot()->data << endl; tree->Remove(51); cout << "root " << tree->GetRoot()->data << endl; tree->Remove(32); cout << "root " << tree->GetRoot()->data << endl; */ RedBlackTree<int>* copytree = new RedBlackTree<int>(*tree); }
int main() { RedBlackTree<int, int> tree; /*for(int i = 0; i<10; i++) { std::pair<int, int> item; item.first = i; item.second = i; tree.insert(item); }*/ std::pair<int, int> item; item.first = 0; item.second = 0; tree.insert(item); tree.print(); item.first = 1; item.second = 1; tree.insert(item); tree.print(); item.first = 2; item.second = 2; tree.insert(item); tree.print(); item.first = 3; item.second = 2; tree.insert(item); tree.print(); item.first = 4; item.second = 2; tree.insert(item); tree.print(); item.first = 6; item.second = 2; tree.insert(item); tree.print(); item.first = 1; item.second = 2; tree.insert(item); tree.print(); for(BinarySearchTree<int, int>::iterator it = tree.begin(); it != tree.end(); ++it) { std::cout << it->first << " "; } return 0; }
int main(int argc, char* argv[]) { string tree_choice = ""; string str_choice = ""; int choice = -1; bool quit = false; string commonName = ""; string sciName = ""; string phenophase = ""; string str_elevation = ""; int elevation = 0; string str_siteID = ""; int siteID = 0; string date = ""; while (!quit) { cout << "Please enter which type of tree you would like to build: (u)nbalanced or (r)ed-black, or (q)uit: " << endl; getline(cin, tree_choice); if (tree_choice == "u") { bool bstQuit = false; cout << "You have chosen: unbalanced BST" << endl; BinarySearchTree *bst = new BinarySearchTree(); readIn(bst, argv[1]); while (!bstQuit) { printMenu(); getline(cin, str_choice); choice = stoi(str_choice); switch(choice) { case 1: bst->printTree(); break; case 2: cout << "enter common name: " << endl; getline(cin, commonName); transform(commonName.begin(), commonName.end(), commonName.begin(), ::tolower); // method from http://blog.fourthwoods.com/2013/12/10/convert-c-string-to-lower-case-or-upper-case/ cout << "enter scientific name: " << endl; getline(cin, sciName); cout << "enter phenophase: " << endl; getline(cin, phenophase); cout << "enter elevation in meters: " << endl; getline(cin, str_elevation); elevation = atoi(str_elevation.c_str()); cout << "enter site ID: " << endl; getline(cin, str_siteID); siteID = atoi(str_siteID.c_str()); cout << "enter date as MMDDYYYY: " << endl; getline(cin, date); bst->addDataNode(commonName, sciName, phenophase, elevation, siteID, date, 1); break; case 3: cout << "enter common name: " << endl; getline(cin, commonName); bst->findDataNode(commonName); break; case 4: cout << "enter common name: " << endl; getline(cin, commonName); bst->deleteDataNode(commonName); break; case 5: bstQuit = true; break; default: cout << "invalid input, please try again" << endl; break; } } } else if (tree_choice == "r") { bool rbQuit = false; cout << "You have chosen: red-black BST" << endl; RedBlackTree *rb = new RedBlackTree(); readIn(rb, argv[1]); while (!rbQuit) { printMenu(); getline(cin, str_choice); choice = stoi(str_choice); switch(choice) { case 1: rb->printTree(); break; case 2: break; case 3: break; case 4: break; case 5: rbQuit = true; break; default: cout << "invalid input, please try again" << endl; break; } } //} else if (tree_choice == "a") { } else if (tree_choice == "q") { cout << "Goodbye!" << endl; quit = true; } else { cout << "invalid input, please try again" << endl; } } return 0; }
void ConstructorTest() { cout << "--------------------------Constructor Test---------------------------" << endl; RedBlackTree<int> tree; cout << "Create Tree..." << endl; tree.Insert(6); tree.Insert(7); tree.Insert(8); tree.Insert(9); tree.Insert(11); tree.Insert(2); tree.Insert(1); tree.Insert(3); tree.Insert(4); tree.Insert(5); cout << endl << "Verify Red Black Tree..." << endl; tree.verify(tree); RedBlackTree<int> treeC(tree); cout << "Perform Copy Tree..." << endl; cout << "Size of Copy: " << treeC.Size() << endl; //Verify order of Tree cout << endl << "Verify Copy Red Black Tree..." << endl; treeC.verify(treeC); cout << "Copy Blank Tree..." << endl; RedBlackTree<int> treeA; cout << "Size of Empty tree1: " << treeA.Size() << endl; RedBlackTree<int> treeC2(treeA); cout << "Size of Copy of tree1: " << treeC2.Size() << endl; //BST NOT RED BLACK (FOR COMPARE)!!! RedBlackTree<int> BST; BST.BSTInsert2(6);BST.BSTInsert2(7); BST.BSTInsert2(8);BST.BSTInsert2(9); BST.BSTInsert2(11);BST.BSTInsert2(2); BST.BSTInsert2(1);BST.BSTInsert2(3); BST.BSTInsert2(4);BST.BSTInsert2(5); cout << endl << "BST Verify..." << endl << "**NOT RED BLACK TREE**" << endl << endl; BST.verify(BST); cout << endl; }