data-structure-typed 2.5.3 → 2.6.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 (158) hide show
  1. package/.github/workflows/ci.yml +7 -2
  2. package/.github/workflows/release-package.yml +9 -2
  3. package/.husky/pre-commit +3 -0
  4. package/CHANGELOG.md +1 -1
  5. package/MIGRATION.md +48 -0
  6. package/README.md +20 -2
  7. package/README_CN.md +20 -2
  8. package/SPECIFICATION.md +24 -0
  9. package/SPECIFICATION.zh-CN.md +24 -0
  10. package/dist/cjs/binary-tree.cjs +1897 -19
  11. package/dist/cjs/graph.cjs +174 -0
  12. package/dist/cjs/hash.cjs +33 -0
  13. package/dist/cjs/heap.cjs +71 -0
  14. package/dist/cjs/index.cjs +2383 -3
  15. package/dist/cjs/linked-list.cjs +224 -2
  16. package/dist/cjs/matrix.cjs +24 -0
  17. package/dist/cjs/priority-queue.cjs +71 -0
  18. package/dist/cjs/queue.cjs +221 -1
  19. package/dist/cjs/stack.cjs +59 -0
  20. package/dist/cjs/trie.cjs +62 -0
  21. package/dist/cjs-legacy/binary-tree.cjs +1897 -19
  22. package/dist/cjs-legacy/graph.cjs +174 -0
  23. package/dist/cjs-legacy/hash.cjs +33 -0
  24. package/dist/cjs-legacy/heap.cjs +71 -0
  25. package/dist/cjs-legacy/index.cjs +2383 -3
  26. package/dist/cjs-legacy/linked-list.cjs +224 -2
  27. package/dist/cjs-legacy/matrix.cjs +24 -0
  28. package/dist/cjs-legacy/priority-queue.cjs +71 -0
  29. package/dist/cjs-legacy/queue.cjs +221 -1
  30. package/dist/cjs-legacy/stack.cjs +59 -0
  31. package/dist/cjs-legacy/trie.cjs +62 -0
  32. package/dist/esm/binary-tree.mjs +1897 -19
  33. package/dist/esm/graph.mjs +174 -0
  34. package/dist/esm/hash.mjs +33 -0
  35. package/dist/esm/heap.mjs +71 -0
  36. package/dist/esm/index.mjs +2383 -3
  37. package/dist/esm/linked-list.mjs +224 -2
  38. package/dist/esm/matrix.mjs +24 -0
  39. package/dist/esm/priority-queue.mjs +71 -0
  40. package/dist/esm/queue.mjs +221 -1
  41. package/dist/esm/stack.mjs +59 -0
  42. package/dist/esm/trie.mjs +62 -0
  43. package/dist/esm-legacy/binary-tree.mjs +1897 -19
  44. package/dist/esm-legacy/graph.mjs +174 -0
  45. package/dist/esm-legacy/hash.mjs +33 -0
  46. package/dist/esm-legacy/heap.mjs +71 -0
  47. package/dist/esm-legacy/index.mjs +2383 -3
  48. package/dist/esm-legacy/linked-list.mjs +224 -2
  49. package/dist/esm-legacy/matrix.mjs +24 -0
  50. package/dist/esm-legacy/priority-queue.mjs +71 -0
  51. package/dist/esm-legacy/queue.mjs +221 -1
  52. package/dist/esm-legacy/stack.mjs +59 -0
  53. package/dist/esm-legacy/trie.mjs +62 -0
  54. package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
  55. package/dist/types/data-structures/base/linear-base.d.ts +6 -0
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
  57. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
  58. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
  59. package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
  60. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
  61. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
  62. package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
  63. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
  64. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
  65. package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
  66. package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
  67. package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
  68. package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
  69. package/dist/types/data-structures/heap/heap.d.ts +42 -0
  70. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
  71. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
  72. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
  73. package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
  74. package/dist/types/data-structures/queue/deque.d.ts +90 -1
  75. package/dist/types/data-structures/queue/queue.d.ts +36 -0
  76. package/dist/types/data-structures/stack/stack.d.ts +30 -0
  77. package/dist/types/data-structures/trie/trie.d.ts +36 -0
  78. package/dist/umd/data-structure-typed.js +2383 -3
  79. package/dist/umd/data-structure-typed.min.js +3 -3
  80. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
  81. package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
  82. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
  83. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
  84. package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
  85. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
  86. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
  87. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
  88. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
  89. package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
  90. package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
  91. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
  92. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
  93. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
  94. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +30 -26
  95. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
  96. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
  97. package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
  98. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
  99. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
  100. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
  101. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
  102. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
  103. package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
  104. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
  105. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
  106. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
  107. package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
  108. package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
  109. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
  110. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
  111. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
  112. package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
  113. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
  114. package/jest.integration.config.js +1 -2
  115. package/package.json +51 -50
  116. package/src/common/error.ts +15 -32
  117. package/src/common/index.ts +0 -3
  118. package/src/data-structures/base/iterable-element-base.ts +32 -3
  119. package/src/data-structures/base/linear-base.ts +13 -36
  120. package/src/data-structures/binary-tree/avl-tree.ts +31 -493
  121. package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
  122. package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
  123. package/src/data-structures/binary-tree/bst.ts +158 -1010
  124. package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
  125. package/src/data-structures/binary-tree/segment-tree.ts +73 -333
  126. package/src/data-structures/binary-tree/tree-map.ts +462 -4749
  127. package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
  128. package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
  129. package/src/data-structures/binary-tree/tree-set.ts +437 -4443
  130. package/src/data-structures/graph/abstract-graph.ts +98 -167
  131. package/src/data-structures/graph/directed-graph.ts +137 -532
  132. package/src/data-structures/graph/map-graph.ts +0 -3
  133. package/src/data-structures/graph/undirected-graph.ts +132 -484
  134. package/src/data-structures/hash/hash-map.ts +154 -549
  135. package/src/data-structures/heap/heap.ts +200 -753
  136. package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
  137. package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
  138. package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
  139. package/src/data-structures/matrix/matrix.ts +179 -494
  140. package/src/data-structures/matrix/navigator.ts +0 -1
  141. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
  142. package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
  143. package/src/data-structures/priority-queue/priority-queue.ts +1 -2
  144. package/src/data-structures/queue/deque.ts +241 -807
  145. package/src/data-structures/queue/queue.ts +102 -589
  146. package/src/data-structures/stack/stack.ts +76 -475
  147. package/src/data-structures/trie/trie.ts +98 -592
  148. package/src/types/common.ts +0 -10
  149. package/src/types/data-structures/binary-tree/bst.ts +0 -7
  150. package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
  151. package/src/types/data-structures/graph/abstract-graph.ts +0 -2
  152. package/src/types/data-structures/hash/hash-map.ts +0 -3
  153. package/src/types/data-structures/hash/index.ts +0 -1
  154. package/src/types/data-structures/matrix/navigator.ts +0 -2
  155. package/src/types/utils/utils.ts +0 -7
  156. package/src/types/utils/validate-type.ts +0 -7
  157. package/src/utils/number.ts +0 -2
  158. package/src/utils/utils.ts +0 -5
