From fa18b01220492323245d89a3fe107b15ec6e8439 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 10 Jan 2023 10:45:11 -0500 Subject: [PATCH] Working on binary trees --- README.md | 2 +- include/blt/profiling/profiler.h | 11 +-- include/blt/std/binary_tree.h | 146 +++++++++++++++++++++++++++++++ include/blt/std/math.h | 16 ++++ include/blt/std/queue.h | 3 +- include/blt/std/random.h | 2 +- include/blt/std/string.h | 2 +- include/blt/std/system.h | 2 +- include/blt/std/time.h | 2 +- src/blt/profiling/profiler.cpp | 2 +- src/blt/std/queues.cpp | 2 +- src/blt/std/system.cpp | 1 + 12 files changed, 177 insertions(+), 14 deletions(-) create mode 100644 include/blt/std/binary_tree.h create mode 100644 include/blt/std/math.h diff --git a/README.md b/README.md index a8d1f29..d1bd486 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# BLT +# blt A common utilties library for my future creations diff --git a/include/blt/profiling/profiler.h b/include/blt/profiling/profiler.h index 28e2e91..da73d6d 100644 --- a/include/blt/profiling/profiler.h +++ b/include/blt/profiling/profiler.h @@ -10,10 +10,11 @@ #include #include #include -#include +#include #include +#include -namespace BLT { +namespace blt { struct CapturePoint { std::string_view name; long point; @@ -34,9 +35,9 @@ namespace BLT { HASHMAP_TYPE intervals{}; // profiling points - std::vector> cyclicPointsHistory{}; - std::queue points{}; - std::queue cyclicPoints{}; + std::vector> cyclicPointsHistory{}; + blt::flat_queue points{}; + blt::flat_queue cyclicPoints{}; std::mutex timerLock{}; public: diff --git a/include/blt/std/binary_tree.h b/include/blt/std/binary_tree.h new file mode 100644 index 0000000..7021c3e --- /dev/null +++ b/include/blt/std/binary_tree.h @@ -0,0 +1,146 @@ +/* + * Created by Brett on 09/01/23. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ + +#include +#include +#include + +#ifndef BLT_BINARY_TREE_H +#define BLT_BINARY_TREE_H + +namespace blt { + + class binary_search_tree_error : public std::runtime_error { + public: + explicit binary_search_tree_error(const std::string& string): runtime_error(string) {} + }; + + template + class node_binary_search_tree { + protected: + struct BST_node { + BST_node* left = nullptr; + BST_node* right = nullptr; + T payload; + + ~BST_node() { + delete (left); + delete (right); + } + }; + + BST_node* m_root = nullptr; + private: + void insert(BST_node* root, const T& element) { + BST_node* searchNode = root; + // basically we are iterating through the tree looking for a valid node to insert into. + while (true) { + // check for left and right tree traversal if it exists + if (searchNode->left != nullptr && element < searchNode->left->payload) { + searchNode = searchNode->left; + continue; + } + if (searchNode->right != nullptr && element > searchNode->right->payload) { + searchNode = searchNode->right; + continue; + } + if (element == searchNode->payload) + throw binary_search_tree_error{"Unable to insert. Nodes cannot have equal values!\n"}; + // insert into the lowest node consistent with a BST + if (element < searchNode->payload) { + searchNode->left = new BST_node(); + searchNode->left->payload = element; + } else { + searchNode->right = new BST_node(); + searchNode->right->payload = element; + } + return; + } + } + + BST_node* search(BST_node*& parent, const T& element) { + BST_node* searchNode = m_root; + // basically we are iterating through the tree looking for a valid node to insert into. + while (true) { + if (searchNode->payload == element) + return searchNode->payload; + // check for left and right tree traversal if it exists + if (searchNode->left != nullptr && element < searchNode->left->payload) { + parent = searchNode; + searchNode = searchNode->left; + continue; + } + if (searchNode->right != nullptr && element > searchNode->right->payload) { + parent = searchNode; + searchNode = searchNode->right; + continue; + } + } + } + + std::vector inOrderTraverse(BST_node* root) { + std::vector nodes{}; + blt::flat_stack nodeStack{}; + + BST_node* current = root; + while (current != nullptr || !nodeStack.isEmpty()) { + // go all the way to the left subtree + while (current != nullptr){ + nodeStack.push(current); + current = current->left; + } + current = nodeStack.front(); + nodeStack.pop(); + nodes.push_back(current); + current = current->right; + } + + return nodes; + } + + public: + node_binary_search_tree() { + m_root = new BST_node(); + } + + void insert(const T& element) { + insert(m_root, element); + } + + [[nodiscard]] BST_node* search(const T& element) const { + search(nullptr, element); + } + + void remove(const T& element) { + BST_node* parent = nullptr; + auto elementNode = search(parent, element); + // reconstruct subtree. More efficient way of doing this... TODO + std::vector subNodes = inOrderTraverse(elementNode); + for (auto* node : subNodes){ + if (node != elementNode) + insert(parent, node->payload); + } + } + + ~node_binary_search_tree() { + delete (m_root); + } + }; + + template + class flat_binary_search_tree { + private: + + }; + + template + using node_BST = node_binary_search_tree; + template + using flat_BST = flat_binary_search_tree; + +} + +#endif //BLT_BINARY_TREE_H diff --git a/include/blt/std/math.h b/include/blt/std/math.h new file mode 100644 index 0000000..8c9e92e --- /dev/null +++ b/include/blt/std/math.h @@ -0,0 +1,16 @@ +/* + * Created by Brett on 09/01/23. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ + +#ifndef BLT_MATH_H +#define BLT_MATH_H + +namespace blt { + + + +} + +#endif //BLT_MATH_H diff --git a/include/blt/std/queue.h b/include/blt/std/queue.h index e0e2da5..e747a1a 100644 --- a/include/blt/std/queue.h +++ b/include/blt/std/queue.h @@ -10,7 +10,7 @@ /** * Do no use any queue in this file. They are slower than std::queue. */ -namespace BLT { +namespace blt { template struct node { @@ -185,7 +185,6 @@ namespace BLT { } } }; - } #endif //BLT_QUEUE_H diff --git a/include/blt/std/random.h b/include/blt/std/random.h index c083a28..4cd5f27 100644 --- a/include/blt/std/random.h +++ b/include/blt/std/random.h @@ -9,7 +9,7 @@ #include -namespace BLT { +namespace blt { /** * Creates a container class for generating random number distributions * @tparam T numeric type diff --git a/include/blt/std/string.h b/include/blt/std/string.h index adbcbdb..d7b2950 100644 --- a/include/blt/std/string.h +++ b/include/blt/std/string.h @@ -12,7 +12,7 @@ #include #include -namespace BLT::String { +namespace blt::String { /** * Converts the string into lower case * @param s string to lower case diff --git a/include/blt/std/system.h b/include/blt/std/system.h index 496562a..e888624 100644 --- a/include/blt/std/system.h +++ b/include/blt/std/system.h @@ -7,7 +7,7 @@ #ifndef BLT_SYSTEM_H #define BLT_SYSTEM_H -namespace BLT::System { +namespace blt::System { // TODO: system memory and current CPU usage. (Linux Only currently) } diff --git a/include/blt/std/time.h b/include/blt/std/time.h index ddc39ed..4610300 100644 --- a/include/blt/std/time.h +++ b/include/blt/std/time.h @@ -11,7 +11,7 @@ #include #include -namespace BLT::System { +namespace blt::System { static inline auto getCurrentTimeNanoseconds() { return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); } diff --git a/src/blt/profiling/profiler.cpp b/src/blt/profiling/profiler.cpp index 45a107d..44986fe 100644 --- a/src/blt/profiling/profiler.cpp +++ b/src/blt/profiling/profiler.cpp @@ -5,6 +5,6 @@ */ #include -namespace BLT { +namespace blt { } \ No newline at end of file diff --git a/src/blt/std/queues.cpp b/src/blt/std/queues.cpp index a69417d..7d853c4 100644 --- a/src/blt/std/queues.cpp +++ b/src/blt/std/queues.cpp @@ -5,6 +5,6 @@ */ #include -namespace BLT { +namespace blt { } \ No newline at end of file diff --git a/src/blt/std/system.cpp b/src/blt/std/system.cpp index 1e7442b..692089f 100644 --- a/src/blt/std/system.cpp +++ b/src/blt/std/system.cpp @@ -4,3 +4,4 @@ * See LICENSE file for license detail */ #include +#include \ No newline at end of file