From 8712bbd3c9eb3a8e9b4c4d3348ea20ddb3a856af Mon Sep 17 00:00:00 2001 From: Revone Date: Tue, 31 Oct 2023 11:15:39 +0800 Subject: [PATCH] [core] All data structures have had their member variables access control optimized. --- src/data-structures/binary-tree/avl-tree.ts | 6 +- .../binary-tree/binary-indexed-tree.ts | 18 +--- .../binary-tree/binary-tree.ts | 59 ++++------- src/data-structures/binary-tree/bst.ts | 11 +-- src/data-structures/binary-tree/rb-tree.ts | 34 +++---- .../binary-tree/segment-tree.ts | 99 +++---------------- .../binary-tree/tree-multiset.ts | 17 ++-- src/data-structures/graph/abstract-graph.ts | 88 ++++------------- src/data-structures/graph/directed-graph.ts | 52 +++------- src/data-structures/graph/map-graph.ts | 39 ++------ src/data-structures/graph/undirected-graph.ts | 35 ++----- src/data-structures/hash/hash-map.ts | 2 +- src/data-structures/hash/tree-map.ts | 3 +- src/data-structures/hash/tree-set.ts | 3 +- src/data-structures/heap/heap.ts | 4 +- src/data-structures/heap/max-heap.ts | 2 +- src/data-structures/heap/min-heap.ts | 2 +- .../linked-list/doubly-linked-list.ts | 2 +- .../linked-list/singly-linked-list.ts | 2 +- src/data-structures/matrix/matrix.ts | 4 +- src/data-structures/matrix/matrix2d.ts | 2 +- src/data-structures/matrix/navigator.ts | 8 +- src/data-structures/matrix/vector2d.ts | 3 +- .../priority-queue/max-priority-queue.ts | 2 +- .../priority-queue/min-priority-queue.ts | 2 +- .../priority-queue/priority-queue.ts | 2 +- src/data-structures/queue/deque.ts | 10 +- src/data-structures/queue/queue.ts | 2 +- src/data-structures/stack/stack.ts | 11 ++- src/data-structures/tree/tree.ts | 10 +- src/data-structures/trie/trie.ts | 21 ++-- src/interfaces/binary-tree.ts | 2 +- src/types/data-structures/binary-tree/bst.ts | 2 +- src/types/data-structures/matrix/navigator.ts | 2 +- src/types/utils/utils.ts | 2 +- src/types/utils/validate-type.ts | 4 +- .../binary-tree/binary-index-tree.test.ts | 4 +- .../graph/abstract-graph.test.ts | 3 +- .../graph/directed-graph.test.ts | 8 +- .../data-structures/graph/map-graph.test.ts | 4 +- 40 files changed, 179 insertions(+), 407 deletions(-) diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index 368f3f7..d98a67c 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -21,8 +21,7 @@ export class AVLTreeNode = AVLTreeNodeNeste export class AVLTree = AVLTreeNode>> extends BST - implements IBinaryTree -{ + implements IBinaryTree { /** * This is a constructor function for an AVL tree data structure in TypeScript. * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the @@ -56,7 +55,6 @@ export class AVLTree = AVLTreeNode = AVLTreeNode) { - this._freqMap = value; - } - protected _msb: number; get msb(): number { return this._msb; } - set msb(value: number) { - this._msb = value; - } - protected _negativeCount: number; get negativeCount(): number { return this._negativeCount; } - set negativeCount(value: number) { - this._negativeCount = value; - } - get freq(): number { return this._freq; } @@ -232,9 +220,9 @@ export class BinaryIndexedTree { */ protected _updateNegativeCount(freqCur: number, freqNew: number): void { if (freqCur < 0 && freqNew >= 0) { - this.negativeCount--; + this._negativeCount--; } else if (freqCur >= 0 && freqNew < 0) { - this.negativeCount++; + this._negativeCount++; } } diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 08a8dfd..e1b2446 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -43,7 +43,7 @@ export class BinaryTreeNode = BinaryTree this.value = value; } - private _left: N | null | undefined; + protected _left: N | null | undefined; /** * Get the left child node. @@ -63,7 +63,7 @@ export class BinaryTreeNode = BinaryTree this._left = v; } - private _right: N | null | undefined; + protected _right: N | null | undefined; /** * Get the right child node. @@ -108,8 +108,9 @@ export class BinaryTreeNode = BinaryTree * @template N - The type of the binary tree's nodes. */ export class BinaryTree = BinaryTreeNode>> - implements IBinaryTree -{ + implements IBinaryTree { + iterationType: IterationType = IterationType.ITERATIVE; + /** * Creates a new instance of BinaryTree. * @param {BinaryTreeOptions} [options] - The options for the binary tree. @@ -117,28 +118,11 @@ export class BinaryTree = BinaryTreeNode constructor(options?: BinaryTreeOptions) { if (options !== undefined) { const {iterationType = IterationType.ITERATIVE} = options; - this._iterationType = iterationType; + this.iterationType = iterationType; } } - private _iterationType: IterationType = IterationType.ITERATIVE; - - /** - * Get the iteration type used in the binary tree. - */ - get iterationType(): IterationType { - return this._iterationType; - } - - /** - * Set the iteration type for the binary tree. - * @param {IterationType} v - The new iteration type to set. - */ - set iterationType(v: IterationType) { - this._iterationType = v; - } - - private _root: N | null = null; + protected _root: N | null = null; /** * Get the root node of the binary tree. @@ -147,7 +131,7 @@ export class BinaryTree = BinaryTreeNode return this._root; } - private _size = 0; + protected _size = 0; /** * Get the number of nodes in the binary tree. @@ -170,7 +154,7 @@ export class BinaryTree = BinaryTreeNode * Clear the binary tree, removing all nodes. */ clear() { - this._root = null; + this._setRoot(null); this._size = 0; } @@ -229,9 +213,9 @@ export class BinaryTree = BinaryTreeNode } else { this._setRoot(needInsert); if (needInsert !== null) { - this._setSize(1); + this._size = 1; } else { - this._setSize(0); + this._size = 0; } inserted = this.root; } @@ -339,7 +323,7 @@ export class BinaryTree = BinaryTreeNode } } } - this._setSize(this.size - 1); + this._size = this.size - 1; bstDeletedResult.push({deleted: orgCurrent, needBalanced}); return bstDeletedResult; @@ -401,7 +385,7 @@ export class BinaryTree = BinaryTreeNode return -1; } - const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}]; + const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}]; let maxHeight = 0; while (stack.length > 0) { @@ -904,7 +888,7 @@ export class BinaryTree = BinaryTreeNode _traverse(beginRoot); } else { // 0: visit, 1: print - const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}]; + const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}]; while (stack.length > 0) { const cur = stack.pop(); @@ -1174,7 +1158,7 @@ export class BinaryTree = BinaryTreeNode * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the * binary tree nodes in a specific order. */ - *[Symbol.iterator](node = this.root): Generator { + * [Symbol.iterator](node = this.root): Generator { if (!node) { return; } @@ -1244,13 +1228,13 @@ export class BinaryTree = BinaryTreeNode if (parent.left === undefined) { parent.left = newNode; if (newNode) { - this._setSize(this.size + 1); + this._size = this.size + 1; } return parent.left; } else if (parent.right === undefined) { parent.right = newNode; if (newNode) { - this._setSize(this.size + 1); + this._size = this.size + 1; } return parent.right; } else { @@ -1274,14 +1258,5 @@ export class BinaryTree = BinaryTreeNode this._root = v; } - /** - * The function sets the value of the protected property "_size" to the given number. - * @param {number} v - The parameter "v" is a number that represents the size value that we want to - * set. - */ - protected _setSize(v: number) { - this._size = v; - } - // --- end additional methods --- } diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 7684bd8..e033122 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -5,7 +5,7 @@ * @copyright Copyright (c) 2022 Tyler Zeng * @license MIT License */ -import type {BTNKey, BSTComparator, BSTNodeNested, BSTOptions, BTNCallback} from '../../types'; +import type {BSTComparator, BSTNodeNested, BSTOptions, BTNCallback, BTNKey} from '../../types'; import {CP, IterationType} from '../../types'; import {BinaryTree, BinaryTreeNode} from './binary-tree'; import {IBinaryTree} from '../../interfaces'; @@ -19,8 +19,7 @@ export class BSTNode = BSTNodeNested> extend export class BST = BSTNode>> extends BinaryTree - implements IBinaryTree -{ + implements IBinaryTree { /** * The constructor function initializes a binary search tree object with an optional comparator * function. @@ -72,7 +71,7 @@ export class BST = BSTNode> } if (this.root === null) { this._setRoot(newNode); - this._setSize(this.size + 1); + this._size = this.size + 1; inserted = this.root; } else { let cur = this.root; @@ -94,7 +93,7 @@ export class BST = BSTNode> } //Add to the left of the current node cur.left = newNode; - this._setSize(this.size + 1); + this._size = this.size + 1; traversing = false; inserted = cur.left; } else { @@ -109,7 +108,7 @@ export class BST = BSTNode> } //Add to the right of the current node cur.right = newNode; - this._setSize(this.size + 1); + this._size = this.size + 1; traversing = false; inserted = cur.right; } else { diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 65edc63..3018dc7 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -5,24 +5,16 @@ import {BST, BSTNode} from './bst'; export class RBTreeNode = RBTreeNodeNested> extends BSTNode { constructor(key: BTNKey, value?: V) { super(key, value); - this._color = RBColor.RED; + this.color = RBColor.RED; } - private _color: RBColor; + color: RBColor; - get color(): RBColor { - return this._color; - } - - set color(value: RBColor) { - this._color = value; - } } export class RBTree = RBTreeNode>> extends BST - implements IBinaryTree -{ + implements IBinaryTree { constructor(options?: RBTreeOptions) { super(options); } @@ -38,7 +30,7 @@ export class RBTree = RBTreeNode = RBTreeNode = RBTreeNode = RBTreeNode = RBTreeNode = RBTreeNode = RBTreeNode = RBTreeNode = RBTreeNode = TreeMultisetNode>> extends AVLTree - implements IBinaryTree -{ + implements IBinaryTree { /** * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to * merge duplicated values. @@ -93,7 +92,7 @@ export class TreeMultiset = TreeMultis } if (!this.root) { this._setRoot(newNode); - this._setSize(this.size + 1); + this._size = this.size + 1; newNode && this._setCount(this.count + newNode.count); inserted = this.root; } else { @@ -113,7 +112,7 @@ export class TreeMultiset = TreeMultis if (cur.left === undefined) { //Add to the left of the current node cur.left = newNode; - this._setSize(this.size + 1); + this._size = this.size + 1; this._setCount(this.count + newNode.count); traversing = false; @@ -127,7 +126,7 @@ export class TreeMultiset = TreeMultis if (cur.right === undefined) { //Add to the right of the current node cur.right = newNode; - this._setSize(this.size + 1); + this._size = this.size + 1; this._setCount(this.count + newNode.count); traversing = false; @@ -162,7 +161,7 @@ export class TreeMultiset = TreeMultis if (parent.left === undefined) { parent.left = newNode; if (newNode !== null) { - this._setSize(this.size + 1); + this._size = this.size + 1; this._setCount(this.count + newNode.count); } @@ -170,7 +169,7 @@ export class TreeMultiset = TreeMultis } else if (parent.right === undefined) { parent.right = newNode; if (newNode !== null) { - this._setSize(this.size + 1); + this._size = (this.size + 1); this._setCount(this.count + newNode.count); } return parent.right; @@ -321,7 +320,7 @@ export class TreeMultiset = TreeMultis } } } - this._setSize(this.size - 1); + this._size = this.size - 1; // TODO How to handle when the count of target node is lesser than current node's count this._setCount(this.count - orgCurrent.count); } diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index bd67db9..6cd5504 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -12,6 +12,9 @@ import {IGraph} from '../../interfaces'; import {Queue} from '../queue'; export abstract class AbstractVertex { + key: VertexKey; + value: V | undefined; + /** * The function is a protected constructor that takes an key and an optional value as parameters. * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is @@ -20,32 +23,16 @@ export abstract class AbstractVertex { * vertex. If no value is provided, it will be set to undefined. */ protected constructor(key: VertexKey, value?: V) { - this._key = key; - this._value = value; + this.key = key; + this.value = value; } - private _key: VertexKey; - - get key(): VertexKey { - return this._key; - } - - set key(v: VertexKey) { - this._key = v; - } - - private _value: V | undefined; - - get value(): V | undefined { - return this._value; - } - - set value(value: V | undefined) { - this._value = value; - } } export abstract class AbstractEdge { + value: E | undefined; + weight: number; + /** * The above function is a protected constructor that initializes the weight, value, and hash code properties of an * object. @@ -56,31 +43,11 @@ export abstract class AbstractEdge { * meaning it can be omitted when creating an instance of the class. */ protected constructor(weight?: number, value?: E) { - this._weight = weight !== undefined ? weight : 1; - this._value = value; + this.weight = weight !== undefined ? weight : 1; + this.value = value; this._hashCode = uuidV4(); } - private _value: E | undefined; - - get value(): E | undefined { - return this._value; - } - - set value(value: E | undefined) { - this._value = value; - } - - private _weight: number; - - get weight(): number { - return this._weight; - } - - set weight(v: number) { - this._weight = v; - } - protected _hashCode: string; get hashCode(): string { @@ -91,15 +58,6 @@ export abstract class AbstractEdge { * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it. * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden. */ - - /** - * The function sets the value of the _hashCode property to the provided string. - * @param {string} v - The parameter "v" is of type string and represents the value that will be assigned to the - * "_hashCode" property. - */ - protected _setHashCode(v: string) { - this._hashCode = v; - } } export abstract class AbstractGraph< @@ -107,9 +65,8 @@ export abstract class AbstractGraph< E = any, VO extends AbstractVertex = AbstractVertex, EO extends AbstractEdge = AbstractEdge -> implements IGraph -{ - private _vertices: Map = new Map(); +> implements IGraph { + protected _vertices: Map = new Map(); get vertices(): Map { return this._vertices; @@ -556,14 +513,14 @@ export abstract class AbstractGraph< } getMinDist && - distMap.forEach((d, v) => { - if (v !== srcVertex) { - if (d < minDist) { - minDist = d; - if (genPaths) minDest = v; - } + distMap.forEach((d, v) => { + if (v !== srcVertex) { + if (d < minDist) { + minDist = d; + if (genPaths) minDest = v; } - }); + } + }); genPaths && getPaths(minDest); @@ -625,7 +582,7 @@ export abstract class AbstractGraph< if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity); } - const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key}); + const heap = new PriorityQueue<{ key: number; value: VO }>({comparator: (a, b) => a.key - b.key}); heap.add({key: 0, value: srcVertex}); distMap.set(srcVertex, 0); @@ -854,7 +811,7 @@ export abstract class AbstractGraph< * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest * path between vertices in the */ - floyd(): {costs: number[][]; predecessor: (VO | null)[][]} { + floyd(): { costs: number[][]; predecessor: (VO | null)[][] } { const idAndVertices = [...this._vertices]; const n = idAndVertices.length; @@ -1040,7 +997,4 @@ export abstract class AbstractGraph< return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey; } - protected _setVertices(value: Map) { - this._vertices = value; - } } diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index 59a3860..23fd02d 100644 --- a/src/data-structures/graph/directed-graph.ts +++ b/src/data-structures/graph/directed-graph.ts @@ -24,6 +24,9 @@ export class DirectedVertex extends AbstractVertex { } export class DirectedEdge extends AbstractEdge { + src: VertexKey; + dest: VertexKey; + /** * The constructor function initializes the source and destination vertices of an edge, along with an optional weight * and value. @@ -37,40 +40,19 @@ export class DirectedEdge extends AbstractEdge { */ constructor(src: VertexKey, dest: VertexKey, weight?: number, value?: E) { super(weight, value); - this._src = src; - this._dest = dest; - } - - private _src: VertexKey; - - get src(): VertexKey { - return this._src; - } - - set src(v: VertexKey) { - this._src = v; - } - - private _dest: VertexKey; - - get dest(): VertexKey { - return this._dest; - } - - set dest(v: VertexKey) { - this._dest = v; + this.src = src; + this.dest = dest; } } export class DirectedGraph< - V = any, - E = any, - VO extends DirectedVertex = DirectedVertex, - EO extends DirectedEdge = DirectedEdge - > + V = any, + E = any, + VO extends DirectedVertex = DirectedVertex, + EO extends DirectedEdge = DirectedEdge +> extends AbstractGraph - implements IGraph -{ + implements IGraph { /** * The constructor function initializes an instance of a class. */ @@ -78,13 +60,13 @@ export class DirectedGraph< super(); } - private _outEdgeMap: Map = new Map(); + protected _outEdgeMap: Map = new Map(); get outEdgeMap(): Map { return this._outEdgeMap; } - private _inEdgeMap: Map = new Map(); + protected _inEdgeMap: Map = new Map(); get inEdgeMap(): Map { return this._inEdgeMap; @@ -464,12 +446,4 @@ export class DirectedGraph< return false; } } - - protected _setOutEdgeMap(value: Map) { - this._outEdgeMap = value; - } - - protected _setInEdgeMap(value: Map) { - this._inEdgeMap = value; - } } diff --git a/src/data-structures/graph/map-graph.ts b/src/data-structures/graph/map-graph.ts index e7d6b5b..7edce9b 100644 --- a/src/data-structures/graph/map-graph.ts +++ b/src/data-structures/graph/map-graph.ts @@ -2,6 +2,9 @@ import {MapGraphCoordinate, VertexKey} from '../../types'; import {DirectedEdge, DirectedGraph, DirectedVertex} from './directed-graph'; export class MapVertex extends DirectedVertex { + lat: number; + long: number; + /** * The constructor function initializes an object with an key, latitude, longitude, and an optional value. * @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. @@ -16,28 +19,8 @@ export class MapVertex extends DirectedVertex { */ constructor(key: VertexKey, value: V, lat: number, long: number) { super(key, value); - this._lat = lat; - this._long = long; - } - - private _lat: number; - - get lat(): number { - return this._lat; - } - - set lat(value: number) { - this._lat = value; - } - - private _long: number; - - get long(): number { - return this._long; - } - - set long(value: number) { - this._long = value; + this.lat = lat; + this.long = long; } } @@ -78,26 +61,18 @@ export class MapGraph< this._bottomRight = bottomRight; } - private _origin: MapGraphCoordinate = [0, 0]; + protected _origin: MapGraphCoordinate = [0, 0]; get origin(): MapGraphCoordinate { return this._origin; } - set origin(value: MapGraphCoordinate) { - this._origin = value; - } - - private _bottomRight: MapGraphCoordinate | undefined; + protected _bottomRight: MapGraphCoordinate | undefined; get bottomRight(): MapGraphCoordinate | undefined { return this._bottomRight; } - set bottomRight(value: MapGraphCoordinate | undefined) { - this._bottomRight = value; - } - /** * The function creates a new vertex with the given key, value, latitude, and longitude. * @param {VertexKey} key - The key parameter is the unique identifier for the vertex. It is of type VertexKey, which could diff --git a/src/data-structures/graph/undirected-graph.ts b/src/data-structures/graph/undirected-graph.ts index 39b63e7..75f15fc 100644 --- a/src/data-structures/graph/undirected-graph.ts +++ b/src/data-structures/graph/undirected-graph.ts @@ -24,6 +24,8 @@ export class UndirectedVertex extends AbstractVertex { } export class UndirectedEdge extends AbstractEdge { + vertices: [VertexKey, VertexKey]; + /** * The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional * value. @@ -36,29 +38,18 @@ export class UndirectedEdge extends AbstractEdge { */ constructor(v1: VertexKey, v2: VertexKey, weight?: number, value?: E) { super(weight, value); - this._vertices = [v1, v2]; - } - - private _vertices: [VertexKey, VertexKey]; - - get vertices() { - return this._vertices; - } - - set vertices(v: [VertexKey, VertexKey]) { - this._vertices = v; + this.vertices = [v1, v2]; } } export class UndirectedGraph< - V = any, - E = any, - VO extends UndirectedVertex = UndirectedVertex, - EO extends UndirectedEdge = UndirectedEdge - > + V = any, + E = any, + VO extends UndirectedVertex = UndirectedVertex, + EO extends UndirectedEdge = UndirectedEdge +> extends AbstractGraph - implements IGraph -{ + implements IGraph { /** * The constructor initializes a new Map object to store edges. */ @@ -265,12 +256,4 @@ export class UndirectedGraph< } return true; } - - /** - * The function sets the edges of a graph. - * @param v - A map where the keys are of type VO and the values are arrays of type EO. - */ - protected _setEdges(v: Map) { - this._edges = v; - } } diff --git a/src/data-structures/hash/hash-map.ts b/src/data-structures/hash/hash-map.ts index 9595be5..cb59fc3 100644 --- a/src/data-structures/hash/hash-map.ts +++ b/src/data-structures/hash/hash-map.ts @@ -133,7 +133,7 @@ export class HashMap { } } - *entries(): IterableIterator<[K, V]> { + * entries(): IterableIterator<[K, V]> { for (const bucket of this.table) { if (bucket) { for (const [key, value] of bucket) { diff --git a/src/data-structures/hash/tree-map.ts b/src/data-structures/hash/tree-map.ts index fe86360..a6d743d 100644 --- a/src/data-structures/hash/tree-map.ts +++ b/src/data-structures/hash/tree-map.ts @@ -1 +1,2 @@ -export class TreeMap {} +export class TreeMap { +} diff --git a/src/data-structures/hash/tree-set.ts b/src/data-structures/hash/tree-set.ts index 591aeda..65f14db 100644 --- a/src/data-structures/hash/tree-set.ts +++ b/src/data-structures/hash/tree-set.ts @@ -1 +1,2 @@ -export class TreeSet {} +export class TreeSet { +} diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index 60d4533..999454a 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -8,7 +8,7 @@ import type {Comparator, DFSOrderPattern} from '../../types'; export class Heap { - constructor(options: {comparator: Comparator; nodes?: E[]}) { + constructor(options: { comparator: Comparator; nodes?: E[] }) { this._comparator = options.comparator; if (options.nodes && options.nodes.length > 0) { this._nodes = options.nodes; @@ -48,7 +48,7 @@ export class Heap { * @returns A new Heap instance. * @param options */ - static heapify(options: {nodes: E[]; comparator: Comparator}): Heap { + static heapify(options: { nodes: E[]; comparator: Comparator }): Heap { return new Heap(options); } diff --git a/src/data-structures/heap/max-heap.ts b/src/data-structures/heap/max-heap.ts index be2c9b1..139ef64 100644 --- a/src/data-structures/heap/max-heap.ts +++ b/src/data-structures/heap/max-heap.ts @@ -11,7 +11,7 @@ import type {Comparator} from '../../types'; export class MaxHeap extends Heap { constructor( - options: {comparator: Comparator; nodes?: E[]} = { + options: { comparator: Comparator; nodes?: E[] } = { comparator: (a: E, b: E) => { if (!(typeof a === 'number' && typeof b === 'number')) { throw new Error('The a, b params of compare function must be number'); diff --git a/src/data-structures/heap/min-heap.ts b/src/data-structures/heap/min-heap.ts index dc86f87..5057017 100644 --- a/src/data-structures/heap/min-heap.ts +++ b/src/data-structures/heap/min-heap.ts @@ -11,7 +11,7 @@ import type {Comparator} from '../../types'; export class MinHeap extends Heap { constructor( - options: {comparator: Comparator; nodes?: E[]} = { + options: { comparator: Comparator; nodes?: E[] } = { comparator: (a: E, b: E) => { if (!(typeof a === 'number' && typeof b === 'number')) { throw new Error('The a, b params of compare function must be number'); diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index 119ae71..41b9dc9 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -594,7 +594,7 @@ export class DoublyLinkedList { /** * The function returns an iterator that iterates over the values of a linked list. */ - *[Symbol.iterator]() { + * [Symbol.iterator]() { let current = this.head; while (current) { diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index 7c39cf6..04c0bbe 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -565,7 +565,7 @@ export class SinglyLinkedList { /** * The function returns an iterator that iterates over the values of a linked list. */ - *[Symbol.iterator]() { + * [Symbol.iterator]() { let current = this.head; while (current) { diff --git a/src/data-structures/matrix/matrix.ts b/src/data-structures/matrix/matrix.ts index 8f27617..4f1b8b8 100644 --- a/src/data-structures/matrix/matrix.ts +++ b/src/data-structures/matrix/matrix.ts @@ -7,14 +7,14 @@ */ // todo need to be improved export class MatrixNTI2D { - private readonly _matrix: Array>; + protected readonly _matrix: Array>; /** * The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a * given initial value or 0 if not provided. * @param options - An object containing the following properties: */ - constructor(options: {row: number; col: number; initialVal?: V}) { + constructor(options: { row: number; col: number; initialVal?: V }) { const {row, col, initialVal} = options; this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0)); } diff --git a/src/data-structures/matrix/matrix2d.ts b/src/data-structures/matrix/matrix2d.ts index ab9bfef..6ea16c5 100644 --- a/src/data-structures/matrix/matrix2d.ts +++ b/src/data-structures/matrix/matrix2d.ts @@ -8,7 +8,7 @@ import {Vector2D} from './vector2d'; export class Matrix2D { - private readonly _matrix: number[][]; + protected readonly _matrix: number[][]; /** * The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix diff --git a/src/data-structures/matrix/navigator.ts b/src/data-structures/matrix/navigator.ts index dd2f854..2aab50b 100644 --- a/src/data-structures/matrix/navigator.ts +++ b/src/data-structures/matrix/navigator.ts @@ -27,10 +27,10 @@ export class Character { export class Navigator { onMove: (cur: [number, number]) => void; - private readonly _matrix: T[][]; - private readonly _cur: [number, number]; - private _character: Character; - private readonly _VISITED: T; + protected readonly _matrix: T[][]; + protected readonly _cur: [number, number]; + protected _character: Character; + protected readonly _VISITED: T; /** * The constructor initializes the Navigator object with the given parameters and sets the current position as visited diff --git a/src/data-structures/matrix/vector2d.ts b/src/data-structures/matrix/vector2d.ts index 1b2ff44..2f62f4e 100644 --- a/src/data-structures/matrix/vector2d.ts +++ b/src/data-structures/matrix/vector2d.ts @@ -10,7 +10,8 @@ export class Vector2D { public x: number = 0, public y: number = 0, public w: number = 1 // needed for matrix multiplication - ) {} + ) { + } /** * The function checks if the x and y values of a point are both zero. diff --git a/src/data-structures/priority-queue/max-priority-queue.ts b/src/data-structures/priority-queue/max-priority-queue.ts index dbb0793..409c99f 100644 --- a/src/data-structures/priority-queue/max-priority-queue.ts +++ b/src/data-structures/priority-queue/max-priority-queue.ts @@ -10,7 +10,7 @@ import type {Comparator} from '../../types'; export class MaxPriorityQueue extends PriorityQueue { constructor( - options: {comparator: Comparator; nodes?: E[]} = { + options: { comparator: Comparator; nodes?: E[] } = { comparator: (a: E, b: E) => { if (!(typeof a === 'number' && typeof b === 'number')) { throw new Error('The a, b params of compare function must be number'); diff --git a/src/data-structures/priority-queue/min-priority-queue.ts b/src/data-structures/priority-queue/min-priority-queue.ts index 8b8386f..da8ab64 100644 --- a/src/data-structures/priority-queue/min-priority-queue.ts +++ b/src/data-structures/priority-queue/min-priority-queue.ts @@ -10,7 +10,7 @@ import type {Comparator} from '../../types'; export class MinPriorityQueue extends PriorityQueue { constructor( - options: {comparator: Comparator; nodes?: E[]} = { + options: { comparator: Comparator; nodes?: E[] } = { comparator: (a: E, b: E) => { if (!(typeof a === 'number' && typeof b === 'number')) { throw new Error('The a, b params of compare function must be number'); diff --git a/src/data-structures/priority-queue/priority-queue.ts b/src/data-structures/priority-queue/priority-queue.ts index edfbaf2..60deb98 100644 --- a/src/data-structures/priority-queue/priority-queue.ts +++ b/src/data-structures/priority-queue/priority-queue.ts @@ -10,7 +10,7 @@ import {Heap} from '../heap'; import {Comparator} from '../../types'; export class PriorityQueue extends Heap { - constructor(options: {comparator: Comparator; nodes?: E[]}) { + constructor(options: { comparator: Comparator; nodes?: E[] }) { super(options); } } diff --git a/src/data-structures/queue/deque.ts b/src/data-structures/queue/deque.ts index 3dc8c58..f0911ab 100644 --- a/src/data-structures/queue/deque.ts +++ b/src/data-structures/queue/deque.ts @@ -9,7 +9,8 @@ import {DoublyLinkedList} from '../linked-list'; // O(n) time complexity of obtaining the value // O(1) time complexity of adding at the beginning and the end -export class Deque extends DoublyLinkedList {} +export class Deque extends DoublyLinkedList { +} // O(1) time complexity of obtaining the value // O(n) time complexity of adding at the beginning and the end @@ -19,9 +20,9 @@ export class ObjectDeque { if (capacity !== undefined) this._capacity = capacity; } - protected _nodes: {[key: number]: E} = {}; + protected _nodes: { [key: number]: E } = {}; - get nodes(): {[p: number]: E} { + get nodes(): { [p: number]: E } { return this._nodes; } @@ -148,10 +149,11 @@ export class ObjectDeque { // O(1) time complexity of obtaining the value // O(n) time complexity of adding at the beginning and the end export class ArrayDeque { + protected _nodes: E[] = []; + get nodes(): E[] { return this._nodes; } - protected _nodes: E[] = []; get size() { return this.nodes.length; diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index 9991f38..6222f35 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -201,7 +201,7 @@ export class Queue { return new Queue(this.nodes.slice(this.offset)); } - *[Symbol.iterator]() { + * [Symbol.iterator]() { for (const item of this.nodes) { yield item; } diff --git a/src/data-structures/stack/stack.ts b/src/data-structures/stack/stack.ts index 0b2bfce..e343f09 100644 --- a/src/data-structures/stack/stack.ts +++ b/src/data-structures/stack/stack.ts @@ -4,11 +4,6 @@ * @class */ export class Stack { - get elements(): E[] { - return this._elements; - } - protected _elements: E[]; - /** * The constructor initializes an array of elements, which can be provided as an optional parameter. * @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array @@ -19,6 +14,12 @@ export class Stack { this._elements = Array.isArray(elements) ? elements : []; } + protected _elements: E[]; + + get elements(): E[] { + return this._elements; + } + /** * The function "fromArray" creates a new Stack object from an array of elements. * @param {E[]} elements - The `elements` parameter is an array of elements of type `E`. diff --git a/src/data-structures/tree/tree.ts b/src/data-structures/tree/tree.ts index 729365a..e5a7e73 100644 --- a/src/data-structures/tree/tree.ts +++ b/src/data-structures/tree/tree.ts @@ -1,16 +1,14 @@ export class TreeNode { + key: string; + value?: V | undefined; + children?: TreeNode[] | undefined; + constructor(key: string, value?: V, children?: TreeNode[]) { this.key = key; this.value = value || undefined; this.children = children || []; } - key: string; - - value?: V | undefined; - - children?: TreeNode[] | undefined; - addChildren(children: TreeNode | TreeNode[]) { if (!this.children) { this.children = []; diff --git a/src/data-structures/trie/trie.ts b/src/data-structures/trie/trie.ts index 08bd374..25e31ca 100644 --- a/src/data-structures/trie/trie.ts +++ b/src/data-structures/trie/trie.ts @@ -11,28 +11,21 @@ * and a flag indicating whether it's the end of a word. */ export class TrieNode { + key: string; + children: Map; + isEnd: boolean; + constructor(key: string) { this.key = key; this.isEnd = false; this.children = new Map(); } - - key: string; - - children: Map; - - isEnd: boolean; } /** * Trie represents a Trie data structure. It provides basic Trie operations and additional methods. */ export class Trie { - get caseSensitive(): boolean { - return this._caseSensitive; - } - protected _caseSensitive: boolean; - constructor(words?: string[], caseSensitive = true) { this._root = new TrieNode(''); this._caseSensitive = caseSensitive; @@ -43,6 +36,12 @@ export class Trie { } } + protected _caseSensitive: boolean; + + get caseSensitive(): boolean { + return this._caseSensitive; + } + protected _root: TrieNode; get root() { diff --git a/src/interfaces/binary-tree.ts b/src/interfaces/binary-tree.ts index 649cf27..1e71463 100644 --- a/src/interfaces/binary-tree.ts +++ b/src/interfaces/binary-tree.ts @@ -1,5 +1,5 @@ import {BinaryTreeNode} from '../data-structures'; -import {BinaryTreeDeletedResult, BTNKey, BinaryTreeNodeNested, BTNCallback} from '../types'; +import {BinaryTreeDeletedResult, BinaryTreeNodeNested, BTNCallback, BTNKey} from '../types'; export interface IBinaryTree = BinaryTreeNodeNested> { createNode(key: BTNKey, value?: N['value']): N; diff --git a/src/types/data-structures/binary-tree/bst.ts b/src/types/data-structures/binary-tree/bst.ts index 6efe2d2..ad54f3c 100644 --- a/src/types/data-structures/binary-tree/bst.ts +++ b/src/types/data-structures/binary-tree/bst.ts @@ -1,5 +1,5 @@ import {BSTNode} from '../../../data-structures'; -import type {BTNKey, BinaryTreeOptions} from './binary-tree'; +import type {BinaryTreeOptions, BTNKey} from './binary-tree'; export type BSTComparator = (a: BTNKey, b: BTNKey) => number; diff --git a/src/types/data-structures/matrix/navigator.ts b/src/types/data-structures/matrix/navigator.ts index 9d8b9a9..34eddd9 100644 --- a/src/types/data-structures/matrix/navigator.ts +++ b/src/types/data-structures/matrix/navigator.ts @@ -1,6 +1,6 @@ export type Direction = 'up' | 'right' | 'down' | 'left'; -export type Turning = {[key in Direction]: Direction}; +export type Turning = { [key in Direction]: Direction }; export type NavigatorParams = { matrix: T[][]; diff --git a/src/types/utils/utils.ts b/src/types/utils/utils.ts index f4d26c4..1f3a505 100644 --- a/src/types/utils/utils.ts +++ b/src/types/utils/utils.ts @@ -1,5 +1,5 @@ export type ToThunkFn = () => ReturnType; -export type Thunk = () => ReturnType & {__THUNK__: symbol}; +export type Thunk = () => ReturnType & { __THUNK__: symbol }; export type TrlFn = (...args: any[]) => any; export type TrlAsyncFn = (...args: any[]) => any; diff --git a/src/types/utils/validate-type.ts b/src/types/utils/validate-type.ts index ac9ff28..3ebf451 100644 --- a/src/types/utils/validate-type.ts +++ b/src/types/utils/validate-type.ts @@ -1,6 +1,6 @@ -export type KeyValueObject = {[key: string]: any}; +export type KeyValueObject = { [key: string]: any }; -export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol}; +export type KeyValueObjectWithKey = { [key: string]: any; key: string | number | symbol }; export type NonNumberNonObjectButDefined = string | boolean | symbol | null; diff --git a/test/unit/data-structures/binary-tree/binary-index-tree.test.ts b/test/unit/data-structures/binary-tree/binary-index-tree.test.ts index 4135fab..7d63c02 100644 --- a/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +++ b/test/unit/data-structures/binary-tree/binary-index-tree.test.ts @@ -286,8 +286,8 @@ function loopLowerBoundTests(bit: BinaryIndexedTree, values: number[]) { describe('', () => { class NumArrayDC { - private _tree: BinaryIndexedTree; - private readonly _nums: number[]; + protected _tree: BinaryIndexedTree; + protected readonly _nums: number[]; constructor(nums: number[]) { this._nums = nums; diff --git a/test/unit/data-structures/graph/abstract-graph.test.ts b/test/unit/data-structures/graph/abstract-graph.test.ts index a9f6774..5205c66 100644 --- a/test/unit/data-structures/graph/abstract-graph.test.ts +++ b/test/unit/data-structures/graph/abstract-graph.test.ts @@ -19,7 +19,6 @@ class MyEdge extends AbstractEdge { this.src = srcOrV1; this.dest = destOrV2; this.data = value; - this._setHashCode(''); } } @@ -96,6 +95,6 @@ describe('AbstractGraph Operation Test', () => { eAB!.value = eAB.value; const hs = eAB.hashCode; - expect(hs).toBe(''); + expect(hs.length).toBe(36); }); }); diff --git a/test/unit/data-structures/graph/directed-graph.test.ts b/test/unit/data-structures/graph/directed-graph.test.ts index de01fc9..ff8b2ad 100644 --- a/test/unit/data-structures/graph/directed-graph.test.ts +++ b/test/unit/data-structures/graph/directed-graph.test.ts @@ -98,7 +98,7 @@ class MyVertex extends DirectedVertex { this._data = value; } - private _data: V | undefined; + protected _data: V | undefined; get data(): V | undefined { return this._data; @@ -115,7 +115,7 @@ class MyEdge extends DirectedEdge { this._data = value; } - private _data: E | undefined; + protected _data: E | undefined; get data(): E | undefined { return this._data; @@ -141,11 +141,11 @@ class MyDirectedGraph< } setInEdgeMap(value: Map) { - super._setInEdgeMap(value); + this._inEdgeMap = value; } setOutEdgeMap(value: Map) { - super._setOutEdgeMap(value); + this._outEdgeMap = value; } } diff --git a/test/unit/data-structures/graph/map-graph.test.ts b/test/unit/data-structures/graph/map-graph.test.ts index 94555c7..31a4f4c 100644 --- a/test/unit/data-structures/graph/map-graph.test.ts +++ b/test/unit/data-structures/graph/map-graph.test.ts @@ -88,8 +88,8 @@ describe('MapGraph', () => { const edgeAB = new MapEdge('A', 'B', 50, 'Edge from A to B'); const edgeBC = new MapEdge('B', 'C', 60, 'Edge from B to C'); - mapGraph.origin = mapGraph.origin; - mapGraph.bottomRight = mapGraph.bottomRight; + expect(mapGraph.origin).toEqual([0, 0]); + expect(mapGraph.bottomRight).toEqual([100, 100]); mapGraph.addVertex(locationA); mapGraph.addVertex(locationB);