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