binary-tree-typed 2.5.0 → 2.5.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 (90) hide show
  1. package/dist/cjs/index.cjs +771 -68
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +771 -68
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +771 -69
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +771 -69
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/common/error.d.ts +9 -0
  10. package/dist/types/common/index.d.ts +1 -1
  11. package/dist/types/data-structures/base/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  13. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  14. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
  15. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
  16. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
  17. package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
  18. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
  19. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
  20. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
  21. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
  22. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
  23. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
  24. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  25. package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
  27. package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
  28. package/dist/types/data-structures/heap/heap.d.ts +336 -0
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
  31. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
  32. package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
  33. package/dist/types/data-structures/queue/deque.d.ts +364 -4
  34. package/dist/types/data-structures/queue/queue.d.ts +288 -0
  35. package/dist/types/data-structures/stack/stack.d.ts +240 -0
  36. package/dist/types/data-structures/trie/trie.d.ts +292 -4
  37. package/dist/types/interfaces/graph.d.ts +1 -1
  38. package/dist/types/types/common.d.ts +2 -2
  39. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  40. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  41. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  42. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  43. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  44. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  45. package/dist/types/types/utils/validate-type.d.ts +4 -4
  46. package/dist/umd/binary-tree-typed.js +773 -71
  47. package/dist/umd/binary-tree-typed.js.map +1 -1
  48. package/dist/umd/binary-tree-typed.min.js +5 -5
  49. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/common/error.ts +19 -1
  52. package/src/common/index.ts +1 -1
  53. package/src/data-structures/base/index.ts +1 -0
  54. package/src/data-structures/base/iterable-element-base.ts +3 -2
  55. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  56. package/src/data-structures/base/linear-base.ts +3 -3
  57. package/src/data-structures/binary-tree/avl-tree.ts +299 -0
  58. package/src/data-structures/binary-tree/binary-indexed-tree.ts +341 -5
  59. package/src/data-structures/binary-tree/binary-tree.ts +606 -6
  60. package/src/data-structures/binary-tree/bst.ts +946 -7
  61. package/src/data-structures/binary-tree/red-black-tree.ts +472 -0
  62. package/src/data-structures/binary-tree/segment-tree.ts +145 -2
  63. package/src/data-structures/binary-tree/tree-map.ts +3423 -499
  64. package/src/data-structures/binary-tree/tree-multi-map.ts +3537 -596
  65. package/src/data-structures/binary-tree/tree-multi-set.ts +2855 -495
  66. package/src/data-structures/binary-tree/tree-set.ts +3209 -413
  67. package/src/data-structures/graph/abstract-graph.ts +6 -6
  68. package/src/data-structures/graph/directed-graph.ts +240 -0
  69. package/src/data-structures/graph/undirected-graph.ts +216 -0
  70. package/src/data-structures/hash/hash-map.ts +281 -19
  71. package/src/data-structures/heap/heap.ts +340 -4
  72. package/src/data-structures/heap/max-heap.ts +2 -2
  73. package/src/data-structures/linked-list/doubly-linked-list.ts +411 -3
  74. package/src/data-structures/linked-list/singly-linked-list.ts +363 -3
  75. package/src/data-structures/linked-list/skip-linked-list.ts +439 -7
  76. package/src/data-structures/matrix/matrix.ts +202 -10
  77. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  78. package/src/data-structures/queue/deque.ts +365 -5
  79. package/src/data-structures/queue/queue.ts +288 -0
  80. package/src/data-structures/stack/stack.ts +240 -0
  81. package/src/data-structures/trie/trie.ts +295 -7
  82. package/src/interfaces/graph.ts +1 -1
  83. package/src/types/common.ts +2 -2
  84. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  85. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  86. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  87. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
  88. package/src/types/data-structures/heap/heap.ts +1 -0
  89. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  90. package/src/types/utils/validate-type.ts +4 -4
@@ -354,6 +354,102 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
354
354
 
355
355
 
356
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
+
385
+
386
+
387
+
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+
396
+
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
+
357
453
 
358
454
 
359
455
 
@@ -470,6 +566,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
470
566
  node.left = NIL;
471
567
  node.right = NIL;
472
568
  node.color = 'RED';
569
+ this._updateCountAlongPath(node);
473
570
  this._insertFixup(node);
474
571
  if (this.isRealNode(this._root)) this._root.color = 'BLACK';
475
572
  }
@@ -600,6 +697,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
600
697
  newNode.right = NIL;
