From 407c3b104aa0be92d42c41eeb61b9ef8ca42e51d Mon Sep 17 00:00:00 2001 From: Revone Date: Thu, 31 Aug 2023 22:25:41 +0800 Subject: [PATCH] Tidying up the code and identifying any further requirements that need to be marked as TODO. --- .../binary-tree/abstract-binary-tree.ts | 161 ++++++++---------- src/data-structures/binary-tree/avl-tree.ts | 1 + src/data-structures/binary-tree/bst.ts | 1 + .../binary-tree/tree-multiset.ts | 37 ++-- .../interfaces/abstract-binary-tree.ts | 41 +---- .../types/abstract-binary-tree.ts | 2 +- src/data-structures/types/tree-multiset.ts | 4 +- tsconfig.json | 3 +- 8 files changed, 96 insertions(+), 154 deletions(-) diff --git a/src/data-structures/binary-tree/abstract-binary-tree.ts b/src/data-structures/binary-tree/abstract-binary-tree.ts index e3ec846..02c33a8 100644 --- a/src/data-structures/binary-tree/abstract-binary-tree.ts +++ b/src/data-structures/binary-tree/abstract-binary-tree.ts @@ -143,11 +143,15 @@ export abstract class AbstractBinaryTree = []; + private _visitedVal: N['val'][] = []; - get visitedVal(): Array { + get visitedVal(): N['val'][] { return this._visitedVal; } @@ -199,12 +197,6 @@ export abstract class AbstractBinaryTree { const queue: Array = [root]; while (queue.length > 0) { @@ -279,18 +277,20 @@ export abstract class AbstractBinaryTree = new Map(); - if (this.isMergeDuplicatedVal) { + if (this.isMergeDuplicatedNodeById) { for (const idOrNode of idsOrNodes) map.set(idOrNode, (map.get(idOrNode) ?? 0) + 1); } @@ -379,7 +374,7 @@ export abstract class AbstractBinaryTree 0; } @@ -647,6 +643,7 @@ export abstract class AbstractBinaryTree { nodeOrPropertyName = nodeOrPropertyName ?? 'id'; - this._resetResults(); + this._clearResults(); const queue: Array = [this.root]; while (queue.length !== 0) { @@ -978,7 +976,7 @@ export abstract class AbstractBinaryTree { pattern = pattern ?? 'in'; nodeOrPropertyName = nodeOrPropertyName ?? 'id'; - this._resetResults(); + this._clearResults(); const _traverse = (node: N) => { switch (pattern) { case 'in': @@ -1028,7 +1026,7 @@ export abstract class AbstractBinaryTree { pattern = pattern || 'in'; nodeOrPropertyName = nodeOrPropertyName || 'id'; - this._resetResults(); + this._clearResults(); if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName); // 0: visit, 1: print const stack: { opt: 0 | 1, node: N | null | undefined }[] = [{opt: 0, node: this.root}]; @@ -1092,7 +1090,7 @@ export abstract class AbstractBinaryTree 0) { @@ -1226,7 +1224,7 @@ export abstract class AbstractBinaryTree { @@ -1338,14 +1336,6 @@ export abstract class AbstractBinaryTree = AVLTreeNode> extends B * @returns The method is returning the inserted node, or null or undefined if the insertion was not successful. */ override add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined { + // TODO support node as a param const inserted = super.add(id, val); if (inserted) this.balancePath(inserted); return inserted; diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 224047f..47862c5 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -53,6 +53,7 @@ export class BST = BSTNode> extends BinaryTree * If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`. */ override add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined { + // TODO support node as a param let inserted: N | null = null; const newNode = this.createNode(id, val); if (this.root === null) { diff --git a/src/data-structures/binary-tree/tree-multiset.ts b/src/data-structures/binary-tree/tree-multiset.ts index dce06b2..e11d1ed 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -50,7 +50,7 @@ export class TreeMultiset = TreeMultiset * TreeMultiset. */ constructor(options?: TreeMultisetOptions) { - super({...options, isMergeDuplicatedVal: true}); + super({...options, isMergeDuplicatedNodeById: true}); } private _count = 0; @@ -80,28 +80,25 @@ export class TreeMultiset = TreeMultiset * @returns the `destNode` after swapping its values with the `srcNode`. */ override swapLocation(srcNode: N, destNode: N): N { - const {val, count, height, id} = destNode; + const {id, val, count, height} = destNode; const tempNode = this.createNode(id, val, count); if (tempNode) { tempNode.height = height; - if (tempNode instanceof TreeMultisetNode) { - destNode.id = srcNode.id; - destNode.val = srcNode.val; - destNode.count = srcNode.count; - destNode.height = srcNode.height; + destNode.id = srcNode.id; + destNode.val = srcNode.val; + destNode.count = srcNode.count; + destNode.height = srcNode.height; - srcNode.id = tempNode.id; - srcNode.val = tempNode.val; - srcNode.count = tempNode.count; - srcNode.height = tempNode.height; - } + srcNode.id = tempNode.id; + srcNode.val = tempNode.val; + srcNode.count = tempNode.count; + srcNode.height = tempNode.height; } return destNode; } - /** * The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if * necessary. @@ -114,9 +111,7 @@ export class TreeMultiset = TreeMultiset */ override add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val'], count?: number): N | null | undefined { count = count ?? 1; - let inserted: N | null | undefined = undefined; - let newNode; - let id; + let inserted: N | null | undefined = undefined, newNode: N | null; if (idOrNode instanceof TreeMultisetNode) { newNode = this.createNode(idOrNode.id, idOrNode.val, idOrNode.count); } else if (idOrNode === null) { @@ -194,7 +189,6 @@ export class TreeMultiset = TreeMultiset override addTo(newNode: N | null, parent: N): N | null | undefined { if (parent) { if (parent.left === undefined) { - parent.left = newNode; if (newNode !== null) { this._setSize(this.size + 1); @@ -231,7 +225,7 @@ export class TreeMultiset = TreeMultiset const inserted: (N | null | undefined)[] = []; const map: Map = new Map(); - if (this.isMergeDuplicatedVal) { + if (this.isMergeDuplicatedNodeById) { for (const idOrNode of idsOrNodes) map.set(idOrNode, (map.get(idOrNode) ?? 0) + 1); } @@ -247,9 +241,9 @@ export class TreeMultiset = TreeMultiset continue; } - const count = this.isMergeDuplicatedVal ? map.get(idOrNode) : 1; + const count = this.isMergeDuplicatedNodeById ? map.get(idOrNode) : 1; const val = data?.[i]; - if (this.isMergeDuplicatedVal) { + if (this.isMergeDuplicatedNodeById) { if (map.has(idOrNode)) { inserted.push(this.add(idOrNode, val, count)); map.delete(idOrNode); @@ -268,9 +262,10 @@ export class TreeMultiset = TreeMultiset */ override perfectlyBalance(): boolean { const sorted = this.DFS('in', 'node'), n = sorted.length; + if (sorted.length < 1) return false; + this.clear(); - if (sorted.length < 1) return false; if (this.loopType === LoopType.RECURSIVE) { const buildBalanceBST = (l: number, r: number) => { if (l > r) return; diff --git a/src/data-structures/interfaces/abstract-binary-tree.ts b/src/data-structures/interfaces/abstract-binary-tree.ts index 2a0eff3..18f39a0 100644 --- a/src/data-structures/interfaces/abstract-binary-tree.ts +++ b/src/data-structures/interfaces/abstract-binary-tree.ts @@ -51,13 +51,9 @@ export interface IAbstractBinaryTree - // _setLoopType(value: LoopType): void - // - // _setVisitedId(value: BinaryTreeNodeId[]): void - // - // _setVisitedVal(value: Array): void - // - // _setVisitedNode(value: N[]): void - // - // setVisitedCount(value: number[]): void - // - // _setVisitedLeftSum(value: number[]): void - // - // _setAutoIncrementId(value: boolean): void - // - // _setMaxId(value: number): void - // - // _setIsDuplicatedVal(value: boolean): void - // - // _setRoot(v: N | null): void - // - // _setSize(v: number): void - // - // _setCount(v: number): void - // - // _resetResults(): void - - // _pushByPropertyNameStopOrNot(cur: N, result: (N | null | undefined)[], nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean): void - // - // _accumulatedByPropertyName(node: N, nodeOrPropertyName ?: NodeOrPropertyName): void - // - // _getResultByPropertyName(nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties // --- end additional methods --- } \ No newline at end of file diff --git a/src/data-structures/types/abstract-binary-tree.ts b/src/data-structures/types/abstract-binary-tree.ts index 9391112..276a22a 100644 --- a/src/data-structures/types/abstract-binary-tree.ts +++ b/src/data-structures/types/abstract-binary-tree.ts @@ -37,5 +37,5 @@ export type AbstractBinaryTreeNodeNested = AbstractBinaryTreeNode = TreeMultisetNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -export type TreeMultisetOptions = Omit & { - isMergeDuplicatedVal: true, +export type TreeMultisetOptions = Omit & { + isMergeDuplicatedNodeById: true, } diff --git a/tsconfig.json b/tsconfig.json index c920e88..6e9936c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,6 @@ "module": "commonjs", "target": "es6", "lib": [ -// "es2015", "esnext" ], "strict": true, @@ -31,7 +30,7 @@ "src", ], "exclude": [ -// "node_modules/data-structure-typed", + // "node_modules/data-structure-typed", "node_modules", "dist" ]