From e5b5eb3e72b31fc715ed6858acbf9ba8ba997659 Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Sun, 12 May 2019 16:30:24 +0200 Subject: [PATCH] Binary tree fixed --- test/binarytree.cpp | 169 +++++++++++++++++++++++++++++++++----------- 1 file changed, 128 insertions(+), 41 deletions(-) diff --git a/test/binarytree.cpp b/test/binarytree.cpp index 3d90b46..5c8e5d4 100644 --- a/test/binarytree.cpp +++ b/test/binarytree.cpp @@ -1,42 +1,23 @@ #include "../src/tree/binarytree.hpp" #include "testutil.hpp" #include +#include -void FillWithRandomNumbers(BinaryTree::Tree & tree, - std::vector & control, - unsigned const count = 4096u) +void FillWithUniqueRandomNumbers(BinaryTree::Tree & tree, + std::unordered_set & control, + unsigned const count = 10000u) { for(unsigned i = 0u; i < count; ++i) { unsigned const value = Util::GetRandomNumber(); - control.push_back(value); - tree.Insert(value); + control.insert(value); + tree.InsertNoDuplicates(value); } } bool TestInsert() { - std::vector control; - BinaryTree::Tree tree; - - FillWithRandomNumbers(tree, control); - - bool good = true; - for(auto const & num : control) - { - if(!tree.Contains(num)) - { - std::printf("\tValue %u inserted but cannot be found!\n", num); - good = false; - } - } - - return good; -} - -bool TestInsertNoDuplicates() -{ - unsigned const testSize = 8096u; + unsigned const testSize = 4096; std::vector control; BinaryTree::Tree tree; @@ -45,50 +26,155 @@ bool TestInsertNoDuplicates() { unsigned const value = Util::GetRandomNumber(); control.push_back(value); - tree.InsertNoDuplicates(value); + tree.Insert(value); } - bool good = true; for(auto const & num : control) { if(!tree.Contains(num)) { - std::printf("\tValue %u inserted but cannot be found!\n", num); - good = false; + //std::printf("\tValue %u inserted but cannot be found!\n", num); + return false; } } - return good; + return true; +} + +bool TestInsertNoDuplicates() +{ + std::unordered_set control; + BinaryTree::Tree tree; + + FillWithUniqueRandomNumbers(tree, control); + + for(auto const & num : control) + { + if(!tree.Contains(num)) + { + //std::printf("\tValue %u inserted but cannot be found!\n", num); + return false; + } + } + + return true; } bool TestDeletion() { - std::vector control; + std::unordered_set control; BinaryTree::Tree tree; - FillWithRandomNumbers(tree, control); + FillWithUniqueRandomNumbers(tree, control); + std::vector deletedValues; unsigned const toDeleteCount = control.size() / 4; for(unsigned i = 0u; i < toDeleteCount; ++i) { - auto const toDeleteIndex = Util::GetRandomNumber() % control.size(); - auto const toDeleteValue = control[toDeleteIndex]; + auto const valueTodelete = Util::GetRandomNumber(); - control.erase(control.begin() + toDeleteIndex); - tree.Delete(toDeleteValue); + auto controlElement = control.find(valueTodelete); + if(controlElement != control.end()) + { + control.erase(controlElement); + tree.Delete(valueTodelete); + deletedValues.push_back(valueTodelete); + } } - bool good = true; for(auto const & num : control) { if(!tree.Contains(num)) { - std::printf("Value %u inserted but cannot be found!\n", num); - good = false; + std::puts("[INFO] Tree does not contain a value that it should."); + //std::printf("Value %u inserted but cannot be found!\n", num); + return false; } } - return good; + for(auto const & num : deletedValues) + { + if(tree.Contains(num)) + { + std::puts("[INFO] Tree contains deleted value that it should not."); + return false; + } + } + + return true; +} + +bool DebugDeletionCase( + std::vector const toInsert, + std::vector const toDelete, + std::vector const result) +{ + BinaryTree::Tree tree; + for(auto n : toInsert) + { + tree.Insert(n); + } + + for(auto n : toDelete) + { + tree.Delete(n); + } + + bool ok = true; + for(auto n : result) + { + if(!tree.Contains(n)) + { + std::printf("\tError, tree does not contain value %u.\n", n); + ok = false; + } + } + + for(auto n : toDelete) + { + if(tree.Contains(n)) + { + std::printf("\tError, tree contains deleted value %u.\n", n); + ok = false; + } + } + + return ok; +} + +bool DebugDeletion() +{ + std::puts("Testing deleting root with single child right."); + DebugDeletionCase( + std::vector { 10, 12, 11, 13}, + std::vector { 10 }, + std::vector { 12, 11, 13 }); + + std::puts("Testing deleting root with single child left."); + DebugDeletionCase( + std::vector { 15, 12, 11, 13}, + std::vector { 15 }, + std::vector { 12, 11, 13 }); + + std::puts("Testing deleting root with 2 children but no child right left."); + DebugDeletionCase( + std::vector { 15, 10, 20, 22, 25, 18, 5}, + std::vector { 20 }, + std::vector { 15, 10, 22, 25, 18, 5 }); + + std::puts("Testing deleting root with 2 children."); + DebugDeletionCase( + std::vector { 50, 40, 60, 75, 55, 45, 42, 58 }, + std::vector { 50 }, + std::vector { 40, 60, 75, 55, 45, 42, 58 }); + + return true; +} + +void Debug() +{ + std::puts("\n*** DEBUG ***"); + Test::Execute(DebugDeletion, "Deletion debug test"); } int main() @@ -96,6 +182,7 @@ int main() Test::Execute(TestInsert, "Insertion and find test"); Test::Execute(TestInsertNoDuplicates, "Insertion without duplicates test"); Test::Execute(TestDeletion, "Insertion and deletion test"); + Debug(); return 0; } \ No newline at end of file