data-structure-typed 1.15.2 → 1.16.1

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 (208) hide show
  1. package/README.md +24 -24
  2. package/dist/data-structures/heap/heap.d.ts +30 -15
  3. package/dist/data-structures/heap/heap.js +60 -15
  4. package/dist/data-structures/heap/max-heap.d.ts +3 -3
  5. package/dist/data-structures/heap/min-heap.d.ts +3 -3
  6. package/dist/data-structures/index.d.ts +1 -0
  7. package/dist/data-structures/index.js +1 -0
  8. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
  9. package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
  10. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
  11. package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
  12. package/dist/data-structures/priority-queue/priority-queue.d.ts +13 -2
  13. package/dist/data-structures/priority-queue/priority-queue.js +28 -22
  14. package/dist/data-structures/tree/index.d.ts +1 -0
  15. package/dist/data-structures/tree/index.js +17 -0
  16. package/dist/data-structures/tree/tree.d.ts +9 -0
  17. package/dist/data-structures/tree/tree.js +52 -0
  18. package/dist/data-structures/types/heap.d.ts +0 -4
  19. package/dist/utils/types/utils.d.ts +1 -0
  20. package/docs/.nojekyll +1 -0
  21. package/docs/assets/highlight.css +92 -0
  22. package/docs/assets/main.js +58 -0
  23. package/docs/assets/search.js +1 -0
  24. package/docs/assets/style.css +1367 -0
  25. package/docs/classes/AVLTree.html +2192 -0
  26. package/docs/classes/AVLTreeNode.html +574 -0
  27. package/docs/classes/AaTree.html +150 -0
  28. package/docs/classes/AbstractEdge.html +269 -0
  29. package/docs/classes/AbstractGraph.html +927 -0
  30. package/docs/classes/AbstractVertex.html +215 -0
  31. package/docs/classes/ArrayDeque.html +417 -0
  32. package/docs/classes/BST.html +2038 -0
  33. package/docs/classes/BSTNode.html +570 -0
  34. package/docs/classes/BTree.html +150 -0
  35. package/docs/classes/BinaryIndexedTree.html +289 -0
  36. package/docs/classes/BinaryTree.html +1827 -0
  37. package/docs/classes/BinaryTreeNode.html +533 -0
  38. package/docs/classes/Character.html +198 -0
  39. package/docs/classes/CoordinateMap.html +469 -0
  40. package/docs/classes/CoordinateSet.html +430 -0
  41. package/docs/classes/Deque.html +767 -0
  42. package/docs/classes/DirectedEdge.html +354 -0
  43. package/docs/classes/DirectedGraph.html +1243 -0
  44. package/docs/classes/DirectedVertex.html +226 -0
  45. package/docs/classes/DoublyLinkedList.html +733 -0
  46. package/docs/classes/DoublyLinkedListNode.html +258 -0
  47. package/docs/classes/Heap.html +482 -0
  48. package/docs/classes/HeapItem.html +233 -0
  49. package/docs/classes/Matrix2D.html +480 -0
  50. package/docs/classes/MatrixNTI2D.html +218 -0
  51. package/docs/classes/MaxHeap.html +500 -0
  52. package/docs/classes/MaxPriorityQueue.html +809 -0
  53. package/docs/classes/MinHeap.html +501 -0
  54. package/docs/classes/MinPriorityQueue.html +811 -0
  55. package/docs/classes/Navigator.html +291 -0
  56. package/docs/classes/ObjectDeque.html +423 -0
  57. package/docs/classes/PriorityQueue.html +733 -0
  58. package/docs/classes/Queue.html +370 -0
  59. package/docs/classes/RBTree.html +150 -0
  60. package/docs/classes/SegmentTree.html +345 -0
  61. package/docs/classes/SegmentTreeNode.html +418 -0
  62. package/docs/classes/SinglyLinkedList.html +1105 -0
  63. package/docs/classes/SinglyLinkedListNode.html +375 -0
  64. package/docs/classes/SplayTree.html +150 -0
  65. package/docs/classes/Stack.html +346 -0
  66. package/docs/classes/TreeMultiSet.html +2036 -0
  67. package/docs/classes/TreeNode.html +236 -0
  68. package/docs/classes/Trie.html +350 -0
  69. package/docs/classes/TrieNode.html +258 -0
  70. package/docs/classes/TwoThreeTree.html +150 -0
  71. package/docs/classes/UndirectedEdge.html +313 -0
  72. package/docs/classes/UndirectedGraph.html +1080 -0
  73. package/docs/classes/UndirectedVertex.html +226 -0
  74. package/docs/classes/Vector2D.html +783 -0
  75. package/docs/enums/CP.html +159 -0
  76. package/docs/enums/FamilyPosition.html +159 -0
  77. package/docs/enums/LoopType.html +160 -0
  78. package/docs/index.html +494 -0
  79. package/docs/interfaces/AVLTreeDeleted.html +161 -0
  80. package/docs/interfaces/BinaryTreeNodeObj.html +168 -0
  81. package/docs/interfaces/HeapOptions.html +167 -0
  82. package/docs/interfaces/IDirectedGraph.html +243 -0
  83. package/docs/interfaces/IGraph.html +427 -0
  84. package/docs/interfaces/NavigatorParams.html +197 -0
  85. package/docs/interfaces/PriorityQueueOptions.html +168 -0
  86. package/docs/modules.html +218 -0
  87. package/docs/types/BSTComparator.html +140 -0
  88. package/docs/types/BSTDeletedResult.html +137 -0
  89. package/docs/types/BinaryTreeDeleted.html +137 -0
  90. package/docs/types/BinaryTreeNodeId.html +125 -0
  91. package/docs/types/BinaryTreeNodePropertyName.html +125 -0
  92. package/docs/types/DFSOrderPattern.html +125 -0
  93. package/docs/types/DijkstraResult.html +145 -0
  94. package/docs/types/Direction.html +125 -0
  95. package/docs/types/DoublyLinkedListGetBy.html +125 -0
  96. package/docs/types/NodeOrPropertyName.html +125 -0
  97. package/docs/types/PriorityQueueComparator.html +145 -0
  98. package/docs/types/PriorityQueueDFSOrderPattern.html +125 -0
  99. package/docs/types/ResultByProperty.html +130 -0
  100. package/docs/types/ResultsByProperty.html +130 -0
  101. package/docs/types/SegmentTreeNodeVal.html +125 -0
  102. package/docs/types/SpecifyOptional.html +132 -0
  103. package/docs/types/Thunk.html +133 -0
  104. package/docs/types/ToThunkFn.html +133 -0
  105. package/docs/types/TopologicalStatus.html +125 -0
  106. package/docs/types/TreeMultiSetDeletedResult.html +137 -0
  107. package/docs/types/TrlAsyncFn.html +138 -0
  108. package/docs/types/TrlFn.html +138 -0
  109. package/docs/types/Turning.html +125 -0
  110. package/docs/types/VertexId.html +125 -0
  111. package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +37 -50
  112. package/notes/note.md +23 -0
  113. package/package.json +1 -1
  114. package/.idea/data-structure-typed.iml +0 -14
  115. package/.idea/modules.xml +0 -8
  116. package/.idea/vcs.xml +0 -6
  117. package/src/assets/complexities-diff.jpg +0 -0
  118. package/src/assets/data-structure-complexities.jpg +0 -0
  119. package/src/assets/logo.png +0 -0
  120. package/src/assets/overview-diagram-of-data-structures.png +0 -0
  121. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  122. package/src/data-structures/binary-tree/avl-tree.ts +0 -293
  123. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  124. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
  125. package/src/data-structures/binary-tree/binary-tree.ts +0 -1492
  126. package/src/data-structures/binary-tree/bst.ts +0 -497
  127. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  128. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  129. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  130. package/src/data-structures/binary-tree/index.ts +0 -11
  131. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  132. package/src/data-structures/binary-tree/segment-tree.ts +0 -267
  133. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  134. package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
  135. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  136. package/src/data-structures/diagrams/README.md +0 -5
  137. package/src/data-structures/graph/abstract-graph.ts +0 -958
  138. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  139. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  140. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  141. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  142. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  143. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  144. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  145. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  146. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  147. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  148. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  149. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  150. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  151. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  152. package/src/data-structures/graph/directed-graph.ts +0 -429
  153. package/src/data-structures/graph/index.ts +0 -3
  154. package/src/data-structures/graph/undirected-graph.ts +0 -259
  155. package/src/data-structures/hash/coordinate-map.ts +0 -74
  156. package/src/data-structures/hash/coordinate-set.ts +0 -63
  157. package/src/data-structures/hash/hash-table.ts +0 -1
  158. package/src/data-structures/hash/index.ts +0 -6
  159. package/src/data-structures/hash/pair.ts +0 -1
  160. package/src/data-structures/hash/tree-map.ts +0 -1
  161. package/src/data-structures/hash/tree-set.ts +0 -1
  162. package/src/data-structures/heap/heap.ts +0 -162
  163. package/src/data-structures/heap/index.ts +0 -3
  164. package/src/data-structures/heap/max-heap.ts +0 -31
  165. package/src/data-structures/heap/min-heap.ts +0 -34
  166. package/src/data-structures/index.ts +0 -13
  167. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -365
  168. package/src/data-structures/linked-list/index.ts +0 -2
  169. package/src/data-structures/linked-list/singly-linked-list.ts +0 -757
  170. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  171. package/src/data-structures/matrix/index.ts +0 -4
  172. package/src/data-structures/matrix/matrix.ts +0 -27
  173. package/src/data-structures/matrix/matrix2d.ts +0 -208
  174. package/src/data-structures/matrix/navigator.ts +0 -122
  175. package/src/data-structures/matrix/vector2d.ts +0 -316
  176. package/src/data-structures/priority-queue/index.ts +0 -3
  177. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
  178. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
  179. package/src/data-structures/priority-queue/priority-queue.ts +0 -349
  180. package/src/data-structures/queue/deque.ts +0 -251
  181. package/src/data-structures/queue/index.ts +0 -2
  182. package/src/data-structures/queue/queue.ts +0 -120
  183. package/src/data-structures/stack/index.ts +0 -1
  184. package/src/data-structures/stack/stack.ts +0 -98
  185. package/src/data-structures/trie/index.ts +0 -1
  186. package/src/data-structures/trie/trie.ts +0 -225
  187. package/src/data-structures/types/abstract-graph.ts +0 -51
  188. package/src/data-structures/types/avl-tree.ts +0 -6
  189. package/src/data-structures/types/binary-tree.ts +0 -15
  190. package/src/data-structures/types/bst.ts +0 -5
  191. package/src/data-structures/types/directed-graph.ts +0 -18
  192. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  193. package/src/data-structures/types/heap.ts +0 -8
  194. package/src/data-structures/types/index.ts +0 -13
  195. package/src/data-structures/types/navigator.ts +0 -13
  196. package/src/data-structures/types/priority-queue.ts +0 -9
  197. package/src/data-structures/types/segment-tree.ts +0 -1
  198. package/src/data-structures/types/singly-linked-list.ts +0 -1
  199. package/src/data-structures/types/tree-multiset.ts +0 -3
  200. package/src/index.ts +0 -1
  201. package/src/utils/index.ts +0 -2
  202. package/src/utils/types/index.ts +0 -1
  203. package/src/utils/types/utils.ts +0 -4
  204. package/src/utils/utils.ts +0 -78
  205. package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
  206. package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -492
  207. package/tests/unit/data-structures/graph/index.ts +0 -3
  208. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
