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:
|
|
|
|
struct node
|
|
|
|
{
|
|
|
|
T val;
|
2023-12-19 00:35:37 -05:00
|
|
|
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;
|
|
|
|
}
|
2023-12-18 23:24:53 -05:00
|
|
|
};
|
2023-12-19 00:35:37 -05:00
|
|
|
|
|
|
|
ALLOC alloc;
|
2023-12-18 23:24:53 -05:00
|
|
|
node* root = nullptr;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
root = new node(t);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
search->left = new node(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)
|
|
|
|
{
|
|
|
|
search->right = new node(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()
|
|
|
|
{
|
|
|
|
delete root;
|
2023-12-18 23:24:53 -05:00
|
|
|
}
|
|
|
|
};
|
2023-01-10 10:45:11 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_BINARY_TREE_H
|