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; }
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; }
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; }
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; }
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); }
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); }
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 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; }
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; }
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 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; }
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; }