#include #include using namespace std; template struct Node { T value; Node *left; Node *right; Node(T val) { this->value = val; } Node(T val, Node left, Node right) { this->value = val; this->left = left; this->right = right; } }; template class BST { private: Node *root; void addHelper(Node *root, T val) { if (root->value > val) { if (!root->left) { root->left = new Node(val); } else { addHelper(root->left, val); } } else { if (!root->right) { root->right = new Node(val); } else { addHelper(root->right, val); } } } void printHelper(Node *root) { if (!root) return; printHelper(root->left); cout<value<<' '; printHelper(root->right); } int nodesCountHelper(Node *root) { if (!root) return 0; else return 1 + nodesCountHelper(root->left) + nodesCountHelper(root->right); } int heightHelper(Node *root) { if (!root) return 0; else return 1 + max(heightHelper(root->left), heightHelper(root->right)); } void printMaxPathHelper(Node *root) { if (!root) return; cout<value<<' '; if (heightHelper(root->left) > heightHelper(root->right)) { printMaxPathHelper(root->left); } else { printMaxPathHelper(root->right); } } bool deleteValueHelper(Node* parent, Node* current, T value) { if (!current) return false; if (current->value == value) { if (current->left == NULL || current->right == NULL) { Node* temp = current->left; if (current->right) temp = current->right; if (parent) { if (parent->left == current) { parent->left = temp; } else { parent->right = temp; } } else { this->root = temp; } } else { Node* validSubs = current->right; while (validSubs->left) { validSubs = validSubs->left; } T temp = current->value; current->value = validSubs->value; validSubs->value = temp; return deleteValueHelper(current, current->right, temp); } delete current; return true; } return deleteValueHelper(current, current->left, value) || deleteValueHelper(current, current->right, value); } public: void add(T val) { if (root) { this->addHelper(root, val); } else { root = new Node(val); } } void print() { printHelper(this->root); } int nodesCount() { return nodesCountHelper(root); } int height() { return heightHelper(this->root); } void printMaxPath() { printMaxPathHelper(this->root); } bool deleteValue(T value) { return this->deleteValueHelper(NULL, this->root, value); } }; int main() { BST *bst = new BST(); bst->add(11); bst->add(1); bst->add(6); bst->add(-1); bst->add(-10); bst->add(100); bst->print(); cout<nodesCount(); cout<height(); cout<printMaxPath(); cout<deleteValue(11); cout<<"11 removed: "; bst->print(); cout<deleteValue(1); bst->print(); cout<deleteValue(-1); bst->print(); cout<deleteValue(6); bst->print(); cout<deleteValue(-10); bst->print(); cout<deleteValue(100); bst->print(); cout<