@picosearch/bk-tree
Version:
Simple, zero dependency, type-safe implementation of a BK-Tree data structure.
43 lines (42 loc) • 1.37 kB
TypeScript
import type { BKTreeNode, DistanceFunction, IBKTree } from './types';
export declare class BKTree implements IBKTree {
private root;
private getDistance;
constructor({ getDistance, root, }?: {
getDistance?: DistanceFunction;
root?: BKTreeNode;
});
/**
* Insert a word into the tree.
*
* @param word - The word to insert.
*/
insert(word: string): void;
/**
* Find all words in the tree that are within a given edit distance of the query.
*
* @param query - The query to find words for.
* @param options - The options to use for the find operation.
* @param options.maxError - The maximum edit distance.
* @param options.limit - The maximum number of results to return.
* @returns An array of words that are within the given edit distance of the query.
*/
find(query: string, options?: {
maxError?: number;
limit?: number;
}): string[];
/**
* Lookup the closest word to the query.
*
* @param query - The query to lookup.
* @returns The closest word to the query, or `null` if the tree is empty.
*/
lookup(query: string): string | null;
/**
* Serialize the tree to a JSON string.
*
* @returns The serialized tree.
*/
toJSON(): string;
static fromJSON(jsonStr: string): BKTree;
}