Uploaded by Tiahna Padilla

BuildBinarySearchTree2024 T347C453

advertisement
// BuildBinarySearchTree2024_T347C453.cpp
//Tiahna Padilla
#include <iostream>
template <typename T>
struct Node {
T data;
Node<T>* left;
Node<T>* right;
};
template <typename T>
class BinarySearchTree {
private:
Node<T>* root;
int count;
Node<T>* insert(Node<T>* node, T value) {
if (node == nullptr) {
node = new Node<T>();
node->data = value;
node->left = node->right = nullptr;
} else if (value < node->data) {
node->left = insert(node->left, value);
} else {
node->right = insert(node->right, value);
}
return node;
}
Node<T>* findMinNode(Node<T>* node) {
while (node->left != nullptr) {
node = node->left;
}
return node;
}
Node<T>* erase(Node<T>* node, T value) {
if (node == nullptr) {
return node;
} else if (value < node->data) {
node->left = erase(node->left, value);
} else if (value > node->data) {
node->right = erase(node->right, value);
} else {
if (node->left == nullptr && node->right == nullptr) {
delete node;
node = nullptr;
} else if (node->left == nullptr) {
Node<T>* temp = node;
node = node->right;
delete temp;
} else if (node->right == nullptr) {
Node<T>* temp = node;
node = node->left;
delete temp;
} else {
Node<T>* temp = findMinNode(node->right);
node->data = temp->data;
node->right = erase(node->right, temp->data);
}
}
return node;
}
void inorder(Node<T>* node) const {
if (node != nullptr) {
inorder(node->left);
std::cout << node->data << " ";
inorder(node->right);
}
}
void preorder(Node<T>* node) const {
if (node != nullptr) {
std::cout << node->data << " ";
preorder(node->left);
preorder(node->right);
}
}
void postorder(Node<T>* node) const {
if (node != nullptr) {
postorder(node->left);
postorder(node->right);
std::cout << node->data << " ";
}
}
bool search(Node<T>* node, T value) const {
if (node == nullptr) {
return false;
} else if (value < node->data) {
return search(node->left, value);
} else if (value > node->data) {
return search(node->right, value);
} else {
return true;
}
}
public:
BinarySearchTree() {
root = nullptr;
count = 0;
}
~BinarySearchTree() {
root = erase(root, root->data);
}
void insert(T value) {
root = insert(root, value);
count++;
}
bool search(T value) const {
return search(root, value);
}
void inorder() const {
inorder(root);
std::cout << std::endl;
}
void preorder() const {
preorder(root);
std::cout << std::endl;
}
void postorder() const {
postorder(root);
std::cout << std::endl;
}
Node<T>* getRoot() {
return root;
}
void erase(T value) {
root = erase(root, value);
count--;
}
};
// TODO: use the code from main() to test your solution.
int main() {
BinarySearchTree<int> bst;
bst.insert(5);
bst.insert(3);
bst.insert(7);
bst.insert(2);
bst.insert(4);
bst.insert(6);
bst.insert(8);
bst.insert(1);
std::cout << "Inorder Traversal: ";
bst.inorder(); // change this line
std::cout << "Preorder Traversal: ";
bst.preorder();
std::cout << "Postorder Traversal: ";
bst.postorder();
bst.erase(3);
std::cout << "Inorder Traversal after erasing 3: ";
bst.inorder(); // and this line
int searchValue = 5;
bool found = bst.search(searchValue);
if (found) {
std::cout << "Value " << searchValue << " found in the tree." << std::endl;
} else {
std::cout << "Value " << searchValue << " not found in the tree." << std::endl;
}
bst.erase(5);
std::cout << "Inorder Traversal after erasing 5: ";
bst.inorder(); // and this line
return 0;
}
Download