-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Red-Black BST to validate tree balance
- Loading branch information
1 parent
01e1e50
commit f8b4ffe
Showing
4 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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[]) { | ||
|
@@ -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; | ||
} |