binary-tree-typed 2.5.2 → 2.6.0

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 (63) hide show
  1. package/dist/cjs/index.cjs +320 -55
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +320 -55
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +320 -55
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +320 -55
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
  10. package/dist/types/data-structures/base/linear-base.d.ts +6 -0
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +191 -15
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +171 -3
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1061 -167
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
  24. package/dist/types/data-structures/heap/heap.d.ts +140 -12
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +150 -2
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +171 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +97 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +72 -2
  32. package/dist/types/data-structures/trie/trie.d.ts +84 -0
  33. package/dist/types/interfaces/binary-tree.d.ts +2 -3
  34. package/dist/umd/binary-tree-typed.js +320 -55
  35. package/dist/umd/binary-tree-typed.js.map +1 -1
  36. package/dist/umd/binary-tree-typed.min.js +5 -5
  37. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  38. package/package.json +2 -2
  39. package/src/data-structures/base/iterable-element-base.ts +32 -0
  40. package/src/data-structures/base/linear-base.ts +11 -0
  41. package/src/data-structures/binary-tree/avl-tree.ts +88 -5
  42. package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
  43. package/src/data-structures/binary-tree/binary-tree.ts +242 -81
  44. package/src/data-structures/binary-tree/bst.ts +173 -7
  45. package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
  46. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  47. package/src/data-structures/binary-tree/tree-map.ts +948 -36
  48. package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
  49. package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
  50. package/src/data-structures/binary-tree/tree-set.ts +1260 -251
  51. package/src/data-structures/graph/directed-graph.ts +71 -1
  52. package/src/data-structures/graph/undirected-graph.ts +64 -1
  53. package/src/data-structures/hash/hash-map.ts +100 -12
  54. package/src/data-structures/heap/heap.ts +149 -19
  55. package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
  56. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  57. package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
  58. package/src/data-structures/matrix/matrix.ts +56 -0
  59. package/src/data-structures/queue/deque.ts +187 -0
  60. package/src/data-structures/queue/queue.ts +109 -0
  61. package/src/data-structures/stack/stack.ts +75 -5
  62. package/src/data-structures/trie/trie.ts +84 -0
  63. package/src/interfaces/binary-tree.ts +1 -9
@@ -280,6 +280,9 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
280
280
 
281
281
 
282
282
 
283
+
284
+
285
+
283
286
 
284
287
 
285
288
 
@@ -300,6 +303,35 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
300
303
  * console.log(deque.length); // 5;
301
304
  */
302
305
 
