// Returns if the word is in the trie. publicbooleansearch(String word){ if(word == null || word.isEmpty()) { returntrue; } char[] chars = word.toCharArray(); TrieNode tmp = root; for(char c : chars) { if(!tmp.containsChild(c)) { returnfalse; } tmp = tmp.getChild(c); } return tmp.isLeaf(); }
// Returns if there is any word in the trie // that starts with the given prefix. publicbooleanstartsWith(String prefix){ if(prefix == null || prefix.isEmpty()) { returntrue; } char[] chars = prefix.toCharArray(); TrieNode tmp = root; for(char c : chars) { if(!tmp.containsChild(c)) { returnfalse; } tmp = tmp.getChild(c); } returntrue; } }