[graph] The removal method in the graph data structure is standardized to 'delete'

This commit is contained in:
Revone 2023-10-28 15:58:48 +08:00
parent f8e0607b25
commit c11cc4527b
10 changed files with 61 additions and 61 deletions

View file

@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
## [v1.38.8](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
## [v1.38.9](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
### Changes

View file

@ -106,7 +106,7 @@ bst.getDepth(6) === 3; // true
bst.getLeftMost()?.id === 1; // true
bst.remove(6);
bst.delete(6);
bst.get(6); // null
bst.isAVLBalanced(); // true
bst.bfs()[0] === 11; // true
@ -121,7 +121,7 @@ objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
objBST.remove(11);
objBST.delete(11);
```
#### JS
@ -142,7 +142,7 @@ bst.getDepth(6) === 3; // true
const leftMost = bst.getLeftMost();
leftMost?.id === 1; // true
expect(leftMost?.id).toBe(1);
bst.remove(6);
bst.delete(6);
bst.get(6); // null
bst.isAVLBalanced(); // true or false
const bfsIDs = bst.bfs();
@ -159,12 +159,12 @@ objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
objBST.remove(11);
objBST.delete(11);
const avlTree = new AVLTree();
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
avlTree.isAVLBalanced(); // true
avlTree.remove(10);
avlTree.delete(10);
avlTree.isAVLBalanced(); // true
```
@ -178,7 +178,7 @@ import {AVLTree} from 'data-structure-typed';
const avlTree = new AVLTree();
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
avlTree.isAVLBalanced(); // true
avlTree.remove(10);
avlTree.delete(10);
avlTree.isAVLBalanced(); // true
```
@ -190,7 +190,7 @@ const {AVLTree} = require('data-structure-typed');
const avlTree = new AVLTree();
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
avlTree.isAVLBalanced(); // true
avlTree.remove(10);
avlTree.delete(10);
avlTree.isAVLBalanced(); // true
```
@ -214,7 +214,7 @@ graph.addEdge('A', 'B');
graph.hasEdge('A', 'B'); // true
graph.hasEdge('B', 'A'); // false
graph.removeEdgeSrcToDest('A', 'B');
graph.deleteEdgeSrcToDest('A', 'B');
graph.hasEdge('A', 'B'); // false
graph.addVertex('C');
@ -237,7 +237,7 @@ graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
graph.addVertex('D');
graph.removeVertex('C');
graph.deleteVertex('C');
graph.addEdge('A', 'B');
graph.addEdge('B', 'D');

