mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
style: reformat code with IDE
This commit is contained in:
parent
124b676685
commit
1064ad4a58
|
@ -42,7 +42,9 @@ module.exports = {
|
|||
"{}": false
|
||||
}
|
||||
}
|
||||
]
|
||||
],
|
||||
"brace-style": ["error", "1tbs", { "allowSingleLine": true }],
|
||||
"object-curly-spacing": ["error", "always"]
|
||||
},
|
||||
"settings": {
|
||||
"import/parsers": {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module.exports = {
|
||||
"arrowParens": "avoid",
|
||||
"bracketSpacing": false,
|
||||
"bracketSpacing": true,
|
||||
"htmlWhitespaceSensitivity": "css",
|
||||
"insertPragma": false,
|
||||
"bracketSameLine": true,
|
||||
|
|
|
@ -21,8 +21,7 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|||
|
||||
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
||||
extends BST<V, N>
|
||||
implements IBinaryTree<V, N>
|
||||
{
|
||||
implements IBinaryTree<V, N> {
|
||||
/**
|
||||
* This is a constructor function for an AVL tree data structure in TypeScript.
|
||||
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
||||
|
@ -114,7 +113,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|||
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
||||
* if either `srcNode` or `destNode` is undefined.
|
||||
*/
|
||||
protected override _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
|
||||
protected override _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
|
||||
srcNode = this.ensureNotKey(srcNode);
|
||||
destNode = this.ensureNotKey(destNode);
|
||||
|
||||
|
@ -210,7 +209,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|||
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
||||
switch (
|
||||
this._balanceFactor(A) // second O(1)
|
||||
) {
|
||||
) {
|
||||
case -2:
|
||||
if (A && A.left) {
|
||||
if (this._balanceFactor(A.left) <= 0) {
|
||||
|
|
|
@ -17,7 +17,7 @@ export class BinaryIndexedTree {
|
|||
* @param - - `frequency`: The default frequency value. It is optional and has a default
|
||||
* value of 0.
|
||||
*/
|
||||
constructor({frequency = 0, max}: {frequency?: number; max: number}) {
|
||||
constructor({frequency = 0, max}: { frequency?: number; max: number }) {
|
||||
this._freq = frequency;
|
||||
this._max = max;
|
||||
this._freqMap = {0: 0};
|
||||
|
|
|
@ -1722,6 +1722,74 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
||||
* @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
|
||||
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
||||
* following types:
|
||||
*/
|
||||
print(beginRoot: BTNKey | N | null | undefined = this.root): void {
|
||||
beginRoot = this.ensureNotKey(beginRoot);
|
||||
if (!beginRoot) return;
|
||||
|
||||
const display = (root: N | null | undefined): void => {
|
||||
const [lines, , ,] = _displayAux(root);
|
||||
for (const line of lines) {
|
||||
console.log(line);
|
||||
}
|
||||
};
|
||||
|
||||
const _displayAux = (node: N | null | undefined): [string[], number, number, number] => {
|
||||
if (!this.isRealNode(node)) {
|
||||
return [[], 0, 0, 0];
|
||||
}
|
||||
|
||||
if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
|
||||
const line = `${node.key}`;
|
||||
const width = line.length;
|
||||
const height = 1;
|
||||
const middle = Math.floor(width / 2);
|
||||
return [[line], width, height, middle];
|
||||
}
|
||||
|
||||
if (this.isRealNode(node) && !this.isRealNode(node.right)) {
|
||||
const [lines, n, p, x] = _displayAux(node.left);
|
||||
const s = `${node.key}`;
|
||||
const u = s.length;
|
||||
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
||||
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
||||
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
||||
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
||||
}
|
||||
|
||||
if (this.isRealNode(node) && !this.isRealNode(node.left)) {
|
||||
const [lines, n, p, u] = _displayAux(node.right);
|
||||
const s = `${node.key}`;
|
||||
const x = s.length;
|
||||
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
||||
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
||||
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
||||
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
||||
}
|
||||
|
||||
const [left, n, p, x] = _displayAux(node.left);
|
||||
const [right, m, q, y] = _displayAux(node.right);
|
||||
const s = `${node.key}`;
|
||||
const u = s.length;
|
||||
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
||||
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
||||
if (p < q) {
|
||||
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
||||
} else if (q < p) {
|
||||
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
||||
}
|
||||
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
||||
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
||||
};
|
||||
|
||||
display(beginRoot);
|
||||
}
|
||||
|
||||
protected _defaultOneParamCallback = (node: N) => node.key;
|
||||
|
||||
/**
|
||||
|
@ -1801,73 +1869,4 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
}
|
||||
this._root = v;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
||||
* @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
|
||||
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
||||
* following types:
|
||||
*/
|
||||
print(beginRoot: BTNKey | N | null | undefined = this.root): void {
|
||||
beginRoot = this.ensureNotKey(beginRoot);
|
||||
if (!beginRoot) return;
|
||||
|
||||
const display = (root: N | null | undefined): void => {
|
||||
const [lines, , ,] = _displayAux(root);
|
||||
for (const line of lines) {
|
||||
console.log(line);
|
||||
}
|
||||
};
|
||||
|
||||
const _displayAux = (node: N | null | undefined): [string[], number, number, number] => {
|
||||
if (!this.isRealNode(node)) {
|
||||
return [[], 0, 0, 0];
|
||||
}
|
||||
|
||||
if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
|
||||
const line = `${node.key}`;
|
||||
const width = line.length;
|
||||
const height = 1;
|
||||
const middle = Math.floor(width / 2);
|
||||
return [[line], width, height, middle];
|
||||
}
|
||||
|
||||
if (this.isRealNode(node) && !this.isRealNode(node.right)) {
|
||||
const [lines, n, p, x] = _displayAux(node.left);
|
||||
const s = `${node.key}`;
|
||||
const u = s.length;
|
||||
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
||||
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
||||
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
||||
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
||||
}
|
||||
|
||||
if (this.isRealNode(node) && !this.isRealNode(node.left)) {
|
||||
const [lines, n, p, u] = _displayAux(node.right);
|
||||
const s = `${node.key}`;
|
||||
const x = s.length;
|
||||
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
||||
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
||||
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
||||
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
||||
}
|
||||
|
||||
const [left, n, p, x] = _displayAux(node.left);
|
||||
const [right, m, q, y] = _displayAux(node.right);
|
||||
const s = `${node.key}`;
|
||||
const u = s.length;
|
||||
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
||||
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
||||
if (p < q) {
|
||||
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
||||
} else if (q < p) {
|
||||
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
||||
}
|
||||
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
||||
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
||||
};
|
||||
|
||||
display(beginRoot);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ import {IBinaryTree} from '../../interfaces';
|
|||
import {Queue} from '../queue';
|
||||
|
||||
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
||||
override parent?: N ;
|
||||
override parent?: N;
|
||||
|
||||
constructor(key: BTNKey, value?: V) {
|
||||
super(key, value);
|
||||
|
@ -21,7 +21,7 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|||
this._right = undefined;
|
||||
}
|
||||
|
||||
protected override _left?: N ;
|
||||
protected override _left?: N;
|
||||
|
||||
/**
|
||||
* Get the left child node.
|
||||
|
@ -42,7 +42,7 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|||
}
|
||||
|
||||
|
||||
protected override _right?: N ;
|
||||
protected override _right?: N;
|
||||
|
||||
/**
|
||||
* Get the right child node.
|
||||
|
@ -83,7 +83,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
}
|
||||
}
|
||||
|
||||
protected override _root?: N ;
|
||||
protected override _root?: N;
|
||||
|
||||
/**
|
||||
* Get the root node of the binary tree.
|
||||
|
@ -113,7 +113,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
/**
|
||||
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
||||
* Space Complexity: O(1) - Constant space is used.
|
||||
*
|
||||
*
|
||||
* The `add` function adds a new node to a binary search tree based on the provided key and value.
|
||||
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
||||
* following types:
|
||||
|
@ -193,7 +193,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
||||
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
||||
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
||||
|
@ -228,12 +228,12 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
if (!isBalanceAdd || !hasNoUndefined(keysOrNodes)) {
|
||||
return super.addMany(keysOrNodes, data).map(n => n ?? undefined);
|
||||
}
|
||||
|
||||
|
||||
const inserted: (N | undefined)[] = [];
|
||||
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
||||
(value: BTNKey | N, index) => [value, data?.[index]] as [BTNKey | N, V]
|
||||
);
|
||||
|
||||
|
||||
let sorted = [];
|
||||
|
||||
function _isNodeOrUndefinedTuple(arr: [BTNKey | N, V][]): arr is [N, V][] {
|
||||
|
@ -344,7 +344,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
const _dfs = (cur: N): N | undefined => {
|
||||
if (cur.key === key) return cur;
|
||||
if (!cur.left && !cur.right) return;
|
||||
|
||||
|
||||
if (this._compare(cur.key, key) === CP.gt && cur.left) return _dfs(cur.left);
|
||||
if (this._compare(cur.key, key) === CP.lt && cur.right) return _dfs(cur.right);
|
||||
};
|
||||
|
@ -380,7 +380,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
||||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
||||
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
||||
|
|
|
@ -12,8 +12,8 @@ import {
|
|||
BTNKey,
|
||||
IterationType,
|
||||
RBTNColor,
|
||||
RedBlackTreeNodeNested,
|
||||
RBTreeOptions
|
||||
RBTreeOptions,
|
||||
RedBlackTreeNodeNested
|
||||
} from '../../types';
|
||||
import {BST, BSTNode} from "./bst";
|
||||
import {IBinaryTree} from "../../interfaces";
|
||||
|
@ -21,6 +21,7 @@ import {BinaryTreeNode} from "./binary-tree";
|
|||
|
||||
export class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
||||
color: RBTNColor;
|
||||
|
||||
constructor(key: BTNKey, value?: V, color: RBTNColor = RBTNColor.BLACK) {
|
||||
super(key, value);
|
||||
this.color = color;
|
||||
|
@ -36,8 +37,9 @@ export class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBla
|
|||
*/
|
||||
export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>>
|
||||
extends BST<V, N>
|
||||
implements IBinaryTree<V, N>
|
||||
{
|
||||
implements IBinaryTree<V, N> {
|
||||
|
||||
NIL: N = new RedBlackTreeNode<V>(NaN) as unknown as N;
|
||||
|
||||
/**
|
||||
* The constructor function initializes a Red-Black Tree with an optional set of options.
|
||||
|
@ -61,8 +63,6 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|||
return this._size;
|
||||
}
|
||||
|
||||
NIL: N = new RedBlackTreeNode<V>(NaN) as unknown as N;
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
||||
* Space Complexity: O(1)
|
||||
|
@ -83,7 +83,7 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|||
let node: N;
|
||||
if (this.isNodeKey(keyOrNode)) {
|
||||
node = this.createNode(keyOrNode, value, RBTNColor.RED);
|
||||
} else if(keyOrNode instanceof RedBlackTreeNode) {
|
||||
} else if (keyOrNode instanceof RedBlackTreeNode) {
|
||||
node = keyOrNode;
|
||||
} else if (keyOrNode === null) {
|
||||
return;
|
||||
|
@ -226,21 +226,21 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|||
callback?: C,
|
||||
beginRoot?: N | undefined,
|
||||
iterationType?: IterationType
|
||||
): N | undefined;
|
||||
): N | undefined;
|
||||
|
||||
getNode<C extends BTNCallback<N, N>>(
|
||||
identifier: N | undefined,
|
||||
callback?: C,
|
||||
beginRoot?: N | undefined,
|
||||
iterationType?: IterationType
|
||||
): N | undefined;
|
||||
): N | undefined;
|
||||
|
||||
getNode<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C>,
|
||||
callback: C,
|
||||
beginRoot?: N | undefined,
|
||||
iterationType?: IterationType
|
||||
): N | undefined;
|
||||
): N | undefined;
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
||||
|
|
|
@ -160,52 +160,6 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
||||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
||||
*
|
||||
* The function adds a new node to a binary tree, either as the left child or the right child of a
|
||||
* given parent node.
|
||||
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
||||
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
||||
* `undefined` if there is no node to add.
|
||||
* @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
|
||||
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
||||
* (`BTNKey`).
|
||||
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
||||
* added, or `undefined` if no node was added.
|
||||
*/
|
||||
protected override _addTo(newNode: N | undefined, parent: BTNKey | N | undefined): N | undefined {
|
||||
parent = this.ensureNotKey(parent);
|
||||
if (parent) {
|
||||
if (parent.left === undefined) {
|
||||
parent.left = newNode;
|
||||
if (newNode !== undefined) {
|
||||
this._size = this.size + 1;
|
||||
this._count += newNode.count;
|
||||
}
|
||||
|
||||
return parent.left;
|
||||
} else if (parent.right === undefined) {
|
||||
parent.right = newNode;
|
||||
if (newNode !== undefined) {
|
||||
this._size = this.size + 1;
|
||||
this._count += newNode.count;
|
||||
}
|
||||
return parent.right;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
||||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
||||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
||||
|
@ -241,8 +195,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
||||
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
||||
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
||||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -295,8 +249,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
||||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
||||
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
||||
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -330,7 +284,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|||
if (!curr) return deletedResult;
|
||||
|
||||
const parent: N | undefined = curr?.parent ? curr.parent : undefined;
|
||||
let needBalanced: N | undefined = undefined,orgCurrent: N | undefined = curr;
|
||||
let needBalanced: N | undefined = undefined, orgCurrent: N | undefined = curr;
|
||||
|
||||
if (curr.count > 1 && !ignoreCount) {
|
||||
curr.count--;
|
||||
|
@ -377,6 +331,11 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|||
return deletedResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
||||
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The clear() function clears the contents of a data structure and sets the count to zero.
|
||||
*/
|
||||
|
@ -385,6 +344,47 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|||
this._count = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
||||
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
||||
*
|
||||
* The function adds a new node to a binary tree, either as the left child or the right child of a
|
||||
* given parent node.
|
||||
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
||||
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
||||
* `undefined` if there is no node to add.
|
||||
* @param {BTNKey | N | undefined} parent - The `parent` parameter represents the parent node to
|
||||
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
||||
* (`BTNKey`).
|
||||
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
||||
* added, or `undefined` if no node was added.
|
||||
*/
|
||||
protected override _addTo(newNode: N | undefined, parent: BTNKey | N | undefined): N | undefined {
|
||||
parent = this.ensureNotKey(parent);
|
||||
if (parent) {
|
||||
if (parent.left === undefined) {
|
||||
parent.left = newNode;
|
||||
if (newNode !== undefined) {
|
||||
this._size = this.size + 1;
|
||||
this._count += newNode.count;
|
||||
}
|
||||
|
||||
return parent.left;
|
||||
} else if (parent.right === undefined) {
|
||||
parent.right = newNode;
|
||||
if (newNode !== undefined) {
|
||||
this._size = this.size + 1;
|
||||
this._count += newNode.count;
|
||||
}
|
||||
return parent.right;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The `_swap` function swaps the key, value, count, and height properties between two nodes.
|
||||
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
||||
|
@ -394,7 +394,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|||
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
||||
* if either `srcNode` or `destNode` is undefined.
|
||||
*/
|
||||
protected _swap(srcNode: BTNKey | N | undefined, destNode:BTNKey | N | undefined): N | undefined{
|
||||
protected _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
|
||||
srcNode = this.ensureNotKey(srcNode);
|
||||
destNode = this.ensureNotKey(destNode);
|
||||
if (srcNode && destNode) {
|
||||
|
@ -416,6 +416,6 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|||
|
||||
return destNode;
|
||||
}
|
||||
return undefined;
|
||||
return undefined;
|
||||
}
|
||||
}
|
|
@ -64,8 +64,7 @@ export abstract class AbstractGraph<
|
|||
E = any,
|
||||
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
||||
EO extends AbstractEdge<E> = AbstractEdge<E>
|
||||
> implements IGraph<V, E, VO, EO>
|
||||
{
|
||||
> implements IGraph<V, E, VO, EO> {
|
||||
protected _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
|
||||
|
||||
get vertices(): Map<VertexKey, VO> {
|
||||
|
@ -301,7 +300,7 @@ export abstract class AbstractGraph<
|
|||
return [];
|
||||
}
|
||||
|
||||
const stack: {vertex: VO; path: VO[]}[] = [];
|
||||
const stack: { vertex: VO; path: VO[] }[] = [];
|
||||
stack.push({vertex: vertex1, path: [vertex1]});
|
||||
|
||||
while (stack.length > 0) {
|
||||
|
@ -496,7 +495,7 @@ export abstract class AbstractGraph<
|
|||
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
||||
*/
|
||||
|
||||
/**
|
||||
/**
|
||||
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
||||
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
||||
*
|
||||
|
@ -615,14 +614,14 @@ export abstract class AbstractGraph<
|
|||
}
|
||||
|
||||
getMinDist &&
|
||||
distMap.forEach((d, v) => {
|
||||
if (v !== srcVertex) {
|
||||
if (d < minDist) {
|
||||
minDist = d;
|
||||
if (genPaths) minDest = v;
|
||||
}
|
||||
distMap.forEach((d, v) => {
|
||||
if (v !== srcVertex) {
|
||||
if (d < minDist) {
|
||||
minDist = d;
|
||||
if (genPaths) minDest = v;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
genPaths && getPaths(minDest);
|
||||
|
||||
|
@ -643,7 +642,7 @@ export abstract class AbstractGraph<
|
|||
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
||||
*/
|
||||
|
||||
/**
|
||||
/**
|
||||
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
||||
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
||||
*
|
||||
|
@ -692,7 +691,7 @@ export abstract class AbstractGraph<
|
|||
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
||||
}
|
||||
|
||||
const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
|
||||
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);
|
||||
|
@ -924,7 +923,7 @@ export abstract class AbstractGraph<
|
|||
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
||||
* path between vertices in the
|
||||
*/
|
||||
floydWarshall(): {costs: number[][]; predecessor: (VO | null)[][]} {
|
||||
floydWarshall(): { costs: number[][]; predecessor: (VO | null)[][] } {
|
||||
const idAndVertices = [...this._vertices];
|
||||
const n = idAndVertices.length;
|
||||
|
||||
|
|
|
@ -46,14 +46,13 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|||
}
|
||||
|
||||
export class DirectedGraph<
|
||||
V = any,
|
||||
E = any,
|
||||
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
||||
EO extends DirectedEdge<E> = DirectedEdge<E>
|
||||
>
|
||||
V = any,
|
||||
E = any,
|
||||
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
||||
EO extends DirectedEdge<E> = DirectedEdge<E>
|
||||
>
|
||||
extends AbstractGraph<V, E, VO, EO>
|
||||
implements IGraph<V, E, VO, EO>
|
||||
{
|
||||
implements IGraph<V, E, VO, EO> {
|
||||
/**
|
||||
* The constructor function initializes an instance of a class.
|
||||
*/
|
||||
|
|
|
@ -43,14 +43,13 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|||
}
|
||||
|
||||
export class UndirectedGraph<
|
||||
V = any,
|
||||
E = any,
|
||||
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
||||
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
||||
>
|
||||
V = any,
|
||||
E = any,
|
||||
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
||||
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
||||
>
|
||||
extends AbstractGraph<V, E, VO, EO>
|
||||
implements IGraph<V, E, VO, EO>
|
||||
{
|
||||
implements IGraph<V, E, VO, EO> {
|
||||
/**
|
||||
* The constructor initializes a new Map object to store edges.
|
||||
*/
|
||||
|
|
|
@ -133,7 +133,7 @@ export class HashMap<K, V> {
|
|||
}
|
||||
}
|
||||
|
||||
*entries(): IterableIterator<[K, V]> {
|
||||
* entries(): IterableIterator<[K, V]> {
|
||||
for (const bucket of this.table) {
|
||||
if (bucket) {
|
||||
for (const [key, value] of bucket) {
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
export class TreeMap {}
|
||||
export class TreeMap {
|
||||
}
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
export class TreeSet {}
|
||||
export class TreeSet {
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
import type {Comparator, DFSOrderPattern} from '../../types';
|
||||
|
||||
export class Heap<E = any> {
|
||||
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
||||
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
||||
this._comparator = options.comparator;
|
||||
if (options.nodes && options.nodes.length > 0) {
|
||||
this._nodes = options.nodes;
|
||||
|
@ -48,7 +48,7 @@ export class Heap<E = any> {
|
|||
* @returns A new Heap instance.
|
||||
* @param options
|
||||
*/
|
||||
static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
|
||||
static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> {
|
||||
return new Heap<E>(options);
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
|
|||
|
||||
export class MaxHeap<E = any> extends Heap<E> {
|
||||
constructor(
|
||||
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
||||
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
||||
comparator: (a: E, b: E) => {
|
||||
if (!(typeof a === 'number' && typeof b === 'number')) {
|
||||
throw new Error('The a, b params of compare function must be number');
|
||||
|
|
|
@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
|
|||
|
||||
export class MinHeap<E = any> extends Heap<E> {
|
||||
constructor(
|
||||
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
||||
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
||||
comparator: (a: E, b: E) => {
|
||||
if (!(typeof a === 'number' && typeof b === 'number')) {
|
||||
throw new Error('The a, b params of compare function must be number');
|
||||
|
|
|
@ -826,7 +826,7 @@ export class DoublyLinkedList<E = any> {
|
|||
/**
|
||||
* The function returns an iterator that iterates over the values of a linked list.
|
||||
*/
|
||||
*[Symbol.iterator]() {
|
||||
* [Symbol.iterator]() {
|
||||
let current = this.head;
|
||||
|
||||
while (current) {
|
||||
|
|
|
@ -773,7 +773,7 @@ export class SinglyLinkedList<E = any> {
|
|||
/**
|
||||
* The function returns an iterator that iterates over the values of a linked list.
|
||||
*/
|
||||
*[Symbol.iterator]() {
|
||||
* [Symbol.iterator]() {
|
||||
let current = this.head;
|
||||
|
||||
while (current) {
|
||||
|
|
|
@ -14,7 +14,7 @@ export class MatrixNTI2D<V = any> {
|
|||
* given initial value or 0 if not provided.
|
||||
* @param options - An object containing the following properties:
|
||||
*/
|
||||
constructor(options: {row: number; col: number; initialVal?: V}) {
|
||||
constructor(options: { row: number; col: number; initialVal?: V }) {
|
||||
const {row, col, initialVal} = options;
|
||||
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
||||
}
|
||||
|
|
|
@ -10,7 +10,8 @@ export class Vector2D {
|
|||
public x: number = 0,
|
||||
public y: number = 0,
|
||||
public w: number = 1 // needed for matrix multiplication
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The function checks if the x and y values of a point are both zero.
|
||||
|
|
|
@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|||
|
||||
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
||||
constructor(
|
||||
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
||||
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
||||
comparator: (a: E, b: E) => {
|
||||
if (!(typeof a === 'number' && typeof b === 'number')) {
|
||||
throw new Error('The a, b params of compare function must be number');
|
||||
|
|
|
@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|||
|
||||
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
||||
constructor(
|
||||
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
||||
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
||||
comparator: (a: E, b: E) => {
|
||||
if (!(typeof a === 'number' && typeof b === 'number')) {
|
||||
throw new Error('The a, b params of compare function must be number');
|
||||
|
|
|
@ -10,7 +10,7 @@ import {Heap} from '../heap';
|
|||
import {Comparator} from '../../types';
|
||||
|
||||
export class PriorityQueue<E = any> extends Heap<E> {
|
||||
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
||||
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
||||
super(options);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,8 @@ import {DoublyLinkedList} from '../linked-list';
|
|||
|
||||
// O(n) time complexity of obtaining the value
|
||||
// O(1) time complexity of adding at the beginning and the end
|
||||
export class Deque<E = any> extends DoublyLinkedList<E> {}
|
||||
export class Deque<E = any> extends DoublyLinkedList<E> {
|
||||
}
|
||||
|
||||
// O(1) time complexity of obtaining the value
|
||||
// O(n) time complexity of adding at the beginning and the end
|
||||
|
@ -19,9 +20,9 @@ export class ObjectDeque<E = number> {
|
|||
if (capacity !== undefined) this._capacity = capacity;
|
||||
}
|
||||
|
||||
protected _nodes: {[key: number]: E} = {};
|
||||
protected _nodes: { [key: number]: E } = {};
|
||||
|
||||
get nodes(): {[p: number]: E} {
|
||||
get nodes(): { [p: number]: E } {
|
||||
return this._nodes;
|
||||
}
|
||||
|
||||
|
|
|
@ -300,7 +300,7 @@ export class Queue<E = any> {
|
|||
return new Queue(this.nodes.slice(this.offset));
|
||||
}
|
||||
|
||||
*[Symbol.iterator]() {
|
||||
* [Symbol.iterator]() {
|
||||
for (const item of this.nodes) {
|
||||
yield item;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {BinaryTreeNode} from '../data-structures';
|
||||
import {BiTreeDeleteResult, BinaryTreeNodeNested, BTNCallback, BTNKey} from '../types';
|
||||
import {BinaryTreeNodeNested, BiTreeDeleteResult, BTNCallback, BTNKey} from '../types';
|
||||
|
||||
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
||||
createNode(key: BTNKey, value?: N['value']): N;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
export type Direction = 'up' | 'right' | 'down' | 'left';
|
||||
|
||||
export type Turning = {[key in Direction]: Direction};
|
||||
export type Turning = { [key in Direction]: Direction };
|
||||
|
||||
export type NavigatorParams<T = any> = {
|
||||
matrix: T[][];
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
export type ToThunkFn = () => ReturnType<TrlFn>;
|
||||
export type Thunk = () => ReturnType<ToThunkFn> & {__THUNK__: symbol};
|
||||
export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: symbol };
|
||||
export type TrlFn = (...args: any[]) => any;
|
||||
export type TrlAsyncFn = (...args: any[]) => any;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
export type KeyValueObject = {[key: string]: any};
|
||||
export type KeyValueObject = { [key: string]: any };
|
||||
|
||||
export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol};
|
||||
export type KeyValueObjectWithKey = { [key: string]: any; key: string | number | symbol };
|
||||
|
||||
export type NonNumberNonObjectButDefined = string | boolean | symbol | null;
|
||||
|
||||
|
|
|
@ -183,7 +183,7 @@ describe('Individual package BST operations test', () => {
|
|||
});
|
||||
|
||||
it('should perform various operations on a Binary Search Tree with object values', () => {
|
||||
const objBST = new BST<{key: number; keyA: number}>();
|
||||
const objBST = new BST<{ key: number; keyA: number }>();
|
||||
expect(objBST).toBeInstanceOf(BST);
|
||||
objBST.add(11, {key: 11, keyA: 11});
|
||||
objBST.add(3, {key: 3, keyA: 3});
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<head>
|
||||
<meta charset='UTF-8'>
|
||||
<title>CDN Test</title>
|
||||
<!-- <script src="../../dist/umd/data-structure-typed.min.js"></script>-->
|
||||
<!-- <script src="../../dist/umd/data-structure-typed.min.js"></script>-->
|
||||
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
|
||||
<script src='https://unpkg.com/js-sdsl@4.4.2/dist/umd/js-sdsl.js'></script>
|
||||
</head>
|
||||
|
@ -56,7 +56,7 @@
|
|||
const tree = new BinaryTree();
|
||||
tree.add(3);
|
||||
tree.add(12);
|
||||
tree.addMany([1, 6, 9, 8,5,2,3,4,7])
|
||||
tree.addMany([1, 6, 9, 8, 5, 2, 3, 4, 7])
|
||||
tree.add(10);
|
||||
console.log(tree.isPerfectlyBalanced());
|
||||
tree.print();
|
||||
|
|
|
@ -15,25 +15,25 @@ suite
|
|||
list.unshift(i);
|
||||
}
|
||||
})
|
||||
if (isCompetitor) {
|
||||
suite.add(`${LINEAR.toLocaleString()} competitor unshift`, () => {
|
||||
const list = new CLinkedList<number>();
|
||||
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
list.pushFront(i);
|
||||
}
|
||||
})
|
||||
}
|
||||
suite.add(`${LINEAR.toLocaleString()} unshift & shift`, () => {
|
||||
const list = new DoublyLinkedList<number>();
|
||||
if (isCompetitor) {
|
||||
suite.add(`${LINEAR.toLocaleString()} competitor unshift`, () => {
|
||||
const list = new CLinkedList<number>();
|
||||
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
list.unshift(i);
|
||||
}
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
list.shift();
|
||||
list.pushFront(i);
|
||||
}
|
||||
})
|
||||
}
|
||||
suite.add(`${LINEAR.toLocaleString()} unshift & shift`, () => {
|
||||
const list = new DoublyLinkedList<number>();
|
||||
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
list.unshift(i);
|
||||
}
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
list.shift();
|
||||
}
|
||||
})
|
||||
.add(`${LINEAR.toLocaleString()} insertBefore`, () => {
|
||||
const doublyList = new DoublyLinkedList<number>();
|
||||
let midNode: DoublyLinkedListNode | null = null;
|
||||
|
|
|
@ -14,18 +14,18 @@ suite
|
|||
deque.push(i);
|
||||
}
|
||||
})
|
||||
if (isCompetitor) {
|
||||
suite.add(`${LINEAR.toLocaleString()} competitor push`, () => {
|
||||
const deque = new CDeque<number>();
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
deque.pushBack(i);
|
||||
}
|
||||
})
|
||||
}
|
||||
suite.add(`${LINEAR.toLocaleString()} shift`, () => {
|
||||
const deque = new Deque<number>();
|
||||
if (isCompetitor) {
|
||||
suite.add(`${LINEAR.toLocaleString()} competitor push`, () => {
|
||||
const deque = new CDeque<number>();
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
deque.push(i);
|
||||
deque.shift();
|
||||
deque.pushBack(i);
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
suite.add(`${LINEAR.toLocaleString()} shift`, () => {
|
||||
const deque = new Deque<number>();
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
deque.push(i);
|
||||
deque.shift();
|
||||
}
|
||||
});
|
||||
|
|
|
@ -15,22 +15,22 @@ suite
|
|||
queue.push(i);
|
||||
}
|
||||
})
|
||||
if (isCompetitor) {
|
||||
suite.add(`${LINEAR.toLocaleString()} competitor push`, () => {
|
||||
const queue = new CQueue<number>();
|
||||
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
queue.push(i);
|
||||
}
|
||||
})
|
||||
}
|
||||
suite.add(`${LINEAR.toLocaleString()} push & shift`, () => {
|
||||
const queue = new Queue<number>();
|
||||
if (isCompetitor) {
|
||||
suite.add(`${LINEAR.toLocaleString()} competitor push`, () => {
|
||||
const queue = new CQueue<number>();
|
||||
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
queue.push(i);
|
||||
queue.shift();
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
suite.add(`${LINEAR.toLocaleString()} push & shift`, () => {
|
||||
const queue = new Queue<number>();
|
||||
|
||||
for (let i = 0; i < LINEAR; i++) {
|
||||
queue.push(i);
|
||||
queue.shift();
|
||||
}
|
||||
});
|
||||
|
||||
export {suite};
|
||||
|
|
|
@ -11,7 +11,7 @@ const reportDistPath = path.join(parentDirectory, 'benchmark');
|
|||
const testDir = path.join(__dirname, 'data-structures');
|
||||
const testFiles = fastGlob.sync(path.join(testDir, '**', '*.test.ts'));
|
||||
|
||||
const report: {[key: string]: any} = {};
|
||||
const report: { [key: string]: any } = {};
|
||||
|
||||
let completedCount = 0;
|
||||
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
import * as Benchmark from 'benchmark';
|
||||
|
||||
export type PerformanceTest = {testName: string; suite: Benchmark.Suite; file: string};
|
||||
export type PerformanceTest = { testName: string; suite: Benchmark.Suite; file: string };
|
||||
|
|
|
@ -1 +1 @@
|
|||
export type Json2htmlOptions = {plainHtml?: boolean} & Partial<{[key: string]: any}>;
|
||||
export type Json2htmlOptions = { plainHtml?: boolean } & Partial<{ [key: string]: any }>;
|
||||
|
|
|
@ -219,7 +219,7 @@ describe('AVL Tree Test recursively', () => {
|
|||
});
|
||||
|
||||
describe('AVLTree APIs test', () => {
|
||||
const avl = new AVLTree<{id: number; text: string}>();
|
||||
const avl = new AVLTree<{ id: number; text: string }>();
|
||||
beforeEach(() => {
|
||||
avl.clear();
|
||||
});
|
||||
|
@ -268,7 +268,7 @@ describe('AVLTree', () => {
|
|||
});
|
||||
|
||||
describe('BinaryTree APIs test', () => {
|
||||
const avl = new AVLTree<{id: number; text: string}>();
|
||||
const avl = new AVLTree<{ id: number; text: string }>();
|
||||
beforeEach(() => {
|
||||
avl.clear();
|
||||
});
|
||||
|
|
|
@ -247,10 +247,10 @@ describe('BinaryTree', () => {
|
|||
expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.ITERATIVE)).toEqual([6, 3, 7]);
|
||||
expect(tree.subTreeTraverse(node => node.key, tree.getNode(6), IterationType.RECURSIVE)).toEqual([6, 3, 7]);
|
||||
expect(
|
||||
tree.subTreeTraverse(node => (node ? node.key : null ), tree.getNode(6), IterationType.ITERATIVE, true)
|
||||
tree.subTreeTraverse(node => (node ? node.key : null), tree.getNode(6), IterationType.ITERATIVE, true)
|
||||
).toEqual([6, 3, 7, null]);
|
||||
expect(
|
||||
tree.subTreeTraverse(node => (node ? node.key : null ), tree.getNode(6), IterationType.RECURSIVE, true)
|
||||
tree.subTreeTraverse(node => (node ? node.key : null), tree.getNode(6), IterationType.RECURSIVE, true)
|
||||
).toEqual([6, 3, 7, null]);
|
||||
});
|
||||
|
||||
|
@ -324,10 +324,10 @@ describe('BinaryTree traversals', () => {
|
|||
const arr = [35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55];
|
||||
tree.refill(arr);
|
||||
expect(
|
||||
tree.bfs(node => node, tree.root, IterationType.ITERATIVE, true).map(node => (node ? node.key : null ))
|
||||
tree.bfs(node => node, tree.root, IterationType.ITERATIVE, true).map(node => (node ? node.key : null))
|
||||
).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
|
||||
expect(
|
||||
tree.bfs(node => node, tree.root, IterationType.RECURSIVE, true).map(node => (node ? node.key : null ))
|
||||
tree.bfs(node => node, tree.root, IterationType.RECURSIVE, true).map(node => (node ? node.key : null))
|
||||
).toEqual([35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55]);
|
||||
expect(
|
||||
tree.bfs(node => node, tree.root, IterationType.ITERATIVE).map(node => (node === null ? null : node.key))
|
||||
|
@ -343,12 +343,12 @@ describe('BinaryTree traversals', () => {
|
|||
expect(
|
||||
tree
|
||||
.dfs(node => node, 'pre', tree.root, IterationType.ITERATIVE, true)
|
||||
.map(node => (node ? node.key : null ))
|
||||
.map(node => (node ? node.key : null))
|
||||
).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
|
||||
expect(
|
||||
tree
|
||||
.dfs(node => node, 'pre', tree.root, IterationType.RECURSIVE, true)
|
||||
.map(node => (node ? node.key : null ))
|
||||
.map(node => (node ? node.key : null))
|
||||
).toEqual([35, 20, 15, null, 16, 29, 28, 30, 40, null, 50, 45, 55]);
|
||||
|
||||
expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
|
||||
|
@ -371,13 +371,13 @@ describe('BinaryTree traversals', () => {
|
|||
[15, 29, 50],
|
||||
[16, 28, 30, 45, 55]
|
||||
]);
|
||||
expect(tree.listLevels(node => (node ? node.key : null ), tree.root, IterationType.ITERATIVE, true)).toEqual([
|
||||
expect(tree.listLevels(node => (node ? node.key : null), tree.root, IterationType.ITERATIVE, true)).toEqual([
|
||||
[35],
|
||||
[20, 40],
|
||||
[15, 29, null, 50],
|
||||
[null, 16, 28, 30, 45, 55]
|
||||
]);
|
||||
expect(tree.listLevels(node => ( node ? node.key : null ), tree.root, IterationType.RECURSIVE, true)).toEqual([
|
||||
expect(tree.listLevels(node => (node ? node.key : null), tree.root, IterationType.RECURSIVE, true)).toEqual([
|
||||
[35],
|
||||
[20, 40],
|
||||
[15, 29, null, 50],
|
||||
|
|
|
@ -189,7 +189,7 @@ describe('BST operations test', () => {
|
|||
});
|
||||
|
||||
it('should perform various operations on a Binary Search Tree with object values', () => {
|
||||
const objBST = new BST<{key: number; keyA: number}>();
|
||||
const objBST = new BST<{ key: number; keyA: number }>();
|
||||
expect(objBST).toBeInstanceOf(BST);
|
||||
objBST.add(11, {key: 11, keyA: 11});
|
||||
objBST.add(3, {key: 3, keyA: 3});
|
||||
|
@ -260,7 +260,7 @@ describe('BST operations test', () => {
|
|||
objBST.perfectlyBalance();
|
||||
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
||||
|
||||
const bfsNodesAfterBalanced: BSTNode<{key: number; keyA: number}>[] = [];
|
||||
const bfsNodesAfterBalanced: BSTNode<{ key: number; keyA: number }>[] = [];
|
||||
objBST.bfs(node => bfsNodesAfterBalanced.push(node));
|
||||
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
||||
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
||||
|
@ -385,7 +385,7 @@ describe('BST operations test', () => {
|
|||
expect(bfsIDs[1]).toBe(12);
|
||||
expect(bfsIDs[2]).toBe(16);
|
||||
|
||||
const bfsNodes: BSTNode<{key: number; keyA: number}>[] = [];
|
||||
const bfsNodes: BSTNode<{ key: number; keyA: number }>[] = [];
|
||||
objBST.bfs(node => bfsNodes.push(node));
|
||||
expect(bfsNodes[0].key).toBe(2);
|
||||
expect(bfsNodes[1].key).toBe(12);
|
||||
|
@ -580,7 +580,7 @@ describe('BST operations test recursively', () => {
|
|||
});
|
||||
|
||||
it('should perform various operations on a Binary Search Tree with object values', () => {
|
||||
const objBST = new BST<{key: number; keyA: number}>();
|
||||
const objBST = new BST<{ key: number; keyA: number }>();
|
||||
expect(objBST).toBeInstanceOf(BST);
|
||||
objBST.add(11, {key: 11, keyA: 11});
|
||||
objBST.add(3, {key: 3, keyA: 3});
|
||||
|
@ -652,7 +652,7 @@ describe('BST operations test recursively', () => {
|
|||
objBST.perfectlyBalance();
|
||||
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
||||
|
||||
const bfsNodesAfterBalanced: BSTNode<{key: number; keyA: number}>[] = [];
|
||||
const bfsNodesAfterBalanced: BSTNode<{ key: number; keyA: number }>[] = [];
|
||||
objBST.bfs(node => bfsNodesAfterBalanced.push(node));
|
||||
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
||||
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
||||
|
@ -777,7 +777,7 @@ describe('BST operations test recursively', () => {
|
|||
expect(bfsIDs[1]).toBe(12);
|
||||
expect(bfsIDs[2]).toBe(16);
|
||||
|
||||
const bfsNodes: BSTNode<{key: number; keyA: number}>[] = [];
|
||||
const bfsNodes: BSTNode<{ key: number; keyA: number }>[] = [];
|
||||
objBST.bfs(node => bfsNodes.push(node));
|
||||
expect(bfsNodes[0].key).toBe(2);
|
||||
expect(bfsNodes[1].key).toBe(12);
|
||||
|
|
|
@ -29,7 +29,7 @@ describe('Overall BinaryTree Test', () => {
|
|||
bfsIDs[0] === 11; // true
|
||||
expect(bfsIDs[0]).toBe(11);
|
||||
|
||||
const objBST = new BST<{key: number; keyA: number}>();
|
||||
const objBST = new BST<{ key: number; keyA: number }>();
|
||||
objBST.add(11, {key: 11, keyA: 11});
|
||||
objBST.add(3, {key: 3, keyA: 3});
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {IterationType, RBTNColor, RedBlackTreeNode, RedBlackTree} from '../../../../src';
|
||||
import {IterationType, RBTNColor, RedBlackTree, RedBlackTreeNode} from '../../../../src';
|
||||
import {getRandomInt, getRandomIntArray, magnitude} from '../../../utils';
|
||||
import {isDebugTest} from '../../../config';
|
||||
import {OrderedMap} from "js-sdsl";
|
||||
|
@ -421,9 +421,9 @@ describe('RedBlackTree', () => {
|
|||
isDebug && tree.print();
|
||||
|
||||
expect(tree.dfs()).toEqual([
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18,
|
||||
19, 22, 23, 25, 28, 33, 50, 110, 111,
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18,
|
||||
19, 22, 23, 25, 28, 33, 50, 110, 111,
|
||||
155, 225
|
||||
])
|
||||
|
||||
|
@ -441,14 +441,14 @@ describe('RedBlackTree', () => {
|
|||
|
||||
expect(tree.size).toBe(51);
|
||||
expect(tree.isBST()).toBe(true);
|
||||
expect(tree.dfs( n => n.key, "in", tree.root, IterationType.ITERATIVE)).toEqual([
|
||||
expect(tree.dfs(n => n.key, "in", tree.root, IterationType.ITERATIVE)).toEqual([
|
||||
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
|
||||
71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
|
||||
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
|
||||
93, 94, 95, 96, 97, 98, 99
|
||||
])
|
||||
expect(tree.dfs( n => n.key, "in", tree.root, IterationType.RECURSIVE)).toEqual([
|
||||
expect(tree.dfs(n => n.key, "in", tree.root, IterationType.RECURSIVE)).toEqual([
|
||||
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
|
||||
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
|
||||
71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
|
||||
|
@ -467,7 +467,7 @@ describe('RedBlackTree', () => {
|
|||
|
||||
expect(tree.size).toBe(0);
|
||||
expect(tree.isBST()).toBe(true);
|
||||
expect(tree.dfs( n => n.key, "in", tree.root, IterationType.ITERATIVE)).toEqual([])
|
||||
expect(tree.dfs(n => n.key, "in", tree.root, IterationType.ITERATIVE)).toEqual([])
|
||||
|
||||
tree.clear();
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
|
|
|
@ -207,7 +207,7 @@ describe('TreeMultimap operations test', () => {
|
|||
});
|
||||
|
||||
it('should perform various operations on a Binary Search Tree with object values', () => {
|
||||
const objTreeMultimap = new TreeMultimap<{key: number; keyA: number}>();
|
||||
const objTreeMultimap = new TreeMultimap<{ key: number; keyA: number }>();
|
||||
expect(objTreeMultimap).toBeInstanceOf(TreeMultimap);
|
||||
objTreeMultimap.add(11, {key: 11, keyA: 11});
|
||||
objTreeMultimap.add(3, {key: 3, keyA: 3});
|
||||
|
@ -447,7 +447,7 @@ describe('TreeMultimap operations test recursively', () => {
|
|||
});
|
||||
|
||||
it('should perform various operations on a Binary Search Tree with object values', () => {
|
||||
const objTreeMultimap = new TreeMultimap<{key: number; keyA: number}>();
|
||||
const objTreeMultimap = new TreeMultimap<{ key: number; keyA: number }>();
|
||||
expect(objTreeMultimap).toBeInstanceOf(TreeMultimap);
|
||||
objTreeMultimap.add(11, {key: 11, keyA: 11});
|
||||
objTreeMultimap.add(3, {key: 3, keyA: 3});
|
||||
|
|
|
@ -74,7 +74,8 @@ class MyGraph<
|
|||
describe('AbstractGraph Operation Test', () => {
|
||||
const myGraph: MyGraph<number, string> = new MyGraph<number, string>();
|
||||
|
||||
beforeEach(() => {});
|
||||
beforeEach(() => {
|
||||
});
|
||||
it('should edge cases', function () {
|
||||
myGraph.addVertex('A', 1);
|
||||
myGraph.addVertex('B', 2);
|
||||
|
|
|
@ -150,7 +150,7 @@ describe('UndirectedGraph', () => {
|
|||
});
|
||||
|
||||
it('should getAllPathsBetween work well in 66 vertexes 97 edges graph', () => {
|
||||
const graph = new UndirectedGraph<{name: string}, number>();
|
||||
const graph = new UndirectedGraph<{ name: string }, number>();
|
||||
for (const v of saltyVertexes) {
|
||||
graph.addVertex(v.name, v);
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ describe('Heap Operation Test', () => {
|
|||
});
|
||||
|
||||
it('should object heap work well', function () {
|
||||
const minHeap = new MinHeap<{a: string; key: number}>({comparator: (a, b) => a.key - b.key});
|
||||
const minHeap = new MinHeap<{ a: string; key: number }>({comparator: (a, b) => a.key - b.key});
|
||||
minHeap.add({key: 1, a: 'a1'});
|
||||
minHeap.add({key: 6, a: 'a6'});
|
||||
minHeap.add({key: 2, a: 'a2'});
|
||||
|
@ -37,7 +37,7 @@ describe('Heap Operation Test', () => {
|
|||
i++;
|
||||
}
|
||||
|
||||
const maxHeap = new MaxHeap<{key: number; a: string}>({comparator: (a, b) => b.key - a.key});
|
||||
const maxHeap = new MaxHeap<{ key: number; a: string }>({comparator: (a, b) => b.key - a.key});
|
||||
maxHeap.add({key: 1, a: 'a1'});
|
||||
maxHeap.add({key: 6, a: 'a6'});
|
||||
maxHeap.add({key: 5, a: 'a5'});
|
||||
|
|
|
@ -60,7 +60,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|||
|
||||
describe('DoublyLinkedList Operation Test', () => {
|
||||
let list: DoublyLinkedList<number>;
|
||||
let objectList: DoublyLinkedList<{keyA: number}>;
|
||||
let objectList: DoublyLinkedList<{ keyA: number }>;
|
||||
|
||||
beforeEach(() => {
|
||||
list = new DoublyLinkedList();
|
||||
|
|
|
@ -11,10 +11,10 @@ describe('SinglyLinkedListNode', () => {
|
|||
|
||||
describe('SinglyLinkedList Operation Test', () => {
|
||||
let list: SinglyLinkedList<number>;
|
||||
let objectList: SinglyLinkedList<{keyA: number}>;
|
||||
let objectList: SinglyLinkedList<{ keyA: number }>;
|
||||
beforeEach(() => {
|
||||
list = new SinglyLinkedList<number>();
|
||||
objectList = new SinglyLinkedList<{keyA: number}>();
|
||||
objectList = new SinglyLinkedList<{ keyA: number }>();
|
||||
});
|
||||
|
||||
describe('push', () => {
|
||||
|
|
|
@ -16,7 +16,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|||
});
|
||||
|
||||
it('should add elements and maintain heap property in a object MaxPriorityQueue', () => {
|
||||
const priorityQueue = new MaxPriorityQueue<{keyA: number}>({comparator: (a, b) => b.keyA - a.keyA});
|
||||
const priorityQueue = new MaxPriorityQueue<{ keyA: number }>({comparator: (a, b) => b.keyA - a.keyA});
|
||||
priorityQueue.refill([{keyA: 5}, {keyA: 3}, {keyA: 1}]);
|
||||
priorityQueue.add({keyA: 7});
|
||||
|
||||
|
@ -63,7 +63,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|||
|
||||
it('should correctly heapify an object array', () => {
|
||||
const nodes = [{keyA: 5}, {keyA: 3}, {keyA: 7}, {keyA: 1}];
|
||||
const maxPQ = MaxPriorityQueue.heapify<{keyA: number}>({nodes: nodes, comparator: (a, b) => b.keyA - a.keyA});
|
||||
const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>({nodes: nodes, comparator: (a, b) => b.keyA - a.keyA});
|
||||
|
||||
expect(maxPQ.poll()?.keyA).toBe(7);
|
||||
expect(maxPQ.poll()?.keyA).toBe(5);
|
||||
|
|
|
@ -32,7 +32,7 @@ export const bigO = {
|
|||
|
||||
function findPotentialN(input: any): number {
|
||||
let longestArray: any[] = [];
|
||||
let mostProperties: {[key: string]: any} = {};
|
||||
let mostProperties: { [key: string]: any } = {};
|
||||
|
||||
function recurse(obj: any) {
|
||||
if (Array.isArray(obj)) {
|
||||
|
|
Loading…
Reference in a new issue