Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
binary_search_tree2.cpp File Reference

A generic binary search tree implementation. Here you can find more information about the algorithm: Scaler - Binary Search tree. 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().
 
static void test_remove ()
 Function for testing remove().
 
static void test_contains ()
 Function for testing contains().
 
static void test_find_min ()
 Function for testing find_min().
 
static void test_find_max ()
 Function for testing find_max().
 
static void test_get_elements_inorder ()
 Function for testing get_elements_inorder().
 
static void test_get_elements_preorder ()
 Function for testing get_elements_preorder().
 
static void test_get_elements_postorder ()
 Function for testing get_elements_postorder().
 
int main ()
 

Detailed Description

A generic binary search tree implementation. Here you can find more information about the algorithm: Scaler - Binary Search tree.

See also
binary_search_tree.cpp

Function Documentation

◆ main()

int main ( void )
556 {
557 test_insert();
558 test_remove();
565}
static void test_get_elements_inorder()
Function for testing get_elements_inorder().
Definition binary_search_tree2.cpp:498
static void test_contains()
Function for testing contains().
Definition binary_search_tree2.cpp:429
static void test_insert()
Function for testing insert().
Definition binary_search_tree2.cpp:358
static void test_get_elements_postorder()
Function for testing get_elements_postorder().
Definition binary_search_tree2.cpp:540
static void test_find_max()
Function for testing find_max().
Definition binary_search_tree2.cpp:475
static void test_get_elements_preorder()
Function for testing get_elements_preorder().
Definition binary_search_tree2.cpp:519
static void test_remove()
Function for testing remove().
Definition binary_search_tree2.cpp:392
static void test_find_min()
Function for testing find_min().
Definition binary_search_tree2.cpp:452

◆ test_contains()

static void test_contains ( )
static

Function for testing contains().

Returns
void
429 {
430 std::cout << "Testing BST contains...";
431
433 tree.insert(5);
434 tree.insert(4);
435 tree.insert(3);
436 tree.insert(6);
437
438 assert(tree.contains(5));
439 assert(tree.contains(4));
440 assert(tree.contains(3));
441 assert(tree.contains(6));
442 assert(!tree.contains(999));
443
444 std::cout << "ok" << std::endl;
445}
The Binary Search Tree class.
Definition binary_search_tree2.cpp:20
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:90
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:177
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
475 {
476 std::cout << "Testing BST find_max...";
477
478 int max = 0;
480 assert(!tree.find_max(max));
481
482 tree.insert(5);
483 tree.insert(4);
484 tree.insert(3);
485 tree.insert(6);
486
487 assert(tree.find_max(max));
488 assert(max == 6);
489
490 std::cout << "ok" << std::endl;
491}
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:53
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
452 {
453 std::cout << "Testing BST find_min...";
454
455 int min = 0;
457 assert(!tree.find_min(min));
458
459 tree.insert(5);
460 tree.insert(4);
461 tree.insert(3);
462 tree.insert(6);
463
464 assert(tree.find_min(min));
465 assert(min == 3);
466
467 std::cout << "ok" << std::endl;
468}
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:71
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
498 {
499 std::cout << "Testing BST get_elements_inorder...";
500
502 tree.insert(5);
503 tree.insert(4);
504 tree.insert(3);
505 tree.insert(6);
506
507 std::vector<int> expected = {3, 4, 5, 6};
509 assert(actual == expected);
510
511 std::cout << "ok" << std::endl;
512}
std::vector< T > get_elements_inorder()
Get all values of the BST in in-order order.
Definition binary_search_tree2.cpp:321
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
540 {
541 std::cout << "Testing BST get_elements_postorder...";
542
544 tree.insert(5);
545 tree.insert(4);
546 tree.insert(3);
547 tree.insert(6);
548
549 std::vector<int> expected = {3, 4, 6, 5};
551 assert(actual == expected);
552
553 std::cout << "ok" << std::endl;
554}
std::vector< T > get_elements_postorder()
Get all values of the BST in post-order order.
Definition binary_search_tree2.cpp:345
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
519 {
520 std::cout << "Testing BST get_elements_preorder...";
521
523 tree.insert(5);
524 tree.insert(4);
525 tree.insert(3);
526 tree.insert(6);
527
528 std::vector<int> expected = {5, 4, 3, 6};
530 assert(actual == expected);
531
532 std::cout << "ok" << std::endl;
533}
std::vector< T > get_elements_preorder()
Get all values of the BST in pre-order order.
Definition binary_search_tree2.cpp:333
Here is the call graph for this function:

◆ test_insert()

static void test_insert ( )
static

Function for testing insert().

Returns
void
358 {
359 std::cout << "Testing BST insert...";
360
362 bool res = tree.insert(5);
363 int min = -1, max = -1;
364 assert(res);
365 assert(tree.find_max(max));
366 assert(tree.find_min(min));
367 assert(max == 5);
368 assert(min == 5);
369 assert(tree.size() == 1);
370
371 tree.insert(4);
372 tree.insert(3);
373 tree.insert(6);
374 assert(tree.find_max(max));
375 assert(tree.find_min(min));
376 assert(max == 6);
377 assert(min == 3);
378 assert(tree.size() == 4);
379
380 bool fail_res = tree.insert(4);
381 assert(!fail_res);
382 assert(tree.size() == 4);
383
384 std::cout << "ok" << std::endl;
385}
std::size_t size()
Get the number of values in the BST.
Definition binary_search_tree2.cpp:314
Here is the call graph for this function:

◆ test_remove()

static void test_remove ( )
static

Function for testing remove().

Returns
void
392 {
393 std::cout << "Testing BST remove...";
394
396 tree.insert(5);
397 tree.insert(4);
398 tree.insert(3);
399 tree.insert(6);
400
401 bool res = tree.remove(5);
402 int min = -1, max = -1;
403 assert(res);
404 assert(tree.find_max(max));
405 assert(tree.find_min(min));
406 assert(max == 6);
407 assert(min == 3);
408 assert(tree.size() == 3);
409 assert(tree.contains(5) == false);
410
411 tree.remove(4);
412 tree.remove(3);
413 tree.remove(6);
414 assert(tree.size() == 0);
415 assert(tree.contains(6) == false);
416
417 bool fail_res = tree.remove(5);
418 assert(!fail_res);
419 assert(tree.size() == 0);
420
421 std::cout << "ok" << std::endl;
422}
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:125
Here is the call graph for this function: