-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbst.hpp
86 lines (68 loc) · 1.82 KB
/
bst.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <unordered_map>
namespace bst {
struct TreeNode {
int key;
int val;
TreeNode* left, right;
TreeNode(T k, U v): key(k), val(v), left(nullptr), right(nullptr) {};
};
class BST {
public:
std::shared_ptr<TreeNode> search(int key) {
return search_recursive(key, root_);
}
std::shared_ptr<TreeNode> search_recursive(int key, std::shared_ptrTreeNode> root) {
if (root == nullptr || root->key == key) {
return root;
}
if (key < root->key) {
return search_recursive(key, root->left);
}
return search_recursive(key, root->right);
}
std::shared_ptr<TreeNode> search_iterative(int key, std::shared_ptr<TreeNode> root) {
TreeNode* ptr = root;
while (ptr != nullptr) {
if (key == ptr->key) {
return ptr;
}
if (key < ptr->key) {
ptr = ptr->left;
} else {
ptr = ptr->right;
}
}
return ptr;
}
void insert(int key, int value) {
void insert_recursive(key, value, root_);
}
void insert_recursive(int key, int value, std::shared_ptr<TreeNode> root) {
if (root == nullptr) {
root = std::make_shared<TreeNode>(key, value);
}
}
void insert_iterative(int key, int value) {}
void insert(TreeNode* ptr) {
if (root_ == nullptr) {
root_ = std::move(ptr);
return;
}
TreeNode* ptr2 = root_.get();
while (true) {
if (ptr->key <= ptr2->key) {
// go left
} else {
// go right
}
}
}
void delete() {}
private:
std::shared_ptr<TreeNode> root_ = nullptr;
};
template<class T>
class AVLTree {
};
};