Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions Src/Dictionary_Application/README.md
Original file line number Diff line number Diff line change
@@ -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.
195 changes: 195 additions & 0 deletions Src/Dictionary_Application/dictionaryApplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

class TrieNode
{
public:
map<char, TrieNode *> children;
bool isEnd;

TrieNode() : isEnd(false) {}
};

class Trie
{
private:
TrieNode *root;

void getAllWordsFromNode(TrieNode *node, string prefix, vector<string> &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<string> autoSuggest(const string &prefix) const
{
vector<string> 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<string> 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<string> 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;
}
Binary file not shown.
Loading