data-structure-typed 1.21.3 → 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 +150 -98
  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,1307 @@
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 { trampoline } from '../../utils';
9
+ import { FamilyPosition, LoopType } from '../../types';
10
+ export class AbstractBinaryTreeNode {
11
+ /**
12
+ * The constructor function initializes a BinaryTreeNode object with an id and an optional value.
13
+ * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
14
+ * of the binary tree node. It is used to distinguish one node from another in the binary tree.
15
+ * @param {T} [val] - The "val" parameter is an optional parameter of type T. It represents the value that will be
16
+ * stored in the binary tree node. If no value is provided, it will be set to undefined.
17
+ */
18
+ constructor(id, val) {
19
+ this._height = 0;
20
+ this._id = id;
21
+ this._val = val;
22
+ }
23
+ get id() {
24
+ return this._id;
25
+ }
26
+ set id(v) {
27
+ this._id = v;
28
+ }
29
+ get val() {
30
+ return this._val;
31
+ }
32
+ set val(value) {
33
+ this._val = value;
34
+ }
35
+ get left() {
36
+ return this._left;
37
+ }
38
+ set left(v) {
39
+ if (v) {
40
+ v.parent = this;
41
+ }
42
+ this._left = v;
43
+ }
44
+ get right() {
45
+ return this._right;
46
+ }
47
+ set right(v) {
48
+ if (v) {
49
+ v.parent = this;
50
+ }
51
+ this._right = v;
52
+ }
53
+ get parent() {
54
+ return this._parent;
55
+ }
56
+ set parent(v) {
57
+ this._parent = v;
58
+ }
59
+ get height() {
60
+ return this._height;
61
+ }
62
+ set height(v) {
63
+ this._height = v;
64
+ }
65
+ /**
66
+ * The function determines the position of a node in a family tree structure.
67
+ * @returns a value of type `FamilyPosition`.
68
+ */
69
+ get familyPosition() {
70
+ const that = this;
71
+ if (that.parent) {
72
+ if (that.parent.left === that) {
73
+ if (that.left || that.right) {
74
+ return FamilyPosition.ROOT_LEFT;
75
+ }
76
+ else {
77
+ return FamilyPosition.LEFT;
78
+ }
79
+ }
80
+ else if (that.parent.right === that) {
81
+ if (that.left || that.right) {
82
+ return FamilyPosition.ROOT_RIGHT;
83
+ }
84
+ else {
85
+ return FamilyPosition.RIGHT;
86
+ }
87
+ }
88
+ else {
89
+ return FamilyPosition.MAL_NODE;
90
+ }
91
+ }
92
+ else {
93
+ if (that.left || that.right) {
94
+ return FamilyPosition.ROOT;
95
+ }
96
+ else {
97
+ return FamilyPosition.ISOLATED;
98
+ }
99
+ }
100
+ }
101
+ }
102
+ export class AbstractBinaryTree {
103
+ /**
104
+ * The protected constructor initializes the options for an abstract binary tree.
105
+ * @param {AbstractBinaryTreeOptions} [options] - An optional object that contains configuration options for the binary
106
+ * tree.
107
+ */
108
+ constructor(options) {
109
+ this._root = null;
110
+ this._size = 0;
111
+ this._loopType = LoopType.ITERATIVE;
112
+ this._visitedId = [];
113
+ this._visitedVal = [];
114
+ this._visitedNode = [];
115
+ this._visitedLeftSum = [];
116
+ if (options !== undefined) {
117
+ const { loopType = LoopType.ITERATIVE } = options;
118
+ this._loopType = loopType;
119
+ }
120
+ this.clear();
121
+ }
122
+ get root() {
123
+ return this._root;
124
+ }
125
+ get size() {
126
+ return this._size;
127
+ }
128
+ get loopType() {
129
+ return this._loopType;
130
+ }
131
+ get visitedId() {
132
+ return this._visitedId;
133
+ }
134
+ get visitedVal() {
135
+ return this._visitedVal;
136
+ }
137
+ get visitedNode() {
138
+ return this._visitedNode;
139
+ }
140
+ get visitedLeftSum() {
141
+ return this._visitedLeftSum;
142
+ }
143
+ /**
144
+ * The `swapLocation` function swaps the location of two nodes in a binary tree.
145
+ * @param {N} srcNode - The source node that you want to swap with the destination node.
146
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
147
+ * be swapped to.
148
+ * @returns The `destNode` is being returned.
149
+ */
150
+ swapLocation(srcNode, destNode) {
151
+ const { id, val, height } = destNode;
152
+ const tempNode = this.createNode(id, val);
153
+ if (tempNode) {
154
+ tempNode.height = height;
155
+ destNode.id = srcNode.id;
156
+ destNode.val = srcNode.val;
157
+ destNode.height = srcNode.height;
158
+ srcNode.id = tempNode.id;
159
+ srcNode.val = tempNode.val;
160
+ srcNode.height = tempNode.height;
161
+ }
162
+ return destNode;
163
+ }
164
+ /**
165
+ * The clear() function resets the root, size, and maxId properties to their initial values.
166
+ */
167
+ clear() {
168
+ this._root = null;
169
+ this._size = 0;
170
+ this._clearResults();
171
+ }
172
+ /**
173
+ * The function checks if the size of an object is equal to zero and returns a boolean value.
174
+ * @returns A boolean value indicating whether the size of the object is 0 or not.
175
+ */
176
+ isEmpty() {
177
+ return this.size === 0;
178
+ }
179
+ /**
180
+ * When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
181
+ * In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
182
+ */
183
+ /**
184
+ * The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
185
+ * @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId`, which
186
+ * is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
187
+ * node itself. It can also be `null` if no node is specified.
188
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
189
+ * being added to the binary tree.
190
+ * @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
191
+ */
192
+ add(idOrNode, val) {
193
+ const _bfs = (root, newNode) => {
194
+ const queue = [root];
195
+ while (queue.length > 0) {
196
+ const cur = queue.shift();
197
+ if (cur) {
198
+ if (newNode && cur.id === newNode.id)
199
+ return;
200
+ const inserted = this._addTo(newNode, cur);
201
+ if (inserted !== undefined)
202
+ return inserted;
203
+ if (cur.left)
204
+ queue.push(cur.left);
205
+ if (cur.right)
206
+ queue.push(cur.right);
207
+ }
208
+ else
209
+ return;
210
+ }
211
+ return;
212
+ };
213
+ let inserted, needInsert;
214
+ if (idOrNode === null) {
215
+ needInsert = null;
216
+ }
217
+ else if (typeof idOrNode === 'number') {
218
+ needInsert = this.createNode(idOrNode, val);
219
+ }
220
+ else if (idOrNode instanceof AbstractBinaryTreeNode) {
221
+ needInsert = idOrNode;
222
+ }
223
+ else {
224
+ return;
225
+ }
226
+ const existNode = idOrNode ? this.get(idOrNode, 'id') : undefined;
227
+ if (this.root) {
228
+ if (existNode) {
229
+ existNode.val = val;
230
+ inserted = existNode;
231
+ }
232
+ else {
233
+ inserted = _bfs(this.root, needInsert);
234
+ }
235
+ }
236
+ else {
237
+ this._setRoot(needInsert);
238
+ if (needInsert !== null) {
239
+ this._setSize(1);
240
+ }
241
+ else {
242
+ this._setSize(0);
243
+ }
244
+ inserted = this.root;
245
+ }
246
+ return inserted;
247
+ }
248
+ /**
249
+ * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
250
+ * values, and adds them to the binary tree.
251
+ * @param {(BinaryTreeNodeId | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeId or BinaryTreeNode
252
+ * objects, or null values.
253
+ * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
254
+ * the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
255
+ * the value of the nodes will be `undefined`.
256
+ * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
257
+ */
258
+ addMany(idsOrNodes, data) {
259
+ // TODO not sure addMany not be run multi times
260
+ const inserted = [];
261
+ for (let i = 0; i < idsOrNodes.length; i++) {
262
+ const idOrNode = idsOrNodes[i];
263
+ if (idOrNode instanceof AbstractBinaryTreeNode) {
264
+ inserted.push(this.add(idOrNode.id, idOrNode.val));
265
+ continue;
266
+ }
267
+ if (idOrNode === null) {
268
+ inserted.push(this.add(null));
269
+ continue;
270
+ }
271
+ const val = data === null || data === void 0 ? void 0 : data[i];
272
+ inserted.push(this.add(idOrNode, val));
273
+ }
274
+ return inserted;
275
+ }
276
+ /**
277
+ * The `fill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
278
+ * @param {(BinaryTreeNodeId | N)[]} idsOrNodes - The `idsOrNodes` parameter is an array that can contain either
279
+ * `BinaryTreeNodeId` or `N` values.
280
+ * @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
281
+ * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `idsOrNodes`
282
+ * array. Each value in the `data` array will be assigned to the
283
+ * @returns The method is returning a boolean value.
284
+ */
285
+ fill(idsOrNodes, data) {
286
+ this.clear();
287
+ return idsOrNodes.length === this.addMany(idsOrNodes, data).length;
288
+ }
289
+ /**
290
+ * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent node
291
+ * that needs to be balanced.
292
+ * @param {N | BinaryTreeNodeId} nodeOrId - The `nodeOrId` parameter can be either a node object (`N`) or a binary tree
293
+ * node ID (`BinaryTreeNodeId`).
294
+ * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
295
+ * determines whether to update the left sum of all nodes in the binary tree after removing a node. If
296
+ * `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be updated. If it
297
+ * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
298
+ */
299
+ remove(nodeOrId, isUpdateAllLeftSum) {
300
+ isUpdateAllLeftSum = isUpdateAllLeftSum === undefined ? true : isUpdateAllLeftSum;
301
+ // TODO may implement update all left sum
302
+ if (isUpdateAllLeftSum) {
303
+ }
304
+ const bstDeletedResult = [];
305
+ if (!this.root)
306
+ return bstDeletedResult;
307
+ const curr = typeof nodeOrId === 'number' ? this.get(nodeOrId) : nodeOrId;
308
+ if (!curr)
309
+ return bstDeletedResult;
310
+ const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
311
+ let needBalanced = null, orgCurrent = curr;
312
+ if (!curr.left) {
313
+ if (!parent) {
314
+ if (curr.right !== undefined)
315
+ this._setRoot(curr.right);
316
+ }
317
+ else {
318
+ const { familyPosition: fp } = curr;
319
+ if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
320
+ parent.left = curr.right;
321
+ }
322
+ else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
323
+ parent.right = curr.right;
324
+ }
325
+ needBalanced = parent;
326
+ }
327
+ }
328
+ else {
329
+ const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
330
+ if (leftSubTreeRightMost) {
331
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
332
+ orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
333
+ if (parentOfLeftSubTreeMax) {
334
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
335
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
336
+ else
337
+ parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
338
+ needBalanced = parentOfLeftSubTreeMax;
339
+ }
340
+ }
341
+ }
342
+ this._setSize(this.size - 1);
343
+ bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
344
+ return bstDeletedResult;
345
+ }
346
+ /**
347
+ * The function calculates the depth of a node in a binary tree.
348
+ * @param {N | BinaryTreeNodeId | null} beginRoot - The `beginRoot` parameter can be one of the following:
349
+ * @returns the depth of the given node or binary tree.
350
+ */
351
+ getDepth(beginRoot) {
352
+ if (typeof beginRoot === 'number')
353
+ beginRoot = this.get(beginRoot, 'id');
354
+ let depth = 0;
355
+ while (beginRoot === null || beginRoot === void 0 ? void 0 : beginRoot.parent) {
356
+ depth++;
357
+ beginRoot = beginRoot.parent;
358
+ }
359
+ return depth;
360
+ }
361
+ /**
362
+ * The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
363
+ * @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
364
+ * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
365
+ * node), or `null`.
366
+ * @returns the height of the binary tree.
367
+ */
368
+ getHeight(beginRoot) {
369
+ beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
370
+ if (typeof beginRoot === 'number')
371
+ beginRoot = this.get(beginRoot, 'id');
372
+ if (!beginRoot)
373
+ return -1;
374
+ if (this._loopType === LoopType.RECURSIVE) {
375
+ const _getMaxHeight = (cur) => {
376
+ if (!cur)
377
+ return -1;
378
+ const leftHeight = _getMaxHeight(cur.left);
379
+ const rightHeight = _getMaxHeight(cur.right);
380
+ return Math.max(leftHeight, rightHeight) + 1;
381
+ };
382
+ return _getMaxHeight(beginRoot);
383
+ }
384
+ else {
385
+ if (!beginRoot) {
386
+ return -1;
387
+ }
388
+ const stack = [{ node: beginRoot, depth: 0 }];
389
+ let maxHeight = 0;
390
+ while (stack.length > 0) {
391
+ const { node, depth } = stack.pop();
392
+ if (node.left) {
393
+ stack.push({ node: node.left, depth: depth + 1 });
394
+ }
395
+ if (node.right) {
396
+ stack.push({ node: node.right, depth: depth + 1 });
397
+ }
398
+ maxHeight = Math.max(maxHeight, depth);
399
+ }
400
+ return maxHeight;
401
+ }
402
+ }
403
+ /**
404
+ * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
405
+ * approach.
406
+ * @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
407
+ * represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
408
+ * for `beginRoot`, the `this.root` property is used as the default value.
409
+ * @returns The function `getMinHeight` returns the minimum height of the binary tree.
410
+ */
411
+ getMinHeight(beginRoot) {
412
+ var _a, _b, _c;
413
+ beginRoot = beginRoot || this.root;
414
+ if (!beginRoot)
415
+ return -1;
416
+ if (this._loopType === LoopType.RECURSIVE) {
417
+ const _getMinHeight = (cur) => {
418
+ if (!cur)
419
+ return 0;
420
+ if (!cur.left && !cur.right)
421
+ return 0;
422
+ const leftMinHeight = _getMinHeight(cur.left);
423
+ const rightMinHeight = _getMinHeight(cur.right);
424
+ return Math.min(leftMinHeight, rightMinHeight) + 1;
425
+ };
426
+ return _getMinHeight(beginRoot);
427
+ }
428
+ else {
429
+ const stack = [];
430
+ let node = beginRoot, last = null;
431
+ const depths = new Map();
432
+ while (stack.length > 0 || node) {
433
+ if (node) {
434
+ stack.push(node);
435
+ node = node.left;
436
+ }
437
+ else {
438
+ node = stack[stack.length - 1];
439
+ if (!node.right || last === node.right) {
440
+ node = stack.pop();
441
+ if (node) {
442
+ const leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
443
+ const rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
444
+ depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
445
+ last = node;
446
+ node = null;
447
+ }
448
+ }
449
+ else
450
+ node = node.right;
451
+ }
452
+ }
453
+ return (_c = depths.get(beginRoot)) !== null && _c !== void 0 ? _c : -1;
454
+ }
455
+ }
456
+ /**
457
+ * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
458
+ * tree.
459
+ * @param {N | null} [beginRoot] - The parameter `beginRoot` is of type `N` or `null`. It represents the root node of a
460
+ * tree or null if the tree is empty.
461
+ * @returns The method is returning a boolean value.
462
+ */
463
+ isPerfectlyBalanced(beginRoot) {
464
+ return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
465
+ }
466
+ /**
467
+ * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
468
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
469
+ * generic type `N`. It represents the property of the binary tree node that you want to search for.
470
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
471
+ * specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
472
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
473
+ * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
474
+ * function will stop traversing the tree and return the first matching node. If `only
475
+ * @returns an array of nodes (type N).
476
+ */
477
+ getNodes(nodeProperty, propertyName, onlyOne) {
478
+ if (!this.root)
479
+ return [];
480
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
481
+ const result = [];
482
+ if (this.loopType === LoopType.RECURSIVE) {
483
+ const _traverse = (cur) => {
484
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
485
+ return;
486
+ if (!cur.left && !cur.right)
487
+ return;
488
+ cur.left && _traverse(cur.left);
489
+ cur.right && _traverse(cur.right);
490
+ };
491
+ _traverse(this.root);
492
+ }
493
+ else {
494
+ const queue = [this.root];
495
+ while (queue.length > 0) {
496
+ const cur = queue.shift();
497
+ if (cur) {
498
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
499
+ return result;
500
+ cur.left && queue.push(cur.left);
501
+ cur.right && queue.push(cur.right);
502
+ }
503
+ }
504
+ }
505
+ return result;
506
+ }
507
+ /**
508
+ * The function checks if a binary tree node has a specific property.
509
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
510
+ * It represents the property of the binary tree node that you want to check.
511
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
512
+ * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'id'.
513
+ * @returns a boolean value.
514
+ */
515
+ has(nodeProperty, propertyName) {
516
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
517
+ // TODO may support finding node by value equal
518
+ return this.getNodes(nodeProperty, propertyName).length > 0;
519
+ }
520
+ /**
521
+ * The function returns the first node that matches the given property name and value, or null if no matching node is
522
+ * found.
523
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
524
+ * It represents the property of the binary tree node that you want to search for.
525
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
526
+ * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
527
+ * default value is set to `'id'`.
528
+ * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
529
+ * If no matching node is found, it returns null.
530
+ */
531
+ get(nodeProperty, propertyName) {
532
+ var _a;
533
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
534
+ // TODO may support finding node by value equal
535
+ return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
536
+ }
537
+ /**
538
+ * The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
539
+ * an option to reverse the order of the nodes.
540
+ * @param {N} node - The `node` parameter represents a node in a tree structure. It is of type `N`, which could be any
541
+ * type that represents a node in your specific implementation.
542
+ * @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
543
+ * path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
544
+ * `isReverse` is set to `false` or not provided, the path will
545
+ * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
546
+ */
547
+ getPathToRoot(node, isReverse = true) {
548
+ // TODO to support get path through passing id
549
+ const result = [];
550
+ while (node.parent) {
551
+ // Array.push + Array.reverse is more efficient than Array.unshift
552
+ // TODO may consider using Deque, so far this is not the performance bottleneck
553
+ result.push(node);
554
+ node = node.parent;
555
+ }
556
+ result.push(node);
557
+ return isReverse ? result.reverse() : result;
558
+ }
559
+ /**
560
+ * The `getLeftMost` function returns the leftmost node in a binary tree, starting from a specified node or the root if
561
+ * no node is specified.
562
+ * @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
563
+ * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
564
+ * node), or `null`.
565
+ * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
566
+ * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the
567
+ * traversal from the root of the binary tree. If there are no nodes in the binary tree, it returns `null`.
568
+ */
569
+ getLeftMost(beginRoot) {
570
+ if (typeof beginRoot === 'number')
571
+ beginRoot = this.get(beginRoot, 'id');
572
+ beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
573
+ if (!beginRoot)
574
+ return beginRoot;
575
+ if (this._loopType === LoopType.RECURSIVE) {
576
+ const _traverse = (cur) => {
577
+ if (!cur.left)
578
+ return cur;
579
+ return _traverse(cur.left);
580
+ };
581
+ return _traverse(beginRoot);
582
+ }
583
+ else {
584
+ // Indirect implementation of iteration using tail recursion optimization
585
+ const _traverse = trampoline((cur) => {
586
+ if (!cur.left)
587
+ return cur;
588
+ return _traverse.cont(cur.left);
589
+ });
590
+ return _traverse(beginRoot);
591
+ }
592
+ }
593
+ /**
594
+ * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using
595
+ * tail recursion optimization.
596
+ * @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
597
+ * starting node from which we want to find the rightmost node. If no node is provided, the `node` parameter defaults
598
+ * to `this.root`, which is the root node of the data structure
599
+ * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the `node` parameter is
600
+ * not provided, it defaults to the root node of the tree. If the tree is empty or the `node` parameter is `null`, the
601
+ * function returns `null`.
602
+ */
603
+ getRightMost(node) {
604
+ // TODO support get right most by passing id in
605
+ node = node !== null && node !== void 0 ? node : this.root;
606
+ if (!node)
607
+ return node;
608
+ if (this._loopType === LoopType.RECURSIVE) {
609
+ const _traverse = (cur) => {
610
+ if (!cur.right)
611
+ return cur;
612
+ return _traverse(cur.right);
613
+ };
614
+ return _traverse(node);
615
+ }
616
+ else {
617
+ // Indirect implementation of iteration using tail recursion optimization
618
+ const _traverse = trampoline((cur) => {
619
+ if (!cur.right)
620
+ return cur;
621
+ return _traverse.cont(cur.right);
622
+ });
623
+ return _traverse(node);
624
+ }
625
+ }
626
+ /**
627
+ * The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
628
+ * @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
629
+ * @returns a boolean value.
630
+ */
631
+ isSubtreeBST(node) {
632
+ // TODO there is a bug
633
+ if (!node)
634
+ return true;
635
+ if (this._loopType === LoopType.RECURSIVE) {
636
+ const dfs = (cur, min, max) => {
637
+ if (!cur)
638
+ return true;
639
+ if (cur.id <= min || cur.id >= max)
640
+ return false;
641
+ return dfs(cur.left, min, cur.id) && dfs(cur.right, cur.id, max);
642
+ };
643
+ return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
644
+ }
645
+ else {
646
+ const stack = [];
647
+ let prev = Number.MIN_SAFE_INTEGER, curr = node;
648
+ while (curr || stack.length > 0) {
649
+ while (curr) {
650
+ stack.push(curr);
651
+ curr = curr.left;
652
+ }
653
+ curr = stack.pop();
654
+ if (!curr || prev >= curr.id)
655
+ return false;
656
+ prev = curr.id;
657
+ curr = curr.right;
658
+ }
659
+ return true;
660
+ }
661
+ }
662
+ /**
663
+ * The function isBST checks if the binary tree is valid binary search tree.
664
+ * @returns The `isBST()` function is returning a boolean value.
665
+ */
666
+ isBST() {
667
+ return this.isSubtreeBST(this.root);
668
+ }
669
+ /**
670
+ * The function calculates the size of a subtree by traversing it either recursively or iteratively.
671
+ * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
672
+ * binary tree.
673
+ * @returns the size of the subtree rooted at `subTreeRoot`.
674
+ */
675
+ getSubTreeSize(subTreeRoot) {
676
+ // TODO support id passed in
677
+ let size = 0;
678
+ if (!subTreeRoot)
679
+ return size;
680
+ if (this._loopType === LoopType.RECURSIVE) {
681
+ const _traverse = (cur) => {
682
+ size++;
683
+ cur.left && _traverse(cur.left);
684
+ cur.right && _traverse(cur.right);
685
+ };
686
+ _traverse(subTreeRoot);
687
+ return size;
688
+ }
689
+ else {
690
+ const stack = [subTreeRoot];
691
+ while (stack.length > 0) {
692
+ const cur = stack.pop();
693
+ size++;
694
+ cur.right && stack.push(cur.right);
695
+ cur.left && stack.push(cur.left);
696
+ }
697
+ return size;
698
+ }
699
+ }
700
+ /**
701
+ * The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
702
+ * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
703
+ * tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
704
+ * @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
705
+ * property of the binary tree node to use for calculating the sum. It can be either 'id' or 'val'. If propertyName is
706
+ * not provided, it defaults to 'id'.
707
+ * @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
708
+ */
709
+ subTreeSum(subTreeRoot, propertyName) {
710
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
711
+ if (typeof subTreeRoot === 'number')
712
+ subTreeRoot = this.get(subTreeRoot, 'id');
713
+ if (!subTreeRoot)
714
+ return 0;
715
+ let sum = 0;
716
+ const _sumByProperty = (cur) => {
717
+ let needSum;
718
+ switch (propertyName) {
719
+ case 'id':
720
+ needSum = cur.id;
721
+ break;
722
+ case 'val':
723
+ needSum = typeof cur.val === 'number' ? cur.val : 0;
724
+ break;
725
+ default:
726
+ needSum = cur.id;
727
+ break;
728
+ }
729
+ return needSum;
730
+ };
731
+ if (this._loopType === LoopType.RECURSIVE) {
732
+ const _traverse = (cur) => {
733
+ sum += _sumByProperty(cur);
734
+ cur.left && _traverse(cur.left);
735
+ cur.right && _traverse(cur.right);
736
+ };
737
+ _traverse(subTreeRoot);
738
+ }
739
+ else {
740
+ const stack = [subTreeRoot];
741
+ while (stack.length > 0) {
742
+ const cur = stack.pop();
743
+ sum += _sumByProperty(cur);
744
+ cur.right && stack.push(cur.right);
745
+ cur.left && stack.push(cur.left);
746
+ }
747
+ }
748
+ return sum;
749
+ }
750
+ /**
751
+ * The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
752
+ * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
753
+ * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
754
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
755
+ * each node in the subtree should be incremented.
756
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
757
+ * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'id'.
758
+ * @returns a boolean value.
759
+ */
760
+ subTreeAdd(subTreeRoot, delta, propertyName) {
761
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
762
+ if (typeof subTreeRoot === 'number')
763
+ subTreeRoot = this.get(subTreeRoot, 'id');
764
+ if (!subTreeRoot)
765
+ return false;
766
+ const _addByProperty = (cur) => {
767
+ switch (propertyName) {
768
+ case 'id':
769
+ cur.id += delta;
770
+ break;
771
+ default:
772
+ cur.id += delta;
773
+ break;
774
+ }
775
+ };
776
+ if (this._loopType === LoopType.RECURSIVE) {
777
+ const _traverse = (cur) => {
778
+ _addByProperty(cur);
779
+ cur.left && _traverse(cur.left);
780
+ cur.right && _traverse(cur.right);
781
+ };
782
+ _traverse(subTreeRoot);
783
+ }
784
+ else {
785
+ const stack = [subTreeRoot];
786
+ while (stack.length > 0) {
787
+ const cur = stack.pop();
788
+ _addByProperty(cur);
789
+ cur.right && stack.push(cur.right);
790
+ cur.left && stack.push(cur.left);
791
+ }
792
+ }
793
+ return true;
794
+ }
795
+ /**
796
+ * The BFS function performs a breadth-first search on a binary tree, accumulating properties of each node based on a
797
+ * specified property name.
798
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
799
+ * represents either a node or a property name. If a node is provided, the breadth-first search (BFS) algorithm will be
800
+ * performed starting from that node. If a property name is provided, the BFS algorithm will be performed starting from
801
+ * the
802
+ * @returns an instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
803
+ */
804
+ BFS(nodeOrPropertyName) {
805
+ nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
806
+ this._clearResults();
807
+ const queue = [this.root];
808
+ while (queue.length !== 0) {
809
+ const cur = queue.shift();
810
+ if (cur) {
811
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
812
+ if ((cur === null || cur === void 0 ? void 0 : cur.left) !== null)
813
+ queue.push(cur.left);
814
+ if ((cur === null || cur === void 0 ? void 0 : cur.right) !== null)
815
+ queue.push(cur.right);
816
+ }
817
+ }
818
+ return this._getResultByPropertyName(nodeOrPropertyName);
819
+ }
820
+ /**
821
+ * The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
822
+ * each node based on the specified pattern and property name.
823
+ * @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is used to specify the traversal order of the
824
+ * binary tree. It can have three possible values:
825
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is a string that represents
826
+ * the name of a property of the nodes in the binary tree. This property will be used to accumulate values during the
827
+ * depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'id'`.
828
+ * @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the
829
+ * binary tree nodes based on the specified pattern and node or property name.
830
+ */
831
+ DFS(pattern, nodeOrPropertyName) {
832
+ pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
833
+ nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
834
+ this._clearResults();
835
+ const _traverse = (node) => {
836
+ switch (pattern) {
837
+ case 'in':
838
+ if (node.left)
839
+ _traverse(node.left);
840
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
841
+ if (node.right)
842
+ _traverse(node.right);
843
+ break;
844
+ case 'pre':
845
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
846
+ if (node.left)
847
+ _traverse(node.left);
848
+ if (node.right)
849
+ _traverse(node.right);
850
+ break;
851
+ case 'post':
852
+ if (node.left)
853
+ _traverse(node.left);
854
+ if (node.right)
855
+ _traverse(node.right);
856
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
857
+ break;
858
+ }
859
+ };
860
+ this.root && _traverse(this.root);
861
+ return this._getResultByPropertyName(nodeOrPropertyName);
862
+ }
863
+ /**
864
+ * The DFSIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
865
+ * specify the traversal pattern and the property name to accumulate results by.
866
+ * @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter determines the order in which the nodes of the
867
+ * binary tree are visited during the depth-first search. It can have one of the following values:
868
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
869
+ * property of the nodes that you want to retrieve or perform operations on during the depth-first search traversal. By
870
+ * default, it is set to `'id'`, which means that the traversal will accumulate results based on the `id` property of
871
+ * the
872
+ * @returns an object of type AbstractBinaryTreeNodeProperties<N>.
873
+ */
874
+ DFSIterative(pattern, nodeOrPropertyName) {
875
+ pattern = pattern || 'in';
876
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
877
+ this._clearResults();
878
+ if (!this.root)
879
+ return this._getResultByPropertyName(nodeOrPropertyName);
880
+ // 0: visit, 1: print
881
+ const stack = [{ opt: 0, node: this.root }];
882
+ while (stack.length > 0) {
883
+ const cur = stack.pop();
884
+ if (!cur || !cur.node)
885
+ continue;
886
+ if (cur.opt === 1) {
887
+ this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
888
+ }
889
+ else {
890
+ switch (pattern) {
891
+ case 'in':
892
+ stack.push({ opt: 0, node: cur.node.right });
893
+ stack.push({ opt: 1, node: cur.node });
894
+ stack.push({ opt: 0, node: cur.node.left });
895
+ break;
896
+ case 'pre':
897
+ stack.push({ opt: 0, node: cur.node.right });
898
+ stack.push({ opt: 0, node: cur.node.left });
899
+ stack.push({ opt: 1, node: cur.node });
900
+ break;
901
+ case 'post':
902
+ stack.push({ opt: 1, node: cur.node });
903
+ stack.push({ opt: 0, node: cur.node.right });
904
+ stack.push({ opt: 0, node: cur.node.left });
905
+ break;
906
+ default:
907
+ stack.push({ opt: 0, node: cur.node.right });
908
+ stack.push({ opt: 1, node: cur.node });
909
+ stack.push({ opt: 0, node: cur.node.left });
910
+ break;
911
+ }
912
+ }
913
+ }
914
+ return this._getResultByPropertyName(nodeOrPropertyName);
915
+ }
916
+ /**
917
+ * The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
918
+ * in an array, based on a specified property name.
919
+ * @param {N | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
920
+ * node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
921
+ * the tree is used as the starting node.
922
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
923
+ * can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
924
+ * will accumulate results based on that property. If no property name is provided, the function will default to
925
+ * accumulating results
926
+ * @returns The function `levelIterative` returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
927
+ */
928
+ levelIterative(node, nodeOrPropertyName) {
929
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
930
+ node = node || this.root;
931
+ if (!node)
932
+ return [];
933
+ this._clearResults();
934
+ const queue = [node];
935
+ while (queue.length > 0) {
936
+ const cur = queue.shift();
937
+ if (cur) {
938
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
939
+ if (cur.left) {
940
+ queue.push(cur.left);
941
+ }
942
+ if (cur.right) {
943
+ queue.push(cur.right);
944
+ }
945
+ }
946
+ }
947
+ return this._getResultByPropertyName(nodeOrPropertyName);
948
+ }
949
+ /**
950
+ * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
951
+ * @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the
952
+ * root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
953
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
954
+ * specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following
955
+ * values:
956
+ * @returns The function `listLevels` returns a 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
957
+ */
958
+ listLevels(node, nodeOrPropertyName) {
959
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
960
+ node = node || this.root;
961
+ if (!node)
962
+ return [];
963
+ const levelsNodes = [];
964
+ const collectByProperty = (node, level) => {
965
+ switch (nodeOrPropertyName) {
966
+ case 'id':
967
+ levelsNodes[level].push(node.id);
968
+ break;
969
+ case 'val':
970
+ levelsNodes[level].push(node.val);
971
+ break;
972
+ case 'node':
973
+ levelsNodes[level].push(node);
974
+ break;
975
+ default:
976
+ levelsNodes[level].push(node.id);
977
+ break;
978
+ }
979
+ };
980
+ if (this.loopType === LoopType.RECURSIVE) {
981
+ const _recursive = (node, level) => {
982
+ if (!levelsNodes[level])
983
+ levelsNodes[level] = [];
984
+ collectByProperty(node, level);
985
+ if (node.left)
986
+ _recursive(node.left, level + 1);
987
+ if (node.right)
988
+ _recursive(node.right, level + 1);
989
+ };
990
+ _recursive(node, 0);
991
+ }
992
+ else {
993
+ const stack = [[node, 0]];
994
+ while (stack.length > 0) {
995
+ const head = stack.pop();
996
+ const [node, level] = head;
997
+ if (!levelsNodes[level])
998
+ levelsNodes[level] = [];
999
+ collectByProperty(node, level);
1000
+ if (node.right)
1001
+ stack.push([node.right, level + 1]);
1002
+ if (node.left)
1003
+ stack.push([node.left, level + 1]);
1004
+ }
1005
+ }
1006
+ return levelsNodes;
1007
+ }
1008
+ /**
1009
+ * The function returns the predecessor of a given node in a binary tree.
1010
+ * @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
1011
+ * @returns the predecessor of the given node in a binary tree.
1012
+ */
1013
+ getPredecessor(node) {
1014
+ if (node.left) {
1015
+ let predecessor = node.left;
1016
+ while (!predecessor || (predecessor.right && predecessor.right !== node)) {
1017
+ if (predecessor) {
1018
+ predecessor = predecessor.right;
1019
+ }
1020
+ }
1021
+ return predecessor;
1022
+ }
1023
+ else {
1024
+ return node;
1025
+ }
1026
+ }
1027
+ /**
1028
+ * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris
1029
+ * traversal algorithm.
1030
+ * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter determines the traversal pattern for the binary
1031
+ * tree. It can have one of three values:
1032
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
1033
+ * property name of the nodes that you want to retrieve. It can be any valid property name of the nodes in the binary
1034
+ * tree.
1035
+ * @returns an array of AbstractBinaryTreeNodeProperties<N> objects.
1036
+ */
1037
+ morris(pattern, nodeOrPropertyName) {
1038
+ if (this.root === null)
1039
+ return [];
1040
+ pattern = pattern || 'in';
1041
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
1042
+ this._clearResults();
1043
+ let cur = this.root;
1044
+ const _reverseEdge = (node) => {
1045
+ let pre = null;
1046
+ let next = null;
1047
+ while (node) {
1048
+ next = node.right;
1049
+ node.right = pre;
1050
+ pre = node;
1051
+ node = next;
1052
+ }
1053
+ return pre;
1054
+ };
1055
+ const _printEdge = (node) => {
1056
+ const tail = _reverseEdge(node);
1057
+ let cur = tail;
1058
+ while (cur) {
1059
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1060
+ cur = cur.right;
1061
+ }
1062
+ _reverseEdge(tail);
1063
+ };
1064
+ switch (pattern) {
1065
+ case 'in':
1066
+ while (cur) {
1067
+ if (cur.left) {
1068
+ const predecessor = this.getPredecessor(cur);
1069
+ if (!predecessor.right) {
1070
+ predecessor.right = cur;
1071
+ cur = cur.left;
1072
+ continue;
1073
+ }
1074
+ else {
1075
+ predecessor.right = null;
1076
+ }
1077
+ }
1078
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1079
+ cur = cur.right;
1080
+ }
1081
+ break;
1082
+ case 'pre':
1083
+ while (cur) {
1084
+ if (cur.left) {
1085
+ const predecessor = this.getPredecessor(cur);
1086
+ if (!predecessor.right) {
1087
+ predecessor.right = cur;
1088
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1089
+ cur = cur.left;
1090
+ continue;
1091
+ }
1092
+ else {
1093
+ predecessor.right = null;
1094
+ }
1095
+ }
1096
+ else {
1097
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1098
+ }
1099
+ cur = cur.right;
1100
+ }
1101
+ break;
1102
+ case 'post':
1103
+ while (cur) {
1104
+ if (cur.left) {
1105
+ const predecessor = this.getPredecessor(cur);
1106
+ if (predecessor.right === null) {
1107
+ predecessor.right = cur;
1108
+ cur = cur.left;
1109
+ continue;
1110
+ }
1111
+ else {
1112
+ predecessor.right = null;
1113
+ _printEdge(cur.left);
1114
+ }
1115
+ }
1116
+ cur = cur.right;
1117
+ }
1118
+ _printEdge(this.root);
1119
+ break;
1120
+ }
1121
+ return this._getResultByPropertyName(nodeOrPropertyName);
1122
+ }
1123
+ /**
1124
+ * The function adds a new node to a binary tree if there is an available position.
1125
+ * @param {N | null} newNode - The `newNode` parameter is of type `N | null`, which means it can either be a node of
1126
+ * type `N` or `null`. It represents the node that you want to add to the binary tree.
1127
+ * @param {N} parent - The parent parameter is of type N, which represents a node in a binary tree.
1128
+ * @returns either the left or right child node of the parent node, depending on which child is available for adding
1129
+ * the new node. If a new node is added, the function also updates the size of the binary tree. If neither the left nor
1130
+ * right child is available, the function returns undefined. If the parent node is null, the function also returns
1131
+ * undefined.
1132
+ */
1133
+ _addTo(newNode, parent) {
1134
+ if (parent) {
1135
+ // When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
1136
+ // In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
1137
+ if (parent.left === undefined) {
1138
+ parent.left = newNode;
1139
+ if (newNode) {
1140
+ this._setSize(this.size + 1);
1141
+ }
1142
+ return parent.left;
1143
+ }
1144
+ else if (parent.right === undefined) {
1145
+ parent.right = newNode;
1146
+ if (newNode) {
1147
+ this._setSize(this.size + 1);
1148
+ }
1149
+ return parent.right;
1150
+ }
1151
+ else {
1152
+ return;
1153
+ }
1154
+ }
1155
+ else {
1156
+ return;
1157
+ }
1158
+ }
1159
+ /**
1160
+ * The function sets the loop type for a protected variable.
1161
+ * @param {LoopType} value - The value parameter is of type LoopType.
1162
+ */
1163
+ _setLoopType(value) {
1164
+ this._loopType = value;
1165
+ }
1166
+ /**
1167
+ * The function sets the value of the `_visitedId` property in a protected manner.
1168
+ * @param {BinaryTreeNodeId[]} value - value is an array of BinaryTreeNodeId values.
1169
+ */
1170
+ _setVisitedId(value) {
1171
+ this._visitedId = value;
1172
+ }
1173
+ /**
1174
+ * The function sets the value of the "_visitedVal" property to the given array.
1175
+ * @param value - An array of type N.
1176
+ */
1177
+ _setVisitedVal(value) {
1178
+ this._visitedVal = value;
1179
+ }
1180
+ /**
1181
+ * The function sets the value of the _visitedNode property.
1182
+ * @param {N[]} value - N[] is an array of elements of type N.
1183
+ */
1184
+ _setVisitedNode(value) {
1185
+ this._visitedNode = value;
1186
+ }
1187
+ /**
1188
+ * The function sets the value of the `_visitedLeftSum` property to the provided array.
1189
+ * @param {number[]} value - An array of numbers that represents the visited left sum.
1190
+ */
1191
+ _setVisitedLeftSum(value) {
1192
+ this._visitedLeftSum = value;
1193
+ }
1194
+ /**
1195
+ * The function sets the root property of an object to a given value, and if the value is not null, it also sets the
1196
+ * parent property of the value to undefined.
1197
+ * @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of type `N` or `null`.
1198
+ */
1199
+ _setRoot(v) {
1200
+ if (v) {
1201
+ v.parent = undefined;
1202
+ }
1203
+ this._root = v;
1204
+ }
1205
+ /**
1206
+ * The function sets the size of a protected variable.
1207
+ * @param {number} v - number
1208
+ */
1209
+ _setSize(v) {
1210
+ this._size = v;
1211
+ }
1212
+ /**
1213
+ * The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
1214
+ * properties.
1215
+ */
1216
+ _clearResults() {
1217
+ this._visitedId = [];
1218
+ this._visitedVal = [];
1219
+ this._visitedNode = [];
1220
+ this._visitedLeftSum = [];
1221
+ }
1222
+ /**
1223
+ * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
1224
+ * a result array.
1225
+ * @param {N} cur - The current node being processed.
1226
+ * @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
1227
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeId` or a `N`
1228
+ * type. It represents the property value that we are comparing against in the switch statement.
1229
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
1230
+ * specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'id'`
1231
+ * or `'val'`. If it is not provided or is not equal to `'id'` or `'val'`, the
1232
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
1233
+ * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
1234
+ * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
1235
+ * @returns a boolean value indicating whether only one matching node should be pushed into the result array.
1236
+ */
1237
+ _pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne) {
1238
+ switch (propertyName) {
1239
+ case 'id':
1240
+ if (cur.id === nodeProperty) {
1241
+ result.push(cur);
1242
+ return !!onlyOne;
1243
+ }
1244
+ break;
1245
+ case 'val':
1246
+ if (cur.val === nodeProperty) {
1247
+ result.push(cur);
1248
+ return !!onlyOne;
1249
+ }
1250
+ break;
1251
+ default:
1252
+ if (cur.id === nodeProperty) {
1253
+ result.push(cur);
1254
+ return !!onlyOne;
1255
+ }
1256
+ break;
1257
+ }
1258
+ }
1259
+ /**
1260
+ * The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
1261
+ * @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
1262
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1263
+ * can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
1264
+ * specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
1265
+ */
1266
+ _accumulatedByPropertyName(node, nodeOrPropertyName) {
1267
+ nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1268
+ switch (nodeOrPropertyName) {
1269
+ case 'id':
1270
+ this._visitedId.push(node.id);
1271
+ break;
1272
+ case 'val':
1273
+ this._visitedVal.push(node.val);
1274
+ break;
1275
+ case 'node':
1276
+ this._visitedNode.push(node);
1277
+ break;
1278
+ default:
1279
+ this._visitedId.push(node.id);
1280
+ break;
1281
+ }
1282
+ }
1283
+ /**
1284
+ * The time complexity of Morris traversal is O(n), it's may slower than others
1285
+ * The space complexity Morris traversal is O(1) because no using stack
1286
+ */
1287
+ /**
1288
+ * The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
1289
+ * name.
1290
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1291
+ * can accept either a `NodeOrPropertyName` type or be undefined.
1292
+ * @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
1293
+ */
1294
+ _getResultByPropertyName(nodeOrPropertyName) {
1295
+ nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
1296
+ switch (nodeOrPropertyName) {
1297
+ case 'id':
1298
+ return this._visitedId;
1299
+ case 'val':
1300
+ return this._visitedVal;
1301
+ case 'node':
1302
+ return this._visitedNode;
1303
+ default:
1304
+ return this._visitedId;
1305
+ }
1306
+ }
1307
+ }