mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
refactor: Rename exemplarToNode to keyValueOrEntryToNode. feat: Implement the clone method for all data structures.
This commit is contained in:
parent
ec649bd8bb
commit
c28deb8d81
|
@ -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<K, V, N>`.
|
||||
* @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<K, V, N>, value?: V): N | null | undefined {
|
||||
keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, 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<K, V, N>, 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
|
||||
|
|
|
@ -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<K, V, N>`, 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<K, V, N>, value?: V): N | undefined {
|
||||
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, 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<K, V, N>, value?: V): boolean {
|
||||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
||||
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
||||
if (newNode === undefined) return false;
|
||||
|
||||
if (this.root === undefined) {
|
||||
|
|
|
@ -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<K, V, N>`, 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<K, V, N>, value?: V): N | undefined {
|
||||
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, 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<K, V, N>, value?: V): boolean {
|
||||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
||||
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
||||
if (newNode === undefined) return false;
|
||||
|
||||
newNode.left = this.Sentinel;
|
||||
|
|
|
@ -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<K, V, N>`, 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<K, V, N>, value?: V, count = 1): N | undefined {
|
||||
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, 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<K, V, N>, 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;
|
||||
|
|
|
@ -112,11 +112,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
||||
*/
|
||||
static fromArray<E>(data: E[]) {
|
||||
const doublyLinkedList = new DoublyLinkedList<E>();
|
||||
for (const item of data) {
|
||||
doublyLinkedList.push(item);
|
||||
}
|
||||
return doublyLinkedList;
|
||||
return new DoublyLinkedList<E>(data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -671,6 +667,24 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
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<E> {
|
||||
return new DoublyLinkedList(this.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(1)
|
||||
* Space Complexity: O(1)
|
||||
|
|
|
@ -678,6 +678,24 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
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<E> {
|
||||
return new SinglyLinkedList<E>(this.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -54,6 +54,10 @@ export class Deque<E> extends IterableElementBase<E> {
|
|||
}
|
||||
}
|
||||
|
||||
get bucketSize() {
|
||||
return this._bucketSize;
|
||||
}
|
||||
|
||||
protected _buckets: E[][] = [];
|
||||
|
||||
get buckets() {
|
||||
|
@ -616,6 +620,25 @@ export class Deque<E> extends IterableElementBase<E> {
|
|||
* 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<E> {
|
||||
return new Deque<E>([...this], { bucketSize: this.bucketSize });
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(n)
|
||||
|
|
|
@ -264,13 +264,14 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends SinglyLinkedList<E> {
|
|||
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<E> {
|
||||
return new LinkedListQueue<E>(this.values());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -351,6 +351,23 @@ export class Trie extends IterableElementBase<string> {
|
|||
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)
|
||||
|
|
Loading…
Reference in a new issue