@@ -25,19 +25,16 @@ public class TrieImp {
25
25
*/
26
26
public class TrieNode {
27
27
28
- // Array to store references to child nodes, one for each letter of the alphabet
29
- // (a-z)
30
28
TrieNode [] child ;
31
-
32
- boolean end ; // Flag to indicate if this node marks the end of a valid word
29
+ boolean end ;
33
30
34
31
/**
35
32
* Constructor to initialize a TrieNode with an empty child array and set end to
36
33
* false.
37
34
*/
38
35
public TrieNode () {
39
- child = new TrieNode [26 ]; // Initialize child array with 26 slots (for each letter a-z)
40
- end = false ; // By default, this node doesn't mark the end of any word
36
+ child = new TrieNode [26 ];
37
+ end = false ;
41
38
}
42
39
}
43
40
@@ -48,31 +45,28 @@ public TrieNode() {
48
45
* The root node is created but doesn't represent any character.
49
46
*/
50
47
public TrieImp () {
51
- root = new TrieNode (); // Initialize the root node of the Trie
48
+ root = new TrieNode ();
52
49
}
53
50
54
51
/**
55
52
* Inserts a word into the Trie.
56
53
* <p>
57
54
* The method traverses the Trie from the root, character by character, and adds
58
- * nodes
59
- * if necessary. It marks the last node of the word as an end node.
55
+ * nodes if necessary. It marks the last node of the word as an end node.
60
56
*
61
57
* @param word The word to be inserted into the Trie.
62
58
*/
63
59
public void insert (String word ) {
64
60
TrieNode currentNode = root ;
65
61
for (int i = 0 ; i < word .length (); i ++) {
66
- // Calculate index of character ('a' -> 0, 'b' -> 1, ..., 'z' -> 25)
67
62
TrieNode node = currentNode .child [word .charAt (i ) - 'a' ];
68
63
if (node == null ) {
69
- // If the node doesn't exist, create a new one and assign it to the child array
70
64
node = new TrieNode ();
71
65
currentNode .child [word .charAt (i ) - 'a' ] = node ;
72
66
}
73
- currentNode = node ; // Move to the next node
67
+ currentNode = node ;
74
68
}
75
- currentNode .end = true ; // Mark the last node as the end of a word
69
+ currentNode .end = true ;
76
70
}
77
71
78
72
/**
@@ -91,11 +85,11 @@ public boolean search(String word) {
91
85
char ch = word .charAt (i );
92
86
TrieNode node = currentNode .child [ch - 'a' ];
93
87
if (node == null ) {
94
- return false ; // Word not found
88
+ return false ;
95
89
}
96
90
currentNode = node ;
97
91
}
98
- return currentNode .end ; // Return true if it's an end node, false otherwise
92
+ return currentNode .end ;
99
93
}
100
94
101
95
/**
@@ -115,16 +109,15 @@ public boolean delete(String word) {
115
109
char ch = word .charAt (i );
116
110
TrieNode node = currentNode .child [ch - 'a' ];
117
111
if (node == null ) {
118
- return false ; // Word not found
112
+ return false ;
119
113
}
120
114
currentNode = node ;
121
115
}
122
116
if (currentNode .end ) {
123
- // If the word exists, mark the end flag as false (word is deleted)
124
117
currentNode .end = false ;
125
118
return true ;
126
119
}
127
- return false ; // Word doesn't exist
120
+ return false ;
128
121
}
129
122
130
123
/**
@@ -147,77 +140,6 @@ public static void sop(String print) {
147
140
* @return true if the word is valid (only a-z), false otherwise.
148
141
*/
149
142
public static boolean isValid (String word ) {
150
- return word .matches ("^[a-z]+$" ); // Regex for lowercase letters only
151
- }
152
-
153
- /**
154
- * Main method to demonstrate Trie operations using user input.
155
- * <p>
156
- * The user can choose between inserting a word, searching for a word,
157
- * deleting a word, or quitting the program. It uses a loop to continuously
158
- * ask for user input until the program is quit.
159
- *
160
- * @param args Command-line arguments (not used in this program).
161
- */
162
- public static void main (String [] args ) {
163
- TrieImp obj = new TrieImp ();
164
- String word ;
165
- @ SuppressWarnings ("resource" ) Scanner scan = new Scanner (System .in );
166
-
167
- sop ("string should contain only a-z character for all operation" );
168
-
169
- // Loop indefinitely until the user decides to quit
170
- while (true ) {
171
- sop ("1. Insert\n 2. Search\n 3. Delete\n 4. Quit" );
172
- try {
173
- int t = scan .nextInt ();
174
- switch (t ) {
175
- case 1 :
176
- // Insert a word into the Trie
177
- word = scan .next ();
178
- if (isValid (word )) {
179
- obj .insert (word );
180
- sop ("Word inserted successfully" );
181
- } else {
182
- sop ("Invalid string: allowed only a-z" );
183
- }
184
- break ;
185
- case 2 :
186
- // Search for a word in the Trie
187
- word = scan .next ();
188
- boolean resS = false ;
189
- if (isValid (word )) {
190
- resS = obj .search (word );
191
- } else {
192
- sop ("Invalid string: allowed only a-z" );
193
- }
194
- sop (resS ? "Word found" : "Word not found" );
195
- break ;
196
- case 3 :
197
- // Delete a word from the Trie
198
- word = scan .next ();
199
- boolean resD = false ;
200
- if (isValid (word )) {
201
- resD = obj .delete (word );
202
- } else {
203
- sop ("Invalid string: allowed only a-z" );
204
- }
205
- sop (resD ? "Word deleted successfully" : "Word not found" );
206
- break ;
207
- case 4 :
208
- // Quit the program
209
- sop ("Quit successfully" );
210
- System .exit (1 );
211
- break ;
212
- default :
213
- sop ("Input int from 1-4" );
214
- break ;
215
- }
216
- } catch (Exception e ) {
217
- // Handle bad input
218
- String badInput = scan .next ();
219
- sop ("This is bad input: " + badInput );
220
- }
221
- }
143
+ return word .matches ("^[a-z]+$" );
222
144
}
223
145
}
0 commit comments