601
698
  newNode.color = 'RED';
602
699
 
700
+ this._updateCountAlongPath(newNode);
603
701
  this._insertFixup(newNode);
604
702
  if (this.isRealNode(this._root)) this._root.color = 'BLACK';
605
703
  else return undefined;
@@ -788,6 +886,102 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
788
886
 
789
887
 
790
888
 
889
+
890
+
891
+
892
+
893
+
894
+
895
+
896
+
897
+
898
+
899
+
900
+
901
+
902
+
903
+
904
+
905
+
906
+
907
+
908
+
909
+
910
+
911
+
912
+
913
+
914
+
915
+
916
+
917
+
918
+
919
+
920
+
921
+
922
+
923
+
924
+
925
+
926
+
927
+
928
+
929
+
930
+
931
+
932
+
933
+
934
+
935
+
936
+
937
+
938
+
939
+
940
+
941
+
942
+
943
+
944
+
945
+
946
+
947
+
948
+
949
+
950
+
951
+
952
+
953
+
954
+
955
+
956
+
957
+
958
+
959
+
960
+
961
+
962
+
963
+
964
+
965
+
966
+
967
+
968
+
969
+
970
+
971
+
972
+
973
+
974
+
975
+
976
+
977
+
978
+
979
+
980
+
981
+
982
+
983
+
984
+
791
985
 
792
986
 
793
987
 
@@ -904,6 +1098,102 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
904
1098
 
905
1099
 
906
1100
 
1101
+
1102
+
1103
+
1104
+
1105
+
1106
+
1107
+
1108
+
1109
+
1110
+
1111
+
1112
+
1113
+
1114
+
1115
+
1116
+
1117
+
1118
+
1119
+
1120
+
1121
+
1122
+
1123
+
1124
+
1125
+
1126
+
1127
+
1128
+
1129
+
1130
+
1131
+
1132
+
1133
+
1134
+
1135
+
1136
+
1137
+
1138
+
1139
+
1140
+
1141
+
1142
+
1143
+
1144
+
1145
+
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1160
+
1161
+
1162
+
1163
+
1164
+
1165
+
1166
+
1167
+
1168
+
1169
+
1170
+
1171
+
1172
+
1173
+
1174
+
1175
+
1176
+
1177
+
1178
+
1179
+
1180
+
1181
+
1182
+
1183
+
1184
+
1185
+
1186
+
1187
+
1188
+
1189
+
1190
+
1191
+
1192
+
1193
+
1194
+
1195
+
1196
+
907
1197
 
908
1198
 
909
1199
 
@@ -993,6 +1283,9 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
993
1283
  if (this._isMapMode) this._store.delete(nodeToDelete.key);
994
1284
  this._size--;
995
1285
 
1286
+ // Update order-statistic counts from replacement up to root
1287
+ this._updateCountAlongPath(replacementNode?.parent as RedBlackTreeNode<K, V> | undefined ?? replacementNode);
1288
+
996
1289
  // Update min/max caches.
997
1290
  if (this._size <= 0) {
998
1291
  this._setMinCache(undefined);
@@ -1053,6 +1346,78 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
1053
1346
 
1054
1347
 
1055
1348
 
1349
+
1350
+
1351
+
1352
+
1353
+
1354
+
1355
+
1356
+
1357
+
1358
+
1359
+
1360
+
1361
+
1362
+
1363
+
1364
+
1365
+
1366
+
1367
+
1368
+
1369
+
1370
+
1371
+
1372
+
1373
+
1374
+
1375
+
1376
+
1377
+
1378
+
1379
+
1380
+
1381
+
1382
+
1383
+
1384
+
1385
+
1386
+
1387
+
1388
+
1389
+
1390
+
1391
+
1392
+
1393
+
1394
+
1395
+
1396
+
1397
+
1398
+
1399
+
1400
+
1401
+
1402
+
1403
+
1404
+
1405
+
1406
+
1407
+
1408
+
1409
+
1410
+
1411
+
1412
+
1413
+
1414
+
1415
+
1416
+
1417
+
1418
+
1419
+
1420
+
1056
1421
 
1057
1422
 
1058
1423
 
@@ -1094,6 +1459,102 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
1094
1459
 
1095
1460
 
1096
1461
 
1462
+
1463
+
1464
+
1465
+
1466
+
1467
+
1468
+
1469
+
1470
+
1471
+
1472
+
1473
+
1474
+
1475
+
1476
+
1477
+
1478
+
1479
+
1480
+
1481
+
1482
+
1483
+
1484
+
1485
+
1486
+
1487
+
1488
+
1489
+
1490
+
1491
+
1492
+
1493
+
1494
+
1495
+
1496
+
1497
+
1498
+
1499
+
1500
+
1501
+
1502
+
1503
+
1504
+
1505
+
1506
+
1507
+
1508
+
1509
+
1510
+
1511
+
1512
+
1513
+
1514
+
1515
+
1516
+
1517
+
1518
+
1519
+
1520
+
1521
+
1522
+
1523
+
1524
+
1525
+
1526
+
1527
+
1528
+
1529
+
1530
+
1531
+
1532
+
1533
+
1534
+
1535
+
1536
+
1537
+
1538
+
1539
+
1540
+
1541
+
1542
+
1543
+
1544
+
1545
+
1546
+
1547
+
1548
+
1549
+
1550
+
1551
+
1552
+
1553
+
1554
+
1555
+
1556
+
1557
+
1097
1558
 
1098
1559
 
1099
1560
 
@@ -1233,6 +1694,9 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
1233
1694
  node.right = NIL;
1234
1695
  node.color = 'RED';
1235
1696
 
1697
+ // Update counts along insertion path before fixup
1698
+ this._updateCountAlongPath(node);
1699
+
1236
1700
  this._insertFixup(node);
1237
1701
  return 'CREATED';
1238
1702
  }
