BinarySearchTree
Loading...
Searching...
No Matches
BinarySearchTree.hpp
Go to the documentation of this file.
1/*
2 MIT License
3
4 Copyright(c) 2023 George Fotopoulos
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files(the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions :
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23*/
24
25#pragma once
26
27#include <algorithm>
28#include <functional>
29#include <initializer_list>
30#include <memory>
31#include <queue>
32#include <utility>
33
34template<typename key_t, typename value_t>
36 binary_search_tree_node_t(const key_t &key, const value_t &value) : key(key), value(value) {}
37
38 std::shared_ptr<binary_search_tree_node_t> lchild;
39 std::shared_ptr<binary_search_tree_node_t> rchild;
40
41 key_t key;
42 value_t value;
43};
44
45template<typename key_t, typename value_t>
46using handler_t = std::function<void(const key_t &, value_t &)>;
47
48template<typename key_t, typename value_t>
50public:
52
54 pre_order_traversal_helper(m_root, handler);
55 }
56
58 in_order_traversal_helper(m_root, handler);
59 }
60
62 post_order_traversal_helper(m_root, handler);
63 }
64
65 void clear() {
66 m_root.reset();
67 }
68
69 void insert(const key_t &key, const value_t &value) {
70 m_root = insert_helper(m_root, key, value);
71 }
72
73 void remove(const key_t &key) {
74 m_root = remove_helper(m_root, key);
75 }
76
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);
79 }
80
81 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> minimum() {
82 return minimum_helper(m_root);
83 }
84
85 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> maximum() {
86 return maximum_helper(m_root);
87 }
88
89 int height() {
90 return height_helper(m_root);
91 }
92
93 int size() {
94 return size_helper(m_root);
95 }
96
97private:
98 void pre_order_traversal_helper(std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> root, handler_t<key_t, value_t> handler) {
99 if (!root) return;
100 handler(root->key, root->value);
101 pre_order_traversal_helper(root->lchild, handler);
102 pre_order_traversal_helper(root->rchild, handler);
103 }
104
105 void in_order_traversal_helper(std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> root, handler_t<key_t, value_t> handler) {
106 if (!root) return;
107 in_order_traversal_helper(root->lchild, handler);
108 handler(root->key, root->value);
109 in_order_traversal_helper(root->rchild, handler);
110 }
111
112 void post_order_traversal_helper(std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> root, handler_t<key_t, value_t> handler) {
113 if (!root) return;
114 post_order_traversal_helper(root->lchild, handler);
115 post_order_traversal_helper(root->rchild, handler);
116 handler(root->key, root->value);
117 }
118
119 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> insert_helper(std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> root, const key_t &key, const value_t &value) {
120 if (!root) return std::make_shared<binary_search_tree_node_t<key_t, value_t>>(key, value);
121 if (key == root->key) {
122 root->value = value;
123 } else if (key < root->key) {
124 root->lchild = insert_helper(root->lchild, key, value);
125 } else {
126 root->rchild = insert_helper(root->rchild, key, value);
127 }
128 return root;
129 }
130
131 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> remove_helper(std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> root, const key_t &key) {
132 if (!root) return nullptr;
133 if (key == root->key) {
134 if (!root->lchild) {
135 return root->rchild;
136 } else if (!root->rchild) {
137 return root->lchild;
138 }
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);
145 } else {
146 root->rchild = remove_helper(root->rchild, key);
147 }
148 return root;
149 }
150
151 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> search_helper(std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> root, const key_t &key) {
152 if (!root) return root;
153 if (root->key == key) {
154 return root;
155 } else if (key < root->key) {
156 return search_helper(root->lchild, key);
157 } else {
158 return search_helper(root->rchild, key);
159 }
160 }
161
162 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> minimum_helper(std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> root) {
163 while (root->lchild) root = root->lchild;
164 return root;
165 }
166
167 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> maximum_helper(std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> root) {
168 while (root->rchild) root = root->rchild;
169 return root;
170 }
171
172 int height_helper(const std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> root) {
173 if (!root) return 0;
174 return std::max(height_helper(root->lchild), height_helper(root->rchild)) + 1;
175 }
176
177 int size_helper(const std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> root) {
178 if (!root) return 0;
179 return size_helper(root->lchild) + size_helper(root->rchild) + 1;
180 }
181
182 std::shared_ptr<binary_search_tree_node_t<key_t, value_t>> m_root;
183};
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