From b14156589c87e96881da41221f0cdd7f33dc2139 Mon Sep 17 00:00:00 2001 From: Piotr Kozak Date: Sun, 4 May 2025 21:54:22 +0200 Subject: [PATCH] Wrote BST tests and list structure --- Lab2/.gitignore | 2 ++ Lab2/benchmark.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++ Lab2/bst/bst.cpp | 33 +++++++++++++++++++++ Lab2/bst/bst.h | 9 ++++++ Lab2/list/list.cpp | 29 +++++++++++++++++++ Lab2/list/list.h | 6 ++++ Lab2/plot.py | 0 7 files changed, 151 insertions(+) create mode 100644 Lab2/.gitignore create mode 100644 Lab2/benchmark.cpp create mode 100644 Lab2/bst/bst.cpp create mode 100644 Lab2/bst/bst.h create mode 100644 Lab2/list/list.cpp create mode 100644 Lab2/list/list.h create mode 100644 Lab2/plot.py diff --git a/Lab2/.gitignore b/Lab2/.gitignore new file mode 100644 index 0000000..7bce85c --- /dev/null +++ b/Lab2/.gitignore @@ -0,0 +1,2 @@ +/dist +benchmarks diff --git a/Lab2/benchmark.cpp b/Lab2/benchmark.cpp new file mode 100644 index 0000000..cbff6d2 --- /dev/null +++ b/Lab2/benchmark.cpp @@ -0,0 +1,72 @@ +#include "bst/bst.h" +#include "list/list.h" + +#include +#include +#include +#include + +void measureBST(std::set *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 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 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; +} diff --git a/Lab2/bst/bst.cpp b/Lab2/bst/bst.cpp new file mode 100644 index 0000000..e78567f --- /dev/null +++ b/Lab2/bst/bst.cpp @@ -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; + } +} diff --git a/Lab2/bst/bst.h b/Lab2/bst/bst.h new file mode 100644 index 0000000..8a29641 --- /dev/null +++ b/Lab2/bst/bst.h @@ -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); diff --git a/Lab2/list/list.cpp b/Lab2/list/list.cpp new file mode 100644 index 0000000..225b9b2 --- /dev/null +++ b/Lab2/list/list.cpp @@ -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; +} diff --git a/Lab2/list/list.h b/Lab2/list/list.h new file mode 100644 index 0000000..4db1a3a --- /dev/null +++ b/Lab2/list/list.h @@ -0,0 +1,6 @@ +struct List { + int data; + List *next; +}; + +List *insert(List *head, int data); diff --git a/Lab2/plot.py b/Lab2/plot.py new file mode 100644 index 0000000..e69de29