UNPKG

tree-multimap-typed

Version:
183 lines (182 loc) 8.61 kB
/** * data-structure-typed * * @author Pablo Zeng * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com> * @license MIT License */ import type { AVLTreeCounterOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, IterationType } from '../../types'; import { IBinaryTree } from '../../interfaces'; import { AVLTree, AVLTreeNode } from './avl-tree'; /** * AVL node with an extra 'count' field; keeps parent/child links. * @remarks Time O(1), Space O(1) * @template K * @template V */ export declare class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> { parent?: AVLTreeCounterNode<K, V>; /** * Create an AVL counter node. * @remarks Time O(1), Space O(1) * @param key - Key of the node. * @param [value] - Associated value (ignored in map mode). * @param [count] - Initial count for this node (default 1). * @returns New AVLTreeCounterNode instance. */ constructor(key: K, value?: V, count?: number); _left?: AVLTreeCounterNode<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(): AVLTreeCounterNode<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: AVLTreeCounterNode<K, V> | null | undefined); _right?: AVLTreeCounterNode<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(): AVLTreeCounterNode<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: AVLTreeCounterNode<K, V> | null | undefined); } /** * AVL tree that tracks an aggregate 'count' across nodes; supports balanced insert/delete and map-like mode. * @remarks Time O(1), Space O(1) * @template K * @template V * @template R */ export declare class AVLTreeCounter<K = any, V = any, R extends object = object> extends AVLTree<K, V, R> implements IBinaryTree<K, V, R> { /** * Create a AVLTreeCounter instance * @remarks Time O(n), Space O(n) * @param keysNodesEntriesOrRaws * @param options * @returns New AVLTreeCounterNode instance. */ constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeCounterOptions<K, V, R>); protected _count: number; get count(): number; /** * Compute the total count by traversing the tree (sums node.count). * @remarks Time O(N), Space O(H) * @returns Total count recomputed from nodes. */ getComputedCount(): number; _createNode(key: K, value?: V, count?: number): AVLTreeCounterNode<K, V>; /** * Type guard: check whether the input is an AVLTreeCounterNode. * @remarks Time O(1), Space O(1) * @returns True if the value is an AVLTreeCounterNode. */ isNode(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeCounterNode<K, V>; /** * Insert or increment a node and update aggregate count. * @remarks Time O(log N), Space O(1) * @param keyNodeOrEntry - Key, node, or [key, value] entry to insert. * @param [value] - Value when a bare key is provided (ignored in map mode). * @param [count] - How much to increase the node's count (default 1). * @returns True if inserted/updated; false if ignored. */ add(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean; /** * Delete a node (or decrement its count) and rebalance if needed. * @remarks Time O(log N), Space O(1) * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node. * @param [ignoreCount] - If true, remove the node regardless of its count. * @returns Array of deletion results including deleted node and a rebalance hint when present. */ delete(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, ignoreCount?: boolean): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[]; /** * Remove all nodes and reset aggregate counters. * @remarks Time O(N), Space O(1) * @returns void */ clear(): void; /** * 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; /** * Deep copy this tree, preserving map mode and aggregate counts. * @remarks Time O(N), Space O(N) * @returns A deep copy of this tree. */ clone(): this; /** * Create a new AVLTreeCounter by mapping each [key, value] entry. * @remarks Time O(N log N), Space O(N) * @template MK * @template MV * @template MR * @param callback - Function mapping (key, value, index, tree) → [newKey, newValue]. * @param [options] - Options for the output tree. * @param [thisArg] - Value for `this` inside the callback. * @returns A new AVLTreeCounter with mapped entries. */ map<MK = K, MV = V, MR extends object = object>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTreeCounter<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<AVLTreeCounterOptions<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 AVLTreeCounter built from the iterable. */ protected _createLike<TK = K, TV = V, TR extends object = R>(iter?: Iterable<TK | AVLTreeCounterNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>): AVLTreeCounter<TK, TV, TR>; /** * (Protected) Normalize input into a node plus its effective value and count. * @remarks Time O(1), Space O(1) * @param keyNodeOrEntry - Key, node, or [key, value] entry. * @param [value] - Value used when a bare key is provided. * @param [count] - Count increment to apply (default 1). * @returns Tuple [node, value] where node may be undefined. */ protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): [AVLTreeCounterNode<K, V> | undefined, V | undefined]; /** * (Protected) Swap keys/values/counters between the source and destination nodes. * @remarks Time O(1), Space O(1) * @param srcNode - Source node (or key) whose properties will be moved. * @param destNode - Destination node (or key) to receive properties. * @returns Destination node after swap, or undefined. */ protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>, destNode: BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>): AVLTreeCounterNode<K, V> | undefined; /** * (Protected) Replace one node by another and adjust counters accordingly. * @remarks Time O(1), Space O(1) * @param oldNode - Node being replaced. * @param newNode - Replacement node. * @returns The new node after replacement. */ protected _replaceNode(oldNode: AVLTreeCounterNode<K, V>, newNode: AVLTreeCounterNode<K, V>): AVLTreeCounterNode<K, V>; }