mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
Improve TrieImp.java
comments & enhance readability (#5526)
This commit is contained in:
@ -0,0 +1,76 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TrieImpTest {
|
||||
private TrieImp trie;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
trie = new TrieImp();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertAndSearchBasic() {
|
||||
String word = "hello";
|
||||
trie.insert(word);
|
||||
assertTrue(trie.search(word), "Search should return true for an inserted word.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchNonExistentWord() {
|
||||
String word = "world";
|
||||
assertFalse(trie.search(word), "Search should return false for a non-existent word.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertAndSearchMultipleWords() {
|
||||
String word1 = "cat";
|
||||
String word2 = "car";
|
||||
trie.insert(word1);
|
||||
trie.insert(word2);
|
||||
|
||||
assertTrue(trie.search(word1), "Search should return true for an inserted word.");
|
||||
assertTrue(trie.search(word2), "Search should return true for another inserted word.");
|
||||
assertFalse(trie.search("dog"), "Search should return false for a word not in the Trie.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteExistingWord() {
|
||||
String word = "remove";
|
||||
trie.insert(word);
|
||||
assertTrue(trie.delete(word), "Delete should return true for an existing word.");
|
||||
assertFalse(trie.search(word), "Search should return false after deletion.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNonExistentWord() {
|
||||
String word = "nonexistent";
|
||||
assertFalse(trie.delete(word), "Delete should return false for a non-existent word.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertAndSearchPrefix() {
|
||||
String prefix = "pre";
|
||||
String word = "prefix";
|
||||
trie.insert(prefix);
|
||||
trie.insert(word);
|
||||
|
||||
assertTrue(trie.search(prefix), "Search should return true for an inserted prefix.");
|
||||
assertTrue(trie.search(word), "Search should return true for a word with the prefix.");
|
||||
assertFalse(trie.search("pref"), "Search should return false for a prefix that is not a full word.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsValidWord() {
|
||||
assertTrue(TrieImp.isValid("validword"), "Word should be valid (only lowercase letters).");
|
||||
assertFalse(TrieImp.isValid("InvalidWord"), "Word should be invalid (contains uppercase letters).");
|
||||
assertFalse(TrieImp.isValid("123abc"), "Word should be invalid (contains numbers).");
|
||||
assertFalse(TrieImp.isValid("hello!"), "Word should be invalid (contains special characters).");
|
||||
assertFalse(TrieImp.isValid(""), "Empty string should be invalid.");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user