Compact Prefix Tree
A serializable compact prefix tree (also known as Radix tree or Patricia tree or space-optimized trie) implementation in JavaScript.
Usage
Installation
npm install compact-prefix-tree
General Usage
;// ORconst CompactPrefixTree = ;const items ="http://www.example.com/foo/""http://www.example.com/baz/";// create a trie from array of stringsconst trie = items;// can add items latertrie;// look for a prefix of a wordconst p1 = trieprefix"http://www.example.com/john/doe";// p1: { prefix: "http://www.example.com/john/", isProper: true }const p2 = trieprefix"http://www.example.com/bazinga";// p2: { prefix: "http://www.example.com/", isProper: false }// above is not proper as it doesn't exist in list of items provided
Serialization
const CompactPrefixTree getWordsFromTrie = ;const items ="http://www.example.com/foo/""https://www.example.com/baz/";const trie = items;const serialized = JSON;// {// "http": {// "://www.example.com/foo/": null,// "s://www.example.com/baz/": null// }// }const words = ;// Set(2) {"http://www.example.com/foo/" "https://www.example.com/baz/"}const trie2 = Array;// assert(isEqual(trie.items, trie2.items));
License
[MIT License] Copyright 2018 Sid Vishnoi (https://sidvishnoi.github.io)