Ternary Search Trees
for (int i = 0; i < expected.length; ++i) { assertEquals(expected[i], actual.get(i));
}
}
To test prefix searching, you created the method testPrefixSearch(). This method assembles a
list of expected values and a prefix and then delegates most of the work to yet another helper method,
assertPrefixEquals():
public void testPrefixSearch() { assertPrefixEquals(
new String[] {“prefabricate”, “prejudice”, “preliminary”, “presume”}, “pre”);
assertPrefixEquals(
new String[] {“ape”, “appeal”, “apple”}, “ap”);
}
The method assertPrefixEquals() then creates a list to hold the results and calls the tree’s prefixSearch() method to populate the list. The expected and actual results are then passed to your custom assertEquals() method for validation:
private void assertPrefixEquals(String[] expected, String prefix) { List words = new LinkedList();
_tree.prefixSearch(prefix, words);
assertEquals(expected, words);
}
The method testPatternMatch() assembles an array of expected results along with a pattern, and delegates to another helper method, assertPatternEquals():
public void testPatternMatch() {
assertPatternEquals(new String[] {“mince”, “mouse”}, “m???e”);
assertPatternEquals(new String[] {“car”, “cat”}, “?a?”);
}
The method assertPatternEquals() calls patternMatch() on the tree and validates the results. Notice the use of the question mark (?) as the wildcard character. The choice of character is largely arbitrary, but a question mark can’t possibly appear in a word by mistake, and it looks very obvious that it means “something, anything, goes here”:
private void assertPatternEquals(String[] expected, String pattern) { List words = new LinkedList();
_tree.patternMatch(pattern, words);
assertEquals(expected, words);
}