binary_search_tree2.cpp File Reference

A generic binary search tree implementation. More...

`#include <cassert>`
`#include <functional>`
`#include <iostream>`
`#include <memory>`
`#include <vector>`
Include dependency graph for binary_search_tree2.cpp:

## Classes

class | binary_search_tree< T >
| The Binary Search Tree class. More...
struct | binary_search_tree< T >::bst_node
| A struct to represent a node in the Binary Search Tree. More...

## Functions

static void | test_insert ()
| Function for testing insert(). More...
static void | test_remove ()
| Function for testing remove(). More...
static void | test_contains ()
| Function for testing contains(). More...
static void | test_find_min ()
| Function for testing find_min(). More...
static void | test_find_max ()
| Function for testing find_max(). More...
static void | test_get_elements_inorder ()
| Function for testing get_elements_inorder(). More...
static void | test_get_elements_preorder ()
| Function for testing get_elements_preorder(). More...
static void | test_get_elements_postorder ()
| Function for testing get_elements_postorder(). More...
int | main ()

## Detailed Description

A generic binary search tree implementation.

See also
  binary_search_tree.cpp

## Function Documentation

## test_contains()

static void test_contains | ( |  | ) |

static

Function for testing contains().

Returns
  `void`

{
std::cout << "Testing BST contains...";
binary_search_tree<int> tree;
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
assert(tree.contains(5));
assert(tree.contains(4));
assert(tree.contains(3));
assert(tree.contains(6));
assert(!tree.contains(999));
std::cout << "ok" << std::endl;
}
std::cout
binary_search_tree
The Binary Search Tree class.
Definition: binary_search_tree2.cpp:19
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
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
std::endl
T endl(T... args)
Here is the call graph for this function:

## test_find_max()

static void test_find_max | ( |  | ) |

static

Function for testing find_max().

Returns
  `void`

{
std::cout << "Testing BST find_max...";
int max = 0;
binary_search_tree<int> tree;
assert(!tree.find_max(max));
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
assert(tree.find_max(max));
assert(max == 6);
std::cout << "ok" << std::endl;
}
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
std::max
T max(T... args)
Here is the call graph for this function:

## test_find_min()

static void test_find_min | ( |  | ) |

static

Function for testing find_min().

Returns
  `void`

{
std::cout << "Testing BST find_min...";
int min = 0;
binary_search_tree<int> tree;
assert(!tree.find_min(min));
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
assert(tree.find_min(min));
assert(min == 3);
std::cout << "ok" << std::endl;
}
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
std::min
T min(T... args)
Here is the call graph for this function:

## test_get_elements_inorder()

static void test_get_elements_inorder | ( |  | ) |

static

Function for testing get_elements_inorder().

Returns
  `void`

{
std::cout << "Testing BST get_elements_inorder...";
binary_search_tree<int> tree;
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
std::vector<int> expected = {3, 4, 5, 6};
std::vector<int> actual = tree.get_elements_inorder();
assert(actual == expected);
std::cout << "ok" << std::endl;
}
binary_search_tree::get_elements_inorder
std::vector< T > get_elements_inorder()
Get all values of the BST in in-order order.
Definition: binary_search_tree2.cpp:320
std::vector< int >
Here is the call graph for this function:

## test_get_elements_postorder()

static void test_get_elements_postorder | ( |  | ) |

static

Function for testing get_elements_postorder().

Returns
  `void`

{
std::cout << "Testing BST get_elements_postorder...";
binary_search_tree<int> tree;
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
std::vector<int> expected = {3, 4, 6, 5};
std::vector<int> actual = tree.get_elements_postorder();
assert(actual == expected);
std::cout << "ok" << std::endl;
}
binary_search_tree::get_elements_postorder
std::vector< T > get_elements_postorder()
Get all values of the BST in post-order order.
Definition: binary_search_tree2.cpp:344
Here is the call graph for this function:

## test_get_elements_preorder()

static void test_get_elements_preorder | ( |  | ) |

static

Function for testing get_elements_preorder().

Returns
  `void`

{
std::cout << "Testing BST get_elements_preorder...";
binary_search_tree<int> tree;
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
std::vector<int> expected = {5, 4, 3, 6};
std::vector<int> actual = tree.get_elements_preorder();
assert(actual == expected);
std::cout << "ok" << std::endl;
}
binary_search_tree::get_elements_preorder
std::vector< T > get_elements_preorder()
Get all values of the BST in pre-order order.
Definition: binary_search_tree2.cpp:332
Here is the call graph for this function:

## test_insert()

static void test_insert | ( |  | ) |

static

Function for testing insert().

Returns
  `void`

{
std::cout << "Testing BST insert...";
binary_search_tree<int> tree;
bool res = tree.insert(5);
int min = -1, max = -1;
assert(res);
assert(tree.find_max(max));
assert(tree.find_min(min));
assert(max == 5);
assert(min == 5);
assert(tree.size() == 1);
tree.insert(4);
tree.insert(3);
tree.insert(6);
assert(tree.find_max(max));
assert(tree.find_min(min));
assert(max == 6);
assert(min == 3);
assert(tree.size() == 4);
bool fail_res = tree.insert(4);
assert(!fail_res);
assert(tree.size() == 4);
std::cout << "ok" << std::endl;
}
binary_search_tree::size
std::size_t size()
Get the number of values in the BST.
Definition: binary_search_tree2.cpp:313
Here is the call graph for this function:

## test_remove()

static void test_remove | ( |  | ) |

static

Function for testing remove().

Returns
  `void`

{
std::cout << "Testing BST remove...";
binary_search_tree<int> tree;
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(6);
bool res = tree.remove(5);
int min = -1, max = -1;
assert(res);
assert(tree.find_max(max));
assert(tree.find_min(min));
assert(max == 6);
assert(min == 3);
assert(tree.size() == 3);
assert(tree.contains(5) == false);
tree.remove(4);
tree.remove(3);
tree.remove(6);
assert(tree.size() == 0);
assert(tree.contains(6) == false);
bool fail_res = tree.remove(5);
assert(!fail_res);
assert(tree.size() == 0);
std::cout << "ok" << std::endl;
}
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
Here is the call graph for this function: