Trie data structures for JavaScript. See docs.
⚠️ Depending on your environment, the code may requireregeneratorRuntime
to be defined, for instance by importing regenerator-runtime/runtime.
import {Trie, ArrayNode} from '@trie-data-structure/uncompressed-trie';
import {range} from '@iterable-iterator/range';
import {map} from '@iterable-iterator/map';
const encode = (key) => map((i) => key.charCodeAt(i), range(key.length));
const degree = 256;
const trie = new Trie(new ArrayNode(degree));
const set = (key, value) => trie.set(encode(key), value);
const get = (key) => trie.get(encode(key));
set('abra', 1);
set('cadabra', 2);
set('abracadabra', 3);
get('abra'); // 1
get('cadabra'); // 2
get('abracadabra'); // 3