Skip to content

Commit a282612

Browse files
authored
Merge pull request #72 from LONECODER1/feature/Dictionary
Dictionary Application
2 parents f31e319 + db62d30 commit a282612

File tree

3 files changed

+289
-0
lines changed

3 files changed

+289
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# C++ Trie-Based Dictionary Application
2+
3+
## Overview
4+
This project is a **Dictionary Application implemented in C++**, using the **Trie (Prefix Tree)** data structure.
5+
It provides efficient operations for storing, searching, suggesting, and deleting words.
6+
7+
The system follows **Object-Oriented Programming (OOP)** principles to ensure modularity, clarity, and easy scalability.
8+
9+
---
10+
11+
## Features
12+
- Trie-based dictionary implementation
13+
- Supports:
14+
- Insertion of new words
15+
- Auto-suggestion based on prefix
16+
- Deletion of existing words
17+
- Display of all dictionary words in sorted order
18+
- Efficient insert, search, and delete operations (O(length of word))
19+
- Clean OOP architecture:
20+
- `TrieNode`
21+
- `Trie`
22+
- `DictionaryApp`
23+
24+
---
25+
26+
## Components
27+
28+
### 1. TrieNode
29+
Represents a single character node in the Trie:
30+
- Stores child pointers
31+
- Indicates whether the node marks the end of a word
32+
33+
### 2. Trie
34+
Handles all dictionary operations:
35+
- Insert
36+
- Search
37+
- Delete
38+
- Auto-suggest
39+
- Display all words
40+
41+
### 3. DictionaryApp
42+
Provides the menu-driven interface:
43+
- Takes user input
44+
- Executes corresponding Trie operations
45+
- Displays results interactively
46+
47+
---
48+
49+
## Detailed Feature Description
50+
51+
### Insert Word
52+
Adds a new word to the Trie and ensures no duplicate insertion.
53+
54+
---
55+
56+
### Auto Suggest
57+
Displays all valid dictionary words that begin with a given prefix, using a depth-first traversal of the Trie.
58+
59+
---
60+
61+
### Delete Word
62+
Removes a word safely by:
63+
- Unmarking the end-of-word
64+
- Recursively cleaning unused nodes
65+
66+
Maintains proper Trie structure after deletion.
67+
68+
---
69+
70+
### Display All Words
71+
Traverses the entire Trie and prints every stored word in lexicographically sorted order.
72+
73+
---
74+
75+
## Implementation Requirements
76+
- Full C++ implementation
77+
- Follows OOP design principles
78+
- Efficient recursive traversal
79+
- Clean and modular class structure
80+
- Handles user input gracefully
81+
82+
---
83+
84+
## Time Complexity
85+
86+
| Operation | Time Complexity |
87+
|------------------|-------------------------|
88+
| Insert | O(n) |
89+
| Search | O(n) |
90+
| Delete | O(n) |
91+
| Auto-suggest | O(n + k) |
92+
| Display All | O(total characters) |
93+
94+
Where **n** is the length of the word and **k** is the number of suggestions.
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#include <iostream>
2+
#include <map>
3+
#include <vector>
4+
#include <algorithm>
5+
using namespace std;
6+
7+
class TrieNode
8+
{
9+
public:
10+
map<char, TrieNode *> children;
11+
bool isEnd;
12+
13+
TrieNode() : isEnd(false) {}
14+
};
15+
16+
class Trie
17+
{
18+
private:
19+
TrieNode *root;
20+
21+
void getAllWordsFromNode(TrieNode *node, string prefix, vector<string> &result) const
22+
{
23+
if (!node)
24+
return;
25+
if (node->isEnd)
26+
result.push_back(prefix);
27+
for (auto &p : node->children)
28+
getAllWordsFromNode(p.second, prefix + p.first, result);
29+
}
30+
31+
bool deleteHelper(TrieNode *node, const string &word, int depth)
32+
{
33+
if (!node)
34+
return false;
35+
36+
if (depth == word.size())
37+
{
38+
if (!node->isEnd)
39+
return false;
40+
node->isEnd = false;
41+
return node->children.empty();
42+
}
43+
44+
char c = word[depth];
45+
46+
if (node->children.find(c) == node->children.end())
47+
return false;
48+
49+
if (deleteHelper(node->children[c], word, depth + 1))
50+
{
51+
delete node->children[c];
52+
node->children.erase(c);
53+
return !node->isEnd && node->children.empty();
54+
}
55+
return false;
56+
}
57+
58+
void freeMemory(TrieNode *node)
59+
{
60+
for (auto &p : node->children)
61+
freeMemory(p.second);
62+
delete node;
63+
}
64+
65+
public:
66+
Trie() { root = new TrieNode(); }
67+
68+
~Trie() { freeMemory(root); }
69+
70+
void insert(const string &word)
71+
{
72+
TrieNode *current = root;
73+
for (char c : word)
74+
{
75+
if (current->children.find(c) == current->children.end())
76+
current->children[c] = new TrieNode();
77+
current = current->children[c];
78+
}
79+
current->isEnd = true;
80+
}
81+
82+
bool search(const string &word) const
83+
{
84+
TrieNode *current = root;
85+
for (char c : word)
86+
{
87+
if (current->children.find(c) == current->children.end())
88+
return false;
89+
current = current->children[c];
90+
}
91+
return current->isEnd;
92+
}
93+
94+
vector<string> autoSuggest(const string &prefix) const
95+
{
96+
vector<string> result;
97+
TrieNode *current = root;
98+
99+
for (char c : prefix)
100+
{
101+
if (current->children.find(c) == current->children.end())
102+
return result;
103+
current = current->children[c];
104+
}
105+
106+
getAllWordsFromNode(current, prefix, result);
107+
return result;
108+
}
109+
110+
void displayAll() const
111+
{
112+
vector<string> words;
113+
getAllWordsFromNode(root, "", words);
114+
for (auto &w : words)
115+
cout << w << "\n";
116+
}
117+
118+
void deleteWord(const string &word)
119+
{
120+
deleteHelper(root, word, 0);
121+
}
122+
};
123+
124+
int main()
125+
{
126+
Trie dictionary;
127+
int choice;
128+
string word, prefix;
129+
130+
while (true)
131+
{
132+
cout << "\n===== Dictionary Application =====\n";
133+
cout << "1. Insert Word\n";
134+
cout << "2. Auto Suggest\n";
135+
cout << "3. Delete Word\n";
136+
cout << "4. Display All Words\n";
137+
cout << "5. Exit\n";
138+
cout << "Enter your choice: ";
139+
140+
cin >> choice;
141+
if (cin.fail())
142+
{
143+
cin.clear();
144+
cin.ignore(1000, '\n');
145+
cout << "Invalid input. Please enter a number.\n";
146+
continue;
147+
}
148+
149+
if (choice == 5)
150+
{
151+
cout << "Exiting program..." << endl;
152+
break;
153+
}
154+
155+
switch (choice)
156+
{
157+
case 1:
158+
cout << "Enter word to insert: ";
159+
cin >> word;
160+
dictionary.insert(word);
161+
cout << "Inserted successfully!\n";
162+
break;
163+
164+
case 2:
165+
cout << "Enter prefix: ";
166+
cin >> prefix;
167+
{
168+
vector<string> suggestions = dictionary.autoSuggest(prefix);
169+
if (suggestions.empty())
170+
cout << "No suggestions found.\n";
171+
else
172+
for (auto &s : suggestions)
173+
cout << s << "\n";
174+
}
175+
break;
176+
177+
case 3:
178+
cout << "Enter word to delete: ";
179+
cin >> word;
180+
dictionary.deleteWord(word);
181+
cout << "Deleted (if existed).\n";
182+
break;
183+
184+
case 4:
185+
cout << "All words stored:\n";
186+
dictionary.displayAll();
187+
break;
188+
189+
default:
190+
cout << "Invalid choice. Try again.\n";
191+
}
192+
}
193+
194+
return 0;
195+
}
147 KB
Binary file not shown.

0 commit comments

Comments
 (0)