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