mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
docs: Complete all code comments.
This commit is contained in:
parent
831ad3bd98
commit
58ea2cb3c3
|
@ -59,24 +59,46 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 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, which is a number.
|
||||
*/
|
||||
get size(): number {
|
||||
return this._size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
||||
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
||||
*/
|
||||
isEmpty(): boolean {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
||||
* size.
|
||||
*/
|
||||
clear() {
|
||||
this._store = {};
|
||||
this._objMap.clear();
|
||||
|
@ -241,6 +263,14 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|||
return filteredMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
@ -288,33 +318,70 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|||
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list.
|
||||
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
||||
*/
|
||||
export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
||||
export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
||||
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
|
||||
protected _objMap = new WeakMap<object, HashMapLinkedNode<K, V | undefined>>();
|
||||
protected _head: HashMapLinkedNode<K, V | undefined>;
|
||||
protected _tail: HashMapLinkedNode<K, V | undefined>;
|
||||
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
||||
|
||||
constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions<K>) {
|
||||
/**
|
||||
* 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<R> = [], options?: LinkedHashMapOptions<K, V, R>) {
|
||||
super();
|
||||
this._sentinel = <HashMapLinkedNode<K, V>>{};
|
||||
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,15 +492,29 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|||
return true;
|
||||
}
|
||||
|
||||
setMany(entries: Iterable<[K, V]>): 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<R>): boolean[] {
|
||||
const results: boolean[] = [];
|
||||
for (const entry of entries) {
|
||||
const [key, value] = entry;
|
||||
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);
|
||||
|
@ -532,7 +613,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<K = any, V = any> extends IterableEntryBase<K, V> {
|
|||
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<K = any, V = any> extends IterableEntryBase<K, V> {
|
|||
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<K, V>` that is a clone
|
||||
* of the original `LinkedHashMap` object.
|
||||
*/
|
||||
clone(): LinkedHashMap<K, V> {
|
||||
const cloned = new LinkedHashMap<K, V>([], { hashFn: this._hashFn, objHashFn: this._objHashFn });
|
||||
for (const entry of this) {
|
||||
|
@ -638,26 +743,33 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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) => <object>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.
|
||||
*/
|
||||
|
|
|
@ -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<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
* 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<E> = [], options?: HeapOptions<E>) {
|
||||
super();
|
||||
|
||||
|
@ -45,12 +55,20 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
return mappedHeap;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
||||
*/
|
||||
protected* _getIterator(): IterableIterator<E> {
|
||||
for (const element of this.elements) {
|
||||
yield element;
|
||||
|
@ -458,6 +484,16 @@ export class FibonacciHeapNode<E> {
|
|||
parent?: FibonacciHeapNode<E>;
|
||||
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<E> {
|
|||
}
|
||||
|
||||
export class FibonacciHeap<E> {
|
||||
/**
|
||||
* 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<E>) {
|
||||
this.clear();
|
||||
this._comparator = comparator || this._defaultComparator;
|
||||
|
@ -477,24 +520,41 @@ export class FibonacciHeap<E> {
|
|||
|
||||
protected _root?: FibonacciHeapNode<E>;
|
||||
|
||||
/**
|
||||
* The function returns the root node of a Fibonacci heap.
|
||||
* @returns The method is returning either a FibonacciHeapNode object or undefined.
|
||||
*/
|
||||
get root(): FibonacciHeapNode<E> | 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<E>;
|
||||
|
||||
/**
|
||||
* 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<E>`. If there is no minimum node, it will return `undefined`.
|
||||
*/
|
||||
get min(): FibonacciHeapNode<E> | undefined {
|
||||
return this._min;
|
||||
}
|
||||
|
||||
protected _comparator: Comparator<E>;
|
||||
|
||||
/**
|
||||
* The function returns the comparator used for comparing elements.
|
||||
* @returns The `_comparator` property of the object.
|
||||
*/
|
||||
get comparator(): Comparator<E> {
|
||||
return this._comparator;
|
||||
}
|
||||
|
@ -808,12 +868,12 @@ export class FibonacciHeap<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
|
|
@ -33,7 +33,10 @@ export class DoublyLinkedListNode<E = any> {
|
|||
*/
|
||||
export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
* 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<E> = []) {
|
||||
super();
|
||||
|
@ -49,29 +52,43 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
|
||||
protected _head: DoublyLinkedListNode<E> | undefined;
|
||||
|
||||
/**
|
||||
* The `head` function returns the first node of a doubly linked list.
|
||||
* @returns The method `getHead()` returns either a `DoublyLinkedListNode<E>` object or `undefined`.
|
||||
*/
|
||||
get head(): DoublyLinkedListNode<E> | undefined {
|
||||
return this._head;
|
||||
}
|
||||
|
||||
protected _tail: DoublyLinkedListNode<E> | undefined;
|
||||
|
||||
/**
|
||||
* The `tail` function returns the last node of a doubly linked list.
|
||||
* @returns The `get tail()` method is returning either a `DoublyLinkedListNode<E>` object or
|
||||
* `undefined`.
|
||||
*/
|
||||
get tail(): DoublyLinkedListNode<E> | 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<E = any> extends IterableElementBase<E> {
|
|||
*/
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -169,7 +186,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
|
@ -196,7 +213,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
|
@ -223,12 +240,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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 `at` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
|
||||
|
@ -247,12 +264,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -272,12 +289,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -300,12 +317,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -339,12 +356,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -384,12 +402,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -428,7 +446,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -458,7 +476,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -494,7 +512,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
||||
* Time Complexity: O(1)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
|
@ -507,7 +525,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
||||
* Time Complexity: O(1)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
|
@ -521,12 +539,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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 returns the index of the first occurrence of a given value in a linked list.
|
||||
|
@ -549,12 +567,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -576,12 +594,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -603,7 +621,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
*/
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -620,12 +638,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -760,7 +778,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
|
@ -777,7 +795,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
||||
* Time Complexity: O(n)
|
||||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
|
|
|
@ -25,7 +25,10 @@ export class SinglyLinkedListNode<E = any> {
|
|||
|
||||
export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
* 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<E> = []) {
|
||||
super();
|
||||
|
@ -36,30 +39,44 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
|
||||
protected _head: SinglyLinkedListNode<E> | 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<E> | undefined {
|
||||
return this._head;
|
||||
}
|
||||
|
||||
protected _tail: SinglyLinkedListNode<E> | 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<E> | 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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,13 +270,14 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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 `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
|
||||
|
@ -274,13 +295,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
|
||||
|
@ -379,13 +400,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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,13 +508,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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 `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.
|
||||
|
@ -514,13 +537,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -542,13 +565,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E>} existingValueOrNode - The existing value or node that you want to insert the
|
||||
|
@ -587,13 +610,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<E>} existingValueOrNode - The existing value or node in the linked list after which
|
||||
|
@ -626,13 +649,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -733,6 +756,9 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|||
return mappedList;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
|
||||
*/
|
||||
protected* _getIterator(): IterableIterator<E> {
|
||||
let current = this.head;
|
||||
|
||||
|
|
|
@ -20,6 +20,14 @@ export class SkipListNode<K, V> {
|
|||
}
|
||||
|
||||
export class SkipList<K, V> {
|
||||
/**
|
||||
* 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<K, V> {
|
|||
|
||||
protected _head: SkipListNode<K, V> = new SkipListNode<K, V>(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<K, V> {
|
||||
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<K, V> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<K, V> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<K, V> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<K, V> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<K, V> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<K, V> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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<K, V> {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,17 @@ export class Deque<E> extends IterableElementBase<E> {
|
|||
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<E> = [], options?: DequeOptions) {
|
||||
super();
|
||||
|
||||
|
@ -54,18 +65,32 @@ export class Deque<E> extends IterableElementBase<E> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
@ -80,6 +105,10 @@ export class Deque<E> extends IterableElementBase<E> {
|
|||
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];
|
||||
|
|
|
@ -32,12 +32,20 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -32,6 +32,10 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|||
|
||||
protected _elements: E[] = [];
|
||||
|
||||
/**
|
||||
* The elements function returns the elements of this set.
|
||||
* @return An array of elements
|
||||
*/
|
||||
get elements(): E[] {
|
||||
return this._elements;
|
||||
}
|
||||
|
|
|
@ -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<string> {
|
||||
/**
|
||||
* 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<string> = [], options?: TrieOptions) {
|
||||
super();
|
||||
if (options) {
|
||||
|
@ -51,18 +57,31 @@ export class Trie extends IterableElementBase<string> {
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -5,14 +5,15 @@ export type HashMapLinkedNode<K, V> = {
|
|||
prev: HashMapLinkedNode<K, V>;
|
||||
};
|
||||
|
||||
export type LinkedHashMapOptions<K> = {
|
||||
export type LinkedHashMapOptions<K, V, R> = {
|
||||
hashFn?: (key: K) => string;
|
||||
objHashFn?: (key: K) => object;
|
||||
toEntryFn?: (rawElement: R) => [K, V];
|
||||
};
|
||||
|
||||
export type HashMapOptions<K, V, T> = {
|
||||
export type HashMapOptions<K, V, R> = {
|
||||
hashFn?: (key: K) => string;
|
||||
toEntryFn?: (rawElement: T) => [K, V];
|
||||
toEntryFn?: (rawElement: R) => [K, V];
|
||||
};
|
||||
|
||||
export type HashMapStoreItem<K, V> = { key: K; value: V };
|
||||
|
|
Loading…
Reference in a new issue