Trie in JavaScript: The data structure behind autocomplete
31–40 of 59 posts
Re: Trie in JavaScript: The data structure behind autocomplete
#32 function trieBuilder(word_list) {
const root = {};
for (const word of words_list) {
let node = root;
for (const char of word) {
let nextNode = node[char];
if (!nextNode) node[char] = nextNode = {};
node = nextNode;
}
node._ = 1; // mark the nodes that are endings of real words
}
function findChildren(node, prefix, list, maxLength) {
if (node._ === 1) list.push(prefix);
for (const char in node) {
findChildren(node[char], prefix + char, list, maxLength);
if (list.length >= maxLength) return list;
}
return list;
}
function findSuffixes(prefix, maxLength) {
prefix = prefix.toLowerCase();
let node = root;
for (const char of prefix) {
let nextNode = node[char];
if (!nextNode) return [""];
node = nextNode;
}
let words = findChildren(node, prefix, [], maxLength);
return words;
}
return {root, findSuffixes};
}
demo: https://observablehq.com/@jobleonard/autocompleteRe: Trie in JavaScript: The data structure behind autocomplete
#33Tries are fun structures! However, for autocomplete you often want a weighted Trie because you have extra information you want to weight nodes by. An example with contacts is that you often want recent and frequent contacts. My company has an open source trie implementation here for a client to do weighted contact auto complete: https://github.com/shortwave/trie
I first learned about Tries when I implemented a spell checker, almost 20 years ago now, with basic suggestions. It’s amazing how easy it is to get an efficient, 80% spell checker and recommendation engine implemented from scratch once you dig into it. Tries make for an efficient enough in memory lookup structure (space, storage, and compute), and are an obvious part of a suggestion engine as well. Coupled with a bas…
Re: Trie in JavaScript: The data structure behind autocomplete
#34Trie is probably my favourite ds. I really enjoy this python implementation I learned while studying leetcode because its so succint. Really useful for interviews. from collections import defaultdict END = object() def make_trie(): return defaultdict(make_trie) def insert(trie, word): for c in word: trie = trie[c] trie[END] = True
Re: Trie in JavaScript: The data structure behind autocomplete
#35There is another aspect of tries that make them really useful: you can do fast, fuzzy word searches on a trie. In Typesense[1], I've implemented fuzzy search based on levenshtein damerau distance and it's incredibly fast. I've found this approach to be a much better (faster + more flexible) alternative to Peter Norvig's brute-force based spell-checker that is quite a popular post [2]. [1]: https://github.com/typesens…
Re: Trie in JavaScript: The data structure behind autocomplete
#36Trie is probably my favourite ds. I really enjoy this python implementation I learned while studying leetcode because its so succint. Really useful for interviews. from collections import defaultdict END = object() def make_trie(): return defaultdict(make_trie) def insert(trie, word): for c in word: trie = trie[c] trie[END] = True
I can't figure out how this works. Shouldn't insert return a reference to the new trie?
{'f': {'o': {'o': {END: True}}}}
I don't think this implementation is efficient enough to ever be worth using in a real program, though.Re: Trie in JavaScript: The data structure behind autocomplete
#37Re: Trie in JavaScript: The data structure behind autocomplete
#38There is another aspect of tries that make them really useful: you can do fast, fuzzy word searches on a trie. In Typesense[1], I've implemented fuzzy search based on levenshtein damerau distance and it's incredibly fast. I've found this approach to be a much better (faster + more flexible) alternative to Peter Norvig's brute-force based spell-checker that is quite a popular post [2]. [1]: https://github.com/typesens…
Re: Trie in JavaScript: The data structure behind autocomplete
#39It's such a simple and elegant as data structure. If you only care about word lookup you can implement it in just 35 lines of modern JavaScript: function trieBuilder(word_list) { const root = {}; for (const word of words_list) { let node = root; for (const char of word) { let nextNode = node[char]; if (!nextNode) node[char] = nextNode = {}; node = nextNode; } node._ = 1; // mark the nodes that are endings of real wor…
There is an option to get all suffixes without traversing subtree, but it comes with extra O(N) memory where N is combined length of all stored words - depending on case might be acceptable since memory for storing words itself is O(N) anyway. https://stackoverflow.com/a/29966616/2104560 (update 1 and update 3)
Re: Trie in JavaScript: The data structure behind autocomplete
#40It's https://en.wikipedia.org/wiki/Hash_array_mapped_trie which used in Scala's immutableMap https://dotty.epfl.ch/api/scala/collection/immutable/HashMap...