Brumski's DSA Learning Project 1.3
For Learning Data Structures and Algorithms in C++
Loading...
Searching...
No Matches
binary_tree.hpp
1#include <memory>
2#include <iostream>
3
7template<class T>
8class BSTree {
9private:
13 template<class N>
14 class BSTNode {
15 public:
16 N data;
17 std::unique_ptr<BSTNode<N>> left;
18 std::unique_ptr<BSTNode<N>> right;
19 };
20public:
21 std::unique_ptr<BSTNode<T>> root;
22
27 virtual void insert(const T& value) {
28 std::unique_ptr<BSTNode<T>> node = std::make_unique<BSTNode<T>>();
29 node->data = value;
30
31 root = insertHelper(root, node);
32 }
33
38 virtual void display(const bool& ascOrder = true) {
39 displayHelper(root, ascOrder);
40 }
41
46 virtual bool search(const T& data){
47 return searchHelper(root, data);
48 }
49
50 virtual void remove() {
51
52 }
53
54private:
60 virtual std::unique_ptr<BSTNode<T>> insertHelper(std::unique_ptr<BSTNode<T>>& root, std::unique_ptr<BSTNode<T>>& node) {
61 T data = node->data;
62
63 if (root == nullptr) {
64 root = std::move(node);
65 return std::move(root);
66 }
67 else if (data < root->data) {
68 root->left = insertHelper(root->left, node);
69 }
70 else {
71 root->right = insertHelper(root->right, node);
72 }
73
74 return std::move(root);
75 }
76
82 static void displayHelper(std::unique_ptr<BSTNode<T>>& root, const bool& ascendingOrder = true) {
83 if(ascendingOrder) {
84 if (root) {
85 displayHelper(root->left);
86 std::cout << root->data << std::endl;
87 displayHelper(root->right);
88 }
89 }
90 else{
91 if (root) {
92 displayHelper(root->right, ascendingOrder);
93 std::cout << root->data << std::endl;
94 displayHelper(root->left, ascendingOrder);
95 }
96 }
97 }
98
104 virtual bool searchHelper(std::unique_ptr<BSTNode<T>>& root, const T& data) {
105 if(!root){
106 return false;
107 }
108 else if(root->data == data){
109 return true;
110 }
111 else if(root->data > data){
112 return searchHelper(root->left, data);
113 }
114 else if(root->data < data){
115 return searchHelper(root->right, data);
116 }
117
118 return false;
119 }
120
121 virtual void removeHelper() {
122
123 }
124
125 T successor(){
126
127 }
128
129 T predecessor(){
130
131 }
132};
133
134void UsingBST();
This is a template class that represents a node/element in a Binary Search Tree.
Definition binary_tree.hpp:14
N data
Holds the data of a single node/element.
Definition binary_tree.hpp:16
std::unique_ptr< BSTNode< N > > right
Pointer to right child.
Definition binary_tree.hpp:18
std::unique_ptr< BSTNode< N > > left
Pointer to left child.
Definition binary_tree.hpp:17
This is a template class that represents a Binary Search Tree.
Definition binary_tree.hpp:8
virtual bool search(const T &data)
Searches for an element in the binary search tree.
Definition binary_tree.hpp:46
virtual bool searchHelper(std::unique_ptr< BSTNode< T > > &root, const T &data)
Helper to BSTree<T>::search()
Definition binary_tree.hpp:104
static void displayHelper(std::unique_ptr< BSTNode< T > > &root, const bool &ascendingOrder=true)
Helper to BSTree<T>::display()
Definition binary_tree.hpp:82
virtual void insert(const T &value)
Inserts a value into the binary search tree.
Definition binary_tree.hpp:27
virtual std::unique_ptr< BSTNode< T > > insertHelper(std::unique_ptr< BSTNode< T > > &root, std::unique_ptr< BSTNode< T > > &node)
Helper to BSTree<T>::insert()
Definition binary_tree.hpp:60
virtual void display(const bool &ascOrder=true)
Displays all the values in the binary search tree.
Definition binary_tree.hpp:38