binary tree

v1
Brett 2023-12-19 00:35:37 -05:00
parent 2440044d72
commit 8e701f6953
2 changed files with 101 additions and 10 deletions

View File

@ -4,14 +4,16 @@
* See LICENSE file for license detail
*/
#ifndef BLT_BINARY_TREE_H
#define BLT_BINARY_TREE_H
#include <stdexcept>
#include <vector>
#include <blt/std/allocator.h>
#include <iostream>
#include <memory>
#ifndef BLT_BINARY_TREE_H
#define BLT_BINARY_TREE_H
// TODO: blt::queue
#include <queue>
namespace blt
{
@ -23,22 +25,109 @@ namespace blt
{}
};
template<typename T, typename alloc = blt::area_allocator<T>>
template<typename T, typename ALLOC = blt::area_allocator<T>>
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<node*> 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;
}
};

View File

@ -17,6 +17,7 @@
*/
#include <blt_tests.h>
#include <blt/std/queue.h>
#include <blt/std/binary_tree.h>
#include <queue>
#include <stack>
#include <vector>
@ -113,10 +114,11 @@ namespace blt
void test::data::run()
{
auto max = static_cast<size_t>(std::log10(max_size));
auto min = static_cast<size_t>(std::log10(min_size));
for (size_t i = min; i <= max; i++)
run_size(exp(10, i));
// auto max = static_cast<size_t>(std::log10(max_size));
// auto min = static_cast<size_t>(std::log10(min_size));
// for (size_t i = min; i <= max; i++)
// run_size(exp(10, i));
}
}