trie-typed
Version:
Trie, prefix tree
136 lines (135 loc) • 6.4 kB
TypeScript
/**
* data-structure-typed
*
* @author Pablo Zeng
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
* @license MIT License
*/
import type { AVLTreeMultiMapOptions, AVLTreeOptions, ElemOf, EntryCallback, IterationType } from '../../types';
import { AVLTree, AVLTreeNode } from './avl-tree';
import { IBinaryTree } from '../../interfaces';
/**
* Node used by AVLTreeMultiMap; stores the key with a bucket of values (array).
* @remarks Time O(1), Space O(1)
* @template K
* @template V
*/
export declare class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
parent?: AVLTreeMultiMapNode<K, V>;
/**
* Create an AVLTreeMultiMap node with a value bucket.
* @remarks Time O(1), Space O(1)
* @param key - Key of the node.
* @param value - Initial array of values.
* @returns New AVLTreeMultiMapNode instance.
*/
constructor(key: K, value: V[]);
_left?: AVLTreeMultiMapNode<K, V> | null | undefined;
/**
* Get the left child pointer.
* @remarks Time O(1), Space O(1)
* @returns Left child node, or null/undefined.
*/
get left(): AVLTreeMultiMapNode<K, V> | null | undefined;
/**
* Set the left child and update its parent pointer.
* @remarks Time O(1), Space O(1)
* @param v - New left child node, or null/undefined.
* @returns void
*/
set left(v: AVLTreeMultiMapNode<K, V> | null | undefined);
_right?: AVLTreeMultiMapNode<K, V> | null | undefined;
/**
* Get the right child pointer.
* @remarks Time O(1), Space O(1)
* @returns Right child node, or null/undefined.
*/
get right(): AVLTreeMultiMapNode<K, V> | null | undefined;
/**
* Set the right child and update its parent pointer.
* @remarks Time O(1), Space O(1)
* @param v - New right child node, or null/undefined.
* @returns void
*/
set right(v: AVLTreeMultiMapNode<K, V> | null | undefined);
}
/**
* AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
* @remarks Time O(1), Space O(1)
* @template K
* @template V
* @template R
*/
export declare class AVLTreeMultiMap<K = any, V = any, R extends object = object> extends AVLTree<K, V[], R> implements IBinaryTree<K, V[], R> {
/**
* Create an AVLTreeMultiMap and optionally bulk-insert items.
* @remarks Time O(N log N), Space O(N)
* @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
* @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
* @returns New AVLTreeMultiMap instance.
*/
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: AVLTreeMultiMapOptions<K, V[], R>);
_createNode(key: K, value?: V[]): AVLTreeMultiMapNode<K, V>;
add(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
add(key: K, value: V): boolean;
/**
* Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
* @remarks Time O(log N), Space O(1)
* @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
* @param value - Value to remove from the bucket.
* @returns True if the value was removed; false if not found.
*/
deleteValue(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
/**
* Rebuild the tree into a perfectly balanced form using in-order nodes.
* @remarks Time O(N), Space O(N)
* @param [iterationType] - Traversal style to use when constructing the balanced tree.
* @returns True if rebalancing succeeded (tree not empty).
*/
perfectlyBalance(iterationType?: IterationType): boolean;
/**
* Create a new tree by mapping each [key, values] bucket.
* @remarks Time O(N log N), Space O(N)
* @template MK
* @template MVArr
* @template MR
* @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
* @param [options] - Options for the output tree.
* @param [thisArg] - Value for `this` inside the callback.
* @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
*/
map<MK = K, MVArr extends unknown[] = V[], MR extends object = object>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<AVLTreeOptions<MK, MVArr, MR>>, thisArg?: unknown): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
/**
* Create a new tree by mapping each [key, values] bucket.
* @remarks Time O(N log N), Space O(N)
* @template MK
* @template MV
* @template MR
* @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
* @param [options] - Options for the output tree.
* @param [thisArg] - Value for `this` inside the callback.
* @returns A new AVLTree when mapping to non-array values; see overloads.
*/
map<MK = K, MV = V[], MR extends object = object>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<AVLTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
/**
* (Protected) Create an empty instance of the same concrete class.
* @remarks Time O(1), Space O(1)
* @template TK
* @template TV
* @template TR
* @param [options] - Optional constructor options for the like-kind instance.
* @returns An empty like-kind instance.
*/
protected _createInstance<TK = K, TV = V, TR extends object = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this;
/**
* (Protected) Create a like-kind instance and seed it from an iterable.
* @remarks Time O(N log N), Space O(N)
* @template TK
* @template TV
* @template TR
* @param iter - Iterable used to seed the new tree.
* @param [options] - Options merged with the current snapshot.
* @returns A like-kind AVLTree built from the iterable.
*/
protected _createLike<TK = K, TV = V, TR extends object = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
}