From 60a315417821dc8093e0fb71020764726998cf9b Mon Sep 17 00:00:00 2001 From: Revone Date: Mon, 11 Dec 2023 14:14:46 +0800 Subject: [PATCH] refactor: Differentiate between type imports and regular object imports. --- src/data-structures/binary-tree/avl-tree.ts | 24 +-- .../binary-tree/binary-tree.ts | 151 +++++++++--------- src/data-structures/binary-tree/bst.ts | 25 +-- src/data-structures/binary-tree/rb-tree.ts | 31 ++-- .../binary-tree/tree-multimap.ts | 38 ++--- src/data-structures/hash/hash-map.ts | 2 +- src/data-structures/hash/hash-table.ts | 6 +- src/data-structures/heap/heap.ts | 3 +- .../linked-list/doubly-linked-list.ts | 2 +- .../linked-list/singly-linked-list.ts | 2 +- .../priority-queue/priority-queue.ts | 2 +- src/data-structures/queue/deque.ts | 2 +- src/data-structures/queue/queue.ts | 2 +- src/data-structures/stack/stack.ts | 2 +- src/data-structures/trie/trie.ts | 2 +- src/interfaces/binary-tree.ts | 10 +- src/types/common.ts | 6 +- .../binary-tree/binary-tree.ts | 2 +- 18 files changed, 155 insertions(+), 157 deletions(-) diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index caafbd1..aa81029 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -10,11 +10,12 @@ import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, - BiTreeDeleteResult, + BinaryTreeDeleteResult, BSTNodeKeyOrNode, - BTNodeExemplar + BTNCallback, + BTNExemplar, + BTNKeyOrNode } from '../../types'; -import { BTNCallback, BTNodeKeyOrNode } from '../../types'; import { IBinaryTree } from '../../interfaces'; export class AVLTreeNode = AVLTreeNodeNested> extends BSTNode { @@ -34,7 +35,6 @@ export class AVLTreeNode = AVLT * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature. * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST. * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes. - * 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST. */ export class AVLTree = AVLTreeNode>, TREE extends AVLTree = AVLTree>> extends BST @@ -42,14 +42,14 @@ export class AVLTree = AVLTreeN /** * The constructor function initializes an AVLTree object with optional elements and options. - * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar` + * @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar` * objects. It represents a collection of elements that will be added to the AVL tree during * initialization. * @param [options] - The `options` parameter is an optional object that allows you to customize the * behavior of the AVL tree. It is of type `Partial`, which means that you can * provide only a subset of the properties defined in the `AVLTreeOptions` interface. */ - constructor(elements?: Iterable>, options?: Partial>) { + constructor(elements?: Iterable>, options?: Partial>) { super([], options); if (elements) super.addMany(elements); } @@ -83,10 +83,10 @@ export class AVLTree = AVLTreeN /** * The function checks if an exemplar is an instance of AVLTreeNode. - * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar`. + * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`. * @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class. */ - override isNode(exemplar: BTNodeExemplar): exemplar is N { + override isNode(exemplar: BTNExemplar): exemplar is N { return exemplar instanceof AVLTreeNode; } @@ -96,7 +96,7 @@ export class AVLTree = AVLTreeN * data type. * @returns a boolean value indicating whether the potentialKey is of type number or not. */ - override isNotNodeInstance(potentialKey: BTNodeKeyOrNode): potentialKey is K { + override isNotNodeInstance(potentialKey: BTNKeyOrNode): potentialKey is K { return !(potentialKey instanceof AVLTreeNode) } @@ -118,7 +118,7 @@ export class AVLTree = AVLTreeN * being added to the binary tree. * @returns The method is returning either the inserted node or undefined. */ - override add(keyOrNodeOrEntry: BTNodeExemplar, value?: V): N | undefined { + override add(keyOrNodeOrEntry: BTNExemplar, value?: V): N | undefined { if (keyOrNodeOrEntry === null) return undefined; const inserted = super.add(keyOrNodeOrEntry, value); if (inserted) this._balancePath(inserted); @@ -143,12 +143,12 @@ export class AVLTree = AVLTreeN * that is deleted from the binary tree. It is an optional parameter and if not provided, it will * default to the `_defaultOneParamCallback` function. The `callback` function should have a single * parameter of type `N - * @returns The method is returning an array of `BiTreeDeleteResult`. + * @returns The method is returning an array of `BinaryTreeDeleteResult`. */ override delete>( identifier: ReturnType, callback: C = this._defaultOneParamCallback as C - ): BiTreeDeleteResult[] { + ): BinaryTreeDeleteResult[] { if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C; const deletedResults = super.delete(identifier, callback); for (const { needBalanced } of deletedResults) { diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 421da3c..b9a3637 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -7,23 +7,20 @@ */ import type { + BinaryTreeDeleteResult, + BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, - BTNCallback, - BTNodeEntry, - BTNodeExemplar, - BTNodeKeyOrNode, -} from '../../types'; -import { - BinaryTreeNested, BinaryTreePrintOptions, - BiTreeDeleteResult, + BTNCallback, + BTNEntry, + BTNExemplar, + BTNKeyOrNode, DFSOrderPattern, EntryCallback, - FamilyPosition, - IterationType, NodeDisplayLayout } from '../../types'; +import { FamilyPosition, IterationType } from '../../types'; import { IBinaryTree } from '../../interfaces'; import { trampoline } from '../../utils'; import { Queue } from '../queue'; @@ -107,14 +104,14 @@ export class BinaryTree = Bi /** * The constructor function initializes a binary tree object with optional elements and options. - * @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the + * @param [elements] - An optional iterable of BTNExemplar objects. These objects represent the * elements 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 * `Partial`, which means that not all properties of `BinaryTreeOptions` are * required. */ - constructor(elements?: Iterable>, options?: Partial>) { + constructor(elements?: Iterable>, options?: Partial>) { super(); if (options) { const { iterationType, extractor } = options; @@ -172,22 +169,22 @@ export class BinaryTree = Bi /** * The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class. - * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar`. + * @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar`. * @returns a boolean value indicating whether the exemplar is an instance of the class N. */ - isNode(exemplar: BTNodeExemplar): exemplar is N { + isNode(exemplar: BTNExemplar): exemplar is N { return exemplar instanceof BinaryTreeNode; } /** * The function `exemplarToNode` converts an exemplar object into a node object. - * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar`. + * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`. * @param {V} [value] - The `value` parameter is an optional value that can be passed to the * `exemplarToNode` function. It represents the value associated with the exemplar node. If no value * is provided, it will be `undefined`. * @returns a value of type N (node), or null, or undefined. */ - exemplarToNode(exemplar: BTNodeExemplar, value?: V): N | null | undefined { + exemplarToNode(exemplar: BTNExemplar, value?: V): N | null | undefined { if (exemplar === undefined) return; let node: N | null | undefined; @@ -214,11 +211,11 @@ export class BinaryTree = Bi /** * The function checks if a given value is an entry in a binary tree node. - * @param kne - BTNodeExemplar - A generic type representing a node in a binary tree. It has + * @param kne - BTNExemplar - A generic type representing a node in a binary tree. It has * two type parameters V and N, representing the value and node type respectively. * @returns a boolean value. */ - isEntry(kne: BTNodeExemplar): kne is BTNodeEntry { + isEntry(kne: BTNExemplar): kne is BTNEntry { return Array.isArray(kne) && kne.length === 2; } @@ -237,7 +234,7 @@ export class BinaryTree = Bi * @param {V} [value] - The value to be inserted into the binary tree. * @returns The function `add` returns either a node (`N`), `null`, or `undefined`. */ - add(keyOrNodeOrEntry: BTNodeExemplar, value?: V): N | null | undefined { + add(keyOrNodeOrEntry: BTNExemplar, value?: V): N | null | undefined { const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return; @@ -303,11 +300,11 @@ export class BinaryTree = Bi * * The `addMany` function takes in a collection of nodes and an optional collection of values, and * adds each node with its corresponding value to the data structure. - * @param nodes - An iterable collection of BTNodeExemplar objects. + * @param nodes - An iterable collection of BTNExemplar objects. * @param [values] - An optional iterable of values that will be assigned to each node being added. * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values. */ - addMany(nodes: Iterable>, values?: Iterable): (N | null | undefined)[] { + addMany(nodes: Iterable>, values?: Iterable): (N | null | undefined)[] { // TODO not sure addMany not be run multi times const inserted: (N | null | undefined)[] = []; @@ -338,7 +335,7 @@ export class BinaryTree = Bi * Space Complexity: O(1) */ - refill(nodesOrKeysOrEntries: Iterable>, values?: Iterable): void { + refill(nodesOrKeysOrEntries: Iterable>, values?: Iterable): void { this.clear(); this.addMany(nodesOrKeysOrEntries, values); } @@ -348,11 +345,11 @@ export class BinaryTree = Bi * Space Complexity: O(1) */ - delete>(identifier: K, callback?: C): BiTreeDeleteResult[]; + delete>(identifier: K, callback?: C): BinaryTreeDeleteResult[]; - delete>(identifier: N | null | undefined, callback?: C): BiTreeDeleteResult[]; + delete>(identifier: N | null | undefined, callback?: C): BinaryTreeDeleteResult[]; - delete>(identifier: ReturnType, callback: C): BiTreeDeleteResult[]; + delete>(identifier: ReturnType, callback: C): BinaryTreeDeleteResult[]; /** * Time Complexity: O(n) @@ -367,13 +364,13 @@ export class BinaryTree = Bi * @param {C} callback - The `callback` parameter is a function that is used to determine the * identifier of the node to be deleted. It is optional and has a default value of * `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node. - * @returns an array of `BiTreeDeleteResult`. + * @returns an array of `BinaryTreeDeleteResult`. */ delete>( identifier: ReturnType | null | undefined, callback: C = this._defaultOneParamCallback as C - ): BiTreeDeleteResult[] { - const deletedResult: BiTreeDeleteResult[] = []; + ): BinaryTreeDeleteResult[] { + const deletedResult: BinaryTreeDeleteResult[] = []; if (!this.root) return deletedResult; if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C; @@ -437,7 +434,7 @@ export class BinaryTree = Bi * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot * @returns the depth of the `distNode` relative to the `beginRoot`. */ - getDepth(distNode: BTNodeKeyOrNode, beginRoot: BTNodeKeyOrNode = this.root): number { + getDepth(distNode: BTNKeyOrNode, beginRoot: BTNKeyOrNode = this.root): number { distNode = this.ensureNode(distNode); beginRoot = this.ensureNode(beginRoot); let depth = 0; @@ -470,7 +467,7 @@ export class BinaryTree = Bi * values: * @returns the height of the binary tree. */ - getHeight(beginRoot: BTNodeKeyOrNode = this.root, iterationType = this.iterationType): number { + getHeight(beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType): number { beginRoot = this.ensureNode(beginRoot); if (!beginRoot) return -1; @@ -519,7 +516,7 @@ export class BinaryTree = Bi * to calculate the minimum height of a binary tree. It can have two possible values: * @returns The function `getMinHeight` returns the minimum height of a binary tree. */ - getMinHeight(beginRoot: BTNodeKeyOrNode = this.root, iterationType = this.iterationType): number { + getMinHeight(beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType): number { beginRoot = this.ensureNode(beginRoot); if (!beginRoot) return -1; @@ -579,7 +576,7 @@ export class BinaryTree = Bi * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If * @returns a boolean value. */ - isPerfectlyBalanced(beginRoot: BTNodeKeyOrNode = this.root): boolean { + isPerfectlyBalanced(beginRoot: BTNKeyOrNode = this.root): boolean { return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot); } @@ -592,7 +589,7 @@ export class BinaryTree = Bi identifier: K, callback?: C, onlyOne?: boolean, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): N[]; @@ -600,7 +597,7 @@ export class BinaryTree = Bi identifier: N | null | undefined, callback?: C, onlyOne?: boolean, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): N[]; @@ -608,7 +605,7 @@ export class BinaryTree = Bi identifier: ReturnType, callback: C, onlyOne?: boolean, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): N[]; @@ -641,7 +638,7 @@ export class BinaryTree = Bi identifier: ReturnType | null | undefined, callback: C = this._defaultOneParamCallback as C, onlyOne = false, - beginRoot: BTNodeKeyOrNode = this.root, + beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType ): N[] { if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) @@ -689,21 +686,21 @@ export class BinaryTree = Bi has>( identifier: K, callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): boolean; has>( identifier: N | null | undefined, callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): boolean; has>( identifier: ReturnType | null | undefined, callback: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): boolean; @@ -730,7 +727,7 @@ export class BinaryTree = Bi has>( identifier: ReturnType | null | undefined, callback: C = this._defaultOneParamCallback as C, - beginRoot: BTNodeKeyOrNode = this.root, + beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType ): boolean { if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) @@ -747,21 +744,21 @@ export class BinaryTree = Bi getNode>( identifier: K, callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): N | null | undefined; getNode>( identifier: N | null | undefined, callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): N | null | undefined; getNode>( identifier: ReturnType, callback: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): N | null | undefined; @@ -789,7 +786,7 @@ export class BinaryTree = Bi getNode>( identifier: ReturnType | null | undefined, callback: C = this._defaultOneParamCallback as C, - beginRoot: BTNodeKeyOrNode = this.root, + beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType ): N | null | undefined { if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) @@ -858,28 +855,28 @@ export class BinaryTree = Bi * @returns either the node corresponding to the given key if it is a valid node key, or the key * itself if it is not a valid node key. */ - ensureNode(key: BTNodeKeyOrNode, iterationType = IterationType.ITERATIVE): N | null | undefined { + ensureNode(key: BTNKeyOrNode, iterationType = IterationType.ITERATIVE): N | null | undefined { return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key; } get>( identifier: K, callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): V | undefined; get>( identifier: N | null | undefined, callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): V | undefined; get>( identifier: ReturnType, callback: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType ): V | undefined; @@ -908,7 +905,7 @@ export class BinaryTree = Bi get>( identifier: ReturnType | null | undefined, callback: C = this._defaultOneParamCallback as C, - beginRoot: BTNodeKeyOrNode = this.root, + beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType ): V | undefined { if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) @@ -952,7 +949,7 @@ export class BinaryTree = Bi * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is * @returns The function `getPathToRoot` returns an array of nodes (`N[]`). */ - getPathToRoot(beginRoot: BTNodeKeyOrNode, isReverse = true): N[] { + getPathToRoot(beginRoot: BTNKeyOrNode, isReverse = true): N[] { // TODO to support get path through passing key const result: N[] = []; beginRoot = this.ensureNode(beginRoot); @@ -989,7 +986,7 @@ export class BinaryTree = Bi * is no leftmost node, it returns `null` or `undefined` depending on the input. */ getLeftMost( - beginRoot: BTNodeKeyOrNode = this.root, + beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType ): N | null | undefined { beginRoot = this.ensureNode(beginRoot); @@ -1035,7 +1032,7 @@ export class BinaryTree = Bi * is no rightmost node, it returns `null` or `undefined`, depending on the input. */ getRightMost( - beginRoot: BTNodeKeyOrNode = this.root, + beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType ): N | null | undefined { // TODO support get right most by passing key in @@ -1077,7 +1074,7 @@ export class BinaryTree = Bi * possible values: * @returns a boolean value. */ - isSubtreeBST(beginRoot: BTNodeKeyOrNode, iterationType = this.iterationType): boolean { + isSubtreeBST(beginRoot: BTNKeyOrNode, iterationType = this.iterationType): boolean { // TODO there is a bug beginRoot = this.ensureNode(beginRoot); if (!beginRoot) return true; @@ -1138,21 +1135,21 @@ export class BinaryTree = Bi subTreeTraverse>( callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: false ): ReturnType[]; subTreeTraverse>( callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: undefined ): ReturnType[]; subTreeTraverse>( callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: true ): ReturnType[]; @@ -1181,7 +1178,7 @@ export class BinaryTree = Bi */ subTreeTraverse>( callback: C = this._defaultOneParamCallback as C, - beginRoot: BTNodeKeyOrNode = this.root, + beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType, includeNull = false ): ReturnType[] { @@ -1236,7 +1233,7 @@ export class BinaryTree = Bi * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type. * @returns a boolean value. */ - isRealNode(node: BTNodeExemplar): node is N { + isRealNode(node: BTNExemplar): node is N { return node instanceof BinaryTreeNode && String(node.key) !== 'NaN'; } @@ -1245,7 +1242,7 @@ export class BinaryTree = Bi * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type. * @returns a boolean value. */ - isNIL(node: BTNodeExemplar) { + isNIL(node: BTNExemplar) { return node instanceof BinaryTreeNode && String(node.key) === 'NaN'; } @@ -1254,7 +1251,7 @@ export class BinaryTree = Bi * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type. * @returns a boolean value. */ - isNodeOrNull(node: BTNodeExemplar): node is N | null { + isNodeOrNull(node: BTNExemplar): node is N | null { return this.isRealNode(node) || node === null; } @@ -1264,14 +1261,14 @@ export class BinaryTree = Bi * data type. * @returns a boolean value indicating whether the potentialKey is of type number or not. */ - isNotNodeInstance(potentialKey: BTNodeKeyOrNode): potentialKey is K { + isNotNodeInstance(potentialKey: BTNKeyOrNode): potentialKey is K { return !(potentialKey instanceof BinaryTreeNode) } dfs>( callback?: C, pattern?: DFSOrderPattern, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: false ): ReturnType[]; @@ -1279,7 +1276,7 @@ export class BinaryTree = Bi dfs>( callback?: C, pattern?: DFSOrderPattern, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: undefined ): ReturnType[]; @@ -1287,7 +1284,7 @@ export class BinaryTree = Bi dfs>( callback?: C, pattern?: DFSOrderPattern, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: true ): ReturnType[]; @@ -1318,7 +1315,7 @@ export class BinaryTree = Bi dfs>( callback: C = this._defaultOneParamCallback as C, pattern: DFSOrderPattern = 'in', - beginRoot: BTNodeKeyOrNode = this.root, + beginRoot: BTNKeyOrNode = this.root, iterationType: IterationType = IterationType.ITERATIVE, includeNull = false ): ReturnType[] { @@ -1417,21 +1414,21 @@ export class BinaryTree = Bi bfs>( callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: false ): ReturnType[]; bfs>( callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: undefined ): ReturnType[]; bfs>( callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: true ): ReturnType[]; @@ -1459,7 +1456,7 @@ export class BinaryTree = Bi */ bfs>( callback: C = this._defaultOneParamCallback as C, - beginRoot: BTNodeKeyOrNode = this.root, + beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType, includeNull = false ): ReturnType[] { @@ -1518,21 +1515,21 @@ export class BinaryTree = Bi listLevels>( callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: false ): ReturnType[][]; listLevels>( callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: undefined ): ReturnType[][]; listLevels>( callback?: C, - beginRoot?: BTNodeKeyOrNode, + beginRoot?: BTNKeyOrNode, iterationType?: IterationType, includeNull?: true ): ReturnType[][]; @@ -1560,7 +1557,7 @@ export class BinaryTree = Bi */ listLevels>( callback: C = this._defaultOneParamCallback as C, - beginRoot: BTNodeKeyOrNode = this.root, + beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType, includeNull = false ): ReturnType[][] { @@ -1677,7 +1674,7 @@ export class BinaryTree = Bi morris>( callback: C = this._defaultOneParamCallback as C, pattern: DFSOrderPattern = 'in', - beginRoot: BTNodeKeyOrNode = this.root + beginRoot: BTNKeyOrNode = this.root ): ReturnType[] { beginRoot = this.ensureNode(beginRoot); if (beginRoot === null) return []; @@ -1856,7 +1853,7 @@ export class BinaryTree = Bi * following types: * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes. */ - print(beginRoot: BTNodeKeyOrNode = this.root, options?: BinaryTreePrintOptions): void { + print(beginRoot: BTNKeyOrNode = this.root, options?: BinaryTreePrintOptions): void { const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options }; beginRoot = this.ensureNode(beginRoot); if (!beginRoot) return; @@ -1968,7 +1965,7 @@ export class BinaryTree = Bi * @param {N} destNode - The destination node to swap. * @returns {N} - The destination node after the swap. */ - protected _swapProperties(srcNode: BTNodeKeyOrNode, destNode: BTNodeKeyOrNode): N | undefined { + protected _swapProperties(srcNode: BTNKeyOrNode, destNode: BTNKeyOrNode): N | undefined { srcNode = this.ensureNode(srcNode); destNode = this.ensureNode(destNode); @@ -2026,7 +2023,7 @@ export class BinaryTree = Bi * the binary tree. If neither the left nor right child is available, the function returns undefined. * If the parent node is null, the function also returns undefined. */ - protected _addTo(newNode: N | null | undefined, parent: BTNodeKeyOrNode): N | null | undefined { + protected _addTo(newNode: N | null | undefined, parent: BTNKeyOrNode): N | null | undefined { if (this.isNotNodeInstance(parent)) parent = this.getNode(parent); if (parent) { diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 1347e0c..d651e55 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -11,10 +11,11 @@ import type { BSTNodeNested, BSTOptions, BTNCallback, - BTNodeExemplar, + BTNExemplar, + BTNKeyOrNode, BTNodePureExemplar } from '../../types'; -import { BSTVariant, BTNodeKeyOrNode, CP, IterationType } from '../../types'; +import { BSTVariant, CP, IterationType } from '../../types'; import { BinaryTree, BinaryTreeNode } from './binary-tree'; import { IBinaryTree } from '../../interfaces'; import { Queue } from '../queue'; @@ -87,12 +88,12 @@ export class BST = BSTNode>, options?: Partial>) { + constructor(elements?: Iterable>, options?: Partial>) { super([], options); if (options) { @@ -147,10 +148,10 @@ export class BST = BSTNode`. + * @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar`. * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class. */ - override isNode(exemplar: BTNodeExemplar): exemplar is N { + override isNode(exemplar: BTNExemplar): exemplar is N { return exemplar instanceof BSTNode; } @@ -158,12 +159,12 @@ export class BST = BSTNode`, where: + * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`, where: * @param {V} [value] - The `value` parameter is an optional value that can be passed to the * `exemplarToNode` function. It represents the value associated with the exemplar node. * @returns a node of type N or undefined. */ - override exemplarToNode(exemplar: BTNodeExemplar, value?: V): N | undefined { + override exemplarToNode(exemplar: BTNExemplar, value?: V): N | undefined { let node: N | undefined; if (exemplar === null || exemplar === undefined) { return; @@ -201,7 +202,7 @@ export class BST = BSTNode, value?: V): N | undefined { + override add(keyOrNodeOrEntry: BTNExemplar, value?: V): N | undefined { const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return; @@ -273,7 +274,7 @@ export class BST = BSTNode>, + keysOrNodesOrEntries: Iterable>, values?: Iterable, isBalanceAdd = true, iterationType = this.iterationType @@ -297,7 +298,7 @@ export class BST = BSTNode[] = []; - const isRealBTNExemplar = (kve: BTNodeExemplar): kve is BTNodePureExemplar => { + const isRealBTNExemplar = (kve: BTNExemplar): kve is BTNodePureExemplar => { if (kve === undefined || kve === null) return false; return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null)); } @@ -448,7 +449,7 @@ export class BST = BSTNode): potentialKey is K { + override isNotNodeInstance(potentialKey: BTNKeyOrNode): potentialKey is K { return !(potentialKey instanceof BSTNode) } diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index cad7928..efd5ea5 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -7,11 +7,11 @@ */ import { - BiTreeDeleteResult, + BinaryTreeDeleteResult, BSTNodeKeyOrNode, BTNCallback, - BTNodeExemplar, - BTNodeKeyOrNode, + BTNExemplar, + BTNKeyOrNode, IterationType, RBTNColor, RBTreeOptions, @@ -20,7 +20,6 @@ import { } from '../../types'; import { BST, BSTNode } from './bst'; import { IBinaryTree } from '../../interfaces'; -import { BinaryTreeNode } from './binary-tree'; export class RedBlackTreeNode = RedBlackTreeNodeNested> extends BSTNode< K, V, @@ -49,7 +48,7 @@ export class RedBlackTree /** * This is the constructor function for a Red-Black Tree data structure in TypeScript, which * initializes the tree with optional elements and options. - * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar` + * @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar` * objects. It represents the initial elements that will be added to the RBTree during its * construction. If this parameter is provided, the `addMany` method is called to add all the * elements to the @@ -57,7 +56,7 @@ export class RedBlackTree * behavior of the RBTree. It is of type `Partial`, which means that you can provide * only a subset of the properties defined in the `RBTreeOptions` interface. */ - constructor(elements?: Iterable>, options?: Partial>) { + constructor(elements?: Iterable>, options?: Partial>) { super([], options); this._root = this.Sentinel; @@ -108,11 +107,11 @@ export class RedBlackTree /** * The function checks if an exemplar is an instance of the RedBlackTreeNode class. - * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar`. + * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`. * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode * class. */ - override isNode(exemplar: BTNodeExemplar): exemplar is N { + override isNode(exemplar: BTNExemplar): exemplar is N { return exemplar instanceof RedBlackTreeNode; } @@ -122,19 +121,19 @@ export class RedBlackTree * data type. * @returns a boolean value indicating whether the potentialKey is of type number or not. */ - override isNotNodeInstance(potentialKey: BTNodeKeyOrNode): potentialKey is K { + override isNotNodeInstance(potentialKey: BTNKeyOrNode): potentialKey is K { return !(potentialKey instanceof RedBlackTreeNode) } /** * The function `exemplarToNode` takes an exemplar and converts it into a node object if possible. - * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar`, where: + * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`, where: * @param {V} [value] - The `value` parameter is an optional value that can be passed to the * `exemplarToNode` function. It represents the value associated with the exemplar node. If a value * is provided, it will be used when creating the new node. If no value is provided, the new node * @returns a node of type N or undefined. */ - override exemplarToNode(exemplar: BTNodeExemplar, value?: V): N | undefined { + override exemplarToNode(exemplar: BTNExemplar, value?: V): N | undefined { let node: N | undefined; if (exemplar === null || exemplar === undefined) { @@ -173,7 +172,7 @@ export class RedBlackTree * being added to the binary search tree. * @returns The method `add` returns either the newly added node (`N`) or `undefined`. */ - override add(keyOrNodeOrEntry: BTNodeExemplar, value?: V): N | undefined { + override add(keyOrNodeOrEntry: BTNExemplar, value?: V): N | undefined { const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return; @@ -242,13 +241,13 @@ export class RedBlackTree * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` and * returns a value of type `ReturnType`. It is used to determine if a node should be deleted based * on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam - * @returns an array of `BiTreeDeleteResult`. + * @returns an array of `BinaryTreeDeleteResult`. */ delete>( identifier: ReturnType | null | undefined, callback: C = this._defaultOneParamCallback as C - ): BiTreeDeleteResult[] { - const ans: BiTreeDeleteResult[] = []; + ): BinaryTreeDeleteResult[] { + const ans: BinaryTreeDeleteResult[] = []; if (identifier === null) return ans; const helper = (node: N | undefined): void => { let z: N = this.Sentinel; @@ -368,7 +367,7 @@ export class RedBlackTree beginRoot: BSTNodeKeyOrNode = this.root, iterationType = this.iterationType ): N | null | undefined { - if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C; + if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C; beginRoot = this.ensureNode(beginRoot); return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined; } diff --git a/src/data-structures/binary-tree/tree-multimap.ts b/src/data-structures/binary-tree/tree-multimap.ts index 70fd770..3100a08 100644 --- a/src/data-structures/binary-tree/tree-multimap.ts +++ b/src/data-structures/binary-tree/tree-multimap.ts @@ -5,15 +5,17 @@ * @copyright Copyright (c) 2022 Tyler Zeng * @license MIT License */ -import type { BSTNodeKeyOrNode, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types'; -import { - BiTreeDeleteResult, +import type { + BinaryTreeDeleteResult, + BSTNodeKeyOrNode, BTNCallback, - BTNodeKeyOrNode, - FamilyPosition, - IterationType, - TreeMultimapNested + BTNExemplar, + BTNKeyOrNode, + TreeMultimapNested, + TreeMultimapNodeNested, + TreeMultimapOptions } from '../../types'; +import { FamilyPosition, IterationType } from '../../types'; import { IBinaryTree } from '../../interfaces'; import { AVLTree, AVLTreeNode } from './avl-tree'; @@ -48,7 +50,7 @@ export class TreeMultimap extends AVLTree implements IBinaryTree { - constructor(elements?: Iterable>, options?: Partial>) { + constructor(elements?: Iterable>, options?: Partial>) { super([], options); if (elements) this.addMany(elements); } @@ -84,11 +86,11 @@ export class TreeMultimap /** * The function checks if an exemplar is an instance of the TreeMultimapNode class. - * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar`. + * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`. * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode * class. */ - override isNode(exemplar: BTNodeExemplar): exemplar is N { + override isNode(exemplar: BTNExemplar): exemplar is N { return exemplar instanceof TreeMultimapNode; } @@ -98,13 +100,13 @@ export class TreeMultimap * data type. * @returns a boolean value indicating whether the potentialKey is of type number or not. */ - override isNotNodeInstance(potentialKey: BTNodeKeyOrNode): potentialKey is K { + override isNotNodeInstance(potentialKey: BTNKeyOrNode): potentialKey is K { return !(potentialKey instanceof TreeMultimapNode) } /** * The function `exemplarToNode` converts an exemplar object into a node object. - * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar`, which means it + * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`, which means it * can be one of the following: * @param {V} [value] - The `value` parameter is an optional argument that represents the value * associated with the node. It is of type `V`, which can be any data type. If no value is provided, @@ -113,7 +115,7 @@ export class TreeMultimap * times the value should be added to the node. If not provided, it defaults to 1. * @returns a node of type `N` or `undefined`. */ - override exemplarToNode(exemplar: BTNodeExemplar, value?: V, count = 1): N | undefined { + override exemplarToNode(exemplar: BTNExemplar, value?: V, count = 1): N | undefined { let node: N | undefined; if (exemplar === undefined || exemplar === null) { return; @@ -155,7 +157,7 @@ export class TreeMultimap * @returns The method is returning either the newly inserted node or `undefined` if the insertion * was not successful. */ - override add(keyOrNodeOrEntry: BTNodeExemplar, value?: V, count = 1): N | undefined { + override add(keyOrNodeOrEntry: BTNExemplar, value?: V, count = 1): N | undefined { const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count); if (newNode === undefined) return; @@ -182,7 +184,7 @@ export class TreeMultimap * either keys, nodes, or entries. * @returns The method is returning an array of type `N | undefined`. */ - override addMany(keysOrNodesOrEntries: Iterable>): (N | undefined)[] { + override addMany(keysOrNodesOrEntries: Iterable>): (N | undefined)[] { return super.addMany(keysOrNodesOrEntries); } @@ -262,14 +264,14 @@ export class TreeMultimap * being deleted. If set to true, the count of the node will not be considered and the node will be * deleted regardless of its count. If set to false (default), the count of the node will be * decremented by 1 and - * @returns an array of `BiTreeDeleteResult`. + * @returns an array of `BinaryTreeDeleteResult`. */ override delete>( identifier: ReturnType, callback: C = this._defaultOneParamCallback as C, ignoreCount = false - ): BiTreeDeleteResult[] { - const deletedResult: BiTreeDeleteResult[] = []; + ): BinaryTreeDeleteResult[] { + const deletedResult: BinaryTreeDeleteResult[] = []; if (!this.root) return deletedResult; const curr: N | undefined = this.getNode(identifier, callback) ?? undefined; diff --git a/src/data-structures/hash/hash-map.ts b/src/data-structures/hash/hash-map.ts index cb3bc8d..bce029e 100644 --- a/src/data-structures/hash/hash-map.ts +++ b/src/data-structures/hash/hash-map.ts @@ -7,7 +7,7 @@ */ import { isWeakKey, rangeCheck } from '../../utils'; -import { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types'; +import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types'; import { IterableEntryBase } from "../base"; export class HashMap extends IterableEntryBase { diff --git a/src/data-structures/hash/hash-table.ts b/src/data-structures/hash/hash-table.ts index 3176e87..a46b135 100644 --- a/src/data-structures/hash/hash-table.ts +++ b/src/data-structures/hash/hash-table.ts @@ -6,7 +6,9 @@ * @license MIT License */ -export class HashTableNode { +import { HashFunction } from '../../types'; + +export class HashTableNode { key: K; value: V; next: HashTableNode | undefined; @@ -18,8 +20,6 @@ export class HashTableNode { } } -import { HashFunction } from '../../types'; - export class HashTable { protected static readonly DEFAULT_CAPACITY = 16; protected static readonly LOAD_FACTOR = 0.75; diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index 60b90ae..a383bd1 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -5,8 +5,7 @@ * @license MIT License */ -import type { Comparator, DFSOrderPattern, ElementCallback } from '../../types'; -import { HeapOptions } from "../../types"; +import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types'; import { IterableElementBase } from "../base"; export class Heap extends IterableElementBase { diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index b99c2ce..4530814 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -1,5 +1,5 @@ import { IterableElementBase } from "../base"; -import { ElementCallback } from "../../types"; +import type { ElementCallback } from "../../types"; /** * data-structure-typed diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index b22bb2b..23eda14 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -1,5 +1,5 @@ import { IterableElementBase } from "../base"; -import { ElementCallback } from "../../types"; +import type { ElementCallback } from "../../types"; /** * data-structure-typed diff --git a/src/data-structures/priority-queue/priority-queue.ts b/src/data-structures/priority-queue/priority-queue.ts index 6978221..0f3a5cc 100644 --- a/src/data-structures/priority-queue/priority-queue.ts +++ b/src/data-structures/priority-queue/priority-queue.ts @@ -7,7 +7,7 @@ */ import { Heap } from '../heap'; -import { PriorityQueueOptions } from '../../types'; +import type { PriorityQueueOptions } from '../../types'; export class PriorityQueue extends Heap { constructor(elements?: Iterable, options?: PriorityQueueOptions) { diff --git a/src/data-structures/queue/deque.ts b/src/data-structures/queue/deque.ts index 432691f..9c86969 100644 --- a/src/data-structures/queue/deque.ts +++ b/src/data-structures/queue/deque.ts @@ -7,7 +7,7 @@ */ -import { ElementCallback, IterableWithSizeOrLength } from "../../types"; +import type { ElementCallback, IterableWithSizeOrLength } from "../../types"; import { calcMinUnitsRequired, rangeCheck } from "../../utils"; import { IterableElementBase } from "../base"; diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index 1a3df3a..ac802e1 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -5,7 +5,7 @@ */ import { SinglyLinkedList } from '../linked-list'; import { IterableElementBase } from "../base"; -import { ElementCallback } from "../../types"; +import type { ElementCallback } from "../../types"; export class Queue extends IterableElementBase { /** diff --git a/src/data-structures/stack/stack.ts b/src/data-structures/stack/stack.ts index d03a77c..30f98f2 100644 --- a/src/data-structures/stack/stack.ts +++ b/src/data-structures/stack/stack.ts @@ -1,5 +1,5 @@ import { IterableElementBase } from "../base"; -import { ElementCallback } from "../../types"; +import type { ElementCallback } from "../../types"; /** * @license MIT diff --git a/src/data-structures/trie/trie.ts b/src/data-structures/trie/trie.ts index 42d7df9..baec3ad 100644 --- a/src/data-structures/trie/trie.ts +++ b/src/data-structures/trie/trie.ts @@ -7,7 +7,7 @@ */ import { IterableElementBase } from "../base"; -import { ElementCallback } from "../../types"; +import type { ElementCallback } from "../../types"; /** * TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes, diff --git a/src/interfaces/binary-tree.ts b/src/interfaces/binary-tree.ts index 16143bd..44c025d 100644 --- a/src/interfaces/binary-tree.ts +++ b/src/interfaces/binary-tree.ts @@ -1,11 +1,11 @@ import { BinaryTree, BinaryTreeNode } from '../data-structures'; import { + BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, - BiTreeDeleteResult, BTNCallback, - BTNodeExemplar, + BTNExemplar, } from '../types'; export interface IBinaryTree = BinaryTreeNodeNested, TREE extends BinaryTree = BinaryTreeNested> { @@ -13,9 +13,9 @@ export interface IBinaryTree>): TREE; - add(keyOrNodeOrEntry: BTNodeExemplar, value?: V, count?: number): N | null | undefined; + add(keyOrNodeOrEntry: BTNExemplar, value?: V, count?: number): N | null | undefined; - addMany(nodes: Iterable>, values?: Iterable): (N | null | undefined)[]; + addMany(nodes: Iterable>, values?: Iterable): (N | null | undefined)[]; - delete>(identifier: ReturnType | null, callback: C): BiTreeDeleteResult[]; + delete>(identifier: ReturnType | null, callback: C): BinaryTreeDeleteResult[]; } diff --git a/src/types/common.ts b/src/types/common.ts index fe3b5de..5780a57 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -27,11 +27,11 @@ export type IterableWithSizeOrLength = IterableWithSize | IterableWithLeng export type BinaryTreePrintOptions = { isShowUndefined?: boolean, isShowNull?: boolean, isShowRedBlackNIL?: boolean } -export type BTNodeEntry = [K | null | undefined, V | undefined]; +export type BTNEntry = [K | null | undefined, V | undefined]; -export type BTNodeKeyOrNode = K | null | undefined | N; +export type BTNKeyOrNode = K | null | undefined | N; -export type BTNodeExemplar = BTNodeEntry | BTNodeKeyOrNode +export type BTNExemplar = BTNEntry | BTNKeyOrNode export type BTNodePureExemplar = [K, V | undefined] | BTNodePureKeyOrNode diff --git a/src/types/data-structures/binary-tree/binary-tree.ts b/src/types/data-structures/binary-tree/binary-tree.ts index 9babb15..6610940 100644 --- a/src/types/data-structures/binary-tree/binary-tree.ts +++ b/src/types/data-structures/binary-tree/binary-tree.ts @@ -22,7 +22,7 @@ export enum FamilyPosition { MAL_NODE = 'MAL_NODE' } -export type BiTreeDeleteResult = { deleted: N | null | undefined; needBalanced: N | null | undefined }; +export type BinaryTreeDeleteResult = { deleted: N | null | undefined; needBalanced: N | null | undefined }; export type BinaryTreeNodeNested = BinaryTreeNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>