binary-tree-typed 2.4.5 → 2.5.1
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 +1476 -404
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +1473 -401
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1476 -404
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +1473 -401
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -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 +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -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 +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/binary-tree-typed.js +1469 -397
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +5 -5
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -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 +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -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 +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
package/dist/cjs/index.cjs
CHANGED
|
@@ -59,55 +59,6 @@ function makeTrampoline(fn) {
|
|
|
59
59
|
}
|
|
60
60
|
__name(makeTrampoline, "makeTrampoline");
|
|
61
61
|
|
|
62
|
-
// src/common/error.ts
|
|
63
|
-
var ERR = {
|
|
64
|
-
// Range / index
|
|
65
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
66
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
67
|
-
// Type / argument
|
|
68
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
69
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
70
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
71
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
72
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
73
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
74
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
75
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
76
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
77
|
-
// State / operation
|
|
78
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
79
|
-
// Matrix
|
|
80
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
81
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
82
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
83
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
84
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
// src/common/index.ts
|
|
88
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
89
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
90
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
91
|
-
return DFSOperation2;
|
|
92
|
-
})(DFSOperation || {});
|
|
93
|
-
var Range = class {
|
|
94
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
95
|
-
this.low = low;
|
|
96
|
-
this.high = high;
|
|
97
|
-
this.includeLow = includeLow;
|
|
98
|
-
this.includeHigh = includeHigh;
|
|
99
|
-
}
|
|
100
|
-
static {
|
|
101
|
-
__name(this, "Range");
|
|
102
|
-
}
|
|
103
|
-
// Determine whether a key is within the range
|
|
104
|
-
isInRange(key, comparator) {
|
|
105
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
106
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
107
|
-
return lowCheck && highCheck;
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
|
|
111
62
|
// src/data-structures/base/iterable-element-base.ts
|
|
112
63
|
var IterableElementBase = class {
|
|
113
64
|
static {
|
|
@@ -126,7 +77,7 @@ var IterableElementBase = class {
|
|
|
126
77
|
if (options) {
|
|
127
78
|
const { toElementFn } = options;
|
|
128
79
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
129
|
-
else if (toElementFn) throw new TypeError(
|
|
80
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
130
81
|
}
|
|
131
82
|
}
|
|
132
83
|
/**
|
|
@@ -289,7 +240,7 @@ var IterableElementBase = class {
|
|
|
289
240
|
acc = initialValue;
|
|
290
241
|
} else {
|
|
291
242
|
const first = iter.next();
|
|
292
|
-
if (first.done) throw new TypeError(
|
|
243
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
293
244
|
acc = first.value;
|
|
294
245
|
index = 1;
|
|
295
246
|
}
|
|
@@ -524,6 +475,237 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
524
475
|
}
|
|
525
476
|
};
|
|
526
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 {
|
|
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
|
+
static {
|
|
517
|
+
__name(this, "Range");
|
|
518
|
+
}
|
|
519
|
+
// Determine whether a key is within the range
|
|
520
|
+
isInRange(key, comparator) {
|
|
521
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
522
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
523
|
+
return lowCheck && highCheck;
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
// src/data-structures/base/iterable-entry-base.ts
|
|
528
|
+
var IterableEntryBase = class {
|
|
529
|
+
static {
|
|
530
|
+
__name(this, "IterableEntryBase");
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Default iterator yielding `[key, value]` entries.
|
|
534
|
+
* @returns Iterator of `[K, V]`.
|
|
535
|
+
* @remarks Time O(n) to iterate, Space O(1)
|
|
536
|
+
*/
|
|
537
|
+
*[Symbol.iterator](...args) {
|
|
538
|
+
yield* this._getIterator(...args);
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
542
|
+
* @returns Iterator of `[K, V | undefined]`.
|
|
543
|
+
* @remarks Time O(n), Space O(1)
|
|
544
|
+
*/
|
|
545
|
+
*entries() {
|
|
546
|
+
for (const item of this) {
|
|
547
|
+
yield item;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Iterate over keys only.
|
|
552
|
+
* @returns Iterator of keys.
|
|
553
|
+
* @remarks Time O(n), Space O(1)
|
|
554
|
+
*/
|
|
555
|
+
*keys() {
|
|
556
|
+
for (const item of this) {
|
|
557
|
+
yield item[0];
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Iterate over values only.
|
|
562
|
+
* @returns Iterator of values.
|
|
563
|
+
* @remarks Time O(n), Space O(1)
|
|
564
|
+
*/
|
|
565
|
+
*values() {
|
|
566
|
+
for (const item of this) {
|
|
567
|
+
yield item[1];
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Test whether all entries satisfy the predicate.
|
|
572
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
573
|
+
* @param thisArg - Optional `this` for callback.
|
|
574
|
+
* @returns `true` if all pass; otherwise `false`.
|
|
575
|
+
* @remarks Time O(n), Space O(1)
|
|
576
|
+
*/
|
|
577
|
+
every(predicate, thisArg) {
|
|
578
|
+
let index = 0;
|
|
579
|
+
for (const item of this) {
|
|
580
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
581
|
+
return false;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
return true;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Test whether any entry satisfies the predicate.
|
|
588
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
589
|
+
* @param thisArg - Optional `this` for callback.
|
|
590
|
+
* @returns `true` if any passes; otherwise `false`.
|
|
591
|
+
* @remarks Time O(n), Space O(1)
|
|
592
|
+
*/
|
|
593
|
+
some(predicate, thisArg) {
|
|
594
|
+
let index = 0;
|
|
595
|
+
for (const item of this) {
|
|
596
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Visit each entry, left-to-right.
|
|
604
|
+
* @param callbackfn - `(key, value, index, self) => void`.
|
|
605
|
+
* @param thisArg - Optional `this` for callback.
|
|
606
|
+
* @remarks Time O(n), Space O(1)
|
|
607
|
+
*/
|
|
608
|
+
forEach(callbackfn, thisArg) {
|
|
609
|
+
let index = 0;
|
|
610
|
+
for (const item of this) {
|
|
611
|
+
const [key, value] = item;
|
|
612
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Find the first entry that matches a predicate.
|
|
617
|
+
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
618
|
+
* @param thisArg - Optional `this` for callback.
|
|
619
|
+
* @returns Matching `[key, value]` or `undefined`.
|
|
620
|
+
* @remarks Time O(n), Space O(1)
|
|
621
|
+
*/
|
|
622
|
+
find(callbackfn, thisArg) {
|
|
623
|
+
let index = 0;
|
|
624
|
+
for (const item of this) {
|
|
625
|
+
const [key, value] = item;
|
|
626
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
627
|
+
}
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Whether the given key exists.
|
|
632
|
+
* @param key - Key to test.
|
|
633
|
+
* @returns `true` if found; otherwise `false`.
|
|
634
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
635
|
+
*/
|
|
636
|
+
has(key) {
|
|
637
|
+
for (const item of this) {
|
|
638
|
+
const [itemKey] = item;
|
|
639
|
+
if (itemKey === key) return true;
|
|
640
|
+
}
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Whether there exists an entry with the given value.
|
|
645
|
+
* @param value - Value to test.
|
|
646
|
+
* @returns `true` if found; otherwise `false`.
|
|
647
|
+
* @remarks Time O(n), Space O(1)
|
|
648
|
+
*/
|
|
649
|
+
hasValue(value) {
|
|
650
|
+
for (const [, elementValue] of this) {
|
|
651
|
+
if (elementValue === value) return true;
|
|
652
|
+
}
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Get the value under a key.
|
|
657
|
+
* @param key - Key to look up.
|
|
658
|
+
* @returns Value or `undefined`.
|
|
659
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
660
|
+
*/
|
|
661
|
+
get(key) {
|
|
662
|
+
for (const item of this) {
|
|
663
|
+
const [itemKey, value] = item;
|
|
664
|
+
if (itemKey === key) return value;
|
|
665
|
+
}
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Reduce entries into a single accumulator.
|
|
670
|
+
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
671
|
+
* @param initialValue - Initial accumulator.
|
|
672
|
+
* @returns Final accumulator.
|
|
673
|
+
* @remarks Time O(n), Space O(1)
|
|
674
|
+
*/
|
|
675
|
+
reduce(callbackfn, initialValue) {
|
|
676
|
+
let accumulator = initialValue;
|
|
677
|
+
let index = 0;
|
|
678
|
+
for (const item of this) {
|
|
679
|
+
const [key, value] = item;
|
|
680
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
681
|
+
}
|
|
682
|
+
return accumulator;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Converts data structure to `[key, value]` pairs.
|
|
686
|
+
* @returns Array of entries.
|
|
687
|
+
* @remarks Time O(n), Space O(n)
|
|
688
|
+
*/
|
|
689
|
+
toArray() {
|
|
690
|
+
return [...this];
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
694
|
+
* @returns Array of entries (default) or a string.
|
|
695
|
+
* @remarks Time O(n), Space O(n)
|
|
696
|
+
*/
|
|
697
|
+
toVisual() {
|
|
698
|
+
return [...this];
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Print a human-friendly representation to the console.
|
|
702
|
+
* @remarks Time O(n), Space O(n)
|
|
703
|
+
*/
|
|
704
|
+
print() {
|
|
705
|
+
console.log(this.toVisual());
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
|
|
527
709
|
// src/data-structures/queue/queue.ts
|
|
528
710
|
var Queue = class _Queue extends LinearBase {
|
|
529
711
|
static {
|
|
@@ -580,19 +762,95 @@ var Queue = class _Queue extends LinearBase {
|
|
|
580
762
|
set autoCompactRatio(value) {
|
|
581
763
|
this._autoCompactRatio = value;
|
|
582
764
|
}
|
|
583
|
-
/**
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
765
|
+
/**
|
|
766
|
+
* Get the number of elements currently in the queue.
|
|
767
|
+
* @remarks Time O(1), Space O(1)
|
|
768
|
+
* @returns Current length.
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
* @example
|
|
802
|
+
* // Track queue length
|
|
803
|
+
* const q = new Queue<number>();
|
|
804
|
+
* console.log(q.length); // 0;
|
|
805
|
+
* q.push(1);
|
|
806
|
+
* q.push(2);
|
|
807
|
+
* console.log(q.length); // 2;
|
|
808
|
+
*/
|
|
588
809
|
get length() {
|
|
589
810
|
return this.elements.length - this._offset;
|
|
590
811
|
}
|
|
591
812
|
/**
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
813
|
+
* Get the first element (front) without removing it.
|
|
814
|
+
* @remarks Time O(1), Space O(1)
|
|
815
|
+
* @returns Front element or undefined.
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
* @example
|
|
849
|
+
* // View the front element
|
|
850
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
851
|
+
* console.log(q.first); // 'first';
|
|
852
|
+
* console.log(q.length); // 3;
|
|
853
|
+
*/
|
|
596
854
|
get first() {
|
|
597
855
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
598
856
|
}
|
|
@@ -615,19 +873,111 @@ var Queue = class _Queue extends LinearBase {
|
|
|
615
873
|
return new _Queue(elements);
|
|
616
874
|
}
|
|
617
875
|
/**
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
876
|
+
* Check whether the queue is empty.
|
|
877
|
+
* @remarks Time O(1), Space O(1)
|
|
878
|
+
* @returns True if length is 0.
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
|
|
911
|
+
* @example
|
|
912
|
+
* // Queue for...of iteration and isEmpty check
|
|
913
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
914
|
+
*
|
|
915
|
+
* const elements: string[] = [];
|
|
916
|
+
* for (const item of queue) {
|
|
917
|
+
* elements.push(item);
|
|
918
|
+
* }
|
|
919
|
+
*
|
|
920
|
+
* // Verify all elements are iterated in order
|
|
921
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
922
|
+
*
|
|
923
|
+
* // Process all elements
|
|
924
|
+
* while (queue.length > 0) {
|
|
925
|
+
* queue.shift();
|
|
926
|
+
* }
|
|
927
|
+
*
|
|
928
|
+
* console.log(queue.length); // 0;
|
|
929
|
+
*/
|
|
622
930
|
isEmpty() {
|
|
623
931
|
return this.length === 0;
|
|
624
932
|
}
|
|
625
933
|
/**
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
934
|
+
* Enqueue one element at the back.
|
|
935
|
+
* @remarks Time O(1), Space O(1)
|
|
936
|
+
* @param element - Element to enqueue.
|
|
937
|
+
* @returns True on success.
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
* @example
|
|
971
|
+
* // basic Queue creation and push operation
|
|
972
|
+
* // Create a simple Queue with initial values
|
|
973
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
974
|
+
*
|
|
975
|
+
* // Verify the queue maintains insertion order
|
|
976
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
977
|
+
*
|
|
978
|
+
* // Check length
|
|
979
|
+
* console.log(queue.length); // 5;
|
|
980
|
+
*/
|
|
631
981
|
push(element) {
|
|
632
982
|
this.elements.push(element);
|
|
633
983
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -648,10 +998,56 @@ var Queue = class _Queue extends LinearBase {
|
|
|
648
998
|
return ans;
|
|
649
999
|
}
|
|
650
1000
|
/**
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
1001
|
+
* Dequeue one element from the front (amortized via offset).
|
|
1002
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
1003
|
+
* @returns Removed element or undefined.
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
* @example
|
|
1037
|
+
* // Queue shift and peek operations
|
|
1038
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1039
|
+
*
|
|
1040
|
+
* // Peek at the front element without removing it
|
|
1041
|
+
* console.log(queue.first); // 10;
|
|
1042
|
+
*
|
|
1043
|
+
* // Remove and get the first element (FIFO)
|
|
1044
|
+
* const first = queue.shift();
|
|
1045
|
+
* console.log(first); // 10;
|
|
1046
|
+
*
|
|
1047
|
+
* // Verify remaining elements and length decreased
|
|
1048
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1049
|
+
* console.log(queue.length); // 3;
|
|
1050
|
+
*/
|
|
655
1051
|
shift() {
|
|
656
1052
|
if (this.length === 0) return void 0;
|
|
657
1053
|
const first = this.first;
|
|
@@ -660,11 +1056,45 @@ var Queue = class _Queue extends LinearBase {
|
|
|
660
1056
|
return first;
|
|
661
1057
|
}
|
|
662
1058
|
/**
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
1059
|
+
* Delete the first occurrence of a specific element.
|
|
1060
|
+
* @remarks Time O(N), Space O(1)
|
|
1061
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1062
|
+
* @returns True if an element was removed.
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
* @example
|
|
1093
|
+
* // Remove specific element
|
|
1094
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1095
|
+
* q.delete(2);
|
|
1096
|
+
* console.log(q.length); // 3;
|
|
1097
|
+
*/
|
|
668
1098
|
delete(element) {
|
|
669
1099
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
670
1100
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -675,11 +1105,45 @@ var Queue = class _Queue extends LinearBase {
|
|
|
675
1105
|
return false;
|
|
676
1106
|
}
|
|
677
1107
|
/**
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
1108
|
+
* Get the element at a given logical index.
|
|
1109
|
+
* @remarks Time O(1), Space O(1)
|
|
1110
|
+
* @param index - Zero-based index from the front.
|
|
1111
|
+
* @returns Element or undefined.
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
* @example
|
|
1142
|
+
* // Access element by index
|
|
1143
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1144
|
+
* console.log(q.at(0)); // 'a';
|
|
1145
|
+
* console.log(q.at(2)); // 'c';
|
|
1146
|
+
*/
|
|
683
1147
|
at(index) {
|
|
684
1148
|
if (index < 0 || index >= this.length) return void 0;
|
|
685
1149
|
return this._elements[this._offset + index];
|
|
@@ -731,19 +1195,90 @@ var Queue = class _Queue extends LinearBase {
|
|
|
731
1195
|
return this;
|
|
732
1196
|
}
|
|
733
1197
|
/**
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
1198
|
+
* Remove all elements and reset offset.
|
|
1199
|
+
* @remarks Time O(1), Space O(1)
|
|
1200
|
+
* @returns void
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
|
|
1225
|
+
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
|
|
1231
|
+
* @example
|
|
1232
|
+
* // Remove all elements
|
|
1233
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1234
|
+
* q.clear();
|
|
1235
|
+
* console.log(q.length); // 0;
|
|
1236
|
+
*/
|
|
738
1237
|
clear() {
|
|
739
1238
|
this._elements = [];
|
|
740
1239
|
this._offset = 0;
|
|
741
1240
|
}
|
|
742
1241
|
/**
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
1242
|
+
* Compact storage by discarding consumed head elements.
|
|
1243
|
+
* @remarks Time O(N), Space O(N)
|
|
1244
|
+
* @returns True when compaction performed.
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
* @example
|
|
1275
|
+
* // Reclaim unused memory
|
|
1276
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1277
|
+
* q.shift();
|
|
1278
|
+
* q.shift();
|
|
1279
|
+
* q.compact();
|
|
1280
|
+
* console.log(q.length); // 3;
|
|
1281
|
+
*/
|
|
747
1282
|
compact() {
|
|
748
1283
|
this._elements = this.elements.slice(this._offset);
|
|
749
1284
|
this._offset = 0;
|
|
@@ -769,10 +1304,47 @@ var Queue = class _Queue extends LinearBase {
|
|
|
769
1304
|
return removed;
|
|
770
1305
|
}
|
|
771
1306
|
/**
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
1307
|
+
* Deep clone this queue and its parameters.
|
|
1308
|
+
* @remarks Time O(N), Space O(N)
|
|
1309
|
+
* @returns A new queue with the same content and options.
|
|
1310
|
+
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
|
|
1315
|
+
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
|
|
1321
|
+
|
|
1322
|
+
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
|
|
1326
|
+
|
|
1327
|
+
|
|
1328
|
+
|
|
1329
|
+
|
|
1330
|
+
|
|
1331
|
+
|
|
1332
|
+
|
|
1333
|
+
|
|
1334
|
+
|
|
1335
|
+
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
|
|
1339
|
+
|
|
1340
|
+
* @example
|
|
1341
|
+
* // Create independent copy
|
|
1342
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1343
|
+
* const copy = q.clone();
|
|
1344
|
+
* copy.shift();
|
|
1345
|
+
* console.log(q.length); // 3;
|
|
1346
|
+
* console.log(copy.length); // 2;
|
|
1347
|
+
*/
|
|
776
1348
|
clone() {
|
|
777
1349
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
778
1350
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -780,12 +1352,47 @@ var Queue = class _Queue extends LinearBase {
|
|
|
780
1352
|
return out;
|
|
781
1353
|
}
|
|
782
1354
|
/**
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
1355
|
+
* Filter elements into a new queue of the same class.
|
|
1356
|
+
* @remarks Time O(N), Space O(N)
|
|
1357
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1358
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1359
|
+
* @returns A new queue with kept elements.
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
|
|
1386
|
+
|
|
1387
|
+
|
|
1388
|
+
|
|
1389
|
+
|
|
1390
|
+
* @example
|
|
1391
|
+
* // Filter elements
|
|
1392
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1393
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1394
|
+
* console.log(evens.length); // 2;
|
|
1395
|
+
*/
|
|
789
1396
|
filter(predicate, thisArg) {
|
|
790
1397
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
791
1398
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -797,15 +1404,49 @@ var Queue = class _Queue extends LinearBase {
|
|
|
797
1404
|
return out;
|
|
798
1405
|
}
|
|
799
1406
|
/**
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
1407
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1408
|
+
* @remarks Time O(N), Space O(N)
|
|
1409
|
+
* @template EM
|
|
1410
|
+
* @template RM
|
|
1411
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1412
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1413
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1414
|
+
* @returns A new Queue with mapped elements.
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
|
|
1431
|
+
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
|
|
1435
|
+
|
|
1436
|
+
|
|
1437
|
+
|
|
1438
|
+
|
|
1439
|
+
|
|
1440
|
+
|
|
1441
|
+
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
* @example
|
|
1445
|
+
* // Transform elements
|
|
1446
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1447
|
+
* const doubled = q.map(x => x * 2);
|
|
1448
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1449
|
+
*/
|
|
809
1450
|
map(callback, options, thisArg) {
|
|
810
1451
|
const out = new this.constructor([], {
|
|
811
1452
|
toElementFn: options?.toElementFn,
|
|
@@ -892,188 +1533,6 @@ var Queue = class _Queue extends LinearBase {
|
|
|
892
1533
|
}
|
|
893
1534
|
};
|
|
894
1535
|
|
|
895
|
-
// src/data-structures/base/iterable-entry-base.ts
|
|
896
|
-
var IterableEntryBase = class {
|
|
897
|
-
static {
|
|
898
|
-
__name(this, "IterableEntryBase");
|
|
899
|
-
}
|
|
900
|
-
/**
|
|
901
|
-
* Default iterator yielding `[key, value]` entries.
|
|
902
|
-
* @returns Iterator of `[K, V]`.
|
|
903
|
-
* @remarks Time O(n) to iterate, Space O(1)
|
|
904
|
-
*/
|
|
905
|
-
*[Symbol.iterator](...args) {
|
|
906
|
-
yield* this._getIterator(...args);
|
|
907
|
-
}
|
|
908
|
-
/**
|
|
909
|
-
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
910
|
-
* @returns Iterator of `[K, V | undefined]`.
|
|
911
|
-
* @remarks Time O(n), Space O(1)
|
|
912
|
-
*/
|
|
913
|
-
*entries() {
|
|
914
|
-
for (const item of this) {
|
|
915
|
-
yield item;
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
/**
|
|
919
|
-
* Iterate over keys only.
|
|
920
|
-
* @returns Iterator of keys.
|
|
921
|
-
* @remarks Time O(n), Space O(1)
|
|
922
|
-
*/
|
|
923
|
-
*keys() {
|
|
924
|
-
for (const item of this) {
|
|
925
|
-
yield item[0];
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
/**
|
|
929
|
-
* Iterate over values only.
|
|
930
|
-
* @returns Iterator of values.
|
|
931
|
-
* @remarks Time O(n), Space O(1)
|
|
932
|
-
*/
|
|
933
|
-
*values() {
|
|
934
|
-
for (const item of this) {
|
|
935
|
-
yield item[1];
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
/**
|
|
939
|
-
* Test whether all entries satisfy the predicate.
|
|
940
|
-
* @param predicate - `(key, value, index, self) => boolean`.
|
|
941
|
-
* @param thisArg - Optional `this` for callback.
|
|
942
|
-
* @returns `true` if all pass; otherwise `false`.
|
|
943
|
-
* @remarks Time O(n), Space O(1)
|
|
944
|
-
*/
|
|
945
|
-
every(predicate, thisArg) {
|
|
946
|
-
let index = 0;
|
|
947
|
-
for (const item of this) {
|
|
948
|
-
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
949
|
-
return false;
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
return true;
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* Test whether any entry satisfies the predicate.
|
|
956
|
-
* @param predicate - `(key, value, index, self) => boolean`.
|
|
957
|
-
* @param thisArg - Optional `this` for callback.
|
|
958
|
-
* @returns `true` if any passes; otherwise `false`.
|
|
959
|
-
* @remarks Time O(n), Space O(1)
|
|
960
|
-
*/
|
|
961
|
-
some(predicate, thisArg) {
|
|
962
|
-
let index = 0;
|
|
963
|
-
for (const item of this) {
|
|
964
|
-
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
965
|
-
return true;
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
return false;
|
|
969
|
-
}
|
|
970
|
-
/**
|
|
971
|
-
* Visit each entry, left-to-right.
|
|
972
|
-
* @param callbackfn - `(key, value, index, self) => void`.
|
|
973
|
-
* @param thisArg - Optional `this` for callback.
|
|
974
|
-
* @remarks Time O(n), Space O(1)
|
|
975
|
-
*/
|
|
976
|
-
forEach(callbackfn, thisArg) {
|
|
977
|
-
let index = 0;
|
|
978
|
-
for (const item of this) {
|
|
979
|
-
const [key, value] = item;
|
|
980
|
-
callbackfn.call(thisArg, value, key, index++, this);
|
|
981
|
-
}
|
|
982
|
-
}
|
|
983
|
-
/**
|
|
984
|
-
* Find the first entry that matches a predicate.
|
|
985
|
-
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
986
|
-
* @param thisArg - Optional `this` for callback.
|
|
987
|
-
* @returns Matching `[key, value]` or `undefined`.
|
|
988
|
-
* @remarks Time O(n), Space O(1)
|
|
989
|
-
*/
|
|
990
|
-
find(callbackfn, thisArg) {
|
|
991
|
-
let index = 0;
|
|
992
|
-
for (const item of this) {
|
|
993
|
-
const [key, value] = item;
|
|
994
|
-
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
995
|
-
}
|
|
996
|
-
return;
|
|
997
|
-
}
|
|
998
|
-
/**
|
|
999
|
-
* Whether the given key exists.
|
|
1000
|
-
* @param key - Key to test.
|
|
1001
|
-
* @returns `true` if found; otherwise `false`.
|
|
1002
|
-
* @remarks Time O(n) generic, Space O(1)
|
|
1003
|
-
*/
|
|
1004
|
-
has(key) {
|
|
1005
|
-
for (const item of this) {
|
|
1006
|
-
const [itemKey] = item;
|
|
1007
|
-
if (itemKey === key) return true;
|
|
1008
|
-
}
|
|
1009
|
-
return false;
|
|
1010
|
-
}
|
|
1011
|
-
/**
|
|
1012
|
-
* Whether there exists an entry with the given value.
|
|
1013
|
-
* @param value - Value to test.
|
|
1014
|
-
* @returns `true` if found; otherwise `false`.
|
|
1015
|
-
* @remarks Time O(n), Space O(1)
|
|
1016
|
-
*/
|
|
1017
|
-
hasValue(value) {
|
|
1018
|
-
for (const [, elementValue] of this) {
|
|
1019
|
-
if (elementValue === value) return true;
|
|
1020
|
-
}
|
|
1021
|
-
return false;
|
|
1022
|
-
}
|
|
1023
|
-
/**
|
|
1024
|
-
* Get the value under a key.
|
|
1025
|
-
* @param key - Key to look up.
|
|
1026
|
-
* @returns Value or `undefined`.
|
|
1027
|
-
* @remarks Time O(n) generic, Space O(1)
|
|
1028
|
-
*/
|
|
1029
|
-
get(key) {
|
|
1030
|
-
for (const item of this) {
|
|
1031
|
-
const [itemKey, value] = item;
|
|
1032
|
-
if (itemKey === key) return value;
|
|
1033
|
-
}
|
|
1034
|
-
return;
|
|
1035
|
-
}
|
|
1036
|
-
/**
|
|
1037
|
-
* Reduce entries into a single accumulator.
|
|
1038
|
-
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
1039
|
-
* @param initialValue - Initial accumulator.
|
|
1040
|
-
* @returns Final accumulator.
|
|
1041
|
-
* @remarks Time O(n), Space O(1)
|
|
1042
|
-
*/
|
|
1043
|
-
reduce(callbackfn, initialValue) {
|
|
1044
|
-
let accumulator = initialValue;
|
|
1045
|
-
let index = 0;
|
|
1046
|
-
for (const item of this) {
|
|
1047
|
-
const [key, value] = item;
|
|
1048
|
-
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
1049
|
-
}
|
|
1050
|
-
return accumulator;
|
|
1051
|
-
}
|
|
1052
|
-
/**
|
|
1053
|
-
* Converts data structure to `[key, value]` pairs.
|
|
1054
|
-
* @returns Array of entries.
|
|
1055
|
-
* @remarks Time O(n), Space O(n)
|
|
1056
|
-
*/
|
|
1057
|
-
toArray() {
|
|
1058
|
-
return [...this];
|
|
1059
|
-
}
|
|
1060
|
-
/**
|
|
1061
|
-
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
1062
|
-
* @returns Array of entries (default) or a string.
|
|
1063
|
-
* @remarks Time O(n), Space O(n)
|
|
1064
|
-
*/
|
|
1065
|
-
toVisual() {
|
|
1066
|
-
return [...this];
|
|
1067
|
-
}
|
|
1068
|
-
/**
|
|
1069
|
-
* Print a human-friendly representation to the console.
|
|
1070
|
-
* @remarks Time O(n), Space O(n)
|
|
1071
|
-
*/
|
|
1072
|
-
print() {
|
|
1073
|
-
console.log(this.toVisual());
|
|
1074
|
-
}
|
|
1075
|
-
};
|
|
1076
|
-
|
|
1077
1536
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
1078
1537
|
var BinaryTreeNode = class {
|
|
1079
1538
|
static {
|
|
@@ -1447,23 +1906,113 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1447
1906
|
return isComparable(key);
|
|
1448
1907
|
}
|
|
1449
1908
|
/**
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1909
|
+
* Adds a new node to the tree.
|
|
1910
|
+
* @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).
|
|
1911
|
+
*
|
|
1912
|
+
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1913
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1914
|
+
|
|
1915
|
+
|
|
1916
|
+
|
|
1917
|
+
|
|
1918
|
+
|
|
1919
|
+
|
|
1920
|
+
|
|
1921
|
+
|
|
1922
|
+
|
|
1923
|
+
|
|
1924
|
+
|
|
1925
|
+
|
|
1926
|
+
|
|
1927
|
+
|
|
1928
|
+
|
|
1929
|
+
|
|
1930
|
+
|
|
1931
|
+
|
|
1932
|
+
|
|
1933
|
+
|
|
1934
|
+
|
|
1935
|
+
|
|
1936
|
+
|
|
1937
|
+
|
|
1938
|
+
|
|
1939
|
+
|
|
1940
|
+
|
|
1941
|
+
* @example
|
|
1942
|
+
* // Add a single node
|
|
1943
|
+
* const tree = new BinaryTree<number>();
|
|
1944
|
+
* tree.add(1);
|
|
1945
|
+
* tree.add(2);
|
|
1946
|
+
* tree.add(3);
|
|
1947
|
+
* console.log(tree.size); // 3;
|
|
1948
|
+
* console.log(tree.has(1)); // true;
|
|
1949
|
+
*/
|
|
1456
1950
|
add(keyNodeOrEntry) {
|
|
1457
1951
|
return this.set(keyNodeOrEntry);
|
|
1458
1952
|
}
|
|
1459
1953
|
/**
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1954
|
+
* Adds or updates a new node to the tree.
|
|
1955
|
+
* @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).
|
|
1956
|
+
*
|
|
1957
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1958
|
+
* @param [value] - The value, if providing just a key.
|
|
1959
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1960
|
+
|
|
1961
|
+
|
|
1962
|
+
|
|
1963
|
+
|
|
1964
|
+
|
|
1965
|
+
|
|
1966
|
+
|
|
1967
|
+
|
|
1968
|
+
|
|
1969
|
+
|
|
1970
|
+
|
|
1971
|
+
|
|
1972
|
+
|
|
1973
|
+
|
|
1974
|
+
|
|
1975
|
+
|
|
1976
|
+
|
|
1977
|
+
|
|
1978
|
+
|
|
1979
|
+
|
|
1980
|
+
|
|
1981
|
+
|
|
1982
|
+
|
|
1983
|
+
|
|
1984
|
+
|
|
1985
|
+
|
|
1986
|
+
|
|
1987
|
+
|
|
1988
|
+
|
|
1989
|
+
|
|
1990
|
+
|
|
1991
|
+
|
|
1992
|
+
* @example
|
|
1993
|
+
* // basic BinaryTree creation and insertion
|
|
1994
|
+
* // Create a BinaryTree with entries
|
|
1995
|
+
* const entries: [number, string][] = [
|
|
1996
|
+
* [6, 'six'],
|
|
1997
|
+
* [1, 'one'],
|
|
1998
|
+
* [2, 'two'],
|
|
1999
|
+
* [7, 'seven'],
|
|
2000
|
+
* [5, 'five'],
|
|
2001
|
+
* [3, 'three'],
|
|
2002
|
+
* [4, 'four'],
|
|
2003
|
+
* [9, 'nine'],
|
|
2004
|
+
* [8, 'eight']
|
|
2005
|
+
* ];
|
|
2006
|
+
*
|
|
2007
|
+
* const tree = new BinaryTree(entries);
|
|
2008
|
+
*
|
|
2009
|
+
* // Verify size
|
|
2010
|
+
* console.log(tree.size); // 9;
|
|
2011
|
+
*
|
|
2012
|
+
* // Add new element
|
|
2013
|
+
* tree.set(10, 'ten');
|
|
2014
|
+
* console.log(tree.size); // 10;
|
|
2015
|
+
*/
|
|
1467
2016
|
set(keyNodeOrEntry, value) {
|
|
1468
2017
|
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1469
2018
|
if (newNode === void 0) return false;
|
|
@@ -1508,23 +2057,86 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1508
2057
|
return false;
|
|
1509
2058
|
}
|
|
1510
2059
|
/**
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
2060
|
+
* Adds multiple items to the tree.
|
|
2061
|
+
* @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).
|
|
2062
|
+
*
|
|
2063
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
2064
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
2065
|
+
|
|
2066
|
+
|
|
2067
|
+
|
|
2068
|
+
|
|
2069
|
+
|
|
2070
|
+
|
|
2071
|
+
|
|
2072
|
+
|
|
2073
|
+
|
|
2074
|
+
|
|
2075
|
+
|
|
2076
|
+
|
|
2077
|
+
|
|
2078
|
+
|
|
2079
|
+
|
|
2080
|
+
|
|
2081
|
+
|
|
2082
|
+
|
|
2083
|
+
|
|
2084
|
+
|
|
2085
|
+
|
|
2086
|
+
|
|
2087
|
+
|
|
2088
|
+
|
|
2089
|
+
|
|
2090
|
+
|
|
2091
|
+
|
|
2092
|
+
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
* @example
|
|
2096
|
+
* // Bulk add
|
|
2097
|
+
* const tree = new BinaryTree<number>();
|
|
2098
|
+
* tree.addMany([1, 2, 3, 4, 5]);
|
|
2099
|
+
* console.log(tree.size); // 5;
|
|
2100
|
+
*/
|
|
1517
2101
|
addMany(keysNodesEntriesOrRaws) {
|
|
1518
2102
|
return this.setMany(keysNodesEntriesOrRaws);
|
|
1519
2103
|
}
|
|
1520
2104
|
/**
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
2105
|
+
* Adds or updates multiple items to the tree.
|
|
2106
|
+
* @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).
|
|
2107
|
+
*
|
|
2108
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
2109
|
+
* @param [values] - An optional parallel iterable of values.
|
|
2110
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
2111
|
+
|
|
2112
|
+
|
|
2113
|
+
|
|
2114
|
+
|
|
2115
|
+
|
|
2116
|
+
|
|
2117
|
+
|
|
2118
|
+
|
|
2119
|
+
|
|
2120
|
+
|
|
2121
|
+
|
|
2122
|
+
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
|
|
2126
|
+
|
|
2127
|
+
|
|
2128
|
+
|
|
2129
|
+
|
|
2130
|
+
|
|
2131
|
+
|
|
2132
|
+
|
|
2133
|
+
|
|
2134
|
+
* @example
|
|
2135
|
+
* // Set multiple entries
|
|
2136
|
+
* const tree = new BinaryTree<number, string>();
|
|
2137
|
+
* tree.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
2138
|
+
* console.log(tree.size); // 3;
|
|
2139
|
+
*/
|
|
1528
2140
|
setMany(keysNodesEntriesOrRaws, values) {
|
|
1529
2141
|
const inserted = [];
|
|
1530
2142
|
let valuesIterator;
|
|
@@ -1545,11 +2157,47 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1545
2157
|
return inserted;
|
|
1546
2158
|
}
|
|
1547
2159
|
/**
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
2160
|
+
* Merges another tree into this one by seting all its nodes.
|
|
2161
|
+
* @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`).
|
|
2162
|
+
*
|
|
2163
|
+
* @param anotherTree - The tree to merge.
|
|
2164
|
+
|
|
2165
|
+
|
|
2166
|
+
|
|
2167
|
+
|
|
2168
|
+
|
|
2169
|
+
|
|
2170
|
+
|
|
2171
|
+
|
|
2172
|
+
|
|
2173
|
+
|
|
2174
|
+
|
|
2175
|
+
|
|
2176
|
+
|
|
2177
|
+
|
|
2178
|
+
|
|
2179
|
+
|
|
2180
|
+
|
|
2181
|
+
|
|
2182
|
+
|
|
2183
|
+
|
|
2184
|
+
|
|
2185
|
+
|
|
2186
|
+
|
|
2187
|
+
|
|
2188
|
+
|
|
2189
|
+
|
|
2190
|
+
|
|
2191
|
+
|
|
2192
|
+
|
|
2193
|
+
|
|
2194
|
+
* @example
|
|
2195
|
+
* // Combine trees
|
|
2196
|
+
* const t1 = new BinaryTree<number>([1, 2]);
|
|
2197
|
+
* const t2 = new BinaryTree<number>([3, 4]);
|
|
2198
|
+
* t1.merge(t2);
|
|
2199
|
+
* console.log(t1.size); // 4;
|
|
2200
|
+
*/
|
|
1553
2201
|
merge(anotherTree) {
|
|
1554
2202
|
this.setMany(anotherTree, []);
|
|
1555
2203
|
}
|
|
@@ -1565,12 +2213,50 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1565
2213
|
this.setMany(keysNodesEntriesOrRaws, values);
|
|
1566
2214
|
}
|
|
1567
2215
|
/**
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
2216
|
+
* Deletes a node from the tree.
|
|
2217
|
+
* @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).
|
|
2218
|
+
*
|
|
2219
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2220
|
+
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
2221
|
+
|
|
2222
|
+
|
|
2223
|
+
|
|
2224
|
+
|
|
2225
|
+
|
|
2226
|
+
|
|
2227
|
+
|
|
2228
|
+
|
|
2229
|
+
|
|
2230
|
+
|
|
2231
|
+
|
|
2232
|
+
|
|
2233
|
+
|
|
2234
|
+
|
|
2235
|
+
|
|
2236
|
+
|
|
2237
|
+
|
|
2238
|
+
|
|
2239
|
+
|
|
2240
|
+
|
|
2241
|
+
|
|
2242
|
+
|
|
2243
|
+
|
|
2244
|
+
|
|
2245
|
+
|
|
2246
|
+
|
|
2247
|
+
|
|
2248
|
+
|
|
2249
|
+
|
|
2250
|
+
|
|
2251
|
+
|
|
2252
|
+
|
|
2253
|
+
* @example
|
|
2254
|
+
* // Remove a node
|
|
2255
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2256
|
+
* tree.delete(3);
|
|
2257
|
+
* console.log(tree.has(3)); // false;
|
|
2258
|
+
* console.log(tree.size); // 4;
|
|
2259
|
+
*/
|
|
1574
2260
|
delete(keyNodeEntryRawOrPredicate) {
|
|
1575
2261
|
const deletedResult = [];
|
|
1576
2262
|
if (!this._root) return deletedResult;
|
|
@@ -1664,14 +2350,48 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1664
2350
|
return this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
1665
2351
|
}
|
|
1666
2352
|
/**
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
2353
|
+
* Gets the first node matching a predicate.
|
|
2354
|
+
* @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`).
|
|
2355
|
+
*
|
|
2356
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
2357
|
+
* @param [startNode=this._root] - The node to start the search from.
|
|
2358
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2359
|
+
* @returns The first matching node, or undefined if not found.
|
|
2360
|
+
|
|
2361
|
+
|
|
2362
|
+
|
|
2363
|
+
|
|
2364
|
+
|
|
2365
|
+
|
|
2366
|
+
|
|
2367
|
+
|
|
2368
|
+
|
|
2369
|
+
|
|
2370
|
+
|
|
2371
|
+
|
|
2372
|
+
|
|
2373
|
+
|
|
2374
|
+
|
|
2375
|
+
|
|
2376
|
+
|
|
2377
|
+
|
|
2378
|
+
|
|
2379
|
+
|
|
2380
|
+
|
|
2381
|
+
|
|
2382
|
+
|
|
2383
|
+
|
|
2384
|
+
|
|
2385
|
+
|
|
2386
|
+
|
|
2387
|
+
|
|
2388
|
+
|
|
2389
|
+
|
|
2390
|
+
* @example
|
|
2391
|
+
* // Get node by key
|
|
2392
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
|
|
2393
|
+
* console.log(tree.getNode(2)?.value); // 'child';
|
|
2394
|
+
*/
|
|
1675
2395
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1676
2396
|
if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
|
|
1677
2397
|
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -1683,14 +2403,51 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1683
2403
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
|
|
1684
2404
|
}
|
|
1685
2405
|
/**
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
2406
|
+
* Gets the value associated with a key.
|
|
2407
|
+
* @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.
|
|
2408
|
+
*
|
|
2409
|
+
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2410
|
+
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
2411
|
+
* @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
|
|
2412
|
+
* @returns The associated value, or undefined.
|
|
2413
|
+
|
|
2414
|
+
|
|
2415
|
+
|
|
2416
|
+
|
|
2417
|
+
|
|
2418
|
+
|
|
2419
|
+
|
|
2420
|
+
|
|
2421
|
+
|
|
2422
|
+
|
|
2423
|
+
|
|
2424
|
+
|
|
2425
|
+
|
|
2426
|
+
|
|
2427
|
+
|
|
2428
|
+
|
|
2429
|
+
|
|
2430
|
+
|
|
2431
|
+
|
|
2432
|
+
|
|
2433
|
+
|
|
2434
|
+
|
|
2435
|
+
|
|
2436
|
+
|
|
2437
|
+
|
|
2438
|
+
|
|
2439
|
+
|
|
2440
|
+
|
|
2441
|
+
|
|
2442
|
+
|
|
2443
|
+
|
|
2444
|
+
|
|
2445
|
+
* @example
|
|
2446
|
+
* // Retrieve value by key
|
|
2447
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
|
|
2448
|
+
* console.log(tree.get(2)); // 'left';
|
|
2449
|
+
* console.log(tree.get(99)); // undefined;
|
|
2450
|
+
*/
|
|
1694
2451
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1695
2452
|
if (this._isMapMode) {
|
|
1696
2453
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
@@ -1710,19 +2467,87 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1710
2467
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
1711
2468
|
}
|
|
1712
2469
|
/**
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
2470
|
+
* Clears the tree of all nodes and values.
|
|
2471
|
+
* @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
|
|
2472
|
+
|
|
2473
|
+
|
|
2474
|
+
|
|
2475
|
+
|
|
2476
|
+
|
|
2477
|
+
|
|
2478
|
+
|
|
2479
|
+
|
|
2480
|
+
|
|
2481
|
+
|
|
2482
|
+
|
|
2483
|
+
|
|
2484
|
+
|
|
2485
|
+
|
|
2486
|
+
|
|
2487
|
+
|
|
2488
|
+
|
|
2489
|
+
|
|
2490
|
+
|
|
2491
|
+
|
|
2492
|
+
|
|
2493
|
+
|
|
2494
|
+
|
|
2495
|
+
|
|
2496
|
+
|
|
2497
|
+
|
|
2498
|
+
|
|
2499
|
+
|
|
2500
|
+
|
|
2501
|
+
|
|
2502
|
+
* @example
|
|
2503
|
+
* // Remove all nodes
|
|
2504
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2505
|
+
* tree.clear();
|
|
2506
|
+
* console.log(tree.isEmpty()); // true;
|
|
2507
|
+
*/
|
|
1716
2508
|
clear() {
|
|
1717
2509
|
this._clearNodes();
|
|
1718
2510
|
if (this._isMapMode) this._clearValues();
|
|
1719
2511
|
}
|
|
1720
2512
|
/**
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
2513
|
+
* Checks if the tree is empty.
|
|
2514
|
+
* @remarks Time O(1), Space O(1)
|
|
2515
|
+
*
|
|
2516
|
+
* @returns True if the tree has no nodes, false otherwise.
|
|
2517
|
+
|
|
2518
|
+
|
|
2519
|
+
|
|
2520
|
+
|
|
2521
|
+
|
|
2522
|
+
|
|
2523
|
+
|
|
2524
|
+
|
|
2525
|
+
|
|
2526
|
+
|
|
2527
|
+
|
|
2528
|
+
|
|
2529
|
+
|
|
2530
|
+
|
|
2531
|
+
|
|
2532
|
+
|
|
2533
|
+
|
|
2534
|
+
|
|
2535
|
+
|
|
2536
|
+
|
|
2537
|
+
|
|
2538
|
+
|
|
2539
|
+
|
|
2540
|
+
|
|
2541
|
+
|
|
2542
|
+
|
|
2543
|
+
|
|
2544
|
+
|
|
2545
|
+
|
|
2546
|
+
|
|
2547
|
+
* @example
|
|
2548
|
+
* // Check empty
|
|
2549
|
+
* console.log(new BinaryTree().isEmpty()); // true;
|
|
2550
|
+
*/
|
|
1726
2551
|
isEmpty() {
|
|
1727
2552
|
return this._size === 0;
|
|
1728
2553
|
}
|
|
@@ -1737,13 +2562,48 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1737
2562
|
return this.getMinHeight(startNode) + 1 >= this.getHeight(startNode);
|
|
1738
2563
|
}
|
|
1739
2564
|
/**
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
2565
|
+
* Checks if the tree is a valid Binary Search Tree (BST).
|
|
2566
|
+
* @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).
|
|
2567
|
+
*
|
|
2568
|
+
* @param [startNode=this._root] - The node to start checking from.
|
|
2569
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2570
|
+
* @returns True if it's a valid BST, false otherwise.
|
|
2571
|
+
|
|
2572
|
+
|
|
2573
|
+
|
|
2574
|
+
|
|
2575
|
+
|
|
2576
|
+
|
|
2577
|
+
|
|
2578
|
+
|
|
2579
|
+
|
|
2580
|
+
|
|
2581
|
+
|
|
2582
|
+
|
|
2583
|
+
|
|
2584
|
+
|
|
2585
|
+
|
|
2586
|
+
|
|
2587
|
+
|
|
2588
|
+
|
|
2589
|
+
|
|
2590
|
+
|
|
2591
|
+
|
|
2592
|
+
|
|
2593
|
+
|
|
2594
|
+
|
|
2595
|
+
|
|
2596
|
+
|
|
2597
|
+
|
|
2598
|
+
|
|
2599
|
+
|
|
2600
|
+
|
|
2601
|
+
* @example
|
|
2602
|
+
* // Check BST property
|
|
2603
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2604
|
+
* // BinaryTree doesn't guarantee BST order
|
|
2605
|
+
* console.log(typeof tree.isBST()); // 'boolean';
|
|
2606
|
+
*/
|
|
1747
2607
|
isBST(startNode = this._root, iterationType = this.iterationType) {
|
|
1748
2608
|
const startNodeSired = this.ensureNode(startNode);
|
|
1749
2609
|
if (!startNodeSired) return true;
|
|
@@ -1781,13 +2641,50 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1781
2641
|
}
|
|
1782
2642
|
}
|
|
1783
2643
|
/**
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
2644
|
+
* Gets the depth of a node (distance from `startNode`).
|
|
2645
|
+
* @remarks Time O(H), where H is the depth of the `dist` node relative to `startNode`. O(N) worst-case. Space O(1).
|
|
2646
|
+
*
|
|
2647
|
+
* @param dist - The node to find the depth of.
|
|
2648
|
+
* @param [startNode=this._root] - The node to measure depth from (defaults to root).
|
|
2649
|
+
* @returns The depth (0 if `dist` is `startNode`).
|
|
2650
|
+
|
|
2651
|
+
|
|
2652
|
+
|
|
2653
|
+
|
|
2654
|
+
|
|
2655
|
+
|
|
2656
|
+
|
|
2657
|
+
|
|
2658
|
+
|
|
2659
|
+
|
|
2660
|
+
|
|
2661
|
+
|
|
2662
|
+
|
|
2663
|
+
|
|
2664
|
+
|
|
2665
|
+
|
|
2666
|
+
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
|
|
2670
|
+
|
|
2671
|
+
|
|
2672
|
+
|
|
2673
|
+
|
|
2674
|
+
|
|
2675
|
+
|
|
2676
|
+
|
|
2677
|
+
|
|
2678
|
+
|
|
2679
|
+
|
|
2680
|
+
|
|
2681
|
+
|
|
2682
|
+
* @example
|
|
2683
|
+
* // Get depth of a node
|
|
2684
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2685
|
+
* const node = tree.getNode(4);
|
|
2686
|
+
* console.log(tree.getDepth(node!)); // 2;
|
|
2687
|
+
*/
|
|
1791
2688
|
getDepth(dist, startNode = this._root) {
|
|
1792
2689
|
let distEnsured = this.ensureNode(dist);
|
|
1793
2690
|
const beginRootEnsured = this.ensureNode(startNode);
|
|
@@ -1802,13 +2699,49 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1802
2699
|
return depth;
|
|
1803
2700
|
}
|
|
1804
2701
|
/**
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
2702
|
+
* Gets the maximum height of the tree (longest path from startNode to a leaf).
|
|
2703
|
+
* @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).
|
|
2704
|
+
*
|
|
2705
|
+
* @param [startNode=this._root] - The node to start measuring from.
|
|
2706
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2707
|
+
* @returns The height ( -1 for an empty tree, 0 for a single-node tree).
|
|
2708
|
+
|
|
2709
|
+
|
|
2710
|
+
|
|
2711
|
+
|
|
2712
|
+
|
|
2713
|
+
|
|
2714
|
+
|
|
2715
|
+
|
|
2716
|
+
|
|
2717
|
+
|
|
2718
|
+
|
|
2719
|
+
|
|
2720
|
+
|
|
2721
|
+
|
|
2722
|
+
|
|
2723
|
+
|
|
2724
|
+
|
|
2725
|
+
|
|
2726
|
+
|
|
2727
|
+
|
|
2728
|
+
|
|
2729
|
+
|
|
2730
|
+
|
|
2731
|
+
|
|
2732
|
+
|
|
2733
|
+
|
|
2734
|
+
|
|
2735
|
+
|
|
2736
|
+
|
|
2737
|
+
|
|
2738
|
+
|
|
2739
|
+
|
|
2740
|
+
* @example
|
|
2741
|
+
* // Get tree height
|
|
2742
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2743
|
+
* console.log(tree.getHeight()); // 2;
|
|
2744
|
+
*/
|
|
1812
2745
|
getHeight(startNode = this._root, iterationType = this.iterationType) {
|
|
1813
2746
|
startNode = this.ensureNode(startNode);
|
|
1814
2747
|
if (!this.isRealNode(startNode)) return -1;
|
|
@@ -2244,24 +3177,95 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2244
3177
|
return ans;
|
|
2245
3178
|
}
|
|
2246
3179
|
/**
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
3180
|
+
* Clones the tree.
|
|
3181
|
+
* @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.
|
|
3182
|
+
*
|
|
3183
|
+
* @returns A new, cloned instance of the tree.
|
|
3184
|
+
|
|
3185
|
+
|
|
3186
|
+
|
|
3187
|
+
|
|
3188
|
+
|
|
3189
|
+
|
|
3190
|
+
|
|
3191
|
+
|
|
3192
|
+
|
|
3193
|
+
|
|
3194
|
+
|
|
3195
|
+
|
|
3196
|
+
|
|
3197
|
+
|
|
3198
|
+
|
|
3199
|
+
|
|
3200
|
+
|
|
3201
|
+
|
|
3202
|
+
|
|
3203
|
+
|
|
3204
|
+
|
|
3205
|
+
|
|
3206
|
+
|
|
3207
|
+
|
|
3208
|
+
|
|
3209
|
+
|
|
3210
|
+
|
|
3211
|
+
|
|
3212
|
+
|
|
3213
|
+
|
|
3214
|
+
* @example
|
|
3215
|
+
* // Deep copy
|
|
3216
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
3217
|
+
* const copy = tree.clone();
|
|
3218
|
+
* copy.delete(1);
|
|
3219
|
+
* console.log(tree.has(1)); // true;
|
|
3220
|
+
*/
|
|
2252
3221
|
clone() {
|
|
2253
3222
|
const out = this._createInstance();
|
|
2254
3223
|
this._clone(out);
|
|
2255
3224
|
return out;
|
|
2256
3225
|
}
|
|
2257
3226
|
/**
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
3227
|
+
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
3228
|
+
* @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.
|
|
3229
|
+
*
|
|
3230
|
+
* @param predicate - A function to test each [key, value] pair.
|
|
3231
|
+
* @param [thisArg] - `this` context for the predicate.
|
|
3232
|
+
* @returns A new, filtered tree.
|
|
3233
|
+
|
|
3234
|
+
|
|
3235
|
+
|
|
3236
|
+
|
|
3237
|
+
|
|
3238
|
+
|
|
3239
|
+
|
|
3240
|
+
|
|
3241
|
+
|
|
3242
|
+
|
|
3243
|
+
|
|
3244
|
+
|
|
3245
|
+
|
|
3246
|
+
|
|
3247
|
+
|
|
3248
|
+
|
|
3249
|
+
|
|
3250
|
+
|
|
3251
|
+
|
|
3252
|
+
|
|
3253
|
+
|
|
3254
|
+
|
|
3255
|
+
|
|
3256
|
+
|
|
3257
|
+
|
|
3258
|
+
|
|
3259
|
+
|
|
3260
|
+
|
|
3261
|
+
|
|
3262
|
+
|
|
3263
|
+
* @example
|
|
3264
|
+
* // Filter nodes by condition
|
|
3265
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4]);
|
|
3266
|
+
* const result = tree.filter((_, key) => key > 2);
|
|
3267
|
+
* console.log(result.size); // 2;
|
|
3268
|
+
*/
|
|
2265
3269
|
filter(predicate, thisArg) {
|
|
2266
3270
|
const out = this._createInstance();
|
|
2267
3271
|
let i = 0;
|
|
@@ -2269,17 +3273,52 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2269
3273
|
return out;
|
|
2270
3274
|
}
|
|
2271
3275
|
/**
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
3276
|
+
* Creates a new tree by mapping each [key, value] pair to a new entry.
|
|
3277
|
+
* @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.
|
|
3278
|
+
*
|
|
3279
|
+
* @template MK - New key type.
|
|
3280
|
+
* @template MV - New value type.
|
|
3281
|
+
* @template MR - New raw type.
|
|
3282
|
+
* @param cb - A function to map each [key, value] pair.
|
|
3283
|
+
* @param [options] - Options for the new tree.
|
|
3284
|
+
* @param [thisArg] - `this` context for the callback.
|
|
3285
|
+
* @returns A new, mapped tree.
|
|
3286
|
+
|
|
3287
|
+
|
|
3288
|
+
|
|
3289
|
+
|
|
3290
|
+
|
|
3291
|
+
|
|
3292
|
+
|
|
3293
|
+
|
|
3294
|
+
|
|
3295
|
+
|
|
3296
|
+
|
|
3297
|
+
|
|
3298
|
+
|
|
3299
|
+
|
|
3300
|
+
|
|
3301
|
+
|
|
3302
|
+
|
|
3303
|
+
|
|
3304
|
+
|
|
3305
|
+
|
|
3306
|
+
|
|
3307
|
+
|
|
3308
|
+
|
|
3309
|
+
|
|
3310
|
+
|
|
3311
|
+
|
|
3312
|
+
|
|
3313
|
+
|
|
3314
|
+
|
|
3315
|
+
|
|
3316
|
+
* @example
|
|
3317
|
+
* // Transform to new tree
|
|
3318
|
+
* const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
|
|
3319
|
+
* const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
|
|
3320
|
+
* console.log([...mapped.values()]); // contains 11;
|
|
3321
|
+
*/
|
|
2283
3322
|
map(cb, options, thisArg) {
|
|
2284
3323
|
const out = this._createLike([], options);
|
|
2285
3324
|
let i = 0;
|
|
@@ -2317,12 +3356,46 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2317
3356
|
return output;
|
|
2318
3357
|
}
|
|
2319
3358
|
/**
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
3359
|
+
* Prints a visual representation of the tree to the console.
|
|
3360
|
+
* @remarks Time O(N) (via `toVisual`). Space O(N*H) or O(N^2) (via `toVisual`).
|
|
3361
|
+
*
|
|
3362
|
+
* @param [options] - Options to control the output.
|
|
3363
|
+
* @param [startNode=this._root] - The node to start printing from.
|
|
3364
|
+
|
|
3365
|
+
|
|
3366
|
+
|
|
3367
|
+
|
|
3368
|
+
|
|
3369
|
+
|
|
3370
|
+
|
|
3371
|
+
|
|
3372
|
+
|
|
3373
|
+
|
|
3374
|
+
|
|
3375
|
+
|
|
3376
|
+
|
|
3377
|
+
|
|
3378
|
+
|
|
3379
|
+
|
|
3380
|
+
|
|
3381
|
+
|
|
3382
|
+
|
|
3383
|
+
|
|
3384
|
+
|
|
3385
|
+
|
|
3386
|
+
|
|
3387
|
+
|
|
3388
|
+
|
|
3389
|
+
|
|
3390
|
+
|
|
3391
|
+
|
|
3392
|
+
|
|
3393
|
+
|
|
3394
|
+
* @example
|
|
3395
|
+
* // Display tree
|
|
3396
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
3397
|
+
* expect(() => tree.print()).not.toThrow();
|
|
3398
|
+
*/
|
|
2326
3399
|
print(options, startNode = this._root) {
|
|
2327
3400
|
console.log(this.toVisual(startNode, options));
|
|
2328
3401
|
}
|
|
@@ -2561,7 +3634,6 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2561
3634
|
* @returns Layout information for this subtree.
|
|
2562
3635
|
*/
|
|
2563
3636
|
_displayAux(node, options) {
|
|
2564
|
-
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
2565
3637
|
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
2566
3638
|
const newFrame = /* @__PURE__ */ __name((n) => ({
|
|
2567
3639
|
node: n,
|