20template <
typename T>
class bst {
27 inline explicit bst(std::vector<T> _elements = {})
noexcept : root(nullptr) {
28 if (!_elements.empty()) {
29 for (T& x : _elements) {
39 inline explicit bst(
const bst& b) : root(b.root), _size(b._size) {}
52 inline ~bst() noexcept { root =
nullptr; }
67 root = _insert(root, key);
76 inline bool search(T key) {
return _search(root, key); }
82 inline void remove(T key) { root = _remove(root, key); }
92 std::vector<T> ino = this->
inorder();
102 std::vector<T> ino = this->
inorder();
111 inline size_t size() {
return _size; }
119 _inorder([&](std::shared_ptr<node> callbacked) { path.push_back(callbacked->info); }, root);
129 _preorder([&](std::shared_ptr<node> callbacked) { path.push_back(callbacked->info); },
140 _postorder([&](std::shared_ptr<node> callbacked) { path.push_back(callbacked->info); },
150 std::vector<std::vector<T>> path;
151 std::queue<std::shared_ptr<node>> q;
154 size_t size = q.size();
155 std::vector<T> level;
156 for (
size_t i = 0; i <
size; i++) {
157 std::shared_ptr<node> current = q.front();
159 level.push_back(current->info);
161 q.push(current->left);
163 if (current->right) {
164 q.push(current->right);
167 path.push_back(level);
176#ifdef TREE_VISUALIZATION_H
177 inline void visualize() {
178 std::string _generated = generate_visualization();
179 tree_visualization::visualize(_generated);
187 std::vector<T> order = t.
inorder();
188 for (
int i = 0; i < order.size(); i++) {
189 if (i != order.size() - 1) {
190 out << order[i] <<
", ";
192 out << order[i] <<
'\n';
205 typedef struct node {
207 std::shared_ptr<node> right;
208 std::shared_ptr<node> left;
209 node(T key) : info(key), right(nullptr), left(nullptr) {}
212 std::shared_ptr<node> root;
215 std::shared_ptr<node> new_node(T& key) {
216 std::shared_ptr<node> p = std::make_shared<node>(key);
220 std::shared_ptr<node> _insert(std::shared_ptr<node> root, T& key) {
222 return new_node(key);
224 if (root->info < key) {
225 root->right = _insert(root->right, key);
227 root->left = _insert(root->left, key);
233 bool _search(std::shared_ptr<node> root, T& key) {
235 if (root->info < key) {
237 }
else if (root->info > key) {
246 std::shared_ptr<node> _remove(std::shared_ptr<node> root, T& key) {
251 if (root->info < key) {
252 root->right = _remove(root->right, key);
253 }
else if (root->info > key) {
254 root->left = _remove(root->left, key);
256 if (!root->left && !root->right) {
259 }
else if (!root->left) {
260 std::shared_ptr<node> temp = root->right;
263 }
else if (!root->right) {
264 std::shared_ptr<node> temp = root->left;
268 std::shared_ptr<node> temp = root->right;
272 root->info = temp->info;
273 root->right = _remove(root->right, temp->info);
279 void _inorder(std::function<
void(std::shared_ptr<node>)> callback, std::shared_ptr<node> root) {
281 _inorder(callback, root->left);
283 _inorder(callback, root->right);
287 void _postorder(std::function<
void(std::shared_ptr<node>)> callback,
288 std::shared_ptr<node> root) {
290 _postorder(callback, root->left);
291 _postorder(callback, root->right);
296 void _preorder(std::function<
void(std::shared_ptr<node>)> callback,
297 std::shared_ptr<node> root) {
300 _preorder(callback, root->left);
301 _preorder(callback, root->right);
305 std::string generate_visualization() {
306 std::string _generate = _inorder_gen(root);
310 std::string _inorder_gen(std::shared_ptr<node> root) {
312 if (std::is_same_v<T, char> || std::is_same_v<T, std::string>) {
316 _s += root->left->info;
318 _s += _inorder_gen(root->left);
323 _s += root->right->info;
325 _s += _inorder_gen(root->right);
329 _s += std::to_string(root->info) +
"->" + std::to_string(root->left->info) +
"\n" +
330 _inorder_gen(root->left);
333 _s += std::to_string(root->info) +
"->" + std::to_string(root->right->info) +
"\n" +
334 _inorder_gen(root->right);