binary-tree-typed 2.5.1 → 2.5.3

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 (75) hide show
  1. package/dist/cjs/index.cjs +340 -107
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +339 -106
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +340 -108
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +339 -107
  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/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 +189 -13
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +270 -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 +1089 -161
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1243 -350
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +980 -255
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1174 -284
  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 +126 -0
  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 +127 -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/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  35. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  36. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  37. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  38. package/dist/umd/binary-tree-typed.js +337 -105
  39. package/dist/umd/binary-tree-typed.js.map +1 -1
  40. package/dist/umd/binary-tree-typed.min.js +5 -5
  41. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  42. package/package.json +2 -2
  43. package/src/common/error.ts +19 -1
  44. package/src/common/index.ts +1 -1
  45. package/src/data-structures/base/iterable-element-base.ts +3 -2
  46. package/src/data-structures/binary-tree/avl-tree.ts +99 -5
  47. package/src/data-structures/binary-tree/binary-indexed-tree.ts +102 -4
  48. package/src/data-structures/binary-tree/binary-tree.ts +239 -78
  49. package/src/data-structures/binary-tree/bst.ts +542 -13
  50. package/src/data-structures/binary-tree/red-black-tree.ts +155 -15
  51. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  52. package/src/data-structures/binary-tree/tree-map.ts +1223 -261
  53. package/src/data-structures/binary-tree/tree-multi-map.ts +939 -30
  54. package/src/data-structures/binary-tree/tree-multi-set.ts +746 -10
  55. package/src/data-structures/binary-tree/tree-set.ts +1018 -99
  56. package/src/data-structures/graph/abstract-graph.ts +2 -2
  57. package/src/data-structures/graph/directed-graph.ts +71 -1
  58. package/src/data-structures/graph/undirected-graph.ts +64 -1
  59. package/src/data-structures/hash/hash-map.ts +102 -16
  60. package/src/data-structures/heap/heap.ts +153 -23
  61. package/src/data-structures/heap/max-heap.ts +2 -2
  62. package/src/data-structures/linked-list/doubly-linked-list.ts +139 -0
  63. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  64. package/src/data-structures/linked-list/skip-linked-list.ts +131 -5
  65. package/src/data-structures/matrix/matrix.ts +65 -9
  66. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  67. package/src/data-structures/queue/deque.ts +130 -0
  68. package/src/data-structures/queue/queue.ts +109 -0
  69. package/src/data-structures/stack/stack.ts +75 -5
  70. package/src/data-structures/trie/trie.ts +86 -2
  71. package/src/interfaces/binary-tree.ts +1 -9
  72. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  73. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  74. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  75. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
@@ -8,7 +8,7 @@
8
8
 
9
9
  import type { DijkstraResult, EntryCallback, GraphOptions, VertexKey } from '../../types';
10
10
  import { uuidV4 } from '../../utils';
11
- import { ERR } from '../../common';
11
+ import { ERR, raise } from '../../common';
12
12
  import { IterableEntryBase } from '../base';
13
13
  import { IGraph } from '../../interfaces';
14
14
  import { Heap } from '../heap';
@@ -275,7 +275,7 @@ export abstract class AbstractGraph<
275
275
  const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
276
276
  return this._addEdge(newEdge);
277
277
  } else {
278
- throw new TypeError(ERR.invalidArgument('dest must be a Vertex or vertex key when srcOrEdge is an Edge.', 'Graph'));
278
+ raise(TypeError, ERR.invalidArgument('dest must be a Vertex or vertex key when srcOrEdge is an Edge.', 'Graph'));
279
279
  }
280
280
  }
281
281
  }
@@ -238,6 +238,13 @@ export class DirectedGraph<
238
238
 
239
239
 
240
240
 
241
+
242
+
243
+
244
+
245
+
246
+
247
+
241
248
 
242
249
 
243
250
 
@@ -330,6 +337,13 @@ export class DirectedGraph<
330
337
 
331
338
 
