find method
Finds all complete words in the trie matching the prefix
.
Returns a list of all matches. If there are no matches, an empty list is returned.
Implementation
List<T> find(String prefix) {
// Find the node of associated with the last character of the prefix.
final lastCharacterNode = findPrefix(prefix, fromNode: _root);
// The prefix does not exist in the tre.
if (lastCharacterNode == null) {
return [];
}
final stack = [lastCharacterNode];
final foundWords = <T>[];
while (stack.isNotEmpty) {
final partialMatchNode = stack.removeLast();
// The partial match node corresponds to an entire word.
if (partialMatchNode.isEndOfWord) {
foundWords.add(partialMatchNode.value!);
}
// Walk each of the child node of the partial match.
for (final child in partialMatchNode.getChildren()) {
stack.add(child);
}
}
return foundWords;
}