@@ -29,7 +29,7 @@ import { ERR, raise } from '../../common';
29
29
  */
30
30
  export class BinaryIndexedTree implements Iterable<number> {
31
31
  protected readonly _size: number;
32
- protected _tree: number[]; // 1-indexed BIT array
32
+ protected _tree: number[]; // 1-indexed BIT array
33
33
 
34
34
  /**
35
35
  * Construct a BIT of given size (all zeros), or from an initial values array.
@@ -53,84 +53,18 @@ export class BinaryIndexedTree implements Iterable<number> {
53
53
 
54
54
  // ─── Core operations ──────────────────────────────────────────
55
55
 
56
+ get size(): number {
57
+ return this._size;
58
+ }
59
+
56
60
  /**
57
61
  * Point update: add delta to the value at index (0-based).
58
62
  * Time: O(log n)
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
- * @example
130
- * // Add delta at index
131
- * const bit = new BinaryIndexedTree([1, 2, 3, 4, 5]);
132
- * bit.update(2, 7);
133
- * console.log(bit.get(2)); // 10;
63
+ * @example
64
+ * // Add delta at index
65
+ * const bit = new BinaryIndexedTree([1, 2, 3, 4, 5]);
66
+ * bit.update(2, 7);
67
+ * console.log(bit.get(2)); // 10;
134
68
  */
135
69
  update(index: number, delta: number): void {
136
70
  this._checkIndex(index);
@@ -140,82 +74,11 @@ export class BinaryIndexedTree implements Iterable<number> {
140
74
  /**
141
75
  * Point set: set the value at index to an absolute value (0-based).
142
76
  * Time: O(log n)
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
- * @example
215
- * // Set value at index
216
- * const bit = new BinaryIndexedTree([1, 2, 3]);
217
- * bit.set(1, 10);
218
- * console.log(bit.get(1)); // 10;
77
+ * @example
78
+ * // Set value at index
79
+ * const bit = new BinaryIndexedTree([1, 2, 3]);
80
+ * bit.set(1, 10);
81
+ * console.log(bit.get(1)); // 10;
219
82
  */
220
83
  set(index: number, value: number): void {
221
84
  this._checkIndex(index);
@@ -226,81 +89,11 @@ export class BinaryIndexedTree implements Iterable<number> {
226
89
  /**
227
90
  * Get the point value at index (0-based).
228
91
  * Time: O(log n)
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
- * @example
300
- * // Get value at index
301
- * const bit = new BinaryIndexedTree([1, 2, 3]);
302
- * console.log(bit.get(0)); // 1;
303
- * console.log(bit.get(2)); // 3;
92
+ * @example
93
+ * // Get value at index
94
+ * const bit = new BinaryIndexedTree([1, 2, 3]);
95
+ * console.log(bit.get(0)); // 1;
96
+ * console.log(bit.get(2)); // 3;
304
97
  */
305
98
  get(index: number): number {
306
99
  this._checkIndex(index);
@@ -310,164 +103,25 @@ export class BinaryIndexedTree implements Iterable<number> {
310
103
  /**
311
104
  * Prefix sum query: returns sum of elements [0..index] (inclusive, 0-based).
312
105
  * Time: O(log n)
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
- * @example
385
- * // Prefix sum
386
- * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
387
- * console.log(bit.query(2)); // 6;
106
+ * @example
107
+ * // Prefix sum
108
+ * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
109
+ * console.log(bit.query(2)); // 6;
388
110
  */
389
111
  query(index: number): number {
390
112
  this._checkIndex(index);
391
113
  return this._prefixSum(index + 1);
392
114
  }
393
115
 
116
+ // ─── Binary search ───────────────────────────────────────────
117
+
394
118
  /**
395
119
  * Range sum query: returns sum of elements [start..end] (inclusive, 0-based).
396
120
  * Time: O(log n)
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
- * @example
468
- * // Range sum
469
- * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
470
- * console.log(bit.queryRange(1, 2)); // 5;
121
+ * @example
122
+ * // Range sum
123
+ * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
124
+ * console.log(bit.queryRange(1, 2)); // 5;
471
125
  */
472
126
  queryRange(start: number, end: number): number {
473
127
  this._checkIndex(start);
@@ -477,89 +131,16 @@ export class BinaryIndexedTree implements Iterable<number> {
477
131
  return this._prefixSum(end + 1) - this._prefixSum(start);
478
132
  }
479
133
 
480
- // ─── Binary search ───────────────────────────────────────────
481
-
482
134
  /**
483
135
  * Find the smallest index i such that prefix sum [0..i] >= sum.
484
136
  * Requires all values to be non-negative (behavior undefined otherwise).
485
137
  * Returns size if no such index exists.
486
138
  * Time: O(log n)
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
-
500
-
501
-
502
-
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
- * @example
559
- * // Find index with prefix sum ≥ target
560
- * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
561
- * const idx = bit.lowerBound(4);
562
- * console.log(idx); // >= 0;
139
+ * @example
140
+ * // Find index with prefix sum ≥ target
141
+ * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
142
+ * const idx = bit.lowerBound(4);
143
+ * console.log(idx); // >= 0;
563
144
  */
564
145
  lowerBound(sum: number): number {
565
146
  let pos = 0;
@@ -577,46 +158,18 @@ export class BinaryIndexedTree implements Iterable<number> {
577
158
  return pos; // 0-based
578
159
  }
579
160
 
161
+ // ─── Standard interface ──────────────────────────────────────
162
+
580
163
  /**
581
164
  * Find the smallest index i such that prefix sum [0..i] > sum.
582
165
  * Requires all values to be non-negative (behavior undefined otherwise).
583
166
  * Returns size if no such index exists.
584
167
  * Time: O(log n)
585
-
586
-
587
-
588
-
589
-
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
606
-
607
-
608
-
609
-
610
-
611
-
612
-
613
-
614
-
615
- * @example
616
- * // Find index with prefix sum > target
617
- * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
618
- * const idx = bit.upperBound(4);
619
- * console.log(idx); // >= 0;
168
+ * @example
169
+ * // Find index with prefix sum > target
170
+ * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
171
+ * const idx = bit.upperBound(4);
172
+ * console.log(idx); // >= 0;
620
173
  */
621
174
  upperBound(sum: number): number {
622
175
  let pos = 0;
@@ -634,12 +187,6 @@ export class BinaryIndexedTree implements Iterable<number> {
634
187
  return pos; // 0-based
635
188
  }
636
189
 
637
- // ─── Standard interface ──────────────────────────────────────
638
-
639
- get size(): number {
640
- return this._size;
641
- }
642
-
643
190
  isEmpty(): boolean {
644
191
  return this._size === 0;
645
192
  }
@@ -655,40 +202,10 @@ export class BinaryIndexedTree implements Iterable<number> {
655
202
  /**
656
203
  * Returns the point values as a plain array.
657
204
  * Time: O(n log n)
658
-
659
-
660
-
661
-
662
-
663
-
664
-
665
-
666
-
667
-
668
-
669
-
670
-
671
-
672
-
673
-
674
-
675
-
676
-
677
-
678
-
679
-
680
-
681
-
682
-
683
-
684
-
685
-
686
-
687
-
688
- * @example
689
- * // Convert to array
690
- * const bit = new BinaryIndexedTree([1, 2, 3]);
691
- * console.log(bit.toArray()); // [1, 2, 3];
205
+ * @example
206
+ * // Convert to array
207
+ * const bit = new BinaryIndexedTree([1, 2, 3]);
208
+ * console.log(bit.toArray()); // [1, 2, 3];
692
209
  */
693
210
  toArray(): number[] {
694
211
  const result: number[] = [];
@@ -752,7 +269,7 @@ export class BinaryIndexedTree implements Iterable<number> {
752
269
  /** 1-based point query: get exact value at pos. */
753
270
  protected _pointQuery(pos: number): number {
754
271
  let val = this._tree[pos];
755
- const lca = pos - (pos & -pos); // parent in prefix-sum sense
272
+ const lca = pos - (pos & -pos); // parent in prefix-sum sense
756
273
  pos--;
757
274
  while (pos > lca) {
758
275
  val -= this._tree[pos];