Last active
August 29, 2015 14:05
-
-
Save olslash/edcc6902c80ecb09008d to your computer and use it in GitHub Desktop.
T9 blogpost - 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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