Support TypeScript v5.1.6. Standardize all getters and setters. Adjust access permissions for accessors, set access permissions for protected or private member variables, and indicate them using the private identifier "_".

This commit is contained in:
Revone 2023-08-21 16:17:01 +08:00
parent dcfa5f1545
commit c1b5969c51
28 changed files with 364 additions and 485 deletions

85
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "data-structure-typed",
"version": "1.16.1",
"version": "1.18.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "data-structure-typed",
"version": "1.16.1",
"version": "1.18.0",
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.5.3",
@ -15,7 +15,7 @@
"jest": "^29.6.2",
"ts-jest": "^29.1.1",
"typedoc": "^0.24.8",
"typescript": "^4.9.5"
"typescript": "^5.1.6"
}
},
"node_modules/@ampproject/remapping": {
@ -1463,9 +1463,9 @@
}
},
"node_modules/@types/node": {
"version": "20.5.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.0.tgz",
"integrity": "sha512-Mgq7eCtoTjT89FqNoTzzXg2XvCi5VMhRV6+I2aYanc6kQCBImeNaAYRs/DyoVqk1YEUJK5gN9VO7HRIdz4Wo3Q==",
"version": "20.5.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.1.tgz",
"integrity": "sha512-4tT2UrL5LBqDwoed9wZ6N3umC4Yhz3W3FloMmiiG4JwmUJWpie0c7lcnUNd4gtMKuDEO4wRVS8B6Xa0uMRsMKg==",
"dev": true
},
"node_modules/@types/stack-utils": {
@ -1865,9 +1865,9 @@
}
},
"node_modules/caniuse-lite": {
"version": "1.0.30001521",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001521.tgz",
"integrity": "sha512-fnx1grfpEOvDGH+V17eccmNjucGUnCbP6KL+l5KqBIerp26WK/+RQ7CIDE37KGJjaPyqWXXlFUyKiWmvdNNKmQ==",
"version": "1.0.30001522",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001522.tgz",
"integrity": "sha512-TKiyTVZxJGhsTszLuzb+6vUZSjVOAhClszBr2Ta2k9IwtNBT/4dzmL6aywt0HCgEZlmwJzXJd8yNiob6HgwTRg==",
"dev": true,
"funding": [
{
@ -2138,9 +2138,9 @@
}
},
"node_modules/dependency-cruiser": {
"version": "13.1.3",
"resolved": "https://registry.npmjs.org/dependency-cruiser/-/dependency-cruiser-13.1.3.tgz",
"integrity": "sha512-bZS4XLBbbzGvP24Zl3KteiIDVuNff0RCvIN8qT9hNwfZOk2rO5dQVpR3a3nUSc8EDhbAH6oWeNZO/AJSLaO1jw==",
"version": "13.1.4",
"resolved": "https://registry.npmjs.org/dependency-cruiser/-/dependency-cruiser-13.1.4.tgz",
"integrity": "sha512-cMrtPuPxp+uqQMt196IJ8lgKQglu+dbTsWgGHV+Y0AkWYJISjByluA1YcmPjglzXB9innj0s+oEcGE/N6Jm4aA==",
"dev": true,
"dependencies": {
"acorn": "8.10.0",
@ -2154,7 +2154,6 @@
"enhanced-resolve": "5.15.0",
"figures": "5.0.0",
"glob": "10.3.3",
"handlebars": "4.7.8",
"ignore": "5.2.4",
"indent-string": "5.0.0",
"interpret": "^3.1.1",
@ -2208,9 +2207,9 @@
"dev": true
},
"node_modules/electron-to-chromium": {
"version": "1.4.492",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.492.tgz",
"integrity": "sha512-36K9b/6skMVwAIEsC7GiQ8I8N3soCALVSHqWHzNDtGemAcI9Xu8hP02cywWM0A794rTHm0b0zHPeLJHtgFVamQ==",
"version": "1.4.496",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.496.tgz",
"integrity": "sha512-qeXC3Zbykq44RCrBa4kr8v/dWzYJA8rAwpyh9Qd+NKWoJfjG5vvJqy9XOJ9H4P/lqulZBCgUWAYi+FeK5AuJ8g==",
"dev": true
},
"node_modules/emittery": {
@ -2537,27 +2536,6 @@
"integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==",
"dev": true
},
"node_modules/handlebars": {
"version": "4.7.8",
"resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.8.tgz",
"integrity": "sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==",
"dev": true,
"dependencies": {
"minimist": "^1.2.5",
"neo-async": "^2.6.2",
"source-map": "^0.6.1",
"wordwrap": "^1.0.0"
},
"bin": {
"handlebars": "bin/handlebars"
},
"engines": {
"node": ">=0.4.7"
},
"optionalDependencies": {
"uglify-js": "^3.1.4"
}
},
"node_modules/has": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
@ -4434,12 +4412,6 @@
"integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==",
"dev": true
},
"node_modules/neo-async": {
"version": "2.6.2",
"resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz",
"integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==",
"dev": true
},
"node_modules/node-int64": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz",
@ -5421,29 +5393,16 @@
}
},
"node_modules/typescript": {
"version": "4.9.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz",
"integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==",
"version": "5.1.6",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz",
"integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==",
"dev": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=4.2.0"
}
},
"node_modules/uglify-js": {
"version": "3.17.4",
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.4.tgz",
"integrity": "sha512-T9q82TJI9e/C1TAxYvfb16xO120tMVFZrGA3f9/P4424DNu6ypK103y0GPFVa17yotwSyZW5iYXgjYHkGrJW/g==",
"dev": true,
"optional": true,
"bin": {
"uglifyjs": "bin/uglifyjs"
},
"engines": {
"node": ">=0.8.0"
"node": ">=14.17"
}
},
"node_modules/update-browserslist-db": {
@ -5553,12 +5512,6 @@
"node": ">= 8"
}
},
"node_modules/wordwrap": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz",
"integrity": "sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==",
"dev": true
},
"node_modules/wrap-ansi": {
"version": "8.1.0",
"resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz",

View file

@ -1,6 +1,6 @@
{
"name": "data-structure-typed",
"version": "1.17.3",
"version": "1.18.0",
"description": "Explore our comprehensive Javascript Data Structure / TypeScript Data Structure Library, meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures, such as Binary Tree, AVL Tree, Binary Search Tree (BST), Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Singly Linked List, Hash, CoordinateSet, CoordinateMap, Heap, Doubly Linked List, Priority Queue, Max Priority Queue, Min Priority Queue, Queue, ObjectDeque, ArrayDeque, Stack, and Trie. Each data structure is thoughtfully designed and implemented using TypeScript to provide efficient, reliable, and easy-to-use solutions for your programming needs. Whether you're optimizing algorithms, managing data, or enhancing performance, our TypeScript Data Structure Library is your go-to resource. Elevate your coding experience with these fundamental building blocks for software development.",
"main": "dist/index.js",
"scripts": {
@ -54,6 +54,6 @@
"jest": "^29.6.2",
"ts-jest": "^29.1.1",
"typedoc": "^0.24.8",
"typescript": "^4.9.5"
"typescript": "^5.1.6"
}
}

View file

@ -141,7 +141,7 @@ export class AVLTree<T> extends BST<T> {
}
if (B) B.parent = parentOfA;
if (A === this.root) {
if (B) this.root = B;
if (B) this._setRoot(B);
} else {
if (parentOfA?.left === A) {
parentOfA.left = B;
@ -183,7 +183,7 @@ export class AVLTree<T> extends BST<T> {
}
if (A === this.root) {
if (C) this.root = C;
if (C) this._setRoot(C);
} else {
if (parentOfA) {
if (parentOfA.left === A) {
@ -222,7 +222,7 @@ export class AVLTree<T> extends BST<T> {
}
if (A === this.root) {
if (B) this.root = B;
if (B) this._setRoot(B);
} else {
if (parentOfA) {
if (parentOfA.left === A) {
@ -268,7 +268,7 @@ export class AVLTree<T> extends BST<T> {
if (A === this.root) {
if (C) this.root = C;
if (C) this._setRoot(C);
} else {
if (parentOfA) {
if (parentOfA.left === A) {

View file

@ -6,7 +6,6 @@
* @license MIT License
*/
export class BinaryIndexedTree {
private readonly _sumTree: number[];
/**
* The constructor initializes an array with a specified length and fills it with zeros.
@ -18,6 +17,12 @@ export class BinaryIndexedTree {
this._sumTree = new Array<number>(n + 1).fill(0);
}
private _sumTree: number[];
get sumTree(): number[] {
return this._sumTree;
}
static lowBit(x: number) {
return x & (-x);
}
@ -66,4 +71,8 @@ export class BinaryIndexedTree {
throw 'Index out of bounds';
return this.getPrefixSum(end) - this.getPrefixSum(start);
}
protected _setSumTree(value: number[]) {
this._sumTree = value;
}
}

View file

@ -36,7 +36,7 @@ export class BinaryTreeNode<T> {
this._count = count ?? 1;
}
protected _id: BinaryTreeNodeId;
private _id: BinaryTreeNodeId;
get id(): BinaryTreeNodeId {
return this._id;
@ -46,7 +46,7 @@ export class BinaryTreeNode<T> {
this._id = v;
}
protected _val: T;
private _val: T;
get val(): T {
return this._val;
}
@ -55,7 +55,7 @@ export class BinaryTreeNode<T> {
this._val = v;
}
protected _left?: BinaryTreeNode<T> | null;
private _left?: BinaryTreeNode<T> | null;
get left(): BinaryTreeNode<T> | null | undefined {
return this._left;
@ -69,7 +69,7 @@ export class BinaryTreeNode<T> {
this._left = v;
}
protected _right?: BinaryTreeNode<T> | null;
private _right?: BinaryTreeNode<T> | null;
get right(): BinaryTreeNode<T> | null | undefined {
return this._right;
@ -83,7 +83,7 @@ export class BinaryTreeNode<T> {
this._right = v;
}
protected _parent: BinaryTreeNode<T> | null | undefined;
private _parent: BinaryTreeNode<T> | null | undefined;
get parent(): BinaryTreeNode<T> | null | undefined {
return this._parent;
@ -93,7 +93,7 @@ export class BinaryTreeNode<T> {
this._parent = v;
}
protected _familyPosition: FamilyPosition = FamilyPosition.root;
private _familyPosition: FamilyPosition = FamilyPosition.root;
get familyPosition(): FamilyPosition {
return this._familyPosition;
@ -103,7 +103,7 @@ export class BinaryTreeNode<T> {
this._familyPosition = v;
}
protected _count = 1;
private _count = 1;
get count(): number {
return this._count;
@ -113,7 +113,7 @@ export class BinaryTreeNode<T> {
this._count = v;
}
protected _height = 0;
private _height = 0;
get height(): number {
return this._height;
@ -123,62 +123,6 @@ export class BinaryTreeNode<T> {
this._height = v;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getId(): BinaryTreeNodeId {
return this._id;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getVal(): T {
return this._val;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getLeft(): BinaryTreeNode<T> | null | undefined {
return this._left;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getRight(): BinaryTreeNode<T> | null | undefined {
return this._right;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getParent(): BinaryTreeNode<T> | null | undefined {
return this._parent;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getFamilyPosition(): FamilyPosition {
return this._familyPosition;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getCount(): number {
return this._count;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getHeight(): number {
return this._height;
}
swapLocation(swapNode: BinaryTreeNode<T>): BinaryTreeNode<T> {
const {val, count, height} = swapNode;
const tempNode = new BinaryTreeNode<T>(swapNode.id, val);
@ -204,15 +148,6 @@ export class BinaryTreeNode<T> {
}
export class BinaryTree<T> {
protected _loopType: LoopType = LoopType.iterative;
protected _visitedId: BinaryTreeNodeId[] = [];
protected _visitedVal: Array<T> = [];
protected _visitedNode: BinaryTreeNode<T>[] = [];
protected _visitedCount: number[] = [];
protected _visitedLeftSum: number[] = [];
private readonly _autoIncrementId: boolean = false;
private _maxId: number = -1;
private readonly _isDuplicatedVal: boolean = false;
/**
* The constructor function accepts an optional options object and sets the values of loopType, autoIncrementId, and
@ -236,62 +171,77 @@ export class BinaryTree<T> {
}
}
protected _root: BinaryTreeNode<T> | null = null;
private _loopType: LoopType = LoopType.iterative;
get loopType(): LoopType {
return this._loopType;
}
private _visitedId: BinaryTreeNodeId[] = [];
get visitedId(): BinaryTreeNodeId[] {
return this._visitedId;
}
private _visitedVal: Array<T> = [];
get visitedVal(): Array<T> {
return this._visitedVal;
}
private _visitedNode: BinaryTreeNode<T>[] = [];
get visitedNode(): BinaryTreeNode<T>[] {
return this._visitedNode;
}
private _visitedCount: number[] = [];
get visitedCount(): number[] {
return this._visitedCount;
}
private _visitedLeftSum: number[] = [];
get visitedLeftSum(): number[] {
return this._visitedLeftSum;
}
private _autoIncrementId: boolean = false;
get autoIncrementId(): boolean {
return this._autoIncrementId;
}
private _maxId: number = -1;
get maxId(): number {
return this._maxId;
}
private _isDuplicatedVal: boolean = false;
get isDuplicatedVal(): boolean {
return this._isDuplicatedVal;
}
private _root: BinaryTreeNode<T> | null = null;
get root(): BinaryTreeNode<T> | null {
return this._root;
}
protected set root(v: BinaryTreeNode<T> | null) {
if (v) {
v.parent = null;
v.familyPosition = FamilyPosition.root;
}
this._root = v;
}
protected _size = 0;
private _size = 0;
get size(): number {
return this._size;
}
protected set size(v: number) {
this._size = v;
}
protected _count = 0;
private _count = 0;
get count(): number {
return this._count;
}
protected set count(v: number) {
this._count = v;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Getters (using the same name as the property) while utilizing separate method names for Setters.
* @returns The method is returning either a BinaryTreeNode object of type T or null.
*/
getRoot(): BinaryTreeNode<T> | null {
return this._root;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getSize(): number {
return this._size;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getCount(): number {
return this._count;
}
/**
* The function creates a new binary tree node with the given id, value, and count, or returns null if the value is
* null.
@ -312,10 +262,10 @@ export class BinaryTree<T> {
* The clear function resets the state of an object by setting its properties to their initial values.
*/
clear() {
this.root = null;
this.size = 0;
this.count = 0;
this._maxId = -1;
this._setRoot(null);
this._setSize(0);
this._setCount(0);
this._setMaxId(-1);
}
/**
@ -362,17 +312,17 @@ export class BinaryTree<T> {
existNode.count += count;
existNode.val = val;
if (needInsert !== null) {
this.count += count;
this._setCount(this.count + count);
inserted = existNode;
}
} else {
inserted = _bfs(this.root, needInsert);
}
} else {
this.root = val !== null ? new BinaryTreeNode<T>(id, val, count) : null;
this._setRoot(val !== null ? new BinaryTreeNode<T>(id, val, count) : null);
if (needInsert !== null) {
this.size = 1;
this.count = count;
this._setSize(1);
this._setCount(count);
}
inserted = this.root;
}
@ -396,8 +346,8 @@ export class BinaryTree<T> {
}
parent.left = newNode;
if (newNode !== null) {
this.size++;
this.count += newNode?.count ?? 0;
this._setSize(this.size + 1);
this._setCount(this.count + newNode?.count ?? 0)
}
return parent.left;
@ -408,8 +358,8 @@ export class BinaryTree<T> {
}
parent.right = newNode;
if (newNode !== null) {
this.size++;
this.count += newNode?.count ?? 0;
this._setSize(this.size + 1);
this._setCount(this.count + newNode?.count ?? 0);
}
return parent.right;
} else {
@ -497,27 +447,27 @@ export class BinaryTree<T> {
if (!node) node = undefined;
else if (node.count > 1 && !ignoreCount) {
node.count--;
this.count--;
this._setCount(this.count - 1);
} else if (node instanceof BinaryTreeNode) {
const [subSize, subCount] = this.getSubTreeSizeAndCount(node);
switch (node.familyPosition) {
case 0:
this.size -= subSize;
this.count -= subCount;
this._setSize(this.size - subSize);
this._setCount(this.count - subCount);
node = undefined;
break;
case 1:
if (node.parent) {
this.size -= subSize;
this.count -= subCount;
this._setSize(this.size - subSize);
this._setCount(this.count - subCount);
node.parent.left = null;
}
break;
case 2:
if (node.parent) {
this.size -= subSize;
this.count -= subCount;
this._setSize(this.size - subSize);
this._setCount(this.count - subCount);
node.parent.right = null;
}
break;
@ -805,8 +755,6 @@ export class BinaryTree<T> {
}
}
// --- start additional methods ---
/**
* The `isBST` function checks if a binary tree is a binary search tree.
* @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
@ -881,6 +829,8 @@ export class BinaryTree<T> {
}
}
// --- start additional methods ---
/**
* The function `subTreeSum` calculates the sum of a specified property in a binary tree, either recursively or
* iteratively.
@ -958,7 +908,7 @@ export class BinaryTree<T> {
break;
case 'count':
cur.count += delta;
this.count += delta;
this._setCount(this.count + delta);
break;
default:
cur.id += delta;
@ -1378,6 +1328,58 @@ export class BinaryTree<T> {
return this._getResultByPropertyName(nodeOrPropertyName);
}
protected _setLoopType(value: LoopType) {
this._loopType = value;
}
protected _setVisitedId(value: BinaryTreeNodeId[]) {
this._visitedId = value;
}
protected _setVisitedVal(value: Array<T>) {
this._visitedVal = value;
}
protected _setVisitedNode(value: BinaryTreeNode<T>[]) {
this._visitedNode = value;
}
protected setVisitedCount(value: number[]) {
this._visitedCount = value;
}
protected _setVisitedLeftSum(value: number[]) {
this._visitedLeftSum = value;
}
protected _setAutoIncrementId(value: boolean) {
this._autoIncrementId = value;
}
protected _setMaxId(value: number) {
this._maxId = value;
}
protected _setIsDuplicatedVal(value: boolean) {
this._isDuplicatedVal = value;
}
protected _setRoot(v: BinaryTreeNode<T> | null) {
if (v) {
v.parent = null;
v.familyPosition = FamilyPosition.root;
}
this._root = v;
}
protected _setSize(v: number) {
this._size = v;
}
protected _setCount(v: number) {
this._count = v;
}
/**
* The function resets the values of several arrays used for tracking visited nodes and their properties.
*/

View file

@ -54,9 +54,9 @@ export class BST<T> extends BinaryTree<T> {
let inserted: BSTNode<T> | null = null;
const newNode = this.createNode(id, val, count);
if (this.root === null) {
this.root = newNode;
this.size++;
this.count += newNode?.count ?? 1;
this._setRoot(newNode);
this._setSize(this.size + 1);
this._setCount(this.count + count);
inserted = (this.root);
} else {
let cur = this.root;
@ -66,7 +66,7 @@ export class BST<T> extends BinaryTree<T> {
if (this._compare(cur.id, id) === CP.eq) {
if (newNode) {
cur.count += newNode.count;
this.count += newNode.count;
this._setCount(this.count + newNode.count);
cur.val = newNode.val;
}
//Duplicates are not accepted.
@ -81,8 +81,8 @@ export class BST<T> extends BinaryTree<T> {
}
//Add to the left of the current node
cur.left = newNode;
this.size++;
this.count += newNode.count;
this._setSize(this.size + 1);
this._setCount(this.count + newNode.count);
traversing = false;
inserted = cur.left;
} else {
@ -98,8 +98,8 @@ export class BST<T> extends BinaryTree<T> {
}
//Add to the right of the current node
cur.right = newNode;
this.size++;
this.count += newNode.count;
this._setSize(this.size + 1);
this._setCount(this.count + newNode.count);
traversing = false;
inserted = (cur.right);
} else {
@ -165,11 +165,11 @@ export class BST<T> extends BinaryTree<T> {
if (curr.count > 1 && !ignoreCount) {
curr.count--;
this.count--;
this._setCount(this.count - 1);
} else {
if (!curr.left) {
if (!parent) {
if (curr.right !== undefined) this.root = curr.right;
if (curr.right !== undefined) this._setRoot(curr.right);
} else {
switch (curr.familyPosition) {
case FamilyPosition.left:
@ -193,8 +193,8 @@ export class BST<T> extends BinaryTree<T> {
}
}
}
this.size--;
this.count -= curr.count;
this._setSize(this.size - 1);
this._setCount(this.count - curr.count);
}
bstDeletedResult.push({deleted: orgCurrent, needBalanced});
@ -219,7 +219,7 @@ export class BST<T> extends BinaryTree<T> {
if (!this.root) return [];
const result: BSTNode<T>[] = [];
if (this._loopType === LoopType.recursive) {
if (this.loopType === LoopType.recursive) {
const _traverse = (cur: BSTNode<T>) => {
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
@ -287,7 +287,7 @@ export class BST<T> extends BinaryTree<T> {
let sum = 0;
if (this._loopType === LoopType.recursive) {
if (this.loopType === LoopType.recursive) {
const _traverse = (cur: BSTNode<T>): void => {
const compared = this._compare(cur.id, id);
if (compared === CP.eq) {
@ -360,7 +360,7 @@ export class BST<T> extends BinaryTree<T> {
}
}
if (this._loopType === LoopType.recursive) {
if (this.loopType === LoopType.recursive) {
const _traverse = (cur: BSTNode<T>) => {
const compared = this._compare(cur.id, node.id);
_sumByPropertyName(cur);
@ -398,7 +398,7 @@ export class BST<T> extends BinaryTree<T> {
this.clear();
if (sorted.length < 1) return false;
if (this._loopType === LoopType.recursive) {
if (this.loopType === LoopType.recursive) {
const buildBalanceBST = (l: number, r: number) => {
if (l > r) return;
const m = l + Math.floor((r - l) / 2);
@ -439,7 +439,7 @@ export class BST<T> extends BinaryTree<T> {
let balanced = true;
if (this._loopType === LoopType.recursive) {
if (this.loopType === LoopType.recursive) {
const _height = (cur: BSTNode<T> | null | undefined): number => {
if (!cur) return 0;
const leftHeight = _height(cur.left), rightHeight = _height(cur.right);

View file

@ -16,7 +16,7 @@ export class SegmentTreeNode {
this._val = val || null;
}
protected _start = 0;
private _start = 0;
get start(): number {
return this._start;
}
@ -25,7 +25,7 @@ export class SegmentTreeNode {
this._start = v;
}
protected _end = 0;
private _end = 0;
get end(): number {
return this._end;
@ -35,7 +35,7 @@ export class SegmentTreeNode {
this._end = v;
}
protected _val: SegmentTreeNodeVal | null = null;
private _val: SegmentTreeNodeVal | null = null;
get val(): SegmentTreeNodeVal | null {
return this._val;
@ -45,7 +45,7 @@ export class SegmentTreeNode {
this._val = v;
}
protected _sum = 0;
private _sum = 0;
get sum(): number {
return this._sum;
@ -55,7 +55,7 @@ export class SegmentTreeNode {
this._sum = v;
}
protected _left: SegmentTreeNode | null = null;
private _left: SegmentTreeNode | null = null;
get left(): SegmentTreeNode | null {
return this._left;
@ -65,7 +65,7 @@ export class SegmentTreeNode {
this._left = v;
}
protected _right: SegmentTreeNode | null = null;
private _right: SegmentTreeNode | null = null;
get right(): SegmentTreeNode | null {
return this._right;
@ -74,55 +74,9 @@ export class SegmentTreeNode {
set right(v: SegmentTreeNode | null) {
this._right = v;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getStart(): number {
return this._start;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getEnd(): number {
return this._end;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getVal(): SegmentTreeNodeVal | null {
return this._val;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getSum(): number {
return this._sum;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getLeft(): SegmentTreeNode | null {
return this._left;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getRight(): SegmentTreeNode | null {
return this._right;
}
}
export class SegmentTree {
protected _values: number[] = [];
protected _start = 0;
protected _end: number;
/**
* The constructor initializes the values, start, end, and root properties of an object.
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
@ -141,22 +95,29 @@ export class SegmentTree {
this._root = this.build(start, end);
}
protected _root: SegmentTreeNode | null;
private _values: number[] = [];
get values(): number[] {
return this._values;
}
private _start = 0;
get start(): number {
return this._start;
}
private _end: number;
get end(): number {
return this._end;
}
private _root: SegmentTreeNode | null;
get root(): SegmentTreeNode | null {
return this._root;
}
set root(v: SegmentTreeNode | null) {
this._root = v;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getRoot(): SegmentTreeNode | null {
return this._root;
}
/**
* The function builds a segment tree by recursively dividing the given range into smaller segments and creating nodes
* for each segment.
@ -167,9 +128,8 @@ export class SegmentTree {
* @returns a SegmentTreeNode object.
*/
build(start: number, end: number): SegmentTreeNode {
if (start === end) {
return new SegmentTreeNode(start, end, this._values[start]);
}
if (start === end) return new SegmentTreeNode(start, end, this._values[start]);
const mid = start + Math.floor((end - start) / 2);
const left = this.build(start, mid);
const right = this.build(mid + 1, end);
@ -264,4 +224,20 @@ export class SegmentTree {
};
return dfs(root, indexA, indexB);
}
protected _setValues(value: number[]) {
this._values = value;
}
protected _setStart(value: number) {
this._start = value;
}
protected _setEnd(value: number) {
this._end = value;
}
protected _setRoot(v: SegmentTreeNode | null) {
this._root = v;
}
}

View file

@ -23,18 +23,10 @@ export class AbstractVertex {
set id(v: VertexId) {
this._id = v;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getId(): VertexId {
return this._id;
}
}
export abstract class AbstractEdge {
static DEFAULT_EDGE_WEIGHT = 1;
/**
* The function is a protected constructor that initializes the weight and generates a unique hash code for an edge.
@ -42,12 +34,12 @@ export abstract class AbstractEdge {
* no weight is provided, it will default to the value of `AbstractEdge.DEFAULT_EDGE_WEIGHT`.
*/
protected constructor(weight?: number) {
if (weight === undefined) weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
this._weight = weight;
this._weight = weight !== undefined ? weight : 1;
this._hashCode = uuidV4();
}
private _weight: number;
protected _weight: number;
get weight(): number {
return this._weight;
}
@ -56,29 +48,15 @@ export abstract class AbstractEdge {
this._weight = v;
}
private _hashCode: string;
protected _hashCode: string;
get hashCode(): string {
return this._hashCode;
}
set hashCode(v: string) {
protected _setHashCode(v: string) {
this._hashCode = v;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getWeight(): number {
return this._weight;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getHashCode(): string {
return this._hashCode;
}
}
// Connected Component === Largest Connected Sub-Graph
@ -411,7 +389,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
*/
dijkstraWithoutHeap(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V> {
if (getMinDist === undefined) getMinDist = false;
if (getMinDist === undefined) getMinDist = false ;
if (genPaths === undefined) genPaths = false;
if (dest === undefined) dest = null;

View file

@ -54,20 +54,6 @@ export class DirectedEdge extends AbstractEdge {
set dest(v: VertexId) {
this._dest = v;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getSrc(): VertexId {
return this._src;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getDest(): VertexId {
return this._dest;
}
}
// Strongly connected, One direction connected, Weakly connected
@ -332,20 +318,20 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
* when stored with adjacency matrix time: O(V^2)
* The `topologicalSort` function performs a topological sort on a graph and returns the sorted vertices in reverse
* order, or null if the graph contains a cycle.
* @returns The `topologicalSort()` function returns an array of vertices (`V[]`) in topological order if there is no
* cycle in the graph. If there is a cycle, it returns `null`.
* @returns The function `topologicalSort()` returns an array of vertices in topological order if there is no cycle in
* the graph. If there is a cycle, it returns `null`.
*/
topologicalSort(): V[] | null {
topologicalSort(): Array<V | VertexId> | null {
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
const statusMap: Map<V, TopologicalStatus> = new Map<V, TopologicalStatus>();
const statusMap: Map<V | VertexId, TopologicalStatus> = new Map<V | VertexId, TopologicalStatus>();
for (const entry of this._vertices) {
statusMap.set(entry[1], 0);
}
const sorted: (V)[] = [];
const sorted: (V | VertexId)[] = [];
let hasCycle = false;
const dfs = (cur: V) => {
const dfs = (cur: V | VertexId) => {
statusMap.set(cur, 1);
const children = this.getDestinations(cur);
for (const child of children) {

View file

@ -63,17 +63,6 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
return this._edges;
}
protected set edges(v: Map<V, E[]>) {
this._edges = v;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getEdges(): Map<V, E[]> {
return this._edges;
}
/**
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
* @param {V | null | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID
@ -256,4 +245,8 @@ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdg
return null;
}
}
protected _setEdges(v: Map<V, E[]>) {
this._edges = v;
}
}

View file

@ -17,17 +17,6 @@ export class CoordinateMap<V> extends Map<any, V> {
return this._joint;
}
protected set joint(v: string) {
this._joint = v;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getJoint(): string {
return this._joint;
}
/**
* The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
* key array with a specified delimiter.
@ -71,4 +60,8 @@ export class CoordinateMap<V> extends Map<any, V> {
override delete(key: number[]) {
return super.delete(key.join(this._joint));
}
protected _setJoint(v: string) {
this._joint = v;
}
}

View file

@ -17,17 +17,6 @@ export class CoordinateSet extends Set {
return this._joint;
}
protected set joint(v: string) {
this._joint = v;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getJoint(): string {
return this._joint;
}
/**
* The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
* joining its elements with a specified separator.
@ -60,4 +49,8 @@ export class CoordinateSet extends Set {
override delete(value: number[]) {
return super.delete(value.join(this._joint));
}
protected _setJoint(v: string) {
this._joint = v;
}
}

View file

@ -1 +1,3 @@
export {};
export class HashTable {
}

View file

@ -1 +1,3 @@
export {};
export class Pair {
}

View file

@ -1 +1,3 @@
export {};
export class TreeMap {
}

View file

@ -1 +1,3 @@
export {};
export class TreeSet {
}

View file

@ -60,19 +60,11 @@ export abstract class Heap<T = number> {
return this._pq;
}
protected set pq(v: PriorityQueue<HeapItem<T>>) {
this._pq = v;
}
protected _priorityCb: (val: T) => number;
get priorityCb() {
return this._priorityCb;
}
protected set priorityCb(v: (val: T) => number) {
this._priorityCb = v;
}
/**
* The function returns the size of a priority queue.
* @returns The size of the priority queue.
@ -81,27 +73,6 @@ export abstract class Heap<T = number> {
return this._pq.size;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getPq() {
return this._pq;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getPriorityCb() {
return this._priorityCb;
}
/**
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
*/
getSize(): number {
return this._pq.size;
}
/**
* The function checks if a priority queue is empty.
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
@ -179,10 +150,10 @@ export abstract class Heap<T = number> {
* @returns a boolean value.
*/
has(node: T | HeapItem<T>): boolean {
if (node instanceof HeapItem<T>) {
return this.getPq().getNodes().includes(node);
if (node instanceof HeapItem) {
return this.pq.getNodes().includes(node);
} else {
return this.getPq().getNodes().findIndex(item => {
return this.pq.getNodes().findIndex(item => {
return item.val === node;
}) !== -1;
}

View file

@ -86,10 +86,6 @@ export class DoublyLinkedList<T> {
return this._length;
}
protected set length(value: number) {
this._length = value;
}
/**
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
* given array.
@ -104,10 +100,6 @@ export class DoublyLinkedList<T> {
return doublyLinkedList;
}
getLength(): number {
return this._length;
}
/**
* The push function adds a new node with the given value to the end of the doubly linked list.
* @param {T} val - The value to be added to the linked list.
@ -122,7 +114,7 @@ export class DoublyLinkedList<T> {
this.tail!.next = newNode;
this.tail = newNode;
}
this.length++;
this._length++;
}
/**
@ -140,7 +132,7 @@ export class DoublyLinkedList<T> {
this.tail = removedNode.prev;
this.tail!.next = null;
}
this.length--;
this._length--;
return removedNode.val;
}
@ -159,7 +151,7 @@ export class DoublyLinkedList<T> {
this.head = removedNode.next;
this.head!.prev = null;
}
this.length--;
this._length--;
return removedNode.val;
}
@ -178,7 +170,7 @@ export class DoublyLinkedList<T> {
this.head!.prev = newNode;
this.head = newNode;
}
this.length++;
this._length++;
}
/**
@ -261,7 +253,7 @@ export class DoublyLinkedList<T> {
newNode.next = nextNode;
prevNode!.next = newNode;
nextNode!.prev = newNode;
this.length++;
this._length++;
return true;
}
@ -282,7 +274,7 @@ export class DoublyLinkedList<T> {
const nextNode = removedNode!.next;
prevNode!.next = nextNode;
nextNode!.prev = prevNode;
this.length--;
this._length--;
return removedNode!.val;
}
@ -298,7 +290,7 @@ export class DoublyLinkedList<T> {
delete(valOrNode: T | DoublyLinkedListNode<T>): boolean {
let node: DoublyLinkedListNode<T> | null;
if (valOrNode instanceof DoublyLinkedListNode<T>) {
if (valOrNode instanceof DoublyLinkedListNode) {
node = valOrNode;
} else {
node = this.findNode(valOrNode);
@ -314,7 +306,7 @@ export class DoublyLinkedList<T> {
const nextNode = node.next;
prevNode!.next = nextNode;
nextNode!.prev = prevNode;
this.length--;
this._length--;
}
return true;
}
@ -515,7 +507,7 @@ export class DoublyLinkedList<T> {
insertAfter(existingValueOrNode: T | DoublyLinkedListNode<T>, newValue: T): boolean {
let existingNode;
if (existingValueOrNode instanceof DoublyLinkedListNode<T>) {
if (existingValueOrNode instanceof DoublyLinkedListNode) {
existingNode = existingValueOrNode;
} else {
existingNode = this.findNode(existingValueOrNode);
@ -532,7 +524,7 @@ export class DoublyLinkedList<T> {
if (existingNode === this.tail) {
this.tail = newNode;
}
this.length++;
this._length++;
return true;
}
@ -554,7 +546,7 @@ export class DoublyLinkedList<T> {
insertBefore(existingValueOrNode: T | DoublyLinkedListNode<T>, newValue: T): boolean {
let existingNode;
if (existingValueOrNode instanceof DoublyLinkedListNode<T>) {
if (existingValueOrNode instanceof DoublyLinkedListNode) {
existingNode = existingValueOrNode;
} else {
existingNode = this.findNode(existingValueOrNode);
@ -571,7 +563,7 @@ export class DoublyLinkedList<T> {
if (existingNode === this.head) {
this.head = newNode;
}
this.length++;
this._length++;
return true;
}

View file

@ -75,10 +75,6 @@ export class SinglyLinkedList<T> {
return this._length;
}
protected set length(value: number) {
this._length = value;
}
/**
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
* array.
@ -111,7 +107,7 @@ export class SinglyLinkedList<T> {
this.tail!.next = newNode;
this.tail = newNode;
}
this.length++;
this._length++;
}
/**
@ -126,7 +122,7 @@ export class SinglyLinkedList<T> {
const val = this.head.val;
this.head = null;
this.tail = null;
this.length--;
this._length--;
return val;
}
@ -137,7 +133,7 @@ export class SinglyLinkedList<T> {
const val = this.tail!.val;
current.next = null;
this.tail = current;
this.length--;
this._length--;
return val;
}
@ -149,7 +145,7 @@ export class SinglyLinkedList<T> {
if (!this.head) return null;
const removedNode = this.head;
this.head = this.head.next;
this.length--;
this._length--;
return removedNode.val;
}
@ -167,7 +163,7 @@ export class SinglyLinkedList<T> {
newNode.next = this.head;
this.head = newNode;
}
this.length++;
this._length++;
}
/**
@ -216,7 +212,7 @@ export class SinglyLinkedList<T> {
const prevNode = this.getNodeAt(index - 1);
const removedNode = prevNode!.next;
prevNode!.next = removedNode!.next;
this.length--;
this._length--;
return removedNode!.val;
}
@ -231,7 +227,7 @@ export class SinglyLinkedList<T> {
*/
delete(valueOrNode: T | SinglyLinkedListNode<T>): boolean {
let value: T;
if (valueOrNode instanceof SinglyLinkedListNode<T>) {
if (valueOrNode instanceof SinglyLinkedListNode) {
value = valueOrNode.val;
} else {
value = valueOrNode;
@ -251,7 +247,7 @@ export class SinglyLinkedList<T> {
this.tail = prev;
}
}
this.length--;
this._length--;
return true;
}
prev = current;
@ -285,7 +281,7 @@ export class SinglyLinkedList<T> {
const prevNode = this.getNodeAt(index - 1);
newNode.next = prevNode!.next;
prevNode!.next = newNode;
this.length++;
this._length++;
return true;
}
@ -415,7 +411,7 @@ export class SinglyLinkedList<T> {
if (!this.head) return false;
let existingValue: T;
if (existingValueOrNode instanceof SinglyLinkedListNode<T>) {
if (existingValueOrNode instanceof SinglyLinkedListNode) {
existingValue = existingValueOrNode.val;
} else {
existingValue = existingValueOrNode;
@ -431,7 +427,7 @@ export class SinglyLinkedList<T> {
const newNode = new SinglyLinkedListNode(newValue);
newNode.next = current.next;
current.next = newNode;
this.length++;
this._length++;
return true;
}
current = current.next;
@ -466,7 +462,7 @@ export class SinglyLinkedList<T> {
if (existingNode === this.tail) {
this.tail = newNode;
}
this.length++;
this._length++;
return true;
}

View file

@ -1,2 +1,3 @@
export class SkipLinkedList {
}

View file

@ -1,24 +1,50 @@
export class TreeNode<T = number> {
id: string;
name?: string | undefined;
value?: T | undefined;
children?: TreeNode<T>[] | undefined;
constructor(id: string, name?: string, value?: T, children?: TreeNode<T>[]) {
this.id = id;
this.name = name || '';
this.value = value || undefined;
this.children = children || [];
this._id = id;
this._name = name || '';
this._value = value || undefined;
this._children = children || [];
}
// TODO get set
// get name (): string | undefined {
// return this.name;
// }
//
// set name (name: string | undefined) {
// this.name = name;
// }
private _id: string;
get id(): string {
return this._id;
}
set id(value: string) {
this._id = value;
}
private _name?: string | undefined;
get name(): string | undefined {
return this._name;
}
set name(value: string | undefined) {
this._name = value;
}
private _value?: T | undefined;
get value(): T | undefined {
return this._value;
}
set value(value: T | undefined) {
this._value = value;
}
private _children?: TreeNode<T>[] | undefined;
get children(): TreeNode<T>[] | undefined {
return this._children;
}
set children(value: TreeNode<T>[] | undefined) {
this._children = value;
}
addChildren(children: TreeNode<T> | TreeNode<T> []) {
if (!this.children) {

View file

@ -6,14 +6,23 @@
* @license MIT License
*/
export class TrieNode {
protected _value;
constructor(v: string) {
this._value = v;
this._val = v;
this._isEnd = false;
this._children = new Map<string, TrieNode>();
}
private _val;
get val(): string {
return this._val;
}
set val(v: string) {
this._val = v;
}
protected _children: Map<string, TrieNode>;
get children(): Map<string, TrieNode> {
@ -34,13 +43,6 @@ export class TrieNode {
this._isEnd = v;
}
get val(): string {
return this._value;
}
set val(v: string) {
this._value = v;
}
}
export class Trie {

View file

@ -1 +1 @@
export type DoublyLinkedListGetBy = 'node' | 'val';
export {}

View file

@ -32,7 +32,7 @@ describe('MaxHeap Operation Test', () => {
let i = 0;
while (maxHeap.size > 0) {
const polled = maxHeap.poll();
expect(polled).toBeInstanceOf(HeapItem<{ keyA: string }>);
expect(polled).toBeInstanceOf(HeapItem);
polled && expect(polled.val).toHaveProperty('keyA');
polled && polled.val && expect(polled.val.keyA).toBe(values[i]);
i++;

View file

@ -22,19 +22,19 @@ describe('MinHeap Operation Test', () => {
expect(minNumHeap.size).toBe(6);
const poll1 = minNumHeap.poll();
expect(poll1).toBeInstanceOf(HeapItem<number>)
expect(poll1).toBeInstanceOf(HeapItem)
poll1 instanceof HeapItem && expect(poll1.val).toBe(0);
const poll2 = minNumHeap.poll();
expect(poll2).toBeInstanceOf(HeapItem<number>)
expect(poll2).toBeInstanceOf(HeapItem)
poll2 instanceof HeapItem && expect(poll2.val).toBe(1);
const peek1 = minNumHeap.peek();
expect(peek1).toBeInstanceOf(HeapItem<number>)
expect(peek1).toBeInstanceOf(HeapItem)
peek1 instanceof HeapItem && expect(peek1.val).toBe(2);
const heapArray = minNumHeap.toArray();
expect(heapArray).toBeInstanceOf(Array<HeapItem>);
expect(heapArray).toBeInstanceOf(Array);
expect(heapArray.map(item => item.priority)).toEqual([2, 5, 9, 6]);
expect(minNumHeap.size).toBe(4);
});
@ -72,7 +72,7 @@ describe('MinHeap Operation Test', () => {
let i = 0;
while (minObjHeap.size > 0) {
const polled = minObjHeap.poll();
expect(polled).toBeInstanceOf(HeapItem<MyObject>);
expect(polled).toBeInstanceOf(HeapItem);
polled && expect(polled.val).toBeInstanceOf(MyObject);
polled && polled.val && expect(polled.val.keyA).toBe(values[i]);
i++;

View file

@ -201,7 +201,7 @@ describe('DoublyLinkedList Operation Test', () => {
list.clear();
expect(list.getLength()).toBe(0);
expect(list.length).toBe(0);
expect(list.head).toBe(null);
expect(list.tail).toBe(null);
});

View file

@ -148,7 +148,7 @@ describe('SinglyLinkedList Operation Test', () => {
list.push(3);
list.clear();
expect(list.toArray()).toEqual([]);
expect(list.getLength()).toBe(0);
expect(list.length).toBe(0);
expect(list.isEmpty()).toBe(true);
});
});
@ -235,10 +235,10 @@ describe('SinglyLinkedList Operation Test', () => {
describe('getLength', () => {
it('should return the correct length of the list', () => {
expect(list.getLength()).toBe(0);
expect(list.length).toBe(0);
list.push(1);
list.push(2);
expect(list.getLength()).toBe(2);
expect(list.length).toBe(2);
});
});

View file

@ -13,8 +13,8 @@
"moduleResolution": "node",
"declarationDir": "./dist",
"skipLibCheck": true,
"downlevelIteration": true,
"downlevelIteration": true,
"experimentalDecorators": true,
// "allowJs": true,
// "allowSyntheticDefaultImports": true,
// "forceConsistentCasingInFileNames": true,