diff --git a/Src/Dictionary_Application/README.md b/Src/Dictionary_Application/README.md new file mode 100644 index 0000000..2c5d5b4 --- /dev/null +++ b/Src/Dictionary_Application/README.md @@ -0,0 +1,94 @@ +# C++ Trie-Based Dictionary Application + +## Overview +This project is a **Dictionary Application implemented in C++**, using the **Trie (Prefix Tree)** data structure. +It provides efficient operations for storing, searching, suggesting, and deleting words. + +The system follows **Object-Oriented Programming (OOP)** principles to ensure modularity, clarity, and easy scalability. + +--- + +## Features +- Trie-based dictionary implementation +- Supports: + - Insertion of new words + - Auto-suggestion based on prefix + - Deletion of existing words + - Display of all dictionary words in sorted order +- Efficient insert, search, and delete operations (O(length of word)) +- Clean OOP architecture: + - `TrieNode` + - `Trie` + - `DictionaryApp` + +--- + +## Components + +### 1. TrieNode +Represents a single character node in the Trie: +- Stores child pointers +- Indicates whether the node marks the end of a word + +### 2. Trie +Handles all dictionary operations: +- Insert +- Search +- Delete +- Auto-suggest +- Display all words + +### 3. DictionaryApp +Provides the menu-driven interface: +- Takes user input +- Executes corresponding Trie operations +- Displays results interactively + +--- + +## Detailed Feature Description + +### Insert Word +Adds a new word to the Trie and ensures no duplicate insertion. + +--- + +### Auto Suggest +Displays all valid dictionary words that begin with a given prefix, using a depth-first traversal of the Trie. + +--- + +### Delete Word +Removes a word safely by: +- Unmarking the end-of-word +- Recursively cleaning unused nodes + +Maintains proper Trie structure after deletion. + +--- + +### Display All Words +Traverses the entire Trie and prints every stored word in lexicographically sorted order. + +--- + +## Implementation Requirements +- Full C++ implementation +- Follows OOP design principles +- Efficient recursive traversal +- Clean and modular class structure +- Handles user input gracefully + +--- + +## Time Complexity + +| Operation | Time Complexity | +|------------------|-------------------------| +| Insert | O(n) | +| Search | O(n) | +| Delete | O(n) | +| Auto-suggest | O(n + k) | +| Display All | O(total characters) | + +Where **n** is the length of the word and **k** is the number of suggestions. diff --git a/Src/Dictionary_Application/dictionaryApplication.cpp b/Src/Dictionary_Application/dictionaryApplication.cpp new file mode 100644 index 0000000..89b934b --- /dev/null +++ b/Src/Dictionary_Application/dictionaryApplication.cpp @@ -0,0 +1,195 @@ +#include +#include +#include +#include +using namespace std; + +class TrieNode +{ +public: + map children; + bool isEnd; + + TrieNode() : isEnd(false) {} +}; + +class Trie +{ +private: + TrieNode *root; + + void getAllWordsFromNode(TrieNode *node, string prefix, vector &result) const + { + if (!node) + return; + if (node->isEnd) + result.push_back(prefix); + for (auto &p : node->children) + getAllWordsFromNode(p.second, prefix + p.first, result); + } + + bool deleteHelper(TrieNode *node, const string &word, int depth) + { + if (!node) + return false; + + if (depth == word.size()) + { + if (!node->isEnd) + return false; + node->isEnd = false; + return node->children.empty(); + } + + char c = word[depth]; + + if (node->children.find(c) == node->children.end()) + return false; + + if (deleteHelper(node->children[c], word, depth + 1)) + { + delete node->children[c]; + node->children.erase(c); + return !node->isEnd && node->children.empty(); + } + return false; + } + + void freeMemory(TrieNode *node) + { + for (auto &p : node->children) + freeMemory(p.second); + delete node; + } + +public: + Trie() { root = new TrieNode(); } + + ~Trie() { freeMemory(root); } + + void insert(const string &word) + { + TrieNode *current = root; + for (char c : word) + { + if (current->children.find(c) == current->children.end()) + current->children[c] = new TrieNode(); + current = current->children[c]; + } + current->isEnd = true; + } + + bool search(const string &word) const + { + TrieNode *current = root; + for (char c : word) + { + if (current->children.find(c) == current->children.end()) + return false; + current = current->children[c]; + } + return current->isEnd; + } + + vector autoSuggest(const string &prefix) const + { + vector result; + TrieNode *current = root; + + for (char c : prefix) + { + if (current->children.find(c) == current->children.end()) + return result; + current = current->children[c]; + } + + getAllWordsFromNode(current, prefix, result); + return result; + } + + void displayAll() const + { + vector words; + getAllWordsFromNode(root, "", words); + for (auto &w : words) + cout << w << "\n"; + } + + void deleteWord(const string &word) + { + deleteHelper(root, word, 0); + } +}; + +int main() +{ + Trie dictionary; + int choice; + string word, prefix; + + while (true) + { + cout << "\n===== Dictionary Application =====\n"; + cout << "1. Insert Word\n"; + cout << "2. Auto Suggest\n"; + cout << "3. Delete Word\n"; + cout << "4. Display All Words\n"; + cout << "5. Exit\n"; + cout << "Enter your choice: "; + + cin >> choice; + if (cin.fail()) + { + cin.clear(); + cin.ignore(1000, '\n'); + cout << "Invalid input. Please enter a number.\n"; + continue; + } + + if (choice == 5) + { + cout << "Exiting program..." << endl; + break; + } + + switch (choice) + { + case 1: + cout << "Enter word to insert: "; + cin >> word; + dictionary.insert(word); + cout << "Inserted successfully!\n"; + break; + + case 2: + cout << "Enter prefix: "; + cin >> prefix; + { + vector suggestions = dictionary.autoSuggest(prefix); + if (suggestions.empty()) + cout << "No suggestions found.\n"; + else + for (auto &s : suggestions) + cout << s << "\n"; + } + break; + + case 3: + cout << "Enter word to delete: "; + cin >> word; + dictionary.deleteWord(word); + cout << "Deleted (if existed).\n"; + break; + + case 4: + cout << "All words stored:\n"; + dictionary.displayAll(); + break; + + default: + cout << "Invalid choice. Try again.\n"; + } + } + + return 0; +} \ No newline at end of file diff --git a/Src/Dictionary_Application/dictionaryApplication.exe b/Src/Dictionary_Application/dictionaryApplication.exe new file mode 100644 index 0000000..bc154c4 Binary files /dev/null and b/Src/Dictionary_Application/dictionaryApplication.exe differ