Binary tree fixed
This commit is contained in:
@@ -1,42 +1,23 @@
|
|||||||
#include "../src/tree/binarytree.hpp"
|
#include "../src/tree/binarytree.hpp"
|
||||||
#include "testutil.hpp"
|
#include "testutil.hpp"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
void FillWithRandomNumbers(BinaryTree::Tree<unsigned> & tree,
|
void FillWithUniqueRandomNumbers(BinaryTree::Tree<unsigned> & tree,
|
||||||
std::vector<unsigned> & control,
|
std::unordered_set<unsigned> & control,
|
||||||
unsigned const count = 4096u)
|
unsigned const count = 10000u)
|
||||||
{
|
{
|
||||||
for(unsigned i = 0u; i < count; ++i)
|
for(unsigned i = 0u; i < count; ++i)
|
||||||
{
|
{
|
||||||
unsigned const value = Util::GetRandomNumber();
|
unsigned const value = Util::GetRandomNumber();
|
||||||
control.push_back(value);
|
control.insert(value);
|
||||||
tree.Insert(value);
|
tree.InsertNoDuplicates(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TestInsert()
|
bool TestInsert()
|
||||||
{
|
{
|
||||||
std::vector<unsigned> control;
|
unsigned const testSize = 4096;
|
||||||
BinaryTree::Tree<unsigned> 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;
|
|
||||||
|
|
||||||
std::vector<unsigned> control;
|
std::vector<unsigned> control;
|
||||||
BinaryTree::Tree<unsigned> tree;
|
BinaryTree::Tree<unsigned> tree;
|
||||||
@@ -45,50 +26,155 @@ bool TestInsertNoDuplicates()
|
|||||||
{
|
{
|
||||||
unsigned const value = Util::GetRandomNumber();
|
unsigned const value = Util::GetRandomNumber();
|
||||||
control.push_back(value);
|
control.push_back(value);
|
||||||
tree.InsertNoDuplicates(value);
|
tree.Insert(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool good = true;
|
|
||||||
for(auto const & num : control)
|
for(auto const & num : control)
|
||||||
{
|
{
|
||||||
if(!tree.Contains(num))
|
if(!tree.Contains(num))
|
||||||
{
|
{
|
||||||
std::printf("\tValue %u inserted but cannot be found!\n", num);
|
//std::printf("\tValue %u inserted but cannot be found!\n", num);
|
||||||
good = false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return good;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TestInsertNoDuplicates()
|
||||||
|
{
|
||||||
|
std::unordered_set<unsigned> control;
|
||||||
|
BinaryTree::Tree<unsigned> 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()
|
bool TestDeletion()
|
||||||
{
|
{
|
||||||
std::vector<unsigned> control;
|
std::unordered_set<unsigned> control;
|
||||||
BinaryTree::Tree<unsigned> tree;
|
BinaryTree::Tree<unsigned> tree;
|
||||||
|
|
||||||
FillWithRandomNumbers(tree, control);
|
FillWithUniqueRandomNumbers(tree, control);
|
||||||
|
|
||||||
|
std::vector<unsigned> deletedValues;
|
||||||
unsigned const toDeleteCount = control.size() / 4;
|
unsigned const toDeleteCount = control.size() / 4;
|
||||||
for(unsigned i = 0u; i < toDeleteCount; ++i)
|
for(unsigned i = 0u; i < toDeleteCount; ++i)
|
||||||
{
|
{
|
||||||
auto const toDeleteIndex = Util::GetRandomNumber() % control.size();
|
auto const valueTodelete = Util::GetRandomNumber();
|
||||||
auto const toDeleteValue = control[toDeleteIndex];
|
|
||||||
|
|
||||||
control.erase(control.begin() + toDeleteIndex);
|
auto controlElement = control.find(valueTodelete);
|
||||||
tree.Delete(toDeleteValue);
|
if(controlElement != control.end())
|
||||||
|
{
|
||||||
|
control.erase(controlElement);
|
||||||
|
tree.Delete(valueTodelete);
|
||||||
|
deletedValues.push_back(valueTodelete);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool good = true;
|
|
||||||
for(auto const & num : control)
|
for(auto const & num : control)
|
||||||
{
|
{
|
||||||
if(!tree.Contains(num))
|
if(!tree.Contains(num))
|
||||||
{
|
{
|
||||||
std::printf("Value %u inserted but cannot be found!\n", num);
|
std::puts("[INFO] Tree does not contain a value that it should.");
|
||||||
good = false;
|
//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<unsigned> const toInsert,
|
||||||
|
std::vector<unsigned> const toDelete,
|
||||||
|
std::vector<unsigned> const result)
|
||||||
|
{
|
||||||
|
BinaryTree::Tree<unsigned> 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<unsigned> { 10, 12, 11, 13},
|
||||||
|
std::vector<unsigned> { 10 },
|
||||||
|
std::vector<unsigned> { 12, 11, 13 });
|
||||||
|
|
||||||
|
std::puts("Testing deleting root with single child left.");
|
||||||
|
DebugDeletionCase(
|
||||||
|
std::vector<unsigned> { 15, 12, 11, 13},
|
||||||
|
std::vector<unsigned> { 15 },
|
||||||
|
std::vector<unsigned> { 12, 11, 13 });
|
||||||
|
|
||||||
|
std::puts("Testing deleting root with 2 children but no child right left.");
|
||||||
|
DebugDeletionCase(
|
||||||
|
std::vector<unsigned> { 15, 10, 20, 22, 25, 18, 5},
|
||||||
|
std::vector<unsigned> { 20 },
|
||||||
|
std::vector<unsigned> { 15, 10, 22, 25, 18, 5 });
|
||||||
|
|
||||||
|
std::puts("Testing deleting root with 2 children.");
|
||||||
|
DebugDeletionCase(
|
||||||
|
std::vector<unsigned> { 50, 40, 60, 75, 55, 45, 42, 58 },
|
||||||
|
std::vector<unsigned> { 50 },
|
||||||
|
std::vector<unsigned> { 40, 60, 75, 55, 45, 42, 58 });
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Debug()
|
||||||
|
{
|
||||||
|
std::puts("\n*** DEBUG ***");
|
||||||
|
Test::Execute(DebugDeletion, "Deletion debug test");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@@ -96,6 +182,7 @@ int main()
|
|||||||
Test::Execute(TestInsert, "Insertion and find test");
|
Test::Execute(TestInsert, "Insertion and find test");
|
||||||
Test::Execute(TestInsertNoDuplicates, "Insertion without duplicates test");
|
Test::Execute(TestInsertNoDuplicates, "Insertion without duplicates test");
|
||||||
Test::Execute(TestDeletion, "Insertion and deletion test");
|
Test::Execute(TestDeletion, "Insertion and deletion test");
|
||||||
|
Debug();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user