package/README.md CHANGED
@@ -15,6 +15,12 @@ Hash, Coordinate Set, Coordinate Map, Heap, Priority Queue, Max Priority Queue,
15
15
 
16
16
  # How
17
17
 
18
+ ## Live Examples
19
+
20
+ [Live Examples](https://data-structure-typed-examples.vercel.app)
21
+
22
+ <a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
23
+
18
24
  ## install
19
25
 
20
26
  ### yarn
@@ -130,12 +136,6 @@ npm install data-structure-typed
130
136
  expect(bfsNodes[2].id).toBe(16);
131
137
  ```
132
138
 
133
- ## Live Examples
134
-
135
- [//]: # ([Live Examples]&#40;https://data-structure-typed-examples.vercel.app&#41;)
136
-
137
- <a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
138
-
139
139
  ### Directed Graph simple snippet
140
140
 
141
141
  ```typescript
@@ -733,41 +733,41 @@ describe('DirectedGraph Test3', () => {
733
733
  </tbody>
734
734
  </table>
735
735
 
736
- ![overview diagram](src/assets/overview-diagram-of-data-structures.png)
736
+ ![overview diagram](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/assets/overview-diagram-of-data-structures.png)
737
737
 
738
- ![complexities](src/assets/complexities-diff.jpg)
738
+ ![complexities](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/assets/complexities-diff.jpg)
739
739
 
740
- ![complexities of data structures](src/assets/data-structure-complexities.jpg)
740
+ ![complexities of data structures](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/assets/data-structure-complexities.jpg)
741
741
 
742
- ![](src/data-structures/binary-tree/diagrams/bst-rotation.gif)
742
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/binary-tree/bst-rotation.gif)
743
743
 
744
- ![](src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif)
744
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/binary-tree/avl-tree-inserting.gif)
745
745
 
746
- ![](src/data-structures/graph/diagrams/tarjan.webp)
746
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan.webp)
747
747
 
