doubly-linked-list-typed 2.1.0 → 2.1.2

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 (281) hide show
  1. package/dist/cjs/index.cjs +1299 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/esm/index.mjs +1294 -0
  4. package/dist/esm/index.mjs.map +1 -0
  5. package/dist/types/data-structures/base/index.d.ts +2 -1
  6. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
  7. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
  8. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
  9. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
  12. package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
  14. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
  15. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
  17. package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
  18. package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
  19. package/dist/types/data-structures/graph/index.d.ts +2 -1
  20. package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
  21. package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
  22. package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
  23. package/dist/types/data-structures/hash/index.d.ts +0 -1
  24. package/dist/types/data-structures/heap/heap.d.ts +503 -5
  25. package/dist/types/data-structures/heap/index.d.ts +2 -0
  26. package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
  27. package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
  28. package/dist/types/data-structures/index.d.ts +7 -7
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
  31. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
  32. package/dist/types/data-structures/matrix/index.d.ts +1 -1
  33. package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
  34. package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
  38. package/dist/types/data-structures/queue/deque.d.ts +431 -4
  39. package/dist/types/data-structures/queue/queue.d.ts +308 -4
  40. package/dist/types/data-structures/stack/stack.d.ts +306 -2
  41. package/dist/types/data-structures/tree/tree.d.ts +62 -1
  42. package/dist/types/data-structures/trie/trie.d.ts +350 -4
  43. package/dist/types/index.d.ts +11 -2
  44. package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +3 -3
  45. package/dist/types/types/data-structures/base/index.d.ts +1 -0
  46. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  47. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
  48. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
  49. package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  50. package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  51. package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
  52. package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
  53. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  54. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  55. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  56. package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
  57. package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
  58. package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
  59. package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
  60. package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
  61. package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
  62. package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
  63. package/dist/types/types/data-structures/hash/index.d.ts +2 -0
  64. package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
  65. package/dist/types/types/data-structures/heap/index.d.ts +1 -0
  66. package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
  67. package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
  68. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
  69. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
  70. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
  71. package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
  72. package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
  73. package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  74. package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  75. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
  76. package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
  77. package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
  78. package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
  79. package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
  80. package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
  81. package/dist/types/types/index.d.ts +3 -0
  82. package/dist/types/types/utils/index.d.ts +2 -0
  83. package/dist/types/types/utils/utils.d.ts +22 -0
  84. package/dist/types/utils/index.d.ts +1 -1
  85. package/dist/types/utils/utils.d.ts +209 -22
  86. package/dist/umd/doubly-linked-list-typed.js +1302 -0
  87. package/dist/umd/doubly-linked-list-typed.js.map +1 -0
  88. package/dist/umd/doubly-linked-list-typed.min.js +9 -0
  89. package/dist/umd/doubly-linked-list-typed.min.js.map +1 -0
  90. package/package.json +25 -5
  91. package/src/data-structures/binary-tree/avl-tree-counter.ts +8 -11
  92. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +6 -11
  93. package/src/data-structures/binary-tree/avl-tree.ts +6 -8
  94. package/src/data-structures/binary-tree/binary-tree.ts +13 -15
  95. package/src/data-structures/binary-tree/bst.ts +6 -11
  96. package/src/data-structures/binary-tree/red-black-tree.ts +6 -11
  97. package/src/data-structures/binary-tree/tree-counter.ts +8 -13
  98. package/src/data-structures/binary-tree/tree-multi-map.ts +6 -11
  99. package/src/data-structures/heap/heap.ts +5 -5
  100. package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
  101. package/src/interfaces/binary-tree.ts +3 -3
  102. package/tsconfig.base.json +23 -0
  103. package/tsconfig.json +8 -34
  104. package/tsconfig.test.json +8 -0
  105. package/tsconfig.types.json +15 -0
  106. package/tsup.config.js +28 -0
  107. package/tsup.node.config.js +37 -0
  108. package/dist/common/index.js +0 -28
  109. package/dist/constants/index.js +0 -8
  110. package/dist/data-structures/base/index.d.ts +0 -2
  111. package/dist/data-structures/base/index.js +0 -18
  112. package/dist/data-structures/base/iterable-element-base.js +0 -243
  113. package/dist/data-structures/base/iterable-entry-base.js +0 -183
  114. package/dist/data-structures/base/linear-base.js +0 -415
  115. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
  116. package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
  117. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
  118. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
  119. package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
  120. package/dist/data-structures/binary-tree/avl-tree.js +0 -611
  121. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
  122. package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
  123. package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
  124. package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
  125. package/dist/data-structures/binary-tree/bst.d.ts +0 -413
  126. package/dist/data-structures/binary-tree/bst.js +0 -903
  127. package/dist/data-structures/binary-tree/index.js +0 -26
  128. package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
  129. package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
  130. package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
  131. package/dist/data-structures/binary-tree/segment-tree.js +0 -297
  132. package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
  133. package/dist/data-structures/binary-tree/tree-counter.js +0 -413
  134. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
  135. package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
  136. package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
  137. package/dist/data-structures/graph/abstract-graph.js +0 -896
  138. package/dist/data-structures/graph/directed-graph.d.ts +0 -207
  139. package/dist/data-structures/graph/directed-graph.js +0 -525
  140. package/dist/data-structures/graph/index.js +0 -20
  141. package/dist/data-structures/graph/map-graph.d.ts +0 -78
  142. package/dist/data-structures/graph/map-graph.js +0 -107
  143. package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
  144. package/dist/data-structures/graph/undirected-graph.js +0 -424
  145. package/dist/data-structures/hash/hash-map.d.ts +0 -345
  146. package/dist/data-structures/hash/hash-map.js +0 -692
  147. package/dist/data-structures/hash/index.d.ts +0 -1
  148. package/dist/data-structures/hash/index.js +0 -17
  149. package/dist/data-structures/heap/heap.d.ts +0 -503
  150. package/dist/data-structures/heap/heap.js +0 -901
  151. package/dist/data-structures/heap/index.d.ts +0 -3
  152. package/dist/data-structures/heap/index.js +0 -19
  153. package/dist/data-structures/heap/max-heap.d.ts +0 -32
  154. package/dist/data-structures/heap/max-heap.js +0 -40
  155. package/dist/data-structures/heap/min-heap.d.ts +0 -33
  156. package/dist/data-structures/heap/min-heap.js +0 -31
  157. package/dist/data-structures/index.js +0 -28
  158. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
  159. package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
  160. package/dist/data-structures/linked-list/index.js +0 -19
  161. package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
  162. package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
  163. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
  164. package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
  165. package/dist/data-structures/matrix/index.js +0 -18
  166. package/dist/data-structures/matrix/matrix.d.ts +0 -168
  167. package/dist/data-structures/matrix/matrix.js +0 -448
  168. package/dist/data-structures/matrix/navigator.d.ts +0 -55
  169. package/dist/data-structures/matrix/navigator.js +0 -111
  170. package/dist/data-structures/priority-queue/index.js +0 -19
  171. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
  172. package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
  173. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
  174. package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
  175. package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
  176. package/dist/data-structures/priority-queue/priority-queue.js +0 -20
  177. package/dist/data-structures/queue/deque.d.ts +0 -431
  178. package/dist/data-structures/queue/deque.js +0 -879
  179. package/dist/data-structures/queue/index.js +0 -18
  180. package/dist/data-structures/queue/queue.d.ts +0 -308
  181. package/dist/data-structures/queue/queue.js +0 -473
  182. package/dist/data-structures/stack/index.js +0 -17
  183. package/dist/data-structures/stack/stack.d.ts +0 -306
  184. package/dist/data-structures/stack/stack.js +0 -401
  185. package/dist/data-structures/tree/index.js +0 -17
  186. package/dist/data-structures/tree/tree.d.ts +0 -62
  187. package/dist/data-structures/tree/tree.js +0 -107
  188. package/dist/data-structures/trie/index.js +0 -17
  189. package/dist/data-structures/trie/trie.d.ts +0 -350
  190. package/dist/data-structures/trie/trie.js +0 -610
  191. package/dist/index.d.ts +0 -12
  192. package/dist/index.js +0 -28
  193. package/dist/interfaces/binary-tree.js +0 -2
  194. package/dist/interfaces/doubly-linked-list.js +0 -2
  195. package/dist/interfaces/graph.js +0 -2
  196. package/dist/interfaces/heap.js +0 -2
  197. package/dist/interfaces/index.js +0 -24
  198. package/dist/interfaces/navigator.js +0 -2
  199. package/dist/interfaces/priority-queue.js +0 -2
  200. package/dist/interfaces/segment-tree.js +0 -2
  201. package/dist/interfaces/singly-linked-list.js +0 -2
  202. package/dist/types/common.js +0 -2
  203. package/dist/types/data-structures/base/base.js +0 -2
  204. package/dist/types/data-structures/base/index.js +0 -17
  205. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
  206. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
  207. package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
  208. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
  209. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
  210. package/dist/types/data-structures/binary-tree/bst.js +0 -2
  211. package/dist/types/data-structures/binary-tree/index.js +0 -25
  212. package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
  213. package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
  214. package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
  215. package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
  216. package/dist/types/data-structures/graph/abstract-graph.js +0 -2
  217. package/dist/types/data-structures/graph/directed-graph.js +0 -2
  218. package/dist/types/data-structures/graph/index.js +0 -19
  219. package/dist/types/data-structures/graph/map-graph.js +0 -2
  220. package/dist/types/data-structures/graph/undirected-graph.js +0 -2
  221. package/dist/types/data-structures/hash/hash-map.js +0 -2
  222. package/dist/types/data-structures/hash/index.js +0 -17
  223. package/dist/types/data-structures/heap/heap.js +0 -2
  224. package/dist/types/data-structures/heap/index.js +0 -17
  225. package/dist/types/data-structures/heap/max-heap.js +0 -2
  226. package/dist/types/data-structures/heap/min-heap.js +0 -2
  227. package/dist/types/data-structures/index.js +0 -28
  228. package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
  229. package/dist/types/data-structures/linked-list/index.js +0 -19
  230. package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
  231. package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
  232. package/dist/types/data-structures/matrix/index.js +0 -18
  233. package/dist/types/data-structures/matrix/matrix.js +0 -2
  234. package/dist/types/data-structures/matrix/navigator.js +0 -2
  235. package/dist/types/data-structures/priority-queue/index.js +0 -19
  236. package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
  237. package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
  238. package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
  239. package/dist/types/data-structures/queue/deque.js +0 -2
  240. package/dist/types/data-structures/queue/index.js +0 -18
  241. package/dist/types/data-structures/queue/queue.js +0 -2
  242. package/dist/types/data-structures/stack/index.js +0 -17
  243. package/dist/types/data-structures/stack/stack.js +0 -2
  244. package/dist/types/data-structures/tree/index.js +0 -17
  245. package/dist/types/data-structures/tree/tree.js +0 -2
  246. package/dist/types/data-structures/trie/index.js +0 -17
  247. package/dist/types/data-structures/trie/trie.js +0 -2
  248. package/dist/types/index.js +0 -19
  249. package/dist/types/utils/index.js +0 -18
  250. package/dist/types/utils/utils.js +0 -2
  251. package/dist/types/utils/validate-type.js +0 -2
  252. package/dist/utils/index.d.ts +0 -2
  253. package/dist/utils/index.js +0 -18
  254. package/dist/utils/number.js +0 -24
  255. package/dist/utils/utils.d.ts +0 -209
  256. package/dist/utils/utils.js +0 -353
  257. package/dist/{common → types/common}/index.d.ts +0 -0
  258. package/dist/{constants → types/constants}/index.d.ts +0 -0
  259. package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
  260. package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
  261. package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
  262. package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
  263. package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
  264. package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
  265. package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
  266. package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
  267. package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
  268. package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
  269. package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
  270. package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
  271. package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
  272. package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
  273. package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
  274. package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
  275. /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
  276. /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
  277. /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
  278. /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
  279. /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
  280. /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
  281. /package/dist/{utils → types/utils}/number.d.ts +0 -0
