ternary-search-trie
Advanced tools
Comparing version
@@ -148,21 +148,14 @@ "use strict"; | ||
callback(prefix, root); | ||
// All words should start with the root node found using the prefix | ||
var word = [root.key]; | ||
// Traversal starts from the middle child - we only care about words that start with the specified prefix | ||
this.traversal(root.middle, function (node) { | ||
// Add each node's key found during the traversal to the word | ||
word.push(node.key); | ||
// If a value is found at a given node, execute the callback | ||
if (node.value) { | ||
callback(word.join(''), node); | ||
// If the found node has any children, the current letter must be popped off the stack | ||
if (node.hasChildren()) { | ||
word.pop(); | ||
} | ||
else { | ||
// Otherwise we reset the word to the root prefix and allow traversal to continue | ||
word = [root.key]; | ||
} | ||
} | ||
}); | ||
// If there is a left child, recurse down the left side of the tree | ||
if (root.left) { | ||
this.searchByPrefix(prefix + root.left.key, callback); | ||
} | ||
// If there is a middle child, recurse down the middle side of the tree | ||
if (root.middle) { | ||
this.searchByPrefix(prefix + root.middle.key, callback); | ||
} | ||
// If there is a right child, recurse down the right side of the tree | ||
if (root.right) { | ||
this.searchByPrefix(prefix + root.right.key, callback); | ||
} | ||
return this; | ||
@@ -169,0 +162,0 @@ }; |
{ | ||
"name": "ternary-search-trie", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A ternary search tree implementation in TypeScript.", | ||
@@ -5,0 +5,0 @@ "author": "Maxwell Brown <maxwellbrown1990@gmail.com>", |
31094
-1.24%669
-1.04%