#include #include "stdafx.h" templatestruct NodeType; template struct NodeType { T key; std::shared_ptr left; std::shared_ptr right; NodeType(T); }; template NodeType::NodeType(T myItem){ key = myItem; left = nullptr; right = nullptr; } template struct BTS { std::shared_ptr>root; BTS(); bool insert(T); bool find(T x); bool erase(T x); NodeType& min(void); NodeType& max(void); }; template BTS::BTS() : root(nullptr) {} template bool BTS::insert(T val) { std::shared_ptr> temp = std::make_shared>(val); if (root == NULL) { root = temp; return true; } else { std::shared_ptr> current = root; std::shared_ptr> parent = root; current = (temp->key < current->key) ? (current->left) : (current->right); while (current != NULL) { parent = current; current = (temp->key < current->key) ? (current->left) : (current->right); } if (temp->key < parent->key) { parent->left = temp; return true; } else if (temp->key > parent->key) { parent->right = temp; return true; } return false; } } template bool BTS::find(T val) { if (root == NULL) { std::cout << "tree is empty, cannot find this value" << std::endl; return false; } else { std::shared_ptr> current = root; while (current != NULL) { if (current->key == val) { std::cout << "Found value: " + current->key << std::endl; return true; } current = (current->key > val) ? (current->left) : (current->right); } std::cout << "Cannot find this value:"<< std::endl; return false; } } template bool BTS::erase(T val) { if (root == NULL) { return false; } else { std::shared_ptr> current = root; std::shared_ptr> parent = root; std::shared_ptr> parent = root; current = (val < current->key) ? (current->left) : (current->right); while (current != NULL) { if (current->key == val) { if (parent->right == current) { parent->right = current->left; } else parent->left = current->right; current.reset(); } parent = current; current = (key < current->key) ? (current->left) : (current->right); } if (temp->key < parent->key) { parent->left = temp; return true; } else if (temp->key > parent->key) { parent->right = temp; return true; } return false; } } template NodeType & BTS::min(void) { std::shared_ptr> temp = root; if (root == NULL) throw ("sth wrong"); else { while (temp->left != NULL) { temp = temp->left; } return *temp; } } template NodeType & BTS::max(void) { std::shared_ptr> temp = root; if (root == NULL) throw("is not elemnt in the tree"); else { while (temp->right != NULL) { temp = temp->right; } std::cout << temp->key << std::endl; return *temp; } }