Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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_noderoot_
 
std::size_t size_ = 0
 

Detailed Description

template<class T>
class binary_search_tree< T >

The Binary Search Tree class.

Template Parameters
TThe 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,
value 
)
inlineprivate

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

Parameters
nodeThe node to search from.
valueThe value to find.
Returns
true If the value was found in the BST.
false Otherwise.
176  {
177  if (!node) {
178  return false;
179  }
180 
181  if (value < node->value) {
182  return contains(node->left, value);
183  } else if (value > node->value) {
184  return contains(node->right, value);
185  } else {
186  return true;
187  }
188  }
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
Definition: avltree.cpp:13

◆ contains() [2/2]

template<class T >
bool binary_search_tree< T >::contains ( value)
inline

Check if a value is in the BST.

Parameters
valueThe value to find.
Returns
true If value is in the BST.
false Otherwise.
288 { return contains(root_, value); }
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
nodeThe node to search from.
ret_valueVariable to hold the maximum value.
Returns
true If the maximum value was successfully found.
false Otherwise.
52  {
53  if (!node) {
54  return false;
55  } else if (!node->right) {
56  ret_value = node->value;
57  return true;
58  }
59  return find_max(node->right, ret_value);
60  }
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_valueVariable to hold the maximum value.
Returns
true If maximum value was successfully found.
false Otherwise.
306 { 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
nodeThe node to search from.
ret_valueVariable to hold the minimum value.
Returns
true If the minimum value was successfully found.
false Otherwise.
70  {
71  if (!node) {
72  return false;
73  } else if (!node->left) {
74  ret_value = node->value;
75  return true;
76  }
77 
78  return find_min(node->left, ret_value);
79  }
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_valueVariable to hold the minimum value.
Returns
true If minimum value was successfully found.
false Otherwise.
297 { 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.
320  {
322  traverse_inorder([&](T node_value) { result.push_back(node_value); },
323  root_);
324  return result;
325  }
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
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
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.
344  {
346  traverse_postorder([&](T node_value) { result.push_back(node_value); },
347  root_);
348  return result;
349  }
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.
332  {
334  traverse_preorder([&](T node_value) { result.push_back(node_value); },
335  root_);
336  return result;
337  }
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,
new_value 
)
inlineprivate

Recursive function to insert a value into the BST.

Parameters
nodeThe node to search from.
new_valueThe value to insert.
Returns
true If the insert operation was successful.
false Otherwise.
89  {
90  if (root_ == node && !root_) {
91  root_ = std::unique_ptr<bst_node>(new bst_node(new_value));
92  return true;
93  }
94 
95  if (new_value < node->value) {
96  if (!node->left) {
97  node->left = std::unique_ptr<bst_node>(new bst_node(new_value));
98  return true;
99  } else {
100  return insert(node->left, new_value);
101  }
102  } else if (new_value > node->value) {
103  if (!node->right) {
104  node->right =
105  std::unique_ptr<bst_node>(new bst_node(new_value));
106  return true;
107  } else {
108  return insert(node->right, new_value);
109  }
110  } else {
111  return false;
112  }
113  }
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

◆ insert() [2/2]

template<class T >
bool binary_search_tree< T >::insert ( new_value)
inline

Insert a new value into the BST.

Parameters
new_valueThe value to insert into the BST.
Returns
true If the insertion was successful.
false Otherwise.
258  {
259  bool result = insert(root_, new_value);
260  if (result) {
261  size_++;
262  }
263  return result;
264  }
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,
rm_value 
)
inlineprivate

Recursive function to remove a value from the BST.

Parameters
parentThe parent node of node.
nodeThe node to search from.
rm_valueThe value to remove.
Returns
true If the removal operation was successful.
false Otherwise.
125  {
126  if (!node) {
127  return false;
128  }
129 
130  if (node->value == rm_value) {
131  if (node->left && node->right) {
132  T successor_node_value{};
133  find_max(node->left, successor_node_value);
134  remove(root_, root_, successor_node_value);
135  node->value = successor_node_value;
136  return true;
137  } else if (node->left || node->right) {
138  std::unique_ptr<bst_node>& non_null =
139  (node->left ? node->left : node->right);
140 
141  if (node == root_) {
142  root_ = std::move(non_null);
143  } else if (rm_value < parent->value) {
144  parent->left = std::move(non_null);
145  } else {
146  parent->right = std::move(non_null);
147  }
148 
149  return true;
150  } else {
151  if (node == root_) {
152  root_.reset(nullptr);
153  } else if (rm_value < parent->value) {
154  parent->left.reset(nullptr);
155  } else {
156  parent->right.reset(nullptr);
157  }
158 
159  return true;
160  }
161  } else if (rm_value < node->value) {
162  return remove(node, node->left, rm_value);
163  } else {
164  return remove(node, node->right, rm_value);
165  }
166  }
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
T move(T... args)

◆ remove() [2/2]

template<class T >
bool binary_search_tree< T >::remove ( rm_value)
inline

Remove a specified value from the BST.

Parameters
rm_valueThe value to remove.
Returns
true If the removal was successful.
false Otherwise.
273  {
274  bool result = remove(root_, root_, rm_value);
275  if (result) {
276  size_--;
277  }
278  return result;
279  }
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.
313 { 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
callbackFunction that is called when a value needs to processed.
nodeThe node to traverse from.
197  {
198  if (!node) {
199  return;
200  }
201 
202  traverse_inorder(callback, node->left);
203  callback(node->value);
204  traverse_inorder(callback, node->right);
205  }

◆ 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
callbackFunction that is called when a value needs to processed.
nodeThe node to traverse from.
231  {
232  if (!node) {
233  return;
234  }
235 
236  traverse_postorder(callback, node->left);
237  traverse_postorder(callback, node->right);
238  callback(node->value);
239  }

◆ 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
callbackFunction that is called when a value needs to processed.
nodeThe node to traverse from.
214  {
215  if (!node) {
216  return;
217  }
218 
219  callback(node->value);
220  traverse_preorder(callback, node->left);
221  traverse_preorder(callback, node->right);
222  }

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: