docs: Almost all methods of data structures have added comments on time and space complexity.

This commit is contained in:
Revone 2023-11-10 16:24:24 +08:00
parent d3a56a5c7a
commit fd416c5d6e
13 changed files with 1439 additions and 40 deletions

View file

@ -728,40 +728,40 @@ optimal approach to data structure design.
[//]: # (No deletion!!! Start of Replace Section)
<div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>avl-tree</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>45.48</td><td>21.99</td><td>0.04</td></tr><tr><td>10,000 add & delete randomly</td><td>132.35</td><td>7.56</td><td>0.10</td></tr><tr><td>10,000 addMany</td><td>79.22</td><td>12.62</td><td>0.07</td></tr><tr><td>10,000 get</td><td>93.13</td><td>10.74</td><td>0.06</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>32.91</td><td>30.38</td><td>6.85e-4</td></tr><tr><td>10,000 add & delete randomly</td><td>73.84</td><td>13.54</td><td>0.00</td></tr><tr><td>10,000 addMany</td><td>43.79</td><td>22.84</td><td>0.00</td></tr><tr><td>10,000 get</td><td>29.07</td><td>34.40</td><td>7.13e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>binary-tree</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 add randomly</td><td>23.89</td><td>41.85</td><td>0.01</td></tr><tr><td>1,000 add & delete randomly</td><td>22.84</td><td>43.78</td><td>0.01</td></tr><tr><td>1,000 addMany</td><td>10.61</td><td>94.22</td><td>4.91e-4</td></tr><tr><td>1,000 get</td><td>18.56</td><td>53.87</td><td>8.86e-4</td></tr><tr><td>1,000 dfs</td><td>158.90</td><td>6.29</td><td>0.00</td></tr><tr><td>1,000 bfs</td><td>58.59</td><td>17.07</td><td>0.00</td></tr><tr><td>1,000 morris</td><td>269.91</td><td>3.70</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 add randomly</td><td>12.65</td><td>79.06</td><td>2.71e-4</td></tr><tr><td>1,000 add & delete randomly</td><td>16.43</td><td>60.86</td><td>0.00</td></tr><tr><td>1,000 addMany</td><td>10.25</td><td>97.59</td><td>1.46e-4</td></tr><tr><td>1,000 get</td><td>18.02</td><td>55.48</td><td>1.61e-4</td></tr><tr><td>1,000 dfs</td><td>156.29</td><td>6.40</td><td>0.00</td></tr><tr><td>1,000 bfs</td><td>56.06</td><td>17.84</td><td>5.29e-4</td></tr><tr><td>1,000 morris</td><td>260.47</td><td>3.84</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>bst</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>31.66</td><td>31.59</td><td>8.90e-4</td></tr><tr><td>10,000 add & delete randomly</td><td>73.97</td><td>13.52</td><td>0.00</td></tr><tr><td>10,000 addMany</td><td>31.66</td><td>31.58</td><td>0.00</td></tr><tr><td>10,000 get</td><td>29.75</td><td>33.61</td><td>5.49e-4</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add randomly</td><td>31.67</td><td>31.58</td><td>9.08e-4</td></tr><tr><td>10,000 add & delete randomly</td><td>73.30</td><td>13.64</td><td>0.01</td></tr><tr><td>10,000 addMany</td><td>30.35</td><td>32.95</td><td>0.00</td></tr><tr><td>10,000 get</td><td>29.49</td><td>33.91</td><td>7.80e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>rb-tree</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add randomly</td><td>87.24</td><td>11.46</td><td>0.00</td></tr><tr><td>100,000 add & delete randomly</td><td>218.78</td><td>4.57</td><td>0.01</td></tr><tr><td>100,000 getNode</td><td>91.39</td><td>10.94</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 add randomly</td><td>90.85</td><td>11.01</td><td>0.01</td></tr><tr><td>100,000 add & delete randomly</td><td>238.18</td><td>4.20</td><td>0.07</td></tr><tr><td>100,000 getNode</td><td>111.04</td><td>9.01</td><td>0.03</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>directed-graph</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 addVertex</td><td>0.10</td><td>9631.84</td><td>5.21e-6</td></tr><tr><td>1,000 addEdge</td><td>6.25</td><td>160.06</td><td>3.97e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.13e+4</td><td>1.05e-6</td></tr><tr><td>1,000 getEdge</td><td>23.83</td><td>41.97</td><td>0.00</td></tr><tr><td>tarjan</td><td>217.69</td><td>4.59</td><td>0.01</td></tr><tr><td>tarjan all</td><td>226.43</td><td>4.42</td><td>0.01</td></tr><tr><td>topologicalSort</td><td>180.71</td><td>5.53</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000 addVertex</td><td>0.10</td><td>9609.82</td><td>5.99e-6</td></tr><tr><td>1,000 addEdge</td><td>6.43</td><td>155.61</td><td>4.69e-4</td></tr><tr><td>1,000 getVertex</td><td>0.05</td><td>2.10e+4</td><td>2.90e-6</td></tr><tr><td>1,000 getEdge</td><td>23.98</td><td>41.70</td><td>0.01</td></tr><tr><td>tarjan</td><td>225.87</td><td>4.43</td><td>0.02</td></tr><tr><td>tarjan all</td><td>229.79</td><td>4.35</td><td>0.03</td></tr><tr><td>topologicalSort</td><td>192.07</td><td>5.21</td><td>0.03</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>heap</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add & pop</td><td>4.69</td><td>213.13</td><td>1.63e-4</td></tr><tr><td>10,000 fib add & pop</td><td>367.49</td><td>2.72</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 add & pop</td><td>4.67</td><td>214.00</td><td>7.34e-5</td></tr><tr><td>10,000 fib add & pop</td><td>370.25</td><td>2.70</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>doubly-linked-list</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 unshift</td><td>221.10</td><td>4.52</td><td>0.06</td></tr><tr><td>1,000,000 unshift & shift</td><td>179.99</td><td>5.56</td><td>0.03</td></tr><tr><td>1,000,000 insertBefore</td><td>331.29</td><td>3.02</td><td>0.06</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 unshift</td><td>208.21</td><td>4.80</td><td>0.03</td></tr><tr><td>1,000,000 unshift & shift</td><td>172.47</td><td>5.80</td><td>0.03</td></tr><tr><td>1,000,000 insertBefore</td><td>319.07</td><td>3.13</td><td>0.07</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>singly-linked-list</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 push & pop</td><td>216.59</td><td>4.62</td><td>0.01</td></tr><tr><td>10,000 insertBefore</td><td>255.08</td><td>3.92</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 push & pop</td><td>222.14</td><td>4.50</td><td>0.01</td></tr><tr><td>10,000 insertBefore</td><td>246.45</td><td>4.06</td><td>0.01</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>max-priority-queue</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 refill & poll</td><td>12.00</td><td>83.34</td><td>6.62e-4</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>10,000 refill & poll</td><td>11.61</td><td>86.10</td><td>2.55e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>deque</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>222.01</td><td>4.50</td><td>0.05</td></tr><tr><td>1,000,000 shift</td><td>25.35</td><td>39.45</td><td>0.00</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>219.76</td><td>4.55</td><td>0.05</td></tr><tr><td>1,000,000 shift</td><td>25.57</td><td>39.10</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>queue</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>45.31</td><td>22.07</td><td>0.01</td></tr><tr><td>1,000,000 push & shift</td><td>83.92</td><td>11.92</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>46.20</td><td>21.64</td><td>0.01</td></tr><tr><td>1,000,000 push & shift</td><td>93.60</td><td>10.68</td><td>0.01</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>trie</span></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 push</td><td>59.08</td><td>16.92</td><td>0.01</td></tr><tr><td>100,000 getWords</td><td>95.74</td><td>10.44</td><td>0.01</td></tr></table></div>
<div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>100,000 push</td><td>57.94</td><td>17.26</td><td>0.01</td></tr><tr><td>100,000 getWords</td><td>118.45</td><td>8.44</td><td>0.03</td></tr></table></div>
</div>
[//]: # (No deletion!!! End of Replace Section)

View file

@ -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",

View file

@ -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.

View file

@ -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;
}
}
}
}

View file

@ -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.

View file

@ -53,6 +53,14 @@ export class Heap<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E> {
}
/**
* 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<E>} FibonacciHeap<E> - The heap itself.
@ -330,8 +445,16 @@ export class FibonacciHeap<E> {
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<E>} FibonacciHeap<E> - The heap itself.
@ -350,8 +473,16 @@ export class FibonacciHeap<E> {
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<E> {
}
/**
* 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<E>} head - The head of the linked list.
* @protected
@ -388,8 +526,9 @@ export class FibonacciHeap<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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
*/

View file

@ -55,6 +55,14 @@ export class DoublyLinkedList<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E>} 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
* a `DoublyLinkedListNode<E>` object.
@ -368,6 +512,14 @@ export class DoublyLinkedList<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E>} 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

View file

@ -49,6 +49,14 @@ export class SinglyLinkedList<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
* or a `SinglyLinkedListNode<E>` object.
@ -263,6 +367,14 @@ export class SinglyLinkedList<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E>} 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<E = any> {
}
/**
* 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<E>} 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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

View file

@ -58,6 +58,14 @@ export class SkipList<K, V> {
}
/**
* 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<K, V> {
}
/**
* 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<K, V> {
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<K, V> {
}
/**
* 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<K, V> {
}
/**
* 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<K, V> {
}
/**
* 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<K, V> {
}
/**
* 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<K, V> {
}
/**
* 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.
*/

View file

@ -50,6 +50,14 @@ export class ObjectDeque<E = number> {
}
/**
* 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<E = number> {
}
/**
* 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<E = number> {
}
/**
* 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<E = number> {
}
/**
* 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<E = number> {
}
/**
* 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<E = number> {
}
/**
* 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<E = number> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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<E> {
}
/**
* 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.

View file

@ -84,6 +84,14 @@ export class Queue<E = any> {
}
/**
* 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<E>` object.
@ -94,6 +102,14 @@ export class Queue<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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.
*/

View file

@ -21,6 +21,14 @@ export class Stack<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E>` object.
@ -67,6 +91,14 @@ export class Stack<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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<E = any> {
}
/**
* 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.
*/

View file

@ -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 ---
}
}