min-heap-typed 1.37.9 → 1.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (334) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +100 -0
  2. package/dist/data-structures/binary-tree/avl-tree.js +341 -0
  3. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +144 -0
  4. package/dist/data-structures/binary-tree/binary-indexed-tree.js +261 -0
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +409 -0
  6. package/dist/data-structures/binary-tree/binary-tree.js +1065 -0
  7. package/dist/data-structures/binary-tree/bst.d.ts +167 -0
  8. package/dist/data-structures/binary-tree/bst.js +512 -0
  9. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  10. package/dist/data-structures/binary-tree/index.js +23 -0
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
  12. package/dist/data-structures/binary-tree/rb-tree.js +27 -0
  13. package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
  14. package/dist/data-structures/binary-tree/segment-tree.js +228 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.d.ts +122 -0
  16. package/dist/data-structures/binary-tree/tree-multiset.js +351 -0
  17. package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
  18. package/dist/data-structures/graph/abstract-graph.js +923 -0
  19. package/dist/data-structures/graph/directed-graph.d.ts +200 -0
  20. package/dist/data-structures/graph/directed-graph.js +422 -0
  21. package/dist/data-structures/graph/index.d.ts +4 -0
  22. package/dist/data-structures/graph/index.js +20 -0
  23. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  24. package/dist/data-structures/graph/map-graph.js +111 -0
  25. package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
  26. package/dist/data-structures/graph/undirected-graph.js +252 -0
  27. package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
  28. package/dist/data-structures/hash/coordinate-map.js +65 -0
  29. package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
  30. package/dist/data-structures/hash/coordinate-set.js +55 -0
  31. package/dist/data-structures/hash/hash-map.d.ts +56 -0
  32. package/dist/data-structures/hash/hash-map.js +171 -0
  33. package/dist/data-structures/hash/hash-table.d.ts +106 -0
  34. package/dist/data-structures/hash/hash-table.js +245 -0
  35. package/dist/data-structures/hash/index.d.ts +6 -0
  36. package/dist/data-structures/hash/index.js +22 -0
  37. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  38. package/dist/data-structures/hash/tree-map.js +6 -0
  39. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  40. package/dist/data-structures/hash/tree-set.js +6 -0
  41. package/dist/data-structures/heap/heap.d.ts +224 -0
  42. package/dist/data-structures/heap/heap.js +497 -0
  43. package/dist/data-structures/heap/index.d.ts +3 -0
  44. package/dist/data-structures/heap/index.js +19 -0
  45. package/dist/data-structures/heap/max-heap.d.ts +12 -0
  46. package/dist/data-structures/heap/max-heap.js +24 -0
  47. package/dist/data-structures/heap/min-heap.d.ts +12 -0
  48. package/dist/data-structures/heap/min-heap.js +24 -0
  49. package/dist/data-structures/index.d.ts +11 -0
  50. package/dist/data-structures/index.js +27 -0
  51. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
  52. package/dist/data-structures/linked-list/doubly-linked-list.js +583 -0
  53. package/dist/data-structures/linked-list/index.d.ts +3 -0
  54. package/dist/data-structures/linked-list/index.js +19 -0
  55. package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
  56. package/dist/data-structures/linked-list/singly-linked-list.js +448 -0
  57. package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
  58. package/dist/data-structures/linked-list/skip-linked-list.js +142 -0
  59. package/dist/data-structures/matrix/index.d.ts +4 -0
  60. package/dist/data-structures/matrix/index.js +20 -0
  61. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  62. package/dist/data-structures/matrix/matrix.js +28 -0
  63. package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
  64. package/dist/data-structures/matrix/matrix2d.js +203 -0
  65. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  66. package/dist/data-structures/matrix/navigator.js +106 -0
  67. package/dist/data-structures/matrix/vector2d.d.ts +201 -0
  68. package/dist/data-structures/matrix/vector2d.js +291 -0
  69. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  70. package/dist/data-structures/priority-queue/index.js +19 -0
  71. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -0
  72. package/dist/data-structures/priority-queue/max-priority-queue.js +24 -0
  73. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -0
  74. package/dist/data-structures/priority-queue/min-priority-queue.js +24 -0
  75. package/dist/data-structures/priority-queue/priority-queue.d.ts +12 -0
  76. package/dist/data-structures/priority-queue/priority-queue.js +17 -0
  77. package/dist/data-structures/queue/deque.d.ts +165 -0
  78. package/dist/data-structures/queue/deque.js +276 -0
  79. package/dist/data-structures/queue/index.d.ts +2 -0
  80. package/dist/data-structures/queue/index.js +18 -0
  81. package/dist/data-structures/queue/queue.d.ts +107 -0
  82. package/dist/data-structures/queue/queue.js +170 -0
  83. package/dist/data-structures/stack/index.d.ts +1 -0
  84. package/dist/data-structures/stack/index.js +17 -0
  85. package/dist/data-structures/stack/stack.d.ts +63 -0
  86. package/dist/data-structures/stack/stack.js +91 -0
  87. package/dist/data-structures/tree/index.d.ts +1 -0
  88. package/dist/data-structures/tree/index.js +17 -0
  89. package/dist/data-structures/tree/tree.d.ts +14 -0
  90. package/dist/data-structures/tree/tree.js +58 -0
  91. package/dist/data-structures/trie/index.d.ts +1 -0
  92. package/dist/data-structures/trie/index.js +17 -0
  93. package/dist/data-structures/trie/trie.d.ts +84 -0
  94. package/dist/data-structures/trie/trie.js +268 -0
  95. package/dist/index.d.ts +3 -3
  96. package/dist/index.js +3 -3
  97. package/dist/interfaces/binary-tree.d.ts +7 -0
  98. package/dist/interfaces/binary-tree.js +2 -0
  99. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  100. package/dist/interfaces/doubly-linked-list.js +2 -0
  101. package/dist/interfaces/graph.d.ts +5 -0
  102. package/dist/interfaces/graph.js +2 -0
  103. package/dist/interfaces/heap.d.ts +1 -0
  104. package/dist/interfaces/heap.js +2 -0
  105. package/dist/interfaces/index.d.ts +8 -0
  106. package/dist/interfaces/index.js +24 -0
  107. package/dist/interfaces/navigator.d.ts +1 -0
  108. package/dist/interfaces/navigator.js +2 -0
  109. package/dist/interfaces/priority-queue.d.ts +1 -0
  110. package/dist/interfaces/priority-queue.js +2 -0
  111. package/dist/interfaces/segment-tree.d.ts +1 -0
  112. package/dist/interfaces/segment-tree.js +2 -0
  113. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  114. package/dist/interfaces/singly-linked-list.js +2 -0
  115. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
  116. package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
  117. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  118. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
  119. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +31 -0
  120. package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
  121. package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
  122. package/dist/types/data-structures/binary-tree/bst.js +2 -0
  123. package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
  124. package/dist/types/data-structures/binary-tree/index.js +22 -0
  125. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +8 -0
  126. package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
  127. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  128. package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
  129. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
  130. package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
  131. package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
  132. package/dist/types/data-structures/graph/abstract-graph.js +2 -0
  133. package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
  134. package/dist/types/data-structures/graph/directed-graph.js +9 -0
  135. package/dist/types/data-structures/graph/index.d.ts +3 -0
  136. package/dist/types/data-structures/graph/index.js +19 -0
  137. package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
  138. package/dist/types/data-structures/graph/map-graph.js +2 -0
  139. package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
  140. package/dist/types/data-structures/graph/undirected-graph.js +2 -0
  141. package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
  142. package/dist/types/data-structures/hash/coordinate-map.js +2 -0
  143. package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
  144. package/dist/types/data-structures/hash/coordinate-set.js +2 -0
  145. package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
  146. package/dist/types/data-structures/hash/hash-map.js +2 -0
  147. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  148. package/dist/types/data-structures/hash/hash-table.js +2 -0
  149. package/dist/types/data-structures/hash/index.d.ts +1 -0
  150. package/dist/types/data-structures/hash/index.js +2 -0
  151. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  152. package/dist/types/data-structures/hash/tree-map.js +2 -0
  153. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  154. package/dist/types/data-structures/hash/tree-set.js +2 -0
  155. package/dist/types/data-structures/heap/heap.d.ts +1 -0
  156. package/dist/types/data-structures/heap/heap.js +2 -0
  157. package/dist/types/data-structures/heap/index.d.ts +1 -0
  158. package/dist/types/data-structures/heap/index.js +17 -0
  159. package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
  160. package/dist/types/data-structures/heap/max-heap.js +2 -0
  161. package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
  162. package/dist/types/data-structures/heap/min-heap.js +2 -0
  163. package/dist/types/data-structures/index.d.ts +11 -0
  164. package/dist/types/data-structures/index.js +27 -0
  165. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
  166. package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
  167. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  168. package/dist/types/data-structures/linked-list/index.js +18 -0
  169. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
  170. package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
  171. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  172. package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
  173. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  174. package/dist/types/data-structures/matrix/index.js +17 -0
  175. package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
  176. package/dist/types/data-structures/matrix/matrix.js +2 -0
  177. package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
  178. package/dist/types/data-structures/matrix/matrix2d.js +2 -0
  179. package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
  180. package/dist/types/data-structures/matrix/navigator.js +2 -0
  181. package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
  182. package/dist/types/data-structures/matrix/vector2d.js +2 -0
  183. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  184. package/dist/types/data-structures/priority-queue/index.js +19 -0
  185. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  186. package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
  187. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  188. package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
  189. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  190. package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
  191. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  192. package/dist/types/data-structures/queue/deque.js +2 -0
  193. package/dist/types/data-structures/queue/index.d.ts +2 -0
  194. package/dist/types/data-structures/queue/index.js +18 -0
  195. package/dist/types/data-structures/queue/queue.d.ts +1 -0
  196. package/dist/types/data-structures/queue/queue.js +2 -0
  197. package/dist/types/data-structures/stack/index.d.ts +1 -0
  198. package/dist/types/data-structures/stack/index.js +17 -0
  199. package/dist/types/data-structures/stack/stack.d.ts +1 -0
  200. package/dist/types/data-structures/stack/stack.js +2 -0
  201. package/dist/types/data-structures/tree/index.d.ts +1 -0
  202. package/dist/types/data-structures/tree/index.js +17 -0
  203. package/dist/types/data-structures/tree/tree.d.ts +1 -0
  204. package/dist/types/data-structures/tree/tree.js +2 -0
  205. package/dist/types/data-structures/trie/index.d.ts +1 -0
  206. package/dist/types/data-structures/trie/index.js +17 -0
  207. package/dist/types/data-structures/trie/trie.d.ts +1 -0
  208. package/dist/types/data-structures/trie/trie.js +2 -0
  209. package/dist/types/helpers.d.ts +9 -0
  210. package/dist/types/helpers.js +9 -0
  211. package/dist/types/index.d.ts +3 -0
  212. package/dist/types/index.js +19 -0
  213. package/dist/types/utils/index.d.ts +2 -0
  214. package/dist/types/utils/index.js +18 -0
  215. package/dist/types/utils/utils.d.ts +7 -0
  216. package/dist/types/utils/utils.js +2 -0
  217. package/dist/types/utils/validate-type.d.ts +19 -0
  218. package/dist/types/utils/validate-type.js +2 -0
  219. package/dist/utils/index.d.ts +1 -0
  220. package/dist/utils/index.js +17 -0
  221. package/dist/utils/utils.d.ts +20 -0
  222. package/dist/utils/utils.js +73 -0
  223. package/package.json +1 -4
  224. package/src/data-structures/binary-tree/avl-tree.ts +342 -0
  225. package/src/data-structures/binary-tree/binary-indexed-tree.ts +298 -0
  226. package/src/data-structures/binary-tree/binary-tree.ts +1141 -0
  227. package/src/data-structures/binary-tree/bst.ts +529 -0
  228. package/src/data-structures/binary-tree/index.ts +7 -0
  229. package/src/data-structures/binary-tree/rb-tree.ts +366 -0
  230. package/src/data-structures/binary-tree/segment-tree.ts +257 -0
  231. package/src/data-structures/binary-tree/tree-multiset.ts +375 -0
  232. package/src/data-structures/graph/abstract-graph.ts +1044 -0
  233. package/src/data-structures/graph/directed-graph.ts +470 -0
  234. package/src/data-structures/graph/index.ts +4 -0
  235. package/src/data-structures/graph/map-graph.ts +134 -0
  236. package/src/data-structures/graph/undirected-graph.ts +274 -0
  237. package/src/data-structures/hash/coordinate-map.ts +67 -0
  238. package/src/data-structures/hash/coordinate-set.ts +56 -0
  239. package/src/data-structures/hash/hash-map.ts +209 -0
  240. package/src/data-structures/hash/hash-table.ts +280 -0
  241. package/src/data-structures/hash/index.ts +6 -0
  242. package/src/data-structures/hash/tree-map.ts +1 -0
  243. package/src/data-structures/hash/tree-set.ts +1 -0
  244. package/src/data-structures/heap/heap.ts +561 -0
  245. package/src/data-structures/heap/index.ts +3 -0
  246. package/src/data-structures/heap/max-heap.ts +24 -0
  247. package/src/data-structures/heap/min-heap.ts +24 -0
  248. package/src/data-structures/index.ts +11 -0
  249. package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
  250. package/src/data-structures/linked-list/index.ts +3 -0
  251. package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
  252. package/src/data-structures/linked-list/skip-linked-list.ts +173 -0
  253. package/src/data-structures/matrix/index.ts +4 -0
  254. package/src/data-structures/matrix/matrix.ts +27 -0
  255. package/src/data-structures/matrix/matrix2d.ts +213 -0
  256. package/src/data-structures/matrix/navigator.ts +121 -0
  257. package/src/data-structures/matrix/vector2d.ts +316 -0
  258. package/src/data-structures/priority-queue/index.ts +3 -0
  259. package/src/data-structures/priority-queue/max-priority-queue.ts +23 -0
  260. package/src/data-structures/priority-queue/min-priority-queue.ts +23 -0
  261. package/src/data-structures/priority-queue/priority-queue.ts +16 -0
  262. package/src/data-structures/queue/deque.ts +297 -0
  263. package/src/data-structures/queue/index.ts +2 -0
  264. package/src/data-structures/queue/queue.ts +191 -0
  265. package/src/data-structures/stack/index.ts +1 -0
  266. package/src/data-structures/stack/stack.ts +98 -0
  267. package/src/data-structures/tree/index.ts +1 -0
  268. package/src/data-structures/tree/tree.ts +67 -0
  269. package/src/data-structures/trie/index.ts +1 -0
  270. package/src/data-structures/trie/trie.ts +286 -0
  271. package/src/index.ts +3 -3
  272. package/src/interfaces/binary-tree.ts +10 -0
  273. package/src/interfaces/doubly-linked-list.ts +1 -0
  274. package/src/interfaces/graph.ts +7 -0
  275. package/src/interfaces/heap.ts +1 -0
  276. package/src/interfaces/index.ts +8 -0
  277. package/src/interfaces/navigator.ts +1 -0
  278. package/src/interfaces/priority-queue.ts +1 -0
  279. package/src/interfaces/segment-tree.ts +1 -0
  280. package/src/interfaces/singly-linked-list.ts +1 -0
  281. package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
  282. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  283. package/src/types/data-structures/binary-tree/binary-tree.ts +35 -0
  284. package/src/types/data-structures/binary-tree/bst.ts +11 -0
  285. package/src/types/data-structures/binary-tree/index.ts +6 -0
  286. package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
  287. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  288. package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
  289. package/src/types/data-structures/graph/abstract-graph.ts +11 -0
  290. package/src/types/data-structures/graph/directed-graph.ts +8 -0
  291. package/src/types/data-structures/graph/index.ts +3 -0
  292. package/src/types/data-structures/graph/map-graph.ts +1 -0
  293. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  294. package/src/types/data-structures/hash/coordinate-map.ts +1 -0
  295. package/src/types/data-structures/hash/coordinate-set.ts +1 -0
  296. package/src/types/data-structures/hash/hash-map.ts +1 -0
  297. package/src/types/data-structures/hash/hash-table.ts +1 -0
  298. package/src/types/data-structures/hash/index.ts +1 -0
  299. package/src/types/data-structures/hash/tree-map.ts +1 -0
  300. package/src/types/data-structures/hash/tree-set.ts +1 -0
  301. package/src/types/data-structures/heap/heap.ts +1 -0
  302. package/src/types/data-structures/heap/index.ts +1 -0
  303. package/src/types/data-structures/heap/max-heap.ts +1 -0
  304. package/src/types/data-structures/heap/min-heap.ts +1 -0
  305. package/src/types/data-structures/index.ts +11 -0
  306. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
  307. package/src/types/data-structures/linked-list/index.ts +2 -0
  308. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
  309. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  310. package/src/types/data-structures/matrix/index.ts +1 -0
  311. package/src/types/data-structures/matrix/matrix.ts +1 -0
  312. package/src/types/data-structures/matrix/matrix2d.ts +1 -0
  313. package/src/types/data-structures/matrix/navigator.ts +14 -0
  314. package/src/types/data-structures/matrix/vector2d.ts +1 -0
  315. package/src/types/data-structures/priority-queue/index.ts +3 -0
  316. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  317. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  318. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  319. package/src/types/data-structures/queue/deque.ts +1 -0
  320. package/src/types/data-structures/queue/index.ts +2 -0
  321. package/src/types/data-structures/queue/queue.ts +1 -0
  322. package/src/types/data-structures/stack/index.ts +1 -0
  323. package/src/types/data-structures/stack/stack.ts +1 -0
  324. package/src/types/data-structures/tree/index.ts +1 -0
  325. package/src/types/data-structures/tree/tree.ts +1 -0
  326. package/src/types/data-structures/trie/index.ts +1 -0
  327. package/src/types/data-structures/trie/trie.ts +1 -0
  328. package/src/types/helpers.ts +13 -0
  329. package/src/types/index.ts +3 -0
  330. package/src/types/utils/index.ts +2 -0
  331. package/src/types/utils/utils.ts +6 -0
  332. package/src/types/utils/validate-type.ts +35 -0
  333. package/src/utils/index.ts +1 -0
  334. package/src/utils/utils.ts +86 -0
