Refactor: Organize the access permissions of all member variables. Docs: Partial documentation for time and space complexity.

This commit is contained in:
Revone 2024-01-05 15:37:28 +08:00
parent cd2db0b45e
commit ba16f311c3
23 changed files with 1234 additions and 329 deletions

View file

@ -8,10 +8,11 @@ All notable changes to this project will be documented in this file.
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
## [v1.50.1](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
## [v1.50.2](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
### Changes
- Tests for DirectedGraph.tarjan [`#69`](https://github.com/zrwusa/data-structure-typed/pull/69)
- Rbtree [`#31`](https://github.com/zrwusa/data-structure-typed/pull/31)
- [graph test] edge cases enriched [`#30`](https://github.com/zrwusa/data-structure-typed/pull/30)
- [graph] Modify the data structure design of the graph to change the g… [`#29`](https://github.com/zrwusa/data-structure-typed/pull/29)

View file

@ -22,11 +22,36 @@ export class AVLTreeNode<
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) {
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;
}
}
@ -488,6 +513,15 @@ export class AVLTree<
}
}
/**
* 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 {
newNode.height = oldNode.height;

View file

@ -27,26 +27,48 @@ export 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> {
return this._freqMap;
}
protected _msb: number;
/**
* The function returns the value of the _msb property.
* @returns The `_msb` property of the object.
*/
get msb(): number {
return this._msb;
}
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 {
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 {
return this._freq;
}
/**
* The above function returns the maximum value.
* @returns The maximum value stored in the variable `_max`.
*/
get max(): number {
return this._max;
}

View file

@ -44,7 +44,7 @@ export class BinaryTreeNode<
/**
* The constructor function initializes an object with a key and an optional value.
* @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
* value associated with the key in the constructor.
*/

View file

@ -34,10 +34,19 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
protected override _left?: NODE;
/**
* 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 {
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) {
if (v) {
v.parent = this as unknown as NODE;
@ -47,10 +56,20 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
protected override _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`.
*/
override get right(): NODE | undefined {
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) {
if (v) {
v.parent = this as unknown as NODE;
@ -77,10 +96,10 @@ export class BST<
extends BinaryTree<K, V, NODE, TREE>
implements IBinaryTree<K, V, NODE, TREE> {
/**
* 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
* configuration options for the binary search tree. It can have the following properties:
*/
@ -99,23 +118,31 @@ export class BST<
protected override _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`.
*/
override get root(): NODE | undefined {
return this._root;
}
protected _variant = BSTVariant.STANDARD;
/**
* The function returns the value of the _variant property.
* @returns The value of the `_variant` property.
*/
get variant() {
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.
*/
override createNode(key: K, value?: V): NODE {
return new BSTNode<K, V, NODE>(key, value) as NODE;
@ -124,9 +151,10 @@ export class BST<
/**
* The function creates a new binary search tree with the specified options.
* @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.
*/
override createTree(options?: Partial<BSTOptions<K>>): TREE {
return new BST<K, V, NODE, TREE>([], {
@ -824,6 +852,11 @@ export class BST<
return balanced;
}
/**
* 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) {
if (v) {
v.parent = undefined;

View file

@ -24,11 +24,38 @@ export class RedBlackTreeNode<
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 = 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;
}
}
@ -47,8 +74,6 @@ export class RedBlackTree<
>
extends BST<K, V, NODE, TREE>
implements IBinaryTree<K, V, NODE, TREE> {
Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
/**
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
* initializes the tree with optional nodes and options.
@ -63,18 +88,36 @@ export class RedBlackTree<
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
super([], options);
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 {
return this._root;
}
protected _size: number = 0;
/**
* The function returns the size of an object.
* @returns The size of the object, which is a number.
*/
get size(): number {
return this._size;
}
@ -149,8 +192,14 @@ export class RedBlackTree<
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
}
/**
* 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;
}
@ -176,13 +225,13 @@ export class RedBlackTree<
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
if (newNode === undefined) return false;
newNode.left = this.Sentinel;
newNode.right = this.Sentinel;
newNode.left = this._Sentinel;
newNode.right = this._Sentinel;
let y: NODE | undefined = undefined;
let x: NODE | undefined = this.root;
while (x !== this.Sentinel) {
while (x !== this._Sentinel) {
y = x;
if (x) {
if (newNode.key < x.key) {
@ -250,9 +299,9 @@ export class RedBlackTree<
const ans: BinaryTreeDeleteResult<NODE>[] = [];
if (identifier === null) return ans;
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) {
z = node;
}
@ -264,17 +313,17 @@ export class RedBlackTree<
}
}
if (z === this.Sentinel) {
if (z === this._Sentinel) {
this._size--;
return;
}
y = z;
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;
this._rbTransplant(z, z.left!);
} else {
@ -346,8 +395,14 @@ export class RedBlackTree<
* Space Complexity: O(1)
*/
/**
* 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;
}
@ -379,6 +434,12 @@ export class RedBlackTree<
return y!;
}
/**
* 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) {
if (v) {
v.parent = undefined;
@ -402,7 +463,7 @@ export class RedBlackTree<
if (x.right) {
const y: NODE = x.right;
x.right = y.left;
if (y.left !== this.Sentinel) {
if (y.left !== this._Sentinel) {
if (y.left) y.left.parent = x;
}
y.parent = x.parent;
@ -435,7 +496,7 @@ export class RedBlackTree<
if (x.left) {
const y: NODE = x.left;
x.left = y.right;
if (y.right !== this.Sentinel) {
if (y.right !== this._Sentinel) {
if (y.right) y.right.parent = x;
}
y.parent = x.parent;

View file

@ -9,18 +9,136 @@
import type { SegmentTreeNodeVal } from '../../types';
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;
}
}
@ -51,24 +169,40 @@ export class SegmentTree {
protected _values: number[] = [];
/**
* The function returns an array of numbers.
* @returns An array of numbers is being returned.
*/
get values(): number[] {
return this._values;
}
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;
}
protected _end: number;
/**
* The function returns the value of the protected variable `_end`.
* @returns The value of the protected property `_end`.
*/
get end(): number {
return this._end;
}
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 {
return this._root;
}

View file

@ -23,8 +23,6 @@ export class TreeMultimapNode<
V = any,
NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNodeNested<K, V>
> extends AVLTreeNode<K, V, NODE> {
count: number;
/**
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
@ -39,6 +37,25 @@ export class TreeMultimapNode<
super(key, value);
this.count = 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;
}
}
/**
@ -57,9 +74,14 @@ export class TreeMultimap<
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
}
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 {
let sum = 0;
this.dfs(node => (sum += node.count));
@ -79,6 +101,15 @@ export class TreeMultimap<
return new TreeMultimapNode(key, value, count) as 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`.
*/
override createTree(options?: TreeMultimapOptions<K>): TREE {
return new TreeMultimap<K, V, NODE, TREE>([], {
iterationType: this.iterationType,
@ -375,6 +406,14 @@ export class TreeMultimap<
return 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 {
newNode.count = oldNode.count + newNode.count;
return super._replaceNode(oldNode, newNode);

View file

@ -22,9 +22,6 @@ import { isWeakKey, rangeCheck } from '../../utils';
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
*/
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();
/**
* The constructor function initializes a HashMap object with an optional initial collection and
* options.
@ -48,6 +45,28 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
}
}
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) => {
if (this.isEntry(rawElement)) {
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
@ -67,16 +86,6 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
return this._toEntryFn;
}
/**
* 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;
/**
@ -87,6 +96,18 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
return this._size;
}
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.
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
@ -126,13 +147,13 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
*/
set(key: K, value: V): boolean {
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++;
}
this._store[strKey] = { key, value };
@ -174,7 +195,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
*/
override get(key: K): V | undefined {
if (this._isObjKey(key)) {
return this._objMap.get(key);
return this.objMap.get(key);
} else {
const strKey = this._getNoObjKey(key);
return this._store[strKey]?.value;
@ -189,10 +210,10 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
*/
override has(key: K): boolean {
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;
}
}
@ -205,15 +226,15 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
*/
delete(key: K): boolean {
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--;
return true;
}
@ -221,6 +242,11 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* The clone function creates a new HashMap with the same key-value pairs as
* this one. The clone function is useful for creating a copy of an existing
@ -229,7 +255,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
* @return A new hashmap with the same values as this one
*/
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 });
}
/**
@ -260,11 +286,6 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
return resultMap;
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -309,27 +330,37 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
* object map.
*/
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];
}
}
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) {
const keyType = typeof key;
return (keyType === 'object' || keyType === 'function') && key !== null;
}
/**
* 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 {
const keyType = typeof key;
let strKey: string;
if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
strKey = this._hashFn(key);
strKey = this.hashFn(key);
} else {
if (keyType === 'number') {
// TODO numeric key should has its own hash
@ -348,10 +379,6 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
*/
export class LinkedHashMap<K = any, V = any, 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>;
/**
@ -386,6 +413,69 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
}
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) => {
if (this.isEntry(rawElement)) {
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
@ -415,6 +505,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return this._size;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -425,9 +520,14 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
*/
get first() {
if (this._size === 0) return;
return <[K, V]>[this._head.key, this._head.value];
return <[K, V]>[this.head.key, this.head.value];
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -438,14 +538,14 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
*/
get last() {
if (this._size === 0) return;
return <[K, V]>[this._tail.key, this._tail.value];
return <[K, V]>[this.tail.key, this.tail.value];
}
/**
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
*/
* begin() {
let node = this._head;
let node = this.head;
while (node !== this._sentinel) {
yield [node.key, node.value];
node = node.next;
@ -457,13 +557,18 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
* key and value.
*/
* reverseBegin() {
let node = this._tail;
let node = this.tail;
while (node !== this._sentinel) {
yield [node.key, node.value];
node = node.prev;
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -481,23 +586,23 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
const isNewKey = !this.has(key); // Check if the key is new
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) {
// Update the value of an existing node
node.value = value;
}
} 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) {
// Update the value of an existing node
node.value = value;
@ -510,8 +615,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
this._head = node;
this._sentinel.next = 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
}
this._tail = node;
this._sentinel.prev = node;
@ -546,14 +651,19 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
*/
override has(key: K): boolean {
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;
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -569,18 +679,23 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
*/
override get(key: K): V | undefined {
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;
}
}
/**
* 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)
*
* The function `at` retrieves the key-value pair at a specified index in a linked list.
@ -592,7 +707,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
*/
at(index: number): V | undefined {
rangeCheck(index, 0, this._size - 1);
let node = this._head;
let node = this.head;
while (index--) {
node = node.next;
}
@ -600,6 +715,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
* /
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
@ -613,27 +733,27 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
let node;
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);
if (!node) {
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) {
return false; // If the node does not exist, return false
}
// Remove nodes from orgMap
delete this._noObjMap[hash];
delete this.noObjMap[hash];
}
// Remove node from doubly linked list
@ -642,6 +762,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
* /
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*
@ -652,7 +777,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
*/
deleteAt(index: number): boolean {
rangeCheck(index, 0, this._size - 1);
let node = this._head;
let node = this.head;
while (index--) {
node = node.next;
}
@ -660,6 +785,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
* /
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
@ -682,6 +812,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
* /
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
@ -708,7 +843,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
* of the original `LinkedHashMap` object.
*/
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) {
const [key, value] = entry;
cloned.set(key, value);
@ -717,6 +852,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* /
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
@ -744,6 +884,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* /
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
@ -791,11 +936,13 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
return this.set(key, value);
}
protected _hashFn: (key: K) => string = (key: K) => String(key);
protected _objHashFn: (key: K) => object = (key: K) => <object>key;
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
* 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.
@ -803,13 +950,18 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
* 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) {
yield [node.key, node.value] as [K, V];
node = node.next;
}
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -825,11 +977,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
prev.next = next;
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;
}

View file

@ -43,7 +43,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
for (const el of elements) {
this.add(el);
}
// this.fix();
}
}
@ -140,6 +139,11 @@ export class Heap<E = any> extends IterableElementBase<E> {
return value;
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@ -167,12 +171,12 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* Clear and add elements of the heap
@ -184,12 +188,12 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* Use a comparison function to check whether a binary heap contains a specific element.
@ -201,12 +205,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `delete` function removes an element from an array-like data structure, maintaining the order
@ -232,13 +237,14 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* Depth-first search (DFS) method, different traversal orders can be selected
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
@ -330,13 +336,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* Fix the entire heap to maintain heap properties.
*/
@ -424,7 +430,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n)
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
@ -448,6 +454,11 @@ export class Heap<E = any> extends IterableElementBase<E> {
return true;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
@ -683,12 +694,12 @@ export class FibonacciHeap<E> {
}
/**
* Time Complexity: O(log n), where n is the number of elements in the heap.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n), where n is the number of elements in the heap.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Remove and return the top element (smallest or largest element) from the heap.
@ -699,12 +710,12 @@ export class FibonacciHeap<E> {
}
/**
* Time Complexity: O(log n), where n is the number of elements in the heap.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n), where n is the number of elements in the heap.
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Remove and return the top element (smallest or largest element) from the heap.

View file

@ -9,19 +9,75 @@ import type { ElementCallback } from '../../types';
import { IterableElementBase } from '../base';
export class DoublyLinkedListNode<E = any> {
value: E;
next: DoublyLinkedListNode<E> | undefined;
prev: DoublyLinkedListNode<E> | undefined;
/**
* The constructor function initializes the value, next, and previous properties of an object.
* @param {E} value - The "value" parameter is the value that will be stored in the node. It can be of any data type, as it
* is defined as a generic type "E".
*/
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;
}
}
@ -82,13 +138,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
@ -104,7 +160,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
*/
/**
* Time Complexity: O(n)
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
@ -115,12 +171,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
@ -186,7 +242,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n)
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
@ -213,7 +269,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n)
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
@ -356,13 +412,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n)
* Time Complexity: O(1) or O(n)
* Space Complexity: O(1)
* 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)
*
* The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
@ -402,12 +458,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
@ -476,7 +532,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
@ -517,6 +578,9 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
*/
/**
* 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.
* @returns A boolean value is being returned.
*/
@ -530,6 +594,9 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
*/
/**
* 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.
*/
clear(): void {
@ -568,7 +635,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* Space Complexity: O(1)
*/
/**
@ -595,7 +662,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
* Space Complexity: O(1)
*/
/**
@ -678,8 +745,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
@ -712,8 +779,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
@ -778,7 +845,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n)
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
@ -795,7 +862,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n)
* Time Complexity: O(1)
* Space Complexity: O(1)
*/

View file

@ -9,17 +9,53 @@ import type { ElementCallback } from '../../types';
import { IterableElementBase } from '../base';
export class SinglyLinkedListNode<E = any> {
value: E;
next: SinglyLinkedListNode<E> | undefined;
/**
* The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
* will be stored in the node of a linked list.
*/
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;
}
}
@ -728,7 +764,7 @@ export 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)
*/
/**

View file

@ -53,7 +53,7 @@ export class SkipList<K, V> {
protected _level: number = 0;
/**
* 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.
*/
get level(): number {
@ -74,7 +74,7 @@ export class SkipList<K, V> {
/**
* 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.
*/
get probability(): number {
return this._probability;

View file

@ -53,7 +53,7 @@ export class Matrix {
protected _cols: number = 0;
/**
* 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.
*/
get cols(): number {

View file

@ -9,6 +9,16 @@ import type { PriorityQueueOptions } from '../../types';
import { PriorityQueue } from './priority-queue';
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(
elements: Iterable<E> = [],
options: PriorityQueueOptions<E> = {

View file

@ -9,6 +9,17 @@ import type { PriorityQueueOptions } from '../../types';
import { PriorityQueue } from './priority-queue';
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(
elements: Iterable<E> = [],
options: PriorityQueueOptions<E> = {

View file

@ -17,6 +17,14 @@ import { Heap } from '../heap';
* 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
*/
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>) {
super(elements, options);
}

View file

@ -17,13 +17,6 @@ import { calcMinUnitsRequired, rangeCheck } from '../../utils';
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
*/
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;
/**
* The constructor initializes a Deque object with an optional iterable of elements and options.
* @param elements - An iterable object (such as an array or a Set) that contains the initial
@ -65,6 +58,8 @@ export class Deque<E> extends IterableElementBase<E> {
}
}
protected _bucketSize: number = 1 << 12;
/**
* The bucketSize function returns the size of the bucket.
*
@ -74,11 +69,62 @@ export class Deque<E> extends IterableElementBase<E> {
return this._bucketSize;
}
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[][] = [];
/**
* The buckets function returns the buckets property of the object.
*
* @return The buckets property
*/
get buckets() {
@ -243,15 +289,30 @@ export class Deque<E> extends IterableElementBase<E> {
}
/**
* 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 {
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
* values.
*/
@ -405,6 +466,26 @@ export class Deque<E> extends IterableElementBase<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 = false): Deque<E> {
if (isCutSelf) {
if (pos < 0) {
@ -429,12 +510,12 @@ export class Deque<E> extends IterableElementBase<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)
*
* The `deleteAt` function removes an element at a specified position in an array-like data
* structure.
@ -736,8 +817,8 @@ export class Deque<E> extends IterableElementBase<E> {
*/
/**
* 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.
*
* The addLast function adds an element to the end of an array.
* @param {E} element - The element parameter represents the element that you want to add to the end of the
@ -748,13 +829,13 @@ export class Deque<E> extends IterableElementBase<E> {
}
/**
* 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)
*
* The function "pollLast" removes and returns the last element of an array.
* @returns The last element of the array is being returned.
@ -764,8 +845,13 @@ export class Deque<E> extends IterableElementBase<E> {
}
/**
* 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)
*
* The "addFirst" function adds an element to the beginning of an array.
* @param {E} element - The parameter "element" represents the element that you want to add to the
@ -776,8 +862,13 @@ export class Deque<E> extends IterableElementBase<E> {
}
/**
* 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)
*
* The function "pollFirst" removes and returns the first element of an array.
* @returns The method `pollFirst()` is returning the first element of the array after removing it
@ -788,6 +879,11 @@ export class Deque<E> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
* /
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*

View file

@ -44,7 +44,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
/**
* 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
*/
get offset(): number {
return this._offset;
@ -59,8 +59,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
@ -71,13 +76,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the
@ -88,11 +93,14 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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.
* @public
* @static
@ -105,13 +113,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* 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.
* @param {E} element - The `element` parameter represents the element that you want to add to the queue.
@ -123,13 +131,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
* necessary to optimize performance.
@ -171,13 +179,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
@ -188,13 +196,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
@ -205,13 +213,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The enqueue function adds a value to the end of a queue.
* @param {E} value - The value parameter represents the value that you want to add to the queue.
@ -221,13 +229,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
* @returns The method is returning a value of type E or undefined.
@ -237,13 +245,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* @param index
*/
@ -252,13 +260,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The function checks if a data structure is empty by comparing its size to zero.
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
@ -268,13 +276,13 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The toArray() function returns an array of elements from the current offset to the end of the _elements array.
* @returns An array of type E is being returned.
@ -284,6 +292,14 @@ export class Queue<E = any> extends IterableElementBase<E> {
}
/**
* 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.
*/
clear(): void {
@ -345,6 +361,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
@ -374,6 +391,12 @@ export class Queue<E = any> extends IterableElementBase<E> {
* Space 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> {
for (const item of this.elements) {
yield item;

View file

@ -40,11 +40,6 @@ export class Stack<E = any> extends IterableElementBase<E> {
return this._elements;
}
/**
* 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.
* @returns The size of the elements array.
@ -54,8 +49,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The function "fromArray" creates a new Stack object from an array of elements.
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
@ -75,13 +75,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `peek` function returns the last element of an array, or undefined if the array is empty.
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
@ -93,13 +93,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The push function adds an element to the stack and returns the updated stack.
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
@ -111,13 +111,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
@ -166,6 +166,14 @@ export class Stack<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The clear function clears the elements array.
*/
clear(): void {
@ -173,13 +181,13 @@ export class Stack<E = any> extends IterableElementBase<E> {
}
/**
* 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)
*
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
@ -250,6 +258,14 @@ export class Stack<E = any> extends IterableElementBase<E> {
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* Custom iterator for the Stack class.
* @returns An iterator object.
*/

View file

@ -1,25 +1,99 @@
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 || [];
}
private _key: string;
/**
* The function returns the value of the private 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 private 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;
}
private _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;
}
private _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() {
let maxDepth = 0;
if (this) {
@ -27,10 +101,10 @@ export class TreeNode<V = any> {
if (level > maxDepth) {
maxDepth = level;
}
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);
}
}
};

View file

@ -13,14 +13,70 @@ import { IterableElementBase } from '../base';
* and a flag indicating whether it's the end of a word.
*/
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 private 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 private 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;
}
}
@ -68,9 +124,8 @@ export class Trie extends IterableElementBase<string, Trie> {
protected _caseSensitive: boolean = true;
/**
* The caseSensitive function is a getter that returns the value of the private _caseSensitive property.
*
* @return The value of the _casesensitive private variable
* The caseSensitive function is a getter that returns the value of the protected _caseSensitive property.
* @return The value of the _caseSensitive protected variable
*/
get caseSensitive(): boolean {
return this._caseSensitive;
@ -87,13 +142,13 @@ export class Trie extends IterableElementBase<string, Trie> {
}
/**
* 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.
*
* Add a word to the Trie structure.
* @param {string} word - The word to add.
@ -120,12 +175,12 @@ export class Trie extends IterableElementBase<string, Trie> {
}
/**
* 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.
*
* Check if the Trie contains a given word.
@ -144,6 +199,14 @@ export class Trie extends IterableElementBase<string, Trie> {
}
/**
* 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.
* @return True if the size of the queue is 0
*/
@ -152,13 +215,13 @@ export class Trie extends IterableElementBase<string, Trie> {
}
/**
* 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(l), where l is the length of the word being deleted.
* Space Complexity: O(n) - Due to the recursive DFS approach.
*/
/**
* 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(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.
* @param{string} word - The word to delete.
@ -201,12 +264,12 @@ export class Trie extends IterableElementBase<string, Trie> {
}
/**
* 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.
*
*/
@ -231,12 +294,12 @@ export class Trie extends IterableElementBase<string, Trie> {
}
/**
* 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.
*
* Check if a given input string has an absolute prefix in the Trie, meaning it's not a complete word.
@ -255,12 +318,12 @@ export class Trie extends IterableElementBase<string, Trie> {
}
/**
* 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.
*
* Check if a given input string is a prefix of any existing word in the Trie, whether as an absolute prefix or a complete word.
@ -279,13 +342,13 @@ export class Trie extends IterableElementBase<string, 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 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.
*
* Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie.
* @param {string} input - The input string representing the common prefix to check for.
@ -306,13 +369,13 @@ export class Trie extends IterableElementBase<string, 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.
*
* Get the longest common prefix among all the words stored in the Trie.
* @returns {string} The longest common prefix found in the Trie.
@ -330,13 +393,13 @@ export class Trie extends IterableElementBase<string, 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.
*
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
@ -454,6 +517,18 @@ export class Trie extends IterableElementBase<string, Trie> {
return newTrie;
}
/**
* 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> {
function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
if (node.isEnd) {
@ -468,12 +543,12 @@ export class Trie extends IterableElementBase<string, Trie> {
}
/**
* 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.
*
* @param str

View file

@ -104,6 +104,8 @@ describe('Queue', () => {
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
queue.delete('5');
expect([...queue]).toEqual(['1', '6', '0', '9']);
queue.deleteAt(2);
expect([...queue]).toEqual(['1', '6', '9']);
expect([...cloned]).toEqual(['1', '6', '0', '5', '9']);
});
});