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
|
@@ -59,54 +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 _Range {
|
|
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
|
-
// Determine whether a key is within the range
|
|
101
|
-
isInRange(key, comparator) {
|
|
102
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
103
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
104
|
-
return lowCheck && highCheck;
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
__name(_Range, "Range");
|
|
108
|
-
var Range = _Range;
|
|
109
|
-
|
|
110
62
|
// src/data-structures/base/iterable-element-base.ts
|
|
111
63
|
var _IterableElementBase = class _IterableElementBase {
|
|
112
64
|
/**
|
|
@@ -129,7 +81,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
129
81
|
if (options) {
|
|
130
82
|
const { toElementFn } = options;
|
|
131
83
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
132
|
-
else if (toElementFn) throw new TypeError(
|
|
84
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
133
85
|
}
|
|
134
86
|
}
|
|
135
87
|
/**
|
|
@@ -285,7 +237,7 @@ var _IterableElementBase = class _IterableElementBase {
|
|
|
285
237
|
acc = initialValue;
|
|
286
238
|
} else {
|
|
287
239
|
const first = iter.next();
|
|
288
|
-
if (first.done) throw new TypeError(
|
|
240
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
289
241
|
acc = first.value;
|
|
290
242
|
index = 1;
|
|
291
243
|
}
|
|
@@ -521,6 +473,235 @@ var _LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
521
473
|
__name(_LinearBase, "LinearBase");
|
|
522
474
|
var LinearBase = _LinearBase;
|
|
523
475
|
|
|
476
|
+
// src/common/error.ts
|
|
477
|
+
var ERR = {
|
|
478
|
+
// Range / index
|
|
479
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
480
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
481
|
+
// Type / argument
|
|
482
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
483
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
484
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
485
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
486
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
487
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
488
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
489
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
490
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
491
|
+
// State / operation
|
|
492
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
493
|
+
// Matrix
|
|
494
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
495
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
496
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
497
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
498
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
// src/common/index.ts
|
|
502
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
503
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
504
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
505
|
+
return DFSOperation2;
|
|
506
|
+
})(DFSOperation || {});
|
|
507
|
+
var _Range = class _Range {
|
|
508
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
509
|
+
this.low = low;
|
|
510
|
+
this.high = high;
|
|
511
|
+
this.includeLow = includeLow;
|
|
512
|
+
this.includeHigh = includeHigh;
|
|
513
|
+
}
|
|
514
|
+
// Determine whether a key is within the range
|
|
515
|
+
isInRange(key, comparator) {
|
|
516
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
517
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
518
|
+
return lowCheck && highCheck;
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
__name(_Range, "Range");
|
|
522
|
+
var Range = _Range;
|
|
523
|
+
|
|
524
|
+
// src/data-structures/base/iterable-entry-base.ts
|
|
525
|
+
var _IterableEntryBase = class _IterableEntryBase {
|
|
526
|
+
/**
|
|
527
|
+
* Default iterator yielding `[key, value]` entries.
|
|
528
|
+
* @returns Iterator of `[K, V]`.
|
|
529
|
+
* @remarks Time O(n) to iterate, Space O(1)
|
|
530
|
+
*/
|
|
531
|
+
*[Symbol.iterator](...args) {
|
|
532
|
+
yield* this._getIterator(...args);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
536
|
+
* @returns Iterator of `[K, V | undefined]`.
|
|
537
|
+
* @remarks Time O(n), Space O(1)
|
|
538
|
+
*/
|
|
539
|
+
*entries() {
|
|
540
|
+
for (const item of this) {
|
|
541
|
+
yield item;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Iterate over keys only.
|
|
546
|
+
* @returns Iterator of keys.
|
|
547
|
+
* @remarks Time O(n), Space O(1)
|
|
548
|
+
*/
|
|
549
|
+
*keys() {
|
|
550
|
+
for (const item of this) {
|
|
551
|
+
yield item[0];
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Iterate over values only.
|
|
556
|
+
* @returns Iterator of values.
|
|
557
|
+
* @remarks Time O(n), Space O(1)
|
|
558
|
+
*/
|
|
559
|
+
*values() {
|
|
560
|
+
for (const item of this) {
|
|
561
|
+
yield item[1];
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Test whether all entries satisfy the predicate.
|
|
566
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
567
|
+
* @param thisArg - Optional `this` for callback.
|
|
568
|
+
* @returns `true` if all pass; otherwise `false`.
|
|
569
|
+
* @remarks Time O(n), Space O(1)
|
|
570
|
+
*/
|
|
571
|
+
every(predicate, thisArg) {
|
|
572
|
+
let index = 0;
|
|
573
|
+
for (const item of this) {
|
|
574
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
575
|
+
return false;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return true;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Test whether any entry satisfies the predicate.
|
|
582
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
583
|
+
* @param thisArg - Optional `this` for callback.
|
|
584
|
+
* @returns `true` if any passes; otherwise `false`.
|
|
585
|
+
* @remarks Time O(n), Space O(1)
|
|
586
|
+
*/
|
|
587
|
+
some(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 true;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return false;
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Visit each entry, left-to-right.
|
|
598
|
+
* @param callbackfn - `(key, value, index, self) => void`.
|
|
599
|
+
* @param thisArg - Optional `this` for callback.
|
|
600
|
+
* @remarks Time O(n), Space O(1)
|
|
601
|
+
*/
|
|
602
|
+
forEach(callbackfn, thisArg) {
|
|
603
|
+
let index = 0;
|
|
604
|
+
for (const item of this) {
|
|
605
|
+
const [key, value] = item;
|
|
606
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Find the first entry that matches a predicate.
|
|
611
|
+
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
612
|
+
* @param thisArg - Optional `this` for callback.
|
|
613
|
+
* @returns Matching `[key, value]` or `undefined`.
|
|
614
|
+
* @remarks Time O(n), Space O(1)
|
|
615
|
+
*/
|
|
616
|
+
find(callbackfn, thisArg) {
|
|
617
|
+
let index = 0;
|
|
618
|
+
for (const item of this) {
|
|
619
|
+
const [key, value] = item;
|
|
620
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
621
|
+
}
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Whether the given key exists.
|
|
626
|
+
* @param key - Key to test.
|
|
627
|
+
* @returns `true` if found; otherwise `false`.
|
|
628
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
629
|
+
*/
|
|
630
|
+
has(key) {
|
|
631
|
+
for (const item of this) {
|
|
632
|
+
const [itemKey] = item;
|
|
633
|
+
if (itemKey === key) return true;
|
|
634
|
+
}
|
|
635
|
+
return false;
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Whether there exists an entry with the given value.
|
|
639
|
+
* @param value - Value to test.
|
|
640
|
+
* @returns `true` if found; otherwise `false`.
|
|
641
|
+
* @remarks Time O(n), Space O(1)
|
|
642
|
+
*/
|
|
643
|
+
hasValue(value) {
|
|
644
|
+
for (const [, elementValue] of this) {
|
|
645
|
+
if (elementValue === value) return true;
|
|
646
|
+
}
|
|
647
|
+
return false;
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Get the value under a key.
|
|
651
|
+
* @param key - Key to look up.
|
|
652
|
+
* @returns Value or `undefined`.
|
|
653
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
654
|
+
*/
|
|
655
|
+
get(key) {
|
|
656
|
+
for (const item of this) {
|
|
657
|
+
const [itemKey, value] = item;
|
|
658
|
+
if (itemKey === key) return value;
|
|
659
|
+
}
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Reduce entries into a single accumulator.
|
|
664
|
+
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
665
|
+
* @param initialValue - Initial accumulator.
|
|
666
|
+
* @returns Final accumulator.
|
|
667
|
+
* @remarks Time O(n), Space O(1)
|
|
668
|
+
*/
|
|
669
|
+
reduce(callbackfn, initialValue) {
|
|
670
|
+
let accumulator = initialValue;
|
|
671
|
+
let index = 0;
|
|
672
|
+
for (const item of this) {
|
|
673
|
+
const [key, value] = item;
|
|
674
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
675
|
+
}
|
|
676
|
+
return accumulator;
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Converts data structure to `[key, value]` pairs.
|
|
680
|
+
* @returns Array of entries.
|
|
681
|
+
* @remarks Time O(n), Space O(n)
|
|
682
|
+
*/
|
|
683
|
+
toArray() {
|
|
684
|
+
return [...this];
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
688
|
+
* @returns Array of entries (default) or a string.
|
|
689
|
+
* @remarks Time O(n), Space O(n)
|
|
690
|
+
*/
|
|
691
|
+
toVisual() {
|
|
692
|
+
return [...this];
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Print a human-friendly representation to the console.
|
|
696
|
+
* @remarks Time O(n), Space O(n)
|
|
697
|
+
*/
|
|
698
|
+
print() {
|
|
699
|
+
console.log(this.toVisual());
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
__name(_IterableEntryBase, "IterableEntryBase");
|
|
703
|
+
var IterableEntryBase = _IterableEntryBase;
|
|
704
|
+
|
|
524
705
|
// src/data-structures/queue/queue.ts
|
|
525
706
|
var _Queue = class _Queue extends LinearBase {
|
|
526
707
|
/**
|
|
@@ -575,18 +756,94 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
575
756
|
this._autoCompactRatio = value;
|
|
576
757
|
}
|
|
577
758
|
/**
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
759
|
+
* Get the number of elements currently in the queue.
|
|
760
|
+
* @remarks Time O(1), Space O(1)
|
|
761
|
+
* @returns Current length.
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
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
|
+
* @example
|
|
795
|
+
* // Track queue length
|
|
796
|
+
* const q = new Queue<number>();
|
|
797
|
+
* console.log(q.length); // 0;
|
|
798
|
+
* q.push(1);
|
|
799
|
+
* q.push(2);
|
|
800
|
+
* console.log(q.length); // 2;
|
|
801
|
+
*/
|
|
582
802
|
get length() {
|
|
583
803
|
return this.elements.length - this._offset;
|
|
584
804
|
}
|
|
585
805
|
/**
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
806
|
+
* Get the first element (front) without removing it.
|
|
807
|
+
* @remarks Time O(1), Space O(1)
|
|
808
|
+
* @returns Front element or undefined.
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
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
|
+
* @example
|
|
842
|
+
* // View the front element
|
|
843
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
844
|
+
* console.log(q.first); // 'first';
|
|
845
|
+
* console.log(q.length); // 3;
|
|
846
|
+
*/
|
|
590
847
|
get first() {
|
|
591
848
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
592
849
|
}
|
|
@@ -609,19 +866,111 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
609
866
|
return new _Queue(elements);
|
|
610
867
|
}
|
|
611
868
|
/**
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
869
|
+
* Check whether the queue is empty.
|
|
870
|
+
* @remarks Time O(1), Space O(1)
|
|
871
|
+
* @returns True if length is 0.
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
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
|
+
* @example
|
|
905
|
+
* // Queue for...of iteration and isEmpty check
|
|
906
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
907
|
+
*
|
|
908
|
+
* const elements: string[] = [];
|
|
909
|
+
* for (const item of queue) {
|
|
910
|
+
* elements.push(item);
|
|
911
|
+
* }
|
|
912
|
+
*
|
|
913
|
+
* // Verify all elements are iterated in order
|
|
914
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
915
|
+
*
|
|
916
|
+
* // Process all elements
|
|
917
|
+
* while (queue.length > 0) {
|
|
918
|
+
* queue.shift();
|
|
919
|
+
* }
|
|
920
|
+
*
|
|
921
|
+
* console.log(queue.length); // 0;
|
|
922
|
+
*/
|
|
616
923
|
isEmpty() {
|
|
617
924
|
return this.length === 0;
|
|
618
925
|
}
|
|
619
926
|
/**
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
927
|
+
* Enqueue one element at the back.
|
|
928
|
+
* @remarks Time O(1), Space O(1)
|
|
929
|
+
* @param element - Element to enqueue.
|
|
930
|
+
* @returns True on success.
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
* @example
|
|
964
|
+
* // basic Queue creation and push operation
|
|
965
|
+
* // Create a simple Queue with initial values
|
|
966
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
967
|
+
*
|
|
968
|
+
* // Verify the queue maintains insertion order
|
|
969
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
970
|
+
*
|
|
971
|
+
* // Check length
|
|
972
|
+
* console.log(queue.length); // 5;
|
|
973
|
+
*/
|
|
625
974
|
push(element) {
|
|
626
975
|
this.elements.push(element);
|
|
627
976
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -642,10 +991,56 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
642
991
|
return ans;
|
|
643
992
|
}
|
|
644
993
|
/**
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
994
|
+
* Dequeue one element from the front (amortized via offset).
|
|
995
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
996
|
+
* @returns Removed element or undefined.
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
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
|
+
* @example
|
|
1030
|
+
* // Queue shift and peek operations
|
|
1031
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
1032
|
+
*
|
|
1033
|
+
* // Peek at the front element without removing it
|
|
1034
|
+
* console.log(queue.first); // 10;
|
|
1035
|
+
*
|
|
1036
|
+
* // Remove and get the first element (FIFO)
|
|
1037
|
+
* const first = queue.shift();
|
|
1038
|
+
* console.log(first); // 10;
|
|
1039
|
+
*
|
|
1040
|
+
* // Verify remaining elements and length decreased
|
|
1041
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
1042
|
+
* console.log(queue.length); // 3;
|
|
1043
|
+
*/
|
|
649
1044
|
shift() {
|
|
650
1045
|
if (this.length === 0) return void 0;
|
|
651
1046
|
const first = this.first;
|
|
@@ -654,11 +1049,45 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
654
1049
|
return first;
|
|
655
1050
|
}
|
|
656
1051
|
/**
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
1052
|
+
* Delete the first occurrence of a specific element.
|
|
1053
|
+
* @remarks Time O(N), Space O(1)
|
|
1054
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
1055
|
+
* @returns True if an element was removed.
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
* @example
|
|
1086
|
+
* // Remove specific element
|
|
1087
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
1088
|
+
* q.delete(2);
|
|
1089
|
+
* console.log(q.length); // 3;
|
|
1090
|
+
*/
|
|
662
1091
|
delete(element) {
|
|
663
1092
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
664
1093
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -669,11 +1098,45 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
669
1098
|
return false;
|
|
670
1099
|
}
|
|
671
1100
|
/**
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
1101
|
+
* Get the element at a given logical index.
|
|
1102
|
+
* @remarks Time O(1), Space O(1)
|
|
1103
|
+
* @param index - Zero-based index from the front.
|
|
1104
|
+
* @returns Element or undefined.
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
* @example
|
|
1135
|
+
* // Access element by index
|
|
1136
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
1137
|
+
* console.log(q.at(0)); // 'a';
|
|
1138
|
+
* console.log(q.at(2)); // 'c';
|
|
1139
|
+
*/
|
|
677
1140
|
at(index) {
|
|
678
1141
|
if (index < 0 || index >= this.length) return void 0;
|
|
679
1142
|
return this._elements[this._offset + index];
|
|
@@ -725,19 +1188,90 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
725
1188
|
return this;
|
|
726
1189
|
}
|
|
727
1190
|
/**
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
1191
|
+
* Remove all elements and reset offset.
|
|
1192
|
+
* @remarks Time O(1), Space O(1)
|
|
1193
|
+
* @returns void
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
* @example
|
|
1225
|
+
* // Remove all elements
|
|
1226
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1227
|
+
* q.clear();
|
|
1228
|
+
* console.log(q.length); // 0;
|
|
1229
|
+
*/
|
|
732
1230
|
clear() {
|
|
733
1231
|
this._elements = [];
|
|
734
1232
|
this._offset = 0;
|
|
735
1233
|
}
|
|
736
1234
|
/**
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
1235
|
+
* Compact storage by discarding consumed head elements.
|
|
1236
|
+
* @remarks Time O(N), Space O(N)
|
|
1237
|
+
* @returns True when compaction performed.
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
* @example
|
|
1268
|
+
* // Reclaim unused memory
|
|
1269
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1270
|
+
* q.shift();
|
|
1271
|
+
* q.shift();
|
|
1272
|
+
* q.compact();
|
|
1273
|
+
* console.log(q.length); // 3;
|
|
1274
|
+
*/
|
|
741
1275
|
compact() {
|
|
742
1276
|
this._elements = this.elements.slice(this._offset);
|
|
743
1277
|
this._offset = 0;
|
|
@@ -763,10 +1297,47 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
763
1297
|
return removed;
|
|
764
1298
|
}
|
|
765
1299
|
/**
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
1300
|
+
* Deep clone this queue and its parameters.
|
|
1301
|
+
* @remarks Time O(N), Space O(N)
|
|
1302
|
+
* @returns A new queue with the same content and options.
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
|
|
1308
|
+
|
|
1309
|
+
|
|
1310
|
+
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
|
|
1315
|
+
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
|
|
1321
|
+
|
|
1322
|
+
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
|
|
1326
|
+
|
|
1327
|
+
|
|
1328
|
+
|
|
1329
|
+
|
|
1330
|
+
|
|
1331
|
+
|
|
1332
|
+
|
|
1333
|
+
* @example
|
|
1334
|
+
* // Create independent copy
|
|
1335
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1336
|
+
* const copy = q.clone();
|
|
1337
|
+
* copy.shift();
|
|
1338
|
+
* console.log(q.length); // 3;
|
|
1339
|
+
* console.log(copy.length); // 2;
|
|
1340
|
+
*/
|
|
770
1341
|
clone() {
|
|
771
1342
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
772
1343
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -774,12 +1345,47 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
774
1345
|
return out;
|
|
775
1346
|
}
|
|
776
1347
|
/**
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
1348
|
+
* Filter elements into a new queue of the same class.
|
|
1349
|
+
* @remarks Time O(N), Space O(N)
|
|
1350
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1351
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1352
|
+
* @returns A new queue with kept elements.
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
|
|
1383
|
+
* @example
|
|
1384
|
+
* // Filter elements
|
|
1385
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1386
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1387
|
+
* console.log(evens.length); // 2;
|
|
1388
|
+
*/
|
|
783
1389
|
filter(predicate, thisArg) {
|
|
784
1390
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
785
1391
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -791,15 +1397,49 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
791
1397
|
return out;
|
|
792
1398
|
}
|
|
793
1399
|
/**
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
1400
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1401
|
+
* @remarks Time O(N), Space O(N)
|
|
1402
|
+
* @template EM
|
|
1403
|
+
* @template RM
|
|
1404
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1405
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1406
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1407
|
+
* @returns A new Queue with mapped elements.
|
|
1408
|
+
|
|
1409
|
+
|
|
1410
|
+
|
|
1411
|
+
|
|
1412
|
+
|
|
1413
|
+
|
|
1414
|
+
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
|
|
1431
|
+
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
|
|
1435
|
+
|
|
1436
|
+
|
|
1437
|
+
* @example
|
|
1438
|
+
* // Transform elements
|
|
1439
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1440
|
+
* const doubled = q.map(x => x * 2);
|
|
1441
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1442
|
+
*/
|
|
803
1443
|
map(callback, options, thisArg) {
|
|
804
1444
|
var _a, _b;
|
|
805
1445
|
const out = new this.constructor([], {
|
|
@@ -890,187 +1530,6 @@ var _Queue = class _Queue extends LinearBase {
|
|
|
890
1530
|
__name(_Queue, "Queue");
|
|
891
1531
|
var Queue = _Queue;
|
|
892
1532
|
|
|
893
|
-
// src/data-structures/base/iterable-entry-base.ts
|
|
894
|
-
var _IterableEntryBase = class _IterableEntryBase {
|
|
895
|
-
/**
|
|
896
|
-
* Default iterator yielding `[key, value]` entries.
|
|
897
|
-
* @returns Iterator of `[K, V]`.
|
|
898
|
-
* @remarks Time O(n) to iterate, Space O(1)
|
|
899
|
-
*/
|
|
900
|
-
*[Symbol.iterator](...args) {
|
|
901
|
-
yield* this._getIterator(...args);
|
|
902
|
-
}
|
|
903
|
-
/**
|
|
904
|
-
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
905
|
-
* @returns Iterator of `[K, V | undefined]`.
|
|
906
|
-
* @remarks Time O(n), Space O(1)
|
|
907
|
-
*/
|
|
908
|
-
*entries() {
|
|
909
|
-
for (const item of this) {
|
|
910
|
-
yield item;
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
/**
|
|
914
|
-
* Iterate over keys only.
|
|
915
|
-
* @returns Iterator of keys.
|
|
916
|
-
* @remarks Time O(n), Space O(1)
|
|
917
|
-
*/
|
|
918
|
-
*keys() {
|
|
919
|
-
for (const item of this) {
|
|
920
|
-
yield item[0];
|
|
921
|
-
}
|
|
922
|
-
}
|
|
923
|
-
/**
|
|
924
|
-
* Iterate over values only.
|
|
925
|
-
* @returns Iterator of values.
|
|
926
|
-
* @remarks Time O(n), Space O(1)
|
|
927
|
-
*/
|
|
928
|
-
*values() {
|
|
929
|
-
for (const item of this) {
|
|
930
|
-
yield item[1];
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
/**
|
|
934
|
-
* Test whether all entries satisfy the predicate.
|
|
935
|
-
* @param predicate - `(key, value, index, self) => boolean`.
|
|
936
|
-
* @param thisArg - Optional `this` for callback.
|
|
937
|
-
* @returns `true` if all pass; otherwise `false`.
|
|
938
|
-
* @remarks Time O(n), Space O(1)
|
|
939
|
-
*/
|
|
940
|
-
every(predicate, thisArg) {
|
|
941
|
-
let index = 0;
|
|
942
|
-
for (const item of this) {
|
|
943
|
-
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
944
|
-
return false;
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
return true;
|
|
948
|
-
}
|
|
949
|
-
/**
|
|
950
|
-
* Test whether any entry satisfies the predicate.
|
|
951
|
-
* @param predicate - `(key, value, index, self) => boolean`.
|
|
952
|
-
* @param thisArg - Optional `this` for callback.
|
|
953
|
-
* @returns `true` if any passes; otherwise `false`.
|
|
954
|
-
* @remarks Time O(n), Space O(1)
|
|
955
|
-
*/
|
|
956
|
-
some(predicate, thisArg) {
|
|
957
|
-
let index = 0;
|
|
958
|
-
for (const item of this) {
|
|
959
|
-
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
960
|
-
return true;
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
return false;
|
|
964
|
-
}
|
|
965
|
-
/**
|
|
966
|
-
* Visit each entry, left-to-right.
|
|
967
|
-
* @param callbackfn - `(key, value, index, self) => void`.
|
|
968
|
-
* @param thisArg - Optional `this` for callback.
|
|
969
|
-
* @remarks Time O(n), Space O(1)
|
|
970
|
-
*/
|
|
971
|
-
forEach(callbackfn, thisArg) {
|
|
972
|
-
let index = 0;
|
|
973
|
-
for (const item of this) {
|
|
974
|
-
const [key, value] = item;
|
|
975
|
-
callbackfn.call(thisArg, value, key, index++, this);
|
|
976
|
-
}
|
|
977
|
-
}
|
|
978
|
-
/**
|
|
979
|
-
* Find the first entry that matches a predicate.
|
|
980
|
-
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
981
|
-
* @param thisArg - Optional `this` for callback.
|
|
982
|
-
* @returns Matching `[key, value]` or `undefined`.
|
|
983
|
-
* @remarks Time O(n), Space O(1)
|
|
984
|
-
*/
|
|
985
|
-
find(callbackfn, thisArg) {
|
|
986
|
-
let index = 0;
|
|
987
|
-
for (const item of this) {
|
|
988
|
-
const [key, value] = item;
|
|
989
|
-
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
990
|
-
}
|
|
991
|
-
return;
|
|
992
|
-
}
|
|
993
|
-
/**
|
|
994
|
-
* Whether the given key exists.
|
|
995
|
-
* @param key - Key to test.
|
|
996
|
-
* @returns `true` if found; otherwise `false`.
|
|
997
|
-
* @remarks Time O(n) generic, Space O(1)
|
|
998
|
-
*/
|
|
999
|
-
has(key) {
|
|
1000
|
-
for (const item of this) {
|
|
1001
|
-
const [itemKey] = item;
|
|
1002
|
-
if (itemKey === key) return true;
|
|
1003
|
-
}
|
|
1004
|
-
return false;
|
|
1005
|
-
}
|
|
1006
|
-
/**
|
|
1007
|
-
* Whether there exists an entry with the given value.
|
|
1008
|
-
* @param value - Value to test.
|
|
1009
|
-
* @returns `true` if found; otherwise `false`.
|
|
1010
|
-
* @remarks Time O(n), Space O(1)
|
|
1011
|
-
*/
|
|
1012
|
-
hasValue(value) {
|
|
1013
|
-
for (const [, elementValue] of this) {
|
|
1014
|
-
if (elementValue === value) return true;
|
|
1015
|
-
}
|
|
1016
|
-
return false;
|
|
1017
|
-
}
|
|
1018
|
-
/**
|
|
1019
|
-
* Get the value under a key.
|
|
1020
|
-
* @param key - Key to look up.
|
|
1021
|
-
* @returns Value or `undefined`.
|
|
1022
|
-
* @remarks Time O(n) generic, Space O(1)
|
|
1023
|
-
*/
|
|
1024
|
-
get(key) {
|
|
1025
|
-
for (const item of this) {
|
|
1026
|
-
const [itemKey, value] = item;
|
|
1027
|
-
if (itemKey === key) return value;
|
|
1028
|
-
}
|
|
1029
|
-
return;
|
|
1030
|
-
}
|
|
1031
|
-
/**
|
|
1032
|
-
* Reduce entries into a single accumulator.
|
|
1033
|
-
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
1034
|
-
* @param initialValue - Initial accumulator.
|
|
1035
|
-
* @returns Final accumulator.
|
|
1036
|
-
* @remarks Time O(n), Space O(1)
|
|
1037
|
-
*/
|
|
1038
|
-
reduce(callbackfn, initialValue) {
|
|
1039
|
-
let accumulator = initialValue;
|
|
1040
|
-
let index = 0;
|
|
1041
|
-
for (const item of this) {
|
|
1042
|
-
const [key, value] = item;
|
|
1043
|
-
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
1044
|
-
}
|
|
1045
|
-
return accumulator;
|
|
1046
|
-
}
|
|
1047
|
-
/**
|
|
1048
|
-
* Converts data structure to `[key, value]` pairs.
|
|
1049
|
-
* @returns Array of entries.
|
|
1050
|
-
* @remarks Time O(n), Space O(n)
|
|
1051
|
-
*/
|
|
1052
|
-
toArray() {
|
|
1053
|
-
return [...this];
|
|
1054
|
-
}
|
|
1055
|
-
/**
|
|
1056
|
-
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
1057
|
-
* @returns Array of entries (default) or a string.
|
|
1058
|
-
* @remarks Time O(n), Space O(n)
|
|
1059
|
-
*/
|
|
1060
|
-
toVisual() {
|
|
1061
|
-
return [...this];
|
|
1062
|
-
}
|
|
1063
|
-
/**
|
|
1064
|
-
* Print a human-friendly representation to the console.
|
|
1065
|
-
* @remarks Time O(n), Space O(n)
|
|
1066
|
-
*/
|
|
1067
|
-
print() {
|
|
1068
|
-
console.log(this.toVisual());
|
|
1069
|
-
}
|
|
1070
|
-
};
|
|
1071
|
-
__name(_IterableEntryBase, "IterableEntryBase");
|
|
1072
|
-
var IterableEntryBase = _IterableEntryBase;
|
|
1073
|
-
|
|
1074
1533
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
1075
1534
|
var _BinaryTreeNode = class _BinaryTreeNode {
|
|
1076
1535
|
/**
|
|
@@ -1448,23 +1907,113 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1448
1907
|
return isComparable(key);
|
|
1449
1908
|
}
|
|
1450
1909
|
/**
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1910
|
+
* Adds a new node to the tree.
|
|
1911
|
+
* @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).
|
|
1912
|
+
*
|
|
1913
|
+
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1914
|
+
* @returns True if the addition was successful, false otherwise.
|
|
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
|
+
|
|
1942
|
+
* @example
|
|
1943
|
+
* // Add a single node
|
|
1944
|
+
* const tree = new BinaryTree<number>();
|
|
1945
|
+
* tree.add(1);
|
|
1946
|
+
* tree.add(2);
|
|
1947
|
+
* tree.add(3);
|
|
1948
|
+
* console.log(tree.size); // 3;
|
|
1949
|
+
* console.log(tree.has(1)); // true;
|
|
1950
|
+
*/
|
|
1457
1951
|
add(keyNodeOrEntry) {
|
|
1458
1952
|
return this.set(keyNodeOrEntry);
|
|
1459
1953
|
}
|
|
1460
1954
|
/**
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1955
|
+
* Adds or updates a new node to the tree.
|
|
1956
|
+
* @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).
|
|
1957
|
+
*
|
|
1958
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1959
|
+
* @param [value] - The value, if providing just a key.
|
|
1960
|
+
* @returns True if the addition was successful, false otherwise.
|
|
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
|
+
|
|
1993
|
+
* @example
|
|
1994
|
+
* // basic BinaryTree creation and insertion
|
|
1995
|
+
* // Create a BinaryTree with entries
|
|
1996
|
+
* const entries: [number, string][] = [
|
|
1997
|
+
* [6, 'six'],
|
|
1998
|
+
* [1, 'one'],
|
|
1999
|
+
* [2, 'two'],
|
|
2000
|
+
* [7, 'seven'],
|
|
2001
|
+
* [5, 'five'],
|
|
2002
|
+
* [3, 'three'],
|
|
2003
|
+
* [4, 'four'],
|
|
2004
|
+
* [9, 'nine'],
|
|
2005
|
+
* [8, 'eight']
|
|
2006
|
+
* ];
|
|
2007
|
+
*
|
|
2008
|
+
* const tree = new BinaryTree(entries);
|
|
2009
|
+
*
|
|
2010
|
+
* // Verify size
|
|
2011
|
+
* console.log(tree.size); // 9;
|
|
2012
|
+
*
|
|
2013
|
+
* // Add new element
|
|
2014
|
+
* tree.set(10, 'ten');
|
|
2015
|
+
* console.log(tree.size); // 10;
|
|
2016
|
+
*/
|
|
1468
2017
|
set(keyNodeOrEntry, value) {
|
|
1469
2018
|
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1470
2019
|
if (newNode === void 0) return false;
|
|
@@ -1509,23 +2058,86 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1509
2058
|
return false;
|
|
1510
2059
|
}
|
|
1511
2060
|
/**
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
2061
|
+
* Adds multiple items to the tree.
|
|
2062
|
+
* @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).
|
|
2063
|
+
*
|
|
2064
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
2065
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
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
|
+
|
|
2096
|
+
* @example
|
|
2097
|
+
* // Bulk add
|
|
2098
|
+
* const tree = new BinaryTree<number>();
|
|
2099
|
+
* tree.addMany([1, 2, 3, 4, 5]);
|
|
2100
|
+
* console.log(tree.size); // 5;
|
|
2101
|
+
*/
|
|
1518
2102
|
addMany(keysNodesEntriesOrRaws) {
|
|
1519
2103
|
return this.setMany(keysNodesEntriesOrRaws);
|
|
1520
2104
|
}
|
|
1521
2105
|
/**
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
2106
|
+
* Adds or updates multiple items to the tree.
|
|
2107
|
+
* @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).
|
|
2108
|
+
*
|
|
2109
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
2110
|
+
* @param [values] - An optional parallel iterable of values.
|
|
2111
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
2112
|
+
|
|
2113
|
+
|
|
2114
|
+
|
|
2115
|
+
|
|
2116
|
+
|
|
2117
|
+
|
|
2118
|
+
|
|
2119
|
+
|
|
2120
|
+
|
|
2121
|
+
|
|
2122
|
+
|
|
2123
|
+
|
|
2124
|
+
|
|
2125
|
+
|
|
2126
|
+
|
|
2127
|
+
|
|
2128
|
+
|
|
2129
|
+
|
|
2130
|
+
|
|
2131
|
+
|
|
2132
|
+
|
|
2133
|
+
|
|
2134
|
+
|
|
2135
|
+
* @example
|
|
2136
|
+
* // Set multiple entries
|
|
2137
|
+
* const tree = new BinaryTree<number, string>();
|
|
2138
|
+
* tree.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
2139
|
+
* console.log(tree.size); // 3;
|
|
2140
|
+
*/
|
|
1529
2141
|
setMany(keysNodesEntriesOrRaws, values) {
|
|
1530
2142
|
const inserted = [];
|
|
1531
2143
|
let valuesIterator;
|
|
@@ -1546,11 +2158,47 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1546
2158
|
return inserted;
|
|
1547
2159
|
}
|
|
1548
2160
|
/**
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
2161
|
+
* Merges another tree into this one by seting all its nodes.
|
|
2162
|
+
* @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`).
|
|
2163
|
+
*
|
|
2164
|
+
* @param anotherTree - The tree to merge.
|
|
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
|
+
|
|
2195
|
+
* @example
|
|
2196
|
+
* // Combine trees
|
|
2197
|
+
* const t1 = new BinaryTree<number>([1, 2]);
|
|
2198
|
+
* const t2 = new BinaryTree<number>([3, 4]);
|
|
2199
|
+
* t1.merge(t2);
|
|
2200
|
+
* console.log(t1.size); // 4;
|
|
2201
|
+
*/
|
|
1554
2202
|
merge(anotherTree) {
|
|
1555
2203
|
this.setMany(anotherTree, []);
|
|
1556
2204
|
}
|
|
@@ -1566,12 +2214,50 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1566
2214
|
this.setMany(keysNodesEntriesOrRaws, values);
|
|
1567
2215
|
}
|
|
1568
2216
|
/**
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
2217
|
+
* Deletes a node from the tree.
|
|
2218
|
+
* @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).
|
|
2219
|
+
*
|
|
2220
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
2221
|
+
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
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
|
+
|
|
2254
|
+
* @example
|
|
2255
|
+
* // Remove a node
|
|
2256
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2257
|
+
* tree.delete(3);
|
|
2258
|
+
* console.log(tree.has(3)); // false;
|
|
2259
|
+
* console.log(tree.size); // 4;
|
|
2260
|
+
*/
|
|
1575
2261
|
delete(keyNodeEntryRawOrPredicate) {
|
|
1576
2262
|
const deletedResult = [];
|
|
1577
2263
|
if (!this._root) return deletedResult;
|
|
@@ -1665,14 +2351,48 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1665
2351
|
return this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
1666
2352
|
}
|
|
1667
2353
|
/**
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
2354
|
+
* Gets the first node matching a predicate.
|
|
2355
|
+
* @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`).
|
|
2356
|
+
*
|
|
2357
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
2358
|
+
* @param [startNode=this._root] - The node to start the search from.
|
|
2359
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2360
|
+
* @returns The first matching node, or undefined if not found.
|
|
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
|
+
|
|
2391
|
+
* @example
|
|
2392
|
+
* // Get node by key
|
|
2393
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
|
|
2394
|
+
* console.log(tree.getNode(2)?.value); // 'child';
|
|
2395
|
+
*/
|
|
1676
2396
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1677
2397
|
if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
|
|
1678
2398
|
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -1684,14 +2404,51 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1684
2404
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
|
|
1685
2405
|
}
|
|
1686
2406
|
/**
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
2407
|
+
* Gets the value associated with a key.
|
|
2408
|
+
* @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.
|
|
2409
|
+
*
|
|
2410
|
+
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2411
|
+
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
2412
|
+
* @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
|
|
2413
|
+
* @returns The associated value, or undefined.
|
|
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
|
+
|
|
2446
|
+
* @example
|
|
2447
|
+
* // Retrieve value by key
|
|
2448
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
|
|
2449
|
+
* console.log(tree.get(2)); // 'left';
|
|
2450
|
+
* console.log(tree.get(99)); // undefined;
|
|
2451
|
+
*/
|
|
1695
2452
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1696
2453
|
var _a, _b;
|
|
1697
2454
|
if (this._isMapMode) {
|
|
@@ -1712,19 +2469,87 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1712
2469
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
1713
2470
|
}
|
|
1714
2471
|
/**
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
2472
|
+
* Clears the tree of all nodes and values.
|
|
2473
|
+
* @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
|
|
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
|
+
|
|
2503
|
+
|
|
2504
|
+
* @example
|
|
2505
|
+
* // Remove all nodes
|
|
2506
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2507
|
+
* tree.clear();
|
|
2508
|
+
* console.log(tree.isEmpty()); // true;
|
|
2509
|
+
*/
|
|
1718
2510
|
clear() {
|
|
1719
2511
|
this._clearNodes();
|
|
1720
2512
|
if (this._isMapMode) this._clearValues();
|
|
1721
2513
|
}
|
|
1722
2514
|
/**
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
2515
|
+
* Checks if the tree is empty.
|
|
2516
|
+
* @remarks Time O(1), Space O(1)
|
|
2517
|
+
*
|
|
2518
|
+
* @returns True if the tree has no nodes, false otherwise.
|
|
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
|
+
|
|
2548
|
+
|
|
2549
|
+
* @example
|
|
2550
|
+
* // Check empty
|
|
2551
|
+
* console.log(new BinaryTree().isEmpty()); // true;
|
|
2552
|
+
*/
|
|
1728
2553
|
isEmpty() {
|
|
1729
2554
|
return this._size === 0;
|
|
1730
2555
|
}
|
|
@@ -1739,13 +2564,48 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1739
2564
|
return this.getMinHeight(startNode) + 1 >= this.getHeight(startNode);
|
|
1740
2565
|
}
|
|
1741
2566
|
/**
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
2567
|
+
* Checks if the tree is a valid Binary Search Tree (BST).
|
|
2568
|
+
* @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).
|
|
2569
|
+
*
|
|
2570
|
+
* @param [startNode=this._root] - The node to start checking from.
|
|
2571
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2572
|
+
* @returns True if it's a valid BST, false otherwise.
|
|
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
|
+
|
|
2602
|
+
|
|
2603
|
+
* @example
|
|
2604
|
+
* // Check BST property
|
|
2605
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2606
|
+
* // BinaryTree doesn't guarantee BST order
|
|
2607
|
+
* console.log(typeof tree.isBST()); // 'boolean';
|
|
2608
|
+
*/
|
|
1749
2609
|
isBST(startNode = this._root, iterationType = this.iterationType) {
|
|
1750
2610
|
const startNodeSired = this.ensureNode(startNode);
|
|
1751
2611
|
if (!startNodeSired) return true;
|
|
@@ -1783,13 +2643,50 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1783
2643
|
}
|
|
1784
2644
|
}
|
|
1785
2645
|
/**
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
2646
|
+
* Gets the depth of a node (distance from `startNode`).
|
|
2647
|
+
* @remarks Time O(H), where H is the depth of the `dist` node relative to `startNode`. O(N) worst-case. Space O(1).
|
|
2648
|
+
*
|
|
2649
|
+
* @param dist - The node to find the depth of.
|
|
2650
|
+
* @param [startNode=this._root] - The node to measure depth from (defaults to root).
|
|
2651
|
+
* @returns The depth (0 if `dist` is `startNode`).
|
|
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
|
+
|
|
2683
|
+
|
|
2684
|
+
* @example
|
|
2685
|
+
* // Get depth of a node
|
|
2686
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2687
|
+
* const node = tree.getNode(4);
|
|
2688
|
+
* console.log(tree.getDepth(node!)); // 2;
|
|
2689
|
+
*/
|
|
1793
2690
|
getDepth(dist, startNode = this._root) {
|
|
1794
2691
|
let distEnsured = this.ensureNode(dist);
|
|
1795
2692
|
const beginRootEnsured = this.ensureNode(startNode);
|
|
@@ -1804,13 +2701,49 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1804
2701
|
return depth;
|
|
1805
2702
|
}
|
|
1806
2703
|
/**
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
2704
|
+
* Gets the maximum height of the tree (longest path from startNode to a leaf).
|
|
2705
|
+
* @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).
|
|
2706
|
+
*
|
|
2707
|
+
* @param [startNode=this._root] - The node to start measuring from.
|
|
2708
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2709
|
+
* @returns The height ( -1 for an empty tree, 0 for a single-node tree).
|
|
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
|
+
|
|
2741
|
+
|
|
2742
|
+
* @example
|
|
2743
|
+
* // Get tree height
|
|
2744
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2745
|
+
* console.log(tree.getHeight()); // 2;
|
|
2746
|
+
*/
|
|
1814
2747
|
getHeight(startNode = this._root, iterationType = this.iterationType) {
|
|
1815
2748
|
startNode = this.ensureNode(startNode);
|
|
1816
2749
|
if (!this.isRealNode(startNode)) return -1;
|
|
@@ -2246,24 +3179,95 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2246
3179
|
return ans;
|
|
2247
3180
|
}
|
|
2248
3181
|
/**
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
3182
|
+
* Clones the tree.
|
|
3183
|
+
* @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.
|
|
3184
|
+
*
|
|
3185
|
+
* @returns A new, cloned instance of the tree.
|
|
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
|
+
|
|
3215
|
+
|
|
3216
|
+
* @example
|
|
3217
|
+
* // Deep copy
|
|
3218
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
3219
|
+
* const copy = tree.clone();
|
|
3220
|
+
* copy.delete(1);
|
|
3221
|
+
* console.log(tree.has(1)); // true;
|
|
3222
|
+
*/
|
|
2254
3223
|
clone() {
|
|
2255
3224
|
const out = this._createInstance();
|
|
2256
3225
|
this._clone(out);
|
|
2257
3226
|
return out;
|
|
2258
3227
|
}
|
|
2259
3228
|
/**
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
3229
|
+
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
3230
|
+
* @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.
|
|
3231
|
+
*
|
|
3232
|
+
* @param predicate - A function to test each [key, value] pair.
|
|
3233
|
+
* @param [thisArg] - `this` context for the predicate.
|
|
3234
|
+
* @returns A new, filtered tree.
|
|
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
|
+
|
|
3264
|
+
|
|
3265
|
+
* @example
|
|
3266
|
+
* // Filter nodes by condition
|
|
3267
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4]);
|
|
3268
|
+
* const result = tree.filter((_, key) => key > 2);
|
|
3269
|
+
* console.log(result.size); // 2;
|
|
3270
|
+
*/
|
|
2267
3271
|
filter(predicate, thisArg) {
|
|
2268
3272
|
const out = this._createInstance();
|
|
2269
3273
|
let i = 0;
|
|
@@ -2271,17 +3275,52 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2271
3275
|
return out;
|
|
2272
3276
|
}
|
|
2273
3277
|
/**
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
3278
|
+
* Creates a new tree by mapping each [key, value] pair to a new entry.
|
|
3279
|
+
* @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.
|
|
3280
|
+
*
|
|
3281
|
+
* @template MK - New key type.
|
|
3282
|
+
* @template MV - New value type.
|
|
3283
|
+
* @template MR - New raw type.
|
|
3284
|
+
* @param cb - A function to map each [key, value] pair.
|
|
3285
|
+
* @param [options] - Options for the new tree.
|
|
3286
|
+
* @param [thisArg] - `this` context for the callback.
|
|
3287
|
+
* @returns A new, mapped tree.
|
|
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
|
+
|
|
3317
|
+
|
|
3318
|
+
* @example
|
|
3319
|
+
* // Transform to new tree
|
|
3320
|
+
* const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
|
|
3321
|
+
* const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
|
|
3322
|
+
* console.log([...mapped.values()]); // contains 11;
|
|
3323
|
+
*/
|
|
2285
3324
|
map(cb, options, thisArg) {
|
|
2286
3325
|
const out = this._createLike([], options);
|
|
2287
3326
|
let i = 0;
|
|
@@ -2319,12 +3358,46 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2319
3358
|
return output;
|
|
2320
3359
|
}
|
|
2321
3360
|
/**
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
3361
|
+
* Prints a visual representation of the tree to the console.
|
|
3362
|
+
* @remarks Time O(N) (via `toVisual`). Space O(N*H) or O(N^2) (via `toVisual`).
|
|
3363
|
+
*
|
|
3364
|
+
* @param [options] - Options to control the output.
|
|
3365
|
+
* @param [startNode=this._root] - The node to start printing from.
|
|
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
|
+
|
|
3395
|
+
|
|
3396
|
+
* @example
|
|
3397
|
+
* // Display tree
|
|
3398
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
3399
|
+
* expect(() => tree.print()).not.toThrow();
|
|
3400
|
+
*/
|
|
2328
3401
|
print(options, startNode = this._root) {
|
|
2329
3402
|
console.log(this.toVisual(startNode, options));
|
|
2330
3403
|
}
|
|
@@ -2557,7 +3630,6 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2557
3630
|
* @returns Layout information for this subtree.
|
|
2558
3631
|
*/
|
|
2559
3632
|
_displayAux(node, options) {
|
|
2560
|
-
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
2561
3633
|
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
2562
3634
|
const newFrame = /* @__PURE__ */ __name((n) => ({
|
|
2563
3635
|
node: n,
|