Skip to content

Commit

Permalink
style: Try to synchronize Prettier with the coding style of the IDE.
Browse files Browse the repository at this point in the history
  • Loading branch information
zrwusa committed Nov 13, 2023
1 parent 1064ad4 commit 8f1f6d6
Show file tree
Hide file tree
Showing 59 changed files with 317 additions and 425 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ module.exports = {
}
}
],
"brace-style": ["error", "1tbs", { "allowSingleLine": true }],
"object-curly-spacing": ["error", "always"]
"brace-style": ["error", "1tbs", {"allowSingleLine": true}],
"object-curly-spacing": ["error", "never"]
},
"settings": {
"import/parsers": {
Expand Down
4 changes: 2 additions & 2 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
"arrowParens": "avoid",
"bracketSpacing": true,
"bracketSpacing": false,
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"bracketSameLine": true,
"bracketSameLine": false,
"jsxSingleQuote": true,
"printWidth": 120,
"proseWrap": "preserve",
Expand Down
2 changes: 1 addition & 1 deletion src/data-structures/binary-tree/avl-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,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) {
Expand Down
2 changes: 1 addition & 1 deletion src/data-structures/binary-tree/binary-indexed-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
40 changes: 17 additions & 23 deletions src/data-structures/binary-tree/binary-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
* Represents a binary tree data structure.
* @template N - The type of the binary tree's nodes.
*/
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
implements IBinaryTree<V, N> {
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> implements IBinaryTree<V, N> {
iterationType: IterationType = IterationType.ITERATIVE;

/**
Expand Down Expand Up @@ -301,7 +300,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
): BiTreeDeleteResult<N>[] {
const deletedResult: BiTreeDeleteResult<N>[] = [];
if (!this.root) return deletedResult;
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
callback = (node => node) as C;

const curr = this.getNode(identifier, callback);
if (!curr) return deletedResult;
Expand Down Expand Up @@ -330,14 +330,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
orgCurrent = this._swap(curr, leftSubTreeRightMost);
if (parentOfLeftSubTreeMax) {
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
needBalanced = parentOfLeftSubTreeMax;
}
}
}

}
this._size = this.size - 1;

Expand Down Expand Up @@ -383,7 +381,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
*/


/**
* Time Complexity: O(n)
* Space Complexity: O(log n)
Expand Down Expand Up @@ -412,7 +409,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode

return _getMaxHeight(beginRoot);
} else {
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
let maxHeight = 0;

while (stack.length > 0) {
Expand Down Expand Up @@ -539,7 +536,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* Space Complexity: O(log n).
*/


/**
* Time Complexity: O(n)
* Space Complexity: O(log n).
Expand Down Expand Up @@ -572,7 +568,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
beginRoot: BTNKey | N | null | undefined = this.root,
iterationType = this.iterationType
): N[] {
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
callback = (node => node) as C;
beginRoot = this.ensureNotKey(beginRoot);
if (!beginRoot) return [];

Expand Down Expand Up @@ -660,7 +657,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
beginRoot: BTNKey | N | null | undefined = this.root,
iterationType = this.iterationType
): boolean {
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
callback = (node => node) as C;

return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
}
Expand Down Expand Up @@ -718,7 +716,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
beginRoot: BTNKey | N | null | undefined = this.root,
iterationType = this.iterationType
): N | null | undefined {
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
callback = (node => node) as C;

return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
}
Expand Down Expand Up @@ -836,7 +835,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
beginRoot: BTNKey | N | null | undefined = this.root,
iterationType = this.iterationType
): V | undefined {
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
callback = (node => node) as C;

return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
}
Expand Down Expand Up @@ -1165,7 +1165,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
* @returns a boolean value.
*/
isNodeOrNull(node: any): node is (N | null) {
isNodeOrNull(node: any): node is N | null {
return this.isRealNode(node) || node === null;
}

Expand Down Expand Up @@ -1238,7 +1238,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
iterationType: IterationType = IterationType.ITERATIVE,
includeNull = false
): ReturnType<C>[] {

beginRoot = this.ensureNotKey(beginRoot);
if (!beginRoot) return [];
const ans: ReturnType<C>[] = [];
Expand Down Expand Up @@ -1285,7 +1284,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
_traverse(beginRoot);
} else {
// 0: visit, 1: print
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];

while (stack.length > 0) {
const cur = stack.pop();
Expand Down Expand Up @@ -1454,7 +1453,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* Space complexity: O(n)
*/


/**
* Time complexity: O(n)
* Space complexity: O(n)
Expand Down Expand Up @@ -1523,8 +1521,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
return levelsNodes;
}

getPredecessor(node: N): N

getPredecessor(node: N): N;

/**
* The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
Expand All @@ -1549,7 +1546,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
}
}


/**
* The function `getSuccessor` returns the next node in a binary tree given a current node.
* @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
Expand Down Expand Up @@ -1681,7 +1677,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
return ans;
}


/**
* The above function is an iterator for a binary tree that can be used to traverse the tree in
* either an iterative or recursive manner.
Expand All @@ -1691,7 +1686,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
* binary tree nodes in a specific order.
*/
* [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
if (!node) {
return;
}
Expand Down Expand Up @@ -1817,7 +1812,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
return destNode;
}
return undefined;

}

/**
Expand Down
16 changes: 4 additions & 12 deletions src/data-structures/binary-tree/bst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
this._left = v;
}


protected override _right?: N;

/**
Expand All @@ -63,10 +62,7 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
}
}

export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
extends BinaryTree<V, N>
implements IBinaryTree<V, N> {

export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
/**
* The constructor function initializes a binary search tree with an optional comparator function.
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
Expand Down Expand Up @@ -109,7 +105,6 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
* Space Complexity: O(1) - Constant space is used.
*/


/**
* 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.
Expand Down Expand Up @@ -230,9 +225,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
}

const inserted: (N | undefined)[] = [];
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
(value: BTNKey | N, index) => [value, data?.[index]] as [BTNKey | N, V]
);
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map((value: BTNKey | N, index) => [value, data?.[index]] as [BTNKey | N, V]);

let sorted = [];

Expand All @@ -244,7 +237,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
const _isBinaryTreeKeyOrNullTuple = (arr: [BTNKey | N, V][]): arr is [BTNKey, V][] => {
for (const [keyOrNode] of arr) if (this.isNodeKey(keyOrNode)) return true;
return false;
}
};

let sortedKeysOrNodes: (number | N | undefined)[] = [],
sortedData: (V | undefined)[] | undefined = [];
Expand Down Expand Up @@ -580,7 +573,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];
debugger
debugger;
this.add(midNode.key, midNode.value);
stack.push([m + 1, r]);
stack.push([l, m - 1]);
Expand Down Expand Up @@ -672,5 +665,4 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
else if (compared < 0) return CP.lt;
else return CP.eq;
}

}
17 changes: 4 additions & 13 deletions src/data-structures/binary-tree/rb-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@
* @license MIT License
*/

import {
BiTreeDeleteResult,
BTNCallback,
BTNKey,
IterationType,
RBTNColor,
RBTreeOptions,
RedBlackTreeNodeNested
} from '../../types';
import {BST, BSTNode} from "./bst";
import {IBinaryTree} from "../../interfaces";
import {BinaryTreeNode} from "./binary-tree";
import {BiTreeDeleteResult, BTNCallback, BTNKey, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNodeNested} from '../../types';
import {BST, BSTNode} from './bst';
import {IBinaryTree} from '../../interfaces';
import {BinaryTreeNode} from './binary-tree';

export class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
color: RBTNColor;
Expand All @@ -38,7 +30,6 @@ 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> {

NIL: N = new RedBlackTreeNode<V>(NaN) as unknown as N;

/**
Expand Down
10 changes: 4 additions & 6 deletions src/data-structures/binary-tree/tree-multimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import {BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType} from
import {IBinaryTree} from '../../interfaces';
import {AVLTree, AVLTreeNode} from './avl-tree';

export class TreeMultimapNode<
V = any,
N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>
> extends AVLTreeNode<V, N> {
export class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
count: number;

/**
Expand Down Expand Up @@ -284,7 +281,8 @@ 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--;
Expand Down Expand Up @@ -418,4 +416,4 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
}
return undefined;
}
}
}
Loading

0 comments on commit 8f1f6d6

Please sign in to comment.