data-structure-typed 1.21.4 → 1.31.0

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 (560) hide show
  1. package/.auto-changelog +9 -0
  2. package/.auto-changelog-template.hbs +36 -0
  3. package/.eslintrc.js +61 -0
  4. package/.github/workflows/ci.yml +30 -0
  5. package/.prettierignore +6 -0
  6. package/.prettierrc.js +16 -0
  7. package/CHANGELOG.md +17 -0
  8. package/README.md +147 -95
  9. package/dist/data-structures/binary-tree/aa-tree.js +6 -2
  10. package/dist/data-structures/binary-tree/aa-tree.js.map +1 -0
  11. package/dist/data-structures/binary-tree/abstract-binary-tree.js +398 -672
  12. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -0
  13. package/dist/data-structures/binary-tree/avl-tree.js +122 -151
  14. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -0
  15. package/dist/data-structures/binary-tree/b-tree.js +6 -2
  16. package/dist/data-structures/binary-tree/b-tree.js.map +1 -0
  17. package/dist/data-structures/binary-tree/binary-indexed-tree.js +24 -54
  18. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -0
  19. package/dist/data-structures/binary-tree/binary-tree.js +31 -32
  20. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -0
  21. package/dist/data-structures/binary-tree/bst.js +266 -208
  22. package/dist/data-structures/binary-tree/bst.js.map +1 -0
  23. package/dist/data-structures/binary-tree/index.js +1 -0
  24. package/dist/data-structures/binary-tree/index.js.map +1 -0
  25. package/dist/data-structures/binary-tree/rb-tree.js +46 -37
  26. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -0
  27. package/dist/data-structures/binary-tree/segment-tree.js +124 -127
  28. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -0
  29. package/dist/data-structures/binary-tree/splay-tree.js +6 -2
  30. package/dist/data-structures/binary-tree/splay-tree.js.map +1 -0
  31. package/dist/data-structures/binary-tree/tree-multiset.js +227 -354
  32. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -0
  33. package/dist/data-structures/binary-tree/two-three-tree.js +6 -2
  34. package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -0
  35. package/dist/data-structures/graph/abstract-graph.js +582 -609
  36. package/dist/data-structures/graph/abstract-graph.js.map +1 -0
  37. package/dist/data-structures/graph/directed-graph.js +276 -312
  38. package/dist/data-structures/graph/directed-graph.js.map +1 -0
  39. package/dist/data-structures/graph/index.js +1 -0
  40. package/dist/data-structures/graph/index.js.map +1 -0
  41. package/dist/data-structures/graph/map-graph.js +88 -100
  42. package/dist/data-structures/graph/map-graph.js.map +1 -0
  43. package/dist/data-structures/graph/undirected-graph.js +180 -195
  44. package/dist/data-structures/graph/undirected-graph.js.map +1 -0
  45. package/dist/data-structures/hash/coordinate-map.js +46 -58
  46. package/dist/data-structures/hash/coordinate-map.js.map +1 -0
  47. package/dist/data-structures/hash/coordinate-set.js +43 -48
  48. package/dist/data-structures/hash/coordinate-set.js.map +1 -0
  49. package/dist/data-structures/hash/hash-table.js +6 -2
  50. package/dist/data-structures/hash/hash-table.js.map +1 -0
  51. package/dist/data-structures/hash/index.js +1 -0
  52. package/dist/data-structures/hash/index.js.map +1 -0
  53. package/dist/data-structures/hash/pair.js +6 -2
  54. package/dist/data-structures/hash/pair.js.map +1 -0
  55. package/dist/data-structures/hash/tree-map.js +6 -2
  56. package/dist/data-structures/hash/tree-map.js.map +1 -0
  57. package/dist/data-structures/hash/tree-set.js +6 -2
  58. package/dist/data-structures/hash/tree-set.js.map +1 -0
  59. package/dist/data-structures/heap/heap.js +83 -120
  60. package/dist/data-structures/heap/heap.js.map +1 -0
  61. package/dist/data-structures/heap/index.js +1 -0
  62. package/dist/data-structures/heap/index.js.map +1 -0
  63. package/dist/data-structures/heap/max-heap.js +27 -24
  64. package/dist/data-structures/heap/max-heap.js.map +1 -0
  65. package/dist/data-structures/heap/min-heap.js +27 -25
  66. package/dist/data-structures/heap/min-heap.js.map +1 -0
  67. package/dist/data-structures/index.js +1 -0
  68. package/dist/data-structures/index.js.map +1 -0
  69. package/dist/data-structures/linked-list/doubly-linked-list.js +202 -307
  70. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -0
  71. package/dist/data-structures/linked-list/index.js +1 -0
  72. package/dist/data-structures/linked-list/index.js.map +1 -0
  73. package/dist/data-structures/linked-list/singly-linked-list.js +223 -251
  74. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -0
  75. package/dist/data-structures/linked-list/skip-linked-list.js +6 -2
  76. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -0
  77. package/dist/data-structures/matrix/index.js +1 -0
  78. package/dist/data-structures/matrix/index.js.map +1 -0
  79. package/dist/data-structures/matrix/matrix.js +9 -22
  80. package/dist/data-structures/matrix/matrix.js.map +1 -0
  81. package/dist/data-structures/matrix/matrix2d.js +75 -149
  82. package/dist/data-structures/matrix/matrix2d.js.map +1 -0
  83. package/dist/data-structures/matrix/navigator.js +38 -46
  84. package/dist/data-structures/matrix/navigator.js.map +1 -0
  85. package/dist/data-structures/matrix/vector2d.js +90 -254
  86. package/dist/data-structures/matrix/vector2d.js.map +1 -0
  87. package/dist/data-structures/priority-queue/index.js +1 -0
  88. package/dist/data-structures/priority-queue/index.js.map +1 -0
  89. package/dist/data-structures/priority-queue/max-priority-queue.js +47 -32
  90. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -0
  91. package/dist/data-structures/priority-queue/min-priority-queue.js +47 -33
  92. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -0
  93. package/dist/data-structures/priority-queue/priority-queue.js +150 -241
  94. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -0
  95. package/dist/data-structures/queue/deque.js +130 -162
  96. package/dist/data-structures/queue/deque.js.map +1 -0
  97. package/dist/data-structures/queue/index.js +1 -0
  98. package/dist/data-structures/queue/index.js.map +1 -0
  99. package/dist/data-structures/queue/queue.js +181 -100
  100. package/dist/data-structures/queue/queue.js.map +1 -0
  101. package/dist/data-structures/stack/index.js +1 -0
  102. package/dist/data-structures/stack/index.js.map +1 -0
  103. package/dist/data-structures/stack/stack.js +23 -71
  104. package/dist/data-structures/stack/stack.js.map +1 -0
  105. package/dist/data-structures/tree/index.js +1 -0
  106. package/dist/data-structures/tree/index.js.map +1 -0
  107. package/dist/data-structures/tree/tree.js +46 -33
  108. package/dist/data-structures/tree/tree.js.map +1 -0
  109. package/dist/data-structures/trie/index.js +1 -0
  110. package/dist/data-structures/trie/index.js.map +1 -0
  111. package/dist/data-structures/trie/trie.js +201 -129
  112. package/dist/data-structures/trie/trie.js.map +1 -0
  113. package/dist/index.js +1 -0
  114. package/dist/index.js.map +1 -0
  115. package/dist/interfaces/abstract-binary-tree.js +1 -0
  116. package/dist/interfaces/abstract-binary-tree.js.map +1 -0
  117. package/dist/interfaces/abstract-graph.js +1 -0
  118. package/dist/interfaces/abstract-graph.js.map +1 -0
  119. package/dist/interfaces/avl-tree.js +1 -0
  120. package/dist/interfaces/avl-tree.js.map +1 -0
  121. package/dist/interfaces/binary-tree.js +1 -0
  122. package/dist/interfaces/binary-tree.js.map +1 -0
  123. package/dist/interfaces/bst.js +1 -0
  124. package/dist/interfaces/bst.js.map +1 -0
  125. package/dist/interfaces/directed-graph.js +1 -0
  126. package/dist/interfaces/directed-graph.js.map +1 -0
  127. package/dist/interfaces/doubly-linked-list.js +1 -0
  128. package/dist/interfaces/doubly-linked-list.js.map +1 -0
  129. package/dist/interfaces/heap.js +1 -0
  130. package/dist/interfaces/heap.js.map +1 -0
  131. package/dist/interfaces/index.js +1 -0
  132. package/dist/interfaces/index.js.map +1 -0
  133. package/dist/interfaces/navigator.js +1 -0
  134. package/dist/interfaces/navigator.js.map +1 -0
  135. package/dist/interfaces/priority-queue.js +1 -0
  136. package/dist/interfaces/priority-queue.js.map +1 -0
  137. package/dist/interfaces/rb-tree.js +1 -0
  138. package/dist/interfaces/rb-tree.js.map +1 -0
  139. package/dist/interfaces/segment-tree.js +1 -0
  140. package/dist/interfaces/segment-tree.js.map +1 -0
  141. package/dist/interfaces/singly-linked-list.js +1 -0
  142. package/dist/interfaces/singly-linked-list.js.map +1 -0
  143. package/dist/interfaces/tree-multiset.js +1 -0
  144. package/dist/interfaces/tree-multiset.js.map +1 -0
  145. package/dist/interfaces/undirected-graph.js +1 -0
  146. package/dist/interfaces/undirected-graph.js.map +1 -0
  147. package/dist/types/data-structures/abstract-binary-tree.js +1 -7
  148. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -0
  149. package/dist/types/data-structures/abstract-graph.js +1 -0
  150. package/dist/types/data-structures/abstract-graph.js.map +1 -0
  151. package/dist/types/data-structures/avl-tree.js +1 -0
  152. package/dist/types/data-structures/avl-tree.js.map +1 -0
  153. package/dist/types/data-structures/binary-tree.js +1 -0
  154. package/dist/types/data-structures/binary-tree.js.map +1 -0
  155. package/dist/types/data-structures/bst.js +1 -0
  156. package/dist/types/data-structures/bst.js.map +1 -0
  157. package/dist/types/data-structures/directed-graph.js +1 -0
  158. package/dist/types/data-structures/directed-graph.js.map +1 -0
  159. package/dist/types/data-structures/doubly-linked-list.js +1 -0
  160. package/dist/types/data-structures/doubly-linked-list.js.map +1 -0
  161. package/dist/types/data-structures/heap.js +1 -0
  162. package/dist/types/data-structures/heap.js.map +1 -0
  163. package/dist/types/data-structures/index.js +1 -0
  164. package/dist/types/data-structures/index.js.map +1 -0
  165. package/dist/types/data-structures/map-graph.js +1 -0
  166. package/dist/types/data-structures/map-graph.js.map +1 -0
  167. package/dist/types/data-structures/navigator.js +1 -0
  168. package/dist/types/data-structures/navigator.js.map +1 -0
  169. package/dist/types/data-structures/priority-queue.js +1 -0
  170. package/dist/types/data-structures/priority-queue.js.map +1 -0
  171. package/dist/types/data-structures/rb-tree.js +1 -0
  172. package/dist/types/data-structures/rb-tree.js.map +1 -0
  173. package/dist/types/data-structures/segment-tree.js +1 -0
  174. package/dist/types/data-structures/segment-tree.js.map +1 -0
  175. package/dist/types/data-structures/singly-linked-list.js +1 -0
  176. package/dist/types/data-structures/singly-linked-list.js.map +1 -0
  177. package/dist/types/data-structures/tree-multiset.js +1 -0
  178. package/dist/types/data-structures/tree-multiset.js.map +1 -0
  179. package/dist/types/helpers.js +1 -0
  180. package/dist/types/helpers.js.map +1 -0
  181. package/dist/types/index.js +1 -0
  182. package/dist/types/index.js.map +1 -0
  183. package/dist/types/utils/index.js +1 -0
  184. package/dist/types/utils/index.js.map +1 -0
  185. package/dist/types/utils/utils.js +1 -0
  186. package/dist/types/utils/utils.js.map +1 -0
  187. package/dist/types/utils/validate-type.js +1 -0
  188. package/dist/types/utils/validate-type.js.map +1 -0
  189. package/dist/utils/index.js +1 -0
  190. package/dist/utils/index.js.map +1 -0
  191. package/dist/utils/utils.js +108 -22
  192. package/dist/utils/utils.js.map +1 -0
  193. package/docs/.nojekyll +1 -0
  194. package/docs/assets/highlight.css +127 -0
  195. package/docs/assets/main.js +58 -0
  196. package/docs/assets/search.js +1 -0
  197. package/docs/assets/style.css +1367 -0
  198. package/docs/classes/AVLTree.html +2259 -0
  199. package/docs/classes/AVLTreeNode.html +399 -0
  200. package/docs/classes/AaTree.html +202 -0
  201. package/docs/classes/AbstractBinaryTree.html +1913 -0
  202. package/docs/classes/AbstractBinaryTreeNode.html +441 -0
  203. package/docs/classes/AbstractEdge.html +345 -0
  204. package/docs/classes/AbstractGraph.html +1105 -0
  205. package/docs/classes/AbstractVertex.html +299 -0
  206. package/docs/classes/ArrayDeque.html +469 -0
  207. package/docs/classes/BST.html +2103 -0
  208. package/docs/classes/BSTNode.html +400 -0
  209. package/docs/classes/BTree.html +202 -0
  210. package/docs/classes/BinaryIndexedTree.html +371 -0
  211. package/docs/classes/BinaryTree.html +1944 -0
  212. package/docs/classes/BinaryTreeNode.html +399 -0
  213. package/docs/classes/Character.html +250 -0
  214. package/docs/classes/CoordinateMap.html +513 -0
  215. package/docs/classes/CoordinateSet.html +474 -0
  216. package/docs/classes/Deque.html +1005 -0
  217. package/docs/classes/DirectedEdge.html +404 -0
  218. package/docs/classes/DirectedGraph.html +1530 -0
  219. package/docs/classes/DirectedVertex.html +286 -0
  220. package/docs/classes/DoublyLinkedList.html +998 -0
  221. package/docs/classes/DoublyLinkedListNode.html +327 -0
  222. package/docs/classes/HashTable.html +202 -0
  223. package/docs/classes/Heap.html +647 -0
  224. package/docs/classes/HeapItem.html +296 -0
  225. package/docs/classes/LinkedListQueue.html +884 -0
  226. package/docs/classes/MapEdge.html +391 -0
  227. package/docs/classes/MapGraph.html +1583 -0
  228. package/docs/classes/MapVertex.html +356 -0
  229. package/docs/classes/Matrix2D.html +532 -0
  230. package/docs/classes/MatrixNTI2D.html +270 -0
  231. package/docs/classes/MaxHeap.html +671 -0
  232. package/docs/classes/MaxPriorityQueue.html +866 -0
  233. package/docs/classes/MinHeap.html +672 -0
  234. package/docs/classes/MinPriorityQueue.html +868 -0
  235. package/docs/classes/Navigator.html +343 -0
  236. package/docs/classes/ObjectDeque.html +527 -0
  237. package/docs/classes/Pair.html +202 -0
  238. package/docs/classes/PriorityQueue.html +790 -0
  239. package/docs/classes/Queue.html +521 -0
  240. package/docs/classes/RBTree.html +2101 -0
  241. package/docs/classes/RBTreeNode.html +431 -0
  242. package/docs/classes/SegmentTree.html +464 -0
  243. package/docs/classes/SegmentTreeNode.html +387 -0
  244. package/docs/classes/SinglyLinkedList.html +830 -0
  245. package/docs/classes/SinglyLinkedListNode.html +300 -0
  246. package/docs/classes/SkipLinkedList.html +202 -0
  247. package/docs/classes/SplayTree.html +202 -0
  248. package/docs/classes/Stack.html +398 -0
  249. package/docs/classes/TreeMap.html +202 -0
  250. package/docs/classes/TreeMultiset.html +2587 -0
  251. package/docs/classes/TreeMultisetNode.html +447 -0
  252. package/docs/classes/TreeNode.html +344 -0
  253. package/docs/classes/TreeSet.html +202 -0
  254. package/docs/classes/Trie.html +402 -0
  255. package/docs/classes/TrieNode.html +310 -0
  256. package/docs/classes/TwoThreeTree.html +202 -0
  257. package/docs/classes/UndirectedEdge.html +374 -0
  258. package/docs/classes/UndirectedGraph.html +1285 -0
  259. package/docs/classes/UndirectedVertex.html +284 -0
  260. package/docs/classes/Vector2D.html +835 -0
  261. package/docs/enums/CP.html +211 -0
  262. package/docs/enums/FamilyPosition.html +239 -0
  263. package/docs/enums/LoopType.html +212 -0
  264. package/docs/enums/RBColor.html +204 -0
  265. package/docs/enums/TopologicalProperty.html +211 -0
  266. package/docs/functions/arrayRemove.html +208 -0
  267. package/docs/functions/isThunk.html +186 -0
  268. package/docs/functions/toThunk.html +186 -0
  269. package/docs/functions/trampoline.html +186 -0
  270. package/docs/functions/trampolineAsync.html +186 -0
  271. package/docs/functions/uuidV4.html +181 -0
  272. package/docs/index.html +693 -0
  273. package/docs/interfaces/IAVLTree.html +1245 -0
  274. package/docs/interfaces/IAbstractBinaryTree.html +1101 -0
  275. package/docs/interfaces/IAbstractBinaryTreeNode.html +335 -0
  276. package/docs/interfaces/IAbstractGraph.html +433 -0
  277. package/docs/interfaces/IBST.html +1245 -0
  278. package/docs/interfaces/IDirectedGraph.html +570 -0
  279. package/docs/interfaces/IRBTree.html +1247 -0
  280. package/docs/interfaces/IUNDirectedGraph.html +463 -0
  281. package/docs/modules.html +328 -0
  282. package/docs/types/AVLTreeNodeNested.html +182 -0
  283. package/docs/types/AVLTreeOptions.html +180 -0
  284. package/docs/types/AbstractBinaryTreeNodeNested.html +182 -0
  285. package/docs/types/AbstractBinaryTreeNodeProperties.html +182 -0
  286. package/docs/types/AbstractBinaryTreeNodeProperty.html +182 -0
  287. package/docs/types/AbstractBinaryTreeOptions.html +182 -0
  288. package/docs/types/BSTComparator.html +192 -0
  289. package/docs/types/BSTNodeNested.html +182 -0
  290. package/docs/types/BSTOptions.html +182 -0
  291. package/docs/types/BinaryTreeDeletedResult.html +189 -0
  292. package/docs/types/BinaryTreeNodeId.html +177 -0
  293. package/docs/types/BinaryTreeNodeNested.html +182 -0
  294. package/docs/types/BinaryTreeNodePropertyName.html +177 -0
  295. package/docs/types/BinaryTreeOptions.html +180 -0
  296. package/docs/types/DFSOrderPattern.html +177 -0
  297. package/docs/types/DijkstraResult.html +199 -0
  298. package/docs/types/Direction.html +177 -0
  299. package/docs/types/DummyAny.html +190 -0
  300. package/docs/types/EdgeId.html +177 -0
  301. package/docs/types/HeapOptions.html +198 -0
  302. package/docs/types/IAVLTreeNode.html +184 -0
  303. package/docs/types/IBSTNode.html +184 -0
  304. package/docs/types/IBinaryTree.html +182 -0
  305. package/docs/types/IBinaryTreeNode.html +184 -0
  306. package/docs/types/IRBTreeNode.html +184 -0
  307. package/docs/types/ITreeMultiset.html +182 -0
  308. package/docs/types/ITreeMultisetNode.html +184 -0
  309. package/docs/types/KeyValueObject.html +182 -0
  310. package/docs/types/KeyValueObjectWithId.html +184 -0
  311. package/docs/types/MapGraphCoordinate.html +177 -0
  312. package/docs/types/NavigatorParams.html +211 -0
  313. package/docs/types/NodeOrPropertyName.html +177 -0
  314. package/docs/types/NonNumberNonObjectButDefined.html +177 -0
  315. package/docs/types/ObjectWithNonNumberId.html +184 -0
  316. package/docs/types/ObjectWithNumberId.html +184 -0
  317. package/docs/types/ObjectWithoutId.html +177 -0
  318. package/docs/types/PriorityQueueComparator.html +197 -0
  319. package/docs/types/PriorityQueueDFSOrderPattern.html +177 -0
  320. package/docs/types/PriorityQueueOptions.html +191 -0
  321. package/docs/types/RBTreeNodeNested.html +182 -0
  322. package/docs/types/RBTreeOptions.html +180 -0
  323. package/docs/types/RestrictValById.html +177 -0
  324. package/docs/types/SegmentTreeNodeVal.html +177 -0
  325. package/docs/types/SpecifyOptional.html +184 -0
  326. package/docs/types/Thunk.html +185 -0
  327. package/docs/types/ToThunkFn.html +185 -0
  328. package/docs/types/TopologicalStatus.html +177 -0
  329. package/docs/types/TreeMultisetNodeNested.html +182 -0
  330. package/docs/types/TreeMultisetOptions.html +180 -0
  331. package/docs/types/TrlAsyncFn.html +190 -0
  332. package/docs/types/TrlFn.html +190 -0
  333. package/docs/types/Turning.html +177 -0
  334. package/docs/types/VertexId.html +177 -0
  335. package/docs/variables/THUNK_SYMBOL.html +177 -0
  336. package/jest.config.js +5 -0
  337. package/lib/data-structures/binary-tree/aa-tree.js +2 -0
  338. package/{dist → lib}/data-structures/binary-tree/abstract-binary-tree.d.ts +25 -20
  339. package/lib/data-structures/binary-tree/abstract-binary-tree.js +1307 -0
  340. package/{dist → lib}/data-structures/binary-tree/avl-tree.d.ts +12 -12
  341. package/lib/data-structures/binary-tree/avl-tree.js +311 -0
  342. package/lib/data-structures/binary-tree/b-tree.js +2 -0
  343. package/lib/data-structures/binary-tree/binary-indexed-tree.js +69 -0
  344. package/lib/data-structures/binary-tree/binary-tree.js +35 -0
  345. package/{dist → lib}/data-structures/binary-tree/bst.d.ts +19 -8
  346. package/lib/data-structures/binary-tree/bst.js +551 -0
  347. package/lib/data-structures/binary-tree/index.js +12 -0
  348. package/{dist → lib}/data-structures/binary-tree/rb-tree.d.ts +0 -6
  349. package/lib/data-structures/binary-tree/rb-tree.js +22 -0
  350. package/lib/data-structures/binary-tree/segment-tree.js +210 -0
  351. package/lib/data-structures/binary-tree/splay-tree.js +2 -0
  352. package/{dist → lib}/data-structures/binary-tree/tree-multiset.d.ts +20 -23
  353. package/lib/data-structures/binary-tree/tree-multiset.js +673 -0
  354. package/lib/data-structures/binary-tree/two-three-tree.js +2 -0
  355. package/lib/data-structures/graph/abstract-graph.js +918 -0
  356. package/lib/data-structures/graph/directed-graph.js +416 -0
  357. package/lib/data-structures/graph/index.js +4 -0
  358. package/lib/data-structures/graph/map-graph.js +105 -0
  359. package/lib/data-structures/graph/undirected-graph.js +246 -0
  360. package/lib/data-structures/hash/coordinate-map.js +61 -0
  361. package/lib/data-structures/hash/coordinate-set.js +51 -0
  362. package/lib/data-structures/hash/hash-table.js +2 -0
  363. package/lib/data-structures/hash/index.js +6 -0
  364. package/lib/data-structures/hash/pair.js +2 -0
  365. package/lib/data-structures/hash/tree-map.js +2 -0
  366. package/lib/data-structures/hash/tree-set.js +2 -0
  367. package/lib/data-structures/heap/heap.js +152 -0
  368. package/lib/data-structures/heap/index.js +3 -0
  369. package/lib/data-structures/heap/max-heap.js +26 -0
  370. package/lib/data-structures/heap/min-heap.js +27 -0
  371. package/lib/data-structures/index.js +11 -0
  372. package/{dist → lib}/data-structures/linked-list/doubly-linked-list.d.ts +5 -5
  373. package/lib/data-structures/linked-list/doubly-linked-list.js +521 -0
  374. package/lib/data-structures/linked-list/index.js +3 -0
  375. package/{dist → lib}/data-structures/linked-list/singly-linked-list.d.ts +6 -5
  376. package/lib/data-structures/linked-list/singly-linked-list.js +443 -0
  377. package/lib/data-structures/linked-list/skip-linked-list.js +2 -0
  378. package/lib/data-structures/matrix/index.js +4 -0
  379. package/lib/data-structures/matrix/matrix.js +24 -0
  380. package/lib/data-structures/matrix/matrix2d.js +195 -0
  381. package/lib/data-structures/matrix/navigator.js +101 -0
  382. package/lib/data-structures/matrix/vector2d.js +287 -0
  383. package/lib/data-structures/priority-queue/index.js +3 -0
  384. package/lib/data-structures/priority-queue/max-priority-queue.js +39 -0
  385. package/lib/data-structures/priority-queue/min-priority-queue.js +40 -0
  386. package/lib/data-structures/priority-queue/priority-queue.js +317 -0
  387. package/{dist → lib}/data-structures/queue/deque.d.ts +45 -0
  388. package/lib/data-structures/queue/deque.js +270 -0
  389. package/lib/data-structures/queue/index.js +2 -0
  390. package/{dist → lib}/data-structures/queue/queue.d.ts +47 -13
  391. package/lib/data-structures/queue/queue.js +165 -0
  392. package/lib/data-structures/stack/index.js +1 -0
  393. package/lib/data-structures/stack/stack.js +87 -0
  394. package/lib/data-structures/tree/index.js +1 -0
  395. package/lib/data-structures/tree/tree.js +56 -0
  396. package/lib/data-structures/trie/index.js +1 -0
  397. package/lib/data-structures/trie/trie.js +205 -0
  398. package/lib/index.js +4 -0
  399. package/{dist → lib}/interfaces/avl-tree.d.ts +1 -9
  400. package/lib/interfaces/binary-tree.d.ts +4 -0
  401. package/{dist → lib}/interfaces/bst.d.ts +1 -2
  402. package/lib/interfaces/heap.js +1 -0
  403. package/lib/interfaces/index.js +15 -0
  404. package/lib/interfaces/navigator.d.ts +1 -0
  405. package/lib/interfaces/navigator.js +1 -0
  406. package/lib/interfaces/priority-queue.d.ts +1 -0
  407. package/lib/interfaces/priority-queue.js +1 -0
  408. package/{dist → lib}/interfaces/rb-tree.d.ts +1 -2
  409. package/lib/interfaces/rb-tree.js +1 -0
  410. package/lib/interfaces/segment-tree.d.ts +1 -0
  411. package/lib/interfaces/segment-tree.js +1 -0
  412. package/lib/interfaces/singly-linked-list.d.ts +1 -0
  413. package/lib/interfaces/singly-linked-list.js +1 -0
  414. package/lib/interfaces/tree-multiset.d.ts +5 -0
  415. package/lib/interfaces/tree-multiset.js +1 -0
  416. package/lib/interfaces/undirected-graph.js +1 -0
  417. package/{dist → lib}/types/data-structures/abstract-binary-tree.d.ts +1 -1
  418. package/lib/types/data-structures/abstract-binary-tree.js +22 -0
  419. package/lib/types/data-structures/abstract-graph.js +1 -0
  420. package/{dist → lib}/types/data-structures/avl-tree.d.ts +1 -1
  421. package/lib/types/data-structures/avl-tree.js +1 -0
  422. package/lib/types/data-structures/binary-tree.js +1 -0
  423. package/lib/types/data-structures/bst.js +6 -0
  424. package/lib/types/data-structures/directed-graph.js +6 -0
  425. package/lib/types/data-structures/doubly-linked-list.d.ts +1 -0
  426. package/lib/types/data-structures/doubly-linked-list.js +1 -0
  427. package/lib/types/data-structures/heap.js +1 -0
  428. package/lib/types/data-structures/index.js +15 -0
  429. package/lib/types/data-structures/map-graph.js +1 -0
  430. package/lib/types/data-structures/navigator.js +1 -0
  431. package/lib/types/data-structures/priority-queue.js +1 -0
  432. package/lib/types/data-structures/rb-tree.js +5 -0
  433. package/lib/types/data-structures/segment-tree.js +1 -0
  434. package/lib/types/data-structures/singly-linked-list.d.ts +1 -0
  435. package/lib/types/data-structures/singly-linked-list.js +1 -0
  436. package/lib/types/data-structures/tree-multiset.js +1 -0
  437. package/lib/types/helpers.d.ts +1 -0
  438. package/lib/types/helpers.js +1 -0
  439. package/lib/types/index.js +3 -0
  440. package/lib/types/utils/index.js +2 -0
  441. package/{dist → lib}/types/utils/utils.d.ts +1 -1
  442. package/lib/types/utils/utils.js +1 -0
  443. package/{dist → lib}/types/utils/validate-type.d.ts +1 -1
  444. package/lib/types/utils/validate-type.js +1 -0
  445. package/lib/utils/index.js +1 -0
  446. package/lib/utils/utils.js +57 -0
  447. package/package.json +147 -56
  448. package/rename_clear_files.sh +29 -0
  449. package/test/integration/avl-tree.test.ts +111 -0
  450. package/test/integration/bst.test.ts +371 -0
  451. package/test/integration/heap.test.js +19 -0
  452. package/test/integration/index.html +44 -0
  453. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +111 -0
  454. package/test/unit/data-structures/binary-tree/bst.test.ts +371 -0
  455. package/test/unit/data-structures/binary-tree/overall.test.ts +57 -0
  456. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +405 -0
  457. package/test/unit/data-structures/graph/abstract-graph.test.ts +5 -0
  458. package/test/unit/data-structures/graph/directed-graph.test.ts +517 -0
  459. package/test/unit/data-structures/graph/index.ts +2 -0
  460. package/test/unit/data-structures/graph/map-graph.test.ts +46 -0
  461. package/test/unit/data-structures/graph/overall.test.ts +50 -0
  462. package/test/unit/data-structures/graph/undirected-graph.test.ts +60 -0
  463. package/test/unit/data-structures/heap/heap.test.ts +56 -0
  464. package/test/unit/data-structures/heap/max-heap.test.ts +42 -0
  465. package/test/unit/data-structures/heap/min-heap.test.ts +81 -0
  466. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +365 -0
  467. package/test/unit/data-structures/linked-list/index.ts +4 -0
  468. package/test/unit/data-structures/linked-list/linked-list.test.ts +37 -0
  469. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +401 -0
  470. package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
  471. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +106 -0
  472. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +105 -0
  473. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +27 -0
  474. package/test/unit/data-structures/queue/queue.test.ts +36 -0
  475. package/test/utils/index.ts +2 -0
  476. package/test/utils/magnitude.ts +21 -0
  477. package/test/utils/number.ts +3 -0
  478. package/tsconfig.build.json +33 -0
  479. package/tsconfig.json +38 -0
  480. package/umd/bundle.min.js +3 -0
  481. package/umd/bundle.min.js.map +1 -0
  482. package/webpack.config.js +28 -0
  483. package/dist/bundle.js +0 -2
  484. package/dist/interfaces/binary-tree.d.ts +0 -6
  485. package/dist/interfaces/tree-multiset.d.ts +0 -7
  486. /package/{dist → lib}/data-structures/binary-tree/aa-tree.d.ts +0 -0
  487. /package/{dist → lib}/data-structures/binary-tree/b-tree.d.ts +0 -0
  488. /package/{dist → lib}/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
  489. /package/{dist → lib}/data-structures/binary-tree/binary-tree.d.ts +0 -0
  490. /package/{dist → lib}/data-structures/binary-tree/index.d.ts +0 -0
  491. /package/{dist → lib}/data-structures/binary-tree/segment-tree.d.ts +0 -0
  492. /package/{dist → lib}/data-structures/binary-tree/splay-tree.d.ts +0 -0
  493. /package/{dist → lib}/data-structures/binary-tree/two-three-tree.d.ts +0 -0
  494. /package/{dist → lib}/data-structures/graph/abstract-graph.d.ts +0 -0
  495. /package/{dist → lib}/data-structures/graph/directed-graph.d.ts +0 -0
  496. /package/{dist → lib}/data-structures/graph/index.d.ts +0 -0
  497. /package/{dist → lib}/data-structures/graph/map-graph.d.ts +0 -0
  498. /package/{dist → lib}/data-structures/graph/undirected-graph.d.ts +0 -0
  499. /package/{dist → lib}/data-structures/hash/coordinate-map.d.ts +0 -0
  500. /package/{dist → lib}/data-structures/hash/coordinate-set.d.ts +0 -0
  501. /package/{dist → lib}/data-structures/hash/hash-table.d.ts +0 -0
  502. /package/{dist → lib}/data-structures/hash/index.d.ts +0 -0
  503. /package/{dist → lib}/data-structures/hash/pair.d.ts +0 -0
  504. /package/{dist → lib}/data-structures/hash/tree-map.d.ts +0 -0
  505. /package/{dist → lib}/data-structures/hash/tree-set.d.ts +0 -0
  506. /package/{dist → lib}/data-structures/heap/heap.d.ts +0 -0
  507. /package/{dist → lib}/data-structures/heap/index.d.ts +0 -0
  508. /package/{dist → lib}/data-structures/heap/max-heap.d.ts +0 -0
  509. /package/{dist → lib}/data-structures/heap/min-heap.d.ts +0 -0
  510. /package/{dist → lib}/data-structures/index.d.ts +0 -0
  511. /package/{dist → lib}/data-structures/linked-list/index.d.ts +0 -0
  512. /package/{dist → lib}/data-structures/linked-list/skip-linked-list.d.ts +0 -0
  513. /package/{dist → lib}/data-structures/matrix/index.d.ts +0 -0
  514. /package/{dist → lib}/data-structures/matrix/matrix.d.ts +0 -0
  515. /package/{dist → lib}/data-structures/matrix/matrix2d.d.ts +0 -0
  516. /package/{dist → lib}/data-structures/matrix/navigator.d.ts +0 -0
  517. /package/{dist → lib}/data-structures/matrix/vector2d.d.ts +0 -0
  518. /package/{dist → lib}/data-structures/priority-queue/index.d.ts +0 -0
  519. /package/{dist → lib}/data-structures/priority-queue/max-priority-queue.d.ts +0 -0
  520. /package/{dist → lib}/data-structures/priority-queue/min-priority-queue.d.ts +0 -0
  521. /package/{dist → lib}/data-structures/priority-queue/priority-queue.d.ts +0 -0
  522. /package/{dist → lib}/data-structures/queue/index.d.ts +0 -0
  523. /package/{dist → lib}/data-structures/stack/index.d.ts +0 -0
  524. /package/{dist → lib}/data-structures/stack/stack.d.ts +0 -0
  525. /package/{dist → lib}/data-structures/tree/index.d.ts +0 -0
  526. /package/{dist → lib}/data-structures/tree/tree.d.ts +0 -0
  527. /package/{dist → lib}/data-structures/trie/index.d.ts +0 -0
  528. /package/{dist → lib}/data-structures/trie/trie.d.ts +0 -0
  529. /package/{dist → lib}/index.d.ts +0 -0
  530. /package/{dist → lib}/interfaces/abstract-binary-tree.d.ts +0 -0
  531. /package/{dist/interfaces/doubly-linked-list.d.ts → lib/interfaces/abstract-binary-tree.js} +0 -0
  532. /package/{dist → lib}/interfaces/abstract-graph.d.ts +0 -0
  533. /package/{dist/interfaces/heap.d.ts → lib/interfaces/abstract-graph.js} +0 -0
  534. /package/{dist/interfaces/navigator.d.ts → lib/interfaces/avl-tree.js} +0 -0
  535. /package/{dist/interfaces/priority-queue.d.ts → lib/interfaces/binary-tree.js} +0 -0
  536. /package/{dist/interfaces/segment-tree.d.ts → lib/interfaces/bst.js} +0 -0
  537. /package/{dist → lib}/interfaces/directed-graph.d.ts +0 -0
  538. /package/{dist/interfaces/singly-linked-list.d.ts → lib/interfaces/directed-graph.js} +0 -0
  539. /package/{dist/types/data-structures → lib/interfaces}/doubly-linked-list.d.ts +0 -0
  540. /package/{dist/types/data-structures/singly-linked-list.d.ts → lib/interfaces/doubly-linked-list.js} +0 -0
  541. /package/{dist/types/helpers.d.ts → lib/interfaces/heap.d.ts} +0 -0
  542. /package/{dist → lib}/interfaces/index.d.ts +0 -0
  543. /package/{dist → lib}/interfaces/undirected-graph.d.ts +0 -0
  544. /package/{dist → lib}/types/data-structures/abstract-graph.d.ts +0 -0
  545. /package/{dist → lib}/types/data-structures/binary-tree.d.ts +0 -0
  546. /package/{dist → lib}/types/data-structures/bst.d.ts +0 -0
  547. /package/{dist → lib}/types/data-structures/directed-graph.d.ts +0 -0
  548. /package/{dist → lib}/types/data-structures/heap.d.ts +0 -0
  549. /package/{dist → lib}/types/data-structures/index.d.ts +0 -0
  550. /package/{dist → lib}/types/data-structures/map-graph.d.ts +0 -0
  551. /package/{dist → lib}/types/data-structures/navigator.d.ts +0 -0
  552. /package/{dist → lib}/types/data-structures/priority-queue.d.ts +0 -0
  553. /package/{dist → lib}/types/data-structures/rb-tree.d.ts +0 -0
  554. /package/{dist → lib}/types/data-structures/segment-tree.d.ts +0 -0
  555. /package/{dist → lib}/types/data-structures/tree-multiset.d.ts +0 -0
  556. /package/{dist → lib}/types/index.d.ts +0 -0
  557. /package/{dist → lib}/types/utils/index.d.ts +0 -0
  558. /package/{dist → lib}/utils/index.d.ts +0 -0
  559. /package/{dist → lib}/utils/utils.d.ts +0 -0
  560. /package/{dist/bundle.js.LICENSE.txt → umd/bundle.min.js.LICENSE.txt} +0 -0
