binary_search_tree< T > Class Template Reference

The Binary Search Tree class. More...

Collaboration diagram for binary_search_tree< T >:
[legend]

## 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. More...
bool | remove (T rm_value)
| Remove a specified value from the BST. More...
bool | contains (T value)
| Check if a value is in the BST. More...
bool | find_min (T &ret_value)
| Find the smallest value in the BST. More...
bool | find_max (T &ret_value)
| Find the largest value in the BST. More...
std::size_t | size ()
| Get the number of values in the BST. More...
std::vector< T > | get_elements_inorder ()
| Get all values of the BST in in-order order. More...
std::vector< T > | get_elements_preorder ()
| Get all values of the BST in pre-order order. More...
std::vector< T > | get_elements_postorder ()
| Get all values of the BST in post-order order. More...

## 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. More...
bool | find_min (std::unique_ptr< bst_node > &node, T &ret_value)
| Recursive function to find the minimum value in the BST. More...
bool | insert (std::unique_ptr< bst_node > &node, T new_value)
| Recursive function to insert a value into the BST. More...
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. More...
bool | contains (std::unique_ptr< bst_node > &node, T value)
| Recursive function to check if a value is in the BST. More...
void | traverse_inorder (std::function< void(T)> callback, std::unique_ptr< bst_node > &node)
| Recursive function to traverse the tree in in-order order. More...
void | traverse_preorder (std::function< void(T)> callback, std::unique_ptr< bst_node > &node)
| Recursive function to traverse the tree in pre-order order. More...
void | traverse_postorder (std::function< void(T)> callback, std::unique_ptr< bst_node > &node)
| Recursive function to traverse the tree in post-order order. More...

## Private Attributes

std::unique_ptr< bst_node > | root_
std::size_t | size_ = 0

## Detailed Description

### template<class T>
class binary_search_tree< T >

The Binary Search Tree class.

Template Parameters

  T | The type of the binary search tree key.

## Member Function Documentation

## contains() [1/2]

template<class T >

bool binary_search_tree< T >::contains | ( | std::unique_ptr< bst_node > & | node,
|  | T | value
| ) |  |

inlineprivate

Recursive function to check if a value is in the BST.

Parameters

  node | The node to search from.
  value | The value to find.

Returns
  true If the value was found in the BST.
  false Otherwise.

{
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;
}
}
binary_search_tree::contains
bool contains(std::unique_ptr< bst_node > &node, T value)
Recursive function to check if a value is in the BST.
Definition: binary_search_tree2.cpp:176
node
Definition: avltree.cpp:13

## contains() [2/2]

template<class T >

bool binary_search_tree< T >::contains | ( | T | value | ) |

inline

Check if a value is in the BST.

Parameters

  value | The value to find.

Returns
  true If value is in the BST.
  false Otherwise.

{ return contains(root_, value); }
binary_search_tree::root_
std::unique_ptr< bst_node > root_
Definition: binary_search_tree2.cpp:41
Here is the call graph for this function:

## find_max() [1/2]

template<class T >

bool binary_search_tree< T >::find_max | ( | std::unique_ptr< bst_node > & | node,
|  | T & | ret_value
| ) |  |

inlineprivate

Recursive function to find the maximum value in the BST.

Parameters

  node | The node to search from.
  ret_value | Variable to hold the maximum value.

Returns
  true If the maximum value was successfully found.
  false Otherwise.

{
if (!node) {
return false;
} else if (!node->right) {
ret_value = node->value;
return true;
}
return find_max(node->right, ret_value);
}
binary_search_tree::find_max
bool find_max(std::unique_ptr< bst_node > &node, T &ret_value)
Recursive function to find the maximum value in the BST.
Definition: binary_search_tree2.cpp:52

## find_max() [2/2]

template<class T >

bool binary_search_tree< T >::find_max | ( | T & | ret_value | ) |

inline

Find the largest value in the BST.

Parameters

  ret_value | Variable to hold the maximum value.

Returns
  true If maximum value was successfully found.
  false Otherwise.

{ return find_max(root_, ret_value); }
Here is the call graph for this function:

## find_min() [1/2]

template<class T >

bool binary_search_tree< T >::find_min | ( | std::unique_ptr< bst_node > & | node,
|  | T & | ret_value
| ) |  |

inlineprivate

Recursive function to find the minimum value in the BST.

Parameters

  node | The node to search from.
  ret_value | Variable to hold the minimum value.

Returns
  true If the minimum value was successfully found.
  false Otherwise.

{
if (!node) {
return false;
} else if (!node->left) {
ret_value = node->value;
return true;
}
return find_min(node->left, ret_value);
}
binary_search_tree::find_min
bool find_min(std::unique_ptr< bst_node > &node, T &ret_value)
Recursive function to find the minimum value in the BST.
Definition: binary_search_tree2.cpp:70

## find_min() [2/2]

template<class T >

bool binary_search_tree< T >::find_min | ( | T & | ret_value | ) |

inline

Find the smallest value in the BST.

Parameters

  ret_value | Variable to hold the minimum value.

Returns
  true If minimum value was successfully found.
  false Otherwise.

{ return find_min(root_, ret_value); }
Here is the call graph for this function:

## get_elements_inorder()

template<class T >

std::vector<T> binary_search_tree< T >::get_elements_inorder | ( |  | ) |

inline

Get all values of the BST in in-order order.

Returns
  std::vector<T> List of values, sorted in in-order order.

