bst-typed 2.4.4 → 2.5.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 (85) hide show
  1. package/README.md +0 -30
  2. package/dist/cjs/index.cjs +1228 -544
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +1225 -541
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +1228 -545
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +1225 -542
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/bst-typed.js +1221 -537
  46. package/dist/umd/bst-typed.js.map +1 -1
  47. package/dist/umd/bst-typed.min.js +2 -2
  48. package/dist/umd/bst-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
@@ -475,6 +475,235 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
475
475
  __name(_LinearBase, "LinearBase");
476
476
  var LinearBase = _LinearBase;
477
477
 
478
+ // src/common/error.ts
479
+ var ERR = {
480
+ // Range / index
481
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
482
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
483
+ // Type / argument
484
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
485
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
486
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
487
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
488
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
489
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
490
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
491
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
492
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
493
+ // State / operation
494
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
495
+ // Matrix
496
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
497
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
498
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
499
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
500
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
501
+ };
502
+
503
+ // src/common/index.ts
504
+ var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
505
+ DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
506
+ DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
507
+ return DFSOperation2;
508
+ })(DFSOperation || {});
509
+ var _Range = class _Range {
510
+ constructor(low, high, includeLow = true, includeHigh = true) {
511
+ this.low = low;
512
+ this.high = high;
513
+ this.includeLow = includeLow;
514
+ this.includeHigh = includeHigh;
515
+ }
516
+ // Determine whether a key is within the range
517
+ isInRange(key, comparator) {
518
+ const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
519
+ const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
520
+ return lowCheck && highCheck;
521
+ }
522
+ };
523
+ __name(_Range, "Range");
524
+ var Range = _Range;
525
+
526
+ // src/data-structures/base/iterable-entry-base.ts
527
+ var _IterableEntryBase = class _IterableEntryBase {
528
+ /**
529
+ * Default iterator yielding `[key, value]` entries.
530
+ * @returns Iterator of `[K, V]`.
531
+ * @remarks Time O(n) to iterate, Space O(1)
532
+ */
533
+ *[Symbol.iterator](...args) {
534
+ yield* this._getIterator(...args);
535
+ }
536
+ /**
537
+ * Iterate over `[key, value]` pairs (may yield `undefined` values).
538
+ * @returns Iterator of `[K, V | undefined]`.
539
+ * @remarks Time O(n), Space O(1)
540
+ */
541
+ *entries() {
542
+ for (const item of this) {
543
+ yield item;
544
+ }
545
+ }
546
+ /**
547
+ * Iterate over keys only.
548
+ * @returns Iterator of keys.
549
+ * @remarks Time O(n), Space O(1)
550
+ */
551
+ *keys() {
552
+ for (const item of this) {
553
+ yield item[0];
554
+ }
555
+ }
556
+ /**
557
+ * Iterate over values only.
558
+ * @returns Iterator of values.
559
+ * @remarks Time O(n), Space O(1)
560
+ */
561
+ *values() {
562
+ for (const item of this) {
563
+ yield item[1];
564
+ }
565
+ }
566
+ /**
567
+ * Test whether all entries satisfy the predicate.
568
+ * @param predicate - `(key, value, index, self) => boolean`.
569
+ * @param thisArg - Optional `this` for callback.
570
+ * @returns `true` if all pass; otherwise `false`.
571
+ * @remarks Time O(n), Space O(1)
572
+ */
573
+ every(predicate, thisArg) {
574
+ let index = 0;
575
+ for (const item of this) {
576
+ if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
577
+ return false;
578
+ }
579
+ }
580
+ return true;
581
+ }
582
+ /**
583
+ * Test whether any entry satisfies the predicate.
584
+ * @param predicate - `(key, value, index, self) => boolean`.
585
+ * @param thisArg - Optional `this` for callback.
586
+ * @returns `true` if any passes; otherwise `false`.
587
+ * @remarks Time O(n), Space O(1)
588
+ */
589
+ some(predicate, thisArg) {
590
+ let index = 0;
591
+ for (const item of this) {
592
+ if (predicate.call(thisArg, item[1], item[0], index++, this)) {
593
+ return true;
594
+ }
595
+ }
596
+ return false;
597
+ }
598
+ /**
599
+ * Visit each entry, left-to-right.
600
+ * @param callbackfn - `(key, value, index, self) => void`.
601
+ * @param thisArg - Optional `this` for callback.
602
+ * @remarks Time O(n), Space O(1)
603
+ */
604
+ forEach(callbackfn, thisArg) {
605
+ let index = 0;
606
+ for (const item of this) {
607
+ const [key, value] = item;
608
+ callbackfn.call(thisArg, value, key, index++, this);
609
+ }
610
+ }
611
+ /**
612
+ * Find the first entry that matches a predicate.
613
+ * @param callbackfn - `(key, value, index, self) => boolean`.
614
+ * @param thisArg - Optional `this` for callback.
615
+ * @returns Matching `[key, value]` or `undefined`.
616
+ * @remarks Time O(n), Space O(1)
617
+ */
618
+ find(callbackfn, thisArg) {
619
+ let index = 0;
620
+ for (const item of this) {
621
+ const [key, value] = item;
622
+ if (callbackfn.call(thisArg, value, key, index++, this)) return item;
623
+ }
624
+ return;
625
+ }
626
+ /**
627
+ * Whether the given key exists.
628
+ * @param key - Key to test.
629
+ * @returns `true` if found; otherwise `false`.
630
+ * @remarks Time O(n) generic, Space O(1)
631
+ */
632
+ has(key) {
633
+ for (const item of this) {
634
+ const [itemKey] = item;
635
+ if (itemKey === key) return true;
636
+ }
637
+ return false;
638
+ }
639
+ /**
640
+ * Whether there exists an entry with the given value.
641
+ * @param value - Value to test.
642
+ * @returns `true` if found; otherwise `false`.
643
+ * @remarks Time O(n), Space O(1)
644
+ */
645
+ hasValue(value) {
646
+ for (const [, elementValue] of this) {
647
+ if (elementValue === value) return true;
648
+ }
649
+ return false;
650
+ }
651
+ /**
652
+ * Get the value under a key.
653
+ * @param key - Key to look up.
654
+ * @returns Value or `undefined`.
655
+ * @remarks Time O(n) generic, Space O(1)
656
+ */
657
+ get(key) {
658
+ for (const item of this) {
659
+ const [itemKey, value] = item;
660
+ if (itemKey === key) return value;
661
+ }
662
+ return;
663
+ }
664
+ /**
665
+ * Reduce entries into a single accumulator.
666
+ * @param callbackfn - `(acc, value, key, index, self) => acc`.
667
+ * @param initialValue - Initial accumulator.
668
+ * @returns Final accumulator.
669
+ * @remarks Time O(n), Space O(1)
670
+ */
671
+ reduce(callbackfn, initialValue) {
672
+ let accumulator = initialValue;
673
+ let index = 0;
674
+ for (const item of this) {
675
+ const [key, value] = item;
676
+ accumulator = callbackfn(accumulator, value, key, index++, this);
677
+ }
678
+ return accumulator;
679
+ }
680
+ /**
681
+ * Converts data structure to `[key, value]` pairs.
682
+ * @returns Array of entries.
683
+ * @remarks Time O(n), Space O(n)
684
+ */
685
+ toArray() {
686
+ return [...this];
687
+ }
688
+ /**
689
+ * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
690
+ * @returns Array of entries (default) or a string.
691
+ * @remarks Time O(n), Space O(n)
692
+ */
693
+ toVisual() {
694
+ return [...this];
695
+ }
696
+ /**
697
+ * Print a human-friendly representation to the console.
698
+ * @remarks Time O(n), Space O(n)
699
+ */
700
+ print() {
701
+ console.log(this.toVisual());
702
+ }
703
+ };
704
+ __name(_IterableEntryBase, "IterableEntryBase");
705
+ var IterableEntryBase = _IterableEntryBase;
706
+
478
707
  // src/data-structures/queue/queue.ts
