-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary_search_tree2.cpp
564 lines (494 loc) · 14.6 KB
/
binary_search_tree2.cpp
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/**
* @file
* @brief A generic [binary search tree](https://en.wikipedia.org/wiki/Binary_search_tree) implementation.
* @see binary_search_tree.cpp
*/
#include <cassert>
#include <functional>
#include <iostream>
#include <memory>
#include <vector>
/**
* @brief The Binary Search Tree class.
*
* @tparam T The type of the binary search tree key.
*/
template <class T>
class binary_search_tree {
private:
/**
* @brief A struct to represent a node in the Binary Search Tree.
*/
struct bst_node {
T value; /**< The value/key of the node. */
std::unique_ptr<bst_node> left; /**< Pointer to left subtree. */
std::unique_ptr<bst_node> right; /**< Pointer to right subtree. */
/**
* Constructor for bst_node, used to simplify node construction and
* smart pointer construction.
* @param _value The value of the constructed node.
*/
explicit bst_node(T _value) {
value = _value;
left = nullptr;
right = nullptr;
}
};
std::unique_ptr<bst_node> root_; /**< Pointer to the root of the BST. */
std::size_t size_ = 0; /**< Number of elements/nodes in the BST. */
/**
* @brief Recursive function to find the maximum value in the BST.
*
* @param node The node to search from.
* @param ret_value Variable to hold the maximum value.
* @return true If the maximum value was successfully found.
* @return false Otherwise.
*/
bool find_max(std::unique_ptr<bst_node>& node, T& ret_value) {
if (!node) {
return false;
} else if (!node->right) {
ret_value = node->value;
return true;
}
return find_max(node->right, ret_value);
}
/**
* @brief Recursive function to find the minimum value in the BST.
*
* @param node The node to search from.
* @param ret_value Variable to hold the minimum value.
* @return true If the minimum value was successfully found.
* @return false Otherwise.
*/
bool find_min(std::unique_ptr<bst_node>& node, T& ret_value) {
if (!node) {
return false;
} else if (!node->left) {
ret_value = node->value;
return true;
}
return find_min(node->left, ret_value);
}
/**
* @brief Recursive function to insert a value into the BST.
*
* @param node The node to search from.
* @param new_value The value to insert.
* @return true If the insert operation was successful.
* @return false Otherwise.
*/
bool insert(std::unique_ptr<bst_node>& node, T new_value) {
if (root_ == node && !root_) {
root_ = std::unique_ptr<bst_node>(new bst_node(new_value));
return true;
}
if (new_value < node->value) {
if (!node->left) {
node->left = std::unique_ptr<bst_node>(new bst_node(new_value));
return true;
} else {
return insert(node->left, new_value);
}
} else if (new_value > node->value) {
if (!node->right) {
node->right =
std::unique_ptr<bst_node>(new bst_node(new_value));
return true;
} else {
return insert(node->right, new_value);
}
} else {
return false;
}
}
/**
* @brief Recursive function to remove a value from the BST.
*
* @param parent The parent node of node.
* @param node The node to search from.
* @param rm_value The value to remove.
* @return true If the removal operation was successful.
* @return false Otherwise.
*/
bool remove(std::unique_ptr<bst_node>& parent,
std::unique_ptr<bst_node>& node, T rm_value) {
if (!node) {
return false;
}
if (node->value == rm_value) {
if (node->left && node->right) {
T successor_node_value{};
find_max(node->left, successor_node_value);
remove(root_, root_, successor_node_value);
node->value = successor_node_value;
return true;
} else if (node->left || node->right) {
std::unique_ptr<bst_node>& non_null =
(node->left ? node->left : node->right);
if (node == root_) {
root_ = std::move(non_null);
} else if (rm_value < parent->value) {
parent->left = std::move(non_null);
} else {
parent->right = std::move(non_null);
}
return true;
} else {
if (node == root_) {
root_.reset(nullptr);
} else if (rm_value < parent->value) {
parent->left.reset(nullptr);
} else {
parent->right.reset(nullptr);
}
return true;
}
} else if (rm_value < node->value) {
return remove(node, node->left, rm_value);
} else {
return remove(node, node->right, rm_value);
}
}
/**
* @brief Recursive function to check if a value is in the BST.
*
* @param node The node to search from.
* @param value The value to find.
* @return true If the value was found in the BST.
* @return false Otherwise.
*/
bool contains(std::unique_ptr<bst_node>& node, T value) {
if (!node) {
return false;
}
if (value < node->value) {
return contains(node->left, value);
} else if (value > node->value) {
return contains(node->right, value);
} else {
return true;
}
}
/**
* @brief Recursive function to traverse the tree in in-order order.
*
* @param callback Function that is called when a value needs to processed.
* @param node The node to traverse from.
*/
void traverse_inorder(std::function<void(T)> callback,
std::unique_ptr<bst_node>& node) {
if (!node) {
return;
}
traverse_inorder(callback, node->left);
callback(node->value);
traverse_inorder(callback, node->right);
}
/**
* @brief Recursive function to traverse the tree in pre-order order.
*
* @param callback Function that is called when a value needs to processed.
* @param node The node to traverse from.
*/
void traverse_preorder(std::function<void(T)> callback,
std::unique_ptr<bst_node>& node) {
if (!node) {
return;
}
callback(node->value);
traverse_preorder(callback, node->left);
traverse_preorder(callback, node->right);
}
/**
* @brief Recursive function to traverse the tree in post-order order.
*
* @param callback Function that is called when a value needs to processed.
* @param node The node to traverse from.
*/
void traverse_postorder(std::function<void(T)> callback,
std::unique_ptr<bst_node>& node) {
if (!node) {
return;
}
traverse_postorder(callback, node->left);
traverse_postorder(callback, node->right);
callback(node->value);
}
public:
/**
* @brief Construct a new Binary Search Tree object.
*
*/
binary_search_tree() {
root_ = nullptr;
size_ = 0;
}
/**
* @brief Insert a new value into the BST.
*
* @param new_value The value to insert into the BST.
* @return true If the insertion was successful.
* @return false Otherwise.
*/
bool insert(T new_value) {
bool result = insert(root_, new_value);
if (result) {
size_++;
}
return result;
}
/**
* @brief Remove a specified value from the BST.
*
* @param rm_value The value to remove.
* @return true If the removal was successful.
* @return false Otherwise.
*/
bool remove(T rm_value) {
bool result = remove(root_, root_, rm_value);
if (result) {
size_--;
}
return result;
}
/**
* @brief Check if a value is in the BST.
*
* @param value The value to find.
* @return true If value is in the BST.
* @return false Otherwise.
*/
bool contains(T value) { return contains(root_, value); }
/**
* @brief Find the smallest value in the BST.
*
* @param ret_value Variable to hold the minimum value.
* @return true If minimum value was successfully found.
* @return false Otherwise.
*/
bool find_min(T& ret_value) { return find_min(root_, ret_value); }
/**
* @brief Find the largest value in the BST.
*
* @param ret_value Variable to hold the maximum value.
* @return true If maximum value was successfully found.
* @return false Otherwise.
*/
bool find_max(T& ret_value) { return find_max(root_, ret_value); }
/**
* @brief Get the number of values in the BST.
*
* @return std::size_t Number of values in the BST.
*/
std::size_t size() { return size_; }
/**
* @brief Get all values of the BST in in-order order.
*
* @return std::vector<T> List of values, sorted in in-order order.
*/
std::vector<T> get_elements_inorder() {
std::vector<T> result;
traverse_inorder([&](T node_value) { result.push_back(node_value); },
root_);
return result;
}
/**
* @brief Get all values of the BST in pre-order order.
*
* @return std::vector<T> List of values, sorted in pre-order order.
*/
std::vector<T> get_elements_preorder() {
std::vector<T> result;
traverse_preorder([&](T node_value) { result.push_back(node_value); },
root_);
return result;
}
/**
* @brief Get all values of the BST in post-order order.
*
* @return std::vector<T> List of values, sorted in post-order order.
*/
std::vector<T> get_elements_postorder() {
std::vector<T> result;
traverse_postorder([&](T node_value) { result.push_back(node_value); },
root_);
return result;
}
};
/**
* @brief Function for testing insert().
*
* @returns `void`
*/
static void test_insert() {
std::cout << "Testing BST insert...";
binary_search_tree<int> tree;
bool res = tree.insert(5);
int min = -1, max = -1;
assert(res);
assert(tree.find_max(max));
assert(tree.find_min(min));
assert(max == 5);
assert(min == 5);
assert(tree.size() == 1);
tree.insert(4);
tree.insert(3);
tree.insert(6);
assert(tree.find_max(max));
assert(tree.find_min(min));
assert(max == 6);
assert(min == 3);
assert(tree.size() == 4);
bool fail_res = tree.insert(4);
assert(!fail_res);
assert(tree.size() == 4);
std::cout << "ok" << std::endl;
}
/**
* @brief Function for testing remove().
*
* @returns `void`
*/
static void test_remove() {
std::cout << "Testing BST remove...";
binary_search_tree<int> tree;
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
bool res = tree.remove(5);
int min = -1, max = -1;
assert(res);
assert(tree.find_max(max));
assert(tree.find_min(min));
assert(max == 6);
assert(min == 3);
assert(tree.size() == 3);
assert(tree.contains(5) == false);
tree.remove(4);
tree.remove(3);
tree.remove(6);
assert(tree.size() == 0);
assert(tree.contains(6) == false);
bool fail_res = tree.remove(5);
assert(!fail_res);
assert(tree.size() == 0);
std::cout << "ok" << std::endl;
}
/**
* @brief Function for testing contains().
*
* @returns `void`
*/
static void test_contains() {
std::cout << "Testing BST contains...";
binary_search_tree<int> tree;
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
assert(tree.contains(5));
assert(tree.contains(4));
assert(tree.contains(3));
assert(tree.contains(6));
assert(!tree.contains(999));
std::cout << "ok" << std::endl;
}
/**
* @brief Function for testing find_min().
*
* @returns `void`
*/
static void test_find_min() {
std::cout << "Testing BST find_min...";
int min = 0;
binary_search_tree<int> tree;
assert(!tree.find_min(min));
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
assert(tree.find_min(min));
assert(min == 3);
std::cout << "ok" << std::endl;
}
/**
* @brief Function for testing find_max().
*
* @returns `void`
*/
static void test_find_max() {
std::cout << "Testing BST find_max...";
int max = 0;
binary_search_tree<int> tree;
assert(!tree.find_max(max));
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
assert(tree.find_max(max));
assert(max == 6);
std::cout << "ok" << std::endl;
}
/**
* @brief Function for testing get_elements_inorder().
*
* @returns `void`
*/
static void test_get_elements_inorder() {
std::cout << "Testing BST get_elements_inorder...";
binary_search_tree<int> tree;
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
std::vector<int> expected = {3, 4, 5, 6};
std::vector<int> actual = tree.get_elements_inorder();
assert(actual == expected);
std::cout << "ok" << std::endl;
}
/**
* @brief Function for testing get_elements_preorder().
*
* @returns `void`
*/
static void test_get_elements_preorder() {
std::cout << "Testing BST get_elements_preorder...";
binary_search_tree<int> tree;
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
std::vector<int> expected = {5, 4, 3, 6};
std::vector<int> actual = tree.get_elements_preorder();
assert(actual == expected);
std::cout << "ok" << std::endl;
}
/**
* @brief Function for testing get_elements_postorder().
*
* @returns `void`
*/
static void test_get_elements_postorder() {
std::cout << "Testing BST get_elements_postorder...";
binary_search_tree<int> tree;
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
std::vector<int> expected = {3, 4, 6, 5};
std::vector<int> actual = tree.get_elements_postorder();
assert(actual == expected);
std::cout << "ok" << std::endl;
}
int main() {
test_insert();
test_remove();
test_contains();
test_find_max();
test_find_min();
test_get_elements_inorder();
test_get_elements_preorder();
test_get_elements_postorder();
}