@@ -57,7 +57,7 @@ public TrieImp() {
57
57
* The method traverses the Trie from the root, character by character, and adds
58
58
* nodes
59
59
* if necessary. It marks the last node of the word as an end node.
60
- *
60
+ *
61
61
* @param word The word to be inserted into the Trie.
62
62
*/
63
63
public void insert (String word ) {
@@ -81,7 +81,7 @@ public void insert(String word) {
81
81
* This method traverses the Trie based on the input word and checks whether
82
82
* the word exists. It returns true if the word is found and its end flag is
83
83
* true.
84
- *
84
+ *
85
85
* @param word The word to search in the Trie.
86
86
* @return true if the word exists in the Trie, false otherwise.
87
87
*/
@@ -105,7 +105,7 @@ public boolean search(String word) {
105
105
* false.
106
106
* It returns true if the word was successfully deleted, false if the word
107
107
* wasn't found.
108
- *
108
+ *
109
109
* @param word The word to be deleted from the Trie.
110
110
* @return true if the word was found and deleted, false if it was not found.
111
111
*/
@@ -129,7 +129,7 @@ public boolean delete(String word) {
129
129
130
130
/**
131
131
* Helper method to print a string to the console.
132
- *
132
+ *
133
133
* @param print The string to be printed.
134
134
*/
135
135
public static void sop (String print ) {
@@ -142,7 +142,7 @@ public static void sop(String print) {
142
142
* <p>
143
143
* The method uses a regular expression to check if the word matches the pattern
144
144
* of only lowercase letters.
145
- *
145
+ *
146
146
* @param word The word to be validated.
147
147
* @return true if the word is valid (only a-z), false otherwise.
148
148
*/
@@ -156,14 +156,13 @@ public static boolean isValid(String word) {
156
156
* The user can choose between inserting a word, searching for a word,
157
157
* deleting a word, or quitting the program. It uses a loop to continuously
158
158
* ask for user input until the program is quit.
159
- *
159
+ *
160
160
* @param args Command-line arguments (not used in this program).
161
161
*/
162
162
public static void main (String [] args ) {
163
163
TrieImp obj = new TrieImp ();
164
164
String word ;
165
- @ SuppressWarnings ("resource" )
166
- Scanner scan = new Scanner (System .in );
165
+ @ SuppressWarnings ("resource" ) Scanner scan = new Scanner (System .in );
167
166
168
167
sop ("string should contain only a-z character for all operation" );
169
168
@@ -173,46 +172,46 @@ public static void main(String[] args) {
173
172
try {
174
173
int t = scan .nextInt ();
175
174
switch (t ) {
176
- case 1 :
177
- // Insert a word into the Trie
178
- word = scan .next ();
179
- if (isValid (word )) {
180
- obj .insert (word );
181
- sop ("Word inserted successfully" );
182
- } else {
183
- sop ("Invalid string: allowed only a-z" );
184
- }
185
- break ;
186
- case 2 :
187
- // Search for a word in the Trie
188
- word = scan .next ();
189
- boolean resS = false ;
190
- if (isValid (word )) {
191
- resS = obj .search (word );
192
- } else {
193
- sop ("Invalid string: allowed only a-z" );
194
- }
195
- sop (resS ? "Word found" : "Word not found" );
196
- break ;
197
- case 3 :
198
- // Delete a word from the Trie
199
- word = scan .next ();
200
- boolean resD = false ;
201
- if (isValid (word )) {
202
- resD = obj .delete (word );
203
- } else {
204
- sop ("Invalid string: allowed only a-z" );
205
- }
206
- sop (resD ? "Word deleted successfully" : "Word not found" );
207
- break ;
208
- case 4 :
209
- // Quit the program
210
- sop ("Quit successfully" );
211
- System .exit (1 );
212
- break ;
213
- default :
214
- sop ("Input int from 1-4" );
215
- break ;
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 ;
216
215
}
217
216
} catch (Exception e ) {
218
217
// Handle bad input
0 commit comments