red-black-tree-typed
Advanced tools
Comparing version 1.50.2 to 1.50.3
@@ -201,3 +201,6 @@ import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from '../../types'; | ||
abstract isEmpty(): boolean; | ||
abstract clear(): void; | ||
abstract clone(): any; | ||
abstract map(...args: any[]): any; | ||
abstract filter(...args: any[]): any; | ||
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>; | ||
@@ -347,4 +350,7 @@ } | ||
abstract isEmpty(): boolean; | ||
abstract clear(): void; | ||
abstract clone(): C; | ||
abstract map(...args: any[]): any; | ||
abstract filter(...args: any[]): any; | ||
protected abstract _getIterator(...args: any[]): IterableIterator<E>; | ||
} |
@@ -12,4 +12,23 @@ /** | ||
export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> { | ||
height: number; | ||
/** | ||
* The constructor function initializes a new instance of a class with a key and an optional value, | ||
* and sets the height property to 0. | ||
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the | ||
* constructor. It is used to initialize the key property of the object being created. | ||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the | ||
* value associated with the key in the constructor. | ||
*/ | ||
constructor(key: K, value?: V); | ||
protected _height: number; | ||
/** | ||
* The function returns the value of the height property. | ||
* @returns The height of the object. | ||
*/ | ||
get height(): number; | ||
/** | ||
* The above function sets the value of the height property. | ||
* @param {number} value - The value parameter is a number that represents the new height value to be | ||
* set. | ||
*/ | ||
set height(value: number); | ||
} | ||
@@ -199,3 +218,12 @@ /** | ||
protected _balancePath(node: KeyOrNodeOrEntry<K, V, NODE>): void; | ||
/** | ||
* The function replaces an old node with a new node while preserving the height of the old node. | ||
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace with the | ||
* `newNode`. | ||
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in | ||
* the data structure. | ||
* @returns the result of calling the `_replaceNode` method on the superclass, passing in the | ||
* `oldNode` and `newNode` as arguments. | ||
*/ | ||
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE; | ||
} |
@@ -13,6 +13,29 @@ "use strict"; | ||
class AVLTreeNode extends bst_1.BSTNode { | ||
/** | ||
* The constructor function initializes a new instance of a class with a key and an optional value, | ||
* and sets the height property to 0. | ||
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the | ||
* constructor. It is used to initialize the key property of the object being created. | ||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the | ||
* value associated with the key in the constructor. | ||
*/ | ||
constructor(key, value) { | ||
super(key, value); | ||
this.height = 0; | ||
this._height = 0; | ||
} | ||
/** | ||
* The function returns the value of the height property. | ||
* @returns The height of the object. | ||
*/ | ||
get height() { | ||
return this._height; | ||
} | ||
/** | ||
* The above function sets the value of the height property. | ||
* @param {number} value - The value parameter is a number that represents the new height value to be | ||
* set. | ||
*/ | ||
set height(value) { | ||
this._height = value; | ||
} | ||
} | ||
@@ -454,2 +477,11 @@ exports.AVLTreeNode = AVLTreeNode; | ||
} | ||
/** | ||
* The function replaces an old node with a new node while preserving the height of the old node. | ||
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace with the | ||
* `newNode`. | ||
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in | ||
* the data structure. | ||
* @returns the result of calling the `_replaceNode` method on the superclass, passing in the | ||
* `oldNode` and `newNode` as arguments. | ||
*/ | ||
_replaceNode(oldNode, newNode) { | ||
@@ -456,0 +488,0 @@ newNode.height = oldNode.height; |
@@ -15,8 +15,30 @@ export declare class BinaryIndexedTree { | ||
protected _freqMap: Record<number, number>; | ||
/** | ||
* The function returns the frequency map of numbers. | ||
* @returns The `_freqMap` property, which is a record with number keys and number values, is being | ||
* returned. | ||
*/ | ||
get freqMap(): Record<number, number>; | ||
protected _msb: number; | ||
/** | ||
* The function returns the value of the _msb property. | ||
* @returns The `_msb` property of the object. | ||
*/ | ||
get msb(): number; | ||
protected _negativeCount: number; | ||
/** | ||
* The function returns the value of the _negativeCount property. | ||
* @returns The method is returning the value of the variable `_negativeCount`, which is of type | ||
* `number`. | ||
*/ | ||
get negativeCount(): number; | ||
/** | ||
* The above function returns the value of the protected variable `_freq`. | ||
* @returns The frequency value stored in the protected variable `_freq`. | ||
*/ | ||
get freq(): number; | ||
/** | ||
* The above function returns the maximum value. | ||
* @returns The maximum value stored in the variable `_max`. | ||
*/ | ||
get max(): number; | ||
@@ -23,0 +45,0 @@ /** |
@@ -26,14 +26,36 @@ "use strict"; | ||
} | ||
/** | ||
* The function returns the frequency map of numbers. | ||
* @returns The `_freqMap` property, which is a record with number keys and number values, is being | ||
* returned. | ||
*/ | ||
get freqMap() { | ||
return this._freqMap; | ||
} | ||
/** | ||
* The function returns the value of the _msb property. | ||
* @returns The `_msb` property of the object. | ||
*/ | ||
get msb() { | ||
return this._msb; | ||
} | ||
/** | ||
* The function returns the value of the _negativeCount property. | ||
* @returns The method is returning the value of the variable `_negativeCount`, which is of type | ||
* `number`. | ||
*/ | ||
get negativeCount() { | ||
return this._negativeCount; | ||
} | ||
/** | ||
* The above function returns the value of the protected variable `_freq`. | ||
* @returns The frequency value stored in the protected variable `_freq`. | ||
*/ | ||
get freq() { | ||
return this._freq; | ||
} | ||
/** | ||
* The above function returns the maximum value. | ||
* @returns The maximum value stored in the variable `_max`. | ||
*/ | ||
get max() { | ||
@@ -40,0 +62,0 @@ return this._max; |
@@ -24,3 +24,3 @@ /** | ||
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the | ||
* constructor. It is used to set the value of the "key" property of the object being created. | ||
* constructor. It is used to set the key property of the object being created. | ||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the | ||
@@ -27,0 +27,0 @@ * value associated with the key in the constructor. |
@@ -16,6 +16,25 @@ /** | ||
protected _left?: NODE; | ||
/** | ||
* The function returns the value of the `_left` property. | ||
* @returns The `_left` property of the current object is being returned. | ||
*/ | ||
get left(): NODE | undefined; | ||
/** | ||
* The function sets the left child of a node and updates the parent reference of the child. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an | ||
* instance of the `NODE` class or `undefined`. | ||
*/ | ||
set left(v: NODE | undefined); | ||
protected _right?: NODE; | ||
/** | ||
* The function returns the right node of a binary tree or undefined if there is no right node. | ||
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or | ||
* `undefined`. | ||
*/ | ||
get right(): NODE | undefined; | ||
/** | ||
* The function sets the right child of a node and updates the parent reference of the child. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a | ||
* `NODE` object or `undefined`. | ||
*/ | ||
set right(v: NODE | undefined); | ||
@@ -34,6 +53,6 @@ } | ||
/** | ||
* This is the constructor function for a binary search tree class in TypeScript, which initializes | ||
* the tree with optional keysOrNodesOrEntries and options. | ||
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* binary search tree. | ||
* This is the constructor function for a TypeScript class that initializes a binary search tree with | ||
* optional keys or nodes or entries and options. | ||
* @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used | ||
* to initialize the binary search tree with the provided keys, nodes, or entries. | ||
* @param [options] - The `options` parameter is an optional object that can contain additional | ||
@@ -44,12 +63,20 @@ * configuration options for the binary search tree. It can have the following properties: | ||
protected _root?: NODE; | ||
/** | ||
* The function returns the root node of a tree structure. | ||
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`. | ||
*/ | ||
get root(): NODE | undefined; | ||
protected _variant: BSTVariant; | ||
/** | ||
* The function returns the value of the _variant property. | ||
* @returns The value of the `_variant` property. | ||
*/ | ||
get variant(): BSTVariant; | ||
/** | ||
* The function creates a new binary search tree node with the given key and value. | ||
* @param {K} key - The key parameter is the key value that will be associated with | ||
* the new node. It is used to determine the position of the node in the binary search tree. | ||
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It | ||
* represents the value associated with the node in a binary search tree. | ||
* @returns a new instance of the BSTNode class with the specified key and value. | ||
* The function creates a new BSTNode with the given key and value and returns it. | ||
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node | ||
* being created. | ||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the | ||
* value associated with the key in the node being created. | ||
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type. | ||
*/ | ||
@@ -60,5 +87,6 @@ createNode(key: K, value?: V): NODE; | ||
* @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type | ||
* that defines various options for creating a binary search tree. | ||
* @returns a new instance of the BST class with the specified options. | ||
* behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a | ||
* partial object of type `BSTOptions<K>`. | ||
* @returns a new instance of the BST class, with the provided options merged with the default | ||
* options. The returned value is casted as TREE. | ||
*/ | ||
@@ -340,2 +368,7 @@ createTree(options?: Partial<BSTOptions<K>>): TREE; | ||
isAVLBalanced(iterationType?: IterationType): boolean; | ||
/** | ||
* The function sets the root property of an object and updates the parent property of the new root. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it | ||
* can either be an object of type `NODE` or it can be `undefined`. | ||
*/ | ||
protected _setRoot(v: NODE | undefined): void; | ||
@@ -342,0 +375,0 @@ /** |
@@ -14,5 +14,14 @@ "use strict"; | ||
} | ||
/** | ||
* The function returns the value of the `_left` property. | ||
* @returns The `_left` property of the current object is being returned. | ||
*/ | ||
get left() { | ||
return this._left; | ||
} | ||
/** | ||
* The function sets the left child of a node and updates the parent reference of the child. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an | ||
* instance of the `NODE` class or `undefined`. | ||
*/ | ||
set left(v) { | ||
@@ -24,5 +33,15 @@ if (v) { | ||
} | ||
/** | ||
* The function returns the right node of a binary tree or undefined if there is no right node. | ||
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or | ||
* `undefined`. | ||
*/ | ||
get right() { | ||
return this._right; | ||
} | ||
/** | ||
* The function sets the right child of a node and updates the parent reference of the child. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a | ||
* `NODE` object or `undefined`. | ||
*/ | ||
set right(v) { | ||
@@ -47,6 +66,6 @@ if (v) { | ||
/** | ||
* This is the constructor function for a binary search tree class in TypeScript, which initializes | ||
* the tree with optional keysOrNodesOrEntries and options. | ||
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* binary search tree. | ||
* This is the constructor function for a TypeScript class that initializes a binary search tree with | ||
* optional keys or nodes or entries and options. | ||
* @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used | ||
* to initialize the binary search tree with the provided keys, nodes, or entries. | ||
* @param [options] - The `options` parameter is an optional object that can contain additional | ||
@@ -67,5 +86,13 @@ * configuration options for the binary search tree. It can have the following properties: | ||
} | ||
/** | ||
* The function returns the root node of a tree structure. | ||
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`. | ||
*/ | ||
get root() { | ||
return this._root; | ||
} | ||
/** | ||
* The function returns the value of the _variant property. | ||
* @returns The value of the `_variant` property. | ||
*/ | ||
get variant() { | ||
@@ -75,8 +102,8 @@ return this._variant; | ||
/** | ||
* The function creates a new binary search tree node with the given key and value. | ||
* @param {K} key - The key parameter is the key value that will be associated with | ||
* the new node. It is used to determine the position of the node in the binary search tree. | ||
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It | ||
* represents the value associated with the node in a binary search tree. | ||
* @returns a new instance of the BSTNode class with the specified key and value. | ||
* The function creates a new BSTNode with the given key and value and returns it. | ||
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node | ||
* being created. | ||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the | ||
* value associated with the key in the node being created. | ||
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type. | ||
*/ | ||
@@ -89,5 +116,6 @@ createNode(key, value) { | ||
* @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type | ||
* that defines various options for creating a binary search tree. | ||
* @returns a new instance of the BST class with the specified options. | ||
* behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a | ||
* partial object of type `BSTOptions<K>`. | ||
* @returns a new instance of the BST class, with the provided options merged with the default | ||
* options. The returned value is casted as TREE. | ||
*/ | ||
@@ -751,2 +779,7 @@ createTree(options) { | ||
} | ||
/** | ||
* The function sets the root property of an object and updates the parent property of the new root. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it | ||
* can either be an object of type `NODE` or it can be `undefined`. | ||
*/ | ||
_setRoot(v) { | ||
@@ -753,0 +786,0 @@ if (v) { |
@@ -12,4 +12,25 @@ /** | ||
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> { | ||
color: RBTNColor; | ||
/** | ||
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a | ||
* color. | ||
* @param {K} key - The key parameter is of type K and represents the key of the node in the | ||
* Red-Black Tree. | ||
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value | ||
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when | ||
* creating a new instance of the Red-Black Tree Node. | ||
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black | ||
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`. | ||
*/ | ||
constructor(key: K, value?: V, color?: RBTNColor); | ||
protected _color: RBTNColor; | ||
/** | ||
* The function returns the color value of a variable. | ||
* @returns The color value stored in the protected variable `_color`. | ||
*/ | ||
get color(): RBTNColor; | ||
/** | ||
* The function sets the color property to the specified value. | ||
* @param {RBTNColor} value - The value parameter is of type RBTNColor. | ||
*/ | ||
set color(value: RBTNColor); | ||
} | ||
@@ -24,3 +45,2 @@ /** | ||
export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>> extends BST<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> { | ||
Sentinel: NODE; | ||
/** | ||
@@ -38,5 +58,19 @@ * This is the constructor function for a Red-Black Tree data structure in TypeScript, which | ||
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K>); | ||
protected _Sentinel: NODE; | ||
/** | ||
* The function returns the value of the `_Sentinel` property. | ||
* @returns The method is returning the value of the `_Sentinel` property. | ||
*/ | ||
get Sentinel(): NODE; | ||
protected _root: NODE; | ||
/** | ||
* The function returns the root node. | ||
* @returns The root node of the data structure. | ||
*/ | ||
get root(): NODE; | ||
protected _size: number; | ||
/** | ||
* The function returns the size of an object. | ||
* @returns The size of the object, which is a number. | ||
*/ | ||
get size(): number; | ||
@@ -80,2 +114,8 @@ /** | ||
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE; | ||
/** | ||
* The function checks if a given node is a real node in a Red-Black Tree. | ||
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means | ||
* it can either be of type `NODE` or `undefined`. | ||
* @returns a boolean value. | ||
*/ | ||
isRealNode(node: NODE | undefined): node is NODE; | ||
@@ -150,2 +190,8 @@ /** | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The "clear" function sets the root node to the sentinel node and resets the size to 0. | ||
*/ | ||
clear(): void; | ||
@@ -166,2 +212,8 @@ /** | ||
getPredecessor(x: NODE): NODE; | ||
/** | ||
* The function sets the root node of a tree structure and updates the parent property of the new | ||
* root node. | ||
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data | ||
* structure. | ||
*/ | ||
protected _setRoot(v: NODE): void; | ||
@@ -168,0 +220,0 @@ /** |
@@ -14,6 +14,31 @@ "use strict"; | ||
class RedBlackTreeNode extends bst_1.BSTNode { | ||
/** | ||
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a | ||
* color. | ||
* @param {K} key - The key parameter is of type K and represents the key of the node in the | ||
* Red-Black Tree. | ||
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value | ||
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when | ||
* creating a new instance of the Red-Black Tree Node. | ||
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black | ||
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`. | ||
*/ | ||
constructor(key, value, color = types_1.RBTNColor.BLACK) { | ||
super(key, value); | ||
this.color = color; | ||
this._color = color; | ||
} | ||
/** | ||
* The function returns the color value of a variable. | ||
* @returns The color value stored in the protected variable `_color`. | ||
*/ | ||
get color() { | ||
return this._color; | ||
} | ||
/** | ||
* The function sets the color property to the specified value. | ||
* @param {RBTNColor} value - The value parameter is of type RBTNColor. | ||
*/ | ||
set color(value) { | ||
this._color = value; | ||
} | ||
} | ||
@@ -42,11 +67,26 @@ exports.RedBlackTreeNode = RedBlackTreeNode; | ||
super([], options); | ||
this.Sentinel = new RedBlackTreeNode(NaN); | ||
this._Sentinel = new RedBlackTreeNode(NaN); | ||
this._size = 0; | ||
this._root = this.Sentinel; | ||
this._root = this._Sentinel; | ||
if (keysOrNodesOrEntries) | ||
super.addMany(keysOrNodesOrEntries); | ||
} | ||
/** | ||
* The function returns the value of the `_Sentinel` property. | ||
* @returns The method is returning the value of the `_Sentinel` property. | ||
*/ | ||
get Sentinel() { | ||
return this._Sentinel; | ||
} | ||
/** | ||
* The function returns the root node. | ||
* @returns The root node of the data structure. | ||
*/ | ||
get root() { | ||
return this._root; | ||
} | ||
/** | ||
* The function returns the size of an object. | ||
* @returns The size of the object, which is a number. | ||
*/ | ||
get size() { | ||
@@ -122,4 +162,10 @@ return this._size; | ||
} | ||
/** | ||
* The function checks if a given node is a real node in a Red-Black Tree. | ||
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means | ||
* it can either be of type `NODE` or `undefined`. | ||
* @returns a boolean value. | ||
*/ | ||
isRealNode(node) { | ||
if (node === this.Sentinel || node === undefined) | ||
if (node === this._Sentinel || node === undefined) | ||
return false; | ||
@@ -149,7 +195,7 @@ return node instanceof RedBlackTreeNode; | ||
return false; | ||
newNode.left = this.Sentinel; | ||
newNode.right = this.Sentinel; | ||
newNode.left = this._Sentinel; | ||
newNode.right = this._Sentinel; | ||
let y = undefined; | ||
let x = this.root; | ||
while (x !== this.Sentinel) { | ||
while (x !== this._Sentinel) { | ||
y = x; | ||
@@ -218,5 +264,5 @@ if (x) { | ||
const helper = (node) => { | ||
let z = this.Sentinel; | ||
let z = this._Sentinel; | ||
let x, y; | ||
while (node !== this.Sentinel) { | ||
while (node !== this._Sentinel) { | ||
if (node && callback(node) === identifier) { | ||
@@ -232,3 +278,3 @@ z = node; | ||
} | ||
if (z === this.Sentinel) { | ||
if (z === this._Sentinel) { | ||
this._size--; | ||
@@ -239,7 +285,7 @@ return; | ||
let yOriginalColor = y.color; | ||
if (z.left === this.Sentinel) { | ||
if (z.left === this._Sentinel) { | ||
x = z.right; | ||
this._rbTransplant(z, z.right); | ||
} | ||
else if (z.right === this.Sentinel) { | ||
else if (z.right === this._Sentinel) { | ||
x = z.left; | ||
@@ -310,4 +356,10 @@ this._rbTransplant(z, z.left); | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The "clear" function sets the root node to the sentinel node and resets the size to 0. | ||
*/ | ||
clear() { | ||
this._root = this.Sentinel; | ||
this._root = this._Sentinel; | ||
this._size = 0; | ||
@@ -339,2 +391,8 @@ } | ||
} | ||
/** | ||
* The function sets the root node of a tree structure and updates the parent property of the new | ||
* root node. | ||
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data | ||
* structure. | ||
*/ | ||
_setRoot(v) { | ||
@@ -361,3 +419,3 @@ if (v) { | ||
x.right = y.left; | ||
if (y.left !== this.Sentinel) { | ||
if (y.left !== this._Sentinel) { | ||
if (y.left) | ||
@@ -396,3 +454,3 @@ y.left.parent = x; | ||
x.left = y.right; | ||
if (y.right !== this.Sentinel) { | ||
if (y.right !== this._Sentinel) { | ||
if (y.right) | ||
@@ -399,0 +457,0 @@ y.right.parent = x; |
@@ -10,9 +10,86 @@ /** | ||
export declare class SegmentTreeNode { | ||
start: number; | ||
end: number; | ||
value: SegmentTreeNodeVal | undefined; | ||
sum: number; | ||
left: SegmentTreeNode | undefined; | ||
right: SegmentTreeNode | undefined; | ||
/** | ||
* The constructor initializes the properties of a SegmentTreeNode object. | ||
* @param {number} start - The `start` parameter represents the starting index of the segment covered | ||
* by this node in a segment tree. | ||
* @param {number} end - The `end` parameter represents the end index of the segment covered by this | ||
* node in a segment tree. | ||
* @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by | ||
* the segment tree node. | ||
* @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter | ||
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node. | ||
*/ | ||
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined); | ||
protected _start: number; | ||
/** | ||
* The function returns the value of the protected variable _start. | ||
* @returns The start value, which is of type number. | ||
*/ | ||
get start(): number; | ||
/** | ||
* The above function sets the value of the "start" property. | ||
* @param {number} value - The value parameter is of type number. | ||
*/ | ||
set start(value: number); | ||
protected _end: number; | ||
/** | ||
* The function returns the value of the protected variable `_end`. | ||
* @returns The value of the protected property `_end`. | ||
*/ | ||
get end(): number; | ||
/** | ||
* The above function sets the value of the "end" property. | ||
* @param {number} value - The value parameter is a number that represents the new value for the end | ||
* property. | ||
*/ | ||
set end(value: number); | ||
protected _value: SegmentTreeNodeVal | undefined; | ||
/** | ||
* The function returns the value of a segment tree node. | ||
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`. | ||
*/ | ||
get value(): SegmentTreeNodeVal | undefined; | ||
/** | ||
* The function sets the value of a segment tree node. | ||
* @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type | ||
* `SegmentTreeNodeVal` or `undefined`. | ||
*/ | ||
set value(value: SegmentTreeNodeVal | undefined); | ||
protected _sum: number; | ||
/** | ||
* The function returns the value of the sum property. | ||
* @returns The method is returning the value of the variable `_sum`. | ||
*/ | ||
get sum(): number; | ||
/** | ||
* The above function sets the value of the sum property. | ||
* @param {number} value - The parameter "value" is of type "number". | ||
*/ | ||
set sum(value: number); | ||
protected _left: SegmentTreeNode | undefined; | ||
/** | ||
* The function returns the left child of a segment tree node. | ||
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type | ||
* `SegmentTreeNode` or `undefined`. | ||
*/ | ||
get left(): SegmentTreeNode | undefined; | ||
/** | ||
* The function sets the value of the left property of a SegmentTreeNode object. | ||
* @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or | ||
* undefined. | ||
*/ | ||
set left(value: SegmentTreeNode | undefined); | ||
protected _right: SegmentTreeNode | undefined; | ||
/** | ||
* The function returns the right child of a segment tree node. | ||
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`. | ||
*/ | ||
get right(): SegmentTreeNode | undefined; | ||
/** | ||
* The function sets the right child of a segment tree node. | ||
* @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode | | ||
* undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its | ||
* value. | ||
*/ | ||
set right(value: SegmentTreeNode | undefined); | ||
} | ||
@@ -31,8 +108,24 @@ export declare class SegmentTree { | ||
protected _values: number[]; | ||
/** | ||
* The function returns an array of numbers. | ||
* @returns An array of numbers is being returned. | ||
*/ | ||
get values(): number[]; | ||
protected _start: number; | ||
/** | ||
* The function returns the value of the protected variable _start. | ||
* @returns The start value, which is of type number. | ||
*/ | ||
get start(): number; | ||
protected _end: number; | ||
/** | ||
* The function returns the value of the protected variable `_end`. | ||
* @returns The value of the protected property `_end`. | ||
*/ | ||
get end(): number; | ||
protected _root: SegmentTreeNode | undefined; | ||
/** | ||
* The function returns the root node of a segment tree. | ||
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined. | ||
*/ | ||
get root(): SegmentTreeNode | undefined; | ||
@@ -39,0 +132,0 @@ /** |
@@ -12,14 +12,115 @@ "use strict"; | ||
class SegmentTreeNode { | ||
/** | ||
* The constructor initializes the properties of a SegmentTreeNode object. | ||
* @param {number} start - The `start` parameter represents the starting index of the segment covered | ||
* by this node in a segment tree. | ||
* @param {number} end - The `end` parameter represents the end index of the segment covered by this | ||
* node in a segment tree. | ||
* @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by | ||
* the segment tree node. | ||
* @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter | ||
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node. | ||
*/ | ||
constructor(start, end, sum, value) { | ||
this.start = 0; | ||
this.end = 0; | ||
this.value = undefined; | ||
this.sum = 0; | ||
this.left = undefined; | ||
this.right = undefined; | ||
this.start = start; | ||
this.end = end; | ||
this.sum = sum; | ||
this.value = value || undefined; | ||
this._start = 0; | ||
this._end = 0; | ||
this._value = undefined; | ||
this._sum = 0; | ||
this._left = undefined; | ||
this._right = undefined; | ||
this._start = start; | ||
this._end = end; | ||
this._sum = sum; | ||
this._value = value || undefined; | ||
} | ||
/** | ||
* The function returns the value of the protected variable _start. | ||
* @returns The start value, which is of type number. | ||
*/ | ||
get start() { | ||
return this._start; | ||
} | ||
/** | ||
* The above function sets the value of the "start" property. | ||
* @param {number} value - The value parameter is of type number. | ||
*/ | ||
set start(value) { | ||
this._start = value; | ||
} | ||
/** | ||
* The function returns the value of the protected variable `_end`. | ||
* @returns The value of the protected property `_end`. | ||
*/ | ||
get end() { | ||
return this._end; | ||
} | ||
/** | ||
* The above function sets the value of the "end" property. | ||
* @param {number} value - The value parameter is a number that represents the new value for the end | ||
* property. | ||
*/ | ||
set end(value) { | ||
this._end = value; | ||
} | ||
/** | ||
* The function returns the value of a segment tree node. | ||
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`. | ||
*/ | ||
get value() { | ||
return this._value; | ||
} | ||
/** | ||
* The function sets the value of a segment tree node. | ||
* @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type | ||
* `SegmentTreeNodeVal` or `undefined`. | ||
*/ | ||
set value(value) { | ||
this._value = value; | ||
} | ||
/** | ||
* The function returns the value of the sum property. | ||
* @returns The method is returning the value of the variable `_sum`. | ||
*/ | ||
get sum() { | ||
return this._sum; | ||
} | ||
/** | ||
* The above function sets the value of the sum property. | ||
* @param {number} value - The parameter "value" is of type "number". | ||
*/ | ||
set sum(value) { | ||
this._sum = value; | ||
} | ||
/** | ||
* The function returns the left child of a segment tree node. | ||
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type | ||
* `SegmentTreeNode` or `undefined`. | ||
*/ | ||
get left() { | ||
return this._left; | ||
} | ||
/** | ||
* The function sets the value of the left property of a SegmentTreeNode object. | ||
* @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or | ||
* undefined. | ||
*/ | ||
set left(value) { | ||
this._left = value; | ||
} | ||
/** | ||
* The function returns the right child of a segment tree node. | ||
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`. | ||
*/ | ||
get right() { | ||
return this._right; | ||
} | ||
/** | ||
* The function sets the right child of a segment tree node. | ||
* @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode | | ||
* undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its | ||
* value. | ||
*/ | ||
set right(value) { | ||
this._right = value; | ||
} | ||
} | ||
@@ -53,11 +154,27 @@ exports.SegmentTreeNode = SegmentTreeNode; | ||
} | ||
/** | ||
* The function returns an array of numbers. | ||
* @returns An array of numbers is being returned. | ||
*/ | ||
get values() { | ||
return this._values; | ||
} | ||
/** | ||
* The function returns the value of the protected variable _start. | ||
* @returns The start value, which is of type number. | ||
*/ | ||
get start() { | ||
return this._start; | ||
} | ||
/** | ||
* The function returns the value of the protected variable `_end`. | ||
* @returns The value of the protected property `_end`. | ||
*/ | ||
get end() { | ||
return this._end; | ||
} | ||
/** | ||
* The function returns the root node of a segment tree. | ||
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined. | ||
*/ | ||
get root() { | ||
@@ -64,0 +181,0 @@ return this._root; |
@@ -13,3 +13,2 @@ /** | ||
export declare class TreeMultimapNode<K = any, V = any, NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> { | ||
count: number; | ||
/** | ||
@@ -26,2 +25,14 @@ * The constructor function initializes a BinaryTreeNode object with a key, value, and count. | ||
constructor(key: K, value?: V, count?: number); | ||
protected _count: number; | ||
/** | ||
* The function returns the value of the protected variable _count. | ||
* @returns The count property of the object, which is of type number. | ||
*/ | ||
get count(): number; | ||
/** | ||
* The above function sets the value of the count property. | ||
* @param {number} value - The value parameter is of type number, which means it can accept any | ||
* numeric value. | ||
*/ | ||
set count(value: number); | ||
} | ||
@@ -33,3 +44,8 @@ /** | ||
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: TreeMultimapOptions<K>); | ||
private _count; | ||
protected _count: number; | ||
/** | ||
* The function calculates the sum of the count property of all nodes in a tree using depth-first | ||
* search. | ||
* @returns the sum of the count property of all nodes in the tree. | ||
*/ | ||
get count(): number; | ||
@@ -46,2 +62,11 @@ /** | ||
createNode(key: K, value?: V, count?: number): NODE; | ||
/** | ||
* The function creates a new TreeMultimap object with the specified options and returns it. | ||
* @param [options] - The `options` parameter is an optional object that contains additional | ||
* configuration options for creating the `TreeMultimap` object. It can include properties such as | ||
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of | ||
* the tree, respectively. These properties can be | ||
* @returns a new instance of the `TreeMultimap` class, with the provided options merged with the | ||
* default options. The returned value is casted as `TREE`. | ||
*/ | ||
createTree(options?: TreeMultimapOptions<K>): TREE; | ||
@@ -161,3 +186,11 @@ /** | ||
protected _swapProperties(srcNode: BSTNKeyOrNode<K, NODE>, destNode: BSTNKeyOrNode<K, NODE>): NODE | undefined; | ||
/** | ||
* The function replaces an old node with a new node and updates the count property of the new node. | ||
* @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that | ||
* needs to be replaced in a data structure. | ||
* @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`. | ||
* @returns The method is returning the result of calling the `_replaceNode` method from the | ||
* superclass, after updating the `count` property of the `newNode` object. | ||
*/ | ||
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE; | ||
} |
@@ -19,4 +19,20 @@ "use strict"; | ||
super(key, value); | ||
this._count = 1; | ||
this.count = count; | ||
} | ||
/** | ||
* The function returns the value of the protected variable _count. | ||
* @returns The count property of the object, which is of type number. | ||
*/ | ||
get count() { | ||
return this._count; | ||
} | ||
/** | ||
* The above function sets the value of the count property. | ||
* @param {number} value - The value parameter is of type number, which means it can accept any | ||
* numeric value. | ||
*/ | ||
set count(value) { | ||
this._count = value; | ||
} | ||
} | ||
@@ -35,2 +51,7 @@ exports.TreeMultimapNode = TreeMultimapNode; | ||
// TODO the _count is not accurate after nodes count modified | ||
/** | ||
* The function calculates the sum of the count property of all nodes in a tree using depth-first | ||
* search. | ||
* @returns the sum of the count property of all nodes in the tree. | ||
*/ | ||
get count() { | ||
@@ -53,2 +74,11 @@ let sum = 0; | ||
} | ||
/** | ||
* The function creates a new TreeMultimap object with the specified options and returns it. | ||
* @param [options] - The `options` parameter is an optional object that contains additional | ||
* configuration options for creating the `TreeMultimap` object. It can include properties such as | ||
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of | ||
* the tree, respectively. These properties can be | ||
* @returns a new instance of the `TreeMultimap` class, with the provided options merged with the | ||
* default options. The returned value is casted as `TREE`. | ||
*/ | ||
createTree(options) { | ||
@@ -325,2 +355,10 @@ return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options)); | ||
} | ||
/** | ||
* The function replaces an old node with a new node and updates the count property of the new node. | ||
* @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that | ||
* needs to be replaced in a data structure. | ||
* @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`. | ||
* @returns The method is returning the result of calling the `_replaceNode` method from the | ||
* superclass, after updating the `count` property of the `newNode` object. | ||
*/ | ||
_replaceNode(oldNode, newNode) { | ||
@@ -327,0 +365,0 @@ newNode.count = oldNode.count + newNode.count; |
@@ -366,80 +366,2 @@ /** | ||
/** | ||
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Linear space (Tarjan's algorithm). | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find cycles in directed or undirected graph | ||
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time, | ||
* Tarjan solve the bi-connected components of undirected graphs; | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
* / | ||
/** | ||
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Linear space (Tarjan's algorithm). | ||
* | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find cycles in directed or undirected graph | ||
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time, | ||
* Tarjan solve the bi-connected components of undirected graphs; | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges, | ||
* strongly connected components (SCCs), and cycles in a graph. | ||
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the | ||
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the | ||
* number of connected components in the graph. | ||
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges | ||
* (edgeMap whose removal would increase the number of connected components in the graph). | ||
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the | ||
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the | ||
* SCCs will not be calculated or returned. | ||
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If | ||
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values | ||
* are arrays of vertexMap that form cycles within the SCCs. | ||
* @returns The function `tarjan` returns an object with the following properties: | ||
*/ | ||
tarjan(needCutVertexes?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean): { | ||
dfnMap: Map<VO, number>; | ||
lowMap: Map<VO, number>; | ||
bridges: EO[]; | ||
cutVertexes: VO[]; | ||
SCCs: Map<number, VO[]>; | ||
cycles: Map<number, VO[]>; | ||
}; | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
*/ | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
* | ||
* The function returns a map that associates each vertex object with its corresponding depth-first | ||
* number. | ||
* @returns A Map object with keys of type VO and values of type number. | ||
*/ | ||
getDFNMap(): Map<VO, number>; | ||
/** | ||
* The function returns a Map object that contains the low values of each vertex in a Tarjan | ||
* algorithm. | ||
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of | ||
* type `number`. | ||
*/ | ||
getLowMap(): Map<VO, number>; | ||
/** | ||
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm. | ||
* @returns an array of VO objects, specifically the cut vertexes. | ||
*/ | ||
getCutVertexes(): VO[]; | ||
/** | ||
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan | ||
* algorithm. | ||
* @returns a map where the keys are numbers and the values are arrays of VO objects. | ||
*/ | ||
getSCCs(): Map<number, VO[]>; | ||
/** | ||
* The function "getBridges" returns an array of bridges using the Tarjan algorithm. | ||
* @returns the bridges found using the Tarjan algorithm. | ||
*/ | ||
getBridges(): EO[]; | ||
/** | ||
* O(V+E+C) | ||
@@ -446,0 +368,0 @@ * O(V+C) |
@@ -824,191 +824,2 @@ "use strict"; | ||
/** | ||
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Linear space (Tarjan's algorithm). | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find cycles in directed or undirected graph | ||
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time, | ||
* Tarjan solve the bi-connected components of undirected graphs; | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
* / | ||
/** | ||
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Linear space (Tarjan's algorithm). | ||
* | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find cycles in directed or undirected graph | ||
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time, | ||
* Tarjan solve the bi-connected components of undirected graphs; | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges, | ||
* strongly connected components (SCCs), and cycles in a graph. | ||
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the | ||
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the | ||
* number of connected components in the graph. | ||
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges | ||
* (edgeMap whose removal would increase the number of connected components in the graph). | ||
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the | ||
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the | ||
* SCCs will not be calculated or returned. | ||
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If | ||
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values | ||
* are arrays of vertexMap that form cycles within the SCCs. | ||
* @returns The function `tarjan` returns an object with the following properties: | ||
*/ | ||
tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) { | ||
// !! in undirected graph we will not let child visit parent when dfs | ||
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2) | ||
// !! bridge: low(child) > dfn(cur) | ||
const defaultConfig = false; | ||
if (needCutVertexes === undefined) | ||
needCutVertexes = defaultConfig; | ||
if (needBridges === undefined) | ||
needBridges = defaultConfig; | ||
if (needSCCs === undefined) | ||
needSCCs = defaultConfig; | ||
if (needCycles === undefined) | ||
needCycles = defaultConfig; | ||
const dfnMap = new Map(); | ||
const lowMap = new Map(); | ||
const vertexMap = this._vertexMap; | ||
vertexMap.forEach(v => { | ||
dfnMap.set(v, -1); | ||
lowMap.set(v, Infinity); | ||
}); | ||
const [root] = vertexMap.values(); | ||
const cutVertexes = []; | ||
const bridges = []; | ||
let dfn = 0; | ||
const dfs = (cur, parent) => { | ||
dfn++; | ||
dfnMap.set(cur, dfn); | ||
lowMap.set(cur, dfn); | ||
const neighbors = this.getNeighbors(cur); | ||
let childCount = 0; // child in dfs tree not child in graph | ||
for (const neighbor of neighbors) { | ||
if (neighbor !== parent) { | ||
if (dfnMap.get(neighbor) === -1) { | ||
childCount++; | ||
dfs(neighbor, cur); | ||
} | ||
const childLow = lowMap.get(neighbor); | ||
const curLow = lowMap.get(cur); | ||
// TODO after no-non-undefined-assertion not ensure the logic | ||
if (curLow !== undefined && childLow !== undefined) { | ||
lowMap.set(cur, Math.min(curLow, childLow)); | ||
} | ||
const curFromMap = dfnMap.get(cur); | ||
if (childLow !== undefined && curFromMap !== undefined) { | ||
if (needCutVertexes) { | ||
if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) { | ||
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) { | ||
cutVertexes.push(cur); | ||
} | ||
} | ||
if (needBridges) { | ||
if (childLow > curFromMap) { | ||
const edgeCurToNeighbor = this.getEdge(cur, neighbor); | ||
if (edgeCurToNeighbor) { | ||
bridges.push(edgeCurToNeighbor); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
dfs(root, undefined); | ||
let SCCs = new Map(); | ||
const getSCCs = () => { | ||
const SCCs = new Map(); | ||
lowMap.forEach((low, vertex) => { | ||
var _a; | ||
if (!SCCs.has(low)) { | ||
SCCs.set(low, [vertex]); | ||
} | ||
else { | ||
(_a = SCCs.get(low)) === null || _a === void 0 ? void 0 : _a.push(vertex); | ||
} | ||
}); | ||
return SCCs; | ||
}; | ||
if (needSCCs) { | ||
SCCs = getSCCs(); | ||
} | ||
const cycles = new Map(); | ||
if (needCycles) { | ||
const visitedMap = new Map(); | ||
const stack = []; | ||
const findCyclesDFS = (cur, parent) => { | ||
visitedMap.set(cur, true); | ||
stack.push(cur); | ||
const neighbors = this.getNeighbors(cur); | ||
for (const neighbor of neighbors) { | ||
if (!visitedMap.get(neighbor)) { | ||
findCyclesDFS(neighbor, cur); | ||
} | ||
else if (stack.includes(neighbor) && neighbor !== parent) { | ||
const cycleStartIndex = stack.indexOf(neighbor); | ||
const cycle = stack.slice(cycleStartIndex); | ||
const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity)); | ||
cycles.set(cycleLow, cycle); | ||
} | ||
} | ||
stack.pop(); | ||
}; | ||
vertexMap.forEach(v => { | ||
if (!visitedMap.get(v)) { | ||
findCyclesDFS(v, undefined); | ||
} | ||
}); | ||
} | ||
return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles }; | ||
} | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
*/ | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
* | ||
* The function returns a map that associates each vertex object with its corresponding depth-first | ||
* number. | ||
* @returns A Map object with keys of type VO and values of type number. | ||
*/ | ||
getDFNMap() { | ||
return this.tarjan(false, false, false, false).dfnMap; | ||
} | ||
/** | ||
* The function returns a Map object that contains the low values of each vertex in a Tarjan | ||
* algorithm. | ||
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of | ||
* type `number`. | ||
*/ | ||
getLowMap() { | ||
return this.tarjan(false, false, false, false).lowMap; | ||
} | ||
/** | ||
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm. | ||
* @returns an array of VO objects, specifically the cut vertexes. | ||
*/ | ||
getCutVertexes() { | ||
return this.tarjan(true, false, false, false).cutVertexes; | ||
} | ||
/** | ||
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan | ||
* algorithm. | ||
* @returns a map where the keys are numbers and the values are arrays of VO objects. | ||
*/ | ||
getSCCs() { | ||
return this.tarjan(false, false, true, false).SCCs; | ||
} | ||
/** | ||
* The function "getBridges" returns an array of bridges using the Tarjan algorithm. | ||
* @returns the bridges found using the Tarjan algorithm. | ||
*/ | ||
getBridges() { | ||
return this.tarjan(false, true, false, false).bridges; | ||
} | ||
/** | ||
* O(V+E+C) | ||
@@ -1015,0 +826,0 @@ * O(V+C) |
@@ -339,2 +339,13 @@ /** | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the vertex map, in-edge map, and out-edge map. | ||
*/ | ||
clear(): void; | ||
/** | ||
* The clone function creates a new DirectedGraph object with the same vertices and edges as the original. | ||
@@ -346,2 +357,50 @@ * | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
*/ | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
* | ||
* The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a | ||
* graph. | ||
* @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and | ||
* `SCCs`. | ||
*/ | ||
tarjan(): { | ||
dfnMap: Map<VO, number>; | ||
lowMap: Map<VO, number>; | ||
SCCs: Map<number, VO[]>; | ||
}; | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
*/ | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
* | ||
* The function returns a map that associates each vertex object with its corresponding depth-first | ||
* number. | ||
* @returns A Map object with keys of type VO and values of type number. | ||
*/ | ||
getDFNMap(): Map<VO, number>; | ||
/** | ||
* The function returns a Map object that contains the low values of each vertex in a Tarjan | ||
* algorithm. | ||
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of | ||
* type `number`. | ||
*/ | ||
getLowMap(): Map<VO, number>; | ||
/** | ||
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan | ||
* algorithm. | ||
* @returns a map where the keys are numbers and the values are arrays of VO objects. | ||
*/ | ||
getSCCs(): Map<number, VO[]>; | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -348,0 +407,0 @@ * Space Complexity: O(1) |
@@ -545,2 +545,17 @@ "use strict"; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the vertex map, in-edge map, and out-edge map. | ||
*/ | ||
clear() { | ||
this._vertexMap = new Map(); | ||
this._inEdgeMap = new Map(); | ||
this._outEdgeMap = new Map(); | ||
} | ||
/** | ||
* The clone function creates a new DirectedGraph object with the same vertices and edges as the original. | ||
@@ -558,2 +573,92 @@ * | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
*/ | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
* | ||
* The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a | ||
* graph. | ||
* @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and | ||
* `SCCs`. | ||
*/ | ||
tarjan() { | ||
const dfnMap = new Map(); | ||
const lowMap = new Map(); | ||
const SCCs = new Map(); | ||
let time = 0; | ||
const stack = []; | ||
const inStack = new Set(); | ||
const dfs = (vertex) => { | ||
dfnMap.set(vertex, time); | ||
lowMap.set(vertex, time); | ||
time++; | ||
stack.push(vertex); | ||
inStack.add(vertex); | ||
const neighbors = this.getNeighbors(vertex); | ||
for (const neighbor of neighbors) { | ||
if (!dfnMap.has(neighbor)) { | ||
dfs(neighbor); | ||
lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor))); | ||
} | ||
else if (inStack.has(neighbor)) { | ||
lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor))); | ||
} | ||
} | ||
if (dfnMap.get(vertex) === lowMap.get(vertex)) { | ||
const SCC = []; | ||
let poppedVertex; | ||
do { | ||
poppedVertex = stack.pop(); | ||
inStack.delete(poppedVertex); | ||
SCC.push(poppedVertex); | ||
} while (poppedVertex !== vertex); | ||
SCCs.set(SCCs.size, SCC); | ||
} | ||
}; | ||
for (const vertex of this.vertexMap.values()) { | ||
if (!dfnMap.has(vertex)) { | ||
dfs(vertex); | ||
} | ||
} | ||
return { dfnMap, lowMap, SCCs }; | ||
} | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
*/ | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
* | ||
* The function returns a map that associates each vertex object with its corresponding depth-first | ||
* number. | ||
* @returns A Map object with keys of type VO and values of type number. | ||
*/ | ||
getDFNMap() { | ||
return this.tarjan().dfnMap; | ||
} | ||
/** | ||
* The function returns a Map object that contains the low values of each vertex in a Tarjan | ||
* algorithm. | ||
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of | ||
* type `number`. | ||
*/ | ||
getLowMap() { | ||
return this.tarjan().lowMap; | ||
} | ||
/** | ||
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan | ||
* algorithm. | ||
* @returns a map where the keys are numbers and the values are arrays of VO objects. | ||
*/ | ||
getSCCs() { | ||
return this.tarjan().SCCs; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -560,0 +665,0 @@ * Space Complexity: O(1) |
@@ -22,3 +22,3 @@ /** | ||
export declare class UndirectedEdge<E = number> extends AbstractEdge<E> { | ||
vertexMap: [VertexKey, VertexKey]; | ||
endpoints: [VertexKey, VertexKey]; | ||
/** | ||
@@ -73,3 +73,3 @@ * The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional | ||
* | ||
* The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists. | ||
* The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists. | ||
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex | ||
@@ -94,3 +94,3 @@ * object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex). | ||
* (VertexKey). It represents the second vertex of the edge that needs to be removed. | ||
* @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist. | ||
* @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist. | ||
*/ | ||
@@ -106,3 +106,3 @@ deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined; | ||
* | ||
* The function `deleteEdge` deletes an edge between two vertexMap in a graph. | ||
* The function `deleteEdge` deletes an edge between two endpoints in a graph. | ||
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be | ||
@@ -180,3 +180,3 @@ * either an edge object or a vertex key. | ||
* | ||
* The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID. | ||
* The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID. | ||
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID | ||
@@ -195,6 +195,6 @@ * (`VertexKey`). | ||
* | ||
* The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise | ||
* The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise | ||
* it returns undefined. | ||
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph. | ||
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the | ||
* @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the | ||
* graph. If the edge does not exist, it returns `undefined`. | ||
@@ -209,2 +209,13 @@ */ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the vertex and edge maps to empty maps. | ||
*/ | ||
clear(): void; | ||
/** | ||
* The clone function creates a new UndirectedGraph object and copies the | ||
@@ -224,2 +235,44 @@ * vertexMap and edgeMap from this graph to the new one. This is done by | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time | ||
* | ||
* The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a | ||
* graph. | ||
* @returns The function `tarjan()` returns an object with the following properties: | ||
*/ | ||
tarjan(): { | ||
dfnMap: Map<VO, number>; | ||
lowMap: Map<VO, number>; | ||
bridges: EO[]; | ||
cutVertices: VO[]; | ||
}; | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time | ||
*/ | ||
/** | ||
* The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm. | ||
* @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm. | ||
*/ | ||
getBridges(): EO[]; | ||
/** | ||
* The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm. | ||
* @returns the cut vertices found using the Tarjan's algorithm. | ||
*/ | ||
getCutVertices(): VO[]; | ||
/** | ||
* The function returns the dfnMap property of the result of the tarjan() function. | ||
* @returns the `dfnMap` property of the result of calling the `tarjan()` function. | ||
*/ | ||
getDFNMap(): Map<VO, number>; | ||
/** | ||
* The function returns the lowMap property of the result of the tarjan() function. | ||
* @returns the lowMap property of the result of calling the tarjan() function. | ||
*/ | ||
getLowMap(): Map<VO, number>; | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -226,0 +279,0 @@ * Space Complexity: O(1) |
@@ -32,3 +32,3 @@ "use strict"; | ||
super(weight, value); | ||
this.vertexMap = [v1, v2]; | ||
this.endpoints = [v1, v2]; | ||
} | ||
@@ -84,3 +84,3 @@ } | ||
* | ||
* The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists. | ||
* The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists. | ||
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex | ||
@@ -99,3 +99,3 @@ * object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex). | ||
if (vertex1 && vertex2) { | ||
edgeMap = (_a = this._edgeMap.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(e => e.vertexMap.includes(vertex2.key)); | ||
edgeMap = (_a = this._edgeMap.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(e => e.endpoints.includes(vertex2.key)); | ||
} | ||
@@ -117,3 +117,3 @@ } | ||
* (VertexKey). It represents the second vertex of the edge that needs to be removed. | ||
* @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist. | ||
* @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist. | ||
*/ | ||
@@ -129,7 +129,7 @@ deleteEdgeBetween(v1, v2) { | ||
if (v1Edges) { | ||
removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.vertexMap.includes(vertex2.key))[0] || undefined; | ||
removed = (0, utils_1.arrayRemove)(v1Edges, (e) => e.endpoints.includes(vertex2.key))[0] || undefined; | ||
} | ||
const v2Edges = this._edgeMap.get(vertex2); | ||
if (v2Edges) { | ||
(0, utils_1.arrayRemove)(v2Edges, (e) => e.vertexMap.includes(vertex1.key)); | ||
(0, utils_1.arrayRemove)(v2Edges, (e) => e.endpoints.includes(vertex1.key)); | ||
} | ||
@@ -146,3 +146,3 @@ return removed; | ||
* | ||
* The function `deleteEdge` deletes an edge between two vertexMap in a graph. | ||
* The function `deleteEdge` deletes an edge between two endpoints in a graph. | ||
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be | ||
@@ -168,4 +168,4 @@ * either an edge object or a vertex key. | ||
else { | ||
oneSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[0]); | ||
otherSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[1]); | ||
oneSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[0]); | ||
otherSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[1]); | ||
} | ||
@@ -209,3 +209,3 @@ if (oneSide && otherSide) { | ||
const restEdges = neighborEdges.filter(edge => { | ||
return !edge.vertexMap.includes(vertexKey); | ||
return !edge.endpoints.includes(vertexKey); | ||
}); | ||
@@ -293,3 +293,3 @@ this._edgeMap.set(neighbor, restEdges); | ||
* | ||
* The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID. | ||
* The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID. | ||
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID | ||
@@ -305,3 +305,3 @@ * (`VertexKey`). | ||
for (const edge of neighborEdges) { | ||
const neighbor = this._getVertex(edge.vertexMap.filter(e => e !== vertex.key)[0]); | ||
const neighbor = this._getVertex(edge.endpoints.filter(e => e !== vertex.key)[0]); | ||
if (neighbor) { | ||
@@ -322,14 +322,14 @@ neighbors.push(neighbor); | ||
* | ||
* The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise | ||
* The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise | ||
* it returns undefined. | ||
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph. | ||
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the | ||
* @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the | ||
* graph. If the edge does not exist, it returns `undefined`. | ||
*/ | ||
getEndsOfEdge(edge) { | ||
if (!this.hasEdge(edge.vertexMap[0], edge.vertexMap[1])) { | ||
if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) { | ||
return undefined; | ||
} | ||
const v1 = this._getVertex(edge.vertexMap[0]); | ||
const v2 = this._getVertex(edge.vertexMap[1]); | ||
const v1 = this._getVertex(edge.endpoints[0]); | ||
const v2 = this._getVertex(edge.endpoints[1]); | ||
if (v1 && v2) { | ||
@@ -350,2 +350,16 @@ return [v1, v2]; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the vertex and edge maps to empty maps. | ||
*/ | ||
clear() { | ||
this._vertexMap = new Map(); | ||
this._edgeMap = new Map(); | ||
} | ||
/** | ||
* The clone function creates a new UndirectedGraph object and copies the | ||
@@ -370,2 +384,96 @@ * vertexMap and edgeMap from this graph to the new one. This is done by | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time | ||
* | ||
* The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a | ||
* graph. | ||
* @returns The function `tarjan()` returns an object with the following properties: | ||
*/ | ||
tarjan() { | ||
const dfnMap = new Map(); | ||
const lowMap = new Map(); | ||
const bridges = []; | ||
const cutVertices = []; | ||
let time = 0; | ||
const dfs = (vertex, parent) => { | ||
dfnMap.set(vertex, time); | ||
lowMap.set(vertex, time); | ||
time++; | ||
const neighbors = this.getNeighbors(vertex); | ||
let childCount = 0; | ||
for (const neighbor of neighbors) { | ||
if (!dfnMap.has(neighbor)) { | ||
childCount++; | ||
dfs(neighbor, vertex); | ||
lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor))); | ||
if (lowMap.get(neighbor) > dfnMap.get(vertex)) { | ||
// Found a bridge | ||
const edge = this.getEdge(vertex, neighbor); | ||
if (edge) { | ||
bridges.push(edge); | ||
} | ||
} | ||
if (parent !== undefined && lowMap.get(neighbor) >= dfnMap.get(vertex)) { | ||
// Found an articulation point | ||
cutVertices.push(vertex); | ||
} | ||
} | ||
else if (neighbor !== parent) { | ||
lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor))); | ||
} | ||
} | ||
if (parent === undefined && childCount > 1) { | ||
// Special case for root in DFS tree | ||
cutVertices.push(vertex); | ||
} | ||
}; | ||
for (const vertex of this.vertexMap.values()) { | ||
if (!dfnMap.has(vertex)) { | ||
dfs(vertex, undefined); | ||
} | ||
} | ||
return { | ||
dfnMap, | ||
lowMap, | ||
bridges, | ||
cutVertices | ||
}; | ||
} | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time | ||
*/ | ||
/** | ||
* The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm. | ||
* @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm. | ||
*/ | ||
getBridges() { | ||
return this.tarjan().bridges; | ||
} | ||
/** | ||
* The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm. | ||
* @returns the cut vertices found using the Tarjan's algorithm. | ||
*/ | ||
getCutVertices() { | ||
return this.tarjan().cutVertices; | ||
} | ||
/** | ||
* The function returns the dfnMap property of the result of the tarjan() function. | ||
* @returns the `dfnMap` property of the result of calling the `tarjan()` function. | ||
*/ | ||
getDFNMap() { | ||
return this.tarjan().dfnMap; | ||
} | ||
/** | ||
* The function returns the lowMap property of the result of the tarjan() function. | ||
* @returns the lowMap property of the result of calling the tarjan() function. | ||
*/ | ||
getLowMap() { | ||
return this.tarjan().lowMap; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -379,3 +487,3 @@ * Space Complexity: O(1) | ||
_addEdge(edge) { | ||
for (const end of edge.vertexMap) { | ||
for (const end of edge.endpoints) { | ||
const endVertex = this._getVertex(end); | ||
@@ -382,0 +490,0 @@ if (endVertex === undefined) |
@@ -17,6 +17,2 @@ /** | ||
export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> { | ||
protected _store: { | ||
[key: string]: HashMapStoreItem<K, V>; | ||
}; | ||
protected _objMap: Map<object, V>; | ||
/** | ||
@@ -30,2 +26,20 @@ * The constructor function initializes a HashMap object with an optional initial collection and | ||
constructor(rawCollection?: Iterable<R | [K, V]>, options?: HashMapOptions<K, V, R>); | ||
protected _store: { | ||
[key: string]: HashMapStoreItem<K, V>; | ||
}; | ||
/** | ||
* The function returns the store object, which is a dictionary of HashMapStoreItem objects. | ||
* @returns The store property is being returned. It is a dictionary-like object with string keys and | ||
* values of type HashMapStoreItem<K, V>. | ||
*/ | ||
get store(): { | ||
[p: string]: HashMapStoreItem<K, V>; | ||
}; | ||
protected _objMap: Map<object, V>; | ||
/** | ||
* The function returns the object map. | ||
* @returns The `objMap` property is being returned, which is a `Map` object with keys of type | ||
* `object` and values of type `V`. | ||
*/ | ||
get objMap(): Map<object, V>; | ||
protected _toEntryFn: (rawElement: R) => [K, V]; | ||
@@ -37,3 +51,10 @@ /** | ||
get toEntryFn(): (rawElement: R) => [K, V]; | ||
protected _size: number; | ||
/** | ||
* The function returns the size of an object. | ||
* @returns The size of the object, which is a number. | ||
*/ | ||
get size(): number; | ||
protected _hashFn: (key: K) => string; | ||
/** | ||
* The hasFn function is a function that takes in an item and returns a boolean | ||
@@ -44,10 +65,4 @@ * indicating whether the item is contained within the hash table. | ||
*/ | ||
get hasFn(): (key: K) => string; | ||
protected _size: number; | ||
get hashFn(): (key: K) => string; | ||
/** | ||
* The function returns the size of an object. | ||
* @returns The size of the object, which is a number. | ||
*/ | ||
get size(): number; | ||
/** | ||
* The function checks if a given element is an array with exactly two elements. | ||
@@ -112,2 +127,6 @@ * @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* The clone function creates a new HashMap with the same key-value pairs as | ||
@@ -142,6 +161,2 @@ * this one. The clone function is useful for creating a copy of an existing | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -175,4 +190,15 @@ * The `filter` function creates a new HashMap containing key-value pairs from the original HashMap | ||
protected _getIterator(): IterableIterator<[K, V]>; | ||
protected _hashFn: (key: K) => string; | ||
/** | ||
* The function checks if a given key is an object or a function. | ||
* @param {any} key - The parameter "key" can be of any type. | ||
* @returns a boolean value. | ||
*/ | ||
protected _isObjKey(key: any): key is object | ((...args: any[]) => any); | ||
/** | ||
* The function `_getNoObjKey` takes a key and returns a string representation of the key, handling | ||
* different types of keys. | ||
* @param {K} key - The `key` parameter is of type `K`, which represents the type of the key being | ||
* passed to the `_getNoObjKey` function. | ||
* @returns a string value. | ||
*/ | ||
protected _getNoObjKey(key: K): string; | ||
@@ -186,6 +212,2 @@ } | ||
export declare class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> { | ||
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>; | ||
protected _objMap: 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>; | ||
@@ -202,2 +224,41 @@ /** | ||
constructor(rawCollection?: Iterable<R>, options?: LinkedHashMapOptions<K, V, R>); | ||
protected _hashFn: (key: K) => string; | ||
/** | ||
* The function returns the hash function used for generating a hash value for a given key. | ||
* @returns The hash function that takes a key of type K and returns a string. | ||
*/ | ||
get hashFn(): (key: K) => string; | ||
protected _objHashFn: (key: K) => object; | ||
/** | ||
* The function returns the object hash function. | ||
* @returns The function `objHashFn` is being returned. | ||
*/ | ||
get objHashFn(): (key: K) => object; | ||
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>; | ||
/** | ||
* The function returns a record of HashMapLinkedNode objects with string keys. | ||
* @returns The method is returning a Record object, which is a TypeScript type that represents an | ||
* object with string keys and values that are HashMapLinkedNode objects with keys of type K and | ||
* values of type V or undefined. | ||
*/ | ||
get noObjMap(): Record<string, HashMapLinkedNode<K, V | undefined>>; | ||
protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>; | ||
/** | ||
* The function returns the WeakMap object used to map objects to HashMapLinkedNode instances. | ||
* @returns The `objMap` property is being returned. | ||
*/ | ||
get objMap(): WeakMap<object, HashMapLinkedNode<K, V | undefined>>; | ||
protected _head: HashMapLinkedNode<K, V | undefined>; | ||
/** | ||
* The function returns the head node of a HashMapLinkedNode. | ||
* @returns The method `getHead()` is returning a `HashMapLinkedNode` object with key type `K` and | ||
* value type `V | undefined`. | ||
*/ | ||
get head(): HashMapLinkedNode<K, V | undefined>; | ||
protected _tail: HashMapLinkedNode<K, V | undefined>; | ||
/** | ||
* The function returns the tail node of a HashMapLinkedNode. | ||
* @returns The `_tail` property of type `HashMapLinkedNode<K, V | undefined>` is being returned. | ||
*/ | ||
get tail(): HashMapLinkedNode<K, V | undefined>; | ||
protected _toEntryFn: (rawElement: R) => [K, V]; | ||
@@ -218,2 +279,6 @@ /** | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -228,2 +293,6 @@ * The function returns the key-value pair at the front of a data structure. | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -247,2 +316,6 @@ * The function returns the key-value pair at the end of a data structure. | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -277,2 +350,6 @@ * The `set` function adds a new key-value pair to a data structure, either using an object key or a | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -290,4 +367,9 @@ * The function `get` retrieves the value associated with a given key from a map, either by using the | ||
/** | ||
* Time Complexity: O(n), where n is the index. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -305,2 +387,7 @@ * The function `at` retrieves the key-value pair at a specified index in a linked list. | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -317,2 +404,7 @@ * The `delete` function removes a key-value pair from a map-like data structure. | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -328,2 +420,7 @@ * The `deleteAt` function deletes a node at a specified index in a linked list. | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -345,2 +442,7 @@ * The function checks if a data structure is empty by comparing its size to zero. | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -367,2 +469,7 @@ * The `clear` function clears all the entries in a data structure and resets its properties. | ||
* Space Complexity: O(n) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -384,2 +491,7 @@ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original | ||
* Space Complexity: O(n) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -416,4 +528,2 @@ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to | ||
put(key: K, value: V): boolean; | ||
protected _hashFn: (key: K) => string; | ||
protected _objHashFn: (key: K) => object; | ||
/** | ||
@@ -423,2 +533,8 @@ * 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. | ||
* | ||
@@ -431,2 +547,6 @@ * The above function is an iterator that yields key-value pairs from a linked list. | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -433,0 +553,0 @@ * The `_deleteNode` function removes a node from a doubly linked list and updates the head and tail |
@@ -49,2 +49,18 @@ "use strict"; | ||
/** | ||
* The function returns the store object, which is a dictionary of HashMapStoreItem objects. | ||
* @returns The store property is being returned. It is a dictionary-like object with string keys and | ||
* values of type HashMapStoreItem<K, V>. | ||
*/ | ||
get store() { | ||
return this._store; | ||
} | ||
/** | ||
* The function returns the object map. | ||
* @returns The `objMap` property is being returned, which is a `Map` object with keys of type | ||
* `object` and values of type `V`. | ||
*/ | ||
get objMap() { | ||
return this._objMap; | ||
} | ||
/** | ||
* The function returns the value of the _toEntryFn property. | ||
@@ -57,2 +73,9 @@ * @returns The function being returned is `this._toEntryFn`. | ||
/** | ||
* The function returns the size of an object. | ||
* @returns The size of the object, which is a number. | ||
*/ | ||
get size() { | ||
return this._size; | ||
} | ||
/** | ||
* The hasFn function is a function that takes in an item and returns a boolean | ||
@@ -63,13 +86,6 @@ * indicating whether the item is contained within the hash table. | ||
*/ | ||
get hasFn() { | ||
get hashFn() { | ||
return this._hashFn; | ||
} | ||
/** | ||
* The function returns the size of an object. | ||
* @returns The size of the object, which is a number. | ||
*/ | ||
get size() { | ||
return this._size; | ||
} | ||
/** | ||
* The function checks if a given element is an array with exactly two elements. | ||
@@ -110,10 +126,10 @@ * @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any | ||
if (this._isObjKey(key)) { | ||
if (!this._objMap.has(key)) { | ||
if (!this.objMap.has(key)) { | ||
this._size++; | ||
} | ||
this._objMap.set(key, value); | ||
this.objMap.set(key, value); | ||
} | ||
else { | ||
const strKey = this._getNoObjKey(key); | ||
if (this._store[strKey] === undefined) { | ||
if (this.store[strKey] === undefined) { | ||
this._size++; | ||
@@ -160,3 +176,3 @@ } | ||
if (this._isObjKey(key)) { | ||
return this._objMap.get(key); | ||
return this.objMap.get(key); | ||
} | ||
@@ -176,7 +192,7 @@ else { | ||
if (this._isObjKey(key)) { | ||
return this._objMap.has(key); | ||
return this.objMap.has(key); | ||
} | ||
else { | ||
const strKey = this._getNoObjKey(key); | ||
return strKey in this._store; | ||
return strKey in this.store; | ||
} | ||
@@ -193,11 +209,11 @@ } | ||
if (this._isObjKey(key)) { | ||
if (this._objMap.has(key)) { | ||
if (this.objMap.has(key)) { | ||
this._size--; | ||
} | ||
return this._objMap.delete(key); | ||
return this.objMap.delete(key); | ||
} | ||
else { | ||
const strKey = this._getNoObjKey(key); | ||
if (strKey in this._store) { | ||
delete this._store[strKey]; | ||
if (strKey in this.store) { | ||
delete this.store[strKey]; | ||
this._size--; | ||
@@ -210,2 +226,6 @@ return true; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* The clone function creates a new HashMap with the same key-value pairs as | ||
@@ -218,3 +238,3 @@ * this one. The clone function is useful for creating a copy of an existing | ||
clone() { | ||
return new HashMap(this, { hashFn: this._hashFn, toEntryFn: this.toEntryFn }); | ||
return new HashMap(this, { hashFn: this.hashFn, toEntryFn: this.toEntryFn }); | ||
} | ||
@@ -250,6 +270,2 @@ /** | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -294,9 +310,14 @@ * The `filter` function creates a new HashMap containing key-value pairs from the original HashMap | ||
*_getIterator() { | ||
for (const node of Object.values(this._store)) { | ||
for (const node of Object.values(this.store)) { | ||
yield [node.key, node.value]; | ||
} | ||
for (const node of this._objMap) { | ||
for (const node of this.objMap) { | ||
yield node; | ||
} | ||
} | ||
/** | ||
* The function checks if a given key is an object or a function. | ||
* @param {any} key - The parameter "key" can be of any type. | ||
* @returns a boolean value. | ||
*/ | ||
_isObjKey(key) { | ||
@@ -306,2 +327,9 @@ const keyType = typeof key; | ||
} | ||
/** | ||
* The function `_getNoObjKey` takes a key and returns a string representation of the key, handling | ||
* different types of keys. | ||
* @param {K} key - The `key` parameter is of type `K`, which represents the type of the key being | ||
* passed to the `_getNoObjKey` function. | ||
* @returns a string value. | ||
*/ | ||
_getNoObjKey(key) { | ||
@@ -311,3 +339,3 @@ const keyType = typeof key; | ||
if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') { | ||
strKey = this._hashFn(key); | ||
strKey = this.hashFn(key); | ||
} | ||
@@ -344,2 +372,4 @@ else { | ||
super(); | ||
this._hashFn = (key) => String(key); | ||
this._objHashFn = (key) => key; | ||
this._noObjMap = {}; | ||
@@ -357,4 +387,2 @@ this._objMap = new WeakMap(); | ||
this._size = 0; | ||
this._hashFn = (key) => String(key); | ||
this._objHashFn = (key) => key; | ||
this._sentinel = {}; | ||
@@ -380,2 +408,47 @@ this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel; | ||
/** | ||
* The function returns the hash function used for generating a hash value for a given key. | ||
* @returns The hash function that takes a key of type K and returns a string. | ||
*/ | ||
get hashFn() { | ||
return this._hashFn; | ||
} | ||
/** | ||
* The function returns the object hash function. | ||
* @returns The function `objHashFn` is being returned. | ||
*/ | ||
get objHashFn() { | ||
return this._objHashFn; | ||
} | ||
/** | ||
* The function returns a record of HashMapLinkedNode objects with string keys. | ||
* @returns The method is returning a Record object, which is a TypeScript type that represents an | ||
* object with string keys and values that are HashMapLinkedNode objects with keys of type K and | ||
* values of type V or undefined. | ||
*/ | ||
get noObjMap() { | ||
return this._noObjMap; | ||
} | ||
/** | ||
* The function returns the WeakMap object used to map objects to HashMapLinkedNode instances. | ||
* @returns The `objMap` property is being returned. | ||
*/ | ||
get objMap() { | ||
return this._objMap; | ||
} | ||
/** | ||
* The function returns the head node of a HashMapLinkedNode. | ||
* @returns The method `getHead()` is returning a `HashMapLinkedNode` object with key type `K` and | ||
* value type `V | undefined`. | ||
*/ | ||
get head() { | ||
return this._head; | ||
} | ||
/** | ||
* The function returns the tail node of a HashMapLinkedNode. | ||
* @returns The `_tail` property of type `HashMapLinkedNode<K, V | undefined>` is being returned. | ||
*/ | ||
get tail() { | ||
return this._tail; | ||
} | ||
/** | ||
* The function returns the value of the _toEntryFn property. | ||
@@ -397,2 +470,6 @@ * @returns The function being returned is `this._toEntryFn`. | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -406,3 +483,3 @@ * The function returns the key-value pair at the front of a data structure. | ||
return; | ||
return [this._head.key, this._head.value]; | ||
return [this.head.key, this.head.value]; | ||
} | ||
@@ -412,2 +489,6 @@ /** | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -421,3 +502,3 @@ * The function returns the key-value pair at the end of a data structure. | ||
return; | ||
return [this._tail.key, this._tail.value]; | ||
return [this.tail.key, this.tail.value]; | ||
} | ||
@@ -428,3 +509,3 @@ /** | ||
*begin() { | ||
let node = this._head; | ||
let node = this.head; | ||
while (node !== this._sentinel) { | ||
@@ -440,3 +521,3 @@ yield [node.key, node.value]; | ||
*reverseBegin() { | ||
let node = this._tail; | ||
let node = this.tail; | ||
while (node !== this._sentinel) { | ||
@@ -450,2 +531,6 @@ yield [node.key, node.value]; | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -464,8 +549,8 @@ * The `set` function adds a new key-value pair to a data structure, either using an object key or a | ||
if ((0, utils_1.isWeakKey)(key)) { | ||
const hash = this._objHashFn(key); | ||
node = this._objMap.get(hash); | ||
const hash = this.objHashFn(key); | ||
node = this.objMap.get(hash); | ||
if (!node && isNewKey) { | ||
// Create new node | ||
node = { key: hash, value, prev: this._tail, next: this._sentinel }; | ||
this._objMap.set(hash, node); | ||
node = { key: hash, value, prev: this.tail, next: this._sentinel }; | ||
this.objMap.set(hash, node); | ||
} | ||
@@ -478,6 +563,6 @@ else if (node) { | ||
else { | ||
const hash = this._hashFn(key); | ||
node = this._noObjMap[hash]; | ||
const hash = this.hashFn(key); | ||
node = this.noObjMap[hash]; | ||
if (!node && isNewKey) { | ||
this._noObjMap[hash] = node = { key, value, prev: this._tail, next: this._sentinel }; | ||
this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel }; | ||
} | ||
@@ -496,4 +581,4 @@ else if (node) { | ||
else { | ||
this._tail.next = node; | ||
node.prev = this._tail; // Make sure that the prev of the new node points to the current tail node | ||
this.tail.next = node; | ||
node.prev = this.tail; // Make sure that the prev of the new node points to the current tail node | ||
} | ||
@@ -530,8 +615,8 @@ this._tail = node; | ||
if ((0, utils_1.isWeakKey)(key)) { | ||
const hash = this._objHashFn(key); | ||
return this._objMap.has(hash); | ||
const hash = this.objHashFn(key); | ||
return this.objMap.has(hash); | ||
} | ||
else { | ||
const hash = this._hashFn(key); | ||
return hash in this._noObjMap; | ||
const hash = this.hashFn(key); | ||
return hash in this.noObjMap; | ||
} | ||
@@ -542,2 +627,6 @@ } | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -555,9 +644,9 @@ * The function `get` retrieves the value associated with a given key from a map, either by using the | ||
if ((0, utils_1.isWeakKey)(key)) { | ||
const hash = this._objHashFn(key); | ||
const node = this._objMap.get(hash); | ||
const hash = this.objHashFn(key); | ||
const node = this.objMap.get(hash); | ||
return node ? node.value : undefined; | ||
} | ||
else { | ||
const hash = this._hashFn(key); | ||
const node = this._noObjMap[hash]; | ||
const hash = this.hashFn(key); | ||
const node = this.noObjMap[hash]; | ||
return node ? node.value : undefined; | ||
@@ -567,4 +656,9 @@ } | ||
/** | ||
* Time Complexity: O(n), where n is the index. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -580,3 +674,3 @@ * The function `at` retrieves the key-value pair at a specified index in a linked list. | ||
(0, utils_1.rangeCheck)(index, 0, this._size - 1); | ||
let node = this._head; | ||
let node = this.head; | ||
while (index--) { | ||
@@ -590,2 +684,7 @@ node = node.next; | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -601,5 +700,5 @@ * The `delete` function removes a key-value pair from a map-like data structure. | ||
if ((0, utils_1.isWeakKey)(key)) { | ||
const hash = this._objHashFn(key); | ||
const hash = this.objHashFn(key); | ||
// Get nodes from WeakMap | ||
node = this._objMap.get(hash); | ||
node = this.objMap.get(hash); | ||
if (!node) { | ||
@@ -609,8 +708,8 @@ return false; // If the node does not exist, return false | ||
// Remove nodes from WeakMap | ||
this._objMap.delete(hash); | ||
this.objMap.delete(hash); | ||
} | ||
else { | ||
const hash = this._hashFn(key); | ||
const hash = this.hashFn(key); | ||
// Get nodes from noObjMap | ||
node = this._noObjMap[hash]; | ||
node = this.noObjMap[hash]; | ||
if (!node) { | ||
@@ -620,3 +719,3 @@ return false; // If the node does not exist, return false | ||
// Remove nodes from orgMap | ||
delete this._noObjMap[hash]; | ||
delete this.noObjMap[hash]; | ||
} | ||
@@ -630,2 +729,7 @@ // Remove node from doubly linked list | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -639,3 +743,3 @@ * The `deleteAt` function deletes a node at a specified index in a linked list. | ||
(0, utils_1.rangeCheck)(index, 0, this._size - 1); | ||
let node = this._head; | ||
let node = this.head; | ||
while (index--) { | ||
@@ -649,2 +753,7 @@ node = node.next; | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -670,2 +779,7 @@ * The function checks if a data structure is empty by comparing its size to zero. | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -693,3 +807,3 @@ * The `clear` function clears all the entries in a data structure and resets its properties. | ||
clone() { | ||
const cloned = new LinkedHashMap([], { hashFn: this._hashFn, objHashFn: this._objHashFn }); | ||
const cloned = new LinkedHashMap([], { hashFn: this.hashFn, objHashFn: this.objHashFn }); | ||
for (const entry of this) { | ||
@@ -704,2 +818,7 @@ const [key, value] = entry; | ||
* Space Complexity: O(n) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -731,2 +850,7 @@ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original | ||
* Space Complexity: O(n) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -778,2 +902,8 @@ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to | ||
* 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. | ||
* | ||
@@ -783,3 +913,3 @@ * The above function is an iterator that yields key-value pairs from a linked list. | ||
*_getIterator() { | ||
let node = this._head; | ||
let node = this.head; | ||
while (node !== this._sentinel) { | ||
@@ -793,2 +923,6 @@ yield [node.key, node.value]; | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -805,6 +939,6 @@ * The `_deleteNode` function removes a node from a doubly linked list and updates the head and tail | ||
next.prev = prev; | ||
if (node === this._head) { | ||
if (node === this.head) { | ||
this._head = next; | ||
} | ||
if (node === this._tail) { | ||
if (node === this.tail) { | ||
this._tail = prev; | ||
@@ -811,0 +945,0 @@ } |
@@ -92,2 +92,6 @@ /** | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -108,7 +112,7 @@ * Peek at the top element of the heap without removing it. | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the elements array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the elements array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -121,7 +125,7 @@ * | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
@@ -135,7 +139,8 @@ * | ||
/** | ||
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* The worst-case O(n) This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. | ||
*/ | ||
/** | ||
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
@@ -152,8 +157,9 @@ * | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Space Complexity: O(h), where h is the height of the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* where log n is the height of the heap. | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Space Complexity: O(h), where h is the height of the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -202,8 +208,8 @@ * Depth-first search (DFS) method, different traversal orders can be selected。 | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -264,3 +270,3 @@ * Fix the entire heap to maintain heap properties. | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -279,2 +285,6 @@ */ | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -408,7 +418,7 @@ * Sinking operation to maintain heap properties after removing the top element. | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -421,7 +431,7 @@ * | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -428,0 +438,0 @@ * |
@@ -54,3 +54,2 @@ "use strict"; | ||
} | ||
// this.fix(); | ||
} | ||
@@ -137,2 +136,6 @@ } | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -159,7 +162,7 @@ * Peek at the top element of the heap without removing it. | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the elements array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the elements array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -175,7 +178,7 @@ * | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
@@ -191,7 +194,8 @@ * | ||
/** | ||
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* The worst-case O(n) This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. | ||
*/ | ||
/** | ||
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
@@ -224,8 +228,9 @@ * | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Space Complexity: O(h), where h is the height of the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* where log n is the height of the heap. | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Space Complexity: O(h), where h is the height of the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -314,8 +319,8 @@ * Depth-first search (DFS) method, different traversal orders can be selected。 | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -403,3 +408,3 @@ * Fix the entire heap to maintain heap properties. | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -430,2 +435,6 @@ */ | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -631,7 +640,7 @@ * Sinking operation to maintain heap properties after removing the top element. | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -646,7 +655,7 @@ * | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -653,0 +662,0 @@ * |
@@ -11,5 +11,2 @@ /** | ||
export declare class DoublyLinkedListNode<E = any> { | ||
value: E; | ||
next: DoublyLinkedListNode<E> | undefined; | ||
prev: DoublyLinkedListNode<E> | undefined; | ||
/** | ||
@@ -21,2 +18,41 @@ * The constructor function initializes the value, next, and previous properties of an object. | ||
constructor(value: E); | ||
protected _value: E; | ||
/** | ||
* The function returns the value of a protected variable. | ||
* @returns The value of the variable `_value` is being returned. | ||
*/ | ||
get value(): E; | ||
/** | ||
* The above function sets the value of a variable. | ||
* @param {E} value - The parameter "value" is of type E, which means it can be any type. | ||
*/ | ||
set value(value: E); | ||
protected _next: DoublyLinkedListNode<E> | undefined; | ||
/** | ||
* The "next" function returns the next node in a doubly linked list. | ||
* @returns The `next` property is being returned. It can be either a `DoublyLinkedListNode<E>` | ||
* object or `undefined`. | ||
*/ | ||
get next(): DoublyLinkedListNode<E> | undefined; | ||
/** | ||
* The "next" property of a DoublyLinkedListNode is set to the provided value. | ||
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type | ||
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a | ||
* `DoublyLinkedListNode` object or `undefined` as its value. | ||
*/ | ||
set next(value: DoublyLinkedListNode<E> | undefined); | ||
protected _prev: DoublyLinkedListNode<E> | undefined; | ||
/** | ||
* The `prev` function returns the previous node in a doubly linked list. | ||
* @returns The `prev` property of the `DoublyLinkedListNode` class is being returned. It can either | ||
* be a `DoublyLinkedListNode` object or `undefined`. | ||
*/ | ||
get prev(): DoublyLinkedListNode<E> | undefined; | ||
/** | ||
* The function sets the previous node of a doubly linked list node. | ||
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type | ||
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a | ||
* `DoublyLinkedListNode` object or `undefined` as its value. | ||
*/ | ||
set prev(value: DoublyLinkedListNode<E> | undefined); | ||
} | ||
@@ -57,8 +93,8 @@ /** | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* where n is the number of elements in the linked list. | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -75,3 +111,3 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -84,7 +120,7 @@ * | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the size of the input array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -124,3 +160,3 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -138,3 +174,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -215,3 +251,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
@@ -221,3 +257,3 @@ * where n is the number of elements in the linked list. | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
@@ -236,7 +272,7 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
@@ -265,4 +301,8 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -281,2 +321,5 @@ * The `delete` function removes a node from a doubly linked list based on either the node itself or its value. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if a variable has a size greater than zero and returns a boolean value. | ||
@@ -291,2 +334,5 @@ * @returns A boolean value is being returned. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively. | ||
@@ -312,3 +358,3 @@ */ | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
@@ -329,3 +375,3 @@ /** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
@@ -378,4 +424,4 @@ /** | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
@@ -401,4 +447,4 @@ /** | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
@@ -449,3 +495,3 @@ /** | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -463,3 +509,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -466,0 +512,0 @@ */ |
@@ -12,6 +12,54 @@ "use strict"; | ||
constructor(value) { | ||
this.value = value; | ||
this.next = undefined; | ||
this.prev = undefined; | ||
this._value = value; | ||
this._next = undefined; | ||
this._prev = undefined; | ||
} | ||
/** | ||
* The function returns the value of a protected variable. | ||
* @returns The value of the variable `_value` is being returned. | ||
*/ | ||
get value() { | ||
return this._value; | ||
} | ||
/** | ||
* The above function sets the value of a variable. | ||
* @param {E} value - The parameter "value" is of type E, which means it can be any type. | ||
*/ | ||
set value(value) { | ||
this._value = value; | ||
} | ||
/** | ||
* The "next" function returns the next node in a doubly linked list. | ||
* @returns The `next` property is being returned. It can be either a `DoublyLinkedListNode<E>` | ||
* object or `undefined`. | ||
*/ | ||
get next() { | ||
return this._next; | ||
} | ||
/** | ||
* The "next" property of a DoublyLinkedListNode is set to the provided value. | ||
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type | ||
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a | ||
* `DoublyLinkedListNode` object or `undefined` as its value. | ||
*/ | ||
set next(value) { | ||
this._next = value; | ||
} | ||
/** | ||
* The `prev` function returns the previous node in a doubly linked list. | ||
* @returns The `prev` property of the `DoublyLinkedListNode` class is being returned. It can either | ||
* be a `DoublyLinkedListNode` object or `undefined`. | ||
*/ | ||
get prev() { | ||
return this._prev; | ||
} | ||
/** | ||
* The function sets the previous node of a doubly linked list node. | ||
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type | ||
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a | ||
* `DoublyLinkedListNode` object or `undefined` as its value. | ||
*/ | ||
set prev(value) { | ||
this._prev = value; | ||
} | ||
} | ||
@@ -66,8 +114,8 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* where n is the number of elements in the linked list. | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -87,3 +135,3 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -99,7 +147,7 @@ * | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the size of the input array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -168,3 +216,3 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -196,3 +244,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -331,3 +379,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
@@ -337,3 +385,3 @@ * where n is the number of elements in the linked list. | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
@@ -375,7 +423,7 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
@@ -445,4 +493,8 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -486,2 +538,5 @@ * The `delete` function removes a node from a doubly linked list based on either the node itself or its value. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if a variable has a size greater than zero and returns a boolean value. | ||
@@ -498,2 +553,5 @@ * @returns A boolean value is being returned. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively. | ||
@@ -534,3 +592,3 @@ */ | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
@@ -560,3 +618,3 @@ /** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
@@ -636,4 +694,4 @@ /** | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
@@ -669,4 +727,4 @@ /** | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
@@ -729,3 +787,3 @@ /** | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -745,3 +803,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -748,0 +806,0 @@ */ |
@@ -11,4 +11,2 @@ /** | ||
export declare class SinglyLinkedListNode<E = any> { | ||
value: E; | ||
next: SinglyLinkedListNode<E> | undefined; | ||
/** | ||
@@ -20,2 +18,27 @@ * The constructor function initializes an instance of a class with a given value and sets the next property to undefined. | ||
constructor(value: E); | ||
protected _value: E; | ||
/** | ||
* The function returns the value of a protected variable. | ||
* @returns The value of the variable `_value` is being returned. | ||
*/ | ||
get value(): E; | ||
/** | ||
* The above function sets the value of a variable. | ||
* @param {E} value - The parameter "value" is of type E, which means it can be any type. | ||
*/ | ||
set value(value: E); | ||
protected _next: SinglyLinkedListNode<E> | undefined; | ||
/** | ||
* The `next` function returns the next node in a singly linked list. | ||
* @returns The `next` property is being returned. It can be either a `SinglyLinkedListNode<E>` | ||
* object or `undefined`. | ||
*/ | ||
get next(): SinglyLinkedListNode<E> | undefined; | ||
/** | ||
* The "next" property of a SinglyLinkedListNode is set to the provided value. | ||
* @param {SinglyLinkedListNode<E> | undefined} value - The `value` parameter is of type | ||
* `SinglyLinkedListNode<E> | undefined`. This means that it can accept either a | ||
* `SinglyLinkedListNode` object or `undefined` as its value. | ||
*/ | ||
set next(value: SinglyLinkedListNode<E> | undefined); | ||
} | ||
@@ -396,3 +419,3 @@ export declare class SinglyLinkedList<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) | ||
@@ -399,0 +422,0 @@ */ |
@@ -12,5 +12,36 @@ "use strict"; | ||
constructor(value) { | ||
this.value = value; | ||
this.next = undefined; | ||
this._value = value; | ||
this._next = undefined; | ||
} | ||
/** | ||
* The function returns the value of a protected variable. | ||
* @returns The value of the variable `_value` is being returned. | ||
*/ | ||
get value() { | ||
return this._value; | ||
} | ||
/** | ||
* The above function sets the value of a variable. | ||
* @param {E} value - The parameter "value" is of type E, which means it can be any type. | ||
*/ | ||
set value(value) { | ||
this._value = value; | ||
} | ||
/** | ||
* The `next` function returns the next node in a singly linked list. | ||
* @returns The `next` property is being returned. It can be either a `SinglyLinkedListNode<E>` | ||
* object or `undefined`. | ||
*/ | ||
get next() { | ||
return this._next; | ||
} | ||
/** | ||
* The "next" property of a SinglyLinkedListNode is set to the provided value. | ||
* @param {SinglyLinkedListNode<E> | undefined} value - The `value` parameter is of type | ||
* `SinglyLinkedListNode<E> | undefined`. This means that it can accept either a | ||
* `SinglyLinkedListNode` object or `undefined` as its value. | ||
*/ | ||
set next(value) { | ||
this._next = value; | ||
} | ||
} | ||
@@ -660,3 +691,3 @@ exports.SinglyLinkedListNode = SinglyLinkedListNode; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -663,0 +694,0 @@ */ |
@@ -33,3 +33,3 @@ /** | ||
/** | ||
* The function returns the value of the private variable _level. | ||
* The function returns the value of the protected variable _level. | ||
* @returns The level property of the object. | ||
@@ -47,3 +47,3 @@ */ | ||
* The function returns the probability value. | ||
* @returns The probability value stored in the private variable `_probability` is being returned. | ||
* @returns The probability value stored in the protected variable `_probability` is being returned. | ||
*/ | ||
@@ -50,0 +50,0 @@ get probability(): number; |
@@ -46,3 +46,3 @@ "use strict"; | ||
/** | ||
* The function returns the value of the private variable _level. | ||
* The function returns the value of the protected variable _level. | ||
* @returns The level property of the object. | ||
@@ -62,3 +62,3 @@ */ | ||
* The function returns the probability value. | ||
* @returns The probability value stored in the private variable `_probability` is being returned. | ||
* @returns The probability value stored in the protected variable `_probability` is being returned. | ||
*/ | ||
@@ -65,0 +65,0 @@ get probability() { |
@@ -26,3 +26,3 @@ /** | ||
/** | ||
* The function returns the value of the private variable _cols. | ||
* The function returns the value of the protected variable _cols. | ||
* @returns The number of columns. | ||
@@ -29,0 +29,0 @@ */ |
@@ -55,3 +55,3 @@ "use strict"; | ||
/** | ||
* The function returns the value of the private variable _cols. | ||
* The function returns the value of the protected variable _cols. | ||
* @returns The number of columns. | ||
@@ -58,0 +58,0 @@ */ |
@@ -11,3 +11,13 @@ /** | ||
export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> { | ||
/** | ||
* The constructor initializes a PriorityQueue with optional elements and options, including a | ||
* comparator function. | ||
* @param elements - The `elements` parameter is an iterable object that contains the initial | ||
* elements to be added to the priority queue. It is optional and defaults to an empty array if not | ||
* provided. | ||
* @param options - The `options` parameter is an object that contains additional configuration | ||
* options for the priority queue. In this case, it has a property called `comparator` which is a | ||
* function used to compare elements in the priority queue. | ||
*/ | ||
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>); | ||
} |
@@ -6,2 +6,12 @@ "use strict"; | ||
class MaxPriorityQueue extends priority_queue_1.PriorityQueue { | ||
/** | ||
* The constructor initializes a PriorityQueue with optional elements and options, including a | ||
* comparator function. | ||
* @param elements - The `elements` parameter is an iterable object that contains the initial | ||
* elements to be added to the priority queue. It is optional and defaults to an empty array if not | ||
* provided. | ||
* @param options - The `options` parameter is an object that contains additional configuration | ||
* options for the priority queue. In this case, it has a property called `comparator` which is a | ||
* function used to compare elements in the priority queue. | ||
*/ | ||
constructor(elements = [], options = { | ||
@@ -8,0 +18,0 @@ comparator: (a, b) => { |
@@ -11,3 +11,14 @@ /** | ||
export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> { | ||
/** | ||
* The constructor initializes a PriorityQueue with optional elements and options, including a | ||
* comparator function. | ||
* @param elements - The `elements` parameter is an iterable object that contains the initial | ||
* elements to be added to the priority queue. It is optional and defaults to an empty array if not | ||
* provided. | ||
* @param options - The `options` parameter is an object that contains additional configuration | ||
* options for the priority queue. In this case, it has a property called `comparator` which is a | ||
* function used to compare elements in the priority queue. The `comparator` function takes two | ||
* parameters `a` and `b`, | ||
*/ | ||
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>); | ||
} |
@@ -6,2 +6,13 @@ "use strict"; | ||
class MinPriorityQueue extends priority_queue_1.PriorityQueue { | ||
/** | ||
* The constructor initializes a PriorityQueue with optional elements and options, including a | ||
* comparator function. | ||
* @param elements - The `elements` parameter is an iterable object that contains the initial | ||
* elements to be added to the priority queue. It is optional and defaults to an empty array if not | ||
* provided. | ||
* @param options - The `options` parameter is an object that contains additional configuration | ||
* options for the priority queue. In this case, it has a property called `comparator` which is a | ||
* function used to compare elements in the priority queue. The `comparator` function takes two | ||
* parameters `a` and `b`, | ||
*/ | ||
constructor(elements = [], options = { | ||
@@ -8,0 +19,0 @@ comparator: (a, b) => { |
@@ -19,3 +19,11 @@ /** | ||
export declare class PriorityQueue<E = any> extends Heap<E> { | ||
/** | ||
* The constructor initializes a priority queue with optional elements and options. | ||
* @param elements - The `elements` parameter is an iterable object that contains the initial | ||
* elements to be added to the priority queue. It is an optional parameter and if not provided, the | ||
* priority queue will be initialized as empty. | ||
* @param [options] - The `options` parameter is an optional object that can be used to customize the | ||
* behavior of the priority queue. It can contain the following properties: | ||
*/ | ||
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>); | ||
} |
@@ -14,2 +14,10 @@ "use strict"; | ||
class PriorityQueue extends heap_1.Heap { | ||
/** | ||
* The constructor initializes a priority queue with optional elements and options. | ||
* @param elements - The `elements` parameter is an iterable object that contains the initial | ||
* elements to be added to the priority queue. It is an optional parameter and if not provided, the | ||
* priority queue will be initialized as empty. | ||
* @param [options] - The `options` parameter is an optional object that can be used to customize the | ||
* behavior of the priority queue. It can contain the following properties: | ||
*/ | ||
constructor(elements = [], options) { | ||
@@ -16,0 +24,0 @@ super(elements, options); |
@@ -18,8 +18,2 @@ /** | ||
export declare class Deque<E> extends IterableElementBase<E> { | ||
protected _bucketFirst: number; | ||
protected _firstInBucket: number; | ||
protected _bucketLast: number; | ||
protected _lastInBucket: number; | ||
protected _bucketCount: number; | ||
protected readonly _bucketSize: number; | ||
/** | ||
@@ -37,2 +31,3 @@ * The constructor initializes a Deque object with an optional iterable of elements and options. | ||
constructor(elements?: IterableWithSizeOrLength<E>, options?: DequeOptions); | ||
protected _bucketSize: number; | ||
/** | ||
@@ -44,6 +39,37 @@ * The bucketSize function returns the size of the bucket. | ||
get bucketSize(): number; | ||
protected _bucketFirst: number; | ||
/** | ||
* The function returns the value of the protected variable `_bucketFirst`. | ||
* @returns The value of the `_bucketFirst` property. | ||
*/ | ||
get bucketFirst(): number; | ||
protected _firstInBucket: number; | ||
/** | ||
* The function returns the value of the protected variable _firstInBucket. | ||
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type | ||
* `number`. | ||
*/ | ||
get firstInBucket(): number; | ||
protected _bucketLast: number; | ||
/** | ||
* The function returns the value of the protected variable `_bucketLast`. | ||
* @returns The value of the `_bucketLast` property, which is a number. | ||
*/ | ||
get bucketLast(): number; | ||
protected _lastInBucket: number; | ||
/** | ||
* The function returns the value of the protected variable _lastInBucket. | ||
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type | ||
* `number`. | ||
*/ | ||
get lastInBucket(): number; | ||
protected _bucketCount: number; | ||
/** | ||
* The function returns the number of buckets. | ||
* @returns The number of buckets. | ||
*/ | ||
get bucketCount(): number; | ||
protected _buckets: E[][]; | ||
/** | ||
* The buckets function returns the buckets property of the object. | ||
* | ||
* @return The buckets property | ||
@@ -126,7 +152,21 @@ */ | ||
/** | ||
* Time Complexity: O(1) - Removes the last element. | ||
* Space Complexity: O(1) - Operates in-place. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* 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; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear() function resets the state of the object by initializing all variables to their default | ||
@@ -211,10 +251,29 @@ * values. | ||
cut(pos: number, isCutSelf?: boolean): Deque<E>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
* | ||
* The `cutRest` function cuts the elements from a specified position in a deque and returns a new | ||
* deque with the cut elements. | ||
* @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It | ||
* is a number that indicates the index of the element in the Deque where the cut should start. | ||
* @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original | ||
* Deque should be modified or a new Deque should be created. If isCutSelf is true, the original | ||
* Deque will be modified by cutting off elements starting from the specified position. If isCutSelf | ||
* is false, a new De | ||
* @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque | ||
* (`newDeque`) depending on the value of the `isCutSelf` parameter. | ||
*/ | ||
cutRest(pos: number, isCutSelf?: boolean): Deque<E>; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
* | ||
@@ -386,4 +445,4 @@ * The `deleteAt` function removes an element at a specified position in an array-like data | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(n) - In worst case, resizing doubles the array size. | ||
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n). | ||
* Space Complexity: O(n) - Due to potential resizing. | ||
* | ||
@@ -396,8 +455,8 @@ * The addLast function adds an element to the end of an array. | ||
/** | ||
* Time Complexity: O(1) - Removes the first element. | ||
* Space Complexity: O(1) - In-place operation. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - Removes the last element. | ||
* Space Complexity: O(1) - Operates in-place. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -409,4 +468,9 @@ * The function "pollLast" removes and returns the last element of an array. | ||
/** | ||
* Time Complexity: O(1). | ||
* Space Complexity: O(n) - Due to potential resizing. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -419,4 +483,9 @@ * The "addFirst" function adds an element to the beginning of an array. | ||
/** | ||
* Time Complexity: O(1) - Removes the first element. | ||
* Space Complexity: O(1) - In-place operation. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -431,2 +500,7 @@ * The function "pollFirst" removes and returns the first element of an array. | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -433,0 +507,0 @@ * The above function is an implementation of the iterator protocol in TypeScript, allowing the |
@@ -27,2 +27,3 @@ "use strict"; | ||
super(); | ||
this._bucketSize = 1 << 12; | ||
this._bucketFirst = 0; | ||
@@ -33,3 +34,2 @@ this._firstInBucket = 0; | ||
this._bucketCount = 0; | ||
this._bucketSize = 1 << 12; | ||
this._buckets = []; | ||
@@ -75,4 +75,40 @@ this._size = 0; | ||
/** | ||
* The function returns the value of the protected variable `_bucketFirst`. | ||
* @returns The value of the `_bucketFirst` property. | ||
*/ | ||
get bucketFirst() { | ||
return this._bucketFirst; | ||
} | ||
/** | ||
* The function returns the value of the protected variable _firstInBucket. | ||
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type | ||
* `number`. | ||
*/ | ||
get firstInBucket() { | ||
return this._firstInBucket; | ||
} | ||
/** | ||
* The function returns the value of the protected variable `_bucketLast`. | ||
* @returns The value of the `_bucketLast` property, which is a number. | ||
*/ | ||
get bucketLast() { | ||
return this._bucketLast; | ||
} | ||
/** | ||
* The function returns the value of the protected variable _lastInBucket. | ||
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type | ||
* `number`. | ||
*/ | ||
get lastInBucket() { | ||
return this._lastInBucket; | ||
} | ||
/** | ||
* The function returns the number of buckets. | ||
* @returns The number of buckets. | ||
*/ | ||
get bucketCount() { | ||
return this._bucketCount; | ||
} | ||
/** | ||
* The buckets function returns the buckets property of the object. | ||
* | ||
* @return The buckets property | ||
@@ -242,5 +278,12 @@ */ | ||
/** | ||
* Time Complexity: O(1) - Removes the last element. | ||
* Space Complexity: O(1) - Operates in-place. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* 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() { | ||
@@ -250,2 +293,9 @@ return this.size === 0; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear() function resets the state of the object by initializing all variables to their default | ||
@@ -398,2 +448,21 @@ * values. | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
* | ||
* The `cutRest` function cuts the elements from a specified position in a deque and returns a new | ||
* deque with the cut elements. | ||
* @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It | ||
* is a number that indicates the index of the element in the Deque where the cut should start. | ||
* @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original | ||
* Deque should be modified or a new Deque should be created. If isCutSelf is true, the original | ||
* Deque will be modified by cutting off elements starting from the specified position. If isCutSelf | ||
* is false, a new De | ||
* @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque | ||
* (`newDeque`) depending on the value of the `isCutSelf` parameter. | ||
*/ | ||
cutRest(pos, isCutSelf = false) { | ||
@@ -421,7 +490,7 @@ if (isCutSelf) { | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
* | ||
@@ -713,4 +782,4 @@ * The `deleteAt` function removes an element at a specified position in an array-like data | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(n) - In worst case, resizing doubles the array size. | ||
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n). | ||
* Space Complexity: O(n) - Due to potential resizing. | ||
* | ||
@@ -725,8 +794,8 @@ * The addLast function adds an element to the end of an array. | ||
/** | ||
* Time Complexity: O(1) - Removes the first element. | ||
* Space Complexity: O(1) - In-place operation. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - Removes the last element. | ||
* Space Complexity: O(1) - Operates in-place. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -740,4 +809,9 @@ * The function "pollLast" removes and returns the last element of an array. | ||
/** | ||
* Time Complexity: O(1). | ||
* Space Complexity: O(n) - Due to potential resizing. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -752,4 +826,9 @@ * The "addFirst" function adds an element to the beginning of an array. | ||
/** | ||
* Time Complexity: O(1) - Removes the first element. | ||
* Space Complexity: O(1) - In-place operation. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -766,2 +845,7 @@ * The function "pollFirst" removes and returns the first element of an array. | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -768,0 +852,0 @@ * The above function is an implementation of the iterator protocol in TypeScript, allowing the |
@@ -35,3 +35,3 @@ /** | ||
* The offset function returns the offset of the current page. | ||
* @return The value of the private variable _offset | ||
* @return The value of the protected variable _offset | ||
*/ | ||
@@ -45,4 +45,8 @@ get offset(): number; | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -55,8 +59,8 @@ * The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
/** | ||
* Time Complexity: O(1) - constant time as it adds an element to the end of the array. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -69,6 +73,9 @@ * The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
/** | ||
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array. | ||
@@ -83,8 +90,8 @@ * @public | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it adds an element to the end of the array. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -97,8 +104,8 @@ * The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -123,8 +130,8 @@ * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -137,8 +144,8 @@ * The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -151,8 +158,8 @@ * The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -164,8 +171,8 @@ * The enqueue function adds a value to the end of a queue. | ||
/** | ||
* Time Complexity: O(n) - same as shift(). | ||
* Space Complexity: O(1) - same as shift(). | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - same as shift(). | ||
* Space Complexity: O(1) - same as shift(). | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -177,8 +184,8 @@ * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -189,8 +196,8 @@ * @param index | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -202,8 +209,8 @@ * The function checks if a data structure is empty by comparing its size to zero. | ||
/** | ||
* Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array. | ||
* Space Complexity: O(n) - where n is the number of elements in the queue. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array. | ||
* Space Complexity: O(n) - where n is the number of elements in the queue. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -215,2 +222,9 @@ * The toArray() function returns an array of elements from the current offset to the end of the _elements array. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the elements array and offset to their initial values. | ||
@@ -276,2 +290,8 @@ */ | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `_getIterator` returns an iterable iterator for the elements in the class. | ||
*/ | ||
protected _getIterator(): IterableIterator<E>; | ||
@@ -278,0 +298,0 @@ } |
@@ -40,3 +40,3 @@ "use strict"; | ||
* The offset function returns the offset of the current page. | ||
* @return The value of the private variable _offset | ||
* @return The value of the protected variable _offset | ||
*/ | ||
@@ -54,4 +54,8 @@ get offset() { | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -66,8 +70,8 @@ * The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
/** | ||
* Time Complexity: O(1) - constant time as it adds an element to the end of the array. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -82,6 +86,9 @@ * The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
/** | ||
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array. | ||
@@ -98,8 +105,8 @@ * @public | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it adds an element to the end of the array. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -115,8 +122,8 @@ * The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -159,8 +166,8 @@ * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -175,8 +182,8 @@ * The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -191,8 +198,8 @@ * The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -206,8 +213,8 @@ * The enqueue function adds a value to the end of a queue. | ||
/** | ||
* Time Complexity: O(n) - same as shift(). | ||
* Space Complexity: O(1) - same as shift(). | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - same as shift(). | ||
* Space Complexity: O(1) - same as shift(). | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -221,8 +228,8 @@ * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -235,8 +242,8 @@ * @param index | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -250,8 +257,8 @@ * The function checks if a data structure is empty by comparing its size to zero. | ||
/** | ||
* Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array. | ||
* Space Complexity: O(n) - where n is the number of elements in the queue. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array. | ||
* Space Complexity: O(n) - where n is the number of elements in the queue. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -265,2 +272,9 @@ * The toArray() function returns an array of elements from the current offset to the end of the _elements array. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the elements array and offset to their initial values. | ||
@@ -349,2 +363,8 @@ */ | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `_getIterator` returns an iterable iterator for the elements in the class. | ||
*/ | ||
*_getIterator() { | ||
@@ -351,0 +371,0 @@ for (const item of this.elements) { |
@@ -33,6 +33,2 @@ /** | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element. | ||
* Space Complexity: O(n), as it creates a new stack with the elements from the input array. | ||
*/ | ||
/** | ||
* The size() function returns the number of elements in an array. | ||
@@ -43,4 +39,8 @@ * @returns The size of the elements array. | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element. | ||
* Space Complexity: O(n), as it creates a new stack with the elements from the input array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -59,8 +59,8 @@ * The function "fromArray" creates a new Stack object from an array of elements. | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -72,8 +72,8 @@ * The `peek` function returns the last element of an array, or undefined if the array is empty. | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -86,8 +86,8 @@ * The push function adds an element to the stack and returns the updated stack. | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -124,2 +124,9 @@ * The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function clears the elements array. | ||
@@ -129,8 +136,8 @@ */ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it. | ||
* Space Complexity: O(n), as it creates a new stack. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it. | ||
* Space Complexity: O(n), as it creates a new stack. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -181,2 +188,9 @@ * The `clone()` function returns a new `Stack` object with the same elements as the original stack. | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* Custom iterator for the Stack class. | ||
@@ -183,0 +197,0 @@ * @returns An iterator object. |
@@ -36,6 +36,2 @@ "use strict"; | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element. | ||
* Space Complexity: O(n), as it creates a new stack with the elements from the input array. | ||
*/ | ||
/** | ||
* The size() function returns the number of elements in an array. | ||
@@ -48,4 +44,8 @@ * @returns The size of the elements array. | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element. | ||
* Space Complexity: O(n), as it creates a new stack with the elements from the input array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -68,8 +68,8 @@ * The function "fromArray" creates a new Stack object from an array of elements. | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -85,8 +85,8 @@ * The `peek` function returns the last element of an array, or undefined if the array is empty. | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -102,8 +102,8 @@ * The push function adds an element to the stack and returns the updated stack. | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -152,2 +152,9 @@ * The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function clears the elements array. | ||
@@ -159,8 +166,8 @@ */ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it. | ||
* Space Complexity: O(n), as it creates a new stack. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it. | ||
* Space Complexity: O(n), as it creates a new stack. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -231,2 +238,9 @@ * The `clone()` function returns a new `Stack` object with the same elements as the original stack. | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* Custom iterator for the Stack class. | ||
@@ -233,0 +247,0 @@ * @returns An iterator object. |
export declare class TreeNode<V = any> { | ||
key: string; | ||
value?: V | undefined; | ||
children?: TreeNode<V>[] | undefined; | ||
/** | ||
* The constructor function initializes a TreeNode object with a key, optional value, and optional | ||
* children. | ||
* @param {string} key - A string representing the key of the tree node. | ||
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the | ||
* value associated with the node. If no value is provided, it defaults to `undefined`. | ||
* @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>` | ||
* objects. It represents the child nodes of the current node. If no children are provided, the | ||
* default value is an empty array. | ||
*/ | ||
constructor(key: string, value?: V, children?: TreeNode<V>[]); | ||
protected _key: string; | ||
/** | ||
* The function returns the value of the protected variable _key. | ||
* @returns The value of the `_key` property, which is a string. | ||
*/ | ||
get key(): string; | ||
/** | ||
* The above function sets the value of a protected variable called "key". | ||
* @param {string} value - The value parameter is a string that represents the value to be assigned | ||
* to the key. | ||
*/ | ||
set key(value: string); | ||
protected _value?: V | undefined; | ||
/** | ||
* The function returns the value stored in a variable, or undefined if the variable is empty. | ||
* @returns The value of the variable `_value` is being returned. | ||
*/ | ||
get value(): V | undefined; | ||
/** | ||
* The function sets the value of a variable. | ||
* @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it | ||
* can accept a value of type "V" or it can be undefined. | ||
*/ | ||
set value(value: V | undefined); | ||
protected _children?: TreeNode<V>[] | undefined; | ||
/** | ||
* The function returns an array of TreeNode objects or undefined. | ||
* @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`, | ||
* which means it can either be an array of `TreeNode<V>` objects or `undefined`. | ||
*/ | ||
get children(): TreeNode<V>[] | undefined; | ||
/** | ||
* The function sets the value of the children property of a TreeNode object. | ||
* @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] | | ||
* undefined. This means that it can accept an array of TreeNode objects or undefined. | ||
*/ | ||
set children(value: TreeNode<V>[] | undefined); | ||
/** | ||
* The function `addChildren` adds one or more child nodes to the current node. | ||
* @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single | ||
* `TreeNode<V>` object or an array of `TreeNode<V>` objects. | ||
*/ | ||
addChildren(children: TreeNode<V> | TreeNode<V>[]): void; | ||
/** | ||
* The function `getHeight()` calculates the maximum depth of a tree structure by performing a | ||
* breadth-first search. | ||
* @returns the maximum depth or height of the tree. | ||
*/ | ||
getHeight(): number; | ||
} |
@@ -5,18 +5,84 @@ "use strict"; | ||
class TreeNode { | ||
/** | ||
* The constructor function initializes a TreeNode object with a key, optional value, and optional | ||
* children. | ||
* @param {string} key - A string representing the key of the tree node. | ||
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the | ||
* value associated with the node. If no value is provided, it defaults to `undefined`. | ||
* @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>` | ||
* objects. It represents the child nodes of the current node. If no children are provided, the | ||
* default value is an empty array. | ||
*/ | ||
constructor(key, value, children) { | ||
this.key = key; | ||
this.value = value || undefined; | ||
this.children = children || []; | ||
this._key = key; | ||
this._value = value || undefined; | ||
this._children = children || []; | ||
} | ||
/** | ||
* The function returns the value of the protected variable _key. | ||
* @returns The value of the `_key` property, which is a string. | ||
*/ | ||
get key() { | ||
return this._key; | ||
} | ||
/** | ||
* The above function sets the value of a protected variable called "key". | ||
* @param {string} value - The value parameter is a string that represents the value to be assigned | ||
* to the key. | ||
*/ | ||
set key(value) { | ||
this._key = value; | ||
} | ||
/** | ||
* The function returns the value stored in a variable, or undefined if the variable is empty. | ||
* @returns The value of the variable `_value` is being returned. | ||
*/ | ||
get value() { | ||
return this._value; | ||
} | ||
/** | ||
* The function sets the value of a variable. | ||
* @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it | ||
* can accept a value of type "V" or it can be undefined. | ||
*/ | ||
set value(value) { | ||
this._value = value; | ||
} | ||
/** | ||
* The function returns an array of TreeNode objects or undefined. | ||
* @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`, | ||
* which means it can either be an array of `TreeNode<V>` objects or `undefined`. | ||
*/ | ||
get children() { | ||
return this._children; | ||
} | ||
/** | ||
* The function sets the value of the children property of a TreeNode object. | ||
* @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] | | ||
* undefined. This means that it can accept an array of TreeNode objects or undefined. | ||
*/ | ||
set children(value) { | ||
this._children = value; | ||
} | ||
/** | ||
* The function `addChildren` adds one or more child nodes to the current node. | ||
* @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single | ||
* `TreeNode<V>` object or an array of `TreeNode<V>` objects. | ||
*/ | ||
addChildren(children) { | ||
if (!this.children) { | ||
this.children = []; | ||
if (!this._children) { | ||
this._children = []; | ||
} | ||
if (children instanceof TreeNode) { | ||
this.children.push(children); | ||
this._children.push(children); | ||
} | ||
else { | ||
this.children = this.children.concat(children); | ||
this._children = this._children.concat(children); | ||
} | ||
} | ||
/** | ||
* The function `getHeight()` calculates the maximum depth of a tree structure by performing a | ||
* breadth-first search. | ||
* @returns the maximum depth or height of the tree. | ||
*/ | ||
getHeight() { | ||
@@ -29,6 +95,6 @@ let maxDepth = 0; | ||
} | ||
const { children } = node; | ||
if (children) { | ||
for (let i = 0, len = children.length; i < len; i++) { | ||
bfs(children[i], level + 1); | ||
const { _children } = node; | ||
if (_children) { | ||
for (let i = 0, len = _children.length; i < len; i++) { | ||
bfs(_children[i], level + 1); | ||
} | ||
@@ -35,0 +101,0 @@ } |
@@ -15,6 +15,42 @@ /** | ||
export declare class TrieNode { | ||
key: string; | ||
children: Map<string, TrieNode>; | ||
isEnd: boolean; | ||
constructor(key: string); | ||
protected _key: string; | ||
/** | ||
* The function returns the value of the protected variable _key. | ||
* @returns The value of the `_key` property, which is a string. | ||
*/ | ||
get key(): string; | ||
/** | ||
* The above function sets the value of a protected variable called "key". | ||
* @param {string} value - The value parameter is a string that represents the value to be assigned | ||
* to the key. | ||
*/ | ||
set key(value: string); | ||
protected _children: Map<string, TrieNode>; | ||
/** | ||
* The function returns the children of a TrieNode as a Map. | ||
* @returns The `children` property of the TrieNode object, which is a Map containing string keys and | ||
* TrieNode values. | ||
*/ | ||
get children(): Map<string, TrieNode>; | ||
/** | ||
* The function sets the value of the `_children` property of a TrieNode object. | ||
* @param value - The value parameter is a Map object that represents the children of a TrieNode. The | ||
* keys of the map are strings, which represent the characters that are associated with each child | ||
* TrieNode. The values of the map are TrieNode objects, which represent the child nodes of the | ||
* current TrieNode. | ||
*/ | ||
set children(value: Map<string, TrieNode>); | ||
protected _isEnd: boolean; | ||
/** | ||
* The function returns a boolean value indicating whether a certain condition is met. | ||
* @returns The method is returning a boolean value, specifically the value of the variable `_isEnd`. | ||
*/ | ||
get isEnd(): boolean; | ||
/** | ||
* The function sets the value of the "_isEnd" property. | ||
* @param {boolean} value - The value parameter is a boolean value that indicates whether the current | ||
* state is the end state or not. | ||
*/ | ||
set isEnd(value: boolean); | ||
} | ||
@@ -50,5 +86,4 @@ /** | ||
/** | ||
* The caseSensitive function is a getter that returns the value of the private _caseSensitive property. | ||
* | ||
* @return The value of the _casesensitive private variable | ||
* The caseSensitive function is a getter that returns the value of the protected _caseSensitive property. | ||
* @return The value of the _caseSensitive protected variable | ||
*/ | ||
@@ -63,8 +98,8 @@ get caseSensitive(): boolean; | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being added. | ||
* Space Complexity: O(M) - Each character in the word adds a TrieNode. | ||
* Time Complexity: O(l), where l is the length of the word being added. | ||
* Space Complexity: O(l) - Each character in the word adds a TrieNode. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being added. | ||
* Space Complexity: O(M) - Each character in the word adds a TrieNode. | ||
* Time Complexity: O(l), where l is the length of the word being added. | ||
* Space Complexity: O(l) - Each character in the word adds a TrieNode. | ||
* | ||
@@ -77,7 +112,7 @@ * Add a word to the Trie structure. | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input word. | ||
* Time Complexity: O(l), where l is the length of the input word. | ||
* Space Complexity: O(1) - Constant space. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input word. | ||
* Time Complexity: O(l), where l is the length of the input word. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -91,2 +126,9 @@ * | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The isEmpty function checks if the size of the queue is 0. | ||
@@ -97,9 +139,20 @@ * @return True if the size of the queue is 0 | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being deleted. | ||
* Space Complexity: O(M) - Due to the recursive DFS approach. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being deleted. | ||
* Space Complexity: O(M) - Due to the recursive DFS approach. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the size of the Trie to 0 and creates a new root TrieNode. | ||
*/ | ||
clear(): void; | ||
/** | ||
* Time Complexity: O(l), where l is the length of the word being deleted. | ||
* Space Complexity: O(n) - Due to the recursive DFS approach. | ||
*/ | ||
/** | ||
* Time Complexity: O(l), where l is the length of the word being deleted. | ||
* Space Complexity: O(n) - Due to the recursive DFS approach. | ||
* | ||
* Remove a word from the Trie structure. | ||
@@ -111,7 +164,7 @@ * @param{string} word - The word to delete. | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(1) - Constant space. | ||
*/ | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -122,7 +175,7 @@ * | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -136,7 +189,7 @@ * | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -150,8 +203,8 @@ * | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the input prefix. | ||
*/ | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the input prefix. | ||
* | ||
@@ -164,8 +217,8 @@ * Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie. | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the longest common prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the longest common prefix. | ||
*/ | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the longest common prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the longest common prefix. | ||
* | ||
@@ -177,8 +230,8 @@ * Get the longest common prefix among all the words stored in the Trie. | ||
/** | ||
* Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words. | ||
* Space Complexity: O(K * L) - The space required for the output array. | ||
* Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words. | ||
* Space Complexity: O(w * l) - The space required for the output array. | ||
*/ | ||
/** | ||
* Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words. | ||
* Space Complexity: O(K * L) - The space required for the output array. | ||
* Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words. | ||
* Space Complexity: O(w * l) - The space required for the output array. | ||
* | ||
@@ -243,9 +296,20 @@ * The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix. | ||
map(callback: ElementCallback<string, string>, thisArg?: any): Trie; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `_getIterator` returns an iterable iterator that performs a depth-first search on a | ||
* trie data structure and yields all the paths to the end nodes. | ||
*/ | ||
protected _getIterator(): IterableIterator<string>; | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input string. | ||
* Time Complexity: O(l), where l is the length of the input string. | ||
* Space Complexity: O(1) - Constant space. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input string. | ||
* Time Complexity: O(l), where l is the length of the input string. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -252,0 +316,0 @@ * |
@@ -11,6 +11,54 @@ "use strict"; | ||
constructor(key) { | ||
this.key = key; | ||
this.isEnd = false; | ||
this.children = new Map(); | ||
this._key = key; | ||
this._isEnd = false; | ||
this._children = new Map(); | ||
} | ||
/** | ||
* The function returns the value of the protected variable _key. | ||
* @returns The value of the `_key` property, which is a string. | ||
*/ | ||
get key() { | ||
return this._key; | ||
} | ||
/** | ||
* The above function sets the value of a protected variable called "key". | ||
* @param {string} value - The value parameter is a string that represents the value to be assigned | ||
* to the key. | ||
*/ | ||
set key(value) { | ||
this._key = value; | ||
} | ||
/** | ||
* The function returns the children of a TrieNode as a Map. | ||
* @returns The `children` property of the TrieNode object, which is a Map containing string keys and | ||
* TrieNode values. | ||
*/ | ||
get children() { | ||
return this._children; | ||
} | ||
/** | ||
* The function sets the value of the `_children` property of a TrieNode object. | ||
* @param value - The value parameter is a Map object that represents the children of a TrieNode. The | ||
* keys of the map are strings, which represent the characters that are associated with each child | ||
* TrieNode. The values of the map are TrieNode objects, which represent the child nodes of the | ||
* current TrieNode. | ||
*/ | ||
set children(value) { | ||
this._children = value; | ||
} | ||
/** | ||
* The function returns a boolean value indicating whether a certain condition is met. | ||
* @returns The method is returning a boolean value, specifically the value of the variable `_isEnd`. | ||
*/ | ||
get isEnd() { | ||
return this._isEnd; | ||
} | ||
/** | ||
* The function sets the value of the "_isEnd" property. | ||
* @param {boolean} value - The value parameter is a boolean value that indicates whether the current | ||
* state is the end state or not. | ||
*/ | ||
set isEnd(value) { | ||
this._isEnd = value; | ||
} | ||
} | ||
@@ -61,5 +109,4 @@ exports.TrieNode = TrieNode; | ||
/** | ||
* The caseSensitive function is a getter that returns the value of the private _caseSensitive property. | ||
* | ||
* @return The value of the _casesensitive private variable | ||
* The caseSensitive function is a getter that returns the value of the protected _caseSensitive property. | ||
* @return The value of the _caseSensitive protected variable | ||
*/ | ||
@@ -77,8 +124,8 @@ get caseSensitive() { | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being added. | ||
* Space Complexity: O(M) - Each character in the word adds a TrieNode. | ||
* Time Complexity: O(l), where l is the length of the word being added. | ||
* Space Complexity: O(l) - Each character in the word adds a TrieNode. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being added. | ||
* Space Complexity: O(M) - Each character in the word adds a TrieNode. | ||
* Time Complexity: O(l), where l is the length of the word being added. | ||
* Space Complexity: O(l) - Each character in the word adds a TrieNode. | ||
* | ||
@@ -109,7 +156,7 @@ * Add a word to the Trie structure. | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input word. | ||
* Time Complexity: O(l), where l is the length of the input word. | ||
* Space Complexity: O(1) - Constant space. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input word. | ||
* Time Complexity: O(l), where l is the length of the input word. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -133,2 +180,9 @@ * | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The isEmpty function checks if the size of the queue is 0. | ||
@@ -141,9 +195,23 @@ * @return True if the size of the queue is 0 | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being deleted. | ||
* Space Complexity: O(M) - Due to the recursive DFS approach. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being deleted. | ||
* Space Complexity: O(M) - Due to the recursive DFS approach. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the size of the Trie to 0 and creates a new root TrieNode. | ||
*/ | ||
clear() { | ||
this._size = 0; | ||
this._root = new TrieNode(''); | ||
} | ||
/** | ||
* Time Complexity: O(l), where l is the length of the word being deleted. | ||
* Space Complexity: O(n) - Due to the recursive DFS approach. | ||
*/ | ||
/** | ||
* Time Complexity: O(l), where l is the length of the word being deleted. | ||
* Space Complexity: O(n) - Due to the recursive DFS approach. | ||
* | ||
* Remove a word from the Trie structure. | ||
@@ -189,7 +257,7 @@ * @param{string} word - The word to delete. | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(1) - Constant space. | ||
*/ | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -218,7 +286,7 @@ * | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -242,7 +310,7 @@ * | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -266,8 +334,8 @@ * | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the input prefix. | ||
*/ | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the input prefix. | ||
* | ||
@@ -296,8 +364,8 @@ * Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie. | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the longest common prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the longest common prefix. | ||
*/ | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the longest common prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the longest common prefix. | ||
* | ||
@@ -322,8 +390,8 @@ * Get the longest common prefix among all the words stored in the Trie. | ||
/** | ||
* Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words. | ||
* Space Complexity: O(K * L) - The space required for the output array. | ||
* Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words. | ||
* Space Complexity: O(w * l) - The space required for the output array. | ||
*/ | ||
/** | ||
* Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words. | ||
* Space Complexity: O(K * L) - The space required for the output array. | ||
* Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words. | ||
* Space Complexity: O(w * l) - The space required for the output array. | ||
* | ||
@@ -437,2 +505,13 @@ * The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix. | ||
} | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `_getIterator` returns an iterable iterator that performs a depth-first search on a | ||
* trie data structure and yields all the paths to the end nodes. | ||
*/ | ||
*_getIterator() { | ||
@@ -450,7 +529,7 @@ function* _dfs(node, path) { | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input string. | ||
* Time Complexity: O(l), where l is the length of the input string. | ||
* Space Complexity: O(1) - Constant space. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input string. | ||
* Time Complexity: O(l), where l is the length of the input string. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -457,0 +536,0 @@ * |
{ | ||
"name": "red-black-tree-typed", | ||
"version": "1.50.2", | ||
"version": "1.50.3", | ||
"description": "RedBlackTree. Javascript & Typescript Data Structure.", | ||
@@ -145,4 +145,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.50.2" | ||
"data-structure-typed": "^1.50.3" | ||
} | ||
} |
@@ -292,4 +292,10 @@ import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from '../../types'; | ||
abstract clear(): void; | ||
abstract clone(): any; | ||
abstract map(...args: any[]): any; | ||
abstract filter(...args: any[]): any; | ||
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>; | ||
@@ -501,5 +507,11 @@ } | ||
abstract clear(): void; | ||
abstract clone(): C; | ||
abstract map(...args: any[]): any; | ||
abstract filter(...args: any[]): any; | ||
protected abstract _getIterator(...args: any[]): IterableIterator<E>; | ||
} |
@@ -25,8 +25,33 @@ /** | ||
> extends BSTNode<K, V, NODE> { | ||
height: number; | ||
/** | ||
* The constructor function initializes a new instance of a class with a key and an optional value, | ||
* and sets the height property to 0. | ||
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the | ||
* constructor. It is used to initialize the key property of the object being created. | ||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the | ||
* value associated with the key in the constructor. | ||
*/ | ||
constructor(key: K, value?: V) { | ||
super(key, value); | ||
this.height = 0; | ||
this._height = 0; | ||
} | ||
protected _height: number; | ||
/** | ||
* The function returns the value of the height property. | ||
* @returns The height of the object. | ||
*/ | ||
get height(): number { | ||
return this._height; | ||
} | ||
/** | ||
* The above function sets the value of the height property. | ||
* @param {number} value - The value parameter is a number that represents the new height value to be | ||
* set. | ||
*/ | ||
set height(value: number) { | ||
this._height = value; | ||
} | ||
} | ||
@@ -492,2 +517,11 @@ | ||
/** | ||
* The function replaces an old node with a new node while preserving the height of the old node. | ||
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace with the | ||
* `newNode`. | ||
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in | ||
* the data structure. | ||
* @returns the result of calling the `_replaceNode` method on the superclass, passing in the | ||
* `oldNode` and `newNode` as arguments. | ||
*/ | ||
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE { | ||
@@ -494,0 +528,0 @@ newNode.height = oldNode.height; |
@@ -30,2 +30,7 @@ /** | ||
/** | ||
* The function returns the frequency map of numbers. | ||
* @returns The `_freqMap` property, which is a record with number keys and number values, is being | ||
* returned. | ||
*/ | ||
get freqMap(): Record<number, number> { | ||
@@ -37,2 +42,6 @@ return this._freqMap; | ||
/** | ||
* The function returns the value of the _msb property. | ||
* @returns The `_msb` property of the object. | ||
*/ | ||
get msb(): number { | ||
@@ -44,2 +53,7 @@ return this._msb; | ||
/** | ||
* The function returns the value of the _negativeCount property. | ||
* @returns The method is returning the value of the variable `_negativeCount`, which is of type | ||
* `number`. | ||
*/ | ||
get negativeCount(): number { | ||
@@ -49,2 +63,6 @@ return this._negativeCount; | ||
/** | ||
* The above function returns the value of the protected variable `_freq`. | ||
* @returns The frequency value stored in the protected variable `_freq`. | ||
*/ | ||
get freq(): number { | ||
@@ -54,2 +72,6 @@ return this._freq; | ||
/** | ||
* The above function returns the maximum value. | ||
* @returns The maximum value stored in the variable `_max`. | ||
*/ | ||
get max(): number { | ||
@@ -56,0 +78,0 @@ return this._max; |
@@ -37,2 +37,6 @@ /** | ||
/** | ||
* The function returns the value of the `_left` property. | ||
* @returns The `_left` property of the current object is being returned. | ||
*/ | ||
override get left(): NODE | undefined { | ||
@@ -42,2 +46,7 @@ return this._left; | ||
/** | ||
* The function sets the left child of a node and updates the parent reference of the child. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an | ||
* instance of the `NODE` class or `undefined`. | ||
*/ | ||
override set left(v: NODE | undefined) { | ||
@@ -52,2 +61,7 @@ if (v) { | ||
/** | ||
* The function returns the right node of a binary tree or undefined if there is no right node. | ||
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or | ||
* `undefined`. | ||
*/ | ||
override get right(): NODE | undefined { | ||
@@ -57,2 +71,7 @@ return this._right; | ||
/** | ||
* The function sets the right child of a node and updates the parent reference of the child. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a | ||
* `NODE` object or `undefined`. | ||
*/ | ||
override set right(v: NODE | undefined) { | ||
@@ -84,6 +103,6 @@ if (v) { | ||
/** | ||
* This is the constructor function for a binary search tree class in TypeScript, which initializes | ||
* the tree with optional keysOrNodesOrEntries and options. | ||
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the | ||
* binary search tree. | ||
* This is the constructor function for a TypeScript class that initializes a binary search tree with | ||
* optional keys or nodes or entries and options. | ||
* @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used | ||
* to initialize the binary search tree with the provided keys, nodes, or entries. | ||
* @param [options] - The `options` parameter is an optional object that can contain additional | ||
@@ -107,2 +126,6 @@ * configuration options for the binary search tree. It can have the following properties: | ||
/** | ||
* The function returns the root node of a tree structure. | ||
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`. | ||
*/ | ||
override get root(): NODE | undefined { | ||
@@ -114,2 +137,6 @@ return this._root; | ||
/** | ||
* The function returns the value of the _variant property. | ||
* @returns The value of the `_variant` property. | ||
*/ | ||
get variant() { | ||
@@ -120,8 +147,8 @@ return this._variant; | ||
/** | ||
* The function creates a new binary search tree node with the given key and value. | ||
* @param {K} key - The key parameter is the key value that will be associated with | ||
* the new node. It is used to determine the position of the node in the binary search tree. | ||
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It | ||
* represents the value associated with the node in a binary search tree. | ||
* @returns a new instance of the BSTNode class with the specified key and value. | ||
* The function creates a new BSTNode with the given key and value and returns it. | ||
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node | ||
* being created. | ||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the | ||
* value associated with the key in the node being created. | ||
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type. | ||
*/ | ||
@@ -135,5 +162,6 @@ override createNode(key: K, value?: V): NODE { | ||
* @param [options] - The `options` parameter is an optional object that allows you to customize the | ||
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type | ||
* that defines various options for creating a binary search tree. | ||
* @returns a new instance of the BST class with the specified options. | ||
* behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a | ||
* partial object of type `BSTOptions<K>`. | ||
* @returns a new instance of the BST class, with the provided options merged with the default | ||
* options. The returned value is casted as TREE. | ||
*/ | ||
@@ -836,2 +864,7 @@ override createTree(options?: Partial<BSTOptions<K>>): TREE { | ||
/** | ||
* The function sets the root property of an object and updates the parent property of the new root. | ||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it | ||
* can either be an object of type `NODE` or it can be `undefined`. | ||
*/ | ||
protected _setRoot(v: NODE | undefined) { | ||
@@ -838,0 +871,0 @@ if (v) { |
@@ -27,8 +27,35 @@ /** | ||
> extends BSTNode<K, V, NODE> { | ||
color: RBTNColor; | ||
/** | ||
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a | ||
* color. | ||
* @param {K} key - The key parameter is of type K and represents the key of the node in the | ||
* Red-Black Tree. | ||
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value | ||
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when | ||
* creating a new instance of the Red-Black Tree Node. | ||
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black | ||
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`. | ||
*/ | ||
constructor(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK) { | ||
super(key, value); | ||
this.color = color; | ||
this._color = color; | ||
} | ||
protected _color: RBTNColor; | ||
/** | ||
* The function returns the color value of a variable. | ||
* @returns The color value stored in the protected variable `_color`. | ||
*/ | ||
get color(): RBTNColor { | ||
return this._color; | ||
} | ||
/** | ||
* The function sets the color property to the specified value. | ||
* @param {RBTNColor} value - The value parameter is of type RBTNColor. | ||
*/ | ||
set color(value: RBTNColor) { | ||
this._color = value; | ||
} | ||
} | ||
@@ -51,4 +78,2 @@ | ||
implements IBinaryTree<K, V, NODE, TREE> { | ||
Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE; | ||
/** | ||
@@ -68,8 +93,22 @@ * This is the constructor function for a Red-Black Tree data structure in TypeScript, which | ||
this._root = this.Sentinel; | ||
this._root = this._Sentinel; | ||
if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries); | ||
} | ||
protected _Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE; | ||
/** | ||
* The function returns the value of the `_Sentinel` property. | ||
* @returns The method is returning the value of the `_Sentinel` property. | ||
*/ | ||
get Sentinel(): NODE { | ||
return this._Sentinel; | ||
} | ||
protected _root: NODE; | ||
/** | ||
* The function returns the root node. | ||
* @returns The root node of the data structure. | ||
*/ | ||
get root(): NODE { | ||
@@ -81,2 +120,6 @@ return this._root; | ||
/** | ||
* The function returns the size of an object. | ||
* @returns The size of the object, which is a number. | ||
*/ | ||
get size(): number { | ||
@@ -156,4 +199,10 @@ return this._size; | ||
/** | ||
* The function checks if a given node is a real node in a Red-Black Tree. | ||
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means | ||
* it can either be of type `NODE` or `undefined`. | ||
* @returns a boolean value. | ||
*/ | ||
override isRealNode(node: NODE | undefined): node is NODE { | ||
if (node === this.Sentinel || node === undefined) return false; | ||
if (node === this._Sentinel || node === undefined) return false; | ||
return node instanceof RedBlackTreeNode; | ||
@@ -184,4 +233,4 @@ } | ||
newNode.left = this.Sentinel; | ||
newNode.right = this.Sentinel; | ||
newNode.left = this._Sentinel; | ||
newNode.right = this._Sentinel; | ||
@@ -191,3 +240,3 @@ let y: NODE | undefined = undefined; | ||
while (x !== this.Sentinel) { | ||
while (x !== this._Sentinel) { | ||
y = x; | ||
@@ -260,5 +309,5 @@ if (x) { | ||
const helper = (node: NODE | undefined): void => { | ||
let z: NODE = this.Sentinel; | ||
let z: NODE = this._Sentinel; | ||
let x: NODE | undefined, y: NODE; | ||
while (node !== this.Sentinel) { | ||
while (node !== this._Sentinel) { | ||
if (node && callback(node) === identifier) { | ||
@@ -275,3 +324,3 @@ z = node; | ||
if (z === this.Sentinel) { | ||
if (z === this._Sentinel) { | ||
this._size--; | ||
@@ -283,6 +332,6 @@ return; | ||
let yOriginalColor: number = y.color; | ||
if (z.left === this.Sentinel) { | ||
if (z.left === this._Sentinel) { | ||
x = z.right; | ||
this._rbTransplant(z, z.right!); | ||
} else if (z.right === this.Sentinel) { | ||
} else if (z.right === this._Sentinel) { | ||
x = z.left; | ||
@@ -359,4 +408,10 @@ this._rbTransplant(z, z.left!); | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The "clear" function sets the root node to the sentinel node and resets the size to 0. | ||
*/ | ||
override clear() { | ||
this._root = this.Sentinel; | ||
this._root = this._Sentinel; | ||
this._size = 0; | ||
@@ -393,2 +448,8 @@ } | ||
/** | ||
* The function sets the root node of a tree structure and updates the parent property of the new | ||
* root node. | ||
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data | ||
* structure. | ||
*/ | ||
protected override _setRoot(v: NODE) { | ||
@@ -417,3 +478,3 @@ if (v) { | ||
x.right = y.left; | ||
if (y.left !== this.Sentinel) { | ||
if (y.left !== this._Sentinel) { | ||
if (y.left) y.left.parent = x; | ||
@@ -451,3 +512,3 @@ } | ||
x.left = y.right; | ||
if (y.right !== this.Sentinel) { | ||
if (y.right !== this._Sentinel) { | ||
if (y.right) y.right.parent = x; | ||
@@ -454,0 +515,0 @@ } |
@@ -12,15 +12,133 @@ /** | ||
export class SegmentTreeNode { | ||
start = 0; | ||
end = 0; | ||
value: SegmentTreeNodeVal | undefined = undefined; | ||
sum = 0; | ||
left: SegmentTreeNode | undefined = undefined; | ||
right: SegmentTreeNode | undefined = undefined; | ||
/** | ||
* The constructor initializes the properties of a SegmentTreeNode object. | ||
* @param {number} start - The `start` parameter represents the starting index of the segment covered | ||
* by this node in a segment tree. | ||
* @param {number} end - The `end` parameter represents the end index of the segment covered by this | ||
* node in a segment tree. | ||
* @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by | ||
* the segment tree node. | ||
* @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter | ||
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node. | ||
*/ | ||
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined) { | ||
this.start = start; | ||
this.end = end; | ||
this.sum = sum; | ||
this.value = value || undefined; | ||
this._start = start; | ||
this._end = end; | ||
this._sum = sum; | ||
this._value = value || undefined; | ||
} | ||
protected _start = 0; | ||
/** | ||
* The function returns the value of the protected variable _start. | ||
* @returns The start value, which is of type number. | ||
*/ | ||
get start(): number { | ||
return this._start; | ||
} | ||
/** | ||
* The above function sets the value of the "start" property. | ||
* @param {number} value - The value parameter is of type number. | ||
*/ | ||
set start(value: number) { | ||
this._start = value; | ||
} | ||
protected _end = 0; | ||
/** | ||
* The function returns the value of the protected variable `_end`. | ||
* @returns The value of the protected property `_end`. | ||
*/ | ||
get end(): number { | ||
return this._end; | ||
} | ||
/** | ||
* The above function sets the value of the "end" property. | ||
* @param {number} value - The value parameter is a number that represents the new value for the end | ||
* property. | ||
*/ | ||
set end(value: number) { | ||
this._end = value; | ||
} | ||
protected _value: SegmentTreeNodeVal | undefined = undefined; | ||
/** | ||
* The function returns the value of a segment tree node. | ||
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`. | ||
*/ | ||
get value(): SegmentTreeNodeVal | undefined { | ||
return this._value; | ||
} | ||
/** | ||
* The function sets the value of a segment tree node. | ||
* @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type | ||
* `SegmentTreeNodeVal` or `undefined`. | ||
*/ | ||
set value(value: SegmentTreeNodeVal | undefined) { | ||
this._value = value; | ||
} | ||
protected _sum = 0; | ||
/** | ||
* The function returns the value of the sum property. | ||
* @returns The method is returning the value of the variable `_sum`. | ||
*/ | ||
get sum(): number { | ||
return this._sum; | ||
} | ||
/** | ||
* The above function sets the value of the sum property. | ||
* @param {number} value - The parameter "value" is of type "number". | ||
*/ | ||
set sum(value: number) { | ||
this._sum = value; | ||
} | ||
protected _left: SegmentTreeNode | undefined = undefined; | ||
/** | ||
* The function returns the left child of a segment tree node. | ||
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type | ||
* `SegmentTreeNode` or `undefined`. | ||
*/ | ||
get left(): SegmentTreeNode | undefined { | ||
return this._left; | ||
} | ||
/** | ||
* The function sets the value of the left property of a SegmentTreeNode object. | ||
* @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or | ||
* undefined. | ||
*/ | ||
set left(value: SegmentTreeNode | undefined) { | ||
this._left = value; | ||
} | ||
protected _right: SegmentTreeNode | undefined = undefined; | ||
/** | ||
* The function returns the right child of a segment tree node. | ||
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`. | ||
*/ | ||
get right(): SegmentTreeNode | undefined { | ||
return this._right; | ||
} | ||
/** | ||
* The function sets the right child of a segment tree node. | ||
* @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode | | ||
* undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its | ||
* value. | ||
*/ | ||
set right(value: SegmentTreeNode | undefined) { | ||
this._right = value; | ||
} | ||
} | ||
@@ -55,2 +173,6 @@ | ||
/** | ||
* The function returns an array of numbers. | ||
* @returns An array of numbers is being returned. | ||
*/ | ||
get values(): number[] { | ||
@@ -62,2 +184,6 @@ return this._values; | ||
/** | ||
* The function returns the value of the protected variable _start. | ||
* @returns The start value, which is of type number. | ||
*/ | ||
get start(): number { | ||
@@ -69,2 +195,6 @@ return this._start; | ||
/** | ||
* The function returns the value of the protected variable `_end`. | ||
* @returns The value of the protected property `_end`. | ||
*/ | ||
get end(): number { | ||
@@ -76,2 +206,6 @@ return this._end; | ||
/** | ||
* The function returns the root node of a segment tree. | ||
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined. | ||
*/ | ||
get root(): SegmentTreeNode | undefined { | ||
@@ -78,0 +212,0 @@ return this._root; |
@@ -26,4 +26,2 @@ /** | ||
> extends AVLTreeNode<K, V, NODE> { | ||
count: number; | ||
/** | ||
@@ -43,2 +41,21 @@ * The constructor function initializes a BinaryTreeNode object with a key, value, and count. | ||
} | ||
protected _count: number = 1; | ||
/** | ||
* The function returns the value of the protected variable _count. | ||
* @returns The count property of the object, which is of type number. | ||
*/ | ||
get count(): number { | ||
return this._count; | ||
} | ||
/** | ||
* The above function sets the value of the count property. | ||
* @param {number} value - The value parameter is of type number, which means it can accept any | ||
* numeric value. | ||
*/ | ||
set count(value: number) { | ||
this._count = value; | ||
} | ||
} | ||
@@ -62,5 +79,10 @@ | ||
private _count = 0; | ||
protected _count = 0; | ||
// TODO the _count is not accurate after nodes count modified | ||
/** | ||
* The function calculates the sum of the count property of all nodes in a tree using depth-first | ||
* search. | ||
* @returns the sum of the count property of all nodes in the tree. | ||
*/ | ||
get count(): number { | ||
@@ -85,2 +107,11 @@ let sum = 0; | ||
/** | ||
* The function creates a new TreeMultimap object with the specified options and returns it. | ||
* @param [options] - The `options` parameter is an optional object that contains additional | ||
* configuration options for creating the `TreeMultimap` object. It can include properties such as | ||
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of | ||
* the tree, respectively. These properties can be | ||
* @returns a new instance of the `TreeMultimap` class, with the provided options merged with the | ||
* default options. The returned value is casted as `TREE`. | ||
*/ | ||
override createTree(options?: TreeMultimapOptions<K>): TREE { | ||
@@ -382,2 +413,10 @@ return new TreeMultimap<K, V, NODE, TREE>([], { | ||
/** | ||
* The function replaces an old node with a new node and updates the count property of the new node. | ||
* @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that | ||
* needs to be replaced in a data structure. | ||
* @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`. | ||
* @returns The method is returning the result of calling the `_replaceNode` method from the | ||
* superclass, after updating the `count` property of the `newNode` object. | ||
*/ | ||
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE { | ||
@@ -384,0 +423,0 @@ newNode.count = oldNode.count + newNode.count; |
@@ -958,213 +958,2 @@ /** | ||
/** | ||
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Linear space (Tarjan's algorithm). | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find cycles in directed or undirected graph | ||
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time, | ||
* Tarjan solve the bi-connected components of undirected graphs; | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
* / | ||
/** | ||
* Time Complexity: O(V + E) - Linear time (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Linear space (Tarjan's algorithm). | ||
* | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find cycles in directed or undirected graph | ||
* Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time, | ||
* Tarjan solve the bi-connected components of undirected graphs; | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
* The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges, | ||
* strongly connected components (SCCs), and cycles in a graph. | ||
* @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the | ||
* articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the | ||
* number of connected components in the graph. | ||
* @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges | ||
* (edgeMap whose removal would increase the number of connected components in the graph). | ||
* @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the | ||
* graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the | ||
* SCCs will not be calculated or returned. | ||
* @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If | ||
* set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values | ||
* are arrays of vertexMap that form cycles within the SCCs. | ||
* @returns The function `tarjan` returns an object with the following properties: | ||
*/ | ||
tarjan( | ||
needCutVertexes: boolean = false, | ||
needBridges: boolean = false, | ||
needSCCs: boolean = true, | ||
needCycles: boolean = false | ||
) { | ||
// !! in undirected graph we will not let child visit parent when dfs | ||
// !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2) | ||
// !! bridge: low(child) > dfn(cur) | ||
const defaultConfig = false; | ||
if (needCutVertexes === undefined) needCutVertexes = defaultConfig; | ||
if (needBridges === undefined) needBridges = defaultConfig; | ||
if (needSCCs === undefined) needSCCs = defaultConfig; | ||
if (needCycles === undefined) needCycles = defaultConfig; | ||
const dfnMap: Map<VO, number> = new Map(); | ||
const lowMap: Map<VO, number> = new Map(); | ||
const vertexMap = this._vertexMap; | ||
vertexMap.forEach(v => { | ||
dfnMap.set(v, -1); | ||
lowMap.set(v, Infinity); | ||
}); | ||
const [root] = vertexMap.values(); | ||
const cutVertexes: VO[] = []; | ||
const bridges: EO[] = []; | ||
let dfn = 0; | ||
const dfs = (cur: VO, parent: VO | undefined) => { | ||
dfn++; | ||
dfnMap.set(cur, dfn); | ||
lowMap.set(cur, dfn); | ||
const neighbors = this.getNeighbors(cur); | ||
let childCount = 0; // child in dfs tree not child in graph | ||
for (const neighbor of neighbors) { | ||
if (neighbor !== parent) { | ||
if (dfnMap.get(neighbor) === -1) { | ||
childCount++; | ||
dfs(neighbor, cur); | ||
} | ||
const childLow = lowMap.get(neighbor); | ||
const curLow = lowMap.get(cur); | ||
// TODO after no-non-undefined-assertion not ensure the logic | ||
if (curLow !== undefined && childLow !== undefined) { | ||
lowMap.set(cur, Math.min(curLow, childLow)); | ||
} | ||
const curFromMap = dfnMap.get(cur); | ||
if (childLow !== undefined && curFromMap !== undefined) { | ||
if (needCutVertexes) { | ||
if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) { | ||
// todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) { | ||
cutVertexes.push(cur); | ||
} | ||
} | ||
if (needBridges) { | ||
if (childLow > curFromMap) { | ||
const edgeCurToNeighbor = this.getEdge(cur, neighbor); | ||
if (edgeCurToNeighbor) { | ||
bridges.push(edgeCurToNeighbor); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
dfs(root, undefined); | ||
let SCCs: Map<number, VO[]> = new Map(); | ||
const getSCCs = () => { | ||
const SCCs: Map<number, VO[]> = new Map(); | ||
lowMap.forEach((low, vertex) => { | ||
if (!SCCs.has(low)) { | ||
SCCs.set(low, [vertex]); | ||
} else { | ||
SCCs.get(low)?.push(vertex); | ||
} | ||
}); | ||
return SCCs; | ||
}; | ||
if (needSCCs) { | ||
SCCs = getSCCs(); | ||
} | ||
const cycles: Map<number, VO[]> = new Map(); | ||
if (needCycles) { | ||
const visitedMap: Map<VO, boolean> = new Map(); | ||
const stack: VO[] = []; | ||
const findCyclesDFS = (cur: VO, parent: VO | undefined) => { | ||
visitedMap.set(cur, true); | ||
stack.push(cur); | ||
const neighbors = this.getNeighbors(cur); | ||
for (const neighbor of neighbors) { | ||
if (!visitedMap.get(neighbor)) { | ||
findCyclesDFS(neighbor, cur); | ||
} else if (stack.includes(neighbor) && neighbor !== parent) { | ||
const cycleStartIndex = stack.indexOf(neighbor); | ||
const cycle = stack.slice(cycleStartIndex); | ||
const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity)); | ||
cycles.set(cycleLow, cycle); | ||
} | ||
} | ||
stack.pop(); | ||
}; | ||
vertexMap.forEach(v => { | ||
if (!visitedMap.get(v)) { | ||
findCyclesDFS(v, undefined); | ||
} | ||
}); | ||
} | ||
return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles }; | ||
} | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
*/ | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
* | ||
* The function returns a map that associates each vertex object with its corresponding depth-first | ||
* number. | ||
* @returns A Map object with keys of type VO and values of type number. | ||
*/ | ||
getDFNMap(): Map<VO, number> { | ||
return this.tarjan(false, false, false, false).dfnMap; | ||
} | ||
/** | ||
* The function returns a Map object that contains the low values of each vertex in a Tarjan | ||
* algorithm. | ||
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of | ||
* type `number`. | ||
*/ | ||
getLowMap(): Map<VO, number> { | ||
return this.tarjan(false, false, false, false).lowMap; | ||
} | ||
/** | ||
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm. | ||
* @returns an array of VO objects, specifically the cut vertexes. | ||
*/ | ||
getCutVertexes(): VO[] { | ||
return this.tarjan(true, false, false, false).cutVertexes; | ||
} | ||
/** | ||
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan | ||
* algorithm. | ||
* @returns a map where the keys are numbers and the values are arrays of VO objects. | ||
*/ | ||
getSCCs(): Map<number, VO[]> { | ||
return this.tarjan(false, false, true, false).SCCs; | ||
} | ||
/** | ||
* The function "getBridges" returns an array of bridges using the Tarjan algorithm. | ||
* @returns the bridges found using the Tarjan algorithm. | ||
*/ | ||
getBridges() { | ||
return this.tarjan(false, true, false, false).bridges; | ||
} | ||
/** | ||
* O(V+E+C) | ||
@@ -1171,0 +960,0 @@ * O(V+C) |
@@ -620,2 +620,19 @@ /** | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the vertex map, in-edge map, and out-edge map. | ||
*/ | ||
clear() { | ||
this._vertexMap = new Map<VertexKey, VO>(); | ||
this._inEdgeMap = new Map<VO, EO[]>(); | ||
this._outEdgeMap = new Map<VO, EO[]>(); | ||
} | ||
/** | ||
* The clone function creates a new DirectedGraph object with the same vertices and edges as the original. | ||
@@ -634,2 +651,107 @@ * | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
*/ | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs. | ||
* | ||
* The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a | ||
* graph. | ||
* @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and | ||
* `SCCs`. | ||
*/ | ||
tarjan(): { dfnMap: Map<VO, number>; lowMap: Map<VO, number>; SCCs: Map<number, VO[]> } { | ||
const dfnMap = new Map<VO, number>(); | ||
const lowMap = new Map<VO, number>(); | ||
const SCCs = new Map<number, VO[]>(); | ||
let time = 0; | ||
const stack: VO[] = []; | ||
const inStack: Set<VO> = new Set(); | ||
const dfs = (vertex: VO) => { | ||
dfnMap.set(vertex, time); | ||
lowMap.set(vertex, time); | ||
time++; | ||
stack.push(vertex); | ||
inStack.add(vertex); | ||
const neighbors = this.getNeighbors(vertex); | ||
for (const neighbor of neighbors) { | ||
if (!dfnMap.has(neighbor)) { | ||
dfs(neighbor); | ||
lowMap.set(vertex, Math.min(lowMap.get(vertex)!, lowMap.get(neighbor)!)); | ||
} else if (inStack.has(neighbor)) { | ||
lowMap.set(vertex, Math.min(lowMap.get(vertex)!, dfnMap.get(neighbor)!)); | ||
} | ||
} | ||
if (dfnMap.get(vertex) === lowMap.get(vertex)) { | ||
const SCC: VO[] = []; | ||
let poppedVertex: VO | undefined; | ||
do { | ||
poppedVertex = stack.pop(); | ||
inStack.delete(poppedVertex!); | ||
SCC.push(poppedVertex!); | ||
} while (poppedVertex !== vertex); | ||
SCCs.set(SCCs.size, SCC); | ||
} | ||
}; | ||
for (const vertex of this.vertexMap.values()) { | ||
if (!dfnMap.has(vertex)) { | ||
dfs(vertex); | ||
} | ||
} | ||
return { dfnMap, lowMap, SCCs }; | ||
} | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
*/ | ||
/** | ||
* Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm). | ||
* Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm). | ||
* | ||
* The function returns a map that associates each vertex object with its corresponding depth-first | ||
* number. | ||
* @returns A Map object with keys of type VO and values of type number. | ||
*/ | ||
getDFNMap(): Map<VO, number> { | ||
return this.tarjan().dfnMap; | ||
} | ||
/** | ||
* The function returns a Map object that contains the low values of each vertex in a Tarjan | ||
* algorithm. | ||
* @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of | ||
* type `number`. | ||
*/ | ||
getLowMap(): Map<VO, number> { | ||
return this.tarjan().lowMap; | ||
} | ||
/** | ||
* The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan | ||
* algorithm. | ||
* @returns a map where the keys are numbers and the values are arrays of VO objects. | ||
*/ | ||
getSCCs(): Map<number, VO[]> { | ||
return this.tarjan().SCCs; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -636,0 +758,0 @@ * Space Complexity: O(1) |
@@ -27,3 +27,3 @@ /** | ||
export class UndirectedEdge<E = number> extends AbstractEdge<E> { | ||
vertexMap: [VertexKey, VertexKey]; | ||
endpoints: [VertexKey, VertexKey]; | ||
@@ -42,3 +42,3 @@ /** | ||
super(weight, value); | ||
this.vertexMap = [v1, v2]; | ||
this.endpoints = [v1, v2]; | ||
} | ||
@@ -109,3 +109,3 @@ } | ||
* | ||
* The function `getEdge` returns the first edge that connects two vertexMap, or undefined if no such edge exists. | ||
* The function `getEdge` returns the first edge that connects two endpoints, or undefined if no such edge exists. | ||
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex | ||
@@ -125,3 +125,3 @@ * object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex). | ||
if (vertex1 && vertex2) { | ||
edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.vertexMap.includes(vertex2.key)); | ||
edgeMap = this._edgeMap.get(vertex1)?.filter(e => e.endpoints.includes(vertex2.key)); | ||
} | ||
@@ -146,3 +146,3 @@ } | ||
* (VertexKey). It represents the second vertex of the edge that needs to be removed. | ||
* @returns the removed edge (EO) if it exists, or undefined if either of the vertexMap (VO) does not exist. | ||
* @returns the removed edge (EO) if it exists, or undefined if either of the endpoints (VO) does not exist. | ||
*/ | ||
@@ -160,7 +160,7 @@ deleteEdgeBetween(v1: VO | VertexKey, v2: VO | VertexKey): EO | undefined { | ||
if (v1Edges) { | ||
removed = arrayRemove<EO>(v1Edges, (e: EO) => e.vertexMap.includes(vertex2.key))[0] || undefined; | ||
removed = arrayRemove<EO>(v1Edges, (e: EO) => e.endpoints.includes(vertex2.key))[0] || undefined; | ||
} | ||
const v2Edges = this._edgeMap.get(vertex2); | ||
if (v2Edges) { | ||
arrayRemove<EO>(v2Edges, (e: EO) => e.vertexMap.includes(vertex1.key)); | ||
arrayRemove<EO>(v2Edges, (e: EO) => e.endpoints.includes(vertex1.key)); | ||
} | ||
@@ -179,3 +179,3 @@ return removed; | ||
* | ||
* The function `deleteEdge` deletes an edge between two vertexMap in a graph. | ||
* The function `deleteEdge` deletes an edge between two endpoints in a graph. | ||
* @param {EO | VertexKey} edgeOrOneSideVertexKey - The parameter `edgeOrOneSideVertexKey` can be | ||
@@ -199,4 +199,4 @@ * either an edge object or a vertex key. | ||
} else { | ||
oneSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[0]); | ||
otherSide = this._getVertex(edgeOrOneSideVertexKey.vertexMap[1]); | ||
oneSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[0]); | ||
otherSide = this._getVertex(edgeOrOneSideVertexKey.endpoints[1]); | ||
} | ||
@@ -243,3 +243,3 @@ | ||
const restEdges = neighborEdges.filter(edge => { | ||
return !edge.vertexMap.includes(vertexKey); | ||
return !edge.endpoints.includes(vertexKey); | ||
}); | ||
@@ -333,3 +333,3 @@ this._edgeMap.set(neighbor, restEdges); | ||
* | ||
* The function "getNeighbors" returns an array of neighboring vertexMap for a given vertex or vertex ID. | ||
* The function "getNeighbors" returns an array of neighboring endpoints for a given vertex or vertex ID. | ||
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID | ||
@@ -345,3 +345,3 @@ * (`VertexKey`). | ||
for (const edge of neighborEdges) { | ||
const neighbor = this._getVertex(edge.vertexMap.filter(e => e !== vertex.key)[0]); | ||
const neighbor = this._getVertex(edge.endpoints.filter(e => e !== vertex.key)[0]); | ||
if (neighbor) { | ||
@@ -364,14 +364,14 @@ neighbors.push(neighbor); | ||
* | ||
* The function "getEndsOfEdge" returns the vertexMap at the ends of an edge if the edge exists in the graph, otherwise | ||
* The function "getEndsOfEdge" returns the endpoints at the ends of an edge if the edge exists in the graph, otherwise | ||
* it returns undefined. | ||
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph. | ||
* @returns The function `getEndsOfEdge` returns an array containing two vertexMap `[VO, VO]` if the edge exists in the | ||
* @returns The function `getEndsOfEdge` returns an array containing two endpoints `[VO, VO]` if the edge exists in the | ||
* graph. If the edge does not exist, it returns `undefined`. | ||
*/ | ||
getEndsOfEdge(edge: EO): [VO, VO] | undefined { | ||
if (!this.hasEdge(edge.vertexMap[0], edge.vertexMap[1])) { | ||
if (!this.hasEdge(edge.endpoints[0], edge.endpoints[1])) { | ||
return undefined; | ||
} | ||
const v1 = this._getVertex(edge.vertexMap[0]); | ||
const v2 = this._getVertex(edge.vertexMap[1]); | ||
const v1 = this._getVertex(edge.endpoints[0]); | ||
const v2 = this._getVertex(edge.endpoints[1]); | ||
if (v1 && v2) { | ||
@@ -393,2 +393,18 @@ return [v1, v2]; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the vertex and edge maps to empty maps. | ||
*/ | ||
clear() { | ||
this._vertexMap = new Map<VertexKey, VO>(); | ||
this._edgeMap = new Map<VO, EO[]>(); | ||
} | ||
/** | ||
* The clone function creates a new UndirectedGraph object and copies the | ||
@@ -415,2 +431,110 @@ * vertexMap and edgeMap from this graph to the new one. This is done by | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time | ||
* | ||
* The function `tarjan` implements the Tarjan's algorithm to find bridges and cut vertices in a | ||
* graph. | ||
* @returns The function `tarjan()` returns an object with the following properties: | ||
*/ | ||
tarjan(): { dfnMap: Map<VO, number>; lowMap: Map<VO, number>; bridges: EO[]; cutVertices: VO[] } { | ||
const dfnMap = new Map<VO, number>(); | ||
const lowMap = new Map<VO, number>(); | ||
const bridges: EO[] = []; | ||
const cutVertices: VO[] = []; | ||
let time = 0; | ||
const dfs = (vertex: VO, parent: VO | undefined) => { | ||
dfnMap.set(vertex, time); | ||
lowMap.set(vertex, time); | ||
time++; | ||
const neighbors = this.getNeighbors(vertex); | ||
let childCount = 0; | ||
for (const neighbor of neighbors) { | ||
if (!dfnMap.has(neighbor)) { | ||
childCount++; | ||
dfs(neighbor, vertex); | ||
lowMap.set(vertex, Math.min(lowMap.get(vertex)!, lowMap.get(neighbor)!)); | ||
if (lowMap.get(neighbor)! > dfnMap.get(vertex)!) { | ||
// Found a bridge | ||
const edge = this.getEdge(vertex, neighbor); | ||
if (edge) { | ||
bridges.push(edge); | ||
} | ||
} | ||
if (parent !== undefined && lowMap.get(neighbor)! >= dfnMap.get(vertex)!) { | ||
// Found an articulation point | ||
cutVertices.push(vertex); | ||
} | ||
} else if (neighbor !== parent) { | ||
lowMap.set(vertex, Math.min(lowMap.get(vertex)!, dfnMap.get(neighbor)!)); | ||
} | ||
} | ||
if (parent === undefined && childCount > 1) { | ||
// Special case for root in DFS tree | ||
cutVertices.push(vertex); | ||
} | ||
}; | ||
for (const vertex of this.vertexMap.values()) { | ||
if (!dfnMap.has(vertex)) { | ||
dfs(vertex, undefined); | ||
} | ||
} | ||
return { | ||
dfnMap, | ||
lowMap, | ||
bridges, | ||
cutVertices | ||
}; | ||
} | ||
/** | ||
* Time Complexity: O(V + E) | ||
* Space Complexity: O(V) | ||
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs. | ||
* 1. Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time | ||
*/ | ||
/** | ||
* The function "getBridges" returns an array of bridges in a graph using the Tarjan's algorithm. | ||
* @returns The function `getBridges()` is returning the bridges found using the Tarjan's algorithm. | ||
*/ | ||
getBridges() { | ||
return this.tarjan().bridges; | ||
} | ||
/** | ||
* The function "getCutVertices" returns an array of cut vertices using the Tarjan's algorithm. | ||
* @returns the cut vertices found using the Tarjan's algorithm. | ||
*/ | ||
getCutVertices() { | ||
return this.tarjan().cutVertices; | ||
} | ||
/** | ||
* The function returns the dfnMap property of the result of the tarjan() function. | ||
* @returns the `dfnMap` property of the result of calling the `tarjan()` function. | ||
*/ | ||
getDFNMap() { | ||
return this.tarjan().dfnMap; | ||
} | ||
/** | ||
* The function returns the lowMap property of the result of the tarjan() function. | ||
* @returns the lowMap property of the result of calling the tarjan() function. | ||
*/ | ||
getLowMap() { | ||
return this.tarjan().lowMap; | ||
} | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -424,3 +548,3 @@ * Space Complexity: O(1) | ||
protected _addEdge(edge: EO): boolean { | ||
for (const end of edge.vertexMap) { | ||
for (const end of edge.endpoints) { | ||
const endVertex = this._getVertex(end); | ||
@@ -427,0 +551,0 @@ if (endVertex === undefined) return false; |
@@ -25,5 +25,2 @@ /** | ||
export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> { | ||
protected _store: { [key: string]: HashMapStoreItem<K, V> } = {}; | ||
protected _objMap: Map<object, V> = new Map(); | ||
/** | ||
@@ -52,2 +49,24 @@ * The constructor function initializes a HashMap object with an optional initial collection and | ||
protected _store: { [key: string]: HashMapStoreItem<K, V> } = {}; | ||
/** | ||
* The function returns the store object, which is a dictionary of HashMapStoreItem objects. | ||
* @returns The store property is being returned. It is a dictionary-like object with string keys and | ||
* values of type HashMapStoreItem<K, V>. | ||
*/ | ||
get store(): { [p: string]: HashMapStoreItem<K, V> } { | ||
return this._store; | ||
} | ||
protected _objMap: Map<object, V> = new Map(); | ||
/** | ||
* The function returns the object map. | ||
* @returns The `objMap` property is being returned, which is a `Map` object with keys of type | ||
* `object` and values of type `V`. | ||
*/ | ||
get objMap(): Map<object, V> { | ||
return this._objMap; | ||
} | ||
protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => { | ||
@@ -72,12 +91,2 @@ if (this.isEntry(rawElement)) { | ||
/** | ||
* The hasFn function is a function that takes in an item and returns a boolean | ||
* indicating whether the item is contained within the hash table. | ||
* | ||
* @return The hash function | ||
*/ | ||
get hasFn() { | ||
return this._hashFn; | ||
} | ||
protected _size = 0; | ||
@@ -93,3 +102,15 @@ | ||
protected _hashFn: (key: K) => string = (key: K) => String(key); | ||
/** | ||
* The hasFn function is a function that takes in an item and returns a boolean | ||
* indicating whether the item is contained within the hash table. | ||
* | ||
* @return The hash function | ||
*/ | ||
get hashFn() { | ||
return this._hashFn; | ||
} | ||
/** | ||
* The function checks if a given element is an array with exactly two elements. | ||
@@ -133,9 +154,9 @@ * @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any | ||
if (this._isObjKey(key)) { | ||
if (!this._objMap.has(key)) { | ||
if (!this.objMap.has(key)) { | ||
this._size++; | ||
} | ||
this._objMap.set(key, value); | ||
this.objMap.set(key, value); | ||
} else { | ||
const strKey = this._getNoObjKey(key); | ||
if (this._store[strKey] === undefined) { | ||
if (this.store[strKey] === undefined) { | ||
this._size++; | ||
@@ -182,3 +203,3 @@ } | ||
if (this._isObjKey(key)) { | ||
return this._objMap.get(key); | ||
return this.objMap.get(key); | ||
} else { | ||
@@ -198,6 +219,6 @@ const strKey = this._getNoObjKey(key); | ||
if (this._isObjKey(key)) { | ||
return this._objMap.has(key); | ||
return this.objMap.has(key); | ||
} else { | ||
const strKey = this._getNoObjKey(key); | ||
return strKey in this._store; | ||
return strKey in this.store; | ||
} | ||
@@ -215,11 +236,11 @@ } | ||
if (this._isObjKey(key)) { | ||
if (this._objMap.has(key)) { | ||
if (this.objMap.has(key)) { | ||
this._size--; | ||
} | ||
return this._objMap.delete(key); | ||
return this.objMap.delete(key); | ||
} else { | ||
const strKey = this._getNoObjKey(key); | ||
if (strKey in this._store) { | ||
delete this._store[strKey]; | ||
if (strKey in this.store) { | ||
delete this.store[strKey]; | ||
this._size--; | ||
@@ -233,2 +254,7 @@ return true; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* The clone function creates a new HashMap with the same key-value pairs as | ||
@@ -241,3 +267,3 @@ * this one. The clone function is useful for creating a copy of an existing | ||
clone(): HashMap<K, V, R> { | ||
return new HashMap<K, V, R>(this, { hashFn: this._hashFn, toEntryFn: this.toEntryFn }); | ||
return new HashMap<K, V, R>(this, { hashFn: this.hashFn, toEntryFn: this.toEntryFn }); | ||
} | ||
@@ -276,7 +302,2 @@ | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -323,6 +344,6 @@ * The `filter` function creates a new HashMap containing key-value pairs from the original HashMap | ||
protected* _getIterator(): IterableIterator<[K, V]> { | ||
for (const node of Object.values(this._store)) { | ||
for (const node of Object.values(this.store)) { | ||
yield [node.key, node.value] as [K, V]; | ||
} | ||
for (const node of this._objMap) { | ||
for (const node of this.objMap) { | ||
yield node as [K, V]; | ||
@@ -332,4 +353,7 @@ } | ||
protected _hashFn: (key: K) => string = (key: K) => String(key); | ||
/** | ||
* The function checks if a given key is an object or a function. | ||
* @param {any} key - The parameter "key" can be of any type. | ||
* @returns a boolean value. | ||
*/ | ||
protected _isObjKey(key: any): key is object | ((...args: any[]) => any) { | ||
@@ -340,2 +364,9 @@ const keyType = typeof key; | ||
/** | ||
* The function `_getNoObjKey` takes a key and returns a string representation of the key, handling | ||
* different types of keys. | ||
* @param {K} key - The `key` parameter is of type `K`, which represents the type of the key being | ||
* passed to the `_getNoObjKey` function. | ||
* @returns a string value. | ||
*/ | ||
protected _getNoObjKey(key: K): string { | ||
@@ -346,3 +377,3 @@ const keyType = typeof key; | ||
if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') { | ||
strKey = this._hashFn(key); | ||
strKey = this.hashFn(key); | ||
} else { | ||
@@ -366,6 +397,2 @@ if (keyType === 'number') { | ||
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>; | ||
@@ -405,2 +432,65 @@ | ||
protected _hashFn: (key: K) => string = (key: K) => String(key); | ||
/** | ||
* The function returns the hash function used for generating a hash value for a given key. | ||
* @returns The hash function that takes a key of type K and returns a string. | ||
*/ | ||
get hashFn(): (key: K) => string { | ||
return this._hashFn; | ||
} | ||
protected _objHashFn: (key: K) => object = (key: K) => <object>key; | ||
/** | ||
* The function returns the object hash function. | ||
* @returns The function `objHashFn` is being returned. | ||
*/ | ||
get objHashFn(): (key: K) => object { | ||
return this._objHashFn; | ||
} | ||
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {}; | ||
/** | ||
* The function returns a record of HashMapLinkedNode objects with string keys. | ||
* @returns The method is returning a Record object, which is a TypeScript type that represents an | ||
* object with string keys and values that are HashMapLinkedNode objects with keys of type K and | ||
* values of type V or undefined. | ||
*/ | ||
get noObjMap(): Record<string, HashMapLinkedNode<K, V | undefined>> { | ||
return this._noObjMap; | ||
} | ||
protected _objMap = new WeakMap<object, HashMapLinkedNode<K, V | undefined>>(); | ||
/** | ||
* The function returns the WeakMap object used to map objects to HashMapLinkedNode instances. | ||
* @returns The `objMap` property is being returned. | ||
*/ | ||
get objMap(): WeakMap<object, HashMapLinkedNode<K, V | undefined>> { | ||
return this._objMap; | ||
} | ||
protected _head: HashMapLinkedNode<K, V | undefined>; | ||
/** | ||
* The function returns the head node of a HashMapLinkedNode. | ||
* @returns The method `getHead()` is returning a `HashMapLinkedNode` object with key type `K` and | ||
* value type `V | undefined`. | ||
*/ | ||
get head(): HashMapLinkedNode<K, V | undefined> { | ||
return this._head; | ||
} | ||
protected _tail: HashMapLinkedNode<K, V | undefined>; | ||
/** | ||
* The function returns the tail node of a HashMapLinkedNode. | ||
* @returns The `_tail` property of type `HashMapLinkedNode<K, V | undefined>` is being returned. | ||
*/ | ||
get tail(): HashMapLinkedNode<K, V | undefined> { | ||
return this._tail; | ||
} | ||
protected _toEntryFn: (rawElement: R) => [K, V] = (rawElement: R) => { | ||
@@ -438,2 +528,7 @@ if (this.isEntry(rawElement)) { | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -446,3 +541,3 @@ * The function returns the key-value pair at the front of a data structure. | ||
if (this._size === 0) return; | ||
return <[K, V]>[this._head.key, this._head.value]; | ||
return <[K, V]>[this.head.key, this.head.value]; | ||
} | ||
@@ -453,2 +548,7 @@ | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -461,3 +561,3 @@ * The function returns the key-value pair at the end of a data structure. | ||
if (this._size === 0) return; | ||
return <[K, V]>[this._tail.key, this._tail.value]; | ||
return <[K, V]>[this.tail.key, this.tail.value]; | ||
} | ||
@@ -469,3 +569,3 @@ | ||
* begin() { | ||
let node = this._head; | ||
let node = this.head; | ||
while (node !== this._sentinel) { | ||
@@ -482,3 +582,3 @@ yield [node.key, node.value]; | ||
* reverseBegin() { | ||
let node = this._tail; | ||
let node = this.tail; | ||
while (node !== this._sentinel) { | ||
@@ -493,2 +593,7 @@ yield [node.key, node.value]; | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -508,9 +613,9 @@ * The `set` function adds a new key-value pair to a data structure, either using an object key or a | ||
if (isWeakKey(key)) { | ||
const hash = this._objHashFn(key); | ||
node = this._objMap.get(hash); | ||
const hash = this.objHashFn(key); | ||
node = this.objMap.get(hash); | ||
if (!node && isNewKey) { | ||
// Create new node | ||
node = { key: <K>hash, value, prev: this._tail, next: this._sentinel }; | ||
this._objMap.set(hash, node); | ||
node = { key: <K>hash, value, prev: this.tail, next: this._sentinel }; | ||
this.objMap.set(hash, node); | ||
} else if (node) { | ||
@@ -521,7 +626,7 @@ // Update the value of an existing node | ||
} else { | ||
const hash = this._hashFn(key); | ||
node = this._noObjMap[hash]; | ||
const hash = this.hashFn(key); | ||
node = this.noObjMap[hash]; | ||
if (!node && isNewKey) { | ||
this._noObjMap[hash] = node = { key, value, prev: this._tail, next: this._sentinel }; | ||
this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel }; | ||
} else if (node) { | ||
@@ -539,4 +644,4 @@ // Update the value of an existing node | ||
} else { | ||
this._tail.next = node; | ||
node.prev = this._tail; // Make sure that the prev of the new node points to the current tail node | ||
this.tail.next = node; | ||
node.prev = this.tail; // Make sure that the prev of the new node points to the current tail node | ||
} | ||
@@ -576,7 +681,7 @@ this._tail = node; | ||
if (isWeakKey(key)) { | ||
const hash = this._objHashFn(key); | ||
return this._objMap.has(hash); | ||
const hash = this.objHashFn(key); | ||
return this.objMap.has(hash); | ||
} else { | ||
const hash = this._hashFn(key); | ||
return hash in this._noObjMap; | ||
const hash = this.hashFn(key); | ||
return hash in this.noObjMap; | ||
} | ||
@@ -588,2 +693,7 @@ } | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -601,8 +711,8 @@ * The function `get` retrieves the value associated with a given key from a map, either by using the | ||
if (isWeakKey(key)) { | ||
const hash = this._objHashFn(key); | ||
const node = this._objMap.get(hash); | ||
const hash = this.objHashFn(key); | ||
const node = this.objMap.get(hash); | ||
return node ? node.value : undefined; | ||
} else { | ||
const hash = this._hashFn(key); | ||
const node = this._noObjMap[hash]; | ||
const hash = this.hashFn(key); | ||
const node = this.noObjMap[hash]; | ||
return node ? node.value : undefined; | ||
@@ -613,4 +723,9 @@ } | ||
/** | ||
* Time Complexity: O(n), where n is the index. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -626,3 +741,3 @@ * The function `at` retrieves the key-value pair at a specified index in a linked list. | ||
rangeCheck(index, 0, this._size - 1); | ||
let node = this._head; | ||
let node = this.head; | ||
while (index--) { | ||
@@ -637,2 +752,7 @@ node = node.next; | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -649,5 +769,5 @@ * The `delete` function removes a key-value pair from a map-like data structure. | ||
if (isWeakKey(key)) { | ||
const hash = this._objHashFn(key); | ||
const hash = this.objHashFn(key); | ||
// Get nodes from WeakMap | ||
node = this._objMap.get(hash); | ||
node = this.objMap.get(hash); | ||
@@ -659,7 +779,7 @@ if (!node) { | ||
// Remove nodes from WeakMap | ||
this._objMap.delete(hash); | ||
this.objMap.delete(hash); | ||
} else { | ||
const hash = this._hashFn(key); | ||
const hash = this.hashFn(key); | ||
// Get nodes from noObjMap | ||
node = this._noObjMap[hash]; | ||
node = this.noObjMap[hash]; | ||
@@ -671,3 +791,3 @@ if (!node) { | ||
// Remove nodes from orgMap | ||
delete this._noObjMap[hash]; | ||
delete this.noObjMap[hash]; | ||
} | ||
@@ -683,2 +803,7 @@ | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -692,3 +817,3 @@ * The `deleteAt` function deletes a node at a specified index in a linked list. | ||
rangeCheck(index, 0, this._size - 1); | ||
let node = this._head; | ||
let node = this.head; | ||
while (index--) { | ||
@@ -703,2 +828,7 @@ node = node.next; | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -726,2 +856,7 @@ * The function checks if a data structure is empty by comparing its size to zero. | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -751,3 +886,3 @@ * The `clear` function clears all the entries in a data structure and resets its properties. | ||
clone(): LinkedHashMap<K, V> { | ||
const cloned = new LinkedHashMap<K, V>([], { hashFn: this._hashFn, objHashFn: this._objHashFn }); | ||
const cloned = new LinkedHashMap<K, V>([], { hashFn: this.hashFn, objHashFn: this.objHashFn }); | ||
for (const entry of this) { | ||
@@ -763,2 +898,7 @@ const [key, value] = entry; | ||
* Space Complexity: O(n) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -791,2 +931,7 @@ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original | ||
* Space Complexity: O(n) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -837,6 +982,2 @@ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to | ||
protected _hashFn: (key: K) => string = (key: K) => String(key); | ||
protected _objHashFn: (key: K) => object = (key: K) => <object>key; | ||
/** | ||
@@ -846,2 +987,8 @@ * 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. | ||
* | ||
@@ -851,3 +998,3 @@ * The above function is an iterator that yields key-value pairs from a linked list. | ||
protected* _getIterator() { | ||
let node = this._head; | ||
let node = this.head; | ||
while (node !== this._sentinel) { | ||
@@ -862,2 +1009,7 @@ yield [node.key, node.value] as [K, V]; | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -875,7 +1027,7 @@ * The `_deleteNode` function removes a node from a doubly linked list and updates the head and tail | ||
if (node === this._head) { | ||
if (node === this.head) { | ||
this._head = next; | ||
} | ||
if (node === this._tail) { | ||
if (node === this.tail) { | ||
this._tail = prev; | ||
@@ -882,0 +1034,0 @@ } |
@@ -46,3 +46,2 @@ /** | ||
} | ||
// this.fix(); | ||
} | ||
@@ -147,2 +146,7 @@ } | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -172,3 +176,3 @@ * Peek at the top element of the heap without removing it. | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the elements array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -178,3 +182,3 @@ */ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the elements array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -191,3 +195,3 @@ * | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
@@ -197,3 +201,3 @@ */ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
@@ -210,8 +214,9 @@ * | ||
/** | ||
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* The worst-case O(n) This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. | ||
*/ | ||
/** | ||
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
@@ -242,9 +247,10 @@ * | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Space Complexity: O(h), where h is the height of the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* where log n is the height of the heap. | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the heap. | ||
* Space Complexity: O(h), where h is the height of the heap. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* | ||
@@ -341,9 +347,9 @@ * Depth-first search (DFS) method, different traversal orders can be selected。 | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n log n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -436,3 +442,3 @@ * Fix the entire heap to maintain heap properties. | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -464,2 +470,7 @@ */ | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -697,3 +708,3 @@ * Sinking operation to maintain heap properties after removing the top element. | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -703,3 +714,3 @@ */ | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -715,3 +726,3 @@ * | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -721,3 +732,3 @@ */ | ||
/** | ||
* Time Complexity: O(log n), where n is the number of elements in the heap. | ||
* Time Complexity: O(log n) | ||
* Space Complexity: O(1) | ||
@@ -724,0 +735,0 @@ * |
@@ -12,6 +12,2 @@ /** | ||
export class DoublyLinkedListNode<E = any> { | ||
value: E; | ||
next: DoublyLinkedListNode<E> | undefined; | ||
prev: DoublyLinkedListNode<E> | undefined; | ||
/** | ||
@@ -23,6 +19,66 @@ * The constructor function initializes the value, next, and previous properties of an object. | ||
constructor(value: E) { | ||
this.value = value; | ||
this.next = undefined; | ||
this.prev = undefined; | ||
this._value = value; | ||
this._next = undefined; | ||
this._prev = undefined; | ||
} | ||
protected _value: E; | ||
/** | ||
* The function returns the value of a protected variable. | ||
* @returns The value of the variable `_value` is being returned. | ||
*/ | ||
get value(): E { | ||
return this._value; | ||
} | ||
/** | ||
* The above function sets the value of a variable. | ||
* @param {E} value - The parameter "value" is of type E, which means it can be any type. | ||
*/ | ||
set value(value: E) { | ||
this._value = value; | ||
} | ||
protected _next: DoublyLinkedListNode<E> | undefined; | ||
/** | ||
* The "next" function returns the next node in a doubly linked list. | ||
* @returns The `next` property is being returned. It can be either a `DoublyLinkedListNode<E>` | ||
* object or `undefined`. | ||
*/ | ||
get next(): DoublyLinkedListNode<E> | undefined { | ||
return this._next; | ||
} | ||
/** | ||
* The "next" property of a DoublyLinkedListNode is set to the provided value. | ||
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type | ||
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a | ||
* `DoublyLinkedListNode` object or `undefined` as its value. | ||
*/ | ||
set next(value: DoublyLinkedListNode<E> | undefined) { | ||
this._next = value; | ||
} | ||
protected _prev: DoublyLinkedListNode<E> | undefined; | ||
/** | ||
* The `prev` function returns the previous node in a doubly linked list. | ||
* @returns The `prev` property of the `DoublyLinkedListNode` class is being returned. It can either | ||
* be a `DoublyLinkedListNode` object or `undefined`. | ||
*/ | ||
get prev(): DoublyLinkedListNode<E> | undefined { | ||
return this._prev; | ||
} | ||
/** | ||
* The function sets the previous node of a doubly linked list node. | ||
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type | ||
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a | ||
* `DoublyLinkedListNode` object or `undefined` as its value. | ||
*/ | ||
set prev(value: DoublyLinkedListNode<E> | undefined) { | ||
this._prev = value; | ||
} | ||
} | ||
@@ -87,4 +143,4 @@ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* where n is the number of elements in the linked list. | ||
@@ -94,3 +150,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -111,3 +167,3 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -123,8 +179,8 @@ * | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the size of the input array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -195,3 +251,3 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -223,3 +279,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -367,3 +423,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
@@ -374,3 +430,3 @@ * where n is the number of elements in the linked list. | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
@@ -415,3 +471,3 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
@@ -421,3 +477,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
@@ -491,4 +547,9 @@ * | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) or O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -533,2 +594,5 @@ * The `delete` function removes a node from a doubly linked list based on either the node itself or its value. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function checks if a variable has a size greater than zero and returns a boolean value. | ||
@@ -547,2 +611,5 @@ * @returns A boolean value is being returned. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively. | ||
@@ -586,3 +653,3 @@ */ | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
@@ -614,3 +681,3 @@ | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
@@ -698,4 +765,4 @@ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
@@ -733,4 +800,4 @@ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
@@ -800,3 +867,3 @@ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -818,3 +885,3 @@ */ | ||
/** | ||
* Time Complexity: O(n) | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
@@ -821,0 +888,0 @@ */ |
@@ -12,5 +12,2 @@ /** | ||
export class SinglyLinkedListNode<E = any> { | ||
value: E; | ||
next: SinglyLinkedListNode<E> | undefined; | ||
/** | ||
@@ -22,5 +19,44 @@ * The constructor function initializes an instance of a class with a given value and sets the next property to undefined. | ||
constructor(value: E) { | ||
this.value = value; | ||
this.next = undefined; | ||
this._value = value; | ||
this._next = undefined; | ||
} | ||
protected _value: E; | ||
/** | ||
* The function returns the value of a protected variable. | ||
* @returns The value of the variable `_value` is being returned. | ||
*/ | ||
get value(): E { | ||
return this._value; | ||
} | ||
/** | ||
* The above function sets the value of a variable. | ||
* @param {E} value - The parameter "value" is of type E, which means it can be any type. | ||
*/ | ||
set value(value: E) { | ||
this._value = value; | ||
} | ||
protected _next: SinglyLinkedListNode<E> | undefined; | ||
/** | ||
* The `next` function returns the next node in a singly linked list. | ||
* @returns The `next` property is being returned. It can be either a `SinglyLinkedListNode<E>` | ||
* object or `undefined`. | ||
*/ | ||
get next(): SinglyLinkedListNode<E> | undefined { | ||
return this._next; | ||
} | ||
/** | ||
* The "next" property of a SinglyLinkedListNode is set to the provided value. | ||
* @param {SinglyLinkedListNode<E> | undefined} value - The `value` parameter is of type | ||
* `SinglyLinkedListNode<E> | undefined`. This means that it can accept either a | ||
* `SinglyLinkedListNode` object or `undefined` as its value. | ||
*/ | ||
set next(value: SinglyLinkedListNode<E> | undefined) { | ||
this._next = value; | ||
} | ||
} | ||
@@ -733,3 +769,3 @@ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the linked list. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@@ -736,0 +772,0 @@ */ |
@@ -56,3 +56,3 @@ /** | ||
/** | ||
* The function returns the value of the private variable _level. | ||
* The function returns the value of the protected variable _level. | ||
* @returns The level property of the object. | ||
@@ -78,3 +78,3 @@ */ | ||
* The function returns the probability value. | ||
* @returns The probability value stored in the private variable `_probability` is being returned. | ||
* @returns The probability value stored in the protected variable `_probability` is being returned. | ||
*/ | ||
@@ -81,0 +81,0 @@ get probability(): number { |
@@ -56,3 +56,3 @@ /** | ||
/** | ||
* The function returns the value of the private variable _cols. | ||
* The function returns the value of the protected variable _cols. | ||
* @returns The number of columns. | ||
@@ -59,0 +59,0 @@ */ |
@@ -12,2 +12,12 @@ /** | ||
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> { | ||
/** | ||
* The constructor initializes a PriorityQueue with optional elements and options, including a | ||
* comparator function. | ||
* @param elements - The `elements` parameter is an iterable object that contains the initial | ||
* elements to be added to the priority queue. It is optional and defaults to an empty array if not | ||
* provided. | ||
* @param options - The `options` parameter is an object that contains additional configuration | ||
* options for the priority queue. In this case, it has a property called `comparator` which is a | ||
* function used to compare elements in the priority queue. | ||
*/ | ||
constructor( | ||
@@ -14,0 +24,0 @@ elements: Iterable<E> = [], |
@@ -12,2 +12,13 @@ /** | ||
export class MinPriorityQueue<E = any> extends PriorityQueue<E> { | ||
/** | ||
* The constructor initializes a PriorityQueue with optional elements and options, including a | ||
* comparator function. | ||
* @param elements - The `elements` parameter is an iterable object that contains the initial | ||
* elements to be added to the priority queue. It is optional and defaults to an empty array if not | ||
* provided. | ||
* @param options - The `options` parameter is an object that contains additional configuration | ||
* options for the priority queue. In this case, it has a property called `comparator` which is a | ||
* function used to compare elements in the priority queue. The `comparator` function takes two | ||
* parameters `a` and `b`, | ||
*/ | ||
constructor( | ||
@@ -14,0 +25,0 @@ elements: Iterable<E> = [], |
@@ -20,2 +20,10 @@ /** | ||
export class PriorityQueue<E = any> extends Heap<E> { | ||
/** | ||
* The constructor initializes a priority queue with optional elements and options. | ||
* @param elements - The `elements` parameter is an iterable object that contains the initial | ||
* elements to be added to the priority queue. It is an optional parameter and if not provided, the | ||
* priority queue will be initialized as empty. | ||
* @param [options] - The `options` parameter is an optional object that can be used to customize the | ||
* behavior of the priority queue. It can contain the following properties: | ||
*/ | ||
constructor(elements: Iterable<E> = [], options?: PriorityQueueOptions<E>) { | ||
@@ -22,0 +30,0 @@ super(elements, options); |
@@ -20,9 +20,2 @@ /** | ||
export class Deque<E> extends IterableElementBase<E> { | ||
protected _bucketFirst = 0; | ||
protected _firstInBucket = 0; | ||
protected _bucketLast = 0; | ||
protected _lastInBucket = 0; | ||
protected _bucketCount = 0; | ||
protected readonly _bucketSize: number = 1 << 12; | ||
/** | ||
@@ -69,2 +62,4 @@ * The constructor initializes a Deque object with an optional iterable of elements and options. | ||
protected _bucketSize: number = 1 << 12; | ||
/** | ||
@@ -79,2 +74,54 @@ * The bucketSize function returns the size of the bucket. | ||
protected _bucketFirst = 0; | ||
/** | ||
* The function returns the value of the protected variable `_bucketFirst`. | ||
* @returns The value of the `_bucketFirst` property. | ||
*/ | ||
get bucketFirst(): number { | ||
return this._bucketFirst; | ||
} | ||
protected _firstInBucket = 0; | ||
/** | ||
* The function returns the value of the protected variable _firstInBucket. | ||
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type | ||
* `number`. | ||
*/ | ||
get firstInBucket(): number { | ||
return this._firstInBucket; | ||
} | ||
protected _bucketLast = 0; | ||
/** | ||
* The function returns the value of the protected variable `_bucketLast`. | ||
* @returns The value of the `_bucketLast` property, which is a number. | ||
*/ | ||
get bucketLast(): number { | ||
return this._bucketLast; | ||
} | ||
protected _lastInBucket = 0; | ||
/** | ||
* The function returns the value of the protected variable _lastInBucket. | ||
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type | ||
* `number`. | ||
*/ | ||
get lastInBucket(): number { | ||
return this._lastInBucket; | ||
} | ||
protected _bucketCount = 0; | ||
/** | ||
* The function returns the number of buckets. | ||
* @returns The number of buckets. | ||
*/ | ||
get bucketCount(): number { | ||
return this._bucketCount; | ||
} | ||
protected _buckets: E[][] = []; | ||
@@ -84,3 +131,2 @@ | ||
* The buckets function returns the buckets property of the object. | ||
* | ||
* @return The buckets property | ||
@@ -250,6 +296,13 @@ */ | ||
/** | ||
* Time Complexity: O(1) - Removes the last element. | ||
* Space Complexity: O(1) - Operates in-place. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* 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 { | ||
@@ -260,2 +313,10 @@ return this.size === 0; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear() function resets the state of the object by initializing all variables to their default | ||
@@ -414,2 +475,22 @@ * values. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
* | ||
* The `cutRest` function cuts the elements from a specified position in a deque and returns a new | ||
* deque with the cut elements. | ||
* @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It | ||
* is a number that indicates the index of the element in the Deque where the cut should start. | ||
* @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original | ||
* Deque should be modified or a new Deque should be created. If isCutSelf is true, the original | ||
* Deque will be modified by cutting off elements starting from the specified position. If isCutSelf | ||
* is false, a new De | ||
* @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque | ||
* (`newDeque`) depending on the value of the `isCutSelf` parameter. | ||
*/ | ||
cutRest(pos: number, isCutSelf = false): Deque<E> { | ||
@@ -439,3 +520,3 @@ if (isCutSelf) { | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
*/ | ||
@@ -445,3 +526,3 @@ | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* Space Complexity: O(1) or O(n) | ||
* | ||
@@ -748,4 +829,4 @@ * The `deleteAt` function removes an element at a specified position in an array-like data | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(n) - In worst case, resizing doubles the array size. | ||
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n). | ||
* Space Complexity: O(n) - Due to potential resizing. | ||
* | ||
@@ -761,9 +842,9 @@ * The addLast function adds an element to the end of an array. | ||
/** | ||
* Time Complexity: O(1) - Removes the first element. | ||
* Space Complexity: O(1) - In-place operation. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - Removes the last element. | ||
* Space Complexity: O(1) - Operates in-place. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -778,4 +859,9 @@ * The function "pollLast" removes and returns the last element of an array. | ||
/** | ||
* Time Complexity: O(1). | ||
* Space Complexity: O(n) - Due to potential resizing. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -791,4 +877,9 @@ * The "addFirst" function adds an element to the beginning of an array. | ||
/** | ||
* Time Complexity: O(1) - Removes the first element. | ||
* Space Complexity: O(1) - In-place operation. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -806,2 +897,7 @@ * The function "pollFirst" removes and returns the first element of an array. | ||
* Space Complexity: O(1) | ||
* / | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -808,0 +904,0 @@ * The above function is an implementation of the iterator protocol in TypeScript, allowing the |
@@ -47,3 +47,3 @@ /** | ||
* The offset function returns the offset of the current page. | ||
* @return The value of the private variable _offset | ||
* @return The value of the protected variable _offset | ||
*/ | ||
@@ -63,4 +63,9 @@ get offset(): number { | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -76,9 +81,9 @@ * The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
/** | ||
* Time Complexity: O(1) - constant time as it adds an element to the end of the array. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -94,7 +99,10 @@ * The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
/** | ||
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array. | ||
@@ -112,9 +120,9 @@ * @public | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it adds an element to the end of the array. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -131,9 +139,9 @@ * The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -180,9 +188,9 @@ * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -198,9 +206,9 @@ * The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -216,9 +224,9 @@ * The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -233,9 +241,9 @@ * The enqueue function adds a value to the end of a queue. | ||
/** | ||
* Time Complexity: O(n) - same as shift(). | ||
* Space Complexity: O(1) - same as shift(). | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) - same as shift(). | ||
* Space Complexity: O(1) - same as shift(). | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -250,9 +258,9 @@ * The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty. | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -266,9 +274,9 @@ * @param index | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index. | ||
* Space Complexity: O(1) - no additional space is used. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -283,9 +291,9 @@ * The function checks if a data structure is empty by comparing its size to zero. | ||
/** | ||
* Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array. | ||
* Space Complexity: O(n) - where n is the number of elements in the queue. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array. | ||
* Space Complexity: O(n) - where n is the number of elements in the queue. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -300,2 +308,10 @@ * The toArray() function returns an array of elements from the current offset to the end of the _elements array. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the elements array and offset to their initial values. | ||
@@ -362,2 +378,3 @@ */ | ||
*/ | ||
/** | ||
@@ -392,2 +409,8 @@ * Time Complexity: O(n) | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `_getIterator` returns an iterable iterator for the elements in the class. | ||
*/ | ||
protected* _getIterator(): IterableIterator<E> { | ||
@@ -394,0 +417,0 @@ for (const item of this.elements) { |
@@ -44,7 +44,2 @@ /** | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element. | ||
* Space Complexity: O(n), as it creates a new stack with the elements from the input array. | ||
*/ | ||
/** | ||
* The size() function returns the number of elements in an array. | ||
@@ -58,4 +53,9 @@ * @returns The size of the elements array. | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element. | ||
* Space Complexity: O(n), as it creates a new stack with the elements from the input array. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -80,9 +80,9 @@ * The function "fromArray" creates a new Stack object from an array of elements. | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -99,9 +99,9 @@ * The `peek` function returns the last element of an array, or undefined if the array is empty. | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -118,9 +118,9 @@ * The push function adds an element to the stack and returns the updated stack. | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1), as it only involves accessing the last element of the array. | ||
* Space Complexity: O(1), as it does not use any additional space. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
@@ -174,2 +174,10 @@ * The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty. | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function clears the elements array. | ||
@@ -182,9 +190,9 @@ */ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it. | ||
* Space Complexity: O(n), as it creates a new stack. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it. | ||
* Space Complexity: O(n), as it creates a new stack. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
@@ -260,2 +268,10 @@ * The `clone()` function returns a new `Stack` object with the same elements as the original stack. | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* Custom iterator for the Stack class. | ||
@@ -262,0 +278,0 @@ * @returns An iterator object. |
export class TreeNode<V = any> { | ||
key: string; | ||
value?: V | undefined; | ||
children?: TreeNode<V>[] | undefined; | ||
/** | ||
* The constructor function initializes a TreeNode object with a key, optional value, and optional | ||
* children. | ||
* @param {string} key - A string representing the key of the tree node. | ||
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the | ||
* value associated with the node. If no value is provided, it defaults to `undefined`. | ||
* @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>` | ||
* objects. It represents the child nodes of the current node. If no children are provided, the | ||
* default value is an empty array. | ||
*/ | ||
constructor(key: string, value?: V, children?: TreeNode<V>[]) { | ||
this.key = key; | ||
this.value = value || undefined; | ||
this.children = children || []; | ||
this._key = key; | ||
this._value = value || undefined; | ||
this._children = children || []; | ||
} | ||
protected _key: string; | ||
/** | ||
* The function returns the value of the protected variable _key. | ||
* @returns The value of the `_key` property, which is a string. | ||
*/ | ||
get key(): string { | ||
return this._key; | ||
} | ||
/** | ||
* The above function sets the value of a protected variable called "key". | ||
* @param {string} value - The value parameter is a string that represents the value to be assigned | ||
* to the key. | ||
*/ | ||
set key(value: string) { | ||
this._key = value; | ||
} | ||
protected _value?: V | undefined; | ||
/** | ||
* The function returns the value stored in a variable, or undefined if the variable is empty. | ||
* @returns The value of the variable `_value` is being returned. | ||
*/ | ||
get value(): V | undefined { | ||
return this._value; | ||
} | ||
/** | ||
* The function sets the value of a variable. | ||
* @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it | ||
* can accept a value of type "V" or it can be undefined. | ||
*/ | ||
set value(value: V | undefined) { | ||
this._value = value; | ||
} | ||
protected _children?: TreeNode<V>[] | undefined; | ||
/** | ||
* The function returns an array of TreeNode objects or undefined. | ||
* @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`, | ||
* which means it can either be an array of `TreeNode<V>` objects or `undefined`. | ||
*/ | ||
get children(): TreeNode<V>[] | undefined { | ||
return this._children; | ||
} | ||
/** | ||
* The function sets the value of the children property of a TreeNode object. | ||
* @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] | | ||
* undefined. This means that it can accept an array of TreeNode objects or undefined. | ||
*/ | ||
set children(value: TreeNode<V>[] | undefined) { | ||
this._children = value; | ||
} | ||
/** | ||
* The function `addChildren` adds one or more child nodes to the current node. | ||
* @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single | ||
* `TreeNode<V>` object or an array of `TreeNode<V>` objects. | ||
*/ | ||
addChildren(children: TreeNode<V> | TreeNode<V>[]) { | ||
if (!this.children) { | ||
this.children = []; | ||
if (!this._children) { | ||
this._children = []; | ||
} | ||
if (children instanceof TreeNode) { | ||
this.children.push(children); | ||
this._children.push(children); | ||
} else { | ||
this.children = this.children.concat(children); | ||
this._children = this._children.concat(children); | ||
} | ||
} | ||
/** | ||
* The function `getHeight()` calculates the maximum depth of a tree structure by performing a | ||
* breadth-first search. | ||
* @returns the maximum depth or height of the tree. | ||
*/ | ||
getHeight() { | ||
@@ -30,6 +104,6 @@ let maxDepth = 0; | ||
} | ||
const { children } = node; | ||
if (children) { | ||
for (let i = 0, len = children.length; i < len; i++) { | ||
bfs(children[i], level + 1); | ||
const { _children } = node; | ||
if (_children) { | ||
for (let i = 0, len = _children.length; i < len; i++) { | ||
bfs(_children[i], level + 1); | ||
} | ||
@@ -36,0 +110,0 @@ } |
@@ -16,11 +16,67 @@ /** | ||
export class TrieNode { | ||
key: string; | ||
children: Map<string, TrieNode>; | ||
isEnd: boolean; | ||
constructor(key: string) { | ||
this.key = key; | ||
this.isEnd = false; | ||
this.children = new Map<string, TrieNode>(); | ||
this._key = key; | ||
this._isEnd = false; | ||
this._children = new Map<string, TrieNode>(); | ||
} | ||
protected _key: string; | ||
/** | ||
* The function returns the value of the protected variable _key. | ||
* @returns The value of the `_key` property, which is a string. | ||
*/ | ||
get key(): string { | ||
return this._key; | ||
} | ||
/** | ||
* The above function sets the value of a protected variable called "key". | ||
* @param {string} value - The value parameter is a string that represents the value to be assigned | ||
* to the key. | ||
*/ | ||
set key(value: string) { | ||
this._key = value; | ||
} | ||
protected _children: Map<string, TrieNode>; | ||
/** | ||
* The function returns the children of a TrieNode as a Map. | ||
* @returns The `children` property of the TrieNode object, which is a Map containing string keys and | ||
* TrieNode values. | ||
*/ | ||
get children(): Map<string, TrieNode> { | ||
return this._children; | ||
} | ||
/** | ||
* The function sets the value of the `_children` property of a TrieNode object. | ||
* @param value - The value parameter is a Map object that represents the children of a TrieNode. The | ||
* keys of the map are strings, which represent the characters that are associated with each child | ||
* TrieNode. The values of the map are TrieNode objects, which represent the child nodes of the | ||
* current TrieNode. | ||
*/ | ||
set children(value: Map<string, TrieNode>) { | ||
this._children = value; | ||
} | ||
protected _isEnd: boolean; | ||
/** | ||
* The function returns a boolean value indicating whether a certain condition is met. | ||
* @returns The method is returning a boolean value, specifically the value of the variable `_isEnd`. | ||
*/ | ||
get isEnd(): boolean { | ||
return this._isEnd; | ||
} | ||
/** | ||
* The function sets the value of the "_isEnd" property. | ||
* @param {boolean} value - The value parameter is a boolean value that indicates whether the current | ||
* state is the end state or not. | ||
*/ | ||
set isEnd(value: boolean) { | ||
this._isEnd = value; | ||
} | ||
} | ||
@@ -72,5 +128,4 @@ | ||
/** | ||
* The caseSensitive function is a getter that returns the value of the private _caseSensitive property. | ||
* | ||
* @return The value of the _casesensitive private variable | ||
* The caseSensitive function is a getter that returns the value of the protected _caseSensitive property. | ||
* @return The value of the _caseSensitive protected variable | ||
*/ | ||
@@ -92,9 +147,9 @@ get caseSensitive(): boolean { | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being added. | ||
* Space Complexity: O(M) - Each character in the word adds a TrieNode. | ||
* Time Complexity: O(l), where l is the length of the word being added. | ||
* Space Complexity: O(l) - Each character in the word adds a TrieNode. | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being added. | ||
* Space Complexity: O(M) - Each character in the word adds a TrieNode. | ||
* Time Complexity: O(l), where l is the length of the word being added. | ||
* Space Complexity: O(l) - Each character in the word adds a TrieNode. | ||
* | ||
@@ -126,3 +181,3 @@ * Add a word to the Trie structure. | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input word. | ||
* Time Complexity: O(l), where l is the length of the input word. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -132,3 +187,3 @@ */ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input word. | ||
* Time Complexity: O(l), where l is the length of the input word. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -152,2 +207,10 @@ * | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The isEmpty function checks if the size of the queue is 0. | ||
@@ -161,10 +224,26 @@ * @return True if the size of the queue is 0 | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being deleted. | ||
* Space Complexity: O(M) - Due to the recursive DFS approach. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the word being deleted. | ||
* Space Complexity: O(M) - Due to the recursive DFS approach. | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The clear function resets the size of the Trie to 0 and creates a new root TrieNode. | ||
*/ | ||
clear(): void { | ||
this._size = 0; | ||
this._root = new TrieNode(''); | ||
} | ||
/** | ||
* Time Complexity: O(l), where l is the length of the word being deleted. | ||
* Space Complexity: O(n) - Due to the recursive DFS approach. | ||
*/ | ||
/** | ||
* Time Complexity: O(l), where l is the length of the word being deleted. | ||
* Space Complexity: O(n) - Due to the recursive DFS approach. | ||
* | ||
* Remove a word from the Trie structure. | ||
@@ -211,3 +290,3 @@ * @param{string} word - The word to delete. | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -217,3 +296,3 @@ */ | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -243,3 +322,3 @@ * | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -249,3 +328,3 @@ */ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -269,3 +348,3 @@ * | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -275,3 +354,3 @@ */ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(l), where l is the length of the input prefix. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -295,9 +374,9 @@ * | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the input prefix. | ||
*/ | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the input prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the input prefix. | ||
* | ||
@@ -323,9 +402,9 @@ * Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie. | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the longest common prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the longest common prefix. | ||
*/ | ||
/** | ||
* Time Complexity: O(N), where N is the total number of nodes in the trie. | ||
* Space Complexity: O(M), where M is the length of the longest common prefix. | ||
* Time Complexity: O(n), where n is the total number of nodes in the trie. | ||
* Space Complexity: O(l), where l is the length of the longest common prefix. | ||
* | ||
@@ -348,9 +427,9 @@ * Get the longest common prefix among all the words stored in the Trie. | ||
/** | ||
* Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words. | ||
* Space Complexity: O(K * L) - The space required for the output array. | ||
* Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words. | ||
* Space Complexity: O(w * l) - The space required for the output array. | ||
*/ | ||
/** | ||
* Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words. | ||
* Space Complexity: O(K * L) - The space required for the output array. | ||
* Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words. | ||
* Space Complexity: O(w * l) - The space required for the output array. | ||
* | ||
@@ -473,2 +552,14 @@ * The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix. | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
*/ | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
* | ||
* The function `_getIterator` returns an iterable iterator that performs a depth-first search on a | ||
* trie data structure and yields all the paths to the end nodes. | ||
*/ | ||
protected* _getIterator(): IterableIterator<string> { | ||
@@ -488,3 +579,3 @@ function* _dfs(node: TrieNode, path: string): IterableIterator<string> { | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input string. | ||
* Time Complexity: O(l), where l is the length of the input string. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -494,3 +585,3 @@ */ | ||
/** | ||
* Time Complexity: O(M), where M is the length of the input string. | ||
* Time Complexity: O(l), where l is the length of the input string. | ||
* Space Complexity: O(1) - Constant space. | ||
@@ -497,0 +588,0 @@ * |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
2081574
39373
+ Addeddata-structure-typed@1.54.0(transitive)
- Removeddata-structure-typed@1.53.7(transitive)
Updateddata-structure-typed@^1.50.3