data-structure-typed 1.31.0 → 1.32.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 (365) hide show
  1. package/.gitattributes +112 -0
  2. package/.github/workflows/ci.yml +12 -1
  3. package/.prettierrc.js +1 -1
  4. package/CHANGELOG.md +1 -1
  5. package/CODE-OF-CONDUCT.md +80 -0
  6. package/COMMANDS.md +28 -0
  7. package/README.md +20 -3
  8. package/SECURITY.md +15 -0
  9. package/coverage/clover.xml +3393 -0
  10. package/coverage/coverage-final.json +67 -0
  11. package/coverage/coverage-summary.json +68 -0
  12. package/coverage/lcov-report/base.css +224 -0
  13. package/coverage/lcov-report/block-navigation.js +87 -0
  14. package/coverage/lcov-report/favicon.png +0 -0
  15. package/coverage/lcov-report/index.html +386 -0
  16. package/coverage/lcov-report/prettify.css +1 -0
  17. package/coverage/lcov-report/prettify.js +2 -0
  18. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  19. package/coverage/lcov-report/sorter.js +196 -0
  20. package/coverage/lcov-report/src/data-structures/binary-tree/aa-tree.ts.html +88 -0
  21. package/coverage/lcov-report/src/data-structures/binary-tree/abstract-binary-tree.ts.html +4978 -0
  22. package/coverage/lcov-report/src/data-structures/binary-tree/avl-tree.ts.html +1015 -0
  23. package/coverage/lcov-report/src/data-structures/binary-tree/b-tree.ts.html +88 -0
  24. package/coverage/lcov-report/src/data-structures/binary-tree/binary-indexed-tree.ts.html +313 -0
  25. package/coverage/lcov-report/src/data-structures/binary-tree/binary-tree.ts.html +226 -0
  26. package/coverage/lcov-report/src/data-structures/binary-tree/bst.ts.html +1696 -0
  27. package/coverage/lcov-report/src/data-structures/binary-tree/index.html +296 -0
  28. package/coverage/lcov-report/src/data-structures/binary-tree/index.ts.html +121 -0
  29. package/coverage/lcov-report/src/data-structures/binary-tree/rb-tree.ts.html +388 -0
  30. package/coverage/lcov-report/src/data-structures/binary-tree/segment-tree.ts.html +811 -0
  31. package/coverage/lcov-report/src/data-structures/binary-tree/splay-tree.ts.html +88 -0
  32. package/coverage/lcov-report/src/data-structures/binary-tree/tree-multiset.ts.html +2185 -0
  33. package/coverage/lcov-report/src/data-structures/binary-tree/two-three-tree.ts.html +88 -0
  34. package/coverage/lcov-report/src/data-structures/graph/abstract-graph.ts.html +3205 -0
  35. package/coverage/lcov-report/src/data-structures/graph/directed-graph.ts.html +1495 -0
  36. package/coverage/lcov-report/src/data-structures/graph/index.html +176 -0
  37. package/coverage/lcov-report/src/data-structures/graph/index.ts.html +97 -0
  38. package/coverage/lcov-report/src/data-structures/graph/map-graph.ts.html +472 -0
  39. package/coverage/lcov-report/src/data-structures/graph/undirected-graph.ts.html +907 -0
  40. package/coverage/lcov-report/src/data-structures/hash/coordinate-map.ts.html +286 -0
  41. package/coverage/lcov-report/src/data-structures/hash/coordinate-set.ts.html +253 -0
  42. package/coverage/lcov-report/src/data-structures/hash/hash-table.ts.html +88 -0
  43. package/coverage/lcov-report/src/data-structures/hash/index.html +206 -0
  44. package/coverage/lcov-report/src/data-structures/hash/index.ts.html +103 -0
  45. package/coverage/lcov-report/src/data-structures/hash/pair.ts.html +88 -0
  46. package/coverage/lcov-report/src/data-structures/hash/tree-map.ts.html +88 -0
  47. package/coverage/lcov-report/src/data-structures/hash/tree-set.ts.html +88 -0
  48. package/coverage/lcov-report/src/data-structures/heap/heap.ts.html +721 -0
  49. package/coverage/lcov-report/src/data-structures/heap/index.html +161 -0
  50. package/coverage/lcov-report/src/data-structures/heap/index.ts.html +94 -0
  51. package/coverage/lcov-report/src/data-structures/heap/max-heap.ts.html +178 -0
  52. package/coverage/lcov-report/src/data-structures/heap/min-heap.ts.html +181 -0
  53. package/coverage/lcov-report/src/data-structures/index.html +116 -0
  54. package/coverage/lcov-report/src/data-structures/index.ts.html +118 -0
  55. package/coverage/lcov-report/src/data-structures/linked-list/doubly-linked-list.ts.html +1804 -0
  56. package/coverage/lcov-report/src/data-structures/linked-list/index.html +161 -0
  57. package/coverage/lcov-report/src/data-structures/linked-list/index.ts.html +94 -0
  58. package/coverage/lcov-report/src/data-structures/linked-list/singly-linked-list.ts.html +1588 -0
  59. package/coverage/lcov-report/src/data-structures/linked-list/skip-linked-list.ts.html +88 -0
  60. package/coverage/lcov-report/src/data-structures/matrix/index.html +176 -0
  61. package/coverage/lcov-report/src/data-structures/matrix/index.ts.html +97 -0
  62. package/coverage/lcov-report/src/data-structures/matrix/matrix.ts.html +166 -0
  63. package/coverage/lcov-report/src/data-structures/matrix/matrix2d.ts.html +721 -0
  64. package/coverage/lcov-report/src/data-structures/matrix/navigator.ts.html +448 -0
  65. package/coverage/lcov-report/src/data-structures/matrix/vector2d.ts.html +1033 -0
  66. package/coverage/lcov-report/src/data-structures/priority-queue/index.html +161 -0
  67. package/coverage/lcov-report/src/data-structures/priority-queue/index.ts.html +94 -0
  68. package/coverage/lcov-report/src/data-structures/priority-queue/max-priority-queue.ts.html +253 -0
  69. package/coverage/lcov-report/src/data-structures/priority-queue/min-priority-queue.ts.html +256 -0
  70. package/coverage/lcov-report/src/data-structures/priority-queue/priority-queue.ts.html +1162 -0
  71. package/coverage/lcov-report/src/data-structures/queue/deque.ts.html +976 -0
  72. package/coverage/lcov-report/src/data-structures/queue/index.html +146 -0
  73. package/coverage/lcov-report/src/data-structures/queue/index.ts.html +91 -0
  74. package/coverage/lcov-report/src/data-structures/queue/queue.ts.html +658 -0
  75. package/coverage/lcov-report/src/data-structures/stack/index.html +131 -0
  76. package/coverage/lcov-report/src/data-structures/stack/index.ts.html +88 -0
  77. package/coverage/lcov-report/src/data-structures/stack/stack.ts.html +379 -0
  78. package/coverage/lcov-report/src/data-structures/tree/index.html +131 -0
  79. package/coverage/lcov-report/src/data-structures/tree/index.ts.html +88 -0
  80. package/coverage/lcov-report/src/data-structures/tree/tree.ts.html +292 -0
  81. package/coverage/lcov-report/src/data-structures/trie/index.html +131 -0
  82. package/coverage/lcov-report/src/data-structures/trie/index.ts.html +88 -0
  83. package/coverage/lcov-report/src/data-structures/trie/trie.ts.html +760 -0
  84. package/coverage/lcov-report/src/index.html +116 -0
  85. package/coverage/lcov-report/src/index.ts.html +97 -0
  86. package/coverage/lcov-report/src/interfaces/index.html +116 -0
  87. package/coverage/lcov-report/src/interfaces/index.ts.html +130 -0
  88. package/coverage/lcov-report/src/types/data-structures/abstract-binary-tree.ts.html +235 -0
  89. package/coverage/lcov-report/src/types/data-structures/bst.ts.html +124 -0
  90. package/coverage/lcov-report/src/types/data-structures/directed-graph.ts.html +109 -0
  91. package/coverage/lcov-report/src/types/data-structures/index.html +176 -0
  92. package/coverage/lcov-report/src/types/data-structures/index.ts.html +130 -0
  93. package/coverage/lcov-report/src/types/data-structures/rb-tree.ts.html +106 -0
  94. package/coverage/lcov-report/src/types/index.html +116 -0
  95. package/coverage/lcov-report/src/types/index.ts.html +94 -0
  96. package/coverage/lcov-report/src/types/utils/index.html +116 -0
  97. package/coverage/lcov-report/src/types/utils/index.ts.html +91 -0
  98. package/coverage/lcov-report/src/utils/index.html +131 -0
  99. package/coverage/lcov-report/src/utils/index.ts.html +88 -0
  100. package/coverage/lcov-report/src/utils/utils.ts.html +322 -0
  101. package/coverage/lcov-report/test/utils/index.html +146 -0
  102. package/coverage/lcov-report/test/utils/index.ts.html +91 -0
  103. package/coverage/lcov-report/test/utils/magnitude.ts.html +148 -0
  104. package/coverage/lcov-report/test/utils/number.ts.html +94 -0
  105. package/coverage/lcov.info +6676 -0
  106. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  107. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  108. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  109. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  110. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
  111. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  112. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  113. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  114. package/dist/data-structures/graph/map-graph.js.map +1 -1
  115. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  116. package/dist/data-structures/heap/heap.js.map +1 -1
  117. package/dist/data-structures/heap/max-heap.js.map +1 -1
  118. package/dist/data-structures/heap/min-heap.js.map +1 -1
  119. package/dist/data-structures/matrix/matrix.js.map +1 -1
  120. package/dist/data-structures/matrix/navigator.js.map +1 -1
  121. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  122. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  123. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  124. package/dist/data-structures/queue/deque.js.map +1 -1
  125. package/dist/data-structures/queue/queue.js.map +1 -1
  126. package/dist/data-structures/tree/tree.js.map +1 -1
  127. package/dist/utils/utils.js.map +1 -1
  128. package/docs/classes/AVLTree.html +205 -282
  129. package/docs/classes/AVLTreeNode.html +15 -15
  130. package/docs/classes/AaTree.html +1 -1
  131. package/docs/classes/AbstractBinaryTree.html +198 -275
  132. package/docs/classes/AbstractBinaryTreeNode.html +21 -21
  133. package/docs/classes/AbstractEdge.html +11 -11
  134. package/docs/classes/AbstractGraph.html +36 -36
  135. package/docs/classes/AbstractVertex.html +8 -8
  136. package/docs/classes/ArrayDeque.html +14 -14
  137. package/docs/classes/BST.html +198 -275
  138. package/docs/classes/BSTNode.html +15 -15
  139. package/docs/classes/BTree.html +1 -1
  140. package/docs/classes/BinaryIndexedTree.html +9 -9
  141. package/docs/classes/BinaryTree.html +191 -268
  142. package/docs/classes/BinaryTreeNode.html +15 -15
  143. package/docs/classes/Character.html +4 -4
  144. package/docs/classes/CoordinateMap.html +9 -9
  145. package/docs/classes/CoordinateSet.html +8 -8
  146. package/docs/classes/Deque.html +34 -34
  147. package/docs/classes/DirectedEdge.html +15 -15
  148. package/docs/classes/DirectedGraph.html +52 -52
  149. package/docs/classes/DirectedVertex.html +6 -6
  150. package/docs/classes/DoublyLinkedList.html +37 -37
  151. package/docs/classes/DoublyLinkedListNode.html +11 -11
  152. package/docs/classes/HashTable.html +1 -1
  153. package/docs/classes/Heap.html +26 -26
  154. package/docs/classes/HeapItem.html +8 -8
  155. package/docs/classes/LinkedListQueue.html +35 -35
  156. package/docs/classes/MapEdge.html +13 -13
  157. package/docs/classes/MapGraph.html +56 -56
  158. package/docs/classes/MapVertex.html +12 -12
  159. package/docs/classes/Matrix2D.html +16 -16
  160. package/docs/classes/MatrixNTI2D.html +4 -4
  161. package/docs/classes/MaxHeap.html +26 -26
  162. package/docs/classes/MaxPriorityQueue.html +34 -34
  163. package/docs/classes/MinHeap.html +26 -26
  164. package/docs/classes/MinPriorityQueue.html +34 -34
  165. package/docs/classes/Navigator.html +10 -10
  166. package/docs/classes/ObjectDeque.html +25 -25
  167. package/docs/classes/Pair.html +1 -1
  168. package/docs/classes/PriorityQueue.html +32 -32
  169. package/docs/classes/Queue.html +22 -22
  170. package/docs/classes/RBTree.html +198 -275
  171. package/docs/classes/RBTreeNode.html +18 -18
  172. package/docs/classes/SegmentTree.html +17 -17
  173. package/docs/classes/SegmentTreeNode.html +20 -20
  174. package/docs/classes/SinglyLinkedList.html +35 -35
  175. package/docs/classes/SinglyLinkedListNode.html +8 -8
  176. package/docs/classes/SkipLinkedList.html +1 -1
  177. package/docs/classes/SplayTree.html +1 -1
  178. package/docs/classes/Stack.html +12 -12
  179. package/docs/classes/TreeMap.html +1 -1
  180. package/docs/classes/TreeMultiset.html +219 -296
  181. package/docs/classes/TreeMultisetNode.html +18 -18
  182. package/docs/classes/TreeNode.html +13 -13
  183. package/docs/classes/TreeSet.html +1 -1
  184. package/docs/classes/Trie.html +13 -13
  185. package/docs/classes/TrieNode.html +11 -11
  186. package/docs/classes/TwoThreeTree.html +1 -1
  187. package/docs/classes/UndirectedEdge.html +12 -12
  188. package/docs/classes/UndirectedGraph.html +40 -40
  189. package/docs/classes/UndirectedVertex.html +6 -6
  190. package/docs/classes/Vector2D.html +28 -28
  191. package/docs/enums/CP.html +4 -4
  192. package/docs/enums/FamilyPosition.html +8 -8
  193. package/docs/enums/LoopType.html +3 -3
  194. package/docs/enums/RBColor.html +3 -3
  195. package/docs/enums/TopologicalProperty.html +4 -4
  196. package/docs/functions/arrayRemove.html +1 -1
  197. package/docs/functions/isThunk.html +1 -1
  198. package/docs/functions/toThunk.html +1 -1
  199. package/docs/functions/trampoline.html +1 -1
  200. package/docs/functions/trampolineAsync.html +1 -1
  201. package/docs/functions/uuidV4.html +1 -1
  202. package/docs/index.html +11 -12
  203. package/docs/interfaces/IAVLTree.html +77 -77
  204. package/docs/interfaces/IAbstractBinaryTree.html +72 -72
  205. package/docs/interfaces/IAbstractBinaryTreeNode.html +14 -14
  206. package/docs/interfaces/IAbstractGraph.html +15 -15
  207. package/docs/interfaces/IBST.html +77 -77
  208. package/docs/interfaces/IDirectedGraph.html +23 -23
  209. package/docs/interfaces/IRBTree.html +77 -77
  210. package/docs/interfaces/IUNDirectedGraph.html +16 -16
  211. package/docs/types/AVLTreeNodeNested.html +1 -1
  212. package/docs/types/AVLTreeOptions.html +1 -1
  213. package/docs/types/AbstractBinaryTreeNodeNested.html +1 -1
  214. package/docs/types/AbstractBinaryTreeNodeProperties.html +1 -1
  215. package/docs/types/AbstractBinaryTreeNodeProperty.html +1 -1
  216. package/docs/types/AbstractBinaryTreeOptions.html +1 -1
  217. package/docs/types/BSTComparator.html +1 -1
  218. package/docs/types/BSTNodeNested.html +1 -1
  219. package/docs/types/BSTOptions.html +1 -1
  220. package/docs/types/BinaryTreeDeletedResult.html +1 -1
  221. package/docs/types/BinaryTreeNodeId.html +1 -1
  222. package/docs/types/BinaryTreeNodeNested.html +1 -1
  223. package/docs/types/BinaryTreeNodePropertyName.html +1 -1
  224. package/docs/types/BinaryTreeOptions.html +1 -1
  225. package/docs/types/DFSOrderPattern.html +1 -1
  226. package/docs/types/DijkstraResult.html +1 -1
  227. package/docs/types/Direction.html +1 -1
  228. package/docs/types/DummyAny.html +1 -1
  229. package/docs/types/EdgeId.html +1 -1
  230. package/docs/types/HeapOptions.html +1 -1
  231. package/docs/types/IAVLTreeNode.html +1 -1
  232. package/docs/types/IBSTNode.html +1 -1
  233. package/docs/types/IBinaryTree.html +1 -1
  234. package/docs/types/IBinaryTreeNode.html +1 -1
  235. package/docs/types/IRBTreeNode.html +1 -1
  236. package/docs/types/ITreeMultiset.html +1 -1
  237. package/docs/types/ITreeMultisetNode.html +1 -1
  238. package/docs/types/KeyValueObject.html +1 -1
  239. package/docs/types/KeyValueObjectWithId.html +1 -1
  240. package/docs/types/MapGraphCoordinate.html +1 -1
  241. package/docs/types/NavigatorParams.html +1 -1
  242. package/docs/types/NodeOrPropertyName.html +1 -1
  243. package/docs/types/NonNumberNonObjectButDefined.html +1 -1
  244. package/docs/types/ObjectWithNonNumberId.html +1 -1
  245. package/docs/types/ObjectWithNumberId.html +1 -1
  246. package/docs/types/ObjectWithoutId.html +1 -1
  247. package/docs/types/PriorityQueueComparator.html +1 -1
  248. package/docs/types/PriorityQueueDFSOrderPattern.html +1 -1
  249. package/docs/types/PriorityQueueOptions.html +1 -1
  250. package/docs/types/RBTreeNodeNested.html +1 -1
  251. package/docs/types/RBTreeOptions.html +1 -1
  252. package/docs/types/RestrictValById.html +1 -1
  253. package/docs/types/SegmentTreeNodeVal.html +1 -1
  254. package/docs/types/SpecifyOptional.html +1 -1
  255. package/docs/types/Thunk.html +1 -1
  256. package/docs/types/ToThunkFn.html +1 -1
  257. package/docs/types/TopologicalStatus.html +1 -1
  258. package/docs/types/TreeMultisetNodeNested.html +1 -1
  259. package/docs/types/TreeMultisetOptions.html +1 -1
  260. package/docs/types/TrlAsyncFn.html +1 -1
  261. package/docs/types/TrlFn.html +1 -1
  262. package/docs/types/Turning.html +1 -1
  263. package/docs/types/VertexId.html +1 -1
  264. package/docs/variables/THUNK_SYMBOL.html +1 -1
  265. package/jest.config.js +6 -3
  266. package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +169 -2
  267. package/lib/data-structures/binary-tree/abstract-binary-tree.js +31 -47
  268. package/package.json +40 -25
  269. package/src/data-structures/binary-tree/aa-tree.ts +1 -0
  270. package/src/data-structures/binary-tree/abstract-binary-tree.ts +1631 -0
  271. package/src/data-structures/binary-tree/avl-tree.ts +310 -0
  272. package/src/data-structures/binary-tree/b-tree.ts +1 -0
  273. package/src/data-structures/binary-tree/binary-indexed-tree.ts +76 -0
  274. package/src/data-structures/binary-tree/binary-tree.ts +47 -0
  275. package/src/data-structures/binary-tree/bst.ts +537 -0
  276. package/src/data-structures/binary-tree/index.ts +12 -0
  277. package/src/data-structures/binary-tree/rb-tree.ts +101 -0
  278. package/src/data-structures/binary-tree/segment-tree.ts +242 -0
  279. package/src/data-structures/binary-tree/splay-tree.ts +1 -0
  280. package/src/data-structures/binary-tree/tree-multiset.ts +700 -0
  281. package/src/data-structures/binary-tree/two-three-tree.ts +1 -0
  282. package/src/data-structures/diagrams/README.md +5 -0
  283. package/src/data-structures/graph/abstract-graph.ts +1040 -0
  284. package/src/data-structures/graph/directed-graph.ts +470 -0
  285. package/src/data-structures/graph/index.ts +4 -0
  286. package/src/data-structures/graph/map-graph.ts +129 -0
  287. package/src/data-structures/graph/undirected-graph.ts +274 -0
  288. package/src/data-structures/hash/coordinate-map.ts +67 -0
  289. package/src/data-structures/hash/coordinate-set.ts +56 -0
  290. package/src/data-structures/hash/hash-table.ts +1 -0
  291. package/src/data-structures/hash/index.ts +6 -0
  292. package/src/data-structures/hash/pair.ts +1 -0
  293. package/src/data-structures/hash/tree-map.ts +1 -0
  294. package/src/data-structures/hash/tree-set.ts +1 -0
  295. package/src/data-structures/heap/heap.ts +212 -0
  296. package/src/data-structures/heap/index.ts +3 -0
  297. package/src/data-structures/heap/max-heap.ts +31 -0
  298. package/src/data-structures/heap/min-heap.ts +32 -0
  299. package/src/data-structures/index.ts +11 -0
  300. package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
  301. package/src/data-structures/linked-list/index.ts +3 -0
  302. package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
  303. package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
  304. package/src/data-structures/matrix/index.ts +4 -0
  305. package/src/data-structures/matrix/matrix.ts +27 -0
  306. package/src/data-structures/matrix/matrix2d.ts +212 -0
  307. package/src/data-structures/matrix/navigator.ts +121 -0
  308. package/src/data-structures/matrix/vector2d.ts +316 -0
  309. package/src/data-structures/priority-queue/index.ts +3 -0
  310. package/src/data-structures/priority-queue/max-priority-queue.ts +56 -0
  311. package/src/data-structures/priority-queue/min-priority-queue.ts +57 -0
  312. package/src/data-structures/priority-queue/priority-queue.ts +359 -0
  313. package/src/data-structures/queue/deque.ts +297 -0
  314. package/src/data-structures/queue/index.ts +2 -0
  315. package/src/data-structures/queue/queue.ts +191 -0
  316. package/src/data-structures/stack/index.ts +1 -0
  317. package/src/data-structures/stack/stack.ts +98 -0
  318. package/src/data-structures/tree/index.ts +1 -0
  319. package/src/data-structures/tree/tree.ts +69 -0
  320. package/src/data-structures/trie/index.ts +1 -0
  321. package/src/data-structures/trie/trie.ts +225 -0
  322. package/src/index.ts +4 -0
  323. package/src/interfaces/abstract-binary-tree.ts +191 -0
  324. package/src/interfaces/abstract-graph.ts +31 -0
  325. package/src/interfaces/avl-tree.ts +25 -0
  326. package/src/interfaces/binary-tree.ts +6 -0
  327. package/src/interfaces/bst.ts +31 -0
  328. package/src/interfaces/directed-graph.ts +20 -0
  329. package/src/interfaces/doubly-linked-list.ts +1 -0
  330. package/src/interfaces/heap.ts +1 -0
  331. package/src/interfaces/index.ts +15 -0
  332. package/src/interfaces/navigator.ts +1 -0
  333. package/src/interfaces/priority-queue.ts +1 -0
  334. package/src/interfaces/rb-tree.ts +9 -0
  335. package/src/interfaces/segment-tree.ts +1 -0
  336. package/src/interfaces/singly-linked-list.ts +1 -0
  337. package/src/interfaces/tree-multiset.ts +7 -0
  338. package/src/interfaces/undirected-graph.ts +6 -0
  339. package/src/types/data-structures/abstract-binary-tree.ts +50 -0
  340. package/src/types/data-structures/abstract-graph.ts +11 -0
  341. package/src/types/data-structures/avl-tree.ts +5 -0
  342. package/src/types/data-structures/binary-tree.ts +5 -0
  343. package/src/types/data-structures/bst.ts +13 -0
  344. package/src/types/data-structures/directed-graph.ts +8 -0
  345. package/src/types/data-structures/doubly-linked-list.ts +1 -0
  346. package/src/types/data-structures/heap.ts +5 -0
  347. package/src/types/data-structures/index.ts +15 -0
  348. package/src/types/data-structures/map-graph.ts +1 -0
  349. package/src/types/data-structures/navigator.ts +13 -0
  350. package/src/types/data-structures/priority-queue.ts +9 -0
  351. package/src/types/data-structures/rb-tree.ts +8 -0
  352. package/src/types/data-structures/segment-tree.ts +1 -0
  353. package/src/types/data-structures/singly-linked-list.ts +1 -0
  354. package/src/types/data-structures/tree-multiset.ts +6 -0
  355. package/src/types/helpers.ts +1 -0
  356. package/src/types/index.ts +3 -0
  357. package/src/types/utils/index.ts +2 -0
  358. package/src/types/utils/utils.ts +6 -0
  359. package/src/types/utils/validate-type.ts +35 -0
  360. package/src/utils/index.ts +1 -0
  361. package/src/utils/utils.ts +79 -0
  362. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +1 -1
  363. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +3 -1
  364. package/tsconfig.json +1 -1
  365. package/umd/bundle.min.js.map +1 -1
