From 8e701f6953a1b80d070caeb5d1f38048e142c51f Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 19 Dec 2023 00:35:37 -0500 Subject: [PATCH] binary tree --- include/blt/std/binary_tree.h | 101 ++++++++++++++++++++++++++++-- tests/src/datastructure_tests.cpp | 10 +-- 2 files changed, 101 insertions(+), 10 deletions(-) diff --git a/include/blt/std/binary_tree.h b/include/blt/std/binary_tree.h index 446b5bb..ca553a0 100755 --- a/include/blt/std/binary_tree.h +++ b/include/blt/std/binary_tree.h @@ -4,14 +4,16 @@ * See LICENSE file for license detail */ +#ifndef BLT_BINARY_TREE_H +#define BLT_BINARY_TREE_H + #include #include #include #include #include - -#ifndef BLT_BINARY_TREE_H -#define BLT_BINARY_TREE_H +// TODO: blt::queue +#include namespace blt { @@ -23,22 +25,109 @@ namespace blt {} }; - template> + template> class AVL_node_tree { private: struct node { - node* left, right; T val; + node* left; + node* right; + + + node(const T& t): val(t) + {} + + node(T&& m): val(m) + {} + + node(const node& copy) = delete; + + node(node&& move) = delete; + + node& operator=(const node& copy) = delete; + + node& operator=(node&& move) = delete; + + ~node() + { + delete left; + delete right; + } }; + + ALLOC alloc; node* root = nullptr; public: AVL_node_tree() = default; + AVL_node_tree(const AVL_node_tree& copy) = delete; + + AVL_node_tree(AVL_node_tree&& move) = delete; + + AVL_node_tree& operator=(const AVL_node_tree& copy) = delete; + + AVL_node_tree& operator=(AVL_node_tree&& move) = delete; + + size_t height(node* start = nullptr) + { + if (start == nullptr) + start = root; + if (start == nullptr) + return 0; + std::queue nodes; + nodes.push(start); + size_t height = 0; + while (!nodes.empty()) + { + height++; + size_t level_count = nodes.size(); + while (level_count-- > 0) + { + if (nodes.front()->left != nullptr) + nodes.push(nodes.front()->left); + if (nodes.front()->right != nullptr) + nodes.push(nodes.front()->right); + nodes.pop(); + } + } + return height; + } + + void insert(const T& t) + { + if (root == nullptr) + { + root = new node(t); + return; + } + node* search = root; + while (true) + { + if (t < search->val) + { + if (search->left == nullptr) + { + search->left = new node(t); + return; + } + search = search->left; + } else + { + if (search->right == nullptr) + { + search->right = new node(t); + return; + } + search = search->right; + } + } + } + ~AVL_node_tree() { - + delete root; } }; diff --git a/tests/src/datastructure_tests.cpp b/tests/src/datastructure_tests.cpp index 1f40ce3..7b5141b 100644 --- a/tests/src/datastructure_tests.cpp +++ b/tests/src/datastructure_tests.cpp @@ -17,6 +17,7 @@ */ #include #include +#include #include #include #include @@ -113,10 +114,11 @@ namespace blt void test::data::run() { - auto max = static_cast(std::log10(max_size)); - auto min = static_cast(std::log10(min_size)); - for (size_t i = min; i <= max; i++) - run_size(exp(10, i)); +// auto max = static_cast(std::log10(max_size)); +// auto min = static_cast(std::log10(min_size)); +// for (size_t i = min; i <= max; i++) +// run_size(exp(10, i)); + } } \ No newline at end of file