@@ -1,546 +0,0 @@
1
- "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Pablo Zeng
6
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
- * @license MIT License
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.RedBlackTree = exports.RedBlackTreeNode = void 0;
11
- const bst_1 = require("./bst");
12
- class RedBlackTreeNode extends bst_1.BSTNode {
13
- /**
14
- * Create a Red-Black Tree and optionally bulk-insert items.
15
- * @remarks Time O(n log n), Space O(n)
16
- * @param key - See parameter type for details.
17
- * @param [value]- See parameter type for details.
18
- * @param color - See parameter type for details.
19
- * @returns New RedBlackTree instance.
20
- */
21
- constructor(key, value, color = 'BLACK') {
22
- super(key, value);
23
- this.parent = undefined;
24
- this._left = undefined;
25
- this._right = undefined;
26
- this._color = color;
27
- }
28
- /**
29
- * Get the left child pointer.
30
- * @remarks Time O(1), Space O(1)
31
- * @returns Left child node, or null/undefined.
32
- */
33
- get left() {
34
- return this._left;
35
- }
36
- /**
37
- * Set the left child and update its parent pointer.
38
- * @remarks Time O(1), Space O(1)
39
- * @param v - New left node, or null/undefined.
40
- * @returns void
41
- */
42
- set left(v) {
43
- if (v) {
44
- v.parent = this;
45
- }
46
- this._left = v;
47
- }
48
- /**
49
- * Get the right child pointer.
50
- * @remarks Time O(1), Space O(1)
51
- * @returns Right child node, or null/undefined.
52
- */
53
- get right() {
54
- return this._right;
55
- }
56
- /**
57
- * Set the right child and update its parent pointer.
58
- * @remarks Time O(1), Space O(1)
59
- * @param v - New right node, or null/undefined.
60
- * @returns void
61
- */
62
- set right(v) {
63
- if (v) {
64
- v.parent = this;
65
- }
66
- this._right = v;
67
- }
68
- }
69
- exports.RedBlackTreeNode = RedBlackTreeNode;
70
- /**
71
- * RRRRed-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
72
- * @remarks Time O(1), Space O(1)
73
- * @template K
74
- * @template V
75
- * @template R
76
- * 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
77
- * 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
78
- * @example
79
- * // using Red-Black Tree as a price-based index for stock data
80
- * // Define the structure of individual stock records
81
- * interface StockRecord {
82
- * price: number; // Stock price (key for indexing)
83
- * symbol: string; // Stock ticker symbol
84
- * volume: number; // Trade volume
85
- * }
86
- *
87
- * // Simulate stock market data as it might come from an external feed
88
- * const marketStockData: StockRecord[] = [
89
- * { price: 142.5, symbol: 'AAPL', volume: 1000000 },
90
- * { price: 335.2, symbol: 'MSFT', volume: 800000 },
91
- * { price: 3285.04, symbol: 'AMZN', volume: 500000 },
92
- * { price: 267.98, symbol: 'META', volume: 750000 },
93
- * { price: 234.57, symbol: 'GOOGL', volume: 900000 }
94
- * ];
95
- *
96
- * // Extend the stock record type to include metadata for database usage
97
- * type StockTableRecord = StockRecord & { lastUpdated: Date };
98
- *
99
- * // Create a Red-Black Tree to index stock records by price
100
- * // Simulates a database index with stock price as the key for quick lookups
101
- * const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
102
- * toEntryFn: stockRecord => [
103
- * stockRecord.price, // Use stock price as the key
104
- * {
105
- * ...stockRecord,
106
- * lastUpdated: new Date() // Add a timestamp for when the record was indexed
107
- * }
108
- * ]
109
- * });
110
- *
111
- * // Query the stock with the highest price
112
- * const highestPricedStock = priceIndex.getRightMost();
113
- * console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
114
- *
115
- * // Query stocks within a specific price range (200 to 400)
116
- * const stocksInRange = priceIndex.rangeSearch(
117
- * [200, 400], // Price range
118
- * node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
119
- * );
120
- * console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
121
- */
122
- class RedBlackTree extends bst_1.BST {
123
- constructor(keysNodesEntriesOrRaws = [], options) {
124
- super([], options);
125
- this._root = this.NIL;
126
- if (keysNodesEntriesOrRaws) {
127
- this.addMany(keysNodesEntriesOrRaws);
128
- }
129
- }
130
- /**
131
- * Get the current root node.
132
- * @remarks Time O(1), Space O(1)
133
- * @returns Root node, or undefined.
134
- */
135
- get root() {
136
- return this._root;
137
- }
138
- /**
139
- * Create a red-black node for the given key/value (value ignored in map mode).
140
- * @remarks Time O(1), Space O(1)
141
- * @param key - See parameter type for details.
142
- * @param [value] - See parameter type for details.
143
- * @param color - See parameter type for details.
144
- * @returns A new RedBlackTreeNode instance.
145
- */
146
- _createNode(key, value, color = 'BLACK') {
147
- return new RedBlackTreeNode(key, this._isMapMode ? undefined : value, color);
148
- }
149
- /**
150
- * Type guard: check whether the input is a RedBlackTreeNode.
151
- * @remarks Time O(1), Space O(1)
152
- * @param keyNodeOrEntry - See parameter type for details.
153
- * @returns True if the value is a RedBlackTreeNode.
154
- */
155
- isNode(keyNodeOrEntry) {
156
- return keyNodeOrEntry instanceof RedBlackTreeNode;
157
- }
158
- /**
159
- * Remove all nodes and clear the key→value store (if in map mode).
160
- * @remarks Time O(n), Space O(1)
161
- * @returns void
162
- */
163
- clear() {
164
- super.clear();
165
- this._root = this.NIL;
166
- }
167
- /**
168
- * Insert or replace an entry using BST order and red-black fix-up.
169
- * @remarks Time O(log n), Space O(1)
170
- * @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
171
- * @param [value]- See parameter type for details.
172
- * @returns True if inserted or updated; false if ignored.
173
- */
174
- add(keyNodeOrEntry, value) {
175
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
176
- if (!this.isRealNode(newNode))
177
- return false;
178
- const insertStatus = this._insert(newNode);
179
- if (insertStatus === 'CREATED') {
180
- if (this.isRealNode(this._root)) {
181
- this._root.color = 'BLACK';
182
- }
183
- else {
184
- return false;
185
- }
186
- if (this._isMapMode)
187
- this._setValue(newNode.key, newValue);
188
- this._size++;
189
- return true;
190
- }
191
- if (insertStatus === 'UPDATED') {
192
- if (this._isMapMode)
193
- this._setValue(newNode.key, newValue);
194
- return true;
195
- }
196
- return false;
197
- }
198
- /**
199
- * Delete a node by key/node/entry and rebalance as needed.
200
- * @remarks Time O(log n), Space O(1)
201
- * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node to delete.
202
- * @returns Array with deletion metadata (removed node, rebalancing hint if any).
203
- */
204
- delete(keyNodeOrEntry) {
205
- if (keyNodeOrEntry === null)
206
- return [];
207
- const results = [];
208
- let nodeToDelete;
209
- if (this._isPredicate(keyNodeOrEntry))
210
- nodeToDelete = this.getNode(keyNodeOrEntry);
211
- else
212
- nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
213
- if (!nodeToDelete) {
214
- return results;
215
- }
216
- let originalColor = nodeToDelete.color;
217
- let replacementNode;
218
- if (!this.isRealNode(nodeToDelete.left)) {
219
- if (nodeToDelete.right !== null) {
220
- replacementNode = nodeToDelete.right;
221
- this._transplant(nodeToDelete, nodeToDelete.right);
222
- }
223
- }
224
- else if (!this.isRealNode(nodeToDelete.right)) {
225
- replacementNode = nodeToDelete.left;
226
- this._transplant(nodeToDelete, nodeToDelete.left);
227
- }
228
- else {
229
- const successor = this.getLeftMost(node => node, nodeToDelete.right);
230
- if (successor) {
231
- originalColor = successor.color;
232
- if (successor.right !== null)
233
- replacementNode = successor.right;
234
- if (successor.parent === nodeToDelete) {
235
- if (this.isRealNode(replacementNode)) {
236
- replacementNode.parent = successor;
237
- }
238
- }
239
- else {
240
- if (successor.right !== null) {
241
- this._transplant(successor, successor.right);
242
- successor.right = nodeToDelete.right;
243
- }
244
- if (this.isRealNode(successor.right)) {
245
- successor.right.parent = successor;
246
- }
247
- }
248
- this._transplant(nodeToDelete, successor);
249
- successor.left = nodeToDelete.left;
250
- if (this.isRealNode(successor.left)) {
251
- successor.left.parent = successor;
252
- }
253
- successor.color = nodeToDelete.color;
254
- }
255
- }
256
- if (this._isMapMode)
257
- this._store.delete(nodeToDelete.key);
258
- this._size--;
259
- if (originalColor === 'BLACK') {
260
- this._deleteFixup(replacementNode);
261
- }
262
- results.push({ deleted: nodeToDelete, needBalanced: undefined });
263
- return results;
264
- }
265
- /**
266
- * Transform entries into a like-kind red-black tree with possibly different key/value types.
267
- * @remarks Time O(n), Space O(n)
268
- * @template MK
269
- * @template MV
270
- * @template MR
271
- * @param callback - Mapping function from (key, value, index, tree) to a new [key, value].
272
- * @param [options] - See parameter type for details.
273
- * @param [thisArg] - See parameter type for details.
274
- * @returns A new RedBlackTree with mapped entries.
275
- */
276
- map(callback, options, thisArg) {
277
- const out = this._createLike([], options);
278
- let index = 0;
279
- for (const [key, value] of this) {
280
- out.add(callback.call(thisArg, key, value, index++, this));
281
- }
282
- return out;
283
- }
284
- _createInstance(options) {
285
- const Ctor = this.constructor;
286
- return new Ctor([], Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
287
- }
288
- _createLike(iter = [], options) {
289
- const Ctor = this.constructor;
290
- return new Ctor(iter, Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
291
- }
292
- _setRoot(v) {
293
- if (v) {
294
- v.parent = undefined;
295
- }
296
- this._root = v;
297
- }
298
- _replaceNode(oldNode, newNode) {
299
- newNode.color = oldNode.color;
300
- return super._replaceNode(oldNode, newNode);
301
- }
302
- /**
303
- * (Protected) Standard BST insert followed by red-black fix-up.
304
- * @remarks Time O(log n), Space O(1)
305
- * @param node - Node to insert.
306
- * @returns Status string: 'CREATED' or 'UPDATED'.
307
- */
308
- _insert(node) {
309
- var _a, _b;
310
- let current = this.root;
311
- let parent = undefined;
312
- while (this.isRealNode(current)) {
313
- parent = current;
314
- const compared = this._compare(node.key, current.key);
315
- if (compared < 0) {
316
- current = (_a = current.left) !== null && _a !== void 0 ? _a : this.NIL;
317
- }
318
- else if (compared > 0) {
319
- current = (_b = current.right) !== null && _b !== void 0 ? _b : this.NIL;
320
- }
321
- else {
322
- this._replaceNode(current, node);
323
- return 'UPDATED';
324
- }
325
- }
326
- node.parent = parent;
327
- if (!parent) {
328
- this._setRoot(node);
329
- }
330
- else if (this._compare(node.key, parent.key) < 0) {
331
- parent.left = node;
332
- }
333
- else {
334
- parent.right = node;
335
- }
336
- node.left = this.NIL;
337
- node.right = this.NIL;
338
- node.color = 'RED';
339
- this._insertFixup(node);
340
- return 'CREATED';
341
- }
342
- /**
343
- * (Protected) Transplant a subtree in place of another during deletion.
344
- * @remarks Time O(1), Space O(1)
345
- * @param u - Node to replace.
346
- * @param v - Replacement subtree root (may be undefined).
347
- * @returns void
348
- */
349
- _transplant(u, v) {
350
- if (!u.parent) {
351
- this._setRoot(v);
352
- }
353
- else if (u === u.parent.left) {
354
- u.parent.left = v;
355
- }
356
- else {
357
- u.parent.right = v;
358
- }
359
- if (v) {
360
- v.parent = u.parent;
361
- }
362
- }
363
- /**
364
- * (Protected) Restore red-black properties after insertion (recolor/rotate).
365
- * @remarks Time O(log n), Space O(1)
366
- * @param z - Recently inserted node.
367
- * @returns void
368
- */
369
- _insertFixup(z) {
370
- var _a, _b, _c, _d, _e;
371
- while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
372
- if (z.parent === ((_b = z.parent.parent) === null || _b === void 0 ? void 0 : _b.left)) {
373
- const y = z.parent.parent.right;
374
- if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
375
- z.parent.color = 'BLACK';
376
- y.color = 'BLACK';
377
- z.parent.parent.color = 'RED';
378
- z = z.parent.parent;
379
- }
380
- else {
381
- if (z === z.parent.right) {
382
- z = z.parent;
383
- this._leftRotate(z);
384
- }
385
- if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
386
- z.parent.color = 'BLACK';
387
- z.parent.parent.color = 'RED';
388
- this._rightRotate(z.parent.parent);
389
- }
390
- }
391
- }
392
- else {
393
- const y = (_e = (_d = (_c = z === null || z === void 0 ? void 0 : z.parent) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.left) !== null && _e !== void 0 ? _e : undefined;
394
- if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
395
- z.parent.color = 'BLACK';
396
- y.color = 'BLACK';
397
- z.parent.parent.color = 'RED';
398
- z = z.parent.parent;
399
- }
400
- else {
401
- if (z === z.parent.left) {
402
- z = z.parent;
403
- this._rightRotate(z);
404
- }
405
- if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
406
- z.parent.color = 'BLACK';
407
- z.parent.parent.color = 'RED';
408
- this._leftRotate(z.parent.parent);
409
- }
410
- }
411
- }
412
- }
413
- if (this.isRealNode(this._root))
414
- this._root.color = 'BLACK';
415
- }
416
- /**
417
- * (Protected) Restore red-black properties after deletion (recolor/rotate).
418
- * @remarks Time O(log n), Space O(1)
419
- * @param node - Child that replaced the deleted node (may be undefined).
420
- * @returns void
421
- */
422
- _deleteFixup(node) {
423
- var _a, _b, _c, _d;
424
- if (!node || node === this.root || node.color === 'BLACK') {
425
- if (node) {
426
- node.color = 'BLACK';
427
- }
428
- return;
429
- }
430
- while (node && node !== this.root && node.color === 'BLACK') {
431
- const parent = node.parent;
432
- if (!parent) {
433
- break;
434
- }
435
- if (node === parent.left) {
436
- let sibling = parent.right;
437
- if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) === 'RED') {
438
- sibling.color = 'BLACK';
439
- parent.color = 'RED';
440
- this._leftRotate(parent);
441
- sibling = parent.right;
442
- }
443
- if (((_b = (_a = sibling === null || sibling === void 0 ? void 0 : sibling.left) === null || _a === void 0 ? void 0 : _a.color) !== null && _b !== void 0 ? _b : 'BLACK') === 'BLACK') {
444
- if (sibling)
445
- sibling.color = 'RED';
446
- node = parent;
447
- }
448
- else {
449
- if (sibling === null || sibling === void 0 ? void 0 : sibling.left)
450
- sibling.left.color = 'BLACK';
451
- if (sibling)
452
- sibling.color = parent.color;
453
- parent.color = 'BLACK';
454
- this._rightRotate(parent);
455
- node = this.root;
456
- }
457
- }
458
- else {
459
- let sibling = parent.left;
460
- if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) === 'RED') {
461
- sibling.color = 'BLACK';
462
- if (parent)
463
- parent.color = 'RED';
464
- this._rightRotate(parent);
465
- if (parent)
466
- sibling = parent.left;
467
- }
468
- if (((_d = (_c = sibling === null || sibling === void 0 ? void 0 : sibling.right) === null || _c === void 0 ? void 0 : _c.color) !== null && _d !== void 0 ? _d : 'BLACK') === 'BLACK') {
469
- if (sibling)
470
- sibling.color = 'RED';
471
- node = parent;
472
- }
473
- else {
474
- if (sibling === null || sibling === void 0 ? void 0 : sibling.right)
475
- sibling.right.color = 'BLACK';
476
- if (sibling)
477
- sibling.color = parent.color;
478
- if (parent)
479
- parent.color = 'BLACK';
480
- this._leftRotate(parent);
481
- node = this.root;
482
- }
483
- }
484
- }
485
- if (node) {
486
- node.color = 'BLACK';
487
- }
488
- }
489
- /**
490
- * (Protected) Perform a left rotation around x.
491
- * @remarks Time O(1), Space O(1)
492
- * @param x - Pivot node to rotate around.
493
- * @returns void
494
- */
495
- _leftRotate(x) {
496
- if (!x || !x.right) {
497
- return;
498
- }
499
- const y = x.right;
500
- x.right = y.left;
501
- if (this.isRealNode(y.left)) {
502
- y.left.parent = x;
503
- }
504
- y.parent = x.parent;
505
- if (!x.parent) {
506
- this._setRoot(y);
507
- }
508
- else if (x === x.parent.left) {
509
- x.parent.left = y;
510
- }
511
- else {
512
- x.parent.right = y;
513
- }
514
- y.left = x;
515
- x.parent = y;
516
- }
517
- /**
518
- * (Protected) Perform a right rotation around y.
519
- * @remarks Time O(1), Space O(1)
520
- * @param y - Pivot node to rotate around.
521
- * @returns void
522
- */
523
- _rightRotate(y) {
524
- if (!y || !y.left) {
525
- return;
526
- }
527
- const x = y.left;
528
- y.left = x.right;
529
- if (this.isRealNode(x.right)) {
530
- x.right.parent = y;
531
- }
532
- x.parent = y.parent;
533
- if (!y.parent) {
534
- this._setRoot(x);
535
- }
536
- else if (y === y.parent.left) {
537
- y.parent.left = x;
538
- }
539
- else {
540
- y.parent.right = x;
541
- }
542
- x.right = y;
543
- y.parent = x;
544
- }
545
- }
546
- exports.RedBlackTree = RedBlackTree;
@@ -1,160 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import type { SegmentTreeNodeVal } from '../../types';
9
- export declare class SegmentTreeNode {
10
- /**
11
- * The constructor initializes the properties of a SegmentTreeNode object.
12
- * @param {number} start - The `start` parameter represents the starting index of the segment covered
13
- * by this node in a segment tree.
14
- * @param {number} end - The `end` parameter represents the end index of the segment covered by this
15
- * node in a segment tree.
16
- * @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
17
- * the segment tree node.
18
- * @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
19
- * of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
20
- */
21
- constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined);
22
- protected _start: number;
23
- /**
24
- * The function returns the value of the protected variable _start.
25
- * @returns The start value, which is of type number.
26
- */
27
- get start(): number;
28
- /**
29
- * The above function sets the value of the "start" property.
30
- * @param {number} value - The value parameter is of type number.
31
- */
32
- set start(value: number);
33
- protected _end: number;
34
- /**
35
- * The function returns the value of the protected variable `_end`.
36
- * @returns The value of the protected property `_end`.
37
- */
38
- get end(): number;
39
- /**
40
- * The above function sets the value of the "end" property.
41
- * @param {number} value - The value parameter is a number that represents the new value for the end
42
- * property.
43
- */
44
- set end(value: number);
45
- protected _value: SegmentTreeNodeVal | undefined;
46
- /**
47
- * The function returns the value of a segment tree node.
48
- * @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
49
- */
50
- get value(): SegmentTreeNodeVal | undefined;
51
- /**
52
- * The function sets the value of a segment tree node.
53
- * @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
54
- * `SegmentTreeNodeVal` or `undefined`.
55
- */
56
- set value(value: SegmentTreeNodeVal | undefined);
57
- protected _sum: number;
58
- /**
59
- * The function returns the value of the sum property.
60
- * @returns The method is returning the value of the variable `_sum`.
61
- */
62
- get sum(): number;
63
- /**
64
- * The above function sets the value of the sum property.
65
- * @param {number} value - The parameter "value" is of type "number".
66
- */
67
- set sum(value: number);
68
- protected _left: SegmentTreeNode | undefined;
69
- /**
70
- * The function returns the left child of a segment tree node.
71
- * @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
72
- * `SegmentTreeNode` or `undefined`.
73
- */
74
- get left(): SegmentTreeNode | undefined;
75
- /**
76
- * The function sets the value of the left property of a SegmentTreeNode object.
77
- * @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
78
- * undefined.
79
- */
80
- set left(value: SegmentTreeNode | undefined);
81
- protected _right: SegmentTreeNode | undefined;
82
- /**
83
- * The function returns the right child of a segment tree node.
84
- * @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
85
- */
86
- get right(): SegmentTreeNode | undefined;
87
- /**
88
- * The function sets the right child of a segment tree node.
89
- * @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
90
- * undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
91
- * value.
92
- */
93
- set right(value: SegmentTreeNode | undefined);
94
- }
95
- export declare class SegmentTree {
96
- /**
97
- * The constructor initializes the values, start, end, and root properties of an object.
98
- * @param {number[]} values - An array of numbers that will be used to build a binary search tree.
99
- * @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
100
- * be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
101
- * the beginning of the array.
102
- * @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
103
- * included in the range. If not provided, it defaults to the index of the last element in the "values" array.
104
- */
105
- constructor(values: number[], start?: number, end?: number);
106
- protected _values: number[];
107
- /**
108
- * The function returns an array of numbers.
109
- * @returns An array of numbers is being returned.
110
- */
111
- get values(): number[];
112
- protected _start: number;
113
- /**
114
- * The function returns the value of the protected variable _start.
115
- * @returns The start value, which is of type number.
116
- */
117
- get start(): number;
118
- protected _end: number;
119
- /**
120
- * The function returns the value of the protected variable `_end`.
121
- * @returns The value of the protected property `_end`.
122
- */
123
- get end(): number;
124
- protected _root: SegmentTreeNode | undefined;
125
- /**
126
- * The function returns the root node of a segment tree.
127
- * @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
128
- */
129
- get root(): SegmentTreeNode | undefined;
130
- /**
131
- * The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
132
- * the sum of values to each segment.
133
- * @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
134
- * building the segment tree.
135
- * @param {number} end - The "end" parameter represents the ending index of the segment or range for which we want to
136
- * build a segment tree.
137
- * @returns a SegmentTreeNode object.
138
- */
139
- build(start: number, end: number): SegmentTreeNode;
140
- /**
141
- * The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
142
- * @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
143
- * updated.
144
- * @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
145
- * the `SegmentTreeNode` at the specified `index`.
146
- * @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
147
- * property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
148
- * cur.value = value;` and pass a value for `value` in the
149
- * @returns The function does not return anything.
150
- */
151
- updateNode(index: number, sum: number, value?: SegmentTreeNodeVal): void;
152
- /**
153
- * The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
154
- * @param {number} indexA - The starting index of the range for which you want to calculate the sum.
155
- * @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
156
- * calculate the sum.
157
- * @returns The function `querySumByRange` returns a number.
158
- */
159
- querySumByRange(indexA: number, indexB: number): number;
160
- }