Wrote BST tests and list structure

This commit is contained in:
2025-05-04 21:54:22 +02:00
parent 1c8ab00c13
commit b14156589c
7 changed files with 151 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
#include "bst/bst.h"
#include "list/list.h"
#include <iostream>
#include <set>
#include <random>
#include <chrono>
void measureBST(std::set<int> *sequence){
int buildTime = 0, searchTime = 0, deleteTime = 0;
for (int i = 0; i < 10; i++) {
std::cout << "- Running test " << i + 1 << "/10\r";
fflush(stdout);
// Measure build time
auto start = std::chrono::high_resolution_clock::now();
Tree *root = nullptr;
for (int value : *sequence) {
root = insert(root, value);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
buildTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
// Now we run the search
for (int value : *sequence) {
search(root, value);
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end - start;
searchTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
// Measure deletion time for tree
start = std::chrono::high_resolution_clock::now();
freeTree(root);
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end - start;
deleteTime += elapsed_seconds.count() * 1000; // Convert to milliseconds
}
buildTime /= 10;
searchTime /= 10;
deleteTime /= 10;
std::cout << "- Tree built in " << buildTime << "ms" << std::endl;
std::cout << "- Tree searched in " << searchTime << "ms" << std::endl;
std::cout << "- Tree deleted in " << deleteTime << "ms" << std::endl;
}
int main() {
// Init the random number generator
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 1000000);
for (int n = 1; n < 16; n++) {
std::set<int> sequence;
while (sequence.size() < n * 1000) {
sequence.insert(dis(gen));
}
// Display this like a cascade
std::cout << "Running tests for " << n * 1000 << " elements..."
<< std::endl;
measureBST(&sequence);
}
return 0;
}