Broke the binary tree, please fix :-)

This commit is contained in:
2019-05-12 12:01:09 +02:00
parent 8a69c5065e
commit 19bc9aed6b
3 changed files with 84 additions and 89 deletions

View File

@@ -1,22 +1,22 @@
#include "../tree/binarytree.hpp"
#include "testutil.hpp"
#include <unordered_set>
#include <vector>
void FillWithRandomNumbers(BinaryTree::Tree<unsigned> & tree,
std::unordered_set<unsigned> & control,
std::vector<unsigned> & control,
unsigned const count = 512u)
{
for(unsigned i = 0u; i < count; ++i)
{
unsigned const value = Util::GetRandomNumber();
control.insert(value);
control.push_back(value);
tree.Insert(value);
}
}
bool TestInsertContains()
{
std::unordered_set<unsigned> control;
std::vector<unsigned> control;
BinaryTree::Tree<unsigned> tree;
FillWithRandomNumbers(tree, control);
@@ -26,7 +26,7 @@ bool TestInsertContains()
{
if(!tree.Contains(num))
{
std::printf("Value %u inserted but cannot be found!\n", num);
std::printf("\tValue %u inserted but cannot be found!\n", num);
good = false;
}
}
@@ -36,23 +36,19 @@ bool TestInsertContains()
bool TestDeletion()
{
std::unordered_set<unsigned> control;
std::vector<unsigned> control;
BinaryTree::Tree<unsigned> tree;
FillWithRandomNumbers(tree, control);
unsigned i = 0u;
unsigned const toDeleteCount = control.size() / 4;
while(i < toDeleteCount)
for(unsigned i = 0u; i < toDeleteCount; ++i)
{
auto const toDelete = Util::GetRandomNumber();
auto iter = control.find(toDelete);
if(iter != control.end())
{
control.erase(iter);
tree.Delete(i);
++i;
}
auto const toDeleteIndex = Util::GetRandomNumber() % control.size();
auto const toDeleteValue = control[toDeleteIndex];
control.erase(control.begin() + toDeleteIndex);
tree.Delete(toDeleteValue);
}
bool good = true;