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