diff --git a/src/data-structures/base/iterable-base.ts b/src/data-structures/base/iterable-base.ts index b9333e0..968438a 100644 --- a/src/data-structures/base/iterable-base.ts +++ b/src/data-structures/base/iterable-base.ts @@ -154,6 +154,105 @@ export abstract class IterableEntryBase { * Time Complexity: O(n) * Space Complexity: O(1) */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + * + * The `find` function iterates over the entries of a collection and returns the first value for + * which the callback function returns true. + * @param callbackfn - The callback function that will be called for each entry in the collection. It + * takes three arguments: the value of the entry, the key of the entry, and the index of the entry in + * the collection. It should return a boolean value indicating whether the current entry matches the + * desired condition. + * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value + * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will + * be passed as the `this` value to the `callbackfn` function. If `thisArg + * @returns The method `find` returns the value of the first element in the iterable that satisfies + * the provided callback function. If no element satisfies the callback function, `undefined` is + * returned. + */ + find(callbackfn: EntryCallback, thisArg?: any): [K, V] | undefined { + let index = 0; + for (const item of this) { + const [key, value] = item; + if (callbackfn.call(thisArg, value, key, index++, this)) return item; + } + return; + } + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + * + * The function checks if a given key exists in a collection. + * @param {K} key - The parameter "key" is of type K, which means it can be any type. It represents + * the key that we want to check for existence in the data structure. + * @returns a boolean value. It returns true if the key is found in the collection, and false + * otherwise. + */ + has(key: K): boolean { + for (const item of this) { + const [itemKey] = item; + if (itemKey === key) return true; + } + return false; + } + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + * + * The function checks if a given value exists in a collection. + * @param {V} value - The parameter "value" is the value that we want to check if it exists in the + * collection. + * @returns a boolean value, either true or false. + */ + hasValue(value: V): boolean { + for (const [, elementValue] of this) { + if (elementValue === value) return true; + } + return false; + } + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + * + * The `get` function retrieves the value associated with a given key from a collection. + * @param {K} key - K (the type of the key) - This parameter represents the key that is being + * searched for in the collection. + * @returns The `get` method returns the value associated with the specified key if it exists in the + * collection, otherwise it returns `undefined`. + */ + get(key: K): V | undefined { + for (const item of this) { + const [itemKey, value] = item; + if (itemKey === key) return value; + } + return; + } + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + */ + /** * Time Complexity: O(n) * Space Complexity: O(1) @@ -180,13 +279,6 @@ export abstract class IterableEntryBase { return accumulator; } - hasValue(value: V): boolean { - for (const [, elementValue] of this) { - if (elementValue === value) return true; - } - return false; - } - /** * Time Complexity: O(n) * Space Complexity: O(n) @@ -198,7 +290,7 @@ export abstract class IterableEntryBase { protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>; } -export abstract class IterableElementBase { +export abstract class IterableElementBase { /** * Time Complexity: O(n) * Space Complexity: O(1) @@ -212,7 +304,7 @@ export abstract class IterableElementBase { * allows the function to accept any number of arguments as an array. In this case, the `args` * parameter is used to pass any number of arguments to the `_getIterator` method. */ - * [Symbol.iterator](...args: any[]): IterableIterator { + * [Symbol.iterator](...args: any[]): IterableIterator { yield* this._getIterator(...args); } @@ -226,7 +318,7 @@ export abstract class IterableElementBase { * * The function returns an iterator that yields all the values in the object. */ - * values(): IterableIterator { + * values(): IterableIterator { for (const item of this) { yield item; } @@ -250,10 +342,10 @@ export abstract class IterableElementBase { * @returns The `every` method is returning a boolean value. It returns `true` if every element in * the array satisfies the provided predicate function, and `false` otherwise. */ - every(predicate: ElementCallback, thisArg?: any): boolean { + every(predicate: ElementCallback, thisArg?: any): boolean { let index = 0; for (const item of this) { - if (!predicate.call(thisArg, item as V, index++, this)) { + if (!predicate.call(thisArg, item, index++, this)) { return false; } } @@ -278,10 +370,10 @@ export abstract class IterableElementBase { * @returns a boolean value. It returns true if the predicate function returns true for any element * in the collection, and false otherwise. */ - some(predicate: ElementCallback, thisArg?: any): boolean { + some(predicate: ElementCallback, thisArg?: any): boolean { let index = 0; for (const item of this) { - if (predicate.call(thisArg, item as V, index++, this)) { + if (predicate.call(thisArg, item, index++, this)) { return true; } } @@ -292,6 +384,7 @@ export abstract class IterableElementBase { * Time Complexity: O(n) * Space Complexity: O(1) */ + /** * Time Complexity: O(n) * Space Complexity: O(1) @@ -305,13 +398,65 @@ export abstract class IterableElementBase { * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will * be passed as the `this` value to the `callbackfn` function. If `thisArg */ - forEach(callbackfn: ElementCallback, thisArg?: any): void { + forEach(callbackfn: ElementCallback, thisArg?: any): void { let index = 0; for (const item of this) { - callbackfn.call(thisArg, item as V, index++, this); + callbackfn.call(thisArg, item, index++, this); } } + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + * + * The `find` function iterates over the elements of an array-like object and returns the first + * element that satisfies the provided callback function. + * @param callbackfn - The callbackfn parameter is a function that will be called for each element in + * the array. It takes three arguments: the current element being processed, the index of the current + * element, and the array itself. The function should return a boolean value indicating whether the + * current element matches the desired condition. + * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value + * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will + * be passed as the `this` value to the `callbackfn` function. If `thisArg + * @returns The `find` method returns the first element in the array that satisfies the provided + * callback function. If no element satisfies the callback function, `undefined` is returned. + */ + find(callbackfn: ElementCallback, thisArg?: any): E | undefined { + let index = 0; + for (const item of this) { + if (callbackfn.call(thisArg, item, index++, this)) return item; + } + + return; + } + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(1) + * + * The function checks if a given element exists in a collection. + * @param {E} element - The parameter "element" is of type E, which means it can be any type. It + * represents the element that we want to check for existence in the collection. + * @returns a boolean value. It returns true if the element is found in the collection, and false + * otherwise. + */ + has(element: E): boolean { + for (const ele of this) { + if (ele === element) return true; + } + return false; + } + /** * Time Complexity: O(n) * Space Complexity: O(1) @@ -329,11 +474,11 @@ export abstract class IterableElementBase { * @returns The `reduce` method is returning the final value of the accumulator after iterating over * all the elements in the array and applying the callback function to each element. */ - reduce(callbackfn: ReduceElementCallback, initialValue: U): U { + reduce(callbackfn: ReduceElementCallback, initialValue: U): U { let accumulator = initialValue; let index = 0; for (const item of this) { - accumulator = callbackfn(accumulator, item as V, index++, this); + accumulator = callbackfn(accumulator, item as E, index++, this); } return accumulator; } @@ -346,5 +491,5 @@ export abstract class IterableElementBase { console.log([...this]); } - protected abstract _getIterator(...args: any[]): IterableIterator; + protected abstract _getIterator(...args: any[]): IterableIterator; } diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 88c3a89..72de2cd 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -172,14 +172,14 @@ export class BinaryTree< } /** - * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object. + * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object. * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry`. * @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 keyOrNodeOrEntry node. If no value + * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value * is provided, it will be `undefined`. * @returns a value of type N (node), or null, or undefined. */ - exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): N | null | undefined { + keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): N | null | undefined { if (keyOrNodeOrEntry === undefined) return; let node: N | null | undefined; @@ -308,7 +308,7 @@ export class BinaryTree< * @returns The function `add` returns either a node (`N`), `null`, or `undefined`. */ add(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): boolean { - const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); + const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return false; // If the tree is empty, directly set the new node as the root node @@ -768,21 +768,21 @@ export class BinaryTree< * Space Complexity: O(log n). */ - has>( + override has>( identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry, iterationType?: IterationType ): boolean; - has>( + override has>( identifier: N | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry, iterationType?: IterationType ): boolean; - has>( + override has>( identifier: ReturnType | null | undefined, callback: C, beginRoot?: KeyOrNodeOrEntry, @@ -810,7 +810,7 @@ export class BinaryTree< * be performed in a pre-order, in-order, or post-order manner. * @returns a boolean value. */ - has>( + override has>( identifier: ReturnType | null | undefined, callback: C = this._defaultOneParamCallback as C, beginRoot: KeyOrNodeOrEntry = this.root, @@ -925,21 +925,21 @@ export class BinaryTree< } } - get>( + override get>( identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry, iterationType?: IterationType ): V | undefined; - get>( + override get>( identifier: N | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry, iterationType?: IterationType ): V | undefined; - get>( + override get>( identifier: ReturnType, callback: C, beginRoot?: KeyOrNodeOrEntry, @@ -968,7 +968,7 @@ export class BinaryTree< * @returns The value of the node with the given identifier is being returned. If the node is not * found, `undefined` is returned. */ - get>( + override get>( identifier: ReturnType | null | undefined, callback: C = this._defaultOneParamCallback as C, beginRoot: KeyOrNodeOrEntry = this.root, diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index d94b78d..6a8d247 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -151,14 +151,14 @@ export class BST< } /** - * The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid, + * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid, * otherwise it returns undefined. * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry`, 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 keyOrNodeOrEntry node. + * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. * @returns a node of type N or undefined. */ - override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): N | undefined { + override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): N | undefined { let node: N | undefined; if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) { return; @@ -240,7 +240,7 @@ export class BST< * node was not added. */ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): boolean { - const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); + const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return false; 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 2ba0ee9..cc577e3 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -111,14 +111,14 @@ export class RedBlackTree< } /** - * The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible. + * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible. * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry`, 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 keyOrNodeOrEntry node. If a value + * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry 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(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): N | undefined { + override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): N | undefined { let node: N | undefined; if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) { @@ -179,7 +179,7 @@ export class RedBlackTree< * @returns The method `add` returns either the newly added node (`N`) or `undefined`. */ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): boolean { - const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); + const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return false; 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 f7dea55..38c8656 100644 --- a/src/data-structures/binary-tree/tree-multimap.ts +++ b/src/data-structures/binary-tree/tree-multimap.ts @@ -88,7 +88,7 @@ export class TreeMultimap< } /** - * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object. + * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object. * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry`, which means it * can be one of the following: * @param {V} [value] - The `value` parameter is an optional argument that represents the value @@ -98,7 +98,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(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V, count = 1): N | undefined { + override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V, count = 1): N | undefined { let node: N | undefined; if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) { return; @@ -152,7 +152,7 @@ export class TreeMultimap< * was not successful. */ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V, count = 1): boolean { - const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count); + const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count); if (newNode === undefined) return false; const orgNodeCount = newNode?.count || 0; diff --git a/src/data-structures/hash/hash-map.ts b/src/data-structures/hash/hash-map.ts index dca1144..17ea30d 100644 --- a/src/data-structures/hash/hash-map.ts +++ b/src/data-structures/hash/hash-map.ts @@ -59,24 +59,46 @@ export class HashMap extends IterableEntryBase extends IterableEntryBase extends IterableEntryBase extends IterableEntryBase extends IterableEntryBase extends IterableEntryBase { +export class LinkedHashMap extends IterableEntryBase { protected _noObjMap: Record> = {}; protected _objMap = new WeakMap>(); protected _head: HashMapLinkedNode; protected _tail: HashMapLinkedNode; protected readonly _sentinel: HashMapLinkedNode; - constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions) { + /** + * The constructor initializes a LinkedHashMap object with an optional raw collection and options. + * @param rawCollection - The `rawCollection` parameter is an iterable collection of elements. It is + * used to initialize the HashMapLinked instance with key-value pairs. Each element in the + * `rawCollection` is converted to a key-value pair using the `toEntryFn` function (if provided) and + * then added to the HashMap + * @param [options] - The `options` parameter is an optional object that can contain the following + * properties: + */ + constructor(rawCollection: Iterable = [], options?: LinkedHashMapOptions) { super(); this._sentinel = >{}; this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel; if (options) { - const { hashFn, objHashFn } = options; + const { hashFn, objHashFn, toEntryFn } = options; if (hashFn) this._hashFn = hashFn; if (objHashFn) this._objHashFn = objHashFn; + + if (toEntryFn) { + this._toEntryFn = toEntryFn; + } } - if (entries) { - for (const el of entries) { - this.set(el[0], el[1]); + if (rawCollection) { + for (const el of rawCollection) { + const [key, value] = this.toEntryFn(el); + this.set(key, value); } } } + protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => { + if (this.isEntry(rawElement)) { + // TODO, For performance optimization, it may be necessary to only inspect the first element traversed. + return rawElement; + } else { + throw new Error( + "If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified." + ); + } + }; + + /** + * The function returns the value of the _toEntryFn property. + * @returns The function being returned is `this._toEntryFn`. + */ + get toEntryFn() { + return this._toEntryFn; + } + protected _size = 0; + /** + * The function returns the size of an object. + * @returns The size of the object. + */ get size() { return this._size; } @@ -425,7 +492,30 @@ export class LinkedHashMap extends IterableEntryBase { return true; } - has(key: K): boolean { + /** + * The function `setMany` takes an iterable collection, converts each element into a key-value pair + * using a provided function, and sets each key-value pair in the current object, returning an array + * of booleans indicating the success of each set operation. + * @param rawCollection - The rawCollection parameter is an iterable collection of elements of type + * R. + * @returns The `setMany` function returns an array of booleans. + */ + setMany(rawCollection: Iterable): boolean[] { + const results: boolean[] = []; + for (const rawEle of rawCollection) { + const [key, value] = this.toEntryFn(rawEle); + results.push(this.set(key, value)); + } + return results; + } + + /** + * The function checks if a given key exists in a map, using different logic depending on whether the + * key is a weak key or not. + * @param {K} key - The `key` parameter is the key that is being checked for existence in the map. + * @returns The method `has` is returning a boolean value. + */ + override has(key: K): boolean { if (isWeakKey(key)) { const hash = this._objHashFn(key); return this._objMap.has(hash); @@ -435,15 +525,6 @@ export class LinkedHashMap extends IterableEntryBase { } } - setMany(entries: Iterable<[K, V]>): boolean[] { - const results: boolean[] = []; - for (const entry of entries) { - const [key, value] = entry; - results.push(this.set(key, value)); - } - return results; - } - /** * Time Complexity: O(1) * Space Complexity: O(1) @@ -457,7 +538,7 @@ export class LinkedHashMap extends IterableEntryBase { * property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object * using the key itself. If the key is not found, `undefined` is */ - get(key: K): V | undefined { + override get(key: K): V | undefined { if (isWeakKey(key)) { const hash = this._objHashFn(key); const node = this._objMap.get(hash); @@ -473,14 +554,14 @@ export class LinkedHashMap extends IterableEntryBase { * Time Complexity: O(n), where n is the index. * Space Complexity: O(1) * - * The function `getAt` retrieves the key-value pair at a specified index in a linked list. + * The function `at` retrieves the key-value pair at a specified index in a linked list. * @param {number} index - The index parameter is a number that represents the position of the * element we want to retrieve from the data structure. - * @returns The method `getAt(index: number)` is returning an array containing the key-value pair at + * @returns The method `at(index: number)` is returning an array containing the key-value pair at * the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`, * where `K` is the key and `V` is the value. */ - getAt(index: number): V | undefined { + at(index: number): V | undefined { rangeCheck(index, 0, this._size - 1); let node = this._head; while (index--) { @@ -532,7 +613,7 @@ export class LinkedHashMap extends IterableEntryBase { } /** - * Time Complexity: O(n), where n is the index. + * Time Complexity: O(n) * Space Complexity: O(1) * * The `deleteAt` function deletes a node at a specified index in a linked list. @@ -561,6 +642,16 @@ export class LinkedHashMap extends IterableEntryBase { return this._size === 0; } + /** + * The function checks if a given element is an array with exactly two elements. + * @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any + * data type. + * @returns a boolean value. + */ + isEntry(rawElement: any): rawElement is [K, V] { + return Array.isArray(rawElement) && rawElement.length === 2; + } + /** * Time Complexity: O(1) * Space Complexity: O(1) @@ -573,6 +664,20 @@ export class LinkedHashMap extends IterableEntryBase { this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel; } + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + * + * The `clone` function creates a new instance of a `LinkedHashMap` with the same key-value pairs as + * the original. + * @returns The `clone()` method is returning a new instance of `LinkedHashMap` that is a clone + * of the original `LinkedHashMap` object. + */ clone(): LinkedHashMap { const cloned = new LinkedHashMap([], { hashFn: this._hashFn, objHashFn: this._objHashFn }); for (const entry of this) { @@ -638,26 +743,33 @@ export class LinkedHashMap extends IterableEntryBase { } /** - * Time Complexity: O(n) - * Space Complexity: O(n) + * Time Complexity: O(1) + * Space Complexity: O(1) */ + /** + * Time Complexity: O(1) + * Space Complexity: O(1) + * + * The put function sets a value in a data structure using a specified key. + * @param {K} key - The key parameter is of type K, which represents the type of the key being passed + * to the function. + * @param {V} value - The value parameter represents the value that you want to associate with the + * specified key in the data structure. + * @returns The method is returning a boolean value. + */ put(key: K, value: V): boolean { return this.set(key, value); } - /** - * Time Complexity: O(n) - * Space Complexity: O(n) - */ - protected _hashFn: (key: K) => string = (key: K) => String(key); protected _objHashFn: (key: K) => object = (key: K) => key; /** - * Time Complexity: O(n), where n is the number of entries in the LinkedHashMap. + * Time Complexity: O(n) * Space Complexity: O(1) + * where n is the number of entries in the LinkedHashMap. * * The above function is an iterator that yields key-value pairs from a linked list. */ diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index 8e2d9fe..80bdd52 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -21,6 +21,16 @@ import { IterableElementBase } from '../base'; * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance. */ export class Heap extends IterableElementBase { + /** + * The constructor initializes a heap data structure with optional elements and options. + * @param elements - The `elements` parameter is an iterable object that contains the initial + * elements to be added to the heap. It is an optional parameter and if not provided, the heap will + * be initialized as empty. + * @param [options] - The `options` parameter is an optional object that can contain additional + * configuration options for the heap. In this case, it is used to specify a custom comparator + * function for comparing elements in the heap. The comparator function is used to determine the + * order of elements in the heap. + */ constructor(elements: Iterable = [], options?: HeapOptions) { super(); @@ -45,12 +55,20 @@ export class Heap extends IterableElementBase { } }; + /** + * The function returns the value of the _comparator property. + * @returns The `_comparator` property is being returned. + */ get comparator() { return this._comparator; } protected _elements: E[] = []; + /** + * The function returns an array of elements. + * @returns The elements array is being returned. + */ get elements(): E[] { return this._elements; } @@ -81,12 +99,13 @@ export class Heap extends IterableElementBase { } /** - * Time Complexity: O(log n), where n is the number of elements in the heap. + * Time Complexity: O(log n) * Space Complexity: O(1) + * where n is the number of elements in the heap. */ /** - * Time Complexity: O(log n), where n is the number of elements in the heap. + * Time Complexity: O(log n) * Space Complexity: O(1) * * Insert an element into the heap and maintain the heap properties. @@ -98,12 +117,13 @@ export class Heap extends IterableElementBase { } /** - * Time Complexity: O(log n), where n is the number of elements in the heap. + * Time Complexity: O(log n) * Space Complexity: O(1) + * where n is the number of elements in the heap. */ /** - * Time Complexity: O(log n), where n is the number of elements in the heap. + * Time Complexity: O(log n) * Space Complexity: O(1) * * Remove and return the top element (smallest or largest element) from the heap. @@ -121,6 +141,9 @@ export class Heap extends IterableElementBase { } /** + * Time Complexity: O(1) + * Space Complexity: O(1) + * * Peek at the top element of the heap without removing it. * @returns The top element or undefined if the heap is empty. */ @@ -391,6 +414,9 @@ export class Heap extends IterableElementBase { return mappedHeap; } + /** + * The function `_getIterator` returns an iterable iterator for the elements in the class. + */ protected* _getIterator(): IterableIterator { for (const element of this.elements) { yield element; @@ -458,6 +484,16 @@ export class FibonacciHeapNode { parent?: FibonacciHeapNode; marked: boolean; + /** + * The constructor function initializes an object with an element and a degree, and sets the marked + * property to false. + * @param {E} element - The "element" parameter represents the value or data that will be stored in + * the node of a data structure. It can be any type of data, such as a number, string, object, or + * even another data structure. + * @param [degree=0] - The degree parameter represents the degree of the element in a data structure + * called a Fibonacci heap. The degree of a node is the number of children it has. By default, the + * degree is set to 0 when a new node is created. + */ constructor(element: E, degree = 0) { this.element = element; this.degree = degree; @@ -466,6 +502,13 @@ export class FibonacciHeapNode { } export class FibonacciHeap { + /** + * The constructor function initializes a FibonacciHeap object with an optional comparator function. + * @param [comparator] - The `comparator` parameter is an optional argument that represents a + * function used to compare elements in the FibonacciHeap. If a comparator function is provided, it + * will be used to determine the order of elements in the heap. If no comparator function is + * provided, a default comparator function will be used. + */ constructor(comparator?: Comparator) { this.clear(); this._comparator = comparator || this._defaultComparator; @@ -477,24 +520,41 @@ export class FibonacciHeap { protected _root?: FibonacciHeapNode; + /** + * The function returns the root node of a Fibonacci heap. + * @returns The method is returning either a FibonacciHeapNode object or undefined. + */ get root(): FibonacciHeapNode | undefined { return this._root; } protected _size = 0; + /** + * The function returns the size of an object. + * @returns The size of the object, which is a number. + */ get size(): number { return this._size; } protected _min?: FibonacciHeapNode; + /** + * The function returns the minimum node in a Fibonacci heap. + * @returns The method is returning the minimum node of the Fibonacci heap, which is of type + * `FibonacciHeapNode`. If there is no minimum node, it will return `undefined`. + */ get min(): FibonacciHeapNode | undefined { return this._min; } protected _comparator: Comparator; + /** + * The function returns the comparator used for comparing elements. + * @returns The `_comparator` property of the object. + */ get comparator(): Comparator { return this._comparator; } @@ -808,12 +868,12 @@ export class FibonacciHeap { } /** - * Time Complexity: O(n log n), where n is the number of elements in the heap. + * Time Complexity: O(n log n) * Space Complexity: O(n) */ /** - * Time Complexity: O(n log n), where n is the number of elements in the heap. + * Time Complexity: O(n log n) * Space Complexity: O(n) * * Remove and return the top element (smallest or largest element) from the heap. diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index caaffc9..1275b8a 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -33,7 +33,10 @@ export class DoublyLinkedListNode { */ export class DoublyLinkedList extends IterableElementBase { /** - * The constructor initializes the linked list with an empty head, tail, and size. + * The constructor initializes a linked list with optional elements. + * @param elements - The `elements` parameter is an optional iterable object that contains the + * initial elements to be added to the data structure. It defaults to an empty array if no elements + * are provided. */ constructor(elements: Iterable = []) { super(); @@ -49,29 +52,43 @@ export class DoublyLinkedList extends IterableElementBase { protected _head: DoublyLinkedListNode | undefined; + /** + * The `head` function returns the first node of a doubly linked list. + * @returns The method `getHead()` returns either a `DoublyLinkedListNode` object or `undefined`. + */ get head(): DoublyLinkedListNode | undefined { return this._head; } protected _tail: DoublyLinkedListNode | undefined; + /** + * The `tail` function returns the last node of a doubly linked list. + * @returns The `get tail()` method is returning either a `DoublyLinkedListNode` object or + * `undefined`. + */ get tail(): DoublyLinkedListNode | undefined { return this._tail; } protected _size: number; + /** + * The function returns the size of an object. + * @returns The size of the object, which is a number. + */ get size(): number { return this._size; } /** - * Time Complexity: O(n), where n is the size of the input array. + * Time Complexity: O(n) * Space Complexity: O(n) + * where n is the number of elements in the linked list. */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty. @@ -87,7 +104,7 @@ export class DoublyLinkedList extends IterableElementBase { */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty. @@ -112,11 +129,7 @@ export class DoublyLinkedList extends IterableElementBase { * @returns The `fromArray` function returns a DoublyLinkedList object. */ static fromArray(data: E[]) { - const doublyLinkedList = new DoublyLinkedList(); - for (const item of data) { - doublyLinkedList.push(item); - } - return doublyLinkedList; + return new DoublyLinkedList(data); } /** @@ -173,7 +186,7 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) */ @@ -200,7 +213,7 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) */ @@ -227,21 +240,21 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * - * The `getAt` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds. + * The `at` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds. * @param {number} index - The index parameter is a number that represents the position of the element we want to * retrieve from the list. * @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds * or the linked list is empty, it will return undefined. */ - getAt(index: number): E | undefined { + at(index: number): E | undefined { if (index < 0 || index >= this.size) return undefined; let current = this.head; for (let i = 0; i < index; i++) { @@ -251,12 +264,12 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The function `getNodeAt` returns the node at a given index in a doubly linked list, or undefined if the index is out of @@ -276,12 +289,12 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the @@ -304,12 +317,12 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The `insert` function inserts a value at a specified index in a doubly linked list. @@ -343,12 +356,13 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) + * where n is the number of elements in the linked list. */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The `addBefore` function inserts a new value before an existing value or node in a doubly linked list. @@ -388,12 +402,12 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list. @@ -432,7 +446,7 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element. @@ -462,7 +476,7 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The `delete` function removes a node from a doubly linked list based on either the node itself or its value. @@ -498,7 +512,7 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(1) * Space Complexity: O(1) */ @@ -511,7 +525,7 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(1) * Space Complexity: O(1) */ @@ -525,38 +539,12 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. - * Space Complexity: O(1) - * - * The `find` function iterates through a linked list and returns the first element that satisfies a given condition. - * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This - * function is used to determine whether a particular value in the linked list satisfies a certain condition. - * @returns The method `find` returns the first element in the linked list that satisfies the condition specified by - * the callback function. If no element satisfies the condition, it returns `undefined`. - */ - find(callback: (value: E) => boolean): E | undefined { - let current = this.head; - while (current) { - if (callback(current.value)) { - return current.value; - } - current = current.next; - } - return undefined; - } - - /** - * Time Complexity: O(n), where n is the number of elements in the linked list. - * Space Complexity: O(1) - */ - - /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The function returns the index of the first occurrence of a given value in a linked list. @@ -579,12 +567,12 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(n) */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The `findBackward` function iterates through a linked list from the last node to the first node and returns the last @@ -606,12 +594,12 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(n) */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) * * The `reverse` function reverses the order of the elements in a doubly linked list. @@ -633,7 +621,7 @@ export class DoublyLinkedList extends IterableElementBase { */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(n) * * The `toArray` function converts a linked list into an array. @@ -650,12 +638,12 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(n) */ /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(n) * * The `toReversedArray` function converts a doubly linked list into an array in reverse order. @@ -671,6 +659,24 @@ export class DoublyLinkedList extends IterableElementBase { return array; } + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + * + * The `clone` function creates a new instance of the `DoublyLinkedList` class with the same values + * as the original list. + * @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which + * is a copy of the original list. + */ + clone(): DoublyLinkedList { + return new DoublyLinkedList(this.values()); + } + /** * Time Complexity: O(1) * Space Complexity: O(1) @@ -772,7 +778,7 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) */ @@ -789,7 +795,7 @@ export class DoublyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n), where n is the number of elements in the linked list. + * Time Complexity: O(n) * Space Complexity: O(1) */ diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index aa32809..654e7ab 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -25,7 +25,10 @@ export class SinglyLinkedListNode { export class SinglyLinkedList extends IterableElementBase { /** - * The constructor initializes the linked list with an empty head, tail, and length. + * The constructor initializes a new instance of a class with an optional iterable of elements. + * @param elements - The `elements` parameter is an optional iterable object that contains the + * initial elements to be added to the instance of the class. If no `elements` are provided, an empty + * array will be used as the default value. */ constructor(elements: Iterable = []) { super(); @@ -36,30 +39,44 @@ export class SinglyLinkedList extends IterableElementBase { protected _head: SinglyLinkedListNode | undefined; + /** + * The `head` function returns the first node of a singly linked list. + * @returns The method is returning either a SinglyLinkedListNode object or undefined. + */ get head(): SinglyLinkedListNode | undefined { return this._head; } protected _tail: SinglyLinkedListNode | undefined; + /** + * The `tail` function returns the last node of a singly linked list. + * @returns The method is returning either a SinglyLinkedListNode object or undefined. + */ get tail(): SinglyLinkedListNode | undefined { return this._tail; } protected _size: number = 0; + /** + * The function returns the size of an object. + * @returns The size of the object, which is a number. + */ get size(): number { return this._size; } /** - * Time Complexity: O(n) - Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list. - * Space Complexity: O(n) - Linear space, as it creates a new node for each element in the array. + * Time Complexity: O(n) + * Space Complexity: O(n) + * Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list. + * Linear space, as it creates a new node for each element in the array. */ /** - * Time Complexity: O(n) - Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list. - * Space Complexity: O(n) - Linear space, as it creates a new node for each element in the array. + * Time Complexity: O(n) + * Space Complexity: O(n) * * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given * array. @@ -75,13 +92,15 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments. - * Space Complexity: O(1) - Constant space, as it only creates a new node. + * Time Complexity: O(1) + * Space Complexity: O(1) + * Constant time, as it involves basic pointer adjustments. + * Constant space, as it only creates a new node. */ /** - * Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments. - * Space Complexity: O(1) - Constant space, as it only creates a new node. + * Time Complexity: O(1) + * Space Complexity: O(1) * * The `push` function adds a new node with the given value to the end of a singly linked list. * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of @@ -101,13 +120,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments. - * Space Complexity: O(1) - Constant space, as it only creates a new node. + * Time Complexity: O(1) + * Space Complexity: O(1) */ /** - * Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments. - * Space Complexity: O(1) - Constant space, as it only creates a new node. + * Time Complexity: O(1) + * Space Complexity: O(1) * * The `push` function adds a new node with the given value to the end of a singly linked list. * @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of @@ -118,13 +137,14 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) + * Linear time in the worst case, as it may need to traverse the list to find the last element. */ /** - * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail * pointers accordingly. @@ -153,13 +173,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail * pointers accordingly. @@ -171,13 +191,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(1) + * Space Complexity: O(1) */ /** - * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(1) + * Space Complexity: O(1) * * The `shift()` function removes and returns the value of the first node in a linked list. * @returns The value of the node that is being removed from the beginning of the linked list. @@ -191,13 +211,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(1) + * Space Complexity: O(1) */ /** - * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(1) + * Space Complexity: O(1) * * The `pollFirst()` function removes and returns the value of the first node in a linked list. * @returns The value of the node that is being removed from the beginning of the linked list. @@ -207,13 +227,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(1) + * Space Complexity: O(1) */ /** - * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(1) + * Space Complexity: O(1) * * The unshift function adds a new node with the given value to the beginning of a singly linked list. * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the @@ -233,13 +253,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(1) + * Space Complexity: O(1) */ /** - * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(1) + * Space Complexity: O(1) * * The addFirst function adds a new node with the given value to the beginning of a singly linked list. * @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the @@ -250,21 +270,22 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) + * Linear time, where n is the index, as it may need to traverse the list to find the desired node. */ /** - * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * - * The function `getAt` returns the value at a specified index in a linked list, or undefined if the index is out of range. + * The function `at` returns the value at a specified index in a linked list, or undefined if the index is out of range. * @param {number} index - The index parameter is a number that represents the position of the element we want to * retrieve from the list. - * @returns The method `getAt(index: number): E | undefined` returns the value at the specified index in the linked list, or + * @returns The method `at(index: number): E | undefined` returns the value at the specified index in the linked list, or * `undefined` if the index is out of bounds. */ - getAt(index: number): E | undefined { + at(index: number): E | undefined { if (index < 0 || index >= this.size) return undefined; let current = this.head; for (let i = 0; i < index; i++) { @@ -274,13 +295,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The function `getNodeAt` returns the node at a given index in a singly linked list. * @param {number} index - The `index` parameter is a number that represents the position of the node we want to @@ -297,13 +318,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element. * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the @@ -330,13 +351,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The delete function removes a node with a specific value from a singly linked list. * @param {E | SinglyLinkedListNode} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E` @@ -379,13 +400,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The `addAt` function inserts a value at a specified index in a singly linked list. * @param {number} index - The index parameter represents the position at which the new value should be inserted in the @@ -433,13 +454,15 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array. - * Space Complexity: O(n) - Linear space, as it creates an array with the same length as the list. + * Time Complexity: O(n) + * Space Complexity: O(n) + * Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array. + * Linear space, as it creates an array with the same length as the list. */ /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array. - * Space Complexity: O(n) - Linear space, as it creates an array with the same length as the list. + * Time Complexity: O(n) + * Space Complexity: O(n) * * The `toArray` function converts a linked list into an array. * @returns The `toArray()` method is returning an array of type `E[]`. @@ -455,13 +478,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The `reverse` function reverses the order of the nodes in a singly linked list. * @returns The reverse() method does not return anything. It has a return type of void. @@ -485,39 +508,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. - * - * The `find` function iterates through a linked list and returns the first element that satisfies a given condition. - * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This - * function is used to determine whether a particular value in the linked list satisfies a certain condition. - * @returns The method `find` returns the first element in the linked list that satisfies the condition specified by - * the callback function. If no element satisfies the condition, it returns `undefined`. - */ - find(callback: (value: E) => boolean): E | undefined { - let current = this.head; - while (current) { - if (callback(current.value)) { - return current.value; - } - current = current.next; - } - return undefined; - } - - /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. - */ - - /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The `indexOf` function returns the index of the first occurrence of a given value in a linked list. * @param {E} value - The value parameter is the value that you want to find the index of in the linked list. @@ -540,13 +537,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns * undefined. @@ -568,13 +565,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The `addBefore` function inserts a new value before an existing value in a singly linked list. * @param {E | SinglyLinkedListNode} existingValueOrNode - The existing value or node that you want to insert the @@ -613,13 +610,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list. * @param {E | SinglyLinkedListNode} existingValueOrNode - The existing value or node in the linked list after which @@ -652,13 +649,13 @@ export class SinglyLinkedList extends IterableElementBase { } /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node. - * Space Complexity: O(1) - Constant space. + * Time Complexity: O(n) + * Space Complexity: O(1) * * The function counts the number of occurrences of a given value in a linked list. * @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list. @@ -678,6 +675,24 @@ export class SinglyLinkedList extends IterableElementBase { return count; } + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + * + * The `clone` function returns a new instance of the `SinglyLinkedList` class with the same values + * as the original list. + * @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which + * is a clone of the original list. + */ + clone(): SinglyLinkedList { + return new SinglyLinkedList(this.values()); + } + /** * Time Complexity: O(n) * Space Complexity: O(n) @@ -741,6 +756,9 @@ export class SinglyLinkedList extends IterableElementBase { return mappedList; } + /** + * The function `_getIterator` returns an iterable iterator that yields the values of a linked list. + */ protected* _getIterator(): IterableIterator { let current = this.head; diff --git a/src/data-structures/linked-list/skip-linked-list.ts b/src/data-structures/linked-list/skip-linked-list.ts index da03ad4..01e58ae 100644 --- a/src/data-structures/linked-list/skip-linked-list.ts +++ b/src/data-structures/linked-list/skip-linked-list.ts @@ -20,6 +20,14 @@ export class SkipListNode { } export class SkipList { + /** + * The constructor function initializes a SkipLinkedList object with optional options and elements. + * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It + * is used to initialize the SkipLinkedList with the given key-value pairs. If no elements are + * provided, the SkipLinkedList will be empty. + * @param {SkipLinkedListOptions} [options] - The `options` parameter is an optional object that can + * contain two properties: + */ constructor(elements: Iterable<[K, V]> = [], options?: SkipLinkedListOptions) { if (options) { const { maxLevel, probability } = options; @@ -34,36 +42,52 @@ export class SkipList { protected _head: SkipListNode = new SkipListNode(undefined as any, undefined as any, this.maxLevel); + /** + * The function returns the head node of a SkipList. + * @returns The method is returning a SkipListNode object with generic key type K and value type V. + */ get head(): SkipListNode { return this._head; } protected _level: number = 0; + /** + * The function returns the value of the private variable _level. + * @returns The level property of the object. + */ get level(): number { return this._level; } protected _maxLevel: number = 16; + /** + * The function returns the maximum level. + * @returns The value of the variable `_maxLevel` is being returned. + */ get maxLevel(): number { return this._maxLevel; } protected _probability: number = 0.5; + /** + * The function returns the probability value. + * @returns The probability value stored in the private variable `_probability` is being returned. + */ get probability(): number { return this._probability; } /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(1) + * Space Complexity: O(1) * * Get the value of the first element (the smallest element) in the Skip List. * @returns The value of the first element, or undefined if the Skip List is empty. @@ -74,13 +98,13 @@ export class SkipList { } /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) * * Get the value of the last element (the largest element) in the Skip List. * @returns The value of the last element, or undefined if the Skip List is empty. @@ -96,13 +120,13 @@ export class SkipList { } /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) * * The add function adds a new node with a given key and value to a Skip List data structure. * @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list. @@ -132,13 +156,13 @@ export class SkipList { } /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) * * The function `get` retrieves the value associated with a given key from a skip list data structure. * @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure. @@ -163,27 +187,28 @@ export class SkipList { } /** - * Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * The function checks if a key exists in a data structure. + * @param {K} key - The parameter "key" is of type K, which represents the type of the key being + * checked. + * @returns a boolean value. */ - has(key: K): boolean { return this.get(key) !== undefined; } /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) * * The `delete` function removes a node with a specific key from a Skip List data structure. * @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list. @@ -220,13 +245,13 @@ export class SkipList { } /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) * * Get the value of the first element in the Skip List that is greater than the given key. * @param key - the given key. @@ -244,13 +269,13 @@ export class SkipList { } /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) */ /** - * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList. - * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList. + * Time Complexity: O(log n) + * Space Complexity: O(1) * * Get the value of the last element in the Skip List that is less than the given key. * @param key - the given key. @@ -273,13 +298,14 @@ export class SkipList { } /** - * Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case. - * Space Complexity: O(1) - constant space. + * Time Complexity: O(maxLevel) + * Space Complexity: O(1) + * where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case. */ /** - * Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case. - * Space Complexity: O(1) - constant space. + * Time Complexity: O(maxLevel) + * Space Complexity: O(1) * * The function "_randomLevel" generates a random level based on a given probability and maximum level. * @returns the level, which is a number. diff --git a/src/data-structures/matrix/matrix.ts b/src/data-structures/matrix/matrix.ts index 21a988d..f381125 100644 --- a/src/data-structures/matrix/matrix.ts +++ b/src/data-structures/matrix/matrix.ts @@ -42,30 +42,54 @@ export class Matrix { protected _rows: number = 0; + /** + * The function returns the number of rows. + * @returns The number of rows. + */ get rows(): number { return this._rows; } protected _cols: number = 0; + /** + * The function returns the value of the private variable _cols. + * @returns The number of columns. + */ get cols(): number { return this._cols; } protected _data: number[][]; + /** + * The function returns a two-dimensional array of numbers. + * @returns The data property, which is a two-dimensional array of numbers. + */ get data(): number[][] { return this._data; } + /** + * The above function returns the value of the _addFn property. + * @returns The value of the property `_addFn` is being returned. + */ get addFn() { return this._addFn; } + /** + * The function returns the value of the _subtractFn property. + * @returns The `_subtractFn` property is being returned. + */ get subtractFn() { return this._subtractFn; } + /** + * The function returns the value of the _multiplyFn property. + * @returns The `_multiplyFn` property is being returned. + */ get multiplyFn() { return this._multiplyFn; } @@ -373,6 +397,34 @@ export class Matrix { }); } + /** + * The function checks if a given row and column index is valid within a specified range. + * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or + * matrix. It is a number that indicates the specific row in the matrix. + * @param {number} col - The "col" parameter represents the column index in a two-dimensional array + * or grid. It is used to check if the given column index is valid within the bounds of the grid. + * @returns A boolean value is being returned. + */ + isValidIndex(row: number, col: number): boolean { + return row >= 0 && row < this.rows && col >= 0 && col < this.cols; + } + + /** + * The `clone` function returns a new instance of the Matrix class with the same data and properties + * as the original instance. + * @returns The `clone()` method is returning a new instance of the `Matrix` class with the same data + * and properties as the current instance. + */ + clone(): Matrix { + return new Matrix(this.data, { + rows: this.rows, + cols: this.cols, + addFn: this.addFn, + subtractFn: this.subtractFn, + multiplyFn: this.multiplyFn + }); + } + protected _addFn(a: number | undefined, b: number): number | undefined { if (a === undefined) return b; return a + b; @@ -386,18 +438,6 @@ export class Matrix { return a * b; } - /** - * The function checks if a given row and column index is valid within a specified range. - * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or - * matrix. It is a number that indicates the specific row in the matrix. - * @param {number} col - The "col" parameter represents the column index in a two-dimensional array - * or grid. It is used to check if the given column index is valid within the bounds of the grid. - * @returns A boolean value is being returned. - */ - protected isValidIndex(row: number, col: number): boolean { - return row >= 0 && row < this.rows && col >= 0 && col < this.cols; - } - /** * The function `_swapRows` swaps the positions of two rows in an array. * @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap. diff --git a/src/data-structures/queue/deque.ts b/src/data-structures/queue/deque.ts index 02aa162..28ab0e5 100644 --- a/src/data-structures/queue/deque.ts +++ b/src/data-structures/queue/deque.ts @@ -24,6 +24,17 @@ export class Deque extends IterableElementBase { protected _bucketCount = 0; protected readonly _bucketSize: number = 1 << 12; + /** + * The constructor initializes a Deque object with an optional iterable of elements and options. + * @param elements - An iterable object (such as an array or a Set) that contains the initial + * elements to be added to the deque. It can also be an object with a `length` or `size` property + * that represents the number of elements in the iterable object. If no elements are provided, an + * empty deque + * @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain + * configuration options for the deque. In this code, it is used to set the `bucketSize` option, + * which determines the size of each bucket in the deque. If the `bucketSize` option is not provided + * or is not a number + */ constructor(elements: IterableWithSizeOrLength = [], options?: DequeOptions) { super(); @@ -54,14 +65,32 @@ export class Deque extends IterableElementBase { } } + /** + * The bucketSize function returns the size of the bucket. + * + * @return The size of the bucket + */ + get bucketSize() { + return this._bucketSize; + } + protected _buckets: E[][] = []; + /** + * The buckets function returns the buckets property of the object. + * + * @return The buckets property + */ get buckets() { return this._buckets; } protected _size = 0; + /** + * The size function returns the number of items in the stack. + * @return The number of values in the set + */ get size() { return this._size; } @@ -76,6 +105,10 @@ export class Deque extends IterableElementBase { return this._buckets[this._bucketFirst][this._firstInBucket]; } + /** + * The last function returns the last element in the queue. + * @return The last element in the array + */ get last(): E | undefined { if (this.size === 0) return; return this._buckets[this._bucketLast][this._lastInBucket]; @@ -235,7 +268,7 @@ export class Deque extends IterableElementBase { * begin(): Generator { let index = 0; while (index < this.size) { - yield this.getAt(index); + yield this.at(index); index++; } } @@ -247,7 +280,7 @@ export class Deque extends IterableElementBase { * reverseBegin(): Generator { let index = this.size - 1; while (index >= 0) { - yield this.getAt(index); + yield this.at(index); index--; } } @@ -261,13 +294,13 @@ export class Deque extends IterableElementBase { * Time Complexity: O(1) * Space Complexity: O(1) * - * The `getAt` function retrieves an element at a specified position in an array-like data structure. + * The `at` function retrieves an element at a specified position in an array-like data structure. * @param {number} pos - The `pos` parameter represents the position of the element that you want to * retrieve from the data structure. It is of type `number` and should be a valid index within the * range of the data structure. * @returns The element at the specified position in the data structure is being returned. */ - getAt(pos: number): E { + at(pos: number): E { rangeCheck(pos, 0, this.size - 1); const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos); return this._buckets[bucketIndex][indexInBucket]!; @@ -325,7 +358,7 @@ export class Deque extends IterableElementBase { } else { const arr: E[] = []; for (let i = pos; i < this.size; ++i) { - arr.push(this.getAt(i)); + arr.push(this.at(i)); } this.cut(pos - 1); for (let i = 0; i < num; ++i) this.push(element); @@ -416,7 +449,7 @@ export class Deque extends IterableElementBase { let i = 0; let index = 0; while (i < size) { - const oldElement = this.getAt(i); + const oldElement = this.at(i); if (oldElement !== element) { this.setAt(index, oldElement!); index += 1; @@ -471,9 +504,9 @@ export class Deque extends IterableElementBase { return this; } let index = 1; - let prev = this.getAt(0); + let prev = this.at(0); for (let i = 1; i < this.size; ++i) { - const cur = this.getAt(i); + const cur = this.at(i); if (cur !== prev) { prev = cur; this.setAt(index++, cur); @@ -501,7 +534,7 @@ export class Deque extends IterableElementBase { sort(comparator?: (x: E, y: E) => number): this { const arr: E[] = []; for (let i = 0; i < this.size; ++i) { - arr.push(this.getAt(i)); + arr.push(this.at(i)); } arr.sort(comparator); for (let i = 0; i < this.size; ++i) { @@ -545,32 +578,6 @@ export class Deque extends IterableElementBase { this._buckets = newBuckets; } - /** - * Time Complexity: O(n) - * Space Complexity: O(1) - */ - - /** - * Time Complexity: O(n) - * Space Complexity: O(1) - * - * The `find` function iterates over the elements in a deque and returns the first element for which - * the callback function returns true, or undefined if no such element is found. - * @param callback - A function that takes three parameters: element, index, and deque. It should - * return a boolean value indicating whether the element satisfies a certain condition. - * @returns The method `find` returns the first element in the deque that satisfies the condition - * specified by the callback function. If no element satisfies the condition, it returns `undefined`. - */ - find(callback: (element: E, index: number, deque: Deque) => boolean): E | undefined { - for (let i = 0; i < this.size; ++i) { - const element = this.getAt(i); - if (callback(element, i, this)) { - return element; - } - } - return; - } - /** * Time Complexity: O(n) * Space Complexity: O(1) @@ -589,7 +596,7 @@ export class Deque extends IterableElementBase { */ indexOf(element: E): number { for (let i = 0; i < this.size; ++i) { - if (this.getAt(i) === element) { + if (this.at(i) === element) { return i; } } @@ -616,6 +623,25 @@ export class Deque extends IterableElementBase { * Time Complexity: O(n) * Space Complexity: O(n) */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + * + * The `clone()` function returns a new instance of the `Deque` class with the same elements and + * bucket size as the original instance. + * @returns The `clone()` method is returning a new instance of the `Deque` class with the same + * elements as the original deque (`this`) and the same bucket size. + */ + clone(): Deque { + return new Deque([...this], { bucketSize: this.bucketSize }); + } + + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + */ + /** * Time Complexity: O(n) * Space Complexity: O(n) @@ -737,7 +763,7 @@ export class Deque extends IterableElementBase { */ protected* _getIterator(): IterableIterator { for (let i = 0; i < this.size; ++i) { - yield this.getAt(i); + yield this.at(i); } } diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index 1c2d4e7..8e7d125 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -32,12 +32,20 @@ export class Queue extends IterableElementBase { protected _elements: E[] = []; + /** + * The elements function returns the elements of this set. + * @return An array of the elements in the stack + */ get elements(): E[] { return this._elements; } protected _offset: number = 0; + /** + * The offset function returns the offset of the current page. + * @return The value of the private variable _offset + */ get offset(): number { return this._offset; } @@ -219,7 +227,7 @@ export class Queue extends IterableElementBase { * * @param index */ - getAt(index: number): E | undefined { + at(index: number): E | undefined { return this.elements[index]; } @@ -264,13 +272,14 @@ export class Queue extends IterableElementBase { } /** - * Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array. - * Space Complexity: O(n) - the space required is proportional to the number of elements in the queue. + * Time Complexity: O(n) + * Space Complexity: O(n) + * where n is the number of elements in the queue. It creates a shallow copy of the internal array. the space required is proportional to the number of elements in the queue. */ /** - * Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array. - * Space Complexity: O(n) - the space required is proportional to the number of elements in the queue. + * Time Complexity: O(n) + * Space Complexity: O(n) * * The `clone()` function returns a new Queue object with the same elements as the original Queue. * @returns The `clone()` method is returning a new instance of the `Queue` class. @@ -390,4 +399,21 @@ export class LinkedListQueue extends SinglyLinkedList { peek(): E | undefined { return this.first; } + + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + * The `clone` function returns a new instance of the `LinkedListQueue` class with the same values as + * the current instance. + * @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same + * values as the original `LinkedListQueue`. + */ + clone(): LinkedListQueue { + return new LinkedListQueue(this.values()); + } } diff --git a/src/data-structures/stack/stack.ts b/src/data-structures/stack/stack.ts index 63cabfa..ee11149 100644 --- a/src/data-structures/stack/stack.ts +++ b/src/data-structures/stack/stack.ts @@ -32,6 +32,10 @@ export class Stack extends IterableElementBase { protected _elements: E[] = []; + /** + * The elements function returns the elements of this set. + * @return An array of elements + */ get elements(): E[] { return this._elements; } diff --git a/src/data-structures/trie/trie.ts b/src/data-structures/trie/trie.ts index d4a1eb9..6894e90 100644 --- a/src/data-structures/trie/trie.ts +++ b/src/data-structures/trie/trie.ts @@ -38,6 +38,12 @@ export class TrieNode { * 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data." */ export class Trie extends IterableElementBase { + /** + * The constructor function for the Trie class. + * @param words: Iterable string Initialize the trie with a set of words + * @param options?: TrieOptions Allow the user to pass in options for the trie + * @return This + */ constructor(words: Iterable = [], options?: TrieOptions) { super(); if (options) { @@ -51,18 +57,31 @@ export class Trie extends IterableElementBase { protected _size: number = 0; + /** + * The size function returns the size of the stack. + * @return The number of elements in the list + */ get size(): number { return this._size; } protected _caseSensitive: boolean = true; + /** + * The caseSensitive function is a getter that returns the value of the private _caseSensitive property. + * + * @return The value of the _casesensitive private variable + */ get caseSensitive(): boolean { return this._caseSensitive; } protected _root: TrieNode = new TrieNode(''); + /** + * The root function returns the root node of the tree. + * @return The root node + */ get root() { return this._root; } @@ -351,6 +370,23 @@ export class Trie extends IterableElementBase { return words; } + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + */ + + /** + * Time Complexity: O(n) + * Space Complexity: O(n) + * + * The `clone` function returns a new instance of the Trie class with the same values and case + * sensitivity as the original Trie. + * @returns A new instance of the Trie class is being returned. + */ + clone(): Trie { + return new Trie(this.values(), { caseSensitive: this.caseSensitive }); + } + /** * Time Complexity: O(n) * Space Complexity: O(n) diff --git a/src/types/data-structures/hash/hash-map.ts b/src/types/data-structures/hash/hash-map.ts index c930899..d886e56 100644 --- a/src/types/data-structures/hash/hash-map.ts +++ b/src/types/data-structures/hash/hash-map.ts @@ -5,14 +5,15 @@ export type HashMapLinkedNode = { prev: HashMapLinkedNode; }; -export type LinkedHashMapOptions = { +export type LinkedHashMapOptions = { hashFn?: (key: K) => string; objHashFn?: (key: K) => object; + toEntryFn?: (rawElement: R) => [K, V]; }; -export type HashMapOptions = { +export type HashMapOptions = { hashFn?: (key: K) => string; - toEntryFn?: (rawElement: T) => [K, V]; + toEntryFn?: (rawElement: R) => [K, V]; }; export type HashMapStoreItem = { key: K; value: V }; diff --git a/test/unit/data-structures/hash/hash-map.test.ts b/test/unit/data-structures/hash/hash-map.test.ts index fe23133..47c262e 100644 --- a/test/unit/data-structures/hash/hash-map.test.ts +++ b/test/unit/data-structures/hash/hash-map.test.ts @@ -385,7 +385,7 @@ describe('LinkedHashMap', () => { expect(hashMap.last).toEqual([key, value]); expect(hashMap.reverseBegin().next().value).toEqual([key, value]); } else if (index <= 1000) { - expect(hashMap.getAt(index)).toBe(value); + expect(hashMap.at(index)).toBe(value); } expect(hashMap.get(key)).toEqual(value); index++; @@ -434,7 +434,7 @@ describe('LinkedHashMap', () => { test('should get element at specific index', () => { hashMap.set('key1', 'value1'); hashMap.set('key2', 'value2'); - expect(hashMap.getAt(1)).toBe('value2'); + expect(hashMap.at(1)).toBe('value2'); }); describe('LinkedHashMap basic', () => { diff --git a/test/unit/data-structures/linked-list/doubly-linked-list.test.ts b/test/unit/data-structures/linked-list/doubly-linked-list.test.ts index f5036e4..bc6cd6f 100644 --- a/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +++ b/test/unit/data-structures/linked-list/doubly-linked-list.test.ts @@ -39,6 +39,8 @@ describe('DoublyLinkedList Operation Test', () => { it('should find undefined', () => { expect(list.find(value => value === 6)).toBe(undefined); + expect(list.find(value => value === 4)).toBe(4); + expect(list.find(value => value === 3)).toBe(3); }); it('should indexOf -1', () => { @@ -99,19 +101,19 @@ describe('DoublyLinkedList Operation Test', () => { // Inserting at the beginning list.addAt(0, 0); expect(list.size).toBe(4); - expect(list.getAt(0)).toBe(0); - expect(list.getAt(1)).toBe(1); + expect(list.at(0)).toBe(0); + expect(list.at(1)).toBe(1); // Inserting in the middle list.addAt(2, 1.5); expect(list.size).toBe(5); - expect(list.getAt(2)).toBe(1.5); - expect(list.getAt(3)).toBe(2); + expect(list.at(2)).toBe(1.5); + expect(list.at(3)).toBe(2); // Inserting at the end list.addAt(5, 4); expect(list.size).toBe(6); - expect(list.getAt(5)).toBe(4); + expect(list.at(5)).toBe(4); expect(list.tail!.value).toBe(4); }); diff --git a/test/unit/data-structures/linked-list/singly-linked-list.test.ts b/test/unit/data-structures/linked-list/singly-linked-list.test.ts index b9debbb..ac7e7aa 100644 --- a/test/unit/data-structures/linked-list/singly-linked-list.test.ts +++ b/test/unit/data-structures/linked-list/singly-linked-list.test.ts @@ -72,14 +72,14 @@ describe('SinglyLinkedList Operation Test', () => { list.push(1); list.push(2); list.push(3); - const element = list.getAt(1); + const element = list.at(1); expect(element).toBe(2); expect(list.getNodeAt(2)?.value).toBe(3); }); it('should return undefined for an out-of-bounds index', () => { list.push(1); - const element = list.getAt(1); + const element = list.at(1); expect(element).toBeUndefined(); }); }); @@ -327,6 +327,7 @@ describe('SinglyLinkedList Operation Test', () => { list.push(3); const result = list.find(data => data % 2 === 0); expect(result).toBe(2); + expect(list.find(value => value === 3)).toBe(3); }); it('should return undefined if element is not found', () => { diff --git a/test/unit/data-structures/queue/deque.test.ts b/test/unit/data-structures/queue/deque.test.ts index a12135b..2e87965 100644 --- a/test/unit/data-structures/queue/deque.test.ts +++ b/test/unit/data-structures/queue/deque.test.ts @@ -40,14 +40,14 @@ describe('Deque - Basic Operations', () => { expect(deque.isEmpty()).toBeTruthy(); }); - test('getAt should retrieve the correct element', () => { - expect(deque.getAt(0)).toBe(1); - expect(deque.getAt(1)).toBe(2); + test('at should retrieve the correct element', () => { + expect(deque.at(0)).toBe(1); + expect(deque.at(1)).toBe(2); }); test('setAt should set the correct element', () => { deque.setAt(0, 3); - expect(deque.getAt(0)).toBe(3); + expect(deque.at(0)).toBe(3); }); }); describe('Deque - Complex Operations', () => { diff --git a/test/unit/data-structures/queue/queue.test.ts b/test/unit/data-structures/queue/queue.test.ts index 5e945fb..5502dc6 100644 --- a/test/unit/data-structures/queue/queue.test.ts +++ b/test/unit/data-structures/queue/queue.test.ts @@ -156,17 +156,17 @@ describe('Queue - Additional Methods', () => { expect(queue.peekLast()).toBeUndefined(); }); - test('getAt should return the element at the specified index', () => { + test('at should return the element at the specified index', () => { queue.push(1); queue.push(2); queue.push(3); - expect(queue.getAt(1)).toBe(2); + expect(queue.at(1)).toBe(2); }); - test('getAt should return undefined for an invalid index', () => { + test('at should return undefined for an invalid index', () => { queue.push(1); - expect(queue.getAt(3)).toBeUndefined(); - expect(queue.getAt(-1)).toBeUndefined(); + expect(queue.at(3)).toBeUndefined(); + expect(queue.at(-1)).toBeUndefined(); }); test('print should not throw any errors', () => {