27 std::unique_ptr<bst_node>
left;
28 std::unique_ptr<bst_node>
right;
42 std::unique_ptr<bst_node>
root_;
56 }
else if (!
node->right) {
57 ret_value =
node->value;
74 }
else if (!
node->left) {
75 ret_value =
node->value;
90 bool insert(std::unique_ptr<bst_node>&
node, T new_value) {
96 if (new_value < node->value) {
98 node->left = std::unique_ptr<bst_node>(
new bst_node(new_value));
103 }
else if (new_value >
node->value) {
106 std::unique_ptr<bst_node>(
new bst_node(new_value));
125 bool remove(std::unique_ptr<bst_node>& parent,
126 std::unique_ptr<bst_node>&
node, T rm_value) {
131 if (
node->value == rm_value) {
133 T successor_node_value{};
136 node->value = successor_node_value;
138 }
else if (
node->left ||
node->right) {
139 std::unique_ptr<bst_node>& non_null =
143 root_ = std::move(non_null);
144 }
else if (rm_value < parent->value) {
145 parent->left = std::move(non_null);
147 parent->right = std::move(non_null);
153 root_.reset(
nullptr);
154 }
else if (rm_value < parent->value) {
155 parent->left.reset(
nullptr);
157 parent->right.reset(
nullptr);
162 }
else if (rm_value < node->value) {
182 if (value < node->value) {
184 }
else if (value >
node->value) {
198 std::unique_ptr<bst_node>&
node) {
204 callback(
node->value);
215 std::unique_ptr<bst_node>&
node) {
220 callback(
node->value);
232 std::unique_ptr<bst_node>&
node) {
239 callback(
node->value);
322 std::vector<T> result;
334 std::vector<T> result;
346 std::vector<T> result;
359 std::cout <<
"Testing BST insert...";
362 bool res = tree.
insert(5);
363 int min = -1, max = -1;
369 assert(tree.
size() == 1);
378 assert(tree.
size() == 4);
380 bool fail_res = tree.
insert(4);
382 assert(tree.
size() == 4);
384 std::cout <<
"ok" << std::endl;
393 std::cout <<
"Testing BST remove...";
401 bool res = tree.
remove(5);
402 int min = -1, max = -1;
408 assert(tree.
size() == 3);
414 assert(tree.
size() == 0);
417 bool fail_res = tree.
remove(5);
419 assert(tree.
size() == 0);
421 std::cout <<
"ok" << std::endl;
430 std::cout <<
"Testing BST contains...";
444 std::cout <<
"ok" << std::endl;
453 std::cout <<
"Testing BST find_min...";
467 std::cout <<
"ok" << std::endl;
476 std::cout <<
"Testing BST find_max...";
490 std::cout <<
"ok" << std::endl;
499 std::cout <<
"Testing BST get_elements_inorder...";
507 std::vector<int> expected = {3, 4, 5, 6};
509 assert(actual == expected);
511 std::cout <<
"ok" << std::endl;
520 std::cout <<
"Testing BST get_elements_preorder...";
528 std::vector<int> expected = {5, 4, 3, 6};
530 assert(actual == expected);
532 std::cout <<
"ok" << std::endl;
541 std::cout <<
"Testing BST get_elements_postorder...";
549 std::vector<int> expected = {3, 4, 6, 5};
551 assert(actual == expected);
553 std::cout <<
"ok" << std::endl;
static void test_get_elements_inorder()
Function for testing get_elements_inorder().
static void test_contains()
Function for testing contains().
static void test_insert()
Function for testing insert().
static void test_get_elements_postorder()
Function for testing get_elements_postorder().
static void test_find_max()
Function for testing find_max().
static void test_get_elements_preorder()
Function for testing get_elements_preorder().
static void test_remove()
Function for testing remove().
static void test_find_min()
Function for testing find_min().
The Binary Search Tree class.
std::vector< T > get_elements_inorder()
Get all values of the BST in in-order order.
void traverse_inorder(std::function< void(T)> callback, std::unique_ptr< bst_node > &node)
Recursive function to traverse the tree in in-order order.
bool find_max(T &ret_value)
Find the largest value in the BST.
std::size_t size()
Get the number of values in the BST.
std::vector< T > get_elements_preorder()
Get all values of the BST in pre-order order.
std::vector< T > get_elements_postorder()
Get all values of the BST in post-order order.
bool contains(T value)
Check if a value is in the BST.
bool find_max(std::unique_ptr< bst_node > &node, T &ret_value)
Recursive function to find the maximum value in the BST.
bool insert(T new_value)
Insert a new value into the BST.
void traverse_postorder(std::function< void(T)> callback, std::unique_ptr< bst_node > &node)
Recursive function to traverse the tree in post-order order.
bool remove(T rm_value)
Remove a specified value from the BST.
bool insert(std::unique_ptr< bst_node > &node, T new_value)
Recursive function to insert a value into the BST.
std::unique_ptr< bst_node > root_
bool contains(std::unique_ptr< bst_node > &node, T value)
Recursive function to check if a value is in the BST.
binary_search_tree()
Construct a new Binary Search Tree object.
void traverse_preorder(std::function< void(T)> callback, std::unique_ptr< bst_node > &node)
Recursive function to traverse the tree in pre-order order.
bool find_min(T &ret_value)
Find the smallest value in the BST.
bool remove(std::unique_ptr< bst_node > &parent, std::unique_ptr< bst_node > &node, T rm_value)
Recursive function to remove a value from the BST.
bool find_min(std::unique_ptr< bst_node > &node, T &ret_value)
Recursive function to find the minimum value in the BST.
A struct to represent a node in the Binary Search Tree.
std::unique_ptr< bst_node > right
std::unique_ptr< bst_node > left