I'm having trouble understanding the concept of a trie. From the "trie" wikipedia entry I have this picture:
If I see this correctly, all leaf nodes in a trie will have the entire word spelled out and all parent nodes hold the characters leading up the the final leaf node. So, if I have a class called DigitalTreeNode defined by
public class DigitalTreeNode {
public boolean isAWord;
public String wordToHere; (compiles all the characters in a word together)
public Map<String, DTN> children;
}
If I wanted to implement a method that returns the longest word in the trie would it simply involve finding the longest word at each leaf node? How would I implement a method such as:
public static String longestWord (DigitalTreeNode d);
I'm guessing it involves setting up a longest String variable, recursively going through each node and checking if it is a word, if it is a word and it's length is greater than the longest variable then longest = newWordLength . But, I'm not sure how the Map children fits in. How would I find the longest word in any trie using the method above?