306
+ /**
307
+ * Peek at the front element without removing it (alias for `first`).
308
+ * @remarks Time O(1), Space O(1)
309
+ * @returns Front element or undefined.
310
+ */
311
+ peek(): E | undefined {
312
+ return this.first;
313
+ }
314
+
315
+ /**
316
+ * Deque peek at both ends
317
+
318
+
319
+
320
+ * @example
321
+ * // Deque peek at both ends
322
+ * const deque = new Deque<number>([10, 20, 30, 40, 50]);
323
+ *
324
+ * // Get first element without removing
325
+ * const first = deque.at(0);
326
+ * console.log(first); // 10;
327
+ *
328
+ * // Get last element without removing
329
+ * const last = deque.at(deque.length - 1);
330
+ * console.log(last); // 50;
331
+ *
332
+ * // Length unchanged
333
+ * console.log(deque.length); // 5;
334
+ */
303
335
  get first(): E | undefined {
304
336
  if (this._length === 0) return;
305
337
  return this._buckets[this._bucketFirst][this._firstInBucket];
@@ -336,6 +368,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
336
368
 
337
369
 
338
370
 
371
+
372
+
373
+
374
+
375
+
376
+
377
+
339
378
 
340
379
 
341
380
 
@@ -410,6 +449,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
410
449
 
411
450
 
412
451
 
452
+
453
+
454
+
455
+
456
+
457
+
458
+
413
459
 
414
460
 
415
461
 
@@ -488,6 +534,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
488
534
 
489
535
 
490
536
 
537
+
538
+
539
+
540
+
541
+
542
+
543
+
491
544
 
492
545
 
493
546
 
@@ -553,6 +606,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
553
606
 
554
607
 
555
608
 
609
+
610
+
611
+
612
+
613
+
614
+
615
+
556
616
 
557
617
 
558
618
 
@@ -619,6 +679,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
619
679
 
620
680
 
621
681
 
682
+
683
+
684
+
685
+
686
+
687
+
688
+
622
689
 
623
690
 
624
691
 
@@ -730,6 +797,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
730
797
 
731
798
 
732
799
 
800
+
801
+
802
+
803
+
804
+
805
+
806
+
733
807
 
734
808
 
735
809
 
@@ -777,6 +851,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
777
851
 
778
852
 
779
853
 
854
+
855
+
856
+
857
+
858
+
859
+
860
+
780
861
 
781
862
 
782
863
 
@@ -828,6 +909,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
828
909
 
829
910
 
830
911
 
912
+
913
+
914
+
915
+
916
+
917
+
918
+
831
919
 
832
920
 
833
921
 
@@ -1052,6 +1140,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
1052
1140
 
1053
1141
 
1054
1142
 
1143
+
1144
+
1145
+
1146
+
1147
+
1148
+
1149
+
1055
1150
 
1056
1151
 
1057
1152
 
@@ -1148,6 +1243,10 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
1148
1243
 
1149
1244
 
1150
1245
 
1246
+
1247
+
1248
+
1249
+
1151
1250
 
1152
1251
 
1153
1252
 
@@ -1173,6 +1272,66 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
1173
1272
  * console.log(backward); // ['D', 'C', 'B', 'A'];
1174
1273
  */
1175
1274
 
1275
+ /**
1276
+ * Find the last value matching a predicate (scans back-to-front).
1277
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
1278
+ * @param predicate - Function called with (value, index, deque).
1279
+ * @returns Matching value or undefined.
1280
+ * @example
1281
+ * // Find last matching value
1282
+ * const d = new Deque([1, 2, 3, 4, 5]);
1283
+ * console.log(d.findLast(v => v > 2)); // 5;
1284
+ * console.log(d.findLast(v => v % 2 === 0)); // 4;
1285
+ */
1286
+ findLast(predicate: (value: E, index: number, deque: this) => boolean): E | undefined {
1287
+ for (let i = this.length - 1; i >= 0; i--) {
1288
+ const val = this.at(i)!;
1289
+ if (predicate(val, i, this)) return val;
1290
+ }
1291
+ return undefined;
1292
+ }
1293
+
1294
+ /**
1295
+ * Find the index of the last value matching a predicate (scans back-to-front).
1296
+ * @remarks Provided for familiarity when migrating from Array. Time O(n), Space O(1).
1297
+ * @param predicate - Function called with (value, index, deque).
1298
+ * @returns Matching index, or -1 if not found.
1299
+ * @example
1300
+ * // Find last matching index
1301
+ * const d = new Deque([10, 20, 30, 20, 10]);
1302
+ * console.log(d.findLastIndex(v => v === 20)); // 3;
1303
+ * console.log(d.findLastIndex(v => v === 10)); // 4;
1304
+ */
1305
+ findLastIndex(predicate: (value: E, index: number, deque: this) => boolean): number {
1306
+ for (let i = this.length - 1; i >= 0; i--) {
1307
+ if (predicate(this.at(i)!, i, this)) return i;
1308
+ }
1309
+ return -1;
1310
+ }
1311
+
1312
+ /**
1313
+ * Deque for...of iteration and reverse
1314
+
1315
+
1316
+ * @example
1317
+ * // Deque for...of iteration and reverse
1318
+ * const deque = new Deque<string>(['A', 'B', 'C', 'D']);
1319
+ *
1320
+ * // Iterate forward
1321
+ * const forward: string[] = [];
1322
+ * for (const item of deque) {
1323
+ * forward.push(item);
1324
+ * }
1325
+ * console.log(forward); // ['A', 'B', 'C', 'D'];
1326
+ *
1327
+ * // Reverse the deque
1328
+ * deque.reverse();
1329
+ * const backward: string[] = [];
1330
+ * for (const item of deque) {
1331
+ * backward.push(item);
1332
+ * }
1333
+ * console.log(backward); // ['D', 'C', 'B', 'A'];
1334
+ */
1176
1335
  reverse(): this {
1177
1336
  this._buckets.reverse().forEach(function (bucket) {
1178
1337
  bucket.reverse();
@@ -1265,6 +1424,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
1265
1424
 
1266
1425
 
1267
1426
 
1427
+
1428
+
1429
+
1430
+
1431
+
1432
+
1433
+
1268
1434
 
1269
1435
 
1270
1436
 
@@ -1338,6 +1504,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
1338
1504
 
1339
1505
 
1340
1506
 
1507
+
1508
+
1509
+
1510
+
1511
+
1512
+
1513
+
1341
1514
 
1342
1515
 
1343
1516
 
@@ -1394,6 +1567,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
1394
1567
 
1395
1568
 
1396
1569
 
1570
+
1571
+
1572
+
1573
+
1574
+
1575
+
1576
+
1397
1577
 
1398
1578
 
1399
1579
 
@@ -1472,6 +1652,13 @@ export class Deque<E = any, R = any> extends LinearBase<E, R> {
1472
1652
 
1473
1653
 
1474
1654
 
1655
+
1656
+
1657
+
1658
+
1659
+
1660
+
1661
+
1475
1662
 
1476
1663
 
1477
1664
 
@@ -184,6 +184,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
184
184
 
185
185
 
186
186
 
187
+
188
+
189
+
190
+
191
+
192
+
193
+
187
194
 
188
195
 
189
196
 
@@ -236,6 +243,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
236
243
 
237
244
 
238
245
 
246
+
247
+
248
+
249
+
250
+
251
+
252
+
239
253
 
240
254
 
241
255
 
@@ -255,6 +269,15 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
255
269
  return this.length > 0 ? this.elements[this._offset] : undefined;
256
270
  }
257
271
 
272
+ /**
273
+ * Peek at the front element without removing it (alias for `first`).
274
+ * @remarks Time O(1), Space O(1)
275
+ * @returns Front element or undefined.
276
+ */
277
+ peek(): E | undefined {
278
+ return this.first;
279
+ }
280
+
258
281
  /**
259
282
  * Get the last element (back) without removing it.
260
283
  * @remarks Time O(1), Space O(1)
@@ -308,6 +331,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
308
331
 
309
332
 
310
333
 
334
+
335
+
336
+
337
+
338
+
339
+
340
+
311
341
 
312
342
 
313
343
 
@@ -372,6 +402,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
372
402
 
373
403
 
374
404
 
405
+
406
+
407
+
408
+
409
+
410
+
411
+
375
412
 
376
413
 
377
414
 
@@ -445,6 +482,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
445
482
 
446
483
 
447
484
 
485
+
486
+
487
+
488
+
489
+
490
+
491
+
448
492
 
449
493
 
450
494
 
@@ -506,6 +550,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
506
550
 
507
551
 
508
552
 
553
+
554
+
555
+
556
+
557
+
558
+
559
+
509
560
 
510
561
 
511
562
 
@@ -560,6 +611,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
560
611
 
561
612
 
562
613
 
614
+
615
+
616
+
617
+
618
+
619
+
620
+
563
621
 
564
622
 
565
623
 
@@ -622,6 +680,22 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
622
680
  return true;
623
681
  }
624
682
 
683
+ /**
684
+ * Delete the first element that satisfies a predicate.
685
+ * @remarks Time O(N), Space O(N)
686
+ * @param predicate - Function (value, index, queue) → boolean to decide deletion.
687
+ * @returns True if a match was removed.
688
+ */
689
+ deleteWhere(predicate: (value: E, index: number, queue: this) => boolean): boolean {
690
+ for (let i = 0; i < this.length; i++) {
691
+ if (predicate(this._elements[this._offset + i], i, this)) {
692
+ this.deleteAt(i);
693
+ return true;
694
+ }
695
+ }
696
+ return false;
697
+ }
698
+
625
699
  /**
626
700
  * Reverse the queue in-place by compacting then reversing.
627
701
  * @remarks Time O(N), Space O(N)
@@ -663,6 +737,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
663
737
 
664
738
 
665
739
 
740
+
741
+
742
+
743
+
744
+
745
+
746
+
666
747
 
667
748
 
668
749
 
@@ -711,6 +792,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
711
792
 
712
793
 
713
794
 
795
+
796
+
797
+
798
+
799
+
800
+
801
+
714
802
 
715
803
 
716
804
 
@@ -788,6 +876,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
788
876
 
789
877
 
790
878
 
879
+
880
+
881
+
882
+
883
+
884
+
885
+
791
886
 
792
887
 
793
888
 
@@ -843,6 +938,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
843
938
 
844
939
 
845
940
 
941
+
942
+
943
+
944
+
945
+
946
+
947
+
846
948
 
847
949
 
848
950
 
@@ -902,6 +1004,13 @@ export class Queue<E = any, R = any> extends LinearBase<E, R> {
902
1004
 
903
1005
 
904
1006
 
1007
+
1008
+
1009
+
1010
+
1011
+
1012
+
1013
+
905
1014
 
906
1015
 
907
1016
 
@@ -189,6 +189,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
189
189
 
190
190
 
191
191
 
192
+
193
+
194
+
195
+
196
+
197
+
198
+
192
199
 
193
200
 
194
201
 
@@ -257,6 +264,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
257
264
 
258
265
 
259
266
 
267
+
268
+
269
+
270
+
271
+
272
+
273
+
260
274
 
261
275
 
262
276
 
@@ -308,6 +322,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
308
322
 
309
323
 
310
324
 
325
+
326
+
327
+
328
+
329
+
330
+
331
+
311
332
 
312
333
 
313
334
 
@@ -359,6 +380,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
359
380
 
360
381
 
361
382
 
383
+
384
+
385
+
386
+
387
+
388
+
389
+
362
390
 
363
391
 
364
392
 
@@ -419,6 +447,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
419
447
 
420
448
 
421
449
 
450
+
451
+
452
+
453
+
454
+
455
+
456
+
422
457
 
423
458
 
424
459
 
@@ -496,6 +531,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
496
531
 
497
532
 
498
533
 
534
+
535
+
536
+
537
+
538
+
539
+
540
+
499
541
 
500
542
 
501
543
 
@@ -513,20 +555,20 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
513
555
 
514
556
  delete(element: E): boolean {
515
557
  const idx = this._indexOfByEquals(element);
516
- return this.deleteAt(idx);
558
+ return this.deleteAt(idx) !== undefined;
517
559
  }
518
560
 
519
561
  /**
520
562
  * Delete the element at an index.
521
563
  * @remarks Time O(N), Space O(1)
522
564
  * @param index - Zero-based index from the bottom.
523
- * @returns True if removed.
565
+ * @returns The removed element, or undefined if the index is out of range.
524
566
  */
525
567
 
526
- deleteAt(index: number): boolean {
527
- if (index < 0 || index >= this.elements.length) return false;
568
+ deleteAt(index: number): E | undefined {
569
+ if (index < 0 || index >= this.elements.length) return undefined;
528
570
  const spliced = this.elements.splice(index, 1);
529
- return spliced.length === 1;
571
+ return spliced[0];
530
572
  }
531
573
 
532
574
  /**
@@ -575,6 +617,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
575
617
 
576
618
 
577
619
 
620
+
621
+
622
+
623
+
624
+
625
+
626
+
578
627
 
579
628
 
580
629
 
@@ -623,6 +672,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
623
672
 
624
673
 
625
674
 
675
+
676
+
677
+
678
+
679
+
680
+
681
+
626
682
 
627
683
 
628
684
 
@@ -677,6 +733,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
677
733
 
678
734
 
679
735
 
736
+
737
+
738
+
739
+
740
+
741
+
742
+
680
743
 
681
744
 
682
745
 
@@ -753,6 +816,13 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R> {
753
816
 
754
817
 
755
818
 
819
+
820
+
821
+
822
+
823
+
824
+
825
+
756
826
 
757
827
 
758
828