TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
The Binary Search Tree class. More...
Classes | |
struct | bst_node |
A struct to represent a node in the Binary Search Tree. More... | |
Public Member Functions | |
binary_search_tree () | |
Construct a new Binary Search Tree object. | |
bool | insert (T new_value) |
Insert a new value into the BST. | |
bool | remove (T rm_value) |
Remove a specified value from the BST. | |
bool | contains (T value) |
Check if a value is in the BST. | |
bool | find_min (T &ret_value) |
Find the smallest value in the BST. | |
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_inorder () |
Get all values of the BST in in-order order. | |
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. | |
Private Member Functions | |
bool | find_max (std::unique_ptr< bst_node > &node, T &ret_value) |
Recursive function to find the maximum value in the BST. | |
bool | find_min (std::unique_ptr< bst_node > &node, T &ret_value) |
Recursive function to find the minimum value in the BST. | |
bool | insert (std::unique_ptr< bst_node > &node, T new_value) |
Recursive function to insert a value into 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 | contains (std::unique_ptr< bst_node > &node, T value) |
Recursive function to check if a value is in the BST. | |
void | traverse_inorder (std::function< void(T)> callback, std::unique_ptr< bst_node > &node) |
Recursive function to traverse the tree in in-order order. | |
void | traverse_preorder (std::function< void(T)> callback, std::unique_ptr< bst_node > &node) |
Recursive function to traverse the tree in pre-order order. | |
void | traverse_postorder (std::function< void(T)> callback, std::unique_ptr< bst_node > &node) |
Recursive function to traverse the tree in post-order order. | |
Private Attributes | |
std::unique_ptr< bst_node > | root_ |
std::size_t | size_ = 0 |
The Binary Search Tree class.
T | The type of the binary search tree key. |
Definition at line 20 of file binary_search_tree2.cpp.
|
inline |
Construct a new Binary Search Tree object.
Definition at line 247 of file binary_search_tree2.cpp.
|
inlineprivate |
Recursive function to check if a value is in the BST.
node | The node to search from. |
value | The value to find. |
Definition at line 177 of file binary_search_tree2.cpp.
|
inline |
Check if a value is in the BST.
value | The value to find. |
Definition at line 289 of file binary_search_tree2.cpp.
|
inlineprivate |
Recursive function to find the maximum value in the BST.
node | The node to search from. |
ret_value | Variable to hold the maximum value. |
Definition at line 53 of file binary_search_tree2.cpp.
|
inline |
Find the largest value in the BST.
ret_value | Variable to hold the maximum value. |
Definition at line 307 of file binary_search_tree2.cpp.
|
inlineprivate |
Recursive function to find the minimum value in the BST.
node | The node to search from. |
ret_value | Variable to hold the minimum value. |
Definition at line 71 of file binary_search_tree2.cpp.
|
inline |
Find the smallest value in the BST.
ret_value | Variable to hold the minimum value. |
Definition at line 298 of file binary_search_tree2.cpp.
|
inline |
Get all values of the BST in in-order order.
Definition at line 321 of file binary_search_tree2.cpp.
|
inline |
Get all values of the BST in post-order order.
Definition at line 345 of file binary_search_tree2.cpp.
|
inline |
Get all values of the BST in pre-order order.
Definition at line 333 of file binary_search_tree2.cpp.
|
inlineprivate |
Recursive function to insert a value into the BST.
node | The node to search from. |
new_value | The value to insert. |
Definition at line 90 of file binary_search_tree2.cpp.
|
inline |
Insert a new value into the BST.
new_value | The value to insert into the BST. |
Definition at line 259 of file binary_search_tree2.cpp.
|
inlineprivate |
Recursive function to remove a value from the BST.
parent | The parent node of node. |
node | The node to search from. |
rm_value | The value to remove. |
Definition at line 125 of file binary_search_tree2.cpp.
|
inline |
Remove a specified value from the BST.
rm_value | The value to remove. |
Definition at line 274 of file binary_search_tree2.cpp.
|
inline |
Get the number of values in the BST.
Definition at line 314 of file binary_search_tree2.cpp.
|
inlineprivate |
Recursive function to traverse the tree in in-order order.
callback | Function that is called when a value needs to processed. |
node | The node to traverse from. |
Definition at line 197 of file binary_search_tree2.cpp.
|
inlineprivate |
Recursive function to traverse the tree in post-order order.
callback | Function that is called when a value needs to processed. |
node | The node to traverse from. |
Definition at line 231 of file binary_search_tree2.cpp.
|
inlineprivate |
Recursive function to traverse the tree in pre-order order.
callback | Function that is called when a value needs to processed. |
node | The node to traverse from. |
Definition at line 214 of file binary_search_tree2.cpp.
|
private |
Pointer to the root of the BST.
Definition at line 42 of file binary_search_tree2.cpp.
|
private |
Number of elements/nodes in the BST.
Definition at line 43 of file binary_search_tree2.cpp.