binary-tree-typed 2.4.5 → 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.
- package/README.md +0 -84
- package/dist/cjs/index.cjs +867 -404
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +864 -401
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +867 -404
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +864 -401
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/binary-tree-typed.js +860 -397
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +2 -2
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -81,52 +81,6 @@ var binaryTreeTyped = (() => {
|
|
|
81
81
|
return (...args) => trampoline(fn(...args));
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
// src/common/error.ts
|
|
85
|
-
var ERR = {
|
|
86
|
-
// Range / index
|
|
87
|
-
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
88
|
-
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
89
|
-
// Type / argument
|
|
90
|
-
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
91
|
-
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
92
|
-
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
93
|
-
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
94
|
-
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
95
|
-
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
96
|
-
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
97
|
-
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
98
|
-
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
99
|
-
// State / operation
|
|
100
|
-
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
101
|
-
// Matrix
|
|
102
|
-
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
103
|
-
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
104
|
-
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
105
|
-
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
106
|
-
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
// src/common/index.ts
|
|
110
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
111
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
112
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
113
|
-
return DFSOperation2;
|
|
114
|
-
})(DFSOperation || {});
|
|
115
|
-
var Range = class {
|
|
116
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
117
|
-
this.low = low;
|
|
118
|
-
this.high = high;
|
|
119
|
-
this.includeLow = includeLow;
|
|
120
|
-
this.includeHigh = includeHigh;
|
|
121
|
-
}
|
|
122
|
-
// Determine whether a key is within the range
|
|
123
|
-
isInRange(key, comparator) {
|
|
124
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
125
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
126
|
-
return lowCheck && highCheck;
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
|
|
130
84
|
// src/data-structures/base/iterable-element-base.ts
|
|
131
85
|
var IterableElementBase = class {
|
|
132
86
|
/**
|
|
@@ -149,7 +103,7 @@ var binaryTreeTyped = (() => {
|
|
|
149
103
|
if (options) {
|
|
150
104
|
const { toElementFn } = options;
|
|
151
105
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
152
|
-
else if (toElementFn) throw new TypeError(
|
|
106
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
153
107
|
}
|
|
154
108
|
}
|
|
155
109
|
/**
|
|
@@ -305,7 +259,7 @@ var binaryTreeTyped = (() => {
|
|
|
305
259
|
acc = initialValue;
|
|
306
260
|
} else {
|
|
307
261
|
const first = iter.next();
|
|
308
|
-
if (first.done) throw new TypeError(
|
|
262
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
309
263
|
acc = first.value;
|
|
310
264
|
index = 1;
|
|
311
265
|
}
|
|
@@ -537,6 +491,231 @@ var binaryTreeTyped = (() => {
|
|
|
537
491
|
}
|
|
538
492
|
};
|
|
539
493
|
|
|
494
|
+
// src/common/error.ts
|
|
495
|
+
var ERR = {
|
|
496
|
+
// Range / index
|
|
497
|
+
indexOutOfRange: (index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`,
|
|
498
|
+
invalidIndex: (ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`,
|
|
499
|
+
// Type / argument
|
|
500
|
+
invalidArgument: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
501
|
+
comparatorRequired: (ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`,
|
|
502
|
+
invalidKey: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
503
|
+
notAFunction: (name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`,
|
|
504
|
+
invalidEntry: (ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`,
|
|
505
|
+
invalidNaN: (ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`,
|
|
506
|
+
invalidDate: (ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`,
|
|
507
|
+
reduceEmpty: (ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`,
|
|
508
|
+
callbackReturnType: (expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`,
|
|
509
|
+
// State / operation
|
|
510
|
+
invalidOperation: (reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`,
|
|
511
|
+
// Matrix
|
|
512
|
+
matrixDimensionMismatch: (op) => `Matrix: Dimensions must be compatible for ${op}.`,
|
|
513
|
+
matrixSingular: () => "Matrix: Singular matrix, inverse does not exist.",
|
|
514
|
+
matrixNotSquare: () => "Matrix: Must be square for inversion.",
|
|
515
|
+
matrixNotRectangular: () => "Matrix: Must be rectangular for transposition.",
|
|
516
|
+
matrixRowMismatch: (expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
// src/common/index.ts
|
|
520
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
521
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
522
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
523
|
+
return DFSOperation2;
|
|
524
|
+
})(DFSOperation || {});
|
|
525
|
+
var Range = class {
|
|
526
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
527
|
+
this.low = low;
|
|
528
|
+
this.high = high;
|
|
529
|
+
this.includeLow = includeLow;
|
|
530
|
+
this.includeHigh = includeHigh;
|
|
531
|
+
}
|
|
532
|
+
// Determine whether a key is within the range
|
|
533
|
+
isInRange(key, comparator) {
|
|
534
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
535
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
536
|
+
return lowCheck && highCheck;
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
// src/data-structures/base/iterable-entry-base.ts
|
|
541
|
+
var IterableEntryBase = class {
|
|
542
|
+
/**
|
|
543
|
+
* Default iterator yielding `[key, value]` entries.
|
|
544
|
+
* @returns Iterator of `[K, V]`.
|
|
545
|
+
* @remarks Time O(n) to iterate, Space O(1)
|
|
546
|
+
*/
|
|
547
|
+
*[Symbol.iterator](...args) {
|
|
548
|
+
yield* this._getIterator(...args);
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
552
|
+
* @returns Iterator of `[K, V | undefined]`.
|
|
553
|
+
* @remarks Time O(n), Space O(1)
|
|
554
|
+
*/
|
|
555
|
+
*entries() {
|
|
556
|
+
for (const item of this) {
|
|
557
|
+
yield item;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Iterate over keys only.
|
|
562
|
+
* @returns Iterator of keys.
|
|
563
|
+
* @remarks Time O(n), Space O(1)
|
|
564
|
+
*/
|
|
565
|
+
*keys() {
|
|
566
|
+
for (const item of this) {
|
|
567
|
+
yield item[0];
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Iterate over values only.
|
|
572
|
+
* @returns Iterator of values.
|
|
573
|
+
* @remarks Time O(n), Space O(1)
|
|
574
|
+
*/
|
|
575
|
+
*values() {
|
|
576
|
+
for (const item of this) {
|
|
577
|
+
yield item[1];
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Test whether all entries satisfy the predicate.
|
|
582
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
583
|
+
* @param thisArg - Optional `this` for callback.
|
|
584
|
+
* @returns `true` if all pass; otherwise `false`.
|
|
585
|
+
* @remarks Time O(n), Space O(1)
|
|
586
|
+
*/
|
|
587
|
+
every(predicate, thisArg) {
|
|
588
|
+
let index = 0;
|
|
589
|
+
for (const item of this) {
|
|
590
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
591
|
+
return false;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return true;
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Test whether any entry satisfies the predicate.
|
|
598
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
599
|
+
* @param thisArg - Optional `this` for callback.
|
|
600
|
+
* @returns `true` if any passes; otherwise `false`.
|
|
601
|
+
* @remarks Time O(n), Space O(1)
|
|
602
|
+
*/
|
|
603
|
+
some(predicate, thisArg) {
|
|
604
|
+
let index = 0;
|
|
605
|
+
for (const item of this) {
|
|
606
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
607
|
+
return true;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return false;
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Visit each entry, left-to-right.
|
|
614
|
+
* @param callbackfn - `(key, value, index, self) => void`.
|
|
615
|
+
* @param thisArg - Optional `this` for callback.
|
|
616
|
+
* @remarks Time O(n), Space O(1)
|
|
617
|
+
*/
|
|
618
|
+
forEach(callbackfn, thisArg) {
|
|
619
|
+
let index = 0;
|
|
620
|
+
for (const item of this) {
|
|
621
|
+
const [key, value] = item;
|
|
622
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Find the first entry that matches a predicate.
|
|
627
|
+
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
628
|
+
* @param thisArg - Optional `this` for callback.
|
|
629
|
+
* @returns Matching `[key, value]` or `undefined`.
|
|
630
|
+
* @remarks Time O(n), Space O(1)
|
|
631
|
+
*/
|
|
632
|
+
find(callbackfn, thisArg) {
|
|
633
|
+
let index = 0;
|
|
634
|
+
for (const item of this) {
|
|
635
|
+
const [key, value] = item;
|
|
636
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
637
|
+
}
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Whether the given key exists.
|
|
642
|
+
* @param key - Key to test.
|
|
643
|
+
* @returns `true` if found; otherwise `false`.
|
|
644
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
645
|
+
*/
|
|
646
|
+
has(key) {
|
|
647
|
+
for (const item of this) {
|
|
648
|
+
const [itemKey] = item;
|
|
649
|
+
if (itemKey === key) return true;
|
|
650
|
+
}
|
|
651
|
+
return false;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Whether there exists an entry with the given value.
|
|
655
|
+
* @param value - Value to test.
|
|
656
|
+
* @returns `true` if found; otherwise `false`.
|
|
657
|
+
* @remarks Time O(n), Space O(1)
|
|
658
|
+
*/
|
|
659
|
+
hasValue(value) {
|
|
660
|
+
for (const [, elementValue] of this) {
|
|
661
|
+
if (elementValue === value) return true;
|
|
662
|
+
}
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Get the value under a key.
|
|
667
|
+
* @param key - Key to look up.
|
|
668
|
+
* @returns Value or `undefined`.
|
|
669
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
670
|
+
*/
|
|
671
|
+
get(key) {
|
|
672
|
+
for (const item of this) {
|
|
673
|
+
const [itemKey, value] = item;
|
|
674
|
+
if (itemKey === key) return value;
|
|
675
|
+
}
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Reduce entries into a single accumulator.
|
|
680
|
+
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
681
|
+
* @param initialValue - Initial accumulator.
|
|
682
|
+
* @returns Final accumulator.
|
|
683
|
+
* @remarks Time O(n), Space O(1)
|
|
684
|
+
*/
|
|
685
|
+
reduce(callbackfn, initialValue) {
|
|
686
|
+
let accumulator = initialValue;
|
|
687
|
+
let index = 0;
|
|
688
|
+
for (const item of this) {
|
|
689
|
+
const [key, value] = item;
|
|
690
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
691
|
+
}
|
|
692
|
+
return accumulator;
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Converts data structure to `[key, value]` pairs.
|
|
696
|
+
* @returns Array of entries.
|
|
697
|
+
* @remarks Time O(n), Space O(n)
|
|
698
|
+
*/
|
|
699
|
+
toArray() {
|
|
700
|
+
return [...this];
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
704
|
+
* @returns Array of entries (default) or a string.
|
|
705
|
+
* @remarks Time O(n), Space O(n)
|
|
706
|
+
*/
|
|
707
|
+
toVisual() {
|
|
708
|
+
return [...this];
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Print a human-friendly representation to the console.
|
|
712
|
+
* @remarks Time O(n), Space O(n)
|
|
713
|
+
*/
|
|
714
|
+
print() {
|
|
715
|
+
console.log(this.toVisual());
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
|
|
540
719
|
// src/data-structures/queue/queue.ts
|
|
541
720
|
var Queue = class _Queue extends LinearBase {
|
|
542
721
|
/**
|
|
@@ -591,18 +770,52 @@ var binaryTreeTyped = (() => {
|
|
|
591
770
|
this._autoCompactRatio = value;
|
|
592
771
|
}
|
|
593
772
|
/**
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
773
|
+
* Get the number of elements currently in the queue.
|
|
774
|
+
* @remarks Time O(1), Space O(1)
|
|
775
|
+
* @returns Current length.
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
* @example
|
|
788
|
+
* // Track queue length
|
|
789
|
+
* const q = new Queue<number>();
|
|
790
|
+
* console.log(q.length); // 0;
|
|
791
|
+
* q.push(1);
|
|
792
|
+
* q.push(2);
|
|
793
|
+
* console.log(q.length); // 2;
|
|
794
|
+
*/
|
|
598
795
|
get length() {
|
|
599
796
|
return this.elements.length - this._offset;
|
|
600
797
|
}
|
|
601
798
|
/**
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
799
|
+
* Get the first element (front) without removing it.
|
|
800
|
+
* @remarks Time O(1), Space O(1)
|
|
801
|
+
* @returns Front element or undefined.
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
* @example
|
|
814
|
+
* // View the front element
|
|
815
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
816
|
+
* console.log(q.first); // 'first';
|
|
817
|
+
* console.log(q.length); // 3;
|
|
818
|
+
*/
|
|
606
819
|
get first() {
|
|
607
820
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
608
821
|
}
|
|
@@ -625,19 +838,69 @@ var binaryTreeTyped = (() => {
|
|
|
625
838
|
return new _Queue(elements);
|
|
626
839
|
}
|
|
627
840
|
/**
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
841
|
+
* Check whether the queue is empty.
|
|
842
|
+
* @remarks Time O(1), Space O(1)
|
|
843
|
+
* @returns True if length is 0.
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
* @example
|
|
856
|
+
* // Queue for...of iteration and isEmpty check
|
|
857
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
858
|
+
*
|
|
859
|
+
* const elements: string[] = [];
|
|
860
|
+
* for (const item of queue) {
|
|
861
|
+
* elements.push(item);
|
|
862
|
+
* }
|
|
863
|
+
*
|
|
864
|
+
* // Verify all elements are iterated in order
|
|
865
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
866
|
+
*
|
|
867
|
+
* // Process all elements
|
|
868
|
+
* while (queue.length > 0) {
|
|
869
|
+
* queue.shift();
|
|
870
|
+
* }
|
|
871
|
+
*
|
|
872
|
+
* console.log(queue.length); // 0;
|
|
873
|
+
*/
|
|
632
874
|
isEmpty() {
|
|
633
875
|
return this.length === 0;
|
|
634
876
|
}
|
|
635
877
|
/**
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
878
|
+
* Enqueue one element at the back.
|
|
879
|
+
* @remarks Time O(1), Space O(1)
|
|
880
|
+
* @param element - Element to enqueue.
|
|
881
|
+
* @returns True on success.
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
* @example
|
|
894
|
+
* // basic Queue creation and push operation
|
|
895
|
+
* // Create a simple Queue with initial values
|
|
896
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
897
|
+
*
|
|
898
|
+
* // Verify the queue maintains insertion order
|
|
899
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
900
|
+
*
|
|
901
|
+
* // Check length
|
|
902
|
+
* console.log(queue.length); // 5;
|
|
903
|
+
*/
|
|
641
904
|
push(element) {
|
|
642
905
|
this.elements.push(element);
|
|
643
906
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -658,10 +921,35 @@ var binaryTreeTyped = (() => {
|
|
|
658
921
|
return ans;
|
|
659
922
|
}
|
|
660
923
|
/**
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
924
|
+
* Dequeue one element from the front (amortized via offset).
|
|
925
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
926
|
+
* @returns Removed element or undefined.
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
* @example
|
|
939
|
+
* // Queue shift and peek operations
|
|
940
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
941
|
+
*
|
|
942
|
+
* // Peek at the front element without removing it
|
|
943
|
+
* console.log(queue.first); // 10;
|
|
944
|
+
*
|
|
945
|
+
* // Remove and get the first element (FIFO)
|
|
946
|
+
* const first = queue.shift();
|
|
947
|
+
* console.log(first); // 10;
|
|
948
|
+
*
|
|
949
|
+
* // Verify remaining elements and length decreased
|
|
950
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
951
|
+
* console.log(queue.length); // 3;
|
|
952
|
+
*/
|
|
665
953
|
shift() {
|
|
666
954
|
if (this.length === 0) return void 0;
|
|
667
955
|
const first = this.first;
|
|
@@ -670,11 +958,24 @@ var binaryTreeTyped = (() => {
|
|
|
670
958
|
return first;
|
|
671
959
|
}
|
|
672
960
|
/**
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
961
|
+
* Delete the first occurrence of a specific element.
|
|
962
|
+
* @remarks Time O(N), Space O(1)
|
|
963
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
964
|
+
* @returns True if an element was removed.
|
|
965
|
+
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
* @example
|
|
974
|
+
* // Remove specific element
|
|
975
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
976
|
+
* q.delete(2);
|
|
977
|
+
* console.log(q.length); // 3;
|
|
978
|
+
*/
|
|
678
979
|
delete(element) {
|
|
679
980
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
680
981
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -685,11 +986,24 @@ var binaryTreeTyped = (() => {
|
|
|
685
986
|
return false;
|
|
686
987
|
}
|
|
687
988
|
/**
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
989
|
+
* Get the element at a given logical index.
|
|
990
|
+
* @remarks Time O(1), Space O(1)
|
|
991
|
+
* @param index - Zero-based index from the front.
|
|
992
|
+
* @returns Element or undefined.
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
* @example
|
|
1002
|
+
* // Access element by index
|
|
1003
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1004
|
+
* console.log(q.at(0)); // 'a';
|
|
1005
|
+
* console.log(q.at(2)); // 'c';
|
|
1006
|
+
*/
|
|
693
1007
|
at(index) {
|
|
694
1008
|
if (index < 0 || index >= this.length) return void 0;
|
|
695
1009
|
return this._elements[this._offset + index];
|
|
@@ -741,19 +1055,48 @@ var binaryTreeTyped = (() => {
|
|
|
741
1055
|
return this;
|
|
742
1056
|
}
|
|
743
1057
|
/**
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
1058
|
+
* Remove all elements and reset offset.
|
|
1059
|
+
* @remarks Time O(1), Space O(1)
|
|
1060
|
+
* @returns void
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
* @example
|
|
1071
|
+
* // Remove all elements
|
|
1072
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1073
|
+
* q.clear();
|
|
1074
|
+
* console.log(q.length); // 0;
|
|
1075
|
+
*/
|
|
748
1076
|
clear() {
|
|
749
1077
|
this._elements = [];
|
|
750
1078
|
this._offset = 0;
|
|
751
1079
|
}
|
|
752
1080
|
/**
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
1081
|
+
* Compact storage by discarding consumed head elements.
|
|
1082
|
+
* @remarks Time O(N), Space O(N)
|
|
1083
|
+
* @returns True when compaction performed.
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
* @example
|
|
1093
|
+
* // Reclaim unused memory
|
|
1094
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1095
|
+
* q.shift();
|
|
1096
|
+
* q.shift();
|
|
1097
|
+
* q.compact();
|
|
1098
|
+
* console.log(q.length); // 3;
|
|
1099
|
+
*/
|
|
757
1100
|
compact() {
|
|
758
1101
|
this._elements = this.elements.slice(this._offset);
|
|
759
1102
|
this._offset = 0;
|
|
@@ -779,10 +1122,26 @@ var binaryTreeTyped = (() => {
|
|
|
779
1122
|
return removed;
|
|
780
1123
|
}
|
|
781
1124
|
/**
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
1125
|
+
* Deep clone this queue and its parameters.
|
|
1126
|
+
* @remarks Time O(N), Space O(N)
|
|
1127
|
+
* @returns A new queue with the same content and options.
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
* @example
|
|
1138
|
+
* // Create independent copy
|
|
1139
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1140
|
+
* const copy = q.clone();
|
|
1141
|
+
* copy.shift();
|
|
1142
|
+
* console.log(q.length); // 3;
|
|
1143
|
+
* console.log(copy.length); // 2;
|
|
1144
|
+
*/
|
|
786
1145
|
clone() {
|
|
787
1146
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
788
1147
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -790,12 +1149,26 @@ var binaryTreeTyped = (() => {
|
|
|
790
1149
|
return out;
|
|
791
1150
|
}
|
|
792
1151
|
/**
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
1152
|
+
* Filter elements into a new queue of the same class.
|
|
1153
|
+
* @remarks Time O(N), Space O(N)
|
|
1154
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1155
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1156
|
+
* @returns A new queue with kept elements.
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
|
|
1164
|
+
|
|
1165
|
+
|
|
1166
|
+
* @example
|
|
1167
|
+
* // Filter elements
|
|
1168
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1169
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1170
|
+
* console.log(evens.length); // 2;
|
|
1171
|
+
*/
|
|
799
1172
|
filter(predicate, thisArg) {
|
|
800
1173
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
801
1174
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -807,15 +1180,28 @@ var binaryTreeTyped = (() => {
|
|
|
807
1180
|
return out;
|
|
808
1181
|
}
|
|
809
1182
|
/**
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
1183
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1184
|
+
* @remarks Time O(N), Space O(N)
|
|
1185
|
+
* @template EM
|
|
1186
|
+
* @template RM
|
|
1187
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1188
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1189
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1190
|
+
* @returns A new Queue with mapped elements.
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
* @example
|
|
1200
|
+
* // Transform elements
|
|
1201
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1202
|
+
* const doubled = q.map(x => x * 2);
|
|
1203
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1204
|
+
*/
|
|
819
1205
|
map(callback, options, thisArg) {
|
|
820
1206
|
var _a, _b;
|
|
821
1207
|
const out = new this.constructor([], {
|
|
@@ -904,185 +1290,6 @@ var binaryTreeTyped = (() => {
|
|
|
904
1290
|
}
|
|
905
1291
|
};
|
|
906
1292
|
|
|
907
|
-
// src/data-structures/base/iterable-entry-base.ts
|
|
908
|
-
var IterableEntryBase = class {
|
|
909
|
-
/**
|
|
910
|
-
* Default iterator yielding `[key, value]` entries.
|
|
911
|
-
* @returns Iterator of `[K, V]`.
|
|
912
|
-
* @remarks Time O(n) to iterate, Space O(1)
|
|
913
|
-
*/
|
|
914
|
-
*[Symbol.iterator](...args) {
|
|
915
|
-
yield* this._getIterator(...args);
|
|
916
|
-
}
|
|
917
|
-
/**
|
|
918
|
-
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
919
|
-
* @returns Iterator of `[K, V | undefined]`.
|
|
920
|
-
* @remarks Time O(n), Space O(1)
|
|
921
|
-
*/
|
|
922
|
-
*entries() {
|
|
923
|
-
for (const item of this) {
|
|
924
|
-
yield item;
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
/**
|
|
928
|
-
* Iterate over keys only.
|
|
929
|
-
* @returns Iterator of keys.
|
|
930
|
-
* @remarks Time O(n), Space O(1)
|
|
931
|
-
*/
|
|
932
|
-
*keys() {
|
|
933
|
-
for (const item of this) {
|
|
934
|
-
yield item[0];
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
/**
|
|
938
|
-
* Iterate over values only.
|
|
939
|
-
* @returns Iterator of values.
|
|
940
|
-
* @remarks Time O(n), Space O(1)
|
|
941
|
-
*/
|
|
942
|
-
*values() {
|
|
943
|
-
for (const item of this) {
|
|
944
|
-
yield item[1];
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
/**
|
|
948
|
-
* Test whether all entries satisfy the predicate.
|
|
949
|
-
* @param predicate - `(key, value, index, self) => boolean`.
|
|
950
|
-
* @param thisArg - Optional `this` for callback.
|
|
951
|
-
* @returns `true` if all pass; otherwise `false`.
|
|
952
|
-
* @remarks Time O(n), Space O(1)
|
|
953
|
-
*/
|
|
954
|
-
every(predicate, thisArg) {
|
|
955
|
-
let index = 0;
|
|
956
|
-
for (const item of this) {
|
|
957
|
-
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
958
|
-
return false;
|
|
959
|
-
}
|
|
960
|
-
}
|
|
961
|
-
return true;
|
|
962
|
-
}
|
|
963
|
-
/**
|
|
964
|
-
* Test whether any entry satisfies the predicate.
|
|
965
|
-
* @param predicate - `(key, value, index, self) => boolean`.
|
|
966
|
-
* @param thisArg - Optional `this` for callback.
|
|
967
|
-
* @returns `true` if any passes; otherwise `false`.
|
|
968
|
-
* @remarks Time O(n), Space O(1)
|
|
969
|
-
*/
|
|
970
|
-
some(predicate, thisArg) {
|
|
971
|
-
let index = 0;
|
|
972
|
-
for (const item of this) {
|
|
973
|
-
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
974
|
-
return true;
|
|
975
|
-
}
|
|
976
|
-
}
|
|
977
|
-
return false;
|
|
978
|
-
}
|
|
979
|
-
/**
|
|
980
|
-
* Visit each entry, left-to-right.
|
|
981
|
-
* @param callbackfn - `(key, value, index, self) => void`.
|
|
982
|
-
* @param thisArg - Optional `this` for callback.
|
|
983
|
-
* @remarks Time O(n), Space O(1)
|
|
984
|
-
*/
|
|
985
|
-
forEach(callbackfn, thisArg) {
|
|
986
|
-
let index = 0;
|
|
987
|
-
for (const item of this) {
|
|
988
|
-
const [key, value] = item;
|
|
989
|
-
callbackfn.call(thisArg, value, key, index++, this);
|
|
990
|
-
}
|
|
991
|
-
}
|
|
992
|
-
/**
|
|
993
|
-
* Find the first entry that matches a predicate.
|
|
994
|
-
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
995
|
-
* @param thisArg - Optional `this` for callback.
|
|
996
|
-
* @returns Matching `[key, value]` or `undefined`.
|
|
997
|
-
* @remarks Time O(n), Space O(1)
|
|
998
|
-
*/
|
|
999
|
-
find(callbackfn, thisArg) {
|
|
1000
|
-
let index = 0;
|
|
1001
|
-
for (const item of this) {
|
|
1002
|
-
const [key, value] = item;
|
|
1003
|
-
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
1004
|
-
}
|
|
1005
|
-
return;
|
|
1006
|
-
}
|
|
1007
|
-
/**
|
|
1008
|
-
* Whether the given key exists.
|
|
1009
|
-
* @param key - Key to test.
|
|
1010
|
-
* @returns `true` if found; otherwise `false`.
|
|
1011
|
-
* @remarks Time O(n) generic, Space O(1)
|
|
1012
|
-
*/
|
|
1013
|
-
has(key) {
|
|
1014
|
-
for (const item of this) {
|
|
1015
|
-
const [itemKey] = item;
|
|
1016
|
-
if (itemKey === key) return true;
|
|
1017
|
-
}
|
|
1018
|
-
return false;
|
|
1019
|
-
}
|
|
1020
|
-
/**
|
|
1021
|
-
* Whether there exists an entry with the given value.
|
|
1022
|
-
* @param value - Value to test.
|
|
1023
|
-
* @returns `true` if found; otherwise `false`.
|
|
1024
|
-
* @remarks Time O(n), Space O(1)
|
|
1025
|
-
*/
|
|
1026
|
-
hasValue(value) {
|
|
1027
|
-
for (const [, elementValue] of this) {
|
|
1028
|
-
if (elementValue === value) return true;
|
|
1029
|
-
}
|
|
1030
|
-
return false;
|
|
1031
|
-
}
|
|
1032
|
-
/**
|
|
1033
|
-
* Get the value under a key.
|
|
1034
|
-
* @param key - Key to look up.
|
|
1035
|
-
* @returns Value or `undefined`.
|
|
1036
|
-
* @remarks Time O(n) generic, Space O(1)
|
|
1037
|
-
*/
|
|
1038
|
-
get(key) {
|
|
1039
|
-
for (const item of this) {
|
|
1040
|
-
const [itemKey, value] = item;
|
|
1041
|
-
if (itemKey === key) return value;
|
|
1042
|
-
}
|
|
1043
|
-
return;
|
|
1044
|
-
}
|
|
1045
|
-
/**
|
|
1046
|
-
* Reduce entries into a single accumulator.
|
|
1047
|
-
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
1048
|
-
* @param initialValue - Initial accumulator.
|
|
1049
|
-
* @returns Final accumulator.
|
|
1050
|
-
* @remarks Time O(n), Space O(1)
|
|
1051
|
-
*/
|
|
1052
|
-
reduce(callbackfn, initialValue) {
|
|
1053
|
-
let accumulator = initialValue;
|
|
1054
|
-
let index = 0;
|
|
1055
|
-
for (const item of this) {
|
|
1056
|
-
const [key, value] = item;
|
|
1057
|
-
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
1058
|
-
}
|
|
1059
|
-
return accumulator;
|
|
1060
|
-
}
|
|
1061
|
-
/**
|
|
1062
|
-
* Converts data structure to `[key, value]` pairs.
|
|
1063
|
-
* @returns Array of entries.
|
|
1064
|
-
* @remarks Time O(n), Space O(n)
|
|
1065
|
-
*/
|
|
1066
|
-
toArray() {
|
|
1067
|
-
return [...this];
|
|
1068
|
-
}
|
|
1069
|
-
/**
|
|
1070
|
-
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
1071
|
-
* @returns Array of entries (default) or a string.
|
|
1072
|
-
* @remarks Time O(n), Space O(n)
|
|
1073
|
-
*/
|
|
1074
|
-
toVisual() {
|
|
1075
|
-
return [...this];
|
|
1076
|
-
}
|
|
1077
|
-
/**
|
|
1078
|
-
* Print a human-friendly representation to the console.
|
|
1079
|
-
* @remarks Time O(n), Space O(n)
|
|
1080
|
-
*/
|
|
1081
|
-
print() {
|
|
1082
|
-
console.log(this.toVisual());
|
|
1083
|
-
}
|
|
1084
|
-
};
|
|
1085
|
-
|
|
1086
1293
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
1087
1294
|
var BinaryTreeNode = class {
|
|
1088
1295
|
/**
|
|
@@ -1458,23 +1665,71 @@ var binaryTreeTyped = (() => {
|
|
|
1458
1665
|
return isComparable(key);
|
|
1459
1666
|
}
|
|
1460
1667
|
/**
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1668
|
+
* Adds a new node to the tree.
|
|
1669
|
+
* @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).
|
|
1670
|
+
*
|
|
1671
|
+
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1672
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1673
|
+
|
|
1674
|
+
|
|
1675
|
+
|
|
1676
|
+
|
|
1677
|
+
|
|
1678
|
+
|
|
1679
|
+
* @example
|
|
1680
|
+
* // Add a single node
|
|
1681
|
+
* const tree = new BinaryTree<number>();
|
|
1682
|
+
* tree.add(1);
|
|
1683
|
+
* tree.add(2);
|
|
1684
|
+
* tree.add(3);
|
|
1685
|
+
* console.log(tree.size); // 3;
|
|
1686
|
+
* console.log(tree.has(1)); // true;
|
|
1687
|
+
*/
|
|
1467
1688
|
add(keyNodeOrEntry) {
|
|
1468
1689
|
return this.set(keyNodeOrEntry);
|
|
1469
1690
|
}
|
|
1470
1691
|
/**
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1692
|
+
* Adds or updates a new node to the tree.
|
|
1693
|
+
* @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).
|
|
1694
|
+
*
|
|
1695
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1696
|
+
* @param [value] - The value, if providing just a key.
|
|
1697
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1698
|
+
|
|
1699
|
+
|
|
1700
|
+
|
|
1701
|
+
|
|
1702
|
+
|
|
1703
|
+
|
|
1704
|
+
|
|
1705
|
+
|
|
1706
|
+
|
|
1707
|
+
|
|
1708
|
+
|
|
1709
|
+
* @example
|
|
1710
|
+
* // basic BinaryTree creation and insertion
|
|
1711
|
+
* // Create a BinaryTree with entries
|
|
1712
|
+
* const entries: [number, string][] = [
|
|
1713
|
+
* [6, 'six'],
|
|
1714
|
+
* [1, 'one'],
|
|
1715
|
+
* [2, 'two'],
|
|
1716
|
+
* [7, 'seven'],
|
|
1717
|
+
* [5, 'five'],
|
|
1718
|
+
* [3, 'three'],
|
|
1719
|
+
* [4, 'four'],
|
|
1720
|
+
* [9, 'nine'],
|
|
1721
|
+
* [8, 'eight']
|
|
1722
|
+
* ];
|
|
1723
|
+
*
|
|
1724
|
+
* const tree = new BinaryTree(entries);
|
|
1725
|
+
*
|
|
1726
|
+
* // Verify size
|
|
1727
|
+
* console.log(tree.size); // 9;
|
|
1728
|
+
*
|
|
1729
|
+
* // Add new element
|
|
1730
|
+
* tree.set(10, 'ten');
|
|
1731
|
+
* console.log(tree.size); // 10;
|
|
1732
|
+
*/
|
|
1478
1733
|
set(keyNodeOrEntry, value) {
|
|
1479
1734
|
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1480
1735
|
if (newNode === void 0) return false;
|
|
@@ -1519,23 +1774,44 @@ var binaryTreeTyped = (() => {
|
|
|
1519
1774
|
return false;
|
|
1520
1775
|
}
|
|
1521
1776
|
/**
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1777
|
+
* Adds multiple items to the tree.
|
|
1778
|
+
* @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).
|
|
1779
|
+
*
|
|
1780
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
1781
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1782
|
+
|
|
1783
|
+
|
|
1784
|
+
|
|
1785
|
+
|
|
1786
|
+
|
|
1787
|
+
|
|
1788
|
+
|
|
1789
|
+
|
|
1790
|
+
|
|
1791
|
+
* @example
|
|
1792
|
+
* // Bulk add
|
|
1793
|
+
* const tree = new BinaryTree<number>();
|
|
1794
|
+
* tree.addMany([1, 2, 3, 4, 5]);
|
|
1795
|
+
* console.log(tree.size); // 5;
|
|
1796
|
+
*/
|
|
1528
1797
|
addMany(keysNodesEntriesOrRaws) {
|
|
1529
1798
|
return this.setMany(keysNodesEntriesOrRaws);
|
|
1530
1799
|
}
|
|
1531
1800
|
/**
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1801
|
+
* Adds or updates multiple items to the tree.
|
|
1802
|
+
* @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).
|
|
1803
|
+
*
|
|
1804
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
1805
|
+
* @param [values] - An optional parallel iterable of values.
|
|
1806
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1807
|
+
|
|
1808
|
+
|
|
1809
|
+
* @example
|
|
1810
|
+
* // Set multiple entries
|
|
1811
|
+
* const tree = new BinaryTree<number, string>();
|
|
1812
|
+
* tree.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
1813
|
+
* console.log(tree.size); // 3;
|
|
1814
|
+
*/
|
|
1539
1815
|
setMany(keysNodesEntriesOrRaws, values) {
|
|
1540
1816
|
const inserted = [];
|
|
1541
1817
|
let valuesIterator;
|
|
@@ -1556,11 +1832,26 @@ var binaryTreeTyped = (() => {
|
|
|
1556
1832
|
return inserted;
|
|
1557
1833
|
}
|
|
1558
1834
|
/**
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1835
|
+
* Merges another tree into this one by seting all its nodes.
|
|
1836
|
+
* @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`).
|
|
1837
|
+
*
|
|
1838
|
+
* @param anotherTree - The tree to merge.
|
|
1839
|
+
|
|
1840
|
+
|
|
1841
|
+
|
|
1842
|
+
|
|
1843
|
+
|
|
1844
|
+
|
|
1845
|
+
|
|
1846
|
+
|
|
1847
|
+
|
|
1848
|
+
* @example
|
|
1849
|
+
* // Combine trees
|
|
1850
|
+
* const t1 = new BinaryTree<number>([1, 2]);
|
|
1851
|
+
* const t2 = new BinaryTree<number>([3, 4]);
|
|
1852
|
+
* t1.merge(t2);
|
|
1853
|
+
* console.log(t1.size); // 4;
|
|
1854
|
+
*/
|
|
1564
1855
|
merge(anotherTree) {
|
|
1565
1856
|
this.setMany(anotherTree, []);
|
|
1566
1857
|
}
|
|
@@ -1576,12 +1867,29 @@ var binaryTreeTyped = (() => {
|
|
|
1576
1867
|
this.setMany(keysNodesEntriesOrRaws, values);
|
|
1577
1868
|
}
|
|
1578
1869
|
/**
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1870
|
+
* Deletes a node from the tree.
|
|
1871
|
+
* @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).
|
|
1872
|
+
*
|
|
1873
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
1874
|
+
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
1875
|
+
|
|
1876
|
+
|
|
1877
|
+
|
|
1878
|
+
|
|
1879
|
+
|
|
1880
|
+
|
|
1881
|
+
|
|
1882
|
+
|
|
1883
|
+
|
|
1884
|
+
|
|
1885
|
+
|
|
1886
|
+
* @example
|
|
1887
|
+
* // Remove a node
|
|
1888
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1889
|
+
* tree.delete(3);
|
|
1890
|
+
* console.log(tree.has(3)); // false;
|
|
1891
|
+
* console.log(tree.size); // 4;
|
|
1892
|
+
*/
|
|
1585
1893
|
delete(keyNodeEntryRawOrPredicate) {
|
|
1586
1894
|
const deletedResult = [];
|
|
1587
1895
|
if (!this._root) return deletedResult;
|
|
@@ -1675,14 +1983,27 @@ var binaryTreeTyped = (() => {
|
|
|
1675
1983
|
return this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
1676
1984
|
}
|
|
1677
1985
|
/**
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1986
|
+
* Gets the first node matching a predicate.
|
|
1987
|
+
* @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`).
|
|
1988
|
+
*
|
|
1989
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
1990
|
+
* @param [startNode=this._root] - The node to start the search from.
|
|
1991
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
1992
|
+
* @returns The first matching node, or undefined if not found.
|
|
1993
|
+
|
|
1994
|
+
|
|
1995
|
+
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
|
|
1999
|
+
|
|
2000
|
+
|
|
2001
|
+
|
|
2002
|
+
* @example
|
|
2003
|
+
* // Get node by key
|
|
2004
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
|
|
2005
|
+
* console.log(tree.getNode(2)?.value); // 'child';
|
|
2006
|
+
*/
|
|
1686
2007
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1687
2008
|
if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
|
|
1688
2009
|
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -1694,14 +2015,30 @@ var binaryTreeTyped = (() => {
|
|
|
1694
2015
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
|
|
1695
2016
|
}
|
|
1696
2017
|
/**
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
2018
|
+
* Gets the value associated with a key.
|
|
2019
|
+
* @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.
|
|
2020
|
+
*
|
|
2021
|
+
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2022
|
+
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
2023
|
+
* @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
|
|
2024
|
+
* @returns The associated value, or undefined.
|
|
2025
|
+
|
|
2026
|
+
|
|
2027
|
+
|
|
2028
|
+
|
|
2029
|
+
|
|
2030
|
+
|
|
2031
|
+
|
|
2032
|
+
|
|
2033
|
+
|
|
2034
|
+
|
|
2035
|
+
|
|
2036
|
+
* @example
|
|
2037
|
+
* // Retrieve value by key
|
|
2038
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
|
|
2039
|
+
* console.log(tree.get(2)); // 'left';
|
|
2040
|
+
* console.log(tree.get(99)); // undefined;
|
|
2041
|
+
*/
|
|
1705
2042
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1706
2043
|
var _a, _b;
|
|
1707
2044
|
if (this._isMapMode) {
|
|
@@ -1722,19 +2059,45 @@ var binaryTreeTyped = (() => {
|
|
|
1722
2059
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
1723
2060
|
}
|
|
1724
2061
|
/**
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
2062
|
+
* Clears the tree of all nodes and values.
|
|
2063
|
+
* @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
|
|
2064
|
+
|
|
2065
|
+
|
|
2066
|
+
|
|
2067
|
+
|
|
2068
|
+
|
|
2069
|
+
|
|
2070
|
+
|
|
2071
|
+
|
|
2072
|
+
|
|
2073
|
+
* @example
|
|
2074
|
+
* // Remove all nodes
|
|
2075
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2076
|
+
* tree.clear();
|
|
2077
|
+
* console.log(tree.isEmpty()); // true;
|
|
2078
|
+
*/
|
|
1728
2079
|
clear() {
|
|
1729
2080
|
this._clearNodes();
|
|
1730
2081
|
if (this._isMapMode) this._clearValues();
|
|
1731
2082
|
}
|
|
1732
2083
|
/**
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
2084
|
+
* Checks if the tree is empty.
|
|
2085
|
+
* @remarks Time O(1), Space O(1)
|
|
2086
|
+
*
|
|
2087
|
+
* @returns True if the tree has no nodes, false otherwise.
|
|
2088
|
+
|
|
2089
|
+
|
|
2090
|
+
|
|
2091
|
+
|
|
2092
|
+
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
|
|
2096
|
+
|
|
2097
|
+
* @example
|
|
2098
|
+
* // Check empty
|
|
2099
|
+
* console.log(new BinaryTree().isEmpty()); // true;
|
|
2100
|
+
*/
|
|
1738
2101
|
isEmpty() {
|
|
1739
2102
|
return this._size === 0;
|
|
1740
2103
|
}
|
|
@@ -1749,13 +2112,27 @@ var binaryTreeTyped = (() => {
|
|
|
1749
2112
|
return this.getMinHeight(startNode) + 1 >= this.getHeight(startNode);
|
|
1750
2113
|
}
|
|
1751
2114
|
/**
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
2115
|
+
* Checks if the tree is a valid Binary Search Tree (BST).
|
|
2116
|
+
* @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).
|
|
2117
|
+
*
|
|
2118
|
+
* @param [startNode=this._root] - The node to start checking from.
|
|
2119
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2120
|
+
* @returns True if it's a valid BST, false otherwise.
|
|
2121
|
+
|
|
2122
|
+
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
|
|
2126
|
+
|
|
2127
|
+
|
|
2128
|
+
|
|
2129
|
+
|
|
2130
|
+
* @example
|
|
2131
|
+
* // Check BST property
|
|
2132
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2133
|
+
* // BinaryTree doesn't guarantee BST order
|
|
2134
|
+
* console.log(typeof tree.isBST()); // 'boolean';
|
|
2135
|
+
*/
|
|
1759
2136
|
isBST(startNode = this._root, iterationType = this.iterationType) {
|
|
1760
2137
|
const startNodeSired = this.ensureNode(startNode);
|
|
1761
2138
|
if (!startNodeSired) return true;
|
|
@@ -1793,13 +2170,29 @@ var binaryTreeTyped = (() => {
|
|
|
1793
2170
|
}
|
|
1794
2171
|
}
|
|
1795
2172
|
/**
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
2173
|
+
* Gets the depth of a node (distance from `startNode`).
|
|
2174
|
+
* @remarks Time O(H), where H is the depth of the `dist` node relative to `startNode`. O(N) worst-case. Space O(1).
|
|
2175
|
+
*
|
|
2176
|
+
* @param dist - The node to find the depth of.
|
|
2177
|
+
* @param [startNode=this._root] - The node to measure depth from (defaults to root).
|
|
2178
|
+
* @returns The depth (0 if `dist` is `startNode`).
|
|
2179
|
+
|
|
2180
|
+
|
|
2181
|
+
|
|
2182
|
+
|
|
2183
|
+
|
|
2184
|
+
|
|
2185
|
+
|
|
2186
|
+
|
|
2187
|
+
|
|
2188
|
+
|
|
2189
|
+
|
|
2190
|
+
* @example
|
|
2191
|
+
* // Get depth of a node
|
|
2192
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2193
|
+
* const node = tree.getNode(4);
|
|
2194
|
+
* console.log(tree.getDepth(node!)); // 2;
|
|
2195
|
+
*/
|
|
1803
2196
|
getDepth(dist, startNode = this._root) {
|
|
1804
2197
|
let distEnsured = this.ensureNode(dist);
|
|
1805
2198
|
const beginRootEnsured = this.ensureNode(startNode);
|
|
@@ -1814,13 +2207,28 @@ var binaryTreeTyped = (() => {
|
|
|
1814
2207
|
return depth;
|
|
1815
2208
|
}
|
|
1816
2209
|
/**
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
2210
|
+
* Gets the maximum height of the tree (longest path from startNode to a leaf).
|
|
2211
|
+
* @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).
|
|
2212
|
+
*
|
|
2213
|
+
* @param [startNode=this._root] - The node to start measuring from.
|
|
2214
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2215
|
+
* @returns The height ( -1 for an empty tree, 0 for a single-node tree).
|
|
2216
|
+
|
|
2217
|
+
|
|
2218
|
+
|
|
2219
|
+
|
|
2220
|
+
|
|
2221
|
+
|
|
2222
|
+
|
|
2223
|
+
|
|
2224
|
+
|
|
2225
|
+
|
|
2226
|
+
|
|
2227
|
+
* @example
|
|
2228
|
+
* // Get tree height
|
|
2229
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2230
|
+
* console.log(tree.getHeight()); // 2;
|
|
2231
|
+
*/
|
|
1824
2232
|
getHeight(startNode = this._root, iterationType = this.iterationType) {
|
|
1825
2233
|
startNode = this.ensureNode(startNode);
|
|
1826
2234
|
if (!this.isRealNode(startNode)) return -1;
|
|
@@ -2256,24 +2664,53 @@ var binaryTreeTyped = (() => {
|
|
|
2256
2664
|
return ans;
|
|
2257
2665
|
}
|
|
2258
2666
|
/**
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2667
|
+
* Clones the tree.
|
|
2668
|
+
* @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.
|
|
2669
|
+
*
|
|
2670
|
+
* @returns A new, cloned instance of the tree.
|
|
2671
|
+
|
|
2672
|
+
|
|
2673
|
+
|
|
2674
|
+
|
|
2675
|
+
|
|
2676
|
+
|
|
2677
|
+
|
|
2678
|
+
|
|
2679
|
+
|
|
2680
|
+
* @example
|
|
2681
|
+
* // Deep copy
|
|
2682
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2683
|
+
* const copy = tree.clone();
|
|
2684
|
+
* copy.delete(1);
|
|
2685
|
+
* console.log(tree.has(1)); // true;
|
|
2686
|
+
*/
|
|
2264
2687
|
clone() {
|
|
2265
2688
|
const out = this._createInstance();
|
|
2266
2689
|
this._clone(out);
|
|
2267
2690
|
return out;
|
|
2268
2691
|
}
|
|
2269
2692
|
/**
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2693
|
+
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
2694
|
+
* @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.
|
|
2695
|
+
*
|
|
2696
|
+
* @param predicate - A function to test each [key, value] pair.
|
|
2697
|
+
* @param [thisArg] - `this` context for the predicate.
|
|
2698
|
+
* @returns A new, filtered tree.
|
|
2699
|
+
|
|
2700
|
+
|
|
2701
|
+
|
|
2702
|
+
|
|
2703
|
+
|
|
2704
|
+
|
|
2705
|
+
|
|
2706
|
+
|
|
2707
|
+
|
|
2708
|
+
* @example
|
|
2709
|
+
* // Filter nodes by condition
|
|
2710
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4]);
|
|
2711
|
+
* const result = tree.filter((_, key) => key > 2);
|
|
2712
|
+
* console.log(result.size); // 2;
|
|
2713
|
+
*/
|
|
2277
2714
|
filter(predicate, thisArg) {
|
|
2278
2715
|
const out = this._createInstance();
|
|
2279
2716
|
let i = 0;
|
|
@@ -2281,17 +2718,31 @@ var binaryTreeTyped = (() => {
|
|
|
2281
2718
|
return out;
|
|
2282
2719
|
}
|
|
2283
2720
|
/**
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2721
|
+
* Creates a new tree by mapping each [key, value] pair to a new entry.
|
|
2722
|
+
* @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.
|
|
2723
|
+
*
|
|
2724
|
+
* @template MK - New key type.
|
|
2725
|
+
* @template MV - New value type.
|
|
2726
|
+
* @template MR - New raw type.
|
|
2727
|
+
* @param cb - A function to map each [key, value] pair.
|
|
2728
|
+
* @param [options] - Options for the new tree.
|
|
2729
|
+
* @param [thisArg] - `this` context for the callback.
|
|
2730
|
+
* @returns A new, mapped tree.
|
|
2731
|
+
|
|
2732
|
+
|
|
2733
|
+
|
|
2734
|
+
|
|
2735
|
+
|
|
2736
|
+
|
|
2737
|
+
|
|
2738
|
+
|
|
2739
|
+
|
|
2740
|
+
* @example
|
|
2741
|
+
* // Transform to new tree
|
|
2742
|
+
* const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
|
|
2743
|
+
* const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
|
|
2744
|
+
* console.log([...mapped.values()]); // contains 11;
|
|
2745
|
+
*/
|
|
2295
2746
|
map(cb, options, thisArg) {
|
|
2296
2747
|
const out = this._createLike([], options);
|
|
2297
2748
|
let i = 0;
|
|
@@ -2329,12 +2780,25 @@ var binaryTreeTyped = (() => {
|
|
|
2329
2780
|
return output;
|
|
2330
2781
|
}
|
|
2331
2782
|
/**
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2783
|
+
* Prints a visual representation of the tree to the console.
|
|
2784
|
+
* @remarks Time O(N) (via `toVisual`). Space O(N*H) or O(N^2) (via `toVisual`).
|
|
2785
|
+
*
|
|
2786
|
+
* @param [options] - Options to control the output.
|
|
2787
|
+
* @param [startNode=this._root] - The node to start printing from.
|
|
2788
|
+
|
|
2789
|
+
|
|
2790
|
+
|
|
2791
|
+
|
|
2792
|
+
|
|
2793
|
+
|
|
2794
|
+
|
|
2795
|
+
|
|
2796
|
+
|
|
2797
|
+
* @example
|
|
2798
|
+
* // Display tree
|
|
2799
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2800
|
+
* expect(() => tree.print()).not.toThrow();
|
|
2801
|
+
*/
|
|
2338
2802
|
print(options, startNode = this._root) {
|
|
2339
2803
|
console.log(this.toVisual(startNode, options));
|
|
2340
2804
|
}
|
|
@@ -2567,7 +3031,6 @@ var binaryTreeTyped = (() => {
|
|
|
2567
3031
|
* @returns Layout information for this subtree.
|
|
2568
3032
|
*/
|
|
2569
3033
|
_displayAux(node, options) {
|
|
2570
|
-
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
2571
3034
|
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
2572
3035
|
const newFrame = (n) => ({
|
|
2573
3036
|
node: n,
|