data-structure-typed 1.47.5 → 1.47.7
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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +103 -28
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +46 -29
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +182 -184
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/cjs/data-structures/binary-tree/bst.js +168 -169
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +77 -31
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +66 -136
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/cjs/data-structures/heap/heap.js +52 -34
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/min-heap.js +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/cjs/data-structures/queue/deque.js +3 -0
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/cjs/data-structures/queue/queue.js +3 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/data-structures/stack/stack.js +10 -2
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -2
- package/dist/cjs/types/common.d.ts +7 -0
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +49 -30
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +182 -194
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/mjs/data-structures/binary-tree/bst.js +171 -170
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +81 -33
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +70 -138
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/mjs/data-structures/heap/heap.js +53 -35
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/mjs/data-structures/queue/deque.js +3 -0
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/mjs/data-structures/queue/queue.js +3 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/data-structures/stack/stack.js +10 -2
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -2
- package/dist/mjs/types/common.d.ts +7 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +629 -595
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +61 -31
- package/src/data-structures/binary-tree/binary-tree.ts +283 -254
- package/src/data-structures/binary-tree/bst.ts +193 -170
- package/src/data-structures/binary-tree/rb-tree.ts +87 -32
- package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/heap/heap.ts +57 -39
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +14 -2
- package/src/types/common.ts +15 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +87 -0
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -57
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +156 -0
- package/tsconfig-cjs.json +1 -1
- package/test/integration/conversion.test.ts +0 -0
|
@@ -517,13 +517,7 @@ var dataStructureTyped = (() => {
|
|
|
517
517
|
|
|
518
518
|
// src/data-structures/hash/hash-map.ts
|
|
519
519
|
var HashMap = class _HashMap {
|
|
520
|
-
|
|
521
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
522
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
523
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
524
|
-
*/
|
|
525
|
-
constructor(options = {
|
|
526
|
-
elements: [],
|
|
520
|
+
constructor(elements, options = {
|
|
527
521
|
hashFn: (key) => String(key),
|
|
528
522
|
objHashFn: (key) => key
|
|
529
523
|
}) {
|
|
@@ -537,7 +531,7 @@ var dataStructureTyped = (() => {
|
|
|
537
531
|
__publicField(this, "_size", 0);
|
|
538
532
|
this._sentinel = {};
|
|
539
533
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
540
|
-
const {
|
|
534
|
+
const { hashFn, objHashFn } = options;
|
|
541
535
|
this._hashFn = hashFn;
|
|
542
536
|
this._objHashFn = objHashFn;
|
|
543
537
|
if (elements) {
|
|
@@ -844,6 +838,9 @@ var dataStructureTyped = (() => {
|
|
|
844
838
|
node = node.next;
|
|
845
839
|
}
|
|
846
840
|
}
|
|
841
|
+
print() {
|
|
842
|
+
console.log([...this]);
|
|
843
|
+
}
|
|
847
844
|
/**
|
|
848
845
|
* Time Complexity: O(1)
|
|
849
846
|
* Space Complexity: O(1)
|
|
@@ -886,13 +883,17 @@ var dataStructureTyped = (() => {
|
|
|
886
883
|
/**
|
|
887
884
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
888
885
|
*/
|
|
889
|
-
constructor() {
|
|
886
|
+
constructor(elements) {
|
|
890
887
|
__publicField(this, "_head");
|
|
891
888
|
__publicField(this, "_tail");
|
|
892
889
|
__publicField(this, "_length");
|
|
893
890
|
this._head = null;
|
|
894
891
|
this._tail = null;
|
|
895
892
|
this._length = 0;
|
|
893
|
+
if (elements) {
|
|
894
|
+
for (const el of elements)
|
|
895
|
+
this.push(el);
|
|
896
|
+
}
|
|
896
897
|
}
|
|
897
898
|
get head() {
|
|
898
899
|
return this._head;
|
|
@@ -1568,6 +1569,9 @@ var dataStructureTyped = (() => {
|
|
|
1568
1569
|
}
|
|
1569
1570
|
return accumulator;
|
|
1570
1571
|
}
|
|
1572
|
+
print() {
|
|
1573
|
+
console.log([...this]);
|
|
1574
|
+
}
|
|
1571
1575
|
};
|
|
1572
1576
|
|
|
1573
1577
|
// src/data-structures/linked-list/doubly-linked-list.ts
|
|
@@ -1590,13 +1594,18 @@ var dataStructureTyped = (() => {
|
|
|
1590
1594
|
/**
|
|
1591
1595
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
1592
1596
|
*/
|
|
1593
|
-
constructor() {
|
|
1597
|
+
constructor(elements) {
|
|
1594
1598
|
__publicField(this, "_head");
|
|
1595
1599
|
__publicField(this, "_tail");
|
|
1596
1600
|
__publicField(this, "_length");
|
|
1597
1601
|
this._head = null;
|
|
1598
1602
|
this._tail = null;
|
|
1599
1603
|
this._length = 0;
|
|
1604
|
+
if (elements) {
|
|
1605
|
+
for (const el of elements) {
|
|
1606
|
+
this.push(el);
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1600
1609
|
}
|
|
1601
1610
|
get head() {
|
|
1602
1611
|
return this._head;
|
|
@@ -2325,6 +2334,9 @@ var dataStructureTyped = (() => {
|
|
|
2325
2334
|
}
|
|
2326
2335
|
return accumulator;
|
|
2327
2336
|
}
|
|
2337
|
+
print() {
|
|
2338
|
+
console.log([...this]);
|
|
2339
|
+
}
|
|
2328
2340
|
};
|
|
2329
2341
|
|
|
2330
2342
|
// src/data-structures/linked-list/skip-linked-list.ts
|
|
@@ -2585,7 +2597,12 @@ var dataStructureTyped = (() => {
|
|
|
2585
2597
|
*/
|
|
2586
2598
|
constructor(elements) {
|
|
2587
2599
|
__publicField(this, "_elements");
|
|
2588
|
-
this._elements =
|
|
2600
|
+
this._elements = [];
|
|
2601
|
+
if (elements) {
|
|
2602
|
+
for (const el of elements) {
|
|
2603
|
+
this.push(el);
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2589
2606
|
}
|
|
2590
2607
|
get elements() {
|
|
2591
2608
|
return this._elements;
|
|
@@ -2708,7 +2725,7 @@ var dataStructureTyped = (() => {
|
|
|
2708
2725
|
* @returns An iterator object.
|
|
2709
2726
|
*/
|
|
2710
2727
|
*[Symbol.iterator]() {
|
|
2711
|
-
for (let i = this.elements.length
|
|
2728
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
2712
2729
|
yield this.elements[i];
|
|
2713
2730
|
}
|
|
2714
2731
|
}
|
|
@@ -2752,6 +2769,9 @@ var dataStructureTyped = (() => {
|
|
|
2752
2769
|
}
|
|
2753
2770
|
return accumulator;
|
|
2754
2771
|
}
|
|
2772
|
+
print() {
|
|
2773
|
+
console.log([...this]);
|
|
2774
|
+
}
|
|
2755
2775
|
};
|
|
2756
2776
|
|
|
2757
2777
|
// src/data-structures/queue/queue.ts
|
|
@@ -3012,6 +3032,9 @@ var dataStructureTyped = (() => {
|
|
|
3012
3032
|
clone() {
|
|
3013
3033
|
return new _Queue(this.nodes.slice(this.offset));
|
|
3014
3034
|
}
|
|
3035
|
+
print() {
|
|
3036
|
+
console.log([...this]);
|
|
3037
|
+
}
|
|
3015
3038
|
*[Symbol.iterator]() {
|
|
3016
3039
|
for (const item of this.nodes) {
|
|
3017
3040
|
yield item;
|
|
@@ -3845,6 +3868,9 @@ var dataStructureTyped = (() => {
|
|
|
3845
3868
|
}
|
|
3846
3869
|
return accumulator;
|
|
3847
3870
|
}
|
|
3871
|
+
print() {
|
|
3872
|
+
console.log([...this]);
|
|
3873
|
+
}
|
|
3848
3874
|
/**
|
|
3849
3875
|
* Time Complexity: O(n)
|
|
3850
3876
|
* Space Complexity: O(n)
|
|
@@ -4075,21 +4101,32 @@ var dataStructureTyped = (() => {
|
|
|
4075
4101
|
|
|
4076
4102
|
// src/data-structures/heap/heap.ts
|
|
4077
4103
|
var Heap = class _Heap {
|
|
4078
|
-
constructor(options) {
|
|
4104
|
+
constructor(elements, options) {
|
|
4105
|
+
__publicField(this, "options");
|
|
4079
4106
|
__publicField(this, "_elements", []);
|
|
4080
|
-
|
|
4081
|
-
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4107
|
+
const defaultComparator = (a, b) => {
|
|
4108
|
+
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4109
|
+
throw new Error("The a, b params of compare function must be number");
|
|
4110
|
+
} else {
|
|
4111
|
+
return a - b;
|
|
4112
|
+
}
|
|
4113
|
+
};
|
|
4114
|
+
if (options) {
|
|
4115
|
+
this.options = options;
|
|
4116
|
+
} else {
|
|
4117
|
+
this.options = {
|
|
4118
|
+
comparator: defaultComparator
|
|
4119
|
+
};
|
|
4120
|
+
}
|
|
4121
|
+
if (elements) {
|
|
4122
|
+
for (const el of elements) {
|
|
4123
|
+
this.push(el);
|
|
4124
|
+
}
|
|
4085
4125
|
}
|
|
4086
4126
|
}
|
|
4087
4127
|
get elements() {
|
|
4088
4128
|
return this._elements;
|
|
4089
4129
|
}
|
|
4090
|
-
get comparator() {
|
|
4091
|
-
return this._comparator;
|
|
4092
|
-
}
|
|
4093
4130
|
/**
|
|
4094
4131
|
* Get the size (number of elements) of the heap.
|
|
4095
4132
|
*/
|
|
@@ -4107,10 +4144,11 @@ var dataStructureTyped = (() => {
|
|
|
4107
4144
|
/**
|
|
4108
4145
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
4109
4146
|
* @returns A new Heap instance.
|
|
4147
|
+
* @param elements
|
|
4110
4148
|
* @param options
|
|
4111
4149
|
*/
|
|
4112
|
-
static heapify(options) {
|
|
4113
|
-
return new _Heap(options);
|
|
4150
|
+
static heapify(elements, options) {
|
|
4151
|
+
return new _Heap(elements, options);
|
|
4114
4152
|
}
|
|
4115
4153
|
/**
|
|
4116
4154
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
@@ -4319,7 +4357,7 @@ var dataStructureTyped = (() => {
|
|
|
4319
4357
|
* @returns A new Heap instance containing the same elements.
|
|
4320
4358
|
*/
|
|
4321
4359
|
clone() {
|
|
4322
|
-
const clonedHeap = new _Heap(
|
|
4360
|
+
const clonedHeap = new _Heap([], this.options);
|
|
4323
4361
|
clonedHeap._elements = [...this.elements];
|
|
4324
4362
|
return clonedHeap;
|
|
4325
4363
|
}
|
|
@@ -4371,7 +4409,7 @@ var dataStructureTyped = (() => {
|
|
|
4371
4409
|
}
|
|
4372
4410
|
}
|
|
4373
4411
|
filter(predicate) {
|
|
4374
|
-
const filteredHeap = new _Heap(
|
|
4412
|
+
const filteredHeap = new _Heap([], this.options);
|
|
4375
4413
|
let index = 0;
|
|
4376
4414
|
for (const el of this) {
|
|
4377
4415
|
if (predicate(el, index, this)) {
|
|
@@ -4382,7 +4420,7 @@ var dataStructureTyped = (() => {
|
|
|
4382
4420
|
return filteredHeap;
|
|
4383
4421
|
}
|
|
4384
4422
|
map(callback, comparator) {
|
|
4385
|
-
const mappedHeap = new _Heap({ comparator });
|
|
4423
|
+
const mappedHeap = new _Heap([], { comparator });
|
|
4386
4424
|
let index = 0;
|
|
4387
4425
|
for (const el of this) {
|
|
4388
4426
|
mappedHeap.add(callback(el, index, this));
|
|
@@ -4403,6 +4441,13 @@ var dataStructureTyped = (() => {
|
|
|
4403
4441
|
* Time Complexity: O(log n)
|
|
4404
4442
|
* Space Complexity: O(1)
|
|
4405
4443
|
*/
|
|
4444
|
+
print() {
|
|
4445
|
+
console.log([...this]);
|
|
4446
|
+
}
|
|
4447
|
+
/**
|
|
4448
|
+
* Time Complexity: O(n)
|
|
4449
|
+
* Space Complexity: O(1)
|
|
4450
|
+
*/
|
|
4406
4451
|
/**
|
|
4407
4452
|
* Time Complexity: O(log n)
|
|
4408
4453
|
* Space Complexity: O(1)
|
|
@@ -4415,17 +4460,13 @@ var dataStructureTyped = (() => {
|
|
|
4415
4460
|
while (index > 0) {
|
|
4416
4461
|
const parent = index - 1 >> 1;
|
|
4417
4462
|
const parentItem = this.elements[parent];
|
|
4418
|
-
if (this.
|
|
4463
|
+
if (this.options.comparator(parentItem, element) <= 0)
|
|
4419
4464
|
break;
|
|
4420
4465
|
this.elements[index] = parentItem;
|
|
4421
4466
|
index = parent;
|
|
4422
4467
|
}
|
|
4423
4468
|
this.elements[index] = element;
|
|
4424
4469
|
}
|
|
4425
|
-
/**
|
|
4426
|
-
* Time Complexity: O(n)
|
|
4427
|
-
* Space Complexity: O(1)
|
|
4428
|
-
*/
|
|
4429
4470
|
/**
|
|
4430
4471
|
* Time Complexity: O(log n)
|
|
4431
4472
|
* Space Complexity: O(1)
|
|
@@ -4440,11 +4481,11 @@ var dataStructureTyped = (() => {
|
|
|
4440
4481
|
let left = index << 1 | 1;
|
|
4441
4482
|
const right = left + 1;
|
|
4442
4483
|
let minItem = this.elements[left];
|
|
4443
|
-
if (right < this.elements.length && this.
|
|
4484
|
+
if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) {
|
|
4444
4485
|
left = right;
|
|
4445
4486
|
minItem = this.elements[right];
|
|
4446
4487
|
}
|
|
4447
|
-
if (this.
|
|
4488
|
+
if (this.options.comparator(minItem, element) >= 0)
|
|
4448
4489
|
break;
|
|
4449
4490
|
this.elements[index] = minItem;
|
|
4450
4491
|
index = left;
|
|
@@ -4473,7 +4514,7 @@ var dataStructureTyped = (() => {
|
|
|
4473
4514
|
__publicField(this, "_min");
|
|
4474
4515
|
__publicField(this, "_comparator");
|
|
4475
4516
|
this.clear();
|
|
4476
|
-
this._comparator = comparator || this.
|
|
4517
|
+
this._comparator = comparator || this._defaultComparator;
|
|
4477
4518
|
if (typeof this.comparator !== "function") {
|
|
4478
4519
|
throw new Error("FibonacciHeap constructor: given comparator should be a function.");
|
|
4479
4520
|
}
|
|
@@ -4642,7 +4683,7 @@ var dataStructureTyped = (() => {
|
|
|
4642
4683
|
this._root = void 0;
|
|
4643
4684
|
} else {
|
|
4644
4685
|
this._min = z.right;
|
|
4645
|
-
this.
|
|
4686
|
+
this._consolidate();
|
|
4646
4687
|
}
|
|
4647
4688
|
this._size--;
|
|
4648
4689
|
return z.element;
|
|
@@ -4678,27 +4719,27 @@ var dataStructureTyped = (() => {
|
|
|
4678
4719
|
this._size += heapToMerge.size;
|
|
4679
4720
|
heapToMerge.clear();
|
|
4680
4721
|
}
|
|
4722
|
+
/**
|
|
4723
|
+
* Create a new node.
|
|
4724
|
+
* @param element
|
|
4725
|
+
* @protected
|
|
4726
|
+
*/
|
|
4727
|
+
createNode(element) {
|
|
4728
|
+
return new FibonacciHeapNode(element);
|
|
4729
|
+
}
|
|
4681
4730
|
/**
|
|
4682
4731
|
* Default comparator function used by the heap.
|
|
4683
4732
|
* @param {E} a
|
|
4684
4733
|
* @param {E} b
|
|
4685
4734
|
* @protected
|
|
4686
4735
|
*/
|
|
4687
|
-
|
|
4736
|
+
_defaultComparator(a, b) {
|
|
4688
4737
|
if (a < b)
|
|
4689
4738
|
return -1;
|
|
4690
4739
|
if (a > b)
|
|
4691
4740
|
return 1;
|
|
4692
4741
|
return 0;
|
|
4693
4742
|
}
|
|
4694
|
-
/**
|
|
4695
|
-
* Create a new node.
|
|
4696
|
-
* @param element
|
|
4697
|
-
* @protected
|
|
4698
|
-
*/
|
|
4699
|
-
createNode(element) {
|
|
4700
|
-
return new FibonacciHeapNode(element);
|
|
4701
|
-
}
|
|
4702
4743
|
/**
|
|
4703
4744
|
* Time Complexity: O(1)
|
|
4704
4745
|
* Space Complexity: O(1)
|
|
@@ -4753,7 +4794,7 @@ var dataStructureTyped = (() => {
|
|
|
4753
4794
|
* @param x
|
|
4754
4795
|
* @protected
|
|
4755
4796
|
*/
|
|
4756
|
-
|
|
4797
|
+
_link(y, x) {
|
|
4757
4798
|
this.removeFromRoot(y);
|
|
4758
4799
|
y.left = y;
|
|
4759
4800
|
y.right = y;
|
|
@@ -4772,7 +4813,7 @@ var dataStructureTyped = (() => {
|
|
|
4772
4813
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
4773
4814
|
* @protected
|
|
4774
4815
|
*/
|
|
4775
|
-
|
|
4816
|
+
_consolidate() {
|
|
4776
4817
|
const A = new Array(this.size);
|
|
4777
4818
|
const elements = this.consumeLinkedList(this.root);
|
|
4778
4819
|
let x, y, d, t;
|
|
@@ -4786,7 +4827,7 @@ var dataStructureTyped = (() => {
|
|
|
4786
4827
|
x = y;
|
|
4787
4828
|
y = t;
|
|
4788
4829
|
}
|
|
4789
|
-
this.
|
|
4830
|
+
this._link(y, x);
|
|
4790
4831
|
A[d] = void 0;
|
|
4791
4832
|
d++;
|
|
4792
4833
|
}
|
|
@@ -4802,7 +4843,7 @@ var dataStructureTyped = (() => {
|
|
|
4802
4843
|
|
|
4803
4844
|
// src/data-structures/heap/max-heap.ts
|
|
4804
4845
|
var MaxHeap = class extends Heap {
|
|
4805
|
-
constructor(options = {
|
|
4846
|
+
constructor(elements, options = {
|
|
4806
4847
|
comparator: (a, b) => {
|
|
4807
4848
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4808
4849
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -4811,13 +4852,13 @@ var dataStructureTyped = (() => {
|
|
|
4811
4852
|
}
|
|
4812
4853
|
}
|
|
4813
4854
|
}) {
|
|
4814
|
-
super(options);
|
|
4855
|
+
super(elements, options);
|
|
4815
4856
|
}
|
|
4816
4857
|
};
|
|
4817
4858
|
|
|
4818
4859
|
// src/data-structures/heap/min-heap.ts
|
|
4819
4860
|
var MinHeap = class extends Heap {
|
|
4820
|
-
constructor(options = {
|
|
4861
|
+
constructor(elements, options = {
|
|
4821
4862
|
comparator: (a, b) => {
|
|
4822
4863
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4823
4864
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -4826,20 +4867,20 @@ var dataStructureTyped = (() => {
|
|
|
4826
4867
|
}
|
|
4827
4868
|
}
|
|
4828
4869
|
}) {
|
|
4829
|
-
super(options);
|
|
4870
|
+
super(elements, options);
|
|
4830
4871
|
}
|
|
4831
4872
|
};
|
|
4832
4873
|
|
|
4833
4874
|
// src/data-structures/priority-queue/priority-queue.ts
|
|
4834
4875
|
var PriorityQueue = class extends Heap {
|
|
4835
|
-
constructor(options) {
|
|
4836
|
-
super(options);
|
|
4876
|
+
constructor(elements, options) {
|
|
4877
|
+
super(elements, options);
|
|
4837
4878
|
}
|
|
4838
4879
|
};
|
|
4839
4880
|
|
|
4840
4881
|
// src/data-structures/priority-queue/min-priority-queue.ts
|
|
4841
4882
|
var MinPriorityQueue = class extends PriorityQueue {
|
|
4842
|
-
constructor(options = {
|
|
4883
|
+
constructor(elements, options = {
|
|
4843
4884
|
comparator: (a, b) => {
|
|
4844
4885
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4845
4886
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -4848,13 +4889,13 @@ var dataStructureTyped = (() => {
|
|
|
4848
4889
|
}
|
|
4849
4890
|
}
|
|
4850
4891
|
}) {
|
|
4851
|
-
super(options);
|
|
4892
|
+
super(elements, options);
|
|
4852
4893
|
}
|
|
4853
4894
|
};
|
|
4854
4895
|
|
|
4855
4896
|
// src/data-structures/priority-queue/max-priority-queue.ts
|
|
4856
4897
|
var MaxPriorityQueue = class extends PriorityQueue {
|
|
4857
|
-
constructor(options = {
|
|
4898
|
+
constructor(elements, options = {
|
|
4858
4899
|
comparator: (a, b) => {
|
|
4859
4900
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4860
4901
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -4863,7 +4904,7 @@ var dataStructureTyped = (() => {
|
|
|
4863
4904
|
}
|
|
4864
4905
|
}
|
|
4865
4906
|
}) {
|
|
4866
|
-
super(options);
|
|
4907
|
+
super(elements, options);
|
|
4867
4908
|
}
|
|
4868
4909
|
};
|
|
4869
4910
|
|
|
@@ -5446,7 +5487,7 @@ var dataStructureTyped = (() => {
|
|
|
5446
5487
|
if (vertexOrKey instanceof AbstractVertex)
|
|
5447
5488
|
distMap.set(vertexOrKey, Infinity);
|
|
5448
5489
|
}
|
|
5449
|
-
const heap = new PriorityQueue({ comparator: (a, b) => a.key - b.key });
|
|
5490
|
+
const heap = new PriorityQueue([], { comparator: (a, b) => a.key - b.key });
|
|
5450
5491
|
heap.add({ key: 0, value: srcVertex });
|
|
5451
5492
|
distMap.set(srcVertex, 0);
|
|
5452
5493
|
preMap.set(srcVertex, null);
|
|
@@ -6772,10 +6813,10 @@ var dataStructureTyped = (() => {
|
|
|
6772
6813
|
};
|
|
6773
6814
|
|
|
6774
6815
|
// src/types/data-structures/binary-tree/binary-tree.ts
|
|
6775
|
-
var IterationType = /* @__PURE__ */ ((
|
|
6776
|
-
|
|
6777
|
-
|
|
6778
|
-
return
|
|
6816
|
+
var IterationType = /* @__PURE__ */ ((IterationType3) => {
|
|
6817
|
+
IterationType3["ITERATIVE"] = "ITERATIVE";
|
|
6818
|
+
IterationType3["RECURSIVE"] = "RECURSIVE";
|
|
6819
|
+
return IterationType3;
|
|
6779
6820
|
})(IterationType || {});
|
|
6780
6821
|
var FamilyPosition = /* @__PURE__ */ ((FamilyPosition2) => {
|
|
6781
6822
|
FamilyPosition2["ROOT"] = "ROOT";
|
|
@@ -6805,55 +6846,27 @@ var dataStructureTyped = (() => {
|
|
|
6805
6846
|
|
|
6806
6847
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
6807
6848
|
var BinaryTreeNode = class {
|
|
6808
|
-
/**
|
|
6809
|
-
* Creates a new instance of BinaryTreeNode.
|
|
6810
|
-
* @param {BTNKey} key - The key associated with the node.
|
|
6811
|
-
* @param {V} value - The value stored in the node.
|
|
6812
|
-
*/
|
|
6813
6849
|
constructor(key, value) {
|
|
6814
|
-
/**
|
|
6815
|
-
* The key associated with the node.
|
|
6816
|
-
*/
|
|
6817
6850
|
__publicField(this, "key");
|
|
6818
|
-
/**
|
|
6819
|
-
* The value stored in the node.
|
|
6820
|
-
*/
|
|
6821
6851
|
__publicField(this, "value");
|
|
6822
|
-
/**
|
|
6823
|
-
* The parent node of the current node.
|
|
6824
|
-
*/
|
|
6825
6852
|
__publicField(this, "parent");
|
|
6826
6853
|
__publicField(this, "_left");
|
|
6827
6854
|
__publicField(this, "_right");
|
|
6828
6855
|
this.key = key;
|
|
6829
6856
|
this.value = value;
|
|
6830
6857
|
}
|
|
6831
|
-
/**
|
|
6832
|
-
* Get the left child node.
|
|
6833
|
-
*/
|
|
6834
6858
|
get left() {
|
|
6835
6859
|
return this._left;
|
|
6836
6860
|
}
|
|
6837
|
-
/**
|
|
6838
|
-
* Set the left child node.
|
|
6839
|
-
* @param {N | null | undefined} v - The left child node.
|
|
6840
|
-
*/
|
|
6841
6861
|
set left(v) {
|
|
6842
6862
|
if (v) {
|
|
6843
6863
|
v.parent = this;
|
|
6844
6864
|
}
|
|
6845
6865
|
this._left = v;
|
|
6846
6866
|
}
|
|
6847
|
-
/**
|
|
6848
|
-
* Get the right child node.
|
|
6849
|
-
*/
|
|
6850
6867
|
get right() {
|
|
6851
6868
|
return this._right;
|
|
6852
6869
|
}
|
|
6853
|
-
/**
|
|
6854
|
-
* Set the right child node.
|
|
6855
|
-
* @param {N | null | undefined} v - The right child node.
|
|
6856
|
-
*/
|
|
6857
6870
|
set right(v) {
|
|
6858
6871
|
if (v) {
|
|
6859
6872
|
v.parent = this;
|
|
@@ -6879,30 +6892,32 @@ var dataStructureTyped = (() => {
|
|
|
6879
6892
|
};
|
|
6880
6893
|
var BinaryTree = class _BinaryTree {
|
|
6881
6894
|
/**
|
|
6882
|
-
*
|
|
6883
|
-
* @param
|
|
6895
|
+
* The constructor function initializes a binary tree object with optional elements and options.
|
|
6896
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
|
|
6897
|
+
* elements to be added to the binary tree.
|
|
6898
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
6899
|
+
* configuration options for the binary tree. In this case, it is of type
|
|
6900
|
+
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
6901
|
+
* required.
|
|
6884
6902
|
*/
|
|
6885
|
-
constructor(options) {
|
|
6886
|
-
__publicField(this, "
|
|
6903
|
+
constructor(elements, options) {
|
|
6904
|
+
__publicField(this, "iterationType", "ITERATIVE" /* ITERATIVE */);
|
|
6887
6905
|
__publicField(this, "_root");
|
|
6888
6906
|
__publicField(this, "_size");
|
|
6889
6907
|
__publicField(this, "_defaultOneParamCallback", (node) => node.key);
|
|
6890
6908
|
if (options) {
|
|
6891
|
-
|
|
6892
|
-
|
|
6893
|
-
|
|
6909
|
+
const { iterationType } = options;
|
|
6910
|
+
if (iterationType) {
|
|
6911
|
+
this.iterationType = iterationType;
|
|
6912
|
+
}
|
|
6894
6913
|
}
|
|
6895
6914
|
this._size = 0;
|
|
6915
|
+
if (elements)
|
|
6916
|
+
this.addMany(elements);
|
|
6896
6917
|
}
|
|
6897
|
-
/**
|
|
6898
|
-
* Get the root node of the binary tree.
|
|
6899
|
-
*/
|
|
6900
6918
|
get root() {
|
|
6901
6919
|
return this._root;
|
|
6902
6920
|
}
|
|
6903
|
-
/**
|
|
6904
|
-
* Get the number of nodes in the binary tree.
|
|
6905
|
-
*/
|
|
6906
6921
|
get size() {
|
|
6907
6922
|
return this._size;
|
|
6908
6923
|
}
|
|
@@ -6915,35 +6930,46 @@ var dataStructureTyped = (() => {
|
|
|
6915
6930
|
createNode(key, value) {
|
|
6916
6931
|
return new BinaryTreeNode(key, value);
|
|
6917
6932
|
}
|
|
6933
|
+
/**
|
|
6934
|
+
* The function creates a binary tree with the given options.
|
|
6935
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
6936
|
+
* behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
|
|
6937
|
+
* you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
|
|
6938
|
+
* @returns a new instance of a binary tree.
|
|
6939
|
+
*/
|
|
6918
6940
|
createTree(options) {
|
|
6919
|
-
return new _BinaryTree(__spreadValues(
|
|
6941
|
+
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
6920
6942
|
}
|
|
6921
6943
|
/**
|
|
6922
|
-
*
|
|
6923
|
-
*
|
|
6924
|
-
*
|
|
6944
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
6945
|
+
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
6946
|
+
* two type parameters V and N, representing the value and node type respectively.
|
|
6947
|
+
* @returns a boolean value.
|
|
6925
6948
|
*/
|
|
6949
|
+
isEntry(kne) {
|
|
6950
|
+
return Array.isArray(kne) && kne.length === 2;
|
|
6951
|
+
}
|
|
6926
6952
|
/**
|
|
6927
|
-
* Time Complexity
|
|
6928
|
-
* Space Complexity
|
|
6953
|
+
* Time Complexity O(log n) - O(n)
|
|
6954
|
+
* Space Complexity O(1)
|
|
6955
|
+
*/
|
|
6956
|
+
/**
|
|
6957
|
+
* Time Complexity O(log n) - O(n)
|
|
6958
|
+
* Space Complexity O(1)
|
|
6929
6959
|
*
|
|
6930
|
-
* The `add` function adds a new node
|
|
6931
|
-
*
|
|
6932
|
-
* @
|
|
6933
|
-
* following types:
|
|
6934
|
-
* @param {V} [value] - The value to be associated with the key or node being added to the binary
|
|
6935
|
-
* tree.
|
|
6936
|
-
* @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
|
|
6937
|
-
* tree, or `null` or `undefined` if the insertion was not successful.
|
|
6960
|
+
* The `add` function adds a new node to a binary tree, either by key or by providing a node object.
|
|
6961
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be one of the following:
|
|
6962
|
+
* @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
|
|
6938
6963
|
*/
|
|
6939
|
-
add(
|
|
6964
|
+
add(keyOrNodeOrEntry) {
|
|
6965
|
+
let inserted, needInsert;
|
|
6940
6966
|
const _bfs = (root, newNode) => {
|
|
6941
6967
|
const queue = new Queue([root]);
|
|
6942
6968
|
while (queue.size > 0) {
|
|
6943
6969
|
const cur = queue.shift();
|
|
6944
6970
|
if (newNode && cur.key === newNode.key) {
|
|
6945
|
-
cur
|
|
6946
|
-
return;
|
|
6971
|
+
this._replaceNode(cur, newNode);
|
|
6972
|
+
return newNode;
|
|
6947
6973
|
}
|
|
6948
6974
|
const inserted2 = this._addTo(newNode, cur);
|
|
6949
6975
|
if (inserted2 !== void 0)
|
|
@@ -6954,13 +6980,21 @@ var dataStructureTyped = (() => {
|
|
|
6954
6980
|
queue.push(cur.right);
|
|
6955
6981
|
}
|
|
6956
6982
|
};
|
|
6957
|
-
|
|
6958
|
-
if (keyOrNode === null) {
|
|
6983
|
+
if (keyOrNodeOrEntry === null) {
|
|
6959
6984
|
needInsert = null;
|
|
6960
|
-
} else if (this.isNodeKey(
|
|
6961
|
-
needInsert = this.createNode(
|
|
6962
|
-
} else if (
|
|
6963
|
-
needInsert =
|
|
6985
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
6986
|
+
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
6987
|
+
} else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
6988
|
+
needInsert = keyOrNodeOrEntry;
|
|
6989
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
6990
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
6991
|
+
if (key === void 0) {
|
|
6992
|
+
return;
|
|
6993
|
+
} else if (key === null) {
|
|
6994
|
+
needInsert = null;
|
|
6995
|
+
} else {
|
|
6996
|
+
needInsert = this.createNode(key, value);
|
|
6997
|
+
}
|
|
6964
6998
|
} else {
|
|
6965
6999
|
return;
|
|
6966
7000
|
}
|
|
@@ -6978,34 +7012,27 @@ var dataStructureTyped = (() => {
|
|
|
6978
7012
|
return inserted;
|
|
6979
7013
|
}
|
|
6980
7014
|
/**
|
|
6981
|
-
* Time Complexity: O(k
|
|
7015
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
6982
7016
|
* Space Complexity: O(1)
|
|
7017
|
+
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
6983
7018
|
*/
|
|
6984
7019
|
/**
|
|
6985
|
-
* Time Complexity: O(k
|
|
7020
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
6986
7021
|
* Space Complexity: O(1)
|
|
6987
7022
|
*
|
|
6988
|
-
* The `addMany`
|
|
6989
|
-
*
|
|
6990
|
-
* @param
|
|
6991
|
-
*
|
|
6992
|
-
*
|
|
6993
|
-
*
|
|
6994
|
-
* correspond to the keys or nodes being added. If provided, the values will be associated with the
|
|
6995
|
-
* keys or nodes during the add operation.
|
|
6996
|
-
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
7023
|
+
* The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
|
|
7024
|
+
* current instance, and returns an array of the inserted nodes.
|
|
7025
|
+
* @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
|
|
7026
|
+
* `BTNodeExemplar<V, N>` objects.
|
|
7027
|
+
* @returns The function `addMany` returns an array of values, where each value is either of type
|
|
7028
|
+
* `N`, `null`, or `undefined`.
|
|
6997
7029
|
*/
|
|
6998
|
-
addMany(
|
|
6999
|
-
|
|
7000
|
-
|
|
7001
|
-
|
|
7002
|
-
|
|
7003
|
-
|
|
7004
|
-
return this.add(null);
|
|
7005
|
-
}
|
|
7006
|
-
const value = values == null ? void 0 : values[i];
|
|
7007
|
-
return this.add(keyOrNode, value);
|
|
7008
|
-
});
|
|
7030
|
+
addMany(nodes) {
|
|
7031
|
+
const inserted = [];
|
|
7032
|
+
for (const kne of nodes) {
|
|
7033
|
+
inserted.push(this.add(kne));
|
|
7034
|
+
}
|
|
7035
|
+
return inserted;
|
|
7009
7036
|
}
|
|
7010
7037
|
/**
|
|
7011
7038
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
@@ -7015,22 +7042,14 @@ var dataStructureTyped = (() => {
|
|
|
7015
7042
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
7016
7043
|
* Space Complexity: O(1)
|
|
7017
7044
|
*
|
|
7018
|
-
* The `refill` function clears the
|
|
7019
|
-
* @param
|
|
7020
|
-
* `
|
|
7021
|
-
* @param {N[] | Array<V>} [values] - The `data` parameter is an optional array of values that will be assigned to
|
|
7022
|
-
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
7023
|
-
* array. Each value in the `data` array will be assigned to the
|
|
7024
|
-
* @returns The method is returning a boolean value.
|
|
7045
|
+
* The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
|
|
7046
|
+
* @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
|
|
7047
|
+
* contain either `BTNodeExemplar` objects, keys, or entries.
|
|
7025
7048
|
*/
|
|
7026
|
-
refill(
|
|
7049
|
+
refill(nodesOrKeysOrEntries) {
|
|
7027
7050
|
this.clear();
|
|
7028
|
-
|
|
7051
|
+
this.addMany(nodesOrKeysOrEntries);
|
|
7029
7052
|
}
|
|
7030
|
-
/**
|
|
7031
|
-
* Time Complexity: O(n)
|
|
7032
|
-
* Space Complexity: O(1)
|
|
7033
|
-
*/
|
|
7034
7053
|
/**
|
|
7035
7054
|
* Time Complexity: O(n)
|
|
7036
7055
|
* Space Complexity: O(1)
|
|
@@ -7075,7 +7094,7 @@ var dataStructureTyped = (() => {
|
|
|
7075
7094
|
const leftSubTreeRightMost = this.getRightMost(curr.left);
|
|
7076
7095
|
if (leftSubTreeRightMost) {
|
|
7077
7096
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
7078
|
-
orgCurrent = this.
|
|
7097
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
7079
7098
|
if (parentOfLeftSubTreeMax) {
|
|
7080
7099
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
7081
7100
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -7108,8 +7127,8 @@ var dataStructureTyped = (() => {
|
|
|
7108
7127
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
7109
7128
|
*/
|
|
7110
7129
|
getDepth(distNode, beginRoot = this.root) {
|
|
7111
|
-
distNode = this.
|
|
7112
|
-
beginRoot = this.
|
|
7130
|
+
distNode = this.ensureNode(distNode);
|
|
7131
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7113
7132
|
let depth = 0;
|
|
7114
7133
|
while (distNode == null ? void 0 : distNode.parent) {
|
|
7115
7134
|
if (distNode === beginRoot) {
|
|
@@ -7122,8 +7141,7 @@ var dataStructureTyped = (() => {
|
|
|
7122
7141
|
}
|
|
7123
7142
|
/**
|
|
7124
7143
|
* Time Complexity: O(n)
|
|
7125
|
-
* Space Complexity: O(
|
|
7126
|
-
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
7144
|
+
* Space Complexity: O(1)
|
|
7127
7145
|
*/
|
|
7128
7146
|
/**
|
|
7129
7147
|
* Time Complexity: O(n)
|
|
@@ -7139,8 +7157,8 @@ var dataStructureTyped = (() => {
|
|
|
7139
7157
|
* values:
|
|
7140
7158
|
* @returns the height of the binary tree.
|
|
7141
7159
|
*/
|
|
7142
|
-
getHeight(beginRoot = this.root, iterationType = this.
|
|
7143
|
-
beginRoot = this.
|
|
7160
|
+
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7161
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7144
7162
|
if (!beginRoot)
|
|
7145
7163
|
return -1;
|
|
7146
7164
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7184,9 +7202,9 @@ var dataStructureTyped = (() => {
|
|
|
7184
7202
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
7185
7203
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
7186
7204
|
*/
|
|
7187
|
-
getMinHeight(beginRoot = this.root, iterationType = this.
|
|
7205
|
+
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7188
7206
|
var _a, _b, _c;
|
|
7189
|
-
beginRoot = this.
|
|
7207
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7190
7208
|
if (!beginRoot)
|
|
7191
7209
|
return -1;
|
|
7192
7210
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7229,6 +7247,7 @@ var dataStructureTyped = (() => {
|
|
|
7229
7247
|
/**
|
|
7230
7248
|
* Time Complexity: O(n)
|
|
7231
7249
|
* Space Complexity: O(log n)
|
|
7250
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
7232
7251
|
*/
|
|
7233
7252
|
/**
|
|
7234
7253
|
* Time Complexity: O(n)
|
|
@@ -7244,10 +7263,6 @@ var dataStructureTyped = (() => {
|
|
|
7244
7263
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
7245
7264
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
7246
7265
|
}
|
|
7247
|
-
/**
|
|
7248
|
-
* Time Complexity: O(n)
|
|
7249
|
-
* Space Complexity: O(log n).
|
|
7250
|
-
*/
|
|
7251
7266
|
/**
|
|
7252
7267
|
* Time Complexity: O(n)
|
|
7253
7268
|
* Space Complexity: O(log n).
|
|
@@ -7273,10 +7288,10 @@ var dataStructureTyped = (() => {
|
|
|
7273
7288
|
* traverse the binary tree. It can have two possible values:
|
|
7274
7289
|
* @returns an array of nodes of type `N`.
|
|
7275
7290
|
*/
|
|
7276
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.
|
|
7291
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7277
7292
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7278
7293
|
callback = (node) => node;
|
|
7279
|
-
beginRoot = this.
|
|
7294
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7280
7295
|
if (!beginRoot)
|
|
7281
7296
|
return [];
|
|
7282
7297
|
const ans = [];
|
|
@@ -7310,10 +7325,6 @@ var dataStructureTyped = (() => {
|
|
|
7310
7325
|
}
|
|
7311
7326
|
return ans;
|
|
7312
7327
|
}
|
|
7313
|
-
/**
|
|
7314
|
-
* Time Complexity: O(n)
|
|
7315
|
-
* Space Complexity: O(log n).
|
|
7316
|
-
*/
|
|
7317
7328
|
/**
|
|
7318
7329
|
* Time Complexity: O(n)
|
|
7319
7330
|
*
|
|
@@ -7334,15 +7345,11 @@ var dataStructureTyped = (() => {
|
|
|
7334
7345
|
* be performed in a pre-order, in-order, or post-order manner.
|
|
7335
7346
|
* @returns a boolean value.
|
|
7336
7347
|
*/
|
|
7337
|
-
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7348
|
+
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7338
7349
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7339
7350
|
callback = (node) => node;
|
|
7340
7351
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
7341
7352
|
}
|
|
7342
|
-
/**
|
|
7343
|
-
* Time Complexity: O(n)
|
|
7344
|
-
* Space Complexity: O(log n)
|
|
7345
|
-
*/
|
|
7346
7353
|
/**
|
|
7347
7354
|
* Time Complexity: O(n)
|
|
7348
7355
|
* Space Complexity: O(log n)
|
|
@@ -7364,7 +7371,7 @@ var dataStructureTyped = (() => {
|
|
|
7364
7371
|
* nodes are visited during the search.
|
|
7365
7372
|
* @returns a value of type `N | null | undefined`.
|
|
7366
7373
|
*/
|
|
7367
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7374
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7368
7375
|
var _a;
|
|
7369
7376
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7370
7377
|
callback = (node) => node;
|
|
@@ -7417,7 +7424,11 @@ var dataStructureTyped = (() => {
|
|
|
7417
7424
|
}
|
|
7418
7425
|
}
|
|
7419
7426
|
/**
|
|
7420
|
-
*
|
|
7427
|
+
* Time Complexity: O(n)
|
|
7428
|
+
* Space Complexity: O(log n)
|
|
7429
|
+
*/
|
|
7430
|
+
/**
|
|
7431
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
7421
7432
|
* key, otherwise it returns the key itself.
|
|
7422
7433
|
* @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
|
|
7423
7434
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
@@ -7427,13 +7438,9 @@ var dataStructureTyped = (() => {
|
|
|
7427
7438
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
7428
7439
|
* itself if it is not a valid node key.
|
|
7429
7440
|
*/
|
|
7430
|
-
|
|
7441
|
+
ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
7431
7442
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
7432
7443
|
}
|
|
7433
|
-
/**
|
|
7434
|
-
* Time Complexity: O(n)
|
|
7435
|
-
* Space Complexity: O(log n)
|
|
7436
|
-
*/
|
|
7437
7444
|
/**
|
|
7438
7445
|
* Time Complexity: O(n)
|
|
7439
7446
|
* Space Complexity: O(log n)
|
|
@@ -7456,12 +7463,16 @@ var dataStructureTyped = (() => {
|
|
|
7456
7463
|
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
7457
7464
|
* found, `undefined` is returned.
|
|
7458
7465
|
*/
|
|
7459
|
-
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7466
|
+
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7460
7467
|
var _a, _b;
|
|
7461
7468
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7462
7469
|
callback = (node) => node;
|
|
7463
7470
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value) != null ? _b : void 0;
|
|
7464
7471
|
}
|
|
7472
|
+
/**
|
|
7473
|
+
* Time Complexity: O(n)
|
|
7474
|
+
* Space Complexity: O(log n)
|
|
7475
|
+
*/
|
|
7465
7476
|
/**
|
|
7466
7477
|
* Clear the binary tree, removing all nodes.
|
|
7467
7478
|
*/
|
|
@@ -7476,10 +7487,6 @@ var dataStructureTyped = (() => {
|
|
|
7476
7487
|
isEmpty() {
|
|
7477
7488
|
return this.size === 0;
|
|
7478
7489
|
}
|
|
7479
|
-
/**
|
|
7480
|
-
* Time Complexity: O(log n)
|
|
7481
|
-
* Space Complexity: O(log n)
|
|
7482
|
-
*/
|
|
7483
7490
|
/**
|
|
7484
7491
|
* Time Complexity: O(log n)
|
|
7485
7492
|
* Space Complexity: O(log n)
|
|
@@ -7496,7 +7503,7 @@ var dataStructureTyped = (() => {
|
|
|
7496
7503
|
*/
|
|
7497
7504
|
getPathToRoot(beginRoot, isReverse = true) {
|
|
7498
7505
|
const result = [];
|
|
7499
|
-
beginRoot = this.
|
|
7506
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7500
7507
|
if (!beginRoot)
|
|
7501
7508
|
return result;
|
|
7502
7509
|
while (beginRoot.parent) {
|
|
@@ -7508,7 +7515,7 @@ var dataStructureTyped = (() => {
|
|
|
7508
7515
|
}
|
|
7509
7516
|
/**
|
|
7510
7517
|
* Time Complexity: O(log n)
|
|
7511
|
-
* Space Complexity: O(
|
|
7518
|
+
* Space Complexity: O(log n)
|
|
7512
7519
|
*/
|
|
7513
7520
|
/**
|
|
7514
7521
|
* Time Complexity: O(log n)
|
|
@@ -7524,8 +7531,8 @@ var dataStructureTyped = (() => {
|
|
|
7524
7531
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
7525
7532
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
7526
7533
|
*/
|
|
7527
|
-
getLeftMost(beginRoot = this.root, iterationType = this.
|
|
7528
|
-
beginRoot = this.
|
|
7534
|
+
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7535
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7529
7536
|
if (!beginRoot)
|
|
7530
7537
|
return beginRoot;
|
|
7531
7538
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7563,8 +7570,8 @@ var dataStructureTyped = (() => {
|
|
|
7563
7570
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
7564
7571
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
7565
7572
|
*/
|
|
7566
|
-
getRightMost(beginRoot = this.root, iterationType = this.
|
|
7567
|
-
beginRoot = this.
|
|
7573
|
+
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7574
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7568
7575
|
if (!beginRoot)
|
|
7569
7576
|
return beginRoot;
|
|
7570
7577
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7584,7 +7591,7 @@ var dataStructureTyped = (() => {
|
|
|
7584
7591
|
}
|
|
7585
7592
|
}
|
|
7586
7593
|
/**
|
|
7587
|
-
* Time Complexity: O(n)
|
|
7594
|
+
* Time Complexity: O(log n)
|
|
7588
7595
|
* Space Complexity: O(1)
|
|
7589
7596
|
*/
|
|
7590
7597
|
/**
|
|
@@ -7599,8 +7606,8 @@ var dataStructureTyped = (() => {
|
|
|
7599
7606
|
* possible values:
|
|
7600
7607
|
* @returns a boolean value.
|
|
7601
7608
|
*/
|
|
7602
|
-
isSubtreeBST(beginRoot, iterationType = this.
|
|
7603
|
-
beginRoot = this.
|
|
7609
|
+
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
7610
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7604
7611
|
if (!beginRoot)
|
|
7605
7612
|
return true;
|
|
7606
7613
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7644,15 +7651,11 @@ var dataStructureTyped = (() => {
|
|
|
7644
7651
|
* expected to be
|
|
7645
7652
|
* @returns a boolean value.
|
|
7646
7653
|
*/
|
|
7647
|
-
isBST(iterationType = this.
|
|
7654
|
+
isBST(iterationType = this.iterationType) {
|
|
7648
7655
|
if (this.root === null)
|
|
7649
7656
|
return true;
|
|
7650
7657
|
return this.isSubtreeBST(this.root, iterationType);
|
|
7651
7658
|
}
|
|
7652
|
-
/**
|
|
7653
|
-
* Time complexity: O(n)
|
|
7654
|
-
* Space complexity: O(log n)
|
|
7655
|
-
*/
|
|
7656
7659
|
/**
|
|
7657
7660
|
* Time complexity: O(n)
|
|
7658
7661
|
* Space complexity: O(log n)
|
|
@@ -7675,8 +7678,8 @@ var dataStructureTyped = (() => {
|
|
|
7675
7678
|
* the `callback` function on each node in the subtree. The type of the array elements is determined
|
|
7676
7679
|
* by the return type of the `callback` function.
|
|
7677
7680
|
*/
|
|
7678
|
-
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7679
|
-
beginRoot = this.
|
|
7681
|
+
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7682
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7680
7683
|
const ans = [];
|
|
7681
7684
|
if (!beginRoot)
|
|
7682
7685
|
return ans;
|
|
@@ -7712,6 +7715,10 @@ var dataStructureTyped = (() => {
|
|
|
7712
7715
|
}
|
|
7713
7716
|
return ans;
|
|
7714
7717
|
}
|
|
7718
|
+
/**
|
|
7719
|
+
* Time complexity: O(n)
|
|
7720
|
+
* Space complexity: O(log n)
|
|
7721
|
+
*/
|
|
7715
7722
|
/**
|
|
7716
7723
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
7717
7724
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -7746,10 +7753,6 @@ var dataStructureTyped = (() => {
|
|
|
7746
7753
|
isNodeKey(potentialKey) {
|
|
7747
7754
|
return typeof potentialKey === "number";
|
|
7748
7755
|
}
|
|
7749
|
-
/**
|
|
7750
|
-
* Time complexity: O(n)
|
|
7751
|
-
* Space complexity: O(n)
|
|
7752
|
-
*/
|
|
7753
7756
|
/**
|
|
7754
7757
|
* Time complexity: O(n)
|
|
7755
7758
|
* Space complexity: O(n)
|
|
@@ -7774,7 +7777,7 @@ var dataStructureTyped = (() => {
|
|
|
7774
7777
|
* @returns an array of values that are the return values of the callback function.
|
|
7775
7778
|
*/
|
|
7776
7779
|
dfs(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root, iterationType = "ITERATIVE" /* ITERATIVE */, includeNull = false) {
|
|
7777
|
-
beginRoot = this.
|
|
7780
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7778
7781
|
if (!beginRoot)
|
|
7779
7782
|
return [];
|
|
7780
7783
|
const ans = [];
|
|
@@ -7872,10 +7875,6 @@ var dataStructureTyped = (() => {
|
|
|
7872
7875
|
}
|
|
7873
7876
|
return ans;
|
|
7874
7877
|
}
|
|
7875
|
-
/**
|
|
7876
|
-
* Time complexity: O(n)
|
|
7877
|
-
* Space complexity: O(n)
|
|
7878
|
-
*/
|
|
7879
7878
|
/**
|
|
7880
7879
|
* Time complexity: O(n)
|
|
7881
7880
|
* Space complexity: O(n)
|
|
@@ -7897,8 +7896,8 @@ var dataStructureTyped = (() => {
|
|
|
7897
7896
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
7898
7897
|
* the breadth-first traversal of a binary tree.
|
|
7899
7898
|
*/
|
|
7900
|
-
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7901
|
-
beginRoot = this.
|
|
7899
|
+
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7900
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7902
7901
|
if (!beginRoot)
|
|
7903
7902
|
return [];
|
|
7904
7903
|
const ans = [];
|
|
@@ -7946,10 +7945,6 @@ var dataStructureTyped = (() => {
|
|
|
7946
7945
|
}
|
|
7947
7946
|
return ans;
|
|
7948
7947
|
}
|
|
7949
|
-
/**
|
|
7950
|
-
* Time complexity: O(n)
|
|
7951
|
-
* Space complexity: O(n)
|
|
7952
|
-
*/
|
|
7953
7948
|
/**
|
|
7954
7949
|
* Time complexity: O(n)
|
|
7955
7950
|
* Space complexity: O(n)
|
|
@@ -7971,8 +7966,8 @@ var dataStructureTyped = (() => {
|
|
|
7971
7966
|
* be excluded
|
|
7972
7967
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
7973
7968
|
*/
|
|
7974
|
-
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7975
|
-
beginRoot = this.
|
|
7969
|
+
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7970
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7976
7971
|
const levelsNodes = [];
|
|
7977
7972
|
if (!beginRoot)
|
|
7978
7973
|
return levelsNodes;
|
|
@@ -8024,7 +8019,7 @@ var dataStructureTyped = (() => {
|
|
|
8024
8019
|
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
8025
8020
|
*/
|
|
8026
8021
|
getPredecessor(node) {
|
|
8027
|
-
node = this.
|
|
8022
|
+
node = this.ensureNode(node);
|
|
8028
8023
|
if (!this.isRealNode(node))
|
|
8029
8024
|
return void 0;
|
|
8030
8025
|
if (node.left) {
|
|
@@ -8046,7 +8041,7 @@ var dataStructureTyped = (() => {
|
|
|
8046
8041
|
* after the given node in the inorder traversal of the binary tree.
|
|
8047
8042
|
*/
|
|
8048
8043
|
getSuccessor(x) {
|
|
8049
|
-
x = this.
|
|
8044
|
+
x = this.ensureNode(x);
|
|
8050
8045
|
if (!x)
|
|
8051
8046
|
return void 0;
|
|
8052
8047
|
if (x.right) {
|
|
@@ -8059,10 +8054,6 @@ var dataStructureTyped = (() => {
|
|
|
8059
8054
|
}
|
|
8060
8055
|
return y;
|
|
8061
8056
|
}
|
|
8062
|
-
/**
|
|
8063
|
-
* Time complexity: O(n)
|
|
8064
|
-
* Space complexity: O(1)
|
|
8065
|
-
*/
|
|
8066
8057
|
/**
|
|
8067
8058
|
* Time complexity: O(n)
|
|
8068
8059
|
* Space complexity: O(1)
|
|
@@ -8082,7 +8073,7 @@ var dataStructureTyped = (() => {
|
|
|
8082
8073
|
* by the return type of the `callback` function.
|
|
8083
8074
|
*/
|
|
8084
8075
|
morris(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root) {
|
|
8085
|
-
beginRoot = this.
|
|
8076
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8086
8077
|
if (beginRoot === null)
|
|
8087
8078
|
return [];
|
|
8088
8079
|
const ans = [];
|
|
@@ -8162,6 +8153,10 @@ var dataStructureTyped = (() => {
|
|
|
8162
8153
|
}
|
|
8163
8154
|
return ans;
|
|
8164
8155
|
}
|
|
8156
|
+
/**
|
|
8157
|
+
* Time complexity: O(n)
|
|
8158
|
+
* Space complexity: O(1)
|
|
8159
|
+
*/
|
|
8165
8160
|
/**
|
|
8166
8161
|
* The `forEach` function iterates over each entry in a tree and calls a callback function with the
|
|
8167
8162
|
* entry and the tree as arguments.
|
|
@@ -8185,19 +8180,11 @@ var dataStructureTyped = (() => {
|
|
|
8185
8180
|
const newTree = this.createTree();
|
|
8186
8181
|
for (const [key, value] of this) {
|
|
8187
8182
|
if (predicate([key, value], this)) {
|
|
8188
|
-
newTree.add(key, value);
|
|
8183
|
+
newTree.add([key, value]);
|
|
8189
8184
|
}
|
|
8190
8185
|
}
|
|
8191
8186
|
return newTree;
|
|
8192
8187
|
}
|
|
8193
|
-
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
8194
|
-
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
8195
|
-
// const newTree = this.createTree();
|
|
8196
|
-
// for (const [key, value] of this) {
|
|
8197
|
-
// newTree.add(key, callback([key, value], this));
|
|
8198
|
-
// }
|
|
8199
|
-
// return newTree;
|
|
8200
|
-
// }
|
|
8201
8188
|
/**
|
|
8202
8189
|
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
8203
8190
|
* tree.
|
|
@@ -8207,10 +8194,18 @@ var dataStructureTyped = (() => {
|
|
|
8207
8194
|
map(callback) {
|
|
8208
8195
|
const newTree = this.createTree();
|
|
8209
8196
|
for (const [key, value] of this) {
|
|
8210
|
-
newTree.add(key, callback([key, value], this));
|
|
8197
|
+
newTree.add([key, callback([key, value], this)]);
|
|
8211
8198
|
}
|
|
8212
8199
|
return newTree;
|
|
8213
8200
|
}
|
|
8201
|
+
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
8202
|
+
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
8203
|
+
// const newTree = this.createTree();
|
|
8204
|
+
// for (const [key, value] of this) {
|
|
8205
|
+
// newTree.add(key, callback([key, value], this));
|
|
8206
|
+
// }
|
|
8207
|
+
// return newTree;
|
|
8208
|
+
// }
|
|
8214
8209
|
/**
|
|
8215
8210
|
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
8216
8211
|
* entry, accumulating a single value.
|
|
@@ -8242,7 +8237,7 @@ var dataStructureTyped = (() => {
|
|
|
8242
8237
|
*[Symbol.iterator](node = this.root) {
|
|
8243
8238
|
if (!node)
|
|
8244
8239
|
return;
|
|
8245
|
-
if (this.
|
|
8240
|
+
if (this.iterationType === "ITERATIVE" /* ITERATIVE */) {
|
|
8246
8241
|
const stack = [];
|
|
8247
8242
|
let current = node;
|
|
8248
8243
|
while (current || stack.length > 0) {
|
|
@@ -8275,7 +8270,7 @@ var dataStructureTyped = (() => {
|
|
|
8275
8270
|
*/
|
|
8276
8271
|
print(beginRoot = this.root, options) {
|
|
8277
8272
|
const opts = __spreadValues({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
|
|
8278
|
-
beginRoot = this.
|
|
8273
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8279
8274
|
if (!beginRoot)
|
|
8280
8275
|
return;
|
|
8281
8276
|
if (opts.isShowUndefined)
|
|
@@ -8331,9 +8326,9 @@ var dataStructureTyped = (() => {
|
|
|
8331
8326
|
* @param {N} destNode - The destination node to swap.
|
|
8332
8327
|
* @returns {N} - The destination node after the swap.
|
|
8333
8328
|
*/
|
|
8334
|
-
|
|
8335
|
-
srcNode = this.
|
|
8336
|
-
destNode = this.
|
|
8329
|
+
_swapProperties(srcNode, destNode) {
|
|
8330
|
+
srcNode = this.ensureNode(srcNode);
|
|
8331
|
+
destNode = this.ensureNode(destNode);
|
|
8337
8332
|
if (srcNode && destNode) {
|
|
8338
8333
|
const { key, value } = destNode;
|
|
8339
8334
|
const tempNode = this.createNode(key, value);
|
|
@@ -8347,6 +8342,30 @@ var dataStructureTyped = (() => {
|
|
|
8347
8342
|
}
|
|
8348
8343
|
return void 0;
|
|
8349
8344
|
}
|
|
8345
|
+
/**
|
|
8346
|
+
* The function replaces an old node with a new node in a binary tree.
|
|
8347
|
+
* @param {N} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
8348
|
+
* tree.
|
|
8349
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
8350
|
+
* tree.
|
|
8351
|
+
* @returns The method is returning the newNode.
|
|
8352
|
+
*/
|
|
8353
|
+
_replaceNode(oldNode, newNode) {
|
|
8354
|
+
if (oldNode.parent) {
|
|
8355
|
+
if (oldNode.parent.left === oldNode) {
|
|
8356
|
+
oldNode.parent.left = newNode;
|
|
8357
|
+
} else if (oldNode.parent.right === oldNode) {
|
|
8358
|
+
oldNode.parent.right = newNode;
|
|
8359
|
+
}
|
|
8360
|
+
}
|
|
8361
|
+
newNode.left = oldNode.left;
|
|
8362
|
+
newNode.right = oldNode.right;
|
|
8363
|
+
newNode.parent = oldNode.parent;
|
|
8364
|
+
if (this.root === oldNode) {
|
|
8365
|
+
this._root = newNode;
|
|
8366
|
+
}
|
|
8367
|
+
return newNode;
|
|
8368
|
+
}
|
|
8350
8369
|
/**
|
|
8351
8370
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
8352
8371
|
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|
|
@@ -8441,24 +8460,27 @@ var dataStructureTyped = (() => {
|
|
|
8441
8460
|
};
|
|
8442
8461
|
var BST = class _BST extends BinaryTree {
|
|
8443
8462
|
/**
|
|
8444
|
-
*
|
|
8445
|
-
*
|
|
8446
|
-
*
|
|
8463
|
+
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
8464
|
+
* the tree with optional elements and options.
|
|
8465
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
|
|
8466
|
+
* binary search tree.
|
|
8467
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
8468
|
+
* configuration options for the binary search tree. It can have the following properties:
|
|
8447
8469
|
*/
|
|
8448
|
-
constructor(options) {
|
|
8449
|
-
super(options);
|
|
8450
|
-
__publicField(this, "options");
|
|
8470
|
+
constructor(elements, options) {
|
|
8471
|
+
super([], options);
|
|
8451
8472
|
__publicField(this, "_root");
|
|
8473
|
+
__publicField(this, "comparator", (a, b) => a - b);
|
|
8452
8474
|
if (options) {
|
|
8453
|
-
|
|
8454
|
-
|
|
8455
|
-
|
|
8475
|
+
const { comparator } = options;
|
|
8476
|
+
if (comparator) {
|
|
8477
|
+
this.comparator = comparator;
|
|
8478
|
+
}
|
|
8456
8479
|
}
|
|
8457
8480
|
this._root = void 0;
|
|
8481
|
+
if (elements)
|
|
8482
|
+
this.addMany(elements);
|
|
8458
8483
|
}
|
|
8459
|
-
/**
|
|
8460
|
-
* Get the root node of the binary tree.
|
|
8461
|
-
*/
|
|
8462
8484
|
get root() {
|
|
8463
8485
|
return this._root;
|
|
8464
8486
|
}
|
|
@@ -8473,8 +8495,18 @@ var dataStructureTyped = (() => {
|
|
|
8473
8495
|
createNode(key, value) {
|
|
8474
8496
|
return new BSTNode(key, value);
|
|
8475
8497
|
}
|
|
8498
|
+
/**
|
|
8499
|
+
* The function creates a new binary search tree with the specified options.
|
|
8500
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
8501
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
|
|
8502
|
+
* that defines various options for creating a binary search tree.
|
|
8503
|
+
* @returns a new instance of the BST class with the specified options.
|
|
8504
|
+
*/
|
|
8476
8505
|
createTree(options) {
|
|
8477
|
-
return new _BST(__spreadValues(
|
|
8506
|
+
return new _BST([], __spreadValues({
|
|
8507
|
+
iterationType: this.iterationType,
|
|
8508
|
+
comparator: this.comparator
|
|
8509
|
+
}, options));
|
|
8478
8510
|
}
|
|
8479
8511
|
/**
|
|
8480
8512
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
@@ -8484,141 +8516,124 @@ var dataStructureTyped = (() => {
|
|
|
8484
8516
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8485
8517
|
* Space Complexity: O(1) - Constant space is used.
|
|
8486
8518
|
*
|
|
8487
|
-
* The `add` function adds a new node to a binary search tree
|
|
8488
|
-
*
|
|
8489
|
-
* following
|
|
8490
|
-
* @
|
|
8491
|
-
*
|
|
8492
|
-
* @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
|
|
8493
|
-
* no node was inserted, it returns `undefined`.
|
|
8519
|
+
* The `add` function adds a new node to a binary search tree, either by key or by providing a node
|
|
8520
|
+
* object.
|
|
8521
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
8522
|
+
* @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
|
|
8523
|
+
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
8494
8524
|
*/
|
|
8495
|
-
add(
|
|
8496
|
-
if (
|
|
8525
|
+
add(keyOrNodeOrEntry) {
|
|
8526
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
8497
8527
|
return void 0;
|
|
8498
|
-
|
|
8528
|
+
}
|
|
8499
8529
|
let newNode;
|
|
8500
|
-
if (
|
|
8501
|
-
newNode =
|
|
8502
|
-
} else if (this.isNodeKey(
|
|
8503
|
-
newNode = this.createNode(
|
|
8530
|
+
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
8531
|
+
newNode = keyOrNodeOrEntry;
|
|
8532
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
8533
|
+
newNode = this.createNode(keyOrNodeOrEntry);
|
|
8534
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
8535
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
8536
|
+
if (key === void 0 || key === null) {
|
|
8537
|
+
return;
|
|
8538
|
+
} else {
|
|
8539
|
+
newNode = this.createNode(key, value);
|
|
8540
|
+
}
|
|
8504
8541
|
} else {
|
|
8505
|
-
|
|
8542
|
+
return;
|
|
8506
8543
|
}
|
|
8507
8544
|
if (this.root === void 0) {
|
|
8508
8545
|
this._setRoot(newNode);
|
|
8509
|
-
this._size
|
|
8510
|
-
|
|
8511
|
-
}
|
|
8512
|
-
|
|
8513
|
-
|
|
8514
|
-
|
|
8515
|
-
|
|
8516
|
-
|
|
8517
|
-
|
|
8518
|
-
|
|
8519
|
-
|
|
8520
|
-
|
|
8521
|
-
|
|
8522
|
-
|
|
8523
|
-
|
|
8524
|
-
|
|
8525
|
-
|
|
8526
|
-
|
|
8527
|
-
|
|
8528
|
-
|
|
8529
|
-
|
|
8530
|
-
|
|
8531
|
-
} else {
|
|
8532
|
-
if (cur.left)
|
|
8533
|
-
cur = cur.left;
|
|
8534
|
-
}
|
|
8535
|
-
} else if (this._compare(cur.key, newNode.key) === "lt" /* lt */) {
|
|
8536
|
-
if (cur.right === void 0) {
|
|
8537
|
-
if (newNode) {
|
|
8538
|
-
newNode.parent = cur;
|
|
8539
|
-
}
|
|
8540
|
-
cur.right = newNode;
|
|
8541
|
-
this._size = this.size + 1;
|
|
8542
|
-
traversing = false;
|
|
8543
|
-
inserted = cur.right;
|
|
8544
|
-
} else {
|
|
8545
|
-
if (cur.right)
|
|
8546
|
-
cur = cur.right;
|
|
8547
|
-
}
|
|
8548
|
-
}
|
|
8549
|
-
} else {
|
|
8550
|
-
traversing = false;
|
|
8546
|
+
this._size++;
|
|
8547
|
+
return this.root;
|
|
8548
|
+
}
|
|
8549
|
+
let current = this.root;
|
|
8550
|
+
while (current !== void 0) {
|
|
8551
|
+
if (this._compare(current.key, newNode.key) === "eq" /* eq */) {
|
|
8552
|
+
this._replaceNode(current, newNode);
|
|
8553
|
+
return newNode;
|
|
8554
|
+
} else if (this._compare(current.key, newNode.key) === "gt" /* gt */) {
|
|
8555
|
+
if (current.left === void 0) {
|
|
8556
|
+
current.left = newNode;
|
|
8557
|
+
newNode.parent = current;
|
|
8558
|
+
this._size++;
|
|
8559
|
+
return newNode;
|
|
8560
|
+
}
|
|
8561
|
+
current = current.left;
|
|
8562
|
+
} else {
|
|
8563
|
+
if (current.right === void 0) {
|
|
8564
|
+
current.right = newNode;
|
|
8565
|
+
newNode.parent = current;
|
|
8566
|
+
this._size++;
|
|
8567
|
+
return newNode;
|
|
8551
8568
|
}
|
|
8569
|
+
current = current.right;
|
|
8552
8570
|
}
|
|
8553
8571
|
}
|
|
8554
|
-
return
|
|
8572
|
+
return void 0;
|
|
8555
8573
|
}
|
|
8556
8574
|
/**
|
|
8557
|
-
* Time Complexity: O(
|
|
8558
|
-
* Space Complexity: O(
|
|
8575
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
8576
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
8559
8577
|
*/
|
|
8560
8578
|
/**
|
|
8561
|
-
* Time Complexity: O(
|
|
8562
|
-
* Space Complexity: O(
|
|
8579
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
8580
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
8563
8581
|
*
|
|
8564
|
-
* The `addMany` function
|
|
8565
|
-
*
|
|
8566
|
-
* @param
|
|
8567
|
-
* binary
|
|
8568
|
-
* node), or `undefined`.
|
|
8569
|
-
* @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
|
|
8570
|
-
* nodes being added. If provided, the length of the `data` array must be the same as the length of
|
|
8571
|
-
* the `keysOrNodes` array.
|
|
8582
|
+
* The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
|
|
8583
|
+
* unbalanced manner, and returns an array of the inserted nodes.
|
|
8584
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
|
|
8585
|
+
* binary tree.
|
|
8572
8586
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
8573
|
-
* adding the nodes. The default value is
|
|
8587
|
+
* adding the nodes. The default value is true.
|
|
8574
8588
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
8575
|
-
* type of iteration to use when adding multiple keys or nodes to the binary
|
|
8576
|
-
*
|
|
8577
|
-
*
|
|
8578
|
-
* @returns The
|
|
8589
|
+
* type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
|
|
8590
|
+
* value of `this.iterationType`, which means it will use the iteration type specified by the binary
|
|
8591
|
+
* tree instance.
|
|
8592
|
+
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
8579
8593
|
*/
|
|
8580
|
-
addMany(
|
|
8581
|
-
function hasNoUndefined(arr) {
|
|
8582
|
-
return arr.indexOf(void 0) === -1;
|
|
8583
|
-
}
|
|
8584
|
-
if (!isBalanceAdd || !hasNoUndefined(keysOrNodes)) {
|
|
8585
|
-
return super.addMany(keysOrNodes, data).map((n) => n != null ? n : void 0);
|
|
8586
|
-
}
|
|
8594
|
+
addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
8587
8595
|
const inserted = [];
|
|
8588
|
-
|
|
8589
|
-
(
|
|
8590
|
-
|
|
8591
|
-
|
|
8592
|
-
|
|
8593
|
-
|
|
8594
|
-
if (keyOrNode instanceof BSTNode)
|
|
8595
|
-
return true;
|
|
8596
|
-
return false;
|
|
8596
|
+
if (!isBalanceAdd) {
|
|
8597
|
+
for (const kve of keysOrNodesOrEntries) {
|
|
8598
|
+
const nn = this.add(kve);
|
|
8599
|
+
inserted.push(nn);
|
|
8600
|
+
}
|
|
8601
|
+
return inserted;
|
|
8597
8602
|
}
|
|
8598
|
-
const
|
|
8599
|
-
|
|
8600
|
-
|
|
8601
|
-
|
|
8602
|
-
return
|
|
8603
|
+
const realBTNExemplars = [];
|
|
8604
|
+
const isRealBTNExemplar = (kve) => {
|
|
8605
|
+
if (kve === void 0 || kve === null)
|
|
8606
|
+
return false;
|
|
8607
|
+
return !(this.isEntry(kve) && (kve[0] === void 0 || kve[0] === null));
|
|
8603
8608
|
};
|
|
8604
|
-
|
|
8605
|
-
|
|
8606
|
-
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
8607
|
-
} else if (_isBinaryTreeKeyOrNullTuple(combinedArr)) {
|
|
8608
|
-
sorted = combinedArr.sort((a, b) => a[0] - b[0]);
|
|
8609
|
-
} else {
|
|
8610
|
-
throw new Error("Invalid input keysOrNodes");
|
|
8609
|
+
for (const kve of keysOrNodesOrEntries) {
|
|
8610
|
+
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
8611
8611
|
}
|
|
8612
|
-
|
|
8613
|
-
|
|
8614
|
-
|
|
8612
|
+
let sorted = [];
|
|
8613
|
+
sorted = realBTNExemplars.sort((a, b) => {
|
|
8614
|
+
let aR, bR;
|
|
8615
|
+
if (this.isEntry(a))
|
|
8616
|
+
aR = a[0];
|
|
8617
|
+
else if (this.isRealNode(a))
|
|
8618
|
+
aR = a.key;
|
|
8619
|
+
else
|
|
8620
|
+
aR = a;
|
|
8621
|
+
if (this.isEntry(b))
|
|
8622
|
+
bR = b[0];
|
|
8623
|
+
else if (this.isRealNode(b))
|
|
8624
|
+
bR = b.key;
|
|
8625
|
+
else
|
|
8626
|
+
bR = b;
|
|
8627
|
+
return aR - bR;
|
|
8628
|
+
});
|
|
8629
|
+
const _dfs = (arr) => {
|
|
8615
8630
|
if (arr.length === 0)
|
|
8616
8631
|
return;
|
|
8617
8632
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
8618
|
-
const newNode = this.add(arr[mid]
|
|
8633
|
+
const newNode = this.add(arr[mid]);
|
|
8619
8634
|
inserted.push(newNode);
|
|
8620
|
-
_dfs(arr.slice(0, mid)
|
|
8621
|
-
_dfs(arr.slice(mid + 1)
|
|
8635
|
+
_dfs(arr.slice(0, mid));
|
|
8636
|
+
_dfs(arr.slice(mid + 1));
|
|
8622
8637
|
};
|
|
8623
8638
|
const _iterate = () => {
|
|
8624
8639
|
const n = sorted.length;
|
|
@@ -8629,7 +8644,7 @@ var dataStructureTyped = (() => {
|
|
|
8629
8644
|
const [l, r] = popped;
|
|
8630
8645
|
if (l <= r) {
|
|
8631
8646
|
const m = l + Math.floor((r - l) / 2);
|
|
8632
|
-
const newNode = this.add(
|
|
8647
|
+
const newNode = this.add(sorted[m]);
|
|
8633
8648
|
inserted.push(newNode);
|
|
8634
8649
|
stack.push([m + 1, r]);
|
|
8635
8650
|
stack.push([l, m - 1]);
|
|
@@ -8638,15 +8653,15 @@ var dataStructureTyped = (() => {
|
|
|
8638
8653
|
}
|
|
8639
8654
|
};
|
|
8640
8655
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
8641
|
-
_dfs(
|
|
8656
|
+
_dfs(sorted);
|
|
8642
8657
|
} else {
|
|
8643
8658
|
_iterate();
|
|
8644
8659
|
}
|
|
8645
8660
|
return inserted;
|
|
8646
8661
|
}
|
|
8647
8662
|
/**
|
|
8648
|
-
* Time Complexity: O(log n) -
|
|
8649
|
-
* Space Complexity: O(
|
|
8663
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
8664
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
8650
8665
|
*/
|
|
8651
8666
|
/**
|
|
8652
8667
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -8663,7 +8678,7 @@ var dataStructureTyped = (() => {
|
|
|
8663
8678
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
8664
8679
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
8665
8680
|
*/
|
|
8666
|
-
lastKey(beginRoot = this.root, iterationType = this.
|
|
8681
|
+
lastKey(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8667
8682
|
var _a, _b, _c, _d, _e, _f;
|
|
8668
8683
|
if (this._compare(0, 1) === "lt" /* lt */)
|
|
8669
8684
|
return (_b = (_a = this.getRightMost(beginRoot, iterationType)) == null ? void 0 : _a.key) != null ? _b : 0;
|
|
@@ -8674,7 +8689,7 @@ var dataStructureTyped = (() => {
|
|
|
8674
8689
|
}
|
|
8675
8690
|
/**
|
|
8676
8691
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
8677
|
-
* Space Complexity: O(
|
|
8692
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
8678
8693
|
*/
|
|
8679
8694
|
/**
|
|
8680
8695
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -8721,7 +8736,11 @@ var dataStructureTyped = (() => {
|
|
|
8721
8736
|
}
|
|
8722
8737
|
}
|
|
8723
8738
|
/**
|
|
8724
|
-
*
|
|
8739
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
8740
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
8741
|
+
*/
|
|
8742
|
+
/**
|
|
8743
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
8725
8744
|
* otherwise it returns the key itself.
|
|
8726
8745
|
* @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
|
|
8727
8746
|
* `undefined`.
|
|
@@ -8729,13 +8748,9 @@ var dataStructureTyped = (() => {
|
|
|
8729
8748
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
8730
8749
|
* @returns either a node object (N) or undefined.
|
|
8731
8750
|
*/
|
|
8732
|
-
|
|
8751
|
+
ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
8733
8752
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
8734
8753
|
}
|
|
8735
|
-
/**
|
|
8736
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
8737
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
8738
|
-
*/
|
|
8739
8754
|
/**
|
|
8740
8755
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
8741
8756
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -8759,8 +8774,8 @@ var dataStructureTyped = (() => {
|
|
|
8759
8774
|
* performed on the binary tree. It can have two possible values:
|
|
8760
8775
|
* @returns The method returns an array of nodes (`N[]`).
|
|
8761
8776
|
*/
|
|
8762
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.
|
|
8763
|
-
beginRoot = this.
|
|
8777
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8778
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8764
8779
|
if (!beginRoot)
|
|
8765
8780
|
return [];
|
|
8766
8781
|
const ans = [];
|
|
@@ -8835,8 +8850,8 @@ var dataStructureTyped = (() => {
|
|
|
8835
8850
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
8836
8851
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
8837
8852
|
*/
|
|
8838
|
-
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = "lt" /* lt */, targetNode = this.root, iterationType = this.
|
|
8839
|
-
targetNode = this.
|
|
8853
|
+
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = "lt" /* lt */, targetNode = this.root, iterationType = this.iterationType) {
|
|
8854
|
+
targetNode = this.ensureNode(targetNode);
|
|
8840
8855
|
const ans = [];
|
|
8841
8856
|
if (!targetNode)
|
|
8842
8857
|
return ans;
|
|
@@ -8875,17 +8890,8 @@ var dataStructureTyped = (() => {
|
|
|
8875
8890
|
}
|
|
8876
8891
|
}
|
|
8877
8892
|
/**
|
|
8878
|
-
*
|
|
8879
|
-
*
|
|
8880
|
-
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
8881
|
-
*
|
|
8882
|
-
* Use Cases and Efficiency:
|
|
8883
|
-
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
8884
|
-
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
8885
|
-
*/
|
|
8886
|
-
/**
|
|
8887
|
-
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
8888
|
-
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
8893
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
8894
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
8889
8895
|
*/
|
|
8890
8896
|
/**
|
|
8891
8897
|
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
@@ -8898,7 +8904,7 @@ var dataStructureTyped = (() => {
|
|
|
8898
8904
|
* values:
|
|
8899
8905
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
8900
8906
|
*/
|
|
8901
|
-
perfectlyBalance(iterationType = this.
|
|
8907
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
8902
8908
|
const sorted = this.dfs((node) => node, "in"), n = sorted.length;
|
|
8903
8909
|
this.clear();
|
|
8904
8910
|
if (sorted.length < 1)
|
|
@@ -8909,7 +8915,7 @@ var dataStructureTyped = (() => {
|
|
|
8909
8915
|
return;
|
|
8910
8916
|
const m = l + Math.floor((r - l) / 2);
|
|
8911
8917
|
const midNode = sorted[m];
|
|
8912
|
-
this.add(midNode.key, midNode.value);
|
|
8918
|
+
this.add([midNode.key, midNode.value]);
|
|
8913
8919
|
buildBalanceBST(l, m - 1);
|
|
8914
8920
|
buildBalanceBST(m + 1, r);
|
|
8915
8921
|
};
|
|
@@ -8925,7 +8931,7 @@ var dataStructureTyped = (() => {
|
|
|
8925
8931
|
const m = l + Math.floor((r - l) / 2);
|
|
8926
8932
|
const midNode = sorted[m];
|
|
8927
8933
|
debugger;
|
|
8928
|
-
this.add(midNode.key, midNode.value);
|
|
8934
|
+
this.add([midNode.key, midNode.value]);
|
|
8929
8935
|
stack.push([m + 1, r]);
|
|
8930
8936
|
stack.push([l, m - 1]);
|
|
8931
8937
|
}
|
|
@@ -8935,8 +8941,17 @@ var dataStructureTyped = (() => {
|
|
|
8935
8941
|
}
|
|
8936
8942
|
}
|
|
8937
8943
|
/**
|
|
8938
|
-
*
|
|
8939
|
-
*
|
|
8944
|
+
* Balancing Adjustment:
|
|
8945
|
+
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
8946
|
+
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
8947
|
+
*
|
|
8948
|
+
* Use Cases and Efficiency:
|
|
8949
|
+
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
8950
|
+
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
8951
|
+
*/
|
|
8952
|
+
/**
|
|
8953
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
8954
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
8940
8955
|
*/
|
|
8941
8956
|
/**
|
|
8942
8957
|
* Time Complexity: O(n) - Visiting each node once.
|
|
@@ -8947,7 +8962,7 @@ var dataStructureTyped = (() => {
|
|
|
8947
8962
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
8948
8963
|
* @returns a boolean value.
|
|
8949
8964
|
*/
|
|
8950
|
-
isAVLBalanced(iterationType = this.
|
|
8965
|
+
isAVLBalanced(iterationType = this.iterationType) {
|
|
8951
8966
|
var _a, _b;
|
|
8952
8967
|
if (!this.root)
|
|
8953
8968
|
return true;
|
|
@@ -9005,7 +9020,7 @@ var dataStructureTyped = (() => {
|
|
|
9005
9020
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
9006
9021
|
*/
|
|
9007
9022
|
_compare(a, b) {
|
|
9008
|
-
const compared = this.
|
|
9023
|
+
const compared = this.comparator(a, b);
|
|
9009
9024
|
if (compared > 0)
|
|
9010
9025
|
return "gt" /* gt */;
|
|
9011
9026
|
else if (compared < 0)
|
|
@@ -9451,19 +9466,18 @@ var dataStructureTyped = (() => {
|
|
|
9451
9466
|
};
|
|
9452
9467
|
var AVLTree = class _AVLTree extends BST {
|
|
9453
9468
|
/**
|
|
9454
|
-
*
|
|
9455
|
-
* @param
|
|
9456
|
-
*
|
|
9457
|
-
*
|
|
9469
|
+
* The constructor function initializes an AVLTree object with optional elements and options.
|
|
9470
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
9471
|
+
* objects. It represents a collection of elements that will be added to the AVL tree during
|
|
9472
|
+
* initialization.
|
|
9473
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
9474
|
+
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
9475
|
+
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
9458
9476
|
*/
|
|
9459
|
-
constructor(options) {
|
|
9460
|
-
super(options);
|
|
9461
|
-
|
|
9462
|
-
|
|
9463
|
-
this.options = __spreadValues({ iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b }, options);
|
|
9464
|
-
} else {
|
|
9465
|
-
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b };
|
|
9466
|
-
}
|
|
9477
|
+
constructor(elements, options) {
|
|
9478
|
+
super([], options);
|
|
9479
|
+
if (elements)
|
|
9480
|
+
super.addMany(elements);
|
|
9467
9481
|
}
|
|
9468
9482
|
/**
|
|
9469
9483
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -9477,8 +9491,18 @@ var dataStructureTyped = (() => {
|
|
|
9477
9491
|
createNode(key, value) {
|
|
9478
9492
|
return new AVLTreeNode(key, value);
|
|
9479
9493
|
}
|
|
9494
|
+
/**
|
|
9495
|
+
* The function creates a new AVL tree with the specified options and returns it.
|
|
9496
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
9497
|
+
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
9498
|
+
* being created.
|
|
9499
|
+
* @returns a new AVLTree object.
|
|
9500
|
+
*/
|
|
9480
9501
|
createTree(options) {
|
|
9481
|
-
return new _AVLTree(__spreadValues(
|
|
9502
|
+
return new _AVLTree([], __spreadValues({
|
|
9503
|
+
iterationType: this.iterationType,
|
|
9504
|
+
comparator: this.comparator
|
|
9505
|
+
}, options));
|
|
9482
9506
|
}
|
|
9483
9507
|
/**
|
|
9484
9508
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
@@ -9488,24 +9512,22 @@ var dataStructureTyped = (() => {
|
|
|
9488
9512
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
9489
9513
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9490
9514
|
*
|
|
9491
|
-
* The function overrides the add method of a
|
|
9492
|
-
*
|
|
9493
|
-
* @param
|
|
9494
|
-
*
|
|
9495
|
-
* @
|
|
9496
|
-
* added to the binary search tree.
|
|
9497
|
-
* @returns The method is returning either a node (N) or undefined.
|
|
9515
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
9516
|
+
* a new node.
|
|
9517
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
|
|
9518
|
+
* entry.
|
|
9519
|
+
* @returns The method is returning either the inserted node or `undefined`.
|
|
9498
9520
|
*/
|
|
9499
|
-
add(
|
|
9500
|
-
if (
|
|
9521
|
+
add(keyOrNodeOrEntry) {
|
|
9522
|
+
if (keyOrNodeOrEntry === null)
|
|
9501
9523
|
return void 0;
|
|
9502
|
-
const inserted = super.add(
|
|
9524
|
+
const inserted = super.add(keyOrNodeOrEntry);
|
|
9503
9525
|
if (inserted)
|
|
9504
9526
|
this._balancePath(inserted);
|
|
9505
9527
|
return inserted;
|
|
9506
9528
|
}
|
|
9507
9529
|
/**
|
|
9508
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The
|
|
9530
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
9509
9531
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9510
9532
|
*/
|
|
9511
9533
|
/**
|
|
@@ -9535,7 +9557,7 @@ var dataStructureTyped = (() => {
|
|
|
9535
9557
|
return deletedResults;
|
|
9536
9558
|
}
|
|
9537
9559
|
/**
|
|
9538
|
-
* The `
|
|
9560
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
9539
9561
|
* tree.
|
|
9540
9562
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
9541
9563
|
* needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
@@ -9544,9 +9566,9 @@ var dataStructureTyped = (() => {
|
|
|
9544
9566
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
9545
9567
|
* if either `srcNode` or `destNode` is undefined.
|
|
9546
9568
|
*/
|
|
9547
|
-
|
|
9548
|
-
srcNode = this.
|
|
9549
|
-
destNode = this.
|
|
9569
|
+
_swapProperties(srcNode, destNode) {
|
|
9570
|
+
srcNode = this.ensureNode(srcNode);
|
|
9571
|
+
destNode = this.ensureNode(destNode);
|
|
9550
9572
|
if (srcNode && destNode) {
|
|
9551
9573
|
const { key, value, height } = destNode;
|
|
9552
9574
|
const tempNode = this.createNode(key, value);
|
|
@@ -9833,6 +9855,10 @@ var dataStructureTyped = (() => {
|
|
|
9833
9855
|
B && this._updateHeight(B);
|
|
9834
9856
|
C && this._updateHeight(C);
|
|
9835
9857
|
}
|
|
9858
|
+
_replaceNode(oldNode, newNode) {
|
|
9859
|
+
newNode.height = oldNode.height;
|
|
9860
|
+
return super._replaceNode(oldNode, newNode);
|
|
9861
|
+
}
|
|
9836
9862
|
};
|
|
9837
9863
|
|
|
9838
9864
|
// src/data-structures/binary-tree/rb-tree.ts
|
|
@@ -9845,22 +9871,24 @@ var dataStructureTyped = (() => {
|
|
|
9845
9871
|
};
|
|
9846
9872
|
var RedBlackTree = class _RedBlackTree extends BST {
|
|
9847
9873
|
/**
|
|
9848
|
-
*
|
|
9849
|
-
*
|
|
9850
|
-
*
|
|
9851
|
-
|
|
9852
|
-
|
|
9853
|
-
|
|
9874
|
+
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
9875
|
+
* initializes the tree with optional elements and options.
|
|
9876
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
9877
|
+
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
9878
|
+
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
9879
|
+
* elements to the
|
|
9880
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
9881
|
+
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
9882
|
+
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
9883
|
+
*/
|
|
9884
|
+
constructor(elements, options) {
|
|
9885
|
+
super([], options);
|
|
9854
9886
|
__publicField(this, "Sentinel", new RedBlackTreeNode(NaN));
|
|
9855
|
-
__publicField(this, "options");
|
|
9856
9887
|
__publicField(this, "_root");
|
|
9857
9888
|
__publicField(this, "_size", 0);
|
|
9858
|
-
if (options) {
|
|
9859
|
-
this.options = __spreadValues({ iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b }, options);
|
|
9860
|
-
} else {
|
|
9861
|
-
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b };
|
|
9862
|
-
}
|
|
9863
9889
|
this._root = this.Sentinel;
|
|
9890
|
+
if (elements)
|
|
9891
|
+
super.addMany(elements);
|
|
9864
9892
|
}
|
|
9865
9893
|
get root() {
|
|
9866
9894
|
return this._root;
|
|
@@ -9868,37 +9896,59 @@ var dataStructureTyped = (() => {
|
|
|
9868
9896
|
get size() {
|
|
9869
9897
|
return this._size;
|
|
9870
9898
|
}
|
|
9899
|
+
/**
|
|
9900
|
+
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
9901
|
+
* @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
|
|
9902
|
+
* identify and compare nodes in the Red-Black Tree.
|
|
9903
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
9904
|
+
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
9905
|
+
* specific type when using the `createNode` method.
|
|
9906
|
+
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
9907
|
+
* Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
|
|
9908
|
+
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
9909
|
+
* value, and color.
|
|
9910
|
+
*/
|
|
9871
9911
|
createNode(key, value, color = 0 /* BLACK */) {
|
|
9872
9912
|
return new RedBlackTreeNode(key, value, color);
|
|
9873
9913
|
}
|
|
9914
|
+
/**
|
|
9915
|
+
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
9916
|
+
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
9917
|
+
* passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
|
|
9918
|
+
* class.
|
|
9919
|
+
* @returns a new instance of a RedBlackTree object.
|
|
9920
|
+
*/
|
|
9874
9921
|
createTree(options) {
|
|
9875
|
-
return new _RedBlackTree(__spreadValues(
|
|
9922
|
+
return new _RedBlackTree([], __spreadValues({
|
|
9923
|
+
iterationType: this.iterationType,
|
|
9924
|
+
comparator: this.comparator
|
|
9925
|
+
}, options));
|
|
9876
9926
|
}
|
|
9877
9927
|
/**
|
|
9878
9928
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
9879
9929
|
* Space Complexity: O(1)
|
|
9880
9930
|
*/
|
|
9881
9931
|
/**
|
|
9882
|
-
*
|
|
9883
|
-
*
|
|
9884
|
-
*
|
|
9885
|
-
*
|
|
9886
|
-
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
9887
|
-
* following types:
|
|
9888
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
9889
|
-
* key in the node being added to the Red-Black Tree.
|
|
9890
|
-
* @returns The method returns either a node (`N`) or `undefined`.
|
|
9932
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
9933
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
9934
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
9935
|
+
* `undefined`.
|
|
9891
9936
|
*/
|
|
9892
|
-
add(
|
|
9937
|
+
add(keyOrNodeOrEntry) {
|
|
9893
9938
|
let node;
|
|
9894
|
-
if (this.isNodeKey(
|
|
9895
|
-
node = this.createNode(
|
|
9896
|
-
} else if (
|
|
9897
|
-
node =
|
|
9898
|
-
} else if (
|
|
9899
|
-
return;
|
|
9900
|
-
} else if (keyOrNode === void 0) {
|
|
9939
|
+
if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
9940
|
+
node = this.createNode(keyOrNodeOrEntry, void 0, 1 /* RED */);
|
|
9941
|
+
} else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
9942
|
+
node = keyOrNodeOrEntry;
|
|
9943
|
+
} else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
9901
9944
|
return;
|
|
9945
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9946
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
9947
|
+
if (key === void 0 || key === null) {
|
|
9948
|
+
return;
|
|
9949
|
+
} else {
|
|
9950
|
+
node = this.createNode(key, value, 1 /* RED */);
|
|
9951
|
+
}
|
|
9902
9952
|
} else {
|
|
9903
9953
|
return;
|
|
9904
9954
|
}
|
|
@@ -9914,6 +9964,9 @@ var dataStructureTyped = (() => {
|
|
|
9914
9964
|
} else if (node.key > x.key) {
|
|
9915
9965
|
x = x == null ? void 0 : x.right;
|
|
9916
9966
|
} else {
|
|
9967
|
+
if (node !== x) {
|
|
9968
|
+
this._replaceNode(x, node);
|
|
9969
|
+
}
|
|
9917
9970
|
return;
|
|
9918
9971
|
}
|
|
9919
9972
|
}
|
|
@@ -10010,6 +10063,10 @@ var dataStructureTyped = (() => {
|
|
|
10010
10063
|
helper(this.root);
|
|
10011
10064
|
return ans;
|
|
10012
10065
|
}
|
|
10066
|
+
/**
|
|
10067
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10068
|
+
* Space Complexity: O(1)
|
|
10069
|
+
*/
|
|
10013
10070
|
isRealNode(node) {
|
|
10014
10071
|
return node !== this.Sentinel && node !== void 0;
|
|
10015
10072
|
}
|
|
@@ -10038,11 +10095,11 @@ var dataStructureTyped = (() => {
|
|
|
10038
10095
|
* `getNodes` method, which is called within the `getNode` method.
|
|
10039
10096
|
* @returns a value of type `N`, `null`, or `undefined`.
|
|
10040
10097
|
*/
|
|
10041
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
10098
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
10042
10099
|
var _a;
|
|
10043
10100
|
if (identifier instanceof BinaryTreeNode)
|
|
10044
10101
|
callback = (node) => node;
|
|
10045
|
-
beginRoot = this.
|
|
10102
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
10046
10103
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
|
|
10047
10104
|
}
|
|
10048
10105
|
/**
|
|
@@ -10093,6 +10150,10 @@ var dataStructureTyped = (() => {
|
|
|
10093
10150
|
}
|
|
10094
10151
|
return y;
|
|
10095
10152
|
}
|
|
10153
|
+
/**
|
|
10154
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10155
|
+
* Space Complexity: O(1)
|
|
10156
|
+
*/
|
|
10096
10157
|
clear() {
|
|
10097
10158
|
this._root = this.Sentinel;
|
|
10098
10159
|
this._size = 0;
|
|
@@ -10314,6 +10375,19 @@ var dataStructureTyped = (() => {
|
|
|
10314
10375
|
}
|
|
10315
10376
|
this.root.color = 0 /* BLACK */;
|
|
10316
10377
|
}
|
|
10378
|
+
/**
|
|
10379
|
+
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
10380
|
+
* @param {N} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
10381
|
+
* data structure. It is of type `N`, which is the type of the nodes in the data structure.
|
|
10382
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
10383
|
+
* data structure.
|
|
10384
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
10385
|
+
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
10386
|
+
*/
|
|
10387
|
+
_replaceNode(oldNode, newNode) {
|
|
10388
|
+
newNode.color = oldNode.color;
|
|
10389
|
+
return super._replaceNode(oldNode, newNode);
|
|
10390
|
+
}
|
|
10317
10391
|
};
|
|
10318
10392
|
|
|
10319
10393
|
// src/data-structures/binary-tree/tree-multimap.ts
|
|
@@ -10335,24 +10409,17 @@ var dataStructureTyped = (() => {
|
|
|
10335
10409
|
}
|
|
10336
10410
|
};
|
|
10337
10411
|
var TreeMultimap = class _TreeMultimap extends AVLTree {
|
|
10338
|
-
|
|
10339
|
-
|
|
10340
|
-
* merge duplicated values.
|
|
10341
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
10342
|
-
* TreeMultimap.
|
|
10343
|
-
*/
|
|
10344
|
-
constructor(options = { iterationType: "ITERATIVE" /* ITERATIVE */ }) {
|
|
10345
|
-
super(options);
|
|
10346
|
-
__publicField(this, "options");
|
|
10412
|
+
constructor(elements, options) {
|
|
10413
|
+
super([], options);
|
|
10347
10414
|
__publicField(this, "_count", 0);
|
|
10348
|
-
if (
|
|
10349
|
-
this.
|
|
10350
|
-
} else {
|
|
10351
|
-
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b };
|
|
10352
|
-
}
|
|
10415
|
+
if (elements)
|
|
10416
|
+
this.addMany(elements);
|
|
10353
10417
|
}
|
|
10418
|
+
// TODO the _count is not accurate after nodes count modified
|
|
10354
10419
|
get count() {
|
|
10355
|
-
|
|
10420
|
+
let sum = 0;
|
|
10421
|
+
this.subTreeTraverse((node) => sum += node.count);
|
|
10422
|
+
return sum;
|
|
10356
10423
|
}
|
|
10357
10424
|
/**
|
|
10358
10425
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
@@ -10367,7 +10434,10 @@ var dataStructureTyped = (() => {
|
|
|
10367
10434
|
return new TreeMultimapNode(key, value, count);
|
|
10368
10435
|
}
|
|
10369
10436
|
createTree(options) {
|
|
10370
|
-
return new _TreeMultimap(__spreadValues(
|
|
10437
|
+
return new _TreeMultimap([], __spreadValues({
|
|
10438
|
+
iterationType: this.iterationType,
|
|
10439
|
+
comparator: this.comparator
|
|
10440
|
+
}, options));
|
|
10371
10441
|
}
|
|
10372
10442
|
/**
|
|
10373
10443
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
@@ -10377,115 +10447,59 @@ var dataStructureTyped = (() => {
|
|
|
10377
10447
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10378
10448
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10379
10449
|
*
|
|
10380
|
-
* The `add` function
|
|
10381
|
-
*
|
|
10382
|
-
* @param
|
|
10383
|
-
* following types:
|
|
10384
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
10385
|
-
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
10450
|
+
* The `add` function overrides the base class `add` function to add a new node to the tree multimap
|
|
10451
|
+
* and update the count.
|
|
10452
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
10386
10453
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
10387
|
-
* times the key
|
|
10388
|
-
*
|
|
10454
|
+
* times the key or node or entry should be added to the multimap. If not provided, the default value
|
|
10455
|
+
* is 1.
|
|
10456
|
+
* @returns either a node (`N`) or `undefined`.
|
|
10389
10457
|
*/
|
|
10390
|
-
add(
|
|
10391
|
-
|
|
10392
|
-
|
|
10393
|
-
|
|
10394
|
-
if (
|
|
10395
|
-
newNode =
|
|
10396
|
-
} else if (
|
|
10397
|
-
newNode = void 0;
|
|
10458
|
+
add(keyOrNodeOrEntry, count = 1) {
|
|
10459
|
+
let newNode;
|
|
10460
|
+
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
10461
|
+
return;
|
|
10462
|
+
} else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
10463
|
+
newNode = keyOrNodeOrEntry;
|
|
10464
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
10465
|
+
newNode = this.createNode(keyOrNodeOrEntry, void 0, count);
|
|
10466
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
10467
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
10468
|
+
if (key === void 0 || key === null) {
|
|
10469
|
+
return;
|
|
10470
|
+
} else {
|
|
10471
|
+
newNode = this.createNode(key, value, count);
|
|
10472
|
+
}
|
|
10398
10473
|
} else {
|
|
10399
|
-
|
|
10474
|
+
return;
|
|
10400
10475
|
}
|
|
10401
|
-
|
|
10402
|
-
|
|
10403
|
-
|
|
10404
|
-
|
|
10405
|
-
this._count += newNode.count;
|
|
10406
|
-
inserted = this.root;
|
|
10407
|
-
} else {
|
|
10408
|
-
let cur = this.root;
|
|
10409
|
-
let traversing = true;
|
|
10410
|
-
while (traversing) {
|
|
10411
|
-
if (cur) {
|
|
10412
|
-
if (newNode) {
|
|
10413
|
-
if (this._compare(cur.key, newNode.key) === "eq" /* eq */) {
|
|
10414
|
-
cur.value = newNode.value;
|
|
10415
|
-
cur.count += newNode.count;
|
|
10416
|
-
this._count += newNode.count;
|
|
10417
|
-
traversing = false;
|
|
10418
|
-
inserted = cur;
|
|
10419
|
-
} else if (this._compare(cur.key, newNode.key) === "gt" /* gt */) {
|
|
10420
|
-
if (cur.left === void 0) {
|
|
10421
|
-
cur.left = newNode;
|
|
10422
|
-
this._size = this.size + 1;
|
|
10423
|
-
this._count += newNode.count;
|
|
10424
|
-
traversing = false;
|
|
10425
|
-
inserted = cur.left;
|
|
10426
|
-
} else {
|
|
10427
|
-
if (cur.left)
|
|
10428
|
-
cur = cur.left;
|
|
10429
|
-
}
|
|
10430
|
-
} else if (this._compare(cur.key, newNode.key) === "lt" /* lt */) {
|
|
10431
|
-
if (cur.right === void 0) {
|
|
10432
|
-
cur.right = newNode;
|
|
10433
|
-
this._size = this.size + 1;
|
|
10434
|
-
this._count += newNode.count;
|
|
10435
|
-
traversing = false;
|
|
10436
|
-
inserted = cur.right;
|
|
10437
|
-
} else {
|
|
10438
|
-
if (cur.right)
|
|
10439
|
-
cur = cur.right;
|
|
10440
|
-
}
|
|
10441
|
-
}
|
|
10442
|
-
} else {
|
|
10443
|
-
}
|
|
10444
|
-
} else {
|
|
10445
|
-
traversing = false;
|
|
10446
|
-
}
|
|
10447
|
-
}
|
|
10476
|
+
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
10477
|
+
const inserted = super.add(newNode);
|
|
10478
|
+
if (inserted) {
|
|
10479
|
+
this._count += orgNodeCount;
|
|
10448
10480
|
}
|
|
10449
|
-
if (inserted)
|
|
10450
|
-
this._balancePath(inserted);
|
|
10451
10481
|
return inserted;
|
|
10452
10482
|
}
|
|
10453
10483
|
/**
|
|
10454
|
-
* Time Complexity: O(
|
|
10455
|
-
* Space Complexity: O(1) - constant space, as it
|
|
10484
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10485
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10456
10486
|
*/
|
|
10457
10487
|
/**
|
|
10458
|
-
* Time Complexity: O(k log n) - logarithmic time
|
|
10488
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10459
10489
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10460
10490
|
*
|
|
10461
|
-
* The function
|
|
10462
|
-
*
|
|
10463
|
-
* @param
|
|
10464
|
-
*
|
|
10465
|
-
* @
|
|
10466
|
-
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
10467
|
-
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
10468
|
-
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
10491
|
+
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
10492
|
+
* structure.
|
|
10493
|
+
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
10494
|
+
* either keys, nodes, or entries.
|
|
10495
|
+
* @returns The method is returning an array of type `N | undefined`.
|
|
10469
10496
|
*/
|
|
10470
|
-
addMany(
|
|
10471
|
-
|
|
10472
|
-
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
10473
|
-
const keyOrNode = keysOrNodes[i];
|
|
10474
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
10475
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
10476
|
-
continue;
|
|
10477
|
-
}
|
|
10478
|
-
if (keyOrNode === void 0) {
|
|
10479
|
-
inserted.push(this.add(NaN, void 0, 0));
|
|
10480
|
-
continue;
|
|
10481
|
-
}
|
|
10482
|
-
inserted.push(this.add(keyOrNode, data == null ? void 0 : data[i], 1));
|
|
10483
|
-
}
|
|
10484
|
-
return inserted;
|
|
10497
|
+
addMany(keysOrNodesOrEntries) {
|
|
10498
|
+
return super.addMany(keysOrNodesOrEntries);
|
|
10485
10499
|
}
|
|
10486
10500
|
/**
|
|
10487
|
-
* Time Complexity: O(
|
|
10488
|
-
* Space Complexity: O(1) - constant space, as it
|
|
10501
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
10502
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
10489
10503
|
*/
|
|
10490
10504
|
/**
|
|
10491
10505
|
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
@@ -10498,7 +10512,7 @@ var dataStructureTyped = (() => {
|
|
|
10498
10512
|
* values:
|
|
10499
10513
|
* @returns a boolean value.
|
|
10500
10514
|
*/
|
|
10501
|
-
perfectlyBalance(iterationType = this.
|
|
10515
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
10502
10516
|
const sorted = this.dfs((node) => node, "in"), n = sorted.length;
|
|
10503
10517
|
if (sorted.length < 1)
|
|
10504
10518
|
return false;
|
|
@@ -10509,7 +10523,7 @@ var dataStructureTyped = (() => {
|
|
|
10509
10523
|
return;
|
|
10510
10524
|
const m = l + Math.floor((r - l) / 2);
|
|
10511
10525
|
const midNode = sorted[m];
|
|
10512
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
10526
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
10513
10527
|
buildBalanceBST(l, m - 1);
|
|
10514
10528
|
buildBalanceBST(m + 1, r);
|
|
10515
10529
|
};
|
|
@@ -10524,7 +10538,7 @@ var dataStructureTyped = (() => {
|
|
|
10524
10538
|
if (l <= r) {
|
|
10525
10539
|
const m = l + Math.floor((r - l) / 2);
|
|
10526
10540
|
const midNode = sorted[m];
|
|
10527
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
10541
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
10528
10542
|
stack.push([m + 1, r]);
|
|
10529
10543
|
stack.push([l, m - 1]);
|
|
10530
10544
|
}
|
|
@@ -10534,8 +10548,8 @@ var dataStructureTyped = (() => {
|
|
|
10534
10548
|
}
|
|
10535
10549
|
}
|
|
10536
10550
|
/**
|
|
10537
|
-
* Time Complexity: O(
|
|
10538
|
-
* Space Complexity: O(
|
|
10551
|
+
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
10552
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10539
10553
|
*/
|
|
10540
10554
|
/**
|
|
10541
10555
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
@@ -10587,7 +10601,7 @@ var dataStructureTyped = (() => {
|
|
|
10587
10601
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : void 0;
|
|
10588
10602
|
if (leftSubTreeRightMost) {
|
|
10589
10603
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
10590
|
-
orgCurrent = this.
|
|
10604
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
10591
10605
|
if (parentOfLeftSubTreeMax) {
|
|
10592
10606
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
10593
10607
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -10609,8 +10623,8 @@ var dataStructureTyped = (() => {
|
|
|
10609
10623
|
return deletedResult;
|
|
10610
10624
|
}
|
|
10611
10625
|
/**
|
|
10612
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree.
|
|
10613
|
-
* Space Complexity: O(
|
|
10626
|
+
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
10627
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
10614
10628
|
*/
|
|
10615
10629
|
/**
|
|
10616
10630
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
@@ -10635,7 +10649,7 @@ var dataStructureTyped = (() => {
|
|
|
10635
10649
|
* added, or `undefined` if no node was added.
|
|
10636
10650
|
*/
|
|
10637
10651
|
_addTo(newNode, parent) {
|
|
10638
|
-
parent = this.
|
|
10652
|
+
parent = this.ensureNode(parent);
|
|
10639
10653
|
if (parent) {
|
|
10640
10654
|
if (parent.left === void 0) {
|
|
10641
10655
|
parent.left = newNode;
|
|
@@ -10659,7 +10673,7 @@ var dataStructureTyped = (() => {
|
|
|
10659
10673
|
}
|
|
10660
10674
|
}
|
|
10661
10675
|
/**
|
|
10662
|
-
* The `
|
|
10676
|
+
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
10663
10677
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
10664
10678
|
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
10665
10679
|
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
@@ -10667,9 +10681,9 @@ var dataStructureTyped = (() => {
|
|
|
10667
10681
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
10668
10682
|
* if either `srcNode` or `destNode` is undefined.
|
|
10669
10683
|
*/
|
|
10670
|
-
|
|
10671
|
-
srcNode = this.
|
|
10672
|
-
destNode = this.
|
|
10684
|
+
_swapProperties(srcNode, destNode) {
|
|
10685
|
+
srcNode = this.ensureNode(srcNode);
|
|
10686
|
+
destNode = this.ensureNode(destNode);
|
|
10673
10687
|
if (srcNode && destNode) {
|
|
10674
10688
|
const { key, value, count, height } = destNode;
|
|
10675
10689
|
const tempNode = this.createNode(key, value, count);
|
|
@@ -10688,6 +10702,10 @@ var dataStructureTyped = (() => {
|
|
|
10688
10702
|
}
|
|
10689
10703
|
return void 0;
|
|
10690
10704
|
}
|
|
10705
|
+
_replaceNode(oldNode, newNode) {
|
|
10706
|
+
newNode.count = oldNode.count + newNode.count;
|
|
10707
|
+
return super._replaceNode(oldNode, newNode);
|
|
10708
|
+
}
|
|
10691
10709
|
};
|
|
10692
10710
|
|
|
10693
10711
|
// src/data-structures/tree/tree.ts
|
|
@@ -11338,16 +11356,21 @@ var dataStructureTyped = (() => {
|
|
|
11338
11356
|
};
|
|
11339
11357
|
var Trie = class _Trie {
|
|
11340
11358
|
constructor(words, caseSensitive = true) {
|
|
11359
|
+
__publicField(this, "_size");
|
|
11341
11360
|
__publicField(this, "_caseSensitive");
|
|
11342
11361
|
__publicField(this, "_root");
|
|
11343
11362
|
this._root = new TrieNode("");
|
|
11344
11363
|
this._caseSensitive = caseSensitive;
|
|
11364
|
+
this._size = 0;
|
|
11345
11365
|
if (words) {
|
|
11346
|
-
for (const
|
|
11347
|
-
this.add(
|
|
11366
|
+
for (const word of words) {
|
|
11367
|
+
this.add(word);
|
|
11348
11368
|
}
|
|
11349
11369
|
}
|
|
11350
11370
|
}
|
|
11371
|
+
get size() {
|
|
11372
|
+
return this._size;
|
|
11373
|
+
}
|
|
11351
11374
|
get caseSensitive() {
|
|
11352
11375
|
return this._caseSensitive;
|
|
11353
11376
|
}
|
|
@@ -11369,6 +11392,7 @@ var dataStructureTyped = (() => {
|
|
|
11369
11392
|
add(word) {
|
|
11370
11393
|
word = this._caseProcess(word);
|
|
11371
11394
|
let cur = this.root;
|
|
11395
|
+
let isNewWord = false;
|
|
11372
11396
|
for (const c of word) {
|
|
11373
11397
|
let nodeC = cur.children.get(c);
|
|
11374
11398
|
if (!nodeC) {
|
|
@@ -11377,8 +11401,12 @@ var dataStructureTyped = (() => {
|
|
|
11377
11401
|
}
|
|
11378
11402
|
cur = nodeC;
|
|
11379
11403
|
}
|
|
11380
|
-
cur.isEnd
|
|
11381
|
-
|
|
11404
|
+
if (!cur.isEnd) {
|
|
11405
|
+
isNewWord = true;
|
|
11406
|
+
cur.isEnd = true;
|
|
11407
|
+
this._size++;
|
|
11408
|
+
}
|
|
11409
|
+
return isNewWord;
|
|
11382
11410
|
}
|
|
11383
11411
|
/**
|
|
11384
11412
|
* Time Complexity: O(M), where M is the length of the input word.
|
|
@@ -11444,6 +11472,9 @@ var dataStructureTyped = (() => {
|
|
|
11444
11472
|
return false;
|
|
11445
11473
|
};
|
|
11446
11474
|
dfs(this.root, 0);
|
|
11475
|
+
if (isDeleted) {
|
|
11476
|
+
this._size--;
|
|
11477
|
+
}
|
|
11447
11478
|
return isDeleted;
|
|
11448
11479
|
}
|
|
11449
11480
|
/**
|
|
@@ -11666,6 +11697,9 @@ var dataStructureTyped = (() => {
|
|
|
11666
11697
|
}
|
|
11667
11698
|
return accumulator;
|
|
11668
11699
|
}
|
|
11700
|
+
print() {
|
|
11701
|
+
console.log([...this]);
|
|
11702
|
+
}
|
|
11669
11703
|
/**
|
|
11670
11704
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
11671
11705
|
* Space Complexity: O(1) - Constant space.
|