data-structure-typed 1.18.7 → 1.18.8

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 (260) hide show
  1. package/README.md +154 -366
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +29 -32
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.js +179 -136
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +4 -4
  5. package/dist/data-structures/binary-tree/avl-tree.js +2 -2
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +3 -3
  7. package/dist/data-structures/binary-tree/binary-tree.js +4 -4
  8. package/dist/data-structures/binary-tree/bst.d.ts +13 -25
  9. package/dist/data-structures/binary-tree/bst.js +20 -74
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +6 -6
  11. package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
  12. package/dist/data-structures/binary-tree/tree-multiset.js +7 -14
  13. package/dist/data-structures/graph/abstract-graph.d.ts +11 -9
  14. package/dist/data-structures/graph/abstract-graph.js +39 -22
  15. package/dist/data-structures/graph/directed-graph.d.ts +17 -15
  16. package/dist/data-structures/graph/directed-graph.js +47 -42
  17. package/dist/data-structures/graph/undirected-graph.d.ts +7 -7
  18. package/dist/data-structures/graph/undirected-graph.js +48 -48
  19. package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +2 -3
  20. package/dist/data-structures/interfaces/abstract-graph.d.ts +2 -4
  21. package/dist/data-structures/interfaces/avl-tree.d.ts +1 -1
  22. package/dist/data-structures/interfaces/bst.d.ts +2 -2
  23. package/dist/data-structures/interfaces/rb-tree.d.ts +2 -2
  24. package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
  25. package/dist/data-structures/tree/tree.d.ts +1 -4
  26. package/dist/data-structures/tree/tree.js +1 -12
  27. package/dist/data-structures/types/abstract-binary-tree.d.ts +9 -5
  28. package/dist/data-structures/types/abstract-binary-tree.js +9 -5
  29. package/dist/data-structures/types/avl-tree.d.ts +1 -1
  30. package/dist/data-structures/types/binary-tree.d.ts +1 -1
  31. package/dist/data-structures/types/bst.d.ts +1 -1
  32. package/dist/data-structures/types/bst.js +1 -1
  33. package/dist/data-structures/types/directed-graph.js +1 -1
  34. package/dist/data-structures/types/helpers.d.ts +1 -8
  35. package/dist/data-structures/types/rb-tree.d.ts +2 -0
  36. package/dist/data-structures/types/rb-tree.js +1 -1
  37. package/dist/data-structures/types/tree-multiset.d.ts +4 -4
  38. package/dist/utils/types/utils.d.ts +18 -0
  39. package/dist/utils/utils.d.ts +6 -1
  40. package/dist/utils/utils.js +21 -1
  41. package/package.json +2 -2
  42. package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
  43. package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
  44. package/backup/recursive-type/src/assets/logo.png +0 -0
  45. package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
  46. package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
  47. package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
  48. package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
  49. package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
  50. package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
  51. package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
  52. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  53. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  54. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  55. package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
  56. package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
  57. package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
  58. package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
  59. package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
  60. package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  61. package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
  62. package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
  63. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  64. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  65. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  66. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  67. package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  68. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  69. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  70. package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  71. package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
  72. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  73. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  74. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  75. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  76. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  77. package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
  78. package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
  79. package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
  80. package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
  81. package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
  82. package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
  83. package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
  84. package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
  85. package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
  86. package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
  87. package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
  88. package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
  89. package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
  90. package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
  91. package/backup/recursive-type/src/data-structures/index.ts +0 -15
  92. package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
  93. package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
  94. package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
  95. package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
  96. package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
  97. package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
  98. package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
  99. package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
  100. package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
  101. package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
  102. package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
  103. package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
  104. package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
  105. package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
  106. package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
  107. package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
  108. package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
  109. package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
  110. package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
  111. package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
  112. package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
  113. package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
  114. package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
  115. package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
  116. package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
  117. package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
  118. package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
  119. package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
  120. package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
  121. package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
  122. package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
  123. package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
  124. package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
  125. package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
  126. package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
  127. package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
  128. package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
  129. package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
  130. package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
  131. package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
  132. package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
  133. package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
  134. package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
  135. package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
  136. package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
  137. package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
  138. package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
  139. package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
  140. package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
  141. package/backup/recursive-type/src/index.ts +0 -1
  142. package/backup/recursive-type/src/utils/index.ts +0 -2
  143. package/backup/recursive-type/src/utils/types/index.ts +0 -1
  144. package/backup/recursive-type/src/utils/types/utils.ts +0 -6
  145. package/backup/recursive-type/src/utils/utils.ts +0 -78
  146. package/docs/.nojekyll +0 -1
  147. package/docs/assets/highlight.css +0 -92
  148. package/docs/assets/main.js +0 -58
  149. package/docs/assets/search.js +0 -1
  150. package/docs/assets/style.css +0 -1367
  151. package/docs/classes/AVLTree.html +0 -2451
  152. package/docs/classes/AVLTreeNode.html +0 -499
  153. package/docs/classes/AaTree.html +0 -172
  154. package/docs/classes/AbstractBinaryTree.html +0 -2118
  155. package/docs/classes/AbstractBinaryTreeNode.html +0 -524
  156. package/docs/classes/AbstractEdge.html +0 -295
  157. package/docs/classes/AbstractGraph.html +0 -1043
  158. package/docs/classes/AbstractVertex.html +0 -258
  159. package/docs/classes/ArrayDeque.html +0 -439
  160. package/docs/classes/BST.html +0 -2297
  161. package/docs/classes/BSTNode.html +0 -503
  162. package/docs/classes/BTree.html +0 -172
  163. package/docs/classes/BinaryIndexedTree.html +0 -341
  164. package/docs/classes/BinaryTree.html +0 -2133
  165. package/docs/classes/BinaryTreeNode.html +0 -501
  166. package/docs/classes/Character.html +0 -220
  167. package/docs/classes/CoordinateMap.html +0 -483
  168. package/docs/classes/CoordinateSet.html +0 -444
  169. package/docs/classes/Deque.html +0 -975
  170. package/docs/classes/DirectedEdge.html +0 -366
  171. package/docs/classes/DirectedGraph.html +0 -1443
  172. package/docs/classes/DirectedVertex.html +0 -254
  173. package/docs/classes/DoublyLinkedList.html +0 -968
  174. package/docs/classes/DoublyLinkedListNode.html +0 -297
  175. package/docs/classes/HashTable.html +0 -172
  176. package/docs/classes/Heap.html +0 -423
  177. package/docs/classes/HeapItem.html +0 -255
  178. package/docs/classes/Matrix2D.html +0 -502
  179. package/docs/classes/MatrixNTI2D.html +0 -240
  180. package/docs/classes/MaxHeap.html +0 -436
  181. package/docs/classes/MaxPriorityQueue.html +0 -836
  182. package/docs/classes/MinHeap.html +0 -437
  183. package/docs/classes/MinPriorityQueue.html +0 -838
  184. package/docs/classes/Navigator.html +0 -313
  185. package/docs/classes/ObjectDeque.html +0 -455
  186. package/docs/classes/Pair.html +0 -172
  187. package/docs/classes/PriorityQueue.html +0 -760
  188. package/docs/classes/Queue.html +0 -392
  189. package/docs/classes/RBTree.html +0 -2388
  190. package/docs/classes/RBTreeNode.html +0 -516
  191. package/docs/classes/SegmentTree.html +0 -434
  192. package/docs/classes/SegmentTreeNode.html +0 -357
  193. package/docs/classes/SinglyLinkedList.html +0 -788
  194. package/docs/classes/SinglyLinkedListNode.html +0 -270
  195. package/docs/classes/SkipLinkedList.html +0 -172
  196. package/docs/classes/SplayTree.html +0 -172
  197. package/docs/classes/Stack.html +0 -368
  198. package/docs/classes/TreeMap.html +0 -172
  199. package/docs/classes/TreeMultiSet.html +0 -2297
  200. package/docs/classes/TreeMultiSetNode.html +0 -499
  201. package/docs/classes/TreeNode.html +0 -343
  202. package/docs/classes/TreeSet.html +0 -172
  203. package/docs/classes/Trie.html +0 -372
  204. package/docs/classes/TrieNode.html +0 -280
  205. package/docs/classes/TwoThreeTree.html +0 -172
  206. package/docs/classes/UndirectedEdge.html +0 -337
  207. package/docs/classes/UndirectedGraph.html +0 -1210
  208. package/docs/classes/UndirectedVertex.html +0 -254
  209. package/docs/classes/Vector2D.html +0 -805
  210. package/docs/enums/CP.html +0 -181
  211. package/docs/enums/FamilyPosition.html +0 -181
  212. package/docs/enums/LoopType.html +0 -182
  213. package/docs/enums/RBColor.html +0 -174
  214. package/docs/enums/TopologicalProperty.html +0 -181
  215. package/docs/index.html +0 -645
  216. package/docs/interfaces/IAVLTree.html +0 -1378
  217. package/docs/interfaces/IAVLTreeNode.html +0 -405
  218. package/docs/interfaces/IAbstractBinaryTree.html +0 -1124
  219. package/docs/interfaces/IAbstractBinaryTreeNode.html +0 -384
  220. package/docs/interfaces/IAbstractGraph.html +0 -433
  221. package/docs/interfaces/IBST.html +0 -1271
  222. package/docs/interfaces/IBSTNode.html +0 -408
  223. package/docs/interfaces/IDirectedGraph.html +0 -572
  224. package/docs/interfaces/IUNDirectedGraph.html +0 -465
  225. package/docs/modules.html +0 -262
  226. package/docs/types/AVLTreeOptions.html +0 -150
  227. package/docs/types/AbstractBinaryTreeNodeProperties.html +0 -152
  228. package/docs/types/AbstractBinaryTreeNodeProperty.html +0 -152
  229. package/docs/types/AbstractBinaryTreeOptions.html +0 -156
  230. package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -152
  231. package/docs/types/BSTComparator.html +0 -162
  232. package/docs/types/BSTOptions.html +0 -152
  233. package/docs/types/BinaryTreeDeletedResult.html +0 -159
  234. package/docs/types/BinaryTreeNodeId.html +0 -147
  235. package/docs/types/BinaryTreeNodePropertyName.html +0 -147
  236. package/docs/types/BinaryTreeOptions.html +0 -150
  237. package/docs/types/DFSOrderPattern.html +0 -147
  238. package/docs/types/DijkstraResult.html +0 -167
  239. package/docs/types/Direction.html +0 -147
  240. package/docs/types/EdgeId.html +0 -147
  241. package/docs/types/HeapOptions.html +0 -168
  242. package/docs/types/IdObject.html +0 -157
  243. package/docs/types/KeyValObject.html +0 -152
  244. package/docs/types/NavigatorParams.html +0 -181
  245. package/docs/types/NodeOrPropertyName.html +0 -147
  246. package/docs/types/PriorityQueueComparator.html +0 -167
  247. package/docs/types/PriorityQueueDFSOrderPattern.html +0 -147
  248. package/docs/types/PriorityQueueOptions.html +0 -161
  249. package/docs/types/RBTreeOptions.html +0 -150
  250. package/docs/types/RecursiveAVLTreeNode.html +0 -152
  251. package/docs/types/RecursiveBSTNode.html +0 -152
  252. package/docs/types/RecursiveBinaryTreeNode.html +0 -152
  253. package/docs/types/RecursiveTreeMultiSetNode.html +0 -152
  254. package/docs/types/SegmentTreeNodeVal.html +0 -147
  255. package/docs/types/TopologicalStatus.html +0 -147
  256. package/docs/types/TreeMultiSetOptions.html +0 -152
  257. package/docs/types/Turning.html +0 -147
  258. package/docs/types/VertexId.html +0 -147
  259. package/notes/bst.test.ts +0 -181
  260. package/notes/note.md +0 -34