332
339
 
340
+
341
+
342
+
343
+
344
+
345
+
346
+
333
347
 
334
348
 
335
349
 
@@ -417,6 +431,13 @@ export class DirectedGraph<
417
431
 
418
432
 
419
433
 
434
+
435
+
436
+
437
+
438
+
439
+
440
+
420
441
 
421
442
 
422
443
 
@@ -504,6 +525,13 @@ export class DirectedGraph<
504
525
 
505
526
 
506
527
 
528
+
529
+
530
+
531
+
532
+
533
+
534
+
507
535
 
508
536
 
509
537
 
@@ -556,6 +584,13 @@ export class DirectedGraph<
556
584
 
557
585
 
558
586
 
587
+
588
+
589
+
590
+
591
+
592
+
593
+
559
594
 
560
595
 
561
596
 
@@ -680,6 +715,13 @@ export class DirectedGraph<
680
715
 
681
716
 
682
717
 
718
+
719
+
720
+
721
+
722
+
723
+
724
+
683
725
 
684
726
 
685
727
 
@@ -771,6 +813,13 @@ export class DirectedGraph<
771
813
 
772
814
 
773
815
 
816
+
817
+
818
+
819
+
820
+
821
+
822
+
774
823
 
775
824
 
776
825
 
@@ -819,6 +868,13 @@ export class DirectedGraph<
819
868
 
820
869
 
821
870
 
871
+
872
+
873
+
874
+
875
+
876
+
877
+
822
878
 
823
879
 
824
880
 
@@ -885,7 +941,7 @@ export class DirectedGraph<
885
941
  * Remove all vertices and edges.
886
942
  * @remarks Time O(V + E), Space O(1)
887
943
  */