@@ -1436,6 +1900,10 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
1436
1900
 
1437
1901
  y.left = x;
1438
1902
  x.parent = y;
1903
+
1904
+ // Update counts: x first (now child), then y (now parent)
1905
+ this._updateCount(x);
1906
+ this._updateCount(y);
1439
1907
  }
1440
1908
 
1441
1909
  /**
@@ -1468,5 +1936,9 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
1468
1936
 
1469
1937
  x.right = y;
1470
1938
  y.parent = x;
1939
+
1940
+ // Update counts: y first (now child), then x (now parent)
1941
+ this._updateCount(y);
1942
+ this._updateCount(x);
1471
1943
  }
1472
1944
  }
@@ -2,7 +2,6 @@
2
2
  * data-structure-typed
3
3
  *
4
4
  * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
5
  * @license MIT License
7
6
  */
8
7
 
@@ -90,6 +89,30 @@ export class SegmentTree<E = number> implements Iterable<E> {
90
89
 
91
90
 
92
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
+
93
116
  * @example
94
117
  * // Temperature monitoring with range queries
95
118
  * // Hourly temperatures for a day (24 readings)
@@ -141,6 +164,30 @@ export class SegmentTree<E = number> implements Iterable<E> {
141
164
 
142
165
 
143
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
+
144
191
  * @example
145
192
  * // Dynamic range sum with updates
146
193
  * // Monthly revenue data (in thousands)
@@ -187,6 +234,30 @@ export class SegmentTree<E = number> implements Iterable<E> {
187
234
 
188
235
 
189
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
+
190
261
  * @example
191
262
  * // Range sum query on an array
192
263
  * const tree = SegmentTree.sum([1, 3, 5, 7, 9, 11]);
@@ -240,6 +311,30 @@ export class SegmentTree<E = number> implements Iterable<E> {
240
311
 
241
312
 
242
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
+
243
338
  * @example
244
339
  * // Point access on segment tree
245
340
  * const st = SegmentTree.sum([10, 20, 30, 40]);
@@ -269,6 +364,30 @@ export class SegmentTree<E = number> implements Iterable<E> {
269
364
 
270
365
 
271
366
 
367
+
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+
388
+
389
+
390
+
272
391
  * @example
273
392
  * // Find rightmost position where predicate holds
274
393
  * // Prefix sums: find the rightmost index where prefix sum < 10
@@ -337,6 +456,30 @@ export class SegmentTree<E = number> implements Iterable<E> {
337
456
 
338
457
 
339
458
 
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+
340
483
  * @example
341
484
  * // Find leftmost position where predicate holds
342
485
  * const st = SegmentTree.sum([3, 1, 4, 1, 5]);
@@ -429,7 +572,7 @@ export class SegmentTree<E = number> implements Iterable<E> {
429
572
  if (i < n) {
430
573
  return { value: tree[treeSize + i++], done: false };
431
574
  }
432
- return { value: undefined as any, done: true };
575
+ return { value: undefined, done: true } as IteratorResult<E>;
433
576
  }
434
577
  };
435
578
  }