mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
[core] Rename all 'val' variables to 'value'.
This commit is contained in:
parent
ce35f0421a
commit
121516f303
|
@ -13,8 +13,8 @@ import {IBinaryTree} from '../../interfaces';
|
|||
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
||||
height: number;
|
||||
|
||||
constructor(key: BTNKey, val?: V) {
|
||||
super(key, val);
|
||||
constructor(key: BTNKey, value?: V) {
|
||||
super(key, value);
|
||||
this.height = 0;
|
||||
}
|
||||
}
|
||||
|
@ -37,13 +37,13 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|||
* The function creates a new AVL tree node with the specified key and value.
|
||||
* @param {BTNKey} 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 [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
|
||||
* type `V`, which means it can be any value that is assignable to the `val` property of the
|
||||
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
||||
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
||||
* node type `N`.
|
||||
* @returns a new AVLTreeNode object with the specified key and value.
|
||||
*/
|
||||
override createNode(key: BTNKey, val?: V): N {
|
||||
return new AVLTreeNode<V, N>(key, val) as N;
|
||||
override createNode(key: BTNKey, value?: V): N {
|
||||
return new AVLTreeNode<V, N>(key, value) as N;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,13 +51,13 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|||
* a new node.
|
||||
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
||||
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
||||
* @param [val] - The `val` parameter is the value that you want to assign to the new node that you
|
||||
* @param [value] - The `value` parameter is the value that you want to assign to the new node that you
|
||||
* are adding to the binary search tree.
|
||||
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
||||
*/
|
||||
override add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
|
||||
override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
||||
// TODO support node as a param
|
||||
const inserted = super.add(keyOrNode, val);
|
||||
const inserted = super.add(keyOrNode, value);
|
||||
if (inserted) this._balancePath(inserted);
|
||||
return inserted;
|
||||
}
|
||||
|
@ -97,18 +97,18 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|||
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
||||
*/
|
||||
protected override _swap(srcNode: N, destNode: N): N {
|
||||
const {key, val, height} = destNode;
|
||||
const tempNode = this.createNode(key, val);
|
||||
const {key, value, height} = destNode;
|
||||
const tempNode = this.createNode(key, value);
|
||||
|
||||
if (tempNode) {
|
||||
tempNode.height = height;
|
||||
|
||||
destNode.key = srcNode.key;
|
||||
destNode.val = srcNode.val;
|
||||
destNode.value = srcNode.value;
|
||||
destNode.height = srcNode.height;
|
||||
|
||||
srcNode.key = tempNode.key;
|
||||
srcNode.val = tempNode.val;
|
||||
srcNode.value = tempNode.value;
|
||||
srcNode.height = tempNode.height;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|||
/**
|
||||
* The value stored in the node.
|
||||
*/
|
||||
val: V | undefined;
|
||||
value: V | undefined;
|
||||
|
||||
/**
|
||||
* The parent node of the current node.
|
||||
|
@ -36,11 +36,11 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|||
/**
|
||||
* Creates a new instance of BinaryTreeNode.
|
||||
* @param {BTNKey} key - The key associated with the node.
|
||||
* @param {V} val - The value stored in the node.
|
||||
* @param {V} value - The value stored in the node.
|
||||
*/
|
||||
constructor(key: BTNKey, val?: V) {
|
||||
constructor(key: BTNKey, value?: V) {
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
private _left: N | null | undefined;
|
||||
|
@ -159,11 +159,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
/**
|
||||
* Creates a new instance of BinaryTreeNode with the given key and value.
|
||||
* @param {BTNKey} key - The key for the new node.
|
||||
* @param {V} val - The value for the new node.
|
||||
* @param {V} value - The value for the new node.
|
||||
* @returns {N} - The newly created BinaryTreeNode.
|
||||
*/
|
||||
createNode(key: BTNKey, val?: V): N {
|
||||
return new BinaryTreeNode<V, N>(key, val) as N;
|
||||
createNode(key: BTNKey, value?: V): N {
|
||||
return new BinaryTreeNode<V, N>(key, value) as N;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -185,10 +185,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
/**
|
||||
* Add a node with the given key and value to the binary tree.
|
||||
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
||||
* @param {V} val - The value for the new node (optional).
|
||||
* @param {V} value - The value for the new node (optional).
|
||||
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
||||
*/
|
||||
add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
|
||||
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
||||
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
||||
const queue = new Queue<N | null>([root]);
|
||||
while (queue.size > 0) {
|
||||
|
@ -209,7 +209,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
if (keyOrNode === null) {
|
||||
needInsert = null;
|
||||
} else if (typeof keyOrNode === 'number') {
|
||||
needInsert = this.createNode(keyOrNode, val);
|
||||
needInsert = this.createNode(keyOrNode, value);
|
||||
} else if (keyOrNode instanceof BinaryTreeNode) {
|
||||
needInsert = keyOrNode;
|
||||
} else {
|
||||
|
@ -221,7 +221,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
|
||||
if (this.root) {
|
||||
if (existNode) {
|
||||
existNode.val = val;
|
||||
existNode.value = value;
|
||||
inserted = existNode;
|
||||
} else {
|
||||
inserted = _bfs(this.root, needInsert);
|
||||
|
@ -252,15 +252,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
// TODO not sure addMany not be run multi times
|
||||
return keysOrNodes.map((keyOrNode, i) => {
|
||||
if (keyOrNode instanceof BinaryTreeNode) {
|
||||
return this.add(keyOrNode.key, keyOrNode.val);
|
||||
return this.add(keyOrNode.key, keyOrNode.value);
|
||||
}
|
||||
|
||||
if (keyOrNode === null) {
|
||||
return this.add(null);
|
||||
}
|
||||
|
||||
const val = values?.[i];
|
||||
return this.add(keyOrNode, val);
|
||||
const value = values?.[i];
|
||||
return this.add(keyOrNode, value);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1212,15 +1212,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
* @returns {N} - The destination node after the swap.
|
||||
*/
|
||||
protected _swap(srcNode: N, destNode: N): N {
|
||||
const {key, val} = destNode;
|
||||
const tempNode = this.createNode(key, val);
|
||||
const {key, value} = destNode;
|
||||
const tempNode = this.createNode(key, value);
|
||||
|
||||
if (tempNode) {
|
||||
destNode.key = srcNode.key;
|
||||
destNode.val = srcNode.val;
|
||||
destNode.value = srcNode.value;
|
||||
|
||||
srcNode.key = tempNode.key;
|
||||
srcNode.val = tempNode.val;
|
||||
srcNode.value = tempNode.value;
|
||||
}
|
||||
|
||||
return destNode;
|
||||
|
|
|
@ -12,8 +12,8 @@ import {IBinaryTree} from '../../interfaces';
|
|||
import {Queue} from '../queue';
|
||||
|
||||
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
||||
constructor(key: BTNKey, val?: V) {
|
||||
super(key, val);
|
||||
constructor(key: BTNKey, value?: V) {
|
||||
super(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,12 +41,12 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
* The function creates a new binary search tree node with the given key and value.
|
||||
* @param {BTNKey} 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 [val] - The parameter `val` is an optional value that can be assigned to the node. It
|
||||
* @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.
|
||||
*/
|
||||
override createNode(key: BTNKey, val?: V): N {
|
||||
return new BSTNode<V, N>(key, val) as N;
|
||||
override createNode(key: BTNKey, value?: V): N {
|
||||
return new BSTNode<V, N>(key, value) as N;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -54,19 +54,19 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
* into the tree.
|
||||
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
||||
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
||||
* @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
|
||||
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
||||
* binary search tree.
|
||||
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
||||
* was not added or if the parameters were invalid, it returns null or undefined.
|
||||
*/
|
||||
override add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
|
||||
override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
||||
// TODO support node as a parameter
|
||||
let inserted: N | null = null;
|
||||
let newNode: N | null = null;
|
||||
if (keyOrNode instanceof BSTNode) {
|
||||
newNode = keyOrNode;
|
||||
} else if (typeof keyOrNode === 'number') {
|
||||
newNode = this.createNode(keyOrNode, val);
|
||||
newNode = this.createNode(keyOrNode, value);
|
||||
} else if (keyOrNode === null) {
|
||||
newNode = null;
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
if (cur !== null && newNode !== null) {
|
||||
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
||||
if (newNode) {
|
||||
cur.val = newNode.val;
|
||||
cur.value = newNode.value;
|
||||
}
|
||||
//Duplicates are not accepted.
|
||||
traversing = false;
|
||||
|
@ -128,7 +128,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
/**
|
||||
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
||||
* maintaining balance.
|
||||
* @param {[BTNKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
||||
* @param {[BTNKey | N, N['value']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
||||
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
||||
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
||||
* `null
|
||||
|
@ -154,15 +154,15 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
return super.addMany(keysOrNodes, data);
|
||||
}
|
||||
const inserted: (N | null | undefined)[] = [];
|
||||
const combinedArr: [BTNKey | N, N['val']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
|
||||
const combinedArr: [BTNKey | N, N['value']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
|
||||
let sorted = [];
|
||||
|
||||
function isNodeOrNullTuple(arr: [BTNKey | N, N['val']][]): arr is [N, N['val']][] {
|
||||
function isNodeOrNullTuple(arr: [BTNKey | N, N['value']][]): arr is [N, N['value']][] {
|
||||
for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N, N['val']][]): arr is [BTNKey, N['val']][] {
|
||||
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N, N['value']][]): arr is [BTNKey, N['value']][] {
|
||||
for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
|
||||
return false;
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
throw new Error('Invalid input keysOrNodes');
|
||||
}
|
||||
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
||||
sortedData = sorted.map(([, val]) => val);
|
||||
sortedData = sorted.map(([, value]) => value);
|
||||
const recursive = (arr: (BTNKey | null | N)[], data?: (V | undefined)[]) => {
|
||||
if (arr.length === 0) return;
|
||||
|
||||
|
@ -426,7 +426,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
if (l > r) return;
|
||||
const m = l + Math.floor((r - l) / 2);
|
||||
const midNode = sorted[m];
|
||||
this.add(midNode.key, midNode.val);
|
||||
this.add(midNode.key, midNode.value);
|
||||
buildBalanceBST(l, m - 1);
|
||||
buildBalanceBST(m + 1, r);
|
||||
};
|
||||
|
@ -442,7 +442,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
if (l <= r) {
|
||||
const m = l + Math.floor((r - l) / 2);
|
||||
const midNode = sorted[m];
|
||||
this.add(midNode.key, midNode.val);
|
||||
this.add(midNode.key, midNode.value);
|
||||
stack.push([m + 1, r]);
|
||||
stack.push([l, m - 1]);
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ import {IBinaryTree} from '../../interfaces';
|
|||
import {BST, BSTNode} from './bst';
|
||||
|
||||
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
||||
constructor(key: BTNKey, val?: V) {
|
||||
super(key, val);
|
||||
constructor(key: BTNKey, value?: V) {
|
||||
super(key, value);
|
||||
this._color = RBColor.RED;
|
||||
}
|
||||
|
||||
|
@ -27,12 +27,12 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|||
super(options);
|
||||
}
|
||||
|
||||
override createNode(key: BTNKey, val?: V): N {
|
||||
return new RBTreeNode(key, val) as N;
|
||||
override createNode(key: BTNKey, value?: V): N {
|
||||
return new RBTreeNode(key, value) as N;
|
||||
}
|
||||
|
||||
// override add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
|
||||
// const inserted = super.add(keyOrNode, val);
|
||||
// override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
||||
// const inserted = super.add(keyOrNode, value);
|
||||
// if (inserted) this._fixInsertViolation(inserted);
|
||||
// return inserted;
|
||||
// }
|
||||
|
|
|
@ -9,11 +9,11 @@
|
|||
import type {SegmentTreeNodeVal} from '../../types';
|
||||
|
||||
export class SegmentTreeNode {
|
||||
constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null) {
|
||||
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | null) {
|
||||
this._start = start;
|
||||
this._end = end;
|
||||
this._sum = sum;
|
||||
this._val = val || null;
|
||||
this._value = value || null;
|
||||
}
|
||||
|
||||
private _start = 0;
|
||||
|
@ -35,14 +35,14 @@ export class SegmentTreeNode {
|
|||
this._end = v;
|
||||
}
|
||||
|
||||
private _val: SegmentTreeNodeVal | null = null;
|
||||
private _value: SegmentTreeNodeVal | null = null;
|
||||
|
||||
get val(): SegmentTreeNodeVal | null {
|
||||
return this._val;
|
||||
get value(): SegmentTreeNodeVal | null {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
set val(v: SegmentTreeNodeVal | null) {
|
||||
this._val = v;
|
||||
set value(v: SegmentTreeNodeVal | null) {
|
||||
this._value = v;
|
||||
}
|
||||
|
||||
private _sum = 0;
|
||||
|
@ -154,30 +154,30 @@ export class SegmentTree {
|
|||
* updated.
|
||||
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
||||
* the `SegmentTreeNode` at the specified `index`.
|
||||
* @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
|
||||
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
||||
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
||||
* cur.val = val;` and pass a value for `val` in the
|
||||
* cur.value = value;` and pass a value for `value` in the
|
||||
* @returns The function does not return anything.
|
||||
*/
|
||||
updateNode(index: number, sum: number, val?: SegmentTreeNodeVal) {
|
||||
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal) {
|
||||
const root = this.root || null;
|
||||
if (!root) {
|
||||
return;
|
||||
}
|
||||
const dfs = (cur: SegmentTreeNode, index: number, sum: number, val?: SegmentTreeNodeVal) => {
|
||||
const dfs = (cur: SegmentTreeNode, index: number, sum: number, value?: SegmentTreeNodeVal) => {
|
||||
if (cur.start === cur.end && cur.start === index) {
|
||||
cur.sum = sum;
|
||||
if (val !== undefined) cur.val = val;
|
||||
if (value !== undefined) cur.value = value;
|
||||
return;
|
||||
}
|
||||
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
||||
if (index <= mid) {
|
||||
if (cur.left) {
|
||||
dfs(cur.left, index, sum, val);
|
||||
dfs(cur.left, index, sum, value);
|
||||
}
|
||||
} else {
|
||||
if (cur.right) {
|
||||
dfs(cur.right, index, sum, val);
|
||||
dfs(cur.right, index, sum, value);
|
||||
}
|
||||
}
|
||||
if (cur.left && cur.right) {
|
||||
|
@ -185,7 +185,7 @@ export class SegmentTree {
|
|||
}
|
||||
};
|
||||
|
||||
dfs(root, index, sum, val);
|
||||
dfs(root, index, sum, value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -20,14 +20,14 @@ export class TreeMultisetNode<
|
|||
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
||||
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
||||
* of the binary tree node.
|
||||
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
||||
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
|
||||
* tree node. If no value is provided, it will be `undefined`.
|
||||
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
||||
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
||||
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
||||
*/
|
||||
constructor(key: BTNKey, val?: V, count = 1) {
|
||||
super(key, val);
|
||||
constructor(key: BTNKey, value?: V, count = 1) {
|
||||
super(key, value);
|
||||
this.count = count;
|
||||
}
|
||||
}
|
||||
|
@ -59,13 +59,13 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|||
* The function creates a new BSTNode with the given key, value, and count.
|
||||
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
||||
* distinguish one node from another in the tree.
|
||||
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
||||
* @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
||||
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
||||
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
||||
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
||||
*/
|
||||
override createNode(key: BTNKey, val?: V, count?: number): N {
|
||||
return new TreeMultisetNode(key, val, count) as N;
|
||||
override createNode(key: BTNKey, value?: V, count?: number): N {
|
||||
return new TreeMultisetNode(key, value, count) as N;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -74,22 +74,22 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|||
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
||||
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
||||
* node to be added), or `null` (which represents a null node).
|
||||
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
||||
* @param [value] - The `value` parameter represents the value associated with the key that is being
|
||||
* added to the binary tree.
|
||||
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
||||
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
||||
* count is specified, the default count will be 1.
|
||||
* @returns The function `add` returns a value of type `N | null | undefined`.
|
||||
*/
|
||||
override add(keyOrNode: BTNKey | N | null, val?: V, count = 1): N | null | undefined {
|
||||
override add(keyOrNode: BTNKey | N | null, value?: V, count = 1): N | null | undefined {
|
||||
let inserted: N | null | undefined = undefined,
|
||||
newNode: N | null;
|
||||
if (keyOrNode instanceof TreeMultisetNode) {
|
||||
newNode = this.createNode(keyOrNode.key, keyOrNode.val, keyOrNode.count);
|
||||
newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
|
||||
} else if (keyOrNode === null) {
|
||||
newNode = null;
|
||||
} else {
|
||||
newNode = this.createNode(keyOrNode, val, count);
|
||||
newNode = this.createNode(keyOrNode, value, count);
|
||||
}
|
||||
if (!this.root) {
|
||||
this._setRoot(newNode);
|
||||
|
@ -103,7 +103,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|||
if (cur) {
|
||||
if (newNode) {
|
||||
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
||||
cur.val = newNode.val;
|
||||
cur.value = newNode.value;
|
||||
cur.count += newNode.count;
|
||||
this._setCount(this.count + newNode.count);
|
||||
traversing = false;
|
||||
|
@ -199,7 +199,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|||
const keyOrNode = keysOrNodes[i];
|
||||
|
||||
if (keyOrNode instanceof TreeMultisetNode) {
|
||||
inserted.push(this.add(keyOrNode.key, keyOrNode.val, keyOrNode.count));
|
||||
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -233,7 +233,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|||
if (l > r) return;
|
||||
const m = l + Math.floor((r - l) / 2);
|
||||
const midNode = sorted[m];
|
||||
this.add(midNode.key, midNode.val, midNode.count);
|
||||
this.add(midNode.key, midNode.value, midNode.count);
|
||||
buildBalanceBST(l, m - 1);
|
||||
buildBalanceBST(m + 1, r);
|
||||
};
|
||||
|
@ -249,7 +249,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|||
if (l <= r) {
|
||||
const m = l + Math.floor((r - l) / 2);
|
||||
const midNode = sorted[m];
|
||||
this.add(midNode.key, midNode.val, midNode.count);
|
||||
this.add(midNode.key, midNode.value, midNode.count);
|
||||
stack.push([m + 1, r]);
|
||||
stack.push([l, m - 1]);
|
||||
}
|
||||
|
@ -351,18 +351,18 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|||
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
||||
*/
|
||||
protected override _swap(srcNode: N, destNode: N): N {
|
||||
const {key, val, count, height} = destNode;
|
||||
const tempNode = this.createNode(key, val, count);
|
||||
const {key, value, count, height} = destNode;
|
||||
const tempNode = this.createNode(key, value, count);
|
||||
if (tempNode) {
|
||||
tempNode.height = height;
|
||||
|
||||
destNode.key = srcNode.key;
|
||||
destNode.val = srcNode.val;
|
||||
destNode.value = srcNode.value;
|
||||
destNode.count = srcNode.count;
|
||||
destNode.height = srcNode.height;
|
||||
|
||||
srcNode.key = tempNode.key;
|
||||
srcNode.val = tempNode.val;
|
||||
srcNode.value = tempNode.value;
|
||||
srcNode.count = tempNode.count;
|
||||
srcNode.height = tempNode.height;
|
||||
}
|
||||
|
|
|
@ -16,12 +16,12 @@ export abstract class AbstractVertex<V = any> {
|
|||
* The function is a protected constructor that takes an key and an optional value as parameters.
|
||||
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
||||
* used to uniquely identify the vertex object.
|
||||
* @param {V} [val] - The parameter "val" is an optional parameter of type V. It is used to assign a value to the
|
||||
* @param {V} [value] - The parameter "value" is an optional parameter of type V. It is used to assign a value to the
|
||||
* vertex. If no value is provided, it will be set to undefined.
|
||||
*/
|
||||
protected constructor(key: VertexKey, val?: V) {
|
||||
protected constructor(key: VertexKey, value?: V) {
|
||||
this._key = key;
|
||||
this._val = val;
|
||||
this._value = value;
|
||||
}
|
||||
|
||||
private _key: VertexKey;
|
||||
|
@ -34,14 +34,14 @@ export abstract class AbstractVertex<V = any> {
|
|||
this._key = v;
|
||||
}
|
||||
|
||||
private _val: V | undefined;
|
||||
private _value: V | undefined;
|
||||
|
||||
get val(): V | undefined {
|
||||
return this._val;
|
||||
get value(): V | undefined {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
set val(value: V | undefined) {
|
||||
this._val = value;
|
||||
set value(value: V | undefined) {
|
||||
this._value = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,23 +52,23 @@ export abstract class AbstractEdge<E = any> {
|
|||
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
||||
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
||||
* will be assigned.
|
||||
* @param {VO} [val] - The `val` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
|
||||
* @param {VO} [value] - The `value` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
|
||||
* meaning it can be omitted when creating an instance of the class.
|
||||
*/
|
||||
protected constructor(weight?: number, val?: E) {
|
||||
protected constructor(weight?: number, value?: E) {
|
||||
this._weight = weight !== undefined ? weight : 1;
|
||||
this._val = val;
|
||||
this._value = value;
|
||||
this._hashCode = uuidV4();
|
||||
}
|
||||
|
||||
private _val: E | undefined;
|
||||
private _value: E | undefined;
|
||||
|
||||
get val(): E | undefined {
|
||||
return this._val;
|
||||
get value(): E | undefined {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
set val(value: E | undefined) {
|
||||
this._val = value;
|
||||
set value(value: E | undefined) {
|
||||
this._value = value;
|
||||
}
|
||||
|
||||
private _weight: number;
|
||||
|
@ -119,9 +119,9 @@ export abstract class AbstractGraph<
|
|||
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
||||
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
||||
* @param key
|
||||
* @param val
|
||||
* @param value
|
||||
*/
|
||||
abstract createVertex(key: VertexKey, val?: V): VO;
|
||||
abstract createVertex(key: VertexKey, value?: V): VO;
|
||||
|
||||
/**
|
||||
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
||||
|
@ -129,9 +129,9 @@ export abstract class AbstractGraph<
|
|||
* @param srcOrV1
|
||||
* @param destOrV2
|
||||
* @param weight
|
||||
* @param val
|
||||
* @param value
|
||||
*/
|
||||
abstract createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, val?: E): EO;
|
||||
abstract createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
||||
|
||||
abstract deleteEdge(edge: EO): EO | null;
|
||||
|
||||
|
@ -170,13 +170,13 @@ export abstract class AbstractGraph<
|
|||
|
||||
addVertex(vertex: VO): boolean;
|
||||
|
||||
addVertex(key: VertexKey, val?: V): boolean;
|
||||
addVertex(key: VertexKey, value?: V): boolean;
|
||||
|
||||
addVertex(keyOrVertex: VertexKey | VO, val?: V): boolean {
|
||||
addVertex(keyOrVertex: VertexKey | VO, value?: V): boolean {
|
||||
if (keyOrVertex instanceof AbstractVertex) {
|
||||
return this._addVertexOnly(keyOrVertex);
|
||||
} else {
|
||||
const newVertex = this.createVertex(keyOrVertex, val);
|
||||
const newVertex = this.createVertex(keyOrVertex, value);
|
||||
return this._addVertexOnly(newVertex);
|
||||
}
|
||||
}
|
||||
|
@ -222,9 +222,9 @@ export abstract class AbstractGraph<
|
|||
|
||||
addEdge(edge: EO): boolean;
|
||||
|
||||
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number, val?: E): boolean;
|
||||
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number, value?: E): boolean;
|
||||
|
||||
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, val?: E): boolean {
|
||||
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, value?: E): boolean {
|
||||
if (srcOrEdge instanceof AbstractEdge) {
|
||||
return this._addEdgeOnly(srcOrEdge);
|
||||
} else {
|
||||
|
@ -232,7 +232,7 @@ export abstract class AbstractGraph<
|
|||
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest))) return false;
|
||||
if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.key;
|
||||
if (dest instanceof AbstractVertex) dest = dest.key;
|
||||
const newEdge = this.createEdge(srcOrEdge, dest, weight, val);
|
||||
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
||||
return this._addEdgeOnly(newEdge);
|
||||
} else {
|
||||
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
||||
|
@ -493,10 +493,10 @@ export abstract class AbstractGraph<
|
|||
const getMinOfNoSeen = () => {
|
||||
let min = Infinity;
|
||||
let minV: VO | null = null;
|
||||
for (const [key, val] of distMap) {
|
||||
for (const [key, value] of distMap) {
|
||||
if (!seen.has(key)) {
|
||||
if (val < min) {
|
||||
min = val;
|
||||
if (value < min) {
|
||||
min = value;
|
||||
minV = key;
|
||||
}
|
||||
}
|
||||
|
@ -625,8 +625,8 @@ export abstract class AbstractGraph<
|
|||
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
||||
}
|
||||
|
||||
const heap = new PriorityQueue<{key: number; val: VO}>({comparator: (a, b) => a.key - b.key});
|
||||
heap.add({key: 0, val: srcVertex});
|
||||
const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
|
||||
heap.add({key: 0, value: srcVertex});
|
||||
|
||||
distMap.set(srcVertex, 0);
|
||||
preMap.set(srcVertex, null);
|
||||
|
@ -656,7 +656,7 @@ export abstract class AbstractGraph<
|
|||
while (heap.size > 0) {
|
||||
const curHeapNode = heap.poll();
|
||||
const dist = curHeapNode?.key;
|
||||
const cur = curHeapNode?.val;
|
||||
const cur = curHeapNode?.value;
|
||||
if (dist !== undefined) {
|
||||
if (cur) {
|
||||
seen.add(cur);
|
||||
|
@ -677,7 +677,7 @@ export abstract class AbstractGraph<
|
|||
const distSrcToNeighbor = distMap.get(neighbor);
|
||||
if (distSrcToNeighbor) {
|
||||
if (dist + weight < distSrcToNeighbor) {
|
||||
heap.add({key: dist + weight, val: neighbor});
|
||||
heap.add({key: dist + weight, value: neighbor});
|
||||
preMap.set(neighbor, cur);
|
||||
distMap.set(neighbor, dist + weight);
|
||||
}
|
||||
|
|
|
@ -15,11 +15,11 @@ export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|||
* The constructor function initializes a vertex with an optional value.
|
||||
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
||||
* used to uniquely identify the vertex within a graph or data structure.
|
||||
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It is used to initialize the value of the
|
||||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
|
||||
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
||||
*/
|
||||
constructor(key: VertexKey, val?: V) {
|
||||
super(key, val);
|
||||
constructor(key: VertexKey, value?: V) {
|
||||
super(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,11 +32,11 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|||
* @param {VertexKey} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
|
||||
* `VertexKey`, which is likely a unique identifier for a vertex in a graph.
|
||||
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
||||
* @param {E} [val] - The `val` parameter is an optional parameter of type `E`. It represents the value associated with
|
||||
* @param {E} [value] - The `value` parameter is an optional parameter of type `E`. It represents the value associated with
|
||||
* the edge.
|
||||
*/
|
||||
constructor(src: VertexKey, dest: VertexKey, weight?: number, val?: E) {
|
||||
super(weight, val);
|
||||
constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
|
||||
super(weight, value);
|
||||
this._src = src;
|
||||
this._dest = dest;
|
||||
}
|
||||
|
@ -99,13 +99,13 @@ export class DirectedGraph<
|
|||
* The function creates a new vertex with an optional value and returns it.
|
||||
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is of type `VertexKey`, which
|
||||
* could be a number or a string depending on how you want to identify your vertices.
|
||||
* @param [val] - The 'val' parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
||||
* it will be assigned to the 'val' property of the vertex. If no value is provided, the 'val' property will be
|
||||
* @param [value] - The 'value' parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
||||
* it will be assigned to the 'value' property of the vertex. If no value is provided, the 'value' property will be
|
||||
* assigned the same value as the 'key' parameter
|
||||
* @returns a new instance of a DirectedVertex object, casted as type VO.
|
||||
*/
|
||||
createVertex(key: VertexKey, val?: V): VO {
|
||||
return new DirectedVertex(key, val ?? key) as VO;
|
||||
createVertex(key: VertexKey, value?: V): VO {
|
||||
return new DirectedVertex(key, value ?? key) as VO;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,12 +119,12 @@ export class DirectedGraph<
|
|||
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
|
||||
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
|
||||
* weight is provided, it defaults to 1.
|
||||
* @param [val] - The 'val' parameter is an optional value that can be assigned to the edge. It can be of any type and
|
||||
* @param [value] - The 'value' parameter is an optional value that can be assigned to the edge. It can be of any type and
|
||||
* is used to store additional information or data associated with the edge.
|
||||
* @returns a new instance of a DirectedEdge object, casted as type EO.
|
||||
*/
|
||||
createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E): EO {
|
||||
return new DirectedEdge(src, dest, weight ?? 1, val) as EO;
|
||||
createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
||||
return new DirectedEdge(src, dest, weight ?? 1, value) as EO;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -11,11 +11,11 @@ export class MapVertex<V = any> extends DirectedVertex<V> {
|
|||
* @param {number} long - The "long" parameter represents the longitude of a location. Longitude is a geographic
|
||||
* coordinate that specifies the east-west position of a point on the Earth's surface. It is measured in degrees, with
|
||||
* values ranging from -180 to 180.
|
||||
* @param {V} [val] - The "val" parameter is an optional value of type V. It is not required to be provided when
|
||||
* @param {V} [value] - The "value" parameter is an optional value of type V. It is not required to be provided when
|
||||
* creating an instance of the class.
|
||||
*/
|
||||
constructor(key: VertexKey, val: V, lat: number, long: number) {
|
||||
super(key, val);
|
||||
constructor(key: VertexKey, value: V, lat: number, long: number) {
|
||||
super(key, value);
|
||||
this._lat = lat;
|
||||
this._long = long;
|
||||
}
|
||||
|
@ -49,11 +49,11 @@ export class MapEdge<E = any> extends DirectedEdge<E> {
|
|||
* a graph.
|
||||
* @param {VertexKey} dest - The `dest` parameter is the identifier of the destination vertex for an edge.
|
||||
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
||||
* @param {E} [val] - The "val" parameter is an optional parameter of type E. It is used to store additional
|
||||
* @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store additional
|
||||
* information or data associated with the edge.
|
||||
*/
|
||||
constructor(src: VertexKey, dest: VertexKey, weight?: number, val?: E) {
|
||||
super(src, dest, weight, val);
|
||||
constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) {
|
||||
super(src, dest, weight, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,15 +102,15 @@ export class MapGraph<
|
|||
* The function creates a new vertex with the given key, value, latitude, and longitude.
|
||||
* @param {VertexKey} key - The key parameter is the unique identifier for the vertex. It is of type VertexKey, which could
|
||||
* be a string or a number depending on how you define it in your code.
|
||||
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the vertex. It
|
||||
* is of type `V`, which means it should be of the same type as the `val` property of the vertex class `VO`.
|
||||
* @param [value] - The `value` parameter is an optional value that can be assigned to the `value` property of the vertex. It
|
||||
* is of type `V`, which means it should be of the same type as the `value` property of the vertex class `VO`.
|
||||
* @param {number} lat - The `lat` parameter represents the latitude of the vertex. It is a number that specifies the
|
||||
* position of the vertex on the Earth's surface in the north-south direction.
|
||||
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
||||
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `VO`.
|
||||
*/
|
||||
override createVertex(key: VertexKey, val?: V, lat: number = this.origin[0], long: number = this.origin[1]): VO {
|
||||
return new MapVertex(key, val, lat, long) as VO;
|
||||
override createVertex(key: VertexKey, value?: V, lat: number = this.origin[0], long: number = this.origin[1]): VO {
|
||||
return new MapVertex(key, value, lat, long) as VO;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -121,11 +121,11 @@ export class MapGraph<
|
|||
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. It
|
||||
* is used to assign a numerical value to the edge, which can be used in algorithms such as shortest path algorithms.
|
||||
* If the weight is not provided, it can be set to a default value or left undefined.
|
||||
* @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type,
|
||||
* @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type,
|
||||
* depending on the specific implementation of the `MapEdge` class.
|
||||
* @returns a new instance of the `MapEdge` class, cast as type `EO`.
|
||||
*/
|
||||
override createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E): EO {
|
||||
return new MapEdge(src, dest, weight, val) as EO;
|
||||
override createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
||||
return new MapEdge(src, dest, weight, value) as EO;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,11 +15,11 @@ export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|||
* The constructor function initializes a vertex with an optional value.
|
||||
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
||||
* used to uniquely identify the vertex within a graph or network.
|
||||
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It is used to initialize the value of the
|
||||
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
|
||||
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
||||
*/
|
||||
constructor(key: VertexKey, val?: V) {
|
||||
super(key, val);
|
||||
constructor(key: VertexKey, value?: V) {
|
||||
super(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,11 +31,11 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|||
* @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
|
||||
* graph edge.
|
||||
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
||||
* @param {E} [val] - The "val" parameter is an optional parameter of type E. It is used to store a value associated
|
||||
* @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store a value associated
|
||||
* with the edge.
|
||||
*/
|
||||
constructor(v1: VertexKey, v2: VertexKey, weight?: number, val?: E) {
|
||||
super(weight, val);
|
||||
constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
|
||||
super(weight, value);
|
||||
this._vertices = [v1, v2];
|
||||
}
|
||||
|
||||
|
@ -77,13 +77,13 @@ export class UndirectedGraph<
|
|||
* The function creates a new vertex with an optional value and returns it.
|
||||
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
|
||||
* vertex from another in the graph.
|
||||
* @param [val] - The `val` parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
||||
* @param [value] - The `value` parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
||||
* it will be used as the value of the vertex. If no value is provided, the `key` parameter will be used as the value of
|
||||
* the vertex.
|
||||
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `VO`.
|
||||
*/
|
||||
override createVertex(key: VertexKey, val?: VO['val']): VO {
|
||||
return new UndirectedVertex(key, val ?? key) as VO;
|
||||
override createVertex(key: VertexKey, value?: VO['value']): VO {
|
||||
return new UndirectedVertex(key, value ?? key) as VO;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -92,12 +92,12 @@ export class UndirectedGraph<
|
|||
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
||||
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
||||
* no weight is provided, it defaults to 1.
|
||||
* @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type and
|
||||
* @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type and
|
||||
* is used to store additional information or data associated with the edge.
|
||||
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
|
||||
*/
|
||||
override createEdge(v1: VertexKey, v2: VertexKey, weight?: number, val?: EO['val']): EO {
|
||||
return new UndirectedEdge(v1, v2, weight ?? 1, val) as EO;
|
||||
override createEdge(v1: VertexKey, v2: VertexKey, weight?: number, value?: EO['value']): EO {
|
||||
return new UndirectedEdge(v1, v2, weight ?? 1, value) as EO;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -8,12 +8,12 @@
|
|||
|
||||
export class HashTableNode<K, V> {
|
||||
key: K;
|
||||
val: V;
|
||||
value: V;
|
||||
next: HashTableNode<K, V> | null;
|
||||
|
||||
constructor(key: K, val: V) {
|
||||
constructor(key: K, value: V) {
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
this.value = value;
|
||||
this.next = null;
|
||||
}
|
||||
}
|
||||
|
@ -71,14 +71,14 @@ export class HashTable<K, V> {
|
|||
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
||||
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
||||
* table. It is of type K, which is a generic type representing the key's data type.
|
||||
* @param {V} val - The parameter `val` represents the value that you want to associate with the given key in the hash
|
||||
* @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
|
||||
* table.
|
||||
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
||||
* value.
|
||||
*/
|
||||
set(key: K, val: V): void {
|
||||
set(key: K, value: V): void {
|
||||
const index = this._hash(key);
|
||||
const newNode = new HashTableNode<K, V>(key, val);
|
||||
const newNode = new HashTableNode<K, V>(key, value);
|
||||
|
||||
if (!this._buckets[index]) {
|
||||
this._buckets[index] = newNode;
|
||||
|
@ -88,7 +88,7 @@ export class HashTable<K, V> {
|
|||
while (currentNode) {
|
||||
if (currentNode.key === key) {
|
||||
// If the key already exists, update the value
|
||||
currentNode.val = val;
|
||||
currentNode.value = value;
|
||||
return;
|
||||
}
|
||||
if (!currentNode.next) {
|
||||
|
@ -120,7 +120,7 @@ export class HashTable<K, V> {
|
|||
|
||||
while (currentNode) {
|
||||
if (currentNode.key === key) {
|
||||
return currentNode.val;
|
||||
return currentNode.value;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
@ -259,7 +259,7 @@ export class HashTable<K, V> {
|
|||
let currentNode = bucket;
|
||||
while (currentNode) {
|
||||
const newIndex = this._hash(currentNode.key);
|
||||
const newNode = new HashTableNode<K, V>(currentNode.key, currentNode.val);
|
||||
const newNode = new HashTableNode<K, V>(currentNode.key, currentNode.value);
|
||||
|
||||
if (!newBuckets[newIndex]) {
|
||||
newBuckets[newIndex] = newNode;
|
||||
|
|
|
@ -8,23 +8,23 @@
|
|||
export class DoublyLinkedListNode<E = any> {
|
||||
/**
|
||||
* The constructor function initializes the value, next, and previous properties of an object.
|
||||
* @param {E} val - The "val" parameter is the value that will be stored in the node. It can be of any data type, as it
|
||||
* @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(val: E) {
|
||||
this._val = val;
|
||||
constructor(value: E) {
|
||||
this._value = value;
|
||||
this._next = null;
|
||||
this._prev = null;
|
||||
}
|
||||
|
||||
private _val: E;
|
||||
private _value: E;
|
||||
|
||||
get val(): E {
|
||||
return this._val;
|
||||
get value(): E {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
set val(value: E) {
|
||||
this._val = value;
|
||||
set value(value: E) {
|
||||
this._value = value;
|
||||
}
|
||||
|
||||
private _next: DoublyLinkedListNode<E> | null;
|
||||
|
@ -104,10 +104,10 @@ export class DoublyLinkedList<E = any> {
|
|||
|
||||
/**
|
||||
* The push function adds a new node with the given value to the end of the doubly linked list.
|
||||
* @param {E} val - The value to be added to the linked list.
|
||||
* @param {E} value - The value to be added to the linked list.
|
||||
*/
|
||||
push(val: E): void {
|
||||
const newNode = new DoublyLinkedListNode(val);
|
||||
push(value: E): void {
|
||||
const newNode = new DoublyLinkedListNode(value);
|
||||
if (!this.head) {
|
||||
this.head = newNode;
|
||||
this.tail = newNode;
|
||||
|
@ -121,15 +121,15 @@ export class DoublyLinkedList<E = any> {
|
|||
|
||||
/**
|
||||
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
||||
* @param {E} val - The value to be added to the linked list.
|
||||
* @param {E} value - The value to be added to the linked list.
|
||||
*/
|
||||
addLast(val: E): void {
|
||||
this.push(val);
|
||||
addLast(value: E): void {
|
||||
this.push(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
||||
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
||||
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
||||
* list is empty, it returns null.
|
||||
*/
|
||||
pop(): E | undefined {
|
||||
|
@ -143,12 +143,12 @@ export class DoublyLinkedList<E = any> {
|
|||
this.tail!.next = null;
|
||||
}
|
||||
this._length--;
|
||||
return removedNode.val;
|
||||
return removedNode.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
||||
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
||||
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
||||
* list is empty, it returns null.
|
||||
*/
|
||||
popLast(): E | undefined {
|
||||
|
@ -171,7 +171,7 @@ export class DoublyLinkedList<E = any> {
|
|||
this.head!.prev = null;
|
||||
}
|
||||
this._length--;
|
||||
return removedNode.val;
|
||||
return removedNode.value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -185,11 +185,11 @@ export class DoublyLinkedList<E = any> {
|
|||
|
||||
/**
|
||||
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
||||
* @param {E} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
|
||||
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
||||
* doubly linked list.
|
||||
*/
|
||||
unshift(val: E): void {
|
||||
const newNode = new DoublyLinkedListNode(val);
|
||||
unshift(value: E): void {
|
||||
const newNode = new DoublyLinkedListNode(value);
|
||||
if (!this.head) {
|
||||
this.head = newNode;
|
||||
this.tail = newNode;
|
||||
|
@ -203,11 +203,11 @@ export class DoublyLinkedList<E = any> {
|
|||
|
||||
/**
|
||||
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
||||
* @param {E} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
|
||||
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
||||
* doubly linked list.
|
||||
*/
|
||||
addFirst(val: E): void {
|
||||
this.unshift(val);
|
||||
addFirst(value: E): void {
|
||||
this.unshift(value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -215,7 +215,7 @@ export class DoublyLinkedList<E = any> {
|
|||
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
||||
*/
|
||||
getFirst(): E | undefined {
|
||||
return this.head?.val;
|
||||
return this.head?.value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -223,7 +223,7 @@ export class DoublyLinkedList<E = any> {
|
|||
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
||||
*/
|
||||
getLast(): E | undefined {
|
||||
return this.tail?.val;
|
||||
return this.tail?.value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -239,7 +239,7 @@ export class DoublyLinkedList<E = any> {
|
|||
for (let i = 0; i < index; i++) {
|
||||
current = current!.next;
|
||||
}
|
||||
return current!.val;
|
||||
return current!.value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -262,15 +262,15 @@ export class DoublyLinkedList<E = any> {
|
|||
/**
|
||||
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
||||
* node if found, otherwise it returns null.
|
||||
* @param {E} val - The `val` parameter is the value that we want to search for in the doubly linked list.
|
||||
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
|
||||
* @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
|
||||
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
|
||||
* is found in the linked list. If no such node is found, it returns `null`.
|
||||
*/
|
||||
getNode(val: E | null): DoublyLinkedListNode<E> | null {
|
||||
getNode(value: E | null): DoublyLinkedListNode<E> | null {
|
||||
let current = this.head;
|
||||
|
||||
while (current) {
|
||||
if (current.val === val) {
|
||||
if (current.value === value) {
|
||||
return current;
|
||||
}
|
||||
current = current.next;
|
||||
|
@ -283,23 +283,23 @@ export class DoublyLinkedList<E = any> {
|
|||
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
||||
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
||||
* DoublyLinkedList. It is of type number.
|
||||
* @param {E} val - The `val` parameter represents the value that you want to insert into the Doubly Linked List at the
|
||||
* @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
|
||||
* specified index.
|
||||
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
||||
* if the index is out of bounds.
|
||||
*/
|
||||
insertAt(index: number, val: E): boolean {
|
||||
insertAt(index: number, value: E): boolean {
|
||||
if (index < 0 || index > this.length) return false;
|
||||
if (index === 0) {
|
||||
this.unshift(val);
|
||||
this.unshift(value);
|
||||
return true;
|
||||
}
|
||||
if (index === this.length) {
|
||||
this.push(val);
|
||||
this.push(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
const newNode = new DoublyLinkedListNode(val);
|
||||
const newNode = new DoublyLinkedListNode(value);
|
||||
const prevNode = this.getNodeAt(index - 1);
|
||||
const nextNode = prevNode!.next;
|
||||
newNode.prev = prevNode;
|
||||
|
@ -365,7 +365,7 @@ export class DoublyLinkedList<E = any> {
|
|||
prevNode!.next = nextNode;
|
||||
nextNode!.prev = prevNode;
|
||||
this._length--;
|
||||
return removedNode!.val;
|
||||
return removedNode!.value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -409,7 +409,7 @@ export class DoublyLinkedList<E = any> {
|
|||
const array: E[] = [];
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
array.push(current.val);
|
||||
array.push(current.value);
|
||||
current = current.next;
|
||||
}
|
||||
return array;
|
||||
|
@ -439,11 +439,11 @@ export class DoublyLinkedList<E = any> {
|
|||
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
||||
* the callback function. If no element satisfies the condition, it returns `null`.
|
||||
*/
|
||||
find(callback: (val: E) => boolean): E | null {
|
||||
find(callback: (value: E) => boolean): E | null {
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
if (callback(current.val)) {
|
||||
return current.val;
|
||||
if (callback(current.value)) {
|
||||
return current.value;
|
||||
}
|
||||
current = current.next;
|
||||
}
|
||||
|
@ -452,16 +452,16 @@ export class DoublyLinkedList<E = any> {
|
|||
|
||||
/**
|
||||
* The function returns the index of the first occurrence of a given value in a linked list.
|
||||
* @param {E} val - The parameter `val` is of type `E`, which means it can be any data type. It represents the value
|
||||
* @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
|
||||
* that we are searching for in the linked list.
|
||||
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `val` in the linked
|
||||
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
|
||||
* list. If the value is not found, it returns -1.
|
||||
*/
|
||||
indexOf(val: E): number {
|
||||
indexOf(value: E): number {
|
||||
let index = 0;
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
if (current.val === val) {
|
||||
if (current.value === value) {
|
||||
return index;
|
||||
}
|
||||
index++;
|
||||
|
@ -478,11 +478,11 @@ export class DoublyLinkedList<E = any> {
|
|||
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
||||
* the callback function. If no value satisfies the condition, it returns `null`.
|
||||
*/
|
||||
findBackward(callback: (val: E) => boolean): E | null {
|
||||
findBackward(callback: (value: E) => boolean): E | null {
|
||||
let current = this.tail;
|
||||
while (current) {
|
||||
if (callback(current.val)) {
|
||||
return current.val;
|
||||
if (callback(current.value)) {
|
||||
return current.value;
|
||||
}
|
||||
current = current.prev;
|
||||
}
|
||||
|
@ -497,7 +497,7 @@ export class DoublyLinkedList<E = any> {
|
|||
const array: E[] = [];
|
||||
let current = this.tail;
|
||||
while (current) {
|
||||
array.push(current.val);
|
||||
array.push(current.value);
|
||||
current = current.prev;
|
||||
}
|
||||
return array;
|
||||
|
@ -518,15 +518,15 @@ export class DoublyLinkedList<E = any> {
|
|||
|
||||
/**
|
||||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
||||
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
||||
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
||||
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
||||
* current node in the linked list.
|
||||
*/
|
||||
forEach(callback: (val: E, index: number) => void): void {
|
||||
forEach(callback: (value: E, index: number) => void): void {
|
||||
let current = this.head;
|
||||
let index = 0;
|
||||
while (current) {
|
||||
callback(current.val, index);
|
||||
callback(current.value, index);
|
||||
current = current.next;
|
||||
index++;
|
||||
}
|
||||
|
@ -540,11 +540,11 @@ export class DoublyLinkedList<E = any> {
|
|||
* DoublyLinkedList).
|
||||
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
||||
*/
|
||||
map<U>(callback: (val: E) => U): DoublyLinkedList<U> {
|
||||
map<U>(callback: (value: E) => U): DoublyLinkedList<U> {
|
||||
const mappedList = new DoublyLinkedList<U>();
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
mappedList.push(callback(current.val));
|
||||
mappedList.push(callback(current.value));
|
||||
current = current.next;
|
||||
}
|
||||
return mappedList;
|
||||
|
@ -557,12 +557,12 @@ export class DoublyLinkedList<E = any> {
|
|||
* It is used to determine whether a value should be included in the filtered list or not.
|
||||
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
||||
*/
|
||||
filter(callback: (val: E) => boolean): DoublyLinkedList<E> {
|
||||
filter(callback: (value: E) => boolean): DoublyLinkedList<E> {
|
||||
const filteredList = new DoublyLinkedList<E>();
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
if (callback(current.val)) {
|
||||
filteredList.push(current.val);
|
||||
if (callback(current.value)) {
|
||||
filteredList.push(current.value);
|
||||
}
|
||||
current = current.next;
|
||||
}
|
||||
|
@ -572,18 +572,18 @@ export class DoublyLinkedList<E = any> {
|
|||
/**
|
||||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
||||
* single value.
|
||||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
||||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
||||
* used to perform a specific operation on each element of the linked list.
|
||||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
||||
* point for the reduction operation.
|
||||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
||||
* elements in the linked list.
|
||||
*/
|
||||
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U {
|
||||
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U {
|
||||
let accumulator = initialValue;
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
accumulator = callback(accumulator, current.val);
|
||||
accumulator = callback(accumulator, current.value);
|
||||
current = current.next;
|
||||
}
|
||||
return accumulator;
|
||||
|
@ -632,7 +632,7 @@ export class DoublyLinkedList<E = any> {
|
|||
let current = this.head;
|
||||
|
||||
while (current) {
|
||||
yield current.val;
|
||||
yield current.value;
|
||||
current = current.next;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,22 +8,22 @@
|
|||
export class SinglyLinkedListNode<E = any> {
|
||||
/**
|
||||
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
||||
* @param {E} val - The "val" parameter is of type E, which means it can be any data type. It represents the value that
|
||||
* @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(val: E) {
|
||||
this._val = val;
|
||||
constructor(value: E) {
|
||||
this._value = value;
|
||||
this._next = null;
|
||||
}
|
||||
|
||||
private _val: E;
|
||||
private _value: E;
|
||||
|
||||
get val(): E {
|
||||
return this._val;
|
||||
get value(): E {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
set val(value: E) {
|
||||
this._val = value;
|
||||
set value(value: E) {
|
||||
this._value = value;
|
||||
}
|
||||
|
||||
private _next: SinglyLinkedListNode<E> | null;
|
||||
|
@ -88,12 +88,12 @@ export class SinglyLinkedList<E = any> {
|
|||
}
|
||||
|
||||
/**
|
||||
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
||||
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
||||
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
||||
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
||||
* any type (E) as specified in the generic type declaration of the class or function.
|
||||
*/
|
||||
push(val: E): void {
|
||||
const newNode = new SinglyLinkedListNode(val);
|
||||
push(value: E): void {
|
||||
const newNode = new SinglyLinkedListNode(value);
|
||||
if (!this.head) {
|
||||
this.head = newNode;
|
||||
this.tail = newNode;
|
||||
|
@ -105,12 +105,12 @@ export class SinglyLinkedList<E = any> {
|
|||
}
|
||||
|
||||
/**
|
||||
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
||||
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
||||
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
||||
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
||||
* any type (E) as specified in the generic type declaration of the class or function.
|
||||
*/
|
||||
addLast(val: E): void {
|
||||
this.push(val);
|
||||
addLast(value: E): void {
|
||||
this.push(value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -122,22 +122,22 @@ export class SinglyLinkedList<E = any> {
|
|||
pop(): E | undefined {
|
||||
if (!this.head) return undefined;
|
||||
if (this.head === this.tail) {
|
||||
const val = this.head.val;
|
||||
const value = this.head.value;
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
this._length--;
|
||||
return val;
|
||||
return value;
|
||||
}
|
||||
|
||||
let current = this.head;
|
||||
while (current.next !== this.tail) {
|
||||
current = current.next!;
|
||||
}
|
||||
const val = this.tail!.val;
|
||||
const value = this.tail!.value;
|
||||
current.next = null;
|
||||
this.tail = current;
|
||||
this._length--;
|
||||
return val;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -159,7 +159,7 @@ export class SinglyLinkedList<E = any> {
|
|||
const removedNode = this.head;
|
||||
this.head = this.head.next;
|
||||
this._length--;
|
||||
return removedNode.val;
|
||||
return removedNode.value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -172,11 +172,11 @@ export class SinglyLinkedList<E = any> {
|
|||
|
||||
/**
|
||||
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
||||
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
||||
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
||||
* linked list.
|
||||
*/
|
||||
unshift(val: E): void {
|
||||
const newNode = new SinglyLinkedListNode(val);
|
||||
unshift(value: E): void {
|
||||
const newNode = new SinglyLinkedListNode(value);
|
||||
if (!this.head) {
|
||||
this.head = newNode;
|
||||
this.tail = newNode;
|
||||
|
@ -189,11 +189,11 @@ export class SinglyLinkedList<E = any> {
|
|||
|
||||
/**
|
||||
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
||||
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
||||
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
||||
* linked list.
|
||||
*/
|
||||
addFirst(val: E): void {
|
||||
this.unshift(val);
|
||||
addFirst(value: E): void {
|
||||
this.unshift(value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -209,7 +209,7 @@ export class SinglyLinkedList<E = any> {
|
|||
for (let i = 0; i < index; i++) {
|
||||
current = current!.next;
|
||||
}
|
||||
return current!.val;
|
||||
return current!.value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -243,7 +243,7 @@ export class SinglyLinkedList<E = any> {
|
|||
const removedNode = prevNode!.next;
|
||||
prevNode!.next = removedNode!.next;
|
||||
this._length--;
|
||||
return removedNode!.val;
|
||||
return removedNode!.value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -257,7 +257,7 @@ export class SinglyLinkedList<E = any> {
|
|||
if (!valueOrNode) return false;
|
||||
let value: E;
|
||||
if (valueOrNode instanceof SinglyLinkedListNode) {
|
||||
value = valueOrNode.val;
|
||||
value = valueOrNode.value;
|
||||
} else {
|
||||
value = valueOrNode;
|
||||
}
|
||||
|
@ -265,7 +265,7 @@ export class SinglyLinkedList<E = any> {
|
|||
prev = null;
|
||||
|
||||
while (current) {
|
||||
if (current.val === value) {
|
||||
if (current.value === value) {
|
||||
if (prev === null) {
|
||||
this.head = current.next;
|
||||
if (current === this.tail) {
|
||||
|
@ -291,23 +291,23 @@ export class SinglyLinkedList<E = any> {
|
|||
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
||||
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
||||
* linked list. It is of type number.
|
||||
* @param {E} val - The `val` parameter represents the value that you want to insert into the linked list at the
|
||||
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
||||
* specified index.
|
||||
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
||||
* if the index is out of bounds.
|
||||
*/
|
||||
insertAt(index: number, val: E): boolean {
|
||||
insertAt(index: number, value: E): boolean {
|
||||
if (index < 0 || index > this.length) return false;
|
||||
if (index === 0) {
|
||||
this.unshift(val);
|
||||
this.unshift(value);
|
||||
return true;
|
||||
}
|
||||
if (index === this.length) {
|
||||
this.push(val);
|
||||
this.push(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
const newNode = new SinglyLinkedListNode(val);
|
||||
const newNode = new SinglyLinkedListNode(value);
|
||||
const prevNode = this.getNodeAt(index - 1);
|
||||
newNode.next = prevNode!.next;
|
||||
prevNode!.next = newNode;
|
||||
|
@ -341,7 +341,7 @@ export class SinglyLinkedList<E = any> {
|
|||
const array: E[] = [];
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
array.push(current.val);
|
||||
array.push(current.value);
|
||||
current = current.next;
|
||||
}
|
||||
return array;
|
||||
|
@ -375,11 +375,11 @@ export class SinglyLinkedList<E = any> {
|
|||
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
||||
* the callback function. If no element satisfies the condition, it returns `null`.
|
||||
*/
|
||||
find(callback: (val: E) => boolean): E | null {
|
||||
find(callback: (value: E) => boolean): E | null {
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
if (callback(current.val)) {
|
||||
return current.val;
|
||||
if (callback(current.value)) {
|
||||
return current.value;
|
||||
}
|
||||
current = current.next;
|
||||
}
|
||||
|
@ -397,7 +397,7 @@ export class SinglyLinkedList<E = any> {
|
|||
let current = this.head;
|
||||
|
||||
while (current) {
|
||||
if (current.val === value) {
|
||||
if (current.value === value) {
|
||||
return index;
|
||||
}
|
||||
index++;
|
||||
|
@ -418,7 +418,7 @@ export class SinglyLinkedList<E = any> {
|
|||
let current = this.head;
|
||||
|
||||
while (current) {
|
||||
if (current.val === value) {
|
||||
if (current.value === value) {
|
||||
return current;
|
||||
}
|
||||
current = current.next;
|
||||
|
@ -440,18 +440,18 @@ export class SinglyLinkedList<E = any> {
|
|||
|
||||
let existingValue: E;
|
||||
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
||||
existingValue = existingValueOrNode.val;
|
||||
existingValue = existingValueOrNode.value;
|
||||
} else {
|
||||
existingValue = existingValueOrNode;
|
||||
}
|
||||
if (this.head.val === existingValue) {
|
||||
if (this.head.value === existingValue) {
|
||||
this.unshift(newValue);
|
||||
return true;
|
||||
}
|
||||
|
||||
let current = this.head;
|
||||
while (current.next) {
|
||||
if (current.next.val === existingValue) {
|
||||
if (current.next.value === existingValue) {
|
||||
const newNode = new SinglyLinkedListNode(newValue);
|
||||
newNode.next = current.next;
|
||||
current.next = newNode;
|
||||
|
@ -505,7 +505,7 @@ export class SinglyLinkedList<E = any> {
|
|||
let current = this.head;
|
||||
|
||||
while (current) {
|
||||
if (current.val === value) {
|
||||
if (current.value === value) {
|
||||
count++;
|
||||
}
|
||||
current = current.next;
|
||||
|
@ -516,15 +516,15 @@ export class SinglyLinkedList<E = any> {
|
|||
|
||||
/**
|
||||
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
||||
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
||||
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
||||
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
||||
* current node in the linked list.
|
||||
*/
|
||||
forEach(callback: (val: E, index: number) => void): void {
|
||||
forEach(callback: (value: E, index: number) => void): void {
|
||||
let current = this.head;
|
||||
let index = 0;
|
||||
while (current) {
|
||||
callback(current.val, index);
|
||||
callback(current.value, index);
|
||||
current = current.next;
|
||||
index++;
|
||||
}
|
||||
|
@ -538,11 +538,11 @@ export class SinglyLinkedList<E = any> {
|
|||
* SinglyLinkedList).
|
||||
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
||||
*/
|
||||
map<U>(callback: (val: E) => U): SinglyLinkedList<U> {
|
||||
map<U>(callback: (value: E) => U): SinglyLinkedList<U> {
|
||||
const mappedList = new SinglyLinkedList<U>();
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
mappedList.push(callback(current.val));
|
||||
mappedList.push(callback(current.value));
|
||||
current = current.next;
|
||||
}
|
||||
return mappedList;
|
||||
|
@ -555,12 +555,12 @@ export class SinglyLinkedList<E = any> {
|
|||
* It is used to determine whether a value should be included in the filtered list or not.
|
||||
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
||||
*/
|
||||
filter(callback: (val: E) => boolean): SinglyLinkedList<E> {
|
||||
filter(callback: (value: E) => boolean): SinglyLinkedList<E> {
|
||||
const filteredList = new SinglyLinkedList<E>();
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
if (callback(current.val)) {
|
||||
filteredList.push(current.val);
|
||||
if (callback(current.value)) {
|
||||
filteredList.push(current.value);
|
||||
}
|
||||
current = current.next;
|
||||
}
|
||||
|
@ -570,18 +570,18 @@ export class SinglyLinkedList<E = any> {
|
|||
/**
|
||||
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
||||
* single value.
|
||||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
||||
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
||||
* used to perform a specific operation on each element of the linked list.
|
||||
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
||||
* point for the reduction operation.
|
||||
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
||||
* elements in the linked list.
|
||||
*/
|
||||
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U {
|
||||
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U {
|
||||
let accumulator = initialValue;
|
||||
let current = this.head;
|
||||
while (current) {
|
||||
accumulator = callback(accumulator, current.val);
|
||||
accumulator = callback(accumulator, current.value);
|
||||
current = current.next;
|
||||
}
|
||||
return accumulator;
|
||||
|
@ -594,7 +594,7 @@ export class SinglyLinkedList<E = any> {
|
|||
let current = this.head;
|
||||
|
||||
while (current) {
|
||||
yield current.val;
|
||||
yield current.value;
|
||||
current = current.next;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ export class SkipQueue<E = any> extends SinglyLinkedList<E> {
|
|||
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
||||
*/
|
||||
peek(): E | undefined {
|
||||
return this.head?.val;
|
||||
return this.head?.value;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,9 +2,9 @@ import {BinaryTreeNode} from '../data-structures';
|
|||
import {BinaryTreeDeletedResult, BTNKey, BinaryTreeNodeNested, BTNCallback} from '../types';
|
||||
|
||||
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
||||
createNode(key: BTNKey, val?: N['val']): N;
|
||||
createNode(key: BTNKey, value?: N['value']): N;
|
||||
|
||||
add(keyOrNode: BTNKey | N | null, val?: N['val']): N | null | undefined;
|
||||
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
||||
|
||||
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {VertexKey} from '../types';
|
||||
|
||||
export interface IGraph<V, E, VO, EO> {
|
||||
createVertex(key: VertexKey, val?: V): VO;
|
||||
createVertex(key: VertexKey, value?: V): VO;
|
||||
|
||||
createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, val?: E): EO;
|
||||
createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
||||
}
|
||||
|
|
|
@ -6,8 +6,8 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bst).toBeInstanceOf(BST);
|
||||
bst.add(11, 11);
|
||||
bst.add(3, 3);
|
||||
const idsOrVals = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
||||
bst.addMany(idsOrVals, idsOrVals, false);
|
||||
const idsOrValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
||||
bst.addMany(idsOrValues, idsOrValues, false);
|
||||
expect(bst.root).toBeInstanceOf(BSTNode);
|
||||
|
||||
if (bst.root) expect(bst.root.key).toBe(11);
|
||||
|
|
|
@ -31,7 +31,7 @@ describe('AVL Tree Test', () => {
|
|||
expect(lesserSum).toBe(45);
|
||||
|
||||
// node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
|
||||
expect(node15?.val).toBe(15);
|
||||
expect(node15?.value).toBe(15);
|
||||
|
||||
const dfs = tree.dfs(node => node, 'in');
|
||||
expect(dfs[0].key).toBe(1);
|
||||
|
@ -140,7 +140,7 @@ describe('AVL Tree Test recursively', () => {
|
|||
expect(lesserSum).toBe(45);
|
||||
|
||||
// node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
|
||||
expect(node15?.val).toBe(15);
|
||||
expect(node15?.value).toBe(15);
|
||||
|
||||
const dfs = tree.dfs(node => node, 'in');
|
||||
expect(dfs[0].key).toBe(1);
|
||||
|
|
|
@ -297,9 +297,9 @@ describe('', () => {
|
|||
}
|
||||
}
|
||||
|
||||
update(index: number, val: number): void {
|
||||
this._tree.update(index + 1, val - this._nums[index]);
|
||||
this._nums[index] = val;
|
||||
update(index: number, value: number): void {
|
||||
this._tree.update(index + 1, value - this._nums[index]);
|
||||
this._nums[index] = value;
|
||||
}
|
||||
|
||||
sumRange(left: number, right: number): number {
|
||||
|
|
|
@ -20,10 +20,10 @@ describe('BinaryTreeNode', () => {
|
|||
|
||||
it('should set and get the value correctly', () => {
|
||||
const node: BinaryTreeNode<number> = new BinaryTreeNode<number>(1, 42);
|
||||
expect(node.val).toBe(42);
|
||||
expect(node.value).toBe(42);
|
||||
|
||||
node.val = 55;
|
||||
expect(node.val).toBe(55);
|
||||
node.value = 55;
|
||||
expect(node.value).toBe(55);
|
||||
});
|
||||
|
||||
it('should set and get the left child correctly', () => {
|
||||
|
@ -110,7 +110,7 @@ describe('BinaryTree', () => {
|
|||
const node4 = tree.get(4);
|
||||
expect(tree.has(node4)).toBe(false);
|
||||
expect(tree.has(node4, node => node)).toBe(false);
|
||||
expect(tree.has('3', node => node.val?.toString())).toBe(true);
|
||||
expect(tree.has('3', node => node.value?.toString())).toBe(true);
|
||||
});
|
||||
|
||||
it('should getDepth return correct depth', () => {
|
||||
|
@ -343,9 +343,9 @@ describe('BinaryTree', () => {
|
|||
const nodeB = tree.get(3);
|
||||
|
||||
expect(nodeA?.key).toBe(5);
|
||||
expect(nodeA?.val).toBe('A');
|
||||
expect(nodeA?.value).toBe('A');
|
||||
expect(nodeB?.key).toBe(3);
|
||||
expect(nodeB?.val).toBe('B');
|
||||
expect(nodeB?.value).toBe('B');
|
||||
});
|
||||
|
||||
it('should return null when getting a non-existent node', () => {
|
||||
|
@ -437,14 +437,14 @@ describe('BinaryTree', () => {
|
|||
tree.add(3, 'B');
|
||||
tree.add(7, 'C');
|
||||
|
||||
const nodes = tree.getNodes('B', (node: BinaryTreeNode<string>) => node.val);
|
||||
const nodes = tree.getNodes('B', (node: BinaryTreeNode<string>) => node.value);
|
||||
|
||||
expect(nodes.length).toBe(1);
|
||||
expect(nodes[0].key).toBe(3);
|
||||
|
||||
const nodesRec = tree.getNodes(
|
||||
'B',
|
||||
(node: BinaryTreeNode<string>) => node.val,
|
||||
(node: BinaryTreeNode<string>) => node.value,
|
||||
false,
|
||||
tree.root,
|
||||
IterationType.RECURSIVE
|
||||
|
|
|
@ -26,7 +26,7 @@ describe('BST operations test', () => {
|
|||
const nodeId10 = bst.get(10);
|
||||
expect(nodeId10?.key).toBe(10);
|
||||
|
||||
const nodeVal9 = bst.get(9, node => node.val);
|
||||
const nodeVal9 = bst.get(9, node => node.value);
|
||||
expect(nodeVal9?.key).toBe(9);
|
||||
|
||||
const leftMost = bst.getLeftMost();
|
||||
|
@ -236,7 +236,7 @@ describe('BST operations test', () => {
|
|||
expect(leftMost?.key).toBe(1);
|
||||
|
||||
const node15 = objBST.get(15);
|
||||
expect(node15?.val).toEqual({key: 15, keyA: 15});
|
||||
expect(node15?.value).toEqual({key: 15, keyA: 15});
|
||||
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
||||
expect(minNodeBySpecificNode?.key).toBe(12);
|
||||
|
||||
|
@ -416,7 +416,7 @@ describe('BST operations test recursively', () => {
|
|||
const nodeId10 = bst.get(10);
|
||||
expect(nodeId10?.key).toBe(10);
|
||||
|
||||
const nodeVal9 = bst.get(9, node => node.val);
|
||||
const nodeVal9 = bst.get(9, node => node.value);
|
||||
expect(nodeVal9?.key).toBe(9);
|
||||
|
||||
const leftMost = bst.getLeftMost();
|
||||
|
@ -626,7 +626,7 @@ describe('BST operations test recursively', () => {
|
|||
expect(leftMost?.key).toBe(1);
|
||||
|
||||
const node15 = objBST.get(15);
|
||||
expect(node15?.val).toEqual({key: 15, keyA: 15});
|
||||
expect(node15?.value).toEqual({key: 15, keyA: 15});
|
||||
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
||||
expect(minNodeBySpecificNode?.key).toBe(12);
|
||||
|
||||
|
|
|
@ -16,10 +16,10 @@ describe('RBTreeNode', () => {
|
|||
|
||||
it('should set and get the value correctly', () => {
|
||||
const node: RBTreeNode<number> = new RBTreeNode<number>(1, 42);
|
||||
expect(node.val).toBe(42);
|
||||
expect(node.value).toBe(42);
|
||||
|
||||
node.val = 55;
|
||||
expect(node.val).toBe(55);
|
||||
node.value = 55;
|
||||
expect(node.value).toBe(55);
|
||||
});
|
||||
|
||||
it('should set and get the left child correctly', () => {
|
||||
|
|
|
@ -26,7 +26,7 @@ describe('TreeMultiset operations test', () => {
|
|||
const nodeId10 = treeMultiset.get(10);
|
||||
expect(nodeId10?.key).toBe(10);
|
||||
|
||||
const nodeVal9 = treeMultiset.get(9, node => node.val);
|
||||
const nodeVal9 = treeMultiset.get(9, node => node.value);
|
||||
expect(nodeVal9?.key).toBe(9);
|
||||
|
||||
const nodesByCount1 = treeMultiset.getNodes(1, node => node.count);
|
||||
|
@ -266,7 +266,7 @@ describe('TreeMultiset operations test recursively', () => {
|
|||
const nodeId10 = treeMultiset.get(10);
|
||||
expect(nodeId10?.key).toBe(10);
|
||||
|
||||
const nodeVal9 = treeMultiset.get(9, node => node.val);
|
||||
const nodeVal9 = treeMultiset.get(9, node => node.value);
|
||||
expect(nodeVal9?.key).toBe(9);
|
||||
|
||||
const nodesByCount1 = treeMultiset.getNodes(1, node => node.count);
|
||||
|
|
|
@ -3,9 +3,9 @@ import {AbstractEdge, AbstractGraph, AbstractVertex, VertexKey} from '../../../.
|
|||
class MyVertex<V = any> extends AbstractVertex<V> {
|
||||
data?: V;
|
||||
|
||||
constructor(key: VertexKey, val?: V) {
|
||||
super(key, val);
|
||||
this.data = val;
|
||||
constructor(key: VertexKey, value?: V) {
|
||||
super(key, value);
|
||||
this.data = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,11 +14,11 @@ class MyEdge<E = any> extends AbstractEdge<E> {
|
|||
src: VertexKey;
|
||||
dest: VertexKey;
|
||||
|
||||
constructor(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, val?: E) {
|
||||
super(weight, val);
|
||||
constructor(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E) {
|
||||
super(weight, value);
|
||||
this.src = srcOrV1;
|
||||
this.dest = destOrV2;
|
||||
this.data = val;
|
||||
this.data = value;
|
||||
this._setHashCode('');
|
||||
}
|
||||
}
|
||||
|
@ -29,12 +29,12 @@ class MyGraph<
|
|||
VO extends MyVertex<V> = MyVertex<V>,
|
||||
EO extends MyEdge<E> = MyEdge<E>
|
||||
> extends AbstractGraph<V, E, VO, EO> {
|
||||
createVertex(key: VertexKey, val?: V): VO {
|
||||
return new MyVertex(key, val) as VO;
|
||||
createVertex(key: VertexKey, value?: V): VO {
|
||||
return new MyVertex(key, value) as VO;
|
||||
}
|
||||
|
||||
createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, val?: E): EO {
|
||||
return new MyEdge(srcOrV1, destOrV2, weight, val) as EO;
|
||||
createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO {
|
||||
return new MyEdge(srcOrV1, destOrV2, weight, value) as EO;
|
||||
}
|
||||
|
||||
deleteEdge(edge: EO): EO | null {
|
||||
|
@ -92,9 +92,9 @@ describe('AbstractGraph Operation Test', () => {
|
|||
// const eBC = new MyEdge('B', 'C');
|
||||
// const eCD = new MyEdge('C', 'D');
|
||||
vA!.key = vA?.key || 1;
|
||||
vA!.val = vA?.val ?? 2;
|
||||
vA!.value = vA?.value ?? 2;
|
||||
|
||||
eAB!.val = eAB.val;
|
||||
eAB!.value = eAB.value;
|
||||
const hs = eAB.hashCode;
|
||||
|
||||
expect(hs).toBe('');
|
||||
|
|
|
@ -93,9 +93,9 @@ describe('DirectedGraph Operation Test', () => {
|
|||
});
|
||||
|
||||
class MyVertex<V = any> extends DirectedVertex<V> {
|
||||
constructor(key: VertexKey, val?: V) {
|
||||
super(key, val);
|
||||
this._data = val;
|
||||
constructor(key: VertexKey, value?: V) {
|
||||
super(key, value);
|
||||
this._data = value;
|
||||
}
|
||||
|
||||
private _data: V | undefined;
|
||||
|
@ -110,9 +110,9 @@ class MyVertex<V = any> extends DirectedVertex<V> {
|
|||
}
|
||||
|
||||
class MyEdge<E = any> extends DirectedEdge<E> {
|
||||
constructor(v1: VertexKey, v2: VertexKey, weight?: number, val?: E) {
|
||||
super(v1, v2, weight, val);
|
||||
this._data = val;
|
||||
constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) {
|
||||
super(v1, v2, weight, value);
|
||||
this._data = value;
|
||||
}
|
||||
|
||||
private _data: E | undefined;
|
||||
|
@ -132,12 +132,12 @@ class MyDirectedGraph<
|
|||
VO extends MyVertex<V> = MyVertex<V>,
|
||||
EO extends MyEdge<E> = MyEdge<E>
|
||||
> extends DirectedGraph<V, E, VO, EO> {
|
||||
createVertex(key: VertexKey, val: V): VO {
|
||||
return new MyVertex(key, val) as VO;
|
||||
createVertex(key: VertexKey, value: V): VO {
|
||||
return new MyVertex(key, value) as VO;
|
||||
}
|
||||
|
||||
createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E): EO {
|
||||
return new MyEdge(src, dest, weight ?? 1, val) as EO;
|
||||
createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
||||
return new MyEdge(src, dest, weight ?? 1, value) as EO;
|
||||
}
|
||||
|
||||
setInEdgeMap(value: Map<VO, EO[]>) {
|
||||
|
@ -192,7 +192,7 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|||
expect(edge1).toBeInstanceOf(MyEdge);
|
||||
if (edge1) {
|
||||
expect(edge1.data).toBe('val1');
|
||||
expect(edge1?.val).toBe('val1');
|
||||
expect(edge1?.value).toBe('val1');
|
||||
expect(edge1).toBeInstanceOf(MyEdge);
|
||||
expect(edge1.src).toBe(1);
|
||||
expect(edge1).toEqual(edge2);
|
||||
|
@ -214,7 +214,7 @@ describe('Inherit from DirectedGraph and perform operations', () => {
|
|||
|
||||
expect(removedEdge).toBeInstanceOf(MyEdge);
|
||||
if (removedEdge) {
|
||||
removedEdge && expect(removedEdge.val).toBe('edge-data1-2');
|
||||
removedEdge && expect(removedEdge.value).toBe('edge-data1-2');
|
||||
removedEdge && expect(removedEdge.src).toBe(1);
|
||||
}
|
||||
expect(edgeAfterRemoval).toBeNull();
|
||||
|
|
|
@ -7,7 +7,7 @@ describe('HashNode', () => {
|
|||
const hashNode = new HashTableNode(key, value);
|
||||
|
||||
expect(hashNode.key).toBe(key);
|
||||
expect(hashNode.val).toBe(value);
|
||||
expect(hashNode.value).toBe(value);
|
||||
expect(hashNode.next).toBe(null);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -4,9 +4,9 @@ import {bigO, magnitude} from '../../../utils';
|
|||
describe('DoublyLinkedListNode', () => {
|
||||
it('should DoublyLinkedListNode', () => {
|
||||
const node1 = new DoublyLinkedListNode<number>(2);
|
||||
expect(node1.val).toBe(2);
|
||||
node1.val = 1;
|
||||
expect(node1.val).toBe(1);
|
||||
expect(node1.value).toBe(2);
|
||||
node1.value = 1;
|
||||
expect(node1.value).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -33,13 +33,13 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
|
||||
it('should delete tail', () => {
|
||||
expect(list.delete(list.tail)).toBe(true);
|
||||
expect(list.tail?.val).toBe(4);
|
||||
expect(list.tail?.value).toBe(4);
|
||||
expect(list.delete(6)).toBe(false);
|
||||
expect(list.tail?.val).toBe(4);
|
||||
expect(list.tail?.value).toBe(4);
|
||||
});
|
||||
|
||||
it('should find null', () => {
|
||||
expect(list.find(val => val === 6)).toBe(null);
|
||||
expect(list.find(value => value === 6)).toBe(null);
|
||||
});
|
||||
|
||||
it('should indexOf -1', () => {
|
||||
|
@ -47,7 +47,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
});
|
||||
|
||||
it('should findBackward null', () => {
|
||||
expect(list.findBackward(val => val === 0)).toBe(null);
|
||||
expect(list.findBackward(value => value === 0)).toBe(null);
|
||||
});
|
||||
|
||||
it('should insertAfter tail', () => {
|
||||
|
@ -80,8 +80,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.push(2);
|
||||
list.push(3);
|
||||
expect(list.length).toBe(3);
|
||||
expect(list.head!.val).toBe(1);
|
||||
expect(list.tail!.val).toBe(3);
|
||||
expect(list.head!.value).toBe(1);
|
||||
expect(list.tail!.value).toBe(3);
|
||||
});
|
||||
|
||||
it('should pop elements from the end of the list', () => {
|
||||
|
@ -90,8 +90,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
const poppedValue = list.pop();
|
||||
expect(poppedValue).toBe(2);
|
||||
expect(list.length).toBe(1);
|
||||
expect(list.head!.val).toBe(1);
|
||||
expect(list.tail!.val).toBe(1);
|
||||
expect(list.head!.value).toBe(1);
|
||||
expect(list.tail!.value).toBe(1);
|
||||
});
|
||||
it('should insert elements at specific positions', () => {
|
||||
list.push(1);
|
||||
|
@ -114,7 +114,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.insertAt(5, 4);
|
||||
expect(list.length).toBe(6);
|
||||
expect(list.getAt(5)).toBe(4);
|
||||
expect(list.tail!.val).toBe(4);
|
||||
expect(list.tail!.value).toBe(4);
|
||||
});
|
||||
|
||||
it('should delete elements at specific positions', () => {
|
||||
|
@ -126,12 +126,12 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
const deletedValue = list.deleteAt(0);
|
||||
expect(deletedValue).toBe(1);
|
||||
expect(list.length).toBe(2);
|
||||
expect(list.head!.val).toBe(2);
|
||||
expect(list.head!.value).toBe(2);
|
||||
|
||||
// Deleting from the middle
|
||||
list.deleteAt(0); // Deleting the second element
|
||||
expect(list.length).toBe(1);
|
||||
expect(list.head!.val).toBe(3);
|
||||
expect(list.head!.value).toBe(3);
|
||||
|
||||
// Deleting from the end
|
||||
list.deleteAt(0);
|
||||
|
@ -147,12 +147,12 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
|
||||
list.delete(2);
|
||||
expect(list.length).toBe(2);
|
||||
expect(list.head!.val).toBe(1);
|
||||
expect(list.tail!.val).toBe(3);
|
||||
expect(list.head!.value).toBe(1);
|
||||
expect(list.tail!.value).toBe(3);
|
||||
|
||||
list.delete(1);
|
||||
expect(list.length).toBe(1);
|
||||
expect(list.head!.val).toBe(3);
|
||||
expect(list.head!.value).toBe(3);
|
||||
|
||||
list.delete(3);
|
||||
expect(list.length).toBe(0);
|
||||
|
@ -176,7 +176,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.push(2);
|
||||
list.push(3);
|
||||
|
||||
const mappedList = list.map(val => val * 2);
|
||||
const mappedList = list.map(value => value * 2);
|
||||
|
||||
expect(mappedList.toArray()).toEqual([2, 4, 6]);
|
||||
});
|
||||
|
@ -187,7 +187,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.push(3);
|
||||
list.push(4);
|
||||
|
||||
const filteredList = list.filter(val => val % 2 === 0);
|
||||
const filteredList = list.filter(value => value % 2 === 0);
|
||||
|
||||
expect(filteredList.toArray()).toEqual([2, 4]);
|
||||
});
|
||||
|
@ -198,7 +198,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.push(3);
|
||||
list.push(4);
|
||||
|
||||
const sum = list.reduce((acc, val) => acc + val, 0);
|
||||
const sum = list.reduce((acc, value) => acc + value, 0);
|
||||
|
||||
expect(sum).toBe(10);
|
||||
});
|
||||
|
@ -227,7 +227,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.push(2);
|
||||
list.push(3);
|
||||
|
||||
const found = list.find(val => val % 2 === 0);
|
||||
const found = list.find(value => value % 2 === 0);
|
||||
|
||||
expect(found).toBe(2);
|
||||
});
|
||||
|
@ -248,7 +248,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.push(3);
|
||||
list.push(4);
|
||||
|
||||
const lastEven = list.findBackward(val => val % 2 === 0);
|
||||
const lastEven = list.findBackward(value => value % 2 === 0);
|
||||
|
||||
expect(lastEven).toBe(4);
|
||||
});
|
||||
|
@ -283,8 +283,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.reverse();
|
||||
|
||||
expect(list.toArray()).toEqual([3, 2, 1]);
|
||||
expect(list.head?.val).toBe(3);
|
||||
expect(list.tail?.val).toBe(1);
|
||||
expect(list.head?.value).toBe(3);
|
||||
expect(list.tail?.value).toBe(1);
|
||||
});
|
||||
|
||||
it('should iterate over each element and apply a callback', () => {
|
||||
|
@ -293,8 +293,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.push(3);
|
||||
|
||||
const result: number[] = [];
|
||||
list.forEach(val => {
|
||||
result.push(val * 2);
|
||||
list.forEach(value => {
|
||||
result.push(value * 2);
|
||||
});
|
||||
|
||||
expect(result).toEqual([2, 4, 6]);
|
||||
|
@ -305,7 +305,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.push(2);
|
||||
list.push(3);
|
||||
|
||||
const mappedList = list.map(val => val * 2);
|
||||
const mappedList = list.map(value => value * 2);
|
||||
|
||||
expect(mappedList.toArray()).toEqual([2, 4, 6]);
|
||||
});
|
||||
|
@ -316,7 +316,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.push(3);
|
||||
list.push(4);
|
||||
|
||||
const filteredList = list.filter(val => val % 2 === 0);
|
||||
const filteredList = list.filter(value => value % 2 === 0);
|
||||
|
||||
expect(filteredList.toArray()).toEqual([2, 4]);
|
||||
});
|
||||
|
@ -326,7 +326,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
list.push(2);
|
||||
list.push(3);
|
||||
|
||||
const sum = list.reduce((acc, val) => acc + val, 0);
|
||||
const sum = list.reduce((acc, value) => acc + value, 0);
|
||||
|
||||
expect(sum).toBe(6);
|
||||
});
|
||||
|
@ -387,7 +387,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
expect(insertSuccess).toBe(true);
|
||||
|
||||
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
||||
expect(getNode?.val).toEqual(newObj);
|
||||
expect(getNode?.value).toEqual(newObj);
|
||||
|
||||
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
||||
expect(deleted).toBe(true);
|
||||
|
|
|
@ -26,7 +26,7 @@ describe('LinkedList Performance Test', () => {
|
|||
if (i === midIndex) {
|
||||
midSinglyNode = singlyList.getNode(i);
|
||||
} else if (i > midIndex && midSinglyNode) {
|
||||
singlyList.insertBefore(midSinglyNode.val, i);
|
||||
singlyList.insertBefore(midSinglyNode.value, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,9 +4,9 @@ import {bigO, magnitude} from '../../../utils';
|
|||
describe('SinglyLinkedListNode', () => {
|
||||
it('should SinglyLinkedList', () => {
|
||||
const node1 = new SinglyLinkedListNode<number>(2);
|
||||
expect(node1.val).toBe(2);
|
||||
node1.val = 1;
|
||||
expect(node1.val).toBe(1);
|
||||
expect(node1.value).toBe(2);
|
||||
node1.value = 1;
|
||||
expect(node1.value).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -75,7 +75,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|||
list.push(3);
|
||||
const element = list.getAt(1);
|
||||
expect(element).toBe(2);
|
||||
expect(list.getNodeAt(2)?.val).toBe(3);
|
||||
expect(list.getNodeAt(2)?.value).toBe(3);
|
||||
});
|
||||
|
||||
it('should return undefined for an out-of-bounds index', () => {
|
||||
|
@ -382,7 +382,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|||
expect(insertSuccess).toBe(true);
|
||||
|
||||
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
||||
expect(getNode?.val).toEqual(newObj);
|
||||
expect(getNode?.value).toEqual(newObj);
|
||||
|
||||
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
||||
expect(deleted).toBe(true);
|
||||
|
@ -431,8 +431,8 @@ describe('SinglyLinkedList', () => {
|
|||
it('should push elements to the end of the list', () => {
|
||||
list.push(1);
|
||||
list.push(2);
|
||||
expect(list.head!.val).toBe(1);
|
||||
expect(list.tail!.val).toBe(2);
|
||||
expect(list.head!.value).toBe(1);
|
||||
expect(list.tail!.value).toBe(2);
|
||||
expect(list.length).toBe(2);
|
||||
});
|
||||
|
||||
|
@ -441,8 +441,8 @@ describe('SinglyLinkedList', () => {
|
|||
list.push(2);
|
||||
const popped = list.pop();
|
||||
expect(popped).toBe(2);
|
||||
expect(list.head!.val).toBe(1);
|
||||
expect(list.tail!.val).toBe(1);
|
||||
expect(list.head!.value).toBe(1);
|
||||
expect(list.tail!.value).toBe(1);
|
||||
expect(list.length).toBe(1);
|
||||
});
|
||||
|
||||
|
@ -451,8 +451,8 @@ describe('SinglyLinkedList', () => {
|
|||
list.push(2);
|
||||
list.push(3);
|
||||
list.reverse();
|
||||
expect(list.head!.val).toBe(3);
|
||||
expect(list.tail!.val).toBe(1);
|
||||
expect(list.head!.value).toBe(3);
|
||||
expect(list.tail!.value).toBe(1);
|
||||
// Add more assertions for reversed order.
|
||||
});
|
||||
|
||||
|
@ -470,14 +470,14 @@ describe('SinglyLinkedList', () => {
|
|||
list.push(1);
|
||||
list.push(2);
|
||||
list.push(3);
|
||||
expect(list.filter(val => val !== 2).toArray()).toEqual([1, 3]);
|
||||
expect(list.filter(value => value !== 2).toArray()).toEqual([1, 3]);
|
||||
});
|
||||
|
||||
it('should forEach the list', () => {
|
||||
list.push(1);
|
||||
list.push(2);
|
||||
list.push(3);
|
||||
list.forEach(val => val++);
|
||||
list.forEach(value => value++);
|
||||
expect(list.toArray()).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
|
@ -485,11 +485,11 @@ describe('SinglyLinkedList', () => {
|
|||
list.addLast(1);
|
||||
list.push(2);
|
||||
list.push(3);
|
||||
expect(list.map(val => val * 2).toArray()).toEqual([2, 4, 6]);
|
||||
expect(list.map(value => value * 2).toArray()).toEqual([2, 4, 6]);
|
||||
});
|
||||
|
||||
it('should reduce the list', () => {
|
||||
const list1 = SinglyLinkedList.fromArray([1, 2, 3]);
|
||||
expect(list1.reduce((acc, val) => acc + val, 0)).toEqual(6);
|
||||
expect(list1.reduce((acc, value) => acc + value, 0)).toEqual(6);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -59,19 +59,19 @@ function findPotentialN(input: any): number {
|
|||
function linearRegression(x: number[], y: number[]) {
|
||||
const n = x.length;
|
||||
|
||||
const sumX = x.reduce((acc, val) => acc + val, 0);
|
||||
const sumY = y.reduce((acc, val) => acc + val, 0);
|
||||
const sumX = x.reduce((acc, value) => acc + value, 0);
|
||||
const sumY = y.reduce((acc, value) => acc + value, 0);
|
||||
|
||||
const sumXSquared = x.reduce((acc, val) => acc + val ** 2, 0);
|
||||
const sumXY = x.reduce((acc, val, i) => acc + val * y[i], 0);
|
||||
const sumXSquared = x.reduce((acc, value) => acc + value ** 2, 0);
|
||||
const sumXY = x.reduce((acc, value, i) => acc + value * y[i], 0);
|
||||
|
||||
const slope = (n * sumXY - sumX * sumY) / (n * sumXSquared - sumX ** 2);
|
||||
const intercept = (sumY - slope * sumX) / n;
|
||||
|
||||
const yHat = x.map(val => slope * val + intercept);
|
||||
const yHat = x.map(value => slope * value + intercept);
|
||||
|
||||
const totalVariation = y.map((val, i) => (val - yHat[i]) ** 2).reduce((acc, val) => acc + val, 0);
|
||||
const explainedVariation = y.map(val => (val - sumY / n) ** 2).reduce((acc, val) => acc + val, 0);
|
||||
const totalVariation = y.map((value, i) => (value - yHat[i]) ** 2).reduce((acc, value) => acc + value, 0);
|
||||
const explainedVariation = y.map(value => (value - sumY / n) ** 2).reduce((acc, value) => acc + value, 0);
|
||||
|
||||
const rSquared = 1 - totalVariation / explainedVariation;
|
||||
|
||||
|
|
Loading…
Reference in a new issue