data-structure-typed 1.19.5 → 1.19.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/README.md +7 -11
  2. package/dist/data-structures/graph/abstract-graph.d.ts +20 -20
  3. package/dist/data-structures/graph/abstract-graph.js +19 -19
  4. package/dist/data-structures/heap/heap.d.ts +16 -25
  5. package/dist/data-structures/heap/heap.js +51 -38
  6. package/dist/data-structures/priority-queue/priority-queue.d.ts +1 -1
  7. package/dist/data-structures/priority-queue/priority-queue.js +1 -2
  8. package/jest.config.js +1 -1
  9. package/package.json +4 -3
  10. package/src/data-structures/graph/abstract-graph.ts +27 -27
  11. package/src/data-structures/heap/heap.ts +68 -42
  12. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  13. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  14. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  15. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  16. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  17. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  18. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  19. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  20. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  21. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  22. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  23. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  24. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  25. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  26. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  27. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  28. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  29. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
package/README.md CHANGED
@@ -1,31 +1,27 @@
1
1
  # What
2
2
 
3
3
  ## Brief
4
- Javascript & TypeScript Data Structure Library.
4
+ Javascript & TypeScript Data Structure collections.
5
5
 
6
- Binary Tree, Binary Search Tree (BST), AVL Tree, Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Linked List, Singly Linked List, Doubly Linked List, Queue, Object Deque, Array Deque, Stack, Hash, Coordinate Set, Coordinate Map, Heap, Priority Queue, Max Priority Queue, Min Priority Queue, Trie
6
+ ## Algorithms
7
7
 
8
- ## Algorithms list only a few out, you can discover more in API docs
9
-
10
- DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm
8
+ DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm. Listed only a few out, you can discover more in API docs
11
9
 
12
10
  ## Code design
13
11
  By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design.
14
12
 
15
13
  # How
16
-
14
+ ### npm
15
+ ```bash
16
+ npm install data-structure-typed
17
+ ```
17
18
  ## install
18
19
  ### yarn
19
-
20
20
  ```bash
21
21
  yarn add data-structure-typed
22
22
  ```
23
23
 
24
- ### npm
25
24
 
26
- ```bash
27
- npm install data-structure-typed
28
- ```
29
25
 
30
26
  ### Binary Search Tree (BST) snippet
31
27
 
@@ -72,7 +72,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
72
72
  abstract edgesOf(vertexOrId: V | VertexId): E[];
73
73
  abstract getNeighbors(vertexOrId: V | VertexId): V[];
74
74
  abstract getEndsOfEdge(edge: E): [V, V] | null;
75
- protected abstract _addEdgeOnly(edge: E): boolean;
76
75
  /**
77
76
  * The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
78
77
  * @param {VertexId} vertexId - The `vertexId` parameter is the identifier of the vertex that you want to retrieve from
@@ -220,25 +219,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
220
219
  * @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
221
220
  */