@@ -1,49 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import {PriorityQueue} from './priority-queue';
9
- import type {PriorityQueueOptions} from '../types';
10
- import {SpecifyOptional} from '../../utils';
11
-
12
- export class MaxPriorityQueue<T = number> extends PriorityQueue<T> {
13
- constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>)
14
- constructor(options: PriorityQueueOptions<T>)
15
- /**
16
- * The constructor initializes a priority queue with an optional comparator function.
17
- * @param [options] - The `options` parameter is an optional object that can contain various properties to configure
18
- * the priority queue.
19
- */
20
- constructor(options?: SpecifyOptional<PriorityQueueOptions<T>, 'comparator'>) {
21
- super({
22
- ...options,
23
- comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
24
- const aKey = a as unknown as number, bKey = b as unknown as number;
25
- return bKey - aKey;
26
- }
27
- });
28
- }
29
-
30
- static override heapify<T extends number>(options?: Omit<PriorityQueueOptions<T>, 'comparator'>): MaxPriorityQueue<T>
31
- static override heapify<T>(options: PriorityQueueOptions<T>): MaxPriorityQueue<T>
32
- /**
33
- * The function `heapify` creates a max priority queue from the given options and returns it.
34
- * @param options - The `options` parameter is an object that contains configuration options for creating a priority
35
- * queue. It can have the following properties:
36
- * @returns a MaxPriorityQueue object.
37
- */
38
- static override heapify<T>(options: PriorityQueueOptions<T>): MaxPriorityQueue<T> {
39
- const maxPQ = new MaxPriorityQueue<T>({
40
- ...options,
41
- comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
42
- const aKey = a as unknown as number, bKey = b as unknown as number;
43
- return bKey - aKey;
44
- }
45
- });
46
- maxPQ._fix();
47
- return maxPQ;
48
- }
49
- }
@@ -1,50 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import {PriorityQueue} from './priority-queue';
9
- import type {PriorityQueueOptions} from '../types';
10
- import {SpecifyOptional} from '../../utils';
11
-
12
- export class MinPriorityQueue<T = number> extends PriorityQueue<T> {
13
- constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>)
14
- constructor(options: PriorityQueueOptions<T>)
15
- /**
16
- * The constructor initializes a priority queue with an optional comparator function.
17
- * @param [options] - The `options` parameter is an optional object that can contain various configuration options for
18
- * the `PriorityQueue` constructor.
19
- */
20
- constructor(options?: SpecifyOptional<PriorityQueueOptions<T>, 'comparator'>) {
21
- super({
22
- ...options,
23
- comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
24
- const aKey = a as unknown as number, bKey = b as unknown as number;
25
- return aKey - bKey;
26
- }
27
- });
28
- }
29
-
30
- static override heapify<T extends number>(options?: Omit<PriorityQueueOptions<T>, 'comparator'>): MinPriorityQueue<T>
31
- static override heapify<T>(options: PriorityQueueOptions<T>): MinPriorityQueue<T>
32
- /**
33
- * The function `heapify` creates a new MinPriorityQueue instance and sets the comparator function based on the options
34
- * provided, and then fixes the heap structure of the queue.
35
- * @param options - The `options` parameter is an object that contains configuration options for creating a priority
36
- * queue. It can have the following properties:
37
- * @returns a MinPriorityQueue object.
38
- */
39
- static override heapify<T>(options: PriorityQueueOptions<T>): MinPriorityQueue<T> {
40
- const minPQ = new MinPriorityQueue<T>({
41
- ...options,
42
- comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
43
- const aKey = a as unknown as number, bKey = b as unknown as number;
44
- return aKey - bKey;
45
- }
46
- });
47
- minPQ._fix();
48
- return minPQ;
49
- }
50
- }
@@ -1,354 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions} from '../types';
9
-
10
- export class PriorityQueue<T = number> {
11
- /**
12
- * The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
13
- * function.
14
- * @param options - The `options` parameter is an object that contains the following properties:
15
- */
16
- constructor(options: PriorityQueueOptions<T>) {
17
- const {nodes, comparator, isFix = true} = options;
18
- this._comparator = comparator;
19
-
20
- if (nodes && Array.isArray(nodes) && nodes.length > 0) {
21
- // TODO support distinct
22
- this._nodes = [...nodes];
23
- isFix && this._fix();
24
- }
25
- }
26
-
27
- protected _nodes: T[] = [];
28
-
29
- get nodes(): T[] {
30
- return this._nodes;
31
- }
32
-
33
- get size(): number {
34
- return this.nodes.length;
35
- }
36
-
37
- /**
38
- * The `heapify` function creates a new PriorityQueue instance and fixes the heap property.
39
- * @param options - The "options" parameter is an object that contains the configuration options for the PriorityQueue.
40
- * It can include properties such as "comparator" which specifies the comparison function used to order the elements in
41
- * the priority queue, and "initialValues" which is an array of initial values to be added to the priority
42
- * @returns a new instance of the PriorityQueue class after performing the heapify operation on it.
43
- */
44
- static heapify<T>(options: PriorityQueueOptions<T>) {
45
- const heap = new PriorityQueue(options);
46
- heap._fix();
47
- return heap;
48
- }
49
-
50
- /**
51
- * The function checks if a priority queue is valid by creating a new priority queue with a fix option and then calling
52
- * the isValid method.
53
- * @param options - An object containing options for creating a priority queue. The options object should have the
54
- * following properties:
55
- * @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
56
- */
57
- static isPriorityQueueified<T>(options: Omit<PriorityQueueOptions<T>, 'isFix'>) {
58
- return new PriorityQueue({...options, isFix: false}).isValid();
59
- }
60
-
61
- /**
62
- * 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.
63
- */
64
- getNodes(): T[] {
65
- return this._nodes;
66
- }
67
-
68
- /**
69
- * The "add" function adds a node to the heap and ensures that the heap property is maintained.
70
- * @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
71
- * that needs to be added to the heap.
72
- */
73
- add(node: T) {
74
- this.nodes.push(node);
75
- this._heapifyUp(this.size - 1);
76
- }
77
-
78
- /**
79
- * The "has" function checks if a given node is present in the list of nodes.
80
- * @param {T} node - The parameter `node` is of type `T`, which means it can be any type. It represents the node that
81
- * we want to check if it exists in the `nodes` array.
82
- * @returns a boolean value indicating whether the given node is included in the array of nodes.
83
- */
84
- has(node: T): boolean {
85
- return this.nodes.includes(node);
86
- }
87
-
88
- /**
89
- * The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
90
- * @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
91
- * Otherwise, it returns `null`.
92
- */
93
- peek(): T | null {
94
- return this.size ? this.nodes[0] : null;
95
- }
96
-
97
- /**
98
- * The `poll` function removes and returns the top element from a heap data structure.
99
- * @returns The `poll()` method returns a value of type `T` or `null`.
100
- */
101
- poll(): T | null {
102
- let res: T | null = null;
103
- if (this.size > 1) {
104
- this._swap(0, this.nodes.length - 1);
105
- res = this.nodes.pop() ?? null;
106
- this._heapifyDown(0);
107
- } else if (this.size === 1) {
108
- res = this.nodes.pop() ?? null;
109
- }
110
- return res;
111
- }
112
-
113
- /**
114
- * The `leaf` function returns the last element in the `nodes` array or `null` if the array is empty.
115
- * @returns The method `leaf()` is returning the last element (`T`) in the `nodes` array if it exists. If the array is
116
- * empty or the last element is `null`, then it returns `null`.
117
- */
118
- leaf(): T | null {
119
- return this.nodes[this.size - 1] ?? null;
120
- }
121
-
122
- /**
123
- * The function checks if the size of an object is equal to zero and returns a boolean value indicating whether the
124
- * object is empty or not.
125
- * @returns The method `isEmpty()` is returning a boolean value indicating whether the size of the object is equal to
126
- * 0.
127
- */
128
- isEmpty() {
129
- return this.size === 0;
130
- }
131
-
132
- /**
133
- * The clear function clears the nodes array.
134
- */
135
- clear() {
136
- this._setNodes([]);
137
- }
138
-
139
- /**
140
- * The toArray function returns an array containing all the elements in the nodes property.
141
- * @returns An array of type T, which is the elements of the nodes property.
142
- */
143
- toArray(): T[] {
144
- return [...this.nodes];
145
- }
146
-
147
- /**
148
- * The `clone` function returns a new instance of the `PriorityQueue` class with the same nodes and comparator as the
149
- * original instance.
150
- * @returns The `clone()` method is returning a new instance of the `PriorityQueue` class with the same `nodes` and
151
- * `comparator` properties as the original instance.
152
- */
153
- clone(): PriorityQueue<T> {
154
- return new PriorityQueue<T>({nodes: this.nodes, comparator: this._comparator});
155
- }
156
-
157
- // --- start additional methods ---
158
- /**
159
- * The `isValid` function recursively checks if a binary tree satisfies a certain condition.
160
- * @returns The function `isValid()` returns a boolean value.
161
- */
162
- isValid(): boolean {
163
- for (let i = 0; i < this.nodes.length; i++) {
164
- const leftChildIndex = this._getLeft(i);
165
- const rightChildIndex = this._getRight(i);
166
- if (this._isValidIndex(leftChildIndex) && !this._compare(leftChildIndex, i)) {
167
- return false;
168
- }
169
- if (this._isValidIndex(rightChildIndex) && !this._compare(rightChildIndex, i)) {
170
- return false;
171
- }
172
- }
173
- return true;
174
- }
175
-
176
- /**
177
- * Plan to support sorting of duplicate elements.
178
- */
179
- /**
180
- * The function sorts the elements in a data structure and returns them in an array.
181
- * Plan to support sorting of duplicate elements.
182
- * @returns The `sort()` method is returning an array of type `T[]`.
183
- */
184
- sort(): T[] {
185
- // TODO Plan to support sorting of duplicate elements.
186
- const visitedNode: T[] = [];
187
- while (this.size !== 0) {
188
- const top = this.poll();
189
- if (top) visitedNode.push(top);
190
- }
191
- return visitedNode;
192
- }
193
-
194
- /**
195
- * The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
196
- * based on the specified traversal order.
197
- * @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which
198
- * the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
199
- * @returns an array of type `(T | null)[]`.
200
- */
201
- DFS(dfsMode: PriorityQueueDFSOrderPattern): (T | null)[] {
202
- const visitedNode: (T | null)[] = [];
203
-
204
- const traverse = (cur: number) => {
205
- const leftChildIndex = this._getLeft(cur);
206
- const rightChildIndex = this._getRight(cur);
207
- switch (dfsMode) {
208
- case 'in':
209
- this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
210
- visitedNode.push(this.nodes[cur] ?? null);
211
- this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
212
- break;
213
- case 'pre':
214
- visitedNode.push(this.nodes[cur] ?? null);
215
- this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
216
- this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
217
- break;
218
- case 'post':
219
- this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
220
- this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
221
- visitedNode.push(this.nodes[cur] ?? null);
222
- break;
223
- }
224
- };
225
-
226
- this._isValidIndex(0) && traverse(0);
227
- return visitedNode;
228
- }
229
-
230
- protected _setNodes(value: T[]) {
231
- this._nodes = value;
232
- }
233
-
234
- protected readonly _comparator: PriorityQueueComparator<T> = (a: T, b: T) => {
235
- const aKey = a as unknown as number, bKey = b as unknown as number;
236
- return aKey - bKey;
237
- };
238
-
239
- /**
240
- * The function compares two numbers using a custom comparator function.
241
- * @param {number} a - The parameter "a" is a number that represents the index of a node in an array.
242
- * @param {number} b - The parameter "b" is a number.
243
- * @returns the result of the comparison between the elements at indices `a` and `b` in the `nodes` array. The
244
- * comparison is done using the `_comparator` function, and if the result is greater than 0, `true` is returned,
245
- * indicating that the element at index `a` is greater than the element at index `b`.
246
- */
247
- protected _compare(a: number, b: number) {
248
- return this._comparator(this.nodes[a], this.nodes[b]) > 0;
249
- }
250
-
251
- /**
252
- * The function swaps two elements in an array.
253
- * @param {number} a - The parameter "a" is a number that represents the index of an element in an array.
254
- * @param {number} b - The parameter "b" is a number.
255
- */
256
- protected _swap(a: number, b: number) {
257
- const temp = this.nodes[a];
258
- this.nodes[a] = this.nodes[b];
259
- this.nodes[b] = temp;
260
- }
261
-
262
- /**
263
- * The function checks if a given index is valid within an array.
264
- * @param {number} index - The parameter "index" is of type number and represents the index value that needs to be
265
- * checked for validity.
266
- * @returns A boolean value indicating whether the given index is valid or not.
267
- */
268
- protected _isValidIndex(index: number): boolean {
269
- return index > -1 && index < this.nodes.length;
270
- }
271
-
272
- /**
273
- * The function returns the index of the parent node given the index of a child node in a binary tree.
274
- * @param {number} child - The "child" parameter is a number representing the index of a child node in a binary tree.
275
- * @returns the parent of the given child node.
276
- */
277
- protected _getParent(child: number): number {
278
- return Math.floor((child - 1) / 2);
279
- }
280
-
281
- /**
282
- * The function returns the index of the left child node in a binary tree given the index of its parent node.
283
- * @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
284
- * @returns the left child of a given parent node in a binary tree.
285
- */
286
- protected _getLeft(parent: number): number {
287
- return (2 * parent) + 1;
288
- }
289
-
290
- /**
291
- * The function returns the index of the right child node in a binary tree given the index of its parent node.
292
- * @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
293
- * @returns the right child of a given parent node in a binary tree.
294
- */
295
- protected _getRight(parent: number): number {
296
- return (2 * parent) + 2;
297
- }
298
-
299
- /**
300
- * The function returns the index of the smallest child node of a given parent node.
301
- * @param {number} parent - The parent parameter is a number that represents the index of the parent node in a binary
302
- * tree.
303
- * @returns the minimum value between the parent node and its left and right child nodes.
304
- */
305
- protected _getComparedChild(parent: number) {
306
- let min = parent;
307
- const left = this._getLeft(parent), right = this._getRight(parent);
308
-
309
- if (left < this.size && this._compare(min, left)) {
310
- min = left;
311
- }
312
- if (right < this.size && this._compare(min, right)) {
313
- min = right;
314
- }
315
- return min;
316
- }
317
-
318
- /**
319
- * The function `_heapifyUp` is used to maintain the heap property by moving an element up the heap until it is in the
320
- * correct position.
321
- * @param {number} start - The start parameter is the index of the element that needs to be moved up in the heap.
322
- */
323
- protected _heapifyUp(start: number) {
324
- while (start > 0 && this._compare(this._getParent(start), start)) {
325
- const parent = this._getParent(start);
326
- this._swap(start, parent);
327
- start = parent;
328
- }
329
- }
330
-
331
- /**
332
- * The function performs a heapify operation by comparing and swapping elements in a binary heap.
333
- * @param {number} start - The start parameter is the index of the element in the heap from where the heapifyDown
334
- * operation should start.
335
- */
336
- protected _heapifyDown(start: number) {
337
- let min = this._getComparedChild(start);
338
- while (this._compare(start, min)) {
339
- this._swap(min, start);
340
- start = min;
341
- min = this._getComparedChild(start);
342
- }
343
- }
344
-
345
- /**
346
- * The _fix function performs a heapify operation on the elements of the heap starting from the middle and moving
347
- * towards the root.
348
- */
349
- protected _fix() {
350
- for (let i = Math.floor(this.size / 2); i > -1; i--) this._heapifyDown(i);
351
- }
352
-
353
- // --- end additional methods ---
354
- }