50
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "data-structure-typed",
"version": "1.38.8",
"version": "1.38.9",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "data-structure-typed",
"version": "1.38.8",
"version": "1.38.9",
"license": "MIT",
"devDependencies": {
"@types/benchmark": "^2.1.3",
@ -15,17 +15,17 @@
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"auto-changelog": "^2.4.0",
"avl-tree-typed": "^1.38.7",
"avl-tree-typed": "^1.38.8",
"benchmark": "^2.1.4",
"binary-tree-typed": "^1.38.7",
"bst-typed": "^1.38.7",
"binary-tree-typed": "^1.38.8",
"bst-typed": "^1.38.8",
"dependency-cruiser": "^14.1.0",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"heap-typed": "^1.38.7",
"heap-typed": "^1.38.8",
"istanbul-badges-readme": "^1.8.5",
"jest": "^29.7.0",
"prettier": "^3.0.3",
@ -2728,12 +2728,12 @@
}
},
"node_modules/avl-tree-typed": {
"version": "1.38.7",
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.38.7.tgz",
"integrity": "sha512-B5lOJoYI/8Y+WUI+atnN8FtKwjOZk6X91O55Qu8p6nauduOZlJvx0HtN3mmTKk0bYNqEHDNK5iyHD60Rwqx5Hw==",
"version": "1.38.8",
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.38.8.tgz",
"integrity": "sha512-TMR5vZgH2fGUWDHqG7LNnGmWximwzg4tGthKNT3l9bL7zGnAg2C406j/6Hp/J29mvb5DV+j2NCCeBXmhu/B+AA==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.38.7"
"data-structure-typed": "^1.38.8"
}
},
"node_modules/babel-jest": {
@ -2927,12 +2927,12 @@
}
},
"node_modules/binary-tree-typed": {
"version": "1.38.7",
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.38.7.tgz",
"integrity": "sha512-Z/xh9iEg0r7WNUzRAPa/yPHJJpc3s0jgC8bioiKgoIU67wxV8Mbn5WbuF+m2oXXpYyaM4qhC+AGU43OkHss7rQ==",
"version": "1.38.8",
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.38.8.tgz",
"integrity": "sha512-sS1Efx5XDFY8d1r+pLrt17sZH2bDQbhzJm27OFPO6p/Qkz7qSeJZhYTrpnbr2IFjk3eh0ljkIa3e9+s7AYfhow==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.38.7"
"data-structure-typed": "^1.38.8"
}
},
"node_modules/brace-expansion": {
@ -3011,12 +3011,12 @@
}
},
"node_modules/bst-typed": {
"version": "1.38.7",
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.38.7.tgz",
"integrity": "sha512-jo8owKRnKPOAbdqR6SmvWjF2yceqN/3j736xujDum4Uh8devBLguV7m49HUv7pfkKAx8uwuuE+6b0dPQaxR4vA==",
"version": "1.38.8",
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.38.8.tgz",
"integrity": "sha512-gv3w5u3YIpnmQltcxWbLtpFqom3/z9b/NPdtc57m6rPaRAAWQy4pzyXAETgQX29p8RrCkVn/2cZLC6i6YfEjWw==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.38.7"
"data-structure-typed": "^1.38.8"
}
},
"node_modules/buffer-from": {
@ -3413,9 +3413,9 @@
}
},
"node_modules/data-structure-typed": {
"version": "1.38.7",
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.38.7.tgz",
"integrity": "sha512-YPV3hlUFTAG93+LoW8eaMv+KS4s9PF5MfpvvUkZybuR7WxqLtamCXWE1o3TRKkYFa9MEguxcq5ZaniCdeLg8Mw==",
"version": "1.38.8",
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.38.8.tgz",
"integrity": "sha512-HU+9+HqDLN9f9ipANtibwkK3d65f0FzpvuKavrHSbBMiJvufwchZUxNkfmwGLqgdX8MUGw/Rrk15GpEloYhKNw==",
"dev": true
},
"node_modules/debug": {
@ -4771,12 +4771,12 @@
}
},
"node_modules/heap-typed": {
"version": "1.38.7",
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.38.7.tgz",
"integrity": "sha512-9ASCFfeKkN8iPFqgRUskah+a3RAxG4f+sbTVE8xRv4xwHpPLfwGo7gbJLkiJ5fkV+0PRN/ZXUkQPT5cI0XcbZQ==",
"version": "1.38.8",
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.38.8.tgz",
"integrity": "sha512-jckb42KnKLidVpfbAChCkrYKFU1rVvTXiH/2G6ABsipzhOIvpM5ItIiR20hT2QP6wkUsEg/CG/K7h3HDQIsXXg==",
"dev": true,
"dependencies": {
"data-structure-typed": "^1.38.7"
"data-structure-typed": "^1.38.8"
}
},
"node_modules/html-escaper": {

View file

@ -1,6 +1,6 @@
{
"name": "data-structure-typed",
"version": "1.38.8",
"version": "1.38.9",
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
@ -61,17 +61,17 @@
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"auto-changelog": "^2.4.0",
"avl-tree-typed": "^1.38.7",
"avl-tree-typed": "^1.38.8",
"benchmark": "^2.1.4",
"binary-tree-typed": "^1.38.7",
"bst-typed": "^1.38.7",
"binary-tree-typed": "^1.38.8",
"bst-typed": "^1.38.8",
"dependency-cruiser": "^14.1.0",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"heap-typed": "^1.38.7",
"heap-typed": "^1.38.8",
"istanbul-badges-readme": "^1.8.5",
"jest": "^29.7.0",
"prettier": "^3.0.3",

View file

@ -130,7 +130,7 @@ export abstract class AbstractGraph<
*/
abstract createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): E;
abstract removeEdge(edge: E): E | null;
abstract deleteEdge(edge: E): E | null;
abstract getEdge(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null;
@ -179,12 +179,12 @@ export abstract class AbstractGraph<
}
/**
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
* @param {V | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`V`) or a vertex ID
* (`VertexKey`).
* @returns The method is returning a boolean value.
*/
removeVertex(vertexOrKey: V | VertexKey): boolean {
deleteVertex(vertexOrKey: V | VertexKey): boolean {
const vertexKey = this._getVertexKey(vertexOrKey);
return this._vertices.delete(vertexKey);
}
@ -199,7 +199,7 @@ export abstract class AbstractGraph<
removeAllVertices(vertices: V[] | VertexKey[]): boolean {
const removed: boolean[] = [];
for (const v of vertices) {
removed.push(this.removeVertex(v));
removed.push(this.deleteVertex(v));
}
return removed.length > 0;
}

View file

@ -153,7 +153,7 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
* @param {V | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
* @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
*/
removeEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
deleteEdgeSrcToDest(srcOrKey: V | VertexKey, destOrKey: V | VertexKey): E | null {
const src: V | null = this._getVertex(srcOrKey);
const dest: V | null = this._getVertex(destOrKey);
let removed: E | null = null;
@ -177,9 +177,9 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
* @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
* and `dest`, which represent the source and destination vertices of the edge, respectively.
* @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
* @returns The method `deleteEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
*/
removeEdge(edge: E): E | null {
deleteEdge(edge: E): E | null {
let removed: E | null = null;
const src = this._getVertex(edge.src);
const dest = this._getVertex(edge.dest);
@ -206,12 +206,12 @@ export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E ext
* the second vertex in the edge that needs to be removed.
* @returns an array of removed edges (E[]).
*/
removeEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
deleteEdgesBetween(v1: VertexKey | V, v2: VertexKey | V): E[] {
const removed: E[] = [];
if (v1 && v2) {
const v1ToV2 = this.removeEdgeSrcToDest(v1, v2);
const v2ToV1 = this.removeEdgeSrcToDest(v2, v1);
const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
v1ToV2 && removed.push(v1ToV2);
v2ToV1 && removed.push(v2ToV1);

View file

@ -127,7 +127,7 @@ export class UndirectedGraph<
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
* @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
*/
removeEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null {
deleteEdgeBetween(v1: V | VertexKey, v2: V | VertexKey): E | null {
const vertex1: V | null = this._getVertex(v1);
const vertex2: V | null = this._getVertex(v2);
@ -148,12 +148,12 @@ export class UndirectedGraph<
}
/**
* The removeEdge function removes an edge between two vertices in a graph.
* The deleteEdge function removes an edge between two vertices in a graph.
* @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
* @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
*/
removeEdge(edge: E): E | null {
return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
deleteEdge(edge: E): E | null {
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
}
/**

View file

@ -40,7 +40,7 @@ describe('DirectedGraph Operation Test', () => {
graph.addVertex(vertex2);
graph.addEdge(edge);
expect(graph.removeEdge(edge)).toBe(edge);
expect(graph.deleteEdge(edge)).toBe(edge);
expect(graph.hasEdge('A', 'B')).toBe(false);
});
@ -164,7 +164,7 @@ describe('Inherit from DirectedGraph and perform operations', () => {
myGraph.addVertex(2, 'data2');
myGraph.addEdge(1, 2, 10, 'edge-data1-2');
const removedEdge = myGraph.removeEdgeSrcToDest(1, 2);
const removedEdge = myGraph.deleteEdgeSrcToDest(1, 2);
const edgeAfterRemoval = myGraph.getEdge(1, 2);
expect(removedEdge).toBeInstanceOf(MyEdge);
@ -233,7 +233,7 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
expect(myGraph.getEdge(2, 1)).toBeTruthy();
expect(myGraph.getEdge(1, '100')).toBeFalsy();
myGraph.removeEdgeSrcToDest(1, 2);
myGraph.deleteEdgeSrcToDest(1, 2);
expect(myGraph.getEdge(1, 2)).toBeFalsy();
myGraph.addEdge(3, 1, 3, 'edge-data-3-1');

View file

@ -20,7 +20,7 @@ describe('Overall Graph Operation Test', () => {
expect(graph.hasEdge('A', 'B')).toBe(true); // true
expect(graph.hasEdge('B', 'A')).toBe(false); // false
graph.removeEdgeSrcToDest('A', 'B');
graph.deleteEdgeSrcToDest('A', 'B');
graph.hasEdge('A', 'B'); // false
expect(graph.hasEdge('A', 'B')).toBe(false); // false
@ -38,7 +38,7 @@ describe('Overall Graph Operation Test', () => {
graph.addVertex('B');
graph.addVertex('C');
graph.addVertex('D');
graph.removeVertex('C');
graph.deleteVertex('C');
graph.addEdge('A', 'B');
graph.addEdge('B', 'D');

View file

@ -40,7 +40,7 @@ describe('UndirectedGraph Operation Test', () => {
graph.addVertex(vertex2);
graph.addEdge(edge);
expect(graph.removeEdge(edge)).toBe(edge);
expect(graph.deleteEdge(edge)).toBe(edge);
expect(graph.hasEdge('A', 'B')).toBe(false);
});
@ -49,7 +49,7 @@ describe('UndirectedGraph Operation Test', () => {
graph.addVertex('B');
graph.addVertex('C');
graph.addVertex('D');
graph.removeVertex('C');
graph.deleteVertex('C');
graph.addEdge('A', 'B');
graph.addEdge('B', 'D');