From c28deb8d81e1e20925a3b48140ca267e62df35e6 Mon Sep 17 00:00:00 2001 From: Revone Date: Sat, 30 Dec 2023 17:50:57 +0800 Subject: [PATCH] refactor: Rename exemplarToNode to keyValueOrEntryToNode. feat: Implement the clone method for all data structures. --- .../binary-tree/binary-tree.ts | 8 ++-- src/data-structures/binary-tree/bst.ts | 8 ++-- src/data-structures/binary-tree/rb-tree.ts | 8 ++-- .../binary-tree/tree-multimap.ts | 6 +-- .../linked-list/doubly-linked-list.ts | 24 ++++++++--- .../linked-list/singly-linked-list.ts | 18 +++++++++ src/data-structures/matrix/matrix.ts | 40 +++++++++++++------ src/data-structures/queue/deque.ts | 23 +++++++++++ src/data-structures/queue/queue.ts | 26 ++++++++++-- src/data-structures/trie/trie.ts | 17 ++++++++ 10 files changed, 142 insertions(+), 36 deletions(-) diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 88c3a89..32c309b 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 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/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index caaffc9..5413188 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -112,11 +112,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); } /** @@ -671,6 +667,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) diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index aa32809..f4773dc 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -678,6 +678,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) diff --git a/src/data-structures/matrix/matrix.ts b/src/data-structures/matrix/matrix.ts index 21a988d..abbe39c 100644 --- a/src/data-structures/matrix/matrix.ts +++ b/src/data-structures/matrix/matrix.ts @@ -373,6 +373,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 +414,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..db702e1 100644 --- a/src/data-structures/queue/deque.ts +++ b/src/data-structures/queue/deque.ts @@ -54,6 +54,10 @@ export class Deque extends IterableElementBase { } } + get bucketSize() { + return this._bucketSize; + } + protected _buckets: E[][] = []; get buckets() { @@ -616,6 +620,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) diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index 1c2d4e7..e953ec6 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -264,13 +264,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 +391,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/trie/trie.ts b/src/data-structures/trie/trie.ts index d4a1eb9..22cfaa4 100644 --- a/src/data-structures/trie/trie.ts +++ b/src/data-structures/trie/trie.ts @@ -351,6 +351,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)