directed-graph-typed
Advanced tools
Comparing version 1.54.0 to 1.54.1
@@ -8,200 +8,94 @@ /** | ||
*/ | ||
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType } from '../../types'; | ||
import { AVLTreeMultiMapOptions, BTNRep, OptNodeOrNull } from '../../types'; | ||
import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> { | ||
export declare class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> { | ||
/** | ||
* The constructor function initializes a BinaryTreeNode object with a key, value, and count. | ||
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier | ||
* of the binary tree node. | ||
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary | ||
* tree node. If no value is provided, it will be `undefined`. | ||
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value | ||
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count` | ||
* parameter when creating a new instance of the `BinaryTreeNode` class. | ||
* This TypeScript constructor initializes an object with a key of type K and an array of values of | ||
* type V. | ||
* @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the | ||
* data being stored in the data structure. It helps in quickly accessing or retrieving the | ||
* associated value in the data structure. | ||
* @param {V[]} value - The `value` parameter in the constructor represents an array of values of | ||
* type `V`. | ||
*/ | ||
constructor(key: K, value?: V, count?: number); | ||
constructor(key: K, value: V[]); | ||
parent?: AVLTreeMultiMapNode<K, V>; | ||
_left?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>; | ||
get left(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>>; | ||
set left(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>); | ||
_right?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>; | ||
get right(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>>; | ||
set right(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>); | ||
} | ||
/** | ||
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters. | ||
* | ||
*/ | ||
export declare class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>, TREE extends AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE>>> extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> { | ||
export declare class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends AVLTree<K, V[], R, MK, MV[], MR> implements IBinaryTree<K, V[], R, MK, MV, MR> { | ||
/** | ||
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable object that can contain either keys, nodes, entries, or raw elements. | ||
* @param [options] - The `options` parameter is an optional object that can be used to customize the | ||
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and | ||
* `compareValues` functions to define custom comparison logic for keys and values, respectively. | ||
* The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data | ||
* and options. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either key-value pairs represented as `BTNRep<K, V[], | ||
* AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize | ||
* the AVLTreeMulti | ||
* @param [options] - The `options` parameter in the constructor is of type | ||
* `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify | ||
* additional options for configuring the AVLTreeMultiMap instance. | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: AVLTreeMultiMapOptions<K, V, R>); | ||
protected _count: number; | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | R>, options?: AVLTreeMultiMapOptions<K, V[], R>); | ||
/** | ||
* 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. | ||
*/ | ||
get count(): number; | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* 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. | ||
* The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with | ||
* specified options. | ||
* @param [options] - The `options` parameter in the `createTree` function is of type | ||
* `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type | ||
* `K`, `V[]`, and `R`. The function creates a new `AVL | ||
* @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the | ||
* provided options. | ||
*/ | ||
getComputedCount(): number; | ||
createTree(options?: AVLTreeMultiMapOptions<K, V[], R>): AVLTreeMultiMap<K, V, R, MK, MV, MR>; | ||
/** | ||
* The function creates a new AVLTreeMultiMapNode with the specified key, value, 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 that can be replaced with any specific type when using the function. | ||
* @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 {number} [count] - The `count` parameter represents the number of occurrences of a | ||
* key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when | ||
* calling the `createNode` method. If provided, it specifies the initial count for the node. | ||
* @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE. | ||
*/ | ||
createNode(key: K, value?: V, count?: number): NODE; | ||
/** | ||
* The function creates a new AVLTreeMultiMap object with the specified options and returns it. | ||
* @param [options] - The `options` parameter is an optional object that contains additional | ||
* configuration options for creating the AVLTreeMultiMap. It can have the following properties: | ||
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE | ||
* object. | ||
*/ | ||
createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE; | ||
/** | ||
* The function checks if the input is an instance of AVLTreeMultiMapNode. | ||
* @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 `AVLTreeMultiMapNode` class. | ||
*/ | ||
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function overrides the add method of a TypeScript class to add a new node to a data structure | ||
* and update the count. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The | ||
* `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It | ||
* can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node, | ||
* entry, or raw element | ||
* @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 the key-value pair will | ||
* be added once. However, you can specify a different value for `count` if you want to add | ||
* @returns a boolean value. | ||
* The function `createNode` overrides the method to create a new AVLTreeMultiMapNode with a | ||
* specified key and an empty array of values. | ||
* @param {K} key - The `key` parameter in the `createNode` method represents the key of the node | ||
* that will be created in the AVLTreeMultiMap. | ||
* @returns An AVLTreeMultiMapNode object is being returned, initialized with the provided key and an | ||
* empty array. | ||
*/ | ||
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): boolean; | ||
createNode(key: K): AVLTreeMultiMapNode<K, V>; | ||
add(node: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>>): boolean; | ||
add(key: K, value: V): boolean; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
* The function overrides the delete method in a binary tree data structure, handling deletion of | ||
* nodes 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 for deleting a node from the | ||
* binary tree. It can be a key, node, or entry that determines which | ||
* node(s) should be deleted. | ||
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a | ||
* boolean flag that determines whether to ignore the count of the node being deleted. If | ||
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If | ||
* `ignoreCount` is set to | ||
* @returns The `delete` method overrides the default delete behavior in a binary tree data | ||
* structure. It takes a predicate or node to be deleted and an optional flag to ignore count. The | ||
* method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the | ||
* deleted node and whether balancing is needed in the tree. | ||
*/ | ||
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 currently set in | ||
* the 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. | ||
* The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data | ||
* structure and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value | ||
* pair in the AVLTreeMultiMapNode, or just the key itself. | ||
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific | ||
* value that you want to delete from the multi-map data structure associated with a particular key. | ||
* The function checks if the value exists in the array of values associated with the key, and if | ||
* found, removes it from the array. | ||
* @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified | ||
* `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If | ||
* the value was not found in the array, it returns `false`. | ||
*/ | ||
clone(): TREE; | ||
deleteValue(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value: V): boolean; | ||
/** | ||
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap | ||
* with modified entries based on a provided callback. | ||
* @param callback - The `callback` parameter is a function that will be called for each entry in the | ||
* AVLTreeMultiMap. It takes four arguments: | ||
* @param [options] - The `options` parameter in the `override map` function is of type | ||
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional | ||
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function. | ||
* These options | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function. This can be useful when you want to access properties | ||
* or | ||
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries | ||
* transformed by the provided `callback` function. Each entry in the original tree is passed to the | ||
* `callback` function along with the index and the original tree itself. The transformed entries are | ||
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end. | ||
*/ | ||
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: AVLTreeMultiMapOptions<MK, MV, MR>, thisArg?: any): AVLTreeMultiMap<MK, MV, MR>; | ||
/** | ||
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into | ||
* a node object. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The | ||
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `override` function. It represents the value associated with the key in the data structure. If no | ||
* value is provided, it will default to `undefined`. | ||
* @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. | ||
*/ | ||
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined]; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `_swapProperties` function swaps the properties (key, value, count, height) 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`. | ||
* @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`. | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns A cloned tree object is being returned. | ||
*/ | ||
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 represents the node that needs to be replaced in the | ||
* data structure. It is of type NODE. | ||
* @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; | ||
clone(): AVLTreeMultiMap<K, V, R, MK, MV, MR>; | ||
} |
@@ -7,389 +7,184 @@ "use strict"; | ||
/** | ||
* The constructor function initializes a BinaryTreeNode object with a key, value, and count. | ||
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier | ||
* of the binary tree node. | ||
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary | ||
* tree node. If no value is provided, it will be `undefined`. | ||
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value | ||
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count` | ||
* parameter when creating a new instance of the `BinaryTreeNode` class. | ||
* This TypeScript constructor initializes an object with a key of type K and an array of values of | ||
* type V. | ||
* @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the | ||
* data being stored in the data structure. It helps in quickly accessing or retrieving the | ||
* associated value in the data structure. | ||
* @param {V[]} value - The `value` parameter in the constructor represents an array of values of | ||
* type `V`. | ||
*/ | ||
constructor(key, value, count = 1) { | ||
constructor(key, value) { | ||
super(key, value); | ||
this.count = count; | ||
this.parent = undefined; | ||
this._left = undefined; | ||
this._right = undefined; | ||
} | ||
get left() { | ||
return this._left; | ||
} | ||
set left(v) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._left = v; | ||
} | ||
get right() { | ||
return this._right; | ||
} | ||
set right(v) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._right = v; | ||
} | ||
} | ||
exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode; | ||
/** | ||
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters. | ||
* | ||
*/ | ||
class AVLTreeMultiMap extends avl_tree_1.AVLTree { | ||
/** | ||
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable object that can contain either keys, nodes, entries, or raw elements. | ||
* @param [options] - The `options` parameter is an optional object that can be used to customize the | ||
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and | ||
* `compareValues` functions to define custom comparison logic for keys and values, respectively. | ||
* The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data | ||
* and options. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either key-value pairs represented as `BTNRep<K, V[], | ||
* AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize | ||
* the AVLTreeMulti | ||
* @param [options] - The `options` parameter in the constructor is of type | ||
* `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify | ||
* additional options for configuring the AVLTreeMultiMap instance. | ||
*/ | ||
constructor(keysNodesEntriesOrRaws = [], options) { | ||
super([], options); | ||
this._count = 0; | ||
if (keysNodesEntriesOrRaws) | ||
super([], Object.assign(Object.assign({}, options), { isMapMode: true })); | ||
if (keysNodesEntriesOrRaws) { | ||
this.addMany(keysNodesEntriesOrRaws); | ||
} | ||
} | ||
/** | ||
* 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. | ||
*/ | ||
get count() { | ||
return this._count; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* 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. | ||
* The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with | ||
* specified options. | ||
* @param [options] - The `options` parameter in the `createTree` function is of type | ||
* `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type | ||
* `K`, `V[]`, and `R`. The function creates a new `AVL | ||
* @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the | ||
* provided options. | ||
*/ | ||
getComputedCount() { | ||
let sum = 0; | ||
this.dfs(node => (sum += node.count)); | ||
return sum; | ||
} | ||
/** | ||
* The function creates a new AVLTreeMultiMapNode with the specified key, value, 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 that can be replaced with any specific type when using the function. | ||
* @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 {number} [count] - The `count` parameter represents the number of occurrences of a | ||
* key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when | ||
* calling the `createNode` method. If provided, it specifies the initial count for the node. | ||
* @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE. | ||
*/ | ||
createNode(key, value, count) { | ||
return new AVLTreeMultiMapNode(key, this._isMapMode ? undefined : value, count); | ||
} | ||
/** | ||
* The function creates a new AVLTreeMultiMap object with the specified options and returns it. | ||
* @param [options] - The `options` parameter is an optional object that contains additional | ||
* configuration options for creating the AVLTreeMultiMap. It can have the following properties: | ||
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE | ||
* object. | ||
*/ | ||
createTree(options) { | ||
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options)); | ||
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options)); | ||
} | ||
/** | ||
* The function checks if the input is an instance of AVLTreeMultiMapNode. | ||
* @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 `AVLTreeMultiMapNode` class. | ||
*/ | ||
isNode(keyNodeEntryOrRaw) { | ||
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function overrides the add method of a TypeScript class to add a new node to a data structure | ||
* and update the count. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The | ||
* `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It | ||
* can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node, | ||
* entry, or raw element | ||
* @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 the key-value pair will | ||
* be added once. However, you can specify a different value for `count` if you want to add | ||
* @returns a boolean value. | ||
* The function `createNode` overrides the method to create a new AVLTreeMultiMapNode with a | ||
* specified key and an empty array of values. | ||
* @param {K} key - The `key` parameter in the `createNode` method represents the key of the node | ||
* that will be created in the AVLTreeMultiMap. | ||
* @returns An AVLTreeMultiMapNode object is being returned, initialized with the provided key and an | ||
* empty array. | ||
*/ | ||
add(keyNodeEntryOrRaw, value, count = 1) { | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
if (newNode === undefined) | ||
return false; | ||
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0; | ||
const inserted = super.add(newNode, newValue); | ||
if (inserted) { | ||
this._count += orgNodeCount; | ||
} | ||
return true; | ||
createNode(key) { | ||
return new AVLTreeMultiMapNode(key, []); | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the delete method in a binary tree data structure, handling deletion of | ||
* nodes 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 for deleting a node from the | ||
* binary tree. It can be a key, node, or entry that determines which | ||
* node(s) should be deleted. | ||
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a | ||
* boolean flag that determines whether to ignore the count of the node being deleted. If | ||
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If | ||
* `ignoreCount` is set to | ||
* @returns The `delete` method overrides the default delete behavior in a binary tree data | ||
* structure. It takes a predicate or node to be deleted and an optional flag to ignore count. The | ||
* method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the | ||
* deleted node and whether balancing is needed in the tree. | ||
* The function `add` in TypeScript overrides the superclass method to add key-value pairs to an AVL | ||
* tree multi-map. | ||
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override add` method can be either a key-value pair entry or just a key. If it | ||
* is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and | ||
* `values` | ||
* @param {V} [value] - The `value` parameter in the `override add` method represents the value that | ||
* you want to add to the AVLTreeMultiMap. It can be a single value or an array of values associated | ||
* with a specific key. | ||
* @returns The `override add` method is returning a boolean value, which indicates whether the | ||
* addition operation was successful or not. | ||
*/ | ||
delete(keyNodeEntryOrRaw, ignoreCount = false) { | ||
var _a; | ||
const deletedResult = []; | ||
if (!this.root) | ||
return deletedResult; | ||
const curr = (_a = this.getNode(keyNodeEntryOrRaw)) !== null && _a !== void 0 ? _a : undefined; | ||
if (!curr) | ||
return deletedResult; | ||
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : undefined; | ||
let needBalanced = undefined, orgCurrent = curr; | ||
if (curr.count > 1 && !ignoreCount) { | ||
curr.count--; | ||
this._count--; | ||
} | ||
else { | ||
if (!curr.left) { | ||
if (!parent) { | ||
if (curr.right !== undefined && curr.right !== null) | ||
this._setRoot(curr.right); | ||
add(keyNodeOrEntry, value) { | ||
if (this.isRealNode(keyNodeOrEntry)) | ||
return super.add(keyNodeOrEntry); | ||
const _commonAdd = (key, values) => { | ||
if (key === undefined || key === null) | ||
return false; | ||
const existingValues = this.get(key); | ||
if (existingValues !== undefined && values !== undefined) { | ||
for (const value of values) | ||
existingValues.push(value); | ||
return true; | ||
} | ||
const existingNode = this.getNode(key); | ||
if (this.isRealNode(existingNode)) { | ||
if (existingValues === undefined) { | ||
super.add(key, values); | ||
return true; | ||
} | ||
if (values !== undefined) { | ||
for (const value of values) | ||
existingValues.push(value); | ||
return true; | ||
} | ||
else { | ||
const { familyPosition: fp } = curr; | ||
if (fp === 'LEFT' || fp === 'ROOT_LEFT') { | ||
parent.left = curr.right; | ||
} | ||
else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') { | ||
parent.right = curr.right; | ||
} | ||
needBalanced = parent; | ||
return false; | ||
} | ||
} | ||
else { | ||
const leftSubTreeRightMost = curr.left ? this.getRightMost(node => node, curr.left) : undefined; | ||
if (leftSubTreeRightMost) { | ||
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent; | ||
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost); | ||
if (parentOfLeftSubTreeMax) { | ||
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) { | ||
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left; | ||
} | ||
else { | ||
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left; | ||
} | ||
needBalanced = parentOfLeftSubTreeMax; | ||
} | ||
} | ||
return super.add(key, values); | ||
} | ||
this._size = this._size - 1; | ||
// TODO How to handle when the count of target node is lesser than current node's count | ||
if (orgCurrent) | ||
this._count -= orgCurrent.count; | ||
}; | ||
if (this.isEntry(keyNodeOrEntry)) { | ||
const [key, values] = keyNodeOrEntry; | ||
return _commonAdd(key, value !== undefined ? [value] : values); | ||
} | ||
deletedResult.push({ deleted: orgCurrent, needBalanced }); | ||
if (needBalanced) { | ||
this._balancePath(needBalanced); | ||
} | ||
return deletedResult; | ||
return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
* The "clear" function overrides the parent class's "clear" function and also resets the count to | ||
* zero. | ||
* The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data | ||
* structure and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value | ||
* pair in the AVLTreeMultiMapNode, or just the key itself. | ||
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific | ||
* value that you want to delete from the multi-map data structure associated with a particular key. | ||
* The function checks if the value exists in the array of values associated with the key, and if | ||
* found, removes it from the array. | ||
* @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified | ||
* `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If | ||
* the value was not found in the array, it returns `false`. | ||
*/ | ||
clear() { | ||
super.clear(); | ||
this._count = 0; | ||
} | ||
/** | ||
* 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 currently set in | ||
* the 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 = this.iterationType) { | ||
const sorted = this.dfs(node => node, 'IN'), n = sorted.length; | ||
if (sorted.length < 1) | ||
return false; | ||
this.clear(); | ||
if (iterationType === 'RECURSIVE') { | ||
const buildBalanceBST = (l, r) => { | ||
if (l > r) | ||
return; | ||
const m = l + Math.floor((r - l) / 2); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) | ||
this.add(midNode.key, undefined, midNode.count); | ||
else | ||
this.add(midNode.key, midNode.value, midNode.count); | ||
buildBalanceBST(l, m - 1); | ||
buildBalanceBST(m + 1, r); | ||
}; | ||
buildBalanceBST(0, n - 1); | ||
deleteValue(keyNodeOrEntry, value) { | ||
const values = this.get(keyNodeOrEntry); | ||
if (Array.isArray(values)) { | ||
const index = values.indexOf(value); | ||
if (index === -1) | ||
return false; | ||
values.splice(index, 1); | ||
// If no values left, remove the entire node | ||
if (values.length === 0) | ||
this.delete(keyNodeOrEntry); | ||
return true; | ||
} | ||
else { | ||
const stack = [[0, n - 1]]; | ||
while (stack.length > 0) { | ||
const popped = stack.pop(); | ||
if (popped) { | ||
const [l, r] = popped; | ||
if (l <= r) { | ||
const m = l + Math.floor((r - l) / 2); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) | ||
this.add(midNode.key, undefined, midNode.count); | ||
else | ||
this.add(midNode.key, midNode.value, midNode.count); | ||
stack.push([m + 1, r]); | ||
stack.push([l, m - 1]); | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* 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. | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns A cloned tree object is being returned. | ||
*/ | ||
clone() { | ||
const cloned = this.createTree(); | ||
if (this._isMapMode) | ||
this.bfs(node => cloned.add(node.key, undefined, node.count)); | ||
else | ||
this.bfs(node => cloned.add(node.key, node.value, node.count)); | ||
if (this._isMapMode) | ||
cloned._store = this._store; | ||
this._clone(cloned); | ||
return cloned; | ||
} | ||
/** | ||
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap | ||
* with modified entries based on a provided callback. | ||
* @param callback - The `callback` parameter is a function that will be called for each entry in the | ||
* AVLTreeMultiMap. It takes four arguments: | ||
* @param [options] - The `options` parameter in the `override map` function is of type | ||
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional | ||
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function. | ||
* These options | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function. This can be useful when you want to access properties | ||
* or | ||
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries | ||
* transformed by the provided `callback` function. Each entry in the original tree is passed to the | ||
* `callback` function along with the index and the original tree itself. The transformed entries are | ||
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end. | ||
*/ | ||
map(callback, options, thisArg) { | ||
const newTree = new AVLTreeMultiMap([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
/** | ||
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into | ||
* a node object. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The | ||
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `override` function. It represents the value associated with the key in the data structure. If no | ||
* value is provided, it will default to `undefined`. | ||
* @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, value, count = 1) { | ||
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) | ||
return [undefined, undefined]; | ||
if (this.isNode(keyNodeEntryOrRaw)) | ||
return [keyNodeEntryOrRaw, value]; | ||
if (this.isEntry(keyNodeEntryOrRaw)) { | ||
const [key, entryValue] = keyNodeEntryOrRaw; | ||
if (key === undefined || key === null) | ||
return [undefined, undefined]; | ||
const finalValue = value !== null && value !== void 0 ? value : entryValue; | ||
return [this.createNode(key, finalValue, count), finalValue]; | ||
} | ||
if (this.isRaw(keyNodeEntryOrRaw)) { | ||
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw); | ||
const finalValue = value !== null && value !== void 0 ? value : entryValue; | ||
if (this.isKey(key)) | ||
return [this.createNode(key, finalValue, count), finalValue]; | ||
} | ||
if (this.isKey(keyNodeEntryOrRaw)) | ||
return [this.createNode(keyNodeEntryOrRaw, value, count), value]; | ||
return [undefined, undefined]; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `_swapProperties` function swaps the properties (key, value, count, height) 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`. | ||
* @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`. | ||
*/ | ||
_swapProperties(srcNode, destNode) { | ||
srcNode = this.ensureNode(srcNode); | ||
destNode = this.ensureNode(destNode); | ||
if (srcNode && destNode) { | ||
const { key, value, count, height } = destNode; | ||
const tempNode = this.createNode(key, value, count); | ||
if (tempNode) { | ||
tempNode.height = height; | ||
destNode.key = srcNode.key; | ||
if (!this._isMapMode) | ||
destNode.value = srcNode.value; | ||
destNode.count = srcNode.count; | ||
destNode.height = srcNode.height; | ||
srcNode.key = tempNode.key; | ||
if (!this._isMapMode) | ||
srcNode.value = tempNode.value; | ||
srcNode.count = tempNode.count; | ||
srcNode.height = tempNode.height; | ||
} | ||
return destNode; | ||
} | ||
return 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 represents the node that needs to be replaced in the | ||
* data structure. It is of type NODE. | ||
* @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`. | ||
*/ | ||
_replaceNode(oldNode, newNode) { | ||
newNode.count = oldNode.count + newNode.count; | ||
return super._replaceNode(oldNode, newNode); | ||
} | ||
} | ||
exports.AVLTreeMultiMap = AVLTreeMultiMap; |
@@ -9,14 +9,22 @@ /** | ||
import { BST, BSTNode } from './bst'; | ||
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback } from '../../types'; | ||
import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, OptNodeOrNull } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> { | ||
export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> { | ||
/** | ||
* The constructor function initializes a new instance of a class with a key and an optional value, | ||
* and sets the height property to 0. | ||
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the | ||
* constructor. It is used to initialize the key property of the object being created. | ||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the | ||
* value associated with the key in the constructor. | ||
* This TypeScript constructor function initializes an instance with a key and an optional value. | ||
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element | ||
* within a data structure. It serves as a reference or identifier for accessing or manipulating the | ||
* associated value or data. | ||
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not | ||
* have to be provided when creating an instance of the class. If a value is not provided, it will | ||
* default to `undefined`. | ||
*/ | ||
constructor(key: K, value?: V); | ||
parent?: AVLTreeNode<K, V>; | ||
_left?: OptNodeOrNull<AVLTreeNode<K, V>>; | ||
get left(): OptNodeOrNull<AVLTreeNode<K, V>>; | ||
set left(v: OptNodeOrNull<AVLTreeNode<K, V>>); | ||
_right?: OptNodeOrNull<AVLTreeNode<K, V>>; | ||
get right(): OptNodeOrNull<AVLTreeNode<K, V>>; | ||
set right(v: OptNodeOrNull<AVLTreeNode<K, V>>); | ||
} | ||
@@ -32,16 +40,19 @@ /** | ||
*/ | ||
export declare class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> = AVLTree<K, V, R, MK, MV, MR, NODE, AVLTreeNested<K, V, R, MK, MV, MR, NODE>>> extends BST<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> { | ||
export declare class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends BST<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> { | ||
/** | ||
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes, | ||
* entries, or raw elements. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will | ||
* be used to initialize the AVLTree. | ||
* @param [options] - The `options` parameter is an optional object that can be used to customize the | ||
* behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare | ||
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and | ||
* `nodeBuilder` ( | ||
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided | ||
* in an iterable format. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is | ||
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided | ||
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V, | ||
* R>`. It is an optional parameter that allows you to specify additional options for configuring the | ||
* AVL tree. These options could include things like custom comparators, initial capacity, or any | ||
* other configuration settings specific | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: AVLTreeOptions<K, V, R>); | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, AVLTreeNode<K, V>> | R>, options?: AVLTreeOptions<K, V, R>); | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new AVL tree node with the given key and value. | ||
@@ -53,6 +64,9 @@ * @param {K} key - The key parameter is of type K, which represents the key of the node being | ||
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic | ||
* type NODE. | ||
* type AVLTreeNode<K, V>. | ||
*/ | ||
createNode(key: K, value?: V): NODE; | ||
createNode(key: K, value?: V): AVLTreeNode<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new AVL tree with the specified options and returns it. | ||
@@ -64,20 +78,22 @@ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be | ||
*/ | ||
createTree(options?: AVLTreeOptions<K, V, R>): TREE; | ||
createTree(options?: AVLTreeOptions<K, V, R>): AVLTree<K, V, R, MK, MV, MR>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the input is an instance of AVLTreeNode. | ||
* @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 | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `AVLTreeNode` class. | ||
*/ | ||
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE; | ||
isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V>; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the add method of a class and inserts a key-value pair into a data | ||
* structure, then balances the path. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept values of type `R`, `BTNRep<K, V, NODE>`, or | ||
* `RawElement`. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>` | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -87,10 +103,10 @@ * the key or node being added to the data structure. | ||
*/ | ||
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean; | ||
add(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>, value?: V): boolean; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the delete method in a TypeScript class, performs deletion, and then | ||
* balances the tree if necessary. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw` | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override delete` method can be one of the following types: | ||
@@ -102,5 +118,34 @@ * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete` | ||
*/ | ||
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[]; | ||
delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[]; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default map behavior of an AVLTree data structure | ||
* by applying a callback function to each entry and creating a new AVLTree with the results. | ||
* @param callback - A function that will be called for each entry in the AVLTree. It takes four | ||
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to | ||
* the AVLTree itself. | ||
* @param [options] - The `options` parameter in the `override map` function is of type | ||
* `AVLTreeOptions<MK, MV, MR>`. It is an optional parameter that allows you to specify additional | ||
* options for the AVL tree being created during the mapping process. These options could include | ||
* custom comparators, initial | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) within the callback function. This can be useful when you want to access | ||
* properties or | ||
* @returns The `map` method is returning a new AVLTree instance (`newTree`) with the entries | ||
* modified by the provided callback function. | ||
*/ | ||
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: AVLTreeOptions<MK, MV, MR>, thisArg?: any): AVLTree<MK, MV, MR>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns A cloned tree object is being returned. | ||
*/ | ||
clone(): AVLTree<K, V, R, MK, MV, MR>; | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -111,10 +156,10 @@ * Space Complexity: O(1) | ||
* binary search tree. | ||
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node | ||
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node. | ||
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of | ||
* `R` or an instance of `BSTNOptKeyOrNode<K, NODE>`. | ||
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} srcNode - The `srcNode` parameter represents either a node | ||
* object (`AVLTreeNode<K, V>`) or a key-value pair (`R`) that is being swapped with another node. | ||
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} destNode - The `destNode` parameter is either an instance of | ||
* `R` or an instance of `BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>`. | ||
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and | ||
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`. | ||
*/ | ||
protected _swapProperties(srcNode: R | BSTNOptKeyOrNode<K, NODE>, destNode: R | BSTNOptKeyOrNode<K, NODE>): NODE | undefined; | ||
protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>, destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>): AVLTreeNode<K, V> | undefined; | ||
/** | ||
@@ -125,3 +170,3 @@ * Time Complexity: O(1) | ||
* The function calculates the balance factor of a node in a binary tree. | ||
* @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a | ||
* @param {AVLTreeNode<K, V>} node - The parameter "node" is of type "AVLTreeNode<K, V>", which likely represents a node in a | ||
* binary tree data structure. | ||
@@ -131,3 +176,3 @@ * @returns the balance factor of a given node. The balance factor is calculated by subtracting the | ||
*/ | ||
protected _balanceFactor(node: NODE): number; | ||
protected _balanceFactor(node: AVLTreeNode<K, V>): number; | ||
/** | ||
@@ -139,5 +184,5 @@ * Time Complexity: O(1) | ||
* right children. | ||
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure. | ||
* @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure. | ||
*/ | ||
protected _updateHeight(node: NODE): void; | ||
protected _updateHeight(node: AVLTreeNode<K, V>): void; | ||
/** | ||
@@ -148,5 +193,5 @@ * Time Complexity: O(1) | ||
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
protected _balanceLL(A: NODE): void; | ||
protected _balanceLL(A: AVLTreeNode<K, V>): void; | ||
/** | ||
@@ -157,5 +202,5 @@ * Time Complexity: O(1) | ||
* The `_balanceLR` function performs a left-right rotation to balance a binary tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
protected _balanceLR(A: NODE): void; | ||
protected _balanceLR(A: AVLTreeNode<K, V>): void; | ||
/** | ||
@@ -166,5 +211,5 @@ * Time Complexity: O(1) | ||
* The function `_balanceRR` performs a right-right rotation to balance a binary tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
protected _balanceRR(A: NODE): void; | ||
protected _balanceRR(A: AVLTreeNode<K, V>): void; | ||
/** | ||
@@ -175,5 +220,5 @@ * Time Complexity: O(1) | ||
* The function `_balanceRL` performs a right-left rotation to balance a binary tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
protected _balanceRL(A: NODE): void; | ||
protected _balanceRL(A: AVLTreeNode<K, V>): void; | ||
/** | ||
@@ -185,6 +230,6 @@ * Time Complexity: O(log n) | ||
* to restore balance in an AVL tree after inserting a node. | ||
* @param {BTNRep<K, V, NODE> | R} node - The `node` parameter can be of type `R` or | ||
* `BTNRep<K, V, NODE>`. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or | ||
* `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
*/ | ||
protected _balancePath(node: BTNRep<K, V, NODE> | R): void; | ||
protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void; | ||
/** | ||
@@ -196,5 +241,5 @@ * Time Complexity: O(1) | ||
* same as the old node. | ||
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* @param {AVLTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* the data structure. | ||
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in | ||
* @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in | ||
* the data structure. | ||
@@ -204,3 +249,3 @@ * @returns The method is returning the result of calling the `_replaceNode` method from the | ||
*/ | ||
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE; | ||
protected _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V>; | ||
} |
@@ -14,12 +14,34 @@ "use strict"; | ||
/** | ||
* The constructor function initializes a new instance of a class with a key and an optional value, | ||
* and sets the height property to 0. | ||
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the | ||
* constructor. It is used to initialize the key property of the object being created. | ||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the | ||
* value associated with the key in the constructor. | ||
* This TypeScript constructor function initializes an instance with a key and an optional value. | ||
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element | ||
* within a data structure. It serves as a reference or identifier for accessing or manipulating the | ||
* associated value or data. | ||
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not | ||
* have to be provided when creating an instance of the class. If a value is not provided, it will | ||
* default to `undefined`. | ||
*/ | ||
constructor(key, value) { | ||
super(key, value); | ||
this.parent = undefined; | ||
this._left = undefined; | ||
this._right = undefined; | ||
} | ||
get left() { | ||
return this._left; | ||
} | ||
set left(v) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._left = v; | ||
} | ||
get right() { | ||
return this._right; | ||
} | ||
set right(v) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._right = v; | ||
} | ||
} | ||
@@ -38,11 +60,11 @@ exports.AVLTreeNode = AVLTreeNode; | ||
/** | ||
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes, | ||
* entries, or raw elements. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will | ||
* be used to initialize the AVLTree. | ||
* @param [options] - The `options` parameter is an optional object that can be used to customize the | ||
* behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare | ||
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and | ||
* `nodeBuilder` ( | ||
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided | ||
* in an iterable format. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is | ||
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided | ||
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V, | ||
* R>`. It is an optional parameter that allows you to specify additional options for configuring the | ||
* AVL tree. These options could include things like custom comparators, initial capacity, or any | ||
* other configuration settings specific | ||
*/ | ||
@@ -55,2 +77,5 @@ constructor(keysNodesEntriesOrRaws = [], options) { | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new AVL tree node with the given key and value. | ||
@@ -62,3 +87,3 @@ * @param {K} key - The key parameter is of type K, which represents the key of the node being | ||
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic | ||
* type NODE. | ||
* type AVLTreeNode<K, V>. | ||
*/ | ||
@@ -69,2 +94,5 @@ createNode(key, value) { | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new AVL tree with the specified options and returns it. | ||
@@ -80,20 +108,22 @@ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the input is an instance of AVLTreeNode. | ||
* @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 | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `AVLTreeNode` class. | ||
*/ | ||
isNode(keyNodeEntryOrRaw) { | ||
return keyNodeEntryOrRaw instanceof AVLTreeNode; | ||
isNode(keyNodeOrEntry) { | ||
return keyNodeOrEntry instanceof AVLTreeNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the add method of a class and inserts a key-value pair into a data | ||
* structure, then balances the path. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept values of type `R`, `BTNRep<K, V, NODE>`, or | ||
* `RawElement`. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>` | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -103,8 +133,8 @@ * the key or node being added to the data structure. | ||
*/ | ||
add(keyNodeEntryOrRaw, value) { | ||
if (keyNodeEntryOrRaw === null) | ||
add(keyNodeOrEntry, value) { | ||
if (keyNodeOrEntry === null) | ||
return false; | ||
const inserted = super.add(keyNodeEntryOrRaw, value); | ||
const inserted = super.add(keyNodeOrEntry, value); | ||
if (inserted) | ||
this._balancePath(keyNodeEntryOrRaw); | ||
this._balancePath(keyNodeOrEntry); | ||
return inserted; | ||
@@ -114,7 +144,7 @@ } | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the delete method in a TypeScript class, performs deletion, and then | ||
* balances the tree if necessary. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw` | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override delete` method can be one of the following types: | ||
@@ -126,4 +156,4 @@ * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete` | ||
*/ | ||
delete(keyNodeEntryOrRaw) { | ||
const deletedResults = super.delete(keyNodeEntryOrRaw); | ||
delete(keyNodeOrEntry) { | ||
const deletedResults = super.delete(keyNodeOrEntry); | ||
for (const { needBalanced } of deletedResults) { | ||
@@ -136,2 +166,22 @@ if (needBalanced) { | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default map behavior of an AVLTree data structure | ||
* by applying a callback function to each entry and creating a new AVLTree with the results. | ||
* @param callback - A function that will be called for each entry in the AVLTree. It takes four | ||
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to | ||
* the AVLTree itself. | ||
* @param [options] - The `options` parameter in the `override map` function is of type | ||
* `AVLTreeOptions<MK, MV, MR>`. It is an optional parameter that allows you to specify additional | ||
* options for the AVL tree being created during the mapping process. These options could include | ||
* custom comparators, initial | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) within the callback function. This can be useful when you want to access | ||
* properties or | ||
* @returns The `map` method is returning a new AVLTree instance (`newTree`) with the entries | ||
* modified by the provided callback function. | ||
*/ | ||
map(callback, options, thisArg) { | ||
@@ -146,2 +196,15 @@ const newTree = new AVLTree([], options); | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns A cloned tree object is being returned. | ||
*/ | ||
clone() { | ||
const cloned = this.createTree(); | ||
this._clone(cloned); | ||
return cloned; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -152,6 +215,6 @@ * Space Complexity: O(1) | ||
* binary search tree. | ||
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node | ||
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node. | ||
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of | ||
* `R` or an instance of `BSTNOptKeyOrNode<K, NODE>`. | ||
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} srcNode - The `srcNode` parameter represents either a node | ||
* object (`AVLTreeNode<K, V>`) or a key-value pair (`R`) that is being swapped with another node. | ||
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} destNode - The `destNode` parameter is either an instance of | ||
* `R` or an instance of `BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>`. | ||
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and | ||
@@ -186,3 +249,3 @@ * `destNodeEnsured` are truthy. Otherwise, it returns `undefined`. | ||
* The function calculates the balance factor of a node in a binary tree. | ||
* @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a | ||
* @param {AVLTreeNode<K, V>} node - The parameter "node" is of type "AVLTreeNode<K, V>", which likely represents a node in a | ||
* binary tree data structure. | ||
@@ -208,3 +271,3 @@ * @returns the balance factor of a given node. The balance factor is calculated by subtracting the | ||
* right children. | ||
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure. | ||
* @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure. | ||
*/ | ||
@@ -228,3 +291,3 @@ _updateHeight(node) { | ||
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
@@ -267,3 +330,3 @@ _balanceLL(A) { | ||
* The `_balanceLR` function performs a left-right rotation to balance a binary tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
@@ -323,3 +386,3 @@ _balanceLR(A) { | ||
* The function `_balanceRR` performs a right-right rotation to balance a binary tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
@@ -364,3 +427,3 @@ _balanceRR(A) { | ||
* The function `_balanceRL` performs a right-left rotation to balance a binary tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
@@ -422,4 +485,4 @@ _balanceRL(A) { | ||
* to restore balance in an AVL tree after inserting a node. | ||
* @param {BTNRep<K, V, NODE> | R} node - The `node` parameter can be of type `R` or | ||
* `BTNRep<K, V, NODE>`. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or | ||
* `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
*/ | ||
@@ -474,5 +537,5 @@ _balancePath(node) { | ||
* same as the old node. | ||
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* @param {AVLTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* the data structure. | ||
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in | ||
* @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in | ||
* the data structure. | ||
@@ -479,0 +542,0 @@ * @returns The method is returning the result of calling the `_replaceNode` method from the |
@@ -0,1 +1,4 @@ | ||
/** | ||
* | ||
*/ | ||
export declare class BinaryIndexedTree { | ||
@@ -2,0 +5,0 @@ protected readonly _freq: number; |
@@ -12,2 +12,5 @@ "use strict"; | ||
const utils_1 = require("../../utils"); | ||
/** | ||
* | ||
*/ | ||
class BinaryIndexedTree { | ||
@@ -14,0 +17,0 @@ /** |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, type RBTNColor, ToEntryFn } from '../../types'; | ||
import type { BinaryTreeDeleteResult, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, RBTNColor, ToEntryFn } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
@@ -16,15 +16,23 @@ import { IterableEntryBase } from '../base'; | ||
* @template V - The type of data stored in the node. | ||
* @template NODE - The type of the family relationship in the binary tree. | ||
* @template BinaryTreeNode<K, V> - The type of the family relationship in the binary tree. | ||
*/ | ||
export declare class BinaryTreeNode<K = any, V = any, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>> { | ||
export declare class BinaryTreeNode<K = any, V = any> { | ||
/** | ||
* The constructor function initializes an object with a key and an optional value in TypeScript. | ||
* @param {K} key - The `key` parameter in the constructor function is used to store the key value | ||
* for the key-value pair. | ||
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not | ||
* have to be provided when creating an instance of the class. If a `value` is not provided, it will | ||
* default to `undefined`. | ||
*/ | ||
constructor(key: K, value?: V); | ||
key: K; | ||
value?: V; | ||
parent?: NODE; | ||
constructor(key: K, value?: V); | ||
_left?: OptNodeOrNull<NODE>; | ||
get left(): OptNodeOrNull<NODE>; | ||
set left(v: OptNodeOrNull<NODE>); | ||
_right?: OptNodeOrNull<NODE>; | ||
get right(): OptNodeOrNull<NODE>; | ||
set right(v: OptNodeOrNull<NODE>); | ||
parent?: BinaryTreeNode<K, V>; | ||
_left?: OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
get left(): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
set left(v: OptNodeOrNull<BinaryTreeNode<K, V>>); | ||
_right?: OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
get right(): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
set right(v: OptNodeOrNull<BinaryTreeNode<K, V>>); | ||
_height: number; | ||
@@ -48,14 +56,14 @@ get height(): number; | ||
*/ | ||
export declare class BinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTree<K, V, R, MK, MV, MR, NODE, BinaryTreeNested<K, V, R, MK, MV, MR, NODE>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> { | ||
iterationType: IterationType; | ||
export declare class BinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R, MK, MV, MR> { | ||
/** | ||
* The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or | ||
* raw data if provided. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor | ||
* is an iterable that can contain elements of type `BTNRep<K, V, NODE>` or `R`. It is | ||
* initialized with an empty array `[]` by default. | ||
* @param [options] - The `options` parameter in the constructor is an object that can contain the | ||
* following properties: | ||
* This TypeScript constructor function initializes a binary tree with optional options and adds | ||
* elements based on the provided input. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either objects of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It | ||
* is used to initialize the binary tree with keys, nodes, entries, or raw data. | ||
* @param [options] - The `options` parameter in the constructor is an optional object that can | ||
* contain the following properties: | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, NODE> | R>, options?: BinaryTreeOptions<K, V, R>); | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, BinaryTreeNode<K, V>> | R>, options?: BinaryTreeOptions<K, V, R>); | ||
iterationType: IterationType; | ||
protected _isMapMode: boolean; | ||
@@ -65,8 +73,8 @@ get isMapMode(): boolean; | ||
get store(): Map<K, V | undefined>; | ||
protected _root?: OptNodeOrNull<NODE>; | ||
get root(): OptNodeOrNull<NODE>; | ||
protected _root?: OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
get root(): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
protected _size: number; | ||
get size(): number; | ||
protected _NIL: NODE; | ||
get NIL(): NODE; | ||
protected _NIL: BinaryTreeNode<K, V>; | ||
get NIL(): BinaryTreeNode<K, V>; | ||
protected _toEntryFn?: ToEntryFn<K, V, R>; | ||
@@ -84,6 +92,9 @@ get toEntryFn(): ToEntryFn<K, V, R> | undefined; | ||
* @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted | ||
* as NODE. | ||
* as BinaryTreeNode<K, V>. | ||
*/ | ||
createNode(key: K, value?: V): NODE; | ||
createNode(key: K, value?: V): BinaryTreeNode<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a binary tree with the specified options. | ||
@@ -96,20 +107,4 @@ * @param [options] - The `options` parameter in the `createTree` function is an optional parameter | ||
*/ | ||
createTree(options?: BinaryTreeOptions<K, V, R>): TREE; | ||
createTree(options?: BinaryTreeOptions<K, V, R>): BinaryTree<K, V, R, MK, MV, MR>; | ||
/** | ||
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object | ||
* or returns null. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The | ||
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which | ||
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a | ||
* node, an entry | ||
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is | ||
* an optional parameter of type `V`. It represents the value associated with the key in the node | ||
* being created. If a `value` is provided, it will be used when creating the node. If | ||
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node | ||
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the | ||
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null | ||
* value. | ||
*/ | ||
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNodeOrNull<NODE>, V | undefined]; | ||
/** | ||
* Time Complexity: O(n) | ||
@@ -120,4 +115,4 @@ * Space Complexity: O(log n) | ||
* value and returns the corresponding node or null. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw` | ||
* parameter in the `ensureNode` function can be of type `BTNRep<K, V, NODE>` or `R`. It | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `ensureNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It | ||
* is used to determine whether the input is a key, node, entry, or raw data. The | ||
@@ -130,10 +125,13 @@ * @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function | ||
*/ | ||
ensureNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, iterationType?: IterationType): OptNodeOrNull<NODE>; | ||
ensureNode(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function isNode checks if the input is an instance of BinaryTreeNode. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can be either a key, a node, an entry, or raw data. The function is | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is | ||
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value | ||
* accordingly. | ||
* @returns The function `isNode` is checking if the input `keyNodeEntryOrRaw` is an instance of | ||
* @returns The function `isNode` is checking if the input `keyNodeOrEntry` is an instance of | ||
* `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If | ||
@@ -143,6 +141,9 @@ * it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input | ||
*/ | ||
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE; | ||
isNode(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): keyNodeOrEntry is BinaryTreeNode<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | R} keyNodeEntryOrRaw - BTNRep<K, V, BinaryTreeNode<K, V>> | ||
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by | ||
@@ -152,9 +153,12 @@ * checking if it is an object. If the parameter is an object, the function will return `true`, | ||
*/ | ||
isRaw(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is R; | ||
isRaw(keyNodeEntryOrRaw: BTNRep<K, V, BinaryTreeNode<K, V>> | R): keyNodeEntryOrRaw is R; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `isRealNode` checks if a given input is a valid node in a binary tree. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw` | ||
* parameter in the `isRealNode` function can be of type `BTNRep<K, V, NODE>` or `R`. | ||
* The function checks if the input parameter is a `NODE` type by verifying if it is not equal | ||
* @returns The function `isRealNode` is checking if the input `keyNodeEntryOrRaw` is a valid | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `isRealNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. | ||
* The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal | ||
* @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid | ||
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these | ||
@@ -164,44 +168,70 @@ * values, it then calls the `isNode` method to further determine if the input is a node. The | ||
*/ | ||
isRealNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE; | ||
isRealNode(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): keyNodeOrEntry is BinaryTreeNode<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if a given input is a valid node or null. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` in the `isRealNodeOrNull` function can be of type `BTNRep<K, | ||
* V, NODE>` or `R`. It is a union type that can either be a key, a node, an entry, or | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K, | ||
* V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or | ||
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input | ||
* `keyNodeEntryOrRaw` is either `null` or a real node, and returns `true` if it is a node or | ||
* `keyNodeOrEntry` is either `null` or a real node, and returns `true` if it is a node or | ||
* `null`, and `false` otherwise. | ||
*/ | ||
isRealNodeOrNull(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE | null; | ||
isRealNodeOrNull(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): keyNodeOrEntry is BinaryTreeNode<K, V> | null; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, | ||
* NODE> | R | ||
* @returns The function is checking if the `keyNodeEntryOrRaw` parameter is equal to the `_NIL` | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - BTNRep<K, V, | ||
* BinaryTreeNode<K, V>> | ||
* @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL` | ||
* property of the current object and returning a boolean value based on that comparison. | ||
*/ | ||
isNIL(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): boolean; | ||
isRange(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>): keyNodeEntryRawOrPredicate is Range<K>; | ||
isNIL(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): boolean; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `isRange` checks if the input parameter is an instance of the `Range` class. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>> | Range<K>} | ||
* keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be | ||
* of type `BTNRep<K, V, BinaryTreeNode<K, V>>`, `NodePredicate<BinaryTreeNode<K, V>>`, or | ||
* `Range<K>`. The function checks if the `keyNodeEntry | ||
* @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an | ||
* instance of the `Range` class. If it is an instance of `Range`, the function will return `true`, | ||
* indicating that the parameter is a `Range<K>`. If it is not an instance of `Range`, the function | ||
* will return `false`. | ||
*/ | ||
isRange(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>> | Range<K>): keyNodeEntryOrPredicate is Range<K>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary | ||
* tree. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can be of type `BTNRep<K, V, NODE>` or `R`. It represents a | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It represents a | ||
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the | ||
* provided | ||
* @returns The function `isLeaf` returns a boolean value indicating whether the input | ||
* `keyNodeEntryOrRaw` is a leaf node in a binary tree. | ||
* `keyNodeOrEntry` is a leaf node in a binary tree. | ||
*/ | ||
isLeaf(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): boolean; | ||
isLeaf(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): boolean; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array | ||
* with a length of 2. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw` | ||
* parameter in the `isEntry` function can be of type `BTNRep<K, V, NODE>` or type `R`. | ||
* The function checks if the provided `keyNodeEntryOrRaw` is of type `BTN | ||
* @returns The `isEntry` function is checking if the `keyNodeEntryOrRaw` parameter is an array | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `isEntry` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or type `R`. | ||
* The function checks if the provided `keyNodeOrEntry` is of type `BTN | ||
* @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array | ||
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type | ||
* `BTNEntry<K, V>`. If the condition is not met, it returns `false`. | ||
*/ | ||
isEntry(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is BTNEntry<K, V>; | ||
isEntry(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): keyNodeOrEntry is BTNEntry<K, V>; | ||
/** | ||
@@ -211,10 +241,10 @@ * Time Complexity O(1) | ||
* | ||
* The function `isKey` checks if a given key is comparable. | ||
* The function `isValidKey` checks if a given key is comparable. | ||
* @param {any} key - The `key` parameter is of type `any`, which means it can be any data type in | ||
* TypeScript. | ||
* @returns The function `isKey` is checking if the `key` parameter is `null` or if it is comparable. | ||
* @returns The function `isValidKey` is checking if the `key` parameter is `null` or if it is comparable. | ||
* If the `key` is `null`, the function returns `true`. Otherwise, it returns the result of the | ||
* `isComparable` function, which is not provided in the code snippet. | ||
*/ | ||
isKey(key: any): key is K; | ||
isValidKey(key: any): key is K; | ||
/** | ||
@@ -226,4 +256,4 @@ * Time Complexity O(n) | ||
* and finding the correct insertion position. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `add` method you provided | ||
* seems to be for adding a new node to a binary tree structure. The `keyNodeEntryOrRaw` | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `add` method you provided | ||
* seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry` | ||
* parameter in the method can accept different types of values: | ||
@@ -238,6 +268,6 @@ * @param {V} [value] - The `value` parameter in the `add` method represents the value associated | ||
*/ | ||
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean; | ||
add(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V): boolean; | ||
/** | ||
* Time Complexity: O(k * n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(k) | ||
* | ||
@@ -249,3 +279,3 @@ * The `addMany` function takes in multiple keys or nodes or entries or raw values along with | ||
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type | ||
* `BTNRep<K, V, NODE>` or `R`. | ||
* `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. | ||
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that | ||
@@ -259,3 +289,3 @@ * accepts an iterable of values. These values correspond to the keys or nodes being added in the | ||
*/ | ||
addMany(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, NODE> | R>, values?: Iterable<V | undefined>): boolean[]; | ||
addMany(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, BinaryTreeNode<K, V>> | R>, values?: Iterable<V | undefined>): boolean[]; | ||
/** | ||
@@ -267,5 +297,5 @@ * Time Complexity: O(k * n) | ||
* elements from the other tree. | ||
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>` | ||
* @param anotherTree - BinaryTree<K, V, R, MK, MV, MR> | ||
*/ | ||
merge(anotherTree: BinaryTree<K, V, R, NODE, TREE>): void; | ||
merge(anotherTree: BinaryTree<K, V, R, MK, MV, MR>): void; | ||
/** | ||
@@ -278,3 +308,3 @@ * Time Complexity: O(k * n) | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill` | ||
* method can accept an iterable containing a mix of `BTNRep<K, V, NODE>` objects or `R` | ||
* method can accept an iterable containing a mix of `BTNRep<K, V, BinaryTreeNode<K, V>>` objects or `R` | ||
* objects. | ||
@@ -284,3 +314,3 @@ * @param [values] - The `values` parameter in the `refill` method is an optional parameter that | ||
*/ | ||
refill(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, NODE> | R>, values?: Iterable<V | undefined>): void; | ||
refill(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, BinaryTreeNode<K, V>> | R>, values?: Iterable<V | undefined>): void; | ||
/** | ||
@@ -292,3 +322,3 @@ * Time Complexity: O(n) | ||
* the deleted node along with information for tree balancing. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry | ||
* - The `delete` method you provided is used to delete a node from a binary tree based on the key, | ||
@@ -302,3 +332,3 @@ * node, entry or raw data. The method returns an array of | ||
*/ | ||
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[]; | ||
delete(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[]; | ||
/** | ||
@@ -310,4 +340,4 @@ * Time Complexity: O(n) | ||
* structure based on a given predicate or key, with options to return multiple results or just one. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The | ||
* `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values: | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values: | ||
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that | ||
@@ -318,4 +348,4 @@ * determines whether the search should stop after finding the first matching node. If `onlyOne` is | ||
* that will be called on each node that matches the search criteria. It is of type `C`, which | ||
* extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is | ||
* extends `NodeCallback<BinaryTreeNode<K, V>>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `search` function is | ||
* used to specify the node from which the search operation should begin. It represents the starting | ||
@@ -330,3 +360,3 @@ * point in the binary tree where the search will be performed. If no specific `startNode` is | ||
*/ | ||
search<C extends NodeCallback<NODE>>(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[]; | ||
search<C extends NodeCallback<BinaryTreeNode<K, V>>>(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -338,8 +368,8 @@ * Time Complexity: O(n) | ||
* or predicate, with options for recursive or iterative traversal. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate | ||
* - The `getNodes` function you provided takes several parameters: | ||
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that | ||
* determines whether to return only the first node that matches the criteria specified by the | ||
* `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the | ||
* `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `getNodes` function is used to specify the starting point for traversing the binary tree. It | ||
@@ -354,13 +384,13 @@ * represents the root node of the binary tree or the node from which the traversal should begin. If | ||
*/ | ||
getNodes(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, onlyOne?: boolean, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): NODE[]; | ||
getNodes(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>, onlyOne?: boolean, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): BinaryTreeNode<K, V>[]; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n). | ||
* Space Complexity: O(log n) | ||
* | ||
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or | ||
* predicate. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate | ||
* - The `keyNodeEntryRawOrPredicate` parameter in the `getNode` function can accept a key, | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate | ||
* - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key, | ||
* node, entry, raw data, or a predicate function. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `getNode` function is used to specify the starting point for searching for a node in a binary | ||
@@ -376,3 +406,3 @@ * tree. If no specific starting point is provided, the default value is set to `this._root`, which | ||
*/ | ||
getNode(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): OptNodeOrNull<NODE>; | ||
getNode(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
/** | ||
@@ -384,6 +414,6 @@ * Time Complexity: O(n) | ||
* node, entry, raw data, or predicate in a data structure. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate | ||
* - The `keyNodeEntryRawOrPredicate` parameter in the `get` method can accept one of the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate | ||
* - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the | ||
* following types: | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `get` | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `get` | ||
* method is used to specify the starting point for searching for a key or node in the binary tree. | ||
@@ -401,3 +431,3 @@ * If no specific starting point is provided, the default starting point is the root of the binary | ||
*/ | ||
get(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): V | undefined; | ||
get(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>>, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): V | undefined; | ||
/** | ||
@@ -409,6 +439,6 @@ * Time Complexity: O(n) | ||
* exists in the data structure. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate | ||
* - The `keyNodeEntryRawOrPredicate` parameter in the `override has` method can accept one of | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate | ||
* - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of | ||
* the following types: | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `override` method is used to specify the starting point for the search operation within the data | ||
@@ -425,3 +455,3 @@ * structure. It defaults to `this._root` if not provided explicitly. | ||
*/ | ||
has(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): boolean; | ||
has(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): boolean; | ||
/** | ||
@@ -431,3 +461,3 @@ * Time Complexity: O(1) | ||
* | ||
* The `clear` function resets the root node and size of a data structure to empty. | ||
* The clear function removes nodes and values in map mode. | ||
*/ | ||
@@ -451,3 +481,3 @@ clear(): void; | ||
* its height. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for checking if the binary tree is perfectly balanced. It represents the root node of the | ||
@@ -461,10 +491,10 @@ * binary tree or a specific node from which the balance check should begin. | ||
*/ | ||
isPerfectlyBalanced(startNode?: BTNRep<K, V, NODE> | R): boolean; | ||
isPerfectlyBalanced(startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>): boolean; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive | ||
* or iterative methods. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `isBST` | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `isBST` | ||
* function represents the starting point for checking whether a binary search tree (BST) is valid. | ||
@@ -481,12 +511,12 @@ * It can be a node in the BST or a reference to the root of the BST. If no specific node is | ||
*/ | ||
isBST(startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): boolean; | ||
isBST(startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): boolean; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The `getDepth` function calculates the depth between two nodes in a binary tree. | ||
* @param {BTNRep<K, V, NODE> | R} dist - The `dist` parameter in the `getDepth` | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} dist - The `dist` parameter in the `getDepth` | ||
* function represents the node or entry in a binary tree map, or a reference to a node in the tree. | ||
* It is the target node for which you want to calculate the depth from the `startNode` node. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `getDepth` function represents the starting point from which you want to calculate the depth of a | ||
@@ -499,10 +529,10 @@ * given node or entry in a binary tree. If no specific starting point is provided, the default value | ||
*/ | ||
getDepth(dist: BTNRep<K, V, NODE> | R, startNode?: BTNRep<K, V, NODE> | R): number; | ||
getDepth(dist: BTNRep<K, V, BinaryTreeNode<K, V>>, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>): number; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive | ||
* or iterative approach in TypeScript. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point from which the height of the binary tree will be calculated. It can be a node in the binary | ||
@@ -518,3 +548,3 @@ * tree or a reference to the root of the tree. If not provided, it defaults to the root of the | ||
*/ | ||
getHeight(startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): number; | ||
getHeight(startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): number; | ||
/** | ||
@@ -526,3 +556,3 @@ * Time Complexity: O(n) | ||
* recursive or iterative approach in TypeScript. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `getMinHeight` function represents the starting node from which the minimum height of the binary | ||
@@ -539,3 +569,3 @@ * tree will be calculated. It is either a node in the binary tree or a reference to the root of the | ||
*/ | ||
getMinHeight(startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): number; | ||
getMinHeight(startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): number; | ||
/** | ||
@@ -551,3 +581,3 @@ * Time Complexity: O(log n) | ||
* type `C | ||
* @param {BTNRep<K, V, NODE> | R} beginNode - The `beginNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} beginNode - The `beginNode` parameter in the | ||
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`. | ||
@@ -562,6 +592,6 @@ * @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines | ||
*/ | ||
getPathToRoot<C extends NodeCallback<OptNodeOrNull<NODE>>>(beginNode: BTNRep<K, V, NODE> | R, callback?: C, isReverse?: boolean): ReturnType<C>[]; | ||
getPathToRoot<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(beginNode: BTNRep<K, V, BinaryTreeNode<K, V>>, callback?: C, isReverse?: boolean): ReturnType<C>[]; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -573,3 +603,3 @@ * The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or | ||
* value of `_DEFAULT_NODE_CALLBACK` if not specified. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary | ||
@@ -586,6 +616,6 @@ * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific | ||
*/ | ||
getLeftMost<C extends NodeCallback<OptNodeOrNull<NODE>>>(callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>; | ||
getLeftMost<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): ReturnType<C>; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -595,6 +625,6 @@ * The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive | ||
* @param {C} callback - The `callback` parameter is a function that will be called with the result | ||
* of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<NODE>>`, | ||
* of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`, | ||
* which means it is a callback function that can accept either an optional binary tree node or null | ||
* as | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `getRightMost` function represents the starting point for finding the rightmost node in a binary | ||
@@ -611,25 +641,25 @@ * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific | ||
*/ | ||
getRightMost<C extends NodeCallback<OptNodeOrNull<NODE>>>(callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>; | ||
getRightMost<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): ReturnType<C>; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a | ||
* binary tree. | ||
* @param {NODE} node - The `getPredecessor` function you provided seems to be attempting to find the | ||
* @param {BinaryTreeNode<K, V>} node - The `getPredecessor` function you provided seems to be attempting to find the | ||
* predecessor of a given node in a binary tree. However, there seems to be a logical issue in the | ||
* while loop condition that might cause an infinite loop. | ||
* @returns The `getPredecessor` function returns the predecessor node of the input `NODE` parameter. | ||
* @returns The `getPredecessor` function returns the predecessor node of the input `BinaryTreeNode<K, V>` parameter. | ||
* If the left child of the input node exists, it traverses to the rightmost node of the left subtree | ||
* to find the predecessor. If the left child does not exist, it returns the input node itself. | ||
*/ | ||
getPredecessor(node: NODE): NODE; | ||
getPredecessor(node: BinaryTreeNode<K, V>): BinaryTreeNode<K, V>; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a | ||
* binary tree. | ||
* @param {K | NODE | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of | ||
* type `K`, `NODE`, or `null`. | ||
* @param {K | BinaryTreeNode<K, V> | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of | ||
* type `K`, `BinaryTreeNode<K, V>`, or `null`. | ||
* @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has | ||
@@ -640,7 +670,7 @@ * a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not | ||
*/ | ||
getSuccessor(x?: K | NODE | null): OptNodeOrNull<NODE>; | ||
dfs<C extends NodeCallback<NODE>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[]; | ||
dfs<C extends NodeCallback<NODE | null>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType, includeNull?: boolean): ReturnType<C>[]; | ||
bfs<C extends NodeCallback<NODE>>(callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType, includeNull?: false): ReturnType<C>[]; | ||
bfs<C extends NodeCallback<NODE | null>>(callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType, includeNull?: true): ReturnType<C>[]; | ||
getSuccessor(x?: K | BinaryTreeNode<K, V> | null): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
dfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: boolean): ReturnType<C>[]; | ||
bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[]; | ||
bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[]; | ||
/** | ||
@@ -654,3 +684,3 @@ * Time complexity: O(n) | ||
* in the binary tree. It is optional and defaults to a default callback function if not provided. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `leaves` | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `leaves` | ||
* method is used to specify the starting point for finding and processing the leaves of a binary | ||
@@ -665,5 +695,5 @@ * tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not | ||
*/ | ||
leaves<C extends NodeCallback<NODE | null>>(callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[]; | ||
listLevels<C extends NodeCallback<NODE>>(callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][]; | ||
listLevels<C extends NodeCallback<NODE | null>>(callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][]; | ||
leaves<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][]; | ||
listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][]; | ||
/** | ||
@@ -677,7 +707,7 @@ * Time complexity: O(n) | ||
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the | ||
* `NodeCallback<NODE>` type. The default value for `callback` is `this._DEFAULT | ||
* `NodeCallback<BinaryTreeNode<K, V>>` type. The default value for `callback` is `this._DEFAULT | ||
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies | ||
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible | ||
* values for the `pattern` parameter are: | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `morris` | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `morris` | ||
* function is the starting point for the Morris traversal algorithm. It represents the root node of | ||
@@ -690,3 +720,3 @@ * the binary tree or the node from which the traversal should begin. It can be provided as either a | ||
*/ | ||
morris<C extends NodeCallback<NODE>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, NODE> | R): ReturnType<C>[]; | ||
morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>): ReturnType<C>[]; | ||
/** | ||
@@ -703,3 +733,4 @@ * Time complexity: O(n) | ||
*/ | ||
clone(): TREE; | ||
clone(): BinaryTree<K, V, R, MK, MV, MR>; | ||
protected _clone(cloned: BinaryTree<K, V, R, MK, MV, MR>): void; | ||
/** | ||
@@ -721,3 +752,3 @@ * Time Complexity: O(n) | ||
*/ | ||
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any): TREE; | ||
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any): BinaryTree<K, V, R, MK, MV, MR>; | ||
/** | ||
@@ -749,3 +780,3 @@ * Time Complexity: O(n) | ||
* customizable options for displaying undefined, null, and sentinel nodes. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure. | ||
@@ -762,3 +793,3 @@ * It can be a node, key, entry, or the root of the tree. If no specific starting point is provided, | ||
*/ | ||
toVisual(startNode?: BTNRep<K, V, NODE> | R, options?: BinaryTreePrintOptions): string; | ||
toVisual(startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, options?: BinaryTreePrintOptions): string; | ||
/** | ||
@@ -774,3 +805,3 @@ * Time Complexity: O(n) | ||
* options. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `override print` method is used to specify the starting point for printing the binary tree. It can | ||
@@ -780,4 +811,23 @@ * be either a key, a node, an entry, or the root of the tree. If no specific starting point is | ||
*/ | ||
print(options?: BinaryTreePrintOptions, startNode?: BTNRep<K, V, NODE> | R): void; | ||
print(options?: BinaryTreePrintOptions, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>): void; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object | ||
* or returns null. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The | ||
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which | ||
* can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. This parameter represents either a key, a | ||
* node, an entry | ||
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is | ||
* an optional parameter of type `V`. It represents the value associated with the key in the node | ||
* being created. If a `value` is provided, it will be used when creating the node. If | ||
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node | ||
* (`OptNodeOrNull<BinaryTreeNode<K, V>>`) based on the input parameters provided. The function checks the type of the | ||
* input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null | ||
* value. | ||
*/ | ||
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V): [OptNodeOrNull<BinaryTreeNode<K, V>>, V | undefined]; | ||
/** | ||
* Time complexity: O(n) | ||
@@ -790,7 +840,7 @@ * Space complexity: O(n) | ||
* called on each node visited during the depth-first search traversal. It is of type `C`, which | ||
* extends `NodeCallback<OptNodeOrNull<NODE>>`. The default value for this parameter is `this._DEFAULT | ||
* extends `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`. The default value for this parameter is `this._DEFAULT | ||
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the | ||
* order in which the nodes are visited during the Depth-First Search traversal. It can have one of | ||
* the following values: | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `_dfs` | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `_dfs` | ||
* method is used to specify the starting point for the depth-first search traversal in a binary | ||
@@ -825,3 +875,3 @@ * tree. It can be provided as either a `BTNRep` object or a reference to the root node | ||
*/ | ||
protected _dfs<C extends NodeCallback<OptNodeOrNull<NODE>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType, includeNull?: boolean, shouldVisitLeft?: (node: OptNodeOrNull<NODE>) => boolean, shouldVisitRight?: (node: OptNodeOrNull<NODE>) => boolean, shouldVisitRoot?: (node: OptNodeOrNull<NODE>) => boolean, shouldProcessRoot?: (node: OptNodeOrNull<NODE>) => boolean): ReturnType<C>[]; | ||
protected _dfs<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: boolean, shouldVisitLeft?: (node: OptNodeOrNull<BinaryTreeNode<K, V>>) => boolean, shouldVisitRight?: (node: OptNodeOrNull<BinaryTreeNode<K, V>>) => boolean, shouldVisitRoot?: (node: OptNodeOrNull<BinaryTreeNode<K, V>>) => boolean, shouldProcessRoot?: (node: OptNodeOrNull<BinaryTreeNode<K, V>>) => boolean): ReturnType<C>[]; | ||
/** | ||
@@ -842,3 +892,3 @@ * Time Complexity: O(1) | ||
*/ | ||
protected _getIterator(node?: OptNodeOrNull<NODE>): IterableIterator<[K, V | undefined]>; | ||
protected _getIterator(node?: OptNodeOrNull<BinaryTreeNode<K, V>>): IterableIterator<[K, V | undefined]>; | ||
/** | ||
@@ -859,4 +909,4 @@ * Time Complexity: O(n) | ||
*/ | ||
protected _displayAux(node: OptNodeOrNull<NODE>, options: BinaryTreePrintOptions): NodeDisplayLayout; | ||
protected _DEFAULT_NODE_CALLBACK: (node: OptNodeOrNull<NODE>) => K | undefined; | ||
protected _displayAux(node: OptNodeOrNull<BinaryTreeNode<K, V>>, options: BinaryTreePrintOptions): NodeDisplayLayout; | ||
protected _DEFAULT_NODE_CALLBACK: (node: OptNodeOrNull<BinaryTreeNode<K, V>>) => K | undefined; | ||
/** | ||
@@ -867,8 +917,8 @@ * Time Complexity: O(1) | ||
* The _swapProperties function swaps key and value properties between two nodes in a binary tree. | ||
* @param {BTNRep<K, V, NODE> | R} srcNode - The `srcNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} srcNode - The `srcNode` parameter in the | ||
* `_swapProperties` method can be either a BTNRep object containing key and value | ||
* properties, or it can be of type R. | ||
* @param {BTNRep<K, V, NODE> | R} destNode - The `destNode` parameter in the | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} destNode - The `destNode` parameter in the | ||
* `_swapProperties` method represents the node or entry where the properties will be swapped with | ||
* the `srcNode`. It can be of type `BTNRep<K, V, NODE>` or `R`. The method ensures that | ||
* the `srcNode`. It can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. The method ensures that | ||
* both `srcNode | ||
@@ -878,3 +928,3 @@ * @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped | ||
*/ | ||
protected _swapProperties(srcNode: BTNRep<K, V, NODE> | R, destNode: BTNRep<K, V, NODE> | R): NODE | undefined; | ||
protected _swapProperties(srcNode: BTNRep<K, V, BinaryTreeNode<K, V>>, destNode: BTNRep<K, V, BinaryTreeNode<K, V>>): BinaryTreeNode<K, V> | undefined; | ||
/** | ||
@@ -885,5 +935,5 @@ * Time Complexity: O(1) | ||
* The _replaceNode function replaces an old node with a new node in a binary tree structure. | ||
* @param {NODE} oldNode - The `oldNode` parameter represents the node that you want to replace in a | ||
* @param {BinaryTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that you want to replace in a | ||
* tree data structure. | ||
* @param {NODE} newNode - The `newNode` parameter in the `_replaceNode` function represents the node | ||
* @param {BinaryTreeNode<K, V>} newNode - The `newNode` parameter in the `_replaceNode` function represents the node | ||
* that will replace the `oldNode` in a tree data structure. This function is responsible for | ||
@@ -895,3 +945,3 @@ * updating the parent, left child, right child, and root (if necessary) references when replacing a | ||
*/ | ||
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE; | ||
protected _replaceNode(oldNode: BinaryTreeNode<K, V>, newNode: BinaryTreeNode<K, V>): BinaryTreeNode<K, V>; | ||
/** | ||
@@ -903,6 +953,6 @@ * Time Complexity: O(1) | ||
* of the previous root node. | ||
* @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<NODE>`, which means | ||
* it can either be an optional `NODE` type or `null`. | ||
* @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<BinaryTreeNode<K, V>>`, which means | ||
* it can either be an optional `BinaryTreeNode<K, V>` type or `null`. | ||
*/ | ||
protected _setRoot(v: OptNodeOrNull<NODE>): void; | ||
protected _setRoot(v: OptNodeOrNull<BinaryTreeNode<K, V>>): void; | ||
/** | ||
@@ -914,9 +964,9 @@ * Time Complexity: O(1) | ||
* predicate function for a binary tree node. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input | ||
* parameter `keyNodeEntryRawOrPredicate` is transformed into a valid predicate function that can be | ||
* parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be | ||
* used for filtering nodes in a binary tree. | ||
* @returns A NodePredicate<NODE> function is being returned. | ||
* @returns A NodePredicate<BinaryTreeNode<K, V>> function is being returned. | ||
*/ | ||
protected _ensurePredicate(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>): NodePredicate<NODE>; | ||
protected _ensurePredicate(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>): NodePredicate<BinaryTreeNode<K, V>>; | ||
/** | ||
@@ -929,3 +979,3 @@ * Time Complexity: O(1) | ||
* of value. In this context, the function `_isPredicate` is checking if `p` is a function that | ||
* satisfies the type `NodePredicate<NODE>`. | ||
* satisfies the type `NodePredicate<BinaryTreeNode<K, V>>`. | ||
* @returns The function is checking if the input `p` is a function and returning a boolean value | ||
@@ -935,3 +985,3 @@ * based on that check. If `p` is a function, it will return `true`, indicating that `p` is a | ||
*/ | ||
protected _isPredicate(p: any): p is NodePredicate<NODE>; | ||
protected _isPredicate(p: any): p is NodePredicate<BinaryTreeNode<K, V>>; | ||
/** | ||
@@ -943,10 +993,10 @@ * Time Complexity: O(1) | ||
* entry, raw data, or null/undefined. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a | ||
* TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`, | ||
* where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and ` | ||
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw` | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `_extractKey` method you provided is a | ||
* TypeScript method that takes in a parameter `keyNodeOrEntry` of type `BTNRep<K, V, BinaryTreeNode<K, V>>`, | ||
* where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and ` | ||
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry` | ||
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on | ||
* the conditions checked in the method. | ||
*/ | ||
protected _extractKey(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): K | null | undefined; | ||
protected _extractKey(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): K | null | undefined; | ||
/** | ||
@@ -953,0 +1003,0 @@ * Time Complexity: O(1) |
@@ -8,34 +8,24 @@ /** | ||
*/ | ||
import { BSTNested, BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode, OptNodeOrNull } from '../../types'; | ||
import type { BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode, OptNodeOrNull } from '../../types'; | ||
import { BinaryTree, BinaryTreeNode } from './binary-tree'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { Range } from '../../common'; | ||
export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> { | ||
parent?: NODE; | ||
constructor(key: K, value?: V); | ||
_left?: OptNodeOrNull<NODE>; | ||
export declare class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> { | ||
/** | ||
* The function returns the value of the `_left` property. | ||
* @returns The `_left` property of the current object is being returned. | ||
* This TypeScript constructor function initializes an instance with a key and an optional value. | ||
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element | ||
* within a data structure. It serves as a reference or identifier for accessing or manipulating the | ||
* associated value. | ||
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not | ||
* have to be provided when creating an instance of the class. If a value is not provided, it will | ||
* default to `undefined`. | ||
*/ | ||
get left(): OptNodeOrNull<NODE>; | ||
/** | ||
* The function sets the left child of a node and updates the parent reference of the child. | ||
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an | ||
* instance of the `NODE` class or `undefined`. | ||
*/ | ||
set left(v: OptNodeOrNull<NODE>); | ||
_right?: OptNodeOrNull<NODE>; | ||
/** | ||
* The function returns the right node of a binary tree or undefined if there is no right node. | ||
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or | ||
* `undefined`. | ||
*/ | ||
get right(): OptNodeOrNull<NODE>; | ||
/** | ||
* The function sets the right child of a node and updates the parent reference of the child. | ||
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a | ||
* `NODE` object or `undefined`. | ||
*/ | ||
set right(v: OptNodeOrNull<NODE>); | ||
constructor(key: K, value?: V); | ||
parent?: BSTNode<K, V>; | ||
_left?: OptNodeOrNull<BSTNode<K, V>>; | ||
get left(): OptNodeOrNull<BSTNode<K, V>>; | ||
set left(v: OptNodeOrNull<BSTNode<K, V>>); | ||
_right?: OptNodeOrNull<BSTNode<K, V>>; | ||
get right(): OptNodeOrNull<BSTNode<K, V>>; | ||
set right(v: OptNodeOrNull<BSTNode<K, V>>); | ||
} | ||
@@ -107,26 +97,25 @@ /** | ||
*/ | ||
export declare class BST<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, R, MK, MV, MR, NODE, TREE> = BST<K, V, R, MK, MV, MR, NODE, BSTNested<K, V, R, MK, MV, MR, NODE>>> extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> { | ||
export declare class BST<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends BinaryTree<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> { | ||
/** | ||
* This is the constructor function for a Binary Search Tree class in TypeScript. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be | ||
* added to the binary search tree during the construction of the object. | ||
* @param [options] - An optional object that contains additional options for the Binary Search Tree. | ||
* It can include a comparator function that defines the order of the elements in the tree. | ||
* This TypeScript constructor initializes a binary search tree with optional options and adds | ||
* elements if provided. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to | ||
* initialize the binary search tree with keys, nodes, entries, or raw data. | ||
* @param [options] - The `options` parameter is an optional object that can contain the following | ||
* properties: | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: BSTOptions<K, V, R>); | ||
protected _root?: NODE; | ||
/** | ||
* The function returns the root node of a tree structure. | ||
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`. | ||
*/ | ||
get root(): OptNode<NODE>; | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, BSTNode<K, V>> | R>, options?: BSTOptions<K, V, R>); | ||
protected _root?: BSTNode<K, V>; | ||
get root(): OptNode<BSTNode<K, V>>; | ||
protected _isReverse: boolean; | ||
/** | ||
* The above function is a getter method in TypeScript that returns the value of the private property | ||
* `_isReverse`. | ||
* @returns The `isReverse` property of the object, which is a boolean value. | ||
*/ | ||
get isReverse(): boolean; | ||
protected _comparator: Comparator<K>; | ||
get comparator(): Comparator<K>; | ||
protected _specifyComparable?: (key: K) => Comparable; | ||
get specifyComparable(): ((key: K) => Comparable) | undefined; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new BSTNode with the given key and value and returns it. | ||
@@ -137,6 +126,9 @@ * @param {K} key - The key parameter is of type K, which represents the type of the key for the node | ||
* value associated with the key in the node being created. | ||
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type. | ||
* @returns The method is returning a new instance of the BSTNode class, casted as the BSTNode<K, V> type. | ||
*/ | ||
createNode(key: K, value?: V): NODE; | ||
createNode(key: K, value?: V): BSTNode<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new binary search tree with the specified options. | ||
@@ -148,14 +140,4 @@ * @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
*/ | ||
createTree(options?: BSTOptions<K, V, R>): TREE; | ||
createTree(options?: BSTOptions<K, V, R>): BST<K, V, R, MK, MV, MR>; | ||
/** | ||
* The function overrides a method and converts a key, value pair or entry or raw element to a node. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of | ||
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw | ||
* element. | ||
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the | ||
* value associated with a key in a key-value pair. | ||
* @returns either a NODE object or undefined. | ||
*/ | ||
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined]; | ||
/** | ||
* Time Complexity: O(log n) | ||
@@ -166,4 +148,4 @@ * Space Complexity: O(log n) | ||
* it doesn't exist. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept a value of type `R`, which represents the key, node, | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node, | ||
* entry, or raw element that needs to be ensured in the tree. | ||
@@ -176,27 +158,33 @@ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional | ||
*/ | ||
ensureNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, iterationType?: IterationType): OptNode<NODE>; | ||
ensureNode(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the input is an instance of the BSTNode 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 | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `BSTNode` class. | ||
*/ | ||
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE; | ||
isNode(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>): keyNodeOrEntry is BSTNode<K, V>; | ||
/** | ||
* The function "override isKey" checks if a key is comparable based on a given comparator. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function "override isValidKey" checks if a key is comparable based on a given comparator. | ||
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of | ||
* type `K`. | ||
* @returns The `override isKey(key: any): key is K` function is returning a boolean value based on | ||
* @returns The `override isValidKey(key: any): key is K` function is returning a boolean value based on | ||
* the result of the `isComparable` function with the condition `this._compare !== | ||
* this._DEFAULT_COMPARATOR`. | ||
*/ | ||
isKey(key: any): key is K; | ||
isValidKey(key: any): key is K; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the | ||
@@ -206,3 +194,3 @@ * key in the binary search tree. If provided, it will be stored in the node along with the key. | ||
*/ | ||
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean; | ||
add(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, value?: V): boolean; | ||
/** | ||
@@ -229,3 +217,3 @@ * Time Complexity: O(k log n) | ||
*/ | ||
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[]; | ||
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[]; | ||
/** | ||
@@ -237,4 +225,4 @@ * Time Complexity: O(log n) | ||
* on specified criteria. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The | ||
* `keyNodeEntryRawOrPredicate` parameter in the `override search` method can accept one of the | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the | ||
* following types: | ||
@@ -246,5 +234,5 @@ * @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the | ||
* that will be called on each node that matches the search criteria. It is of type `C`, which | ||
* extends `NodeCallback<NODE>`. The callback function should accept a node of type `NODE` as its | ||
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its | ||
* argument and | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `override search` | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search` | ||
* method represents the node from which the search operation will begin. It is the starting point | ||
@@ -261,6 +249,6 @@ * for searching within the tree data structure. The method ensures that the `startNode` is a valid | ||
*/ | ||
search<C extends NodeCallback<NODE>>(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[]; | ||
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(k + log n) | ||
* | ||
@@ -272,5 +260,5 @@ * The `rangeSearch` function searches for nodes within a specified range in a binary search tree. | ||
* function that is used to process each node that is found within the specified range during the | ||
* search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the | ||
* search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the | ||
* data structure. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch` | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch` | ||
* function represents the node from which the search for nodes within the specified range will | ||
@@ -285,11 +273,11 @@ * begin. It is the starting point for the range search operation. | ||
*/ | ||
rangeSearch<C extends NodeCallback<NODE>>(range: Range<K> | [K, K], callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[]; | ||
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate` | ||
* parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`. | ||
* @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method | ||
* This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure. | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` | ||
* parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`. | ||
* @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method | ||
* is used to specify the starting point for searching nodes in the binary search tree. If no | ||
@@ -302,8 +290,8 @@ * specific starting point is provided, the default value is set to `this._root`, which is the root | ||
* no value is provided when calling the method. | ||
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`). | ||
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at | ||
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<BSTNode<K, V>>`). | ||
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryOrPredicate, beginning at | ||
* the specified root node (`startNode`) and using the specified iteration type. The method then | ||
* returns the first node found or `undefined` if no node is found. | ||
*/ | ||
getNode(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, startNode?: R | BSTNOptKeyOrNode<K, NODE>, iterationType?: IterationType): OptNode<NODE>; | ||
getNode(keyNodeEntryOrPredicate: BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>, startNode?: BSTNOptKeyOrNode<K, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>; | ||
/** | ||
@@ -321,3 +309,3 @@ * Time complexity: O(n) | ||
* take one of the following values: | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a | ||
@@ -330,3 +318,3 @@ * node entry. If not specified, the default value is the root of the tree. | ||
*/ | ||
dfs<C extends NodeCallback<NODE>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[]; | ||
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -341,3 +329,3 @@ * Time complexity: O(n) | ||
* node being visited, and it can return a value of any type. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry | ||
@@ -350,3 +338,3 @@ * object. If no value is provided, the default value is the root of the tree. | ||
*/ | ||
bfs<C extends NodeCallback<NODE>>(callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[]; | ||
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -359,5 +347,5 @@ * Time complexity: O(n) | ||
* @param {C} callback - The `callback` parameter is a generic type `C` that extends | ||
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the | ||
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the | ||
* tree during the iteration process. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for listing the levels of the binary tree. It can be either a root node of the tree, a | ||
@@ -371,3 +359,3 @@ * key-value pair representing a node in the tree, or a key representing a node in the tree. If no | ||
*/ | ||
listLevels<C extends NodeCallback<NODE>>(callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[][]; | ||
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[][]; | ||
/** | ||
@@ -385,3 +373,3 @@ * Time complexity: O(n) | ||
* 0, or 1, where: | ||
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in | ||
* the binary tree that you want to start traversing from. It can be specified either by providing | ||
@@ -395,3 +383,3 @@ * the key of the node, the node itself, or an entry containing the key and value of the node. If no | ||
*/ | ||
lesserOrGreaterTraverse<C extends NodeCallback<NODE>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[]; | ||
lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -423,23 +411,68 @@ * Time complexity: O(n) | ||
isAVLBalanced(iterationType?: IterationType): boolean; | ||
protected _comparator: Comparator<K>; | ||
/** | ||
* The function returns the value of the _comparator property. | ||
* @returns The `_comparator` property is being returned. | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default map behavior for a binary search tree by | ||
* applying a callback function to each entry and creating a new tree with the results. | ||
* @param callback - A function that will be called for each entry in the BST. It takes four | ||
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to | ||
* the BST itself. | ||
* @param [options] - The `options` parameter in the `override map` method is of type `BSTOptions<MK, | ||
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the Binary | ||
* Search Tree (BST) being created in the `map` method. These options could include configuration | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` method is used to specify | ||
* the value of `this` that should be used when executing the `callback` function. It allows you to | ||
* set the context or scope in which the callback function will be called. This can be useful when | ||
* you want | ||
* @returns The `map` method is returning a new Binary Search Tree (`BST`) instance with the entries | ||
* transformed by the provided callback function. | ||
*/ | ||
get comparator(): Comparator<K>; | ||
protected _specifyComparable?: (key: K) => Comparable; | ||
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BSTOptions<MK, MV, MR>, thisArg?: any): BST<MK, MV, MR>; | ||
/** | ||
* This function returns the value of the `_specifyComparable` property. | ||
* @returns The method `specifyComparable()` is being returned, which is a getter method for the | ||
* `_specifyComparable` property. | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns The `cloned` object is being returned. | ||
*/ | ||
get specifyComparable(): ((key: K) => Comparable) | undefined; | ||
clone(): BST<K, V, R, MK, MV, MR>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function overrides a method and converts a key, value pair or entry or raw element to a node. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of | ||
* type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw | ||
* element. | ||
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the | ||
* value associated with a key in a key-value pair. | ||
* @returns either a BSTNode<K, V> object or undefined. | ||
*/ | ||
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, value?: V): [OptNode<BSTNode<K, V>>, V | undefined]; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function sets the root of a tree-like structure and updates the parent property of the new | ||
* root. | ||
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined. | ||
* @param {OptNode<BSTNode<K, V>>} v - v is a parameter of type BSTNode<K, V> or undefined. | ||
*/ | ||
protected _setRoot(v: OptNode<NODE>): void; | ||
protected _setRoot(v: OptNode<BSTNode<K, V>>): void; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The _compare function compares two values using a specified comparator function and optionally | ||
* reverses the result. | ||
* @param {K} a - The parameter `a` is of type `K`, which is used as an input for comparison in the | ||
* `_compare` method. | ||
* @param {K} b - The parameter `b` in the `_compare` function is of type `K`. | ||
* @returns The `_compare` method is returning the result of the ternary expression. If `_isReverse` | ||
* is true, it returns the negation of the result of calling the `_comparator` function with | ||
* arguments `a` and `b`. If `_isReverse` is false, it returns the result of calling the | ||
* `_comparator` function with arguments `a` and `b`. | ||
*/ | ||
protected _compare(a: K, b: K): number; | ||
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BSTOptions<MK, MV, MR>, thisArg?: any): BST<MK, MV, MR>; | ||
} |
@@ -9,2 +9,11 @@ "use strict"; | ||
class BSTNode extends binary_tree_1.BinaryTreeNode { | ||
/** | ||
* This TypeScript constructor function initializes an instance with a key and an optional value. | ||
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element | ||
* within a data structure. It serves as a reference or identifier for accessing or manipulating the | ||
* associated value. | ||
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not | ||
* have to be provided when creating an instance of the class. If a value is not provided, it will | ||
* default to `undefined`. | ||
*/ | ||
constructor(key, value) { | ||
@@ -16,14 +25,5 @@ super(key, value); | ||
} | ||
/** | ||
* The function returns the value of the `_left` property. | ||
* @returns The `_left` property of the current object is being returned. | ||
*/ | ||
get left() { | ||
return this._left; | ||
} | ||
/** | ||
* The function sets the left child of a node and updates the parent reference of the child. | ||
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an | ||
* instance of the `NODE` class or `undefined`. | ||
*/ | ||
set left(v) { | ||
@@ -35,15 +35,5 @@ if (v) { | ||
} | ||
/** | ||
* The function returns the right node of a binary tree or undefined if there is no right node. | ||
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or | ||
* `undefined`. | ||
*/ | ||
get right() { | ||
return this._right; | ||
} | ||
/** | ||
* The function sets the right child of a node and updates the parent reference of the child. | ||
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a | ||
* `NODE` object or `undefined`. | ||
*/ | ||
set right(v) { | ||
@@ -124,8 +114,9 @@ if (v) { | ||
/** | ||
* This is the constructor function for a Binary Search Tree class in TypeScript. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be | ||
* added to the binary search tree during the construction of the object. | ||
* @param [options] - An optional object that contains additional options for the Binary Search Tree. | ||
* It can include a comparator function that defines the order of the elements in the tree. | ||
* This TypeScript constructor initializes a binary search tree with optional options and adds | ||
* elements if provided. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to | ||
* initialize the binary search tree with keys, nodes, entries, or raw data. | ||
* @param [options] - The `options` parameter is an optional object that can contain the following | ||
* properties: | ||
*/ | ||
@@ -166,18 +157,18 @@ constructor(keysNodesEntriesOrRaws = [], options) { | ||
} | ||
/** | ||
* The function returns the root node of a tree structure. | ||
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`. | ||
*/ | ||
get root() { | ||
return this._root; | ||
} | ||
/** | ||
* The above function is a getter method in TypeScript that returns the value of the private property | ||
* `_isReverse`. | ||
* @returns The `isReverse` property of the object, which is a boolean value. | ||
*/ | ||
get isReverse() { | ||
return this._isReverse; | ||
} | ||
get comparator() { | ||
return this._comparator; | ||
} | ||
get specifyComparable() { | ||
return this._specifyComparable; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new BSTNode with the given key and value and returns it. | ||
@@ -188,3 +179,3 @@ * @param {K} key - The key parameter is of type K, which represents the type of the key for the node | ||
* value associated with the key in the node being created. | ||
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type. | ||
* @returns The method is returning a new instance of the BSTNode class, casted as the BSTNode<K, V> type. | ||
*/ | ||
@@ -195,2 +186,5 @@ createNode(key, value) { | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new binary search tree with the specified options. | ||
@@ -206,17 +200,2 @@ * @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
/** | ||
* The function overrides a method and converts a key, value pair or entry or raw element to a node. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of | ||
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw | ||
* element. | ||
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the | ||
* value associated with a key in a key-value pair. | ||
* @returns either a NODE object or undefined. | ||
*/ | ||
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) { | ||
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
if (node === null) | ||
return [undefined, undefined]; | ||
return [node, value !== null && value !== void 0 ? value : entryValue]; | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
@@ -227,4 +206,4 @@ * Space Complexity: O(log n) | ||
* it doesn't exist. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept a value of type `R`, which represents the key, node, | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node, | ||
* entry, or raw element that needs to be ensured in the tree. | ||
@@ -237,25 +216,31 @@ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional | ||
*/ | ||
ensureNode(keyNodeEntryOrRaw, iterationType = this.iterationType) { | ||
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) { | ||
var _a; | ||
return (_a = super.ensureNode(keyNodeEntryOrRaw, iterationType)) !== null && _a !== void 0 ? _a : undefined; | ||
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) !== null && _a !== void 0 ? _a : undefined; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the input is an instance of the BSTNode 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 | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `BSTNode` class. | ||
*/ | ||
isNode(keyNodeEntryOrRaw) { | ||
return keyNodeEntryOrRaw instanceof BSTNode; | ||
isNode(keyNodeOrEntry) { | ||
return keyNodeOrEntry instanceof BSTNode; | ||
} | ||
/** | ||
* The function "override isKey" checks if a key is comparable based on a given comparator. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function "override isValidKey" checks if a key is comparable based on a given comparator. | ||
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of | ||
* type `K`. | ||
* @returns The `override isKey(key: any): key is K` function is returning a boolean value based on | ||
* @returns The `override isValidKey(key: any): key is K` function is returning a boolean value based on | ||
* the result of the `isComparable` function with the condition `this._compare !== | ||
* this._DEFAULT_COMPARATOR`. | ||
*/ | ||
isKey(key) { | ||
isValidKey(key) { | ||
return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined); | ||
@@ -265,7 +250,7 @@ } | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the | ||
@@ -275,4 +260,4 @@ * key in the binary search tree. If provided, it will be stored in the node along with the key. | ||
*/ | ||
add(keyNodeEntryOrRaw, value) { | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
add(keyNodeOrEntry, value) { | ||
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value); | ||
if (newNode === undefined) | ||
@@ -348,4 +333,6 @@ return false; | ||
if (!isBalanceAdd) { | ||
for (const kve of keysNodesEntriesOrRaws) { | ||
for (let kve of keysNodesEntriesOrRaws) { | ||
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value; | ||
if (this.isRaw(kve)) | ||
kve = this._toEntryFn(kve); | ||
inserted.push(this.add(kve, value)); | ||
@@ -364,19 +351,17 @@ } | ||
let keyA, keyB; | ||
if (this.isEntry(a)) | ||
if (this.isRaw(a)) | ||
keyA = this._toEntryFn(a)[0]; | ||
else if (this.isEntry(a)) | ||
keyA = a[0]; | ||
else if (this.isRealNode(a)) | ||
keyA = a.key; | ||
else if (this._toEntryFn) { | ||
keyA = this._toEntryFn(a)[0]; | ||
} | ||
else { | ||
keyA = a; | ||
} | ||
if (this.isEntry(b)) | ||
if (this.isRaw(b)) | ||
keyB = this._toEntryFn(b)[0]; | ||
else if (this.isEntry(b)) | ||
keyB = b[0]; | ||
else if (this.isRealNode(b)) | ||
keyB = b.key; | ||
else if (this._toEntryFn) { | ||
keyB = this._toEntryFn(b)[0]; | ||
} | ||
else { | ||
@@ -391,6 +376,13 @@ keyB = b; | ||
const _dfs = (arr) => { | ||
var _a; | ||
if (arr.length === 0) | ||
return; | ||
const mid = Math.floor((arr.length - 1) / 2); | ||
const { key, value, orgIndex } = arr[mid]; | ||
let { key, value } = arr[mid]; | ||
const { orgIndex } = arr[mid]; | ||
if (this.isRaw(key)) { | ||
const entry = this._toEntryFn(key); | ||
key = entry[0]; | ||
value = (_a = entry[1]) !== null && _a !== void 0 ? _a : value; | ||
} | ||
inserted[orgIndex] = this.add(key, value); | ||
@@ -401,2 +393,3 @@ _dfs(arr.slice(0, mid)); | ||
const _iterate = () => { | ||
var _a; | ||
const n = sorted.length; | ||
@@ -410,3 +403,9 @@ const stack = [[0, n - 1]]; | ||
const m = l + Math.floor((r - l) / 2); | ||
const { key, value, orgIndex } = sorted[m]; | ||
let { key, value } = sorted[m]; | ||
const { orgIndex } = sorted[m]; | ||
if (this.isRaw(key)) { | ||
const entry = this._toEntryFn(key); | ||
key = entry[0]; | ||
value = (_a = entry[1]) !== null && _a !== void 0 ? _a : value; | ||
} | ||
inserted[orgIndex] = this.add(key, value); | ||
@@ -433,4 +432,4 @@ stack.push([m + 1, r]); | ||
* on specified criteria. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The | ||
* `keyNodeEntryRawOrPredicate` parameter in the `override search` method can accept one of the | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the | ||
* following types: | ||
@@ -442,5 +441,5 @@ * @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the | ||
* that will be called on each node that matches the search criteria. It is of type `C`, which | ||
* extends `NodeCallback<NODE>`. The callback function should accept a node of type `NODE` as its | ||
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its | ||
* argument and | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `override search` | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search` | ||
* method represents the node from which the search operation will begin. It is the starting point | ||
@@ -457,6 +456,6 @@ * for searching within the tree data structure. The method ensures that the `startNode` is a valid | ||
*/ | ||
search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) { | ||
if (keyNodeEntryRawOrPredicate === undefined) | ||
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) { | ||
if (keyNodeEntryOrPredicate === undefined) | ||
return []; | ||
if (keyNodeEntryRawOrPredicate === null) | ||
if (keyNodeEntryOrPredicate === null) | ||
return []; | ||
@@ -467,13 +466,13 @@ startNode = this.ensureNode(startNode); | ||
let predicate; | ||
const isRange = this.isRange(keyNodeEntryRawOrPredicate); | ||
const isRange = this.isRange(keyNodeEntryOrPredicate); | ||
// Set predicate based on parameter type | ||
if (isRange) { | ||
predicate = node => keyNodeEntryRawOrPredicate.isInRange(node.key, this._comparator); | ||
predicate = node => keyNodeEntryOrPredicate.isInRange(node.key, this._comparator); | ||
} | ||
else { | ||
predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate); | ||
predicate = this._ensurePredicate(keyNodeEntryOrPredicate); | ||
} | ||
const isToLeftByRange = (cur) => { | ||
if (isRange) { | ||
const range = keyNodeEntryRawOrPredicate; | ||
const range = keyNodeEntryOrPredicate; | ||
const leftS = this.isReverse ? range.high : range.low; | ||
@@ -487,3 +486,3 @@ const leftI = this.isReverse ? range.includeHigh : range.includeLow; | ||
if (isRange) { | ||
const range = keyNodeEntryRawOrPredicate; | ||
const range = keyNodeEntryOrPredicate; | ||
const rightS = this.isReverse ? range.low : range.high; | ||
@@ -511,4 +510,4 @@ const rightI = this.isReverse ? range.includeLow : range.includeLow; | ||
} | ||
else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate); | ||
else if (!this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
if (this.isRealNode(cur.left) && | ||
@@ -549,4 +548,4 @@ benchmarkKey !== null && | ||
} | ||
else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate); | ||
else if (!this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
if (this.isRealNode(cur.right) && | ||
@@ -575,3 +574,3 @@ benchmarkKey !== null && | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(k + log n) | ||
* | ||
@@ -583,5 +582,5 @@ * The `rangeSearch` function searches for nodes within a specified range in a binary search tree. | ||
* function that is used to process each node that is found within the specified range during the | ||
* search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the | ||
* search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the | ||
* data structure. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch` | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch` | ||
* function represents the node from which the search for nodes within the specified range will | ||
@@ -602,8 +601,8 @@ * begin. It is the starting point for the range search operation. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate` | ||
* parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`. | ||
* @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method | ||
* This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure. | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` | ||
* parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`. | ||
* @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method | ||
* is used to specify the starting point for searching nodes in the binary search tree. If no | ||
@@ -616,10 +615,10 @@ * specific starting point is provided, the default value is set to `this._root`, which is the root | ||
* no value is provided when calling the method. | ||
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`). | ||
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at | ||
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<BSTNode<K, V>>`). | ||
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryOrPredicate, beginning at | ||
* the specified root node (`startNode`) and using the specified iteration type. The method then | ||
* returns the first node found or `undefined` if no node is found. | ||
*/ | ||
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) { | ||
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) { | ||
var _a; | ||
return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined; | ||
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined; | ||
} | ||
@@ -638,3 +637,3 @@ /** | ||
* take one of the following values: | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a | ||
@@ -659,3 +658,3 @@ * node entry. If not specified, the default value is the root of the tree. | ||
* node being visited, and it can return a value of any type. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry | ||
@@ -678,5 +677,5 @@ * object. If no value is provided, the default value is the root of the tree. | ||
* @param {C} callback - The `callback` parameter is a generic type `C` that extends | ||
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the | ||
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the | ||
* tree during the iteration process. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for listing the levels of the binary tree. It can be either a root node of the tree, a | ||
@@ -705,3 +704,3 @@ * key-value pair representing a node in the tree, or a key representing a node in the tree. If no | ||
* 0, or 1, where: | ||
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in | ||
* the binary tree that you want to start traversing from. It can be specified either by providing | ||
@@ -867,20 +866,66 @@ * the key of the node, the node itself, or an entry containing the key and value of the node. If no | ||
/** | ||
* The function returns the value of the _comparator property. | ||
* @returns The `_comparator` property is being returned. | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default map behavior for a binary search tree by | ||
* applying a callback function to each entry and creating a new tree with the results. | ||
* @param callback - A function that will be called for each entry in the BST. It takes four | ||
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to | ||
* the BST itself. | ||
* @param [options] - The `options` parameter in the `override map` method is of type `BSTOptions<MK, | ||
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the Binary | ||
* Search Tree (BST) being created in the `map` method. These options could include configuration | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` method is used to specify | ||
* the value of `this` that should be used when executing the `callback` function. It allows you to | ||
* set the context or scope in which the callback function will be called. This can be useful when | ||
* you want | ||
* @returns The `map` method is returning a new Binary Search Tree (`BST`) instance with the entries | ||
* transformed by the provided callback function. | ||
*/ | ||
get comparator() { | ||
return this._comparator; | ||
map(callback, options, thisArg) { | ||
const newTree = new BST([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
/** | ||
* This function returns the value of the `_specifyComparable` property. | ||
* @returns The method `specifyComparable()` is being returned, which is a getter method for the | ||
* `_specifyComparable` property. | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns The `cloned` object is being returned. | ||
*/ | ||
get specifyComparable() { | ||
return this._specifyComparable; | ||
clone() { | ||
const cloned = this.createTree(); | ||
this._clone(cloned); | ||
return cloned; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function overrides a method and converts a key, value pair or entry or raw element to a node. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of | ||
* type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw | ||
* element. | ||
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the | ||
* value associated with a key in a key-value pair. | ||
* @returns either a BSTNode<K, V> object or undefined. | ||
*/ | ||
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) { | ||
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value); | ||
if (node === null) | ||
return [undefined, undefined]; | ||
return [node, value !== null && value !== void 0 ? value : entryValue]; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function sets the root of a tree-like structure and updates the parent property of the new | ||
* root. | ||
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined. | ||
* @param {OptNode<BSTNode<K, V>>} v - v is a parameter of type BSTNode<K, V> or undefined. | ||
*/ | ||
@@ -893,14 +938,20 @@ _setRoot(v) { | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The _compare function compares two values using a specified comparator function and optionally | ||
* reverses the result. | ||
* @param {K} a - The parameter `a` is of type `K`, which is used as an input for comparison in the | ||
* `_compare` method. | ||
* @param {K} b - The parameter `b` in the `_compare` function is of type `K`. | ||
* @returns The `_compare` method is returning the result of the ternary expression. If `_isReverse` | ||
* is true, it returns the negation of the result of calling the `_comparator` function with | ||
* arguments `a` and `b`. If `_isReverse` is false, it returns the result of calling the | ||
* `_comparator` function with arguments `a` and `b`. | ||
*/ | ||
_compare(a, b) { | ||
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b); | ||
} | ||
map(callback, options, thisArg) { | ||
const newTree = new BST([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
} | ||
exports.BST = BST; |
@@ -9,1 +9,3 @@ export * from './binary-tree'; | ||
export * from './tree-multi-map'; | ||
export * from './tree-counter'; | ||
export * from './avl-tree-counter'; |
@@ -25,1 +25,3 @@ "use strict"; | ||
__exportStar(require("./tree-multi-map"), exports); | ||
__exportStar(require("./tree-counter"), exports); | ||
__exportStar(require("./avl-tree-counter"), exports); |
@@ -1,17 +0,23 @@ | ||
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types'; | ||
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, OptNodeOrNull, RBTNColor, RedBlackTreeOptions } from '../../types'; | ||
import { BST, BSTNode } from './bst'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> { | ||
export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> { | ||
/** | ||
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a | ||
* color. | ||
* @param {K} key - The key parameter is of type K and represents the key of the node in the | ||
* Red-Black 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 instance of the Red-Black Tree Node. | ||
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black | ||
* Tree Node. It is an optional parameter with a default value of `'BLACK'`. | ||
* The constructor initializes a node with a key, value, and color for a Red-Black Tree. | ||
* @param {K} key - The `key` parameter is a key of type `K` that is used to identify the node in a | ||
* Red-Black Tree data structure. | ||
* @param {V} [value] - The `value` parameter in the constructor is an optional parameter of type | ||
* `V`. It represents the value associated with the key in the data structure being constructed. | ||
* @param {RBTNColor} [color=BLACK] - The `color` parameter in the constructor is used to specify the | ||
* color of the node in a Red-Black Tree. It has a default value of 'BLACK' if not provided | ||
* explicitly. | ||
*/ | ||
constructor(key: K, value?: V, color?: RBTNColor); | ||
parent?: RedBlackTreeNode<K, V>; | ||
_left?: OptNodeOrNull<RedBlackTreeNode<K, V>>; | ||
get left(): OptNodeOrNull<RedBlackTreeNode<K, V>>; | ||
set left(v: OptNodeOrNull<RedBlackTreeNode<K, V>>); | ||
_right?: OptNodeOrNull<RedBlackTreeNode<K, V>>; | ||
get right(): OptNodeOrNull<RedBlackTreeNode<K, V>>; | ||
set right(v: OptNodeOrNull<RedBlackTreeNode<K, V>>); | ||
} | ||
@@ -71,21 +77,21 @@ /** | ||
*/ | ||
export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE> = RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTreeNested<K, V, R, MK, MV, MR, NODE>>> extends BST<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> { | ||
export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends BST<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> { | ||
/** | ||
* This is the constructor function for a Red-Black Tree data structure in TypeScript. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to | ||
* initialize the RBTree with the provided elements. | ||
* @param [options] - The `options` parameter is an optional object that can be passed to the | ||
* constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for | ||
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would | ||
* depend on the implementation | ||
* This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or | ||
* raw data. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It | ||
* is used to initialize the Red-Black Tree with keys, nodes, entries, or | ||
* @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K, | ||
* V, R>`. It is an optional parameter that allows you to specify additional options for the | ||
* RedBlackTree class. These options could include configuration settings, behavior customization, or | ||
* any other parameters that are specific to | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: RedBlackTreeOptions<K, V, R>); | ||
protected _root: NODE | undefined; | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, RedBlackTreeNode<K, V>> | R>, options?: RedBlackTreeOptions<K, V, R>); | ||
protected _root: RedBlackTreeNode<K, V> | undefined; | ||
get root(): RedBlackTreeNode<K, V> | undefined; | ||
/** | ||
* The function returns the root node of a tree or undefined if there is no root. | ||
* @returns The root node of the tree structure, or undefined if there is no root node. | ||
*/ | ||
get root(): NODE | undefined; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new Red-Black Tree node with the specified key, value, and color. | ||
@@ -104,4 +110,7 @@ * @param {K} key - The key parameter represents the key value of the node being created. It is of | ||
*/ | ||
createNode(key: K, value?: V, color?: RBTNColor): NODE; | ||
createNode(key: K, value?: V, color?: RBTNColor): RedBlackTreeNode<K, V>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new Red-Black Tree with the specified options. | ||
@@ -112,3 +121,3 @@ * @param [options] - The `options` parameter is an optional object that contains additional | ||
*/ | ||
createTree(options?: RedBlackTreeOptions<K, V, R>): TREE; | ||
createTree(options?: RedBlackTreeOptions<K, V, R>): RedBlackTree<K, V, R, MK, MV, MR>; | ||
/** | ||
@@ -119,8 +128,8 @@ * Time Complexity: O(1) | ||
* The function checks if the input is an instance of the RedBlackTreeNode 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 | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `RedBlackTreeNode` class. | ||
*/ | ||
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE; | ||
isNode(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>): keyNodeOrEntry is RedBlackTreeNode<K, V>; | ||
/** | ||
@@ -136,8 +145,8 @@ * Time Complexity: O(1) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function adds a new node to a binary search tree and returns true if the node was successfully | ||
* added. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -150,19 +159,49 @@ * the key in the data structure. It represents the value that you want to add or update in the data | ||
*/ | ||
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean; | ||
add(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>, value?: V): boolean; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the delete method in a binary tree data structure to remove a node based on | ||
* a given predicate and maintain the binary search tree properties. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw` | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override 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, an entry, or a predicate | ||
* function that determines which node(s) should be deleted. | ||
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<NODE>` | ||
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>` | ||
* objects. Each object in the array contains information about the deleted node and whether | ||
* balancing is needed. | ||
*/ | ||
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[]; | ||
delete(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[]; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by | ||
* applying a callback to each entry in the original tree. | ||
* @param callback - A function that will be called for each entry in the tree, with parameters | ||
* representing the key, value, index, and the tree itself. It should return an entry for the new | ||
* tree. | ||
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV, | ||
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black | ||
* Tree that will be created during the mapping process. These options could include things like | ||
* custom comparators | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function. This can be useful when you want to access properties | ||
* or | ||
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the | ||
* provided callback function. | ||
*/ | ||
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: RedBlackTreeOptions<MK, MV, MR>, thisArg?: any): RedBlackTree<MK, MV, MR>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns The `cloned` object is being returned. | ||
*/ | ||
clone(): RedBlackTree<K, V, R, MK, MV, MR>; | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -173,5 +212,5 @@ * Space Complexity: O(1) | ||
* root. | ||
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined. | ||
* @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined. | ||
*/ | ||
protected _setRoot(v: NODE | undefined): void; | ||
protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void; | ||
/** | ||
@@ -182,5 +221,5 @@ * Time Complexity: O(1) | ||
* The function replaces an old node with a new node while preserving the color of the old node. | ||
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* the data structure. | ||
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a | ||
* @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a | ||
* data structure. | ||
@@ -190,10 +229,10 @@ * @returns The method is returning the result of calling the `_replaceNode` method from the | ||
*/ | ||
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE; | ||
protected _replaceNode(oldNode: RedBlackTreeNode<K, V>, newNode: RedBlackTreeNode<K, V>): RedBlackTreeNode<K, V>; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to | ||
* maintain the red-black tree properties. | ||
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the | ||
* @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the | ||
* binary search tree. | ||
@@ -204,3 +243,3 @@ * @returns a string value indicating the result of the insertion operation. It can return either | ||
*/ | ||
protected _insert(node: NODE): CRUD; | ||
protected _insert(node: RedBlackTreeNode<K, V>): CRUD; | ||
/** | ||
@@ -211,7 +250,7 @@ * Time Complexity: O(1) | ||
* The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree. | ||
* @param {NODE} u - The parameter "u" represents a node in a binary tree. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can | ||
* either be a `NODE` object or `undefined`. | ||
* @param {RedBlackTreeNode<K, V>} u - The parameter "u" represents a node in a binary tree. | ||
* @param {RedBlackTreeNode<K, V> | undefined} v - The parameter `v` is of type `RedBlackTreeNode<K, V> | undefined`, which means it can | ||
* either be a `RedBlackTreeNode<K, V>` object or `undefined`. | ||
*/ | ||
protected _transplant(u: NODE, v: NODE | undefined): void; | ||
protected _transplant(u: RedBlackTreeNode<K, V>, v: RedBlackTreeNode<K, V> | undefined): void; | ||
/** | ||
@@ -222,6 +261,6 @@ * Time Complexity: O(log n) | ||
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node. | ||
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data | ||
* @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data | ||
* structure. It can either be a valid node or `undefined`. | ||
*/ | ||
protected _insertFixup(z: NODE | undefined): void; | ||
protected _insertFixup(z: RedBlackTreeNode<K, V> | undefined): void; | ||
/** | ||
@@ -233,3 +272,3 @@ * Time Complexity: O(log n) | ||
* the colors and performing rotations. | ||
* @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can | ||
* @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can | ||
* be either a valid node object or `undefined`. | ||
@@ -239,3 +278,3 @@ * @returns The function does not return any value. It has a return type of `void`, which means it | ||
*/ | ||
protected _deleteFixup(node: NODE | undefined): void; | ||
protected _deleteFixup(node: RedBlackTreeNode<K, V> | undefined): void; | ||
/** | ||
@@ -246,7 +285,7 @@ * Time Complexity: O(1) | ||
* The `_leftRotate` function performs a left rotation on a given node in a binary tree. | ||
* @param {NODE | undefined} x - The parameter `x` is of type `NODE | undefined`. It represents a | ||
* @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a | ||
* node in a binary tree or `undefined` if there is no node. | ||
* @returns void, which means it does not return any value. | ||
*/ | ||
protected _leftRotate(x: NODE | undefined): void; | ||
protected _leftRotate(x: RedBlackTreeNode<K, V> | undefined): void; | ||
/** | ||
@@ -257,28 +296,7 @@ * Time Complexity: O(1) | ||
* The `_rightRotate` function performs a right rotation on a given node in a binary tree. | ||
* @param {NODE | undefined} y - The parameter `y` is of type `NODE | undefined`. It represents a | ||
* @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a | ||
* node in a binary tree or `undefined` if there is no node. | ||
* @returns void, which means it does not return any value. | ||
*/ | ||
protected _rightRotate(y: NODE | undefined): void; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by | ||
* applying a callback to each entry in the original tree. | ||
* @param callback - A function that will be called for each entry in the tree, with parameters | ||
* representing the key, value, index, and the tree itself. It should return an entry for the new | ||
* tree. | ||
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV, | ||
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black | ||
* Tree that will be created during the mapping process. These options could include things like | ||
* custom comparators | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function. This can be useful when you want to access properties | ||
* or | ||
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the | ||
* provided callback function. | ||
*/ | ||
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: RedBlackTreeOptions<MK, MV, MR>, thisArg?: any): RedBlackTree<MK, MV, MR>; | ||
protected _rightRotate(y: RedBlackTreeNode<K, V> | undefined): void; | ||
} |
@@ -7,16 +7,36 @@ "use strict"; | ||
/** | ||
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a | ||
* color. | ||
* @param {K} key - The key parameter is of type K and represents the key of the node in the | ||
* Red-Black 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 instance of the Red-Black Tree Node. | ||
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black | ||
* Tree Node. It is an optional parameter with a default value of `'BLACK'`. | ||
* The constructor initializes a node with a key, value, and color for a Red-Black Tree. | ||
* @param {K} key - The `key` parameter is a key of type `K` that is used to identify the node in a | ||
* Red-Black Tree data structure. | ||
* @param {V} [value] - The `value` parameter in the constructor is an optional parameter of type | ||
* `V`. It represents the value associated with the key in the data structure being constructed. | ||
* @param {RBTNColor} [color=BLACK] - The `color` parameter in the constructor is used to specify the | ||
* color of the node in a Red-Black Tree. It has a default value of 'BLACK' if not provided | ||
* explicitly. | ||
*/ | ||
constructor(key, value, color = 'BLACK') { | ||
super(key, value); | ||
this.parent = undefined; | ||
this._left = undefined; | ||
this._right = undefined; | ||
this._color = color; | ||
} | ||
get left() { | ||
return this._left; | ||
} | ||
set left(v) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._left = v; | ||
} | ||
get right() { | ||
return this._right; | ||
} | ||
set right(v) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._right = v; | ||
} | ||
} | ||
@@ -79,10 +99,11 @@ exports.RedBlackTreeNode = RedBlackTreeNode; | ||
/** | ||
* This is the constructor function for a Red-Black Tree data structure in TypeScript. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to | ||
* initialize the RBTree with the provided elements. | ||
* @param [options] - The `options` parameter is an optional object that can be passed to the | ||
* constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for | ||
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would | ||
* depend on the implementation | ||
* This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or | ||
* raw data. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It | ||
* is used to initialize the Red-Black Tree with keys, nodes, entries, or | ||
* @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K, | ||
* V, R>`. It is an optional parameter that allows you to specify additional options for the | ||
* RedBlackTree class. These options could include configuration settings, behavior customization, or | ||
* any other parameters that are specific to | ||
*/ | ||
@@ -96,6 +117,2 @@ constructor(keysNodesEntriesOrRaws = [], options) { | ||
} | ||
/** | ||
* The function returns the root node of a tree or undefined if there is no root. | ||
* @returns The root node of the tree structure, or undefined if there is no root node. | ||
*/ | ||
get root() { | ||
@@ -105,2 +122,5 @@ return this._root; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new Red-Black Tree node with the specified key, value, and color. | ||
@@ -123,2 +143,5 @@ * @param {K} key - The key parameter represents the key value of the node being created. It is of | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new Red-Black Tree with the specified options. | ||
@@ -137,9 +160,9 @@ * @param [options] - The `options` parameter is an optional object that contains additional | ||
* The function checks if the input is an instance of the RedBlackTreeNode 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 | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `RedBlackTreeNode` class. | ||
*/ | ||
isNode(keyNodeEntryOrRaw) { | ||
return keyNodeEntryOrRaw instanceof RedBlackTreeNode; | ||
isNode(keyNodeOrEntry) { | ||
return keyNodeOrEntry instanceof RedBlackTreeNode; | ||
} | ||
@@ -159,8 +182,8 @@ /** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function adds a new node to a binary search tree and returns true if the node was successfully | ||
* added. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -173,4 +196,4 @@ * the key in the data structure. It represents the value that you want to add or update in the data | ||
*/ | ||
add(keyNodeEntryOrRaw, value) { | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
add(keyNodeOrEntry, value) { | ||
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value); | ||
if (!this.isRealNode(newNode)) | ||
@@ -201,23 +224,23 @@ return false; | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the delete method in a binary tree data structure to remove a node based on | ||
* a given predicate and maintain the binary search tree properties. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw` | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override 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, an entry, or a predicate | ||
* function that determines which node(s) should be deleted. | ||
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<NODE>` | ||
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>` | ||
* objects. Each object in the array contains information about the deleted node and whether | ||
* balancing is needed. | ||
*/ | ||
delete(keyNodeEntryOrRaw) { | ||
if (keyNodeEntryOrRaw === null) | ||
delete(keyNodeOrEntry) { | ||
if (keyNodeOrEntry === null) | ||
return []; | ||
const results = []; | ||
let nodeToDelete; | ||
if (this._isPredicate(keyNodeEntryOrRaw)) | ||
nodeToDelete = this.getNode(keyNodeEntryOrRaw); | ||
if (this._isPredicate(keyNodeOrEntry)) | ||
nodeToDelete = this.getNode(keyNodeOrEntry); | ||
else | ||
nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw); | ||
nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry); | ||
if (!nodeToDelete) { | ||
@@ -277,2 +300,43 @@ return results; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by | ||
* applying a callback to each entry in the original tree. | ||
* @param callback - A function that will be called for each entry in the tree, with parameters | ||
* representing the key, value, index, and the tree itself. It should return an entry for the new | ||
* tree. | ||
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV, | ||
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black | ||
* Tree that will be created during the mapping process. These options could include things like | ||
* custom comparators | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function. This can be useful when you want to access properties | ||
* or | ||
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the | ||
* provided callback function. | ||
*/ | ||
map(callback, options, thisArg) { | ||
const newTree = new RedBlackTree([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns The `cloned` object is being returned. | ||
*/ | ||
clone() { | ||
const cloned = this.createTree(); | ||
this._clone(cloned); | ||
return cloned; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -283,3 +347,3 @@ * Space Complexity: O(1) | ||
* root. | ||
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined. | ||
* @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined. | ||
*/ | ||
@@ -297,5 +361,5 @@ _setRoot(v) { | ||
* The function replaces an old node with a new node while preserving the color of the old node. | ||
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* the data structure. | ||
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a | ||
* @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a | ||
* data structure. | ||
@@ -311,7 +375,7 @@ * @returns The method is returning the result of calling the `_replaceNode` method from the | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to | ||
* maintain the red-black tree properties. | ||
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the | ||
* @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the | ||
* binary search tree. | ||
@@ -361,5 +425,5 @@ * @returns a string value indicating the result of the insertion operation. It can return either | ||
* The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree. | ||
* @param {NODE} u - The parameter "u" represents a node in a binary tree. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can | ||
* either be a `NODE` object or `undefined`. | ||
* @param {RedBlackTreeNode<K, V>} u - The parameter "u" represents a node in a binary tree. | ||
* @param {RedBlackTreeNode<K, V> | undefined} v - The parameter `v` is of type `RedBlackTreeNode<K, V> | undefined`, which means it can | ||
* either be a `RedBlackTreeNode<K, V>` object or `undefined`. | ||
*/ | ||
@@ -385,3 +449,3 @@ _transplant(u, v) { | ||
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node. | ||
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data | ||
* @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data | ||
* structure. It can either be a valid node or `undefined`. | ||
@@ -454,3 +518,3 @@ */ | ||
* the colors and performing rotations. | ||
* @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can | ||
* @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can | ||
* be either a valid node object or `undefined`. | ||
@@ -541,3 +605,3 @@ * @returns The function does not return any value. It has a return type of `void`, which means it | ||
* The `_leftRotate` function performs a left rotation on a given node in a binary tree. | ||
* @param {NODE | undefined} x - The parameter `x` is of type `NODE | undefined`. It represents a | ||
* @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a | ||
* node in a binary tree or `undefined` if there is no node. | ||
@@ -573,3 +637,3 @@ * @returns void, which means it does not return any value. | ||
* The `_rightRotate` function performs a right rotation on a given node in a binary tree. | ||
* @param {NODE | undefined} y - The parameter `y` is of type `NODE | undefined`. It represents a | ||
* @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a | ||
* node in a binary tree or `undefined` if there is no node. | ||
@@ -600,31 +664,3 @@ * @returns void, which means it does not return any value. | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by | ||
* applying a callback to each entry in the original tree. | ||
* @param callback - A function that will be called for each entry in the tree, with parameters | ||
* representing the key, value, index, and the tree itself. It should return an entry for the new | ||
* tree. | ||
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV, | ||
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black | ||
* Tree that will be created during the mapping process. These options could include things like | ||
* custom comparators | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function. This can be useful when you want to access properties | ||
* or | ||
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the | ||
* provided callback function. | ||
*/ | ||
map(callback, options, thisArg) { | ||
const newTree = new RedBlackTree([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
} | ||
exports.RedBlackTree = RedBlackTree; |
@@ -8,196 +8,100 @@ /** | ||
*/ | ||
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types'; | ||
import type { BTNRep, OptNodeOrNull, TreeMultiMapOptions } from '../../types'; | ||
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree'; | ||
export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> { | ||
export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> { | ||
/** | ||
* 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'`. | ||
* This TypeScript constructor initializes an object with a key of type K and an array of values of | ||
* type V. | ||
* @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the | ||
* data being stored in the data structure. It helps in quickly accessing or retrieving the | ||
* associated value in the data structure. | ||
* @param {V[]} value - The `value` parameter in the constructor represents an array of values of | ||
* type `V`. | ||
*/ | ||
constructor(key: K, value?: V, count?: number, color?: RBTNColor); | ||
constructor(key: K, value: V[]); | ||
parent?: TreeMultiMapNode<K, V>; | ||
_left?: OptNodeOrNull<TreeMultiMapNode<K, V>>; | ||
get left(): OptNodeOrNull<TreeMultiMapNode<K, V>>; | ||
set left(v: OptNodeOrNull<TreeMultiMapNode<K, V>>); | ||
_right?: OptNodeOrNull<TreeMultiMapNode<K, V>>; | ||
get right(): OptNodeOrNull<TreeMultiMapNode<K, V>>; | ||
set right(v: OptNodeOrNull<TreeMultiMapNode<K, V>>); | ||
} | ||
export declare class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = TreeMultiMap<K, V, R, MK, MV, MR, NODE, TreeMultiMapNested<K, V, R, MK, MV, MR, NODE>>> extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> { | ||
/** | ||
* | ||
* @example | ||
* // Find elements in a range | ||
* const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(tmm.search(new Range(5, 10))); // [5, 10, 7] | ||
* console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7] | ||
* console.log(tmm.search(new Range(15, 20))); // [15, 18] | ||
*/ | ||
export declare class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends RedBlackTree<K, V[], R, MK, MV[], MR> implements IBinaryTree<K, V[], R, MK, MV, MR> { | ||
/** | ||
* 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. | ||
* The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data | ||
* and options. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either key-value pairs represented as `BTNRep<K, V[], | ||
* TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize | ||
* the RedBlackTreeMulti | ||
* @param [options] - The `options` parameter in the constructor is of type | ||
* `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify | ||
* additional options for configuring the TreeMultiMap instance. | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, NODE>>, options?: TreeMultiMapOptions<K, V, R>); | ||
protected _count: number; | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V[], TreeMultiMapNode<K, V>> | R>, options?: TreeMultiMapOptions<K, V[], R>); | ||
/** | ||
* 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) | ||
* Time Complexity: O(1) | ||
* 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. | ||
* The `createTree` function in TypeScript overrides the default implementation to create a new | ||
* TreeMultiMap with specified options. | ||
* @param [options] - The `options` parameter in the `createTree` method is of type | ||
* `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration | ||
* options when creating a new `TreeMultiMap` instance. It includes properties such as | ||
* `iterationType`, `specifyComparable | ||
* @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial | ||
* data and the provided options merged with the existing properties of the current object. | ||
*/ | ||
getComputedCount(): number; | ||
createTree(options?: TreeMultiMapOptions<K, V[], R>): TreeMultiMap<K, V, R, MK, MV, MR>; | ||
/** | ||
* 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. | ||
*/ | ||
protected _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) | ||
* Time Complexity: O(1) | ||
* 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. | ||
* The function `createNode` overrides the method to create a new `TreeMultiMapNode` with a specified | ||
* key and an empty array of values. | ||
* @param {K} key - The `key` parameter in the `createNode` method represents the key of the node | ||
* that will be created in the TreeMultiMap data structure. | ||
* @returns A new instance of `TreeMultiMapNode<K, V>` is being returned, with the specified key and | ||
* an empty array as its value. | ||
*/ | ||
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): boolean; | ||
createNode(key: K): TreeMultiMapNode<K, V>; | ||
add(node: BTNRep<K, V[], TreeMultiMapNode<K, V>>): boolean; | ||
add(key: K, value: V): 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. | ||
* The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure | ||
* and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an | ||
* array of values, or just a key itself. | ||
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific | ||
* value that you want to remove from the multi-map data structure associated with a particular key. | ||
* The function checks if the value exists in the array of values associated with the key, and if | ||
* found, removes it from the array. | ||
* @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was | ||
* successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise. | ||
*/ | ||
perfectlyBalance(iterationType?: IterationType): boolean; | ||
deleteValue(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value: V): boolean; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* 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 | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* 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`. | ||
* @returns The `cloned` object is being returned. | ||
*/ | ||
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE; | ||
/** | ||
* The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with | ||
* modified entries based on a provided callback. | ||
* @param callback - The `callback` parameter is a function that will be called for each entry in the | ||
* map. It takes four arguments: | ||
* @param [options] - The `options` parameter in the `override map` function is of type | ||
* `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration | ||
* options when creating a new `TreeMultiMap` instance within the `map` function. These options could | ||
* include things like | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function when it is called within the `map` function. This | ||
* @returns A new TreeMultiMap instance is being returned, which is populated with entries generated | ||
* by the provided callback function. | ||
*/ | ||
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: TreeMultiMapOptions<MK, MV, MR>, thisArg?: any): TreeMultiMap<MK, MV, MR>; | ||
clone(): TreeMultiMap<K, V, R, MK, MV, MR>; | ||
} |
@@ -7,424 +7,189 @@ "use strict"; | ||
/** | ||
* 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'`. | ||
* This TypeScript constructor initializes an object with a key of type K and an array of values of | ||
* type V. | ||
* @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the | ||
* data being stored in the data structure. It helps in quickly accessing or retrieving the | ||
* associated value in the data structure. | ||
* @param {V[]} value - The `value` parameter in the constructor represents an array of values of | ||
* type `V`. | ||
*/ | ||
constructor(key, value, count = 1, color = 'BLACK') { | ||
super(key, value, color); | ||
this.count = count; | ||
constructor(key, value) { | ||
super(key, value); | ||
this.parent = undefined; | ||
this._left = undefined; | ||
this._right = undefined; | ||
} | ||
get left() { | ||
return this._left; | ||
} | ||
set left(v) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._left = v; | ||
} | ||
get right() { | ||
return this._right; | ||
} | ||
set right(v) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._right = v; | ||
} | ||
} | ||
exports.TreeMultiMapNode = TreeMultiMapNode; | ||
/** | ||
* | ||
* @example | ||
* // Find elements in a range | ||
* const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(tmm.search(new Range(5, 10))); // [5, 10, 7] | ||
* console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7] | ||
* console.log(tmm.search(new Range(15, 20))); // [15, 18] | ||
*/ | ||
class TreeMultiMap extends red_black_tree_1.RedBlackTree { | ||
/** | ||
* 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. | ||
* The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data | ||
* and options. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either key-value pairs represented as `BTNRep<K, V[], | ||
* TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize | ||
* the RedBlackTreeMulti | ||
* @param [options] - The `options` parameter in the constructor is of type | ||
* `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify | ||
* additional options for configuring the TreeMultiMap instance. | ||
*/ | ||
constructor(keysNodesEntriesOrRaws = [], options) { | ||
super([], options); | ||
this._count = 0; | ||
if (keysNodesEntriesOrRaws) | ||
super([], Object.assign(Object.assign({}, options), { isMapMode: true })); | ||
if (keysNodesEntriesOrRaws) { | ||
this.addMany(keysNodesEntriesOrRaws); | ||
} | ||
} | ||
// TODO the _count is not accurate after nodes count modified | ||
/** | ||
* 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() { | ||
return this._count; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* 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. | ||
* The `createTree` function in TypeScript overrides the default implementation to create a new | ||
* TreeMultiMap with specified options. | ||
* @param [options] - The `options` parameter in the `createTree` method is of type | ||
* `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration | ||
* options when creating a new `TreeMultiMap` instance. It includes properties such as | ||
* `iterationType`, `specifyComparable | ||
* @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial | ||
* data and the provided options merged with the existing properties of the current object. | ||
*/ | ||
getComputedCount() { | ||
let sum = 0; | ||
this.dfs(node => (sum += node.count)); | ||
return sum; | ||
} | ||
/** | ||
* 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, value, color = 'BLACK', count) { | ||
return new TreeMultiMapNode(key, this._isMapMode ? undefined : value, count, color); | ||
} | ||
/** | ||
* 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) { | ||
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options)); | ||
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options)); | ||
} | ||
/** | ||
* 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, value, count = 1) { | ||
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) | ||
return [undefined, undefined]; | ||
if (this.isNode(keyNodeEntryOrRaw)) | ||
return [keyNodeEntryOrRaw, value]; | ||
if (this.isEntry(keyNodeEntryOrRaw)) { | ||
const [key, entryValue] = keyNodeEntryOrRaw; | ||
if (key === undefined || key === null) | ||
return [undefined, undefined]; | ||
const finalValue = value !== null && value !== void 0 ? value : entryValue; | ||
if (this.isKey(key)) | ||
return [this.createNode(key, finalValue, 'BLACK', count), finalValue]; | ||
} | ||
if (this.isRaw(keyNodeEntryOrRaw)) { | ||
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw); | ||
const finalValue = value !== null && value !== void 0 ? value : entryValue; | ||
if (this.isKey(key)) | ||
return [this.createNode(key, finalValue, 'BLACK', count), finalValue]; | ||
} | ||
if (this.isKey(keyNodeEntryOrRaw)) | ||
return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value]; | ||
return [undefined, 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) { | ||
return keyNodeEntryOrRaw instanceof TreeMultiMapNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Time Complexity: O(1) | ||
* 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. | ||
* The function `createNode` overrides the method to create a new `TreeMultiMapNode` with a specified | ||
* key and an empty array of values. | ||
* @param {K} key - The `key` parameter in the `createNode` method represents the key of the node | ||
* that will be created in the TreeMultiMap data structure. | ||
* @returns A new instance of `TreeMultiMapNode<K, V>` is being returned, with the specified key and | ||
* an empty array as its value. | ||
*/ | ||
add(keyNodeEntryOrRaw, value, count = 1) { | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
const orgCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0; | ||
const isSuccessAdded = super.add(newNode, newValue); | ||
if (isSuccessAdded) { | ||
this._count += orgCount; | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
createNode(key) { | ||
return new TreeMultiMapNode(key, []); | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* 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. | ||
* The function `add` in TypeScript overrides the superclass method to add key-value pairs to a | ||
* TreeMultiMapNode, handling different input types and scenarios. | ||
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override add` method can be either a `BTNRep` object containing a key, an array | ||
* of values, and a `TreeMultiMapNode`, or just a key. | ||
* @param {V} [value] - The `value` parameter in the `override add` method represents the value that | ||
* you want to add to the TreeMultiMap. If the key is already present in the map, the new value will | ||
* be added to the existing list of values associated with that key. If the key is not present, | ||
* @returns The `add` method is returning a boolean value, which indicates whether the operation was | ||
* successful or not. | ||
*/ | ||
delete(keyNodeEntryOrRaw, ignoreCount = false) { | ||
if (keyNodeEntryOrRaw === null) | ||
return []; | ||
const results = []; | ||
let nodeToDelete; | ||
if (this._isPredicate(keyNodeEntryOrRaw)) | ||
nodeToDelete = this.getNode(keyNodeEntryOrRaw); | ||
else | ||
nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw); | ||
if (!nodeToDelete) { | ||
return results; | ||
} | ||
let originalColor = nodeToDelete.color; | ||
let replacementNode; | ||
if (!this.isRealNode(nodeToDelete.left)) { | ||
if (nodeToDelete.right !== null) | ||
replacementNode = nodeToDelete.right; | ||
if (ignoreCount || nodeToDelete.count <= 1) { | ||
if (nodeToDelete.right !== null) { | ||
this._transplant(nodeToDelete, nodeToDelete.right); | ||
this._count -= nodeToDelete.count; | ||
} | ||
add(keyNodeOrEntry, value) { | ||
if (this.isRealNode(keyNodeOrEntry)) | ||
return super.add(keyNodeOrEntry); | ||
const _commonAdd = (key, values) => { | ||
if (key === undefined || key === null) | ||
return false; | ||
const existingValues = this.get(key); | ||
if (existingValues !== undefined && values !== undefined) { | ||
for (const value of values) | ||
existingValues.push(value); | ||
return true; | ||
} | ||
else { | ||
nodeToDelete.count--; | ||
this._count--; | ||
results.push({ deleted: nodeToDelete, needBalanced: undefined }); | ||
return results; | ||
} | ||
} | ||
else if (!this.isRealNode(nodeToDelete.right)) { | ||
replacementNode = nodeToDelete.left; | ||
if (ignoreCount || nodeToDelete.count <= 1) { | ||
this._transplant(nodeToDelete, nodeToDelete.left); | ||
this._count -= nodeToDelete.count; | ||
} | ||
else { | ||
nodeToDelete.count--; | ||
this._count--; | ||
results.push({ deleted: nodeToDelete, needBalanced: undefined }); | ||
return results; | ||
} | ||
} | ||
else { | ||
const successor = this.getLeftMost(node => node, nodeToDelete.right); | ||
if (successor) { | ||
originalColor = successor.color; | ||
if (successor.right !== null) | ||
replacementNode = successor.right; | ||
if (successor.parent === nodeToDelete) { | ||
if (this.isRealNode(replacementNode)) { | ||
replacementNode.parent = successor; | ||
} | ||
const existingNode = this.getNode(key); | ||
if (this.isRealNode(existingNode)) { | ||
if (existingValues === undefined) { | ||
super.add(key, values); | ||
return true; | ||
} | ||
else { | ||
if (ignoreCount || nodeToDelete.count <= 1) { | ||
if (successor.right !== null) { | ||
this._transplant(successor, successor.right); | ||
this._count -= nodeToDelete.count; | ||
} | ||
} | ||
else { | ||
nodeToDelete.count--; | ||
this._count--; | ||
results.push({ deleted: nodeToDelete, needBalanced: undefined }); | ||
return results; | ||
} | ||
successor.right = nodeToDelete.right; | ||
if (this.isRealNode(successor.right)) { | ||
successor.right.parent = successor; | ||
} | ||
if (values !== undefined) { | ||
for (const value of values) | ||
existingValues.push(value); | ||
return true; | ||
} | ||
if (ignoreCount || nodeToDelete.count <= 1) { | ||
this._transplant(nodeToDelete, successor); | ||
this._count -= nodeToDelete.count; | ||
} | ||
else { | ||
nodeToDelete.count--; | ||
this._count--; | ||
results.push({ deleted: nodeToDelete, needBalanced: undefined }); | ||
return results; | ||
return false; | ||
} | ||
successor.left = nodeToDelete.left; | ||
if (this.isRealNode(successor.left)) { | ||
successor.left.parent = successor; | ||
} | ||
successor.color = nodeToDelete.color; | ||
} | ||
else { | ||
return super.add(key, values); | ||
} | ||
}; | ||
if (this.isEntry(keyNodeOrEntry)) { | ||
const [key, values] = keyNodeOrEntry; | ||
return _commonAdd(key, value !== undefined ? [value] : values); | ||
} | ||
this._size--; | ||
// If the original color was black, fix the tree | ||
if (originalColor === 'BLACK') { | ||
this._deleteFixup(replacementNode); | ||
} | ||
results.push({ deleted: nodeToDelete, needBalanced: undefined }); | ||
return results; | ||
return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined); | ||
} | ||
/** | ||
* 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() { | ||
super.clear(); | ||
this._count = 0; | ||
} | ||
/** | ||
* Time Complexity: O(n log n) | ||
* Time Complexity: O(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. | ||
* The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure | ||
* and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an | ||
* array of values, or just a key itself. | ||
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific | ||
* value that you want to remove from the multi-map data structure associated with a particular key. | ||
* The function checks if the value exists in the array of values associated with the key, and if | ||
* found, removes it from the array. | ||
* @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was | ||
* successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise. | ||
*/ | ||
perfectlyBalance(iterationType = this.iterationType) { | ||
const sorted = this.dfs(node => node, 'IN'), n = sorted.length; | ||
if (sorted.length < 1) | ||
return false; | ||
this.clear(); | ||
if (iterationType === 'RECURSIVE') { | ||
const buildBalanceBST = (l, r) => { | ||
if (l > r) | ||
return; | ||
const m = l + Math.floor((r - l) / 2); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) | ||
this.add(midNode.key, undefined, midNode.count); | ||
else | ||
this.add(midNode.key, midNode.value, midNode.count); | ||
buildBalanceBST(l, m - 1); | ||
buildBalanceBST(m + 1, r); | ||
}; | ||
buildBalanceBST(0, n - 1); | ||
deleteValue(keyNodeOrEntry, value) { | ||
const values = this.get(keyNodeOrEntry); | ||
if (Array.isArray(values)) { | ||
const index = values.indexOf(value); | ||
if (index === -1) | ||
return false; | ||
values.splice(index, 1); | ||
// If no values left, remove the entire node | ||
if (values.length === 0) | ||
this.delete(keyNodeOrEntry); | ||
return true; | ||
} | ||
else { | ||
const stack = [[0, n - 1]]; | ||
while (stack.length > 0) { | ||
const popped = stack.pop(); | ||
if (popped) { | ||
const [l, r] = popped; | ||
if (l <= r) { | ||
const m = l + Math.floor((r - l) / 2); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) | ||
this.add(midNode.key, undefined, midNode.count); | ||
else | ||
this.add(midNode.key, midNode.value, midNode.count); | ||
stack.push([m + 1, r]); | ||
stack.push([l, m - 1]); | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* 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. | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns The `cloned` object is being returned. | ||
*/ | ||
clone() { | ||
const cloned = this.createTree(); | ||
this.bfs(node => cloned.add(node.key, undefined, node.count)); | ||
if (this._isMapMode) | ||
cloned._store = this._store; | ||
this._clone(cloned); | ||
return cloned; | ||
} | ||
/** | ||
* 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. | ||
*/ | ||
_swapProperties(srcNode, destNode) { | ||
srcNode = this.ensureNode(srcNode); | ||
destNode = this.ensureNode(destNode); | ||
if (srcNode && destNode) { | ||
const { key, value, count, color } = destNode; | ||
const tempNode = this.createNode(key, value, color, count); | ||
if (tempNode) { | ||
tempNode.color = color; | ||
destNode.key = srcNode.key; | ||
if (!this._isMapMode) | ||
destNode.value = srcNode.value; | ||
destNode.count = srcNode.count; | ||
destNode.color = srcNode.color; | ||
srcNode.key = tempNode.key; | ||
if (!this._isMapMode) | ||
srcNode.value = tempNode.value; | ||
srcNode.count = tempNode.count; | ||
srcNode.color = tempNode.color; | ||
} | ||
return destNode; | ||
} | ||
return 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`. | ||
*/ | ||
_replaceNode(oldNode, newNode) { | ||
newNode.count = oldNode.count + newNode.count; | ||
return super._replaceNode(oldNode, newNode); | ||
} | ||
/** | ||
* The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with | ||
* modified entries based on a provided callback. | ||
* @param callback - The `callback` parameter is a function that will be called for each entry in the | ||
* map. It takes four arguments: | ||
* @param [options] - The `options` parameter in the `override map` function is of type | ||
* `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration | ||
* options when creating a new `TreeMultiMap` instance within the `map` function. These options could | ||
* include things like | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function when it is called within the `map` function. This | ||
* @returns A new TreeMultiMap instance is being returned, which is populated with entries generated | ||
* by the provided callback function. | ||
*/ | ||
map(callback, options, thisArg) { | ||
const newTree = new TreeMultiMap([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
} | ||
exports.TreeMultiMap = TreeMultiMap; |
@@ -37,2 +37,5 @@ /** | ||
} | ||
/** | ||
* | ||
*/ | ||
export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V> = DirectedVertex<V>, EO extends DirectedEdge<E> = DirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> { | ||
@@ -39,0 +42,0 @@ /** |
@@ -38,2 +38,5 @@ "use strict"; | ||
exports.DirectedEdge = DirectedEdge; | ||
/** | ||
* | ||
*/ | ||
class DirectedGraph extends abstract_graph_1.AbstractGraph { | ||
@@ -40,0 +43,0 @@ /** |
@@ -33,2 +33,5 @@ import type { MapGraphCoordinate, VertexKey } from '../../types'; | ||
} | ||
/** | ||
* | ||
*/ | ||
export declare class MapGraph<V = any, E = any, VO extends MapVertex<V> = MapVertex<V>, EO extends MapEdge<E> = MapEdge<E>> extends DirectedGraph<V, E, VO, EO> { | ||
@@ -35,0 +38,0 @@ /** |
@@ -41,2 +41,5 @@ "use strict"; | ||
exports.MapEdge = MapEdge; | ||
/** | ||
* | ||
*/ | ||
class MapGraph extends directed_graph_1.DirectedGraph { | ||
@@ -43,0 +46,0 @@ /** |
@@ -35,2 +35,5 @@ /** | ||
} | ||
/** | ||
* | ||
*/ | ||
export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVertex<V> = UndirectedVertex<V>, EO extends UndirectedEdge<E> = UndirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> { | ||
@@ -37,0 +40,0 @@ /** |
@@ -36,2 +36,5 @@ "use strict"; | ||
exports.UndirectedEdge = UndirectedEdge; | ||
/** | ||
* | ||
*/ | ||
class UndirectedGraph extends abstract_graph_1.AbstractGraph { | ||
@@ -38,0 +41,0 @@ /** |
@@ -43,2 +43,5 @@ /** | ||
} | ||
/** | ||
* | ||
*/ | ||
export declare class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> { | ||
@@ -45,0 +48,0 @@ constructor(elements?: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>, options?: SinglyLinkedListOptions<E, R>); |
@@ -48,2 +48,5 @@ "use strict"; | ||
exports.SinglyLinkedListNode = SinglyLinkedListNode; | ||
/** | ||
* | ||
*/ | ||
class SinglyLinkedList extends base_1.IterableElementBase { | ||
@@ -50,0 +53,0 @@ constructor(elements = [], options) { |
@@ -15,2 +15,5 @@ /** | ||
} | ||
/** | ||
* | ||
*/ | ||
export declare class SkipList<K, V> { | ||
@@ -17,0 +20,0 @@ /** |
@@ -12,2 +12,5 @@ "use strict"; | ||
exports.SkipListNode = SkipListNode; | ||
/** | ||
* | ||
*/ | ||
class SkipList { | ||
@@ -14,0 +17,0 @@ /** |
@@ -9,2 +9,5 @@ /** | ||
import type { MatrixOptions } from '../../types'; | ||
/** | ||
* | ||
*/ | ||
export declare class Matrix { | ||
@@ -11,0 +14,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Matrix = void 0; | ||
/** | ||
* | ||
*/ | ||
class Matrix { | ||
@@ -5,0 +8,0 @@ /** |
@@ -22,2 +22,5 @@ /** | ||
} | ||
/** | ||
* | ||
*/ | ||
export declare class Navigator<T = number> { | ||
@@ -24,0 +27,0 @@ onMove: (cur: [number, number]) => void; |
@@ -19,2 +19,5 @@ "use strict"; | ||
exports.Character = Character; | ||
/** | ||
* | ||
*/ | ||
class Navigator { | ||
@@ -21,0 +24,0 @@ /** |
@@ -10,2 +10,5 @@ /** | ||
import { PriorityQueue } from './priority-queue'; | ||
/** | ||
* | ||
*/ | ||
export declare class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> { | ||
@@ -12,0 +15,0 @@ /** |
@@ -5,2 +5,5 @@ "use strict"; | ||
const priority_queue_1 = require("./priority-queue"); | ||
/** | ||
* | ||
*/ | ||
class MaxPriorityQueue extends priority_queue_1.PriorityQueue { | ||
@@ -7,0 +10,0 @@ /** |
@@ -10,2 +10,5 @@ /** | ||
import { PriorityQueue } from './priority-queue'; | ||
/** | ||
* | ||
*/ | ||
export declare class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> { | ||
@@ -12,0 +15,0 @@ /** |
@@ -5,2 +5,5 @@ "use strict"; | ||
const priority_queue_1 = require("./priority-queue"); | ||
/** | ||
* | ||
*/ | ||
class MinPriorityQueue extends priority_queue_1.PriorityQueue { | ||
@@ -7,0 +10,0 @@ /** |
@@ -10,6 +10,2 @@ /** | ||
import { IterableElementBase } from '../base'; | ||
/** | ||
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes, | ||
* and a flag indicating whether it's the end of a word. | ||
*/ | ||
export declare class TrieNode { | ||
@@ -16,0 +12,0 @@ constructor(key: string); |
@@ -5,6 +5,2 @@ "use strict"; | ||
const base_1 = require("../base"); | ||
/** | ||
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes, | ||
* and a flag indicating whether it's the end of a word. | ||
*/ | ||
class TrieNode { | ||
@@ -11,0 +7,0 @@ constructor(key) { |
@@ -1,9 +0,9 @@ | ||
import { BinaryTree, BinaryTreeNode } from '../data-structures'; | ||
import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNRep, NodePredicate } from '../types'; | ||
export interface IBinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTreeNested<K, V, R, MK, MV, MR, NODE>> { | ||
createNode(key: K, value?: NODE['value']): NODE; | ||
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE; | ||
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, NODE>, value?: V, count?: number): boolean; | ||
addMany(nodes: Iterable<BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[]; | ||
delete(predicate: R | BTNRep<K, V, NODE> | NodePredicate<NODE>): BinaryTreeDeleteResult<NODE>[]; | ||
import { BinaryTreeNode } from '../data-structures'; | ||
import type { BinaryTreeDeleteResult, BinaryTreeOptions, BTNRep, NodePredicate } from '../types'; | ||
export interface IBinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> { | ||
createNode(key: K, value?: BinaryTreeNode['value']): BinaryTreeNode; | ||
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R, MK, MV, MR>; | ||
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean; | ||
addMany(nodes: Iterable<BTNRep<K, V, BinaryTreeNode<K, V>>>, values?: Iterable<V | undefined>): boolean[]; | ||
delete(predicate: R | BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[]; | ||
} |
@@ -1,5 +0,2 @@ | ||
import { AVLTreeMultiMap, AVLTreeMultiMapNode } from '../../../data-structures'; | ||
import type { AVLTreeOptions } from './avl-tree'; | ||
export type AVLTreeMultiMapNodeNested<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>>; | ||
export type AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE extends AVLTreeMultiMapNode<K, V, NODE>> = AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, any>>>; | ||
export type AVLTreeMultiMapOptions<K, V, R> = AVLTreeOptions<K, V, R> & {}; | ||
export type AVLTreeMultiMapOptions<K, V, R> = Omit<AVLTreeOptions<K, V, R>, 'isMapMode'> & {}; |
@@ -1,5 +0,2 @@ | ||
import { AVLTree, AVLTreeNode } from '../../../data-structures'; | ||
import { BSTOptions } from './bst'; | ||
export type AVLTreeNodeNested<K, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, any>>>; | ||
export type AVLTreeNested<K, V, R, MK, MV, MR, NODE extends AVLTreeNode<K, V, NODE>> = AVLTree<K, V, R, MK, MV, MR, NODE, AVLTree<K, V, R, MK, MV, MR, NODE, AVLTree<K, V, R, MK, MV, MR, NODE, any>>>; | ||
export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R> & {}; |
@@ -1,6 +0,3 @@ | ||
import { BinaryTree, BinaryTreeNode } from '../../../data-structures'; | ||
import { IterationType, OptValue } from '../../common'; | ||
import { DFSOperation } from '../../../common'; | ||
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>; | ||
export type BinaryTreeNested<K, V, R, MK, MV, MR, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, MK, MV, MR, NODE, BinaryTree<K, V, R, MK, MV, MR, NODE, BinaryTree<K, V, R, MK, MV, MR, NODE, any>>>; | ||
export type ToEntryFn<K, V, R> = (rawElement: R) => BTNEntry<K, V>; | ||
@@ -7,0 +4,0 @@ export type BinaryTreeOptions<K, V, R> = { |
@@ -1,6 +0,4 @@ | ||
import { BST, BSTNode } from '../../../data-structures'; | ||
import type { BinaryTreeOptions } from './binary-tree'; | ||
import { Comparable } from '../../utils'; | ||
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>; | ||
export type BSTNested<K, V, R, MK, MV, MR, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, MK, MV, MR, NODE, BST<K, V, R, MK, MV, MR, NODE, BST<K, V, R, MK, MV, MR, NODE, any>>>; | ||
import { OptValue } from '../../common'; | ||
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & { | ||
@@ -12,2 +10,4 @@ specifyComparable?: (key: K) => Comparable; | ||
export type OptNode<NODE> = NODE | undefined; | ||
export type BSTNEntry<K, V> = [BSTNOptKey<K>, OptValue<V>]; | ||
export type BSTNOptKeyOrNode<K, NODE> = BSTNOptKey<K> | NODE; | ||
export type BSTNRep<K, V, NODE> = BSTNEntry<K, V> | BSTNOptKeyOrNode<K, NODE>; |
@@ -8,1 +8,3 @@ export * from './binary-tree'; | ||
export * from './tree-multi-map'; | ||
export * from './tree-counter'; | ||
export * from './avl-tree-counter'; |
@@ -24,1 +24,3 @@ "use strict"; | ||
__exportStar(require("./tree-multi-map"), exports); | ||
__exportStar(require("./tree-counter"), exports); | ||
__exportStar(require("./avl-tree-counter"), exports); |
@@ -1,6 +0,3 @@ | ||
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures'; | ||
import type { BSTOptions } from "./bst"; | ||
import type { BSTOptions } from './bst'; | ||
export type RBTNColor = 'RED' | 'BLACK'; | ||
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>; | ||
export type RedBlackTreeNested<K, V, R, MK, MV, MR, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTree<K, V, R, MK, MV, MR, NODE, any>>>; | ||
export type RedBlackTreeOptions<K, V, R> = BSTOptions<K, V, R> & {}; |
@@ -1,5 +0,2 @@ | ||
import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures'; | ||
import type { RedBlackTreeOptions } from './rb-tree'; | ||
export type TreeMultiMapNodeNested<K, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, any>>>; | ||
export type TreeMultiMapNested<K, V, R, MK, MV, MR, NODE extends TreeMultiMapNode<K, V, NODE>> = TreeMultiMap<K, V, R, MK, MV, MR, NODE, TreeMultiMap<K, V, R, MK, MV, MR, NODE, TreeMultiMap<K, V, R, MK, MV, MR, NODE, any>>>; | ||
export type TreeMultiMapOptions<K, V, R> = RedBlackTreeOptions<K, V, R> & {}; | ||
export type TreeMultiMapOptions<K, V, R> = Omit<RedBlackTreeOptions<K, V, R>, 'isMapMode'> & {}; |
{ | ||
"name": "directed-graph-typed", | ||
"version": "1.54.0", | ||
"version": "1.54.1", | ||
"description": "Directed Graph. Javascript & Typescript Data Structure.", | ||
@@ -150,4 +150,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.54.0" | ||
"data-structure-typed": "^1.54.1" | ||
} | ||
} |
@@ -8,128 +8,92 @@ /** | ||
*/ | ||
import type { | ||
AVLTreeMultiMapNested, | ||
AVLTreeMultiMapNodeNested, | ||
AVLTreeMultiMapOptions, | ||
BinaryTreeDeleteResult, | ||
BSTNOptKeyOrNode, | ||
BTNRep, | ||
EntryCallback, | ||
IterationType | ||
} from '../../types'; | ||
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull, BTNRep, OptNodeOrNull } from '../../types'; | ||
import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
export class AVLTreeMultiMapNode< | ||
K = any, | ||
V = any, | ||
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V> | ||
> extends AVLTreeNode<K, V, NODE> { | ||
export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> { | ||
/** | ||
* The constructor function initializes a BinaryTreeNode object with a key, value, and count. | ||
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier | ||
* of the binary tree node. | ||
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary | ||
* tree node. If no value is provided, it will be `undefined`. | ||
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value | ||
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count` | ||
* parameter when creating a new instance of the `BinaryTreeNode` class. | ||
* This TypeScript constructor initializes an object with a key of type K and an array of values of | ||
* type V. | ||
* @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the | ||
* data being stored in the data structure. It helps in quickly accessing or retrieving the | ||
* associated value in the data structure. | ||
* @param {V[]} value - The `value` parameter in the constructor represents an array of values of | ||
* type `V`. | ||
*/ | ||
constructor(key: K, value?: V, count = 1) { | ||
constructor(key: K, value: V[]) { | ||
super(key, value); | ||
this.count = count; | ||
} | ||
override parent?: AVLTreeMultiMapNode<K, V> = undefined; | ||
override _left?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> { | ||
return this._left; | ||
} | ||
override set left(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._left = v; | ||
} | ||
override _right?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined; | ||
override get right(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> { | ||
return this._right; | ||
} | ||
override set right(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._right = v; | ||
} | ||
} | ||
/** | ||
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters. | ||
* | ||
*/ | ||
export class AVLTreeMultiMap< | ||
K = any, | ||
V = any, | ||
R = object, | ||
MK = any, | ||
MV = any, | ||
MR = object, | ||
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>, | ||
TREE extends AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = AVLTreeMultiMap< | ||
K, | ||
V, | ||
R, | ||
MK, | ||
MV, | ||
MR, | ||
NODE, | ||
AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE> | ||
> | ||
> | ||
extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> | ||
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> | ||
export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object> | ||
extends AVLTree<K, V[], R, MK, MV[], MR> | ||
implements IBinaryTree<K, V[], R, MK, MV, MR> | ||
{ | ||
/** | ||
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable object that can contain either keys, nodes, entries, or raw elements. | ||
* @param [options] - The `options` parameter is an optional object that can be used to customize the | ||
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and | ||
* `compareValues` functions to define custom comparison logic for keys and values, respectively. | ||
* The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data | ||
* and options. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either key-value pairs represented as `BTNRep<K, V[], | ||
* AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize | ||
* the AVLTreeMulti | ||
* @param [options] - The `options` parameter in the constructor is of type | ||
* `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify | ||
* additional options for configuring the AVLTreeMultiMap instance. | ||
*/ | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], | ||
options?: AVLTreeMultiMapOptions<K, V, R> | ||
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | R> = [], | ||
options?: AVLTreeMultiMapOptions<K, V[], R> | ||
) { | ||
super([], options); | ||
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws); | ||
super([], { ...options, isMapMode: true }); | ||
if (keysNodesEntriesOrRaws) { | ||
this.addMany(keysNodesEntriesOrRaws); | ||
} | ||
} | ||
protected _count = 0; | ||
/** | ||
* 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. | ||
*/ | ||
get count(): number { | ||
return this._count; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* 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. | ||
* The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with | ||
* specified options. | ||
* @param [options] - The `options` parameter in the `createTree` function is of type | ||
* `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type | ||
* `K`, `V[]`, and `R`. The function creates a new `AVL | ||
* @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the | ||
* provided options. | ||
*/ | ||
getComputedCount(): number { | ||
let sum = 0; | ||
this.dfs(node => (sum += node.count)); | ||
return sum; | ||
} | ||
/** | ||
* The function creates a new AVLTreeMultiMapNode with the specified key, value, 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 that can be replaced with any specific type when using the function. | ||
* @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 {number} [count] - The `count` parameter represents the number of occurrences of a | ||
* key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when | ||
* calling the `createNode` method. If provided, it specifies the initial count for the node. | ||
* @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE. | ||
*/ | ||
override createNode(key: K, value?: V, count?: number): NODE { | ||
return new AVLTreeMultiMapNode(key, this._isMapMode ? undefined : value, count) as NODE; | ||
} | ||
/** | ||
* The function creates a new AVLTreeMultiMap object with the specified options and returns it. | ||
* @param [options] - The `options` parameter is an optional object that contains additional | ||
* configuration options for creating the AVLTreeMultiMap. It can have the following properties: | ||
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE | ||
* object. | ||
*/ | ||
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE { | ||
return new AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE>([], { | ||
override createTree(options?: AVLTreeMultiMapOptions<K, V[], R>) { | ||
return new AVLTreeMultiMap<K, V, R, MK, MV, MR>([], { | ||
iterationType: this.iterationType, | ||
isMapMode: this._isMapMode, | ||
specifyComparable: this._specifyComparable, | ||
@@ -139,324 +103,122 @@ toEntryFn: this._toEntryFn, | ||
...options | ||
}) as TREE; | ||
}); | ||
} | ||
/** | ||
* The function checks if the input is an instance of AVLTreeMultiMapNode. | ||
* @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 `AVLTreeMultiMapNode` class. | ||
*/ | ||
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE { | ||
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode; | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function overrides the add method of a TypeScript class to add a new node to a data structure | ||
* and update the count. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The | ||
* `keyNodeEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It | ||
* can also accept a value of type `BTNRep<K, V, NODE>`, which represents a key, node, | ||
* entry, or raw element | ||
* @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 the key-value pair will | ||
* be added once. However, you can specify a different value for `count` if you want to add | ||
* @returns a boolean value. | ||
* The function `createNode` overrides the method to create a new AVLTreeMultiMapNode with a | ||
* specified key and an empty array of values. | ||
* @param {K} key - The `key` parameter in the `createNode` method represents the key of the node | ||
* that will be created in the AVLTreeMultiMap. | ||
* @returns An AVLTreeMultiMapNode object is being returned, initialized with the provided key and an | ||
* empty array. | ||
*/ | ||
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean { | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
if (newNode === undefined) return false; | ||
const orgNodeCount = newNode?.count || 0; | ||
const inserted = super.add(newNode, newValue); | ||
if (inserted) { | ||
this._count += orgNodeCount; | ||
} | ||
return true; | ||
override createNode(key: K): AVLTreeMultiMapNode<K, V> { | ||
return new AVLTreeMultiMapNode<K, V>(key, []); | ||
} | ||
override add(node: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>>): boolean; | ||
override add(key: K, value: V): boolean; | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the delete method in a binary tree data structure, handling deletion of | ||
* nodes 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 for deleting a node from the | ||
* binary tree. It can be a key, node, or entry that determines which | ||
* node(s) should be deleted. | ||
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a | ||
* boolean flag that determines whether to ignore the count of the node being deleted. If | ||
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If | ||
* `ignoreCount` is set to | ||
* @returns The `delete` method overrides the default delete behavior in a binary tree data | ||
* structure. It takes a predicate or node to be deleted and an optional flag to ignore count. The | ||
* method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the | ||
* deleted node and whether balancing is needed in the tree. | ||
* The function `add` in TypeScript overrides the superclass method to add key-value pairs to an AVL | ||
* tree multi-map. | ||
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override add` method can be either a key-value pair entry or just a key. If it | ||
* is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and | ||
* `values` | ||
* @param {V} [value] - The `value` parameter in the `override add` method represents the value that | ||
* you want to add to the AVLTreeMultiMap. It can be a single value or an array of values associated | ||
* with a specific key. | ||
* @returns The `override add` method is returning a boolean value, which indicates whether the | ||
* addition operation was successful or not. | ||
*/ | ||
override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, ignoreCount = false): BinaryTreeDeleteResult<NODE>[] { | ||
const deletedResult: BinaryTreeDeleteResult<NODE>[] = []; | ||
if (!this.root) return deletedResult; | ||
override add(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value?: V): boolean { | ||
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry); | ||
const curr: NODE | undefined = this.getNode(keyNodeEntryOrRaw) ?? undefined; | ||
if (!curr) return deletedResult; | ||
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => { | ||
if (key === undefined || key === null) return false; | ||
const parent: NODE | undefined = curr?.parent ? curr.parent : undefined; | ||
let needBalanced: NODE | undefined = undefined, | ||
orgCurrent: NODE | undefined = curr; | ||
const existingValues = this.get(key); | ||
if (existingValues !== undefined && values !== undefined) { | ||
for (const value of values) existingValues.push(value); | ||
return true; | ||
} | ||
if (curr.count > 1 && !ignoreCount) { | ||
curr.count--; | ||
this._count--; | ||
} else { | ||
if (!curr.left) { | ||
if (!parent) { | ||
if (curr.right !== undefined && curr.right !== null) this._setRoot(curr.right); | ||
const existingNode = this.getNode(key); | ||
if (this.isRealNode(existingNode)) { | ||
if (existingValues === undefined) { | ||
super.add(key, values); | ||
return true; | ||
} | ||
if (values !== undefined) { | ||
for (const value of values) existingValues.push(value); | ||
return true; | ||
} else { | ||
const { familyPosition: fp } = curr; | ||
if (fp === 'LEFT' || fp === 'ROOT_LEFT') { | ||
parent.left = curr.right; | ||
} else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') { | ||
parent.right = curr.right; | ||
} | ||
needBalanced = parent; | ||
return false; | ||
} | ||
} else { | ||
const leftSubTreeRightMost = curr.left ? this.getRightMost(node => node, curr.left) : undefined; | ||
if (leftSubTreeRightMost) { | ||
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent; | ||
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost); | ||
if (parentOfLeftSubTreeMax) { | ||
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) { | ||
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left; | ||
} else { | ||
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left; | ||
} | ||
needBalanced = parentOfLeftSubTreeMax; | ||
} | ||
} | ||
return super.add(key, values); | ||
} | ||
this._size = this._size - 1; | ||
// TODO How to handle when the count of target node is lesser than current node's count | ||
if (orgCurrent) this._count -= orgCurrent.count; | ||
} | ||
}; | ||
deletedResult.push({ deleted: orgCurrent, needBalanced }); | ||
if (needBalanced) { | ||
this._balancePath(needBalanced); | ||
if (this.isEntry(keyNodeOrEntry)) { | ||
const [key, values] = keyNodeOrEntry; | ||
return _commonAdd(key, value !== undefined ? [value] : values); | ||
} | ||
return deletedResult; | ||
return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(log n) | ||
* | ||
* The "clear" function overrides the parent class's "clear" function and also resets the count to | ||
* zero. | ||
* The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data | ||
* structure and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value | ||
* pair in the AVLTreeMultiMapNode, or just the key itself. | ||
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific | ||
* value that you want to delete from the multi-map data structure associated with a particular key. | ||
* The function checks if the value exists in the array of values associated with the key, and if | ||
* found, removes it from the array. | ||
* @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified | ||
* `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If | ||
* the value was not found in the array, it returns `false`. | ||
*/ | ||
override clear() { | ||
super.clear(); | ||
this._count = 0; | ||
} | ||
deleteValue(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value: V): boolean { | ||
const values = this.get(keyNodeOrEntry); | ||
if (Array.isArray(values)) { | ||
const index = values.indexOf(value); | ||
if (index === -1) return false; | ||
values.splice(index, 1); | ||
/** | ||
* 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 currently set in | ||
* the 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. | ||
*/ | ||
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean { | ||
const sorted = this.dfs(node => node, 'IN'), | ||
n = sorted.length; | ||
if (sorted.length < 1) return false; | ||
// If no values left, remove the entire node | ||
if (values.length === 0) this.delete(keyNodeOrEntry); | ||
this.clear(); | ||
if (iterationType === 'RECURSIVE') { | ||
const buildBalanceBST = (l: number, r: number) => { | ||
if (l > r) return; | ||
const m = l + Math.floor((r - l) / 2); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count); | ||
else this.add(midNode.key, midNode.value, midNode.count); | ||
buildBalanceBST(l, m - 1); | ||
buildBalanceBST(m + 1, r); | ||
}; | ||
buildBalanceBST(0, n - 1); | ||
return true; | ||
} else { | ||
const stack: [[number, number]] = [[0, n - 1]]; | ||
while (stack.length > 0) { | ||
const popped = stack.pop(); | ||
if (popped) { | ||
const [l, r] = popped; | ||
if (l <= r) { | ||
const m = l + Math.floor((r - l) / 2); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count); | ||
else this.add(midNode.key, midNode.value, midNode.count); | ||
stack.push([m + 1, r]); | ||
stack.push([l, m - 1]); | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* 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. | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns A cloned tree object is being returned. | ||
*/ | ||
override clone(): TREE { | ||
override clone() { | ||
const cloned = this.createTree(); | ||
if (this._isMapMode) this.bfs(node => cloned.add(node.key, undefined, node.count)); | ||
else this.bfs(node => cloned.add(node.key, node.value, node.count)); | ||
if (this._isMapMode) cloned._store = this._store; | ||
this._clone(cloned); | ||
return cloned; | ||
} | ||
/** | ||
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap | ||
* with modified entries based on a provided callback. | ||
* @param callback - The `callback` parameter is a function that will be called for each entry in the | ||
* AVLTreeMultiMap. It takes four arguments: | ||
* @param [options] - The `options` parameter in the `override map` function is of type | ||
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional | ||
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function. | ||
* These options | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function. This can be useful when you want to access properties | ||
* or | ||
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries | ||
* transformed by the provided `callback` function. Each entry in the original tree is passed to the | ||
* `callback` function along with the index and the original tree itself. The transformed entries are | ||
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end. | ||
*/ | ||
override map<MK, MV, MR>( | ||
callback: EntryCallback<K, V | undefined, [MK, MV]>, | ||
options?: AVLTreeMultiMapOptions<MK, MV, MR>, | ||
thisArg?: any | ||
): AVLTreeMultiMap<MK, MV, MR> { | ||
const newTree = new AVLTreeMultiMap<MK, MV, MR>([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
/** | ||
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into | ||
* a node object. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The | ||
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
* `override` function. It represents the value associated with the key in the data structure. If no | ||
* value is provided, it will default to `undefined`. | ||
* @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. | ||
*/ | ||
protected override _keyValueNodeEntryRawToNodeAndValue( | ||
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, | ||
value?: V, | ||
count = 1 | ||
): [NODE | undefined, V | undefined] { | ||
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined]; | ||
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value]; | ||
if (this.isEntry(keyNodeEntryOrRaw)) { | ||
const [key, entryValue] = keyNodeEntryOrRaw; | ||
if (key === undefined || key === null) return [undefined, undefined]; | ||
const finalValue = value ?? entryValue; | ||
return [this.createNode(key, finalValue, count), finalValue]; | ||
} | ||
if (this.isRaw(keyNodeEntryOrRaw)) { | ||
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw); | ||
const finalValue = value ?? entryValue; | ||
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue]; | ||
} | ||
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value]; | ||
return [undefined, undefined]; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `_swapProperties` function swaps the properties (key, value, count, height) 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`. | ||
* @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 override _swapProperties( | ||
srcNode: R | BSTNOptKeyOrNode<K, NODE>, | ||
destNode: R | BSTNOptKeyOrNode<K, NODE> | ||
): NODE | undefined { | ||
srcNode = this.ensureNode(srcNode); | ||
destNode = this.ensureNode(destNode); | ||
if (srcNode && destNode) { | ||
const { key, value, count, height } = destNode; | ||
const tempNode = this.createNode(key, value, count); | ||
if (tempNode) { | ||
tempNode.height = height; | ||
destNode.key = srcNode.key; | ||
if (!this._isMapMode) destNode.value = srcNode.value; | ||
destNode.count = srcNode.count; | ||
destNode.height = srcNode.height; | ||
srcNode.key = tempNode.key; | ||
if (!this._isMapMode) srcNode.value = tempNode.value; | ||
srcNode.count = tempNode.count; | ||
srcNode.height = tempNode.height; | ||
} | ||
return destNode; | ||
} | ||
return 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 represents the node that needs to be replaced in the | ||
* data structure. It is of type NODE. | ||
* @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 override _replaceNode(oldNode: NODE, newNode: NODE): NODE { | ||
newNode.count = oldNode.count + newNode.count; | ||
return super._replaceNode(oldNode, newNode); | ||
} | ||
} |
@@ -10,4 +10,2 @@ /** | ||
import type { | ||
AVLTreeNested, | ||
AVLTreeNodeNested, | ||
AVLTreeOptions, | ||
@@ -17,18 +15,16 @@ BinaryTreeDeleteResult, | ||
BTNRep, | ||
EntryCallback | ||
EntryCallback, | ||
OptNodeOrNull | ||
} from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
export class AVLTreeNode< | ||
K = any, | ||
V = any, | ||
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V> | ||
> extends BSTNode<K, V, NODE> { | ||
export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> { | ||
/** | ||
* The constructor function initializes a new instance of a class with a key and an optional value, | ||
* and sets the height property to 0. | ||
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the | ||
* constructor. It is used to initialize the key property of the object being created. | ||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the | ||
* value associated with the key in the constructor. | ||
* This TypeScript constructor function initializes an instance with a key and an optional value. | ||
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element | ||
* within a data structure. It serves as a reference or identifier for accessing or manipulating the | ||
* associated value or data. | ||
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not | ||
* have to be provided when creating an instance of the class. If a value is not provided, it will | ||
* default to `undefined`. | ||
*/ | ||
@@ -38,2 +34,30 @@ constructor(key: K, value?: V) { | ||
} | ||
override parent?: AVLTreeNode<K, V> = undefined; | ||
override _left?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<AVLTreeNode<K, V>> { | ||
return this._left; | ||
} | ||
override set left(v: OptNodeOrNull<AVLTreeNode<K, V>>) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._left = v; | ||
} | ||
override _right?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined; | ||
override get right(): OptNodeOrNull<AVLTreeNode<K, V>> { | ||
return this._right; | ||
} | ||
override set right(v: OptNodeOrNull<AVLTreeNode<K, V>>) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._right = v; | ||
} | ||
} | ||
@@ -50,36 +74,21 @@ | ||
*/ | ||
export class AVLTree< | ||
K = any, | ||
V = any, | ||
R = object, | ||
MK = any, | ||
MV = any, | ||
MR = object, | ||
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, | ||
TREE extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> = AVLTree< | ||
K, | ||
V, | ||
R, | ||
MK, | ||
MV, | ||
MR, | ||
NODE, | ||
AVLTreeNested<K, V, R, MK, MV, MR, NODE> | ||
> | ||
> | ||
extends BST<K, V, R, MK, MV, MR, NODE, TREE> | ||
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> | ||
export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> | ||
extends BST<K, V, R, MK, MV, MR> | ||
implements IBinaryTree<K, V, R, MK, MV, MR> | ||
{ | ||
/** | ||
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes, | ||
* entries, or raw elements. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will | ||
* be used to initialize the AVLTree. | ||
* @param [options] - The `options` parameter is an optional object that can be used to customize the | ||
* behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare | ||
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and | ||
* `nodeBuilder` ( | ||
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided | ||
* in an iterable format. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is | ||
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided | ||
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V, | ||
* R>`. It is an optional parameter that allows you to specify additional options for configuring the | ||
* AVL tree. These options could include things like custom comparators, initial capacity, or any | ||
* other configuration settings specific | ||
*/ | ||
constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: AVLTreeOptions<K, V, R>) { | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, AVLTreeNode<K, V>> | R> = [], | ||
options?: AVLTreeOptions<K, V, R> | ||
) { | ||
super([], options); | ||
@@ -90,2 +99,5 @@ if (keysNodesEntriesOrRaws) super.addMany(keysNodesEntriesOrRaws); | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new AVL tree node with the given key and value. | ||
@@ -97,9 +109,12 @@ * @param {K} key - The key parameter is of type K, which represents the key of the node being | ||
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic | ||
* type NODE. | ||
* type AVLTreeNode<K, V>. | ||
*/ | ||
override createNode(key: K, value?: V): NODE { | ||
return new AVLTreeNode<K, V, NODE>(key, this._isMapMode ? undefined : value) as NODE; | ||
override createNode(key: K, value?: V): AVLTreeNode<K, V> { | ||
return new AVLTreeNode<K, V>(key, this._isMapMode ? undefined : value) as AVLTreeNode<K, V>; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new AVL tree with the specified options and returns it. | ||
@@ -111,4 +126,4 @@ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be | ||
*/ | ||
override createTree(options?: AVLTreeOptions<K, V, R>): TREE { | ||
return new AVLTree<K, V, R, MK, MV, MR, NODE, TREE>([], { | ||
override createTree(options?: AVLTreeOptions<K, V, R>) { | ||
return new AVLTree<K, V, R, MK, MV, MR>([], { | ||
iterationType: this.iterationType, | ||
@@ -120,14 +135,17 @@ isMapMode: this._isMapMode, | ||
...options | ||
}) as TREE; | ||
}); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the input is an instance of AVLTreeNode. | ||
* @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 | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `AVLTreeNode` class. | ||
*/ | ||
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE { | ||
return keyNodeEntryOrRaw instanceof AVLTreeNode; | ||
override isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V> { | ||
return keyNodeOrEntry instanceof AVLTreeNode; | ||
} | ||
@@ -137,9 +155,8 @@ | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the add method of a class and inserts a key-value pair into a data | ||
* structure, then balances the path. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept values of type `R`, `BTNRep<K, V, NODE>`, or | ||
* `RawElement`. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>` | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -149,6 +166,6 @@ * the key or node being added to the data structure. | ||
*/ | ||
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean { | ||
if (keyNodeEntryOrRaw === null) return false; | ||
const inserted = super.add(keyNodeEntryOrRaw, value); | ||
if (inserted) this._balancePath(keyNodeEntryOrRaw); | ||
override add(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>, value?: V): boolean { | ||
if (keyNodeOrEntry === null) return false; | ||
const inserted = super.add(keyNodeOrEntry, value); | ||
if (inserted) this._balancePath(keyNodeOrEntry); | ||
return inserted; | ||
@@ -159,7 +176,7 @@ } | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the delete method in a TypeScript class, performs deletion, and then | ||
* balances the tree if necessary. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw` | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override delete` method can be one of the following types: | ||
@@ -171,4 +188,4 @@ * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete` | ||
*/ | ||
override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] { | ||
const deletedResults = super.delete(keyNodeEntryOrRaw); | ||
override delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] { | ||
const deletedResults = super.delete(keyNodeOrEntry); | ||
for (const { needBalanced } of deletedResults) { | ||
@@ -182,2 +199,22 @@ if (needBalanced) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default map behavior of an AVLTree data structure | ||
* by applying a callback function to each entry and creating a new AVLTree with the results. | ||
* @param callback - A function that will be called for each entry in the AVLTree. It takes four | ||
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to | ||
* the AVLTree itself. | ||
* @param [options] - The `options` parameter in the `override map` function is of type | ||
* `AVLTreeOptions<MK, MV, MR>`. It is an optional parameter that allows you to specify additional | ||
* options for the AVL tree being created during the mapping process. These options could include | ||
* custom comparators, initial | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) within the callback function. This can be useful when you want to access | ||
* properties or | ||
* @returns The `map` method is returning a new AVLTree instance (`newTree`) with the entries | ||
* modified by the provided callback function. | ||
*/ | ||
override map( | ||
@@ -197,2 +234,16 @@ callback: EntryCallback<K, V | undefined, [MK, MV]>, | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns A cloned tree object is being returned. | ||
*/ | ||
override clone() { | ||
const cloned = this.createTree(); | ||
this._clone(cloned); | ||
return cloned; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -203,6 +254,6 @@ * Space Complexity: O(1) | ||
* binary search tree. | ||
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node | ||
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node. | ||
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of | ||
* `R` or an instance of `BSTNOptKeyOrNode<K, NODE>`. | ||
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} srcNode - The `srcNode` parameter represents either a node | ||
* object (`AVLTreeNode<K, V>`) or a key-value pair (`R`) that is being swapped with another node. | ||
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} destNode - The `destNode` parameter is either an instance of | ||
* `R` or an instance of `BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>`. | ||
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and | ||
@@ -212,5 +263,5 @@ * `destNodeEnsured` are truthy. Otherwise, it returns `undefined`. | ||
protected override _swapProperties( | ||
srcNode: R | BSTNOptKeyOrNode<K, NODE>, | ||
destNode: R | BSTNOptKeyOrNode<K, NODE> | ||
): NODE | undefined { | ||
srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>, | ||
destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>> | ||
): AVLTreeNode<K, V> | undefined { | ||
const srcNodeEnsured = this.ensureNode(srcNode); | ||
@@ -245,3 +296,3 @@ const destNodeEnsured = this.ensureNode(destNode); | ||
* The function calculates the balance factor of a node in a binary tree. | ||
* @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a | ||
* @param {AVLTreeNode<K, V>} node - The parameter "node" is of type "AVLTreeNode<K, V>", which likely represents a node in a | ||
* binary tree data structure. | ||
@@ -251,3 +302,3 @@ * @returns the balance factor of a given node. The balance factor is calculated by subtracting the | ||
*/ | ||
protected _balanceFactor(node: NODE): number { | ||
protected _balanceFactor(node: AVLTreeNode<K, V>): number { | ||
if (!node.right) | ||
@@ -268,5 +319,5 @@ // node has no right subtree | ||
* right children. | ||
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure. | ||
* @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure. | ||
*/ | ||
protected _updateHeight(node: NODE): void { | ||
protected _updateHeight(node: AVLTreeNode<K, V>): void { | ||
if (!node.left && !node.right) node.height = 0; | ||
@@ -285,5 +336,5 @@ else if (!node.left) { | ||
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
protected _balanceLL(A: NODE): void { | ||
protected _balanceLL(A: AVLTreeNode<K, V>): void { | ||
const parentOfA = A.parent; | ||
@@ -319,5 +370,5 @@ const B = A.left; | ||
* The `_balanceLR` function performs a left-right rotation to balance a binary tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
protected _balanceLR(A: NODE): void { | ||
protected _balanceLR(A: AVLTreeNode<K, V>): void { | ||
const parentOfA = A.parent; | ||
@@ -371,5 +422,5 @@ const B = A.left; | ||
* The function `_balanceRR` performs a right-right rotation to balance a binary tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
protected _balanceRR(A: NODE): void { | ||
protected _balanceRR(A: AVLTreeNode<K, V>): void { | ||
const parentOfA = A.parent; | ||
@@ -410,5 +461,5 @@ const B = A.right; | ||
* The function `_balanceRL` performs a right-left rotation to balance a binary tree. | ||
* @param {NODE} A - A is a node in a binary tree. | ||
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree. | ||
*/ | ||
protected _balanceRL(A: NODE): void { | ||
protected _balanceRL(A: AVLTreeNode<K, V>): void { | ||
const parentOfA = A.parent; | ||
@@ -462,6 +513,6 @@ const B = A.right; | ||
* to restore balance in an AVL tree after inserting a node. | ||
* @param {BTNRep<K, V, NODE> | R} node - The `node` parameter can be of type `R` or | ||
* `BTNRep<K, V, NODE>`. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or | ||
* `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
*/ | ||
protected _balancePath(node: BTNRep<K, V, NODE> | R): void { | ||
protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void { | ||
node = this.ensureNode(node); | ||
@@ -514,5 +565,5 @@ const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n) | ||
* same as the old node. | ||
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* @param {AVLTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* the data structure. | ||
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in | ||
* @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in | ||
* the data structure. | ||
@@ -522,3 +573,3 @@ * @returns The method is returning the result of calling the `_replaceNode` method from the | ||
*/ | ||
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE { | ||
protected override _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V> { | ||
newNode.height = oldNode.height; | ||
@@ -525,0 +576,0 @@ |
@@ -10,2 +10,5 @@ /** | ||
/** | ||
* | ||
*/ | ||
export class BinaryIndexedTree { | ||
@@ -12,0 +15,0 @@ protected readonly _freq: number; |
@@ -8,5 +8,3 @@ /** | ||
*/ | ||
import { | ||
BSTNested, | ||
BSTNodeNested, | ||
import type { | ||
BSTNOptKeyOrNode, | ||
@@ -32,34 +30,27 @@ BSTOptions, | ||
export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode< | ||
K, | ||
V, | ||
NODE | ||
> { | ||
override parent?: NODE; | ||
export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> { | ||
/** | ||
* This TypeScript constructor function initializes an instance with a key and an optional value. | ||
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element | ||
* within a data structure. It serves as a reference or identifier for accessing or manipulating the | ||
* associated value. | ||
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not | ||
* have to be provided when creating an instance of the class. If a value is not provided, it will | ||
* default to `undefined`. | ||
*/ | ||
constructor(key: K, value?: V) { | ||
super(key, value); | ||
this.parent = undefined; | ||
this._left = undefined; | ||
this._right = undefined; | ||
} | ||
override _left?: OptNodeOrNull<NODE>; | ||
override parent?: BSTNode<K, V> = undefined; | ||
/** | ||
* The function returns the value of the `_left` property. | ||
* @returns The `_left` property of the current object is being returned. | ||
*/ | ||
override get left(): OptNodeOrNull<NODE> { | ||
override _left?: OptNodeOrNull<BSTNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<BSTNode<K, V>> { | ||
return this._left; | ||
} | ||
/** | ||
* The function sets the left child of a node and updates the parent reference of the child. | ||
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an | ||
* instance of the `NODE` class or `undefined`. | ||
*/ | ||
override set left(v: OptNodeOrNull<NODE>) { | ||
override set left(v: OptNodeOrNull<BSTNode<K, V>>) { | ||
if (v) { | ||
v.parent = this as unknown as NODE; | ||
v.parent = this; | ||
} | ||
@@ -69,21 +60,11 @@ this._left = v; | ||
override _right?: OptNodeOrNull<NODE>; | ||
override _right?: OptNodeOrNull<BSTNode<K, V>> = undefined; | ||
/** | ||
* The function returns the right node of a binary tree or undefined if there is no right node. | ||
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or | ||
* `undefined`. | ||
*/ | ||
override get right(): OptNodeOrNull<NODE> { | ||
override get right(): OptNodeOrNull<BSTNode<K, V>> { | ||
return this._right; | ||
} | ||
/** | ||
* The function sets the right child of a node and updates the parent reference of the child. | ||
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a | ||
* `NODE` object or `undefined`. | ||
*/ | ||
override set right(v: OptNodeOrNull<NODE>) { | ||
override set right(v: OptNodeOrNull<BSTNode<K, V>>) { | ||
if (v) { | ||
v.parent = this as unknown as NODE; | ||
v.parent = this; | ||
} | ||
@@ -159,33 +140,16 @@ this._right = v; | ||
*/ | ||
export class BST< | ||
K = any, | ||
V = any, | ||
R = object, | ||
MK = any, | ||
MV = any, | ||
MR = object, | ||
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, | ||
TREE extends BST<K, V, R, MK, MV, MR, NODE, TREE> = BST< | ||
K, | ||
V, | ||
R, | ||
MK, | ||
MV, | ||
MR, | ||
NODE, | ||
BSTNested<K, V, R, MK, MV, MR, NODE> | ||
> | ||
> | ||
extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> | ||
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> | ||
export class BST<K = any, V = any, R = object, MK = any, MV = any, MR = object> | ||
extends BinaryTree<K, V, R, MK, MV, MR> | ||
implements IBinaryTree<K, V, R, MK, MV, MR> | ||
{ | ||
/** | ||
* This is the constructor function for a Binary Search Tree class in TypeScript. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be | ||
* added to the binary search tree during the construction of the object. | ||
* @param [options] - An optional object that contains additional options for the Binary Search Tree. | ||
* It can include a comparator function that defines the order of the elements in the tree. | ||
* This TypeScript constructor initializes a binary search tree with optional options and adds | ||
* elements if provided. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to | ||
* initialize the binary search tree with keys, nodes, entries, or raw data. | ||
* @param [options] - The `options` parameter is an optional object that can contain the following | ||
* properties: | ||
*/ | ||
constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: BSTOptions<K, V, R>) { | ||
constructor(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, BSTNode<K, V>> | R> = [], options?: BSTOptions<K, V, R>) { | ||
super([], options); | ||
@@ -202,9 +166,5 @@ | ||
protected override _root?: NODE = undefined; | ||
protected override _root?: BSTNode<K, V> = undefined; | ||
/** | ||
* The function returns the root node of a tree structure. | ||
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`. | ||
*/ | ||
override get root(): OptNode<NODE> { | ||
override get root(): OptNode<BSTNode<K, V>> { | ||
return this._root; | ||
@@ -215,7 +175,2 @@ } | ||
/** | ||
* The above function is a getter method in TypeScript that returns the value of the private property | ||
* `_isReverse`. | ||
* @returns The `isReverse` property of the object, which is a boolean value. | ||
*/ | ||
get isReverse(): boolean { | ||
@@ -225,3 +180,36 @@ return this._isReverse; | ||
protected _comparator: Comparator<K> = (a: K, b: K): number => { | ||
if (isComparable(a) && isComparable(b)) { | ||
if (a > b) return 1; | ||
if (a < b) return -1; | ||
return 0; | ||
} | ||
if (this._specifyComparable) { | ||
if (this._specifyComparable(a) > this._specifyComparable(b)) return 1; | ||
if (this._specifyComparable(a) < this._specifyComparable(b)) return -1; | ||
return 0; | ||
} | ||
if (typeof a === 'object' || typeof b === 'object') { | ||
throw TypeError( | ||
`When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.` | ||
); | ||
} | ||
return 0; | ||
}; | ||
get comparator() { | ||
return this._comparator; | ||
} | ||
protected _specifyComparable?: (key: K) => Comparable; | ||
get specifyComparable() { | ||
return this._specifyComparable; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new BSTNode with the given key and value and returns it. | ||
@@ -232,9 +220,12 @@ * @param {K} key - The key parameter is of type K, which represents the type of the key for the node | ||
* value associated with the key in the node being created. | ||
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type. | ||
* @returns The method is returning a new instance of the BSTNode class, casted as the BSTNode<K, V> type. | ||
*/ | ||
override createNode(key: K, value?: V): NODE { | ||
return new BSTNode<K, V, NODE>(key, this._isMapMode ? undefined : value) as NODE; | ||
override createNode(key: K, value?: V): BSTNode<K, V> { | ||
return new BSTNode<K, V>(key, this._isMapMode ? undefined : value); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new binary search tree with the specified options. | ||
@@ -246,4 +237,4 @@ * @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
*/ | ||
override createTree(options?: BSTOptions<K, V, R>): TREE { | ||
return new BST<K, V, R, MK, MV, MR, NODE, TREE>([], { | ||
override createTree(options?: BSTOptions<K, V, R>) { | ||
return new BST<K, V, R, MK, MV, MR>([], { | ||
iterationType: this.iterationType, | ||
@@ -255,24 +246,6 @@ isMapMode: this._isMapMode, | ||
...options | ||
}) as TREE; | ||
}); | ||
} | ||
/** | ||
* The function overrides a method and converts a key, value pair or entry or raw element to a node. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of | ||
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw | ||
* element. | ||
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the | ||
* value associated with a key in a key-value pair. | ||
* @returns either a NODE object or undefined. | ||
*/ | ||
protected override _keyValueNodeEntryRawToNodeAndValue( | ||
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, | ||
value?: V | ||
): [OptNode<NODE>, V | undefined] { | ||
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
if (node === null) return [undefined, undefined]; | ||
return [node, value ?? entryValue]; | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
@@ -283,4 +256,4 @@ * Space Complexity: O(log n) | ||
* it doesn't exist. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept a value of type `R`, which represents the key, node, | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node, | ||
* entry, or raw element that needs to be ensured in the tree. | ||
@@ -294,28 +267,34 @@ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional | ||
override ensureNode( | ||
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, | ||
keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, | ||
iterationType: IterationType = this.iterationType | ||
): OptNode<NODE> { | ||
return super.ensureNode(keyNodeEntryOrRaw, iterationType) ?? undefined; | ||
): OptNode<BSTNode<K, V>> { | ||
return super.ensureNode(keyNodeOrEntry, iterationType) ?? undefined; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if the input is an instance of the BSTNode 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 | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `BSTNode` class. | ||
*/ | ||
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE { | ||
return keyNodeEntryOrRaw instanceof BSTNode; | ||
override isNode(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>): keyNodeOrEntry is BSTNode<K, V> { | ||
return keyNodeOrEntry instanceof BSTNode; | ||
} | ||
/** | ||
* The function "override isKey" checks if a key is comparable based on a given comparator. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function "override isValidKey" checks if a key is comparable based on a given comparator. | ||
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of | ||
* type `K`. | ||
* @returns The `override isKey(key: any): key is K` function is returning a boolean value based on | ||
* @returns The `override isValidKey(key: any): key is K` function is returning a boolean value based on | ||
* the result of the `isComparable` function with the condition `this._compare !== | ||
* this._DEFAULT_COMPARATOR`. | ||
*/ | ||
override isKey(key: any): key is K { | ||
override isValidKey(key: any): key is K { | ||
return isComparable(key, this._specifyComparable !== undefined); | ||
@@ -326,7 +305,7 @@ } | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the | ||
@@ -336,4 +315,4 @@ * key in the binary search tree. If provided, it will be stored in the node along with the key. | ||
*/ | ||
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean { | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
override add(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, value?: V): boolean { | ||
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value); | ||
if (newNode === undefined) return false; | ||
@@ -398,3 +377,3 @@ | ||
override addMany( | ||
keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>>, | ||
keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, | ||
values?: Iterable<V | undefined>, | ||
@@ -413,4 +392,5 @@ isBalanceAdd = true, | ||
if (!isBalanceAdd) { | ||
for (const kve of keysNodesEntriesOrRaws) { | ||
for (let kve of keysNodesEntriesOrRaws) { | ||
const value = valuesIterator?.next().value; | ||
if (this.isRaw(kve)) kve = this._toEntryFn!(kve); | ||
inserted.push(this.add(kve, value)); | ||
@@ -422,3 +402,3 @@ } | ||
const realBTNExemplars: { | ||
key: R | BTNRep<K, V, NODE>; | ||
key: R | BTNRep<K, V, BSTNode<K, V>>; | ||
value: V | undefined; | ||
@@ -434,19 +414,17 @@ orgIndex: number; | ||
let sorted: { key: R | BTNRep<K, V, NODE>; value: V | undefined; orgIndex: number }[] = []; | ||
let sorted: { key: R | BTNRep<K, V, BSTNode<K, V>>; value: V | undefined; orgIndex: number }[] = []; | ||
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => { | ||
let keyA: K | undefined | null, keyB: K | undefined | null; | ||
if (this.isEntry(a)) keyA = a[0]; | ||
if (this.isRaw(a)) keyA = this._toEntryFn!(a)[0]; | ||
else if (this.isEntry(a)) keyA = a[0]; | ||
else if (this.isRealNode(a)) keyA = a.key; | ||
else if (this._toEntryFn) { | ||
keyA = this._toEntryFn(a as R)[0]; | ||
} else { | ||
else { | ||
keyA = a as K; | ||
} | ||
if (this.isEntry(b)) keyB = b[0]; | ||
if (this.isRaw(b)) keyB = this._toEntryFn!(b)[0]; | ||
else if (this.isEntry(b)) keyB = b[0]; | ||
else if (this.isRealNode(b)) keyB = b.key; | ||
else if (this._toEntryFn) { | ||
keyB = this._toEntryFn(b as R)[0]; | ||
} else { | ||
else { | ||
keyB = b as K; | ||
@@ -461,7 +439,13 @@ } | ||
const _dfs = (arr: { key: R | BTNRep<K, V, NODE>; value: V | undefined; orgIndex: number }[]) => { | ||
const _dfs = (arr: { key: R | BTNRep<K, V, BSTNode<K, V>>; value: V | undefined; orgIndex: number }[]) => { | ||
if (arr.length === 0) return; | ||
const mid = Math.floor((arr.length - 1) / 2); | ||
const { key, value, orgIndex } = arr[mid]; | ||
let { key, value } = arr[mid]; | ||
const { orgIndex } = arr[mid]; | ||
if (this.isRaw(key)) { | ||
const entry = this._toEntryFn!(key); | ||
key = entry[0]; | ||
value = entry[1] ?? value; | ||
} | ||
inserted[orgIndex] = this.add(key, value); | ||
@@ -481,3 +465,9 @@ _dfs(arr.slice(0, mid)); | ||
const m = l + Math.floor((r - l) / 2); | ||
const { key, value, orgIndex } = sorted[m]; | ||
let { key, value } = sorted[m]; | ||
const { orgIndex } = sorted[m]; | ||
if (this.isRaw(key)) { | ||
const entry = this._toEntryFn!(key); | ||
key = entry[0]; | ||
value = entry[1] ?? value; | ||
} | ||
inserted[orgIndex] = this.add(key, value); | ||
@@ -506,4 +496,4 @@ stack.push([m + 1, r]); | ||
* on specified criteria. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The | ||
* `keyNodeEntryRawOrPredicate` parameter in the `override search` method can accept one of the | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the | ||
* following types: | ||
@@ -515,5 +505,5 @@ * @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the | ||
* that will be called on each node that matches the search criteria. It is of type `C`, which | ||
* extends `NodeCallback<NODE>`. The callback function should accept a node of type `NODE` as its | ||
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its | ||
* argument and | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `override search` | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search` | ||
* method represents the node from which the search operation will begin. It is the starting point | ||
@@ -530,25 +520,25 @@ * for searching within the tree data structure. The method ensures that the `startNode` is a valid | ||
*/ | ||
override search<C extends NodeCallback<NODE>>( | ||
keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>, | ||
override search<C extends NodeCallback<BSTNode<K, V>>>( | ||
keyNodeEntryOrPredicate: BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>> | Range<K>, | ||
onlyOne = false, | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
startNode: BTNRep<K, V, NODE> | R = this._root, | ||
startNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
iterationType: IterationType = this.iterationType | ||
): ReturnType<C>[] { | ||
if (keyNodeEntryRawOrPredicate === undefined) return []; | ||
if (keyNodeEntryRawOrPredicate === null) return []; | ||
if (keyNodeEntryOrPredicate === undefined) return []; | ||
if (keyNodeEntryOrPredicate === null) return []; | ||
startNode = this.ensureNode(startNode); | ||
if (!startNode) return []; | ||
let predicate: NodePredicate<NODE>; | ||
let predicate: NodePredicate<BSTNode<K, V>>; | ||
const isRange = this.isRange(keyNodeEntryRawOrPredicate); | ||
const isRange = this.isRange(keyNodeEntryOrPredicate); | ||
// Set predicate based on parameter type | ||
if (isRange) { | ||
predicate = node => keyNodeEntryRawOrPredicate.isInRange(node.key, this._comparator); | ||
predicate = node => keyNodeEntryOrPredicate.isInRange(node.key, this._comparator); | ||
} else { | ||
predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate); | ||
predicate = this._ensurePredicate(keyNodeEntryOrPredicate); | ||
} | ||
const isToLeftByRange = (cur: NODE) => { | ||
const isToLeftByRange = (cur: BSTNode<K, V>) => { | ||
if (isRange) { | ||
const range = keyNodeEntryRawOrPredicate; | ||
const range = keyNodeEntryOrPredicate; | ||
const leftS = this.isReverse ? range.high : range.low; | ||
@@ -561,5 +551,5 @@ const leftI = this.isReverse ? range.includeHigh : range.includeLow; | ||
const isToRightByRange = (cur: NODE) => { | ||
const isToRightByRange = (cur: BSTNode<K, V>) => { | ||
if (isRange) { | ||
const range = keyNodeEntryRawOrPredicate; | ||
const range = keyNodeEntryOrPredicate; | ||
const rightS = this.isReverse ? range.low : range.high; | ||
@@ -574,3 +564,3 @@ const rightI = this.isReverse ? range.includeLow : range.includeLow; | ||
if (iterationType === 'RECURSIVE') { | ||
const dfs = (cur: NODE) => { | ||
const dfs = (cur: BSTNode<K, V>) => { | ||
if (predicate(cur)) { | ||
@@ -586,4 +576,4 @@ ans.push(callback(cur)); | ||
if (this.isRealNode(cur.right) && isToRightByRange(cur)) dfs(cur.right); | ||
} else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate); | ||
} else if (!this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
if ( | ||
@@ -621,4 +611,4 @@ this.isRealNode(cur.left) && | ||
if (this.isRealNode(cur.right) && isToRightByRange(cur)) stack.push(cur.right); | ||
} else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate); | ||
} else if (!this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
if ( | ||
@@ -650,3 +640,3 @@ this.isRealNode(cur.right) && | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(k + log n) | ||
* | ||
@@ -658,5 +648,5 @@ * The `rangeSearch` function searches for nodes within a specified range in a binary search tree. | ||
* function that is used to process each node that is found within the specified range during the | ||
* search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the | ||
* search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the | ||
* data structure. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch` | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch` | ||
* function represents the node from which the search for nodes within the specified range will | ||
@@ -671,6 +661,6 @@ * begin. It is the starting point for the range search operation. | ||
*/ | ||
rangeSearch<C extends NodeCallback<NODE>>( | ||
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>( | ||
range: Range<K> | [K, K], | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
startNode: BTNRep<K, V, NODE> | R = this._root, | ||
startNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
iterationType: IterationType = this.iterationType | ||
@@ -684,8 +674,8 @@ ) { | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure. | ||
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate` | ||
* parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`. | ||
* @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method | ||
* This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure. | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` | ||
* parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`. | ||
* @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method | ||
* is used to specify the starting point for searching nodes in the binary search tree. If no | ||
@@ -698,4 +688,4 @@ * specific starting point is provided, the default value is set to `this._root`, which is the root | ||
* no value is provided when calling the method. | ||
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`). | ||
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at | ||
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<BSTNode<K, V>>`). | ||
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryOrPredicate, beginning at | ||
* the specified root node (`startNode`) and using the specified iteration type. The method then | ||
@@ -705,7 +695,7 @@ * returns the first node found or `undefined` if no node is found. | ||
override getNode( | ||
keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, | ||
startNode: R | BSTNOptKeyOrNode<K, NODE> = this._root, | ||
keyNodeEntryOrPredicate: BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>, | ||
startNode: BSTNOptKeyOrNode<K, BSTNode<K, V>> = this._root, | ||
iterationType: IterationType = this.iterationType | ||
): OptNode<NODE> { | ||
return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0] ?? undefined; | ||
): OptNode<BSTNode<K, V>> { | ||
return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? undefined; | ||
} | ||
@@ -725,3 +715,3 @@ | ||
* take one of the following values: | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a | ||
@@ -734,6 +724,6 @@ * node entry. If not specified, the default value is the root of the tree. | ||
*/ | ||
override dfs<C extends NodeCallback<NODE>>( | ||
override dfs<C extends NodeCallback<BSTNode<K, V>>>( | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
pattern: DFSOrderPattern = 'IN', | ||
startNode: BTNRep<K, V, NODE> | R = this._root, | ||
startNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
iterationType: IterationType = this.iterationType | ||
@@ -753,3 +743,3 @@ ): ReturnType<C>[] { | ||
* node being visited, and it can return a value of any type. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry | ||
@@ -762,5 +752,5 @@ * object. If no value is provided, the default value is the root of the tree. | ||
*/ | ||
override bfs<C extends NodeCallback<NODE>>( | ||
override bfs<C extends NodeCallback<BSTNode<K, V>>>( | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
startNode: BTNRep<K, V, NODE> | R = this._root, | ||
startNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
iterationType: IterationType = this.iterationType | ||
@@ -778,5 +768,5 @@ ): ReturnType<C>[] { | ||
* @param {C} callback - The `callback` parameter is a generic type `C` that extends | ||
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the | ||
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the | ||
* tree during the iteration process. | ||
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for listing the levels of the binary tree. It can be either a root node of the tree, a | ||
@@ -790,5 +780,5 @@ * key-value pair representing a node in the tree, or a key representing a node in the tree. If no | ||
*/ | ||
override listLevels<C extends NodeCallback<NODE>>( | ||
override listLevels<C extends NodeCallback<BSTNode<K, V>>>( | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
startNode: BTNRep<K, V, NODE> | R = this._root, | ||
startNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
iterationType: IterationType = this.iterationType | ||
@@ -811,3 +801,3 @@ ): ReturnType<C>[][] { | ||
* 0, or 1, where: | ||
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in | ||
* the binary tree that you want to start traversing from. It can be specified either by providing | ||
@@ -821,10 +811,10 @@ * the key of the node, the node itself, or an entry containing the key and value of the node. If no | ||
*/ | ||
lesserOrGreaterTraverse<C extends NodeCallback<NODE>>( | ||
lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>( | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
lesserOrGreater: CP = -1, | ||
targetNode: BTNRep<K, V, NODE> | R = this._root, | ||
targetNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
iterationType: IterationType = this.iterationType | ||
): ReturnType<C>[] { | ||
const targetNodeEnsured = this.ensureNode(targetNode); | ||
const ans: ReturnType<NodeCallback<NODE>>[] = []; | ||
const ans: ReturnType<NodeCallback<BSTNode<K, V>>>[] = []; | ||
if (!this._root) return ans; | ||
@@ -836,3 +826,3 @@ if (!targetNodeEnsured) return ans; | ||
if (iterationType === 'RECURSIVE') { | ||
const dfs = (cur: NODE) => { | ||
const dfs = (cur: BSTNode<K, V>) => { | ||
const compared = this._compare(cur.key, targetKey); | ||
@@ -848,3 +838,3 @@ if (Math.sign(compared) === lesserOrGreater) ans.push(callback(cur)); | ||
} else { | ||
const queue = new Queue<NODE>([this._root]); | ||
const queue = new Queue<BSTNode<K, V>>([this._root]); | ||
while (queue.size > 0) { | ||
@@ -933,3 +923,3 @@ const cur = queue.shift(); | ||
if (iterationType === 'RECURSIVE') { | ||
const _height = (cur: OptNodeOrNull<NODE>): number => { | ||
const _height = (cur: OptNodeOrNull<BSTNode<K, V>>): number => { | ||
if (!cur) return 0; | ||
@@ -943,6 +933,6 @@ const leftHeight = _height(cur.left), | ||
} else { | ||
const stack: NODE[] = []; | ||
let node: OptNode<NODE> = this._root, | ||
last: OptNode<NODE> = undefined; | ||
const depths: Map<NODE, number> = new Map(); | ||
const stack: BSTNode<K, V>[] = []; | ||
let node: OptNode<BSTNode<K, V>> = this._root, | ||
last: OptNode<BSTNode<K, V>> = undefined; | ||
const depths: Map<BSTNode<K, V>, number> = new Map(); | ||
@@ -973,47 +963,78 @@ while (stack.length > 0 || node) { | ||
protected _comparator: Comparator<K> = (a: K, b: K): number => { | ||
if (isComparable(a) && isComparable(b)) { | ||
if (a > b) return 1; | ||
if (a < b) return -1; | ||
return 0; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default map behavior for a binary search tree by | ||
* applying a callback function to each entry and creating a new tree with the results. | ||
* @param callback - A function that will be called for each entry in the BST. It takes four | ||
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to | ||
* the BST itself. | ||
* @param [options] - The `options` parameter in the `override map` method is of type `BSTOptions<MK, | ||
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the Binary | ||
* Search Tree (BST) being created in the `map` method. These options could include configuration | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` method is used to specify | ||
* the value of `this` that should be used when executing the `callback` function. It allows you to | ||
* set the context or scope in which the callback function will be called. This can be useful when | ||
* you want | ||
* @returns The `map` method is returning a new Binary Search Tree (`BST`) instance with the entries | ||
* transformed by the provided callback function. | ||
*/ | ||
override map( | ||
callback: EntryCallback<K, V | undefined, [MK, MV]>, | ||
options?: BSTOptions<MK, MV, MR>, | ||
thisArg?: any | ||
): BST<MK, MV, MR> { | ||
const newTree = new BST<MK, MV, MR>([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
if (this._specifyComparable) { | ||
if (this._specifyComparable(a) > this._specifyComparable(b)) return 1; | ||
if (this._specifyComparable(a) < this._specifyComparable(b)) return -1; | ||
return 0; | ||
} | ||
if (typeof a === 'object' || typeof b === 'object') { | ||
throw TypeError( | ||
`When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.` | ||
); | ||
} | ||
return newTree; | ||
} | ||
return 0; | ||
}; | ||
/** | ||
* The function returns the value of the _comparator property. | ||
* @returns The `_comparator` property is being returned. | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns The `cloned` object is being returned. | ||
*/ | ||
get comparator() { | ||
return this._comparator; | ||
override clone() { | ||
const cloned = this.createTree(); | ||
this._clone(cloned); | ||
return cloned; | ||
} | ||
protected _specifyComparable?: (key: K) => Comparable; | ||
/** | ||
* This function returns the value of the `_specifyComparable` property. | ||
* @returns The method `specifyComparable()` is being returned, which is a getter method for the | ||
* `_specifyComparable` property. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function overrides a method and converts a key, value pair or entry or raw element to a node. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of | ||
* type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw | ||
* element. | ||
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the | ||
* value associated with a key in a key-value pair. | ||
* @returns either a BSTNode<K, V> object or undefined. | ||
*/ | ||
get specifyComparable() { | ||
return this._specifyComparable; | ||
protected override _keyValueNodeOrEntryToNodeAndValue( | ||
keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, | ||
value?: V | ||
): [OptNode<BSTNode<K, V>>, V | undefined] { | ||
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value); | ||
if (node === null) return [undefined, undefined]; | ||
return [node, value ?? entryValue]; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function sets the root of a tree-like structure and updates the parent property of the new | ||
* root. | ||
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined. | ||
* @param {OptNode<BSTNode<K, V>>} v - v is a parameter of type BSTNode<K, V> or undefined. | ||
*/ | ||
protected override _setRoot(v: OptNode<NODE>) { | ||
protected override _setRoot(v: OptNode<BSTNode<K, V>>) { | ||
if (v) { | ||
@@ -1025,18 +1046,19 @@ v.parent = undefined; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The _compare function compares two values using a specified comparator function and optionally | ||
* reverses the result. | ||
* @param {K} a - The parameter `a` is of type `K`, which is used as an input for comparison in the | ||
* `_compare` method. | ||
* @param {K} b - The parameter `b` in the `_compare` function is of type `K`. | ||
* @returns The `_compare` method is returning the result of the ternary expression. If `_isReverse` | ||
* is true, it returns the negation of the result of calling the `_comparator` function with | ||
* arguments `a` and `b`. If `_isReverse` is false, it returns the result of calling the | ||
* `_comparator` function with arguments `a` and `b`. | ||
*/ | ||
protected _compare(a: K, b: K) { | ||
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b); | ||
} | ||
override map( | ||
callback: EntryCallback<K, V | undefined, [MK, MV]>, | ||
options?: BSTOptions<MK, MV, MR>, | ||
thisArg?: any | ||
): BST<MK, MV, MR> { | ||
const newTree = new BST<MK, MV, MR>([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
} |
@@ -9,1 +9,3 @@ export * from './binary-tree'; | ||
export * from './tree-multi-map'; | ||
export * from './tree-counter'; | ||
export * from './avl-tree-counter'; |
@@ -7,6 +7,5 @@ import type { | ||
OptNode, | ||
OptNodeOrNull, | ||
RBTNColor, | ||
RedBlackTreeOptions, | ||
RedBlackTreeNested, | ||
RedBlackTreeNodeNested | ||
RedBlackTreeOptions | ||
} from '../../types'; | ||
@@ -16,17 +15,12 @@ import { BST, BSTNode } from './bst'; | ||
export class RedBlackTreeNode< | ||
K = any, | ||
V = any, | ||
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V> | ||
> extends BSTNode<K, V, NODE> { | ||
export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> { | ||
/** | ||
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a | ||
* color. | ||
* @param {K} key - The key parameter is of type K and represents the key of the node in the | ||
* Red-Black 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 instance of the Red-Black Tree Node. | ||
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black | ||
* Tree Node. It is an optional parameter with a default value of `'BLACK'`. | ||
* The constructor initializes a node with a key, value, and color for a Red-Black Tree. | ||
* @param {K} key - The `key` parameter is a key of type `K` that is used to identify the node in a | ||
* Red-Black Tree data structure. | ||
* @param {V} [value] - The `value` parameter in the constructor is an optional parameter of type | ||
* `V`. It represents the value associated with the key in the data structure being constructed. | ||
* @param {RBTNColor} [color=BLACK] - The `color` parameter in the constructor is used to specify the | ||
* color of the node in a Red-Black Tree. It has a default value of 'BLACK' if not provided | ||
* explicitly. | ||
*/ | ||
@@ -37,2 +31,30 @@ constructor(key: K, value?: V, color: RBTNColor = 'BLACK') { | ||
} | ||
override parent?: RedBlackTreeNode<K, V> = undefined; | ||
override _left?: OptNodeOrNull<RedBlackTreeNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<RedBlackTreeNode<K, V>> { | ||
return this._left; | ||
} | ||
override set left(v: OptNodeOrNull<RedBlackTreeNode<K, V>>) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._left = v; | ||
} | ||
override _right?: OptNodeOrNull<RedBlackTreeNode<K, V>> = undefined; | ||
override get right(): OptNodeOrNull<RedBlackTreeNode<K, V>> { | ||
return this._right; | ||
} | ||
override set right(v: OptNodeOrNull<RedBlackTreeNode<K, V>>) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._right = v; | ||
} | ||
} | ||
@@ -93,35 +115,21 @@ | ||
*/ | ||
export class RedBlackTree< | ||
K = any, | ||
V = any, | ||
R = object, | ||
MK = any, | ||
MV = any, | ||
MR = object, | ||
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, | ||
TREE extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE> = RedBlackTree< | ||
K, | ||
V, | ||
R, | ||
MK, | ||
MV, | ||
MR, | ||
NODE, | ||
RedBlackTreeNested<K, V, R, MK, MV, MR, NODE> | ||
> | ||
> | ||
extends BST<K, V, R, MK, MV, MR, NODE, TREE> | ||
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> | ||
export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> | ||
extends BST<K, V, R, MK, MV, MR> | ||
implements IBinaryTree<K, V, R, MK, MV, MR> | ||
{ | ||
/** | ||
* This is the constructor function for a Red-Black Tree data structure in TypeScript. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an | ||
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to | ||
* initialize the RBTree with the provided elements. | ||
* @param [options] - The `options` parameter is an optional object that can be passed to the | ||
* constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for | ||
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would | ||
* depend on the implementation | ||
* This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or | ||
* raw data. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It | ||
* is used to initialize the Red-Black Tree with keys, nodes, entries, or | ||
* @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K, | ||
* V, R>`. It is an optional parameter that allows you to specify additional options for the | ||
* RedBlackTree class. These options could include configuration settings, behavior customization, or | ||
* any other parameters that are specific to | ||
*/ | ||
constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: RedBlackTreeOptions<K, V, R>) { | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, RedBlackTreeNode<K, V>> | R> = [], | ||
options?: RedBlackTreeOptions<K, V, R> | ||
) { | ||
super([], options); | ||
@@ -136,9 +144,5 @@ | ||
protected override _root: NODE | undefined; | ||
protected override _root: RedBlackTreeNode<K, V> | undefined; | ||
/** | ||
* The function returns the root node of a tree or undefined if there is no root. | ||
* @returns The root node of the tree structure, or undefined if there is no root node. | ||
*/ | ||
override get root(): NODE | undefined { | ||
override get root(): RedBlackTreeNode<K, V> | undefined { | ||
return this._root; | ||
@@ -148,2 +152,5 @@ } | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new Red-Black Tree node with the specified key, value, and color. | ||
@@ -162,7 +169,10 @@ * @param {K} key - The key parameter represents the key value of the node being created. It is of | ||
*/ | ||
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): NODE { | ||
return new RedBlackTreeNode<K, V, NODE>(key, this._isMapMode ? undefined : value, color) as NODE; | ||
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): RedBlackTreeNode<K, V> { | ||
return new RedBlackTreeNode<K, V>(key, this._isMapMode ? undefined : value, color); | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function creates a new Red-Black Tree with the specified options. | ||
@@ -173,4 +183,4 @@ * @param [options] - The `options` parameter is an optional object that contains additional | ||
*/ | ||
override createTree(options?: RedBlackTreeOptions<K, V, R>): TREE { | ||
return new RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE>([], { | ||
override createTree(options?: RedBlackTreeOptions<K, V, R>) { | ||
return new RedBlackTree<K, V, R, MK, MV, MR>([], { | ||
iterationType: this.iterationType, | ||
@@ -181,3 +191,3 @@ isMapMode: this._isMapMode, | ||
...options | ||
}) as TREE; | ||
}); | ||
} | ||
@@ -190,9 +200,9 @@ | ||
* The function checks if the input is an instance of the RedBlackTreeNode 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 | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `RedBlackTreeNode` class. | ||
*/ | ||
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE { | ||
return keyNodeEntryOrRaw instanceof RedBlackTreeNode; | ||
override isNode(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>): keyNodeOrEntry is RedBlackTreeNode<K, V> { | ||
return keyNodeOrEntry instanceof RedBlackTreeNode; | ||
} | ||
@@ -214,8 +224,8 @@ | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function adds a new node to a binary search tree and returns true if the node was successfully | ||
* added. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter | ||
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -228,4 +238,4 @@ * the key in the data structure. It represents the value that you want to add or update in the data | ||
*/ | ||
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean { | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); | ||
override add(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>, value?: V): boolean { | ||
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value); | ||
if (!this.isRealNode(newNode)) return false; | ||
@@ -255,21 +265,23 @@ | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The function overrides the delete method in a binary tree data structure to remove a node based on | ||
* a given predicate and maintain the binary search tree properties. | ||
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw` | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override 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, an entry, or a predicate | ||
* function that determines which node(s) should be deleted. | ||
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<NODE>` | ||
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>` | ||
* objects. Each object in the array contains information about the deleted node and whether | ||
* balancing is needed. | ||
*/ | ||
override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] { | ||
if (keyNodeEntryOrRaw === null) return []; | ||
override delete( | ||
keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>> | ||
): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[] { | ||
if (keyNodeOrEntry === null) return []; | ||
const results: BinaryTreeDeleteResult<NODE>[] = []; | ||
let nodeToDelete: OptNode<NODE>; | ||
if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw); | ||
else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw); | ||
const results: BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[] = []; | ||
let nodeToDelete: OptNode<RedBlackTreeNode<K, V>>; | ||
if (this._isPredicate(keyNodeOrEntry)) nodeToDelete = this.getNode(keyNodeOrEntry); | ||
else nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry); | ||
@@ -281,3 +293,3 @@ if (!nodeToDelete) { | ||
let originalColor = nodeToDelete.color; | ||
let replacementNode: NODE | undefined; | ||
let replacementNode: RedBlackTreeNode<K, V> | undefined; | ||
@@ -334,2 +346,49 @@ if (!this.isRealNode(nodeToDelete.left)) { | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by | ||
* applying a callback to each entry in the original tree. | ||
* @param callback - A function that will be called for each entry in the tree, with parameters | ||
* representing the key, value, index, and the tree itself. It should return an entry for the new | ||
* tree. | ||
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV, | ||
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black | ||
* Tree that will be created during the mapping process. These options could include things like | ||
* custom comparators | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function. This can be useful when you want to access properties | ||
* or | ||
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the | ||
* provided callback function. | ||
*/ | ||
override map( | ||
callback: EntryCallback<K, V | undefined, [MK, MV]>, | ||
options?: RedBlackTreeOptions<MK, MV, MR>, | ||
thisArg?: any | ||
): RedBlackTree<MK, MV, MR> { | ||
const newTree = new RedBlackTree<MK, MV, MR>([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns The `cloned` object is being returned. | ||
*/ | ||
override clone() { | ||
const cloned = this.createTree(); | ||
this._clone(cloned); | ||
return cloned; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -340,5 +399,5 @@ * Space Complexity: O(1) | ||
* root. | ||
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined. | ||
* @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined. | ||
*/ | ||
protected override _setRoot(v: NODE | undefined) { | ||
protected override _setRoot(v: RedBlackTreeNode<K, V> | undefined) { | ||
if (v) { | ||
@@ -355,5 +414,5 @@ v.parent = undefined; | ||
* The function replaces an old node with a new node while preserving the color of the old node. | ||
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in | ||
* the data structure. | ||
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a | ||
* @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a | ||
* data structure. | ||
@@ -363,3 +422,6 @@ * @returns The method is returning the result of calling the `_replaceNode` method from the | ||
*/ | ||
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE { | ||
protected override _replaceNode( | ||
oldNode: RedBlackTreeNode<K, V>, | ||
newNode: RedBlackTreeNode<K, V> | ||
): RedBlackTreeNode<K, V> { | ||
newNode.color = oldNode.color; | ||
@@ -372,7 +434,7 @@ | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to | ||
* maintain the red-black tree properties. | ||
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the | ||
* @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the | ||
* binary search tree. | ||
@@ -383,5 +445,5 @@ * @returns a string value indicating the result of the insertion operation. It can return either | ||
*/ | ||
protected _insert(node: NODE): CRUD { | ||
protected _insert(node: RedBlackTreeNode<K, V>): CRUD { | ||
let current = this.root; | ||
let parent: NODE | undefined = undefined; | ||
let parent: RedBlackTreeNode<K, V> | undefined = undefined; | ||
@@ -424,7 +486,7 @@ while (this.isRealNode(current)) { | ||
* The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree. | ||
* @param {NODE} u - The parameter "u" represents a node in a binary tree. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can | ||
* either be a `NODE` object or `undefined`. | ||
* @param {RedBlackTreeNode<K, V>} u - The parameter "u" represents a node in a binary tree. | ||
* @param {RedBlackTreeNode<K, V> | undefined} v - The parameter `v` is of type `RedBlackTreeNode<K, V> | undefined`, which means it can | ||
* either be a `RedBlackTreeNode<K, V>` object or `undefined`. | ||
*/ | ||
protected _transplant(u: NODE, v: NODE | undefined): void { | ||
protected _transplant(u: RedBlackTreeNode<K, V>, v: RedBlackTreeNode<K, V> | undefined): void { | ||
if (!u.parent) { | ||
@@ -448,6 +510,6 @@ this._setRoot(v); | ||
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node. | ||
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data | ||
* @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data | ||
* structure. It can either be a valid node or `undefined`. | ||
*/ | ||
protected _insertFixup(z: NODE | undefined): void { | ||
protected _insertFixup(z: RedBlackTreeNode<K, V> | undefined): void { | ||
// Continue fixing the tree as long as the parent of z is red | ||
@@ -485,3 +547,3 @@ while (z?.parent?.color === 'RED') { | ||
// Follow the same logic as above with left and right exchanged | ||
const y: NODE | undefined = z?.parent?.parent?.left ?? undefined; | ||
const y: RedBlackTreeNode<K, V> | undefined = z?.parent?.parent?.left ?? undefined; | ||
if (y?.color === 'RED') { | ||
@@ -517,3 +579,3 @@ z.parent.color = 'BLACK'; | ||
* the colors and performing rotations. | ||
* @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can | ||
* @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can | ||
* be either a valid node object or `undefined`. | ||
@@ -523,3 +585,3 @@ * @returns The function does not return any value. It has a return type of `void`, which means it | ||
*/ | ||
protected _deleteFixup(node: NODE | undefined): void { | ||
protected _deleteFixup(node: RedBlackTreeNode<K, V> | undefined): void { | ||
// Early exit condition | ||
@@ -534,3 +596,3 @@ if (!node || node === this.root || node.color === 'BLACK') { | ||
while (node && node !== this.root && node.color === 'BLACK') { | ||
const parent: NODE | undefined = node.parent; | ||
const parent: RedBlackTreeNode<K, V> | undefined = node.parent; | ||
@@ -602,7 +664,7 @@ if (!parent) { | ||
* The `_leftRotate` function performs a left rotation on a given node in a binary tree. | ||
* @param {NODE | undefined} x - The parameter `x` is of type `NODE | undefined`. It represents a | ||
* @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a | ||
* node in a binary tree or `undefined` if there is no node. | ||
* @returns void, which means it does not return any value. | ||
*/ | ||
protected _leftRotate(x: NODE | undefined): void { | ||
protected _leftRotate(x: RedBlackTreeNode<K, V> | undefined): void { | ||
if (!x || !x.right) { | ||
@@ -638,7 +700,7 @@ return; | ||
* The `_rightRotate` function performs a right rotation on a given node in a binary tree. | ||
* @param {NODE | undefined} y - The parameter `y` is of type `NODE | undefined`. It represents a | ||
* @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a | ||
* node in a binary tree or `undefined` if there is no node. | ||
* @returns void, which means it does not return any value. | ||
*/ | ||
protected _rightRotate(y: NODE | undefined): void { | ||
protected _rightRotate(y: RedBlackTreeNode<K, V> | undefined): void { | ||
if (!y || !y.left) { | ||
@@ -668,35 +730,2 @@ return; | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by | ||
* applying a callback to each entry in the original tree. | ||
* @param callback - A function that will be called for each entry in the tree, with parameters | ||
* representing the key, value, index, and the tree itself. It should return an entry for the new | ||
* tree. | ||
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV, | ||
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black | ||
* Tree that will be created during the mapping process. These options could include things like | ||
* custom comparators | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function. This can be useful when you want to access properties | ||
* or | ||
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the | ||
* provided callback function. | ||
*/ | ||
override map( | ||
callback: EntryCallback<K, V | undefined, [MK, MV]>, | ||
options?: RedBlackTreeOptions<MK, MV, MR>, | ||
thisArg?: any | ||
): RedBlackTree<MK, MV, MR> { | ||
const newTree = new RedBlackTree<MK, MV, MR>([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
} |
@@ -8,489 +8,221 @@ /** | ||
*/ | ||
import type { | ||
BinaryTreeDeleteResult, | ||
BSTNOptKeyOrNode, | ||
BTNRep, | ||
EntryCallback, | ||
IterationType, | ||
OptNode, | ||
RBTNColor, | ||
TreeMultiMapNested, | ||
TreeMultiMapNodeNested, | ||
TreeMultiMapOptions | ||
} from '../../types'; | ||
import type { BTNOptKeyOrNull, BTNRep, OptNodeOrNull, TreeMultiMapOptions } from '../../types'; | ||
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree'; | ||
export class TreeMultiMapNode< | ||
K = any, | ||
V = any, | ||
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V> | ||
> extends RedBlackTreeNode<K, V, NODE> { | ||
export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> { | ||
/** | ||
* 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'`. | ||
* This TypeScript constructor initializes an object with a key of type K and an array of values of | ||
* type V. | ||
* @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the | ||
* data being stored in the data structure. It helps in quickly accessing or retrieving the | ||
* associated value in the data structure. | ||
* @param {V[]} value - The `value` parameter in the constructor represents an array of values of | ||
* type `V`. | ||
*/ | ||
constructor(key: K, value?: V, count = 1, color: RBTNColor = 'BLACK') { | ||
super(key, value, color); | ||
this.count = count; | ||
constructor(key: K, value: V[]) { | ||
super(key, value); | ||
} | ||
} | ||
export class TreeMultiMap< | ||
K = any, | ||
V = any, | ||
R = object, | ||
MK = any, | ||
MV = any, | ||
MR = object, | ||
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, | ||
TREE extends TreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = TreeMultiMap< | ||
K, | ||
V, | ||
R, | ||
MK, | ||
MV, | ||
MR, | ||
NODE, | ||
TreeMultiMapNested<K, V, R, MK, MV, MR, NODE> | ||
> | ||
> | ||
extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE> | ||
implements IBinaryTree<K, V, R, MK, MV, MR, 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>) { | ||
super([], options); | ||
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws); | ||
override parent?: TreeMultiMapNode<K, V> = undefined; | ||
override _left?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<TreeMultiMapNode<K, V>> { | ||
return this._left; | ||
} | ||
protected _count = 0; | ||
override set left(v: OptNodeOrNull<TreeMultiMapNode<K, V>>) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._left = v; | ||
} | ||
// TODO the _count is not accurate after nodes count modified | ||
/** | ||
* 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 { | ||
return this._count; | ||
override _right?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined; | ||
override get right(): OptNodeOrNull<TreeMultiMapNode<K, V>> { | ||
return this._right; | ||
} | ||
/** | ||
* 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 { | ||
let sum = 0; | ||
this.dfs(node => (sum += node.count)); | ||
return sum; | ||
override set right(v: OptNodeOrNull<TreeMultiMapNode<K, V>>) { | ||
if (v) { | ||
v.parent = this; | ||
} | ||
this._right = v; | ||
} | ||
} | ||
/** | ||
* | ||
* @example | ||
* // Find elements in a range | ||
* const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(tmm.search(new Range(5, 10))); // [5, 10, 7] | ||
* console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7] | ||
* console.log(tmm.search(new Range(15, 20))); // [15, 18] | ||
*/ | ||
export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object> | ||
extends RedBlackTree<K, V[], R, MK, MV[], MR> | ||
implements IBinaryTree<K, V[], R, MK, MV, MR> | ||
{ | ||
/** | ||
* 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. | ||
* The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data | ||
* and options. | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either key-value pairs represented as `BTNRep<K, V[], | ||
* TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize | ||
* the RedBlackTreeMulti | ||
* @param [options] - The `options` parameter in the constructor is of type | ||
* `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify | ||
* additional options for configuring the TreeMultiMap instance. | ||
*/ | ||
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): NODE { | ||
return new TreeMultiMapNode(key, this._isMapMode ? undefined : value, count, color) as NODE; | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], TreeMultiMapNode<K, V>> | R> = [], | ||
options?: TreeMultiMapOptions<K, V[], R> | ||
) { | ||
super([], { ...options, isMapMode: true }); | ||
if (keysNodesEntriesOrRaws) { | ||
this.addMany(keysNodesEntriesOrRaws); | ||
} | ||
} | ||
/** | ||
* 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`. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `createTree` function in TypeScript overrides the default implementation to create a new | ||
* TreeMultiMap with specified options. | ||
* @param [options] - The `options` parameter in the `createTree` method is of type | ||
* `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration | ||
* options when creating a new `TreeMultiMap` instance. It includes properties such as | ||
* `iterationType`, `specifyComparable | ||
* @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial | ||
* data and the provided options merged with the existing properties of the current object. | ||
*/ | ||
override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE { | ||
return new TreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE>([], { | ||
override createTree(options?: TreeMultiMapOptions<K, V[], R>) { | ||
return new TreeMultiMap<K, V, R, MK, MV, MR>([], { | ||
iterationType: this.iterationType, | ||
isMapMode: this._isMapMode, | ||
specifyComparable: this._specifyComparable, | ||
toEntryFn: this._toEntryFn, | ||
isReverse: this._isReverse, | ||
...options | ||
}) as 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. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `createNode` overrides the method to create a new `TreeMultiMapNode` with a specified | ||
* key and an empty array of values. | ||
* @param {K} key - The `key` parameter in the `createNode` method represents the key of the node | ||
* that will be created in the TreeMultiMap data structure. | ||
* @returns A new instance of `TreeMultiMapNode<K, V>` is being returned, with the specified key and | ||
* an empty array as its value. | ||
*/ | ||
protected override _keyValueNodeEntryRawToNodeAndValue( | ||
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, | ||
value?: V, | ||
count = 1 | ||
): [NODE | undefined, V | undefined] { | ||
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined]; | ||
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value]; | ||
if (this.isEntry(keyNodeEntryOrRaw)) { | ||
const [key, entryValue] = keyNodeEntryOrRaw; | ||
if (key === undefined || key === null) return [undefined, undefined]; | ||
const finalValue = value ?? entryValue; | ||
if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue]; | ||
} | ||
if (this.isRaw(keyNodeEntryOrRaw)) { | ||
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw); | ||
const finalValue = value ?? entryValue; | ||
if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue]; | ||
} | ||
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value]; | ||
return [undefined, undefined]; | ||
override createNode(key: K): TreeMultiMapNode<K, V> { | ||
return new TreeMultiMapNode<K, V>(key, []); | ||
} | ||
/** | ||
* 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. | ||
*/ | ||
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE { | ||
return keyNodeEntryOrRaw instanceof TreeMultiMapNode; | ||
} | ||
override add(node: BTNRep<K, V[], TreeMultiMapNode<K, V>>): boolean; | ||
/** | ||
* 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. | ||
*/ | ||
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean { | ||
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count); | ||
const orgCount = newNode?.count || 0; | ||
const isSuccessAdded = super.add(newNode, newValue); | ||
override add(key: K, value: V): boolean; | ||
if (isSuccessAdded) { | ||
this._count += orgCount; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* 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. | ||
* The function `add` in TypeScript overrides the superclass method to add key-value pairs to a | ||
* TreeMultiMapNode, handling different input types and scenarios. | ||
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override add` method can be either a `BTNRep` object containing a key, an array | ||
* of values, and a `TreeMultiMapNode`, or just a key. | ||
* @param {V} [value] - The `value` parameter in the `override add` method represents the value that | ||
* you want to add to the TreeMultiMap. If the key is already present in the map, the new value will | ||
* be added to the existing list of values associated with that key. If the key is not present, | ||
* @returns The `add` method is returning a boolean value, which indicates whether the operation was | ||
* successful or not. | ||
*/ | ||
override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, ignoreCount = false): BinaryTreeDeleteResult<NODE>[] { | ||
if (keyNodeEntryOrRaw === null) return []; | ||
override add(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value?: V): boolean { | ||
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry); | ||
const results: BinaryTreeDeleteResult<NODE>[] = []; | ||
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => { | ||
if (key === undefined || key === null) return false; | ||
let nodeToDelete: OptNode<NODE>; | ||
if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw); | ||
else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw); | ||
if (!nodeToDelete) { | ||
return results; | ||
} | ||
let originalColor = nodeToDelete.color; | ||
let replacementNode: NODE | undefined; | ||
if (!this.isRealNode(nodeToDelete.left)) { | ||
if (nodeToDelete.right !== null) replacementNode = nodeToDelete.right; | ||
if (ignoreCount || nodeToDelete.count <= 1) { | ||
if (nodeToDelete.right !== null) { | ||
this._transplant(nodeToDelete, nodeToDelete.right); | ||
this._count -= nodeToDelete.count; | ||
} | ||
} else { | ||
nodeToDelete.count--; | ||
this._count--; | ||
results.push({ deleted: nodeToDelete, needBalanced: undefined }); | ||
return results; | ||
const existingValues = this.get(key); | ||
if (existingValues !== undefined && values !== undefined) { | ||
for (const value of values) existingValues.push(value); | ||
return true; | ||
} | ||
} else if (!this.isRealNode(nodeToDelete.right)) { | ||
replacementNode = nodeToDelete.left; | ||
if (ignoreCount || nodeToDelete.count <= 1) { | ||
this._transplant(nodeToDelete, nodeToDelete.left); | ||
this._count -= nodeToDelete.count; | ||
} else { | ||
nodeToDelete.count--; | ||
this._count--; | ||
results.push({ deleted: nodeToDelete, needBalanced: undefined }); | ||
return results; | ||
} | ||
} else { | ||
const successor = this.getLeftMost(node => node, nodeToDelete.right); | ||
if (successor) { | ||
originalColor = successor.color; | ||
if (successor.right !== null) replacementNode = successor.right; | ||
if (successor.parent === nodeToDelete) { | ||
if (this.isRealNode(replacementNode)) { | ||
replacementNode.parent = successor; | ||
} | ||
} else { | ||
if (ignoreCount || nodeToDelete.count <= 1) { | ||
if (successor.right !== null) { | ||
this._transplant(successor, successor.right); | ||
this._count -= nodeToDelete.count; | ||
} | ||
} else { | ||
nodeToDelete.count--; | ||
this._count--; | ||
results.push({ deleted: nodeToDelete, needBalanced: undefined }); | ||
return results; | ||
} | ||
successor.right = nodeToDelete.right; | ||
if (this.isRealNode(successor.right)) { | ||
successor.right.parent = successor; | ||
} | ||
const existingNode = this.getNode(key); | ||
if (this.isRealNode(existingNode)) { | ||
if (existingValues === undefined) { | ||
super.add(key, values); | ||
return true; | ||
} | ||
if (ignoreCount || nodeToDelete.count <= 1) { | ||
this._transplant(nodeToDelete, successor); | ||
this._count -= nodeToDelete.count; | ||
if (values !== undefined) { | ||
for (const value of values) existingValues.push(value); | ||
return true; | ||
} else { | ||
nodeToDelete.count--; | ||
this._count--; | ||
results.push({ deleted: nodeToDelete, needBalanced: undefined }); | ||
return results; | ||
return false; | ||
} | ||
successor.left = nodeToDelete.left; | ||
if (this.isRealNode(successor.left)) { | ||
successor.left.parent = successor; | ||
} | ||
successor.color = nodeToDelete.color; | ||
} else { | ||
return super.add(key, values); | ||
} | ||
} | ||
this._size--; | ||
}; | ||
// If the original color was black, fix the tree | ||
if (originalColor === 'BLACK') { | ||
this._deleteFixup(replacementNode); | ||
if (this.isEntry(keyNodeOrEntry)) { | ||
const [key, values] = keyNodeOrEntry; | ||
return _commonAdd(key, value !== undefined ? [value] : values); | ||
} | ||
results.push({ deleted: nodeToDelete, needBalanced: undefined }); | ||
return results; | ||
return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined); | ||
} | ||
/** | ||
* 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. | ||
*/ | ||
override clear() { | ||
super.clear(); | ||
this._count = 0; | ||
} | ||
/** | ||
* Time Complexity: O(n log n) | ||
* Time Complexity: O(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. | ||
* The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure | ||
* and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an | ||
* array of values, or just a key itself. | ||
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific | ||
* value that you want to remove from the multi-map data structure associated with a particular key. | ||
* The function checks if the value exists in the array of values associated with the key, and if | ||
* found, removes it from the array. | ||
* @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was | ||
* successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise. | ||
*/ | ||
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean { | ||
const sorted = this.dfs(node => node, 'IN'), | ||
n = sorted.length; | ||
if (sorted.length < 1) return false; | ||
deleteValue(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value: V): boolean { | ||
const values = this.get(keyNodeOrEntry); | ||
if (Array.isArray(values)) { | ||
const index = values.indexOf(value); | ||
if (index === -1) return false; | ||
values.splice(index, 1); | ||
this.clear(); | ||
// If no values left, remove the entire node | ||
if (values.length === 0) this.delete(keyNodeOrEntry); | ||
if (iterationType === 'RECURSIVE') { | ||
const buildBalanceBST = (l: number, r: number) => { | ||
if (l > r) return; | ||
const m = l + Math.floor((r - l) / 2); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count); | ||
else this.add(midNode.key, midNode.value, midNode.count); | ||
buildBalanceBST(l, m - 1); | ||
buildBalanceBST(m + 1, r); | ||
}; | ||
buildBalanceBST(0, n - 1); | ||
return true; | ||
} else { | ||
const stack: [[number, number]] = [[0, n - 1]]; | ||
while (stack.length > 0) { | ||
const popped = stack.pop(); | ||
if (popped) { | ||
const [l, r] = popped; | ||
if (l <= r) { | ||
const m = l + Math.floor((r - l) / 2); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count); | ||
else this.add(midNode.key, midNode.value, midNode.count); | ||
stack.push([m + 1, r]); | ||
stack.push([l, m - 1]); | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* 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. | ||
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree | ||
* structure. | ||
* @returns The `cloned` object is being returned. | ||
*/ | ||
override clone(): TREE { | ||
override clone() { | ||
const cloned = this.createTree(); | ||
this.bfs(node => cloned.add(node.key, undefined, node.count)); | ||
if (this._isMapMode) cloned._store = this._store; | ||
this._clone(cloned); | ||
return cloned; | ||
} | ||
/** | ||
* 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 override _swapProperties( | ||
srcNode: R | BSTNOptKeyOrNode<K, NODE>, | ||
destNode: R | BSTNOptKeyOrNode<K, NODE> | ||
): NODE | undefined { | ||
srcNode = this.ensureNode(srcNode); | ||
destNode = this.ensureNode(destNode); | ||
if (srcNode && destNode) { | ||
const { key, value, count, color } = destNode; | ||
const tempNode = this.createNode(key, value, color, count); | ||
if (tempNode) { | ||
tempNode.color = color; | ||
destNode.key = srcNode.key; | ||
if (!this._isMapMode) destNode.value = srcNode.value; | ||
destNode.count = srcNode.count; | ||
destNode.color = srcNode.color; | ||
srcNode.key = tempNode.key; | ||
if (!this._isMapMode) srcNode.value = tempNode.value; | ||
srcNode.count = tempNode.count; | ||
srcNode.color = tempNode.color; | ||
} | ||
return destNode; | ||
} | ||
return 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 override _replaceNode(oldNode: NODE, newNode: NODE): NODE { | ||
newNode.count = oldNode.count + newNode.count; | ||
return super._replaceNode(oldNode, newNode); | ||
} | ||
/** | ||
* The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with | ||
* modified entries based on a provided callback. | ||
* @param callback - The `callback` parameter is a function that will be called for each entry in the | ||
* map. It takes four arguments: | ||
* @param [options] - The `options` parameter in the `override map` function is of type | ||
* `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration | ||
* options when creating a new `TreeMultiMap` instance within the `map` function. These options could | ||
* include things like | ||
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify | ||
* the value of `this` when executing the `callback` function. It allows you to set the context | ||
* (value of `this`) for the callback function when it is called within the `map` function. This | ||
* @returns A new TreeMultiMap instance is being returned, which is populated with entries generated | ||
* by the provided callback function. | ||
*/ | ||
override map( | ||
callback: EntryCallback<K, V | undefined, [MK, MV]>, | ||
options?: TreeMultiMapOptions<MK, MV, MR>, | ||
thisArg?: any | ||
): TreeMultiMap<MK, MV, MR> { | ||
const newTree = new TreeMultiMap<MK, MV, MR>([], options); | ||
let index = 0; | ||
for (const [key, value] of this) { | ||
newTree.add(callback.call(thisArg, key, value, index++, this)); | ||
} | ||
return newTree; | ||
} | ||
} |
@@ -48,2 +48,5 @@ /** | ||
/** | ||
* | ||
*/ | ||
export class DirectedGraph< | ||
@@ -50,0 +53,0 @@ V = any, |
@@ -43,2 +43,5 @@ import type { MapGraphCoordinate, VertexKey } from '../../types'; | ||
/** | ||
* | ||
*/ | ||
export class MapGraph< | ||
@@ -45,0 +48,0 @@ V = any, |
@@ -45,2 +45,5 @@ /** | ||
/** | ||
* | ||
*/ | ||
export class UndirectedGraph< | ||
@@ -47,0 +50,0 @@ V = any, |
@@ -62,2 +62,5 @@ /** | ||
/** | ||
* | ||
*/ | ||
export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> { | ||
@@ -64,0 +67,0 @@ constructor( |
@@ -22,2 +22,5 @@ /** | ||
/** | ||
* | ||
*/ | ||
export class SkipList<K, V> { | ||
@@ -24,0 +27,0 @@ /** |
@@ -10,2 +10,5 @@ /** | ||
/** | ||
* | ||
*/ | ||
export class Matrix { | ||
@@ -12,0 +15,0 @@ /** |
@@ -28,2 +28,5 @@ /** | ||
/** | ||
* | ||
*/ | ||
export class Navigator<T = number> { | ||
@@ -30,0 +33,0 @@ onMove: (cur: [number, number]) => void; |
@@ -11,2 +11,5 @@ /** | ||
/** | ||
* | ||
*/ | ||
export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> { | ||
@@ -13,0 +16,0 @@ /** |
@@ -11,2 +11,5 @@ /** | ||
/** | ||
* | ||
*/ | ||
export class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> { | ||
@@ -13,0 +16,0 @@ /** |
@@ -11,6 +11,2 @@ /** | ||
/** | ||
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes, | ||
* and a flag indicating whether it's the end of a word. | ||
*/ | ||
export class TrieNode { | ||
@@ -17,0 +13,0 @@ constructor(key: string) { |
@@ -1,30 +0,16 @@ | ||
import { BinaryTree, BinaryTreeNode } from '../data-structures'; | ||
import type { | ||
BinaryTreeDeleteResult, | ||
BinaryTreeNested, | ||
BinaryTreeNodeNested, | ||
BinaryTreeOptions, | ||
BTNRep, | ||
NodePredicate | ||
} from '../types'; | ||
import { BinaryTreeNode } from '../data-structures'; | ||
import type { BinaryTreeDeleteResult, BinaryTreeOptions, BTNRep, NodePredicate } from '../types'; | ||
export interface IBinaryTree< | ||
K = any, | ||
V = any, | ||
R = object, | ||
MK = any, | ||
MV = any, | ||
MR = object, | ||
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>, | ||
TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTreeNested<K, V, R, MK, MV, MR, NODE> | ||
> { | ||
createNode(key: K, value?: NODE['value']): NODE; | ||
export interface IBinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> { | ||
createNode(key: K, value?: BinaryTreeNode['value']): BinaryTreeNode; | ||
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE; | ||
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R, MK, MV, MR>; | ||
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, NODE>, value?: V, count?: number): boolean; | ||
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean; | ||
addMany(nodes: Iterable<BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[]; | ||
addMany(nodes: Iterable<BTNRep<K, V, BinaryTreeNode<K, V>>>, values?: Iterable<V | undefined>): boolean[]; | ||
delete(predicate: R | BTNRep<K, V, NODE> | NodePredicate<NODE>): BinaryTreeDeleteResult<NODE>[]; | ||
delete( | ||
predicate: R | BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>> | ||
): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[]; | ||
} |
@@ -1,8 +0,3 @@ | ||
import { AVLTreeMultiMap, AVLTreeMultiMapNode } from '../../../data-structures'; | ||
import type { AVLTreeOptions } from './avl-tree'; | ||
export type AVLTreeMultiMapNodeNested<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>> | ||
export type AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE extends AVLTreeMultiMapNode<K, V, NODE>> = AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, any>>> | ||
export type AVLTreeMultiMapOptions<K, V, R> = AVLTreeOptions<K, V, R> & {} | ||
export type AVLTreeMultiMapOptions<K, V, R> = Omit<AVLTreeOptions<K, V, R>, 'isMapMode'> & {} |
@@ -1,8 +0,3 @@ | ||
import { AVLTree, AVLTreeNode } from '../../../data-structures'; | ||
import { BSTOptions } from './bst'; | ||
export type AVLTreeNodeNested<K, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, any>>> | ||
export type AVLTreeNested<K, V, R,MK, MV, MR, NODE extends AVLTreeNode<K, V, NODE>> = AVLTree<K, V, R,MK, MV, MR, NODE, AVLTree<K, V, R,MK, MV, MR, NODE, AVLTree<K, V, R,MK, MV, MR, NODE, any>>> | ||
export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R> & {}; |
@@ -1,9 +0,4 @@ | ||
import { BinaryTree, BinaryTreeNode } from '../../../data-structures'; | ||
import { IterationType, OptValue } from '../../common'; | ||
import { DFSOperation } from '../../../common'; | ||
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>> | ||
export type BinaryTreeNested<K, V, R, MK, MV, MR, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, MK, MV, MR, NODE,BinaryTree<K, V, R, MK, MV, MR, NODE,BinaryTree<K, V, R, MK, MV, MR, NODE,any>>> | ||
export type ToEntryFn<K, V, R> = (rawElement: R) => BTNEntry<K, V>; | ||
@@ -10,0 +5,0 @@ |
@@ -1,9 +0,5 @@ | ||
import { BST, BSTNode } from '../../../data-structures'; | ||
import type { BinaryTreeOptions } from './binary-tree'; | ||
import { Comparable } from '../../utils'; | ||
import { OptValue } from '../../common'; | ||
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>> | ||
export type BSTNested<K, V, R,MK, MV, MR, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R,MK, MV, MR, NODE,BST<K, V, R,MK, MV, MR, NODE,BST<K, V, R,MK, MV, MR, NODE, any>>> | ||
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & { | ||
@@ -18,3 +14,7 @@ specifyComparable?: (key: K) => Comparable | ||
export type BSTNEntry<K, V> = [BSTNOptKey<K>, OptValue<V>]; | ||
export type BSTNOptKeyOrNode<K, NODE> = BSTNOptKey<K> | NODE; | ||
export type BSTNRep<K, V, NODE> = BSTNEntry<K, V> | BSTNOptKeyOrNode<K, NODE>; | ||
@@ -8,1 +8,3 @@ export * from './binary-tree'; | ||
export * from './tree-multi-map'; | ||
export * from './tree-counter'; | ||
export * from './avl-tree-counter'; |
@@ -1,10 +0,5 @@ | ||
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures'; | ||
import type { BSTOptions } from "./bst"; | ||
import type { BSTOptions } from './bst'; | ||
export type RBTNColor = 'RED' | 'BLACK'; | ||
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>> | ||
export type RedBlackTreeNested<K, V, R, MK, MV, MR, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTree<K, V, R, MK, MV, MR, NODE, any>>> | ||
export type RedBlackTreeOptions<K, V, R> = BSTOptions<K, V, R> & {}; |
@@ -1,8 +0,3 @@ | ||
import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures'; | ||
import type { RedBlackTreeOptions } from './rb-tree'; | ||
export type TreeMultiMapNodeNested<K, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, any>>> | ||
export type TreeMultiMapNested<K, V, R, MK, MV, MR, NODE extends TreeMultiMapNode<K, V, NODE>> = TreeMultiMap<K, V, R, MK, MV, MR, NODE, TreeMultiMap<K, V, R, MK, MV, MR, NODE,TreeMultiMap<K, V, R, MK, MV, MR, NODE, any>>> | ||
export type TreeMultiMapOptions<K, V, R> = RedBlackTreeOptions<K, V, R> & {} | ||
export type TreeMultiMapOptions<K, V, R> = Omit<RedBlackTreeOptions<K, V, R>, 'isMapMode'> & {} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
2394701
367
45445
Updateddata-structure-typed@^1.54.1