@@ -0,0 +1,274 @@
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 {arrayRemove} from '../../utils';
9
+ import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
10
+ import type {VertexId} from '../../types';
11
+ import {IUNDirectedGraph} from '../../interfaces';
12
+
13
+ export class UndirectedVertex<T = number> extends AbstractVertex<T> {
14
+ /**
15
+ * The constructor function initializes a vertex with an optional value.
16
+ * @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
17
+ * used to uniquely identify the vertex within a graph or network.
18
+ * @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to initialize the value of the
19
+ * vertex. If no value is provided, the vertex will be initialized with a default value.
20
+ */
21
+ constructor(id: VertexId, val?: T) {
22
+ super(id, val);
23
+ }
24
+ }
25
+
26
+ export class UndirectedEdge<T = number> extends AbstractEdge<T> {
27
+ /**
28
+ * The constructor function creates an instance of a class with two vertex IDs, an optional weight, and an optional
29
+ * value.
30
+ * @param {VertexId} v1 - The first vertex ID of the edge.
31
+ * @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
32
+ * graph edge.
33
+ * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
34
+ * @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to store a value associated
35
+ * with the edge.
36
+ */
37
+ constructor(v1: VertexId, v2: VertexId, weight?: number, val?: T) {
38
+ super(weight, val);
39
+ this._vertices = [v1, v2];
40
+ }
41
+
42
+ private _vertices: [VertexId, VertexId];
43
+
44
+ get vertices() {
45
+ return this._vertices;
46
+ }
47
+
48
+ set vertices(v: [VertexId, VertexId]) {
49
+ this._vertices = v;
50
+ }
51
+ }
52
+
53
+ export class UndirectedGraph<
54
+ V extends UndirectedVertex<any> = UndirectedVertex,
55
+ E extends UndirectedEdge<any> = UndirectedEdge
56
+ >
57
+ extends AbstractGraph<V, E>
58
+ implements IUNDirectedGraph<V, E>
59
+ {
60
+ /**
61
+ * The constructor initializes a new Map object to store edges.
62
+ */
63
+ constructor() {
64
+ super();
65
+ this._edges = new Map<V, E[]>();
66
+ }
67
+
68
+ protected _edges: Map<V, E[]>;
69
+
70
+ get edges(): Map<V, E[]> {
71
+ return this._edges;
72
+ }
73
+
74
+ /**
75
+ * The function creates a new vertex with an optional value and returns it.
76
+ * @param {VertexId} id - The `id` parameter is the unique identifier for the vertex. It is used to distinguish one
77
+ * vertex from another in the graph.
78
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the vertex. If a value is provided,
79
+ * it will be used as the value of the vertex. If no value is provided, the `id` parameter will be used as the value of
80
+ * the vertex.
81
+ * @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `V`.
82
+ */
83
+ override createVertex(id: VertexId, val?: V['val']): V {
84
+ return new UndirectedVertex(id, val ?? id) as V;
85
+ }
86
+
87
+ /**
88
+ * The function creates an undirected edge between two vertices with an optional weight and value.
89
+ * @param {VertexId} v1 - The parameter `v1` represents the first vertex of the edge.
90
+ * @param {VertexId} v2 - The parameter `v2` represents the second vertex of the edge.
91
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
92
+ * no weight is provided, it defaults to 1.
93
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type and
94
+ * is used to store additional information or data associated with the edge.
95
+ * @returns a new instance of the `UndirectedEdge` class, which is casted as type `E`.
96
+ */
97
+ override createEdge(v1: VertexId, v2: VertexId, weight?: number, val?: E['val']): E {
98
+ return new UndirectedEdge(v1, v2, weight ?? 1, val) as E;
99
+ }
100
+
101
+ /**
102
+ * The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
103
+ * @param {V | null | VertexId} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `V` (vertex
104
+ * object), `null`, or `VertexId` (a string or number representing the ID of a vertex).
105
+ * @param {V | null | VertexId} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `V` (vertex
106
+ * object), `null`, or `VertexId` (vertex ID).
107
+ * @returns an edge (E) or null.
108
+ */
109
+ getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null {
110
+ let edges: E[] | undefined = [];
111
+
112
+ if (v1 !== null && v2 !== null) {
113
+ const vertex1: V | null = this._getVertex(v1);
114
+ const vertex2: V | null = this._getVertex(v2);
115
+
116
+ if (vertex1 && vertex2) {
117
+ edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.id));
118
+ }
119
+ }
120
+
121
+ return edges ? edges[0] || null : null;
122
+ }
123
+
124
+ /**
125
+ * The function removes an edge between two vertices in a graph and returns the removed edge.
126
+ * @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
127
+ * @param {V | VertexId} v2 - V | VertexId - This parameter can be either a vertex object (V) or a vertex ID
128
+ * (VertexId). It represents the second vertex of the edge that needs to be removed.
129
+ * @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
130
+ */
131
+ removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null {
132
+ const vertex1: V | null = this._getVertex(v1);
133
+ const vertex2: V | null = this._getVertex(v2);
134
+
135
+ if (!vertex1 || !vertex2) {
136
+ return null;
137
+ }
138
+
139
+ const v1Edges = this._edges.get(vertex1);
140
+ let removed: E | null = null;
141
+ if (v1Edges) {
142
+ removed = arrayRemove<E>(v1Edges, (e: E) => e.vertices.includes(vertex2.id))[0] || null;
143
+ }
144
+ const v2Edges = this._edges.get(vertex2);
145
+ if (v2Edges) {
146
+ arrayRemove<E>(v2Edges, (e: E) => e.vertices.includes(vertex1.id));
147
+ }
148
+ return removed;
149
+ }
150
+
151
+ /**
152
+ * The removeEdge function removes an edge between two vertices in a graph.
153
+ * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
154
+ * @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
155
+ */
156
+ removeEdge(edge: E): E | null {
157
+ return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
158
+ }
159
+
160
+ /**
161
+ * The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
162
+ * vertex.
163
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
164
+ * @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
165
+ * edges connected to that vertex.
166
+ */
167
+ degreeOf(vertexOrId: VertexId | V): number {
168
+ const vertex = this._getVertex(vertexOrId);
169
+ if (vertex) {
170
+ return this._edges.get(vertex)?.length || 0;
171
+ } else {
172
+ return 0;
173
+ }
174
+ }
175
+
176
+ /**
177
+ * The function returns the edges of a given vertex or vertex ID.
178
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`. A `VertexId` is a
179
+ * unique identifier for a vertex in a graph, while `V` represents the type of the vertex.
180
+ * @returns an array of edges.
181
+ */
182
+ edgesOf(vertexOrId: VertexId | V): E[] {
183
+ const vertex = this._getVertex(vertexOrId);
184
+ if (vertex) {
185
+ return this._edges.get(vertex) || [];
186
+ } else {
187
+ return [];
188
+ }
189
+ }
190
+
191
+ /**
192
+ * The function "edgeSet" returns an array of unique edges from a set of edges.
193
+ * @returns The method `edgeSet()` returns an array of type `E[]`.
194
+ */
195
+ edgeSet(): E[] {
196
+ const edgeSet: Set<E> = new Set();
197
+ this._edges.forEach(edges => {
198
+ edges.forEach(edge => {
199
+ edgeSet.add(edge);
200
+ });
201
+ });
202
+ return [...edgeSet];
203
+ }
204
+
205
+ /**
206
+ * The function "getNeighbors" returns an array of neighboring vertices for a given vertex or vertex ID.
207
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
208
+ * (`VertexId`).
209
+ * @returns an array of vertices (V[]).
210
+ */
211
+ getNeighbors(vertexOrId: V | VertexId): V[] {
212
+ const neighbors: V[] = [];
213
+ const vertex = this._getVertex(vertexOrId);
214
+ if (vertex) {
215
+ const neighborEdges = this.edgesOf(vertex);
216
+ for (const edge of neighborEdges) {
217
+ const neighbor = this._getVertex(edge.vertices.filter(e => e !== vertex.id)[0]);
218
+ if (neighbor) {
219
+ neighbors.push(neighbor);
220
+ }
221
+ }
222
+ }
223
+ return neighbors;
224
+ }
225
+
226
+ /**
227
+ * The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
228
+ * it returns null.
229
+ * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
230
+ * @returns The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the
231
+ * graph. If the edge does not exist, it returns `null`.
232
+ */
233
+ getEndsOfEdge(edge: E): [V, V] | null {
234
+ if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
235
+ return null;
236
+ }
237
+ const v1 = this._getVertex(edge.vertices[0]);
238
+ const v2 = this._getVertex(edge.vertices[1]);
239
+ if (v1 && v2) {
240
+ return [v1, v2];
241
+ } else {
242
+ return null;
243
+ }
244
+ }
245
+
246
+ /**
247
+ * The function adds an edge to the graph by updating the adjacency list with the vertices of the edge.
248
+ * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
249
+ * @returns a boolean value.
250
+ */
251
+ protected _addEdgeOnly(edge: E): boolean {
252
+ for (const end of edge.vertices) {
253
+ const endVertex = this._getVertex(end);
254
+ if (endVertex === null) return false;
255
+ if (endVertex) {
256
+ const edges = this._edges.get(endVertex);
257
+ if (edges) {
258
+ edges.push(edge);
259
+ } else {
260
+ this._edges.set(endVertex, [edge]);
261
+ }
262
+ }
263
+ }
264
+ return true;
265
+ }
266
+
267
+ /**
268
+ * The function sets the edges of a graph.
269
+ * @param v - A map where the keys are of type V and the values are arrays of type E.
270
+ */
271
+ protected _setEdges(v: Map<V, E[]>) {
272
+ this._edges = v;
273
+ }
274
+ }
@@ -0,0 +1,67 @@
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
+ export class CoordinateMap<V> extends Map<any, V> {
9
+ constructor(joint?: string) {
10
+ super();
11
+ if (joint !== undefined) this._joint = joint;
12
+ }
13
+
14
+ protected _joint = '_';
15
+
16
+ get joint(): string {
17
+ return this._joint;
18
+ }
19
+
20
+ /**
21
+ * The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
22
+ * key array with a specified delimiter.
23
+ * @param {number[]} key - The parameter "key" is an array of numbers.
24
+ * @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
25
+ * (`super.has`) with the `key` array joined together using the `_joint` property.
26
+ */
27
+ override has(key: number[]) {
28
+ return super.has(key.join(this._joint));
29
+ }
30
+
31
+ /**
32
+ * The function overrides the set method of a Map object to convert the key from an array to a string using a specified
33
+ * delimiter before calling the original set method.
34
+ * @param {number[]} key - The key parameter is an array of numbers.
35
+ * @param {V} value - The value parameter is the value that you want to associate with the specified key.
36
+ * @returns The `set` method is returning the result of calling the `set` method of the superclass
37
+ * (`super.set(key.join(this._joint), value)`).
38
+ */
39
+ override set(key: number[], value: V) {
40
+ return super.set(key.join(this._joint), value);
41
+ }
42
+
43
+ /**
44
+ * The function overrides the get method to join the key array with a specified joint and then calls the super get
45
+ * method.
46
+ * @param {number[]} key - An array of numbers
47
+ * @returns The code is returning the value associated with the specified key in the map.
48
+ */
49
+ override get(key: number[]) {
50
+ return super.get(key.join(this._joint));
51
+ }
52
+
53
+ /**
54
+ * The function overrides the delete method and joins the key array using a specified joint character before calling
55
+ * the super delete method.
56
+ * @param {number[]} key - An array of numbers that represents the key to be deleted.
57
+ * @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
58
+ * `key` array joined together using the `_joint` property.
59
+ */
60
+ override delete(key: number[]) {
61
+ return super.delete(key.join(this._joint));
62
+ }
63
+
64
+ protected _setJoint(v: string) {
65
+ this._joint = v;
66
+ }
67
+ }
@@ -0,0 +1,56 @@
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
+ export class CoordinateSet extends Set<any> {
9
+ constructor(joint?: string) {
10
+ super();
11
+ if (joint !== undefined) this._joint = joint;
12
+ }
13
+
14
+ protected _joint = '_';
15
+
16
+ get joint(): string {
17
+ return this._joint;
18
+ }
19
+
20
+ /**
21
+ * The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
22
+ * joining its elements with a specified separator.
23
+ * @param {number[]} value - The parameter "value" is an array of numbers.
24
+ * @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
25
+ * in the joined value as an argument.
26
+ */
27
+ override has(value: number[]) {
28
+ return super.has(value.join(this._joint));
29
+ }
30
+
31
+ /**
32
+ * The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
33
+ * specified delimiter before calling the parent class's "add" function.
34
+ * @param {number[]} value - An array of numbers
35
+ * @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
36
+ * (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
37
+ */
38
+ override add(value: number[]) {
39
+ return super.add(value.join(this._joint));
40
+ }
41
+
42
+ /**
43
+ * The function overrides the delete method and deletes an element from a Set by joining the elements of the input
44
+ * array with a specified joint and then calling the delete method of the parent class.
45
+ * @param {number[]} value - An array of numbers
46
+ * @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
47
+ * `value` array joined together using the `_joint` property.
48
+ */
49
+ override delete(value: number[]) {
50
+ return super.delete(value.join(this._joint));
51
+ }
52
+
53
+ protected _setJoint(v: string) {
54
+ this._joint = v;
55
+ }
56
+ }
@@ -0,0 +1 @@
1
+ export class HashTable {}
@@ -0,0 +1,6 @@
1
+ export * from './hash-table';
2
+ export * from './coordinate-map';
3
+ export * from './coordinate-set';
4
+ export * from './pair';
5
+ export * from './tree-map';
6
+ export * from './tree-set';
@@ -0,0 +1 @@
1
+ export class Pair {}
@@ -0,0 +1 @@
1
+ export class TreeMap {}
@@ -0,0 +1 @@
1
+ export class TreeSet {}
@@ -0,0 +1,212 @@
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 {HeapOptions} from '../../types';
10
+
11
+ export class HeapItem<T = number> {
12
+ /**
13
+ * The constructor function initializes an instance of a class with a priority and a value.
14
+ * @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
15
+ * optional and has a default value of `NaN`.
16
+ * @param {T | null} [val=null] - The `val` parameter is of type `T | null`, which means it can accept a value of type
17
+ * `T` or `null`.
18
+ */
19
+ constructor(priority: number = Number.MAX_SAFE_INTEGER, val: T | null = null) {
20
+ this._val = val;
21
+ this._priority = priority;
22
+ }
23
+
24
+ private _priority: number;
25
+
26
+ get priority(): number {
27
+ return this._priority;
28
+ }
29
+
30
+ set priority(value: number) {
31
+ this._priority = value;
32
+ }
33
+
34
+ private _val: T | null;
35
+
36
+ get val(): T | null {
37
+ return this._val;
38
+ }
39
+
40
+ set val(value: T | null) {
41
+ this._val = value;
42
+ }
43
+ }
44
+
45
+ export abstract class Heap<T = number> {
46
+ /**
47
+ * The function is a constructor for a class that initializes a priority callback function based on the
48
+ * options provided.
49
+ * @param [options] - An optional object that contains configuration options for the Heap.
50
+ */
51
+ protected constructor(options?: HeapOptions<T>) {
52
+ if (options) {
53
+ const {priorityExtractor} = options;
54
+ if (priorityExtractor !== undefined && typeof priorityExtractor !== 'function') {
55
+ throw new Error('.constructor expects a valid priority function');
56
+ }
57
+ this._priorityExtractor = priorityExtractor || (el => +el);
58
+ } else {
59
+ this._priorityExtractor = el => +el;
60
+ }
61
+ }
62
+
63
+ protected abstract _pq: PriorityQueue<HeapItem<T>>;
64
+
65
+ get pq() {
66
+ return this._pq;
67
+ }
68
+
69
+ protected _priorityExtractor: (val: T) => number;
70
+ get priorityExtractor() {
71
+ return this._priorityExtractor;
72
+ }
73
+
74
+ /**
75
+ * The function returns the size of a priority queue.
76
+ * @returns The size of the priority queue.
77
+ */
78
+ get size(): number {
79
+ return this._pq.size;
80
+ }
81
+
82
+ /**
83
+ * The function checks if a priority queue is empty.
84
+ * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
85
+ */
86
+ isEmpty(): boolean {
87
+ return this._pq.size < 1;
88
+ }
89
+
90
+ peek(isItem?: undefined): T | undefined;
91
+ peek(isItem: false): T | undefined;
92
+ peek(isItem: true): HeapItem<T> | null;
93
+
94
+ /**
95
+ * The `peek` function returns the top item in the priority queue without removing it.
96
+ * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
97
+ */
98
+ peek(isItem?: boolean): HeapItem<T> | null | T | undefined {
99
+ isItem = isItem ?? false;
100
+ const peeked = this._pq.peek();
101
+
102
+ return isItem ? peeked : peeked?.val;
103
+ }
104
+
105
+ peekLast(isItem?: undefined): T | undefined;
106
+ peekLast(isItem: false): T | undefined;
107
+ peekLast(isItem: true): HeapItem<T> | null;
108
+
109
+ /**
110
+ * The `peekLast` function returns the last item in the heap.
111
+ * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
112
+ */
113
+ peekLast(isItem?: boolean): HeapItem<T> | null | T | undefined {
114
+ isItem = isItem ?? false;
115
+ const leafItem = this._pq.leaf();
116
+
117
+ return isItem ? leafItem : leafItem?.val;
118
+ }
119
+
120
+ /**
121
+ * The `add` function adds an val to a priority queue with an optional priority value.
122
+ * @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
123
+ * type.
124
+ * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
125
+ * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
126
+ * the value of `val`. If the `val` parameter is not a number, then the
127
+ * @returns The `add` method returns the instance of the `Heap` class.
128
+ * @throws {Error} if priority is not a valid number
129
+ */
130
+ add(priority: number, val?: T): Heap<T> {
131
+ val = val === undefined ? (priority as unknown as T) : val;
132
+ this._pq.add(new HeapItem<T>(priority, val));
133
+
134
+ return this;
135
+ }
136
+
137
+ poll(isItem?: undefined): T | undefined;
138
+ poll(isItem: false): T | undefined;
139
+ poll(isItem: true): HeapItem<T> | null;
140
+
141
+ /**
142
+ * 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
143
+ * @returns either a HeapItem<T> object or null.
144
+ */
145
+ poll(isItem?: boolean): HeapItem<T> | null | T | undefined {
146
+ isItem = isItem ?? false;
147
+ const top = this._pq.poll();
148
+ if (!top) {
149
+ return null;
150
+ }
151
+
152
+ return isItem ? top : top.val;
153
+ }
154
+
155
+ /**
156
+ * The function checks if a given node or value exists in the priority queue.
157
+ * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
158
+ * @returns a boolean value.
159
+ */
160
+ has(node: T | HeapItem<T>): boolean {
161
+ if (node instanceof HeapItem) {
162
+ return this.pq.getNodes().includes(node);
163
+ } else {
164
+ return (
165
+ this.pq.getNodes().findIndex(item => {
166
+ return item.val === node;
167
+ }) !== -1
168
+ );
169
+ }
170
+ }
171
+
172
+ toArray(isItem?: undefined): (T | undefined)[];
173
+ toArray(isItem: false): (T | undefined)[];
174
+ toArray(isItem: true): (HeapItem<T> | null)[];
175
+
176
+ /**
177
+ * The `toArray` function returns an array of `HeapItem<T>` objects.
178
+ * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
179
+ */
180
+ toArray(isItem?: boolean): (HeapItem<T> | null | T | undefined)[] {
181
+ isItem = isItem ?? false;
182
+ const itemArray = this._pq.toArray();
183
+
184
+ return isItem ? itemArray : itemArray.map(item => item.val);
185
+ }
186
+
187
+ sort(isItem?: undefined): (T | undefined)[];
188
+ sort(isItem: false): (T | undefined)[];
189
+ sort(isItem: true): (HeapItem<T> | null)[];
190
+
191
+ /**
192
+ * The function sorts the elements in the priority queue and returns either the sorted items or their values depending
193
+ * on the value of the isItem parameter.
194
+ * @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the sorted result should
195
+ * be an array of `HeapItem<T>` objects or an array of the values (`T`) of those objects. If `isItem` is `true`, the
196
+ * sorted result will be an array of `HeapItem
197
+ * @returns an array of either `HeapItem<T>`, `null`, `T`, or `undefined` values.
198
+ */
199
+ sort(isItem?: boolean): (HeapItem<T> | null | T | undefined)[] {
200
+ isItem = isItem ?? false;
201
+ const sorted = this._pq.sort();
202
+
203
+ return isItem ? sorted : sorted.map(item => item.val);
204
+ }
205
+
206
+ /**
207
+ * The clear function clears the priority queue.
208
+ */
209
+ clear(): void {
210
+ this._pq.clear();
211
+ }
212
+ }
@@ -0,0 +1,3 @@
1
+ export * from './max-heap';
2
+ export * from './min-heap';
3
+ export * from './heap';
@@ -0,0 +1,31 @@
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
+
9
+ import {Heap, HeapItem} from './heap';
10
+ import {PriorityQueue} from '../priority-queue';
11
+ import type {HeapOptions} from '../../types';
12
+
13
+ /**
14
+ * @class MaxHeap
15
+ * @extends Heap
16
+ */
17
+ export class MaxHeap<T = number> extends Heap<T> {
18
+ protected _pq: PriorityQueue<HeapItem<T>>;
19
+
20
+ /**
21
+ * The constructor initializes a PriorityQueue with a custom comparator function.
22
+ * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
23
+ * type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
24
+ */
25
+ constructor(options?: HeapOptions<T>) {
26
+ super(options);
27
+ this._pq = new PriorityQueue<HeapItem<T>>({
28
+ comparator: (a, b) => b.priority - a.priority
29
+ });
30
+ }
31
+ }