data-structure-typed 1.32.1 → 1.32.9

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 (342) hide show
  1. package/.idea/data-structure-typed.iml +19 -0
  2. package/.idea/inspectionProfiles/Project_Default.xml +6 -0
  3. package/.idea/misc.xml +6 -0
  4. package/.idea/modules.xml +8 -0
  5. package/.idea/vcs.xml +6 -0
  6. package/CHANGELOG.md +1 -1
  7. package/README.md +415 -255
  8. package/coverage/coverage-final.json +4 -4
  9. package/docs/index.html +268 -21
  10. package/package.json +48 -16
  11. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +46 -1
  12. package/.auto-changelog-template.hbs +0 -36
  13. package/coverage/clover.xml +0 -3393
  14. package/coverage/lcov-report/base.css +0 -224
  15. package/coverage/lcov-report/block-navigation.js +0 -87
  16. package/coverage/lcov-report/favicon.png +0 -0
  17. package/coverage/lcov-report/index.html +0 -386
  18. package/coverage/lcov-report/prettify.css +0 -1
  19. package/coverage/lcov-report/prettify.js +0 -2
  20. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  21. package/coverage/lcov-report/sorter.js +0 -196
  22. package/coverage/lcov-report/src/data-structures/binary-tree/aa-tree.ts.html +0 -88
  23. package/coverage/lcov-report/src/data-structures/binary-tree/abstract-binary-tree.ts.html +0 -4978
  24. package/coverage/lcov-report/src/data-structures/binary-tree/avl-tree.ts.html +0 -1015
  25. package/coverage/lcov-report/src/data-structures/binary-tree/b-tree.ts.html +0 -88
  26. package/coverage/lcov-report/src/data-structures/binary-tree/binary-indexed-tree.ts.html +0 -313
  27. package/coverage/lcov-report/src/data-structures/binary-tree/binary-tree.ts.html +0 -226
  28. package/coverage/lcov-report/src/data-structures/binary-tree/bst.ts.html +0 -1696
  29. package/coverage/lcov-report/src/data-structures/binary-tree/index.html +0 -296
  30. package/coverage/lcov-report/src/data-structures/binary-tree/index.ts.html +0 -121
  31. package/coverage/lcov-report/src/data-structures/binary-tree/rb-tree.ts.html +0 -388
  32. package/coverage/lcov-report/src/data-structures/binary-tree/segment-tree.ts.html +0 -811
  33. package/coverage/lcov-report/src/data-structures/binary-tree/splay-tree.ts.html +0 -88
  34. package/coverage/lcov-report/src/data-structures/binary-tree/tree-multiset.ts.html +0 -2185
  35. package/coverage/lcov-report/src/data-structures/binary-tree/two-three-tree.ts.html +0 -88
  36. package/coverage/lcov-report/src/data-structures/graph/abstract-graph.ts.html +0 -3205
  37. package/coverage/lcov-report/src/data-structures/graph/directed-graph.ts.html +0 -1495
  38. package/coverage/lcov-report/src/data-structures/graph/index.html +0 -176
  39. package/coverage/lcov-report/src/data-structures/graph/index.ts.html +0 -97
  40. package/coverage/lcov-report/src/data-structures/graph/map-graph.ts.html +0 -472
  41. package/coverage/lcov-report/src/data-structures/graph/undirected-graph.ts.html +0 -907
  42. package/coverage/lcov-report/src/data-structures/hash/coordinate-map.ts.html +0 -286
  43. package/coverage/lcov-report/src/data-structures/hash/coordinate-set.ts.html +0 -253
  44. package/coverage/lcov-report/src/data-structures/hash/hash-table.ts.html +0 -88
  45. package/coverage/lcov-report/src/data-structures/hash/index.html +0 -206
  46. package/coverage/lcov-report/src/data-structures/hash/index.ts.html +0 -103
  47. package/coverage/lcov-report/src/data-structures/hash/pair.ts.html +0 -88
  48. package/coverage/lcov-report/src/data-structures/hash/tree-map.ts.html +0 -88
  49. package/coverage/lcov-report/src/data-structures/hash/tree-set.ts.html +0 -88
  50. package/coverage/lcov-report/src/data-structures/heap/heap.ts.html +0 -721
  51. package/coverage/lcov-report/src/data-structures/heap/index.html +0 -161
  52. package/coverage/lcov-report/src/data-structures/heap/index.ts.html +0 -94
  53. package/coverage/lcov-report/src/data-structures/heap/max-heap.ts.html +0 -178
  54. package/coverage/lcov-report/src/data-structures/heap/min-heap.ts.html +0 -181
  55. package/coverage/lcov-report/src/data-structures/index.html +0 -116
  56. package/coverage/lcov-report/src/data-structures/index.ts.html +0 -118
  57. package/coverage/lcov-report/src/data-structures/linked-list/doubly-linked-list.ts.html +0 -1804
  58. package/coverage/lcov-report/src/data-structures/linked-list/index.html +0 -161
  59. package/coverage/lcov-report/src/data-structures/linked-list/index.ts.html +0 -94
  60. package/coverage/lcov-report/src/data-structures/linked-list/singly-linked-list.ts.html +0 -1588
  61. package/coverage/lcov-report/src/data-structures/linked-list/skip-linked-list.ts.html +0 -88
  62. package/coverage/lcov-report/src/data-structures/matrix/index.html +0 -176
  63. package/coverage/lcov-report/src/data-structures/matrix/index.ts.html +0 -97
  64. package/coverage/lcov-report/src/data-structures/matrix/matrix.ts.html +0 -166
  65. package/coverage/lcov-report/src/data-structures/matrix/matrix2d.ts.html +0 -721
  66. package/coverage/lcov-report/src/data-structures/matrix/navigator.ts.html +0 -448
  67. package/coverage/lcov-report/src/data-structures/matrix/vector2d.ts.html +0 -1033
  68. package/coverage/lcov-report/src/data-structures/priority-queue/index.html +0 -161
  69. package/coverage/lcov-report/src/data-structures/priority-queue/index.ts.html +0 -94
  70. package/coverage/lcov-report/src/data-structures/priority-queue/max-priority-queue.ts.html +0 -253
  71. package/coverage/lcov-report/src/data-structures/priority-queue/min-priority-queue.ts.html +0 -256
  72. package/coverage/lcov-report/src/data-structures/priority-queue/priority-queue.ts.html +0 -1162
  73. package/coverage/lcov-report/src/data-structures/queue/deque.ts.html +0 -976
  74. package/coverage/lcov-report/src/data-structures/queue/index.html +0 -146
  75. package/coverage/lcov-report/src/data-structures/queue/index.ts.html +0 -91
  76. package/coverage/lcov-report/src/data-structures/queue/queue.ts.html +0 -658
  77. package/coverage/lcov-report/src/data-structures/stack/index.html +0 -131
  78. package/coverage/lcov-report/src/data-structures/stack/index.ts.html +0 -88
  79. package/coverage/lcov-report/src/data-structures/stack/stack.ts.html +0 -379
  80. package/coverage/lcov-report/src/data-structures/tree/index.html +0 -131
  81. package/coverage/lcov-report/src/data-structures/tree/index.ts.html +0 -88
  82. package/coverage/lcov-report/src/data-structures/tree/tree.ts.html +0 -292
  83. package/coverage/lcov-report/src/data-structures/trie/index.html +0 -131
  84. package/coverage/lcov-report/src/data-structures/trie/index.ts.html +0 -88
  85. package/coverage/lcov-report/src/data-structures/trie/trie.ts.html +0 -760
  86. package/coverage/lcov-report/src/index.html +0 -116
  87. package/coverage/lcov-report/src/index.ts.html +0 -97
  88. package/coverage/lcov-report/src/interfaces/index.html +0 -116
  89. package/coverage/lcov-report/src/interfaces/index.ts.html +0 -130
  90. package/coverage/lcov-report/src/types/data-structures/abstract-binary-tree.ts.html +0 -235
  91. package/coverage/lcov-report/src/types/data-structures/bst.ts.html +0 -124
  92. package/coverage/lcov-report/src/types/data-structures/directed-graph.ts.html +0 -109
  93. package/coverage/lcov-report/src/types/data-structures/index.html +0 -176
  94. package/coverage/lcov-report/src/types/data-structures/index.ts.html +0 -130
  95. package/coverage/lcov-report/src/types/data-structures/rb-tree.ts.html +0 -106
  96. package/coverage/lcov-report/src/types/index.html +0 -116
  97. package/coverage/lcov-report/src/types/index.ts.html +0 -94
  98. package/coverage/lcov-report/src/types/utils/index.html +0 -116
  99. package/coverage/lcov-report/src/types/utils/index.ts.html +0 -91
  100. package/coverage/lcov-report/src/utils/index.html +0 -131
  101. package/coverage/lcov-report/src/utils/index.ts.html +0 -88
  102. package/coverage/lcov-report/src/utils/utils.ts.html +0 -322
  103. package/coverage/lcov-report/test/utils/index.html +0 -146
  104. package/coverage/lcov-report/test/utils/index.ts.html +0 -91
  105. package/coverage/lcov-report/test/utils/magnitude.ts.html +0 -148
  106. package/coverage/lcov-report/test/utils/number.ts.html +0 -94
  107. package/coverage/lcov.info +0 -6676
  108. package/docs/assets/highlight.css +0 -127
  109. package/docs/assets/main.js +0 -58
  110. package/docs/assets/search.js +0 -1
  111. package/docs/assets/style.css +0 -1367
  112. package/docs/classes/AVLTree.html +0 -2182
  113. package/docs/classes/AVLTreeNode.html +0 -399
  114. package/docs/classes/AaTree.html +0 -202
  115. package/docs/classes/AbstractBinaryTree.html +0 -1836
  116. package/docs/classes/AbstractBinaryTreeNode.html +0 -441
  117. package/docs/classes/AbstractEdge.html +0 -345
  118. package/docs/classes/AbstractGraph.html +0 -1105
  119. package/docs/classes/AbstractVertex.html +0 -299
  120. package/docs/classes/ArrayDeque.html +0 -469
  121. package/docs/classes/BST.html +0 -2026
  122. package/docs/classes/BSTNode.html +0 -400
  123. package/docs/classes/BTree.html +0 -202
  124. package/docs/classes/BinaryIndexedTree.html +0 -371
  125. package/docs/classes/BinaryTree.html +0 -1867
  126. package/docs/classes/BinaryTreeNode.html +0 -399
  127. package/docs/classes/Character.html +0 -250
  128. package/docs/classes/CoordinateMap.html +0 -513
  129. package/docs/classes/CoordinateSet.html +0 -474
  130. package/docs/classes/Deque.html +0 -1005
  131. package/docs/classes/DirectedEdge.html +0 -404
  132. package/docs/classes/DirectedGraph.html +0 -1530
  133. package/docs/classes/DirectedVertex.html +0 -286
  134. package/docs/classes/DoublyLinkedList.html +0 -998
  135. package/docs/classes/DoublyLinkedListNode.html +0 -327
  136. package/docs/classes/HashTable.html +0 -202
  137. package/docs/classes/Heap.html +0 -647
  138. package/docs/classes/HeapItem.html +0 -296
  139. package/docs/classes/LinkedListQueue.html +0 -884
  140. package/docs/classes/MapEdge.html +0 -391
  141. package/docs/classes/MapGraph.html +0 -1583
  142. package/docs/classes/MapVertex.html +0 -356
  143. package/docs/classes/Matrix2D.html +0 -532
  144. package/docs/classes/MatrixNTI2D.html +0 -270
  145. package/docs/classes/MaxHeap.html +0 -671
  146. package/docs/classes/MaxPriorityQueue.html +0 -866
  147. package/docs/classes/MinHeap.html +0 -672
  148. package/docs/classes/MinPriorityQueue.html +0 -868
  149. package/docs/classes/Navigator.html +0 -343
  150. package/docs/classes/ObjectDeque.html +0 -527
  151. package/docs/classes/Pair.html +0 -202
  152. package/docs/classes/PriorityQueue.html +0 -790
  153. package/docs/classes/Queue.html +0 -521
  154. package/docs/classes/RBTree.html +0 -2024
  155. package/docs/classes/RBTreeNode.html +0 -431
  156. package/docs/classes/SegmentTree.html +0 -464
  157. package/docs/classes/SegmentTreeNode.html +0 -387
  158. package/docs/classes/SinglyLinkedList.html +0 -830
  159. package/docs/classes/SinglyLinkedListNode.html +0 -300
  160. package/docs/classes/SkipLinkedList.html +0 -202
  161. package/docs/classes/SplayTree.html +0 -202
  162. package/docs/classes/Stack.html +0 -398
  163. package/docs/classes/TreeMap.html +0 -202
  164. package/docs/classes/TreeMultiset.html +0 -2510
  165. package/docs/classes/TreeMultisetNode.html +0 -447
  166. package/docs/classes/TreeNode.html +0 -344
  167. package/docs/classes/TreeSet.html +0 -202
  168. package/docs/classes/Trie.html +0 -402
  169. package/docs/classes/TrieNode.html +0 -310
  170. package/docs/classes/TwoThreeTree.html +0 -202
  171. package/docs/classes/UndirectedEdge.html +0 -374
  172. package/docs/classes/UndirectedGraph.html +0 -1285
  173. package/docs/classes/UndirectedVertex.html +0 -284
  174. package/docs/classes/Vector2D.html +0 -835
  175. package/docs/enums/CP.html +0 -211
  176. package/docs/enums/FamilyPosition.html +0 -239
  177. package/docs/enums/LoopType.html +0 -212
  178. package/docs/enums/RBColor.html +0 -204
  179. package/docs/enums/TopologicalProperty.html +0 -211
  180. package/docs/functions/arrayRemove.html +0 -208
  181. package/docs/functions/isThunk.html +0 -186
  182. package/docs/functions/toThunk.html +0 -186
  183. package/docs/functions/trampoline.html +0 -186
  184. package/docs/functions/trampolineAsync.html +0 -186
  185. package/docs/functions/uuidV4.html +0 -181
  186. package/docs/interfaces/IAVLTree.html +0 -1245
  187. package/docs/interfaces/IAbstractBinaryTree.html +0 -1101
  188. package/docs/interfaces/IAbstractBinaryTreeNode.html +0 -335
  189. package/docs/interfaces/IAbstractGraph.html +0 -433
  190. package/docs/interfaces/IBST.html +0 -1245
  191. package/docs/interfaces/IDirectedGraph.html +0 -570
  192. package/docs/interfaces/IRBTree.html +0 -1247
  193. package/docs/interfaces/IUNDirectedGraph.html +0 -463
  194. package/docs/types/AVLTreeNodeNested.html +0 -182
  195. package/docs/types/AVLTreeOptions.html +0 -180
  196. package/docs/types/AbstractBinaryTreeNodeNested.html +0 -182
  197. package/docs/types/AbstractBinaryTreeNodeProperties.html +0 -182
  198. package/docs/types/AbstractBinaryTreeNodeProperty.html +0 -182
  199. package/docs/types/AbstractBinaryTreeOptions.html +0 -182
  200. package/docs/types/BSTComparator.html +0 -192
  201. package/docs/types/BSTNodeNested.html +0 -182
  202. package/docs/types/BSTOptions.html +0 -182
  203. package/docs/types/BinaryTreeDeletedResult.html +0 -189
  204. package/docs/types/BinaryTreeNodeId.html +0 -177
  205. package/docs/types/BinaryTreeNodeNested.html +0 -182
  206. package/docs/types/BinaryTreeNodePropertyName.html +0 -177
  207. package/docs/types/BinaryTreeOptions.html +0 -180
  208. package/docs/types/DFSOrderPattern.html +0 -177
  209. package/docs/types/DijkstraResult.html +0 -199
  210. package/docs/types/Direction.html +0 -177
  211. package/docs/types/DummyAny.html +0 -190
  212. package/docs/types/EdgeId.html +0 -177
  213. package/docs/types/HeapOptions.html +0 -198
  214. package/docs/types/IAVLTreeNode.html +0 -184
  215. package/docs/types/IBSTNode.html +0 -184
  216. package/docs/types/IBinaryTree.html +0 -182
  217. package/docs/types/IBinaryTreeNode.html +0 -184
  218. package/docs/types/IRBTreeNode.html +0 -184
  219. package/docs/types/ITreeMultiset.html +0 -182
  220. package/docs/types/ITreeMultisetNode.html +0 -184
  221. package/docs/types/KeyValueObject.html +0 -182
  222. package/docs/types/KeyValueObjectWithId.html +0 -184
  223. package/docs/types/MapGraphCoordinate.html +0 -177
  224. package/docs/types/NavigatorParams.html +0 -211
  225. package/docs/types/NodeOrPropertyName.html +0 -177
  226. package/docs/types/NonNumberNonObjectButDefined.html +0 -177
  227. package/docs/types/ObjectWithNonNumberId.html +0 -184
  228. package/docs/types/ObjectWithNumberId.html +0 -184
  229. package/docs/types/ObjectWithoutId.html +0 -177
  230. package/docs/types/PriorityQueueComparator.html +0 -197
  231. package/docs/types/PriorityQueueDFSOrderPattern.html +0 -177
  232. package/docs/types/PriorityQueueOptions.html +0 -191
  233. package/docs/types/RBTreeNodeNested.html +0 -182
  234. package/docs/types/RBTreeOptions.html +0 -180
  235. package/docs/types/RestrictValById.html +0 -177
  236. package/docs/types/SegmentTreeNodeVal.html +0 -177
  237. package/docs/types/SpecifyOptional.html +0 -184
  238. package/docs/types/Thunk.html +0 -185
  239. package/docs/types/ToThunkFn.html +0 -185
  240. package/docs/types/TopologicalStatus.html +0 -177
  241. package/docs/types/TreeMultisetNodeNested.html +0 -182
  242. package/docs/types/TreeMultisetOptions.html +0 -180
  243. package/docs/types/TrlAsyncFn.html +0 -190
  244. package/docs/types/TrlFn.html +0 -190
  245. package/docs/types/Turning.html +0 -177
  246. package/docs/types/VertexId.html +0 -177
  247. package/docs/variables/THUNK_SYMBOL.html +0 -177
  248. package/rename_clear_files.sh +0 -29
  249. package/src/data-structures/binary-tree/aa-tree.ts +0 -1
  250. package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1631
  251. package/src/data-structures/binary-tree/avl-tree.ts +0 -310
  252. package/src/data-structures/binary-tree/b-tree.ts +0 -1
  253. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -76
  254. package/src/data-structures/binary-tree/binary-tree.ts +0 -47
  255. package/src/data-structures/binary-tree/bst.ts +0 -537
  256. package/src/data-structures/binary-tree/index.ts +0 -12
  257. package/src/data-structures/binary-tree/rb-tree.ts +0 -101
  258. package/src/data-structures/binary-tree/segment-tree.ts +0 -242
  259. package/src/data-structures/binary-tree/splay-tree.ts +0 -1
  260. package/src/data-structures/binary-tree/tree-multiset.ts +0 -700
  261. package/src/data-structures/binary-tree/two-three-tree.ts +0 -1
  262. package/src/data-structures/diagrams/README.md +0 -5
  263. package/src/data-structures/graph/abstract-graph.ts +0 -1040
  264. package/src/data-structures/graph/directed-graph.ts +0 -470
  265. package/src/data-structures/graph/index.ts +0 -4
  266. package/src/data-structures/graph/map-graph.ts +0 -129
  267. package/src/data-structures/graph/undirected-graph.ts +0 -274
  268. package/src/data-structures/hash/coordinate-map.ts +0 -67
  269. package/src/data-structures/hash/coordinate-set.ts +0 -56
  270. package/src/data-structures/hash/hash-table.ts +0 -1
  271. package/src/data-structures/hash/index.ts +0 -6
  272. package/src/data-structures/hash/pair.ts +0 -1
  273. package/src/data-structures/hash/tree-map.ts +0 -1
  274. package/src/data-structures/hash/tree-set.ts +0 -1
  275. package/src/data-structures/heap/heap.ts +0 -212
  276. package/src/data-structures/heap/index.ts +0 -3
  277. package/src/data-structures/heap/max-heap.ts +0 -31
  278. package/src/data-structures/heap/min-heap.ts +0 -32
  279. package/src/data-structures/index.ts +0 -11
  280. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
  281. package/src/data-structures/linked-list/index.ts +0 -3
  282. package/src/data-structures/linked-list/singly-linked-list.ts +0 -501
  283. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  284. package/src/data-structures/matrix/index.ts +0 -4
  285. package/src/data-structures/matrix/matrix.ts +0 -27
  286. package/src/data-structures/matrix/matrix2d.ts +0 -212
  287. package/src/data-structures/matrix/navigator.ts +0 -121
  288. package/src/data-structures/matrix/vector2d.ts +0 -316
  289. package/src/data-structures/priority-queue/index.ts +0 -3
  290. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -56
  291. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -57
  292. package/src/data-structures/priority-queue/priority-queue.ts +0 -359
  293. package/src/data-structures/queue/deque.ts +0 -297
  294. package/src/data-structures/queue/index.ts +0 -2
  295. package/src/data-structures/queue/queue.ts +0 -191
  296. package/src/data-structures/stack/index.ts +0 -1
  297. package/src/data-structures/stack/stack.ts +0 -98
  298. package/src/data-structures/tree/index.ts +0 -1
  299. package/src/data-structures/tree/tree.ts +0 -69
  300. package/src/data-structures/trie/index.ts +0 -1
  301. package/src/data-structures/trie/trie.ts +0 -225
  302. package/src/index.ts +0 -4
  303. package/src/interfaces/abstract-binary-tree.ts +0 -191
  304. package/src/interfaces/abstract-graph.ts +0 -31
  305. package/src/interfaces/avl-tree.ts +0 -25
  306. package/src/interfaces/binary-tree.ts +0 -6
  307. package/src/interfaces/bst.ts +0 -31
  308. package/src/interfaces/directed-graph.ts +0 -20
  309. package/src/interfaces/doubly-linked-list.ts +0 -1
  310. package/src/interfaces/heap.ts +0 -1
  311. package/src/interfaces/index.ts +0 -15
  312. package/src/interfaces/navigator.ts +0 -1
  313. package/src/interfaces/priority-queue.ts +0 -1
  314. package/src/interfaces/rb-tree.ts +0 -9
  315. package/src/interfaces/segment-tree.ts +0 -1
  316. package/src/interfaces/singly-linked-list.ts +0 -1
  317. package/src/interfaces/tree-multiset.ts +0 -7
  318. package/src/interfaces/undirected-graph.ts +0 -6
  319. package/src/types/data-structures/abstract-binary-tree.ts +0 -50
  320. package/src/types/data-structures/abstract-graph.ts +0 -11
  321. package/src/types/data-structures/avl-tree.ts +0 -5
  322. package/src/types/data-structures/binary-tree.ts +0 -5
  323. package/src/types/data-structures/bst.ts +0 -13
  324. package/src/types/data-structures/directed-graph.ts +0 -8
  325. package/src/types/data-structures/doubly-linked-list.ts +0 -1
  326. package/src/types/data-structures/heap.ts +0 -5
  327. package/src/types/data-structures/index.ts +0 -15
  328. package/src/types/data-structures/map-graph.ts +0 -1
  329. package/src/types/data-structures/navigator.ts +0 -13
  330. package/src/types/data-structures/priority-queue.ts +0 -9
  331. package/src/types/data-structures/rb-tree.ts +0 -8
  332. package/src/types/data-structures/segment-tree.ts +0 -1
  333. package/src/types/data-structures/singly-linked-list.ts +0 -1
  334. package/src/types/data-structures/tree-multiset.ts +0 -6
  335. package/src/types/helpers.ts +0 -1
  336. package/src/types/index.ts +0 -3
  337. package/src/types/utils/index.ts +0 -2
  338. package/src/types/utils/utils.ts +0 -6
  339. package/src/types/utils/validate-type.ts +0 -35
  340. package/src/utils/index.ts +0 -1
  341. package/src/utils/utils.ts +0 -79
  342. package/tsconfig.build.json +0 -33
