2023-01-10 10:45:11 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett on 09/01/23.
|
|
|
|
* Licensed under GNU General Public License V3.0
|
|
|
|
* See LICENSE file for license detail
|
|
|
|
*/
|
|
|
|
|
2023-12-19 00:35:37 -05:00
|
|
|
#ifndef BLT_BINARY_TREE_H
|
|
|
|
#define BLT_BINARY_TREE_H
|
|
|
|
|
2023-01-10 10:45:11 -05:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
2023-12-18 23:24:53 -05:00
|
|
|
#include <blt/std/allocator.h>
|
2023-01-11 18:56:42 -05:00
|
|
|
#include <iostream>
|
2023-01-12 12:18:39 -05:00
|
|
|
#include <memory>
|
2023-12-19 00:35:37 -05:00
|
|
|
// TODO: blt::queue
|
|
|
|
#include <queue>
|
2023-01-10 10:45:11 -05:00
|
|
|
|
2023-12-18 23:24:53 -05:00
|
|
|
namespace blt
|
|
|
|
{
|
2023-01-10 10:45:11 -05:00
|
|
|
|
2023-12-18 23:24:53 -05:00
|
|
|
class binary_search_tree_error : public std::runtime_error
|
|
|
|
{
|
2023-01-10 10:45:11 -05:00
|
|
|
public:
|
2023-12-18 23:24:53 -05:00
|
|
|
explicit binary_search_tree_error(const std::string& string): runtime_error(string)
|
|
|
|
{}
|
2023-01-10 10:45:11 -05:00
|
|
|
};
|
|
|
|
|
2023-12-19 00:35:37 -05:00
|
|
|
template<typename T, typename ALLOC = blt::area_allocator<T>>
|
2023-12-18 23:24:53 -05:00
|
|
|
class AVL_node_tree
|
|
|
|
{
|
|
|
|
private:
|
2023-12-20 14:49:31 -05:00
|
|
|
ALLOC alloc;
|
|
|
|
|
2023-12-18 23:24:53 -05:00
|
|
|
struct node
|
|
|
|
{
|
|
|
|
T val;
|
2023-12-19 00:35:37 -05:00
|
|
|
node* left;
|
|
|
|
node* right;
|
2023-12-20 14:49:31 -05:00
|
|
|
ALLOC& alloc;
|
2023-12-19 00:35:37 -05:00
|
|
|
|
2023-12-20 14:49:31 -05:00
|
|
|
node(const T& t, ALLOC& alloc): val(t), alloc(alloc)
|
2023-12-19 00:35:37 -05:00
|
|
|
{}
|
|
|
|
|
2023-12-20 14:49:31 -05:00
|
|
|
node(T&& m, ALLOC alloc): val(m), alloc(alloc)
|
2023-12-19 00:35:37 -05:00
|
|
|
{}
|
|
|
|
|
|
|
|
node(const node& copy) = delete;
|
|
|
|
|
|
|
|
node(node&& move) = delete;
|
|
|
|
|
|
|
|
node& operator=(const node& copy) = delete;
|
|
|
|
|
|
|
|
node& operator=(node&& move) = delete;
|
|
|
|
|
|
|
|
~node()
|
|
|
|
{
|
2023-12-20 14:49:31 -05:00
|
|
|
left->~node();
|
|
|
|
alloc.deallocate(left);
|
|
|
|
right->~node();
|
|
|
|
alloc.deallocate(right);
|
2023-12-19 00:35:37 -05:00
|
|
|
}
|
2023-12-18 23:24:53 -05:00
|
|
|
};
|
2023-12-19 00:35:37 -05:00
|
|
|
|
2023-12-20 14:45:48 -05:00
|
|
|
inline node* newNode(T&& t)
|
|
|
|
{
|
2023-12-20 14:49:31 -05:00
|
|
|
return new(alloc.allocate(1)) node(t);
|
2023-12-20 14:36:46 -05:00
|
|
|
}
|
2023-12-20 14:49:31 -05:00
|
|
|
|
|
|
|
node* root = nullptr;
|
2023-12-18 23:24:53 -05:00
|
|
|
public:
|
|
|
|
AVL_node_tree() = default;
|
|
|
|
|
2023-12-19 00:35:37 -05:00
|
|
|
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)
|
2023-12-18 23:24:53 -05:00
|
|
|
{
|
2023-12-19 00:35:37 -05:00
|
|
|
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;
|
|
|
|
}
|
2023-12-18 23:24:53 -05:00
|
|
|
|
2023-12-19 00:35:37 -05:00
|
|
|
void insert(const T& t)
|
|
|
|
{
|
|
|
|
if (root == nullptr)
|
|
|
|
{
|
2023-12-20 14:45:48 -05:00
|
|
|
root = newNode(t);
|
2023-12-19 00:35:37 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
node* search = root;
|
2023-12-19 01:28:59 -05:00
|
|
|
node* parent = nullptr;
|
2023-12-19 00:35:37 -05:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (t < search->val)
|
|
|
|
{
|
|
|
|
if (search->left == nullptr)
|
|
|
|
{
|
2023-12-20 14:45:48 -05:00
|
|
|
search->left = newNode(t);
|
2023-12-19 01:28:59 -05:00
|
|
|
break;
|
2023-12-19 00:35:37 -05:00
|
|
|
}
|
|
|
|
search = search->left;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (search->right == nullptr)
|
|
|
|
{
|
2023-12-20 14:45:48 -05:00
|
|
|
search->right = newNode(t);
|
2023-12-19 01:28:59 -05:00
|
|
|
break;
|
2023-12-19 00:35:37 -05:00
|
|
|
}
|
|
|
|
search = search->right;
|
|
|
|
}
|
2023-12-19 01:28:59 -05:00
|
|
|
parent = search;
|
2023-12-19 00:35:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~AVL_node_tree()
|
|
|
|
{
|
2023-12-20 14:49:31 -05:00
|
|
|
root->~node();
|
|
|
|
alloc.deallocate(root);
|
2023-12-18 23:24:53 -05:00
|
|
|
}
|
|
|
|
};
|
2023-01-10 10:45:11 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_BINARY_TREE_H
|