From 26ab0be17bdab93334153705eebca1fbd734e679 Mon Sep 17 00:00:00 2001 From: Revone Date: Thu, 7 Sep 2023 21:00:22 +0800 Subject: [PATCH] [BinaryTree] isMergeDuplicatedNodeById removed, [MapGraph] MapGraph added --- LICENSE | 21 +++ .../binary-tree/abstract-binary-tree.ts | 132 +++++++----------- .../binary-tree/tree-multiset.ts | 50 +++---- src/data-structures/graph/abstract-graph.ts | 15 +- src/data-structures/graph/index.ts | 1 + .../interfaces/abstract-binary-tree.ts | 4 - .../interfaces/tree-multiset.ts | 1 - src/data-structures/tree/tree.ts | 2 +- .../types/abstract-binary-tree.ts | 1 - src/data-structures/types/abstract-graph.ts | 2 +- src/data-structures/types/index.ts | 1 + src/data-structures/types/tree-multiset.ts | 4 +- 12 files changed, 112 insertions(+), 122 deletions(-) diff --git a/LICENSE b/LICENSE index e69de29..ef3b183 100644 --- a/LICENSE +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Tyler Zeng + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/src/data-structures/binary-tree/abstract-binary-tree.ts b/src/data-structures/binary-tree/abstract-binary-tree.ts index 02c33a8..6be277b 100644 --- a/src/data-structures/binary-tree/abstract-binary-tree.ts +++ b/src/data-structures/binary-tree/abstract-binary-tree.ts @@ -141,15 +141,8 @@ export abstract class AbstractBinaryTree 0) { const cur = queue.shift(); if (cur) { - const inserted = this.addTo(newNode, cur); + if (newNode && cur.id === newNode.id) return; + const inserted = this._addTo(newNode, cur); if (inserted !== undefined) return inserted; if (cur.left) queue.push(cur.left); if (cur.right) queue.push(cur.right); @@ -310,39 +297,6 @@ export abstract class AbstractBinaryTree = new Map(); - if (this.isMergeDuplicatedNodeById) { - for (const idOrNode of idsOrNodes) map.set(idOrNode, (map.get(idOrNode) ?? 0) + 1); - } + for (const idOrNode of idsOrNodes) map.set(idOrNode, (map.get(idOrNode) ?? 0) + 1); for (let i = 0; i < idsOrNodes.length; i++) { const idOrNode = idsOrNodes[i]; - if (idOrNode instanceof AbstractBinaryTreeNode) { - inserted.push(this.add(idOrNode.id, idOrNode.val)); - continue; - } - - if (idOrNode === null) { - inserted.push(this.add(null)); - continue; - } - - const val = data?.[i]; - if (this.isMergeDuplicatedNodeById) { - if (map.has(idOrNode)) { - inserted.push(this.add(idOrNode, val)); - map.delete(idOrNode); + if (map.has(idOrNode)) { + if (idOrNode instanceof AbstractBinaryTreeNode) { + inserted.push(this.add(idOrNode.id, idOrNode.val)); + continue; } - } else { + + if (idOrNode === null) { + inserted.push(this.add(null)); + continue; + } + + const val = data?.[i]; + inserted.push(this.add(idOrNode, val)); + map.delete(idOrNode); } + } return inserted; } @@ -1001,11 +951,11 @@ export abstract class AbstractBinaryTree = TreeMultiset * TreeMultiset. */ constructor(options?: TreeMultisetOptions) { - super({...options, isMergeDuplicatedNodeById: true}); + super({...options}); } private _count = 0; @@ -178,15 +178,15 @@ export class TreeMultiset = TreeMultiset } /** - * The function adds a new node to a binary tree as the left or right child of a given parent node. - * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be - * either a node object (`N`) or `null`. + * The function adds a new node to a binary tree if there is an available slot on the left or right side of the parent + * node. + * @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to the tree. It can + * be either a node object (`N`) or `null`. * @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a * child. - * @returns either the left or right child node that was added to the parent node. It can also return `null` or - * `undefined` in certain cases. + * @returns The method returns either the `parent.left`, `parent.right`, or `undefined`. */ - override addTo(newNode: N | null, parent: N): N | null | undefined { + override _addTo(newNode: N | null, parent: N): N | null | undefined { if (parent) { if (parent.left === undefined) { parent.left = newNode; @@ -225,32 +225,28 @@ export class TreeMultiset = TreeMultiset const inserted: (N | null | undefined)[] = []; const map: Map = new Map(); - if (this.isMergeDuplicatedNodeById) { - for (const idOrNode of idsOrNodes) map.set(idOrNode, (map.get(idOrNode) ?? 0) + 1); - } + for (const idOrNode of idsOrNodes) map.set(idOrNode, (map.get(idOrNode) ?? 0) + 1); for (let i = 0; i < idsOrNodes.length; i++) { const idOrNode = idsOrNodes[i]; - if (idOrNode instanceof TreeMultisetNode) { - inserted.push(this.add(idOrNode.id, idOrNode.val, idOrNode.count)); - continue; - } + if (map.has(idOrNode)) { - if (idOrNode === null) { - inserted.push(this.add(NaN, null, 0)); - continue; - } - - const count = this.isMergeDuplicatedNodeById ? map.get(idOrNode) : 1; - const val = data?.[i]; - if (this.isMergeDuplicatedNodeById) { - if (map.has(idOrNode)) { - inserted.push(this.add(idOrNode, val, count)); - map.delete(idOrNode); + if (idOrNode instanceof TreeMultisetNode) { + inserted.push(this.add(idOrNode.id, idOrNode.val, idOrNode.count)); + continue; } - } else { - inserted.push(this.add(idOrNode, val, 1)); + + if (idOrNode === null) { + inserted.push(this.add(NaN, null, 0)); + continue; + } + + const val = data?.[i], count = map.get(idOrNode); + + inserted.push(this.add(idOrNode, val, count)); + map.delete(idOrNode); } + } return inserted; } diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index a2d8962..3634a35 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -10,7 +10,7 @@ import {PriorityQueue} from '../priority-queue'; import type {DijkstraResult, VertexId} from '../types'; import {IAbstractGraph} from '../interfaces'; -export abstract class AbstractVertex { +export abstract class AbstractVertex { /** * The function is a protected constructor that takes an id and an optional value as parameters. @@ -45,7 +45,7 @@ export abstract class AbstractVertex { } } -export abstract class AbstractEdge { +export abstract class AbstractEdge { /** * The above function is a protected constructor that initializes the weight, value, and hash code properties of an @@ -103,7 +103,7 @@ export abstract class AbstractEdge { } } -export abstract class AbstractGraph, E extends AbstractEdge> implements IAbstractGraph { +export abstract class AbstractGraph = AbstractVertex, E extends AbstractEdge = AbstractEdge> implements IAbstractGraph { private _vertices: Map = new Map(); get vertices(): Map { @@ -604,9 +604,7 @@ export abstract class AbstractGraph, E extends Abs const srcVertex = this._getVertex(src); const destVertex = dest ? this._getVertex(dest) : null; - if (!srcVertex) { - return null; - } + if (!srcVertex) return null; for (const vertex of vertices) { const vertexOrId = vertex[1]; @@ -619,6 +617,11 @@ export abstract class AbstractGraph, E extends Abs distMap.set(srcVertex, 0); preMap.set(srcVertex, null); + /** + * The function `getPaths` retrieves all paths from vertices to a specified minimum vertex. + * @param {V | null} minV - The parameter `minV` is of type `V | null`. It represents the minimum vertex value or + * null. + */ const getPaths = (minV: V | null) => { for (const vertex of vertices) { const vertexOrId = vertex[1]; diff --git a/src/data-structures/graph/index.ts b/src/data-structures/graph/index.ts index 8bcd2a0..013277c 100644 --- a/src/data-structures/graph/index.ts +++ b/src/data-structures/graph/index.ts @@ -1,3 +1,4 @@ export * from './abstract-graph'; export * from './directed-graph'; export * from './undirected-graph'; +export * from './map-graph'; diff --git a/src/data-structures/interfaces/abstract-binary-tree.ts b/src/data-structures/interfaces/abstract-binary-tree.ts index 18f39a0..fed8df7 100644 --- a/src/data-structures/interfaces/abstract-binary-tree.ts +++ b/src/data-structures/interfaces/abstract-binary-tree.ts @@ -53,8 +53,6 @@ export interface IAbstractBinaryTree): boolean diff --git a/src/data-structures/interfaces/tree-multiset.ts b/src/data-structures/interfaces/tree-multiset.ts index f9c83e1..64a4267 100644 --- a/src/data-structures/interfaces/tree-multiset.ts +++ b/src/data-structures/interfaces/tree-multiset.ts @@ -8,5 +8,4 @@ export interface ITreeMultisetNode> extends IAVLTree { - } \ No newline at end of file diff --git a/src/data-structures/tree/tree.ts b/src/data-structures/tree/tree.ts index 72ecfda..608b820 100644 --- a/src/data-structures/tree/tree.ts +++ b/src/data-structures/tree/tree.ts @@ -1,4 +1,4 @@ -export class TreeNode { +export class TreeNode { constructor(id: string, value?: T, children?: TreeNode[]) { this._id = id; this._value = value || undefined; diff --git a/src/data-structures/types/abstract-binary-tree.ts b/src/data-structures/types/abstract-binary-tree.ts index 276a22a..4188ba8 100644 --- a/src/data-structures/types/abstract-binary-tree.ts +++ b/src/data-structures/types/abstract-binary-tree.ts @@ -37,5 +37,4 @@ export type AbstractBinaryTreeNodeNested = AbstractBinaryTreeNode = - { distMap: Map, preMap: Map, seen: Set, paths: V[][], minDist: number, minPath: V[] } + { distMap: Map, distPaths?: Map ,preMap: Map, seen: Set, paths: V[][], minDist: number, minPath: V[] } | null; diff --git a/src/data-structures/types/index.ts b/src/data-structures/types/index.ts index 42e67c4..b78574b 100644 --- a/src/data-structures/types/index.ts +++ b/src/data-structures/types/index.ts @@ -4,6 +4,7 @@ export * from './avl-tree'; export * from './segment-tree'; export * from './tree-multiset'; export * from './abstract-graph'; +export * from './map-graph'; export * from './abstract-binary-tree'; export * from './rb-tree'; export * from './directed-graph'; diff --git a/src/data-structures/types/tree-multiset.ts b/src/data-structures/types/tree-multiset.ts index dcf284a..3778140 100644 --- a/src/data-structures/types/tree-multiset.ts +++ b/src/data-structures/types/tree-multiset.ts @@ -3,6 +3,4 @@ import {AVLTreeOptions} from './avl-tree'; export type TreeMultisetNodeNested = TreeMultisetNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -export type TreeMultisetOptions = Omit & { - isMergeDuplicatedNodeById: true, -} +export type TreeMultisetOptions = Omit & {}