Skip to content

Commit

Permalink
Add tests for Red-Black BST to validate tree balance
Browse files Browse the repository at this point in the history
  • Loading branch information
landerrosette committed Dec 28, 2024
1 parent 01e1e50 commit f8b4ffe
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ foreach (ST "SequentialSearchST" "BinarySearchST" "BST" "RedBlackBST" "SeparateC
target_sources(Test${ST} PRIVATE TestOrderedST.cpp)

target_compile_definitions(Test${ST} PRIVATE ORDERED)
elseif (ST STREQUAL "TrieST" OR ST STREQUAL "TST")
endif ()

if (ST STREQUAL "TrieST" OR ST STREQUAL "TST")
target_sources(Test${ST} PRIVATE TestStringST.cpp)

target_compile_definitions(Test${ST} PRIVATE STRING)
endif ()

if (ST STREQUAL "RedBlackBST")
target_sources(Test${ST} PRIVATE TestBalancedTree.cpp)

target_compile_definitions(Test${ST} PRIVATE BALANCED_TREE)
endif ()

unset(ST_INIT_ARGS)
endforeach ()

Expand Down
29 changes: 29 additions & 0 deletions TestBalancedTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "TestBalancedTree.h"
#include <random>

void TestST::testRedBlackBST(RedBlackBST<int, int> &st, int n, std::ostream &os) {
// Insert N elements in order.
os << "Inserting " << n << " elements in order" << std::endl;
os << "--------------------------------" << std::endl;
for (int i = 0; i < n; ++i) {
st.put(i, i);
os << "i = " << i << ", height = " << st.height() << ", size = " << st.size() << std::endl;
}
os << std::endl;

// Delete keys in random order.
os << "Deleting keys in random order" << std::endl;
os << "--------------------------------" << std::endl;
std::default_random_engine e(std::random_device{}());
std::uniform_int_distribution u(0, n - 1);
while (st.size() > 0) {
int i = u(e);
if (st.contains(i)) {
st.remove(i);
os << "i = " << i << ", height = " << st.height() << ", size = " << st.size() << std::endl;
}
}
os << std::endl;

os << "size = " << st.size() << std::endl;
}
12 changes: 12 additions & 0 deletions TestBalancedTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef TESTBALANCEDTREE_H
#define TESTBALANCEDTREE_H


#include <iostream>
#include "RedBlackBST.h"

namespace TestST {
void testRedBlackBST(RedBlackBST<int, int> &st, int n, std::ostream &os);
}

#endif //TESTBALANCEDTREE_H
11 changes: 11 additions & 0 deletions main_TestST.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#ifdef STRING
#include "TestStringST.h"
#endif
#ifdef BALANCED_TREE
#include "TestBalancedTree.h"
#include <string>
#endif
#include "@[email protected]"

int main(int argc, char *argv[]) {
Expand All @@ -29,5 +33,12 @@ int main(int argc, char *argv[]) {
TestST::removeSome(st, std::cout);
std::cout << std::endl;
TestST::removeAll(st, std::cout);
#ifdef BALANCED_TREE
if (argc > 1) {
std::cout << std::endl;
@ST@<int, int> st2{@ST_INIT_ARGS@};
TestST::testRedBlackBST(st2, std::stoi(argv[1]), std::cout);
}
#endif
return 0;
}

0 comments on commit f8b4ffe

Please sign in to comment.