This is my implementation of a Trie.
public class Trie {
private class Node{
private Map<Character, Node> children;
boolean end;
public Node(){
children = new HashMap<>();
end = false;
}
}
private Node root;
public Trie(){
root = new Node();
}
public void insert(String word){
Node current = root;
for (int i=0; i < word.length(); i++){
char c = word.charAt(i);
Node node = current.children.get(c);
if(node == null){
node = new Node();
current.children.put(c, node);
}
current = node;
}
}
public boolean search(String word){
Node current = root;
for(int i =0; i < word.length(); i++){
char c = word.charAt(i);
Node node = current.children.get(c);
if(node == null)
return false;
current = node;
}
return current.end;
}
}
I want to add a method, that given a string return all its children, something that could be used as a real world autocorrect suggestions.
Here's the method signature:
public List<String> returnAllChildren(String str){
}
I'm a little lost with this one. Any help appreciated.