#include #include #include #include #include using namespace std; struct BSTNode { string word; BSTNode* left; BSTNode* right; BSTNode(const string& w) : word(w), left(nullptr), right(nullptr) {} }; class BST { BSTNode* root; bool insertNode(BSTNode*& node, const string& w) { if (!node) { node = new BSTNode(w); return true; } if (w == node->word) return false; if (w < node->word) return insertNode(node->left, w); return insertNode(node->right, w); } bool removeNode(BSTNode*& node, const string& w) { if (!node) return false; if (w < node->word) return removeNode(node->left, w); if (w > node->word) return removeNode(node->right, w); if (!node->left) { BSTNode* temp = node->right; delete node; node = temp; } else if (!node->right) { BSTNode* temp = node->left; delete node; node = temp; } else { BSTNode* minNode = findMin(node->right); node->word = minNode->word; removeNode(node->right, minNode->word); } return true; } BSTNode* findMin(BSTNode* node) { if (!node) return nullptr; while (node->left) node = node->left; return node; } void clearTree(BSTNode*& node) { if (!node) return; clearTree(node->left); clearTree(node->right); delete node; node = nullptr; } void inorder(BSTNode* node, vector& result) const { if (!node) return; inorder(node->left, result); result.push_back(node->word); inorder(node->right, result); } public: BST() : root(nullptr) {} bool insert(const string& w) { return insertNode(root, w); } bool remove(const string& w) { return removeNode(root, w); } void clear() { clearTree(root); } vector getAllWords() const { vector result; inorder(root, result); return result; } }; int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); setlocale(LC_ALL, "Russian"); BST dictionary; cout << "Команды:\n" << "0 - очистить словарь;\n" << "1,<строка> - добавить строку;\n" << "2,<строка> - удалить строку;\n" << "3 - вывести все слова;\n" << "4 - завершить работу.\n\n"; while (true) { string input; if (!getline(cin, input)) break; if (input.empty()) { cout << "Некорректный ввод!\n"; continue; } int commaPos = input.find(','); int command; string cmdStr, word; if (commaPos == (int)string::npos) { cmdStr = input; } else { cmdStr = input.substr(0, commaPos); if (commaPos + 1 < (int)input.size()) { word = input.substr(commaPos + 1); } } try { command = stoi(cmdStr); } catch (...) { cout << "Некорректный ввод!\n"; continue; } switch (command) { case 0: dictionary.clear(); cout << "Словарь очищен\n"; break; case 1: if (word.empty()) { cout << "Некорректный ввод!\n"; break; } if (dictionary.insert(word)) cout << "Строка \"" << word << "\" добавлена в словарь\n"; else cout << "Строка \"" << word << "\" уже есть в словаре\n"; break; case 2: if (word.empty()) { cout << "Некорректный ввод!\n"; break; } if (dictionary.remove(word)) cout << "Строка \"" << word << "\" удалена из словаря\n"; else cout << "Строка \"" << word << "\" не найдена в словаре\n"; break; case 3: { vector allWords = dictionary.getAllWords(); if (allWords.empty()) { cout << "Словарь пуст\n"; break; } for (int i = 0; i < (int)allWords.size(); i++) { cout << allWords[i]; if (i + 1 < (int)allWords.size()) cout << ", "; } cout << "\n"; break; } case 4: return 0; default: cout << "Некорректный ввод!\n"; break; } } return 0; }