From c1b5969c511b4da43b4af29072ecf94f9ad1b567 Mon Sep 17 00:00:00 2001 From: Revone Date: Mon, 21 Aug 2023 16:17:01 +0800 Subject: [PATCH] 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 "_". --- package-lock.json | 85 ++---- package.json | 4 +- src/data-structures/binary-tree/avl-tree.ts | 8 +- .../binary-tree/binary-indexed-tree.ts | 11 +- .../binary-tree/binary-tree.ts | 274 +++++++++--------- src/data-structures/binary-tree/bst.ts | 34 +-- .../binary-tree/segment-tree.ts | 110 +++---- src/data-structures/graph/abstract-graph.ts | 34 +-- src/data-structures/graph/directed-graph.ts | 26 +- src/data-structures/graph/undirected-graph.ts | 15 +- src/data-structures/hash/coordinate-map.ts | 15 +- src/data-structures/hash/coordinate-set.ts | 15 +- src/data-structures/hash/hash-table.ts | 4 +- src/data-structures/hash/pair.ts | 4 +- src/data-structures/hash/tree-map.ts | 4 +- src/data-structures/hash/tree-set.ts | 4 +- src/data-structures/heap/heap.ts | 35 +-- .../linked-list/doubly-linked-list.ts | 32 +- .../linked-list/singly-linked-list.ts | 28 +- .../linked-list/skip-linked-list.ts | 1 + src/data-structures/tree/tree.ts | 60 ++-- src/data-structures/trie/trie.ts | 20 +- .../types/doubly-linked-list.ts | 2 +- .../data-structures/heap/max-heap.test.ts | 2 +- .../data-structures/heap/min-heap.test.ts | 10 +- .../linked-list/doubly-linked-list.test.ts | 2 +- .../linked-list/singly-linked-list.test.ts | 6 +- tsconfig.json | 4 +- 28 files changed, 364 insertions(+), 485 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7d8a9a1..0c22d3d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 08c159a..954dfb4 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index 44a5adb..282fd21 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -141,7 +141,7 @@ export class AVLTree extends BST { } 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 extends BST { } 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 extends BST { } 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 extends BST { if (A === this.root) { - if (C) this.root = C; + if (C) this._setRoot(C); } else { if (parentOfA) { if (parentOfA.left === A) { diff --git a/src/data-structures/binary-tree/binary-indexed-tree.ts b/src/data-structures/binary-tree/binary-indexed-tree.ts index 56880ce..91a0716 100644 --- a/src/data-structures/binary-tree/binary-indexed-tree.ts +++ b/src/data-structures/binary-tree/binary-indexed-tree.ts @@ -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(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; + } } diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 0b7c6ea..c6e4682 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -36,7 +36,7 @@ export class BinaryTreeNode { this._count = count ?? 1; } - protected _id: BinaryTreeNodeId; + private _id: BinaryTreeNodeId; get id(): BinaryTreeNodeId { return this._id; @@ -46,7 +46,7 @@ export class BinaryTreeNode { this._id = v; } - protected _val: T; + private _val: T; get val(): T { return this._val; } @@ -55,7 +55,7 @@ export class BinaryTreeNode { this._val = v; } - protected _left?: BinaryTreeNode | null; + private _left?: BinaryTreeNode | null; get left(): BinaryTreeNode | null | undefined { return this._left; @@ -69,7 +69,7 @@ export class BinaryTreeNode { this._left = v; } - protected _right?: BinaryTreeNode | null; + private _right?: BinaryTreeNode | null; get right(): BinaryTreeNode | null | undefined { return this._right; @@ -83,7 +83,7 @@ export class BinaryTreeNode { this._right = v; } - protected _parent: BinaryTreeNode | null | undefined; + private _parent: BinaryTreeNode | null | undefined; get parent(): BinaryTreeNode | null | undefined { return this._parent; @@ -93,7 +93,7 @@ export class BinaryTreeNode { 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 { this._familyPosition = v; } - protected _count = 1; + private _count = 1; get count(): number { return this._count; @@ -113,7 +113,7 @@ export class BinaryTreeNode { this._count = v; } - protected _height = 0; + private _height = 0; get height(): number { return this._height; @@ -123,62 +123,6 @@ export class BinaryTreeNode { 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 | 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 | 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 | 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): BinaryTreeNode { const {val, count, height} = swapNode; const tempNode = new BinaryTreeNode(swapNode.id, val); @@ -204,15 +148,6 @@ export class BinaryTreeNode { } export class BinaryTree { - protected _loopType: LoopType = LoopType.iterative; - protected _visitedId: BinaryTreeNodeId[] = []; - protected _visitedVal: Array = []; - protected _visitedNode: BinaryTreeNode[] = []; - 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 { } } - protected _root: BinaryTreeNode | 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 = []; + + get visitedVal(): Array { + return this._visitedVal; + } + + private _visitedNode: BinaryTreeNode[] = []; + + get visitedNode(): BinaryTreeNode[] { + 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 | null = null; get root(): BinaryTreeNode | null { return this._root; } - protected set root(v: BinaryTreeNode | 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 | 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 { * 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 { 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(id, val, count) : null; + this._setRoot(val !== null ? new BinaryTreeNode(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 { } 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 { } 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 { 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 { } } - // --- start additional methods --- - /** * The `isBST` function checks if a binary tree is a binary search tree. * @param {BinaryTreeNode | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode @@ -881,6 +829,8 @@ export class BinaryTree { } } + // --- 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 { 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 { return this._getResultByPropertyName(nodeOrPropertyName); } + protected _setLoopType(value: LoopType) { + this._loopType = value; + } + + protected _setVisitedId(value: BinaryTreeNodeId[]) { + this._visitedId = value; + } + + protected _setVisitedVal(value: Array) { + this._visitedVal = value; + } + + protected _setVisitedNode(value: BinaryTreeNode[]) { + 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 | 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. */ diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 4ef2107..6641b44 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -54,9 +54,9 @@ export class BST extends BinaryTree { let inserted: BSTNode | 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 extends BinaryTree { 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 extends BinaryTree { } //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 extends BinaryTree { } //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 extends BinaryTree { 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 extends BinaryTree { } } } - 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 extends BinaryTree { if (!this.root) return []; const result: BSTNode[] = []; - if (this._loopType === LoopType.recursive) { + if (this.loopType === LoopType.recursive) { const _traverse = (cur: BSTNode) => { if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return; @@ -287,7 +287,7 @@ export class BST extends BinaryTree { let sum = 0; - if (this._loopType === LoopType.recursive) { + if (this.loopType === LoopType.recursive) { const _traverse = (cur: BSTNode): void => { const compared = this._compare(cur.id, id); if (compared === CP.eq) { @@ -360,7 +360,7 @@ export class BST extends BinaryTree { } } - if (this._loopType === LoopType.recursive) { + if (this.loopType === LoopType.recursive) { const _traverse = (cur: BSTNode) => { const compared = this._compare(cur.id, node.id); _sumByPropertyName(cur); @@ -398,7 +398,7 @@ export class BST extends BinaryTree { 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 extends BinaryTree { let balanced = true; - if (this._loopType === LoopType.recursive) { + if (this.loopType === LoopType.recursive) { const _height = (cur: BSTNode | null | undefined): number => { if (!cur) return 0; const leftHeight = _height(cur.left), rightHeight = _height(cur.right); diff --git a/src/data-structures/binary-tree/segment-tree.ts b/src/data-structures/binary-tree/segment-tree.ts index 13268fe..e88e4a6 100644 --- a/src/data-structures/binary-tree/segment-tree.ts +++ b/src/data-structures/binary-tree/segment-tree.ts @@ -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; + } } diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 0581f70..2d2db55 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -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`. */ dijkstraWithoutHeap(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult { - if (getMinDist === undefined) getMinDist = false; + if (getMinDist === undefined) getMinDist = false ; if (genPaths === undefined) genPaths = false; if (dest === undefined) dest = null; diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index e76876a..41e6b86 100644 --- a/src/data-structures/graph/directed-graph.ts +++ b/src/data-structures/graph/directed-graph.ts @@ -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 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 | 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 = new Map(); + const statusMap: Map = new Map(); 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) { diff --git a/src/data-structures/graph/undirected-graph.ts b/src/data-structures/graph/undirected-graph.ts index 67623d2..d53d2f6 100644 --- a/src/data-structures/graph/undirected-graph.ts +++ b/src/data-structures/graph/undirected-graph.ts @@ -63,17 +63,6 @@ export class UndirectedGraph) { - 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 { - 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) { + this._edges = v; + } } diff --git a/src/data-structures/hash/coordinate-map.ts b/src/data-structures/hash/coordinate-map.ts index 788cb47..4463845 100644 --- a/src/data-structures/hash/coordinate-map.ts +++ b/src/data-structures/hash/coordinate-map.ts @@ -17,17 +17,6 @@ export class CoordinateMap extends Map { 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 extends Map { override delete(key: number[]) { return super.delete(key.join(this._joint)); } + + protected _setJoint(v: string) { + this._joint = v; + } } \ No newline at end of file diff --git a/src/data-structures/hash/coordinate-set.ts b/src/data-structures/hash/coordinate-set.ts index d5d1011..937ea7e 100644 --- a/src/data-structures/hash/coordinate-set.ts +++ b/src/data-structures/hash/coordinate-set.ts @@ -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; + } } \ No newline at end of file diff --git a/src/data-structures/hash/hash-table.ts b/src/data-structures/hash/hash-table.ts index 509db18..53ba10a 100644 --- a/src/data-structures/hash/hash-table.ts +++ b/src/data-structures/hash/hash-table.ts @@ -1 +1,3 @@ -export {}; +export class HashTable { + +} diff --git a/src/data-structures/hash/pair.ts b/src/data-structures/hash/pair.ts index cb0ff5c..ebe7a6f 100644 --- a/src/data-structures/hash/pair.ts +++ b/src/data-structures/hash/pair.ts @@ -1 +1,3 @@ -export {}; +export class Pair { + +} diff --git a/src/data-structures/hash/tree-map.ts b/src/data-structures/hash/tree-map.ts index cb0ff5c..a7bf001 100644 --- a/src/data-structures/hash/tree-map.ts +++ b/src/data-structures/hash/tree-map.ts @@ -1 +1,3 @@ -export {}; +export class TreeMap { + +} diff --git a/src/data-structures/hash/tree-set.ts b/src/data-structures/hash/tree-set.ts index cb0ff5c..72dc44c 100644 --- a/src/data-structures/hash/tree-set.ts +++ b/src/data-structures/hash/tree-set.ts @@ -1 +1,3 @@ -export {}; +export class TreeSet { + +} diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index 3aae2d6..6e9c896 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -60,19 +60,11 @@ export abstract class Heap { return this._pq; } - protected set pq(v: PriorityQueue>) { - 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 { 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 { * @returns a boolean value. */ has(node: T | HeapItem): boolean { - if (node instanceof HeapItem) { - 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; } diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index d5ad36f..6e5a1de 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -86,10 +86,6 @@ export class DoublyLinkedList { 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 { 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 { this.tail!.next = newNode; this.tail = newNode; } - this.length++; + this._length++; } /** @@ -140,7 +132,7 @@ export class DoublyLinkedList { this.tail = removedNode.prev; this.tail!.next = null; } - this.length--; + this._length--; return removedNode.val; } @@ -159,7 +151,7 @@ export class DoublyLinkedList { this.head = removedNode.next; this.head!.prev = null; } - this.length--; + this._length--; return removedNode.val; } @@ -178,7 +170,7 @@ export class DoublyLinkedList { this.head!.prev = newNode; this.head = newNode; } - this.length++; + this._length++; } /** @@ -261,7 +253,7 @@ export class DoublyLinkedList { newNode.next = nextNode; prevNode!.next = newNode; nextNode!.prev = newNode; - this.length++; + this._length++; return true; } @@ -282,7 +274,7 @@ export class DoublyLinkedList { const nextNode = removedNode!.next; prevNode!.next = nextNode; nextNode!.prev = prevNode; - this.length--; + this._length--; return removedNode!.val; } @@ -298,7 +290,7 @@ export class DoublyLinkedList { delete(valOrNode: T | DoublyLinkedListNode): boolean { let node: DoublyLinkedListNode | null; - if (valOrNode instanceof DoublyLinkedListNode) { + if (valOrNode instanceof DoublyLinkedListNode) { node = valOrNode; } else { node = this.findNode(valOrNode); @@ -314,7 +306,7 @@ export class DoublyLinkedList { const nextNode = node.next; prevNode!.next = nextNode; nextNode!.prev = prevNode; - this.length--; + this._length--; } return true; } @@ -515,7 +507,7 @@ export class DoublyLinkedList { insertAfter(existingValueOrNode: T | DoublyLinkedListNode, newValue: T): boolean { let existingNode; - if (existingValueOrNode instanceof DoublyLinkedListNode) { + if (existingValueOrNode instanceof DoublyLinkedListNode) { existingNode = existingValueOrNode; } else { existingNode = this.findNode(existingValueOrNode); @@ -532,7 +524,7 @@ export class DoublyLinkedList { if (existingNode === this.tail) { this.tail = newNode; } - this.length++; + this._length++; return true; } @@ -554,7 +546,7 @@ export class DoublyLinkedList { insertBefore(existingValueOrNode: T | DoublyLinkedListNode, newValue: T): boolean { let existingNode; - if (existingValueOrNode instanceof DoublyLinkedListNode) { + if (existingValueOrNode instanceof DoublyLinkedListNode) { existingNode = existingValueOrNode; } else { existingNode = this.findNode(existingValueOrNode); @@ -571,7 +563,7 @@ export class DoublyLinkedList { if (existingNode === this.head) { this.head = newNode; } - this.length++; + this._length++; return true; } diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index 7f2805b..fb052b6 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -75,10 +75,6 @@ export class SinglyLinkedList { 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 { this.tail!.next = newNode; this.tail = newNode; } - this.length++; + this._length++; } /** @@ -126,7 +122,7 @@ export class SinglyLinkedList { const val = this.head.val; this.head = null; this.tail = null; - this.length--; + this._length--; return val; } @@ -137,7 +133,7 @@ export class SinglyLinkedList { const val = this.tail!.val; current.next = null; this.tail = current; - this.length--; + this._length--; return val; } @@ -149,7 +145,7 @@ export class SinglyLinkedList { 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 { newNode.next = this.head; this.head = newNode; } - this.length++; + this._length++; } /** @@ -216,7 +212,7 @@ export class SinglyLinkedList { 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 { */ delete(valueOrNode: T | SinglyLinkedListNode): boolean { let value: T; - if (valueOrNode instanceof SinglyLinkedListNode) { + if (valueOrNode instanceof SinglyLinkedListNode) { value = valueOrNode.val; } else { value = valueOrNode; @@ -251,7 +247,7 @@ export class SinglyLinkedList { this.tail = prev; } } - this.length--; + this._length--; return true; } prev = current; @@ -285,7 +281,7 @@ export class SinglyLinkedList { 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 { if (!this.head) return false; let existingValue: T; - if (existingValueOrNode instanceof SinglyLinkedListNode) { + if (existingValueOrNode instanceof SinglyLinkedListNode) { existingValue = existingValueOrNode.val; } else { existingValue = existingValueOrNode; @@ -431,7 +427,7 @@ export class SinglyLinkedList { 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 { if (existingNode === this.tail) { this.tail = newNode; } - this.length++; + this._length++; return true; } diff --git a/src/data-structures/linked-list/skip-linked-list.ts b/src/data-structures/linked-list/skip-linked-list.ts index 9d79cfe..b0a65be 100644 --- a/src/data-structures/linked-list/skip-linked-list.ts +++ b/src/data-structures/linked-list/skip-linked-list.ts @@ -1,2 +1,3 @@ export class SkipLinkedList { + } \ No newline at end of file diff --git a/src/data-structures/tree/tree.ts b/src/data-structures/tree/tree.ts index 40b4ff2..d42b472 100644 --- a/src/data-structures/tree/tree.ts +++ b/src/data-structures/tree/tree.ts @@ -1,24 +1,50 @@ export class TreeNode { - id: string; - name?: string | undefined; - value?: T | undefined; - children?: TreeNode[] | undefined; - constructor(id: string, name?: string, value?: T, children?: TreeNode[]) { - 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[] | undefined; + + get children(): TreeNode[] | undefined { + return this._children; + } + + set children(value: TreeNode[] | undefined) { + this._children = value; + } addChildren(children: TreeNode | TreeNode []) { if (!this.children) { diff --git a/src/data-structures/trie/trie.ts b/src/data-structures/trie/trie.ts index d476ead..4ee1e18 100644 --- a/src/data-structures/trie/trie.ts +++ b/src/data-structures/trie/trie.ts @@ -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(); } + private _val; + + get val(): string { + return this._val; + } + + set val(v: string) { + this._val = v; + } + protected _children: Map; get children(): Map { @@ -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 { diff --git a/src/data-structures/types/doubly-linked-list.ts b/src/data-structures/types/doubly-linked-list.ts index 6576f6d..693da49 100644 --- a/src/data-structures/types/doubly-linked-list.ts +++ b/src/data-structures/types/doubly-linked-list.ts @@ -1 +1 @@ -export type DoublyLinkedListGetBy = 'node' | 'val'; \ No newline at end of file +export {} \ No newline at end of file diff --git a/tests/unit/data-structures/heap/max-heap.test.ts b/tests/unit/data-structures/heap/max-heap.test.ts index 7e9487f..409ef40 100644 --- a/tests/unit/data-structures/heap/max-heap.test.ts +++ b/tests/unit/data-structures/heap/max-heap.test.ts @@ -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++; diff --git a/tests/unit/data-structures/heap/min-heap.test.ts b/tests/unit/data-structures/heap/min-heap.test.ts index 7eb1422..2409208 100644 --- a/tests/unit/data-structures/heap/min-heap.test.ts +++ b/tests/unit/data-structures/heap/min-heap.test.ts @@ -22,19 +22,19 @@ describe('MinHeap Operation Test', () => { expect(minNumHeap.size).toBe(6); const poll1 = minNumHeap.poll(); - expect(poll1).toBeInstanceOf(HeapItem) + expect(poll1).toBeInstanceOf(HeapItem) poll1 instanceof HeapItem && expect(poll1.val).toBe(0); const poll2 = minNumHeap.poll(); - expect(poll2).toBeInstanceOf(HeapItem) + expect(poll2).toBeInstanceOf(HeapItem) poll2 instanceof HeapItem && expect(poll2.val).toBe(1); const peek1 = minNumHeap.peek(); - expect(peek1).toBeInstanceOf(HeapItem) + expect(peek1).toBeInstanceOf(HeapItem) peek1 instanceof HeapItem && expect(peek1.val).toBe(2); const heapArray = minNumHeap.toArray(); - expect(heapArray).toBeInstanceOf(Array); + 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); + expect(polled).toBeInstanceOf(HeapItem); polled && expect(polled.val).toBeInstanceOf(MyObject); polled && polled.val && expect(polled.val.keyA).toBe(values[i]); i++; diff --git a/tests/unit/data-structures/linked-list/doubly-linked-list.test.ts b/tests/unit/data-structures/linked-list/doubly-linked-list.test.ts index bdc68de..1bcdfa1 100644 --- a/tests/unit/data-structures/linked-list/doubly-linked-list.test.ts +++ b/tests/unit/data-structures/linked-list/doubly-linked-list.test.ts @@ -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); }); diff --git a/tests/unit/data-structures/linked-list/singly-linked-list.test.ts b/tests/unit/data-structures/linked-list/singly-linked-list.test.ts index 83fa389..86cfee5 100644 --- a/tests/unit/data-structures/linked-list/singly-linked-list.test.ts +++ b/tests/unit/data-structures/linked-list/singly-linked-list.test.ts @@ -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); }); }); diff --git a/tsconfig.json b/tsconfig.json index a9d74e7..7cb3ea8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,8 +13,8 @@ "moduleResolution": "node", "declarationDir": "./dist", "skipLibCheck": true, - "downlevelIteration": true, - + "downlevelIteration": true, + "experimentalDecorators": true, // "allowJs": true, // "allowSyntheticDefaultImports": true, // "forceConsistentCasingInFileNames": true,