From ec8242044a68c5d8dbed87bb5584c9861f972f46 Mon Sep 17 00:00:00 2001 From: Revone Date: Sat, 16 Mar 2024 09:47:22 +0800 Subject: [PATCH] refactor: Renamed some type aliases --- .../binary-tree/avl-tree-multi-map.ts | 24 +- src/data-structures/binary-tree/avl-tree.ts | 22 +- .../binary-tree/binary-tree.ts | 298 +++++++++--------- src/data-structures/binary-tree/bst.ts | 85 ++--- src/data-structures/binary-tree/rb-tree.ts | 20 +- .../binary-tree/tree-multi-map.ts | 20 +- src/interfaces/binary-tree.ts | 6 +- src/types/common.ts | 26 +- .../binary-tree/binary-tree.ts | 22 +- src/types/data-structures/binary-tree/bst.ts | 7 + 10 files changed, 271 insertions(+), 259 deletions(-) diff --git a/src/data-structures/binary-tree/avl-tree-multi-map.ts b/src/data-structures/binary-tree/avl-tree-multi-map.ts index 29ae882..0b39f1a 100644 --- a/src/data-structures/binary-tree/avl-tree-multi-map.ts +++ b/src/data-structures/binary-tree/avl-tree-multi-map.ts @@ -12,8 +12,8 @@ import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, - IterationType, - KeyOrNodeOrEntry + BTNKeyOrNodeOrEntry, + IterationType } from '../../types'; import { BTNEntry } from '../../types'; import { IBinaryTree } from '../../interfaces'; @@ -86,7 +86,7 @@ export class AVLTreeMultiMap< * `compareValues` functions to define custom comparison logic for keys and values, respectively. */ constructor( - keysOrNodesOrEntriesOrRawElements: Iterable> = [], + keysOrNodesOrEntriesOrRawElements: Iterable> = [], options?: AVLTreeMultiMapOptions ) { super([], options); @@ -155,13 +155,13 @@ export class AVLTreeMultiMap< /** * The function checks if the input is an instance of AVLTreeMultiMapNode. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry`. * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is * an instance of the `AVLTreeMultiMapNode` class. */ override isNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry ): keyOrNodeOrEntryOrRawElement is NODE { return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode; } @@ -169,8 +169,8 @@ export class AVLTreeMultiMap< /** * The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into * a node object. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The - * `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The + * `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `BTNKeyOrNodeOrEntry`. * @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`. @@ -179,7 +179,7 @@ export class AVLTreeMultiMap< * @returns either a NODE object or undefined. */ override keyValueOrEntryOrRawElementToNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V, count = 1 ): NODE | undefined { @@ -213,9 +213,9 @@ export class AVLTreeMultiMap< * * The function overrides the add method of a TypeScript class to add a new node to a data structure * and update the count. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It - * can also accept a value of type `KeyOrNodeOrEntry`, which represents a key, node, + * can also accept a value of type `BTNKeyOrNodeOrEntry`, 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. @@ -224,7 +224,7 @@ export class AVLTreeMultiMap< * be added once. However, you can specify a different value for `count` if you want to add * @returns a boolean value. */ - override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, value?: V, count = 1): boolean { + override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V, count = 1): boolean { const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count); if (newNode === undefined) return false; diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index ebcf26c..d00666b 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -13,7 +13,7 @@ import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, - KeyOrNodeOrEntry + BTNKeyOrNodeOrEntry } from '../../types'; import { BTNEntry } from '../../types'; import { IBinaryTree } from '../../interfaces'; @@ -86,7 +86,7 @@ export class AVLTree< * `nodeBuilder` ( */ constructor( - keysOrNodesOrEntriesOrRawElements: Iterable> = [], + keysOrNodesOrEntriesOrRawElements: Iterable> = [], options?: AVLTreeOptions ) { super([], options); @@ -123,13 +123,13 @@ export class AVLTree< /** * The function checks if the input is an instance of AVLTreeNode. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry`. * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is * an instance of the `AVLTreeNode` class. */ override isNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry ): keyOrNodeOrEntryOrRawElement is NODE { return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode; } @@ -146,14 +146,14 @@ export class AVLTree< * * The function overrides the add method of a class and inserts a key-value pair into a data * structure, then balances the path. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `KeyOrNodeOrEntry`, or + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `BTNKeyOrNodeOrEntry`, or * `RawElement`. * @param {V} [value] - The `value` parameter is an optional value that you want to associate with * the key or node being added to the data structure. * @returns The method is returning a boolean value. */ - override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, value?: V): boolean { + override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V): boolean { if (keyOrNodeOrEntryOrRawElement === null) return false; const inserted = super.add(keyOrNodeOrEntryOrRawElement, value); if (inserted) this._balancePath(keyOrNodeOrEntryOrRawElement); @@ -488,10 +488,10 @@ export class AVLTree< * * The `_balancePath` function is used to update the heights of nodes and perform rotation operations * to restore balance in an AVL tree after inserting a node. - * @param {R | KeyOrNodeOrEntry} node - The `node` parameter can be of type `R` or - * `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} node - The `node` parameter can be of type `R` or + * `BTNKeyOrNodeOrEntry`. */ - protected _balancePath(node: R | KeyOrNodeOrEntry): void { + protected _balancePath(node: R | BTNKeyOrNodeOrEntry): void { node = this.ensureNode(node); const path = this.getPathToRoot(node, false); // first O(log n) + O(log n) for (let i = 0; i < path.length; i++) { diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 54982b2..9828d25 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -14,12 +14,13 @@ import type { BinaryTreePrintOptions, BTNCallback, BTNEntry, + BTNKeyOrNodeOrEntry, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, - KeyOrNodeOrEntry, - NodeDisplayLayout + NodeDisplayLayout, + OptBTNOrNull } from '../../types'; import { IBinaryTree } from '../../interfaces'; import { trampoline } from '../../utils'; @@ -62,16 +63,16 @@ export class BinaryTreeNode< * @returns The left node of the current node is being returned. It can be either a NODE object, * null, or undefined. */ - get left(): NODE | null | undefined { + get left(): OptBTNOrNull { return this._left; } /** * The function sets the left child of a node and updates its parent reference. - * @param {NODE | null | undefined} v - The parameter `v` can be of type `NODE`, `null`, or + * @param {OptBTNOrNull} v - The parameter `v` can be of type `NODE`, `null`, or * `undefined`. */ - set left(v: NODE | null | undefined) { + set left(v: OptBTNOrNull) { if (v) { v.parent = this as unknown as NODE; } @@ -85,16 +86,16 @@ export class BinaryTreeNode< * @returns The method is returning the value of the `_right` property, which can be a `NODE` object, * `null`, or `undefined`. */ - get right(): NODE | null | undefined { + get right(): OptBTNOrNull { return this._right; } /** * The function sets the right child of a node and updates its parent. - * @param {NODE | null | undefined} v - The parameter `v` can be of type `NODE`, `null`, or + * @param {OptBTNOrNull} v - The parameter `v` can be of type `NODE`, `null`, or * `undefined`. */ - set right(v: NODE | null | undefined) { + set right(v: OptBTNOrNull) { if (v) { v.parent = this as unknown as NODE; } @@ -142,7 +143,7 @@ export class BinaryTree< /** * The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options. - * @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of KeyOrNodeOrEntry objects. These objects represent the + * @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of BTNKeyOrNodeOrEntry objects. These objects represent the * nodes to be added to the binary tree. * @param [options] - The `options` parameter is an optional object that can contain additional * configuration options for the binary tree. In this case, it is of type @@ -150,7 +151,7 @@ export class BinaryTree< * required. */ constructor( - keysOrNodesOrEntriesOrRawElements: Iterable> = [], + keysOrNodesOrEntriesOrRawElements: Iterable> = [], options?: BinaryTreeOptions ) { super(); @@ -164,14 +165,14 @@ export class BinaryTree< if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements); } - protected _root?: NODE | null; + protected _root?: OptBTNOrNull; /** * The function returns the root node, which can be of type NODE, null, or undefined. * @returns The method is returning the value of the `_root` property, which can be of type `NODE`, * `null`, or `undefined`. */ - get root(): NODE | null | undefined { + get root(): OptBTNOrNull { return this._root; } @@ -229,8 +230,8 @@ export class BinaryTree< /** * The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element * into a node object. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry`. * @param {V} [value] - The `value` parameter is an optional value that can be passed to the * `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a * key-value pair. If provided, it will be used to create a node with the specified key and value. @@ -238,9 +239,9 @@ export class BinaryTree< * or `undefined`. */ keyValueOrEntryOrRawElementToNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V - ): NODE | null | undefined { + ): OptBTNOrNull { if (keyOrNodeOrEntryOrRawElement === undefined) return; if (keyOrNodeOrEntryOrRawElement === null) return null; @@ -275,8 +276,8 @@ export class BinaryTree< * * The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a * node if it is a key or entry. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `KeyOrNodeOrEntry`, or + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `BTNKeyOrNodeOrEntry`, or * a raw element. * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional * parameter that specifies the type of iteration to be used when searching for a node. It has a @@ -284,9 +285,9 @@ export class BinaryTree< * @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`. */ ensureNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, iterationType: IterationType = 'ITERATIVE' - ): NODE | null | undefined { + ): OptBTNOrNull { if (keyOrNodeOrEntryOrRawElement === null) return null; if (keyOrNodeOrEntryOrRawElement === undefined) return; if (keyOrNodeOrEntryOrRawElement === this.NIL) return; @@ -310,55 +311,55 @@ export class BinaryTree< /** * The function checks if the input is an instance of the BinaryTreeNode class. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry`. * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is * an instance of the `BinaryTreeNode` class. */ - isNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry): keyOrNodeOrEntryOrRawElement is NODE { + isNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry): keyOrNodeOrEntryOrRawElement is NODE { return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode; } /** * The function checks if a given node is a valid node in a binary search tree. - * @param {R | KeyOrNodeOrEntry} node - The parameter `node` can be of type `R` or - * `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} node - The parameter `node` can be of type `R` or + * `BTNKeyOrNodeOrEntry`. * @returns a boolean value. */ - isRealNode(node: R | KeyOrNodeOrEntry): node is NODE { + isRealNode(node: R | BTNKeyOrNodeOrEntry): node is NODE { if (node === this.NIL || node === null || node === undefined) return false; return this.isNode(node); } /** * The function checks if a given node is a real node or null. - * @param {R | KeyOrNodeOrEntry} node - The parameter `node` can be of type `R` or - * `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} node - The parameter `node` can be of type `R` or + * `BTNKeyOrNodeOrEntry`. * @returns a boolean value. */ - isNodeOrNull(node: R | KeyOrNodeOrEntry): node is NODE | null { + isNodeOrNull(node: R | BTNKeyOrNodeOrEntry): node is NODE | null { return this.isRealNode(node) || node === null; } /** * The function checks if a given node is equal to the NIL value. - * @param {R | KeyOrNodeOrEntry} node - The parameter `node` can be of type `R` or - * `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} node - The parameter `node` can be of type `R` or + * `BTNKeyOrNodeOrEntry`. * @returns a boolean value. */ - isNIL(node: R | KeyOrNodeOrEntry) { + isNIL(node: R | BTNKeyOrNodeOrEntry) { return node === this.NIL; } /** * The function checks if the input is an array with two elements, indicating it is a binary tree * node entry. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry`. * @returns a boolean value. */ isEntry( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry ): keyOrNodeOrEntryOrRawElement is BTNEntry { return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2; } @@ -402,17 +403,17 @@ export class BinaryTree< * * The `add` function is used to insert a new node into a binary tree, checking for duplicate keys * and finding the appropriate insertion position. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key, * node, entry, or raw element to be added to the tree. It can also accept a value of type - * `KeyOrNodeOrEntry + * `BTNKeyOrNodeOrEntry * @param {V} [value] - The `value` parameter is an optional value that can be associated with the * key being added to the tree. It represents the value that will be stored in the tree for the given * key. * @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the * insertion position cannot be found or if there are duplicate keys. */ - add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, value?: V): boolean { + add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V): boolean { const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value); if (newNode === undefined) return false; @@ -486,7 +487,7 @@ export class BinaryTree< * successfully added to the data structure. */ addMany( - keysOrNodesOrEntriesOrRawElements: Iterable>, + keysOrNodesOrEntriesOrRawElements: Iterable>, values?: Iterable ): boolean[] { // TODO not sure addMany not be run multi times @@ -525,13 +526,13 @@ export class BinaryTree< * * The `refill` function clears the current data and adds new data to the collection. * @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw - * elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry). + * elements. These can be of any type (R) or a specific type (BTNKeyOrNodeOrEntry). * @param [values] - The `values` parameter is an optional iterable of values that will be associated * with the keys or nodes being added. If provided, the values will be assigned to the corresponding * keys or nodes. If not provided, the values will be set to `undefined`. */ refill( - keysOrNodesOrEntriesOrRawElements: Iterable>, + keysOrNodesOrEntriesOrRawElements: Iterable>, values?: Iterable ): void { this.clear(); @@ -541,7 +542,7 @@ export class BinaryTree< delete>(identifier: K, callback?: C): BinaryTreeDeleteResult[]; delete>( - identifier: NODE | null | undefined, + identifier: OptBTNOrNull, callback?: C ): BinaryTreeDeleteResult[]; @@ -619,15 +620,15 @@ export class BinaryTree< identifier: K, callback?: C, onlyOne?: boolean, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType ): NODE[]; getNodes>( - identifier: NODE | null | undefined, + identifier: OptBTNOrNull, callback?: C, onlyOne?: boolean, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType ): NODE[]; @@ -635,7 +636,7 @@ export class BinaryTree< identifier: ReturnType, callback: C, onlyOne?: boolean, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType ): NODE[]; @@ -661,7 +662,7 @@ export class BinaryTree< * the identifier or all nodes that match the identifier. If set to true, only the first matching * node will be returned. If set to false, all matching nodes will be returned. The default value is * false. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point for the search. It can be either a node object, a key-value pair, or a key. If it is not * provided, the `root` of the data structure is used as the starting point. * @param {IterationType} iterationType - The `iterationType` parameter determines the type of @@ -672,7 +673,7 @@ export class BinaryTree< identifier: ReturnType | null | undefined, callback: C = this._DEFAULT_CALLBACK as C, onlyOne = false, - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): NODE[] { beginRoot = this.ensureNode(beginRoot); @@ -714,23 +715,23 @@ export class BinaryTree< getNode>( identifier: K, callback?: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType - ): NODE | null | undefined; + ): OptBTNOrNull; getNode>( - identifier: NODE | null | undefined, + identifier: OptBTNOrNull, callback?: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType - ): NODE | null | undefined; + ): OptBTNOrNull; getNode>( identifier: ReturnType, callback: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType - ): NODE | null | undefined; + ): OptBTNOrNull; /** * Time Complexity: O(n) @@ -748,7 +749,7 @@ export class BinaryTree< * the `C` callback function, or it can be `null` or `undefined`. * @param {C} callback - The `callback` parameter is a function that will be used to determine if a * node matches the desired criteria. It should return a value that can be used to identify the node. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or * a node entry. If not provided, the search will start from the root of the tree. * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type @@ -758,9 +759,9 @@ export class BinaryTree< getNode>( identifier: ReturnType | null | undefined, callback: C = this._DEFAULT_CALLBACK as C, - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType - ): NODE | null | undefined { + ): OptBTNOrNull { return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null; } @@ -781,28 +782,28 @@ export class BinaryTree< * It has a default value of `'ITERATIVE'`. * @returns a value of type NODE, null, or undefined. */ - getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined { + getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBTNOrNull { return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType); } override get>( identifier: K, callback?: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType ): V | undefined; override get>( - identifier: NODE | null | undefined, + identifier: OptBTNOrNull, callback?: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType ): V | undefined; override get>( identifier: ReturnType, callback: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType ): V | undefined; @@ -822,7 +823,7 @@ export class BinaryTree< * callback function `C`. It can also be `null` or `undefined` if no identifier is provided. * @param {C} callback - The `callback` parameter is a function that will be used to determine if a * node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point for the search in the binary tree. It can be either a root node of the tree or a key, node, * or entry object that exists in the tree. If no specific starting point is provided, the search * will begin from the root of the @@ -835,7 +836,7 @@ export class BinaryTree< override get>( identifier: ReturnType | null | undefined, callback: C = this._DEFAULT_CALLBACK as C, - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): V | undefined { return this.getNode(identifier, callback, beginRoot, iterationType)?.value; @@ -844,21 +845,21 @@ export class BinaryTree< override has>( identifier: K, callback?: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType ): boolean; override has>( - identifier: NODE | null | undefined, + identifier: OptBTNOrNull, callback?: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType ): boolean; override has>( identifier: ReturnType | null | undefined, callback: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType ): boolean; @@ -880,7 +881,7 @@ export class BinaryTree< * @param {C} callback - The `callback` parameter is a function that will be used to determine * whether a node should be included in the result or not. It is of type `C`, which extends the * `BTNCallback` type. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point for the iteration in the data structure. It can be either a root node, a key-value pair, or * a node entry. If not specified, it defaults to the root of the data structure. * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type @@ -890,7 +891,7 @@ export class BinaryTree< override has>( identifier: ReturnType | null | undefined, callback: C = this._DEFAULT_CALLBACK as C, - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): boolean { callback = this._ensureCallback(identifier, callback); @@ -941,13 +942,13 @@ export class BinaryTree< * * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the * height of the tree. - * @param {R | KeyOrNodeOrEntry} beginRoot - The parameter `beginRoot` is optional and + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The parameter `beginRoot` is optional and * has a default value of `this.root`. It represents the starting point for checking if the tree is * perfectly balanced. It can be either a root node (`R`), a key or node or entry - * (`KeyOrNodeOrEntry = this.root): boolean { + isPerfectlyBalanced(beginRoot: R | BTNKeyOrNodeOrEntry = this.root): boolean { return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot); } @@ -961,7 +962,7 @@ export class BinaryTree< * Space Complexity: O(1) * * The function `isBST` checks if a binary search tree is valid, either recursively or iteratively. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the * starting point for checking if a binary search tree (BST) is valid. It can be either a root node * of the BST, a key value of a node in the BST, or an entry object containing both the key and value * of a node in the BST @@ -971,7 +972,7 @@ export class BinaryTree< * @returns a boolean value. */ isBST( - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): boolean { // TODO there is a bug @@ -979,7 +980,7 @@ export class BinaryTree< if (!beginRoot) return true; if (iterationType === 'RECURSIVE') { - const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => { + const dfs = (cur: OptBTNOrNull, min: number, max: number): boolean => { if (!this.isRealNode(cur)) return true; const numKey = Number(cur.key); if (numKey <= min || numKey >= max) return false; @@ -994,7 +995,7 @@ export class BinaryTree< const stack = []; let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER; // @ts-ignore - let curr: NODE | null | undefined = beginRoot; + let curr: OptBTNOrNull = beginRoot; while (this.isRealNode(curr) || stack.length > 0) { while (this.isRealNode(curr)) { stack.push(curr); @@ -1024,16 +1025,19 @@ export class BinaryTree< * Space Complexity: O(1) * * The function calculates the depth of a given node or key in a tree-like data structure. - * @param {R | KeyOrNodeOrEntry} dist - The `dist` parameter can be either a `R` - * (representing a root node), or a `KeyOrNodeOrEntry` (representing a key, node, or + * @param {R | BTNKeyOrNodeOrEntry} dist - The `dist` parameter can be either a `R` + * (representing a root node), or a `BTNKeyOrNodeOrEntry` (representing a key, node, or * entry). - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is optional and + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is optional and * represents the starting point from which to calculate the depth. It can be either a reference to a * node in the tree or a key-value pair or an entry object. If not provided, the default value is * `this.root`, which refers to the root node * @returns the depth of a node in a tree structure. */ - getDepth(dist: R | KeyOrNodeOrEntry, beginRoot: R | KeyOrNodeOrEntry = this.root): number { + getDepth( + dist: R | BTNKeyOrNodeOrEntry, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root + ): number { let distEnsured = this.ensureNode(dist); const beginRootEnsured = this.ensureNode(beginRoot); let depth = 0; @@ -1058,22 +1062,22 @@ export class BinaryTree< * * The `getHeight` function calculates the maximum height of a binary tree using either a recursive * or iterative approach. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the * starting point for calculating the height of a tree. It can be either a root node (`R`), a key or - * node or entry (`KeyOrNodeOrEntry`), or it defaults to the root of the current tree. + * node or entry (`BTNKeyOrNodeOrEntry`), or it defaults to the root of the current tree. * @param {IterationType} iterationType - The `iterationType` parameter determines the type of * iteration used to calculate the height of the tree. It can have two possible values: * @returns the maximum height of the binary tree. */ getHeight( - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): number { beginRoot = this.ensureNode(beginRoot); if (!this.isRealNode(beginRoot)) return -1; if (iterationType === 'RECURSIVE') { - const _getMaxHeight = (cur: NODE | null | undefined): number => { + const _getMaxHeight = (cur: OptBTNOrNull): number => { if (!this.isRealNode(cur)) return -1; const leftHeight = _getMaxHeight(cur.left); const rightHeight = _getMaxHeight(cur.right); @@ -1109,9 +1113,9 @@ export class BinaryTree< * * The `getMinHeight` function calculates the minimum height of a binary tree using either a * recursive or iterative approach. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the * starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a - * key or node or entry (`KeyOrNodeOrEntry`), or it defaults to the root of the current + * key or node or entry (`BTNKeyOrNodeOrEntry`), or it defaults to the root of the current * tree. * @param {IterationType} iterationType - The `iterationType` parameter determines the type of * iteration to be used when calculating the minimum height of the tree. It can have two possible @@ -1120,14 +1124,14 @@ export class BinaryTree< * binary tree. */ getMinHeight( - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): number { beginRoot = this.ensureNode(beginRoot); if (!beginRoot) return -1; if (iterationType === 'RECURSIVE') { - const _getMinHeight = (cur: NODE | null | undefined): number => { + const _getMinHeight = (cur: OptBTNOrNull): number => { if (!this.isRealNode(cur)) return 0; if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return 0; const leftMinHeight = _getMinHeight(cur.left); @@ -1138,8 +1142,8 @@ export class BinaryTree< return _getMinHeight(beginRoot); } else { const stack: NODE[] = []; - let node: NODE | null | undefined = beginRoot, - last: NODE | null | undefined = null; + let node: OptBTNOrNull = beginRoot, + last: OptBTNOrNull = null; const depths: Map = new Map(); while (stack.length > 0 || node) { @@ -1176,14 +1180,14 @@ export class BinaryTree< * * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing * up to the root node, with an option to reverse the order of the nodes. - * @param {R | KeyOrNodeOrEntry} beginNode - The `beginNode` parameter can be either of - * type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} beginNode - The `beginNode` parameter can be either of + * type `R` or `BTNKeyOrNodeOrEntry`. * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will * @returns The function `getPathToRoot` returns an array of `NODE` objects. */ - getPathToRoot(beginNode: R | KeyOrNodeOrEntry, isReverse = true): NODE[] { + getPathToRoot(beginNode: R | BTNKeyOrNodeOrEntry, isReverse = true): NODE[] { const result: NODE[] = []; let beginNodeEnsured = this.ensureNode(beginNode); @@ -1209,17 +1213,17 @@ export class BinaryTree< * * The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or * iterative traversal. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the * starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`), - * a key or node or entry (`KeyOrNodeOrEntry`), or `null` or `undefined`. + * a key or node or entry (`BTNKeyOrNodeOrEntry`), or `null` or `undefined`. * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type * of iteration to be performed. It can have two possible values: * @returns The function `getLeftMost` returns the leftmost node in a binary tree. */ getLeftMost( - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType - ): NODE | null | undefined { + ): OptBTNOrNull { if (this.isNIL(beginRoot)) return beginRoot as NODE; beginRoot = this.ensureNode(beginRoot); @@ -1254,18 +1258,18 @@ export class BinaryTree< * * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or * iteratively. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the * starting point for finding the rightmost node in a binary tree. It can be either a root node - * (`R`), a key or node or entry (`KeyOrNodeOrEntry`), or `null` or `undefined`. + * (`R`), a key or node or entry (`BTNKeyOrNodeOrEntry`), or `null` or `undefined`. * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type * of iteration to be performed when finding the rightmost node in a binary tree. It can have two * possible values: * @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`. */ getRightMost( - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType - ): NODE | null | undefined { + ): OptBTNOrNull { if (this.isNIL(beginRoot)) return beginRoot as NODE; // TODO support get right most by passing key in beginRoot = this.ensureNode(beginRoot); @@ -1305,7 +1309,7 @@ export class BinaryTree< */ getPredecessor(node: NODE): NODE { if (this.isRealNode(node.left)) { - let predecessor: NODE | null | undefined = node.left; + let predecessor: OptBTNOrNull = node.left; while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) { if (this.isRealNode(predecessor)) { predecessor = predecessor.right; @@ -1331,7 +1335,7 @@ export class BinaryTree< * @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if * there is no successor, and `undefined` if the input `x` is not a valid node. */ - getSuccessor(x?: K | NODE | null): NODE | null | undefined { + getSuccessor(x?: K | NODE | null): OptBTNOrNull { x = this.ensureNode(x); if (!this.isRealNode(x)) return undefined; @@ -1339,7 +1343,7 @@ export class BinaryTree< return this.getLeftMost(x.right); } - let y: NODE | null | undefined = x.parent; + let y: OptBTNOrNull = x.parent; while (this.isRealNode(y) && x === y.right) { x = y; y = y.parent; @@ -1350,7 +1354,7 @@ export class BinaryTree< dfs>( callback?: C, pattern?: DFSOrderPattern, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType, includeNull?: false ): ReturnType[]; @@ -1358,7 +1362,7 @@ export class BinaryTree< dfs>( callback?: C, pattern?: DFSOrderPattern, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType, includeNull?: true ): ReturnType[]; @@ -1379,7 +1383,7 @@ export class BinaryTree< * return type of the callback function is determined by the generic type `C`. * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the * nodes are visited during the depth-first search. It can have one of the following values: - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it * is a key or key-value pair, the method will find the corresponding node in the tree and start the * search from there. @@ -1391,10 +1395,10 @@ export class BinaryTree< * values will * @returns an array of the return types of the callback function. */ - dfs>( + dfs>>( callback: C = this._DEFAULT_CALLBACK as C, pattern: DFSOrderPattern = 'IN', - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = 'ITERATIVE', includeNull = false ): ReturnType[] { @@ -1402,7 +1406,7 @@ export class BinaryTree< if (!beginRoot) return []; const ans: ReturnType[] = []; if (iterationType === 'RECURSIVE') { - const dfs = (node: NODE | null | undefined) => { + const dfs = (node: OptBTNOrNull) => { switch (pattern) { case 'IN': if (includeNull) { @@ -1444,7 +1448,7 @@ export class BinaryTree< dfs(beginRoot); } else { // 0: visit, 1: print - const stack: { opt: 0 | 1; node: NODE | null | undefined }[] = [{ opt: 0, node: beginRoot }]; + const stack: { opt: 0 | 1; node: OptBTNOrNull }[] = [{ opt: 0, node: beginRoot }]; while (stack.length > 0) { const cur = stack.pop(); @@ -1488,14 +1492,14 @@ export class BinaryTree< bfs>( callback?: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType, includeNull?: false ): ReturnType[]; bfs>( callback?: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType, includeNull?: true ): ReturnType[]; @@ -1514,7 +1518,7 @@ export class BinaryTree< * @param {C} callback - The `callback` parameter is a function that will be called for each node in * the breadth-first search traversal. It takes a single argument, which is the current node being * visited, and returns a value of any type. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the * starting point of the breadth-first search. It can be either a root node of a tree or a key, node, * or entry object. If no value is provided, the `root` property of the class is used as the default * starting point. @@ -1529,7 +1533,7 @@ export class BinaryTree< */ bfs>( callback: C = this._DEFAULT_CALLBACK as C, - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType, includeNull = false ): ReturnType[] { @@ -1539,7 +1543,7 @@ export class BinaryTree< const ans: ReturnType>[] = []; if (iterationType === 'RECURSIVE') { - const queue: Queue = new Queue([beginRoot]); + const queue: Queue> = new Queue>([beginRoot]); const dfs = (level: number) => { if (queue.size === 0) return; @@ -1560,7 +1564,7 @@ export class BinaryTree< dfs(0); } else { - const queue = new Queue([beginRoot]); + const queue = new Queue>([beginRoot]); while (queue.size > 0) { const levelSize = queue.size; @@ -1583,14 +1587,14 @@ export class BinaryTree< listLevels>( callback?: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType, includeNull?: false ): ReturnType[][]; listLevels>( callback?: C, - beginRoot?: R | KeyOrNodeOrEntry, + beginRoot?: R | BTNKeyOrNodeOrEntry, iterationType?: IterationType, includeNull?: true ): ReturnType[][]; @@ -1609,7 +1613,7 @@ export class BinaryTree< * @param {C} callback - The `callback` parameter is a function that will be called for each node in * the tree. It takes a node as an argument and returns a value. The return type of the callback * function is determined by the generic type `C` which extends `BTNCallback`. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter represents the * starting point for traversing the tree. It can be either a root node, a key-value pair, or a node * entry. If no value is provided, the `root` property of the class is used as the default starting * point. @@ -1623,7 +1627,7 @@ export class BinaryTree< */ listLevels>( callback: C = this._DEFAULT_CALLBACK as C, - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType, includeNull = false ): ReturnType[][] { @@ -1686,7 +1690,7 @@ export class BinaryTree< * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used * to specify the order in which the nodes of a binary tree are traversed. It can take one of the * following values: - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point for the traversal. It can be either a node object, a key, or an entry object. If no value is * provided, the `root` of the tree is used as the starting point. * @returns The function `morris` returns an array of values that are the return values of the @@ -1695,16 +1699,16 @@ export class BinaryTree< morris>( callback: C = this._DEFAULT_CALLBACK as C, pattern: DFSOrderPattern = 'IN', - beginRoot: R | KeyOrNodeOrEntry = this.root + beginRoot: R | BTNKeyOrNodeOrEntry = this.root ): ReturnType[] { beginRoot = this.ensureNode(beginRoot); if (beginRoot === null) return []; const ans: ReturnType>[] = []; - let cur: NODE | null | undefined = beginRoot; - const _reverseEdge = (node: NODE | null | undefined) => { - let pre: NODE | null | undefined = null; - let next: NODE | null | undefined = null; + let cur: OptBTNOrNull = beginRoot; + const _reverseEdge = (node: OptBTNOrNull) => { + let pre: OptBTNOrNull = null; + let next: OptBTNOrNull = null; while (node) { next = node.right; node.right = pre; @@ -1713,9 +1717,9 @@ export class BinaryTree< } return pre; }; - const _printEdge = (node: NODE | null | undefined) => { - const tail: NODE | null | undefined = _reverseEdge(node); - let cur: NODE | null | undefined = tail; + const _printEdge = (node: OptBTNOrNull) => { + const tail: OptBTNOrNull = _reverseEdge(node); + let cur: OptBTNOrNull = tail; while (cur) { ans.push(callback(cur)); cur = cur.right; @@ -1884,7 +1888,7 @@ export class BinaryTree< * Space Complexity: O(n) * * The `print` function in TypeScript prints the binary tree structure with customizable options. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point for printing the binary tree. It can be either a node of the binary tree or a key or entry * that exists in the binary tree. If no value is provided, the root of the binary tree will be used * as the starting point. @@ -1893,7 +1897,7 @@ export class BinaryTree< * @returns Nothing is being returned. The function has a return type of `void`, which means it does * not return any value. */ - override print(beginRoot: R | KeyOrNodeOrEntry = this.root, options?: BinaryTreePrintOptions): void { + override print(beginRoot: R | BTNKeyOrNodeOrEntry = this.root, options?: BinaryTreePrintOptions): void { const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options }; beginRoot = this.ensureNode(beginRoot); if (!beginRoot) return; @@ -1908,7 +1912,7 @@ export class BinaryTree< console.log(`S for Sentinel Node(NIL) `); - const display = (root: NODE | null | undefined): void => { + const display = (root: OptBTNOrNull): void => { const [lines, , ,] = this._displayAux(root, opts); for (const line of lines) { console.log(line); @@ -1937,8 +1941,8 @@ export class BinaryTree< if (!node) return; if (this.iterationType === 'ITERATIVE') { - const stack: (NODE | null | undefined)[] = []; - let current: NODE | null | undefined = node; + const stack: OptBTNOrNull[] = []; + let current: OptBTNOrNull = node; while (current || stack.length > 0) { while (this.isRealNode(current)) { @@ -1975,7 +1979,7 @@ export class BinaryTree< * * The `_displayAux` function is responsible for generating the display layout of a binary tree node, * taking into account various options such as whether to show null, undefined, or NaN nodes. - * @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree. + * @param {OptBTNOrNull} node - The `node` parameter represents a node in a binary tree. * It can be of type `NODE`, `null`, or `undefined`. * @param {BinaryTreePrintOptions} options - The `options` parameter is an object that contains the * following properties: @@ -1986,7 +1990,7 @@ export class BinaryTree< * 3. `totalHeight`: The total height of the node display. * 4. `middleIndex`: The index of the middle character */ - protected _displayAux(node: NODE | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout { + protected _displayAux(node: OptBTNOrNull, options: BinaryTreePrintOptions): NodeDisplayLayout { const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options; const emptyDisplayLayout = [['─'], 1, 0, 0]; @@ -2054,7 +2058,7 @@ export class BinaryTree< } } - protected _DEFAULT_CALLBACK = (node: NODE | null | undefined) => (node ? node.key : undefined); + protected _DEFAULT_CALLBACK = (node: OptBTNOrNull) => (node ? node.key : undefined); /** * Time Complexity: O(1) @@ -2066,17 +2070,17 @@ export class BinaryTree< * Space Complexity: O(1) * * The function `_swapProperties` swaps the key-value properties between two nodes. - * @param {R | KeyOrNodeOrEntry} srcNode - The source node that will be swapped with the + * @param {R | BTNKeyOrNodeOrEntry} srcNode - The source node that will be swapped with the * destination node. It can be either an instance of the class `R`, or an object of type - * `KeyOrNodeOrEntry`. - * @param {R | KeyOrNodeOrEntry} destNode - The `destNode` parameter is the node where + * `BTNKeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} destNode - The `destNode` parameter is the node where * the properties will be swapped with the `srcNode`. * @returns either the `destNode` object with its properties swapped with the `srcNode` object's * properties, or `undefined` if either `srcNode` or `destNode` is falsy. */ protected _swapProperties( - srcNode: R | KeyOrNodeOrEntry, - destNode: R | KeyOrNodeOrEntry + srcNode: R | BTNKeyOrNodeOrEntry, + destNode: R | BTNKeyOrNodeOrEntry ): NODE | undefined { srcNode = this.ensureNode(srcNode); destNode = this.ensureNode(destNode); @@ -2144,10 +2148,10 @@ export class BinaryTree< * * The function sets the root property of an object to the provided value, and also updates the * parent property of the new root. - * @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`. This + * @param {OptBTNOrNull} v - The parameter `v` is of type `OptBTNOrNull`. This * means that it can accept a value of type `NODE`, `null`, or `undefined`. */ - protected _setRoot(v: NODE | null | undefined) { + protected _setRoot(v: OptBTNOrNull) { if (v) { v.parent = undefined; } diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index a0756fd..5894d29 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -11,12 +11,13 @@ import type { BSTNodeNested, BSTOptions, BTNCallback, + BTNKeyOrNodeOrEntry, BTNPureKeyOrNodeOrEntry, Comparator, CP, DFSOrderPattern, IterationType, - KeyOrNodeOrEntry + OptBSTN } from '../../types'; import { BTNEntry } from '../../types'; import { BinaryTree, BinaryTreeNode } from './binary-tree'; @@ -43,16 +44,16 @@ export class BSTNode = BSTNod * The function returns the value of the `_left` property. * @returns The `_left` property of the current object is being returned. */ - override get left(): NODE | undefined { + override get left(): OptBSTN { return this._left; } /** * The function sets the left child of a node and updates the parent reference of the child. - * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an + * @param {OptBSTN} v - The parameter `v` is of type `OptBSTN`. It can either be an * instance of the `NODE` class or `undefined`. */ - override set left(v: NODE | undefined) { + override set left(v: OptBSTN) { if (v) { v.parent = this as unknown as NODE; } @@ -66,16 +67,16 @@ export class BSTNode = BSTNod * @returns The method is returning the value of the `_right` property, which is of type `NODE` or * `undefined`. */ - override get right(): NODE | undefined { + override get right(): OptBSTN { return this._right; } /** * The function sets the right child of a node and updates the parent reference of the child. - * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a + * @param {OptBSTN} v - The parameter `v` is of type `OptBSTN`. It can either be a * `NODE` object or `undefined`. */ - override set right(v: NODE | undefined) { + override set right(v: OptBSTN) { if (v) { v.parent = this as unknown as NODE; } @@ -110,7 +111,7 @@ export class BST< * It can include a comparator function that defines the order of the elements in the tree. */ constructor( - keysOrNodesOrEntriesOrRawElements: Iterable> = [], + keysOrNodesOrEntriesOrRawElements: Iterable> = [], options?: BSTOptions ) { super([], options); @@ -129,7 +130,7 @@ export class BST< * 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(): NODE | undefined { + override get root(): OptBSTN { return this._root; } @@ -162,17 +163,17 @@ export class BST< /** * The function overrides a method and converts a key, value pair or entry or raw element to a node. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - A variable that can be of - * type R or KeyOrNodeOrEntry. It represents either a key, a node, an entry, or a raw + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - A variable that can be of + * type R or BTNKeyOrNodeOrEntry. 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. */ override keyValueOrEntryOrRawElementToNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V - ): NODE | undefined { + ): OptBSTN { return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined; } @@ -182,7 +183,7 @@ export class BST< * * The function ensures the existence of a node in a data structure and returns it, or undefined if * it doesn't exist. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node, * entry, or raw element that needs to be ensured in the tree. * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional @@ -192,21 +193,21 @@ export class BST< * not be ensured. */ override ensureNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, iterationType: IterationType = 'ITERATIVE' - ): NODE | undefined { + ): OptBSTN { return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined; } /** * The function checks if the input is an instance of the BSTNode class. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry`. * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is * an instance of the `BSTNode` class. */ override isNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry ): keyOrNodeOrEntryOrRawElement is NODE { return keyOrNodeOrEntryOrRawElement instanceof BSTNode; } @@ -216,13 +217,13 @@ export class BST< * Space Complexity: O(1) * * The `add` function in TypeScript adds a new node to a binary search tree based on the key value. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry`. * @param {V} [value] - The `value` parameter is an optional value that can be associated with the * key in the binary search tree. If provided, it will be stored in the node along with the key. * @returns a boolean value. */ - override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, value?: V): boolean { + override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V): boolean { const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value); if (newNode === undefined) return false; @@ -284,7 +285,7 @@ export class BST< * successfully inserted into the data structure. */ override addMany( - keysOrNodesOrEntriesOrRawElements: Iterable>, + keysOrNodesOrEntriesOrRawElements: Iterable>, values?: Iterable, isBalanceAdd = true, iterationType: IterationType = this.iterationType @@ -308,7 +309,9 @@ export class BST< const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry)[] = []; - const isRealBTNExemplar = (kve: R | KeyOrNodeOrEntry): kve is BTNPureKeyOrNodeOrEntry => { + const isRealBTNExemplar = ( + kve: R | BTNKeyOrNodeOrEntry + ): kve is BTNPureKeyOrNodeOrEntry => { if (kve === undefined || kve === null) return false; return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null)); }; @@ -395,7 +398,7 @@ export class BST< * @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node * or all matching nodes. If set to true, only the first matching node will be returned. If set to * false, all matching nodes will be returned. The default value is false. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point for the search in the binary tree. It can be either a node object, a key-value pair, or an * entry object. If it is not provided, the `root` of the binary tree is used as the starting point. * @param {IterationType} iterationType - The `iterationType` parameter determines the type of @@ -406,7 +409,7 @@ export class BST< identifier: ReturnType | undefined, callback: C = this._DEFAULT_CALLBACK as C, onlyOne = false, - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): NODE[] { beginRoot = this.ensureNode(beginRoot); @@ -496,7 +499,7 @@ export class BST< callback: C = this._DEFAULT_CALLBACK as C, beginRoot: R | BSTNKeyOrNode = this.root, iterationType: IterationType = this.iterationType - ): NODE | undefined { + ): OptBSTN { return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined; } @@ -518,7 +521,7 @@ export class BST< * It has a default value of `'ITERATIVE'`. * @returns The method is returning a NODE object or undefined. */ - override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined { + override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBSTN { return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType); } @@ -539,7 +542,7 @@ export class BST< * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can * take one of the following values: - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a * node entry. If not specified, the default value is the root of the tree. * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the @@ -550,7 +553,7 @@ export class BST< override dfs>( callback: C = this._DEFAULT_CALLBACK as C, pattern: DFSOrderPattern = 'IN', - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = 'ITERATIVE' ): ReturnType[] { return super.dfs(callback, pattern, beginRoot, iterationType, false); @@ -570,7 +573,7 @@ export class BST< * @param {C} callback - The `callback` parameter is a function that will be called for each node * visited during the breadth-first search. It should take a single argument, which is the current * node being visited, and it can return a value of any type. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry * object. If no value is provided, the default value is the root of the tree. * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type @@ -580,7 +583,7 @@ export class BST< */ override bfs>( callback: C = this._DEFAULT_CALLBACK as C, - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): ReturnType[] { return super.bfs(callback, beginRoot, iterationType, false); @@ -600,7 +603,7 @@ export class BST< * @param {C} callback - The `callback` parameter is a generic type `C` that extends * `BTNCallback`. It represents a callback function that will be called for each node in the * tree during the iteration process. - * @param {R | KeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting + * @param {R | BTNKeyOrNodeOrEntry} beginRoot - The `beginRoot` parameter is the starting * point for listing the levels of the binary tree. It can be either a root node of the tree, a * key-value pair representing a node in the tree, or a key representing a node in the tree. If no * value is provided, the root of @@ -611,7 +614,7 @@ export class BST< */ override listLevels>( callback: C = this._DEFAULT_CALLBACK as C, - beginRoot: R | KeyOrNodeOrEntry = this.root, + beginRoot: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): ReturnType[][] { return super.listLevels(callback, beginRoot, iterationType, false); @@ -634,7 +637,7 @@ export class BST< * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1, * 0, or 1, where: - * @param {R | KeyOrNodeOrEntry} targetNode - The `targetNode` parameter is the node in + * @param {R | BTNKeyOrNodeOrEntry} 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 * the key of the node, the node itself, or an entry containing the key and value of the node. If no * `targetNode` is provided, @@ -646,7 +649,7 @@ export class BST< lesserOrGreaterTraverse>( callback: C = this._DEFAULT_CALLBACK as C, lesserOrGreater: CP = -1, - targetNode: R | KeyOrNodeOrEntry = this.root, + targetNode: R | BTNKeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): ReturnType[] { const targetNodeEnsured = this.ensureNode(targetNode); @@ -760,7 +763,7 @@ export class BST< let balanced = true; if (iterationType === 'RECURSIVE') { - const _height = (cur: NODE | undefined): number => { + const _height = (cur: OptBSTN): number => { if (!cur) return 0; const leftHeight = _height(cur.left), rightHeight = _height(cur.right); @@ -770,8 +773,8 @@ export class BST< _height(this.root); } else { const stack: NODE[] = []; - let node: NODE | undefined = this.root, - last: NODE | undefined = undefined; + let node: OptBSTN = this.root, + last: OptBSTN = undefined; const depths: Map = new Map(); while (stack.length > 0 || node) { @@ -827,9 +830,9 @@ export class BST< /** * The function sets the root of a tree-like structure and updates the parent property of the new * root. - * @param {NODE | undefined} v - v is a parameter of type NODE or undefined. + * @param {OptBSTN} v - v is a parameter of type NODE or undefined. */ - protected override _setRoot(v: NODE | undefined) { + protected override _setRoot(v: OptBSTN) { if (v) { v.parent = undefined; } diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 83d618b..628b452 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -1,8 +1,8 @@ import type { BinaryTreeDeleteResult, BTNCallback, + BTNKeyOrNodeOrEntry, CRUD, - KeyOrNodeOrEntry, RBTNColor, RBTreeOptions, RedBlackTreeNested, @@ -72,7 +72,7 @@ export class RedBlackTree< * depend on the implementation */ constructor( - keysOrNodesOrEntriesOrRawElements: Iterable> = [], + keysOrNodesOrEntriesOrRawElements: Iterable> = [], options?: RBTreeOptions ) { super([], options); @@ -135,13 +135,13 @@ export class RedBlackTree< * Space Complexity: O(1) * * The function checks if the input is an instance of the RedBlackTreeNode class. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry`. * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is * an instance of the `RedBlackTreeNode` class. */ override isNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry ): keyOrNodeOrEntryOrRawElement is NODE { return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode; } @@ -157,11 +157,11 @@ export class RedBlackTree< // * // * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is // * valid, otherwise it returns undefined. - // * @param {KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert. + // * @param {BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert. // * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key). // * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails. // */ - // override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, value?: V): NODE | undefined { + // override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V): NODE | undefined { // // if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return; // if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement; @@ -210,8 +210,8 @@ export class RedBlackTree< * * The function adds a new node to a binary search tree and returns true if the node was successfully * added. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry`. * @param {V} [value] - The `value` parameter is an optional value that you want to associate with * the key in the data structure. It represents the value that you want to add or update in the data * structure. @@ -219,7 +219,7 @@ export class RedBlackTree< * the method returns true. If the node already exists and its value is updated, the method also * returns true. If the node cannot be added or updated, the method returns false. */ - override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, value?: V): boolean { + override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V): boolean { const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value); if (!this.isRealNode(newNode)) return false; diff --git a/src/data-structures/binary-tree/tree-multi-map.ts b/src/data-structures/binary-tree/tree-multi-map.ts index d5fc1b2..226d16b 100644 --- a/src/data-structures/binary-tree/tree-multi-map.ts +++ b/src/data-structures/binary-tree/tree-multi-map.ts @@ -9,8 +9,8 @@ import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, + BTNKeyOrNodeOrEntry, IterationType, - KeyOrNodeOrEntry, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, @@ -81,7 +81,7 @@ export class TreeMultiMap< * `compareValues`, which are functions used to compare keys and values respectively. */ constructor( - keysOrNodesOrEntriesOrRawElements: Iterable> = [], + keysOrNodesOrEntriesOrRawElements: Iterable> = [], options?: TreeMultiMapOptions ) { super([], options); @@ -153,8 +153,8 @@ export class TreeMultiMap< /** * The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a * node based on the input. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry`. * @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. @@ -163,7 +163,7 @@ export class TreeMultiMap< * @returns either a NODE object or undefined. */ override keyValueOrEntryOrRawElementToNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V, count = 1 ): NODE | undefined { @@ -190,13 +190,13 @@ export class TreeMultiMap< /** * The function checks if the input is an instance of the TreeMultiMapNode class. - * @param {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter - * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry`. + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The parameter + * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry`. * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is * an instance of the `TreeMultiMapNode` class. */ override isNode( - keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry + keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry ): keyOrNodeOrEntryOrRawElement is NODE { return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode; } @@ -212,7 +212,7 @@ export class TreeMultiMap< * * 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 {R | KeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The + * @param {R | BTNKeyOrNodeOrEntry} keyOrNodeOrEntryOrRawElement - The * `keyOrNodeOrEntryOrRawElement` 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. @@ -222,7 +222,7 @@ export class TreeMultiMap< * @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(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry, value?: V, count = 1): boolean { + override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry, value?: V, count = 1): boolean { const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count); const orgCount = newNode?.count || 0; const isSuccessAdded = super.add(newNode); diff --git a/src/interfaces/binary-tree.ts b/src/interfaces/binary-tree.ts index e545efa..c78a48d 100644 --- a/src/interfaces/binary-tree.ts +++ b/src/interfaces/binary-tree.ts @@ -5,7 +5,7 @@ import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, - KeyOrNodeOrEntry + BTNKeyOrNodeOrEntry } from '../types'; export interface IBinaryTree< @@ -19,9 +19,9 @@ export interface IBinaryTree< createTree(options?: Partial>): TREE; - add(keyOrNodeOrEntryOrRawElement: KeyOrNodeOrEntry, value?: V, count?: number): boolean; + add(keyOrNodeOrEntryOrRawElement: BTNKeyOrNodeOrEntry, value?: V, count?: number): boolean; - addMany(nodes: Iterable>, values?: Iterable): boolean[]; + addMany(nodes: Iterable>, values?: Iterable): boolean[]; delete>(identifier: ReturnType | null, callback: C): BinaryTreeDeleteResult[]; } diff --git a/src/types/common.ts b/src/types/common.ts index 6aad98d..9e17049 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -1,11 +1,5 @@ export type CP = 1 | -1 | 0; -/** - * Enum representing different loop types. - * - * - `iterative`: Indicates the iterative loop type (with loops that use iterations). - * - `recursive`: Indicates the recursive loop type (with loops that call themselves). - */ export type IterationType = 'ITERATIVE' | 'RECURSIVE'; export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE'; @@ -16,8 +10,6 @@ export type DFSOrderPattern = 'PRE' | 'IN' | 'POST'; export type NodeDisplayLayout = [string[], number, number, number]; -export type BTNCallback = (node: N) => D; - export interface IterableWithSize extends Iterable { size: number | ((...args: any[]) => number); } @@ -26,22 +18,8 @@ export interface IterableWithLength extends Iterable { length: number | ((...args: any[]) => number); } +export type OptValue = V | undefined; + export type IterableWithSizeOrLength = IterableWithSize | IterableWithLength; -export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean }; - -export type BTNEntry = [K | null | undefined, V | undefined]; - -export type BTNKeyOrNode = K | null | undefined | N; - -export type KeyOrNodeOrEntry = BTNEntry | BTNKeyOrNode; - -export type BTNodePureKeyOrNode = K | N; - -export type BTNPureKeyOrNodeOrEntry = [K, V | undefined] | BTNodePureKeyOrNode; - -export type BSTNKeyOrNode = K | undefined | N; - -export type BinaryTreeDeleteResult = { deleted: N | null | undefined; needBalanced: N | null | undefined }; - export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED'; diff --git a/src/types/data-structures/binary-tree/binary-tree.ts b/src/types/data-structures/binary-tree/binary-tree.ts index 4be2252..d367b29 100644 --- a/src/types/data-structures/binary-tree/binary-tree.ts +++ b/src/types/data-structures/binary-tree/binary-tree.ts @@ -1,5 +1,5 @@ import { BinaryTree, BinaryTreeNode } from '../../../data-structures'; -import { BTNEntry, IterationType } from '../../common'; +import { IterationType, OptValue } from '../../common'; export type BinaryTreeNodeNested = BinaryTreeNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> @@ -9,3 +9,23 @@ export type BinaryTreeOptions = { iterationType?: IterationType; toEntryFn?: (rawElement: R) => BTNEntry; } + +export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean }; + +export type OptBTNOrNull = NODE | null | undefined; + +export type OptBTNKeyOrNull = K | null | undefined; + +export type BTNEntry = [OptBTNKeyOrNull, OptValue]; + +export type BTNKeyOrNode = OptBTNKeyOrNull | NODE; + +export type BTNKeyOrNodeOrEntry = BTNEntry | BTNKeyOrNode; + +export type BTNPureKeyOrNode = K | NODE; + +export type BTNPureKeyOrNodeOrEntry = [K, OptValue] | BTNPureKeyOrNode; + +export type BinaryTreeDeleteResult = { deleted: OptBTNOrNull; needBalanced: OptBTNOrNull }; + +export type BTNCallback = (node: NODE) => D; \ No newline at end of file diff --git a/src/types/data-structures/binary-tree/bst.ts b/src/types/data-structures/binary-tree/bst.ts index 7a54856..20d0d81 100644 --- a/src/types/data-structures/binary-tree/bst.ts +++ b/src/types/data-structures/binary-tree/bst.ts @@ -9,3 +9,10 @@ export type BSTNested> = BST = BinaryTreeOptions & { comparator?: Comparator } + +export type OptBSTNKey = K | undefined; + +export type OptBSTN = NODE | undefined; + +export type BSTNKeyOrNode = OptBSTNKey | NODE; +