222
221
  dijkstra(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V>;
223
- /**
224
- * Dijkstra algorithm time: O(logVE) space: O(V + E)
225
- * /
226
-
227
- /**
228
- * Dijkstra algorithm time: O(logVE) space: O(V + E)
229
- * 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.
230
- */
231
- /**
232
- * BellmanFord time:O(VE) space:O(V)
233
- * one to rest pairs
234
- * 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.
235
- * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
236
- */
237
- /**
238
- * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
239
- * all pairs
240
- * 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.
241
- */
242
222
  /**
243
223
  * BellmanFord time:O(VE) space:O(V)
244
224
  * one to rest pairs
@@ -268,6 +248,25 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
268
248
  min: number;
269
249
  minPath: V[];
270
250
  };
251
+ /**
252
+ * Dijkstra algorithm time: O(logVE) space: O(V + E)
253
+ * /
254
+
255
+ /**
256
+ * Dijkstra algorithm time: O(logVE) space: O(V + E)
257
+ * 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.
258
+ */
259
+ /**
260
+ * BellmanFord time:O(VE) space:O(V)
261
+ * one to rest pairs
262
+ * 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.
263
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
264
+ */
265
+ /**
266
+ * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
267
+ * all pairs
268
+ * 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.
269
+ */
271
270
  /**
272
271
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
273
272
  * all pairs
@@ -325,6 +324,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
325
324
  SCCs: Map<number, V[]>;
326
325
  cycles: Map<number, V[]>;
327
326
  };
327
+ protected abstract _addEdgeOnly(edge: E): boolean;
328
328
  protected _addVertexOnly(newVertex: V): boolean;
329
329
  protected _getVertex(vertexOrId: VertexId | V): V | null;
330
330
  protected _getVertexId(vertexOrId: V | VertexId): VertexId;
@@ -600,25 +600,6 @@ class AbstractGraph {
600
600
  }
601
601
  return { distMap, preMap, seen, paths, minDist, minPath };
602
602
  }
603
- /**
604
- * Dijkstra algorithm time: O(logVE) space: O(V + E)
605
- * /
606
-
607
- /**
608
- * Dijkstra algorithm time: O(logVE) space: O(V + E)
609
- * 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.
610
- */
611
- /**
612
- * BellmanFord time:O(VE) space:O(V)
613
- * one to rest pairs
614
- * 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.
615
- * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
616
- */
617
- /**
618
- * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
619
- * all pairs
620
- * 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.
621
- */
622
603
  /**
623
604
  * BellmanFord time:O(VE) space:O(V)
624
605
  * one to rest pairs
@@ -725,6 +706,25 @@ class AbstractGraph {
725
706
  }
726
707
  return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
727
708
  }
709
+ /**
710
+ * Dijkstra algorithm time: O(logVE) space: O(V + E)
711
+ * /
712
+
713
+ /**
714
+ * Dijkstra algorithm time: O(logVE) space: O(V + E)
715
+ * 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.
716
+ */
717
+ /**
718
+ * BellmanFord time:O(VE) space:O(V)
719
+ * one to rest pairs
720
+ * 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.
721
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
722
+ */
723
+ /**
724
+ * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
725
+ * all pairs
726
+ * 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.
727
+ */
728
728
  /**
729
729
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
730
730
  * all pairs
@@ -25,8 +25,7 @@ export declare class HeapItem<T = number> {
25
25
  }
26
26
  export declare abstract class Heap<T = number> {
27
27
  /**
28
- * The function is a constructor for a class that initializes a priority callback function based on the
29
- * options provided.
28
+ * The constructor function initializes a priority queue with an optional priority extractor function.
30
29
  * @param [options] - An optional object that contains configuration options for the Heap.
31
30
  */
32
31
  protected constructor(options?: HeapOptions<T>);
@@ -44,16 +43,12 @@ export declare abstract class Heap<T = number> {
44
43
  * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
45
44
  */
46
45
  isEmpty(): boolean;
47
- /**
48
- * The `peek` function returns the top item in the priority queue without removing it.
49
- * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
50
- */
51
- peek(): HeapItem<T> | null;
52
- /**
53
- * The `peekLast` function returns the last item in the heap.
54
- * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
55
- */
56
- peekLast(): HeapItem<T> | null;
46
+ peek(isItem?: undefined): T | undefined;
47
+ peek(isItem: false): T | undefined;
48
+ peek(isItem: true): HeapItem<T> | null;
49
+ peekLast(isItem?: undefined): T | undefined;
50
+ peekLast(isItem: false): T | undefined;
51
+ peekLast(isItem: true): HeapItem<T> | null;
57
52
  /**
58
53
  * The `add` function adds an val to a priority queue with an optional priority value.
59
54
  * @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
@@ -64,23 +59,19 @@ export declare abstract class Heap<T = number> {
64
59
  * @returns The `add` method returns the instance of the `Heap` class.
65
60
  * @throws {Error} if priority is not a valid number
66
61
  */
67
- add(val: T, priority?: number): Heap<T>;
62
+ add(priority: number, val?: T): Heap<T>;
63
+ poll(isItem?: undefined): T | undefined;
64
+ poll(isItem: false): T | undefined;
65
+ poll(isItem: true): HeapItem<T> | null;
68
66
  /**
69
- * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
70
- * @returns either a HeapItem<T> object or null.
71
- */
72
- poll(): HeapItem<T> | null;
73
- /**
74
- * The function checks if a given node or value exists in the priority queue.
75
- * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
67
+ * The `has` function checks if a given node or value exists in the priority queue.
68
+ * @param {T | HeapItem<T>} node - The `node` parameter can be of type `T` or `HeapItem<T>`.
76
69
  * @returns a boolean value.
77
70
  */
78
71
  has(node: T | HeapItem<T>): boolean;
79
- /**
80
- * The `toArray` function returns an array of `HeapItem<T>` objects.
81
- * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
82
- */
83
- toArray(): HeapItem<T>[];
72
+ toArray(isItem?: undefined): (T | undefined)[];
73
+ toArray(isItem: false): (T | undefined)[];
74
+ toArray(isItem: true): (HeapItem<T> | null)[];
84
75
  /**
85
76
  * The clear function clears the priority queue.
86
77
  */
@@ -29,8 +29,7 @@ class HeapItem {
29
29
  exports.HeapItem = HeapItem;
30
30
  class Heap {
31
31
  /**
32
- * The function is a constructor for a class that initializes a priority callback function based on the
33
- * options provided.
32
+ * The constructor function initializes a priority queue with an optional priority extractor function.
34
33
  * @param [options] - An optional object that contains configuration options for the Heap.
35
34
  */
36
35
  constructor(options) {
@@ -66,18 +65,34 @@ class Heap {
66
65
  return this._pq.size < 1;
67
66
  }
68
67
  /**
69
- * The `peek` function returns the top item in the priority queue without removing it.
70
- * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
68
+ * The `peek` function returns the top item or value in a priority queue, depending on the value of the `isItem`
69
+ * parameter.
70
+ * @param {boolean} [isItem] - The `isItem` parameter is an optional boolean parameter that determines whether the
71
+ * method should return the entire `HeapItem` object or just the value of the item. If `isItem` is set to `true`, the
72
+ * method will return the `HeapItem` object. If `isItem`
73
+ * @returns The `peek` method returns either a `HeapItem<T>` object, `null`, `T`, or `undefined`. The specific return
74
+ * type depends on the value of the `isItem` parameter. If `isItem` is `true`, then the method returns a `HeapItem<T>`
75
+ * object or `null` if the heap is empty. If `isItem` is `false`
71
76
  */
72
- peek() {
73
- return this._pq.peek();
77
+ peek(isItem) {
78
+ isItem = isItem !== null && isItem !== void 0 ? isItem : false;
79
+ const peeked = this._pq.peek();
80
+ return isItem ? peeked : peeked === null || peeked === void 0 ? void 0 : peeked.val;
74
81
  }
75
82
  /**
76
- * The `peekLast` function returns the last item in the heap.
77
- * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
83
+ * The `peekLast` function returns the last item in the heap, either as a `HeapItem` object or just the value depending
84
+ * on the `isItem` parameter.
85
+ * @param {boolean} [isItem] - A boolean parameter that indicates whether the method should return the HeapItem object
86
+ * or just the value of the last item in the heap. If isItem is true, the method will return the HeapItem object. If
87
+ * isItem is false or not provided, the method will return the value of the last item
88
+ * @returns The method `peekLast` returns either a `HeapItem<T>` object, `null`, `T`, or `undefined`. The specific
89
+ * return type depends on the value of the `isItem` parameter. If `isItem` is `true`, then the method returns a
90
+ * `HeapItem<T>` object or `null` if there are no items in the heap. If `isItem`
78
91
  */
79
- peekLast() {
80
- return this._pq.leaf();
92
+ peekLast(isItem) {
93
+ isItem = isItem !== null && isItem !== void 0 ? isItem : false;
94
+ const leafItem = this._pq.leaf();
95
+ return isItem ? leafItem : leafItem === null || leafItem === void 0 ? void 0 : leafItem.val;
81
96
  }
82
97
  /**
83
98
  * The `add` function adds an val to a priority queue with an optional priority value.
@@ -89,40 +104,31 @@ class Heap {
89
104
  * @returns The `add` method returns the instance of the `Heap` class.
90
105
  * @throws {Error} if priority is not a valid number
91
106
  */
92
- add(val, priority) {
93
- if (typeof val === 'number') {
94
- priority = val;
95
- }
96
- else {
97
- if (priority === undefined) {
98
- throw new Error('.add expects a numeric priority');
99
- }
100
- }
101
- if (priority && Number.isNaN(+priority)) {
102
- throw new Error('.add expects a numeric priority');
103
- }
104
- if (Number.isNaN(+priority) && Number.isNaN(this._priorityExtractor(val))) {
105
- throw new Error('.add expects a numeric priority '
106
- + 'or a constructor callback that returns a number');
107
- }
108
- const _priority = !Number.isNaN(+priority) ? priority : this._priorityExtractor(val);
109
- this._pq.add(new HeapItem(_priority, val));
107
+ add(priority, val) {
108
+ val = (val === undefined) ? priority : val;
109
+ this._pq.add(new HeapItem(priority, val));
110
110
  return this;
111
111
  }
112
112
  /**
113
- * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
114
- * @returns either a HeapItem<T> object or null.
113
+ * The `poll` function returns the top item from a priority queue, either as a HeapItem object or its value, depending
114
+ * on the value of the `isItem` parameter.
115
+ * @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the returned value
116
+ * should be a `HeapItem<T>` object or just the value `T` itself. If `isItem` is `true`, the method will return the
117
+ * `HeapItem<T>` object, otherwise it will return just
118
+ * @returns The function `poll` returns either a `HeapItem<T>` object, `null`, or `T` (the value of the `val` property
119
+ * of the `HeapItem<T>` object).
115
120
  */
116
- poll() {
121
+ poll(isItem) {
122
+ isItem = isItem !== null && isItem !== void 0 ? isItem : false;
117
123
  const top = this._pq.poll();
118
124
  if (!top) {
119
125
  return null;
120
126
  }
121
- return top;
127
+ return isItem ? top : top.val;
122
128
  }
123
129
  /**
124
- * The function checks if a given node or value exists in the priority queue.
125
- * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
130
+ * The `has` function checks if a given node or value exists in the priority queue.
131
+ * @param {T | HeapItem<T>} node - The `node` parameter can be of type `T` or `HeapItem<T>`.
126
132
  * @returns a boolean value.
127
133
  */
128
134
  has(node) {
@@ -136,11 +142,18 @@ class Heap {
136
142
  }
137
143
  }
138
144
  /**
139
- * The `toArray` function returns an array of `HeapItem<T>` objects.
140
- * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
145
+ * The `toArray` function returns an array of either HeapItem objects or their values, depending on the value of the
146
+ * `isItem` parameter.
147
+ * @param {boolean} [isItem] - isItem is an optional boolean parameter that determines whether the returned array
148
+ * should contain the HeapItem objects or just the values of the HeapItem objects. If isItem is true, the array will
149
+ * contain the HeapItem objects. If isItem is false or not provided, the array will contain only the values
150
+ * @returns The method `toArray` returns an array of `HeapItem<T>` objects, or an array of `T` values if the `isItem`
151
+ * parameter is set to `false`.
141
152
  */
142
- toArray() {
143
- return this._pq.toArray();
153
+ toArray(isItem) {
154
+ isItem = isItem !== null && isItem !== void 0 ? isItem : false;
155
+ const itemArray = this._pq.toArray();
156
+ return isItem ? itemArray : itemArray.map(item => item.val);
144
157
  }
145
158
  /**
146
159
  * The clear function clears the priority queue.
@@ -95,7 +95,7 @@ export declare class PriorityQueue<T = number> {
95
95
  */
96
96
  isValid(): boolean;
97
97
  /**
98
- * Plan to support sorting of duplicate elements.
98
+ * O(n log n), In scenarios with smaller data sizes, heap sort is generally expected to be slower than QuickSort or MergeSort.
99
99
  */
100
100
  /**
101
101
  * The function sorts the elements in a data structure and returns them in an array.
@@ -157,7 +157,7 @@ class PriorityQueue {
157
157
  return true;
158
158
  }
159
159
  /**
160
- * Plan to support sorting of duplicate elements.
160
+ * O(n log n), In scenarios with smaller data sizes, heap sort is generally expected to be slower than QuickSort or MergeSort.
161
161
  */
162
162
  /**
163
163
  * The function sorts the elements in a data structure and returns them in an array.
@@ -165,7 +165,6 @@ class PriorityQueue {
165
165
  * @returns The `sort()` method is returning an array of type `T[]`.
166
166
  */
167
167
  sort() {
168
- // TODO Plan to support sorting of duplicate elements.
169
168
  const visitedNode = [];
170
169
  while (this.size !== 0) {
171
170
  const top = this.poll();
package/jest.config.js CHANGED
@@ -1,5 +1,5 @@
1
1
  module.exports = {
2
2
  preset: 'ts-jest',
3
3
  testEnvironment: 'node',
4
- testMatch: ['<rootDir>/tests/**/*.test.ts'],
4
+ testMatch: ['<rootDir>/tests/**/*.test.ts', '<rootDir>/tests/**/*.test.js'],
5
5
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.19.5",
3
+ "version": "1.19.7",
4
4
  "description": "Javascript & TypeScript Data Structure Library, meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures, such as Binary Tree, AVL Tree, Binary Search Tree (BST), Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Singly Linked List, Hash, CoordinateSet, CoordinateMap, Heap, Doubly Linked List, Priority Queue, Max Priority Queue, Min Priority Queue, Queue, ObjectDeque, ArrayDeque, Stack, and Trie. Each data structure is thoughtfully designed and implemented using TypeScript to provide efficient, reliable, and easy-to-use solutions for your programming needs. Whether you're optimizing algorithms, managing data, or enhancing performance, our TypeScript Data Structure Library is your go-to resource. Elevate your coding experience with these fundamental building blocks for software development.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -65,8 +65,9 @@
65
65
  "typescript": "^4.9.5"
66
66
  },
67
67
  "dependencies": {
68
- "avl-tree-typed": "^1.19.44",
69
- "bst-typed": "^1.19.45",
68
+ "avl-tree-typed": "^1.19.6",
69
+ "bst-typed": "^1.19.6",
70
+ "heap-typed": "^1.19.6",
70
71
  "zod": "^3.22.2"
71
72
  }
72
73
  }
@@ -129,6 +129,7 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
129
129
  abstract createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): E;
130
130
 
131
131
  abstract removeEdge(edge: E): E | null;
132
+
132
133
  abstract getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
133
134
 
134
135
  abstract degreeOf(vertexOrId: V | VertexId): number;
@@ -141,8 +142,6 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
141
142
 
142
143
  abstract getEndsOfEdge(edge: E): [V, V] | null;
143
144
 
144
- protected abstract _addEdgeOnly(edge: E): boolean;
145
-
146
145
  /**
147
146
  * The function "getVertex" returns the vertex with the specified ID or null if it doesn't exist.
148
147
  * @param {VertexId} vertexId - The `vertexId` parameter is the identifier of the vertex that you want to retrieve from
@@ -165,11 +164,12 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
165
164
  }
166
165
 
167
166
  addVertex(vertex: V): boolean
167
+
168
168
  addVertex(id: VertexId, val?: V['val']): boolean
169
+
169
170
  addVertex(idOrVertex: VertexId | V, val?: V['val']): boolean {
170
171
  if (idOrVertex instanceof AbstractVertex) {
171
172
  return this._addVertexOnly(idOrVertex);
172
-
173
173
  } else {
174
174
  const newVertex = this.createVertex(idOrVertex, val);
175
175
  return this._addVertexOnly(newVertex);
@@ -256,7 +256,6 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
256
256
  }
257
257
  }
258
258
 
259
-
260
259
  /**
261
260
  * The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
262
261
  * @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
@@ -691,29 +690,6 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
691
690
  return {distMap, preMap, seen, paths, minDist, minPath};
692
691
  }
693
692
 
694
- /**
695
- * Dijkstra algorithm time: O(logVE) space: O(V + E)
696
- * /
697
-
698
- /**
699
- * Dijkstra algorithm time: O(logVE) space: O(V + E)
700
- * 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.
701
- */
702
-
703
-
704
- /**
705
- * BellmanFord time:O(VE) space:O(V)
706
- * one to rest pairs
707
- * 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.
708
- * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
709
- */
710
-
711
- /**
712
- * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
713
- * all pairs
714
- * 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.
715
- */
716
-
717
693
  /**
718
694
  * BellmanFord time:O(VE) space:O(V)
719
695
  * one to rest pairs
@@ -823,6 +799,29 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
823
799
  return {hasNegativeCycle, distMap, preMap, paths, min, minPath};
824
800
  }
825
801
 
802
+ /**
803
+ * Dijkstra algorithm time: O(logVE) space: O(V + E)
804
+ * /
805
+
806
+ /**
807
+ * Dijkstra algorithm time: O(logVE) space: O(V + E)
808
+ * 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.
809
+ */
810
+
811
+
812
+ /**
813
+ * BellmanFord time:O(VE) space:O(V)
814
+ * one to rest pairs
815
+ * 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.
816
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
817
+ */
818
+
819
+ /**
820
+ * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
821
+ * all pairs
822
+ * 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.
823
+ */
824
+
826
825
  /**
827
826
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
828
827
  * all pairs
@@ -1007,6 +1006,7 @@ export abstract class AbstractGraph<V extends AbstractVertex<any>, E extends Abs
1007
1006
  return {dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles};
1008
1007
  }
1009
1008
 
1009
+ protected abstract _addEdgeOnly(edge: E): boolean;
1010
1010
 
1011
1011
  protected _addVertexOnly(newVertex: V): boolean {
1012
1012
  if (this.hasVertex(newVertex)) {
@@ -44,9 +44,9 @@ export class HeapItem<T = number> {
44
44
  }
45
45
 
46
46
  export abstract class Heap<T = number> {
47
+
47
48
  /**
48
- * The function is a constructor for a class that initializes a priority callback function based on the
49
- * options provided.
49
+ * The constructor function initializes a priority queue with an optional priority extractor function.
50
50
  * @param [options] - An optional object that contains configuration options for the Heap.
51
51
  */
52
52
  protected constructor(options?: HeapOptions<T>) {
@@ -88,20 +88,44 @@ export abstract class Heap<T = number> {
88
88
  return this._pq.size < 1;
89
89
  }
90
90
 
91
+ peek(isItem?: undefined): T | undefined;
92
+ peek(isItem: false): T | undefined;
93
+ peek(isItem: true): HeapItem<T> | null;
94
+
91
95
  /**
92
- * The `peek` function returns the top item in the priority queue without removing it.
93
- * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
96
+ * The `peek` function returns the top item or value in a priority queue, depending on the value of the `isItem`
97
+ * parameter.
98
+ * @param {boolean} [isItem] - The `isItem` parameter is an optional boolean parameter that determines whether the
99
+ * method should return the entire `HeapItem` object or just the value of the item. If `isItem` is set to `true`, the
100
+ * method will return the `HeapItem` object. If `isItem`
101
+ * @returns The `peek` method returns either a `HeapItem<T>` object, `null`, `T`, or `undefined`. The specific return
102
+ * type depends on the value of the `isItem` parameter. If `isItem` is `true`, then the method returns a `HeapItem<T>`
103
+ * object or `null` if the heap is empty. If `isItem` is `false`
94
104
  */
95
- peek(): HeapItem<T> | null {
96
- return this._pq.peek();
105
+ peek(isItem?: boolean): HeapItem<T> | null | T | undefined {
106
+ isItem = isItem ?? false;
107
+ const peeked = this._pq.peek();
108
+ return isItem ? peeked : peeked?.val;
97
109
  }
98
110
 
111
+ peekLast(isItem?: undefined): T | undefined;
112
+ peekLast(isItem: false): T | undefined;
113
+ peekLast(isItem: true): HeapItem<T> | null;
114
+
99
115
  /**
100
- * The `peekLast` function returns the last item in the heap.
101
- * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
116
+ * The `peekLast` function returns the last item in the heap, either as a `HeapItem` object or just the value depending
117
+ * on the `isItem` parameter.
118
+ * @param {boolean} [isItem] - A boolean parameter that indicates whether the method should return the HeapItem object
119
+ * or just the value of the last item in the heap. If isItem is true, the method will return the HeapItem object. If
120
+ * isItem is false or not provided, the method will return the value of the last item
121
+ * @returns The method `peekLast` returns either a `HeapItem<T>` object, `null`, `T`, or `undefined`. The specific
122
+ * return type depends on the value of the `isItem` parameter. If `isItem` is `true`, then the method returns a
123
+ * `HeapItem<T>` object or `null` if there are no items in the heap. If `isItem`
102
124
  */
103
- peekLast(): HeapItem<T> | null {
104
- return this._pq.leaf();
125
+ peekLast(isItem?: boolean): HeapItem<T> | null | T | undefined {
126
+ isItem = isItem ?? false;
127
+ const leafItem = this._pq.leaf();
128
+ return isItem ? leafItem : leafItem?.val;
105
129
  }
106
130
 
107
131
  /**
@@ -114,46 +138,37 @@ export abstract class Heap<T = number> {
114
138
  * @returns The `add` method returns the instance of the `Heap` class.
115
139
  * @throws {Error} if priority is not a valid number
116
140
  */
117
- add(val: T, priority?: number): Heap<T> {
118
- if (typeof val === 'number') {
119
- priority = val;
120
- } else {
121
- if (priority === undefined) {
122
- throw new Error('.add expects a numeric priority');
123
- }
124
- }
125
-
126
- if (priority && Number.isNaN(+priority)) {
127
- throw new Error('.add expects a numeric priority');
128
- }
129
-
130
- if (Number.isNaN(+priority) && Number.isNaN(this._priorityExtractor(val))) {
131
- throw new Error(
132
- '.add expects a numeric priority '
133
- + 'or a constructor callback that returns a number'
134
- );
135
- }
136
-
137
- const _priority = !Number.isNaN(+priority) ? priority : this._priorityExtractor(val);
138
- this._pq.add(new HeapItem<T>(_priority, val));
141
+ add(priority: number, val?: T,): Heap<T> {
142
+ val = (val === undefined) ? priority as T : val;
143
+ this._pq.add(new HeapItem<T>(priority, val));
139
144
  return this;
140
145
  }
141
146
 
147
+ poll(isItem?: undefined): T | undefined;
148
+ poll(isItem: false): T | undefined;
149
+ poll(isItem: true): HeapItem<T> | null;
150
+
142
151
  /**
143
- * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
144
- * @returns either a HeapItem<T> object or null.
152
+ * The `poll` function returns the top item from a priority queue, either as a HeapItem object or its value, depending
153
+ * on the value of the `isItem` parameter.
154
+ * @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the returned value
155
+ * should be a `HeapItem<T>` object or just the value `T` itself. If `isItem` is `true`, the method will return the
156
+ * `HeapItem<T>` object, otherwise it will return just
157
+ * @returns The function `poll` returns either a `HeapItem<T>` object, `null`, or `T` (the value of the `val` property
158
+ * of the `HeapItem<T>` object).
145
159
  */
146
- poll(): HeapItem<T> | null {
160
+ poll(isItem?: boolean): HeapItem<T> | null | T | undefined {
161
+ isItem = isItem ?? false;
147
162
  const top = this._pq.poll();
148
163
  if (!top) {
149
164
  return null;
150
165
  }
151
- return top;
166
+ return isItem ? top : top.val;
152
167
  }
153
168
 
154
169
  /**
155
- * The function checks if a given node or value exists in the priority queue.
156
- * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
170
+ * The `has` function checks if a given node or value exists in the priority queue.
171
+ * @param {T | HeapItem<T>} node - The `node` parameter can be of type `T` or `HeapItem<T>`.
157
172
  * @returns a boolean value.
158
173
  */
159
174
  has(node: T | HeapItem<T>): boolean {
@@ -166,12 +181,23 @@ export abstract class Heap<T = number> {
166
181
  }
167
182
  }
168
183
 
184
+ toArray(isItem?: undefined): (T | undefined)[];
185
+ toArray(isItem: false): (T | undefined)[];
186
+ toArray(isItem: true): (HeapItem<T> | null)[];
187
+
169
188
  /**
170
- * The `toArray` function returns an array of `HeapItem<T>` objects.
171
- * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
189
+ * The `toArray` function returns an array of either HeapItem objects or their values, depending on the value of the
190
+ * `isItem` parameter.
191
+ * @param {boolean} [isItem] - isItem is an optional boolean parameter that determines whether the returned array
192
+ * should contain the HeapItem objects or just the values of the HeapItem objects. If isItem is true, the array will
193
+ * contain the HeapItem objects. If isItem is false or not provided, the array will contain only the values
194
+ * @returns The method `toArray` returns an array of `HeapItem<T>` objects, or an array of `T` values if the `isItem`
195
+ * parameter is set to `false`.
172
196
  */
173
- toArray(): HeapItem<T>[] {
174
- return this._pq.toArray();
197
+ toArray(isItem?: boolean): (HeapItem<T> | null | T | undefined)[] {
198
+ isItem = isItem ?? false;
199
+ const itemArray = this._pq.toArray();
200
+ return isItem ? itemArray : itemArray.map(item => item.val);
175
201
  }
176
202
 
177
203
  /**
@@ -174,15 +174,15 @@ export class PriorityQueue<T = number> {
174
174
  }
175
175
 
176
176
  /**
177
- * Plan to support sorting of duplicate elements.
177
+ * O(n log n), In scenarios with smaller data sizes, heap sort is generally expected to be slower than QuickSort or MergeSort.
178
178
  */
179
+
179
180
  /**
180
181
  * The function sorts the elements in a data structure and returns them in an array.
181
182
  * Plan to support sorting of duplicate elements.
182
183
  * @returns The `sort()` method is returning an array of type `T[]`.
183
184
  */
184
185
  sort(): T[] {
185
- // TODO Plan to support sorting of duplicate elements.
186
186
  const visitedNode: T[] = [];
187
187
  while (this.size !== 0) {
188
188
  const top = this.poll();