-, , root . ( , HashMap<Character,HashMap> , .) , . , HashMap<Character,TrieNode>. , .:)
, TrieNode:
public Set<String> computeWords() {
Set<String> result;
if(root.size() == 0)
result = new HashSet<String>();
else
result = computeWords(root, "");
return result;
}
protected static Set<String> computeWords(HashMap tree, String prefix) {
Set<String> result=new HashSet<String>();
if(tree.size() == 0)
result.add(prefix);
else
for(Object o : tree.keySet()) {
Character c=(Character) o;
prefix = prefix+c;
result.addAll(computeWords((HashMap) tree.get(c), prefix));
prefix = prefix.substring(0, prefix.length()-1);
}
return result;
}
TrieNode t, t.computeWords() , t.
, , . , , , t :
for(String word : t.computeWords())
System.out.println(word);
, , , HashSet computeWords(HashMap,String), !
EDIT. , HashMap. null , if(tree.size() == 0) static if(tree == null). , .
EDIT. , , , .
: .