-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.cpp
More file actions
200 lines (140 loc) · 3.79 KB
/
Trie.cpp
File metadata and controls
200 lines (140 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// ================= TRIE DATASTRUCTURE =================
// A Trie (pronounced as "try") is a special type of tree used to store associative data structures.
// A common application of a Trie is storing a predictive text or autocomplete dictionary.
// The key feature of a Trie is that it allows for efficient retrieval of keys with a common prefix.
// implementation of Trie data structure in C++.
#include <iostream>
using namespace std;
struct TrieNode
{
char c ;
TrieNode * children[26];
bool isTerminalNode;
TrieNode(char c){
this->c = c;
for (int i = 0; i < 26; i++) {
children[i] = nullptr;
}
isTerminalNode = false;
}
};
class Trie{
TrieNode * root;
// if present util
bool isPresentUtil(TrieNode* root , const string &word , int i){
if(i == word.length()) return root->isTerminalNode;
int index = tolower(word[i]) - 'a';
TrieNode * child;
if(root->children[index] != nullptr){
child = root->children[index];
}else{
return false;
}
// for next char
return isPresentUtil(child , word , i+1);
}
// insertion utility
void insertionUtil(TrieNode * root , const string &word , int i){
if(i == word.length()) {
root->isTerminalNode = true;
return;
}
int index = tolower(word[i]) - 'a';
TrieNode * child;
if(root->children[index] != nullptr){
child = root->children[index];
}else{
child = new TrieNode(word[i]);
root->children[index] = child;
}
// for next char
insertionUtil(child , word , i+1);
}
// deletion utility fucntion
bool deletionUtil( TrieNode* root , const string &word , int i){
if (i == word.length()) {
if (!root->isTerminalNode) return false; // word not found
root->isTerminalNode = false;
// if no children, this node can be deleted
for (int j = 0; j < 26; j++) {
if (root->children[j] != nullptr) return true; // still needed
}
return false; // no children → deletable
}
int index = tolower(word[i]) -'a';
TrieNode * child = root->children[index];
if(!child) return false;
bool shouldKeepChild = deletionUtil(child , word , i+1);
if(!shouldKeepChild){
delete child;
root->children[index] = nullptr;
}
if (!root->isTerminalNode) {
for (int j = 0; j < 26; j++) {
if (root->children[j] != nullptr) return true;
}
return false;
}
return true;
}
public :
Trie(){
root = new TrieNode('\0');
}
void insert(const string &word) {
insertionUtil(root, word , 0);
}
// if present or not
bool isPresent(const string &word) {
return isPresentUtil(root, word , 0 );
}
bool deletion(const string &word){
return deletionUtil(root , word, 0);
}
};
int main() {
// initialsation
Trie* t = new Trie();
// insertion
t->insert("kush");
t->insert("is");
t->insert("a");
t->insert("king");
// if present
string s1 = "kush";
string s2 = "is";
string s3 = "a";
string s4 = "king";
if(t->isPresent(s1)) {
cout << "kush is present" << endl;
}
if(t->isPresent(s2)) {
cout << "is is present" << endl;
}
if(t->isPresent(s3)) {
cout << "a is present" << endl;
}
if(t->isPresent(s4)) {
cout << "king is present" << endl;
}
string s5 = "queen";
if(t->isPresent(s5)) {
cout << "queen is present" << endl;
} else {
cout << "queen is not present" << endl;
}
string s6 = "kushal";
if(t->isPresent(s6)) {
cout << "kushal is present" << endl;
} else {
cout << "kushal is not present" << endl;
}
// deletion
if(t->deletion(s1)){
cout << "deleted successfully" << endl;
}
if(t->isPresent(s1)){
cout << "still Kush is persent " << endl;
}
return 0;
}