Skip to content

Commit 63ae511

Browse files
committed
Remove duplicate comments, add junit tests
1 parent 8f7881b commit 63ae511

File tree

2 files changed

+88
-90
lines changed

2 files changed

+88
-90
lines changed

src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java

+12-90
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,16 @@ public class TrieImp {
2525
*/
2626
public class TrieNode {
2727

28-
// Array to store references to child nodes, one for each letter of the alphabet
29-
// (a-z)
3028
TrieNode[] child;
31-
32-
boolean end; // Flag to indicate if this node marks the end of a valid word
29+
boolean end;
3330

3431
/**
3532
* Constructor to initialize a TrieNode with an empty child array and set end to
3633
* false.
3734
*/
3835
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;
4138
}
4239
}
4340

@@ -48,31 +45,28 @@ public TrieNode() {
4845
* The root node is created but doesn't represent any character.
4946
*/
5047
public TrieImp() {
51-
root = new TrieNode(); // Initialize the root node of the Trie
48+
root = new TrieNode();
5249
}
5350

5451
/**
5552
* Inserts a word into the Trie.
5653
* <p>
5754
* 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.
6056
*
6157
* @param word The word to be inserted into the Trie.
6258
*/
6359
public void insert(String word) {
6460
TrieNode currentNode = root;
6561
for (int i = 0; i < word.length(); i++) {
66-
// Calculate index of character ('a' -> 0, 'b' -> 1, ..., 'z' -> 25)
6762
TrieNode node = currentNode.child[word.charAt(i) - 'a'];
6863
if (node == null) {
69-
// If the node doesn't exist, create a new one and assign it to the child array
7064
node = new TrieNode();
7165
currentNode.child[word.charAt(i) - 'a'] = node;
7266
}
73-
currentNode = node; // Move to the next node
67+
currentNode = node;
7468
}
75-
currentNode.end = true; // Mark the last node as the end of a word
69+
currentNode.end = true;
7670
}
7771

7872
/**
@@ -91,11 +85,11 @@ public boolean search(String word) {
9185
char ch = word.charAt(i);
9286
TrieNode node = currentNode.child[ch - 'a'];
9387
if (node == null) {
94-
return false; // Word not found
88+
return false;
9589
}
9690
currentNode = node;
9791
}
98-
return currentNode.end; // Return true if it's an end node, false otherwise
92+
return currentNode.end;
9993
}
10094

10195
/**
@@ -115,16 +109,15 @@ public boolean delete(String word) {
115109
char ch = word.charAt(i);
116110
TrieNode node = currentNode.child[ch - 'a'];
117111
if (node == null) {
118-
return false; // Word not found
112+
return false;
119113
}
120114
currentNode = node;
121115
}
122116
if (currentNode.end) {
123-
// If the word exists, mark the end flag as false (word is deleted)
124117
currentNode.end = false;
125118
return true;
126119
}
127-
return false; // Word doesn't exist
120+
return false;
128121
}
129122

130123
/**
@@ -147,77 +140,6 @@ public static void sop(String print) {
147140
* @return true if the word is valid (only a-z), false otherwise.
148141
*/
149142
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\n2. Search\n3. Delete\n4. 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]+$");
222144
}
223145
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class TrieImpTest {
10+
private TrieImp trie;
11+
12+
@BeforeEach
13+
public void setUp() {
14+
trie = new TrieImp();
15+
}
16+
17+
@Test
18+
public void testInsertAndSearchBasic() {
19+
String word = "hello";
20+
trie.insert(word);
21+
assertTrue(trie.search(word), "Search should return true for an inserted word.");
22+
}
23+
24+
@Test
25+
public void testSearchNonExistentWord() {
26+
String word = "world";
27+
assertFalse(trie.search(word), "Search should return false for a non-existent word.");
28+
}
29+
30+
@Test
31+
public void testInsertAndSearchMultipleWords() {
32+
String word1 = "cat";
33+
String word2 = "car";
34+
trie.insert(word1);
35+
trie.insert(word2);
36+
37+
assertTrue(trie.search(word1), "Search should return true for an inserted word.");
38+
assertTrue(trie.search(word2), "Search should return true for another inserted word.");
39+
assertFalse(trie.search("dog"), "Search should return false for a word not in the Trie.");
40+
}
41+
42+
@Test
43+
public void testDeleteExistingWord() {
44+
String word = "remove";
45+
trie.insert(word);
46+
assertTrue(trie.delete(word), "Delete should return true for an existing word.");
47+
assertFalse(trie.search(word), "Search should return false after deletion.");
48+
}
49+
50+
@Test
51+
public void testDeleteNonExistentWord() {
52+
String word = "nonexistent";
53+
assertFalse(trie.delete(word), "Delete should return false for a non-existent word.");
54+
}
55+
56+
@Test
57+
public void testInsertAndSearchPrefix() {
58+
String prefix = "pre";
59+
String word = "prefix";
60+
trie.insert(prefix);
61+
trie.insert(word);
62+
63+
assertTrue(trie.search(prefix), "Search should return true for an inserted prefix.");
64+
assertTrue(trie.search(word), "Search should return true for a word with the prefix.");
65+
assertFalse(trie.search("pref"), "Search should return false for a prefix that is not a full word.");
66+
}
67+
68+
@Test
69+
public void testIsValidWord() {
70+
assertTrue(TrieImp.isValid("validword"), "Word should be valid (only lowercase letters).");
71+
assertFalse(TrieImp.isValid("InvalidWord"), "Word should be invalid (contains uppercase letters).");
72+
assertFalse(TrieImp.isValid("123abc"), "Word should be invalid (contains numbers).");
73+
assertFalse(TrieImp.isValid("hello!"), "Word should be invalid (contains special characters).");
74+
assertFalse(TrieImp.isValid(""), "Empty string should be invalid.");
75+
}
76+
}

0 commit comments

Comments
 (0)