A spell-checker would probably want to find the nearest matches to a target word, rather than words which happen to have the same prefix. TSTs are good at finding prefixes, but they're not going to help you much if you want to find similar words.
In your example (assuming that "manly" is not in your dictionary, although it is a word), the suggestion "mainly" is much more likely than "mango", but it won't be found by a scan starting with the longest matching prefix.
However, if you want the scan starting at the longest match prefix:
1) Modify searchTST so that it returns a struct Node*
, and replace the last else clause with:
else if (*(word+1) == '\0')
return root;
else {
struct Node* child = searchTST(root->eq, word+1);
return child ? child : root;
}
2) searchTST
will now return the root for the longest-matching prefix to the target. The caller will have to check whether the returned Node's isEndOfString
flag is set.
3) You can use something like traverseTST
on the node returned by searchTST
in order to produce all the words starting with that prefix.