diff --git a/src/data-structures/base/iterable-base.ts b/src/data-structures/base/iterable-base.ts index a5c3990..66ac832 100644 --- a/src/data-structures/base/iterable-base.ts +++ b/src/data-structures/base/iterable-base.ts @@ -1,6 +1,6 @@ -import { ElementCallback, PairCallback, ReduceElementCallback, ReducePairCallback } from "../../types"; +import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from "../../types"; -export abstract class IterablePairBase { +export abstract class IterableEntryBase { /** * Time Complexity: O(n) @@ -87,7 +87,7 @@ export abstract class IterablePairBase { * @returns The `every` method is returning a boolean value. It returns `true` if every element in * the collection satisfies the provided predicate function, and `false` otherwise. */ - every(predicate: PairCallback, thisArg?: any): boolean { + every(predicate: EntryCallback, thisArg?: any): boolean { let index = 0; for (const item of this) { if (!predicate.call(thisArg, item[1], item[0], index++, this)) { @@ -116,7 +116,7 @@ export abstract class IterablePairBase { * @returns a boolean value. It returns true if the predicate function returns true for any pair in * the collection, and false otherwise. */ - some(predicate: PairCallback, thisArg?: any): boolean { + some(predicate: EntryCallback, thisArg?: any): boolean { let index = 0; for (const item of this) { if (predicate.call(thisArg, item[1], item[0], index++, this)) { @@ -143,7 +143,7 @@ export abstract class IterablePairBase { * specify the value of `this` within the callback function. If `thisArg` is provided, it will be * used as the `this` value when calling the callback function. If `thisArg` is not provided, ` */ - forEach(callbackfn: PairCallback, thisArg?: any): void { + forEach(callbackfn: EntryCallback, thisArg?: any): void { let index = 0; for (const item of this) { const [key, value] = item; @@ -171,7 +171,7 @@ export abstract class IterablePairBase { * @returns The `reduce` method is returning the final value of the accumulator after iterating over * all the elements in the collection. */ - reduce(callbackfn: ReducePairCallback, initialValue: U): U { + reduce(callbackfn: ReduceEntryCallback, initialValue: U): U { let accumulator = initialValue; let index = 0; for (const item of this) { diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index 51fd590..84932c9 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -95,19 +95,10 @@ export class AVLTree = AVLTreeN * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. */ - /** - * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. - * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. - * - * The function overrides the add method of a binary tree node and balances the tree after inserting - * a new node. - * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an - * entry. - * @returns The method is returning either the inserted node or `undefined`. - */ - override add(keyOrNodeOrEntry: BTNodeExemplar): N | undefined { + + override add(keyOrNodeOrEntry: BTNodeExemplar, value?: V): N | undefined { if (keyOrNodeOrEntry === null) return undefined; - const inserted = super.add(keyOrNodeOrEntry); + const inserted = super.add(keyOrNodeOrEntry, value); if (inserted) this._balancePath(inserted); return inserted; } diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 52dd980..a97a651 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -19,15 +19,15 @@ import { BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, + EntryCallback, FamilyPosition, IterationType, - NodeDisplayLayout, - PairCallback + NodeDisplayLayout } from '../../types'; import { IBinaryTree } from '../../interfaces'; import { trampoline } from '../../utils'; import { Queue } from '../queue'; -import { IterablePairBase } from "../base"; +import { IterableEntryBase } from "../base"; /** * Represents a node in a binary tree. @@ -104,7 +104,7 @@ export class BinaryTreeNode * 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right. */ -export class BinaryTree = BinaryTreeNode>, TREE extends BinaryTree = BinaryTree>> extends IterablePairBase +export class BinaryTree = BinaryTreeNode>, TREE extends BinaryTree = BinaryTree>> extends IterableEntryBase implements IBinaryTree { iterationType = IterationType.ITERATIVE @@ -183,14 +183,7 @@ export class BinaryTree = Bi return exemplar instanceof BinaryTreeNode; } - /** - * The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node - * object. - * @param exemplar - BTNodeExemplar - A generic type representing the exemplar parameter of the - * function. It can be any type. - * @returns a value of type `N` (which represents a node), or `null`, or `undefined`. - */ - exemplarToNode(exemplar: BTNodeExemplar): N | null | undefined { + exemplarToNode(exemplar: BTNodeExemplar, value?: V): N | null | undefined { if (exemplar === undefined) return; let node: N | null | undefined; @@ -208,7 +201,7 @@ export class BinaryTree = Bi } else if (this.isNode(exemplar)) { node = exemplar; } else if (this.isNotNodeInstance(exemplar)) { - node = this.createNode(exemplar); + node = this.createNode(exemplar, value); } else { return; } @@ -230,18 +223,11 @@ export class BinaryTree = Bi * Space Complexity O(1) */ - /** - * Time Complexity O(log n) - O(n) - * Space Complexity O(1) - * - * The `add` function adds a new node to a binary tree, either by key or by providing a node object. - * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be one of the following: - * @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`. - */ - add(keyOrNodeOrEntry: BTNodeExemplar): N | null | undefined { + + add(keyOrNodeOrEntry: BTNodeExemplar, value?: V): N | null | undefined { let inserted: N | null | undefined; - const newNode = this.exemplarToNode(keyOrNodeOrEntry); + const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return; const _bfs = (root: N, newNode: N | null): N | undefined | null => { @@ -1775,7 +1761,7 @@ export class BinaryTree = Bi * @returns The `filter` method is returning a new tree object that contains the key-value pairs that * pass the given predicate function. */ - filter(predicate: PairCallback, thisArg?: any) { + filter(predicate: EntryCallback, thisArg?: any) { const newTree = this.createTree(); let index = 0; for (const [key, value] of this) { @@ -1806,7 +1792,7 @@ export class BinaryTree = Bi * will be used as the `this` value when the callback function is called. If you don't pass a value * @returns The `map` method is returning a new tree object. */ - map(callback: PairCallback, thisArg?: any) { + map(callback: EntryCallback, thisArg?: any) { const newTree = this.createTree(); let index = 0; for (const [key, value] of this) { diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 1f5b4d1..c1c9fb1 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -154,13 +154,8 @@ export class BST = BSTNode`. - * @returns a variable `node` which is of type `N` or `undefined`. - */ - override exemplarToNode(exemplar: BTNodeExemplar): N | undefined { + + override exemplarToNode(exemplar: BTNodeExemplar, value?: V): N | undefined { let node: N | undefined; if (exemplar === null || exemplar === undefined) { return; @@ -174,7 +169,7 @@ export class BST = BSTNode = BSTNode): N | undefined { - const newNode = this.exemplarToNode(keyOrNodeOrEntry); + override add(keyOrNodeOrEntry: BTNodeExemplar, value?: V): N | undefined { + const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return; if (this.root === undefined) { diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index a08f3a8..828ad59 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -115,15 +115,7 @@ export class RedBlackTree return exemplar instanceof RedBlackTreeNode; } - /** - * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid, - * otherwise it returns undefined. - * @param exemplar - BTNodeExemplar - A generic type representing an exemplar of a binary tree - * node. It can be either a node itself, an entry (key-value pair), a node key, or any other value - * that is not a valid exemplar. - * @returns a variable `node` which is of type `N | undefined`. - */ - override exemplarToNode(exemplar: BTNodeExemplar): N | undefined { + override exemplarToNode(exemplar: BTNodeExemplar, value?: V): N | undefined { let node: N | undefined; if (exemplar === null || exemplar === undefined) { @@ -138,7 +130,7 @@ export class RedBlackTree node = this.createNode(key, value, RBTNColor.RED); } } else if (this.isNotNodeInstance(exemplar)) { - node = this.createNode(exemplar, undefined, RBTNColor.RED); + node = this.createNode(exemplar, value, RBTNColor.RED); } else { return; } @@ -150,14 +142,8 @@ export class RedBlackTree * Space Complexity: O(1) */ - /** - * The function adds a node to a Red-Black Tree data structure. - * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following: - * @returns The method `add` returns either an instance of `N` (the node that was added) or - * `undefined`. - */ - override add(keyOrNodeOrEntry: BTNodeExemplar): N | undefined { - const newNode = this.exemplarToNode(keyOrNodeOrEntry); + override add(keyOrNodeOrEntry: BTNodeExemplar, value?: V): N | undefined { + const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return; newNode.left = this.Sentinel; diff --git a/src/data-structures/binary-tree/tree-multimap.ts b/src/data-structures/binary-tree/tree-multimap.ts index 95ab9a5..62d18b1 100644 --- a/src/data-structures/binary-tree/tree-multimap.ts +++ b/src/data-structures/binary-tree/tree-multimap.ts @@ -85,15 +85,8 @@ export class TreeMultimap return exemplar instanceof TreeMultimapNode; } - /** - * The function `exemplarToNode` converts an exemplar object into a node object. - * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar`, where `V` represents - * the value type and `N` represents the node type. - * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of - * times the node should be created. If not provided, it defaults to 1. - * @returns a value of type `N` (the generic type parameter) or `undefined`. - */ - override exemplarToNode(exemplar: BTNodeExemplar, count = 1): N | undefined { + + override exemplarToNode(exemplar: BTNodeExemplar, value?: V, count = 1): N | undefined { let node: N | undefined; if (exemplar === undefined || exemplar === null) { return; @@ -107,7 +100,7 @@ export class TreeMultimap node = this.createNode(key, value, count); } } else if (this.isNotNodeInstance(exemplar)) { - node = this.createNode(exemplar, undefined, count); + node = this.createNode(exemplar, value, count); } else { return; } @@ -119,20 +112,8 @@ export class TreeMultimap * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. */ - /** - * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. - * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. - * - * The `add` function overrides the base class `add` function to add a new node to the tree multimap - * and update the count. - * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following: - * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of - * times the key or node or entry should be added to the multimap. If not provided, the default value - * is 1. - * @returns either a node (`N`) or `undefined`. - */ - override add(keyOrNodeOrEntry: BTNodeExemplar, count = 1): N | undefined { - const newNode = this.exemplarToNode(keyOrNodeOrEntry, count); + override add(keyOrNodeOrEntry: BTNodeExemplar, value?: V, count = 1): N | undefined { + const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count); if (newNode === undefined) return; const orgNodeCount = newNode?.count || 0; @@ -190,7 +171,7 @@ export class TreeMultimap if (l > r) return; const m = l + Math.floor((r - l) / 2); const midNode = sorted[m]; - this.add([midNode.key, midNode.value], midNode.count); + this.add(midNode.key, midNode.value, midNode.count); buildBalanceBST(l, m - 1); buildBalanceBST(m + 1, r); }; @@ -206,7 +187,7 @@ export class TreeMultimap if (l <= r) { const m = l + Math.floor((r - l) / 2); const midNode = sorted[m]; - this.add([midNode.key, midNode.value], midNode.count); + this.add(midNode.key, midNode.value, midNode.count); stack.push([m + 1, r]); stack.push([l, m - 1]); } @@ -327,7 +308,7 @@ export class TreeMultimap */ override clone(): TREE { const cloned = this.createTree(); - this.bfs(node => cloned.add([node.key, node.value], node.count)); + this.bfs(node => cloned.add(node.key, node.value, node.count)); return cloned; } diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 4fd6a47..9305d49 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -8,10 +8,10 @@ import { uuidV4 } from '../../utils'; import { PriorityQueue } from '../priority-queue'; import type { DijkstraResult, VertexKey } from '../../types'; -import { PairCallback } from "../../types"; +import { EntryCallback } from "../../types"; import { IGraph } from '../../interfaces'; import { Queue } from '../queue'; -import { IterablePairBase } from "../base"; +import { IterableEntryBase } from "../base"; export abstract class AbstractVertex { key: VertexKey; @@ -66,7 +66,7 @@ export abstract class AbstractGraph< E = any, VO extends AbstractVertex = AbstractVertex, EO extends AbstractEdge = AbstractEdge -> extends IterablePairBase implements IGraph { +> extends IterableEntryBase implements IGraph { constructor() { super(); } @@ -1191,7 +1191,7 @@ export abstract class AbstractGraph< * @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]` * that satisfy the given predicate function. */ - filter(predicate: PairCallback, thisArg?: any): [VertexKey, V | undefined][] { + filter(predicate: EntryCallback, thisArg?: any): [VertexKey, V | undefined][] { const filtered: [VertexKey, V | undefined][] = []; let index = 0; for (const [key, value] of this) { @@ -1221,7 +1221,7 @@ export abstract class AbstractGraph< * used as the `this` value when calling the callback function. If `thisArg` is not provided, ` * @returns The `map` function is returning an array of type `T[]`. */ - map(callback: PairCallback, thisArg?: any): T[] { + map(callback: EntryCallback, thisArg?: any): T[] { const mapped: T[] = []; let index = 0; for (const [key, value] of this) { diff --git a/src/data-structures/hash/hash-map.ts b/src/data-structures/hash/hash-map.ts index b119cd7..def23cf 100644 --- a/src/data-structures/hash/hash-map.ts +++ b/src/data-structures/hash/hash-map.ts @@ -7,10 +7,10 @@ */ import { isWeakKey, rangeCheck } from '../../utils'; -import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem, PairCallback } from '../../types'; -import { IterablePairBase } from "../base"; +import { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types'; +import { IterableEntryBase } from "../base"; -export class HashMap extends IterablePairBase { +export class HashMap extends IterableEntryBase { protected _store: { [key: string]: HashMapStoreItem } = {}; protected _objMap: Map = new Map(); @@ -165,7 +165,7 @@ export class HashMap extends IterablePairBase { * @returns The `map` method is returning a new `HashMap` object with the transformed values based on * the provided callback function. */ - map(callbackfn: PairCallback, thisArg?: any): HashMap { + map(callbackfn: EntryCallback, thisArg?: any): HashMap { const resultMap = new HashMap(); let index = 0; for (const [key, value] of this) { @@ -195,7 +195,7 @@ export class HashMap extends IterablePairBase { * @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs * from the original `HashMap` that pass the provided `predicate` function. */ - filter(predicate: PairCallback, thisArg?: any): HashMap { + filter(predicate: EntryCallback, thisArg?: any): HashMap { const filteredMap = new HashMap(); let index = 0; for (const [key, value] of this) { @@ -248,7 +248,7 @@ export class HashMap extends IterablePairBase { } } -export class LinkedHashMap extends IterablePairBase { +export class LinkedHashMap extends IterableEntryBase { protected _noObjMap: Record> = {}; protected _objMap = new WeakMap>(); @@ -567,7 +567,7 @@ export class LinkedHashMap extends IterablePairBase { * @returns a new `LinkedHashMap` object that contains the key-value pairs from the original * `LinkedHashMap` object that satisfy the given predicate function. */ - filter(predicate: PairCallback, thisArg?: any): LinkedHashMap { + filter(predicate: EntryCallback, thisArg?: any): LinkedHashMap { const filteredMap = new LinkedHashMap(); let index = 0; for (const [key, value] of this) { @@ -601,7 +601,7 @@ export class LinkedHashMap extends IterablePairBase { * @returns a new `LinkedHashMap` object with the values mapped according to the provided callback * function. */ - map(callback: PairCallback, thisArg?: any): LinkedHashMap { + map(callback: EntryCallback, thisArg?: any): LinkedHashMap { const mappedMap = new LinkedHashMap(); let index = 0; for (const [key, value] of this) { diff --git a/src/interfaces/binary-tree.ts b/src/interfaces/binary-tree.ts index fb57e55..d732890 100644 --- a/src/interfaces/binary-tree.ts +++ b/src/interfaces/binary-tree.ts @@ -13,7 +13,7 @@ export interface IBinaryTree>): TREE; - add(keyOrNodeOrEntry: BTNodeExemplar, count?: number): N | null | undefined; + add(keyOrNodeOrEntry: BTNodeExemplar, value?: V, count?: number): N | null | undefined; addMany(nodes: Iterable>): (N | null | undefined)[]; diff --git a/src/types/data-structures/base/base.ts b/src/types/data-structures/base/base.ts index 8b8e111..81ec3e2 100644 --- a/src/types/data-structures/base/base.ts +++ b/src/types/data-structures/base/base.ts @@ -1,6 +1,6 @@ -import { IterableElementBase, IterablePairBase } from "../../../data-structures"; +import { IterableElementBase, IterableEntryBase } from "../../../data-structures"; -export type PairCallback = (value: V, key: K, index: number, container: IterablePairBase) => R; +export type EntryCallback = (value: V, key: K, index: number, container: IterableEntryBase) => R; export type ElementCallback = (element: V, index: number, container: IterableElementBase) => R; -export type ReducePairCallback = (accumulator: R, value: V, key: K, index: number, container: IterablePairBase) => R; +export type ReduceEntryCallback = (accumulator: R, value: V, key: K, index: number, container: IterableEntryBase) => R; export type ReduceElementCallback = (accumulator: R, element: V, index: number, container: IterableElementBase) => R; diff --git a/test/integration/bst.test.ts b/test/integration/bst.test.ts index 4cb8517..7511581 100644 --- a/test/integration/bst.test.ts +++ b/test/integration/bst.test.ts @@ -183,7 +183,7 @@ describe('Individual package BST operations test', () => { }); it('should perform various operations on a Binary Search Tree with object values', () => { - const objBST = new BST(); + const objBST = new BST(); expect(objBST).toBeInstanceOf(BST); objBST.add([11, { key: 11, keyA: 11 }]); objBST.add([3, { key: 3, keyA: 3 }]); diff --git a/test/unit/data-structures/binary-tree/binary-tree.test.ts b/test/unit/data-structures/binary-tree/binary-tree.test.ts index 6ce6d2f..a2bcc6f 100644 --- a/test/unit/data-structures/binary-tree/binary-tree.test.ts +++ b/test/unit/data-structures/binary-tree/binary-tree.test.ts @@ -569,7 +569,7 @@ describe('BinaryTree iterative methods test', () => { beforeEach(() => { binaryTree = new BinaryTree(); binaryTree.add([1, 'a']); - binaryTree.add([2, 'b']); + binaryTree.add(2, 'b'); binaryTree.add([3, 'c']); }); diff --git a/test/unit/data-structures/binary-tree/tree-multimap.test.ts b/test/unit/data-structures/binary-tree/tree-multimap.test.ts index d12d79b..5215093 100644 --- a/test/unit/data-structures/binary-tree/tree-multimap.test.ts +++ b/test/unit/data-structures/binary-tree/tree-multimap.test.ts @@ -605,9 +605,9 @@ describe('TreeMultimap iterative methods test', () => { let treeMM: TreeMultimap; beforeEach(() => { treeMM = new TreeMultimap(); - treeMM.add([1, 'a'], 10); - treeMM.add([2, 'b'], 10); - treeMM.add([3, 'c'], 1); + treeMM.add(1, 'a', 10); + treeMM.add([2, 'b'], undefined, 10); + treeMM.add([3, 'c'], undefined, 1); }); test('The node obtained by get Node should match the node type', () => {