@@ -1,1631 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
-
9
- import {trampoline} from '../../utils';
10
- import type {
11
- AbstractBinaryTreeNodeNested,
12
- AbstractBinaryTreeNodeProperties,
13
- AbstractBinaryTreeNodeProperty,
14
- BinaryTreeDeletedResult,
15
- BinaryTreeNodeId,
16
- BinaryTreeNodePropertyName,
17
- DFSOrderPattern,
18
- NodeOrPropertyName
19
- } from '../../types';
20
- import {AbstractBinaryTreeOptions, FamilyPosition, LoopType} from '../../types';
21
- import {IAbstractBinaryTree, IAbstractBinaryTreeNode} from '../../interfaces';
22
-
23
- export abstract class AbstractBinaryTreeNode<
24
- T = any,
25
- NEIGHBOR extends AbstractBinaryTreeNode<T, NEIGHBOR> = AbstractBinaryTreeNodeNested<T>
26
- > implements IAbstractBinaryTreeNode<T, NEIGHBOR>
27
- {
28
- /**
29
- * The constructor function initializes a BinaryTreeNode object with an id and an optional value.
30
- * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
31
- * of the binary tree node. It is used to distinguish one node from another in the binary tree.
32
- * @param {T} [val] - The "val" parameter is an optional parameter of type T. It represents the value that will be
33
- * stored in the binary tree node. If no value is provided, it will be set to undefined.
34
- */
35
- protected constructor(id: BinaryTreeNodeId, val?: T) {
36
- this._id = id;
37
- this._val = val;
38
- }
39
-
40
- private _id: BinaryTreeNodeId;
41
-
42
- get id(): BinaryTreeNodeId {
43
- return this._id;
44
- }
45
-
46
- set id(v: BinaryTreeNodeId) {
47
- this._id = v;
48
- }
49
-
50
- private _val: T | undefined;
51
-
52
- get val(): T | undefined {
53
- return this._val;
54
- }
55
-
56
- set val(value: T | undefined) {
57
- this._val = value;
58
- }
59
-
60
- private _left: NEIGHBOR | null | undefined;
61
-
62
- get left(): NEIGHBOR | null | undefined {
63
- return this._left;
64
- }
65
-
66
- set left(v: NEIGHBOR | null | undefined) {
67
- if (v) {
68
- v.parent = this as unknown as NEIGHBOR;
69
- }
70
- this._left = v;
71
- }
72
-
73
- private _right: NEIGHBOR | null | undefined;
74
-
75
- get right(): NEIGHBOR | null | undefined {
76
- return this._right;
77
- }
78
-
79
- set right(v: NEIGHBOR | null | undefined) {
80
- if (v) {
81
- v.parent = this as unknown as NEIGHBOR;
82
- }
83
- this._right = v;
84
- }
85
-
86
- private _parent: NEIGHBOR | null | undefined;
87
-
88
- get parent(): NEIGHBOR | null | undefined {
89
- return this._parent;
90
- }
91
-
92
- set parent(v: NEIGHBOR | null | undefined) {
93
- this._parent = v;
94
- }
95
-
96
- private _height = 0;
97
-
98
- get height(): number {
99
- return this._height;
100
- }
101
-
102
- set height(v: number) {
103
- this._height = v;
104
- }
105
-
106
- /**
107
- * The function determines the position of a node in a family tree structure.
108
- * @returns a value of type `FamilyPosition`.
109
- */
110
- get familyPosition(): FamilyPosition {
111
- const that = this as unknown as NEIGHBOR;
112
- if (that.parent) {
113
- if (that.parent.left === that) {
114
- if (that.left || that.right) {
115
- return FamilyPosition.ROOT_LEFT;
116
- } else {
117
- return FamilyPosition.LEFT;
118
- }
119
- } else if (that.parent.right === that) {
120
- if (that.left || that.right) {
121
- return FamilyPosition.ROOT_RIGHT;
122
- } else {
123
- return FamilyPosition.RIGHT;
124
- }
125
- } else {
126
- return FamilyPosition.MAL_NODE;
127
- }
128
- } else {
129
- if (that.left || that.right) {
130
- return FamilyPosition.ROOT;
131
- } else {
132
- return FamilyPosition.ISOLATED;
133
- }
134
- }
135
- }
136
- }
137
-
138
- export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'], N> = AbstractBinaryTreeNode>
139
- implements IAbstractBinaryTree<N>
140
- {
141
- /**
142
- * The protected constructor initializes the options for an abstract binary tree.
143
- * @param {AbstractBinaryTreeOptions} [options] - An optional object that contains configuration options for the binary
144
- * tree.
145
- */
146
- protected constructor(options?: AbstractBinaryTreeOptions) {
147
- if (options !== undefined) {
148
- const {loopType = LoopType.ITERATIVE} = options;
149
- this._loopType = loopType;
150
- }
151
- this.clear();
152
- }
153
-
154
- private _root: N | null = null;
155
-
156
- get root(): N | null {
157
- return this._root;
158
- }
159
-
160
- private _size = 0;
161
-
162
- get size(): number {
163
- return this._size;
164
- }
165
-
166
- private _loopType: LoopType = LoopType.ITERATIVE;
167
-
168
- get loopType(): LoopType {
169
- return this._loopType;
170
- }
171
-
172
- private _visitedId: BinaryTreeNodeId[] = [];
173
-
174
- get visitedId(): BinaryTreeNodeId[] {
175
- return this._visitedId;
176
- }
177
-
178
- private _visitedVal: N['val'][] = [];
179
-
180
- get visitedVal(): N['val'][] {
181
- return this._visitedVal;
182
- }
183
-
184
- private _visitedNode: N[] = [];
185
-
186
- get visitedNode(): N[] {
187
- return this._visitedNode;
188
- }
189
-
190
- private _visitedLeftSum: number[] = [];
191
-
192
- get visitedLeftSum(): number[] {
193
- return this._visitedLeftSum;
194
- }
195
-
196
- abstract createNode(id: BinaryTreeNodeId, val?: N['val']): N | null;
197
-
198
- /**
199
- * The `swapLocation` function swaps the location of two nodes in a binary tree.
200
- * @param {N} srcNode - The source node that you want to swap with the destination node.
201
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
202
- * be swapped to.
203
- * @returns The `destNode` is being returned.
204
- */
205
- swapLocation(srcNode: N, destNode: N): N {
206
- const {id, val, height} = destNode;
207
- const tempNode = this.createNode(id, val);
208
-
209
- if (tempNode) {
210
- tempNode.height = height;
211
-
212
- destNode.id = srcNode.id;
213
- destNode.val = srcNode.val;
214
- destNode.height = srcNode.height;
215
-
216
- srcNode.id = tempNode.id;
217
- srcNode.val = tempNode.val;
218
- srcNode.height = tempNode.height;
219
- }
220
-
221
- return destNode;
222
- }
223
-
224
- /**
225
- * The clear() function resets the root, size, and maxId properties to their initial values.
226
- */
227
- clear() {
228
- this._root = null;
229
- this._size = 0;
230
- this._clearResults();
231
- }
232
-
233
- /**
234
- * The function checks if the size of an object is equal to zero and returns a boolean value.
235
- * @returns A boolean value indicating whether the size of the object is 0 or not.
236
- */
237
- isEmpty(): boolean {
238
- return this.size === 0;
239
- }
240
-
241
- /**
242
- * When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
243
- * In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
244
- */
245
-
246
- /**
247
- * The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
248
- * @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId`, which
249
- * is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
250
- * node itself. It can also be `null` if no node is specified.
251
- * @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
252
- * being added to the binary tree.
253
- * @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
254
- */
255
- add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val']): N | null | undefined {
256
- const _bfs = (root: N, newNode: N | null): N | undefined | null => {
257
- const queue: Array<N | null> = [root];
258
- while (queue.length > 0) {
259
- const cur = queue.shift();
260
- if (cur) {
261
- if (newNode && cur.id === newNode.id) return;
262
- const inserted = this._addTo(newNode, cur);
263
- if (inserted !== undefined) return inserted;
264
- if (cur.left) queue.push(cur.left);
265
- if (cur.right) queue.push(cur.right);
266
- } else return;
267
- }
268
- return;
269
- };
270
-
271
- let inserted: N | null | undefined, needInsert: N | null;
272
-
273
- if (idOrNode === null) {
274
- needInsert = null;
275
- } else if (typeof idOrNode === 'number') {
276
- needInsert = this.createNode(idOrNode, val);
277
- } else if (idOrNode instanceof AbstractBinaryTreeNode) {
278
- needInsert = idOrNode;
279
- } else {
280
- return;
281
- }
282
-
283
- const existNode = idOrNode ? this.get(idOrNode, 'id') : undefined;
284
-
285
- if (this.root) {
286
- if (existNode) {
287
- existNode.val = val;
288
- inserted = existNode;
289
- } else {
290
- inserted = _bfs(this.root, needInsert);
291
- }
292
- } else {
293
- this._setRoot(needInsert);
294
- if (needInsert !== null) {
295
- this._setSize(1);
296
- } else {
297
- this._setSize(0);
298
- }
299
- inserted = this.root;
300
- }
301
- return inserted;
302
- }
303
-
304
- /**
305
- * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
306
- * values, and adds them to the binary tree.
307
- * @param {(BinaryTreeNodeId | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeId or BinaryTreeNode
308
- * objects, or null values.
309
- * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
310
- * the nodes or node IDs being added. It is used to set the value of each node being added. If `data` is not provided,
311
- * the value of the nodes will be `undefined`.
312
- * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
313
- */
314
- addMany(idsOrNodes: (BinaryTreeNodeId | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[] {
315
- // TODO not sure addMany not be run multi times
316
- const inserted: (N | null | undefined)[] = [];
317
-
318
- for (let i = 0; i < idsOrNodes.length; i++) {
319
- const idOrNode = idsOrNodes[i];
320
- if (idOrNode instanceof AbstractBinaryTreeNode) {
321
- inserted.push(this.add(idOrNode.id, idOrNode.val));
322
- continue;
323
- }
324
-
325
- if (idOrNode === null) {
326
- inserted.push(this.add(null));
327
- continue;
328
- }
329
-
330
- const val = data?.[i];
331
- inserted.push(this.add(idOrNode, val));
332
- }
333
- return inserted;
334
- }
335
-
336
- /**
337
- * The `fill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
338
- * @param {(BinaryTreeNodeId | N)[]} idsOrNodes - The `idsOrNodes` parameter is an array that can contain either
339
- * `BinaryTreeNodeId` or `N` values.
340
- * @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
341
- * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `idsOrNodes`
342
- * array. Each value in the `data` array will be assigned to the
343
- * @returns The method is returning a boolean value.
344
- */
345
- fill(idsOrNodes: (BinaryTreeNodeId | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean {
346
- this.clear();
347
- return idsOrNodes.length === this.addMany(idsOrNodes, data).length;
348
- }
349
-
350
- /**
351
- * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent node
352
- * that needs to be balanced.
353
- * @param {N | BinaryTreeNodeId} nodeOrId - The `nodeOrId` parameter can be either a node object (`N`) or a binary tree
354
- * node ID (`BinaryTreeNodeId`).
355
- * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
356
- * determines whether to update the left sum of all nodes in the binary tree after removing a node. If
357
- * `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be updated. If it
358
- * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
359
- */
360
- remove(nodeOrId: N | BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): BinaryTreeDeletedResult<N>[] {
361
- isUpdateAllLeftSum = isUpdateAllLeftSum === undefined ? true : isUpdateAllLeftSum;
362
- // TODO may implement update all left sum
363
- if (isUpdateAllLeftSum) {
364
- }
365
-
366
- const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
367
- if (!this.root) return bstDeletedResult;
368
-
369
- const curr: N | null = typeof nodeOrId === 'number' ? this.get(nodeOrId) : nodeOrId;
370
- if (!curr) return bstDeletedResult;
371
-
372
- const parent: N | null = curr?.parent ? curr.parent : null;
373
- let needBalanced: N | null = null,
374
- orgCurrent = curr;
375
-
376
- if (!curr.left) {
377
- if (!parent) {
378
- if (curr.right !== undefined) this._setRoot(curr.right);
379
- } else {
380
- const {familyPosition: fp} = curr;
381
- if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
382
- parent.left = curr.right;
383
- } else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
384
- parent.right = curr.right;
385
- }
386
- needBalanced = parent;
387
- }
388
- } else {
389
- const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
390
- if (leftSubTreeRightMost) {
391
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
392
- orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
393
- if (parentOfLeftSubTreeMax) {
394
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
395
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
396
- else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
397
- needBalanced = parentOfLeftSubTreeMax;
398
- }
399
- }
400
- }
401
- this._setSize(this.size - 1);
402
-
403
- bstDeletedResult.push({deleted: orgCurrent, needBalanced});
404
- return bstDeletedResult;
405
- }
406
-
407
- /**
408
- * The function calculates the depth of a node in a binary tree.
409
- * @param {N | BinaryTreeNodeId | null} beginRoot - The `beginRoot` parameter can be one of the following:
410
- * @returns the depth of the given node or binary tree.
411
- */
412
- getDepth(beginRoot: N | BinaryTreeNodeId | null): number {
413
- if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
414
-
415
- let depth = 0;
416
- while (beginRoot?.parent) {
417
- depth++;
418
- beginRoot = beginRoot.parent;
419
- }
420
- return depth;
421
- }
422
-
423
- /**
424
- * The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
425
- * @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
426
- * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
427
- * node), or `null`.
428
- * @returns the height of the binary tree.
429
- */
430
- getHeight(beginRoot?: N | BinaryTreeNodeId | null): number {
431
- beginRoot = beginRoot ?? this.root;
432
-
433
- if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
434
- if (!beginRoot) return -1;
435
-
436
- if (this._loopType === LoopType.RECURSIVE) {
437
- const _getMaxHeight = (cur: N | null | undefined): number => {
438
- if (!cur) return -1;
439
- const leftHeight = _getMaxHeight(cur.left);
440
- const rightHeight = _getMaxHeight(cur.right);
441
- return Math.max(leftHeight, rightHeight) + 1;
442
- };
443
-
444
- return _getMaxHeight(beginRoot);
445
- } else {
446
- if (!beginRoot) {
447
- return -1;
448
- }
449
-
450
- const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
451
- let maxHeight = 0;
452
-
453
- while (stack.length > 0) {
454
- const {node, depth} = stack.pop()!;
455
-
456
- if (node.left) {
457
- stack.push({node: node.left, depth: depth + 1});
458
- }
459
-
460
- if (node.right) {
461
- stack.push({node: node.right, depth: depth + 1});
462
- }
463
-
464
- maxHeight = Math.max(maxHeight, depth);
465
- }
466
-
467
- return maxHeight;
468
- }
469
- }
470
-
471
- /**
472
- * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
473
- * approach.
474
- * @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
475
- * represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
476
- * for `beginRoot`, the `this.root` property is used as the default value.
477
- * @returns The function `getMinHeight` returns the minimum height of the binary tree.
478
- */
479
- getMinHeight(beginRoot?: N | null): number {
480
- beginRoot = beginRoot || this.root;
481
- if (!beginRoot) return -1;
482
-
483
- if (this._loopType === LoopType.RECURSIVE) {
484
- const _getMinHeight = (cur: N | null | undefined): number => {
485
- if (!cur) return 0;
486
- if (!cur.left && !cur.right) return 0;
487
- const leftMinHeight = _getMinHeight(cur.left);
488
- const rightMinHeight = _getMinHeight(cur.right);
489
- return Math.min(leftMinHeight, rightMinHeight) + 1;
490
- };
491
-
492
- return _getMinHeight(beginRoot);
493
- } else {
494
- const stack: N[] = [];
495
- let node: N | null | undefined = beginRoot,
496
- last: N | null = null;
497
- const depths: Map<N, number> = new Map();
498
-
499
- while (stack.length > 0 || node) {
500
- if (node) {
501
- stack.push(node);
502
- node = node.left;
503
- } else {
504
- node = stack[stack.length - 1];
505
- if (!node.right || last === node.right) {
506
- node = stack.pop();
507
- if (node) {
508
- const leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
509
- const rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
510
- depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
511
- last = node;
512
- node = null;
513
- }
514
- } else node = node.right;
515
- }
516
- }
517
-
518
- return depths.get(beginRoot) ?? -1;
519
- }
520
- }
521
-
522
- /**
523
- * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
524
- * tree.
525
- * @param {N | null} [beginRoot] - The parameter `beginRoot` is of type `N` or `null`. It represents the root node of a
526
- * tree or null if the tree is empty.
527
- * @returns The method is returning a boolean value.
528
- */
529
- isPerfectlyBalanced(beginRoot?: N | null): boolean {
530
- return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
531
- }
532
-
533
- /**
534
- * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
535
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
536
- * generic type `N`. It represents the property of the binary tree node that you want to search for.
537
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
538
- * specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
539
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
540
- * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
541
- * function will stop traversing the tree and return the first matching node. If `only
542
- * @returns an array of nodes (type N).
543
- */
544
- getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[] {
545
- if (!this.root) return [];
546
- propertyName = propertyName ?? 'id';
547
-
548
- const result: N[] = [];
549
-
550
- if (this.loopType === LoopType.RECURSIVE) {
551
- const _traverse = (cur: N) => {
552
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
553
- if (!cur.left && !cur.right) return;
554
- cur.left && _traverse(cur.left);
555
- cur.right && _traverse(cur.right);
556
- };
557
-
558
- _traverse(this.root);
559
- } else {
560
- const queue: N[] = [this.root];
561
- while (queue.length > 0) {
562
- const cur = queue.shift();
563
- if (cur) {
564
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result;
565
- cur.left && queue.push(cur.left);
566
- cur.right && queue.push(cur.right);
567
- }
568
- }
569
- }
570
-
571
- return result;
572
- }
573
-
574
- /**
575
- * The function checks if a binary tree node has a specific property.
576
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
577
- * It represents the property of the binary tree node that you want to check.
578
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
579
- * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'id'.
580
- * @returns a boolean value.
581
- */
582
- has(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName): boolean {
583
- propertyName = propertyName ?? 'id';
584
- // TODO may support finding node by value equal
585
- return this.getNodes(nodeProperty, propertyName).length > 0;
586
- }
587
-
588
- /**
589
- * The function returns the first node that matches the given property name and value, or null if no matching node is
590
- * found.
591
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
592
- * It represents the property of the binary tree node that you want to search for.
593
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
594
- * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
595
- * default value is set to `'id'`.
596
- * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
597
- * If no matching node is found, it returns null.
598
- */
599
- get(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName): N | null {
600
- propertyName = propertyName ?? 'id';
601
- // TODO may support finding node by value equal
602
- return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
603
- }
604
-
605
- /**
606
- * The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
607
- * an option to reverse the order of the nodes.
608
- * @param {N} node - The `node` parameter represents a node in a tree structure. It is of type `N`, which could be any
609
- * type that represents a node in your specific implementation.
610
- * @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
611
- * path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
612
- * `isReverse` is set to `false` or not provided, the path will
613
- * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
614
- */
615
- getPathToRoot(node: N, isReverse = true): N[] {
616
- // TODO to support get path through passing id
617
- const result: N[] = [];
618
- while (node.parent) {
619
- // Array.push + Array.reverse is more efficient than Array.unshift
620
- // TODO may consider using Deque, so far this is not the performance bottleneck
621
- result.push(node);
622
- node = node.parent;
623
- }
624
- result.push(node);
625
- return isReverse ? result.reverse() : result;
626
- }
627
-
628
- /**
629
- * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
630
- * no node is specified.
631
- * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
632
- * node), or `null`.
633
- * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
634
- * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
635
- * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
636
- * node is found (
637
- */
638
- getLeftMost(): N | null;
639
-
640
- /**
641
- * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
642
- * no node is specified.
643
- * @param {N | BinaryTreeNodeId | null} [node] - The `beginRoot` parameter is optional and can be of type `N` (a
644
- * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
645
- * node).
646
- * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
647
- * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
648
- * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
649
- * node is found (
650
- */
651
- getLeftMost(node: N): N;
652
-
653
- /**
654
- * The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
655
- * no node is specified.
656
- * @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
657
- * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
658
- * node), or `null`.
659
- * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
660
- * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
661
- * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
662
- * node is found (
663
- */
664
- getLeftMost(beginRoot?: N | BinaryTreeNodeId | null): N | null {
665
- if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
666
-
667
- beginRoot = beginRoot ?? this.root;
668
- if (!beginRoot) return beginRoot;
669
-
670
- if (this._loopType === LoopType.RECURSIVE) {
671
- const _traverse = (cur: N): N => {
672
- if (!cur.left) return cur;
673
- return _traverse(cur.left);
674
- };
675
-
676
- return _traverse(beginRoot);
677
- } else {
678
- // Indirect implementation of iteration using tail recursion optimization
679
- const _traverse = trampoline((cur: N) => {
680
- if (!cur.left) return cur;
681
- return _traverse.cont(cur.left);
682
- });
683
-
684
- return _traverse(beginRoot);
685
- }
686
- }
687
-
688
- /**
689
- * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
690
- * recursion optimization.
691
- * @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the
692
- * rightmost node starting from the root of the binary tree.
693
- */
694
- getRightMost(): N | null;
695
-
696
- /**
697
- * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
698
- * recursion optimization.
699
- * @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
700
- * starting node from which we want to find the rightmost node. If no node is provided, the function will default to
701
- * using the root node of the data structure.
702
- * @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the rightmost node
703
- * starting from that node.
704
- */
705
- getRightMost(node: N): N;
706
-
707
- /**
708
- * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
709
- * recursion optimization.
710
- * @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
711
- * starting node from which we want to find the rightmost node. If no node is provided, the function will default to
712
- * using the root node of the data structure.
713
- * @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
714
- * it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
715
- * rightmost node starting from the root of the binary tree.
716
- */
717
- getRightMost(node?: N | null): N | null {
718
- // TODO support get right most by passing id in
719
- node = node ?? this.root;
720
- if (!node) return node;
721
-
722
- if (this._loopType === LoopType.RECURSIVE) {
723
- const _traverse = (cur: N): N => {
724
- if (!cur.right) return cur;
725
- return _traverse(cur.right);
726
- };
727
-
728
- return _traverse(node);
729
- } else {
730
- // Indirect implementation of iteration using tail recursion optimization
731
- const _traverse = trampoline((cur: N) => {
732
- if (!cur.right) return cur;
733
- return _traverse.cont(cur.right);
734
- });
735
-
736
- return _traverse(node);
737
- }
738
- }
739
-
740
- /**
741
- * The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
742
- * @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
743
- * @returns a boolean value.
744
- */
745
- isSubtreeBST(node: N | null): boolean {
746
- // TODO there is a bug
747
- if (!node) return true;
748
-
749
- if (this._loopType === LoopType.RECURSIVE) {
750
- const dfs = (cur: N | null | undefined, min: BinaryTreeNodeId, max: BinaryTreeNodeId): boolean => {
751
- if (!cur) return true;
752
- if (cur.id <= min || cur.id >= max) return false;
753
- return dfs(cur.left, min, cur.id) && dfs(cur.right, cur.id, max);
754
- };
755
-
756
- return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
757
- } else {
758
- const stack = [];
759
- let prev = Number.MIN_SAFE_INTEGER,
760
- curr: N | null | undefined = node;
761
- while (curr || stack.length > 0) {
762
- while (curr) {
763
- stack.push(curr);
764
- curr = curr.left;
765
- }
766
- curr = stack.pop()!;
767
- if (!curr || prev >= curr.id) return false;
768
- prev = curr.id;
769
- curr = curr.right;
770
- }
771
- return true;
772
- }
773
- }
774
-
775
- /**
776
- * The function isBST checks if the binary tree is valid binary search tree.
777
- * @returns The `isBST()` function is returning a boolean value.
778
- */
779
- isBST(): boolean {
780
- return this.isSubtreeBST(this.root);
781
- }
782
-
783
- /**
784
- * The function calculates the size of a subtree by traversing it either recursively or iteratively.
785
- * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
786
- * binary tree.
787
- * @returns the size of the subtree rooted at `subTreeRoot`.
788
- */
789
- getSubTreeSize(subTreeRoot: N | null | undefined) {
790
- // TODO support id passed in
791
- let size = 0;
792
- if (!subTreeRoot) return size;
793
-
794
- if (this._loopType === LoopType.RECURSIVE) {
795
- const _traverse = (cur: N) => {
796
- size++;
797
- cur.left && _traverse(cur.left);
798
- cur.right && _traverse(cur.right);
799
- };
800
-
801
- _traverse(subTreeRoot);
802
- return size;
803
- } else {
804
- const stack: N[] = [subTreeRoot];
805
-
806
- while (stack.length > 0) {
807
- const cur = stack.pop()!;
808
- size++;
809
- cur.right && stack.push(cur.right);
810
- cur.left && stack.push(cur.left);
811
- }
812
-
813
- return size;
814
- }
815
- }
816
-
817
- /**
818
- * The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
819
- * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
820
- * tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
821
- * @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
822
- * property of the binary tree node to use for calculating the sum. It can be either 'id' or 'val'. If propertyName is
823
- * not provided, it defaults to 'id'.
824
- * @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
825
- */
826
- subTreeSum(subTreeRoot: N | BinaryTreeNodeId | null, propertyName?: BinaryTreeNodePropertyName): number {
827
- propertyName = propertyName ?? 'id';
828
- if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
829
-
830
- if (!subTreeRoot) return 0;
831
-
832
- let sum = 0;
833
-
834
- const _sumByProperty = (cur: N) => {
835
- let needSum: number;
836
- switch (propertyName) {
837
- case 'id':
838
- needSum = cur.id;
839
- break;
840
- case 'val':
841
- needSum = typeof cur.val === 'number' ? cur.val : 0;
842
- break;
843
- default:
844
- needSum = cur.id;
845
- break;
846
- }
847
- return needSum;
848
- };
849
-
850
- if (this._loopType === LoopType.RECURSIVE) {
851
- const _traverse = (cur: N): void => {
852
- sum += _sumByProperty(cur);
853
- cur.left && _traverse(cur.left);
854
- cur.right && _traverse(cur.right);
855
- };
856
-
857
- _traverse(subTreeRoot);
858
- } else {
859
- const stack: N[] = [subTreeRoot];
860
-
861
- while (stack.length > 0) {
862
- const cur = stack.pop()!;
863
- sum += _sumByProperty(cur);
864
- cur.right && stack.push(cur.right);
865
- cur.left && stack.push(cur.left);
866
- }
867
- }
868
-
869
- return sum;
870
- }
871
-
872
- /**
873
- * The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
874
- * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
875
- * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
876
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
877
- * each node in the subtree should be incremented.
878
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
879
- * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'id'.
880
- * @returns a boolean value.
881
- */
882
- subTreeAdd(
883
- subTreeRoot: N | BinaryTreeNodeId | null,
884
- delta: number,
885
- propertyName?: BinaryTreeNodePropertyName
886
- ): boolean {
887
- propertyName = propertyName ?? 'id';
888
- if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
889
-
890
- if (!subTreeRoot) return false;
891
-
892
- const _addByProperty = (cur: N) => {
893
- switch (propertyName) {
894
- case 'id':
895
- cur.id += delta;
896
- break;
897
- default:
898
- cur.id += delta;
899
- break;
900
- }
901
- };
902
-
903
- if (this._loopType === LoopType.RECURSIVE) {
904
- const _traverse = (cur: N) => {
905
- _addByProperty(cur);
906
- cur.left && _traverse(cur.left);
907
- cur.right && _traverse(cur.right);
908
- };
909
-
910
- _traverse(subTreeRoot);
911
- } else {
912
- const stack: N[] = [subTreeRoot];
913
-
914
- while (stack.length > 0) {
915
- const cur = stack.pop()!;
916
-
917
- _addByProperty(cur);
918
- cur.right && stack.push(cur.right);
919
- cur.left && stack.push(cur.left);
920
- }
921
- }
922
- return true;
923
- }
924
-
925
- /**
926
- * Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on their 'id' property.
927
- * @returns An array of binary tree node IDs.
928
- */
929
- BFS(): BinaryTreeNodeId[];
930
-
931
- /**
932
- * Performs a breadth-first search (BFS) on a binary tree, accumulating properties of each node based on the specified property name.
933
- * @param {'id'} nodeOrPropertyName - The name of the property to accumulate.
934
- * @returns An array of values corresponding to the specified property.
935
- */
936
- BFS(nodeOrPropertyName: 'id'): BinaryTreeNodeId[];
937
-
938
- /**
939
- * Performs a breadth-first search (BFS) on a binary tree, accumulating the 'val' property of each node.
940
- * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
941
- * @returns An array of 'val' properties from each node.
942
- */
943
- BFS(nodeOrPropertyName: 'val'): N['val'][];
944
-
945
- /**
946
- * Performs a breadth-first search (BFS) on a binary tree, accumulating nodes themselves.
947
- * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
948
- * @returns An array of binary tree nodes.
949
- */
950
- BFS(nodeOrPropertyName: 'node'): N[];
951
-
952
- /**
953
- * The BFS function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
954
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
955
- * If a node is provided, the BFS algorithm will be performed starting from that node.
956
- * If a property name is provided, the BFS algorithm will be performed starting from the root node, accumulating the specified property.
957
- * @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
958
- */
959
- BFS(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
960
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
961
- this._clearResults();
962
- const queue: Array<N | null | undefined> = [this.root];
963
-
964
- while (queue.length !== 0) {
965
- const cur = queue.shift();
966
- if (cur) {
967
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
968
- if (cur?.left !== null) queue.push(cur.left);
969
- if (cur?.right !== null) queue.push(cur.right);
970
- }
971
- }
972
-
973
- return this._getResultByPropertyName(nodeOrPropertyName);
974
- }
975
-
976
- /**
977
- * Performs a depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their 'id' property.
978
- * @returns An array of binary tree node IDs.
979
- */
980
- DFS(): BinaryTreeNodeId[];
981
-
982
- /**
983
- * Performs a depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on the specified property name.
984
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
985
- * @param {string} nodeOrPropertyName - The name of the property to accumulate.
986
- * @returns An array of values corresponding to the specified property.
987
- */
988
- DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
989
-
990
- /**
991
- * Performs a depth-first search (DFS) traversal on a binary tree and accumulates the 'val' property of each node.
992
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
993
- * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
994
- * @returns An array of 'val' properties from each node.
995
- */
996
- DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
997
-
998
- /**
999
- * Performs a depth-first search (DFS) traversal on a binary tree and accumulates nodes themselves.
1000
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1001
- * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
1002
- * @returns An array of binary tree nodes.
1003
- */
1004
- DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
1005
-
1006
- /**
1007
- * The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
1008
- * each node based on the specified pattern and property name.
1009
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1010
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'id'`.
1011
- * @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
1012
- */
1013
- DFS(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1014
- pattern = pattern ?? 'in';
1015
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1016
- this._clearResults();
1017
- const _traverse = (node: N) => {
1018
- switch (pattern) {
1019
- case 'in':
1020
- if (node.left) _traverse(node.left);
1021
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
1022
- if (node.right) _traverse(node.right);
1023
- break;
1024
- case 'pre':
1025
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
1026
- if (node.left) _traverse(node.left);
1027
- if (node.right) _traverse(node.right);
1028
- break;
1029
- case 'post':
1030
- if (node.left) _traverse(node.left);
1031
- if (node.right) _traverse(node.right);
1032
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
1033
- break;
1034
- }
1035
- };
1036
-
1037
- this.root && _traverse(this.root);
1038
- return this._getResultByPropertyName(nodeOrPropertyName);
1039
- }
1040
-
1041
- // --- start additional methods ---
1042
-
1043
- /**
1044
- * Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on their 'id' property.
1045
- * @returns An array of binary tree node IDs.
1046
- */
1047
- DFSIterative(): BinaryTreeNodeId[];
1048
-
1049
- /**
1050
- * Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates properties of each node based on the specified property name.
1051
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1052
- * @param {string} nodeOrPropertyName - The name of the property to accumulate.
1053
- * @returns An array of values corresponding to the specified property.
1054
- */
1055
- DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
1056
-
1057
- /**
1058
- * Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates the 'val' property of each node.
1059
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1060
- * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
1061
- * @returns An array of 'val' properties from each node.
1062
- */
1063
- DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
1064
-
1065
- /**
1066
- * Performs an iterative depth-first search (DFS) traversal on a binary tree and accumulates nodes themselves.
1067
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1068
- * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
1069
- * @returns An array of binary tree nodes.
1070
- */
1071
- DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
1072
-
1073
- /**
1074
- * The DFSIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
1075
- * specify the traversal pattern and the property name to accumulate results by.
1076
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1077
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'id'`.
1078
- * @returns An object of type AbstractBinaryTreeNodeProperties<N>.
1079
- */
1080
- DFSIterative(
1081
- pattern?: 'in' | 'pre' | 'post',
1082
- nodeOrPropertyName?: NodeOrPropertyName
1083
- ): AbstractBinaryTreeNodeProperties<N> {
1084
- pattern = pattern || 'in';
1085
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1086
- this._clearResults();
1087
- if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName);
1088
- // 0: visit, 1: print
1089
- const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: this.root}];
1090
-
1091
- while (stack.length > 0) {
1092
- const cur = stack.pop();
1093
- if (!cur || !cur.node) continue;
1094
- if (cur.opt === 1) {
1095
- this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
1096
- } else {
1097
- switch (pattern) {
1098
- case 'in':
1099
- stack.push({opt: 0, node: cur.node.right});
1100
- stack.push({opt: 1, node: cur.node});
1101
- stack.push({opt: 0, node: cur.node.left});
1102
- break;
1103
- case 'pre':
1104
- stack.push({opt: 0, node: cur.node.right});
1105
- stack.push({opt: 0, node: cur.node.left});
1106
- stack.push({opt: 1, node: cur.node});
1107
- break;
1108
- case 'post':
1109
- stack.push({opt: 1, node: cur.node});
1110
- stack.push({opt: 0, node: cur.node.right});
1111
- stack.push({opt: 0, node: cur.node.left});
1112
- break;
1113
- default:
1114
- stack.push({opt: 0, node: cur.node.right});
1115
- stack.push({opt: 1, node: cur.node});
1116
- stack.push({opt: 0, node: cur.node.left});
1117
- break;
1118
- }
1119
- }
1120
- }
1121
-
1122
- return this._getResultByPropertyName(nodeOrPropertyName);
1123
- }
1124
-
1125
- /**
1126
- * Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'id' property.
1127
- * @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
1128
- * @returns An array of binary tree node IDs.
1129
- */
1130
- levelIterative(node: N | null): BinaryTreeNodeId[];
1131
-
1132
- /**
1133
- * Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on the specified property name.
1134
- * @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
1135
- * @param {string} nodeOrPropertyName - The name of the property to accumulate.
1136
- * @returns An array of values corresponding to the specified property.
1137
- */
1138
- levelIterative(node: N | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
1139
-
1140
- /**
1141
- * Performs a level-order traversal on a binary tree starting from the specified node and accumulates the 'val' property of each node.
1142
- * @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
1143
- * @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
1144
- * @returns An array of 'val' properties from each node.
1145
- */
1146
- levelIterative(node: N | null, nodeOrPropertyName?: 'val'): N['val'][];
1147
-
1148
- /**
1149
- * Performs a level-order traversal on a binary tree starting from the specified node and accumulates nodes themselves.
1150
- * @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
1151
- * @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
1152
- * @returns An array of binary tree nodes.
1153
- */
1154
- levelIterative(node: N | null, nodeOrPropertyName?: 'node'): N[];
1155
-
1156
- /**
1157
- * The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
1158
- * in an array, based on a specified property name.
1159
- * @param {N | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
1160
- * node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
1161
- * the tree is used as the starting node.
1162
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1163
- * can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
1164
- * will accumulate results based on that property. If no property name is provided, the function will default to
1165
- * accumulating results based on the 'id' property.
1166
- * @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
1167
- */
1168
- levelIterative(node: N | null, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1169
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1170
- node = node || this.root;
1171
- if (!node) return [];
1172
-
1173
- this._clearResults();
1174
- const queue: N[] = [node];
1175
-
1176
- while (queue.length > 0) {
1177
- const cur = queue.shift();
1178
- if (cur) {
1179
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1180
- if (cur.left) {
1181
- queue.push(cur.left);
1182
- }
1183
- if (cur.right) {
1184
- queue.push(cur.right);
1185
- }
1186
- }
1187
- }
1188
-
1189
- return this._getResultByPropertyName(nodeOrPropertyName);
1190
- }
1191
-
1192
- /**
1193
- * Collects nodes from a binary tree by a specified property and organizes them into levels.
1194
- * @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
1195
- * @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
1196
- */
1197
- listLevels(node: N | null): BinaryTreeNodeId[][];
1198
-
1199
- /**
1200
- * Collects nodes from a binary tree by a specified property and organizes them into levels.
1201
- * @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
1202
- * @param {'id} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1203
- * @returns A 2D array of values corresponding to the specified property.
1204
- */
1205
- listLevels(node: N | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[][];
1206
-
1207
- /**
1208
- * Collects nodes from a binary tree by a specified property and organizes them into levels.
1209
- * @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
1210
- * @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1211
- * @returns A 2D array of 'val' properties from each node.
1212
- */
1213
- listLevels(node: N | null, nodeOrPropertyName?: 'val'): N['val'][][];
1214
-
1215
- /**
1216
- * Collects nodes from a binary tree by a specified property and organizes them into levels.
1217
- * @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
1218
- * @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1219
- * @returns A 2D array of binary tree nodes.
1220
- */
1221
- listLevels(node: N | null, nodeOrPropertyName?: 'node'): N[][];
1222
-
1223
- /**
1224
- * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
1225
- * @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
1226
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: 'id', 'val', or 'node'. If not provided, it defaults to 'id'.
1227
- * @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
1228
- */
1229
- listLevels(node: N | null, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperty<N>[][] {
1230
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1231
- node = node || this.root;
1232
- if (!node) return [];
1233
-
1234
- const levelsNodes: AbstractBinaryTreeNodeProperty<N>[][] = [];
1235
-
1236
- const collectByProperty = (node: N, level: number) => {
1237
- switch (nodeOrPropertyName) {
1238
- case 'id':
1239
- levelsNodes[level].push(node.id);
1240
- break;
1241
- case 'val':
1242
- levelsNodes[level].push(node.val);
1243
- break;
1244
- case 'node':
1245
- levelsNodes[level].push(node);
1246
- break;
1247
- default:
1248
- levelsNodes[level].push(node.id);
1249
- break;
1250
- }
1251
- };
1252
-
1253
- if (this.loopType === LoopType.RECURSIVE) {
1254
- const _recursive = (node: N, level: number) => {
1255
- if (!levelsNodes[level]) levelsNodes[level] = [];
1256
- collectByProperty(node, level);
1257
- if (node.left) _recursive(node.left, level + 1);
1258
- if (node.right) _recursive(node.right, level + 1);
1259
- };
1260
-
1261
- _recursive(node, 0);
1262
- } else {
1263
- const stack: [N, number][] = [[node, 0]];
1264
-
1265
- while (stack.length > 0) {
1266
- const head = stack.pop()!;
1267
- const [node, level] = head;
1268
-
1269
- if (!levelsNodes[level]) levelsNodes[level] = [];
1270
- collectByProperty(node, level);
1271
- if (node.right) stack.push([node.right, level + 1]);
1272
- if (node.left) stack.push([node.left, level + 1]);
1273
- }
1274
- }
1275
-
1276
- return levelsNodes;
1277
- }
1278
-
1279
- /**
1280
- * The function returns the predecessor of a given node in a binary tree.
1281
- * @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
1282
- * @returns the predecessor of the given node in a binary tree.
1283
- */
1284
- getPredecessor(node: N): N {
1285
- if (node.left) {
1286
- let predecessor: N | null | undefined = node.left;
1287
- while (!predecessor || (predecessor.right && predecessor.right !== node)) {
1288
- if (predecessor) {
1289
- predecessor = predecessor.right;
1290
- }
1291
- }
1292
- return predecessor;
1293
- } else {
1294
- return node;
1295
- }
1296
- }
1297
-
1298
- /**
1299
- * Time complexity is O(n)
1300
- * Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
1301
- */
1302
-
1303
- /**
1304
- * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
1305
- * @returns An array of binary tree node IDs.
1306
- */
1307
- morris(): BinaryTreeNodeId[];
1308
-
1309
- /**
1310
- * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates properties of each node based on the specified property name.
1311
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1312
- * @param {'id'} nodeOrPropertyName - The name of the property to accumulate.
1313
- * @returns An array of values corresponding to the specified property.
1314
- */
1315
- morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
1316
-
1317
- /**
1318
- * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates the 'val' property of each node.
1319
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1320
- * @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1321
- * @returns An array of 'val' properties from each node.
1322
- */
1323
- morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
1324
-
1325
- /**
1326
- * Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates nodes themselves.
1327
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1328
- * @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
1329
- * @returns An array of binary tree nodes.
1330
- */
1331
- morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
1332
-
1333
- /**
1334
- * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
1335
- * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
1336
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'id'.
1337
- * @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
1338
- */
1339
- morris(
1340
- pattern?: 'in' | 'pre' | 'post',
1341
- nodeOrPropertyName?: NodeOrPropertyName
1342
- ): AbstractBinaryTreeNodeProperties<N> {
1343
- if (this.root === null) return [];
1344
-
1345
- pattern = pattern || 'in';
1346
- nodeOrPropertyName = nodeOrPropertyName || 'id';
1347
-
1348
- this._clearResults();
1349
-
1350
- let cur: N | null | undefined = this.root;
1351
- const _reverseEdge = (node: N | null | undefined) => {
1352
- let pre: N | null | undefined = null;
1353
- let next: N | null | undefined = null;
1354
- while (node) {
1355
- next = node.right;
1356
- node.right = pre;
1357
- pre = node;
1358
- node = next;
1359
- }
1360
- return pre;
1361
- };
1362
- const _printEdge = (node: N | null) => {
1363
- const tail: N | null | undefined = _reverseEdge(node);
1364
- let cur: N | null | undefined = tail;
1365
- while (cur) {
1366
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1367
- cur = cur.right;
1368
- }
1369
- _reverseEdge(tail);
1370
- };
1371
- switch (pattern) {
1372
- case 'in':
1373
- while (cur) {
1374
- if (cur.left) {
1375
- const predecessor = this.getPredecessor(cur);
1376
- if (!predecessor.right) {
1377
- predecessor.right = cur;
1378
- cur = cur.left;
1379
- continue;
1380
- } else {
1381
- predecessor.right = null;
1382
- }
1383
- }
1384
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1385
- cur = cur.right;
1386
- }
1387
- break;
1388
- case 'pre':
1389
- while (cur) {
1390
- if (cur.left) {
1391
- const predecessor = this.getPredecessor(cur);
1392
- if (!predecessor.right) {
1393
- predecessor.right = cur;
1394
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1395
- cur = cur.left;
1396
- continue;
1397
- } else {
1398
- predecessor.right = null;
1399
- }
1400
- } else {
1401
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1402
- }
1403
- cur = cur.right;
1404
- }
1405
- break;
1406
- case 'post':
1407
- while (cur) {
1408
- if (cur.left) {
1409
- const predecessor = this.getPredecessor(cur);
1410
- if (predecessor.right === null) {
1411
- predecessor.right = cur;
1412
- cur = cur.left;
1413
- continue;
1414
- } else {
1415
- predecessor.right = null;
1416
- _printEdge(cur.left);
1417
- }
1418
- }
1419
- cur = cur.right;
1420
- }
1421
- _printEdge(this.root);
1422
- break;
1423
- }
1424
-
1425
- return this._getResultByPropertyName(nodeOrPropertyName);
1426
- }
1427
-
1428
- /**
1429
- * The function adds a new node to a binary tree if there is an available position.
1430
- * @param {N | null} newNode - The `newNode` parameter is of type `N | null`, which means it can either be a node of
1431
- * type `N` or `null`. It represents the node that you want to add to the binary tree.
1432
- * @param {N} parent - The parent parameter is of type N, which represents a node in a binary tree.
1433
- * @returns either the left or right child node of the parent node, depending on which child is available for adding
1434
- * the new node. If a new node is added, the function also updates the size of the binary tree. If neither the left nor
1435
- * right child is available, the function returns undefined. If the parent node is null, the function also returns
1436
- * undefined.
1437
- */
1438
- protected _addTo(newNode: N | null, parent: N): N | null | undefined {
1439
- if (parent) {
1440
- // When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
1441
- // In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
1442
- if (parent.left === undefined) {
1443
- parent.left = newNode;
1444
- if (newNode) {
1445
- this._setSize(this.size + 1);
1446
- }
1447
- return parent.left;
1448
- } else if (parent.right === undefined) {
1449
- parent.right = newNode;
1450
- if (newNode) {
1451
- this._setSize(this.size + 1);
1452
- }
1453
- return parent.right;
1454
- } else {
1455
- return;
1456
- }
1457
- } else {
1458
- return;
1459
- }
1460
- }
1461
-
1462
- /**
1463
- * The function sets the loop type for a protected variable.
1464
- * @param {LoopType} value - The value parameter is of type LoopType.
1465
- */
1466
- protected _setLoopType(value: LoopType) {
1467
- this._loopType = value;
1468
- }
1469
-
1470
- /**
1471
- * The function sets the value of the `_visitedId` property in a protected manner.
1472
- * @param {BinaryTreeNodeId[]} value - value is an array of BinaryTreeNodeId values.
1473
- */
1474
- protected _setVisitedId(value: BinaryTreeNodeId[]) {
1475
- this._visitedId = value;
1476
- }
1477
-
1478
- /**
1479
- * The function sets the value of the "_visitedVal" property to the given array.
1480
- * @param value - An array of type N.
1481
- */
1482
- protected _setVisitedVal(value: Array<N>) {
1483
- this._visitedVal = value;
1484
- }
1485
-
1486
- /**
1487
- * The function sets the value of the _visitedNode property.
1488
- * @param {N[]} value - N[] is an array of elements of type N.
1489
- */
1490
- protected _setVisitedNode(value: N[]) {
1491
- this._visitedNode = value;
1492
- }
1493
-
1494
- /**
1495
- * The function sets the value of the `_visitedLeftSum` property to the provided array.
1496
- * @param {number[]} value - An array of numbers that represents the visited left sum.
1497
- */
1498
- protected _setVisitedLeftSum(value: number[]) {
1499
- this._visitedLeftSum = value;
1500
- }
1501
-
1502
- /**
1503
- * The function sets the root property of an object to a given value, and if the value is not null, it also sets the
1504
- * parent property of the value to undefined.
1505
- * @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of type `N` or `null`.
1506
- */
1507
- protected _setRoot(v: N | null) {
1508
- if (v) {
1509
- v.parent = undefined;
1510
- }
1511
- this._root = v;
1512
- }
1513
-
1514
- /**
1515
- * The function sets the size of a protected variable.
1516
- * @param {number} v - number
1517
- */
1518
- protected _setSize(v: number) {
1519
- this._size = v;
1520
- }
1521
-
1522
- /**
1523
- * The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
1524
- * properties.
1525
- */
1526
- protected _clearResults() {
1527
- this._visitedId = [];
1528
- this._visitedVal = [];
1529
- this._visitedNode = [];
1530
- this._visitedLeftSum = [];
1531
- }
1532
-
1533
- /**
1534
- * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
1535
- * a result array.
1536
- * @param {N} cur - The current node being processed.
1537
- * @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
1538
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeId` or a `N`
1539
- * type. It represents the property value that we are comparing against in the switch statement.
1540
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
1541
- * specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'id'`
1542
- * or `'val'`. If it is not provided or is not equal to `'id'` or `'val'`, the
1543
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
1544
- * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
1545
- * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
1546
- * @returns a boolean value indicating whether only one matching node should be pushed into the result array.
1547
- */
1548
- protected _pushByPropertyNameStopOrNot(
1549
- cur: N,
1550
- result: (N | null | undefined)[],
1551
- nodeProperty: BinaryTreeNodeId | N,
1552
- propertyName?: BinaryTreeNodePropertyName,
1553
- onlyOne?: boolean
1554
- ) {
1555
- switch (propertyName) {
1556
- case 'id':
1557
- if (cur.id === nodeProperty) {
1558
- result.push(cur);
1559
- return !!onlyOne;
1560
- }
1561
- break;
1562
- case 'val':
1563
- if (cur.val === nodeProperty) {
1564
- result.push(cur);
1565
- return !!onlyOne;
1566
- }
1567
- break;
1568
- default:
1569
- if (cur.id === nodeProperty) {
1570
- result.push(cur);
1571
- return !!onlyOne;
1572
- }
1573
- break;
1574
- }
1575
- }
1576
-
1577
- /**
1578
- * The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
1579
- * @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
1580
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1581
- * can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
1582
- * specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
1583
- */
1584
- protected _accumulatedByPropertyName(node: N, nodeOrPropertyName?: NodeOrPropertyName) {
1585
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1586
-
1587
- switch (nodeOrPropertyName) {
1588
- case 'id':
1589
- this._visitedId.push(node.id);
1590
- break;
1591
- case 'val':
1592
- this._visitedVal.push(node.val);
1593
- break;
1594
- case 'node':
1595
- this._visitedNode.push(node);
1596
- break;
1597
- default:
1598
- this._visitedId.push(node.id);
1599
- break;
1600
- }
1601
- }
1602
-
1603
- /**
1604
- * The time complexity of Morris traversal is O(n), it's may slower than others
1605
- * The space complexity Morris traversal is O(1) because no using stack
1606
- */
1607
-
1608
- /**
1609
- * The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
1610
- * name.
1611
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1612
- * can accept either a `NodeOrPropertyName` type or be undefined.
1613
- * @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
1614
- */
1615
- protected _getResultByPropertyName(nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1616
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1617
-
1618
- switch (nodeOrPropertyName) {
1619
- case 'id':
1620
- return this._visitedId;
1621
- case 'val':
1622
- return this._visitedVal;
1623
- case 'node':
1624
- return this._visitedNode;
1625
- default:
1626
- return this._visitedId;
1627
- }
1628
- }
1629
-
1630
- // --- end additional methods ---
1631
- }