I've found the same question here Traversing a trie to get all words , but it's for Perl, and I'm familiar only with Java.
my trie structure is the plain integer array, where the first 26 ints are the roots, each integer has firstChild index which is child array element's index (up to 25 highest bits), End-of-List bit flag, End-of-Word bit flag, letter index from 1 to 26(lowest 5 bits).
I can recursively print one word CAGE
if I pass the 2(root index for letter c) as a parameter
private void oneWord(int node) {
System.out.print(getChar(Trie[node]));
if (getEOL(Trie[node]) != 1) {
oneWord(getFirstChild(Trie[node]));
}
}
or a common beginning and distinct endings like YARDELLOW
for words yard and yellow(passing 24 as parameter)
private void DFTSearch(int node) {
System.out.print(getChar(Trie[node]));
if (getFirstChild(Trie[node]) != 0) {
for (int i = 0; i < getSiblingsNum((getFirstChild(Trie[node])); i++) {
DFTSearch(getFirstChild(Trie[node]) + i);
}
}
}
but can't do that with all words(( and I've tried this and that to make it return a string, and haven't got any success(just was getting a very first letter of a string. Please help with the method to print recursively(and what is better - return String array) for all words from my trie. It's not a homework, I'm self-educating))