Skip to content

Instantly share code, notes, and snippets.

@tophyr
Created October 29, 2015 21:08
Show Gist options
  • Save tophyr/0dc955ce9560e0f70716 to your computer and use it in GitHub Desktop.
Save tophyr/0dc955ce9560e0f70716 to your computer and use it in GitHub Desktop.
class PrefixNode {
boolean isWord = false;
Character c = null;
PrefixNode[] children = new PrefixNode[26];
}
PrefixNode buildTree(List<String> words) {
PrefixNode root = new PrefixNode();
for (String word : words) {
PrefixNode node = root;
for (char c : word.toCharArray()) {
if (node.children[c] == null) {
PrefixNode newNode = new PrefixNode();
newNode.c = c;
node.children[c] = newNode;
} else {
node = node.children[c];
}
}
node.isWord = true;
}
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment