-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBinarySearchTree.h
373 lines (328 loc) · 12.4 KB
/
BinarySearchTree.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//
// Created by Ryan.Zurrin001 on 12/15/2021.
//
#ifndef PHYSICSFORMULA_BINARYSEARCHTREE_H
#define PHYSICSFORMULA_BINARYSEARCHTREE_H
// TODO Implement the BST, Braided Search tree and Randomized Search tree as well
#include <list>
#include <functional>
#include <algorithm>
#include <iostream>
using namespace std;
namespace rez {
template< class KType, class VType, class Compare = std::less<KType>>
class BST {
struct BSTNode {
KType key = {};
VType value = {};
BSTNode* left = nullptr;
BSTNode* right = nullptr;
BSTNode* parent = nullptr;
public:
BSTNode() {}
BSTNode(KType& _key, VType& _value, BSTNode* _left = nullptr, BSTNode* _right = nullptr, BSTNode* _parent = nullptr) :
key(_key), value(_value), left(_left), right(_right), parent(_parent) {}
};
BSTNode* root = nullptr;
Compare comp;
BSTNode* find(KType& _key);
void transplant(BSTNode* u, BSTNode* v);
BSTNode* treeMin(BSTNode* branch_root);
BSTNode* treeMax(BSTNode* branch_root);
BSTNode* findSplitNode(KType& _min, KType& _max);
bool isLeaf(BSTNode* _node);
void addTreeToAList(BSTNode* _node, std::list<VType>&, bool addFront = false);
public:
BST(KType& _key, VType& _value) {
root = new BSTNode(_key, _value);
}
BST(std::list<std::pair<KType, VType>>& _data, const unsigned int _root_index = 0) {
root = new BSTNode(_data[_root_index].first, _data[_root_index].second);
for (size_t i = 0; i < _data.size(); i++) {
if (i != _root_index) {
insert(_data[i]);
}
}
}
void insert(const KType& _value);
void insert(std::list<KType>&);
void remove(const KType& _value);
void find(const KType&, const KType&, std::list<VType>&);
std::pair<KType, VType> predecessor(const KType& _value);
std::pair<KType, VType> successor(const KType& _value);
std::pair<KType, VType> minimum(BSTNode* _node = nullptr);
std::pair<KType, VType> maximum(BSTNode* _node = nullptr);
bool isEmpty() {
if (root)
return true;
return false;
}
void inOrderTraverse(BSTNode*, std::list<VType>&);
void inOrderTraverse();
void inOrderHelper(BSTNode* root);
void preOrderTraverse(BSTNode*, std::list<VType>&);
void postOrderTraverse(BSTNode*, std::list<VType>&);
};
#endif //PHYSICSFORMULA_BINARYSEARCHTREE_H
template<class KType, class VType, class Compare>
typename BST<KType, VType, Compare>::BSTNode* BST<KType, VType, Compare>::find(KType& _key) {
BSTNode* current = root;
while (current && current->key != _key) {
if (comp(current->key, _key))
current = current->right_child;
else
current = current->left_child;
}
// Given key is not found in this tree
return current;
}
template<class KType, class VType, class Compare>
void BST<KType, VType, Compare>::transplant(BSTNode* u, BSTNode* v)
{
if (!u->parent)
root = v;
else if (u == u->parent->left_child)
u->parent->left_child = v;
else
u->parent->right_child = v;
if (v)
v->parent = u->parent;
}
template<class KType, class VType, class Compare>
typename BST<KType, VType, Compare>::BSTNode* BST<KType, VType, Compare>::treeMin(BSTNode* _branch_root)
{
BSTNode* temp_node = _branch_root;
while (temp_node && temp_node->left_child) {
temp_node = temp_node->left_child;
}
temp_node;
}
template<class KType, class VType, class Compare>
typename BST<KType, VType, Compare>::BSTNode* BST<KType, VType, Compare>::treeMax(BSTNode* _branch_root) {
BSTNode* temp_node = _branch_root;
while (temp_node && temp_node->right_child) {
temp_node = temp_node->right_child;
}
return temp_node;
}
template<class KType, class VType, class Compare>
typename BST<KType, VType, Compare>::BSTNode* BST<KType, VType, Compare>::findSplitNode(KType& _min, KType& _max) {
auto v = root;
while (!isLeaf(v) && (_max <= v->key || _min > v->key)) {
if (_max <= v->key)
v = v->left;
else
v = v->right;
}
return v;
}
template<class KType, class VType, class Compare>
bool BST<KType, VType, Compare>::isLeaf(BSTNode* _node)
{
if (_node && _node->left && _node->right)
return true;
return false;
}
template<class KType, class VType, class Compare>
void BST<KType, VType, Compare>::addTreeToAList(BSTNode* _node, std::list<VType>& _list, bool addFront) {
std::list<VType> values;
inOrderTravers(_node, values);
if (addFront)
_list.insert(_list.begin(), values.begin(), values.end());
else
_list.insert(_list.end(), values.begin(), values.end());
}
template<class KType, class VType, class Compare>
void BST<KType, VType, Compare>::insert(const KType& _value) {
if (!root) {
root = new BSTNode(_value);
return;
}
else {
BSTNode* temp = root;
while (true) {
if (comp(temp->key, _value)) {
if (temp->left)
temp = temp->left;
else {
temp->left = new BSTNode(_value);
temp->left->parent = temp;
break;
}
}
else {
if (temp->right)
temp = temp->right;
else {
temp->right = new BSTNode(_value);
temp->right->parent = temp;
break;
}
}
}
}
}
template<class KType, class VType, class Compare>
void BST<KType, VType, Compare>::insert(std::list<KType>& _list) {
for (const auto& key : _list) {
insert(key);
}
}
template<class KType, class VType, class Compare>
void BST<KType, VType, Compare>::remove(const KType& _value) {
BSTNode* current_node = find(_value);
if (current_node) {
BSTNode* current_left = current_node->left_child;
BSTNode* current_right = current_node->right_child;
if (isLeaf(current_node))
transplant(current_node, nullptr);
if (!current_left) {
transplant(current_node, current_right);
}
else if (!current_right) {
transplant(current_node, current_left);
}
else {
BSTNode* right_min = treeMin(current_right);
if (right_min->parent != current_node) {
transplant(right_min, right_min->right_child);
right_min->right_child = current_node->right_child;
right_min->right_child->parent = right_min;
}
transplant(current_node, right_min);
right_min->left_child = current_node->left_child;
right_min->left_child->parent = right_min;
}
}
}
template<class KType, class VType, class Compare>
void BST<KType, VType, Compare>::find(const KType& _min, const KType& _max, std::list<VType>& _list)
{
auto v_split = findSplitNode(_min, _max);
if (isLeaf(v_split)) {
if (v_split->key >= _min && v_split->key < _max)
_list.insert(_list.push_front(v_split->key));
}
else {
//Follow the path to left boundary
auto v = v_split->left;
while (!isLeaf(v)) {
if (_min <= v->key) {
addTreeToAList(v->right, _list, true);
_list.push_front(v->key);
v = v->left;
}
else
v = v->right;
}
if (v->key >= _min)
_list.insert(_list.push_front(v->key));
v = v_split->right;
while (!isLeaf(v)) {
if (_max >= v->key) {
addTreeToAList(v->left, _list);
v = v->right;
}
else
v = v->left;
}
if (v->key <= _max)
_list.insert(_list.push_back(v->key));
}
}
template<class KType, class VType, class Compare>
std::pair<KType, VType> BST<KType, VType, Compare>::predecessor(const KType& _value) {
//BSTNode* current_node = find(_value);
//if (current_node){
// BSTNode* max_left = nullptr;
// max_left = treeMax(current_node->left_child);
// if (max_left)
// return max_left->key;
// else {
// auto parent = current_node->parent;
// while (parent && parent->parent){
// parent = parent->parent;
// if (parent->left)
// return parent->key;
// }
// }
//}
//return _value;
}
template<class KType, class VType, class Compare>
std::pair<KType, VType> BST<KType, VType, Compare>::successor(const KType& _value) {
//BSTNode* current_node = find(_value);
//if (current_node){
// BSTNode* min_right = nullptr;
// min_right = treeMax(current_node->right_child);
// if (min_right)
// return min_right->key;
// else {
// auto parent = current_node->parent;
// while (parent && parent->parent) {
// parent = parent->parent;
// if (parent->right)
// return parent->key;
// }
// }
//}
//return _value;
}
template<class KType, class VType, class Compare>
void BST<KType, VType, Compare>::inOrderTraverse(BSTNode* _node, std::list<VType>& _list) {
if (!_node)
return;
inOrderTravers(_node->left, _list);
_list.push_back(_node->value);
inOrderTravers(_node->right, _list);
}
template<class KType, class VType, class Compare>
inline void BST<KType, VType, Compare>::inOrderTraverse()
{
BSTNode* curr = root;
if (!curr)
return;
inOrderHelper(curr);
}
template<class KType, class VType, class Compare>
inline void BST<KType, VType, Compare>::inOrderHelper(BSTNode* root)
{
inOrderHelper(root->left);
std::cout << root->key << " ";
inOrderHelper(root->right);
}
template<class KType, class VType, class Compare>
void BST<KType, VType, Compare>::preOrderTraverse(BSTNode* _node, std::list<VType>& _list) {
if (!_node)
return;
_list.push_back(_node->value);
inOrderTravers(_node->left, _list);
inOrderTravers(_node->right, _list);
}
template<class KType, class VType, class Compare>
void BST<KType, VType, Compare>::postOrderTraverse(BSTNode* _node, std::list<VType>& _list) {
if (!_node)
return;
inOrderTravers(_node->left, _list);
inOrderTravers(_node->right, _list);
_list.push_back(_node->value);
}\
template<class KType, class VType, class Compare>
std::pair<KType, VType> BST<KType, VType, Compare>::minimum(BSTNode* _node) {
if (!_node)
_node = root;
auto temp = _node;
while (temp->left)
temp = temp->left;
return std::pair<KType, VType>(temp->key, temp->value);
}
template<class KType, class VType, class Compare>
std::pair<KType, VType> BST<KType, VType, Compare>::maximum(BSTNode* _node) {
if (!_node)
_node = root;
auto temp = _node;
while (temp->right)
temp = temp->right;
return std::pair<KType, VType>(temp->key, temp->value);
}
}