Wrote BST tests and list structure
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
/dist
|
||||||
|
benchmarks
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#include "bst.h"
|
||||||
|
|
||||||
|
Tree *insert(Tree *root, int value) {
|
||||||
|
if (root == nullptr) {
|
||||||
|
root = new Tree{value, nullptr, nullptr};
|
||||||
|
} else if (value < root->info) {
|
||||||
|
root->left = insert(root->left, value);
|
||||||
|
} else if (value > root->info) {
|
||||||
|
root->right = insert(root->right, value);
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tree *search(Tree *root, int value) {
|
||||||
|
Tree *ptr = root;
|
||||||
|
while (ptr != nullptr) {
|
||||||
|
if (value > ptr->info)
|
||||||
|
ptr = ptr->right;
|
||||||
|
else if (value < ptr->info)
|
||||||
|
ptr = ptr->left;
|
||||||
|
else
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeTree(Tree *root) {
|
||||||
|
if (root != nullptr) {
|
||||||
|
freeTree(root->left);
|
||||||
|
freeTree(root->right);
|
||||||
|
delete root;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
struct Tree {
|
||||||
|
int info;
|
||||||
|
Tree *left;
|
||||||
|
Tree *right;
|
||||||
|
};
|
||||||
|
|
||||||
|
Tree *insert(Tree *root, int value);
|
||||||
|
Tree *search(Tree *root, int value);
|
||||||
|
void freeTree(Tree *root);
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include "list.h"
|
||||||
|
|
||||||
|
List *insert(List *head, int data) {
|
||||||
|
if (head == nullptr) {
|
||||||
|
List *node = new List();
|
||||||
|
node->data = data;
|
||||||
|
node->next = nullptr;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (head->data > data) {
|
||||||
|
List *newHead = new List();
|
||||||
|
newHead->data = data;
|
||||||
|
newHead->next = head;
|
||||||
|
head = newHead;
|
||||||
|
} else {
|
||||||
|
List *tmp = head;
|
||||||
|
while (tmp->next != nullptr && tmp->data < data) {
|
||||||
|
tmp = head->next;
|
||||||
|
}
|
||||||
|
List *tail = new List();
|
||||||
|
tail->data = data;
|
||||||
|
// Set the pointer to the next, we don't know if its at the end or not
|
||||||
|
tail->next = tmp->next;
|
||||||
|
tmp->next = tail;
|
||||||
|
}
|
||||||
|
|
||||||
|
return head;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
struct List {
|
||||||
|
int data;
|
||||||
|
List *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
List *insert(List *head, int data);
|
||||||
Reference in New Issue
Block a user