std::unique_ptr<bst_node>
left;
28std::unique_ptr<bst_node>
right;
42std::unique_ptr<bst_node>
root_;
56}
else if(!
node->right) {
57ret_value =
node->value;
74}
else if(!
node->left) {
75ret_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) {
104 if(!
node->right) {
106std::unique_ptr<bst_node>(
new bst_node(new_value));
125 bool remove(std::unique_ptr<bst_node>& parent,
126std::unique_ptr<bst_node>&
node, T rm_value) {
131 if(
node->value == rm_value) {
133T successor_node_value{};
136 node->value = successor_node_value;
138}
else if(
node->left ||
node->right) {
139std::unique_ptr<bst_node>& non_null =
143 root_= std::move(non_null);
144}
else if(rm_value < parent->value) {
145parent->left = std::move(non_null);
147parent->right = std::move(non_null);
153 root_.reset(
nullptr);
154}
else if(rm_value < parent->value) {
155parent->left.reset(
nullptr);
157parent->right.reset(
nullptr);
162}
else if(rm_value < node->value) {
182 if(value < node->value) {
184}
else if(value >
node->value) {
198std::unique_ptr<bst_node>&
node) {
204callback(
node->value);
215std::unique_ptr<bst_node>&
node) {
220callback(
node->value);
232std::unique_ptr<bst_node>&
node) {
239callback(
node->value);
322std::vector<T> result;
334std::vector<T> result;
346std::vector<T> result;
359std::cout <<
"Testing BST insert...";
362 boolres = tree.
insert(5);
363 intmin = -1, max = -1;
369assert(tree.
size() == 1);
378assert(tree.
size() == 4);
380 boolfail_res = tree.
insert(4);
382assert(tree.
size() == 4);
384std::cout <<
"ok"<< std::endl;
393std::cout <<
"Testing BST remove...";
401 boolres = tree.
remove(5);
402 intmin = -1, max = -1;
408assert(tree.
size() == 3);
409assert(tree.
contains(5) ==
false);
414assert(tree.
size() == 0);
415assert(tree.
contains(6) ==
false);
417 boolfail_res = tree.
remove(5);
419assert(tree.
size() == 0);
421std::cout <<
"ok"<< std::endl;
430std::cout <<
"Testing BST contains...";
444std::cout <<
"ok"<< std::endl;
453std::cout <<
"Testing BST find_min...";
467std::cout <<
"ok"<< std::endl;
476std::cout <<
"Testing BST find_max...";
490std::cout <<
"ok"<< std::endl;
499std::cout <<
"Testing BST get_elements_inorder...";
507std::vector<int> expected = {3, 4, 5, 6};
509assert(actual == expected);
511std::cout <<
"ok"<< std::endl;
520std::cout <<
"Testing BST get_elements_preorder...";
528std::vector<int> expected = {5, 4, 3, 6};
530assert(actual == expected);
532std::cout <<
"ok"<< std::endl;
541std::cout <<
"Testing BST get_elements_postorder...";
549std::vector<int> expected = {3, 4, 6, 5};
551assert(actual == expected);
553std::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.
int main()
Main function.
A struct to represent a node in the Binary Search Tree.
std::unique_ptr< bst_node > right
std::unique_ptr< bst_node > left
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4