Skip to content

Instantly share code, notes, and snippets.

@olslash
Last active August 29, 2015 14:05
Show Gist options
  • Save olslash/edcc6902c80ecb09008d to your computer and use it in GitHub Desktop.
Save olslash/edcc6902c80ecb09008d to your computer and use it in GitHub Desktop.
T9 blogpost - 4
function traverseAddingNodes(node) {
var i = 0, len = word.length;
// Traverse the tree's existing nodes as far as possible.
for(i, len; i < len; i++) {
var thisLetter = word[i];
var thisKey = keys[thisLetter];
if(node.children.hasOwnProperty(thisKey)) {
node = node.children[thisKey];
} else { break; }
}
// If we reach this point and we still aren't at the node we want, it
// means that other words matching this key pattern haven't been
// inserted before. Continue, this time adding the required nodes
// as we go.
for(i, len; i < len; i++) {
thisLetter = word[i];
thisKey = keys[thisLetter];
node.children[thisKey] = new Trie();
node = node.children[thisKey];
}
return node;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment