UNPKG

@kamilmielnik/trie

Version:

Trie data structure implementation in TypeScript. Highly performant. No dependencies. Built for a Scrabble Solver.

99 lines (98 loc) 3.78 kB
import type { Descendant, Node, TraverseCallback, TraverseOptions } from './types'; /** * A class representing the {@link https://en.wikipedia.org/wiki/Trie | Trie data structure}. */ export declare class Trie { /** * Creates a new {@link Trie} by deserializing given string. * * The inverse of {@link Trie.serialize | serialize}. * * @param serialized - String with serialized data. * @returns {@link Trie} representing deserialized data. */ static deserialize(serialized: string): Trie; /** * Creates a new {@link Trie} based on array of words. * * @param words - array of words to put in the {@link Trie}. * @returns New {@link Trie} containing all given words. */ static fromArray(words: string[]): Trie; /** * Represents the root {@link Node} of the {@link Trie}. * It's not a copy. Mutate at your own risk. */ readonly root: Node; /** * Creates a new {@link Trie} with optionally given root {@link Node}. * * @param root - Root {@link Node} of the {@link Trie} to be created. */ constructor(root?: Node); /** * Inserts given word into the {@link Trie}. * * @param word - Word to be inserted into the {@link Trie}. * @returns {@link Node} representing the end of the added word. */ add(word: string): Node; /** * Finds {@link Node} representing given prefix in the {@link Trie}. * * @param prefix - Prefix to be found. * @returns {@link Node} representing a given prefix, undefined if there is no such node. */ find(prefix: string): Node | undefined; /** * Tells you whether given word is in the {@link Trie}. * * @param word - Word to be found. * @returns true if given word is in the {@link Trie}, false otherwise. */ has(word: string): boolean; /** * Tells you whether there are any words with given prefix in the {@link Trie}. * * See: https://en.wikipedia.org/wiki/String_operations#Prefixes * * @param prefix - Prefix to be found. * @returns true if there are any words with given prefix in the {@link Trie}, false otherwise. */ hasPrefix(prefix: string): boolean; /** * Removes given word from the {@link Trie} if it exists. * * @param word - Word to be removed. * @returns true if the word was removed, false otherwise. */ remove(word: string): boolean; /** * Converts the {@link Trie} into a string. * * The inverse of {@link Trie.deserialize | deserialize}. * * It serializes {@link https://sjp.pl/slownik/growy/ | 42.8 MB Polish dictionary} down to 18.7 MB (-56%). * * It serializes {@link https://www.wordgamedictionary.com/twl06/download/twl06.txt | 1.9 MB English (US) dictionary} down to 1.4 MB (-30%). * * It serializes {@link https://www.wordgamedictionary.com/sowpods/download/sowpods.txt | 3 MB English (GB) dictionary} down to 2 MB (-32%). * * @returns String with serialized data. */ serialize(): string; /** * Finds all {@link Descendant | descendants} of the {@link Trie | Trie's} root and returns them as an array. * * @param options - See {@link TraverseOptions}. * @returns An array of {@link Descendant | descendants}. */ toArray(options?: TraverseOptions): Descendant[]; /** * Visits every descendant {@link Node} of the {@link Trie} and calls a callback. * * @param callback - Callback that will be called for each visited {@link Node}. Return true from callback to stop traversing. * @param options - See {@link TraverseOptions}. */ traverse(callback: TraverseCallback, options?: TraverseOptions): void; }