888
- clear() {
944
+ clear(): void {
889
945
  this._vertexMap = new Map<VertexKey, VO>();
890
946
  this._inEdgeMap = new Map<VO, EO[]>();
891
947
  this._outEdgeMap = new Map<VO, EO[]>();
@@ -925,6 +981,13 @@ export class DirectedGraph<
925
981
 
926
982
 
927
983
 
984
+
985
+
986
+
987
+
988
+
989
+
990
+
928
991
 
929
992
 
930
993
 
@@ -1041,6 +1104,13 @@ export class DirectedGraph<
1041
1104
 
1042
1105
 
1043
1106
 
1107
+
1108
+
1109
+
1110
+
1111
+
1112
+
1113
+
1044
1114
 
1045
1115
 
1046
1116
 
@@ -247,6 +247,13 @@ export class UndirectedGraph<
247
247
 
248
248
 
249
249
 
250
+
251
+
252
+
253
+
254
+
255
+
256
+
250
257
 
251
258
 
252
259
 
@@ -335,6 +342,13 @@ export class UndirectedGraph<
335
342
 
336
343
 
337
344
 
345
+
346
+
347
+
348
+
349
+
350
+
351
+
338
352
 
339
353
 
340
354
 
@@ -418,6 +432,13 @@ export class UndirectedGraph<
418
432
 
419
433
 
420
434
 
435
+
436
+
437
+
438
+
439
+
440
+
441
+
421
442
 
422
443
 
423
444
 
@@ -525,6 +546,13 @@ export class UndirectedGraph<
525
546
 
526
547
 
527
548
 
549
+
550
+
551
+
552
+
553
+
554
+
555
+
528
556
 
529
557
 
530
558
 
@@ -577,6 +605,13 @@ export class UndirectedGraph<
577
605
 
578
606
 
579
607
 
608
+
609
+
610
+
611
+
612
+
613
+
614
+
580
615
 
581
616
 
582
617
 
@@ -664,7 +699,7 @@ export class UndirectedGraph<
664
699
  * Remove all vertices and edges.
665
700
  * @remarks Time O(V + E), Space O(1)
666
701
  */
667
- clear() {
702
+ clear(): void {
668
703
  this._vertexMap = new Map<VertexKey, VO>();
669
704
  this._edgeMap = new Map<VO, EO[]>();
670
705
  }
@@ -703,6 +738,13 @@ export class UndirectedGraph<
703
738
 
704
739
 
705
740
 
741
+
742
+
743
+
744
+
745
+
746
+
747
+
706
748
 
707
749
 
708
750
 
@@ -873,6 +915,13 @@ export class UndirectedGraph<
873
915
 
874
916
 
875
917
 
918
+
919
+
920
+
921
+
922
+
923
+
924
+
876
925
 
877
926
 
878
927
 
@@ -941,6 +990,13 @@ export class UndirectedGraph<
941
990
 
942
991
 
943
992
 
993
+
994
+
995
+
996
+
997
+
998
+
999
+
944
1000
 
945
1001
 
946
1002
 
@@ -989,6 +1045,13 @@ export class UndirectedGraph<
989
1045
 
990
1046
 
991
1047
 
1048
+
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
992
1055
 
993
1056
 
994
1057
 
@@ -15,7 +15,7 @@ import type {
15
15
  } from '../../types';
16
16
  import { IterableEntryBase } from '../base';
17
17
  import { isWeakKey, rangeCheck } from '../../utils';
18
- import { ERR } from '../../common';
18
+ import { ERR, raise } from '../../common';
19
19
 
20
20
  /**
21
21
  * Hash-based map. Supports object keys and custom hashing; offers O(1) average set/get/has.
@@ -193,6 +193,13 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
193
193
 
194
194
 
195
195
 
196
+
197
+
198
+
199
+
200
+
201
+
202
+
196
203
 
197
204
 
198
205
 
@@ -236,6 +243,13 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
236
243
 
237
244
 
238
245
 
246
+
247
+
248
+
249
+
250
+
251
+
252
+
239
253
 
240
254
 
241
255
 
@@ -320,6 +334,20 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
320
334
 
321
335
 
322
336
 
337
+
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+
350
+
323
351
 
324
352
 
325
353
 
@@ -354,7 +382,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
354
382
  * // Verify entries
355
383
  * console.log([...map.entries()]); // length: 4;
356
384
  */
357
- set(key: K, value: V): boolean {
385
+ set(key: K, value: V): this {
358
386
  if (this._isObjKey(key)) {
359
387
  if (!this.objMap.has(key)) this._size++;
360
388
  this.objMap.set(key, value);
@@ -363,7 +391,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
363
391
  if (this.store[strKey] === undefined) this._size++;
364
392
  this._store[strKey] = { key, value };
365
393
  }
366
- return true;
394
+ return this;
367
395
  }
368
396
 
369
397
  /**
@@ -393,6 +421,13 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
393
421
 
394
422
 
395
423
 
424
+
425
+
426
+
427
+
428
+
429
+
430
+
396
431
 
397
432
 
398
433
 
@@ -413,7 +448,11 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
413
448
  let key: K | undefined, value: V | undefined;
414
449
  if (this.isEntry(rawEle)) [key, value] = rawEle;
415
450
  else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
416
- if (key !== undefined && value !== undefined) results.push(this.set(key, value));
451
+ if (key !== undefined && value !== undefined) {
452
+ const sizeBefore = this._size;
453
+ this.set(key, value);
454
+ results.push(sizeBefore < this._size);
455
+ }
417
456
  }
418
457
  return results;
419
458
  }
@@ -447,6 +486,13 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
447
486
 
448
487
 
449
488
 
489
+
490
+
491
+
492
+
493
+
494
+
495
+
450
496
 
451
497
 
452
498
 
@@ -512,6 +558,13 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
512
558
 
513
559
 
514
560
 
561
+
562
+
563
+
564
+
565
+
566
+
567
+
515
568
 
516
569
 
517
570
 
@@ -562,6 +615,13 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
562
615
 
563
616
 
564
617
 
618
+
619
+
620
+
621
+
622
+
623
+
624
+
565
625
 
566
626
 
567
627
 
@@ -631,6 +691,13 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
631
691
 
632
692
 
633
693
 
694
+
695
+
696
+
697
+
698
+
699
+
700
+
634
701
 
635
702
 
636
703
 
@@ -682,6 +749,13 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
682
749
 
683
750
 
684
751
 
752
+
753
+
754
+
755
+
756
+
757
+
758
+
685
759
 
686
760
 
687
761
 
@@ -735,6 +809,13 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
735
809
 
736
810
 
737
811
 
812
+
813
+
814
+
815
+
816
+
817
+
818
+
738
819
 
739
820
 
740
821
 
@@ -770,11 +851,11 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
770
851
  * console.log(values); // contains 7;
771
852
  */
772
853
 
773
- filter(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): any {
854
+ filter(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): this {
774
855
  const out = this._createLike<K, V, [K, V]>();
775
856
  let index = 0;
776
857
  for (const [key, value] of this) if (predicate.call(thisArg, value, key, index++, this)) out.set(key, value);
777
- return out;
858
+ return out as this;
778
859
  }
779
860
 
780
861
  /**
@@ -919,9 +1000,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
919
1000
  if (this.isEntry(rawElement)) {
920
1001
  return rawElement;
921
1002
  }
922
- throw new TypeError(
923
- ERR.invalidArgument('If elements do not adhere to [key, value], provide options.toEntryFn to transform raw records.', 'HashMap')
924
- );
1003
+ raise(TypeError, ERR.invalidArgument('If elements do not adhere to [key, value], provide options.toEntryFn to transform raw records.', 'HashMap'));
925
1004
  };
926
1005
 
927
1006
  get toEntryFn() {
@@ -984,9 +1063,9 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
984
1063
  * @remarks Time O(1), Space O(1)
985
1064
  * @param key - Key.
986
1065
  * @param [value] - Value.
987
- * @returns True when the operation succeeds.
1066
+ * @returns This map (for chaining).
988
1067
  */
989
- set(key: K, value?: V): boolean {
1068
+ set(key: K, value?: V): this {
990
1069
  let node: HashMapLinkedNode<K, V | undefined> | undefined;
991
1070
  const isNewKey = !this.has(key);
992
1071
 
@@ -1022,7 +1101,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
1022
1101
  this._size++;
1023
1102
  }
1024
1103
 
1025
- return true;
1104
+ return this;
1026
1105
  }
1027
1106
 
1028
1107
  setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[] {
@@ -1031,7 +1110,11 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
1031
1110
  let key: K | undefined, value: V | undefined;
1032
1111
  if (this.isEntry(rawEle)) [key, value] = rawEle;
1033
1112
  else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
1034
- if (key !== undefined && value !== undefined) results.push(this.set(key, value));
1113
+ if (key !== undefined && value !== undefined) {
1114
+ const sizeBefore = this._size;
1115
+ this.set(key, value);
1116
+ results.push(sizeBefore < this._size);
1117
+ }
1035
1118
  }
1036
1119
  return results;
1037
1120
  }
@@ -1115,13 +1198,16 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
1115
1198
  * Delete the entry at a given index.
1116
1199
  * @remarks Time O(N), Space O(1)
1117
1200
  * @param index - Zero-based index.
1118
- * @returns True if removed.
1201
+ * @returns The removed entry [key, value].
1202
+ * @throws {RangeError} If index is out of bounds.
1119
1203
  */
1120
- deleteAt(index: number): boolean {
1204
+ deleteAt(index: number): [K, V | undefined] {
1121
1205
  rangeCheck(index, 0, this._size - 1);
1122
1206
  let node = this.head;
1123
1207
  while (index--) node = node.next;
1124
- return this._deleteNode(node);
1208
+ const entry: [K, V | undefined] = [node.key as K, node.value];
1209
+ this._deleteNode(node);
1210
+ return entry;
1125
1211
  }
1126
1212
 
1127
1213
  isEmpty(): boolean {