diff --git a/README.md b/README.md index b4ed277..74a8f21 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,10 @@ yarn add data-structure-typed npm install data-structure-typed ``` +## api docs + +[api docs](https://data-structure-typed-docs.vercel.app/) + ## data structures Meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures: diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index f7dbcc0..21bac9e 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -11,7 +11,7 @@ export class AbstractVertex { this._id = id; } - private _id: VertexId; + protected _id: VertexId; public get id(): VertexId { return this._id; diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index 28516ea..36acc65 100644 --- a/src/data-structures/graph/directed-graph.ts +++ b/src/data-structures/graph/directed-graph.ts @@ -158,7 +158,7 @@ export class DirectedGraph ext * 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. */ - removeEdge(edge: E): E | null { + removeEdge(edge: E ): E | null { let removed: E | null = null; const src = this.getVertex(edge.src); const dest = this.getVertex(edge.dest); @@ -304,7 +304,7 @@ export class DirectedGraph ext * @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 in the graph, it returns `null`. */ - topologicalSort(): (V | VertexId)[] | null { + topologicalSort(): V[] | null { // vector> g; // vector color; // int last; @@ -336,14 +336,14 @@ export class DirectedGraph ext // } // 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 | VertexId)[] = []; + const sorted: (V)[] = []; let hasCycle = false; - const dfs = (cur: V | VertexId) => { + const dfs = (cur: V) => { statusMap.set(cur, 1); const children = this.getDestinations(cur); for (const child of children) { diff --git a/tests/data-structures/binary-tree/bst.test.ts b/tests/unit/data-structures/binary-tree/bst.test.ts similarity index 99% rename from tests/data-structures/binary-tree/bst.test.ts rename to tests/unit/data-structures/binary-tree/bst.test.ts index bdd7929..e6a5e7e 100644 --- a/tests/data-structures/binary-tree/bst.test.ts +++ b/tests/unit/data-structures/binary-tree/bst.test.ts @@ -1,4 +1,4 @@ -import {BST, BSTNode} from '../../../src'; +import {BST, BSTNode} from '../../../../src'; describe('bst-case6', () => { it('should perform various operations on a Binary Search Tree', () => { diff --git a/tests/unit/data-structures/graph/abstract-graph.ts b/tests/unit/data-structures/graph/abstract-graph.ts new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/data-structures/graph/directed-graph.test.ts b/tests/unit/data-structures/graph/directed-graph.test.ts new file mode 100644 index 0000000..c5c1ef2 --- /dev/null +++ b/tests/unit/data-structures/graph/directed-graph.test.ts @@ -0,0 +1,71 @@ +import { DirectedGraph, DirectedVertex, DirectedEdge } from '../../../../src'; + +describe('DirectedGraph', () => { + let graph: DirectedGraph; + + beforeEach(() => { + graph = new DirectedGraph(); + }); + + it('should add vertices', () => { + const vertex1 = new DirectedVertex('A'); + const vertex2 = new DirectedVertex('B'); + + graph.addVertex(vertex1); + graph.addVertex(vertex2); + + expect(graph.containsVertex(vertex1)).toBe(true); + expect(graph.containsVertex(vertex2)).toBe(true); + }); + + it('should add edges', () => { + const vertex1 = new DirectedVertex('A'); + const vertex2 = new DirectedVertex('B'); + const edge = new DirectedEdge('A', 'B'); + + graph.addVertex(vertex1); + graph.addVertex(vertex2); + graph.addEdge(edge); + + expect(graph.containsEdge('A', 'B')).toBe(true); + expect(graph.containsEdge('B', 'A')).toBe(false); + }); + + it('should remove edges', () => { + const vertex1 = new DirectedVertex('A'); + const vertex2 = new DirectedVertex('B'); + const edge = new DirectedEdge('A', 'B'); + + graph.addVertex(vertex1); + graph.addVertex(vertex2); + graph.addEdge(edge); + + expect(graph.removeEdge(edge)).toBe(edge); + expect(graph.containsEdge('A', 'B')).toBe(false); + }); + + // Add more test cases for other methods... + + it('should perform topological sort', () => { + // Create a graph with vertices and edges + const vertexA = new DirectedVertex('A'); + const vertexB = new DirectedVertex('B'); + const vertexC = new DirectedVertex('C'); + const edgeAB = new DirectedEdge('A', 'B'); + const edgeBC = new DirectedEdge('B', 'C'); + + graph.addVertex(vertexA); + graph.addVertex(vertexB); + graph.addVertex(vertexC); + graph.addEdge(edgeAB); + graph.addEdge(edgeBC); + + // Perform topological sort + const topologicalOrder = graph.topologicalSort(); + + if (topologicalOrder) expect(topologicalOrder.map(v => v.id)).toEqual(['A', 'B', 'C']); + + }); + + // Add more test cases for other methods... +}); diff --git a/tests/unit/data-structures/graph/index.ts b/tests/unit/data-structures/graph/index.ts new file mode 100644 index 0000000..19f7a4b --- /dev/null +++ b/tests/unit/data-structures/graph/index.ts @@ -0,0 +1,3 @@ +export * from './abstract-graph'; +export * from './directed-graph.test'; +export * from './undirected-graph'; diff --git a/tests/unit/data-structures/graph/undirected-graph.ts b/tests/unit/data-structures/graph/undirected-graph.ts new file mode 100644 index 0000000..e69de29