748
- ![](src/data-structures/graph/diagrams/adjacency-list.jpg)
748
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-list.jpg)
749
749
 
750
- ![](src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg)
750
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-list-pros-cons.jpg)
751
751
 
752
- ![](src/data-structures/graph/diagrams/adjacency-matrix.jpg)
752
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-matrix.jpg)
753
753
 
754
- ![](src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg)
754
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/adjacency-matrix-pros-cons.jpg)
755
755
 
756
- ![](src/data-structures/graph/diagrams/dfs-can-do.jpg)
756
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/dfs-can-do.jpg)
757
757
 
758
- ![](src/data-structures/graph/diagrams/edge-list.jpg)
758
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/edge-list.jpg)
759
759
 
760
- ![](src/data-structures/graph/diagrams/edge-list-pros-cons.jpg)
760
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/edge-list-pros-cons.jpg)
761
761
 
762
- ![](src/data-structures/graph/diagrams/max-flow.jpg)
762
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/max-flow.jpg)
763
763
 
764
- ![](src/data-structures/graph/diagrams/mst.jpg)
764
+ ![](https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/mst.jpg)
765
765
 
766
- [//]: # (![]&#40;src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png&#41;)
766
+ [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan-articulation-point-bridge.png&#41;)
767
767
 
768
- [//]: # (![]&#40;src/data-structures/graph/diagrams/tarjan-complicate-simple.png&#41;)
768
+ [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan-complicate-simple.png&#41;)
769
769
 
770
- [//]: # (![]&#40;src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png&#41;)
770
+ [//]: # (![]&#40;https://github.com/zrwusa/assets/blob/master/images/data-structure-typed/graph/tarjan-strongly-connected-component.png&#41;)
771
771
 
772
772
 
773
773
 
@@ -6,8 +6,17 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { PriorityQueue } from '../priority-queue';
9
- import type { HeapItem, HeapOptions } from '../types';
10
- export declare abstract class Heap<T> {
9
+ import type { HeapOptions } from '../types';
10
+ export declare class HeapItem<T = number> {
11
+ constructor(priority?: number, val?: T | null);
12
+ private _priority;
13
+ get priority(): number;
14
+ set priority(value: number);
15
+ private _val;
16
+ get val(): T | null;
17
+ set val(value: T | null);
18
+ }
19
+ export declare abstract class Heap<T = number> {
11
20
  /**
12
21
  * The function is a constructor for a class that initializes a priority callback function based on the
13
22
  * options provided.
@@ -17,9 +26,9 @@ export declare abstract class Heap<T> {
17
26
  protected abstract _pq: PriorityQueue<HeapItem<T>>;
18
27
  get pq(): PriorityQueue<HeapItem<T>>;
19
28
  protected set pq(v: PriorityQueue<HeapItem<T>>);
20
- protected _priorityCb: (element: T) => number;
21
- get priorityCb(): (element: T) => number;
22
- protected set priorityCb(v: (element: T) => number);
29
+ protected _priorityCb: (val: T) => number;
30
+ get priorityCb(): (val: T) => number;
31
+ protected set priorityCb(v: (val: T) => number);
23
32
  /**
24
33
  * The function returns the size of a priority queue.
25
34
  * @returns The size of the priority queue.
@@ -32,7 +41,7 @@ export declare abstract class Heap<T> {
32
41
  /**
33
42
  * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
34
43
  */
35
- getPriorityCb(): (element: T) => number;
44
+ getPriorityCb(): (val: T) => number;
36
45
  /**
37
46
  * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
38
47
  */
@@ -44,33 +53,39 @@ export declare abstract class Heap<T> {
44
53
  isEmpty(): boolean;
45
54
  /**
46
55
  * The `peek` function returns the top item in the priority queue without removing it.
47
- * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an element with the highest priority in the queue
56
+ * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
48
57
  */
49
58
  peek(): HeapItem<T> | null;
50
59
  /**
51
60
  * The `peekLast` function returns the last item in the heap.
52
- * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an element with the lowest priority in the queue
61
+ * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
53
62
  */
54
63
  peekLast(): HeapItem<T> | null;
55
64
  /**
56
- * The `add` function adds an element to a priority queue with an optional priority value.
57
- * @param {T} element - The `element` parameter represents the value that you want to add to the heap. It can be of any
65
+ * The `add` function adds an val to a priority queue with an optional priority value.
66
+ * @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
58
67
  * type.
59
68
  * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
60
- * element being added to the heap. If the `element` parameter is a number, then the `priority` parameter is set to
61
- * the value of `element`. If the `element` parameter is not a number, then the
69
+ * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
70
+ * the value of `val`. If the `val` parameter is not a number, then the
62
71
  * @returns The `add` method returns the instance of the `Heap` class.
63
72
  * @throws {Error} if priority is not a valid number
64
73
  */
65
- add(element: T, priority?: number): Heap<T>;
74
+ add(val: T, priority?: number): Heap<T>;
66
75
  /**
67
- * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an element with the highest priority in the queue
76
+ * 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
68
77
  * @returns either a HeapItem<T> object or null.
69
78
  */
70
79
  poll(): HeapItem<T> | null;
80
+ /**
81
+ * The function checks if a given node or value exists in the priority queue.
82
+ * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
83
+ * @returns a boolean value.
84
+ */
85
+ has(node: T | HeapItem<T>): boolean;
71
86
  /**
72
87
  * The `toArray` function returns an array of `HeapItem<T>` objects.
73
- * @returns An array of HeapItem<T> objects.Returns a sorted list of elements
88
+ * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
74
89
  */
75
90
  toArray(): HeapItem<T>[];
76
91
  /**
@@ -1,6 +1,36 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Heap = void 0;
3
+ exports.Heap = exports.HeapItem = void 0;
4
+ var HeapItem = /** @class */ (function () {
5
+ function HeapItem(priority, val) {
6
+ if (priority === void 0) { priority = NaN; }
7
+ if (val === void 0) { val = null; }
8
+ this._val = val;
9
+ this._priority = priority;
10
+ }
11
+ Object.defineProperty(HeapItem.prototype, "priority", {
12
+ get: function () {
13
+ return this._priority;
14
+ },
15
+ set: function (value) {
16
+ this._priority = value;
17
+ },
18
+ enumerable: false,
19
+ configurable: true
20
+ });
21
+ Object.defineProperty(HeapItem.prototype, "val", {
22
+ get: function () {
23
+ return this._val;
24
+ },
25
+ set: function (value) {
26
+ this._val = value;
27
+ },
28
+ enumerable: false,
29
+ configurable: true
30
+ });
31
+ return HeapItem;
32
+ }());
33
+ exports.HeapItem = HeapItem;
4
34
  var Heap = /** @class */ (function () {
5
35
  /**
6
36
  * The function is a constructor for a class that initializes a priority callback function based on the
@@ -77,31 +107,31 @@ var Heap = /** @class */ (function () {
77
107
  };
78
108
  /**
79
109
  * The `peek` function returns the top item in the priority queue without removing it.
80
- * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an element with the highest priority in the queue
110
+ * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
81
111
  */
82
112
  Heap.prototype.peek = function () {
83
113
  return this._pq.peek();
84
114
  };
85
115
  /**
86
116
  * The `peekLast` function returns the last item in the heap.
87
- * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an element with the lowest priority in the queue
117
+ * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
88
118
  */
89
119
  Heap.prototype.peekLast = function () {
90
120
  return this._pq.leaf();
91
121
  };
92
122
  /**
93
- * The `add` function adds an element to a priority queue with an optional priority value.
94
- * @param {T} element - The `element` parameter represents the value that you want to add to the heap. It can be of any
123
+ * The `add` function adds an val to a priority queue with an optional priority value.
124
+ * @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
95
125
  * type.
96
126
  * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
97
- * element being added to the heap. If the `element` parameter is a number, then the `priority` parameter is set to
98
- * the value of `element`. If the `element` parameter is not a number, then the
127
+ * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
128
+ * the value of `val`. If the `val` parameter is not a number, then the
99
129
  * @returns The `add` method returns the instance of the `Heap` class.
100
130
  * @throws {Error} if priority is not a valid number
101
131
  */
102
- Heap.prototype.add = function (element, priority) {
103
- if (typeof element === 'number') {
104
- priority = element;
132
+ Heap.prototype.add = function (val, priority) {
133
+ if (typeof val === 'number') {
134
+ priority = val;
105
135
  }
106
136
  else {
107
137
  if (priority === undefined) {
@@ -111,16 +141,16 @@ var Heap = /** @class */ (function () {
111
141
  if (priority && Number.isNaN(+priority)) {
112
142
  throw new Error('.add expects a numeric priority');
113
143
  }
114
- if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(element))) {
144
+ if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(val))) {
115
145
  throw new Error('.add expects a numeric priority '
116
146
  + 'or a constructor callback that returns a number');
117
147
  }
118
- var _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(element);
119
- this._pq.add({ priority: _priority, element: element });
148
+ var _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(val);
149
+ this._pq.add(new HeapItem(_priority, val));
120
150
  return this;
121
151
  };
122
152
  /**
123
- * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an element with the highest priority in the queue
153
+ * 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
124
154
  * @returns either a HeapItem<T> object or null.
125
155
  */
126
156
  Heap.prototype.poll = function () {
@@ -130,9 +160,24 @@ var Heap = /** @class */ (function () {
130
160
  }
131
161
  return top;
132
162
  };
163
+ /**
164
+ * The function checks if a given node or value exists in the priority queue.
165
+ * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
166
+ * @returns a boolean value.
167
+ */
168
+ Heap.prototype.has = function (node) {
169
+ if (node instanceof (HeapItem)) {
170
+ return this.getPq().getNodes().includes(node);
171
+ }
172
+ else {
173
+ return this.getPq().getNodes().findIndex(function (item) {
174
+ return item.val === node;
175
+ }) !== -1;
176
+ }
177
+ };
133
178
  /**
134
179
  * The `toArray` function returns an array of `HeapItem<T>` objects.
135
- * @returns An array of HeapItem<T> objects.Returns a sorted list of elements
180
+ * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
136
181
  */
137
182
  Heap.prototype.toArray = function () {
138
183
  return this._pq.toArray();
@@ -5,14 +5,14 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { Heap } from './heap';
8
+ import { Heap, HeapItem } from './heap';
9
9
  import { PriorityQueue } from '../priority-queue';
10
- import type { HeapItem, HeapOptions } from '../types';
10
+ import type { HeapOptions } from '../types';
11
11
  /**
12
12
  * @class MaxHeap
13
13
  * @extends Heap
14
14
  */
15
- export declare class MaxHeap<T> extends Heap<T> {
15
+ export declare class MaxHeap<T = number> extends Heap<T> {
16
16
  protected _pq: PriorityQueue<HeapItem<T>>;
17
17
  /**
18
18
  * The constructor initializes a PriorityQueue with a custom comparator function.
@@ -5,14 +5,14 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { Heap } from './heap';
8
+ import { Heap, HeapItem } from './heap';
9
9
  import { PriorityQueue } from '../priority-queue';
10
- import type { HeapItem, HeapOptions } from '../types';
10
+ import type { HeapOptions } from '../types';
11
11
  /**
12
12
  * @class MinHeap
13
13
  * @extends Heap
14
14
  */
15
- export declare class MinHeap<T> extends Heap<T> {
15
+ export declare class MinHeap<T = number> extends Heap<T> {
16
16
  protected _pq: PriorityQueue<HeapItem<T>>;
17
17
  /**
18
18
  * The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
@@ -4,6 +4,7 @@ export * from './stack';
4
4
  export * from './queue';
5
5
  export * from './graph';
6
6
  export * from './binary-tree';
7
+ export * from './tree';
7
8
  export * from './heap';
8
9
  export * from './priority-queue';
9
10
  export * from './matrix';
@@ -20,6 +20,7 @@ __exportStar(require("./stack"), exports);
20
20
  __exportStar(require("./queue"), exports);
21
21
  __exportStar(require("./graph"), exports);
22
22
  __exportStar(require("./binary-tree"), exports);
23
+ __exportStar(require("./tree"), exports);
23
24
  __exportStar(require("./heap"), exports);
24
25
  __exportStar(require("./priority-queue"), exports);
25
26
  __exportStar(require("./matrix"), exports);
@@ -8,9 +8,8 @@
8
8
  import { PriorityQueue } from './priority-queue';
9
9
  import type { PriorityQueueOptions } from '../types';
10
10
  export declare class MaxPriorityQueue<T = number> extends PriorityQueue<T> {
11
- /**
12
- * The constructor initializes a PriorityQueue with optional nodes and a comparator function.
13
- * @param [options] - An optional object that contains the following properties:
14
- */
15
- constructor(options?: PriorityQueueOptions<T>);
11
+ constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>);
12
+ constructor(options: PriorityQueueOptions<T>);
13
+ static heapify<T extends number>(options?: Omit<PriorityQueueOptions<T>, 'comparator'>): MaxPriorityQueue<T>;
14
+ static heapify<T>(options: PriorityQueueOptions<T>): MaxPriorityQueue<T>;
16
15
  }
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
17
28
  Object.defineProperty(exports, "__esModule", { value: true });
18
29
  exports.MaxPriorityQueue = void 0;
19
30
  /**
@@ -27,17 +38,30 @@ var priority_queue_1 = require("./priority-queue");
27
38
  var MaxPriorityQueue = /** @class */ (function (_super) {
28
39
  __extends(MaxPriorityQueue, _super);
29
40
  /**
30
- * The constructor initializes a PriorityQueue with optional nodes and a comparator function.
31
- * @param [options] - An optional object that contains the following properties:
41
+ * The constructor initializes a priority queue with an optional comparator function.
42
+ * @param [options] - The `options` parameter is an optional object that can contain various properties to configure
43
+ * the priority queue.
32
44
  */
33
45
  function MaxPriorityQueue(options) {
34
- return _super.call(this, {
35
- nodes: options === null || options === void 0 ? void 0 : options.nodes, comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
46
+ return _super.call(this, __assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
36
47
  var aKey = a, bKey = b;
37
48
  return bKey - aKey;
38
- }
39
- }) || this;
49
+ } })) || this;
40
50
  }
51
+ /**
52
+ * The function `heapify` creates a max priority queue from the given options and returns it.
53
+ * @param options - The `options` parameter is an object that contains configuration options for creating a priority
54
+ * queue. It can have the following properties:
55
+ * @returns a MaxPriorityQueue object.
56
+ */
57
+ MaxPriorityQueue.heapify = function (options) {
58
+ var maxPQ = new MaxPriorityQueue(__assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
59
+ var aKey = a, bKey = b;
60
+ return bKey - aKey;
61
+ } }));
62
+ maxPQ._fix();
63
+ return maxPQ;
64
+ };
41
65
  return MaxPriorityQueue;
42
66
  }(priority_queue_1.PriorityQueue));
43
67
  exports.MaxPriorityQueue = MaxPriorityQueue;
@@ -8,9 +8,8 @@
8
8
  import { PriorityQueue } from './priority-queue';
9
9
  import type { PriorityQueueOptions } from '../types';
10
10
  export declare class MinPriorityQueue<T = number> extends PriorityQueue<T> {
11
- /**
12
- * The constructor initializes a PriorityQueue with optional nodes and a comparator function.
13
- * @param [options] - An optional object that contains the following properties:
14
- */
15
- constructor(options?: PriorityQueueOptions<T>);
11
+ constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>);
12
+ constructor(options: PriorityQueueOptions<T>);
13
+ static heapify<T extends number>(options?: Omit<PriorityQueueOptions<T>, 'comparator'>): MinPriorityQueue<T>;
14
+ static heapify<T>(options: PriorityQueueOptions<T>): MinPriorityQueue<T>;
16
15
  }
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
17
28
  Object.defineProperty(exports, "__esModule", { value: true });
18
29
  exports.MinPriorityQueue = void 0;
19
30
  /**
@@ -27,17 +38,31 @@ var priority_queue_1 = require("./priority-queue");
27
38
  var MinPriorityQueue = /** @class */ (function (_super) {
28
39
  __extends(MinPriorityQueue, _super);
29
40
  /**
30
- * The constructor initializes a PriorityQueue with optional nodes and a comparator function.
31
- * @param [options] - An optional object that contains the following properties:
41
+ * The constructor initializes a priority queue with an optional comparator function.
42
+ * @param [options] - The `options` parameter is an optional object that can contain various configuration options for
43
+ * the `PriorityQueue` constructor.
32
44
  */
33
45
  function MinPriorityQueue(options) {
34
- return _super.call(this, {
35
- nodes: options === null || options === void 0 ? void 0 : options.nodes, comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
46
+ return _super.call(this, __assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
36
47
  var aKey = a, bKey = b;
37
48
  return aKey - bKey;
38
- }
39
- }) || this;
49
+ } })) || this;
40
50
  }
51
+ /**
52
+ * The function `heapify` creates a new MinPriorityQueue instance and sets the comparator function based on the options
53
+ * provided, and then fixes the heap structure of the queue.
54
+ * @param options - The `options` parameter is an object that contains configuration options for creating a priority
55
+ * queue. It can have the following properties:
56
+ * @returns a MinPriorityQueue object.
57
+ */
58
+ MinPriorityQueue.heapify = function (options) {
59
+ var minPQ = new MinPriorityQueue(__assign(__assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : function (a, b) {
60
+ var aKey = a, bKey = b;
61
+ return aKey - bKey;
62
+ } }));
63
+ minPQ._fix();
64
+ return minPQ;
65
+ };
41
66
  return MinPriorityQueue;
42
67
  }(priority_queue_1.PriorityQueue));
43
68
  exports.MinPriorityQueue = MinPriorityQueue;
@@ -43,6 +43,13 @@ export declare class PriorityQueue<T = number> {
43
43
  * that needs to be added to the heap.
44
44
  */
45
45
  add(node: T): void;
46
+ /**
47
+ * The "has" function checks if a given node is present in the list of nodes.
48
+ * @param {T} node - The parameter `node` is of type `T`, which means it can be any type. It represents the node that
49
+ * we want to check if it exists in the `nodes` array.
50
+ * @returns a boolean value indicating whether the given node is included in the array of nodes.
51
+ */
52
+ has(node: T): boolean;
46
53
  /**
47
54
  * The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
48
55
  * @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
@@ -89,8 +96,12 @@ export declare class PriorityQueue<T = number> {
89
96
  */
90
97
  isValid(): boolean;
91
98
  /**
92
- * The function sorts the elements in a data structure and returns them in ascending order.
93
- * @returns The `sort()` function is returning an array of type `T[]`.
99
+ * Plan to support sorting of duplicate elements.
100
+ */
101
+ /**
102
+ * The function sorts the elements in a data structure and returns them in an array.
103
+ * Plan to support sorting of duplicate elements.
104
+ * @returns The `sort()` method is returning an array of type `T[]`.
94
105
  */
95
106
  sort(): T[];
96
107
  /**
@@ -51,9 +51,9 @@ var PriorityQueue = /** @class */ (function () {
51
51
  };
52
52
  var nodes = options.nodes, comparator = options.comparator, _a = options.isFix, isFix = _a === void 0 ? true : _a;
53
53
  this._comparator = comparator;
54
- if (nodes && nodes instanceof Array && nodes.length > 0) {
54
+ if (nodes && Array.isArray(nodes) && nodes.length > 0) {
55
55
  // TODO support distinct
56
- this._nodes = Array.isArray(nodes) ? __spreadArray([], __read(nodes), false) : [];
56
+ this._nodes = __spreadArray([], __read(nodes), false);
57
57
  isFix && this._fix();
58
58
  }
59
59
  }
@@ -94,7 +94,7 @@ var PriorityQueue = /** @class */ (function () {
94
94
  * @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
95
95
  */
96
96
  PriorityQueue.isPriorityQueueified = function (options) {
97
- return new PriorityQueue(__assign(__assign({}, options), { isFix: true })).isValid();
97
+ return new PriorityQueue(__assign(__assign({}, options), { isFix: false })).isValid();
98
98
  };
99
99
  /**
100
100
  * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
@@ -111,6 +111,15 @@ var PriorityQueue = /** @class */ (function () {
111
111
  this.nodes.push(node);
112
112
  this._heapifyUp(this.size - 1);
113
113
  };
114
+ /**
115
+ * The "has" function checks if a given node is present in the list of nodes.
116
+ * @param {T} node - The parameter `node` is of type `T`, which means it can be any type. It represents the node that
117
+ * we want to check if it exists in the `nodes` array.
118
+ * @returns a boolean value indicating whether the given node is included in the array of nodes.
119
+ */
120
+ PriorityQueue.prototype.has = function (node) {
121
+ return this.nodes.includes(node);
122
+ };
114
123
  /**
115
124
  * The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
116
125
  * @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
@@ -182,31 +191,28 @@ var PriorityQueue = /** @class */ (function () {
182
191
  * @returns The function `isValid()` returns a boolean value.
183
192
  */
184
193
  PriorityQueue.prototype.isValid = function () {
185
- var _this = this;
186
- var isValidRecursive = function (parentIndex) {
187
- var isValidLeft = true;
188
- var isValidRight = true;
189
- if (_this._getLeft(parentIndex) !== -1) {
190
- var leftChildIndex = (parentIndex * 2) + 1;
191
- if (!_this._compare(parentIndex, leftChildIndex))
192
- return false;
193
- isValidLeft = isValidRecursive(leftChildIndex);
194
+ for (var i = 0; i < this.nodes.length; i++) {
195
+ var leftChildIndex = this._getLeft(i);
196
+ var rightChildIndex = this._getRight(i);
197
+ if (this._isValidIndex(leftChildIndex) && !this._compare(leftChildIndex, i)) {
198
+ return false;
194
199
  }
195
- if (_this._getRight(parentIndex) !== -1) {
196
- var rightChildIndex = (parentIndex * 2) + 2;
197
- if (!_this._compare(parentIndex, rightChildIndex))
198
- return false;
199
- isValidRight = isValidRecursive(rightChildIndex);
200
+ if (this._isValidIndex(rightChildIndex) && !this._compare(rightChildIndex, i)) {
201
+ return false;
200
202
  }
201
- return isValidLeft && isValidRight;
202
- };
203
- return isValidRecursive(0);
203
+ }
204
+ return true;
204
205
  };
205
206
  /**
206
- * The function sorts the elements in a data structure and returns them in ascending order.
207
- * @returns The `sort()` function is returning an array of type `T[]`.
207
+ * Plan to support sorting of duplicate elements.
208
+ */
209
+ /**
210
+ * The function sorts the elements in a data structure and returns them in an array.
211
+ * Plan to support sorting of duplicate elements.
212
+ * @returns The `sort()` method is returning an array of type `T[]`.
208
213
  */
209
214
  PriorityQueue.prototype.sort = function () {
215
+ // TODO Plan to support sorting of duplicate elements.
210
216
  var visitedNode = [];
211
217
  while (this.size !== 0) {
212
218
  var top = this.poll();
@@ -0,0 +1 @@
1
+ export * from './tree';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./tree"), exports);