directed-graph-typed
Advanced tools
Comparing version 1.39.1 to 1.39.2
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { OneParamCallback, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions } from '../../types'; | ||
import type { BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, OneParamCallback } from '../../types'; | ||
import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types'; | ||
@@ -389,2 +389,12 @@ import { IBinaryTree } from '../../interfaces'; | ||
/** | ||
* The above function is an iterator for a binary tree that can be used to traverse the tree in | ||
* either an iterative or recursive manner. | ||
* @param node - The `node` parameter represents the current node in the binary tree from which the | ||
* iteration starts. It is an optional parameter with a default value of `this.root`, which means | ||
* that if no node is provided, the iteration will start from the root of the binary tree. | ||
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the | ||
* binary tree nodes in a specific order. | ||
*/ | ||
[Symbol.iterator](node?: N | null): Generator<BinaryTreeNodeKey, void, undefined>; | ||
/** | ||
* Swap the data of two nodes in the binary tree. | ||
@@ -391,0 +401,0 @@ * @param {N} srcNode - The source node to swap. |
@@ -1032,2 +1032,40 @@ "use strict"; | ||
/** | ||
* The above function is an iterator for a binary tree that can be used to traverse the tree in | ||
* either an iterative or recursive manner. | ||
* @param node - The `node` parameter represents the current node in the binary tree from which the | ||
* iteration starts. It is an optional parameter with a default value of `this.root`, which means | ||
* that if no node is provided, the iteration will start from the root of the binary tree. | ||
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the | ||
* binary tree nodes in a specific order. | ||
*/ | ||
*[Symbol.iterator](node = this.root) { | ||
if (!node) { | ||
return; | ||
} | ||
if (this.iterationType === types_1.IterationType.ITERATIVE) { | ||
const stack = []; | ||
let current = node; | ||
while (current || stack.length > 0) { | ||
while (current) { | ||
stack.push(current); | ||
current = current.left; | ||
} | ||
current = stack.pop(); | ||
if (current) | ||
yield current.key; | ||
if (current) | ||
current = current.right; | ||
} | ||
} | ||
else { | ||
if (node.left) { | ||
yield* this[Symbol.iterator](node.left); | ||
} | ||
yield node.key; | ||
if (node.right) { | ||
yield* this[Symbol.iterator](node.right); | ||
} | ||
} | ||
} | ||
/** | ||
* Swap the data of two nodes in the binary tree. | ||
@@ -1034,0 +1072,0 @@ * @param {N} srcNode - The source node to swap. |
@@ -63,7 +63,7 @@ /** | ||
/** | ||
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list. | ||
* The `popLast()` function removes and returns the value of the last node in a doubly linked list. | ||
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the | ||
* list is empty, it returns null. | ||
*/ | ||
pollLast(): E | undefined; | ||
popLast(): E | undefined; | ||
/** | ||
@@ -76,7 +76,7 @@ * The `shift()` function removes and returns the value of the first node in a doubly linked list. | ||
/** | ||
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list. | ||
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list. | ||
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked | ||
* list. | ||
*/ | ||
pollFirst(): E | undefined; | ||
popFirst(): E | undefined; | ||
/** | ||
@@ -95,11 +95,11 @@ * The unshift function adds a new node with the given value to the beginning of a doubly linked list. | ||
/** | ||
* The `peekFirst` function returns the first node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `peekFirst()` returns the first node of the doubly linked list, or `null` if the list is empty. | ||
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty. | ||
*/ | ||
peekFirst(): E | undefined; | ||
getFirst(): E | undefined; | ||
/** | ||
* The `peekLast` function returns the last node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty. | ||
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty. | ||
*/ | ||
peekLast(): E | undefined; | ||
getLast(): E | undefined; | ||
/** | ||
@@ -129,3 +129,3 @@ * The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds. | ||
*/ | ||
findNode(val: E | null): DoublyLinkedListNode<E> | null; | ||
getNode(val: E | null): DoublyLinkedListNode<E> | null; | ||
/** | ||
@@ -142,2 +142,13 @@ * The `insert` function inserts a value at a specified index in a doubly linked list. | ||
/** | ||
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* before which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked | ||
* list. | ||
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the | ||
* insertion fails. | ||
*/ | ||
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean; | ||
/** | ||
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element. | ||
@@ -150,5 +161,11 @@ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the | ||
deleteAt(index: number): E | undefined; | ||
delete(valOrNode: E): boolean; | ||
delete(valOrNode: DoublyLinkedListNode<E>): boolean; | ||
/** | ||
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value. | ||
* @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or | ||
* a `DoublyLinkedListNode<E>` object. | ||
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully | ||
* deleted from the doubly linked list, and `false` if the value or node was not found in the list. | ||
*/ | ||
delete(valOrNode: E | DoublyLinkedListNode<E> | null): boolean; | ||
/** | ||
* The `toArray` function converts a linked list into an array. | ||
@@ -184,15 +201,15 @@ * @returns The `toArray()` method is returning an array of type `E[]`. | ||
/** | ||
* The `findLast` function iterates through a linked list from the last node to the first node and returns the last | ||
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last | ||
* value that satisfies the given callback function, or null if no value satisfies the callback. | ||
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This | ||
* function is used to determine whether a given value satisfies a certain condition. | ||
* @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by | ||
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by | ||
* the callback function. If no value satisfies the condition, it returns `null`. | ||
*/ | ||
findLast(callback: (val: E) => boolean): E | null; | ||
findBackward(callback: (val: E) => boolean): E | null; | ||
/** | ||
* The `toArrayReverse` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toArrayReverse()` function returns an array of type `E[]`. | ||
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toArrayBackward()` function returns an array of type `E[]`. | ||
*/ | ||
toArrayReverse(): E[]; | ||
toArrayBackward(): E[]; | ||
/** | ||
@@ -237,15 +254,16 @@ * The `reverse` function reverses the order of the elements in a doubly linked list. | ||
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U; | ||
insertAfter(existingValueOrNode: E, newValue: E): boolean; | ||
insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean; | ||
/** | ||
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list. | ||
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* before which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* after which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked | ||
* list. | ||
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the | ||
* insertion fails. | ||
* @param {E} newValue - The value that you want to insert into the doubly linked list. | ||
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the | ||
* existing value or node is not found in the doubly linked list. | ||
*/ | ||
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean; | ||
insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean; | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
} |
@@ -127,7 +127,7 @@ "use strict"; | ||
/** | ||
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list. | ||
* The `popLast()` function removes and returns the value of the last node in a doubly linked list. | ||
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the | ||
* list is empty, it returns null. | ||
*/ | ||
pollLast() { | ||
popLast() { | ||
return this.pop(); | ||
@@ -156,7 +156,7 @@ } | ||
/** | ||
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list. | ||
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list. | ||
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked | ||
* list. | ||
*/ | ||
pollFirst() { | ||
popFirst() { | ||
return this.shift(); | ||
@@ -191,6 +191,6 @@ } | ||
/** | ||
* The `peekFirst` function returns the first node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `peekFirst()` returns the first node of the doubly linked list, or `null` if the list is empty. | ||
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty. | ||
*/ | ||
peekFirst() { | ||
getFirst() { | ||
var _a; | ||
@@ -200,6 +200,6 @@ return (_a = this.head) === null || _a === void 0 ? void 0 : _a.val; | ||
/** | ||
* The `peekLast` function returns the last node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty. | ||
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty. | ||
*/ | ||
peekLast() { | ||
getLast() { | ||
var _a; | ||
@@ -248,3 +248,3 @@ return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.val; | ||
*/ | ||
findNode(val) { | ||
getNode(val) { | ||
let current = this.head; | ||
@@ -290,2 +290,36 @@ while (current) { | ||
/** | ||
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* before which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked | ||
* list. | ||
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the | ||
* insertion fails. | ||
*/ | ||
insertBefore(existingValueOrNode, newValue) { | ||
let existingNode; | ||
if (existingValueOrNode instanceof DoublyLinkedListNode) { | ||
existingNode = existingValueOrNode; | ||
} | ||
else { | ||
existingNode = this.getNode(existingValueOrNode); | ||
} | ||
if (existingNode) { | ||
const newNode = new DoublyLinkedListNode(newValue); | ||
newNode.prev = existingNode.prev; | ||
if (existingNode.prev) { | ||
existingNode.prev.next = newNode; | ||
} | ||
newNode.next = existingNode; | ||
existingNode.prev = newNode; | ||
if (existingNode === this.head) { | ||
this.head = newNode; | ||
} | ||
this._length++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element. | ||
@@ -325,3 +359,3 @@ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the | ||
else { | ||
node = this.findNode(valOrNode); | ||
node = this.getNode(valOrNode); | ||
} | ||
@@ -411,10 +445,10 @@ if (node) { | ||
/** | ||
* The `findLast` function iterates through a linked list from the last node to the first node and returns the last | ||
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last | ||
* value that satisfies the given callback function, or null if no value satisfies the callback. | ||
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This | ||
* function is used to determine whether a given value satisfies a certain condition. | ||
* @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by | ||
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by | ||
* the callback function. If no value satisfies the condition, it returns `null`. | ||
*/ | ||
findLast(callback) { | ||
findBackward(callback) { | ||
let current = this.tail; | ||
@@ -430,6 +464,6 @@ while (current) { | ||
/** | ||
* The `toArrayReverse` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toArrayReverse()` function returns an array of type `E[]`. | ||
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toArrayBackward()` function returns an array of type `E[]`. | ||
*/ | ||
toArrayReverse() { | ||
toArrayBackward() { | ||
const array = []; | ||
@@ -539,3 +573,3 @@ let current = this.tail; | ||
else { | ||
existingNode = this.findNode(existingValueOrNode); | ||
existingNode = this.getNode(existingValueOrNode); | ||
} | ||
@@ -559,36 +593,12 @@ if (existingNode) { | ||
/** | ||
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* before which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked | ||
* list. | ||
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the | ||
* insertion fails. | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
insertBefore(existingValueOrNode, newValue) { | ||
let existingNode; | ||
if (existingValueOrNode instanceof DoublyLinkedListNode) { | ||
existingNode = existingValueOrNode; | ||
*[Symbol.iterator]() { | ||
let current = this.head; | ||
while (current) { | ||
yield current.val; | ||
current = current.next; | ||
} | ||
else { | ||
existingNode = this.findNode(existingValueOrNode); | ||
} | ||
if (existingNode) { | ||
const newNode = new DoublyLinkedListNode(newValue); | ||
newNode.prev = existingNode.prev; | ||
if (existingNode.prev) { | ||
existingNode.prev.next = newNode; | ||
} | ||
newNode.next = existingNode; | ||
existingNode.prev = newNode; | ||
if (existingNode === this.head) { | ||
this.head = newNode; | ||
} | ||
this._length++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
exports.DoublyLinkedList = DoublyLinkedList; |
@@ -42,10 +42,15 @@ /** | ||
static fromArray<E>(data: E[]): SinglyLinkedList<E>; | ||
getLength(): number; | ||
/** | ||
* The `push` function adds a new node with the given data to the end of a singly linked list. | ||
* @param {E} data - The "data" parameter represents the value that you want to add to the linked list. It can be of | ||
* The `push` function adds a new node with the given val to the end of a singly linked list. | ||
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of | ||
* any type (E) as specified in the generic type declaration of the class or function. | ||
*/ | ||
push(data: E): void; | ||
push(val: E): void; | ||
/** | ||
* The `push` function adds a new node with the given val to the end of a singly linked list. | ||
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of | ||
* any type (E) as specified in the generic type declaration of the class or function. | ||
*/ | ||
addLast(val: E): void; | ||
/** | ||
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail | ||
@@ -58,2 +63,9 @@ * pointers accordingly. | ||
/** | ||
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail | ||
* pointers accordingly. | ||
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If | ||
* the linked list is empty, it returns `null`. | ||
*/ | ||
popLast(): E | undefined; | ||
/** | ||
* The `shift()` function removes and returns the value of the first node in a linked list. | ||
@@ -64,2 +76,7 @@ * @returns The value of the node that is being removed from the beginning of the linked list. | ||
/** | ||
* The `popFirst()` 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. | ||
*/ | ||
popFirst(): E | undefined; | ||
/** | ||
* The unshift function adds a new node with the given value to the beginning of a singly linked list. | ||
@@ -71,2 +88,8 @@ * @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the | ||
/** | ||
* The addFirst function adds a new node with the given value to the beginning of a singly linked list. | ||
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the | ||
* linked list. | ||
*/ | ||
addFirst(val: E): void; | ||
/** | ||
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range. | ||
@@ -155,3 +178,3 @@ * @param {number} index - The index parameter is a number that represents the position of the element we want to | ||
*/ | ||
findNode(value: E): SinglyLinkedListNode<E> | null; | ||
getNode(value: E): SinglyLinkedListNode<E> | null; | ||
/** | ||
@@ -166,5 +189,12 @@ * The `insertBefore` function inserts a new value before an existing value in a singly linked list. | ||
insertBefore(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean; | ||
insertAfter(existingValueOrNode: E, newValue: E): boolean; | ||
insertAfter(existingValueOrNode: SinglyLinkedListNode<E>, newValue: E): boolean; | ||
/** | ||
* The `insertAfter` 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 | ||
* the new value will be inserted. It can be either the value of the existing node or the existing node itself. | ||
* @param {E} newValue - The value that you want to insert into the linked list after the existing value or node. | ||
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the | ||
* existing value or node, and false if the existing value or node was not found in the linked list. | ||
*/ | ||
insertAfter(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean; | ||
/** | ||
* The function counts the number of occurrences of a given value in a linked list. | ||
@@ -175,3 +205,41 @@ * @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list. | ||
countOccurrences(value: E): number; | ||
/** | ||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument | ||
* represents the value of the current node in the linked list, and the index argument represents the index of the | ||
* current node in the linked list. | ||
*/ | ||
forEach(callback: (val: E, index: number) => void): void; | ||
/** | ||
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new | ||
* SinglyLinkedList with the transformed values. | ||
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in | ||
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped | ||
* SinglyLinkedList). | ||
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values. | ||
*/ | ||
map<U>(callback: (val: E) => U): SinglyLinkedList<U>; | ||
/** | ||
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the | ||
* elements that satisfy the given callback function. | ||
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value. | ||
* It is used to determine whether a value should be included in the filtered list or not. | ||
* @returns The filtered list, which is an instance of the SinglyLinkedList class. | ||
*/ | ||
filter(callback: (val: E) => boolean): SinglyLinkedList<E>; | ||
/** | ||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a | ||
* single value. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
* elements in the linked list. | ||
*/ | ||
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U; | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
[Symbol.iterator](): Generator<E, void, unknown>; | ||
} |
@@ -72,12 +72,9 @@ "use strict"; | ||
} | ||
getLength() { | ||
return this._length; | ||
} | ||
/** | ||
* The `push` function adds a new node with the given data to the end of a singly linked list. | ||
* @param {E} data - The "data" parameter represents the value that you want to add to the linked list. It can be of | ||
* The `push` function adds a new node with the given val to the end of a singly linked list. | ||
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of | ||
* any type (E) as specified in the generic type declaration of the class or function. | ||
*/ | ||
push(data) { | ||
const newNode = new SinglyLinkedListNode(data); | ||
push(val) { | ||
const newNode = new SinglyLinkedListNode(val); | ||
if (!this.head) { | ||
@@ -94,2 +91,10 @@ this.head = newNode; | ||
/** | ||
* The `push` function adds a new node with the given val to the end of a singly linked list. | ||
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of | ||
* any type (E) as specified in the generic type declaration of the class or function. | ||
*/ | ||
addLast(val) { | ||
this.push(val); | ||
} | ||
/** | ||
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail | ||
@@ -121,2 +126,11 @@ * pointers accordingly. | ||
/** | ||
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail | ||
* pointers accordingly. | ||
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If | ||
* the linked list is empty, it returns `null`. | ||
*/ | ||
popLast() { | ||
return this.pop(); | ||
} | ||
/** | ||
* The `shift()` function removes and returns the value of the first node in a linked list. | ||
@@ -134,2 +148,9 @@ * @returns The value of the node that is being removed from the beginning of the linked list. | ||
/** | ||
* The `popFirst()` 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. | ||
*/ | ||
popFirst() { | ||
return this.shift(); | ||
} | ||
/** | ||
* The unshift function adds a new node with the given value to the beginning of a singly linked list. | ||
@@ -152,2 +173,10 @@ * @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the | ||
/** | ||
* The addFirst function adds a new node with the given value to the beginning of a singly linked list. | ||
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the | ||
* linked list. | ||
*/ | ||
addFirst(val) { | ||
this.unshift(val); | ||
} | ||
/** | ||
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range. | ||
@@ -358,3 +387,3 @@ * @param {number} index - The index parameter is a number that represents the position of the element we want to | ||
*/ | ||
findNode(value) { | ||
getNode(value) { | ||
let current = this.head; | ||
@@ -418,3 +447,3 @@ while (current) { | ||
else { | ||
existingNode = this.findNode(existingValueOrNode); | ||
existingNode = this.getNode(existingValueOrNode); | ||
} | ||
@@ -449,2 +478,74 @@ if (existingNode) { | ||
} | ||
/** | ||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument | ||
* represents the value of the current node in the linked list, and the index argument represents the index of the | ||
* current node in the linked list. | ||
*/ | ||
forEach(callback) { | ||
let current = this.head; | ||
let index = 0; | ||
while (current) { | ||
callback(current.val, index); | ||
current = current.next; | ||
index++; | ||
} | ||
} | ||
/** | ||
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new | ||
* SinglyLinkedList with the transformed values. | ||
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in | ||
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped | ||
* SinglyLinkedList). | ||
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values. | ||
*/ | ||
map(callback) { | ||
const mappedList = new SinglyLinkedList(); | ||
let current = this.head; | ||
while (current) { | ||
mappedList.push(callback(current.val)); | ||
current = current.next; | ||
} | ||
return mappedList; | ||
} | ||
/** | ||
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the | ||
* elements that satisfy the given callback function. | ||
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value. | ||
* It is used to determine whether a value should be included in the filtered list or not. | ||
* @returns The filtered list, which is an instance of the SinglyLinkedList class. | ||
*/ | ||
filter(callback) { | ||
const filteredList = new SinglyLinkedList(); | ||
let current = this.head; | ||
while (current) { | ||
if (callback(current.val)) { | ||
filteredList.push(current.val); | ||
} | ||
current = current.next; | ||
} | ||
return filteredList; | ||
} | ||
/** | ||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a | ||
* single value. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
* elements in the linked list. | ||
*/ | ||
reduce(callback, initialValue) { | ||
let accumulator = initialValue; | ||
let current = this.head; | ||
while (current) { | ||
accumulator = callback(accumulator, current.val); | ||
current = current.next; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
*[Symbol.iterator]() { | ||
@@ -451,0 +552,0 @@ let current = this.head; |
@@ -40,21 +40,21 @@ /** | ||
/** | ||
* The function `pollFirst()` removes and returns the first element in a data structure. | ||
* The function `popFirst()` removes and returns the first element in a data structure. | ||
* @returns The value of the first element in the data structure. | ||
*/ | ||
pollFirst(): E | undefined; | ||
popFirst(): E | undefined; | ||
/** | ||
* The `peekFirst` function returns the first element in an array-like data structure if it exists. | ||
* The `getFirst` function returns the first element in an array-like data structure if it exists. | ||
* @returns The element at the first position of the `_nodes` array. | ||
*/ | ||
peekFirst(): E | undefined; | ||
getFirst(): E | undefined; | ||
/** | ||
* The `pollLast()` function removes and returns the last element in a data structure. | ||
* The `popLast()` function removes and returns the last element in a data structure. | ||
* @returns The value that was removed from the data structure. | ||
*/ | ||
pollLast(): E | undefined; | ||
popLast(): E | undefined; | ||
/** | ||
* The `peekLast()` function returns the last element in an array-like data structure. | ||
* The `getLast()` function returns the last element in an array-like data structure. | ||
* @returns The last element in the array "_nodes" is being returned. | ||
*/ | ||
peekLast(): E | undefined; | ||
getLast(): E | undefined; | ||
/** | ||
@@ -91,12 +91,12 @@ * The get function returns the element at the specified index in an array-like data structure. | ||
/** | ||
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty. | ||
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty. | ||
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
*/ | ||
pollLast(): E | null; | ||
popLast(): E | null; | ||
/** | ||
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty. | ||
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is | ||
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty. | ||
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is | ||
* empty. | ||
*/ | ||
pollFirst(): E | null; | ||
popFirst(): E | null; | ||
/** | ||
@@ -113,12 +113,12 @@ * O(n) time complexity of adding at the beginning and the end | ||
/** | ||
* The `peekFirst` function returns the first element of an array or null if the array is empty. | ||
* @returns The function `peekFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is | ||
* The `getFirst` function returns the first element of an array or null if the array is empty. | ||
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is | ||
* empty, it will return `null`. | ||
*/ | ||
peekFirst(): E | null; | ||
getFirst(): E | null; | ||
/** | ||
* The `peekLast` function returns the last element of an array or null if the array is empty. | ||
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
* The `getLast` function returns the last element of an array or null if the array is empty. | ||
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
*/ | ||
peekLast(): E | null; | ||
getLast(): E | null; | ||
/** | ||
@@ -125,0 +125,0 @@ * O(1) time complexity of obtaining the value |
@@ -88,9 +88,9 @@ "use strict"; | ||
/** | ||
* The function `pollFirst()` removes and returns the first element in a data structure. | ||
* The function `popFirst()` removes and returns the first element in a data structure. | ||
* @returns The value of the first element in the data structure. | ||
*/ | ||
pollFirst() { | ||
popFirst() { | ||
if (!this._size) | ||
return; | ||
const value = this.peekFirst(); | ||
const value = this.getFirst(); | ||
delete this._nodes[this._first]; | ||
@@ -102,6 +102,6 @@ this._first++; | ||
/** | ||
* The `peekFirst` function returns the first element in an array-like data structure if it exists. | ||
* The `getFirst` function returns the first element in an array-like data structure if it exists. | ||
* @returns The element at the first position of the `_nodes` array. | ||
*/ | ||
peekFirst() { | ||
getFirst() { | ||
if (this._size) | ||
@@ -111,9 +111,9 @@ return this._nodes[this._first]; | ||
/** | ||
* The `pollLast()` function removes and returns the last element in a data structure. | ||
* The `popLast()` function removes and returns the last element in a data structure. | ||
* @returns The value that was removed from the data structure. | ||
*/ | ||
pollLast() { | ||
popLast() { | ||
if (!this._size) | ||
return; | ||
const value = this.peekLast(); | ||
const value = this.getLast(); | ||
delete this._nodes[this._last]; | ||
@@ -125,6 +125,6 @@ this._last--; | ||
/** | ||
* The `peekLast()` function returns the last element in an array-like data structure. | ||
* The `getLast()` function returns the last element in an array-like data structure. | ||
* @returns The last element in the array "_nodes" is being returned. | ||
*/ | ||
peekLast() { | ||
getLast() { | ||
if (this._size) | ||
@@ -179,6 +179,6 @@ return this._nodes[this._last]; | ||
/** | ||
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty. | ||
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty. | ||
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
*/ | ||
pollLast() { | ||
popLast() { | ||
var _a; | ||
@@ -188,7 +188,7 @@ return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null; | ||
/** | ||
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty. | ||
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is | ||
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty. | ||
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is | ||
* empty. | ||
*/ | ||
pollFirst() { | ||
popFirst() { | ||
var _a; | ||
@@ -210,7 +210,7 @@ return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null; | ||
/** | ||
* The `peekFirst` function returns the first element of an array or null if the array is empty. | ||
* @returns The function `peekFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is | ||
* The `getFirst` function returns the first element of an array or null if the array is empty. | ||
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is | ||
* empty, it will return `null`. | ||
*/ | ||
peekFirst() { | ||
getFirst() { | ||
var _a; | ||
@@ -220,6 +220,6 @@ return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null; | ||
/** | ||
* The `peekLast` function returns the last element of an array or null if the array is empty. | ||
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
* The `getLast` function returns the last element of an array or null if the array is empty. | ||
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
*/ | ||
peekLast() { | ||
getLast() { | ||
var _a; | ||
@@ -226,0 +226,0 @@ return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null; |
@@ -71,7 +71,7 @@ /** | ||
/** | ||
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty. | ||
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty. | ||
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* array is empty, it returns `null`. | ||
*/ | ||
peekLast(): E | undefined; | ||
getLast(): E | undefined; | ||
/** | ||
@@ -78,0 +78,0 @@ * The enqueue function adds a value to the end of a queue. |
@@ -112,7 +112,7 @@ "use strict"; | ||
/** | ||
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty. | ||
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty. | ||
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* array is empty, it returns `null`. | ||
*/ | ||
peekLast() { | ||
getLast() { | ||
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined; | ||
@@ -119,0 +119,0 @@ } |
{ | ||
"name": "directed-graph-typed", | ||
"version": "1.39.1", | ||
"version": "1.39.2", | ||
"description": "Directed Graph. Javascript & Typescript Data Structure.", | ||
@@ -149,4 +149,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.39.0" | ||
"data-structure-typed": "^1.39.2" | ||
} | ||
} |
@@ -24,4 +24,3 @@ /** | ||
extends BST<V, N> | ||
implements IBinaryTree<V, N> | ||
{ | ||
implements IBinaryTree<V, N> { | ||
/** | ||
@@ -165,3 +164,3 @@ * This is a constructor function for an AVL tree data structure in TypeScript. | ||
this._balanceFactor(A) // second O(1) | ||
) { | ||
) { | ||
case -2: | ||
@@ -168,0 +167,0 @@ if (A && A.left) { |
@@ -20,3 +20,3 @@ /** | ||
*/ | ||
constructor({frequency = 0, max}: {frequency?: number; max: number}) { | ||
constructor({frequency = 0, max}: { frequency?: number; max: number }) { | ||
this._freq = frequency; | ||
@@ -23,0 +23,0 @@ this._max = max; |
@@ -9,3 +9,3 @@ /** | ||
import type {OneParamCallback, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions} from '../../types'; | ||
import type {BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, OneParamCallback} from '../../types'; | ||
import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types'; | ||
@@ -112,4 +112,3 @@ import {IBinaryTree} from '../../interfaces'; | ||
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> | ||
implements IBinaryTree<V, N> | ||
{ | ||
implements IBinaryTree<V, N> { | ||
/** | ||
@@ -400,3 +399,3 @@ * Creates a new instance of BinaryTree. | ||
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}]; | ||
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}]; | ||
let maxHeight = 0; | ||
@@ -838,3 +837,3 @@ | ||
// 0: visit, 1: print | ||
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}]; | ||
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}]; | ||
@@ -1101,2 +1100,45 @@ while (stack.length > 0) { | ||
/** | ||
* The above function is an iterator for a binary tree that can be used to traverse the tree in | ||
* either an iterative or recursive manner. | ||
* @param node - The `node` parameter represents the current node in the binary tree from which the | ||
* iteration starts. It is an optional parameter with a default value of `this.root`, which means | ||
* that if no node is provided, the iteration will start from the root of the binary tree. | ||
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the | ||
* binary tree nodes in a specific order. | ||
*/ | ||
* [Symbol.iterator](node = this.root): Generator<BinaryTreeNodeKey, void, undefined> { | ||
if (!node) { | ||
return; | ||
} | ||
if (this.iterationType === IterationType.ITERATIVE) { | ||
const stack: (N | null | undefined)[] = []; | ||
let current: N | null | undefined = node; | ||
while (current || stack.length > 0) { | ||
while (current) { | ||
stack.push(current); | ||
current = current.left; | ||
} | ||
current = stack.pop(); | ||
if (current) yield current.key; | ||
if (current) current = current.right; | ||
} | ||
} else { | ||
if (node.left) { | ||
yield* this[Symbol.iterator](node.left); | ||
} | ||
yield node.key; | ||
if (node.right) { | ||
yield* this[Symbol.iterator](node.right); | ||
} | ||
} | ||
} | ||
/** | ||
* Swap the data of two nodes in the binary tree. | ||
@@ -1103,0 +1145,0 @@ * @param {N} srcNode - The source node to swap. |
@@ -22,4 +22,3 @@ /** | ||
extends BinaryTree<V, N> | ||
implements IBinaryTree<V, N> | ||
{ | ||
implements IBinaryTree<V, N> { | ||
/** | ||
@@ -26,0 +25,0 @@ * The constructor function initializes a binary search tree object with an optional comparator |
@@ -24,4 +24,3 @@ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types'; | ||
extends BST<V, N> | ||
implements IBinaryTree<V, N> | ||
{ | ||
implements IBinaryTree<V, N> { | ||
constructor(options?: RBTreeOptions) { | ||
@@ -28,0 +27,0 @@ super(options); |
@@ -40,4 +40,3 @@ /** | ||
extends AVLTree<V, N> | ||
implements IBinaryTree<V, N> | ||
{ | ||
implements IBinaryTree<V, N> { | ||
/** | ||
@@ -44,0 +43,0 @@ * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to |
@@ -108,4 +108,3 @@ /** | ||
E extends AbstractEdge<any> = AbstractEdge<any> | ||
> implements IGraph<V, E> | ||
{ | ||
> implements IGraph<V, E> { | ||
private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>(); | ||
@@ -558,10 +557,10 @@ | ||
getMinDist && | ||
distMap.forEach((d, v) => { | ||
if (v !== srcVertex) { | ||
if (d < minDist) { | ||
minDist = d; | ||
if (genPaths) minDest = v; | ||
} | ||
distMap.forEach((d, v) => { | ||
if (v !== srcVertex) { | ||
if (d < minDist) { | ||
minDist = d; | ||
if (genPaths) minDest = v; | ||
} | ||
}); | ||
} | ||
}); | ||
@@ -628,3 +627,3 @@ genPaths && getPaths(minDest); | ||
const heap = new PriorityQueue<{key: number; val: V}>({comparator: (a, b) => a.key - b.key}); | ||
const heap = new PriorityQueue<{ key: number; val: V }>({comparator: (a, b) => a.key - b.key}); | ||
heap.add({key: 0, val: srcVertex}); | ||
@@ -858,3 +857,3 @@ | ||
*/ | ||
floyd(): {costs: number[][]; predecessor: (V | null)[][]} { | ||
floyd(): { costs: number[][]; predecessor: (V | null)[][] } { | ||
const idAndVertices = [...this._vertices]; | ||
@@ -861,0 +860,0 @@ const n = idAndVertices.length; |
@@ -67,4 +67,3 @@ /** | ||
extends AbstractGraph<V, E> | ||
implements IGraph<V, E> | ||
{ | ||
implements IGraph<V, E> { | ||
/** | ||
@@ -71,0 +70,0 @@ * The constructor function initializes an instance of a class. |
@@ -54,8 +54,7 @@ /** | ||
export class UndirectedGraph< | ||
V extends UndirectedVertex<any> = UndirectedVertex, | ||
E extends UndirectedEdge<any> = UndirectedEdge | ||
> | ||
V extends UndirectedVertex<any> = UndirectedVertex, | ||
E extends UndirectedEdge<any> = UndirectedEdge | ||
> | ||
extends AbstractGraph<V, E> | ||
implements IGraph<V, E> | ||
{ | ||
implements IGraph<V, E> { | ||
/** | ||
@@ -62,0 +61,0 @@ * The constructor initializes a new Map object to store edges. |
@@ -160,3 +160,3 @@ import {HashFunction} from '../../types'; | ||
*entries(): IterableIterator<[K, V]> { | ||
* entries(): IterableIterator<[K, V]> { | ||
for (const bucket of this.table) { | ||
@@ -163,0 +163,0 @@ if (bucket) { |
@@ -1,1 +0,2 @@ | ||
export class TreeMap {} | ||
export class TreeMap { | ||
} |
@@ -1,1 +0,2 @@ | ||
export class TreeSet {} | ||
export class TreeSet { | ||
} |
@@ -14,3 +14,3 @@ /** | ||
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) { | ||
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) { | ||
this.comparator = options.comparator; | ||
@@ -43,3 +43,3 @@ if (options.nodes && options.nodes.length > 0) { | ||
*/ | ||
static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> { | ||
static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> { | ||
return new Heap<E>(options); | ||
@@ -46,0 +46,0 @@ } |
@@ -14,3 +14,3 @@ /** | ||
constructor( | ||
options: {comparator: Comparator<E>; nodes?: E[]} = { | ||
options: { comparator: Comparator<E>; nodes?: E[] } = { | ||
comparator: (a: E, b: E) => { | ||
@@ -17,0 +17,0 @@ if (!(typeof a === 'number' && typeof b === 'number')) { |
@@ -14,3 +14,3 @@ /** | ||
constructor( | ||
options: {comparator: Comparator<E>; nodes?: E[]} = { | ||
options: { comparator: Comparator<E>; nodes?: E[] } = { | ||
comparator: (a: E, b: E) => { | ||
@@ -17,0 +17,0 @@ if (!(typeof a === 'number' && typeof b === 'number')) { |
@@ -150,7 +150,7 @@ /** | ||
/** | ||
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list. | ||
* The `popLast()` function removes and returns the value of the last node in a doubly linked list. | ||
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the | ||
* list is empty, it returns null. | ||
*/ | ||
pollLast(): E | undefined { | ||
popLast(): E | undefined { | ||
return this.pop(); | ||
@@ -179,7 +179,7 @@ } | ||
/** | ||
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list. | ||
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list. | ||
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked | ||
* list. | ||
*/ | ||
pollFirst(): E | undefined { | ||
popFirst(): E | undefined { | ||
return this.shift(); | ||
@@ -216,6 +216,6 @@ } | ||
/** | ||
* The `peekFirst` function returns the first node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `peekFirst()` returns the first node of the doubly linked list, or `null` if the list is empty. | ||
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty. | ||
*/ | ||
peekFirst(): E | undefined { | ||
getFirst(): E | undefined { | ||
return this.head?.val; | ||
@@ -225,6 +225,6 @@ } | ||
/** | ||
* The `peekLast` function returns the last node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty. | ||
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty. | ||
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty. | ||
*/ | ||
peekLast(): E | undefined { | ||
getLast(): E | undefined { | ||
return this.tail?.val; | ||
@@ -273,3 +273,3 @@ } | ||
*/ | ||
findNode(val: E | null): DoublyLinkedListNode<E> | null { | ||
getNode(val: E | null): DoublyLinkedListNode<E> | null { | ||
let current = this.head; | ||
@@ -319,2 +319,39 @@ | ||
/** | ||
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* before which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked | ||
* list. | ||
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the | ||
* insertion fails. | ||
*/ | ||
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean { | ||
let existingNode; | ||
if (existingValueOrNode instanceof DoublyLinkedListNode) { | ||
existingNode = existingValueOrNode; | ||
} else { | ||
existingNode = this.getNode(existingValueOrNode); | ||
} | ||
if (existingNode) { | ||
const newNode = new DoublyLinkedListNode(newValue); | ||
newNode.prev = existingNode.prev; | ||
if (existingNode.prev) { | ||
existingNode.prev.next = newNode; | ||
} | ||
newNode.next = existingNode; | ||
existingNode.prev = newNode; | ||
if (existingNode === this.head) { | ||
this.head = newNode; | ||
} | ||
this._length++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element. | ||
@@ -340,5 +377,2 @@ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the | ||
delete(valOrNode: E): boolean; | ||
delete(valOrNode: DoublyLinkedListNode<E>): boolean; | ||
/** | ||
@@ -357,3 +391,3 @@ * The `delete` function removes a node from a doubly linked list based on either the node itself or its value. | ||
} else { | ||
node = this.findNode(valOrNode); | ||
node = this.getNode(valOrNode); | ||
} | ||
@@ -448,10 +482,10 @@ | ||
/** | ||
* The `findLast` function iterates through a linked list from the last node to the first node and returns the last | ||
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last | ||
* value that satisfies the given callback function, or null if no value satisfies the callback. | ||
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This | ||
* function is used to determine whether a given value satisfies a certain condition. | ||
* @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by | ||
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by | ||
* the callback function. If no value satisfies the condition, it returns `null`. | ||
*/ | ||
findLast(callback: (val: E) => boolean): E | null { | ||
findBackward(callback: (val: E) => boolean): E | null { | ||
let current = this.tail; | ||
@@ -468,6 +502,6 @@ while (current) { | ||
/** | ||
* The `toArrayReverse` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toArrayReverse()` function returns an array of type `E[]`. | ||
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order. | ||
* @returns The `toArrayBackward()` function returns an array of type `E[]`. | ||
*/ | ||
toArrayReverse(): E[] { | ||
toArrayBackward(): E[] { | ||
const array: E[] = []; | ||
@@ -568,5 +602,2 @@ let current = this.tail; | ||
insertAfter(existingValueOrNode: E, newValue: E): boolean; | ||
insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean; | ||
/** | ||
@@ -587,3 +618,3 @@ * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list. | ||
} else { | ||
existingNode = this.findNode(existingValueOrNode); | ||
existingNode = this.getNode(existingValueOrNode); | ||
} | ||
@@ -610,37 +641,12 @@ | ||
/** | ||
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list. | ||
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list | ||
* before which the new value will be inserted. It can be either the value of the existing node or the existing node | ||
* itself. | ||
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked | ||
* list. | ||
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the | ||
* insertion fails. | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean { | ||
let existingNode; | ||
* [Symbol.iterator]() { | ||
let current = this.head; | ||
if (existingValueOrNode instanceof DoublyLinkedListNode) { | ||
existingNode = existingValueOrNode; | ||
} else { | ||
existingNode = this.findNode(existingValueOrNode); | ||
while (current) { | ||
yield current.val; | ||
current = current.next; | ||
} | ||
if (existingNode) { | ||
const newNode = new DoublyLinkedListNode(newValue); | ||
newNode.prev = existingNode.prev; | ||
if (existingNode.prev) { | ||
existingNode.prev.next = newNode; | ||
} | ||
newNode.next = existingNode; | ||
existingNode.prev = newNode; | ||
if (existingNode === this.head) { | ||
this.head = newNode; | ||
} | ||
this._length++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
@@ -90,13 +90,9 @@ /** | ||
getLength(): number { | ||
return this._length; | ||
} | ||
/** | ||
* The `push` function adds a new node with the given data to the end of a singly linked list. | ||
* @param {E} data - The "data" parameter represents the value that you want to add to the linked list. It can be of | ||
* The `push` function adds a new node with the given val to the end of a singly linked list. | ||
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of | ||
* any type (E) as specified in the generic type declaration of the class or function. | ||
*/ | ||
push(data: E): void { | ||
const newNode = new SinglyLinkedListNode(data); | ||
push(val: E): void { | ||
const newNode = new SinglyLinkedListNode(val); | ||
if (!this.head) { | ||
@@ -113,2 +109,11 @@ this.head = newNode; | ||
/** | ||
* The `push` function adds a new node with the given val to the end of a singly linked list. | ||
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of | ||
* any type (E) as specified in the generic type declaration of the class or function. | ||
*/ | ||
addLast(val: E): void { | ||
this.push(val); | ||
} | ||
/** | ||
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail | ||
@@ -141,2 +146,12 @@ * pointers accordingly. | ||
/** | ||
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail | ||
* pointers accordingly. | ||
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If | ||
* the linked list is empty, it returns `null`. | ||
*/ | ||
popLast(): E | undefined { | ||
return this.pop(); | ||
} | ||
/** | ||
* The `shift()` function removes and returns the value of the first node in a linked list. | ||
@@ -154,2 +169,10 @@ * @returns The value of the node that is being removed from the beginning of the linked list. | ||
/** | ||
* The `popFirst()` 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. | ||
*/ | ||
popFirst(): E | undefined { | ||
return this.shift(); | ||
} | ||
/** | ||
* The unshift function adds a new node with the given value to the beginning of a singly linked list. | ||
@@ -172,2 +195,11 @@ * @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the | ||
/** | ||
* The addFirst function adds a new node with the given value to the beginning of a singly linked list. | ||
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the | ||
* linked list. | ||
*/ | ||
addFirst(val: E): void { | ||
this.unshift(val); | ||
} | ||
/** | ||
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range. | ||
@@ -390,3 +422,3 @@ * @param {number} index - The index parameter is a number that represents the position of the element we want to | ||
*/ | ||
findNode(value: E): SinglyLinkedListNode<E> | null { | ||
getNode(value: E): SinglyLinkedListNode<E> | null { | ||
let current = this.head; | ||
@@ -441,5 +473,2 @@ | ||
insertAfter(existingValueOrNode: E, newValue: E): boolean; | ||
insertAfter(existingValueOrNode: SinglyLinkedListNode<E>, newValue: E): boolean; | ||
/** | ||
@@ -459,3 +488,3 @@ * The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list. | ||
} else { | ||
existingNode = this.findNode(existingValueOrNode); | ||
existingNode = this.getNode(existingValueOrNode); | ||
} | ||
@@ -496,6 +525,82 @@ | ||
*[Symbol.iterator]() { | ||
/** | ||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element. | ||
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument | ||
* represents the value of the current node in the linked list, and the index argument represents the index of the | ||
* current node in the linked list. | ||
*/ | ||
forEach(callback: (val: E, index: number) => void): void { | ||
let current = this.head; | ||
let index = 0; | ||
while (current) { | ||
callback(current.val, index); | ||
current = current.next; | ||
index++; | ||
} | ||
} | ||
/** | ||
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new | ||
* SinglyLinkedList with the transformed values. | ||
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in | ||
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped | ||
* SinglyLinkedList). | ||
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values. | ||
*/ | ||
map<U>(callback: (val: E) => U): SinglyLinkedList<U> { | ||
const mappedList = new SinglyLinkedList<U>(); | ||
let current = this.head; | ||
while (current) { | ||
mappedList.push(callback(current.val)); | ||
current = current.next; | ||
} | ||
return mappedList; | ||
} | ||
/** | ||
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the | ||
* elements that satisfy the given callback function. | ||
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value. | ||
* It is used to determine whether a value should be included in the filtered list or not. | ||
* @returns The filtered list, which is an instance of the SinglyLinkedList class. | ||
*/ | ||
filter(callback: (val: E) => boolean): SinglyLinkedList<E> { | ||
const filteredList = new SinglyLinkedList<E>(); | ||
let current = this.head; | ||
while (current) { | ||
if (callback(current.val)) { | ||
filteredList.push(current.val); | ||
} | ||
current = current.next; | ||
} | ||
return filteredList; | ||
} | ||
/** | ||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a | ||
* single value. | ||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is | ||
* used to perform a specific operation on each element of the linked list. | ||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting | ||
* point for the reduction operation. | ||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the | ||
* elements in the linked list. | ||
*/ | ||
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U { | ||
let accumulator = initialValue; | ||
let current = this.head; | ||
while (current) { | ||
accumulator = callback(accumulator, current.val); | ||
current = current.next; | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
* The function returns an iterator that iterates over the values of a linked list. | ||
*/ | ||
* [Symbol.iterator]() { | ||
let current = this.head; | ||
while (current) { | ||
yield current.val; | ||
@@ -502,0 +607,0 @@ current = current.next; |
@@ -17,3 +17,3 @@ /** | ||
*/ | ||
constructor(options: {row: number; col: number; initialVal?: V}) { | ||
constructor(options: { row: number; col: number; initialVal?: V }) { | ||
const {row, col, initialVal} = options; | ||
@@ -20,0 +20,0 @@ this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0)); |
@@ -13,3 +13,4 @@ /** | ||
public w: number = 1 // needed for matrix multiplication | ||
) {} | ||
) { | ||
} | ||
@@ -16,0 +17,0 @@ /** |
@@ -13,3 +13,3 @@ /** | ||
constructor( | ||
options: {comparator: Comparator<E>; nodes?: E[]} = { | ||
options: { comparator: Comparator<E>; nodes?: E[] } = { | ||
comparator: (a: E, b: E) => { | ||
@@ -16,0 +16,0 @@ if (!(typeof a === 'number' && typeof b === 'number')) { |
@@ -13,3 +13,3 @@ /** | ||
constructor( | ||
options: {comparator: Comparator<E>; nodes?: E[]} = { | ||
options: { comparator: Comparator<E>; nodes?: E[] } = { | ||
comparator: (a: E, b: E) => { | ||
@@ -16,0 +16,0 @@ if (!(typeof a === 'number' && typeof b === 'number')) { |
@@ -13,5 +13,5 @@ /** | ||
export class PriorityQueue<E = any> extends Heap<E> { | ||
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) { | ||
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) { | ||
super(options); | ||
} | ||
} |
@@ -12,3 +12,4 @@ /** | ||
// O(1) time complexity of adding at the beginning and the end | ||
export class Deque<E = any> extends DoublyLinkedList<E> {} | ||
export class Deque<E = any> extends DoublyLinkedList<E> { | ||
} | ||
@@ -23,5 +24,5 @@ // O(1) time complexity of obtaining the value | ||
private _nodes: {[key: number]: E} = {}; | ||
private _nodes: { [key: number]: E } = {}; | ||
get nodes(): {[p: number]: E} { | ||
get nodes(): { [p: number]: E } { | ||
return this._nodes; | ||
@@ -100,8 +101,8 @@ } | ||
/** | ||
* The function `pollFirst()` removes and returns the first element in a data structure. | ||
* The function `popFirst()` removes and returns the first element in a data structure. | ||
* @returns The value of the first element in the data structure. | ||
*/ | ||
pollFirst() { | ||
popFirst() { | ||
if (!this._size) return; | ||
const value = this.peekFirst(); | ||
const value = this.getFirst(); | ||
delete this._nodes[this._first]; | ||
@@ -114,6 +115,6 @@ this._first++; | ||
/** | ||
* The `peekFirst` function returns the first element in an array-like data structure if it exists. | ||
* The `getFirst` function returns the first element in an array-like data structure if it exists. | ||
* @returns The element at the first position of the `_nodes` array. | ||
*/ | ||
peekFirst() { | ||
getFirst() { | ||
if (this._size) return this._nodes[this._first]; | ||
@@ -123,8 +124,8 @@ } | ||
/** | ||
* The `pollLast()` function removes and returns the last element in a data structure. | ||
* The `popLast()` function removes and returns the last element in a data structure. | ||
* @returns The value that was removed from the data structure. | ||
*/ | ||
pollLast() { | ||
popLast() { | ||
if (!this._size) return; | ||
const value = this.peekLast(); | ||
const value = this.getLast(); | ||
delete this._nodes[this._last]; | ||
@@ -138,6 +139,6 @@ this._last--; | ||
/** | ||
* The `peekLast()` function returns the last element in an array-like data structure. | ||
* The `getLast()` function returns the last element in an array-like data structure. | ||
* @returns The last element in the array "_nodes" is being returned. | ||
*/ | ||
peekLast() { | ||
getLast() { | ||
if (this._size) return this._nodes[this._last]; | ||
@@ -165,3 +166,3 @@ } | ||
protected _seNodes(value: {[p: number]: E}) { | ||
protected _seNodes(value: { [p: number]: E }) { | ||
this._nodes = value; | ||
@@ -198,6 +199,6 @@ } | ||
/** | ||
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty. | ||
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty. | ||
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
*/ | ||
pollLast(): E | null { | ||
popLast(): E | null { | ||
return this._nodes.pop() ?? null; | ||
@@ -207,7 +208,7 @@ } | ||
/** | ||
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty. | ||
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is | ||
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty. | ||
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is | ||
* empty. | ||
*/ | ||
pollFirst(): E | null { | ||
popFirst(): E | null { | ||
return this._nodes.shift() ?? null; | ||
@@ -231,7 +232,7 @@ } | ||
/** | ||
* The `peekFirst` function returns the first element of an array or null if the array is empty. | ||
* @returns The function `peekFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is | ||
* The `getFirst` function returns the first element of an array or null if the array is empty. | ||
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is | ||
* empty, it will return `null`. | ||
*/ | ||
peekFirst(): E | null { | ||
getFirst(): E | null { | ||
return this._nodes[0] ?? null; | ||
@@ -241,6 +242,6 @@ } | ||
/** | ||
* The `peekLast` function returns the last element of an array or null if the array is empty. | ||
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
* The `getLast` function returns the last element of an array or null if the array is empty. | ||
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty. | ||
*/ | ||
peekLast(): E | null { | ||
getLast(): E | null { | ||
return this._nodes[this._nodes.length - 1] ?? null; | ||
@@ -247,0 +248,0 @@ } |
@@ -126,7 +126,7 @@ /** | ||
/** | ||
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty. | ||
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty. | ||
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the | ||
* array is empty, it returns `null`. | ||
*/ | ||
peekLast(): E | undefined { | ||
getLast(): E | undefined { | ||
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined; | ||
@@ -187,3 +187,3 @@ } | ||
*[Symbol.iterator]() { | ||
* [Symbol.iterator]() { | ||
for (const item of this.nodes) { | ||
@@ -190,0 +190,0 @@ yield item; |
export type Direction = 'up' | 'right' | 'down' | 'left'; | ||
export type Turning = {[key in Direction]: Direction}; | ||
export type Turning = { [key in Direction]: Direction }; | ||
@@ -5,0 +5,0 @@ export type NavigatorParams<T = any> = { |
export type ToThunkFn = () => ReturnType<TrlFn>; | ||
export type Thunk = () => ReturnType<ToThunkFn> & {__THUNK__: symbol}; | ||
export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: symbol }; | ||
export type TrlFn = (...args: any[]) => any; | ||
@@ -4,0 +4,0 @@ export type TrlAsyncFn = (...args: any[]) => any; |
@@ -1,4 +0,4 @@ | ||
export type KeyValueObject = {[key: string]: any}; | ||
export type KeyValueObject = { [key: string]: any }; | ||
export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol}; | ||
export type KeyValueObjectWithKey = { [key: string]: any; key: string | number | symbol }; | ||
@@ -5,0 +5,0 @@ export type NonNumberNonObjectButDefined = string | boolean | symbol | null; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1472738
25704
Updateddata-structure-typed@^1.39.2