binary-tree-typed 2.4.5 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -84
- package/dist/cjs/index.cjs +867 -404
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +864 -401
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +867 -404
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +864 -401
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/binary-tree-typed.js +860 -397
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +2 -2
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -59,55 +59,6 @@ function makeTrampoline(fn) {
|
|
|
59
59
|
}
|
|
60
60
|
__name(makeTrampoline, "makeTrampoline");
|
|
61
61
|
|
|
62
|
-
// src/common/error.ts
|
|
63
|
-
var ERR = {
|
|
64
|
-
// Range / index
|
|
65
|
-
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
66
|
-
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
67
|
-
// Type / argument
|
|
68
|
-
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
69
|
-
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
70
|
-
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
71
|
-
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
72
|
-
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
73
|
-
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
74
|
-
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
75
|
-
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
76
|
-
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
77
|
-
// State / operation
|
|
78
|
-
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
79
|
-
// Matrix
|
|
80
|
-
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
81
|
-
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
82
|
-
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
83
|
-
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
84
|
-
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
// src/common/index.ts
|
|
88
|
-
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
89
|
-
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
90
|
-
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
91
|
-
return DFSOperation2;
|
|
92
|
-
})(DFSOperation || {});
|
|
93
|
-
var Range = class {
|
|
94
|
-
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
95
|
-
this.low = low;
|
|
96
|
-
this.high = high;
|
|
97
|
-
this.includeLow = includeLow;
|
|
98
|
-
this.includeHigh = includeHigh;
|
|
99
|
-
}
|
|
100
|
-
static {
|
|
101
|
-
__name(this, "Range");
|
|
102
|
-
}
|
|
103
|
-
// Determine whether a key is within the range
|
|
104
|
-
isInRange(key, comparator) {
|
|
105
|
-
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
106
|
-
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
107
|
-
return lowCheck && highCheck;
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
|
|
111
62
|
// src/data-structures/base/iterable-element-base.ts
|
|
112
63
|
var IterableElementBase = class {
|
|
113
64
|
static {
|
|
@@ -126,7 +77,7 @@ var IterableElementBase = class {
|
|
|
126
77
|
if (options) {
|
|
127
78
|
const { toElementFn } = options;
|
|
128
79
|
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
129
|
-
else if (toElementFn) throw new TypeError(
|
|
80
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
130
81
|
}
|
|
131
82
|
}
|
|
132
83
|
/**
|
|
@@ -289,7 +240,7 @@ var IterableElementBase = class {
|
|
|
289
240
|
acc = initialValue;
|
|
290
241
|
} else {
|
|
291
242
|
const first = iter.next();
|
|
292
|
-
if (first.done) throw new TypeError(
|
|
243
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
293
244
|
acc = first.value;
|
|
294
245
|
index = 1;
|
|
295
246
|
}
|
|
@@ -524,6 +475,237 @@ var LinearBase = class _LinearBase extends IterableElementBase {
|
|
|
524
475
|
}
|
|
525
476
|
};
|
|
526
477
|
|
|
478
|
+
// src/common/error.ts
|
|
479
|
+
var ERR = {
|
|
480
|
+
// Range / index
|
|
481
|
+
indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
|
|
482
|
+
invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
|
|
483
|
+
// Type / argument
|
|
484
|
+
invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
|
|
485
|
+
comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
|
|
486
|
+
invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
|
|
487
|
+
notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
|
|
488
|
+
invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
|
|
489
|
+
invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
|
|
490
|
+
invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
|
|
491
|
+
reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
|
|
492
|
+
callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
|
|
493
|
+
// State / operation
|
|
494
|
+
invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
|
|
495
|
+
// Matrix
|
|
496
|
+
matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
|
|
497
|
+
matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
|
|
498
|
+
matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
|
|
499
|
+
matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
|
|
500
|
+
matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
// src/common/index.ts
|
|
504
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
505
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
506
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
507
|
+
return DFSOperation2;
|
|
508
|
+
})(DFSOperation || {});
|
|
509
|
+
var Range = class {
|
|
510
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
511
|
+
this.low = low;
|
|
512
|
+
this.high = high;
|
|
513
|
+
this.includeLow = includeLow;
|
|
514
|
+
this.includeHigh = includeHigh;
|
|
515
|
+
}
|
|
516
|
+
static {
|
|
517
|
+
__name(this, "Range");
|
|
518
|
+
}
|
|
519
|
+
// Determine whether a key is within the range
|
|
520
|
+
isInRange(key, comparator) {
|
|
521
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
522
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
523
|
+
return lowCheck && highCheck;
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
// src/data-structures/base/iterable-entry-base.ts
|
|
528
|
+
var IterableEntryBase = class {
|
|
529
|
+
static {
|
|
530
|
+
__name(this, "IterableEntryBase");
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Default iterator yielding `[key, value]` entries.
|
|
534
|
+
* @returns Iterator of `[K, V]`.
|
|
535
|
+
* @remarks Time O(n) to iterate, Space O(1)
|
|
536
|
+
*/
|
|
537
|
+
*[Symbol.iterator](...args) {
|
|
538
|
+
yield* this._getIterator(...args);
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
542
|
+
* @returns Iterator of `[K, V | undefined]`.
|
|
543
|
+
* @remarks Time O(n), Space O(1)
|
|
544
|
+
*/
|
|
545
|
+
*entries() {
|
|
546
|
+
for (const item of this) {
|
|
547
|
+
yield item;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Iterate over keys only.
|
|
552
|
+
* @returns Iterator of keys.
|
|
553
|
+
* @remarks Time O(n), Space O(1)
|
|
554
|
+
*/
|
|
555
|
+
*keys() {
|
|
556
|
+
for (const item of this) {
|
|
557
|
+
yield item[0];
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Iterate over values only.
|
|
562
|
+
* @returns Iterator of values.
|
|
563
|
+
* @remarks Time O(n), Space O(1)
|
|
564
|
+
*/
|
|
565
|
+
*values() {
|
|
566
|
+
for (const item of this) {
|
|
567
|
+
yield item[1];
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Test whether all entries satisfy the predicate.
|
|
572
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
573
|
+
* @param thisArg - Optional `this` for callback.
|
|
574
|
+
* @returns `true` if all pass; otherwise `false`.
|
|
575
|
+
* @remarks Time O(n), Space O(1)
|
|
576
|
+
*/
|
|
577
|
+
every(predicate, thisArg) {
|
|
578
|
+
let index = 0;
|
|
579
|
+
for (const item of this) {
|
|
580
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
581
|
+
return false;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
return true;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Test whether any entry satisfies the predicate.
|
|
588
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
589
|
+
* @param thisArg - Optional `this` for callback.
|
|
590
|
+
* @returns `true` if any passes; otherwise `false`.
|
|
591
|
+
* @remarks Time O(n), Space O(1)
|
|
592
|
+
*/
|
|
593
|
+
some(predicate, thisArg) {
|
|
594
|
+
let index = 0;
|
|
595
|
+
for (const item of this) {
|
|
596
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Visit each entry, left-to-right.
|
|
604
|
+
* @param callbackfn - `(key, value, index, self) => void`.
|
|
605
|
+
* @param thisArg - Optional `this` for callback.
|
|
606
|
+
* @remarks Time O(n), Space O(1)
|
|
607
|
+
*/
|
|
608
|
+
forEach(callbackfn, thisArg) {
|
|
609
|
+
let index = 0;
|
|
610
|
+
for (const item of this) {
|
|
611
|
+
const [key, value] = item;
|
|
612
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Find the first entry that matches a predicate.
|
|
617
|
+
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
618
|
+
* @param thisArg - Optional `this` for callback.
|
|
619
|
+
* @returns Matching `[key, value]` or `undefined`.
|
|
620
|
+
* @remarks Time O(n), Space O(1)
|
|
621
|
+
*/
|
|
622
|
+
find(callbackfn, thisArg) {
|
|
623
|
+
let index = 0;
|
|
624
|
+
for (const item of this) {
|
|
625
|
+
const [key, value] = item;
|
|
626
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
627
|
+
}
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Whether the given key exists.
|
|
632
|
+
* @param key - Key to test.
|
|
633
|
+
* @returns `true` if found; otherwise `false`.
|
|
634
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
635
|
+
*/
|
|
636
|
+
has(key) {
|
|
637
|
+
for (const item of this) {
|
|
638
|
+
const [itemKey] = item;
|
|
639
|
+
if (itemKey === key) return true;
|
|
640
|
+
}
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Whether there exists an entry with the given value.
|
|
645
|
+
* @param value - Value to test.
|
|
646
|
+
* @returns `true` if found; otherwise `false`.
|
|
647
|
+
* @remarks Time O(n), Space O(1)
|
|
648
|
+
*/
|
|
649
|
+
hasValue(value) {
|
|
650
|
+
for (const [, elementValue] of this) {
|
|
651
|
+
if (elementValue === value) return true;
|
|
652
|
+
}
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Get the value under a key.
|
|
657
|
+
* @param key - Key to look up.
|
|
658
|
+
* @returns Value or `undefined`.
|
|
659
|
+
* @remarks Time O(n) generic, Space O(1)
|
|
660
|
+
*/
|
|
661
|
+
get(key) {
|
|
662
|
+
for (const item of this) {
|
|
663
|
+
const [itemKey, value] = item;
|
|
664
|
+
if (itemKey === key) return value;
|
|
665
|
+
}
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Reduce entries into a single accumulator.
|
|
670
|
+
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
671
|
+
* @param initialValue - Initial accumulator.
|
|
672
|
+
* @returns Final accumulator.
|
|
673
|
+
* @remarks Time O(n), Space O(1)
|
|
674
|
+
*/
|
|
675
|
+
reduce(callbackfn, initialValue) {
|
|
676
|
+
let accumulator = initialValue;
|
|
677
|
+
let index = 0;
|
|
678
|
+
for (const item of this) {
|
|
679
|
+
const [key, value] = item;
|
|
680
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
681
|
+
}
|
|
682
|
+
return accumulator;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Converts data structure to `[key, value]` pairs.
|
|
686
|
+
* @returns Array of entries.
|
|
687
|
+
* @remarks Time O(n), Space O(n)
|
|
688
|
+
*/
|
|
689
|
+
toArray() {
|
|
690
|
+
return [...this];
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
694
|
+
* @returns Array of entries (default) or a string.
|
|
695
|
+
* @remarks Time O(n), Space O(n)
|
|
696
|
+
*/
|
|
697
|
+
toVisual() {
|
|
698
|
+
return [...this];
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Print a human-friendly representation to the console.
|
|
702
|
+
* @remarks Time O(n), Space O(n)
|
|
703
|
+
*/
|
|
704
|
+
print() {
|
|
705
|
+
console.log(this.toVisual());
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
|
|
527
709
|
// src/data-structures/queue/queue.ts
|
|
528
710
|
var Queue = class _Queue extends LinearBase {
|
|
529
711
|
static {
|
|
@@ -580,19 +762,53 @@ var Queue = class _Queue extends LinearBase {
|
|
|
580
762
|
set autoCompactRatio(value) {
|
|
581
763
|
this._autoCompactRatio = value;
|
|
582
764
|
}
|
|
583
|
-
/**
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
765
|
+
/**
|
|
766
|
+
* Get the number of elements currently in the queue.
|
|
767
|
+
* @remarks Time O(1), Space O(1)
|
|
768
|
+
* @returns Current length.
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
* @example
|
|
781
|
+
* // Track queue length
|
|
782
|
+
* const q = new Queue<number>();
|
|
783
|
+
* console.log(q.length); // 0;
|
|
784
|
+
* q.push(1);
|
|
785
|
+
* q.push(2);
|
|
786
|
+
* console.log(q.length); // 2;
|
|
787
|
+
*/
|
|
588
788
|
get length() {
|
|
589
789
|
return this.elements.length - this._offset;
|
|
590
790
|
}
|
|
591
791
|
/**
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
792
|
+
* Get the first element (front) without removing it.
|
|
793
|
+
* @remarks Time O(1), Space O(1)
|
|
794
|
+
* @returns Front element or undefined.
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
* @example
|
|
807
|
+
* // View the front element
|
|
808
|
+
* const q = new Queue<string>(['first', 'second', 'third']);
|
|
809
|
+
* console.log(q.first); // 'first';
|
|
810
|
+
* console.log(q.length); // 3;
|
|
811
|
+
*/
|
|
596
812
|
get first() {
|
|
597
813
|
return this.length > 0 ? this.elements[this._offset] : void 0;
|
|
598
814
|
}
|
|
@@ -615,19 +831,69 @@ var Queue = class _Queue extends LinearBase {
|
|
|
615
831
|
return new _Queue(elements);
|
|
616
832
|
}
|
|
617
833
|
/**
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
834
|
+
* Check whether the queue is empty.
|
|
835
|
+
* @remarks Time O(1), Space O(1)
|
|
836
|
+
* @returns True if length is 0.
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
* @example
|
|
849
|
+
* // Queue for...of iteration and isEmpty check
|
|
850
|
+
* const queue = new Queue<string>(['A', 'B', 'C', 'D']);
|
|
851
|
+
*
|
|
852
|
+
* const elements: string[] = [];
|
|
853
|
+
* for (const item of queue) {
|
|
854
|
+
* elements.push(item);
|
|
855
|
+
* }
|
|
856
|
+
*
|
|
857
|
+
* // Verify all elements are iterated in order
|
|
858
|
+
* console.log(elements); // ['A', 'B', 'C', 'D'];
|
|
859
|
+
*
|
|
860
|
+
* // Process all elements
|
|
861
|
+
* while (queue.length > 0) {
|
|
862
|
+
* queue.shift();
|
|
863
|
+
* }
|
|
864
|
+
*
|
|
865
|
+
* console.log(queue.length); // 0;
|
|
866
|
+
*/
|
|
622
867
|
isEmpty() {
|
|
623
868
|
return this.length === 0;
|
|
624
869
|
}
|
|
625
870
|
/**
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
871
|
+
* Enqueue one element at the back.
|
|
872
|
+
* @remarks Time O(1), Space O(1)
|
|
873
|
+
* @param element - Element to enqueue.
|
|
874
|
+
* @returns True on success.
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
* @example
|
|
887
|
+
* // basic Queue creation and push operation
|
|
888
|
+
* // Create a simple Queue with initial values
|
|
889
|
+
* const queue = new Queue([1, 2, 3, 4, 5]);
|
|
890
|
+
*
|
|
891
|
+
* // Verify the queue maintains insertion order
|
|
892
|
+
* console.log([...queue]); // [1, 2, 3, 4, 5];
|
|
893
|
+
*
|
|
894
|
+
* // Check length
|
|
895
|
+
* console.log(queue.length); // 5;
|
|
896
|
+
*/
|
|
631
897
|
push(element) {
|
|
632
898
|
this.elements.push(element);
|
|
633
899
|
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
@@ -648,10 +914,35 @@ var Queue = class _Queue extends LinearBase {
|
|
|
648
914
|
return ans;
|
|
649
915
|
}
|
|
650
916
|
/**
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
917
|
+
* Dequeue one element from the front (amortized via offset).
|
|
918
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
919
|
+
* @returns Removed element or undefined.
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
* @example
|
|
932
|
+
* // Queue shift and peek operations
|
|
933
|
+
* const queue = new Queue<number>([10, 20, 30, 40]);
|
|
934
|
+
*
|
|
935
|
+
* // Peek at the front element without removing it
|
|
936
|
+
* console.log(queue.first); // 10;
|
|
937
|
+
*
|
|
938
|
+
* // Remove and get the first element (FIFO)
|
|
939
|
+
* const first = queue.shift();
|
|
940
|
+
* console.log(first); // 10;
|
|
941
|
+
*
|
|
942
|
+
* // Verify remaining elements and length decreased
|
|
943
|
+
* console.log([...queue]); // [20, 30, 40];
|
|
944
|
+
* console.log(queue.length); // 3;
|
|
945
|
+
*/
|
|
655
946
|
shift() {
|
|
656
947
|
if (this.length === 0) return void 0;
|
|
657
948
|
const first = this.first;
|
|
@@ -660,11 +951,24 @@ var Queue = class _Queue extends LinearBase {
|
|
|
660
951
|
return first;
|
|
661
952
|
}
|
|
662
953
|
/**
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
954
|
+
* Delete the first occurrence of a specific element.
|
|
955
|
+
* @remarks Time O(N), Space O(1)
|
|
956
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
957
|
+
* @returns True if an element was removed.
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
* @example
|
|
967
|
+
* // Remove specific element
|
|
968
|
+
* const q = new Queue<number>([1, 2, 3, 2]);
|
|
969
|
+
* q.delete(2);
|
|
970
|
+
* console.log(q.length); // 3;
|
|
971
|
+
*/
|
|
668
972
|
delete(element) {
|
|
669
973
|
for (let i = this._offset; i < this.elements.length; i++) {
|
|
670
974
|
if (Object.is(this.elements[i], element)) {
|
|
@@ -675,11 +979,24 @@ var Queue = class _Queue extends LinearBase {
|
|
|
675
979
|
return false;
|
|
676
980
|
}
|
|
677
981
|
/**
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
982
|
+
* Get the element at a given logical index.
|
|
983
|
+
* @remarks Time O(1), Space O(1)
|
|
984
|
+
* @param index - Zero-based index from the front.
|
|
985
|
+
* @returns Element or undefined.
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
* @example
|
|
995
|
+
* // Access element by index
|
|
996
|
+
* const q = new Queue<string>(['a', 'b', 'c']);
|
|
997
|
+
* console.log(q.at(0)); // 'a';
|
|
998
|
+
* console.log(q.at(2)); // 'c';
|
|
999
|
+
*/
|
|
683
1000
|
at(index) {
|
|
684
1001
|
if (index < 0 || index >= this.length) return void 0;
|
|
685
1002
|
return this._elements[this._offset + index];
|
|
@@ -731,19 +1048,48 @@ var Queue = class _Queue extends LinearBase {
|
|
|
731
1048
|
return this;
|
|
732
1049
|
}
|
|
733
1050
|
/**
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
1051
|
+
* Remove all elements and reset offset.
|
|
1052
|
+
* @remarks Time O(1), Space O(1)
|
|
1053
|
+
* @returns void
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
* @example
|
|
1064
|
+
* // Remove all elements
|
|
1065
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1066
|
+
* q.clear();
|
|
1067
|
+
* console.log(q.length); // 0;
|
|
1068
|
+
*/
|
|
738
1069
|
clear() {
|
|
739
1070
|
this._elements = [];
|
|
740
1071
|
this._offset = 0;
|
|
741
1072
|
}
|
|
742
1073
|
/**
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
1074
|
+
* Compact storage by discarding consumed head elements.
|
|
1075
|
+
* @remarks Time O(N), Space O(N)
|
|
1076
|
+
* @returns True when compaction performed.
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
* @example
|
|
1086
|
+
* // Reclaim unused memory
|
|
1087
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1088
|
+
* q.shift();
|
|
1089
|
+
* q.shift();
|
|
1090
|
+
* q.compact();
|
|
1091
|
+
* console.log(q.length); // 3;
|
|
1092
|
+
*/
|
|
747
1093
|
compact() {
|
|
748
1094
|
this._elements = this.elements.slice(this._offset);
|
|
749
1095
|
this._offset = 0;
|
|
@@ -769,10 +1115,26 @@ var Queue = class _Queue extends LinearBase {
|
|
|
769
1115
|
return removed;
|
|
770
1116
|
}
|
|
771
1117
|
/**
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
1118
|
+
* Deep clone this queue and its parameters.
|
|
1119
|
+
* @remarks Time O(N), Space O(N)
|
|
1120
|
+
* @returns A new queue with the same content and options.
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
* @example
|
|
1131
|
+
* // Create independent copy
|
|
1132
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1133
|
+
* const copy = q.clone();
|
|
1134
|
+
* copy.shift();
|
|
1135
|
+
* console.log(q.length); // 3;
|
|
1136
|
+
* console.log(copy.length); // 2;
|
|
1137
|
+
*/
|
|
776
1138
|
clone() {
|
|
777
1139
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
778
1140
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -780,12 +1142,26 @@ var Queue = class _Queue extends LinearBase {
|
|
|
780
1142
|
return out;
|
|
781
1143
|
}
|
|
782
1144
|
/**
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
1145
|
+
* Filter elements into a new queue of the same class.
|
|
1146
|
+
* @remarks Time O(N), Space O(N)
|
|
1147
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
1148
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1149
|
+
* @returns A new queue with kept elements.
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
* @example
|
|
1160
|
+
* // Filter elements
|
|
1161
|
+
* const q = new Queue<number>([1, 2, 3, 4, 5]);
|
|
1162
|
+
* const evens = q.filter(x => x % 2 === 0);
|
|
1163
|
+
* console.log(evens.length); // 2;
|
|
1164
|
+
*/
|
|
789
1165
|
filter(predicate, thisArg) {
|
|
790
1166
|
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
791
1167
|
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
@@ -797,15 +1173,28 @@ var Queue = class _Queue extends LinearBase {
|
|
|
797
1173
|
return out;
|
|
798
1174
|
}
|
|
799
1175
|
/**
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
1176
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
1177
|
+
* @remarks Time O(N), Space O(N)
|
|
1178
|
+
* @template EM
|
|
1179
|
+
* @template RM
|
|
1180
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
1181
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
1182
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
1183
|
+
* @returns A new Queue with mapped elements.
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
* @example
|
|
1193
|
+
* // Transform elements
|
|
1194
|
+
* const q = new Queue<number>([1, 2, 3]);
|
|
1195
|
+
* const doubled = q.map(x => x * 2);
|
|
1196
|
+
* console.log(doubled.toArray()); // [2, 4, 6];
|
|
1197
|
+
*/
|
|
809
1198
|
map(callback, options, thisArg) {
|
|
810
1199
|
const out = new this.constructor([], {
|
|
811
1200
|
toElementFn: options?.toElementFn,
|
|
@@ -892,188 +1281,6 @@ var Queue = class _Queue extends LinearBase {
|
|
|
892
1281
|
}
|
|
893
1282
|
};
|
|
894
1283
|
|
|
895
|
-
// src/data-structures/base/iterable-entry-base.ts
|
|
896
|
-
var IterableEntryBase = class {
|
|
897
|
-
static {
|
|
898
|
-
__name(this, "IterableEntryBase");
|
|
899
|
-
}
|
|
900
|
-
/**
|
|
901
|
-
* Default iterator yielding `[key, value]` entries.
|
|
902
|
-
* @returns Iterator of `[K, V]`.
|
|
903
|
-
* @remarks Time O(n) to iterate, Space O(1)
|
|
904
|
-
*/
|
|
905
|
-
*[Symbol.iterator](...args) {
|
|
906
|
-
yield* this._getIterator(...args);
|
|
907
|
-
}
|
|
908
|
-
/**
|
|
909
|
-
* Iterate over `[key, value]` pairs (may yield `undefined` values).
|
|
910
|
-
* @returns Iterator of `[K, V | undefined]`.
|
|
911
|
-
* @remarks Time O(n), Space O(1)
|
|
912
|
-
*/
|
|
913
|
-
*entries() {
|
|
914
|
-
for (const item of this) {
|
|
915
|
-
yield item;
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
/**
|
|
919
|
-
* Iterate over keys only.
|
|
920
|
-
* @returns Iterator of keys.
|
|
921
|
-
* @remarks Time O(n), Space O(1)
|
|
922
|
-
*/
|
|
923
|
-
*keys() {
|
|
924
|
-
for (const item of this) {
|
|
925
|
-
yield item[0];
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
/**
|
|
929
|
-
* Iterate over values only.
|
|
930
|
-
* @returns Iterator of values.
|
|
931
|
-
* @remarks Time O(n), Space O(1)
|
|
932
|
-
*/
|
|
933
|
-
*values() {
|
|
934
|
-
for (const item of this) {
|
|
935
|
-
yield item[1];
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
/**
|
|
939
|
-
* Test whether all entries satisfy the predicate.
|
|
940
|
-
* @param predicate - `(key, value, index, self) => boolean`.
|
|
941
|
-
* @param thisArg - Optional `this` for callback.
|
|
942
|
-
* @returns `true` if all pass; otherwise `false`.
|
|
943
|
-
* @remarks Time O(n), Space O(1)
|
|
944
|
-
*/
|
|
945
|
-
every(predicate, thisArg) {
|
|
946
|
-
let index = 0;
|
|
947
|
-
for (const item of this) {
|
|
948
|
-
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
949
|
-
return false;
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
return true;
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* Test whether any entry satisfies the predicate.
|
|
956
|
-
* @param predicate - `(key, value, index, self) => boolean`.
|
|
957
|
-
* @param thisArg - Optional `this` for callback.
|
|
958
|
-
* @returns `true` if any passes; otherwise `false`.
|
|
959
|
-
* @remarks Time O(n), Space O(1)
|
|
960
|
-
*/
|
|
961
|
-
some(predicate, thisArg) {
|
|
962
|
-
let index = 0;
|
|
963
|
-
for (const item of this) {
|
|
964
|
-
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
965
|
-
return true;
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
return false;
|
|
969
|
-
}
|
|
970
|
-
/**
|
|
971
|
-
* Visit each entry, left-to-right.
|
|
972
|
-
* @param callbackfn - `(key, value, index, self) => void`.
|
|
973
|
-
* @param thisArg - Optional `this` for callback.
|
|
974
|
-
* @remarks Time O(n), Space O(1)
|
|
975
|
-
*/
|
|
976
|
-
forEach(callbackfn, thisArg) {
|
|
977
|
-
let index = 0;
|
|
978
|
-
for (const item of this) {
|
|
979
|
-
const [key, value] = item;
|
|
980
|
-
callbackfn.call(thisArg, value, key, index++, this);
|
|
981
|
-
}
|
|
982
|
-
}
|
|
983
|
-
/**
|
|
984
|
-
* Find the first entry that matches a predicate.
|
|
985
|
-
* @param callbackfn - `(key, value, index, self) => boolean`.
|
|
986
|
-
* @param thisArg - Optional `this` for callback.
|
|
987
|
-
* @returns Matching `[key, value]` or `undefined`.
|
|
988
|
-
* @remarks Time O(n), Space O(1)
|
|
989
|
-
*/
|
|
990
|
-
find(callbackfn, thisArg) {
|
|
991
|
-
let index = 0;
|
|
992
|
-
for (const item of this) {
|
|
993
|
-
const [key, value] = item;
|
|
994
|
-
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
995
|
-
}
|
|
996
|
-
return;
|
|
997
|
-
}
|
|
998
|
-
/**
|
|
999
|
-
* Whether the given key exists.
|
|
1000
|
-
* @param key - Key to test.
|
|
1001
|
-
* @returns `true` if found; otherwise `false`.
|
|
1002
|
-
* @remarks Time O(n) generic, Space O(1)
|
|
1003
|
-
*/
|
|
1004
|
-
has(key) {
|
|
1005
|
-
for (const item of this) {
|
|
1006
|
-
const [itemKey] = item;
|
|
1007
|
-
if (itemKey === key) return true;
|
|
1008
|
-
}
|
|
1009
|
-
return false;
|
|
1010
|
-
}
|
|
1011
|
-
/**
|
|
1012
|
-
* Whether there exists an entry with the given value.
|
|
1013
|
-
* @param value - Value to test.
|
|
1014
|
-
* @returns `true` if found; otherwise `false`.
|
|
1015
|
-
* @remarks Time O(n), Space O(1)
|
|
1016
|
-
*/
|
|
1017
|
-
hasValue(value) {
|
|
1018
|
-
for (const [, elementValue] of this) {
|
|
1019
|
-
if (elementValue === value) return true;
|
|
1020
|
-
}
|
|
1021
|
-
return false;
|
|
1022
|
-
}
|
|
1023
|
-
/**
|
|
1024
|
-
* Get the value under a key.
|
|
1025
|
-
* @param key - Key to look up.
|
|
1026
|
-
* @returns Value or `undefined`.
|
|
1027
|
-
* @remarks Time O(n) generic, Space O(1)
|
|
1028
|
-
*/
|
|
1029
|
-
get(key) {
|
|
1030
|
-
for (const item of this) {
|
|
1031
|
-
const [itemKey, value] = item;
|
|
1032
|
-
if (itemKey === key) return value;
|
|
1033
|
-
}
|
|
1034
|
-
return;
|
|
1035
|
-
}
|
|
1036
|
-
/**
|
|
1037
|
-
* Reduce entries into a single accumulator.
|
|
1038
|
-
* @param callbackfn - `(acc, value, key, index, self) => acc`.
|
|
1039
|
-
* @param initialValue - Initial accumulator.
|
|
1040
|
-
* @returns Final accumulator.
|
|
1041
|
-
* @remarks Time O(n), Space O(1)
|
|
1042
|
-
*/
|
|
1043
|
-
reduce(callbackfn, initialValue) {
|
|
1044
|
-
let accumulator = initialValue;
|
|
1045
|
-
let index = 0;
|
|
1046
|
-
for (const item of this) {
|
|
1047
|
-
const [key, value] = item;
|
|
1048
|
-
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
1049
|
-
}
|
|
1050
|
-
return accumulator;
|
|
1051
|
-
}
|
|
1052
|
-
/**
|
|
1053
|
-
* Converts data structure to `[key, value]` pairs.
|
|
1054
|
-
* @returns Array of entries.
|
|
1055
|
-
* @remarks Time O(n), Space O(n)
|
|
1056
|
-
*/
|
|
1057
|
-
toArray() {
|
|
1058
|
-
return [...this];
|
|
1059
|
-
}
|
|
1060
|
-
/**
|
|
1061
|
-
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
1062
|
-
* @returns Array of entries (default) or a string.
|
|
1063
|
-
* @remarks Time O(n), Space O(n)
|
|
1064
|
-
*/
|
|
1065
|
-
toVisual() {
|
|
1066
|
-
return [...this];
|
|
1067
|
-
}
|
|
1068
|
-
/**
|
|
1069
|
-
* Print a human-friendly representation to the console.
|
|
1070
|
-
* @remarks Time O(n), Space O(n)
|
|
1071
|
-
*/
|
|
1072
|
-
print() {
|
|
1073
|
-
console.log(this.toVisual());
|
|
1074
|
-
}
|
|
1075
|
-
};
|
|
1076
|
-
|
|
1077
1284
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
1078
1285
|
var BinaryTreeNode = class {
|
|
1079
1286
|
static {
|
|
@@ -1447,23 +1654,71 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1447
1654
|
return isComparable(key);
|
|
1448
1655
|
}
|
|
1449
1656
|
/**
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1657
|
+
* Adds a new node to the tree.
|
|
1658
|
+
* @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).
|
|
1659
|
+
*
|
|
1660
|
+
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
1661
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1662
|
+
|
|
1663
|
+
|
|
1664
|
+
|
|
1665
|
+
|
|
1666
|
+
|
|
1667
|
+
|
|
1668
|
+
* @example
|
|
1669
|
+
* // Add a single node
|
|
1670
|
+
* const tree = new BinaryTree<number>();
|
|
1671
|
+
* tree.add(1);
|
|
1672
|
+
* tree.add(2);
|
|
1673
|
+
* tree.add(3);
|
|
1674
|
+
* console.log(tree.size); // 3;
|
|
1675
|
+
* console.log(tree.has(1)); // true;
|
|
1676
|
+
*/
|
|
1456
1677
|
add(keyNodeOrEntry) {
|
|
1457
1678
|
return this.set(keyNodeOrEntry);
|
|
1458
1679
|
}
|
|
1459
1680
|
/**
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1681
|
+
* Adds or updates a new node to the tree.
|
|
1682
|
+
* @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).
|
|
1683
|
+
*
|
|
1684
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
1685
|
+
* @param [value] - The value, if providing just a key.
|
|
1686
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1687
|
+
|
|
1688
|
+
|
|
1689
|
+
|
|
1690
|
+
|
|
1691
|
+
|
|
1692
|
+
|
|
1693
|
+
|
|
1694
|
+
|
|
1695
|
+
|
|
1696
|
+
|
|
1697
|
+
|
|
1698
|
+
* @example
|
|
1699
|
+
* // basic BinaryTree creation and insertion
|
|
1700
|
+
* // Create a BinaryTree with entries
|
|
1701
|
+
* const entries: [number, string][] = [
|
|
1702
|
+
* [6, 'six'],
|
|
1703
|
+
* [1, 'one'],
|
|
1704
|
+
* [2, 'two'],
|
|
1705
|
+
* [7, 'seven'],
|
|
1706
|
+
* [5, 'five'],
|
|
1707
|
+
* [3, 'three'],
|
|
1708
|
+
* [4, 'four'],
|
|
1709
|
+
* [9, 'nine'],
|
|
1710
|
+
* [8, 'eight']
|
|
1711
|
+
* ];
|
|
1712
|
+
*
|
|
1713
|
+
* const tree = new BinaryTree(entries);
|
|
1714
|
+
*
|
|
1715
|
+
* // Verify size
|
|
1716
|
+
* console.log(tree.size); // 9;
|
|
1717
|
+
*
|
|
1718
|
+
* // Add new element
|
|
1719
|
+
* tree.set(10, 'ten');
|
|
1720
|
+
* console.log(tree.size); // 10;
|
|
1721
|
+
*/
|
|
1467
1722
|
set(keyNodeOrEntry, value) {
|
|
1468
1723
|
const [newNode] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
1469
1724
|
if (newNode === void 0) return false;
|
|
@@ -1508,23 +1763,44 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1508
1763
|
return false;
|
|
1509
1764
|
}
|
|
1510
1765
|
/**
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1766
|
+
* Adds multiple items to the tree.
|
|
1767
|
+
* @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).
|
|
1768
|
+
*
|
|
1769
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
1770
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1771
|
+
|
|
1772
|
+
|
|
1773
|
+
|
|
1774
|
+
|
|
1775
|
+
|
|
1776
|
+
|
|
1777
|
+
|
|
1778
|
+
|
|
1779
|
+
|
|
1780
|
+
* @example
|
|
1781
|
+
* // Bulk add
|
|
1782
|
+
* const tree = new BinaryTree<number>();
|
|
1783
|
+
* tree.addMany([1, 2, 3, 4, 5]);
|
|
1784
|
+
* console.log(tree.size); // 5;
|
|
1785
|
+
*/
|
|
1517
1786
|
addMany(keysNodesEntriesOrRaws) {
|
|
1518
1787
|
return this.setMany(keysNodesEntriesOrRaws);
|
|
1519
1788
|
}
|
|
1520
1789
|
/**
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1790
|
+
* Adds or updates multiple items to the tree.
|
|
1791
|
+
* @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).
|
|
1792
|
+
*
|
|
1793
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
1794
|
+
* @param [values] - An optional parallel iterable of values.
|
|
1795
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
1796
|
+
|
|
1797
|
+
|
|
1798
|
+
* @example
|
|
1799
|
+
* // Set multiple entries
|
|
1800
|
+
* const tree = new BinaryTree<number, string>();
|
|
1801
|
+
* tree.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
1802
|
+
* console.log(tree.size); // 3;
|
|
1803
|
+
*/
|
|
1528
1804
|
setMany(keysNodesEntriesOrRaws, values) {
|
|
1529
1805
|
const inserted = [];
|
|
1530
1806
|
let valuesIterator;
|
|
@@ -1545,11 +1821,26 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1545
1821
|
return inserted;
|
|
1546
1822
|
}
|
|
1547
1823
|
/**
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1824
|
+
* Merges another tree into this one by seting all its nodes.
|
|
1825
|
+
* @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`).
|
|
1826
|
+
*
|
|
1827
|
+
* @param anotherTree - The tree to merge.
|
|
1828
|
+
|
|
1829
|
+
|
|
1830
|
+
|
|
1831
|
+
|
|
1832
|
+
|
|
1833
|
+
|
|
1834
|
+
|
|
1835
|
+
|
|
1836
|
+
|
|
1837
|
+
* @example
|
|
1838
|
+
* // Combine trees
|
|
1839
|
+
* const t1 = new BinaryTree<number>([1, 2]);
|
|
1840
|
+
* const t2 = new BinaryTree<number>([3, 4]);
|
|
1841
|
+
* t1.merge(t2);
|
|
1842
|
+
* console.log(t1.size); // 4;
|
|
1843
|
+
*/
|
|
1553
1844
|
merge(anotherTree) {
|
|
1554
1845
|
this.setMany(anotherTree, []);
|
|
1555
1846
|
}
|
|
@@ -1565,12 +1856,29 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1565
1856
|
this.setMany(keysNodesEntriesOrRaws, values);
|
|
1566
1857
|
}
|
|
1567
1858
|
/**
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1859
|
+
* Deletes a node from the tree.
|
|
1860
|
+
* @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).
|
|
1861
|
+
*
|
|
1862
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
1863
|
+
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
1864
|
+
|
|
1865
|
+
|
|
1866
|
+
|
|
1867
|
+
|
|
1868
|
+
|
|
1869
|
+
|
|
1870
|
+
|
|
1871
|
+
|
|
1872
|
+
|
|
1873
|
+
|
|
1874
|
+
|
|
1875
|
+
* @example
|
|
1876
|
+
* // Remove a node
|
|
1877
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1878
|
+
* tree.delete(3);
|
|
1879
|
+
* console.log(tree.has(3)); // false;
|
|
1880
|
+
* console.log(tree.size); // 4;
|
|
1881
|
+
*/
|
|
1574
1882
|
delete(keyNodeEntryRawOrPredicate) {
|
|
1575
1883
|
const deletedResult = [];
|
|
1576
1884
|
if (!this._root) return deletedResult;
|
|
@@ -1664,14 +1972,27 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1664
1972
|
return this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
1665
1973
|
}
|
|
1666
1974
|
/**
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1975
|
+
* Gets the first node matching a predicate.
|
|
1976
|
+
* @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`).
|
|
1977
|
+
*
|
|
1978
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
1979
|
+
* @param [startNode=this._root] - The node to start the search from.
|
|
1980
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
1981
|
+
* @returns The first matching node, or undefined if not found.
|
|
1982
|
+
|
|
1983
|
+
|
|
1984
|
+
|
|
1985
|
+
|
|
1986
|
+
|
|
1987
|
+
|
|
1988
|
+
|
|
1989
|
+
|
|
1990
|
+
|
|
1991
|
+
* @example
|
|
1992
|
+
* // Get node by key
|
|
1993
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
|
|
1994
|
+
* console.log(tree.getNode(2)?.value); // 'child';
|
|
1995
|
+
*/
|
|
1675
1996
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1676
1997
|
if (this._isMapMode && keyNodeEntryOrPredicate !== null && keyNodeEntryOrPredicate !== void 0) {
|
|
1677
1998
|
if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -1683,14 +2004,30 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1683
2004
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType)[0];
|
|
1684
2005
|
}
|
|
1685
2006
|
/**
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
2007
|
+
* Gets the value associated with a key.
|
|
2008
|
+
* @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.
|
|
2009
|
+
*
|
|
2010
|
+
* @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
|
|
2011
|
+
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
2012
|
+
* @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
|
|
2013
|
+
* @returns The associated value, or undefined.
|
|
2014
|
+
|
|
2015
|
+
|
|
2016
|
+
|
|
2017
|
+
|
|
2018
|
+
|
|
2019
|
+
|
|
2020
|
+
|
|
2021
|
+
|
|
2022
|
+
|
|
2023
|
+
|
|
2024
|
+
|
|
2025
|
+
* @example
|
|
2026
|
+
* // Retrieve value by key
|
|
2027
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
|
|
2028
|
+
* console.log(tree.get(2)); // 'left';
|
|
2029
|
+
* console.log(tree.get(99)); // undefined;
|
|
2030
|
+
*/
|
|
1694
2031
|
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
1695
2032
|
if (this._isMapMode) {
|
|
1696
2033
|
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
@@ -1710,19 +2047,45 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1710
2047
|
return this.search(keyNodeEntryOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
1711
2048
|
}
|
|
1712
2049
|
/**
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
2050
|
+
* Clears the tree of all nodes and values.
|
|
2051
|
+
* @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
|
|
2052
|
+
|
|
2053
|
+
|
|
2054
|
+
|
|
2055
|
+
|
|
2056
|
+
|
|
2057
|
+
|
|
2058
|
+
|
|
2059
|
+
|
|
2060
|
+
|
|
2061
|
+
* @example
|
|
2062
|
+
* // Remove all nodes
|
|
2063
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2064
|
+
* tree.clear();
|
|
2065
|
+
* console.log(tree.isEmpty()); // true;
|
|
2066
|
+
*/
|
|
1716
2067
|
clear() {
|
|
1717
2068
|
this._clearNodes();
|
|
1718
2069
|
if (this._isMapMode) this._clearValues();
|
|
1719
2070
|
}
|
|
1720
2071
|
/**
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
2072
|
+
* Checks if the tree is empty.
|
|
2073
|
+
* @remarks Time O(1), Space O(1)
|
|
2074
|
+
*
|
|
2075
|
+
* @returns True if the tree has no nodes, false otherwise.
|
|
2076
|
+
|
|
2077
|
+
|
|
2078
|
+
|
|
2079
|
+
|
|
2080
|
+
|
|
2081
|
+
|
|
2082
|
+
|
|
2083
|
+
|
|
2084
|
+
|
|
2085
|
+
* @example
|
|
2086
|
+
* // Check empty
|
|
2087
|
+
* console.log(new BinaryTree().isEmpty()); // true;
|
|
2088
|
+
*/
|
|
1726
2089
|
isEmpty() {
|
|
1727
2090
|
return this._size === 0;
|
|
1728
2091
|
}
|
|
@@ -1737,13 +2100,27 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1737
2100
|
return this.getMinHeight(startNode) + 1 >= this.getHeight(startNode);
|
|
1738
2101
|
}
|
|
1739
2102
|
/**
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
2103
|
+
* Checks if the tree is a valid Binary Search Tree (BST).
|
|
2104
|
+
* @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).
|
|
2105
|
+
*
|
|
2106
|
+
* @param [startNode=this._root] - The node to start checking from.
|
|
2107
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2108
|
+
* @returns True if it's a valid BST, false otherwise.
|
|
2109
|
+
|
|
2110
|
+
|
|
2111
|
+
|
|
2112
|
+
|
|
2113
|
+
|
|
2114
|
+
|
|
2115
|
+
|
|
2116
|
+
|
|
2117
|
+
|
|
2118
|
+
* @example
|
|
2119
|
+
* // Check BST property
|
|
2120
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2121
|
+
* // BinaryTree doesn't guarantee BST order
|
|
2122
|
+
* console.log(typeof tree.isBST()); // 'boolean';
|
|
2123
|
+
*/
|
|
1747
2124
|
isBST(startNode = this._root, iterationType = this.iterationType) {
|
|
1748
2125
|
const startNodeSired = this.ensureNode(startNode);
|
|
1749
2126
|
if (!startNodeSired) return true;
|
|
@@ -1781,13 +2158,29 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1781
2158
|
}
|
|
1782
2159
|
}
|
|
1783
2160
|
/**
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
2161
|
+
* Gets the depth of a node (distance from `startNode`).
|
|
2162
|
+
* @remarks Time O(H), where H is the depth of the `dist` node relative to `startNode`. O(N) worst-case. Space O(1).
|
|
2163
|
+
*
|
|
2164
|
+
* @param dist - The node to find the depth of.
|
|
2165
|
+
* @param [startNode=this._root] - The node to measure depth from (defaults to root).
|
|
2166
|
+
* @returns The depth (0 if `dist` is `startNode`).
|
|
2167
|
+
|
|
2168
|
+
|
|
2169
|
+
|
|
2170
|
+
|
|
2171
|
+
|
|
2172
|
+
|
|
2173
|
+
|
|
2174
|
+
|
|
2175
|
+
|
|
2176
|
+
|
|
2177
|
+
|
|
2178
|
+
* @example
|
|
2179
|
+
* // Get depth of a node
|
|
2180
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2181
|
+
* const node = tree.getNode(4);
|
|
2182
|
+
* console.log(tree.getDepth(node!)); // 2;
|
|
2183
|
+
*/
|
|
1791
2184
|
getDepth(dist, startNode = this._root) {
|
|
1792
2185
|
let distEnsured = this.ensureNode(dist);
|
|
1793
2186
|
const beginRootEnsured = this.ensureNode(startNode);
|
|
@@ -1802,13 +2195,28 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1802
2195
|
return depth;
|
|
1803
2196
|
}
|
|
1804
2197
|
/**
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
2198
|
+
* Gets the maximum height of the tree (longest path from startNode to a leaf).
|
|
2199
|
+
* @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).
|
|
2200
|
+
*
|
|
2201
|
+
* @param [startNode=this._root] - The node to start measuring from.
|
|
2202
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
2203
|
+
* @returns The height ( -1 for an empty tree, 0 for a single-node tree).
|
|
2204
|
+
|
|
2205
|
+
|
|
2206
|
+
|
|
2207
|
+
|
|
2208
|
+
|
|
2209
|
+
|
|
2210
|
+
|
|
2211
|
+
|
|
2212
|
+
|
|
2213
|
+
|
|
2214
|
+
|
|
2215
|
+
* @example
|
|
2216
|
+
* // Get tree height
|
|
2217
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2218
|
+
* console.log(tree.getHeight()); // 2;
|
|
2219
|
+
*/
|
|
1812
2220
|
getHeight(startNode = this._root, iterationType = this.iterationType) {
|
|
1813
2221
|
startNode = this.ensureNode(startNode);
|
|
1814
2222
|
if (!this.isRealNode(startNode)) return -1;
|
|
@@ -2244,24 +2652,53 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2244
2652
|
return ans;
|
|
2245
2653
|
}
|
|
2246
2654
|
/**
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2655
|
+
* Clones the tree.
|
|
2656
|
+
* @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.
|
|
2657
|
+
*
|
|
2658
|
+
* @returns A new, cloned instance of the tree.
|
|
2659
|
+
|
|
2660
|
+
|
|
2661
|
+
|
|
2662
|
+
|
|
2663
|
+
|
|
2664
|
+
|
|
2665
|
+
|
|
2666
|
+
|
|
2667
|
+
|
|
2668
|
+
* @example
|
|
2669
|
+
* // Deep copy
|
|
2670
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2671
|
+
* const copy = tree.clone();
|
|
2672
|
+
* copy.delete(1);
|
|
2673
|
+
* console.log(tree.has(1)); // true;
|
|
2674
|
+
*/
|
|
2252
2675
|
clone() {
|
|
2253
2676
|
const out = this._createInstance();
|
|
2254
2677
|
this._clone(out);
|
|
2255
2678
|
return out;
|
|
2256
2679
|
}
|
|
2257
2680
|
/**
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2681
|
+
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
2682
|
+
* @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.
|
|
2683
|
+
*
|
|
2684
|
+
* @param predicate - A function to test each [key, value] pair.
|
|
2685
|
+
* @param [thisArg] - `this` context for the predicate.
|
|
2686
|
+
* @returns A new, filtered tree.
|
|
2687
|
+
|
|
2688
|
+
|
|
2689
|
+
|
|
2690
|
+
|
|
2691
|
+
|
|
2692
|
+
|
|
2693
|
+
|
|
2694
|
+
|
|
2695
|
+
|
|
2696
|
+
* @example
|
|
2697
|
+
* // Filter nodes by condition
|
|
2698
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4]);
|
|
2699
|
+
* const result = tree.filter((_, key) => key > 2);
|
|
2700
|
+
* console.log(result.size); // 2;
|
|
2701
|
+
*/
|
|
2265
2702
|
filter(predicate, thisArg) {
|
|
2266
2703
|
const out = this._createInstance();
|
|
2267
2704
|
let i = 0;
|
|
@@ -2269,17 +2706,31 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2269
2706
|
return out;
|
|
2270
2707
|
}
|
|
2271
2708
|
/**
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2709
|
+
* Creates a new tree by mapping each [key, value] pair to a new entry.
|
|
2710
|
+
* @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.
|
|
2711
|
+
*
|
|
2712
|
+
* @template MK - New key type.
|
|
2713
|
+
* @template MV - New value type.
|
|
2714
|
+
* @template MR - New raw type.
|
|
2715
|
+
* @param cb - A function to map each [key, value] pair.
|
|
2716
|
+
* @param [options] - Options for the new tree.
|
|
2717
|
+
* @param [thisArg] - `this` context for the callback.
|
|
2718
|
+
* @returns A new, mapped tree.
|
|
2719
|
+
|
|
2720
|
+
|
|
2721
|
+
|
|
2722
|
+
|
|
2723
|
+
|
|
2724
|
+
|
|
2725
|
+
|
|
2726
|
+
|
|
2727
|
+
|
|
2728
|
+
* @example
|
|
2729
|
+
* // Transform to new tree
|
|
2730
|
+
* const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
|
|
2731
|
+
* const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
|
|
2732
|
+
* console.log([...mapped.values()]); // contains 11;
|
|
2733
|
+
*/
|
|
2283
2734
|
map(cb, options, thisArg) {
|
|
2284
2735
|
const out = this._createLike([], options);
|
|
2285
2736
|
let i = 0;
|
|
@@ -2317,12 +2768,25 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2317
2768
|
return output;
|
|
2318
2769
|
}
|
|
2319
2770
|
/**
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2771
|
+
* Prints a visual representation of the tree to the console.
|
|
2772
|
+
* @remarks Time O(N) (via `toVisual`). Space O(N*H) or O(N^2) (via `toVisual`).
|
|
2773
|
+
*
|
|
2774
|
+
* @param [options] - Options to control the output.
|
|
2775
|
+
* @param [startNode=this._root] - The node to start printing from.
|
|
2776
|
+
|
|
2777
|
+
|
|
2778
|
+
|
|
2779
|
+
|
|
2780
|
+
|
|
2781
|
+
|
|
2782
|
+
|
|
2783
|
+
|
|
2784
|
+
|
|
2785
|
+
* @example
|
|
2786
|
+
* // Display tree
|
|
2787
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2788
|
+
* expect(() => tree.print()).not.toThrow();
|
|
2789
|
+
*/
|
|
2326
2790
|
print(options, startNode = this._root) {
|
|
2327
2791
|
console.log(this.toVisual(startNode, options));
|
|
2328
2792
|
}
|
|
@@ -2561,7 +3025,6 @@ var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
2561
3025
|
* @returns Layout information for this subtree.
|
|
2562
3026
|
*/
|
|
2563
3027
|
_displayAux(node, options) {
|
|
2564
|
-
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
2565
3028
|
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
2566
3029
|
const newFrame = /* @__PURE__ */ __name((n) => ({
|
|
2567
3030
|
node: n,
|