29#include <initializer_list>
34template<
typename key_t,
typename value_t>
38 std::shared_ptr<binary_search_tree_node_t>
lchild;
39 std::shared_ptr<binary_search_tree_node_t>
rchild;
45template<
typename key_t,
typename value_t>
46using handler_t = std::function<void(
const key_t &, value_t &)>;
48template<
typename key_t,
typename value_t>
54 pre_order_traversal_helper(m_root, handler);
58 in_order_traversal_helper(m_root, handler);
62 post_order_traversal_helper(m_root, handler);
69 void insert(
const key_t &key,
const value_t &value) {
70 m_root = insert_helper(m_root, key, value);
74 m_root = remove_helper(m_root, key);
77 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>>
search(
const key_t &key) {
78 return search_helper(m_root, key);
81 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>>
minimum() {
82 return minimum_helper(m_root);
85 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>>
maximum() {
86 return maximum_helper(m_root);
90 return height_helper(m_root);
94 return size_helper(m_root);
100 handler(root->key, root->value);
101 pre_order_traversal_helper(root->lchild, handler);
102 pre_order_traversal_helper(root->rchild, handler);
107 in_order_traversal_helper(root->lchild, handler);
108 handler(root->key, root->value);
109 in_order_traversal_helper(root->rchild, handler);
114 post_order_traversal_helper(root->lchild, handler);
115 post_order_traversal_helper(root->rchild, handler);
116 handler(root->key, root->value);
120 if (!root)
return std::make_shared<binary_search_tree_node_t<key_t, value_t>>(key, value);
121 if (key == root->key) {
123 }
else if (key < root->key) {
124 root->lchild = insert_helper(root->lchild, key, value);
126 root->rchild = insert_helper(root->rchild, key, value);
132 if (!root)
return nullptr;
133 if (key == root->key) {
136 }
else if (!root->rchild) {
139 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> minRight = minimum_helper(root->rchild);
140 root->key = minRight->key;
141 root->value = minRight->value;
142 root->rchild = remove_helper(root->rchild, minRight->key);
143 }
else if (key < root->key) {
144 root->lchild = remove_helper(root->lchild, key);
146 root->rchild = remove_helper(root->rchild, key);
152 if (!root)
return root;
153 if (root->key == key) {
155 }
else if (key < root->key) {
156 return search_helper(root->lchild, key);
158 return search_helper(root->rchild, key);
163 while (root->lchild) root = root->lchild;
168 while (root->rchild) root = root->rchild;
174 return std::max(height_helper(root->lchild), height_helper(root->rchild)) + 1;
179 return size_helper(root->lchild) + size_helper(root->rchild) + 1;
182 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> m_root;
std::function< void(const key_t &, value_t &)> handler_t
Definition: BinarySearchTree.hpp:46
Definition: BinarySearchTree.hpp:49
void remove(const key_t &key)
Definition: BinarySearchTree.hpp:73
void post_order_traversal(handler_t< key_t, value_t > handler)
Definition: BinarySearchTree.hpp:61
void in_order_traversal(handler_t< key_t, value_t > handler)
Definition: BinarySearchTree.hpp:57
std::shared_ptr< binary_search_tree_node_t< key_t, value_t > > minimum()
Definition: BinarySearchTree.hpp:81
int size()
Definition: BinarySearchTree.hpp:93
void pre_order_traversal(handler_t< key_t, value_t > handler)
Definition: BinarySearchTree.hpp:53
binary_search_tree_t()=default
std::shared_ptr< binary_search_tree_node_t< key_t, value_t > > maximum()
Definition: BinarySearchTree.hpp:85
int height()
Definition: BinarySearchTree.hpp:89
std::shared_ptr< binary_search_tree_node_t< key_t, value_t > > search(const key_t &key)
Definition: BinarySearchTree.hpp:77
void clear()
Definition: BinarySearchTree.hpp:65
void insert(const key_t &key, const value_t &value)
Definition: BinarySearchTree.hpp:69
Definition: BinarySearchTree.hpp:35
std::shared_ptr< binary_search_tree_node_t > lchild
Definition: BinarySearchTree.hpp:38
key_t key
Definition: BinarySearchTree.hpp:41
binary_search_tree_node_t(const key_t &key, const value_t &value)
Definition: BinarySearchTree.hpp:36
value_t value
Definition: BinarySearchTree.hpp:42
std::shared_ptr< binary_search_tree_node_t > rchild
Definition: BinarySearchTree.hpp:39