479
708
  var _Queue = class _Queue extends LinearBase {
480
709
  /**
@@ -529,18 +758,52 @@ var _Queue = class _Queue extends LinearBase {
529
758
  this._autoCompactRatio = value;
530
759
  }
531
760
  /**
532
- * Get the number of elements currently in the queue.
533
- * @remarks Time O(1), Space O(1)
534
- * @returns Current length.
535
- */
761
+ * Get the number of elements currently in the queue.
762
+ * @remarks Time O(1), Space O(1)
763
+ * @returns Current length.
764
+
765
+
766
+
767
+
768
+
769
+
770
+
771
+
772
+
773
+
774
+
775
+ * @example
776
+ * // Track queue length
777
+ * const q = new Queue<number>();
778
+ * console.log(q.length); // 0;
779
+ * q.push(1);
780
+ * q.push(2);
781
+ * console.log(q.length); // 2;
782
+ */
536
783
  get length() {
537
784
  return this.elements.length - this._offset;
538
785
  }
539
786
  /**
540
- * Get the first element (front) without removing it.
541
- * @remarks Time O(1), Space O(1)
542
- * @returns Front element or undefined.
543
- */
787
+ * Get the first element (front) without removing it.
788
+ * @remarks Time O(1), Space O(1)
789
+ * @returns Front element or undefined.
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+
799
+
800
+
801
+ * @example
802
+ * // View the front element
803
+ * const q = new Queue<string>(['first', 'second', 'third']);
804
+ * console.log(q.first); // 'first';
805
+ * console.log(q.length); // 3;
806
+ */
544
807
  get first() {
545
808
  return this.length > 0 ? this.elements[this._offset] : void 0;
546
809
  }
@@ -563,19 +826,69 @@ var _Queue = class _Queue extends LinearBase {
563
826
  return new _Queue(elements);
564
827
  }
565
828
  /**
566
- * Check whether the queue is empty.
567
- * @remarks Time O(1), Space O(1)
568
- * @returns True if length is 0.
569
- */
829
+ * Check whether the queue is empty.
830
+ * @remarks Time O(1), Space O(1)
831
+ * @returns True if length is 0.
832
+
833
+
834
+
835
+
836
+
837
+
838
+
839
+
840
+
841
+
842
+
843
+ * @example
844
+ * // Queue for...of iteration and isEmpty check
845
+ * const queue = new Queue<string>(['A', 'B', 'C', 'D']);
846
+ *
847
+ * const elements: string[] = [];
848
+ * for (const item of queue) {
849
+ * elements.push(item);
850
+ * }
851
+ *
852
+ * // Verify all elements are iterated in order
853
+ * console.log(elements); // ['A', 'B', 'C', 'D'];
854
+ *
855
+ * // Process all elements
856
+ * while (queue.length > 0) {
857
+ * queue.shift();
858
+ * }
859
+ *
860
+ * console.log(queue.length); // 0;
861
+ */
570
862
  isEmpty() {
571
863
  return this.length === 0;
572
864
  }
573
865
  /**
574
- * Enqueue one element at the back.
575
- * @remarks Time O(1), Space O(1)
576
- * @param element - Element to enqueue.
577
- * @returns True on success.
578
- */
866
+ * Enqueue one element at the back.
867
+ * @remarks Time O(1), Space O(1)
868
+ * @param element - Element to enqueue.
869
+ * @returns True on success.
870
+
871
+
872
+
873
+
874
+
875
+
876
+
877
+
878
+
879
+
880
+
881
+ * @example
882
+ * // basic Queue creation and push operation
883
+ * // Create a simple Queue with initial values
884
+ * const queue = new Queue([1, 2, 3, 4, 5]);
885
+ *
886
+ * // Verify the queue maintains insertion order
887
+ * console.log([...queue]); // [1, 2, 3, 4, 5];
888
+ *
889
+ * // Check length
890
+ * console.log(queue.length); // 5;
891
+ */
579
892
  push(element) {
580
893
  this.elements.push(element);
581
894
  if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
@@ -596,10 +909,35 @@ var _Queue = class _Queue extends LinearBase {
596
909
  return ans;
597
910
  }
598
911
  /**
599
- * Dequeue one element from the front (amortized via offset).
600
- * @remarks Time O(1) amortized, Space O(1)
601
- * @returns Removed element or undefined.
602
- */
912
+ * Dequeue one element from the front (amortized via offset).
913
+ * @remarks Time O(1) amortized, Space O(1)
914
+ * @returns Removed element or undefined.
915
+
916
+
917
+
918
+
919
+
920
+
921
+
922
+
923
+
924
+
925
+
926
+ * @example
927
+ * // Queue shift and peek operations
928
+ * const queue = new Queue<number>([10, 20, 30, 40]);
929
+ *
930
+ * // Peek at the front element without removing it
931
+ * console.log(queue.first); // 10;
932
+ *
933
+ * // Remove and get the first element (FIFO)
934
+ * const first = queue.shift();
935
+ * console.log(first); // 10;
936
+ *
937
+ * // Verify remaining elements and length decreased
938
+ * console.log([...queue]); // [20, 30, 40];
939
+ * console.log(queue.length); // 3;
940
+ */
603
941
  shift() {
604
942
  if (this.length === 0) return void 0;
605
943
  const first = this.first;
@@ -608,11 +946,24 @@ var _Queue = class _Queue extends LinearBase {
608
946
  return first;
609
947
  }
610
948
  /**
611
- * Delete the first occurrence of a specific element.
612
- * @remarks Time O(N), Space O(1)
613
- * @param element - Element to remove (strict equality via Object.is).
614
- * @returns True if an element was removed.
615
- */
949
+ * Delete the first occurrence of a specific element.
950
+ * @remarks Time O(N), Space O(1)
951
+ * @param element - Element to remove (strict equality via Object.is).
952
+ * @returns True if an element was removed.
953
+
954
+
955
+
956
+
957
+
958
+
959
+
960
+
961
+ * @example
962
+ * // Remove specific element
963
+ * const q = new Queue<number>([1, 2, 3, 2]);
964
+ * q.delete(2);
965
+ * console.log(q.length); // 3;
966
+ */
616
967
  delete(element) {
617
968
  for (let i = this._offset; i < this.elements.length; i++) {
618
969
  if (Object.is(this.elements[i], element)) {
@@ -623,11 +974,24 @@ var _Queue = class _Queue extends LinearBase {
623
974
  return false;
624
975
  }
625
976
  /**
626
- * Get the element at a given logical index.
627
- * @remarks Time O(1), Space O(1)
628
- * @param index - Zero-based index from the front.
629
- * @returns Element or undefined.
630
- */
977
+ * Get the element at a given logical index.
978
+ * @remarks Time O(1), Space O(1)
979
+ * @param index - Zero-based index from the front.
980
+ * @returns Element or undefined.
981
+
982
+
983
+
984
+
985
+
986
+
987
+
988
+
989
+ * @example
990
+ * // Access element by index
991
+ * const q = new Queue<string>(['a', 'b', 'c']);
992
+ * console.log(q.at(0)); // 'a';
993
+ * console.log(q.at(2)); // 'c';
994
+ */
631
995
  at(index) {
632
996
  if (index < 0 || index >= this.length) return void 0;
633
997
  return this._elements[this._offset + index];
@@ -679,19 +1043,48 @@ var _Queue = class _Queue extends LinearBase {
679
1043
  return this;
680
1044
  }
681
1045
  /**
682
- * Remove all elements and reset offset.
683
- * @remarks Time O(1), Space O(1)
684
- * @returns void
685
- */
1046
+ * Remove all elements and reset offset.
1047
+ * @remarks Time O(1), Space O(1)
1048
+ * @returns void
1049
+
1050
+
1051
+
1052
+
1053
+
1054
+
1055
+
1056
+
1057
+
1058
+ * @example
1059
+ * // Remove all elements
1060
+ * const q = new Queue<number>([1, 2, 3]);
1061
+ * q.clear();
1062
+ * console.log(q.length); // 0;
1063
+ */
686
1064
  clear() {
687
1065
  this._elements = [];
688
1066
  this._offset = 0;
689
1067
  }
690
1068
  /**
691
- * Compact storage by discarding consumed head elements.
692
- * @remarks Time O(N), Space O(N)
693
- * @returns True when compaction performed.
694
- */
1069
+ * Compact storage by discarding consumed head elements.
1070
+ * @remarks Time O(N), Space O(N)
1071
+ * @returns True when compaction performed.
1072
+
1073
+
1074
+
1075
+
1076
+
1077
+
1078
+
1079
+
1080
+ * @example
1081
+ * // Reclaim unused memory
1082
+ * const q = new Queue<number>([1, 2, 3, 4, 5]);
1083
+ * q.shift();
1084
+ * q.shift();
1085
+ * q.compact();
1086
+ * console.log(q.length); // 3;
1087
+ */
695
1088
  compact() {
696
1089
  this._elements = this.elements.slice(this._offset);
697
1090
  this._offset = 0;
@@ -717,10 +1110,26 @@ var _Queue = class _Queue extends LinearBase {
717
1110
  return removed;
718
1111
  }
719
1112
  /**
720
- * Deep clone this queue and its parameters.
721
- * @remarks Time O(N), Space O(N)
722
- * @returns A new queue with the same content and options.
723
- */
1113
+ * Deep clone this queue and its parameters.
1114
+ * @remarks Time O(N), Space O(N)
1115
+ * @returns A new queue with the same content and options.
1116
+
1117
+
1118
+
1119
+
1120
+
1121
+
1122
+
1123
+
1124
+
1125
+ * @example
1126
+ * // Create independent copy
1127
+ * const q = new Queue<number>([1, 2, 3]);
1128
+ * const copy = q.clone();
1129
+ * copy.shift();
1130
+ * console.log(q.length); // 3;
1131
+ * console.log(copy.length); // 2;
1132
+ */
724
1133
  clone() {
725
1134
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
726
1135
  out._setAutoCompactRatio(this._autoCompactRatio);
@@ -728,325 +1137,148 @@ var _Queue = class _Queue extends LinearBase {
728
1137
  return out;
729
1138
  }
730
1139
  /**
731
- * Filter elements into a new queue of the same class.
732
- * @remarks Time O(N), Space O(N)
733
- * @param predicate - Predicate (element, index, queue) → boolean to keep element.
734
- * @param [thisArg] - Value for `this` inside the predicate.
735
- * @returns A new queue with kept elements.
736
- */
1140
+ * Filter elements into a new queue of the same class.
1141
+ * @remarks Time O(N), Space O(N)
1142
+ * @param predicate - Predicate (element, index, queue) → boolean to keep element.
1143
+ * @param [thisArg] - Value for `this` inside the predicate.
1144
+ * @returns A new queue with kept elements.
1145
+
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+ * @example
1155
+ * // Filter elements
1156
+ * const q = new Queue<number>([1, 2, 3, 4, 5]);
1157
+ * const evens = q.filter(x => x % 2 === 0);
1158
+ * console.log(evens.length); // 2;
1159
+ */
737
1160
  filter(predicate, thisArg) {
738
1161
  const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
739
- out._setAutoCompactRatio(this._autoCompactRatio);
740
- let index = 0;
741
- for (const v of this) {
742
- if (predicate.call(thisArg, v, index, this)) out.push(v);
743
- index++;
744
- }
745
- return out;
746
- }
747
- /**
748
- * Map each element to a new element in a possibly different-typed queue.
749
- * @remarks Time O(N), Space O(N)
750
- * @template EM
751
- * @template RM
752
- * @param callback - Mapping function (element, index, queue) → newElement.
753
- * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
754
- * @param [thisArg] - Value for `this` inside the callback.
755
- * @returns A new Queue with mapped elements.
756
- */
757
- map(callback, options, thisArg) {
758
- var _a, _b;
759
- const out = new this.constructor([], {
760
- toElementFn: options == null ? void 0 : options.toElementFn,
761
- maxLen: (_a = options == null ? void 0 : options.maxLen) != null ? _a : this._maxLen,
762
- autoCompactRatio: (_b = options == null ? void 0 : options.autoCompactRatio) != null ? _b : this._autoCompactRatio
763
- });
764
- let index = 0;
765
- for (const v of this)
766
- out.push(thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this));
767
- return out;
768
- }
769
- /**
770
- * Map each element to a new value of the same type.
771
- * @remarks Time O(N), Space O(N)
772
- * @param callback - Mapping function (element, index, queue) → element.
773
- * @param [thisArg] - Value for `this` inside the callback.
774
- * @returns A new queue with mapped elements (same element type).
775
- */
776
- mapSame(callback, thisArg) {
777
- var _a;
778
- const Ctor = this.constructor;
779
- const out = new Ctor([], {
780
- toElementFn: this.toElementFn,
781
- maxLen: this._maxLen,
782
- autoCompactRatio: this._autoCompactRatio
783
- });
784
- (_a = out._setAutoCompactRatio) == null ? void 0 : _a.call(out, this._autoCompactRatio);
785
- let index = 0;
786
- for (const v of this) {
787
- const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
788
- out.push(mv);
789
- }
790
- return out;
791
- }
792
- /**
793
- * (Protected) Set the internal auto-compaction ratio.
794
- * @remarks Time O(1), Space O(1)
795
- * @param value - New ratio to assign.
796
- * @returns void
797
- */
798
- _setAutoCompactRatio(value) {
799
- this._autoCompactRatio = value;
800
- }
801
- /**
802
- * (Protected) Iterate elements from front to back.
803
- * @remarks Time O(N), Space O(1)
804
- * @returns Iterator of E.
805
- */
806
- *_getIterator() {
807
- for (let i = this._offset; i < this.elements.length; i++) yield this.elements[i];
808
- }
809
- /**
810
- * (Protected) Iterate elements from back to front.
811
- * @remarks Time O(N), Space O(1)
812
- * @returns Iterator of E.
813
- */
814
- *_getReverseIterator() {
815
- for (let i = this.length - 1; i >= 0; i--) {
816
- const cur = this.at(i);
817
- if (cur !== void 0) yield cur;
818
- }
819
- }
820
- /**
821
- * (Protected) Create an empty instance of the same concrete class.
822
- * @remarks Time O(1), Space O(1)
823
- * @param [options] - Options forwarded to the constructor.
824
- * @returns An empty like-kind queue instance.
825
- */
826
- _createInstance(options) {
827
- const Ctor = this.constructor;
828
- return new Ctor([], options);
829
- }
830
- /**
831
- * (Protected) Create a like-kind queue and seed it from an iterable.
832
- * @remarks Time O(N), Space O(N)
833
- * @template EM
834
- * @template RM
835
- * @param [elements] - Iterable used to seed the new queue.
836
- * @param [options] - Options forwarded to the constructor.
837
- * @returns A like-kind Queue instance.
838
- */
839
- _createLike(elements = [], options) {
840
- const Ctor = this.constructor;
841
- return new Ctor(elements, options);
842
- }
843
- };
844
- __name(_Queue, "Queue");
845
- var Queue = _Queue;
846
-
847
- // src/data-structures/base/iterable-entry-base.ts
848
- var _IterableEntryBase = class _IterableEntryBase {
849
- /**
850
- * Default iterator yielding `[key, value]` entries.
851
- * @returns Iterator of `[K, V]`.
852
- * @remarks Time O(n) to iterate, Space O(1)
853
- */
854
- *[Symbol.iterator](...args) {
855
- yield* this._getIterator(...args);
856
- }
857
- /**
858
- * Iterate over `[key, value]` pairs (may yield `undefined` values).
859
- * @returns Iterator of `[K, V | undefined]`.
860
- * @remarks Time O(n), Space O(1)
861
- */
862
- *entries() {
863
- for (const item of this) {
864
- yield item;
865
- }
866
- }
867
- /**
868
- * Iterate over keys only.
869
- * @returns Iterator of keys.
870
- * @remarks Time O(n), Space O(1)
871
- */
872
- *keys() {
873
- for (const item of this) {
874
- yield item[0];
875
- }
876
- }
877
- /**
878
- * Iterate over values only.
879
- * @returns Iterator of values.
880
- * @remarks Time O(n), Space O(1)
881
- */
882
- *values() {
883
- for (const item of this) {
884
- yield item[1];
885
- }
886
- }
887
- /**
888
- * Test whether all entries satisfy the predicate.
889
- * @param predicate - `(key, value, index, self) => boolean`.
890
- * @param thisArg - Optional `this` for callback.
891
- * @returns `true` if all pass; otherwise `false`.
892
- * @remarks Time O(n), Space O(1)
893
- */
894
- every(predicate, thisArg) {
895
- let index = 0;
896
- for (const item of this) {
897
- if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
898
- return false;
899
- }
900
- }
901
- return true;
902
- }
903
- /**
904
- * Test whether any entry satisfies the predicate.
905
- * @param predicate - `(key, value, index, self) => boolean`.
906
- * @param thisArg - Optional `this` for callback.
907
- * @returns `true` if any passes; otherwise `false`.
908
- * @remarks Time O(n), Space O(1)
909
- */
910
- some(predicate, thisArg) {
911
- let index = 0;
912
- for (const item of this) {
913
- if (predicate.call(thisArg, item[1], item[0], index++, this)) {
914
- return true;
915
- }
916
- }
917
- return false;
918
- }
919
- /**
920
- * Visit each entry, left-to-right.
921
- * @param callbackfn - `(key, value, index, self) => void`.
922
- * @param thisArg - Optional `this` for callback.
923
- * @remarks Time O(n), Space O(1)
924
- */
925
- forEach(callbackfn, thisArg) {
926
- let index = 0;
927
- for (const item of this) {
928
- const [key, value] = item;
929
- callbackfn.call(thisArg, value, key, index++, this);
1162
+ out._setAutoCompactRatio(this._autoCompactRatio);
1163
+ let index = 0;
1164
+ for (const v of this) {
1165
+ if (predicate.call(thisArg, v, index, this)) out.push(v);
1166
+ index++;
930
1167
  }
1168
+ return out;
931
1169
  }
932
1170
  /**
933
- * Find the first entry that matches a predicate.
934
- * @param callbackfn - `(key, value, index, self) => boolean`.
935
- * @param thisArg - Optional `this` for callback.
936
- * @returns Matching `[key, value]` or `undefined`.
937
- * @remarks Time O(n), Space O(1)
938
- */
939
- find(callbackfn, thisArg) {
1171
+ * Map each element to a new element in a possibly different-typed queue.
1172
+ * @remarks Time O(N), Space O(N)
1173
+ * @template EM
1174
+ * @template RM
1175
+ * @param callback - Mapping function (element, index, queue) → newElement.
1176
+ * @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
1177
+ * @param [thisArg] - Value for `this` inside the callback.
1178
+ * @returns A new Queue with mapped elements.
1179
+
1180
+
1181
+
1182
+
1183
+
1184
+
1185
+
1186
+
1187
+ * @example
1188
+ * // Transform elements
1189
+ * const q = new Queue<number>([1, 2, 3]);
1190
+ * const doubled = q.map(x => x * 2);
1191
+ * console.log(doubled.toArray()); // [2, 4, 6];
1192
+ */
1193
+ map(callback, options, thisArg) {
1194
+ var _a, _b;
1195
+ const out = new this.constructor([], {
1196
+ toElementFn: options == null ? void 0 : options.toElementFn,
1197
+ maxLen: (_a = options == null ? void 0 : options.maxLen) != null ? _a : this._maxLen,
1198
+ autoCompactRatio: (_b = options == null ? void 0 : options.autoCompactRatio) != null ? _b : this._autoCompactRatio
1199
+ });
940
1200
  let index = 0;
941
- for (const item of this) {
942
- const [key, value] = item;
943
- if (callbackfn.call(thisArg, value, key, index++, this)) return item;
944
- }
945
- return;
1201
+ for (const v of this)
1202
+ out.push(thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this));
1203
+ return out;
946
1204
  }
947
1205
  /**
948
- * Whether the given key exists.
949
- * @param key - Key to test.
950
- * @returns `true` if found; otherwise `false`.
951
- * @remarks Time O(n) generic, Space O(1)
1206
+ * Map each element to a new value of the same type.
1207
+ * @remarks Time O(N), Space O(N)
1208
+ * @param callback - Mapping function (element, index, queue) → element.
1209
+ * @param [thisArg] - Value for `this` inside the callback.
1210
+ * @returns A new queue with mapped elements (same element type).
952
1211
  */
953
- has(key) {
954
- for (const item of this) {
955
- const [itemKey] = item;
956
- if (itemKey === key) return true;
1212
+ mapSame(callback, thisArg) {
1213
+ var _a;
1214
+ const Ctor = this.constructor;
1215
+ const out = new Ctor([], {
1216
+ toElementFn: this.toElementFn,
1217
+ maxLen: this._maxLen,
1218
+ autoCompactRatio: this._autoCompactRatio
1219
+ });
1220
+ (_a = out._setAutoCompactRatio) == null ? void 0 : _a.call(out, this._autoCompactRatio);
1221
+ let index = 0;
1222
+ for (const v of this) {
1223
+ const mv = thisArg === void 0 ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
1224
+ out.push(mv);
957
1225
  }
958
- return false;
1226
+ return out;
959
1227
  }
960
1228
  /**
961
- * Whether there exists an entry with the given value.
962
- * @param value - Value to test.
963
- * @returns `true` if found; otherwise `false`.
964
- * @remarks Time O(n), Space O(1)
1229
+ * (Protected) Set the internal auto-compaction ratio.
1230
+ * @remarks Time O(1), Space O(1)
1231
+ * @param value - New ratio to assign.
1232
+ * @returns void
965
1233
  */
966
- hasValue(value) {
967
- for (const [, elementValue] of this) {
968
- if (elementValue === value) return true;
969
- }
970
- return false;
1234
+ _setAutoCompactRatio(value) {
1235
+ this._autoCompactRatio = value;
971
1236
  }
972
1237
  /**
973
- * Get the value under a key.
974
- * @param key - Key to look up.
975
- * @returns Value or `undefined`.
976
- * @remarks Time O(n) generic, Space O(1)
1238
+ * (Protected) Iterate elements from front to back.
1239
+ * @remarks Time O(N), Space O(1)
1240
+ * @returns Iterator of E.
977
1241
  */
978
- get(key) {
979
- for (const item of this) {
980
- const [itemKey, value] = item;
981
- if (itemKey === key) return value;
982
- }
983
- return;
1242
+ *_getIterator() {
1243
+ for (let i = this._offset; i < this.elements.length; i++) yield this.elements[i];
984
1244
  }
985
1245
  /**
986
- * Reduce entries into a single accumulator.
987
- * @param callbackfn - `(acc, value, key, index, self) => acc`.
988
- * @param initialValue - Initial accumulator.
989
- * @returns Final accumulator.
990
- * @remarks Time O(n), Space O(1)
1246
+ * (Protected) Iterate elements from back to front.
1247
+ * @remarks Time O(N), Space O(1)
1248
+ * @returns Iterator of E.
991
1249
  */
992
- reduce(callbackfn, initialValue) {
993
- let accumulator = initialValue;
994
- let index = 0;
995
- for (const item of this) {
996
- const [key, value] = item;
997
- accumulator = callbackfn(accumulator, value, key, index++, this);
1250
+ *_getReverseIterator() {
1251
+ for (let i = this.length - 1; i >= 0; i--) {
1252
+ const cur = this.at(i);
1253
+ if (cur !== void 0) yield cur;
998
1254
  }
999
- return accumulator;
1000
- }
1001
- /**
1002
- * Converts data structure to `[key, value]` pairs.
1003
- * @returns Array of entries.
1004
- * @remarks Time O(n), Space O(n)
1005
- */
1006
- toArray() {
1007
- return [...this];
1008
1255
  }
1009
1256
  /**
1010
- * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
1011
- * @returns Array of entries (default) or a string.
1012
- * @remarks Time O(n), Space O(n)
1257
+ * (Protected) Create an empty instance of the same concrete class.
1258
+ * @remarks Time O(1), Space O(1)
1259
+ * @param [options] - Options forwarded to the constructor.
1260
+ * @returns An empty like-kind queue instance.
1013
1261
  */
1014
- toVisual() {
1015
- return [...this];
1262
+ _createInstance(options) {
1263
+ const Ctor = this.constructor;
1264
+ return new Ctor([], options);
1016
1265
  }
1017
1266
  /**
1018
- * Print a human-friendly representation to the console.
1019
- * @remarks Time O(n), Space O(n)
1267
+ * (Protected) Create a like-kind queue and seed it from an iterable.
1268
+ * @remarks Time O(N), Space O(N)
1269
+ * @template EM
1270
+ * @template RM
1271
+ * @param [elements] - Iterable used to seed the new queue.
1272
+ * @param [options] - Options forwarded to the constructor.
1273
+ * @returns A like-kind Queue instance.
1020
1274
  */
1021
- print() {
1022
- console.log(this.toVisual());
1023
- }
1024
- };
1025
- __name(_IterableEntryBase, "IterableEntryBase");
1026
- var IterableEntryBase = _IterableEntryBase;
1027
-
1028
- // src/common/index.ts
1029
- var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
1030
- DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
1031
- DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
1032
- return DFSOperation2;
1033
- })(DFSOperation || {});
1034
- var _Range = class _Range {
1035
- constructor(low, high, includeLow = true, includeHigh = true) {
1036
- this.low = low;
1037
- this.high = high;
1038
- this.includeLow = includeLow;
1039
- this.includeHigh = includeHigh;
1040
- }
1041
- // Determine whether a key is within the range
1042
- isInRange(key, comparator) {
1043
- const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
1044
- const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
1045
- return lowCheck && highCheck;
1275
+ _createLike(elements = [], options) {
1276
+ const Ctor = this.constructor;
1277
+ return new Ctor(elements, options);
1046
1278
  }
1047
1279
  };
1048
- __name(_Range, "Range");
1049
- var Range = _Range;
1280
+ __name(_Queue, "Queue");
1281
+ var Queue = _Queue;
1050
1282
 
1051
1283
  // src/data-structures/binary-tree/binary-tree.ts
1052
1284
  var _BinaryTreeNode = class _BinaryTreeNode {
@@ -1220,7 +1452,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1220
1452
  if (isMapMode !== void 0) this._isMapMode = isMapMode;
1221
1453
  if (isDuplicate !== void 0) this._isDuplicate = isDuplicate;
1222
1454
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
1223
- else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
1455
+ else if (toEntryFn) throw new TypeError(ERR.notAFunction("toEntryFn", "BinaryTree"));
1224
1456
  }
1225
1457
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
1226
1458
  }
@@ -1425,30 +1657,78 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1425
1657
  return isComparable(key);
1426
1658
  }
1427
1659
  /**
1428
- * Adds a new node to the tree.
1429
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
1430
- *
1431
- * @param keyNodeOrEntry - The key, node, or entry to add.
1432
- * @returns True if the addition was successful, false otherwise.
1433
- */
1660
+ * Adds a new node to the tree.
1661
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
1662
+ *
1663
+ * @param keyNodeOrEntry - The key, node, or entry to add.
1664
+ * @returns True if the addition was successful, false otherwise.
1665
+
1666
+
1667
+
1668
+
1669
+
1670
+
1671
+ * @example
1672
+ * // Add a single node
1673
+ * const tree = new BinaryTree<number>();
1674
+ * tree.add(1);
1675
+ * tree.add(2);
1676
+ * tree.add(3);
1677
+ * console.log(tree.size); // 3;
1678
+ * console.log(tree.has(1)); // true;
1679
+ */
1434
1680
  add(keyNodeOrEntry) {
1435
1681
  return this.set(keyNodeOrEntry);
1436
1682
  }
1437
1683
  /**
1438
- * Adds or updates a new node to the tree.
1439
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation sets the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
1440
- *
1441
- * @param keyNodeOrEntry - The key, node, or entry to set or update.
1442
- * @param [value] - The value, if providing just a key.
1443
- * @returns True if the addition was successful, false otherwise.
1444
- */
1684
+ * Adds or updates a new node to the tree.
1685
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation sets the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
1686
+ *
1687
+ * @param keyNodeOrEntry - The key, node, or entry to set or update.
1688
+ * @param [value] - The value, if providing just a key.
1689
+ * @returns True if the addition was successful, false otherwise.
1690
+
1691
+
1692
+
1693
+
1694
+
1695
+
1696
+
1697
+
1698
+
1699
+
1700
+
1701
+ * @example
1702
+ * // basic BinaryTree creation and insertion
1703
+ * // Create a BinaryTree with entries
1704
+ * const entries: [number, string][] = [
1705
+ * [6, 'six'],
1706
+ * [1, 'one'],
1707
+ * [2, 'two'],
1708
+ * [7, 'seven'],
1709
+ * [5, 'five'],
1710
+ * [3, 'three'],
1711
+ * [4, 'four'],
1712
+ * [9, 'nine'],
1713
+ * [8, 'eight']
1714
+ * ];
1715
+ *
1716
+ * const tree = new BinaryTree(entries);
1717
+ *
1718
+ * // Verify size
1719
+ * console.log(tree.size); // 9;
1720
+ *
1721
+ * // Add new element
1722
+ * tree.set(10, 'ten');
1723
+ * console.log(tree.size); // 10;
1724
+ */
1445
1725
  set(keyNodeOrEntry, value) {
1446
1726
  const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
1447
1727
  if (newNode === void 0) return false;
1448
1728
  if (!this._root) {
1449
1729
  this._setRoot(newNode);
1450
1730
  if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1451
- this._size = 1;
1731
+ if (newNode !== null) this._size = 1;
1452
1732
  return true;
1453
1733
  }
1454
1734
  const queue = new Queue([this._root]);
@@ -1480,29 +1760,50 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1480
1760
  potentialParent.right = newNode;
1481
1761
  }
1482
1762
  if (this._isMapMode && newNode !== null && newNode !== void 0) this._store.set(newNode.key, newNode);
1483
- this._size++;
1763
+ if (newNode !== null) this._size++;
1484
1764
  return true;
1485
1765
  }
1486
1766
  return false;
1487
1767
  }
1488
1768
  /**
1489
- * Adds multiple items to the tree.
1490
- * @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
1491
- *
1492
- * @param keysNodesEntriesOrRaws - An iterable of items to set.
1493
- * @returns An array of booleans indicating the success of each individual `set` operation.
1494
- */
1769
+ * Adds multiple items to the tree.
1770
+ * @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
1771
+ *
1772
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
1773
+ * @returns An array of booleans indicating the success of each individual `set` operation.
1774
+
1775
+
1776
+
1777
+
1778
+
1779
+
1780
+
1781
+
1782
+
1783
+ * @example
1784
+ * // Bulk add
1785
+ * const tree = new BinaryTree<number>();
1786
+ * tree.addMany([1, 2, 3, 4, 5]);
1787
+ * console.log(tree.size); // 5;
1788
+ */
1495
1789
  addMany(keysNodesEntriesOrRaws) {
1496
1790
  return this.setMany(keysNodesEntriesOrRaws);
1497
1791
  }
1498
1792
  /**
1499
- * Adds or updates multiple items to the tree.
1500
- * @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
1501
- *
1502
- * @param keysNodesEntriesOrRaws - An iterable of items to set or update.
1503
- * @param [values] - An optional parallel iterable of values.
1504
- * @returns An array of booleans indicating the success of each individual `set` operation.
1505
- */
1793
+ * Adds or updates multiple items to the tree.
1794
+ * @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
1795
+ *
1796
+ * @param keysNodesEntriesOrRaws - An iterable of items to set or update.
1797
+ * @param [values] - An optional parallel iterable of values.
1798
+ * @returns An array of booleans indicating the success of each individual `set` operation.
1799
+
1800
+
1801
+ * @example
1802
+ * // Set multiple entries
1803
+ * const tree = new BinaryTree<number, string>();
1804
+ * tree.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
1805
+ * console.log(tree.size); // 3;
1806
+ */
1506
1807
  setMany(keysNodesEntriesOrRaws, values) {
1507
1808
  const inserted = [];
1508
1809
  let valuesIterator;
@@ -1523,11 +1824,26 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1523
1824
  return inserted;
1524
1825
  }
1525
1826
  /**
1526
- * Merges another tree into this one by seting all its nodes.
1527
- * @remarks Time O(N * M), same as `setMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `set`).
1528
- *
1529
- * @param anotherTree - The tree to merge.
1530
- */
1827
+ * Merges another tree into this one by seting all its nodes.
1828
+ * @remarks Time O(N * M), same as `setMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `set`).
1829
+ *
1830
+ * @param anotherTree - The tree to merge.
1831
+
1832
+
1833
+
1834
+
1835
+
1836
+
1837
+
1838
+
1839
+
1840
+ * @example
1841
+ * // Combine trees
1842
+ * const t1 = new BinaryTree<number>([1, 2]);
1843
+ * const t2 = new BinaryTree<number>([3, 4]);
1844
+ * t1.merge(t2);
1845
+ * console.log(t1.size); // 4;
1846
+ */
1531
1847
  merge(anotherTree) {
1532
1848
  this.setMany(anotherTree, []);
1533
1849
  }
@@ -1543,12 +1859,29 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1543
1859
  this.setMany(keysNodesEntriesOrRaws, values);
1544
1860
  }
1545
1861
  /**
1546
- * Deletes a node from the tree.
1547
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
1548
- *
1549
- * @param keyNodeEntryRawOrPredicate - The node to delete.
1550
- * @returns An array containing deletion results (for compatibility with self-balancing trees).
1551
- */
1862
+ * Deletes a node from the tree.
1863
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
1864
+ *
1865
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
1866
+ * @returns An array containing deletion results (for compatibility with self-balancing trees).
1867
+
1868
+
1869
+
1870
+
1871
+
1872
+
1873
+
1874
+
1875
+
1876
+
1877
+
1878
+ * @example
1879
+ * // Remove a node
1880
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
1881
+ * tree.delete(3);
1882
+ * console.log(tree.has(3)); // false;
1883
+ * console.log(tree.size); // 4;
1884
+ */
1552
1885
  delete(keyNodeEntryRawOrPredicate) {
1553
1886
  const deletedResult = [];
1554
1887
  if (!this._root) return deletedResult;
@@ -1642,14 +1975,27 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1642
1975
  return this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
1643
1976
  }
1644
1977
  /**
1645
- * Gets the first node matching a predicate.
1646
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
1647
- *
1648
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
1649
- * @param [startNode=this._root] - The node to start the search from.
1650
- * @param [iterationType=this.iterationType] - The traversal method.
1651
- * @returns The first matching node, or undefined if not found.
1652
- */
1978
+ * Gets the first node matching a predicate.
1979
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
1980
+ *
1981
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
1982
+ * @param [startNode=this._root] - The node to start the search from.
1983
+ * @param [iterationType=this.iterationType] - The traversal method.
1984
+ * @returns The first matching node, or undefined if not found.
1985
+
1986
+
1987
+
1988
+
1989
+
1990
+
1991
+
1992
+
1993
+
1994
+ * @example
1995
+ * // Get node by key
1996
+ * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
1997
+ * console.log(tree.getNode(2)?.value); // 'child';
1998
+ */
1653
1999
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1654
2000
  if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
1655
2001
  if (!this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -1661,14 +2007,30 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1661
2007
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
1662
2008
  }
1663
2009
  /**
1664
- * Gets the value associated with a key.
1665
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(1) if in Map mode. O(N) if not in Map mode (uses `getNode`). Space O(1) if in Map mode. O(H) or O(N) otherwise.
1666
- *
1667
- * @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
1668
- * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
1669
- * @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
1670
- * @returns The associated value, or undefined.
1671
- */
2010
+ * Gets the value associated with a key.
2011
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(1) if in Map mode. O(N) if not in Map mode (uses `getNode`). Space O(1) if in Map mode. O(H) or O(N) otherwise.
2012
+ *
2013
+ * @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
2014
+ * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
2015
+ * @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
2016
+ * @returns The associated value, or undefined.
2017
+
2018
+
2019
+
2020
+
2021
+
2022
+
2023
+
2024
+
2025
+
2026
+
2027
+
2028
+ * @example
2029
+ * // Retrieve value by key
2030
+ * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
2031
+ * console.log(tree.get(2)); // 'left';
2032
+ * console.log(tree.get(99)); // undefined;
2033
+ */
1672
2034
  get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
1673
2035
  var _a, _b;
1674
2036
  if (this._isMapMode) {
@@ -1689,19 +2051,45 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1689
2051
  return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
1690
2052
  }
1691
2053
  /**
1692
- * Clears the tree of all nodes and values.
1693
- * @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
1694
- */
2054
+ * Clears the tree of all nodes and values.
2055
+ * @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
2056
+
2057
+
2058
+
2059
+
2060
+
2061
+
2062
+
2063
+
2064
+
2065
+ * @example
2066
+ * // Remove all nodes
2067
+ * const tree = new BinaryTree<number>([1, 2, 3]);
2068
+ * tree.clear();
2069
+ * console.log(tree.isEmpty()); // true;
2070
+ */
1695
2071
  clear() {
1696
2072
  this._clearNodes();
1697
2073
  if (this._isMapMode) this._clearValues();
1698
2074
  }
1699
2075
  /**
1700
- * Checks if the tree is empty.
1701
- * @remarks Time O(1), Space O(1)
1702
- *
1703
- * @returns True if the tree has no nodes, false otherwise.
1704
- */
2076
+ * Checks if the tree is empty.
2077
+ * @remarks Time O(1), Space O(1)
2078
+ *
2079
+ * @returns True if the tree has no nodes, false otherwise.
2080
+
2081
+
2082
+
2083
+
2084
+
2085
+
2086
+
2087
+
2088
+
2089
+ * @example
2090
+ * // Check empty
2091
+ * console.log(new BinaryTree().isEmpty()); // true;
2092
+ */
1705
2093
  isEmpty() {
1706
2094
  return this._size === 0;
1707
2095
  }
@@ -1716,13 +2104,27 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1716
2104
  return this.getMinHeight(startNode) + 1 >= this.getHeight(startNode);
1717
2105
  }
1718
2106
  /**
1719
- * Checks if the tree is a valid Binary Search Tree (BST).
1720
- * @remarks Time O(N), as it must visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
1721
- *
1722
- * @param [startNode=this._root] - The node to start checking from.
1723
- * @param [iterationType=this.iterationType] - The traversal method.
1724
- * @returns True if it's a valid BST, false otherwise.
1725
- */
2107
+ * Checks if the tree is a valid Binary Search Tree (BST).
2108
+ * @remarks Time O(N), as it must visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
2109
+ *
2110
+ * @param [startNode=this._root] - The node to start checking from.
2111
+ * @param [iterationType=this.iterationType] - The traversal method.
2112
+ * @returns True if it's a valid BST, false otherwise.
2113
+
2114
+
2115
+
2116
+
2117
+
2118
+
2119
+
2120
+
2121
+
2122
+ * @example
2123
+ * // Check BST property
2124
+ * const tree = new BinaryTree<number>([1, 2, 3]);
2125
+ * // BinaryTree doesn't guarantee BST order
2126
+ * console.log(typeof tree.isBST()); // 'boolean';
2127
+ */
1726
2128
  isBST(startNode = this._root, iterationType = this.iterationType) {
1727
2129
  const startNodeSired = this.ensureNode(startNode);
1728
2130
  if (!startNodeSired) return true;
@@ -1760,13 +2162,29 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1760
2162
  }
1761
2163
  }
1762
2164
  /**
1763
- * Gets the depth of a node (distance from `startNode`).
1764
- * @remarks Time O(H), where H is the depth of the `dist` node relative to `startNode`. O(N) worst-case. Space O(1).
1765
- *
1766
- * @param dist - The node to find the depth of.
1767
- * @param [startNode=this._root] - The node to measure depth from (defaults to root).
1768
- * @returns The depth (0 if `dist` is `startNode`).
1769
- */
2165
+ * Gets the depth of a node (distance from `startNode`).
2166
+ * @remarks Time O(H), where H is the depth of the `dist` node relative to `startNode`. O(N) worst-case. Space O(1).
2167
+ *
2168
+ * @param dist - The node to find the depth of.
2169
+ * @param [startNode=this._root] - The node to measure depth from (defaults to root).
2170
+ * @returns The depth (0 if `dist` is `startNode`).
2171
+
2172
+
2173
+
2174
+
2175
+
2176
+
2177
+
2178
+
2179
+
2180
+
2181
+
2182
+ * @example
2183
+ * // Get depth of a node
2184
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
2185
+ * const node = tree.getNode(4);
2186
+ * console.log(tree.getDepth(node!)); // 2;
2187
+ */
1770
2188
  getDepth(dist, startNode = this._root) {
1771
2189
  let distEnsured = this.ensureNode(dist);
1772
2190
  const beginRootEnsured = this.ensureNode(startNode);
@@ -1781,13 +2199,28 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1781
2199
  return depth;
1782
2200
  }
1783
2201
  /**
1784
- * Gets the maximum height of the tree (longest path from startNode to a leaf).
1785
- * @remarks Time O(N), as it must visit every node. Space O(H) for recursive stack (O(N) worst-case) or O(N) for iterative stack (storing node + depth).
1786
- *
1787
- * @param [startNode=this._root] - The node to start measuring from.
1788
- * @param [iterationType=this.iterationType] - The traversal method.
1789
- * @returns The height ( -1 for an empty tree, 0 for a single-node tree).
1790
- */
2202
+ * Gets the maximum height of the tree (longest path from startNode to a leaf).
2203
+ * @remarks Time O(N), as it must visit every node. Space O(H) for recursive stack (O(N) worst-case) or O(N) for iterative stack (storing node + depth).
2204
+ *
2205
+ * @param [startNode=this._root] - The node to start measuring from.
2206
+ * @param [iterationType=this.iterationType] - The traversal method.
2207
+ * @returns The height ( -1 for an empty tree, 0 for a single-node tree).
2208
+
2209
+
2210
+
2211
+
2212
+
2213
+
2214
+
2215
+
2216
+
2217
+
2218
+
2219
+ * @example
2220
+ * // Get tree height
2221
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
2222
+ * console.log(tree.getHeight()); // 2;
2223
+ */
1791
2224
  getHeight(startNode = this._root, iterationType = this.iterationType) {
1792
2225
  startNode = this.ensureNode(startNode);
1793
2226
  if (!this.isRealNode(startNode)) return -1;
@@ -2049,7 +2482,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2049
2482
  }
2050
2483
  /**
2051
2484
  * Finds all leaf nodes in the tree.
2052
- * @remarks Time O(N), visits every node. Space O(H) for recursive stack or O(N) for iterative queue.
2485
+ * @remarks Time O(N), visits every node. Space O(H) for recursive or iterative stack.
2053
2486
  *
2054
2487
  * @template C - The type of the callback function.
2055
2488
  * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each leaf node.
@@ -2072,15 +2505,15 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2072
2505
  }, "dfs");
2073
2506
  dfs(startNode);
2074
2507
  } else {
2075
- const queue = new Queue([startNode]);
2076
- while (queue.length > 0) {
2077
- const cur = queue.shift();
2508
+ const stack = [startNode];
2509
+ while (stack.length > 0) {
2510
+ const cur = stack.pop();
2078
2511
  if (this.isRealNode(cur)) {
2079
2512
  if (this.isLeaf(cur)) {
2080
2513
  leaves.push(callback(cur));
2081
2514
  }
2082
- if (this.isRealNode(cur.left)) queue.push(cur.left);
2083
- if (this.isRealNode(cur.right)) queue.push(cur.right);
2515
+ if (this.isRealNode(cur.right)) stack.push(cur.right);
2516
+ if (this.isRealNode(cur.left)) stack.push(cur.left);
2084
2517
  }
2085
2518
  }
2086
2519
  }
@@ -2223,24 +2656,53 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2223
2656
  return ans;
2224
2657
  }
2225
2658
  /**
2226
- * Clones the tree.
2227
- * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `set`, and `set` is O(M)). Space O(N) for the new tree and the BFS queue.
2228
- *
2229
- * @returns A new, cloned instance of the tree.
2230
- */
2659
+ * Clones the tree.
2660
+ * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `set`, and `set` is O(M)). Space O(N) for the new tree and the BFS queue.
2661
+ *
2662
+ * @returns A new, cloned instance of the tree.
2663
+
2664
+
2665
+
2666
+
2667
+
2668
+
2669
+
2670
+
2671
+
2672
+ * @example
2673
+ * // Deep copy
2674
+ * const tree = new BinaryTree<number>([1, 2, 3]);
2675
+ * const copy = tree.clone();
2676
+ * copy.delete(1);
2677
+ * console.log(tree.has(1)); // true;
2678
+ */
2231
2679
  clone() {
2232
2680
  const out = this._createInstance();
2233
2681
  this._clone(out);
2234
2682
  return out;
2235
2683
  }
2236
2684
  /**
2237
- * Creates a new tree containing only the entries that satisfy the predicate.
2238
- * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `set` for each item). Space O(N) for the new tree.
2239
- *
2240
- * @param predicate - A function to test each [key, value] pair.
2241
- * @param [thisArg] - `this` context for the predicate.
2242
- * @returns A new, filtered tree.
2243
- */
2685
+ * Creates a new tree containing only the entries that satisfy the predicate.
2686
+ * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `set` for each item). Space O(N) for the new tree.
2687
+ *
2688
+ * @param predicate - A function to test each [key, value] pair.
2689
+ * @param [thisArg] - `this` context for the predicate.
2690
+ * @returns A new, filtered tree.
2691
+
2692
+
2693
+
2694
+
2695
+
2696
+
2697
+
2698
+
2699
+
2700
+ * @example
2701
+ * // Filter nodes by condition
2702
+ * const tree = new BinaryTree<number>([1, 2, 3, 4]);
2703
+ * const result = tree.filter((_, key) => key > 2);
2704
+ * console.log(result.size); // 2;
2705
+ */
2244
2706
  filter(predicate, thisArg) {
2245
2707
  const out = this._createInstance();
2246
2708
  let i = 0;
@@ -2248,17 +2710,31 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2248
2710
  return out;
2249
2711
  }
2250
2712
  /**
2251
- * Creates a new tree by mapping each [key, value] pair to a new entry.
2252
- * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion. Space O(N) for the new tree.
2253
- *
2254
- * @template MK - New key type.
2255
- * @template MV - New value type.
2256
- * @template MR - New raw type.
2257
- * @param cb - A function to map each [key, value] pair.
2258
- * @param [options] - Options for the new tree.
2259
- * @param [thisArg] - `this` context for the callback.
2260
- * @returns A new, mapped tree.
2261
- */
2713
+ * Creates a new tree by mapping each [key, value] pair to a new entry.
2714
+ * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion. Space O(N) for the new tree.
2715
+ *
2716
+ * @template MK - New key type.
2717
+ * @template MV - New value type.
2718
+ * @template MR - New raw type.
2719
+ * @param cb - A function to map each [key, value] pair.
2720
+ * @param [options] - Options for the new tree.
2721
+ * @param [thisArg] - `this` context for the callback.
2722
+ * @returns A new, mapped tree.
2723
+
2724
+
2725
+
2726
+
2727
+
2728
+
2729
+
2730
+
2731
+
2732
+ * @example
2733
+ * // Transform to new tree
2734
+ * const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
2735
+ * const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
2736
+ * console.log([...mapped.values()]); // contains 11;
2737
+ */
2262
2738
  map(cb, options, thisArg) {
2263
2739
  const out = this._createLike([], options);
2264
2740
  let i = 0;
@@ -2296,12 +2772,25 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2296
2772
  return output;
2297
2773
  }
2298
2774
  /**
2299
- * Prints a visual representation of the tree to the console.
2300
- * @remarks Time O(N) (via `toVisual`). Space O(N*H) or O(N^2) (via `toVisual`).
2301
- *
2302
- * @param [options] - Options to control the output.
2303
- * @param [startNode=this._root] - The node to start printing from.
2304
- */
2775
+ * Prints a visual representation of the tree to the console.
2776
+ * @remarks Time O(N) (via `toVisual`). Space O(N*H) or O(N^2) (via `toVisual`).
2777
+ *
2778
+ * @param [options] - Options to control the output.
2779
+ * @param [startNode=this._root] - The node to start printing from.
2780
+
2781
+
2782
+
2783
+
2784
+
2785
+
2786
+
2787
+
2788
+
2789
+ * @example
2790
+ * // Display tree
2791
+ * const tree = new BinaryTree<number>([1, 2, 3]);
2792
+ * expect(() => tree.print()).not.toThrow();
2793
+ */
2305
2794
  print(options, startNode = this._root) {
2306
2795
  console.log(this.toVisual(startNode, options));
2307
2796
  }
@@ -2534,44 +3023,99 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
2534
3023
  * @returns Layout information for this subtree.
2535
3024
  */
2536
3025
  _displayAux(node, options) {
2537
- const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
2538
3026
  const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
2539
- if (node === null && !isShowNull) {
2540
- return emptyDisplayLayout;
2541
- } else if (node === void 0 && !isShowUndefined) {
2542
- return emptyDisplayLayout;
2543
- } else if (this.isNIL(node) && !isShowRedBlackNIL) {
2544
- return emptyDisplayLayout;
2545
- } else if (node !== null && node !== void 0) {
2546
- const key = node.key, line = this.isNIL(node) ? "S" : String(key), width = line.length;
2547
- return _buildNodeDisplay(
2548
- line,
2549
- width,
2550
- this._displayAux(node.left, options),
2551
- this._displayAux(node.right, options)
2552
- );
2553
- } else {
2554
- const line = node === void 0 ? "U" : "N", width = line.length;
2555
- return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
2556
- }
2557
- function _buildNodeDisplay(line, width, left, right) {
2558
- const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
2559
- const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
2560
- const firstLine = " ".repeat(Math.max(0, leftMiddle + 1)) + "_".repeat(Math.max(0, leftWidth - leftMiddle - 1)) + line + "_".repeat(Math.max(0, rightMiddle)) + " ".repeat(Math.max(0, rightWidth - rightMiddle));
2561
- const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
2562
- const mergedLines = [firstLine, secondLine];
2563
- for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
2564
- const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
2565
- const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
2566
- mergedLines.push(leftLine + " ".repeat(width) + rightLine);
3027
+ const newFrame = /* @__PURE__ */ __name((n) => ({
3028
+ node: n,
3029
+ stage: 0,
3030
+ leftLayout: emptyDisplayLayout,
3031
+ rightLayout: emptyDisplayLayout
3032
+ }), "newFrame");
3033
+ const stack = [newFrame(node)];
3034
+ let result = emptyDisplayLayout;
3035
+ const setChildResult = /* @__PURE__ */ __name((layout) => {
3036
+ if (stack.length === 0) {
3037
+ result = layout;
3038
+ return;
3039
+ }
3040
+ const parent = stack[stack.length - 1];
3041
+ if (parent.stage === 1) parent.leftLayout = layout;
3042
+ else parent.rightLayout = layout;
3043
+ }, "setChildResult");
3044
+ while (stack.length > 0) {
3045
+ const frame = stack[stack.length - 1];
3046
+ const cur = frame.node;
3047
+ if (frame.stage === 0) {
3048
+ if (this._isDisplayLeaf(cur, options)) {
3049
+ stack.pop();
3050
+ const layout = this._resolveDisplayLeaf(cur, options, emptyDisplayLayout);
3051
+ setChildResult(layout);
3052
+ continue;
3053
+ }
3054
+ frame.stage = 1;
3055
+ stack.push(newFrame(cur.left));
3056
+ } else if (frame.stage === 1) {
3057
+ frame.stage = 2;
3058
+ stack.push(newFrame(cur.right));
3059
+ } else {
3060
+ stack.pop();
3061
+ const line = this.isNIL(cur) ? "S" : String(cur.key);
3062
+ const layout = _BinaryTree._buildNodeDisplay(line, line.length, frame.leftLayout, frame.rightLayout);
3063
+ setChildResult(layout);
2567
3064
  }
2568
- return [
2569
- mergedLines,
2570
- leftWidth + width + rightWidth,
2571
- Math.max(leftHeight, rightHeight) + 2,
2572
- leftWidth + Math.floor(width / 2)
2573
- ];
2574
3065
  }
3066
+ return result;
3067
+ }
3068
+ static _buildNodeDisplay(line, width, left, right) {
3069
+ const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
3070
+ const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
3071
+ const firstLine = " ".repeat(Math.max(0, leftMiddle + 1)) + "_".repeat(Math.max(0, leftWidth - leftMiddle - 1)) + line + "_".repeat(Math.max(0, rightMiddle)) + " ".repeat(Math.max(0, rightWidth - rightMiddle));
3072
+ const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
3073
+ const mergedLines = [firstLine, secondLine];
3074
+ for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
3075
+ const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
3076
+ const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
3077
+ mergedLines.push(leftLine + " ".repeat(width) + rightLine);
3078
+ }
3079
+ return [
3080
+ mergedLines,
3081
+ leftWidth + width + rightWidth,
3082
+ Math.max(leftHeight, rightHeight) + 2,
3083
+ leftWidth + Math.floor(width / 2)
3084
+ ];
3085
+ }
3086
+ /**
3087
+ * Check if a node is a display leaf (empty, null, undefined, NIL, or real leaf).
3088
+ */
3089
+ _isDisplayLeaf(node, options) {
3090
+ const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
3091
+ if (node === null && !isShowNull) return true;
3092
+ if (node === void 0 && !isShowUndefined) return true;
3093
+ if (this.isNIL(node) && !isShowRedBlackNIL) return true;
3094
+ if (node === null || node === void 0) return true;
3095
+ const hasDisplayableLeft = this._hasDisplayableChild(node.left, options);
3096
+ const hasDisplayableRight = this._hasDisplayableChild(node.right, options);
3097
+ return !hasDisplayableLeft && !hasDisplayableRight;
3098
+ }
3099
+ _hasDisplayableChild(child, options) {
3100
+ if (child === null) return !!options.isShowNull;
3101
+ if (child === void 0) return !!options.isShowUndefined;
3102
+ if (this.isNIL(child)) return !!options.isShowRedBlackNIL;
3103
+ return true;
3104
+ }
3105
+ /**
3106
+ * Resolve a display leaf node to its layout.
3107
+ */
3108
+ _resolveDisplayLeaf(node, options, emptyDisplayLayout) {
3109
+ const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
3110
+ if (node === null && !isShowNull) return emptyDisplayLayout;
3111
+ if (node === void 0 && !isShowUndefined) return emptyDisplayLayout;
3112
+ if (this.isNIL(node) && !isShowRedBlackNIL) return emptyDisplayLayout;
3113
+ if (node !== null && node !== void 0) {
3114
+ const line2 = this.isNIL(node) ? "S" : String(node.key);
3115
+ return _BinaryTree._buildNodeDisplay(line2, line2.length, emptyDisplayLayout, emptyDisplayLayout);
3116
+ }
3117
+ const line = node === void 0 ? "U" : "N";
3118
+ return _BinaryTree._buildNodeDisplay(line, line.length, [[""], 1, 0, 0], [[""], 1, 0, 0]);
2575
3119
  }
2576
3120
  /**
2577
3121
  * (Protected) Swaps the key/value properties of two nodes.
@@ -2775,6 +3319,7 @@ var _BSTNode = class _BSTNode {
2775
3319
  *
2776
3320
  * @returns The height.
2777
3321
  */
3322
+ /* istanbul ignore next -- covered by AVLTree/RedBlackTree tests (subclass uses height) */
2778
3323
  get height() {
2779
3324
  return this._height;
2780
3325
  }
@@ -2784,6 +3329,7 @@ var _BSTNode = class _BSTNode {
2784
3329
  *
2785
3330
  * @param value - The new height.
2786
3331
  */
3332
+ /* istanbul ignore next -- covered by AVLTree/RedBlackTree tests (subclass uses height) */
2787
3333
  set height(value) {
2788
3334
  this._height = value;
2789
3335
  }
@@ -2793,6 +3339,7 @@ var _BSTNode = class _BSTNode {
2793
3339
  *
2794
3340
  * @returns The node's color.
2795
3341
  */
3342
+ /* istanbul ignore next -- covered by RedBlackTree tests (subclass uses color) */
2796
3343
  get color() {
2797
3344
  return this._color;
2798
3345
  }
@@ -2802,6 +3349,7 @@ var _BSTNode = class _BSTNode {
2802
3349
  *
2803
3350
  * @param value - The new color.
2804
3351
  */
3352
+ /* istanbul ignore next -- covered by RedBlackTree tests (subclass uses color) */
2805
3353
  set color(value) {
2806
3354
  this._color = value;
2807
3355
  }
@@ -2811,6 +3359,7 @@ var _BSTNode = class _BSTNode {
2811
3359
  *
2812
3360
  * @returns The subtree node count.
2813
3361
  */
3362
+ /* istanbul ignore next -- internal field used by subclasses */
2814
3363
  get count() {
2815
3364
  return this._count;
2816
3365
  }
@@ -2820,6 +3369,7 @@ var _BSTNode = class _BSTNode {
2820
3369
  *
2821
3370
  * @param value - The new count.
2822
3371
  */
3372
+ /* istanbul ignore next -- internal field used by subclasses */
2823
3373
  set count(value) {
2824
3374
  this._count = value;
2825
3375
  }
@@ -2974,14 +3524,39 @@ var _BST = class _BST extends BinaryTree {
2974
3524
  return super.listLevels(callback, startNode, iterationType, false);
2975
3525
  }
2976
3526
  /**
2977
- * Gets the first node matching a predicate.
2978
- * @remarks Time O(log N) if searching by key, O(N) if searching by predicate. Space O(log N) or O(N).
2979
- *
2980
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
2981
- * @param [startNode=this._root] - The node to start the search from.
2982
- * @param [iterationType=this.iterationType] - The traversal method.
2983
- * @returns The first matching node, or undefined if not found.
2984
- */
3527
+ * Gets the first node matching a predicate.
3528
+ * @remarks Time O(log N) if searching by key, O(N) if searching by predicate. Space O(log N) or O(N).
3529
+ *
3530
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3531
+ * @param [startNode=this._root] - The node to start the search from.
3532
+ * @param [iterationType=this.iterationType] - The traversal method.
3533
+ * @returns The first matching node, or undefined if not found.
3534
+
3535
+
3536
+
3537
+
3538
+
3539
+
3540
+
3541
+
3542
+
3543
+
3544
+
3545
+
3546
+
3547
+
3548
+
3549
+
3550
+
3551
+
3552
+
3553
+ * @example
3554
+ * // Get node object by key
3555
+ * const bst = new BST<number, string>([[5, 'root'], [3, 'left'], [7, 'right']]);
3556
+ * const node = bst.getNode(3);
3557
+ * console.log(node?.key); // 3;
3558
+ * console.log(node?.value); // 'left';
3559
+ */
2985
3560
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
2986
3561
  var _a, _b;
2987
3562
  if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) return void 0;
@@ -3131,13 +3706,44 @@ var _BST = class _BST extends BinaryTree {
3131
3706
  return this.search(searchRange, false, callback, startNode, iterationType);
3132
3707
  }
3133
3708
  /**
3134
- * Adds a new node to the BST based on key comparison.
3135
- * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
3136
- *
3137
- * @param keyNodeOrEntry - The key, node, or entry to set.
3138
- * @param [value] - The value, if providing just a key.
3139
- * @returns True if the addition was successful, false otherwise.
3140
- */
3709
+ * Adds a new node to the BST based on key comparison.
3710
+ * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
3711
+ *
3712
+ * @param keyNodeOrEntry - The key, node, or entry to set.
3713
+ * @param [value] - The value, if providing just a key.
3714
+ * @returns True if the addition was successful, false otherwise.
3715
+
3716
+
3717
+
3718
+
3719
+
3720
+
3721
+
3722
+
3723
+
3724
+
3725
+
3726
+
3727
+
3728
+
3729
+
3730
+
3731
+
3732
+
3733
+
3734
+
3735
+
3736
+
3737
+
3738
+
3739
+
3740
+ * @example
3741
+ * // Set a key-value pair
3742
+ * const bst = new BST<number, string>();
3743
+ * bst.set(1, 'one');
3744
+ * bst.set(2, 'two');
3745
+ * console.log(bst.get(1)); // 'one';
3746
+ */
3141
3747
  set(keyNodeOrEntry, value) {
3142
3748
  const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
3143
3749
  if (newNode === void 0) return false;
@@ -3174,17 +3780,34 @@ var _BST = class _BST extends BinaryTree {
3174
3780
  return false;
3175
3781
  }
3176
3782
  /**
3177
- * Adds multiple items to the tree.
3178
- * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
3179
- * If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
3180
- * Space O(N) for sorting and recursion/iteration stack.
3181
- *
3182
- * @param keysNodesEntriesOrRaws - An iterable of items to set.
3183
- * @param [values] - An optional parallel iterable of values.
3184
- * @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
3185
- * @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
3186
- * @returns An array of booleans indicating the success of each individual `set` operation.
3187
- */
3783
+ * Adds multiple items to the tree.
3784
+ * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
3785
+ * If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
3786
+ * Space O(N) for sorting and recursion/iteration stack.
3787
+ *
3788
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
3789
+ * @param [values] - An optional parallel iterable of values.
3790
+ * @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
3791
+ * @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
3792
+ * @returns An array of booleans indicating the success of each individual `set` operation.
3793
+
3794
+
3795
+
3796
+
3797
+
3798
+
3799
+
3800
+
3801
+
3802
+
3803
+
3804
+ * @example
3805
+ * // Set multiple key-value pairs
3806
+ * const bst = new BST<number, string>();
3807
+ * bst.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
3808
+ * console.log(bst.size); // 3;
3809
+ * console.log(bst.get(2)); // 'b';
3810
+ */
3188
3811
  setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
3189
3812
  const inserted = [];
3190
3813
  const valuesIterator = values == null ? void 0 : values[Symbol.iterator]();
@@ -3427,12 +4050,29 @@ var _BST = class _BST extends BinaryTree {
3427
4050
  }
3428
4051
  }
3429
4052
  /**
3430
- * Rebuilds the tree to be perfectly balanced.
3431
- * @remarks Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
3432
- *
3433
- * @param [iterationType=this.iterationType] - The traversal method for the initial node export.
3434
- * @returns True if successful, false if the tree was empty.
3435
- */
4053
+ * Rebuilds the tree to be perfectly balanced.
4054
+ * @remarks Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
4055
+ *
4056
+ * @param [iterationType=this.iterationType] - The traversal method for the initial node export.
4057
+ * @returns True if successful, false if the tree was empty.
4058
+
4059
+
4060
+
4061
+
4062
+
4063
+
4064
+
4065
+
4066
+
4067
+ * @example
4068
+ * // Rebalance the tree
4069
+ * const bst = new BST<number>();
4070
+ * // Insert in sorted order (worst case for BST)
4071
+ * for (let i = 1; i <= 7; i++) bst.add(i);
4072
+ * console.log(bst.isAVLBalanced()); // false;
4073
+ * bst.perfectlyBalance();
4074
+ * console.log(bst.isAVLBalanced()); // true;
4075
+ */
3436
4076
  perfectlyBalance(iterationType = this.iterationType) {
3437
4077
  const nodes = this.dfs((node) => node, "IN", false, this._root, iterationType);
3438
4078
  const n = nodes.length;
@@ -3455,12 +4095,25 @@ var _BST = class _BST extends BinaryTree {
3455
4095
  return true;
3456
4096
  }
3457
4097
  /**
3458
- * Checks if the tree meets the AVL balance condition (height difference <= 1).
3459
- * @remarks Time O(N), as it must visit every node to compute height. Space O(log N) for recursion or O(N) for iterative map.
3460
- *
3461
- * @param [iterationType=this.iterationType] - The traversal method.
3462
- * @returns True if the tree is AVL balanced, false otherwise.
3463
- */
4098
+ * Checks if the tree meets the AVL balance condition (height difference <= 1).
4099
+ * @remarks Time O(N), as it must visit every node to compute height. Space O(log N) for recursion or O(N) for iterative map.
4100
+ *
4101
+ * @param [iterationType=this.iterationType] - The traversal method.
4102
+ * @returns True if the tree is AVL balanced, false otherwise.
4103
+
4104
+
4105
+
4106
+
4107
+
4108
+
4109
+
4110
+
4111
+
4112
+ * @example
4113
+ * // Check if tree is height-balanced
4114
+ * const bst = new BST<number>([3, 1, 5, 2, 4]);
4115
+ * console.log(bst.isAVLBalanced()); // true;
4116
+ */
3464
4117
  isAVLBalanced(iterationType = this.iterationType) {
3465
4118
  if (!this._root) return true;
3466
4119
  let balanced = true;
@@ -3500,18 +4153,42 @@ var _BST = class _BST extends BinaryTree {
3500
4153
  return balanced;
3501
4154
  }
3502
4155
  /**
3503
- * Creates a new BST by mapping each [key, value] pair to a new entry.
3504
- * @remarks Time O(N * H), where N is nodes in this tree, and H is height of the new tree during insertion.
3505
- * Space O(N) for the new tree.
3506
- *
3507
- * @template MK - New key type.
3508
- * @template MV - New value type.
3509
- * @template MR - New raw type.
3510
- * @param callback - A function to map each [key, value] pair.
3511
- * @param [options] - Options for the new BST.
3512
- * @param [thisArg] - `this` context for the callback.
3513
- * @returns A new, mapped BST.
3514
- */
4156
+ * Creates a new BST by mapping each [key, value] pair to a new entry.
4157
+ * @remarks Time O(N * H), where N is nodes in this tree, and H is height of the new tree during insertion.
4158
+ * Space O(N) for the new tree.
4159
+ *
4160
+ * @template MK - New key type.
4161
+ * @template MV - New value type.
4162
+ * @template MR - New raw type.
4163
+ * @param callback - A function to map each [key, value] pair.
4164
+ * @param [options] - Options for the new BST.
4165
+ * @param [thisArg] - `this` context for the callback.
4166
+ * @returns A new, mapped BST.
4167
+
4168
+
4169
+
4170
+
4171
+
4172
+
4173
+
4174
+
4175
+
4176
+
4177
+
4178
+
4179
+
4180
+
4181
+
4182
+
4183
+
4184
+
4185
+
4186
+ * @example
4187
+ * // Transform to new tree
4188
+ * const bst = new BST<number, number>([[1, 10], [2, 20], [3, 30]]);
4189
+ * const doubled = bst.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
4190
+ * console.log([...doubled.values()]); // [20, 40, 60];
4191
+ */
3515
4192
  map(callback, options, thisArg) {
3516
4193
  const out = this._createLike([], options);
3517
4194
  let index = 0;
@@ -3577,9 +4254,15 @@ var _BST = class _BST extends BinaryTree {
3577
4254
  if (a < b) return -1;
3578
4255
  return 0;
3579
4256
  }
4257
+ if (a instanceof Date && b instanceof Date) {
4258
+ const ta = a.getTime();
4259
+ const tb = b.getTime();
4260
+ if (Number.isNaN(ta) || Number.isNaN(tb)) throw new TypeError(ERR.invalidDate("BST"));
4261
+ return ta > tb ? 1 : ta < tb ? -1 : 0;
4262
+ }
3580
4263
  if (typeof a === "object" || typeof b === "object") {
3581
- throw TypeError(
3582
- `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
4264
+ throw new TypeError(
4265
+ ERR.comparatorRequired("BST")
3583
4266
  );
3584
4267
  }
3585
4268
  return 0;
@@ -4004,6 +4687,7 @@ exports.BSTNode = BSTNode;
4004
4687
  exports.BinaryTree = BinaryTree;
4005
4688
  exports.BinaryTreeNode = BinaryTreeNode;
4006
4689
  exports.DFSOperation = DFSOperation;
4690
+ exports.ERR = ERR;
4007
4691
  exports.Range = Range;
4008
4692
  //# sourceMappingURL=index.cjs.map
4009
4693
  //# sourceMappingURL=index.cjs.map