diff --git a/README.md b/README.md
index c69723f8..119337bf 100644
--- a/README.md
+++ b/README.md
@@ -728,40 +728,40 @@ optimal approach to data structure design.
[//]: # (No deletion!!! Start of Replace Section)
avl-tree
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 45.48 | 21.99 | 0.04 |
10,000 add & delete randomly | 132.35 | 7.56 | 0.10 |
10,000 addMany | 79.22 | 12.62 | 0.07 |
10,000 get | 93.13 | 10.74 | 0.06 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 32.91 | 30.38 | 6.85e-4 |
10,000 add & delete randomly | 73.84 | 13.54 | 0.00 |
10,000 addMany | 43.79 | 22.84 | 0.00 |
10,000 get | 29.07 | 34.40 | 7.13e-4 |
binary-tree
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 add randomly | 23.89 | 41.85 | 0.01 |
1,000 add & delete randomly | 22.84 | 43.78 | 0.01 |
1,000 addMany | 10.61 | 94.22 | 4.91e-4 |
1,000 get | 18.56 | 53.87 | 8.86e-4 |
1,000 dfs | 158.90 | 6.29 | 0.00 |
1,000 bfs | 58.59 | 17.07 | 0.00 |
1,000 morris | 269.91 | 3.70 | 0.01 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 add randomly | 12.65 | 79.06 | 2.71e-4 |
1,000 add & delete randomly | 16.43 | 60.86 | 0.00 |
1,000 addMany | 10.25 | 97.59 | 1.46e-4 |
1,000 get | 18.02 | 55.48 | 1.61e-4 |
1,000 dfs | 156.29 | 6.40 | 0.00 |
1,000 bfs | 56.06 | 17.84 | 5.29e-4 |
1,000 morris | 260.47 | 3.84 | 0.00 |
bst
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 31.66 | 31.59 | 8.90e-4 |
10,000 add & delete randomly | 73.97 | 13.52 | 0.00 |
10,000 addMany | 31.66 | 31.58 | 0.00 |
10,000 get | 29.75 | 33.61 | 5.49e-4 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 31.67 | 31.58 | 9.08e-4 |
10,000 add & delete randomly | 73.30 | 13.64 | 0.01 |
10,000 addMany | 30.35 | 32.95 | 0.00 |
10,000 get | 29.49 | 33.91 | 7.80e-4 |
rb-tree
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add randomly | 87.24 | 11.46 | 0.00 |
100,000 add & delete randomly | 218.78 | 4.57 | 0.01 |
100,000 getNode | 91.39 | 10.94 | 0.00 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add randomly | 90.85 | 11.01 | 0.01 |
100,000 add & delete randomly | 238.18 | 4.20 | 0.07 |
100,000 getNode | 111.04 | 9.01 | 0.03 |
directed-graph
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 addVertex | 0.10 | 9631.84 | 5.21e-6 |
1,000 addEdge | 6.25 | 160.06 | 3.97e-4 |
1,000 getVertex | 0.05 | 2.13e+4 | 1.05e-6 |
1,000 getEdge | 23.83 | 41.97 | 0.00 |
tarjan | 217.69 | 4.59 | 0.01 |
tarjan all | 226.43 | 4.42 | 0.01 |
topologicalSort | 180.71 | 5.53 | 0.01 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 addVertex | 0.10 | 9609.82 | 5.99e-6 |
1,000 addEdge | 6.43 | 155.61 | 4.69e-4 |
1,000 getVertex | 0.05 | 2.10e+4 | 2.90e-6 |
1,000 getEdge | 23.98 | 41.70 | 0.01 |
tarjan | 225.87 | 4.43 | 0.02 |
tarjan all | 229.79 | 4.35 | 0.03 |
topologicalSort | 192.07 | 5.21 | 0.03 |
heap
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add & pop | 4.69 | 213.13 | 1.63e-4 |
10,000 fib add & pop | 367.49 | 2.72 | 0.01 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add & pop | 4.67 | 214.00 | 7.34e-5 |
10,000 fib add & pop | 370.25 | 2.70 | 0.00 |
doubly-linked-list
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 unshift | 221.10 | 4.52 | 0.06 |
1,000,000 unshift & shift | 179.99 | 5.56 | 0.03 |
1,000,000 insertBefore | 331.29 | 3.02 | 0.06 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 unshift | 208.21 | 4.80 | 0.03 |
1,000,000 unshift & shift | 172.47 | 5.80 | 0.03 |
1,000,000 insertBefore | 319.07 | 3.13 | 0.07 |
singly-linked-list
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 push & pop | 216.59 | 4.62 | 0.01 |
10,000 insertBefore | 255.08 | 3.92 | 0.01 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 push & pop | 222.14 | 4.50 | 0.01 |
10,000 insertBefore | 246.45 | 4.06 | 0.01 |
max-priority-queue
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 refill & poll | 12.00 | 83.34 | 6.62e-4 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 refill & poll | 11.61 | 86.10 | 2.55e-4 |
deque
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 222.01 | 4.50 | 0.05 |
1,000,000 shift | 25.35 | 39.45 | 0.00 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 219.76 | 4.55 | 0.05 |
1,000,000 shift | 25.57 | 39.10 | 0.00 |
queue
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 45.31 | 22.07 | 0.01 |
1,000,000 push & shift | 83.92 | 11.92 | 0.01 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 46.20 | 21.64 | 0.01 |
1,000,000 push & shift | 93.60 | 10.68 | 0.01 |
trie
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 push | 59.08 | 16.92 | 0.01 |
100,000 getWords | 95.74 | 10.44 | 0.01 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 push | 57.94 | 17.26 | 0.01 |
100,000 getWords | 118.45 | 8.44 | 0.03 |
[//]: # (No deletion!!! End of Replace Section)
\ No newline at end of file
diff --git a/package.json b/package.json
index 5f3931a3..b35f5fd3 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "data-structure-typed",
- "version": "1.42.8",
+ "version": "1.42.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/src/index.js",
"module": "dist/mjs/src/index.js",
diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts
index c72272b5..26b16fa0 100644
--- a/src/data-structures/graph/abstract-graph.ts
+++ b/src/data-structures/graph/abstract-graph.ts
@@ -105,6 +105,14 @@ export abstract class AbstractGraph<
abstract getEndsOfEdge(edge: EO): [VO, VO] | null;
/**
+ * Time Complexity: O(1) - Constant time for Map lookup.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ */
+
+ /**
+ * Time Complexity: O(1) - Constant time for Map lookup.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ *
* The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
* the `_vertices` map.
@@ -116,6 +124,14 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(1) - Constant time for Map lookup.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ */
+
+ /**
+ * Time Complexity: O(1) - Constant time for Map lookup.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ *
* The function checks if a vertex exists in a graph.
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
* (`VertexKey`).
@@ -129,6 +145,11 @@ export abstract class AbstractGraph<
addVertex(key: VertexKey, value?: V): boolean;
+ /**
+ * Time Complexity: O(1) - Constant time for Map operations.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ */
+
addVertex(keyOrVertex: VertexKey | VO, value?: V): boolean {
if (keyOrVertex instanceof AbstractVertex) {
return this._addVertexOnly(keyOrVertex);
@@ -139,6 +160,14 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(1) - Constant time for Map operations.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ */
+
+ /**
+ * Time Complexity: O(1) - Constant time for Map operations.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ *
* The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
* (`VertexKey`).
@@ -150,6 +179,14 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(K), where K is the number of vertices to be removed.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ */
+
+ /**
+ * Time Complexity: O(K), where K is the number of vertices to be removed.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ *
* The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
* @param {VO[] | VertexKey[]} vertices - The `vertices` parameter can be either an array of vertices (`VO[]`) or an array
* of vertex IDs (`VertexKey[]`).
@@ -165,6 +202,14 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(1) - Depends on the implementation in the concrete class.
+ * Space Complexity: O(1) - Depends on the implementation in the concrete class.
+ */
+
+ /**
+ * Time Complexity: O(1) - Depends on the implementation in the concrete class.
+ * Space Complexity: O(1) - Depends on the implementation in the concrete class.
+ *
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
* @param {VertexKey | VO} v1 - The parameter v1 can be either a VertexKey or a VO. A VertexKey represents the unique
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
@@ -181,6 +226,11 @@ export abstract class AbstractGraph<
addEdge(src: VO | VertexKey, dest: VO | VertexKey, weight?: number, value?: E): boolean;
+ /**
+ * Time Complexity: O(1) - Depends on the implementation in the concrete class.
+ * Space Complexity: O(1) - Depends on the implementation in the concrete class.
+ */
+
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, value?: E): boolean {
if (srcOrEdge instanceof AbstractEdge) {
return this._addEdgeOnly(srcOrEdge);
@@ -198,6 +248,14 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(1) - Constant time for Map and Edge operations.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ */
+
+ /**
+ * Time Complexity: O(1) - Constant time for Map and Edge operations.
+ * Space Complexity: O(1) - Constant space, as it creates only a few variables.
+ *
* The function sets the weight of an edge between two vertices in a graph.
* @param {VertexKey | VO} srcOrKey - The `srcOrKey` parameter can be either a `VertexKey` or a `VO` object. It represents
* the source vertex of the edge.
@@ -219,6 +277,14 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
+ * Space Complexity: O(P) - Linear space, where P is the number of paths found.
+ */
+
+ /**
+ * Time Complexity: O(P), where P is the number of paths found (in the worst case, exploring all paths).
+ * Space Complexity: O(P) - Linear space, where P is the number of paths found.
+ *
* The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
* It is the starting vertex for finding paths.
@@ -258,6 +324,14 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(L), where L is the length of the path.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(L), where L is the length of the path.
+ * Space Complexity: O(1) - Constant space.
+ *
* The function calculates the sum of weights along a given path.
* @param {VO[]} path - An array of vertices (VO) representing a path in a graph.
* @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
@@ -271,6 +345,14 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
+ */
+
+ /**
+ * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
+ *
* The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
* weights or using a breadth-first search algorithm.
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex or its ID.
@@ -330,6 +412,14 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
+ */
+
+ /**
+ * Time Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm or DFS).
+ *
* The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
* using a breadth-first search algorithm.
* @param {VO | VertexKey} v1 - The parameter `v1` represents the starting vertex of the path. It can be either a vertex
@@ -398,11 +488,18 @@ export abstract class AbstractGraph<
}
/**
- * Dijkstra algorithm time: O(VE) space: O(VO + EO)
+ * Dijkstra algorithm time: O(VE) space: O(VO + EO)
* /
/**
- * Dijkstra algorithm time: O(VE) space: O(VO + EO)
+ * Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
+ */
+
+ /**
+ * Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
+ * Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
+ *
* The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
* a graph without using a heap data structure.
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
@@ -533,7 +630,7 @@ export abstract class AbstractGraph<
}
/**
- * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
+ * Dijkstra algorithm time: O(logVE) space: O(VO + EO)
*
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
@@ -542,6 +639,14 @@ export abstract class AbstractGraph<
* /
/**
+ * Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
+ * Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
+ */
+
+ /**
+ * Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
+ * Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
+ *
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
@@ -670,12 +775,15 @@ export abstract class AbstractGraph<
}
/**
- * BellmanFord time:O(VE) space:O(VO)
+ * Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
+ * Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
* one to rest pairs
* /
/**
- * BellmanFord time:O(VE) space:O(VO)
+ * Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
+ * Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
+ *
* one to rest pairs
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
@@ -795,13 +903,18 @@ export abstract class AbstractGraph<
*/
/**
- * Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
+ * Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
+ * Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
+ * Not support graph with negative weight cycle
* all pairs
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
* /
/**
- * Floyd algorithm time: O(VO^3) space: O(VO^2), not support graph with negative weight cycle
+ * Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
+ * Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
+ *
+ * Not support graph with negative weight cycle
* all pairs
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
@@ -847,6 +960,8 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
+ * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
* Tarjan can find cycles in directed or undirected graph
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
@@ -855,6 +970,9 @@ export abstract class AbstractGraph<
* /
/**
+ * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
+ * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
+ *
* Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
* Tarjan can find cycles in directed or undirected graph
* Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
@@ -983,6 +1101,14 @@ export abstract class AbstractGraph<
}
/**
+ * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
+ * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
+ */
+
+ /**
+ * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
+ * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
+ *
* The function returns a map that associates each vertex object with its corresponding depth-first
* number.
* @returns A Map object with keys of type VO and values of type number.
diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts
index 67b16cb2..ff9f5246 100644
--- a/src/data-structures/graph/directed-graph.ts
+++ b/src/data-structures/graph/directed-graph.ts
@@ -111,6 +111,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(|V|) where |V| is the number of vertices
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(|V|) where |V| is the number of vertices
+ * Space Complexity: O(1)
+ *
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
* @param {VO | VertexKey | null} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
* @param {VO | VertexKey | null} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
@@ -137,6 +145,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(1)
+ *
* The function removes an edge between two vertices in a graph and returns the removed edge.
* @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
* @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
@@ -163,6 +179,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(1)
+ *
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
* @param {EO} 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.
@@ -188,6 +212,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(1)
+ *
* The function removes edges between two vertices and returns the removed edges.
* @param {VertexKey | VO} v1 - The parameter `v1` can be either a `VertexKey` or a `VO`. A `VertexKey` represents the
* unique identifier of a vertex in a graph, while `VO` represents the actual vertex object.
@@ -210,6 +242,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
* (`VertexKey`).
@@ -224,6 +264,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can accept either a vertex object (`VO`) or a vertex ID
* (`VertexKey`).
@@ -238,6 +286,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
* @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
@@ -247,6 +303,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function "inDegreeOf" returns the number of incoming edges for a given vertex.
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
* @returns The number of incoming edges of the specified vertex or vertex ID.
@@ -256,6 +320,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
* @returns The number of outgoing edges from the specified vertex or vertex ID.
@@ -265,6 +337,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
* @returns The function `edgesOf` returns an array of edges.
@@ -274,6 +354,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
* @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
* @returns either a vertex object (VO) or null.
@@ -283,6 +371,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function "getEdgeDest" returns the destination vertex of an edge.
* @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
* @returns either a vertex object of type VO or null.
@@ -292,6 +388,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(1)
+ *
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
* @param {VO | VertexKey | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `null`.
@@ -313,6 +417,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(|V| + |E|) where |V| is the number of vertices and |E| is the number of edges
+ * Space Complexity: O(|V|)
+ */
+
+ /**
+ * Time Complexity: O(|V| + |E|) where |V| is the number of vertices and |E| is the number of edges
+ * Space Complexity: O(|V|)
+ *
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
* in the sorted order, or null if the graph contains a cycle.
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
@@ -359,6 +471,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(|E|)
+ */
+
+ /**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(|E|)
+ *
* The `edgeSet` function returns an array of all the edges in the graph.
* @returns The `edgeSet()` method returns an array of edges (`EO[]`).
*/
@@ -371,6 +491,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(|E|) where |E| is the number of edges
+ * Space Complexity: O(1)
+ *
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
* (`VertexKey`).
@@ -393,6 +521,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
* otherwise it returns null.
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
@@ -413,6 +549,14 @@ export class DirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertices exist.
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
* needs to be added to the graph.
@@ -447,4 +591,4 @@ export class DirectedGraph<
return false;
}
}
-}
+}
\ No newline at end of file
diff --git a/src/data-structures/graph/undirected-graph.ts b/src/data-structures/graph/undirected-graph.ts
index 2e534c91..21180f61 100644
--- a/src/data-structures/graph/undirected-graph.ts
+++ b/src/data-structures/graph/undirected-graph.ts
@@ -93,6 +93,14 @@ export class UndirectedGraph<
}
/**
+ * Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
+ * Space Complexity: O(1)
+ *
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
* @param {VO | VertexKey | null} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
* object), `null`, or `VertexKey` (a string or number representing the ID of a vertex).
@@ -116,6 +124,14 @@ export class UndirectedGraph<
}
/**
+ * Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
+ * Space Complexity: O(1)
+ *
* The function removes an edge between two vertices in a graph and returns the removed edge.
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
@@ -143,6 +159,14 @@ export class UndirectedGraph<
}
/**
+ * Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
+ * Space Complexity: O(1)
+ *
* The deleteEdge function removes an edge between two vertices in a graph.
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
* @returns The method is returning either the removed edge (of type EO) or null if the edge was not found.
@@ -152,6 +176,14 @@ export class UndirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
* vertex.
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
@@ -168,6 +200,14 @@ export class UndirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function returns the edges of a given vertex or vertex ID.
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
@@ -183,6 +223,14 @@ export class UndirectedGraph<
}
/**
+ * Time Complexity: O(|V| + |E|), where |V| is the number of vertices and |E| is the number of edges.
+ * Space Complexity: O(|E|)
+ */
+
+ /**
+ * Time Complexity: O(|V| + |E|), where |V| is the number of vertices and |E| is the number of edges.
+ * Space Complexity: O(|E|)
+ *
* The function "edgeSet" returns an array of unique edges from a set of edges.
* @returns The method `edgeSet()` returns an array of type `EO[]`.
*/
@@ -197,6 +245,14 @@ export class UndirectedGraph<
}
/**
+ * Time Complexity: O(|V| + |E|), where |V| is the number of vertices and |E| is the number of edges.
+ * Space Complexity: O(|E|)
+ */
+
+ /**
+ * Time Complexity: O(|V| + |E|), where |V| is the number of vertices and |E| is the number of edges.
+ * Space Complexity: O(|E|)
+ *
* The function "getNeighbors" returns an array of neighboring vertices for a given vertex or vertex ID.
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
* (`VertexKey`).
@@ -218,6 +274,14 @@ export class UndirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
* it returns null.
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
@@ -238,6 +302,14 @@ export class UndirectedGraph<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function adds an edge to the graph by updating the adjacency list with the vertices of the edge.
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
* @returns a boolean value.
diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts
index 60d45336..88667a1c 100644
--- a/src/data-structures/heap/heap.ts
+++ b/src/data-structures/heap/heap.ts
@@ -53,6 +53,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ *
* Insert an element into the heap and maintain the heap properties.
* @param element - The element to be inserted.
*/
@@ -61,6 +69,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ *
* Insert an element into the heap and maintain the heap properties.
* @param element - The element to be inserted.
*/
@@ -71,6 +87,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ *
* Remove and return the top element (smallest or largest element) from the heap.
* @returns The top element or undefined if the heap is empty.
*/
@@ -89,6 +113,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ *
* Remove and return the top element (smallest or largest element) from the heap.
* @returns The top element or undefined if the heap is empty.
*/
@@ -123,6 +155,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the nodes array.
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the nodes array.
+ * Space Complexity: O(n)
+ *
* Clear and add nodes of the heap
* @param nodes
*/
@@ -132,6 +172,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ *
* Use a comparison function to check whether a binary heap contains a specific element.
* @param element - the element to check.
* @returns Returns true if the specified element is contained; otherwise, returns false.
@@ -141,6 +189,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(n), where n is the number of nodes in the heap.
+ * Space Complexity: O(h), where h is the height of the heap.
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of nodes in the heap.
+ * Space Complexity: O(h), where h is the height of the heap.
+ *
* Depth-first search (DFS) method, different traversal orders can be selected。
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
* @returns An array containing elements traversed in the specified order.
@@ -173,6 +229,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(n)
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(n)
+ *
* Convert the heap to an array.
* @returns An array containing the elements of the heap.
*/
@@ -180,11 +244,23 @@ export class Heap {
return [...this.nodes];
}
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
getNodes(): E[] {
return this.nodes;
}
/**
+ * Time Complexity: O(n)
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(n)
+ *
* Clone the heap, creating a new heap with the same elements.
* @returns A new Heap instance containing the same elements.
*/
@@ -195,6 +271,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(n log n)
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n log n)
+ * Space Complexity: O(n)
+ *
* Sort the elements in the heap and return them as an array.
* @returns An array containing the elements sorted in ascending order.
*/
@@ -209,6 +293,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
+ *
* Float operation to maintain heap properties after adding an element.
* @param index - The index of the newly added element.
*/
@@ -228,6 +320,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
+ *
* Sinking operation to maintain heap properties after removing the top element.
* @param index - The index from which to start sinking.
*/
@@ -253,6 +353,14 @@ export class Heap {
}
/**
+ * Time Complexity: O(n)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(1)
+ *
* Fix the entire heap to maintain heap properties.
*/
protected fix() {
@@ -321,7 +429,14 @@ export class FibonacciHeap {
}
/**
- * O(1) time operation.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* Insert an element into the heap and maintain the heap properties.
* @param element
* @returns {FibonacciHeap} FibonacciHeap - The heap itself.
@@ -330,8 +445,16 @@ export class FibonacciHeap {
return this.push(element);
}
+
/**
- * O(1) time operation.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* Insert an element into the heap and maintain the heap properties.
* @param element
* @returns {FibonacciHeap} FibonacciHeap - The heap itself.
@@ -350,8 +473,16 @@ export class FibonacciHeap {
return this;
}
+
/**
- * O(1) time operation.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* Peek at the top element of the heap without removing it.
* @returns The top element or undefined if the heap is empty.
* @protected
@@ -361,7 +492,14 @@ export class FibonacciHeap {
}
/**
- * O(1) time operation.
+ * Time Complexity: O(n), where n is the number of nodes in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of nodes in the linked list.
+ * Space Complexity: O(1)
+ *
* Get the size (number of elements) of the heap.
* @param {FibonacciHeapNode} head - The head of the linked list.
* @protected
@@ -388,8 +526,9 @@ export class FibonacciHeap {
}
/**
- * O(log n) time operation.
- * Remove and return the top element (smallest or largest element) from the heap.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* @param parent
* @param node
*/
@@ -405,7 +544,14 @@ export class FibonacciHeap {
}
/**
- * O(log n) time operation.
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ *
* Remove and return the top element (smallest or largest element) from the heap.
* @returns The top element or undefined if the heap is empty.
*/
@@ -414,7 +560,14 @@ export class FibonacciHeap {
}
/**
- * O(log n) time operation.
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(1)
+ *
* Remove and return the top element (smallest or largest element) from the heap.
* @returns The top element or undefined if the heap is empty.
*/
@@ -446,7 +599,14 @@ export class FibonacciHeap {
}
/**
- * O(log n) time operation.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
* @param heapToMerge
*/
@@ -504,6 +664,14 @@ export class FibonacciHeap {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* Merge the given node with the root list.
* @param node - The node to be merged.
*/
@@ -519,7 +687,14 @@ export class FibonacciHeap {
}
/**
- * O(log n) time operation.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *.
* Remove and return the top element (smallest or largest element) from the heap.
* @param node - The node to be removed.
* @protected
@@ -531,7 +706,14 @@ export class FibonacciHeap {
}
/**
- * O(log n) time operation.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* Remove and return the top element (smallest or largest element) from the heap.
* @param y
* @param x
@@ -547,7 +729,14 @@ export class FibonacciHeap {
}
/**
- * O(log n) time operation.
+ * Time Complexity: O(n log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n log n), where n is the number of nodes in the heap.
+ * Space Complexity: O(n)
+ *
* Remove and return the top element (smallest or largest element) from the heap.
* @protected
*/
diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts
index 119ae71a..d8771379 100644
--- a/src/data-structures/linked-list/doubly-linked-list.ts
+++ b/src/data-structures/linked-list/doubly-linked-list.ts
@@ -55,6 +55,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the length of the input array.
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the length of the input array.
+ * Space Complexity: O(n)
+ *
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
* given array.
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
@@ -69,6 +77,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The push function adds a new node with the given value to the end of the doubly linked list.
* @param {E} value - The value to be added to the linked list.
*/
@@ -86,6 +102,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The addLast function adds a new node with the given value to the end of the doubly linked list.
* @param {E} value - The value to be added to the linked list.
*/
@@ -94,6 +118,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
* list is empty, it returns null.
@@ -113,6 +145,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
* list is empty, it returns null.
@@ -122,6 +162,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
* list.
@@ -141,6 +189,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
* list.
@@ -150,6 +206,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
* doubly linked list.
@@ -168,6 +232,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
* doubly linked list.
@@ -177,6 +249,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
*/
@@ -185,6 +265,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
*/
@@ -193,6 +281,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
* @param {number} index - The index parameter is a number that represents the position of the element we want to
* retrieve from the list.
@@ -209,6 +305,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
* range.
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
@@ -226,6 +330,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
* node if found, otherwise it returns null.
* @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
@@ -246,6 +358,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `insert` function inserts a value at a specified index in a doubly linked list.
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
* DoublyLinkedList. It is of type number.
@@ -277,6 +397,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
* @param {E | DoublyLinkedListNode} existingValueOrNode - The existing value or node in the doubly linked list
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
@@ -314,6 +442,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
* data structure. It is of type number.
@@ -335,6 +471,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
* @param {E | DoublyLinkedListNode} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
* a `DoublyLinkedListNode` object.
@@ -368,6 +512,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(n)
+ *
* The `toArray` function converts a linked list into an array.
* @returns The `toArray()` method is returning an array of type `E[]`.
*/
@@ -399,6 +551,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
@@ -417,6 +577,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The function returns the index of the first occurrence of a given value in a linked list.
* @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
* that we are searching for in the linked list.
@@ -437,6 +605,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
* value that satisfies the given callback function, or null if no value satisfies the callback.
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
@@ -456,6 +632,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(n)
+ *
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
*/
@@ -470,6 +654,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `reverse` function reverses the order of the elements in a doubly linked list.
*/
reverse(): void {
@@ -483,6 +675,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
* represents the value of the current node in the linked list, and the index argument represents the index of the
@@ -499,6 +699,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(n)
+ *
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
* DoublyLinkedList with the transformed values.
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
@@ -517,6 +725,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(n)
+ *
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
* elements that satisfy the given callback function.
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
@@ -536,6 +752,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(n)
+ *
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
* single value.
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
@@ -556,6 +780,14 @@ export class DoublyLinkedList {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
* @param {E | DoublyLinkedListNode} existingValueOrNode - The existing value or node in the doubly linked list
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts
index 7c39cf64..58323ab7 100644
--- a/src/data-structures/linked-list/singly-linked-list.ts
+++ b/src/data-structures/linked-list/singly-linked-list.ts
@@ -49,6 +49,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
+ * Space Complexity: O(n) - Linear space, as it creates a new node for each element in the array.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
+ * Space Complexity: O(n) - Linear space, as it creates a new node for each element in the array.
+ *
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
* array.
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
@@ -63,6 +71,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
+ * Space Complexity: O(1) - Constant space, as it only creates a new node.
+ */
+
+ /**
+ * Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
+ * Space Complexity: O(1) - Constant space, as it only creates a new node.
+ *
* The `push` function adds a new node with the given value to the end of a singly linked list.
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
* any type (E) as specified in the generic type declaration of the class or function.
@@ -80,6 +96,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
+ * Space Complexity: O(1) - Constant space, as it only creates a new node.
+ */
+
+ /**
+ * Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
+ * Space Complexity: O(1) - Constant space, as it only creates a new node.
+ *
* The `push` function adds a new node with the given value to the end of a singly linked list.
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
* any type (E) as specified in the generic type declaration of the class or function.
@@ -89,6 +113,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
* pointers accordingly.
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
@@ -116,6 +148,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
* pointers accordingly.
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
@@ -126,6 +166,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `shift()` function removes and returns the value of the first node in a linked list.
* @returns The value of the node that is being removed from the beginning of the linked list.
*/
@@ -138,6 +186,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `popFirst()` function removes and returns the value of the first node in a linked list.
* @returns The value of the node that is being removed from the beginning of the linked list.
*/
@@ -146,6 +202,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
+ * Space Complexity: O(1) - Constant space.
+ *
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
* linked list.
@@ -163,6 +227,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
+ * Space Complexity: O(1) - Constant space.
+ *
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
* linked list.
@@ -172,6 +244,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
* @param {number} index - The index parameter is a number that represents the position of the element we want to
* retrieve from the list.
@@ -188,6 +268,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The function `getNodeAt` returns the node at a given index in a singly linked list.
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
@@ -203,6 +291,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
* data structure. It is of type number.
@@ -222,6 +318,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The delete function removes a node with a specific value from a singly linked list.
* @param {E | SinglyLinkedListNode} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
* or a `SinglyLinkedListNode` object.
@@ -263,6 +367,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `insertAt` function inserts a value at a specified index in a singly linked list.
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
* linked list. It is of type number.
@@ -309,6 +421,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
+ * Space Complexity: O(n) - Linear space, as it creates an array with the same length as the list.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
+ * Space Complexity: O(n) - Linear space, as it creates an array with the same length as the list.
+ *
* The `toArray` function converts a linked list into an array.
* @returns The `toArray()` method is returning an array of type `E[]`.
*/
@@ -323,6 +443,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `reverse` function reverses the order of the nodes in a singly linked list.
* @returns The reverse() method does not return anything. It has a return type of void.
*/
@@ -344,6 +472,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
@@ -362,6 +498,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
* @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
* @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
@@ -383,6 +527,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
* null.
* @param {E} value - The value parameter is the value that we want to search for in the linked list.
@@ -403,6 +555,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
* @param {E | SinglyLinkedListNode} existingValueOrNode - The existing value or node that you want to insert the
* new value before. It can be either the value itself or a node containing the value in the linked list.
@@ -440,6 +600,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
* @param {E | SinglyLinkedListNode} existingValueOrNode - The existing value or node in the linked list after which
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
@@ -471,6 +639,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The function counts the number of occurrences of a given value in a linked list.
* @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
* @returns The count of occurrences of the given value in the linked list.
@@ -490,6 +666,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
+ * Space Complexity: O(1) - Constant space.
+ *
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
* represents the value of the current node in the linked list, and the index argument represents the index of the
@@ -506,6 +690,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
+ * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
+ * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
+ *
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
* SinglyLinkedList with the transformed values.
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
@@ -524,6 +716,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
+ * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
+ * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
+ *
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
* elements that satisfy the given callback function.
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
@@ -543,6 +743,14 @@ export class SinglyLinkedList {
}
/**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
+ * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
+ */
+
+ /**
+ * Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
+ * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
+ *
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
* single value.
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
diff --git a/src/data-structures/linked-list/skip-linked-list.ts b/src/data-structures/linked-list/skip-linked-list.ts
index 05148630..57847125 100644
--- a/src/data-structures/linked-list/skip-linked-list.ts
+++ b/src/data-structures/linked-list/skip-linked-list.ts
@@ -58,6 +58,14 @@ export class SkipList {
}
/**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ */
+
+ /**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ *
* The add function adds a new node with a given key and value to a Skip List data structure.
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
* @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
@@ -86,6 +94,14 @@ export class SkipList {
}
/**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ */
+
+ /**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ *
* The function `get` retrieves the value associated with a given key from a skip list data structure.
* @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
* @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
@@ -108,11 +124,29 @@ export class SkipList {
return undefined;
}
+ /**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ */
+
+ /**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ */
+
has(key: K): boolean {
return this.get(key) !== undefined;
}
/**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ */
+
+ /**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ *
* The `delete` function removes a node with a specific key from a Skip List data structure.
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
* @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
@@ -148,6 +182,14 @@ export class SkipList {
}
/**
+ * Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ */
+
+ /**
+ * Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ *
* Get the value of the first element (the smallest element) in the Skip List.
* @returns The value of the first element, or undefined if the Skip List is empty.
*/
@@ -157,6 +199,14 @@ export class SkipList {
}
/**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ */
+
+ /**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ *
* Get the value of the last element (the largest element) in the Skip List.
* @returns The value of the last element, or undefined if the Skip List is empty.
*/
@@ -171,6 +221,14 @@ export class SkipList {
}
/**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ */
+
+ /**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ *
* Get the value of the first element in the Skip List that is greater than the given key.
* @param key - the given key.
* @returns The value of the first element greater than the given key, or undefined if there is no such element.
@@ -187,6 +245,14 @@ export class SkipList {
}
/**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ */
+
+ /**
+ * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
+ * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
+ *
* Get the value of the last element in the Skip List that is less than the given key.
* @param key - the given key.
* @returns The value of the last element less than the given key, or undefined if there is no such element.
@@ -208,6 +274,14 @@ export class SkipList {
}
/**
+ * Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
+ * Space Complexity: O(1) - constant space.
+ */
+
+ /**
+ * Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
+ * Space Complexity: O(1) - constant space.
+ *
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
* @returns the level, which is a number.
*/
diff --git a/src/data-structures/queue/deque.ts b/src/data-structures/queue/deque.ts
index 413af8ff..e3cb2298 100644
--- a/src/data-structures/queue/deque.ts
+++ b/src/data-structures/queue/deque.ts
@@ -50,6 +50,14 @@ export class ObjectDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The "addFirst" function adds a value to the beginning of an array-like data structure.
* @param {E} value - The `value` parameter represents the value that you want to add to the beginning of the data
* structure.
@@ -67,6 +75,14 @@ export class ObjectDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The addLast function adds a value to the end of an array-like data structure.
* @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
*/
@@ -83,6 +99,14 @@ export class ObjectDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function `popFirst()` removes and returns the first element in a data structure.
* @returns The value of the first element in the data structure.
*/
@@ -96,6 +120,14 @@ export class ObjectDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The `getFirst` function returns the first element in an array-like data structure if it exists.
* @returns The element at the first position of the `_nodes` array.
*/
@@ -104,6 +136,14 @@ export class ObjectDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The `popLast()` function removes and returns the last element in a data structure.
* @returns The value that was removed from the data structure.
*/
@@ -118,6 +158,14 @@ export class ObjectDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The `getLast()` function returns the last element in an array-like data structure.
* @returns The last element in the array "_nodes" is being returned.
*/
@@ -126,6 +174,14 @@ export class ObjectDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The get function returns the element at the specified index in an array-like data structure.
* @param {number} index - The index parameter is a number that represents the position of the element you want to
* retrieve from the array.
@@ -159,10 +215,14 @@ export class ArrayDeque {
}
/**
- * O(n) time complexity of adding at the beginning and the end
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*/
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function "addLast" adds a value to the end of an array.
* @param {E} value - The value parameter represents the value that you want to add to the end of the array.
* @returns The return value is the new length of the array after the value has been added.
@@ -172,6 +232,14 @@ export class ArrayDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
*/
@@ -180,6 +248,14 @@ export class ArrayDeque {
}
/**
+ * Time Complexity: O(n)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(1)
+ *
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
* empty.
@@ -189,10 +265,14 @@ export class ArrayDeque {
}
/**
- * O(n) time complexity of adding at the beginning and the end
+ * Time Complexity: O(n)
+ * Space Complexity: O(1)
*/
/**
+ * Time Complexity: O(n)
+ * Space Complexity: O(1)
+ *
* The function "addFirst" adds a value to the beginning of an array.
* @param {E} value - The value parameter represents the value that you want to add to the beginning of the array.
* @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
@@ -203,6 +283,14 @@ export class ArrayDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The `getFirst` function returns the first element of an array or null if the array is empty.
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
* empty, it will return `null`.
@@ -212,6 +300,14 @@ export class ArrayDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The `getLast` function returns the last element of an array or null if the array is empty.
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
*/
@@ -220,10 +316,14 @@ export class ArrayDeque {
}
/**
- * O(1) time complexity of obtaining the value
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*/
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
* @param {number} index - The index parameter is a number that represents the position of the element you want to
* retrieve from the array.
@@ -235,6 +335,14 @@ export class ArrayDeque {
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* The set function assigns a value to a specific index in an array.
* @param {number} index - The index parameter is a number that represents the position of the element in the array
* that you want to set a new value for.
@@ -247,6 +355,14 @@ export class ArrayDeque {
}
/**
+ * Time Complexity: O(n)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(1)
+ *
* The insert function adds a value at a specified index in an array.
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
@@ -261,6 +377,14 @@ export class ArrayDeque {
}
/**
+ * Time Complexity: O(n)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(1)
+ *
* The delete function removes an element from an array at a specified index.
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
* is a number that represents the index of the element to be removed.
diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts
index 9991f389..c8460740 100644
--- a/src/data-structures/queue/queue.ts
+++ b/src/data-structures/queue/queue.ts
@@ -84,6 +84,14 @@ export class Queue {
}
/**
+ * Time Complexity: O(1) - constant time as it adds an element to the end of the array.
+ * Space Complexity: O(1) - no additional space is used.
+ */
+
+ /**
+ * Time Complexity: O(1) - constant time as it adds an element to the end of the array.
+ * Space Complexity: O(1) - no additional space is used.
+ *
* The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
* @param {E} element - The `element` parameter represents the element that you want to add to the queue.
* @returns The `add` method is returning a `Queue` object.
@@ -94,6 +102,14 @@ export class Queue {
}
/**
+ * Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
+ * Space Complexity: O(1) - no additional space is used.
+ */
+
+ /**
+ * Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
+ * Space Complexity: O(1) - no additional space is used.
+ *
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
* necessary to optimize performance.
* @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
@@ -114,6 +130,14 @@ export class Queue {
}
/**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
+ * Space Complexity: O(1) - no additional space is used.
+ */
+
+ /**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
+ * Space Complexity: O(1) - no additional space is used.
+ *
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
@@ -123,6 +147,14 @@ export class Queue {
}
/**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
+ * Space Complexity: O(1) - no additional space is used.
+ */
+
+ /**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
+ * Space Complexity: O(1) - no additional space is used.
+ *
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
@@ -132,6 +164,14 @@ export class Queue {
}
/**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
+ * Space Complexity: O(1) - no additional space is used.
+ */
+
+ /**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
+ * Space Complexity: O(1) - no additional space is used.
+ *
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
* array is empty, it returns `null`.
@@ -141,6 +181,14 @@ export class Queue {
}
/**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
+ * Space Complexity: O(1) - no additional space is used.
+ */
+
+ /**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
+ * Space Complexity: O(1) - no additional space is used.
+ *
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
* array is empty, it returns `null`.
@@ -150,6 +198,14 @@ export class Queue {
}
/**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
+ * Space Complexity: O(1) - no additional space is used.
+ */
+
+ /**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
+ * Space Complexity: O(1) - no additional space is used.
+ *
* The enqueue function adds a value to the end of a queue.
* @param {E} value - The value parameter represents the value that you want to add to the queue.
*/
@@ -158,6 +214,14 @@ export class Queue {
}
/**
+ * Time Complexity: O(n) - same as shift().
+ * Space Complexity: O(1) - same as shift().
+ */
+
+ /**
+ * Time Complexity: O(n) - same as shift().
+ * Space Complexity: O(1) - same as shift().
+ *
* The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
* @returns The method is returning a value of type E or null.
*/
@@ -165,11 +229,30 @@ export class Queue {
return this.shift();
}
+ /**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
+ * Space Complexity: O(1) - no additional space is used.
+ */
+
+ /**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
+ * Space Complexity: O(1) - no additional space is used.
+ *
+ * @param index
+ */
getAt(index: number): E | undefined {
return this.nodes[index];
}
/**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
+ * Space Complexity: O(1) - no additional space is used.
+ */
+
+ /**
+ * Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
+ * Space Complexity: O(1) - no additional space is used.
+ *
* The function checks if a data structure is empty by comparing its size to zero.
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
*/
@@ -178,6 +261,14 @@ export class Queue {
}
/**
+ * Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array.
+ * Space Complexity: O(n) - where n is the number of elements in the queue.
+ */
+
+ /**
+ * Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array.
+ * Space Complexity: O(n) - where n is the number of elements in the queue.
+ *
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
* @returns An array of type E is being returned.
*/
@@ -194,6 +285,14 @@ export class Queue {
}
/**
+ * Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array.
+ * Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
+ */
+
+ /**
+ * Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array.
+ * Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
+ *
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
* @returns The `clone()` method is returning a new instance of the `Queue` class.
*/
diff --git a/src/data-structures/stack/stack.ts b/src/data-structures/stack/stack.ts
index e343f09f..20eb2f36 100644
--- a/src/data-structures/stack/stack.ts
+++ b/src/data-structures/stack/stack.ts
@@ -21,6 +21,14 @@ export class Stack {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
+ * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
+ * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
+ *
* 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`.
* @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
@@ -47,6 +55,14 @@ export class Stack {
}
/**
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
+ * Space Complexity: O(1), as it does not use any additional space.
+ */
+
+ /**
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
+ * Space Complexity: O(1), as it does not use any additional space.
+ *
* The `peek` function returns the last element of an array, or null if the array is empty.
* @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
*/
@@ -57,6 +73,14 @@ export class Stack {
}
/**
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
+ * Space Complexity: O(1), as it does not use any additional space.
+ */
+
+ /**
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
+ * Space Complexity: O(1), as it does not use any additional space.
+ *
* The push function adds an element to the stack and returns the updated stack.
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
* @returns The `push` method is returning the updated `Stack` object.
@@ -67,6 +91,14 @@ export class Stack {
}
/**
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
+ * Space Complexity: O(1), as it does not use any additional space.
+ */
+
+ /**
+ * Time Complexity: O(1), as it only involves accessing the last element of the array.
+ * Space Complexity: O(1), as it does not use any additional space.
+ *
* The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
* array is empty, it returns `null`.
@@ -78,6 +110,14 @@ export class Stack {
}
/**
+ * Time Complexity: O(n)
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(n)
+ *
* The toArray function returns a copy of the elements in an array.
* @returns An array of type E.
*/
@@ -93,6 +133,14 @@ export class Stack {
}
/**
+ * Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it.
+ * Space Complexity: O(n), as it creates a new stack.
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it.
+ * Space Complexity: O(n), as it creates a new stack.
+ *
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
*/
diff --git a/src/data-structures/trie/trie.ts b/src/data-structures/trie/trie.ts
index 25e31ca3..5de530e9 100644
--- a/src/data-structures/trie/trie.ts
+++ b/src/data-structures/trie/trie.ts
@@ -49,6 +49,14 @@ export class Trie {
}
/**
+ * Time Complexity: O(M), where M is the length of the word being added.
+ * Space Complexity: O(M) - Each character in the word adds a TrieNode.
+ */
+
+ /**
+ * Time Complexity: O(M), where M is the length of the word being added.
+ * Space Complexity: O(M) - Each character in the word adds a TrieNode.
+ *
* Add a word to the Trie structure.
* @param {string} word - The word to add.
* @returns {boolean} True if the word was successfully added.
@@ -69,6 +77,14 @@ export class Trie {
}
/**
+ * Time Complexity: O(M), where M is the length of the input word.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(M), where M is the length of the input word.
+ * Space Complexity: O(1) - Constant space.
+ *
* Check if the Trie contains a given word.
* @param {string} word - The word to check for.
* @returns {boolean} True if the word is present in the Trie.
@@ -85,6 +101,14 @@ export class Trie {
}
/**
+ * Time Complexity: O(M), where M is the length of the word being deleted.
+ * Space Complexity: O(M) - Due to the recursive DFS approach.
+ */
+
+ /**
+ * Time Complexity: O(M), where M is the length of the word being deleted.
+ * Space Complexity: O(M) - Due to the recursive DFS approach.
+ *
* Remove a word from the Trie structure.
* @param{string} word - The word to delete.
* @returns {boolean} True if the word was successfully removed.
@@ -122,6 +146,16 @@ export class Trie {
return isDeleted;
}
+ /**
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
+ * Space Complexity: O(1) - Constant space.
+ *
+ */
getHeight() {
const beginRoot = this.root;
let maxDepth = 0;
@@ -142,8 +176,15 @@ export class Trie {
return maxDepth;
}
- // --- start additional methods ---
/**
+ * Time Complexity: O(M), where M is the length of the input prefix.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(M), where M is the length of the input prefix.
+ * Space Complexity: O(1) - Constant space.
+ *
* Check if a given input string has an absolute prefix in the Trie, meaning it's not a complete word.
* @param {string} input - The input string to check.
* @returns {boolean} True if it's an absolute prefix in the Trie.
@@ -160,6 +201,14 @@ export class Trie {
}
/**
+ * Time Complexity: O(M), where M is the length of the input prefix.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(M), where M is the length of the input prefix.
+ * Space Complexity: O(1) - Constant space.
+ *
* Check if a given input string is a prefix of any existing word in the Trie, whether as an absolute prefix or a complete word.
* @param {string} input - The input string representing the prefix to check.
* @returns {boolean} True if it's a prefix in the Trie.
@@ -176,6 +225,14 @@ export class Trie {
}
/**
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
+ * Space Complexity: O(M), where M is the length of the input prefix.
+ */
+
+ /**
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
+ * Space Complexity: O(M), where M is the length of the input prefix.
+ *
* Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie.
* @param {string} input - The input string representing the common prefix to check for.
* @returns {boolean} True if it's a common prefix in the Trie.
@@ -195,6 +252,14 @@ export class Trie {
}
/**
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
+ * Space Complexity: O(M), where M is the length of the longest common prefix.
+ */
+
+ /**
+ * Time Complexity: O(N), where N is the total number of nodes in the trie.
+ * Space Complexity: O(M), where M is the length of the longest common prefix.
+ *
* Get the longest common prefix among all the words stored in the Trie.
* @returns {string} The longest common prefix found in the Trie.
*/
@@ -211,6 +276,14 @@ export class Trie {
}
/**
+ * Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words.
+ * Space Complexity: O(K * L) - The space required for the output array.
+ */
+
+ /**
+ * Time Complexity: O(K * L), where K is the number of words retrieved, and L is the average length of the words.
+ * Space Complexity: O(K * L) - The space required for the output array.
+ *
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
@@ -251,12 +324,22 @@ export class Trie {
return words;
}
+ /**
+ * Time Complexity: O(M), where M is the length of the input string.
+ * Space Complexity: O(1) - Constant space.
+ */
+
+ /**
+ * Time Complexity: O(M), where M is the length of the input string.
+ * Space Complexity: O(1) - Constant space.
+ *
+ * @param str
+ * @protected
+ */
protected _caseProcess(str: string) {
if (!this._caseSensitive) {
str = str.toLowerCase(); // Convert str to lowercase if case-insensitive
}
return str;
}
-
- // --- end additional methods ---
-}
+}
\ No newline at end of file