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,700 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import type {BinaryTreeNodeId, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
9
- import {BinaryTreeDeletedResult, CP, DFSOrderPattern, FamilyPosition, LoopType} from '../../types';
10
- import {ITreeMultiset, ITreeMultisetNode} from '../../interfaces';
11
- import {AVLTree, AVLTreeNode} from './avl-tree';
12
-
13
- export class TreeMultisetNode<T = any, NEIGHBOR extends TreeMultisetNode<T, NEIGHBOR> = TreeMultisetNodeNested<T>>
14
- extends AVLTreeNode<T, NEIGHBOR>
15
- implements ITreeMultisetNode<T, NEIGHBOR>
16
- {
17
- /**
18
- * The constructor function initializes a BinaryTreeNode object with an id, value, and count.
19
- * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
20
- * of the binary tree node.
21
- * @param {T} [val] - The `val` parameter is an optional parameter of type `T`. It represents the value of the binary
22
- * tree node. If no value is provided, it will be `undefined`.
23
- * @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
24
- * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
25
- * parameter when creating a new instance of the `BinaryTreeNode` class,
26
- */
27
- constructor(id: BinaryTreeNodeId, val?: T, count = 1) {
28
- super(id, val);
29
- this._count = count;
30
- }
31
-
32
- private _count: number;
33
-
34
- get count(): number {
35
- return this._count;
36
- }
37
-
38
- set count(v: number) {
39
- this._count = v;
40
- }
41
- }
42
-
43
- /**
44
- * The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
45
- */
46
- export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode>
47
- extends AVLTree<N>
48
- implements ITreeMultiset<N>
49
- {
50
- /**
51
- * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
52
- * merge duplicated values.
53
- * @param {TreeMultisetOptions} [options] - An optional object that contains additional configuration options for the
54
- * TreeMultiset.
55
- */
56
- constructor(options?: TreeMultisetOptions) {
57
- super({...options});
58
- }
59
-
60
- private _count = 0;
61
-
62
- get count(): number {
63
- return this._count;
64
- }
65
-
66
- /**
67
- * The function creates a new BSTNode with the given id, value, and count.
68
- * @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
69
- * distinguish one node from another in the tree.
70
- * @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
71
- * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
72
- * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
73
- * @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
74
- */
75
- override createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N {
76
- return new TreeMultisetNode(id, val, count) as N;
77
- }
78
-
79
- /**
80
- * The function swaps the location of two nodes in a tree data structure.
81
- * @param {N} srcNode - The source node that we want to swap with the destination node.
82
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
83
- * be swapped with.
84
- * @returns the `destNode` after swapping its values with the `srcNode`.
85
- */
86
- override swapLocation(srcNode: N, destNode: N): N {
87
- const {id, val, count, height} = destNode;
88
- const tempNode = this.createNode(id, val, count);
89
- if (tempNode) {
90
- tempNode.height = height;
91
-
92
- destNode.id = srcNode.id;
93
- destNode.val = srcNode.val;
94
- destNode.count = srcNode.count;
95
- destNode.height = srcNode.height;
96
-
97
- srcNode.id = tempNode.id;
98
- srcNode.val = tempNode.val;
99
- srcNode.count = tempNode.count;
100
- srcNode.height = tempNode.height;
101
- }
102
-
103
- return destNode;
104
- }
105
-
106
- /**
107
- * The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
108
- * necessary.
109
- * @param {BinaryTreeNodeId | N} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId` or a `N` (which
110
- * represents a `BinaryTreeNode`).
111
- * @param [val] - The `val` parameter represents the value to be added to the binary tree node.
112
- * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
113
- * value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
114
- * @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
115
- */
116
- override add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val'], count?: number): N | null | undefined {
117
- count = count ?? 1;
118
- let inserted: N | null | undefined = undefined,
119
- newNode: N | null;
120
- if (idOrNode instanceof TreeMultisetNode) {
121
- newNode = this.createNode(idOrNode.id, idOrNode.val, idOrNode.count);
122
- } else if (idOrNode === null) {
123
- newNode = null;
124
- } else {
125
- newNode = this.createNode(idOrNode, val, count);
126
- }
127
- if (!this.root) {
128
- this._setRoot(newNode);
129
- this._setSize(this.size + 1);
130
- newNode && this._setCount(this.count + newNode.count);
131
- inserted = this.root;
132
- } else {
133
- let cur = this.root;
134
- let traversing = true;
135
- while (traversing) {
136
- if (cur) {
137
- if (newNode) {
138
- if (this._compare(cur.id, newNode.id) === CP.eq) {
139
- cur.val = newNode.val;
140
- cur.count += newNode.count;
141
- this._setCount(this.count + newNode.count);
142
- traversing = false;
143
- inserted = cur;
144
- } else if (this._compare(cur.id, newNode.id) === CP.gt) {
145
- // Traverse left of the node
146
- if (cur.left === undefined) {
147
- //Add to the left of the current node
148
- cur.left = newNode;
149
- this._setSize(this.size + 1);
150
- this._setCount(this.count + newNode.count);
151
-
152
- traversing = false;
153
- inserted = cur.left;
154
- } else {
155
- //Traverse the left of the current node
156
- if (cur.left) cur = cur.left;
157
- }
158
- } else if (this._compare(cur.id, newNode.id) === CP.lt) {
159
- // Traverse right of the node
160
- if (cur.right === undefined) {
161
- //Add to the right of the current node
162
- cur.right = newNode;
163
- this._setSize(this.size + 1);
164
- this._setCount(this.count + newNode.count);
165
-
166
- traversing = false;
167
- inserted = cur.right;
168
- } else {
169
- //Traverse the left of the current node
170
- if (cur.right) cur = cur.right;
171
- }
172
- }
173
- } else {
174
- // TODO may need to support null inserted
175
- }
176
- } else {
177
- traversing = false;
178
- }
179
- }
180
- }
181
- if (inserted) this._balancePath(inserted);
182
- return inserted;
183
- }
184
-
185
- /**
186
- * The function adds a new node to a binary tree if there is an available slot on the left or right side of the parent
187
- * node.
188
- * @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to the tree. It can
189
- * be either a node object (`N`) or `null`.
190
- * @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
191
- * child.
192
- * @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
193
- */
194
- override _addTo(newNode: N | null, parent: N): N | null | undefined {
195
- if (parent) {
196
- if (parent.left === undefined) {
197
- parent.left = newNode;
198
- if (newNode !== null) {
199
- this._setSize(this.size + 1);
200
- this._setCount(this.count + newNode.count);
201
- }
202
-
203
- return parent.left;
204
- } else if (parent.right === undefined) {
205
- parent.right = newNode;
206
- if (newNode !== null) {
207
- this._setSize(this.size + 1);
208
- this._setCount(this.count + newNode.count);
209
- }
210
- return parent.right;
211
- } else {
212
- return;
213
- }
214
- } else {
215
- return;
216
- }
217
- }
218
-
219
- /**
220
- * The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
221
- * the inserted nodes.
222
- * @param {(BinaryTreeNodeId | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeId or BinaryTreeNode
223
- * objects, or null values.
224
- * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
225
- * the nodes being added. It is used when adding nodes using the `idOrNode` and `data` arguments in the `this.add()`
226
- * method. If provided, the `data` array should
227
- * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
228
- */
229
- override addMany(
230
- idsOrNodes: (BinaryTreeNodeId | null)[] | (N | null)[],
231
- data?: N['val'][]
232
- ): (N | null | undefined)[] {
233
- const inserted: (N | null | undefined)[] = [];
234
-
235
- for (let i = 0; i < idsOrNodes.length; i++) {
236
- const idOrNode = idsOrNodes[i];
237
-
238
- if (idOrNode instanceof TreeMultisetNode) {
239
- inserted.push(this.add(idOrNode.id, idOrNode.val, idOrNode.count));
240
- continue;
241
- }
242
-
243
- if (idOrNode === null) {
244
- inserted.push(this.add(NaN, null, 0));
245
- continue;
246
- }
247
-
248
- inserted.push(this.add(idOrNode, data?.[i], 1));
249
- }
250
- return inserted;
251
- }
252
-
253
- /**
254
- * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
255
- * constructs a balanced binary search tree using either a recursive or iterative approach.
256
- * @returns The function `perfectlyBalance()` returns a boolean value.
257
- */
258
- override perfectlyBalance(): boolean {
259
- const sorted = this.DFS('in', 'node'),
260
- n = sorted.length;
261
- if (sorted.length < 1) return false;
262
-
263
- this.clear();
264
-
265
- if (this.loopType === LoopType.RECURSIVE) {
266
- const buildBalanceBST = (l: number, r: number) => {
267
- if (l > r) return;
268
- const m = l + Math.floor((r - l) / 2);
269
- const midNode = sorted[m];
270
- this.add(midNode.id, midNode.val, midNode.count);
271
- buildBalanceBST(l, m - 1);
272
- buildBalanceBST(m + 1, r);
273
- };
274
-
275
- buildBalanceBST(0, n - 1);
276
- return true;
277
- } else {
278
- const stack: [[number, number]] = [[0, n - 1]];
279
- while (stack.length > 0) {
280
- const popped = stack.pop();
281
- if (popped) {
282
- const [l, r] = popped;
283
- if (l <= r) {
284
- const m = l + Math.floor((r - l) / 2);
285
- const midNode = sorted[m];
286
- this.add(midNode.id, midNode.val, midNode.count);
287
- stack.push([m + 1, r]);
288
- stack.push([l, m - 1]);
289
- }
290
- }
291
- }
292
- return true;
293
- }
294
- }
295
-
296
- /**
297
- * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
298
- * node that needs to be balanced.
299
- * @param {N | BinaryTreeNodeId | null} nodeOrId - The `nodeOrId` parameter can be one of the following:
300
- * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
301
- * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
302
- * not be taken into account when removing it. If `ignoreCount` is set to `false
303
- * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
304
- */
305
- override remove(nodeOrId: N | BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[] {
306
- const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
307
- if (!this.root) return bstDeletedResult;
308
-
309
- const curr: N | null = this.get(nodeOrId);
310
- if (!curr) return bstDeletedResult;
311
-
312
- const parent: N | null = curr?.parent ? curr.parent : null;
313
- let needBalanced: N | null = null,
314
- orgCurrent = curr;
315
-
316
- if (curr.count > 1 && !ignoreCount) {
317
- curr.count--;
318
- this._setCount(this.count - 1);
319
- } else {
320
- if (!curr.left) {
321
- if (!parent) {
322
- if (curr.right !== undefined) this._setRoot(curr.right);
323
- } else {
324
- const {familyPosition: fp} = curr;
325
- if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
326
- parent.left = curr.right;
327
- } else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
328
- parent.right = curr.right;
329
- }
330
- needBalanced = parent;
331
- }
332
- } else {
333
- const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
334
- if (leftSubTreeRightMost) {
335
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
336
- orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
337
- if (parentOfLeftSubTreeMax) {
338
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
339
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
340
- } else {
341
- parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
342
- }
343
- needBalanced = parentOfLeftSubTreeMax;
344
- }
345
- }
346
- }
347
- this._setSize(this.size - 1);
348
- // TODO How to handle when the count of target node is lesser than current node's count
349
- this._setCount(this.count - orgCurrent.count);
350
- }
351
-
352
- bstDeletedResult.push({deleted: orgCurrent, needBalanced});
353
-
354
- if (needBalanced) {
355
- this._balancePath(needBalanced);
356
- }
357
-
358
- return bstDeletedResult;
359
- }
360
-
361
- /**
362
- * The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
363
- * recursive or iterative traversal.
364
- * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
365
- * binary tree.
366
- * @returns The function `getSubTreeCount` returns an array `[number, number]`.
367
- */
368
- getSubTreeCount(subTreeRoot: N | null | undefined) {
369
- const res: [number, number] = [0, 0];
370
- if (!subTreeRoot) return res;
371
-
372
- if (this.loopType === LoopType.RECURSIVE) {
373
- const _traverse = (cur: N) => {
374
- res[0]++;
375
- res[1] += cur.count;
376
- cur.left && _traverse(cur.left);
377
- cur.right && _traverse(cur.right);
378
- };
379
-
380
- _traverse(subTreeRoot);
381
- return res;
382
- } else {
383
- const stack: N[] = [subTreeRoot];
384
-
385
- while (stack.length > 0) {
386
- const cur = stack.pop()!;
387
- res[0]++;
388
- res[1] += cur.count;
389
- cur.right && stack.push(cur.right);
390
- cur.left && stack.push(cur.left);
391
- }
392
-
393
- return res;
394
- }
395
- }
396
-
397
- /**
398
- * The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
399
- * recursively or iteratively.
400
- * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
401
- * in a binary tree. It can be either a `BinaryTreeNodeId` (a unique identifier for a node in the binary tree) or
402
- * `null` if the subtree is empty.
403
- * @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
404
- */
405
- subTreeSumCount(subTreeRoot: N | BinaryTreeNodeId | null): number {
406
- if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
407
-
408
- if (!subTreeRoot) return 0;
409
-
410
- let sum = 0;
411
-
412
- if (this.loopType === LoopType.RECURSIVE) {
413
- const _traverse = (cur: N): void => {
414
- sum += cur.count;
415
- cur.left && _traverse(cur.left);
416
- cur.right && _traverse(cur.right);
417
- };
418
-
419
- _traverse(subTreeRoot);
420
- } else {
421
- const stack: N[] = [subTreeRoot];
422
-
423
- while (stack.length > 0) {
424
- const cur = stack.pop()!;
425
- sum += cur.count;
426
- cur.right && stack.push(cur.right);
427
- cur.left && stack.push(cur.left);
428
- }
429
- }
430
-
431
- return sum;
432
- }
433
-
434
- /**
435
- * The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
436
- * the `count` property of each node.
437
- * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
438
- * in a binary tree. It can be either a `BinaryTreeNodeId` (a unique identifier for a node in the binary tree), a
439
- * `BinaryTreeNode` object, or `null` if the subtree is empty.
440
- * @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
441
- * in the subtree should be increased or decreased.
442
- * @returns a boolean value.
443
- */
444
- subTreeAddCount(subTreeRoot: N | BinaryTreeNodeId | null, delta: number): boolean {
445
- if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
446
-
447
- if (!subTreeRoot) return false;
448
-
449
- const _addByProperty = (cur: N) => {
450
- cur.count += delta;
451
- this._setCount(this.count + delta);
452
- };
453
-
454
- if (this.loopType === LoopType.RECURSIVE) {
455
- const _traverse = (cur: N) => {
456
- _addByProperty(cur);
457
- cur.left && _traverse(cur.left);
458
- cur.right && _traverse(cur.right);
459
- };
460
-
461
- _traverse(subTreeRoot);
462
- } else {
463
- const stack: N[] = [subTreeRoot];
464
-
465
- while (stack.length > 0) {
466
- const cur = stack.pop()!;
467
-
468
- _addByProperty(cur);
469
- cur.right && stack.push(cur.right);
470
- cur.left && stack.push(cur.left);
471
- }
472
- }
473
- return true;
474
- }
475
-
476
- /**
477
- * The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
478
- * using a queue.
479
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
480
- * `N`. It represents the property of the nodes that you want to search for.
481
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
482
- * return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
483
- * to `true`, the function will return only one node. If `onlyOne`
484
- * @returns an array of nodes that match the given nodeProperty.
485
- */
486
- getNodesByCount(nodeProperty: BinaryTreeNodeId | N, onlyOne?: boolean): N[] {
487
- if (!this.root) return [];
488
- const result: N[] = [];
489
-
490
- if (this.loopType === LoopType.RECURSIVE) {
491
- const _traverse = (cur: N) => {
492
- if (cur.count === nodeProperty) {
493
- result.push(cur);
494
- if (onlyOne) return;
495
- }
496
-
497
- if (!cur.left && !cur.right) return;
498
- cur.left && _traverse(cur.left);
499
- cur.right && _traverse(cur.right);
500
- };
501
-
502
- _traverse(this.root);
503
- } else {
504
- const queue: N[] = [this.root];
505
- while (queue.length > 0) {
506
- const cur = queue.shift();
507
- if (cur) {
508
- if (cur.count === nodeProperty) {
509
- result.push(cur);
510
- if (onlyOne) return result;
511
- }
512
-
513
- cur.left && queue.push(cur.left);
514
- cur.right && queue.push(cur.right);
515
- }
516
- }
517
- }
518
-
519
- return result;
520
- }
521
-
522
- /**
523
- * The BFSCount function returns an array of counts from a breadth-first search of nodes.
524
- * @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
525
- * BFS traversal.
526
- */
527
- BFSCount(): number[] {
528
- const nodes = super.BFS('node');
529
- return nodes.map(node => node.count);
530
- }
531
-
532
- /**
533
- * The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
534
- * count property of each node at that level.
535
- * @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
536
- * the class `N` or `null`.
537
- * @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
538
- * array represents the count property of a node in that level.
539
- */
540
- listLevelsCount(node: N | null): number[][] {
541
- const levels = super.listLevels(node, 'node');
542
- return levels.map(level => level.map(node => node.count));
543
- }
544
-
545
- /**
546
- * The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
547
- * pattern.
548
- * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
549
- * traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
550
- * @returns The function `morrisCount` returns an array of numbers.
551
- */
552
- morrisCount(pattern?: 'in' | 'pre' | 'post'): number[] {
553
- pattern = pattern || 'in';
554
- const nodes = super.morris(pattern, 'node');
555
- return nodes.map(node => node.count);
556
- }
557
-
558
- /**
559
- * The function DFSIterativeCount performs an iterative depth-first search and returns an array of node counts based on
560
- * the specified traversal pattern.
561
- * @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
562
- * the Depth-First Search (DFS) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
563
- * @returns The DFSIterativeCount function returns an array of numbers, which represents the count property of each node
564
- * in the DFS traversal.
565
- */
566
- DFSIterativeCount(pattern?: 'in' | 'pre' | 'post'): number[] {
567
- pattern = pattern ?? 'in';
568
- const nodes = super.DFSIterative(pattern, 'node');
569
- return nodes.map(node => node.count);
570
- }
571
-
572
- /**
573
- * The DFSCount function returns an array of counts for each node in a depth-first search traversal.
574
- * @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
575
- * the Depth-First Search (DFS) algorithm should traverse the nodes. It can have one of the following values:
576
- * @returns The DFSCount function returns an array of numbers, specifically the count property of each node in the DFS
577
- * traversal.
578
- */
579
- DFSCount(pattern?: DFSOrderPattern): number[] {
580
- pattern = pattern ?? 'in';
581
- const nodes = super.DFS(pattern, 'node');
582
- return nodes.map(node => node.count);
583
- }
584
-
585
- /**
586
- * The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
587
- * value than a given node.
588
- * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
589
- * @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
590
- */
591
- lesserSumCount(beginNode: N | BinaryTreeNodeId | null): number {
592
- if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'id');
593
- if (!beginNode) return 0;
594
- if (!this.root) return 0;
595
- const id = beginNode.id;
596
-
597
- let sum = 0;
598
-
599
- if (this.loopType === LoopType.RECURSIVE) {
600
- const _traverse = (cur: N): void => {
601
- const compared = this._compare(cur.id, id);
602
- if (compared === CP.eq) {
603
- if (cur.right) sum += this.subTreeSumCount(cur.right);
604
- return;
605
- } else if (compared === CP.lt) {
606
- if (cur.left) sum += this.subTreeSumCount(cur.left);
607
- sum += cur.count;
608
- if (cur.right) _traverse(cur.right);
609
- else return;
610
- } else {
611
- if (cur.left) _traverse(cur.left);
612
- else return;
613
- }
614
- };
615
-
616
- _traverse(this.root);
617
- } else {
618
- const queue: N[] = [this.root];
619
- while (queue.length > 0) {
620
- const cur = queue.shift();
621
- if (cur) {
622
- const compared = this._compare(cur.id, id);
623
- if (compared === CP.eq) {
624
- if (cur.right) sum += this.subTreeSumCount(cur.right);
625
- return sum;
626
- } else if (compared === CP.lt) {
627
- // todo maybe a bug
628
- if (cur.left) sum += this.subTreeSumCount(cur.left);
629
- sum += cur.count;
630
- if (cur.right) queue.push(cur.right);
631
- else return sum;
632
- } else {
633
- if (cur.left) queue.push(cur.left);
634
- else return sum;
635
- }
636
- }
637
- }
638
- }
639
-
640
- return sum;
641
- }
642
-
643
- /**
644
- * The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
645
- * greater than a given ID by a specified delta value.
646
- * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be one of the following:
647
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
648
- * of each node should be increased.
649
- * @returns a boolean value.
650
- */
651
- allGreaterNodesAddCount(node: N | BinaryTreeNodeId | null, delta: number): boolean {
652
- if (typeof node === 'number') node = this.get(node, 'id');
653
- if (!node) return false;
654
- const id = node.id;
655
- if (!this.root) return false;
656
-
657
- if (this.loopType === LoopType.RECURSIVE) {
658
- const _traverse = (cur: N) => {
659
- const compared = this._compare(cur.id, id);
660
- if (compared === CP.gt) cur.count += delta;
661
-
662
- if (!cur.left && !cur.right) return;
663
- if (cur.left && this._compare(cur.left.id, id) === CP.gt) _traverse(cur.left);
664
- if (cur.right && this._compare(cur.right.id, id) === CP.gt) _traverse(cur.right);
665
- };
666
-
667
- _traverse(this.root);
668
- return true;
669
- } else {
670
- const queue: N[] = [this.root];
671
- while (queue.length > 0) {
672
- const cur = queue.shift();
673
- if (cur) {
674
- const compared = this._compare(cur.id, id);
675
- if (compared === CP.gt) cur.count += delta;
676
-
677
- if (cur.left && this._compare(cur.left.id, id) === CP.gt) queue.push(cur.left);
678
- if (cur.right && this._compare(cur.right.id, id) === CP.gt) queue.push(cur.right);
679
- }
680
- }
681
- return true;
682
- }
683
- }
684
-
685
- /**
686
- * The clear() function clears the data and sets the count to 0.
687
- */
688
- clear() {
689
- super.clear();
690
- this._setCount(0);
691
- }
692
-
693
- /**
694
- * The function "_setCount" is used to set the value of the "_count" property.
695
- * @param {number} v - number
696
- */
697
- protected _setCount(v: number) {
698
- this._count = v;
699
- }
700
- }
@@ -1 +0,0 @@
1
- export class TwoThreeTree {}