@kamilmielnik/trie
Version:
Trie data structure implementation in TypeScript. Highly performant. No dependencies. Built for a Scrabble Solver.
125 lines (124 loc) • 4.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Trie = void 0;
const lib_1 = require("./lib");
/**
* A class representing the {@link https://en.wikipedia.org/wiki/Trie | Trie data structure}.
*/
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) {
return new Trie((0, lib_1.deserialize)(serialized));
}
/**
* 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) {
return new Trie((0, lib_1.fromArray)(words));
}
/**
* Represents the root {@link Node} of the {@link Trie}.
* It's not a copy. Mutate at your own risk.
*/
root;
/**
* 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 = {}) {
this.root = root;
}
/**
* 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) {
return (0, lib_1.add)(this.root, word);
}
/**
* 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) {
return (0, lib_1.find)(this.root, prefix);
}
/**
* 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) {
return (0, lib_1.has)(this.root, word);
}
/**
* 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) {
return (0, lib_1.hasPrefix)(this.root, prefix);
}
/**
* 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) {
return (0, lib_1.remove)(this.root, word);
}
/**
* 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() {
return (0, lib_1.serialize)(this.root);
}
/**
* 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) {
return (0, lib_1.toArray)(this.root, options);
}
/**
* 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, options) {
return (0, lib_1.traverse)(this.root, callback, options);
}
}
exports.Trie = Trie;