{
std::vector<T> result;
traverse_inorder([&](T node_value) { result.push_back(node_value); },
root_);
return result;
}
binary_search_tree::traverse_inorder
void traverse_inorder(std::function< void(T)> callback, std::unique_ptr< bst_node > &node)
Recursive function to traverse the tree in in-order order.
Definition: binary_search_tree2.cpp:196
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
std::vector
Here is the call graph for this function:

## get_elements_postorder()

template<class T >

std::vector<T> binary_search_tree< T >::get_elements_postorder | ( |  | ) |

inline

Get all values of the BST in post-order order.

Returns
  std::vector<T> List of values, sorted in post-order order.

{
std::vector<T> result;
traverse_postorder([&](T node_value) { result.push_back(node_value); },
root_);
return result;
}
binary_search_tree::traverse_postorder
void traverse_postorder(std::function< void(T)> callback, std::unique_ptr< bst_node > &node)
Recursive function to traverse the tree in post-order order.
Definition: binary_search_tree2.cpp:230
Here is the call graph for this function:

## get_elements_preorder()

template<class T >

std::vector<T> binary_search_tree< T >::get_elements_preorder | ( |  | ) |

inline

Get all values of the BST in pre-order order.

Returns
  std::vector<T> List of values, sorted in pre-order order.

{
std::vector<T> result;
traverse_preorder([&](T node_value) { result.push_back(node_value); },
root_);
return result;
}
binary_search_tree::traverse_preorder
void traverse_preorder(std::function< void(T)> callback, std::unique_ptr< bst_node > &node)
Recursive function to traverse the tree in pre-order order.
Definition: binary_search_tree2.cpp:213
Here is the call graph for this function:

## insert() [1/2]

template<class T >

bool binary_search_tree< T >::insert | ( | std::unique_ptr< bst_node > & | node,
|  | T | new_value
| ) |  |

inlineprivate

Recursive function to insert a value into the BST.

Parameters

  node | The node to search from.
  new_value | The value to insert.

Returns
  true If the insert operation was successful.
  false Otherwise.

{
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;
}
}
binary_search_tree::insert
bool insert(std::unique_ptr< bst_node > &node, T new_value)
Recursive function to insert a value into the BST.
Definition: binary_search_tree2.cpp:89
std::unique_ptr

## insert() [2/2]

template<class T >

bool binary_search_tree< T >::insert | ( | T | new_value | ) |

inline

Insert a new value into the BST.

Parameters

  new_value | The value to insert into the BST.

Returns
  true If the insertion was successful.
  false Otherwise.

{
bool result = insert(root_, new_value);
if (result) {
size_++;
}
return result;
}
binary_search_tree::size_
std::size_t size_
Definition: binary_search_tree2.cpp:42
Here is the call graph for this function:

## remove() [1/2]

template<class T >

bool binary_search_tree< T >::remove | ( | std::unique_ptr< bst_node > & | parent,
|  | std::unique_ptr< bst_node > & | node,
|  | T | rm_value
| ) |  |

inlineprivate

Recursive function to remove a value from the BST.

Parameters

  parent | The parent node of node.
  node | The node to search from.
  rm_value | The value to remove.

Returns
  true If the removal operation was successful.
  false Otherwise.

{
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);
}
}
binary_search_tree::remove
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.
Definition: binary_search_tree2.cpp:124
std::move
T move(T... args)

## remove() [2/2]

template<class T >

bool binary_search_tree< T >::remove | ( | T | rm_value | ) |

inline

Remove a specified value from the BST.

Parameters

  rm_value | The value to remove.

Returns
  true If the removal was successful.
  false Otherwise.

{
bool result = remove(root_, root_, rm_value);
if (result) {
size_--;
}
return result;
}
Here is the call graph for this function:

## size()

template<class T >

std::size_t binary_search_tree< T >::size | ( |  | ) |

inline

Get the number of values in the BST.

Returns
  std::size_t Number of values in the BST.

{ return size_; }

## traverse_inorder()

template<class T >

void binary_search_tree< T >::traverse_inorder | ( | std::function< void(T)> | callback,
|  | std::unique_ptr< bst_node > & | node
| ) |  |

inlineprivate

Recursive function to traverse the tree in in-order order.

Parameters

  callback | Function that is called when a value needs to processed.
  node | The node to traverse from.

{
if (!node) {
return;
}
traverse_inorder(callback, node->left);
callback(node->value);
traverse_inorder(callback, node->right);
}

## traverse_postorder()

template<class T >

void binary_search_tree< T >::traverse_postorder | ( | std::function< void(T)> | callback,
|  | std::unique_ptr< bst_node > & | node
| ) |  |

inlineprivate

Recursive function to traverse the tree in post-order order.

Parameters

  callback | Function that is called when a value needs to processed.
  node | The node to traverse from.

{
if (!node) {
return;
}
traverse_postorder(callback, node->left);
traverse_postorder(callback, node->right);
callback(node->value);
}

## traverse_preorder()

template<class T >

void binary_search_tree< T >::traverse_preorder | ( | std::function< void(T)> | callback,
|  | std::unique_ptr< bst_node > & | node
| ) |  |

inlineprivate

Recursive function to traverse the tree in pre-order order.

Parameters

  callback | Function that is called when a value needs to processed.
  node | The node to traverse from.

{
if (!node) {
return;
}
callback(node->value);
traverse_preorder(callback, node->left);
traverse_preorder(callback, node->right);
}

## Member Data Documentation

## root_

template<class T >

std::unique_ptr<bst_node> binary_search_tree< T >::root_

private

Pointer to the root of the BST.

## size_

template<class T >

std::size_t binary_search_tree< T >::size_ = 0

private

Number of elements/nodes in the BST.

The documentation for this class was generated from the following file:

- data_structures/binary_search_tree2.cpp