@@ -0,0 +1,551 @@
1
+ import { CP, LoopType } from '../../types';
2
+ import { BinaryTree, BinaryTreeNode } from './binary-tree';
3
+ export class BSTNode extends BinaryTreeNode {
4
+ constructor(id, val) {
5
+ super(id, val);
6
+ }
7
+ }
8
+ export class BST extends BinaryTree {
9
+ /**
10
+ * The constructor function initializes a binary search tree object with an optional comparator function.
11
+ * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
12
+ */
13
+ constructor(options) {
14
+ super(options);
15
+ this._comparator = (a, b) => a - b;
16
+ if (options !== undefined) {
17
+ const { comparator } = options;
18
+ if (comparator !== undefined) {
19
+ this._comparator = comparator;
20
+ }
21
+ }
22
+ }
23
+ /**
24
+ * The function creates a new binary search tree node with the given id and value.
25
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
26
+ * identify each node in the binary tree.
27
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
28
+ * that will be stored in the node.
29
+ * @returns a new instance of the BSTNode class with the specified id and value.
30
+ */
31
+ createNode(id, val) {
32
+ return new BSTNode(id, val);
33
+ }
34
+ /**
35
+ * The `add` function adds a new node to a binary search tree, either by creating a new node or by updating an existing
36
+ * node with the same ID.
37
+ * @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId` or a `N`
38
+ * (which represents a binary tree node) or `null`.
39
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
40
+ * being added to the binary search tree.
41
+ * @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
42
+ */
43
+ add(idOrNode, val) {
44
+ // TODO support node as a param
45
+ let inserted = null;
46
+ let newNode = null;
47
+ if (idOrNode instanceof BSTNode) {
48
+ newNode = idOrNode;
49
+ }
50
+ else if (typeof idOrNode === 'number') {
51
+ newNode = this.createNode(idOrNode, val);
52
+ }
53
+ else if (idOrNode === null) {
54
+ newNode = null;
55
+ }
56
+ if (this.root === null) {
57
+ this._setRoot(newNode);
58
+ this._setSize(this.size + 1);
59
+ inserted = this.root;
60
+ }
61
+ else {
62
+ let cur = this.root;
63
+ let traversing = true;
64
+ while (traversing) {
65
+ if (cur !== null && newNode !== null) {
66
+ if (this._compare(cur.id, newNode.id) === CP.eq) {
67
+ if (newNode) {
68
+ cur.val = newNode.val;
69
+ }
70
+ //Duplicates are not accepted.
71
+ traversing = false;
72
+ inserted = cur;
73
+ }
74
+ else if (this._compare(cur.id, newNode.id) === CP.gt) {
75
+ // Traverse left of the node
76
+ if (cur.left === undefined) {
77
+ if (newNode) {
78
+ newNode.parent = cur;
79
+ }
80
+ //Add to the left of the current node
81
+ cur.left = newNode;
82
+ this._setSize(this.size + 1);
83
+ traversing = false;
84
+ inserted = cur.left;
85
+ }
86
+ else {
87
+ //Traverse the left of the current node
88
+ if (cur.left)
89
+ cur = cur.left;
90
+ }
91
+ }
92
+ else if (this._compare(cur.id, newNode.id) === CP.lt) {
93
+ // Traverse right of the node
94
+ if (cur.right === undefined) {
95
+ if (newNode) {
96
+ newNode.parent = cur;
97
+ }
98
+ //Add to the right of the current node
99
+ cur.right = newNode;
100
+ this._setSize(this.size + 1);
101
+ traversing = false;
102
+ inserted = cur.right;
103
+ }
104
+ else {
105
+ //Traverse the left of the current node
106
+ if (cur.right)
107
+ cur = cur.right;
108
+ }
109
+ }
110
+ }
111
+ else {
112
+ traversing = false;
113
+ }
114
+ }
115
+ }
116
+ return inserted;
117
+ }
118
+ /**
119
+ * The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
120
+ * manner.
121
+ * @param {[BinaryTreeNodeId | N , N['val']][]} idsOrNodes - The `idsOrNodes` parameter in the `addMany` function is an array of
122
+ * `BinaryTreeNodeId` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
123
+ * to the binary search tree.
124
+ * @param {N['val'][]} data - The values of tree nodes
125
+ * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
126
+ * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
127
+ */
128
+ addMany(idsOrNodes, data, isBalanceAdd = false) {
129
+ function hasNoNull(arr) {
130
+ return arr.indexOf(null) === -1;
131
+ }
132
+ if (!isBalanceAdd || !hasNoNull(idsOrNodes)) {
133
+ return super.addMany(idsOrNodes, data);
134
+ }
135
+ const inserted = [];
136
+ const combinedArr = idsOrNodes.map((value, index) => [value, data === null || data === void 0 ? void 0 : data[index]]);
137
+ let sorted = [];
138
+ function isNodeOrNullTuple(arr) {
139
+ for (const [idOrNode] of arr)
140
+ if (idOrNode instanceof BSTNode)
141
+ return true;
142
+ return false;
143
+ }
144
+ function isBinaryTreeIdOrNullTuple(arr) {
145
+ for (const [idOrNode] of arr)
146
+ if (typeof idOrNode === 'number')
147
+ return true;
148
+ return false;
149
+ }
150
+ let sortedIdsOrNodes = [], sortedData = [];
151
+ if (isNodeOrNullTuple(combinedArr)) {
152
+ sorted = combinedArr.sort((a, b) => a[0].id - b[0].id);
153
+ }
154
+ else if (isBinaryTreeIdOrNullTuple(combinedArr)) {
155
+ sorted = combinedArr.sort((a, b) => a[0] - b[0]);
156
+ }
157
+ else {
158
+ throw new Error('Invalid input idsOrNodes');
159
+ }
160
+ sortedIdsOrNodes = sorted.map(([idOrNode]) => idOrNode);
161
+ sortedData = sorted.map(([, val]) => val);
162
+ const recursive = (arr, data) => {
163
+ if (arr.length === 0)
164
+ return;
165
+ const mid = Math.floor((arr.length - 1) / 2);
166
+ const newNode = this.add(arr[mid], data === null || data === void 0 ? void 0 : data[mid]);
167
+ inserted.push(newNode);
168
+ recursive(arr.slice(0, mid), data === null || data === void 0 ? void 0 : data.slice(0, mid));
169
+ recursive(arr.slice(mid + 1), data === null || data === void 0 ? void 0 : data.slice(mid + 1));
170
+ };
171
+ const iterative = () => {
172
+ const n = sorted.length;
173
+ const stack = [[0, n - 1]];
174
+ while (stack.length > 0) {
175
+ const popped = stack.pop();
176
+ if (popped) {
177
+ const [l, r] = popped;
178
+ if (l <= r) {
179
+ const m = l + Math.floor((r - l) / 2);
180
+ const newNode = this.add(sortedIdsOrNodes[m], sortedData === null || sortedData === void 0 ? void 0 : sortedData[m]);
181
+ inserted.push(newNode);
182
+ stack.push([m + 1, r]);
183
+ stack.push([l, m - 1]);
184
+ }
185
+ }
186
+ }
187
+ };
188
+ if (this.loopType === LoopType.RECURSIVE) {
189
+ recursive(sortedIdsOrNodes, sortedData);
190
+ }
191
+ else {
192
+ iterative();
193
+ }
194
+ return inserted;
195
+ }
196
+ /**
197
+ * The function returns the first node in a binary tree that matches the given property name and value.
198
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
199
+ * generic type `N`. It represents the property of the binary tree node that you want to search for.
200
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
201
+ * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
202
+ * @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
203
+ */
204
+ get(nodeProperty, propertyName) {
205
+ var _a;
206
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
207
+ return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
208
+ }
209
+ /**
210
+ * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
211
+ * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
212
+ * @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
213
+ * the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
214
+ * equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
215
+ */
216
+ lastKey() {
217
+ var _a, _b, _c, _d, _e, _f;
218
+ if (this._compare(0, 1) === CP.lt)
219
+ return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
220
+ else if (this._compare(0, 1) === CP.gt)
221
+ return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
222
+ else
223
+ return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
224
+ }
225
+ /**
226
+ * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
227
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
228
+ * `N` type. It represents the property of the binary tree node that you want to compare with.
229
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
230
+ * specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
231
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
232
+ * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
233
+ * is set to `true`, the function will return an array with only one node (if
234
+ * @returns an array of nodes (type N).
235
+ */
236
+ getNodes(nodeProperty, propertyName = 'id', onlyOne) {
237
+ if (!this.root)
238
+ return [];
239
+ const result = [];
240
+ if (this.loopType === LoopType.RECURSIVE) {
241
+ const _traverse = (cur) => {
242
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
243
+ return;
244
+ if (!cur.left && !cur.right)
245
+ return;
246
+ if (propertyName === 'id') {
247
+ if (this._compare(cur.id, nodeProperty) === CP.gt)
248
+ cur.left && _traverse(cur.left);
249
+ if (this._compare(cur.id, nodeProperty) === CP.lt)
250
+ cur.right && _traverse(cur.right);
251
+ }
252
+ else {
253
+ cur.left && _traverse(cur.left);
254
+ cur.right && _traverse(cur.right);
255
+ }
256
+ };
257
+ _traverse(this.root);
258
+ }
259
+ else {
260
+ const queue = [this.root];
261
+ while (queue.length > 0) {
262
+ const cur = queue.shift();
263
+ if (cur) {
264
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
265
+ return result;
266
+ if (propertyName === 'id') {
267
+ if (this._compare(cur.id, nodeProperty) === CP.gt)
268
+ cur.left && queue.push(cur.left);
269
+ if (this._compare(cur.id, nodeProperty) === CP.lt)
270
+ cur.right && queue.push(cur.right);
271
+ }
272
+ else {
273
+ cur.left && queue.push(cur.left);
274
+ cur.right && queue.push(cur.right);
275
+ }
276
+ }
277
+ }
278
+ }
279
+ return result;
280
+ }
281
+ // --- start additional functions
282
+ /**
283
+ * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
284
+ * less than a given node.
285
+ * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
286
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
287
+ * specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
288
+ * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
289
+ * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
290
+ */
291
+ lesserSum(beginNode, propertyName) {
292
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
293
+ if (typeof beginNode === 'number')
294
+ beginNode = this.get(beginNode, 'id');
295
+ if (!beginNode)
296
+ return 0;
297
+ if (!this.root)
298
+ return 0;
299
+ const id = beginNode.id;
300
+ const getSumByPropertyName = (cur) => {
301
+ let needSum;
302
+ switch (propertyName) {
303
+ case 'id':
304
+ needSum = cur.id;
305
+ break;
306
+ default:
307
+ needSum = cur.id;
308
+ break;
309
+ }
310
+ return needSum;
311
+ };
312
+ let sum = 0;
313
+ if (this.loopType === LoopType.RECURSIVE) {
314
+ const _traverse = (cur) => {
315
+ const compared = this._compare(cur.id, id);
316
+ if (compared === CP.eq) {
317
+ if (cur.right)
318
+ sum += this.subTreeSum(cur.right, propertyName);
319
+ return;
320
+ }
321
+ else if (compared === CP.lt) {
322
+ if (cur.left)
323
+ sum += this.subTreeSum(cur.left, propertyName);
324
+ sum += getSumByPropertyName(cur);
325
+ if (cur.right)
326
+ _traverse(cur.right);
327
+ else
328
+ return;
329
+ }
330
+ else {
331
+ if (cur.left)
332
+ _traverse(cur.left);
333
+ else
334
+ return;
335
+ }
336
+ };
337
+ _traverse(this.root);
338
+ }
339
+ else {
340
+ const queue = [this.root];
341
+ while (queue.length > 0) {
342
+ const cur = queue.shift();
343
+ if (cur) {
344
+ const compared = this._compare(cur.id, id);
345
+ if (compared === CP.eq) {
346
+ if (cur.right)
347
+ sum += this.subTreeSum(cur.right, propertyName);
348
+ return sum;
349
+ }
350
+ else if (compared === CP.lt) {
351
+ // todo maybe a bug
352
+ if (cur.left)
353
+ sum += this.subTreeSum(cur.left, propertyName);
354
+ sum += getSumByPropertyName(cur);
355
+ if (cur.right)
356
+ queue.push(cur.right);
357
+ else
358
+ return sum;
359
+ }
360
+ else {
361
+ if (cur.left)
362
+ queue.push(cur.left);
363
+ else
364
+ return sum;
365
+ }
366
+ }
367
+ }
368
+ }
369
+ return sum;
370
+ }
371
+ /**
372
+ * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
373
+ * have a greater value than a given node.
374
+ * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
375
+ * `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
376
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
377
+ * each greater node should be increased.
378
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
379
+ * specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
380
+ * 'id'.
381
+ * @returns a boolean value.
382
+ */
383
+ allGreaterNodesAdd(node, delta, propertyName) {
384
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
385
+ if (typeof node === 'number')
386
+ node = this.get(node, 'id');
387
+ if (!node)
388
+ return false;
389
+ const id = node.id;
390
+ if (!this.root)
391
+ return false;
392
+ const _sumByPropertyName = (cur) => {
393
+ switch (propertyName) {
394
+ case 'id':
395
+ cur.id += delta;
396
+ break;
397
+ default:
398
+ cur.id += delta;
399
+ break;
400
+ }
401
+ };
402
+ if (this.loopType === LoopType.RECURSIVE) {
403
+ const _traverse = (cur) => {
404
+ const compared = this._compare(cur.id, id);
405
+ if (compared === CP.gt)
406
+ _sumByPropertyName(cur);
407
+ if (!cur.left && !cur.right)
408
+ return;
409
+ if (cur.left && this._compare(cur.left.id, id) === CP.gt)
410
+ _traverse(cur.left);
411
+ if (cur.right && this._compare(cur.right.id, id) === CP.gt)
412
+ _traverse(cur.right);
413
+ };
414
+ _traverse(this.root);
415
+ return true;
416
+ }
417
+ else {
418
+ const queue = [this.root];
419
+ while (queue.length > 0) {
420
+ const cur = queue.shift();
421
+ if (cur) {
422
+ const compared = this._compare(cur.id, id);
423
+ if (compared === CP.gt)
424
+ _sumByPropertyName(cur);
425
+ if (cur.left && this._compare(cur.left.id, id) === CP.gt)
426
+ queue.push(cur.left);
427
+ if (cur.right && this._compare(cur.right.id, id) === CP.gt)
428
+ queue.push(cur.right);
429
+ }
430
+ }
431
+ return true;
432
+ }
433
+ }
434
+ /**
435
+ * Balancing Adjustment:
436
+ * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
437
+ * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
438
+ *
439
+ * Use Cases and Efficiency:
440
+ * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
441
+ * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
442
+ */
443
+ /**
444
+ * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
445
+ * constructs a balanced binary search tree using either a recursive or iterative approach.
446
+ * @returns The function `perfectlyBalance()` returns a boolean value.
447
+ */
448
+ perfectlyBalance() {
449
+ const sorted = this.DFS('in', 'node'), n = sorted.length;
450
+ this.clear();
451
+ if (sorted.length < 1)
452
+ return false;
453
+ if (this.loopType === LoopType.RECURSIVE) {
454
+ const buildBalanceBST = (l, r) => {
455
+ if (l > r)
456
+ return;
457
+ const m = l + Math.floor((r - l) / 2);
458
+ const midNode = sorted[m];
459
+ this.add(midNode.id, midNode.val);
460
+ buildBalanceBST(l, m - 1);
461
+ buildBalanceBST(m + 1, r);
462
+ };
463
+ buildBalanceBST(0, n - 1);
464
+ return true;
465
+ }
466
+ else {
467
+ const stack = [[0, n - 1]];
468
+ while (stack.length > 0) {
469
+ const popped = stack.pop();
470
+ if (popped) {
471
+ const [l, r] = popped;
472
+ if (l <= r) {
473
+ const m = l + Math.floor((r - l) / 2);
474
+ const midNode = sorted[m];
475
+ this.add(midNode.id, midNode.val);
476
+ stack.push([m + 1, r]);
477
+ stack.push([l, m - 1]);
478
+ }
479
+ }
480
+ }
481
+ return true;
482
+ }
483
+ }
484
+ /**
485
+ * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
486
+ * @returns a boolean value.
487
+ */
488
+ isAVLBalanced() {
489
+ var _a, _b;
490
+ if (!this.root)
491
+ return true;
492
+ let balanced = true;
493
+ if (this.loopType === LoopType.RECURSIVE) {
494
+ const _height = (cur) => {
495
+ if (!cur)
496
+ return 0;
497
+ const leftHeight = _height(cur.left), rightHeight = _height(cur.right);
498
+ if (Math.abs(leftHeight - rightHeight) > 1)
499
+ balanced = false;
500
+ return Math.max(leftHeight, rightHeight) + 1;
501
+ };
502
+ _height(this.root);
503
+ }
504
+ else {
505
+ const stack = [];
506
+ let node = this.root, last = null;
507
+ const depths = new Map();
508
+ while (stack.length > 0 || node) {
509
+ if (node) {
510
+ stack.push(node);
511
+ node = node.left;
512
+ }
513
+ else {
514
+ node = stack[stack.length - 1];
515
+ if (!node.right || last === node.right) {
516
+ node = stack.pop();
517
+ if (node) {
518
+ const left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
519
+ const right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
520
+ if (Math.abs(left - right) > 1)
521
+ return false;
522
+ depths.set(node, 1 + Math.max(left, right));
523
+ last = node;
524
+ node = null;
525
+ }
526
+ }
527
+ else
528
+ node = node.right;
529
+ }
530
+ }
531
+ }
532
+ return balanced;
533
+ }
534
+ /**
535
+ * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
536
+ * greater than, less than, or equal to the second ID.
537
+ * @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
538
+ * @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
539
+ * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
540
+ * than), or CP.eq (equal).
541
+ */
542
+ _compare(a, b) {
543
+ const compared = this._comparator(a, b);
544
+ if (compared > 0)
545
+ return CP.gt;
546
+ else if (compared < 0)
547
+ return CP.lt;
548
+ else
549
+ return CP.eq;
550
+ }
551
+ }
@@ -0,0 +1,12 @@
1
+ export * from './abstract-binary-tree';
2
+ export * from './binary-tree';
3
+ export * from './bst';
4
+ export * from './binary-indexed-tree';
5
+ export * from './segment-tree';
6
+ export * from './avl-tree';
7
+ export * from './b-tree';
8
+ export * from './rb-tree';
9
+ export * from './splay-tree';
10
+ export * from './aa-tree';
11
+ export * from './tree-multiset';
12
+ export * from './two-three-tree';
@@ -10,10 +10,4 @@ export declare class RBTreeNode<T = any, NEIGHBOR extends RBTreeNode<T, NEIGHBOR
10
10
  export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
11
11
  constructor(options?: RBTreeOptions);
12
12
  createNode(id: BinaryTreeNodeId, val?: N['val']): N;
13
- insert(id: number, val?: N | null): void;
14
- private leftRotate;
15
- private rightRotate;
16
- private insertFixup;
17
- private deleteFixup;
18
- private transplant;
19
13
  }
@@ -0,0 +1,22 @@
1
+ import { RBColor } from '../../types';
2
+ import { BST, BSTNode } from './bst';
3
+ export class RBTreeNode extends BSTNode {
4
+ constructor(id, val, color = RBColor.RED) {
5
+ super(id, val);
6
+ this._color = color;
7
+ }
8
+ get color() {
9
+ return this._color;
10
+ }
11
+ set color(value) {
12
+ this._color = value;
13
+ }
14
+ }
15
+ export class RBTree extends BST {
16
+ constructor(options) {
17
+ super(options);
18
+ }
19
+ createNode(id, val) {
20
+ return new RBTreeNode(id, val, RBColor.RED);
21
+ }
22
+ }