@@ -0,0 +1,366 @@
1
+ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types';
2
+ import {IBinaryTree} from '../../interfaces';
3
+ import {BST, BSTNode} from './bst';
4
+
5
+ export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode<
6
+ V,
7
+ FAMILY
8
+ > {
9
+ constructor(key: BinaryTreeNodeKey, val?: V) {
10
+ super(key, val);
11
+ this._color = RBColor.RED;
12
+ }
13
+
14
+ private _color: RBColor;
15
+
16
+ get color(): RBColor {
17
+ return this._color;
18
+ }
19
+
20
+ set color(value: RBColor) {
21
+ this._color = value;
22
+ }
23
+ }
24
+
25
+ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IBinaryTree<N> {
26
+ constructor(options?: RBTreeOptions) {
27
+ super(options);
28
+ }
29
+
30
+ override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
31
+ return new RBTreeNode(key, val) as N;
32
+ }
33
+
34
+ // override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
35
+ // const inserted = super.add(keyOrNode, val);
36
+ // if (inserted) this._fixInsertViolation(inserted);
37
+ // return inserted;
38
+ // }
39
+ //
40
+ // // Method for fixing insert violations in a red-black tree
41
+ // private _fixInsertViolation(node: N) {
42
+ // while (node !== this.root! && node.color === RBColor.RED && node.parent!.color === RBColor.RED) {
43
+ // const parent = node.parent!;
44
+ // const grandparent = parent.parent!;
45
+ // let uncle: N | null | undefined = null;
46
+ //
47
+ // if (parent === grandparent.left) {
48
+ // uncle = grandparent.right;
49
+ //
50
+ // // Case 1: The uncle node is red
51
+ // if (uncle && uncle.color === RBColor.RED) {
52
+ // grandparent.color = RBColor.RED;
53
+ // parent.color = RBColor.BLACK;
54
+ // uncle.color = RBColor.BLACK;
55
+ // node = grandparent;
56
+ // } else {
57
+ // // Case 2: The uncle node is black, and the current node is a right child
58
+ // if (node === parent.right) {
59
+ // this._rotateLeft(parent);
60
+ // node = parent;
61
+ // // Update parent reference
62
+ // node.parent = grandparent;
63
+ // parent.parent = node;
64
+ // }
65
+ //
66
+ // // Case 3: The uncle node is black, and the current node is a left child
67
+ // parent.color = RBColor.BLACK;
68
+ // grandparent.color = RBColor.RED;
69
+ // this._rotateRight(grandparent);
70
+ // }
71
+ // } else {
72
+ // // Symmetric case: The parent is the right child of the grandparent
73
+ // uncle = grandparent.left;
74
+ //
75
+ // // Case 1: The uncle node is red
76
+ // if (uncle && uncle.color === RBColor.RED) {
77
+ // grandparent.color = RBColor.RED;
78
+ // parent.color = RBColor.BLACK;
79
+ // uncle.color = RBColor.BLACK;
80
+ // node = grandparent;
81
+ // } else {
82
+ // // Case 2: The uncle node is black, and the current node is a left child
83
+ // if (node === parent.left) {
84
+ // this._rotateRight(parent);
85
+ // node = parent;
86
+ // // Update parent reference
87
+ // node.parent = grandparent;
88
+ // parent.parent = node;
89
+ // }
90
+ //
91
+ // // Case 3: The uncle node is black, and the current node is a right child
92
+ // parent.color = RBColor.BLACK;
93
+ // grandparent.color = RBColor.RED;
94
+ // this._rotateLeft(grandparent);
95
+ // }
96
+ // }
97
+ // }
98
+ //
99
+ // // The root node is always black
100
+ // this.root!.color = RBColor.BLACK;
101
+ // }
102
+ //
103
+ // // Left rotation operation
104
+ // private _rotateLeft(node: N) {
105
+ // const rightChild = node.right;
106
+ // node.right = rightChild!.left;
107
+ //
108
+ // if (rightChild!.left) {
109
+ // rightChild!.left.parent = node;
110
+ // }
111
+ //
112
+ // rightChild!.parent = node.parent;
113
+ //
114
+ // if (node === this.root) {
115
+ // // @ts-ignore
116
+ // this._setRoot(rightChild);
117
+ // } else if (node === node.parent!.left) {
118
+ // node.parent!.left = rightChild;
119
+ // } else {
120
+ // node.parent!.right = rightChild;
121
+ // }
122
+ //
123
+ // rightChild!.left = node;
124
+ // node.parent = rightChild;
125
+ // }
126
+ //
127
+ // // Right rotation operation
128
+ // private _rotateRight(node: N) {
129
+ // const leftChild = node.left;
130
+ // node.left = leftChild!.right;
131
+ //
132
+ // if (leftChild!.right) {
133
+ // leftChild!.right.parent = node;
134
+ // }
135
+ //
136
+ // leftChild!.parent = node.parent;
137
+ //
138
+ // if (node === this.root) {
139
+ // // @ts-ignore
140
+ // this._setRoot(leftChild);
141
+ // } else if (node === node.parent!.right) {
142
+ // node.parent!.right = leftChild;
143
+ // } else {
144
+ // node.parent!.left = leftChild;
145
+ // }
146
+ //
147
+ // leftChild!.right = node;
148
+ // node.parent = leftChild;
149
+ // }
150
+ //
151
+ // private _isNodeRed(node: N | null | undefined): boolean {
152
+ // return node ? node.color === RBColor.RED : false;
153
+ // }
154
+ //
155
+ // // Find the sibling node
156
+ // private _findSibling(node: N): N | null | undefined {
157
+ // if (!node.parent) {
158
+ // return undefined;
159
+ // }
160
+ //
161
+ // return node === node.parent.left ? node.parent.right : node.parent.left;
162
+ // }
163
+ //
164
+ // // Remove a node
165
+ // private _removeNode(node: N, replacement: N | null | undefined): void {
166
+ // if (node === this.root && !replacement) {
167
+ // // If there's only the root node and no replacement, simply delete the root node
168
+ // this._setRoot(null);
169
+ // } else if (node === this.root || this._isNodeRed(node)) {
170
+ // // If the node is the root or a red node, delete it directly
171
+ // if (node.parent!.left === node) {
172
+ // node.parent!.left = replacement;
173
+ // } else {
174
+ // node.parent!.right = replacement;
175
+ // }
176
+ //
177
+ // if (replacement) {
178
+ // replacement.parent = node.parent!;
179
+ // replacement.color = RBColor.BLACK; // Set the replacement node's color to black
180
+ // }
181
+ // } else {
182
+ // // If the node is a black node, perform removal and repair
183
+ // const sibling = this._findSibling(node);
184
+ //
185
+ // if (node.parent!.left === node) {
186
+ // node.parent!.left = replacement;
187
+ // } else {
188
+ // node.parent!.right = replacement;
189
+ // }
190
+ //
191
+ // if (replacement) {
192
+ // replacement.parent = node.parent;
193
+ // }
194
+ //
195
+ // if (!this._isNodeRed(sibling)) {
196
+ // // If the sibling node is black, perform repair
197
+ // this._fixDeleteViolation(replacement || node);
198
+ // }
199
+ // }
200
+ //
201
+ // if (node.parent) {
202
+ // node.parent = null;
203
+ // }
204
+ // node.left = null;
205
+ // node.right = null;
206
+ // }
207
+ //
208
+ // override delete(nodeOrKey: BinaryTreeNodeKey | N): BinaryTreeDeletedResult<N>[] {
209
+ // const node = this.get(nodeOrKey);
210
+ // const result: BinaryTreeDeletedResult<N>[] = [{deleted: undefined, needBalanced: null}];
211
+ // if (!node) return result; // Node does not exist
212
+ //
213
+ // const replacement = this._getReplacementNode(node);
214
+ //
215
+ // const isRed = this._isNodeRed(node);
216
+ // const isRedReplacement = this._isNodeRed(replacement);
217
+ //
218
+ // // Remove the node
219
+ // this._removeNode(node, replacement);
220
+ //
221
+ // if (isRed || isRedReplacement) {
222
+ // // If the removed node is red or the replacement node is red, no repair is needed
223
+ // return result;
224
+ // }
225
+ //
226
+ // // Repair any violation introduced by the removal
227
+ // this._fixDeleteViolation(replacement);
228
+ //
229
+ // return result;
230
+ // }
231
+ //
232
+ // // Repair operation after node deletion
233
+ // private _fixDeleteViolation(node: N | null | undefined) {
234
+ // let sibling;
235
+ //
236
+ // while (node && node !== this.root && !this._isNodeRed(node)) {
237
+ // if (node === node.parent!.left) {
238
+ // sibling = node.parent!.right;
239
+ //
240
+ // if (sibling && this._isNodeRed(sibling)) {
241
+ // // Case 1: The sibling node is red
242
+ // sibling.color = RBColor.BLACK;
243
+ // node.parent!.color = RBColor.RED;
244
+ // this._rotateLeft(node.parent!);
245
+ // sibling = node.parent!.right;
246
+ // }
247
+ //
248
+ // if (!sibling) return;
249
+ //
250
+ // if (
251
+ // (!sibling.left || sibling.left.color === RBColor.BLACK) &&
252
+ // (!sibling.right || sibling.right.color === RBColor.BLACK)
253
+ // ) {
254
+ // // Case 2: The sibling node and its children are all black
255
+ // sibling.color = RBColor.RED;
256
+ // node = node.parent!;
257
+ // } else {
258
+ // if (!(sibling.right && this._isNodeRed(sibling.right))) {
259
+ // // Case 3: The sibling node is black, and the left child is red, the right child is black
260
+ // sibling.left!.color = RBColor.BLACK;
261
+ // sibling.color = RBColor.RED;
262
+ // this._rotateRight(sibling);
263
+ // sibling = node.parent!.right;
264
+ // }
265
+ //
266
+ // // Case 4: The sibling node is black, and the right child is red
267
+ // if (sibling) {
268
+ // sibling.color = node.parent!.color;
269
+ // }
270
+ // if (node.parent) {
271
+ // node.parent.color = RBColor.BLACK;
272
+ // }
273
+ // if (sibling!.right) {
274
+ // sibling!.right.color = RBColor.BLACK;
275
+ // }
276
+ // this._rotateLeft(node.parent!);
277
+ // node = this.root;
278
+ // }
279
+ // } else {
280
+ // // Symmetric case: The parent is the right child of the grandparent
281
+ // sibling = node.parent!.left;
282
+ //
283
+ // if (sibling && this._isNodeRed(sibling)) {
284
+ // // Case 1: The sibling node is red
285
+ // sibling.color = RBColor.BLACK;
286
+ // node.parent!.color = RBColor.RED;
287
+ // this._rotateRight(node.parent!);
288
+ // sibling = node.parent!.left;
289
+ // }
290
+ //
291
+ // if (!sibling) return;
292
+ //
293
+ // if (
294
+ // (!sibling.left || sibling.left.color === RBColor.BLACK) &&
295
+ // (!sibling.right || sibling.right.color === RBColor.BLACK)
296
+ // ) {
297
+ // // Case 2: The sibling node and its children are all black
298
+ // sibling.color = RBColor.RED;
299
+ // node = node.parent!;
300
+ // } else {
301
+ // if (!(sibling.left && this._isNodeRed(sibling.left))) {
302
+ // // Case 3: The sibling node is black, and the right child is red, the left child is black
303
+ // sibling.right!.color = RBColor.BLACK;
304
+ // sibling.color = RBColor.RED;
305
+ // this._rotateLeft(sibling);
306
+ // sibling = node.parent!.left;
307
+ // }
308
+ //
309
+ // // Case 4: The sibling node is black, and the left child is red
310
+ // if (sibling) {
311
+ // sibling.color = node.parent!.color;
312
+ // }
313
+ // if (node.parent) {
314
+ // node.parent.color = RBColor.BLACK;
315
+ // }
316
+ // if (sibling!.left) {
317
+ // sibling!.left.color = RBColor.BLACK;
318
+ // }
319
+ // this._rotateRight(node.parent!);
320
+ // node = this.root;
321
+ // }
322
+ // }
323
+ // }
324
+ //
325
+ // if (node) {
326
+ // node.color = RBColor.BLACK;
327
+ // }
328
+ // }
329
+ //
330
+ // private _findMin(node: N): N {
331
+ // while (node.left) {
332
+ // node = node.left;
333
+ // }
334
+ // return node;
335
+ // }
336
+ //
337
+ // // Get the replacement node
338
+ // private _getReplacementNode(node: N): N | null | undefined {
339
+ // if (node.left && node.right) {
340
+ // return this._findSuccessor(node);
341
+ // }
342
+ //
343
+ // if (!node.left && !node.right) {
344
+ // return null; // Return a fake node with color black
345
+ // }
346
+ //
347
+ // return node.left || node.right;
348
+ // }
349
+ //
350
+ // // Find the successor node
351
+ // private _findSuccessor(node: N): N | null | undefined {
352
+ // if (node.right) {
353
+ // // If the node has a right child, find the minimum node in the right subtree as the successor
354
+ // return this._findMin(node.right);
355
+ // }
356
+ //
357
+ // // Otherwise, traverse upward until finding the first parent whose left child is the current node
358
+ // let parent = node.parent;
359
+ // while (parent && node === parent.right) {
360
+ // node = parent;
361
+ // parent = parent.parent;
362
+ // }
363
+ //
364
+ // return parent;
365
+ // }
366
+ }
@@ -0,0 +1,257 @@
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 type {SegmentTreeNodeVal} from '../../types';
10
+
11
+ export class SegmentTreeNode {
12
+ constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null) {
13
+ this._start = start;
14
+ this._end = end;
15
+ this._sum = sum;
16
+ this._val = val || null;
17
+ }
18
+
19
+ private _start = 0;
20
+ get start(): number {
21
+ return this._start;
22
+ }
23
+
24
+ set start(v: number) {
25
+ this._start = v;
26
+ }
27
+
28
+ private _end = 0;
29
+
30
+ get end(): number {
31
+ return this._end;
32
+ }
33
+
34
+ set end(v: number) {
35
+ this._end = v;
36
+ }
37
+
38
+ private _val: SegmentTreeNodeVal | null = null;
39
+
40
+ get val(): SegmentTreeNodeVal | null {
41
+ return this._val;
42
+ }
43
+
44
+ set val(v: SegmentTreeNodeVal | null) {
45
+ this._val = v;
46
+ }
47
+
48
+ private _sum = 0;
49
+
50
+ get sum(): number {
51
+ return this._sum;
52
+ }
53
+
54
+ set sum(v: number) {
55
+ this._sum = v;
56
+ }
57
+
58
+ private _left: SegmentTreeNode | null = null;
59
+
60
+ get left(): SegmentTreeNode | null {
61
+ return this._left;
62
+ }
63
+
64
+ set left(v: SegmentTreeNode | null) {
65
+ this._left = v;
66
+ }
67
+
68
+ private _right: SegmentTreeNode | null = null;
69
+
70
+ get right(): SegmentTreeNode | null {
71
+ return this._right;
72
+ }
73
+
74
+ set right(v: SegmentTreeNode | null) {
75
+ this._right = v;
76
+ }
77
+ }
78
+
79
+ export class SegmentTree {
80
+ /**
81
+ * The constructor initializes the values, start, end, and root properties of an object.
82
+ * @param {number[]} values - An array of numbers that will be used to build a binary search tree.
83
+ * @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
84
+ * be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
85
+ * the beginning of the array.
86
+ * @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
87
+ * included in the range. If not provided, it defaults to the index of the last element in the "values" array.
88
+ */
89
+ constructor(values: number[], start?: number, end?: number) {
90
+ start = start || 0;
91
+ end = end || values.length - 1;
92
+ this._values = values;
93
+ this._start = start;
94
+ this._end = end;
95
+
96
+ if (values.length > 0) {
97
+ this._root = this.build(start, end);
98
+ } else {
99
+ this._root = null;
100
+ this._values = [];
101
+ }
102
+ }
103
+
104
+ private _values: number[] = [];
105
+
106
+ get values(): number[] {
107
+ return this._values;
108
+ }
109
+
110
+ private _start = 0;
111
+ get start(): number {
112
+ return this._start;
113
+ }
114
+
115
+ private _end: number;
116
+
117
+ get end(): number {
118
+ return this._end;
119
+ }
120
+
121
+ private _root: SegmentTreeNode | null;
122
+
123
+ get root(): SegmentTreeNode | null {
124
+ return this._root;
125
+ }
126
+
127
+ /**
128
+ * The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
129
+ * the sum of values to each segment.
130
+ * @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
131
+ * building the segment tree.
132
+ * @param {number} end - The "end" parameter represents the ending index of the segment or range for which we want to
133
+ * build a segment tree.
134
+ * @returns a SegmentTreeNode object.
135
+ */
136
+ build(start: number, end: number): SegmentTreeNode {
137
+ if (start > end) {
138
+ return new SegmentTreeNode(start, end, 0);
139
+ }
140
+ if (start === end) return new SegmentTreeNode(start, end, this._values[start]);
141
+
142
+ const mid = start + Math.floor((end - start) / 2);
143
+ const left = this.build(start, mid);
144
+ const right = this.build(mid + 1, end);
145
+ const cur = new SegmentTreeNode(start, end, left.sum + right.sum);
146
+ cur.left = left;
147
+ cur.right = right;
148
+ return cur;
149
+ }
150
+
151
+ /**
152
+ * The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
153
+ * @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
154
+ * updated.
155
+ * @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
156
+ * the `SegmentTreeNode` at the specified `index`.
157
+ * @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
158
+ * property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
159
+ * cur.val = val;` and pass a value for `val` in the
160
+ * @returns The function does not return anything.
161
+ */
162
+ updateNode(index: number, sum: number, val?: SegmentTreeNodeVal) {
163
+ const root = this.root || null;
164
+ if (!root) {
165
+ return;
166
+ }
167
+ const dfs = (cur: SegmentTreeNode, index: number, sum: number, val?: SegmentTreeNodeVal) => {
168
+ if (cur.start === cur.end && cur.start === index) {
169
+ cur.sum = sum;
170
+ if (val !== undefined) cur.val = val;
171
+ return;
172
+ }
173
+ const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
174
+ if (index <= mid) {
175
+ if (cur.left) {
176
+ dfs(cur.left, index, sum, val);
177
+ }
178
+ } else {
179
+ if (cur.right) {
180
+ dfs(cur.right, index, sum, val);
181
+ }
182
+ }
183
+ if (cur.left && cur.right) {
184
+ cur.sum = cur.left.sum + cur.right.sum;
185
+ }
186
+ };
187
+
188
+ dfs(root, index, sum, val);
189
+ }
190
+
191
+ /**
192
+ * The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
193
+ * @param {number} indexA - The starting index of the range for which you want to calculate the sum.
194
+ * @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
195
+ * calculate the sum.
196
+ * @returns The function `querySumByRange` returns a number.
197
+ */
198
+ querySumByRange(indexA: number, indexB: number): number {
199
+ const root = this.root || null;
200
+ if (!root) {
201
+ return 0;
202
+ }
203
+
204
+ if (indexA < 0 || indexB >= this.values.length || indexA > indexB) {
205
+ return NaN;
206
+ }
207
+
208
+ const dfs = (cur: SegmentTreeNode, i: number, j: number): number => {
209
+ if (i <= cur.start && j >= cur.end) {
210
+ // The range [i, j] completely covers the current node's range [cur.start, cur.end]
211
+ return cur.sum;
212
+ }
213
+ const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
214
+ if (j <= mid) {
215
+ if (cur.left) {
216
+ return dfs(cur.left, i, j);
217
+ } else {
218
+ return NaN;
219
+ }
220
+ } else if (i > mid) {
221
+ if (cur.right) {
222
+ return dfs(cur.right, i, j);
223
+ } else {
224
+ return NaN;
225
+ }
226
+ } else {
227
+ // Query both left and right subtrees
228
+ let leftSum = 0;
229
+ let rightSum = 0;
230
+ if (cur.left) {
231
+ leftSum = dfs(cur.left, i, mid);
232
+ }
233
+ if (cur.right) {
234
+ rightSum = dfs(cur.right, mid + 1, j);
235
+ }
236
+ return leftSum + rightSum;
237
+ }
238
+ };
239
+ return dfs(root, indexA, indexB);
240
+ }
241
+
242
+ protected _setValues(value: number[]) {
243
+ this._values = value;
244
+ }
245
+
246
+ protected _setStart(value: number) {
247
+ this._start = value;
248
+ }
249
+
250
+ protected _setEnd(value: number) {
251
+ this._end = value;
252
+ }
253
+
254
+ protected _setRoot(v: SegmentTreeNode | null) {
255
+ this._root = v;
256
+ }
257
+ }