UNPKG

heap-typed

Version:

Heap. Javascript & Typescript Data Structure.

199 lines (198 loc) 11.9 kB
/** * data-structure-typed * * @author Pablo Zeng * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com> * @license MIT License */ import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, IterationType, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types'; import { IBinaryTree } from '../../interfaces'; import { RedBlackTree, RedBlackTreeNode } from './rb-tree'; export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> { /** * The constructor function initializes a Red-Black Tree node with a key, value, count, and color. * @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is * used to identify and locate the node within the tree. * @param {V} [value] - The `value` parameter is an optional parameter that represents the value * associated with the key in the Red-Black Tree node. It is not required and can be omitted when * creating a new node. * @param [count=1] - The `count` parameter represents the number of occurrences of a particular key * in the Red-Black Tree. It is an optional parameter with a default value of 1. * @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node * in a Red-Black Tree. It is optional and has a default value of `'BLACK'`. */ constructor(key: K, value?: V, count?: number, color?: RBTNColor); protected _count: number; /** * The function returns the value of the private variable _count. * @returns The count property of the object, which is of type number. */ get count(): number; /** * The above function sets the value of the count property. * @param {number} value - The value parameter is of type number, which means it can accept any * numeric value. */ set count(value: number); } export declare class TreeMultiMap<K = any, V = any, R = object, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>> extends RedBlackTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> { /** * The constructor function initializes a TreeMultiMap object with optional initial data. * @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an * iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the * TreeMultiMap with initial data. * @param [options] - The `options` parameter is an optional object that can be used to customize the * behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and * `compareValues`, which are functions used to compare keys and values respectively. */ constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, NODE>>, options?: TreeMultiMapOptions<K, V, R>); protected _count: number; /** * The function calculates the sum of the count property of all nodes in a tree structure. * @returns the sum of the count property of all nodes in the tree. */ get count(): number; /** * Time Complexity: O(n) * Space Complexity: O(1) * * The function calculates the sum of the count property of all nodes in a tree using depth-first * search. * @returns the sum of the count property of all nodes in the tree. */ getComputedCount(): number; /** * The function creates a new TreeMultiMapNode with the specified key, value, color, and count. * @param {K} key - The key parameter represents the key of the node being created. It is of type K, * which is a generic type representing the type of keys in the tree. * @param {V} [value] - The `value` parameter is an optional parameter that represents the value * associated with the key in the node. It is of type `V`, which can be any data type. * @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in * a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'. * @param {number} [count] - The `count` parameter represents the number of occurrences of a key in * the tree. It is an optional parameter and is used to keep track of the number of values associated * with a key in the tree. * @returns A new instance of the TreeMultiMapNode class, casted as NODE. */ createNode(key: K, value?: V, color?: RBTNColor, count?: number): NODE; /** * The function creates a new instance of a TreeMultiMap with the specified options and returns it. * @param [options] - The `options` parameter is an optional object that contains additional * configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V, * R>`. * @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the * existing `iterationType` property. The returned value is casted as `TREE`. */ createTree(options?: TreeMultiMapOptions<K, V, R>): TREE; /** * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a * node based on the input. * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`. * @param {V} [value] - The `value` parameter is an optional value that represents the value * associated with the key in the node. It is used when creating a new node or updating the value of * an existing node. * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of * times the key-value pair should be added to the data structure. If not provided, it defaults to 1. * @returns either a NODE object or undefined. */ keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined]; /** * The function checks if the input is an instance of the TreeMultiMapNode class. * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`. * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is * an instance of the `TreeMultiMapNode` class. */ isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE; /** * Time Complexity: O(log n) * Space Complexity: O(1) * * The function overrides the add method of a class and adds a new node to a data structure, updating * the count and returning a boolean indicating success. * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The * `keyNodeEntryOrRaw` parameter can accept one of the following types: * @param {V} [value] - The `value` parameter represents the value associated with the key in the * data structure. It is an optional parameter, so it can be omitted if not needed. * @param [count=1] - The `count` parameter represents the number of times the key-value pair should * be added to the data structure. By default, it is set to 1, meaning that if no value is provided * for `count`, the key-value pair will be added once. * @returns The method is returning a boolean value. It returns true if the addition of the new node * was successful, and false otherwise. */ add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): boolean; /** * Time Complexity: O(log n) * Space Complexity: O(1) * * The function `delete` in TypeScript overrides the deletion operation in a binary tree data * structure, handling cases where nodes have children and maintaining balance in the tree. * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate` * parameter in the `delete` method is used to specify the condition or key based on which a node * should be deleted from the binary tree. It can be a key, a node, or an entry. * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a * boolean flag that determines whether to ignore the count of nodes when performing deletion. If * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If * `ignoreCount` is `false * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects. */ delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, ignoreCount?: boolean): BinaryTreeDeleteResult<NODE>[]; /** * Time Complexity: O(1) * Space Complexity: O(1) * * The "clear" function overrides the parent class's "clear" function and also resets the count to * zero. */ clear(): void; /** * Time Complexity: O(n log n) * Space Complexity: O(log n) * * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search * tree using either a recursive or iterative approach. * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that * specifies the type of iteration to use when building the balanced binary search tree. It has a * default value of `this.iterationType`, which means it will use the iteration type specified by the * `iterationType` property of the current object. * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the * balancing operation is successful, and `false` if there are no nodes to balance. */ perfectlyBalance(iterationType?: IterationType): boolean; /** * Time complexity: O(n) * Space complexity: O(n) * * The function overrides the clone method to create a deep copy of a tree object. * @returns The `clone()` method is returning a cloned instance of the `TREE` object. */ clone(): TREE; /** * Time Complexity: O(1) * Space Complexity: O(1) * * The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes * in a binary search tree. * @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node * that will be swapped with the `destNode`. It can be either an instance of the `R` class or an * instance of the `BSTNOptKeyOrNode<K, NODE>` class. * @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination * node where the properties will be swapped with the source node. * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`. * If either `srcNode` or `destNode` is undefined, it returns undefined. */ protected _swapProperties(srcNode: R | BSTNOptKeyOrNode<K, NODE>, destNode: R | BSTNOptKeyOrNode<K, NODE>): NODE | undefined; /** * Time Complexity: O(1) * Space Complexity: O(1) * * The function replaces an old node with a new node and updates the count property of the new node. * @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data * structure. * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class. * @returns The method is returning the result of calling the `_replaceNode` method from the * superclass, which is of type `NODE`. */ protected _replaceNode(oldNode: NODE, newNode: NODE): NODE; }