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.
- package/dist/cjs/index.cjs +771 -68
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +771 -68
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +771 -69
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +771 -69
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +9 -0
- package/dist/types/common/index.d.ts +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
- package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
- package/dist/types/data-structures/heap/heap.d.ts +336 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
- package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
- package/dist/types/data-structures/queue/deque.d.ts +364 -4
- package/dist/types/data-structures/queue/queue.d.ts +288 -0
- package/dist/types/data-structures/stack/stack.d.ts +240 -0
- package/dist/types/data-structures/trie/trie.d.ts +292 -4
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/binary-tree-typed.js +773 -71
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +5 -5
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +19 -1
- package/src/common/index.ts +1 -1
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +3 -2
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +299 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +341 -5
- package/src/data-structures/binary-tree/binary-tree.ts +606 -6
- package/src/data-structures/binary-tree/bst.ts +946 -7
- package/src/data-structures/binary-tree/red-black-tree.ts +472 -0
- package/src/data-structures/binary-tree/segment-tree.ts +145 -2
- package/src/data-structures/binary-tree/tree-map.ts +3423 -499
- package/src/data-structures/binary-tree/tree-multi-map.ts +3537 -596
- package/src/data-structures/binary-tree/tree-multi-set.ts +2855 -495
- package/src/data-structures/binary-tree/tree-set.ts +3209 -413
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +240 -0
- package/src/data-structures/graph/undirected-graph.ts +216 -0
- package/src/data-structures/hash/hash-map.ts +281 -19
- package/src/data-structures/heap/heap.ts +340 -4
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +411 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +363 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +439 -7
- package/src/data-structures/matrix/matrix.ts +202 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +365 -5
- package/src/data-structures/queue/queue.ts +288 -0
- package/src/data-structures/stack/stack.ts +240 -0
- package/src/data-structures/trie/trie.ts +295 -7
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types';
|
|
10
10
|
import { IterableElementBase } from '../base';
|
|
11
|
-
import { ERR } from '../../common';
|
|
11
|
+
import { ERR, raise } from '../../common';
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Binary heap with pluggable comparator; supports fast insertion and removal of the top element.
|
|
@@ -196,6 +196,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
196
196
|
|
|
197
197
|
|
|
198
198
|
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
199
223
|
* @example
|
|
200
224
|
* // Track heap capacity
|
|
201
225
|
* const heap = new Heap<number>();
|
|
@@ -270,6 +294,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
270
294
|
|
|
271
295
|
|
|
272
296
|
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
273
321
|
* @example
|
|
274
322
|
* // basic Heap creation and add operation
|
|
275
323
|
* // Create a min heap (default)
|
|
@@ -305,6 +353,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
305
353
|
|
|
306
354
|
|
|
307
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
|
+
|
|
308
380
|
* @example
|
|
309
381
|
* // Add multiple elements
|
|
310
382
|
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
@@ -342,6 +414,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
342
414
|
|
|
343
415
|
|
|
344
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
|
+
|
|
345
441
|
* @example
|
|
346
442
|
* // Heap with custom comparator (MaxHeap behavior)
|
|
347
443
|
* interface Task {
|
|
@@ -395,6 +491,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
395
491
|
|
|
396
492
|
|
|
397
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
|
+
|
|
398
518
|
* @example
|
|
399
519
|
* // Heap for event processing with priority
|
|
400
520
|
* interface Event {
|
|
@@ -473,6 +593,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
473
593
|
|
|
474
594
|
|
|
475
595
|
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
476
620
|
* @example
|
|
477
621
|
* // Check if heap is empty
|
|
478
622
|
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
@@ -498,6 +642,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
498
642
|
|
|
499
643
|
|
|
500
644
|
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
|
|
501
669
|
* @example
|
|
502
670
|
* // Remove all elements
|
|
503
671
|
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
@@ -528,6 +696,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
528
696
|
* @returns True if found.
|
|
529
697
|
|
|
530
698
|
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
531
723
|
* @example
|
|
532
724
|
* // Check element existence
|
|
533
725
|
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
@@ -553,6 +745,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
553
745
|
|
|
554
746
|
|
|
555
747
|
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
556
772
|
* @example
|
|
557
773
|
* // Remove specific element
|
|
558
774
|
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
@@ -628,6 +844,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
628
844
|
* @returns Array of visited elements.
|
|
629
845
|
|
|
630
846
|
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
631
871
|
* @example
|
|
632
872
|
* // Depth-first traversal
|
|
633
873
|
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
@@ -689,6 +929,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
689
929
|
|
|
690
930
|
|
|
691
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
|
+
|
|
692
956
|
* @example
|
|
693
957
|
* // Sort elements using heap
|
|
694
958
|
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
@@ -720,6 +984,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
720
984
|
|
|
721
985
|
|
|
722
986
|
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
723
1011
|
* @example
|
|
724
1012
|
* // Create independent copy
|
|
725
1013
|
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
@@ -750,6 +1038,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
750
1038
|
|
|
751
1039
|
|
|
752
1040
|
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
753
1065
|
* @example
|
|
754
1066
|
* // Filter elements
|
|
755
1067
|
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
@@ -787,6 +1099,30 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
787
1099
|
|
|
788
1100
|
|
|
789
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
|
+
|
|
790
1126
|
* @example
|
|
791
1127
|
* // Transform elements
|
|
792
1128
|
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
@@ -800,7 +1136,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
800
1136
|
thisArg?: unknown
|
|
801
1137
|
): Heap<EM, RM> {
|
|
802
1138
|
const { comparator, toElementFn, ...rest } = options ?? {};
|
|
803
|
-
if (!comparator)
|
|
1139
|
+
if (!comparator) raise(TypeError, ERR.comparatorRequired('Heap.map'));
|
|
804
1140
|
const out = this._createLike<EM, RM>([], { ...rest, comparator, toElementFn });
|
|
805
1141
|
let i = 0;
|
|
806
1142
|
for (const x of this) {
|
|
@@ -830,7 +1166,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
830
1166
|
|
|
831
1167
|
protected readonly _DEFAULT_COMPARATOR: Comparator<E> = (a: E, b: E): number => {
|
|
832
1168
|
if (typeof a === 'object' || typeof b === 'object') {
|
|
833
|
-
|
|
1169
|
+
raise(TypeError, ERR.comparatorRequired('Heap'));
|
|
834
1170
|
}
|
|
835
1171
|
if (a > b) return 1;
|
|
836
1172
|
if (a < b) return -1;
|
|
@@ -972,7 +1308,7 @@ export class FibonacciHeap<E> {
|
|
|
972
1308
|
constructor(comparator?: Comparator<E>) {
|
|
973
1309
|
this.clear();
|
|
974
1310
|
this._comparator = comparator || this._defaultComparator;
|
|
975
|
-
if (typeof this.comparator !== 'function')
|
|
1311
|
+
if (typeof this.comparator !== 'function') raise(TypeError, ERR.notAFunction('comparator', 'FibonacciHeap'));
|
|
976
1312
|
}
|
|
977
1313
|
|
|
978
1314
|
protected _root?: FibonacciHeapNode<E>;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import type { HeapOptions } from '../../types';
|
|
8
8
|
import { Heap } from './heap';
|
|
9
|
-
import { ERR } from '../../common';
|
|
9
|
+
import { ERR, raise } from '../../common';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* @template E
|
|
@@ -80,7 +80,7 @@ export class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
80
80
|
super(elements, {
|
|
81
81
|
comparator: (a: E, b: E): number => {
|
|
82
82
|
if (typeof a === 'object' || typeof b === 'object') {
|
|
83
|
-
|
|
83
|
+
raise(TypeError, ERR.comparatorRequired('MaxHeap'));
|
|
84
84
|
}
|
|
85
85
|
if (a < b) return 1;
|
|
86
86
|
if (a > b) return -1;
|