-
Notifications
You must be signed in to change notification settings - Fork 1
/
BST.h
145 lines (134 loc) · 2.71 KB
/
BST.h
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <vector>
namespace CG {
template<typename T>
struct BSTNode {
BSTNode *left, *right, *parent;
float key;
T* data;
BSTNode(float k, T *d) : key{k}, data{d}, left{nullptr},
right{nullptr}, parent{nullptr} {};
//std::vector<T> inorder();
T* search(float);
BSTNode* min();
BSTNode* max();
BSTNode* pred();
BSTNode* succ();
};
template<typename T>
struct BSTree {
BSTNode<T> *root;
BSTree() : root{nullptr} {};
BSTree(BSTNode<T> *p) : root{p} {};
void insert(BSTNode<T> *);
void transplant(BSTNode<T> *, BSTNode<T> *);
void del(BSTNode<T> *);
};
template<typename T>
T* BSTNode<T>::search(float k)
{
BSTNode<T> *curr = this;
while (k != curr->key) {
if (k < curr->key && curr->left != nullptr) {
curr = curr->left;
} else if (curr->right != nullptr) {
curr = curr->right;
}
}
if (curr->key == k) return curr->data;
return nullptr;
}
template<typename T>
BSTNode<T>* BSTNode<T>::min()
{
BSTNode<T> *curr = this;
while (curr->left != nullptr)
curr = curr->left;
return curr;
}
template<typename T>
BSTNode<T>* BSTNode<T>::max()
{
BSTNode<T> *curr = this;
while (curr->right != nullptr)
curr = curr->right;
return curr;
}
template<typename T>
BSTNode<T>* BSTNode<T>::succ()
{
if (right != nullptr)
return right->min();
BSTNode<T> *x = this;
BSTNode<T> *y = parent;
while (y != nullptr && y->right == x) {
x = y;
y = y->parent;
}
}
template<typename T>
BSTNode<T>* BSTNode<T>::pred()
{
if (left != nullptr)
return left->max();
BSTNode<T> *x = this;
BSTNode<T> *y = parent;
while (y != nullptr && y->left == x) {
x = y;
y = y->parent;
}
}
template<typename T>
void BSTree<T>::insert(BSTNode<T> *n)
{
BSTNode<T> *curr = root;
BSTNode<T> *p = nullptr;
while (curr != nullptr) {
p = curr;
if (n->key < curr->key) {
curr = curr->left;
} else {
curr = curr->right;
}
}
n->parent = p;
if (p == nullptr) {
root = n;
} else if (n->key < p->key) {
p->left = n;
} else {
p->right = n;
}
}
template<typename T>
void BSTree<T>::transplant(BSTNode<T> *u, BSTNode<T> *v)
{
if (u->parent == nullptr) {
root = v;
} else if (u == u->parent->left) {
u->parent->left = v;
} else {
u->parent->right = v;
}
if (v != nullptr)
v->parent = v->parent;
}
template<typename T>
void BSTree<T>::del(BSTNode<T> *n)
{
if (n->left == nullptr) {
transplant(n, n->right);
} else if (n->right == nullptr) {
transplant(n, n->left);
} else {
BSTNode<T> *y = n->right->min();
if (y->parent != n) {
transplant(y, y->right);
y->right = n->right;
y->right->parent = y;
}
transplant(n, y);
y->left = n->left;
y->left->parent = y;
}
}
}