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>
Go to the source code of this file.
A generic binary search tree implementation. Here you can find more information about the algorithm: Scaler - Binary Search tree.
Definition in file binary_search_tree2.cpp.
◆ main()Definition at line 556 of file binary_search_tree2.cpp.
556 {
565}
static void test_get_elements_inorder()
Function for testing get_elements_inorder().
static void test_contains()
Function for testing contains().
static void test_insert()
Function for testing insert().
static void test_get_elements_postorder()
Function for testing get_elements_postorder().
static void test_find_max()
Function for testing find_max().
static void test_get_elements_preorder()
Function for testing get_elements_preorder().
static void test_remove()
Function for testing remove().
static void test_find_min()
Function for testing find_min().
◆ test_contains()Function for testing contains().
Definition at line 429 of file binary_search_tree2.cpp.
429 {
430 std::cout << "Testing BST contains...";
431
437
443
444 std::cout << "ok" << std::endl;
445}
The Binary Search Tree class.
bool insert(std::unique_ptr< bst_node > &node, T new_value)
Recursive function to insert a value into the BST.
bool contains(std::unique_ptr< bst_node > &node, T value)
Recursive function to check if a value is in the BST.
◆ test_find_max()Function for testing find_max().
Definition at line 475 of file binary_search_tree2.cpp.
475 {
476 std::cout << "Testing BST find_max...";
477
478 int max = 0;
481
486
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.
◆ test_find_min()Function for testing find_min().
Definition at line 452 of file binary_search_tree2.cpp.
452 {
453 std::cout << "Testing BST find_min...";
454
455 int min = 0;
458
463
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.
◆ test_get_elements_inorder() void test_get_elements_inorder ( ) staticFunction for testing get_elements_inorder().
Definition at line 498 of file binary_search_tree2.cpp.
498 {
499 std::cout << "Testing BST get_elements_inorder...";
500
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.
◆ test_get_elements_postorder() void test_get_elements_postorder ( ) staticFunction for testing get_elements_postorder().
Definition at line 540 of file binary_search_tree2.cpp.
540 {
541 std::cout << "Testing BST get_elements_postorder...";
542
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.
◆ test_get_elements_preorder() void test_get_elements_preorder ( ) staticFunction for testing get_elements_preorder().
Definition at line 519 of file binary_search_tree2.cpp.
519 {
520 std::cout << "Testing BST get_elements_preorder...";
521
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.
◆ test_insert()Function for testing insert().
Definition at line 358 of file binary_search_tree2.cpp.
358 {
359 std::cout << "Testing BST insert...";
360
362 boolres = tree.
insert(5);
363 int min = -1, max = -1;
364 assert(res);
367 assert(max == 5);
368 assert(min == 5);
369assert(tree.
size() == 1);
370
376 assert(max == 6);
377 assert(min == 3);
378assert(tree.
size() == 4);
379
380 boolfail_res = tree.
insert(4);
381 assert(!fail_res);
382assert(tree.
size() == 4);
383
384 std::cout << "ok" << std::endl;
385}
std::size_t size()
Get the number of values in the BST.
◆ test_remove()Function for testing remove().
Definition at line 392 of file binary_search_tree2.cpp.
392 {
393 std::cout << "Testing BST remove...";
394
400
401 boolres = tree.
remove(5);
402 int min = -1, max = -1;
403 assert(res);
406 assert(max == 6);
407 assert(min == 3);
408assert(tree.
size() == 3);
409assert(tree.
contains(5) ==
false);
410
414assert(tree.
size() == 0);
415assert(tree.
contains(6) ==
false);
416
417 boolfail_res = tree.
remove(5);
418 assert(!fail_res);
419assert(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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4