Package Exports
- @kamilmielnik/trie
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@kamilmielnik/trie) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@kamilmielnik/trie
Trie data structure implementation (in TypeScript). Highly performant. Built for a Scrabble Solver.
Installation
npm install --save @kamilmielnik/trieUsage
import Trie from '@kamilmielnik/trie';
const trie = new Trie();
trie.add('master');
trie.add('mask');
console.log(trie.hasPrefix('man')); // false
console.log(trie.hasPrefix('mas')); // true
console.log(trie.has('mas')); // false
console.log(trie.has('master')); // true
console.log(trie.serialize()); // "(m(a(s(t(e(r)),k))))"API
Trie
A class representing the Trie data structure.
Import
import Trie from '@kamilmielnik/trie';Static functions
Trie.deserialize(serialized: string): TrieThe inverse of
Trie.prototype.serialize.Trie.fromArray(words: string[]): TrieBuilds a
Trieinstance based on array of words.
Instance methods
Trie.prototype.add(word: string): voidInserts
wordinto theTrieinstance.Trie.prototype.has(word: string): booleanReturns
trueif givenwordis in theTrieinstance.Trie.prototype.hasPrefix(prefix: string): booleanReturns
trueif there are any words with givenprefix.Trie.prototype.serialize(): stringThe inverse of
Trie.deserialize.It serializes 41 MB Polish dictionary down to 15 MB (63%).
It serializes 1.9 MB English (US) dictionary down to 1.2 MB (37%).
It serializes 2.9 MB English (GB) dictionary down to 1.8 MB (38%).
Trie.prototype.toJson(): NodeReturns the root
Nodeof theTrie. It's not a copy, it's not safe to mutate (due to performance optimization).
Node
It's a type (TypeScript-only).
import { Node } from '@kamilmielnik/trie';Properties
[key: string]: Nodekeyis a single character (string of length 1).wordEnd?: trueIndicates that keys of all parent nodes make a valid word when joined together.