data-structure-typed 1.47.4 → 1.47.6
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/CHANGELOG.md +1 -1
- package/benchmark/report.html +2 -2
- package/benchmark/report.json +12 -18
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
- 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 +3 -3
- package/dist/cjs/data-structures/hash/hash-map.js +10 -4
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/cjs/data-structures/hash/hash-table.js +50 -5
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +25 -22
- package/dist/cjs/data-structures/heap/heap.js +101 -41
- 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 +58 -57
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +125 -119
- 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 +38 -37
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +65 -60
- 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 +50 -49
- package/dist/cjs/data-structures/queue/deque.js +81 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +46 -0
- package/dist/cjs/data-structures/queue/queue.js +80 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +20 -6
- package/dist/cjs/data-structures/stack/stack.js +65 -8
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/cjs/data-structures/trie/trie.js +47 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/cjs/types/common.d.ts +2 -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/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 +8 -8
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.js +10 -4
- package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/mjs/data-structures/hash/hash-table.js +50 -5
- package/dist/mjs/data-structures/heap/heap.d.ts +25 -22
- package/dist/mjs/data-structures/heap/heap.js +102 -42
- 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 +58 -57
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +65 -60
- 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 +50 -49
- package/dist/mjs/data-structures/queue/deque.js +81 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +46 -0
- package/dist/mjs/data-structures/queue/queue.js +80 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +20 -6
- package/dist/mjs/data-structures/stack/stack.js +65 -8
- package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/mjs/data-structures/trie/trie.js +47 -0
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/mjs/types/common.d.ts +2 -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/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 +856 -509
- 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 +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +115 -46
- 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 +137 -128
- package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
- 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 +86 -75
- package/src/data-structures/queue/queue.ts +88 -0
- package/src/data-structures/stack/stack.ts +75 -10
- package/src/data-structures/trie/trie.ts +53 -0
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -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/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +14 -5
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
- 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 +49 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- 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/queue/queue.test.ts +37 -0
- package/test/unit/data-structures/stack/stack.test.ts +55 -5
- package/test/unit/data-structures/trie/trie.test.ts +33 -0
- package/test/unit/unrestricted-interconversion.test.ts +100 -0
|
@@ -182,7 +182,7 @@ var dataStructureTyped = (() => {
|
|
|
182
182
|
__publicField(this, "next");
|
|
183
183
|
this.key = key;
|
|
184
184
|
this.value = value;
|
|
185
|
-
this.next =
|
|
185
|
+
this.next = void 0;
|
|
186
186
|
}
|
|
187
187
|
};
|
|
188
188
|
var _HashTable = class _HashTable {
|
|
@@ -194,7 +194,7 @@ var dataStructureTyped = (() => {
|
|
|
194
194
|
this._hashFn = hashFn || this._defaultHashFn;
|
|
195
195
|
this._capacity = Math.max(capacity, _HashTable.DEFAULT_CAPACITY);
|
|
196
196
|
this._size = 0;
|
|
197
|
-
this._buckets = new Array(this._capacity).fill(
|
|
197
|
+
this._buckets = new Array(this._capacity).fill(void 0);
|
|
198
198
|
}
|
|
199
199
|
get capacity() {
|
|
200
200
|
return this._capacity;
|
|
@@ -269,7 +269,7 @@ var dataStructureTyped = (() => {
|
|
|
269
269
|
delete(key) {
|
|
270
270
|
const index = this._hash(key);
|
|
271
271
|
let currentNode = this._buckets[index];
|
|
272
|
-
let prevNode =
|
|
272
|
+
let prevNode = void 0;
|
|
273
273
|
while (currentNode) {
|
|
274
274
|
if (currentNode.key === key) {
|
|
275
275
|
if (prevNode) {
|
|
@@ -278,13 +278,58 @@ var dataStructureTyped = (() => {
|
|
|
278
278
|
this._buckets[index] = currentNode.next;
|
|
279
279
|
}
|
|
280
280
|
this._size--;
|
|
281
|
-
currentNode.next =
|
|
281
|
+
currentNode.next = void 0;
|
|
282
282
|
return;
|
|
283
283
|
}
|
|
284
284
|
prevNode = currentNode;
|
|
285
285
|
currentNode = currentNode.next;
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
|
+
*[Symbol.iterator]() {
|
|
289
|
+
for (const bucket of this._buckets) {
|
|
290
|
+
let currentNode = bucket;
|
|
291
|
+
while (currentNode) {
|
|
292
|
+
yield [currentNode.key, currentNode.value];
|
|
293
|
+
currentNode = currentNode.next;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
forEach(callback) {
|
|
298
|
+
let index = 0;
|
|
299
|
+
for (const entry of this) {
|
|
300
|
+
callback(entry, index, this);
|
|
301
|
+
index++;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
filter(predicate) {
|
|
305
|
+
const newTable = new _HashTable();
|
|
306
|
+
let index = 0;
|
|
307
|
+
for (const [key, value] of this) {
|
|
308
|
+
if (predicate([key, value], index, this)) {
|
|
309
|
+
newTable.set(key, value);
|
|
310
|
+
}
|
|
311
|
+
index++;
|
|
312
|
+
}
|
|
313
|
+
return newTable;
|
|
314
|
+
}
|
|
315
|
+
map(callback) {
|
|
316
|
+
const newTable = new _HashTable();
|
|
317
|
+
let index = 0;
|
|
318
|
+
for (const [key, value] of this) {
|
|
319
|
+
newTable.set(key, callback([key, value], index, this));
|
|
320
|
+
index++;
|
|
321
|
+
}
|
|
322
|
+
return newTable;
|
|
323
|
+
}
|
|
324
|
+
reduce(callback, initialValue) {
|
|
325
|
+
let accumulator = initialValue;
|
|
326
|
+
let index = 0;
|
|
327
|
+
for (const entry of this) {
|
|
328
|
+
accumulator = callback(accumulator, entry, index, this);
|
|
329
|
+
index++;
|
|
330
|
+
}
|
|
331
|
+
return accumulator;
|
|
332
|
+
}
|
|
288
333
|
/**
|
|
289
334
|
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
290
335
|
* capacity of the data structure.
|
|
@@ -370,7 +415,7 @@ var dataStructureTyped = (() => {
|
|
|
370
415
|
*/
|
|
371
416
|
_expand() {
|
|
372
417
|
const newCapacity = this._capacity * 2;
|
|
373
|
-
const newBuckets = new Array(newCapacity).fill(
|
|
418
|
+
const newBuckets = new Array(newCapacity).fill(void 0);
|
|
374
419
|
for (const bucket of this._buckets) {
|
|
375
420
|
let currentNode = bucket;
|
|
376
421
|
while (currentNode) {
|
|
@@ -739,10 +784,12 @@ var dataStructureTyped = (() => {
|
|
|
739
784
|
*/
|
|
740
785
|
filter(predicate) {
|
|
741
786
|
const filteredMap = new _HashMap();
|
|
787
|
+
let index = 0;
|
|
742
788
|
for (const [key, value] of this) {
|
|
743
|
-
if (predicate([key, value], this)) {
|
|
789
|
+
if (predicate([key, value], index, this)) {
|
|
744
790
|
filteredMap.set(key, value);
|
|
745
791
|
}
|
|
792
|
+
index++;
|
|
746
793
|
}
|
|
747
794
|
return filteredMap;
|
|
748
795
|
}
|
|
@@ -755,9 +802,11 @@ var dataStructureTyped = (() => {
|
|
|
755
802
|
*/
|
|
756
803
|
map(callback) {
|
|
757
804
|
const mappedMap = new _HashMap();
|
|
805
|
+
let index = 0;
|
|
758
806
|
for (const [key, value] of this) {
|
|
759
|
-
const newValue = callback([key, value], this);
|
|
807
|
+
const newValue = callback([key, value], index, this);
|
|
760
808
|
mappedMap.set(key, newValue);
|
|
809
|
+
index++;
|
|
761
810
|
}
|
|
762
811
|
return mappedMap;
|
|
763
812
|
}
|
|
@@ -775,8 +824,10 @@ var dataStructureTyped = (() => {
|
|
|
775
824
|
*/
|
|
776
825
|
reduce(callback, initialValue) {
|
|
777
826
|
let accumulator = initialValue;
|
|
778
|
-
|
|
779
|
-
|
|
827
|
+
let index = 0;
|
|
828
|
+
for (const entry of this) {
|
|
829
|
+
accumulator = callback(accumulator, entry, index, this);
|
|
830
|
+
index++;
|
|
780
831
|
}
|
|
781
832
|
return accumulator;
|
|
782
833
|
}
|
|
@@ -835,13 +886,17 @@ var dataStructureTyped = (() => {
|
|
|
835
886
|
/**
|
|
836
887
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
837
888
|
*/
|
|
838
|
-
constructor() {
|
|
889
|
+
constructor(elements) {
|
|
839
890
|
__publicField(this, "_head");
|
|
840
891
|
__publicField(this, "_tail");
|
|
841
892
|
__publicField(this, "_length");
|
|
842
893
|
this._head = null;
|
|
843
894
|
this._tail = null;
|
|
844
895
|
this._length = 0;
|
|
896
|
+
if (elements) {
|
|
897
|
+
for (const el of elements)
|
|
898
|
+
this.push(el);
|
|
899
|
+
}
|
|
845
900
|
}
|
|
846
901
|
get head() {
|
|
847
902
|
return this._head;
|
|
@@ -1413,58 +1468,42 @@ var dataStructureTyped = (() => {
|
|
|
1413
1468
|
return count;
|
|
1414
1469
|
}
|
|
1415
1470
|
/**
|
|
1416
|
-
*
|
|
1417
|
-
* Space Complexity: O(1) - Constant space.
|
|
1418
|
-
*/
|
|
1419
|
-
/**
|
|
1420
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
1421
|
-
* Space Complexity: O(1) - Constant space.
|
|
1422
|
-
*
|
|
1423
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
1424
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
1425
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
1426
|
-
* current node in the linked list.
|
|
1471
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
1427
1472
|
*/
|
|
1428
|
-
|
|
1473
|
+
*[Symbol.iterator]() {
|
|
1429
1474
|
let current = this.head;
|
|
1430
|
-
let index = 0;
|
|
1431
1475
|
while (current) {
|
|
1432
|
-
|
|
1476
|
+
yield current.value;
|
|
1433
1477
|
current = current.next;
|
|
1434
|
-
index++;
|
|
1435
1478
|
}
|
|
1436
1479
|
}
|
|
1437
1480
|
/**
|
|
1438
|
-
* Time Complexity: O(n)
|
|
1439
|
-
* Space Complexity: O(
|
|
1481
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1482
|
+
* Space Complexity: O(1)
|
|
1440
1483
|
*/
|
|
1441
1484
|
/**
|
|
1442
|
-
* Time Complexity: O(n)
|
|
1443
|
-
* Space Complexity: O(
|
|
1485
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1486
|
+
* Space Complexity: O(1)
|
|
1444
1487
|
*
|
|
1445
|
-
* The `
|
|
1446
|
-
*
|
|
1447
|
-
*
|
|
1448
|
-
*
|
|
1449
|
-
* SinglyLinkedList).
|
|
1450
|
-
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
1488
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
1489
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
1490
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
1491
|
+
* current node in the linked list.
|
|
1451
1492
|
*/
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
current = current.next;
|
|
1493
|
+
forEach(callback) {
|
|
1494
|
+
let index = 0;
|
|
1495
|
+
for (const el of this) {
|
|
1496
|
+
callback(el, index, this);
|
|
1497
|
+
index++;
|
|
1458
1498
|
}
|
|
1459
|
-
return mappedList;
|
|
1460
1499
|
}
|
|
1461
1500
|
/**
|
|
1462
|
-
* Time Complexity: O(n)
|
|
1463
|
-
* Space Complexity: O(n)
|
|
1501
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1502
|
+
* Space Complexity: O(n)
|
|
1464
1503
|
*/
|
|
1465
1504
|
/**
|
|
1466
|
-
* Time Complexity: O(n)
|
|
1467
|
-
* Space Complexity: O(n)
|
|
1505
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1506
|
+
* Space Complexity: O(n)
|
|
1468
1507
|
*
|
|
1469
1508
|
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
1470
1509
|
* elements that satisfy the given callback function.
|
|
@@ -1474,50 +1513,67 @@ var dataStructureTyped = (() => {
|
|
|
1474
1513
|
*/
|
|
1475
1514
|
filter(callback) {
|
|
1476
1515
|
const filteredList = new _SinglyLinkedList();
|
|
1477
|
-
let
|
|
1478
|
-
|
|
1479
|
-
if (callback(current
|
|
1480
|
-
filteredList.push(current
|
|
1516
|
+
let index = 0;
|
|
1517
|
+
for (const current of this) {
|
|
1518
|
+
if (callback(current, index, this)) {
|
|
1519
|
+
filteredList.push(current);
|
|
1481
1520
|
}
|
|
1482
|
-
|
|
1521
|
+
index++;
|
|
1483
1522
|
}
|
|
1484
1523
|
return filteredList;
|
|
1485
1524
|
}
|
|
1486
1525
|
/**
|
|
1487
|
-
* Time Complexity: O(n)
|
|
1488
|
-
* Space Complexity: O(n)
|
|
1526
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1527
|
+
* Space Complexity: O(n)
|
|
1489
1528
|
*/
|
|
1490
1529
|
/**
|
|
1491
|
-
* Time Complexity: O(n)
|
|
1492
|
-
* Space Complexity: O(n)
|
|
1530
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1531
|
+
* Space Complexity: O(n)
|
|
1532
|
+
*
|
|
1533
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
1534
|
+
* SinglyLinkedList with the transformed values.
|
|
1535
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
1536
|
+
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
1537
|
+
* SinglyLinkedList).
|
|
1538
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
|
|
1539
|
+
*/
|
|
1540
|
+
map(callback) {
|
|
1541
|
+
const mappedList = new _SinglyLinkedList();
|
|
1542
|
+
let index = 0;
|
|
1543
|
+
for (const current of this) {
|
|
1544
|
+
mappedList.push(callback(current, index, this));
|
|
1545
|
+
index++;
|
|
1546
|
+
}
|
|
1547
|
+
return mappedList;
|
|
1548
|
+
}
|
|
1549
|
+
/**
|
|
1550
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1551
|
+
* Space Complexity: O(n)
|
|
1552
|
+
*/
|
|
1553
|
+
/**
|
|
1554
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1555
|
+
* Space Complexity: O(n)
|
|
1493
1556
|
*
|
|
1494
1557
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
1495
1558
|
* single value.
|
|
1496
1559
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
1497
1560
|
* used to perform a specific operation on each element of the linked list.
|
|
1498
|
-
* @param {
|
|
1561
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
1499
1562
|
* point for the reduction operation.
|
|
1500
1563
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
1501
1564
|
* elements in the linked list.
|
|
1502
1565
|
*/
|
|
1503
1566
|
reduce(callback, initialValue) {
|
|
1504
1567
|
let accumulator = initialValue;
|
|
1505
|
-
let
|
|
1506
|
-
|
|
1507
|
-
accumulator = callback(accumulator, current
|
|
1508
|
-
|
|
1568
|
+
let index = 0;
|
|
1569
|
+
for (const current of this) {
|
|
1570
|
+
accumulator = callback(accumulator, current, index, this);
|
|
1571
|
+
index++;
|
|
1509
1572
|
}
|
|
1510
1573
|
return accumulator;
|
|
1511
1574
|
}
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
*/
|
|
1515
|
-
*[Symbol.iterator]() {
|
|
1516
|
-
let current = this.head;
|
|
1517
|
-
while (current) {
|
|
1518
|
-
yield current.value;
|
|
1519
|
-
current = current.next;
|
|
1520
|
-
}
|
|
1575
|
+
print() {
|
|
1576
|
+
console.log([...this]);
|
|
1521
1577
|
}
|
|
1522
1578
|
};
|
|
1523
1579
|
|
|
@@ -1541,13 +1597,18 @@ var dataStructureTyped = (() => {
|
|
|
1541
1597
|
/**
|
|
1542
1598
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
1543
1599
|
*/
|
|
1544
|
-
constructor() {
|
|
1600
|
+
constructor(elements) {
|
|
1545
1601
|
__publicField(this, "_head");
|
|
1546
1602
|
__publicField(this, "_tail");
|
|
1547
1603
|
__publicField(this, "_length");
|
|
1548
1604
|
this._head = null;
|
|
1549
1605
|
this._tail = null;
|
|
1550
1606
|
this._length = 0;
|
|
1607
|
+
if (elements) {
|
|
1608
|
+
for (const el of elements) {
|
|
1609
|
+
this.push(el);
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1551
1612
|
}
|
|
1552
1613
|
get head() {
|
|
1553
1614
|
return this._head;
|
|
@@ -1917,6 +1978,45 @@ var dataStructureTyped = (() => {
|
|
|
1917
1978
|
}
|
|
1918
1979
|
return false;
|
|
1919
1980
|
}
|
|
1981
|
+
/**
|
|
1982
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1983
|
+
* Space Complexity: O(1)
|
|
1984
|
+
*/
|
|
1985
|
+
/**
|
|
1986
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1987
|
+
* Space Complexity: O(1)
|
|
1988
|
+
*
|
|
1989
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
1990
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
1991
|
+
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
1992
|
+
* itself.
|
|
1993
|
+
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
1994
|
+
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
1995
|
+
* existing value or node is not found in the doubly linked list.
|
|
1996
|
+
*/
|
|
1997
|
+
insertAfter(existingValueOrNode, newValue) {
|
|
1998
|
+
let existingNode;
|
|
1999
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
2000
|
+
existingNode = existingValueOrNode;
|
|
2001
|
+
} else {
|
|
2002
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
2003
|
+
}
|
|
2004
|
+
if (existingNode) {
|
|
2005
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
2006
|
+
newNode.next = existingNode.next;
|
|
2007
|
+
if (existingNode.next) {
|
|
2008
|
+
existingNode.next.prev = newNode;
|
|
2009
|
+
}
|
|
2010
|
+
newNode.prev = existingNode;
|
|
2011
|
+
existingNode.next = newNode;
|
|
2012
|
+
if (existingNode === this.tail) {
|
|
2013
|
+
this._tail = newNode;
|
|
2014
|
+
}
|
|
2015
|
+
this._length++;
|
|
2016
|
+
return true;
|
|
2017
|
+
}
|
|
2018
|
+
return false;
|
|
2019
|
+
}
|
|
1920
2020
|
/**
|
|
1921
2021
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1922
2022
|
* Space Complexity: O(1)
|
|
@@ -1983,26 +2083,6 @@ var dataStructureTyped = (() => {
|
|
|
1983
2083
|
}
|
|
1984
2084
|
return false;
|
|
1985
2085
|
}
|
|
1986
|
-
/**
|
|
1987
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1988
|
-
* Space Complexity: O(n)
|
|
1989
|
-
*/
|
|
1990
|
-
/**
|
|
1991
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1992
|
-
* Space Complexity: O(n)
|
|
1993
|
-
*
|
|
1994
|
-
* The `toArray` function converts a linked list into an array.
|
|
1995
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
1996
|
-
*/
|
|
1997
|
-
toArray() {
|
|
1998
|
-
const array = [];
|
|
1999
|
-
let current = this.head;
|
|
2000
|
-
while (current) {
|
|
2001
|
-
array.push(current.value);
|
|
2002
|
-
current = current.next;
|
|
2003
|
-
}
|
|
2004
|
-
return array;
|
|
2005
|
-
}
|
|
2006
2086
|
/**
|
|
2007
2087
|
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
2008
2088
|
* @returns A boolean value is being returned.
|
|
@@ -2093,6 +2173,25 @@ var dataStructureTyped = (() => {
|
|
|
2093
2173
|
}
|
|
2094
2174
|
return null;
|
|
2095
2175
|
}
|
|
2176
|
+
/**
|
|
2177
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2178
|
+
* Space Complexity: O(1)
|
|
2179
|
+
*/
|
|
2180
|
+
/**
|
|
2181
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2182
|
+
* Space Complexity: O(1)
|
|
2183
|
+
*
|
|
2184
|
+
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
2185
|
+
*/
|
|
2186
|
+
reverse() {
|
|
2187
|
+
let current = this.head;
|
|
2188
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
2189
|
+
while (current) {
|
|
2190
|
+
const next = current.next;
|
|
2191
|
+
[current.prev, current.next] = [current.next, current.prev];
|
|
2192
|
+
current = next;
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2096
2195
|
/**
|
|
2097
2196
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2098
2197
|
* Space Complexity: O(n)
|
|
@@ -2101,35 +2200,46 @@ var dataStructureTyped = (() => {
|
|
|
2101
2200
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2102
2201
|
* Space Complexity: O(n)
|
|
2103
2202
|
*
|
|
2104
|
-
* The `
|
|
2105
|
-
* @returns The `
|
|
2203
|
+
* The `toArray` function converts a linked list into an array.
|
|
2204
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
2106
2205
|
*/
|
|
2107
|
-
|
|
2206
|
+
toArray() {
|
|
2108
2207
|
const array = [];
|
|
2109
|
-
let current = this.
|
|
2208
|
+
let current = this.head;
|
|
2110
2209
|
while (current) {
|
|
2111
2210
|
array.push(current.value);
|
|
2112
|
-
current = current.
|
|
2211
|
+
current = current.next;
|
|
2113
2212
|
}
|
|
2114
2213
|
return array;
|
|
2115
2214
|
}
|
|
2116
2215
|
/**
|
|
2117
2216
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2118
|
-
* Space Complexity: O(
|
|
2217
|
+
* Space Complexity: O(n)
|
|
2119
2218
|
*/
|
|
2120
2219
|
/**
|
|
2121
2220
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2122
|
-
* Space Complexity: O(
|
|
2221
|
+
* Space Complexity: O(n)
|
|
2123
2222
|
*
|
|
2124
|
-
* The `
|
|
2223
|
+
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
2224
|
+
* @returns The `toReversedArray()` function returns an array of type `E[]`.
|
|
2125
2225
|
*/
|
|
2126
|
-
|
|
2226
|
+
toReversedArray() {
|
|
2227
|
+
const array = [];
|
|
2228
|
+
let current = this.tail;
|
|
2229
|
+
while (current) {
|
|
2230
|
+
array.push(current.value);
|
|
2231
|
+
current = current.prev;
|
|
2232
|
+
}
|
|
2233
|
+
return array;
|
|
2234
|
+
}
|
|
2235
|
+
/**
|
|
2236
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
2237
|
+
*/
|
|
2238
|
+
*[Symbol.iterator]() {
|
|
2127
2239
|
let current = this.head;
|
|
2128
|
-
[this._head, this._tail] = [this.tail, this.head];
|
|
2129
2240
|
while (current) {
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
current = next;
|
|
2241
|
+
yield current.value;
|
|
2242
|
+
current = current.next;
|
|
2133
2243
|
}
|
|
2134
2244
|
}
|
|
2135
2245
|
/**
|
|
@@ -2146,11 +2256,9 @@ var dataStructureTyped = (() => {
|
|
|
2146
2256
|
* current node in the linked list.
|
|
2147
2257
|
*/
|
|
2148
2258
|
forEach(callback) {
|
|
2149
|
-
let current = this.head;
|
|
2150
2259
|
let index = 0;
|
|
2151
|
-
|
|
2152
|
-
callback(
|
|
2153
|
-
current = current.next;
|
|
2260
|
+
for (const el of this) {
|
|
2261
|
+
callback(el, index, this);
|
|
2154
2262
|
index++;
|
|
2155
2263
|
}
|
|
2156
2264
|
}
|
|
@@ -2162,21 +2270,22 @@ var dataStructureTyped = (() => {
|
|
|
2162
2270
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2163
2271
|
* Space Complexity: O(n)
|
|
2164
2272
|
*
|
|
2165
|
-
* The `
|
|
2166
|
-
*
|
|
2167
|
-
* @param callback - The callback parameter is a function that takes a value of type E
|
|
2168
|
-
*
|
|
2169
|
-
* DoublyLinkedList
|
|
2170
|
-
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
2273
|
+
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
2274
|
+
* elements that satisfy the given callback function.
|
|
2275
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
2276
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
2277
|
+
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
2171
2278
|
*/
|
|
2172
|
-
|
|
2173
|
-
const
|
|
2174
|
-
let
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2279
|
+
filter(callback) {
|
|
2280
|
+
const filteredList = new _DoublyLinkedList();
|
|
2281
|
+
let index = 0;
|
|
2282
|
+
for (const current of this) {
|
|
2283
|
+
if (callback(current, index, this)) {
|
|
2284
|
+
filteredList.push(current);
|
|
2285
|
+
}
|
|
2286
|
+
index++;
|
|
2178
2287
|
}
|
|
2179
|
-
return
|
|
2288
|
+
return filteredList;
|
|
2180
2289
|
}
|
|
2181
2290
|
/**
|
|
2182
2291
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2186,22 +2295,21 @@ var dataStructureTyped = (() => {
|
|
|
2186
2295
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2187
2296
|
* Space Complexity: O(n)
|
|
2188
2297
|
*
|
|
2189
|
-
* The `
|
|
2190
|
-
*
|
|
2191
|
-
* @param callback - The
|
|
2192
|
-
*
|
|
2193
|
-
*
|
|
2298
|
+
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
2299
|
+
* DoublyLinkedList with the transformed values.
|
|
2300
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
2301
|
+
* the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
2302
|
+
* DoublyLinkedList).
|
|
2303
|
+
* @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
|
|
2194
2304
|
*/
|
|
2195
|
-
|
|
2196
|
-
const
|
|
2197
|
-
let
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
}
|
|
2202
|
-
current = current.next;
|
|
2305
|
+
map(callback) {
|
|
2306
|
+
const mappedList = new _DoublyLinkedList();
|
|
2307
|
+
let index = 0;
|
|
2308
|
+
for (const current of this) {
|
|
2309
|
+
mappedList.push(callback(current, index, this));
|
|
2310
|
+
index++;
|
|
2203
2311
|
}
|
|
2204
|
-
return
|
|
2312
|
+
return mappedList;
|
|
2205
2313
|
}
|
|
2206
2314
|
/**
|
|
2207
2315
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2215,68 +2323,22 @@ var dataStructureTyped = (() => {
|
|
|
2215
2323
|
* single value.
|
|
2216
2324
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
2217
2325
|
* used to perform a specific operation on each element of the linked list.
|
|
2218
|
-
* @param {
|
|
2326
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
2219
2327
|
* point for the reduction operation.
|
|
2220
2328
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
2221
2329
|
* elements in the linked list.
|
|
2222
2330
|
*/
|
|
2223
2331
|
reduce(callback, initialValue) {
|
|
2224
2332
|
let accumulator = initialValue;
|
|
2225
|
-
let
|
|
2226
|
-
|
|
2227
|
-
accumulator = callback(accumulator, current
|
|
2228
|
-
|
|
2333
|
+
let index = 0;
|
|
2334
|
+
for (const current of this) {
|
|
2335
|
+
accumulator = callback(accumulator, current, index, this);
|
|
2336
|
+
index++;
|
|
2229
2337
|
}
|
|
2230
2338
|
return accumulator;
|
|
2231
2339
|
}
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
* Space Complexity: O(1)
|
|
2235
|
-
*/
|
|
2236
|
-
/**
|
|
2237
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2238
|
-
* Space Complexity: O(1)
|
|
2239
|
-
*
|
|
2240
|
-
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
2241
|
-
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
2242
|
-
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
2243
|
-
* itself.
|
|
2244
|
-
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
2245
|
-
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
2246
|
-
* existing value or node is not found in the doubly linked list.
|
|
2247
|
-
*/
|
|
2248
|
-
insertAfter(existingValueOrNode, newValue) {
|
|
2249
|
-
let existingNode;
|
|
2250
|
-
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
2251
|
-
existingNode = existingValueOrNode;
|
|
2252
|
-
} else {
|
|
2253
|
-
existingNode = this.getNode(existingValueOrNode);
|
|
2254
|
-
}
|
|
2255
|
-
if (existingNode) {
|
|
2256
|
-
const newNode = new DoublyLinkedListNode(newValue);
|
|
2257
|
-
newNode.next = existingNode.next;
|
|
2258
|
-
if (existingNode.next) {
|
|
2259
|
-
existingNode.next.prev = newNode;
|
|
2260
|
-
}
|
|
2261
|
-
newNode.prev = existingNode;
|
|
2262
|
-
existingNode.next = newNode;
|
|
2263
|
-
if (existingNode === this.tail) {
|
|
2264
|
-
this._tail = newNode;
|
|
2265
|
-
}
|
|
2266
|
-
this._length++;
|
|
2267
|
-
return true;
|
|
2268
|
-
}
|
|
2269
|
-
return false;
|
|
2270
|
-
}
|
|
2271
|
-
/**
|
|
2272
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
2273
|
-
*/
|
|
2274
|
-
*[Symbol.iterator]() {
|
|
2275
|
-
let current = this.head;
|
|
2276
|
-
while (current) {
|
|
2277
|
-
yield current.value;
|
|
2278
|
-
current = current.next;
|
|
2279
|
-
}
|
|
2340
|
+
print() {
|
|
2341
|
+
console.log([...this]);
|
|
2280
2342
|
}
|
|
2281
2343
|
};
|
|
2282
2344
|
|
|
@@ -2538,7 +2600,12 @@ var dataStructureTyped = (() => {
|
|
|
2538
2600
|
*/
|
|
2539
2601
|
constructor(elements) {
|
|
2540
2602
|
__publicField(this, "_elements");
|
|
2541
|
-
this._elements =
|
|
2603
|
+
this._elements = [];
|
|
2604
|
+
if (elements) {
|
|
2605
|
+
for (const el of elements) {
|
|
2606
|
+
this.push(el);
|
|
2607
|
+
}
|
|
2608
|
+
}
|
|
2542
2609
|
}
|
|
2543
2610
|
get elements() {
|
|
2544
2611
|
return this._elements;
|
|
@@ -2547,6 +2614,13 @@ var dataStructureTyped = (() => {
|
|
|
2547
2614
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
2548
2615
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
2549
2616
|
*/
|
|
2617
|
+
/**
|
|
2618
|
+
* The size() function returns the number of elements in an array.
|
|
2619
|
+
* @returns The size of the elements array.
|
|
2620
|
+
*/
|
|
2621
|
+
get size() {
|
|
2622
|
+
return this.elements.length;
|
|
2623
|
+
}
|
|
2550
2624
|
/**
|
|
2551
2625
|
* Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
|
|
2552
2626
|
* Space Complexity: O(n), as it creates a new stack with the elements from the input array.
|
|
@@ -2566,13 +2640,6 @@ var dataStructureTyped = (() => {
|
|
|
2566
2640
|
isEmpty() {
|
|
2567
2641
|
return this.elements.length === 0;
|
|
2568
2642
|
}
|
|
2569
|
-
/**
|
|
2570
|
-
* The size() function returns the number of elements in an array.
|
|
2571
|
-
* @returns The size of the elements array.
|
|
2572
|
-
*/
|
|
2573
|
-
size() {
|
|
2574
|
-
return this.elements.length;
|
|
2575
|
-
}
|
|
2576
2643
|
/**
|
|
2577
2644
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
2578
2645
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
@@ -2656,6 +2723,58 @@ var dataStructureTyped = (() => {
|
|
|
2656
2723
|
clone() {
|
|
2657
2724
|
return new _Stack(this.elements.slice());
|
|
2658
2725
|
}
|
|
2726
|
+
/**
|
|
2727
|
+
* Custom iterator for the Stack class.
|
|
2728
|
+
* @returns An iterator object.
|
|
2729
|
+
*/
|
|
2730
|
+
*[Symbol.iterator]() {
|
|
2731
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
2732
|
+
yield this.elements[i];
|
|
2733
|
+
}
|
|
2734
|
+
}
|
|
2735
|
+
/**
|
|
2736
|
+
* Applies a function to each element of the stack.
|
|
2737
|
+
* @param {function(E): void} callback - A function to apply to each element.
|
|
2738
|
+
*/
|
|
2739
|
+
forEach(callback) {
|
|
2740
|
+
let index = 0;
|
|
2741
|
+
for (const el of this) {
|
|
2742
|
+
callback(el, index, this);
|
|
2743
|
+
index++;
|
|
2744
|
+
}
|
|
2745
|
+
}
|
|
2746
|
+
filter(predicate) {
|
|
2747
|
+
const newStack = new _Stack();
|
|
2748
|
+
let index = 0;
|
|
2749
|
+
for (const el of this) {
|
|
2750
|
+
if (predicate(el, index, this)) {
|
|
2751
|
+
newStack.push(el);
|
|
2752
|
+
}
|
|
2753
|
+
index++;
|
|
2754
|
+
}
|
|
2755
|
+
return newStack;
|
|
2756
|
+
}
|
|
2757
|
+
map(callback) {
|
|
2758
|
+
const newStack = new _Stack();
|
|
2759
|
+
let index = 0;
|
|
2760
|
+
for (const el of this) {
|
|
2761
|
+
newStack.push(callback(el, index, this));
|
|
2762
|
+
index++;
|
|
2763
|
+
}
|
|
2764
|
+
return newStack;
|
|
2765
|
+
}
|
|
2766
|
+
reduce(callback, initialValue) {
|
|
2767
|
+
let accumulator = initialValue;
|
|
2768
|
+
let index = 0;
|
|
2769
|
+
for (const el of this) {
|
|
2770
|
+
accumulator = callback(accumulator, el, index, this);
|
|
2771
|
+
index++;
|
|
2772
|
+
}
|
|
2773
|
+
return accumulator;
|
|
2774
|
+
}
|
|
2775
|
+
print() {
|
|
2776
|
+
console.log([...this]);
|
|
2777
|
+
}
|
|
2659
2778
|
};
|
|
2660
2779
|
|
|
2661
2780
|
// src/data-structures/queue/queue.ts
|
|
@@ -2907,19 +3026,99 @@ var dataStructureTyped = (() => {
|
|
|
2907
3026
|
* Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
|
|
2908
3027
|
*/
|
|
2909
3028
|
/**
|
|
2910
|
-
* Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array.
|
|
2911
|
-
* Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
|
|
3029
|
+
* Time Complexity: O(n) - where n is the number of elements in the queue. It creates a shallow copy of the internal array.
|
|
3030
|
+
* Space Complexity: O(n) - the space required is proportional to the number of elements in the queue.
|
|
3031
|
+
*
|
|
3032
|
+
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
3033
|
+
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
3034
|
+
*/
|
|
3035
|
+
clone() {
|
|
3036
|
+
return new _Queue(this.nodes.slice(this.offset));
|
|
3037
|
+
}
|
|
3038
|
+
print() {
|
|
3039
|
+
console.log([...this]);
|
|
3040
|
+
}
|
|
3041
|
+
*[Symbol.iterator]() {
|
|
3042
|
+
for (const item of this.nodes) {
|
|
3043
|
+
yield item;
|
|
3044
|
+
}
|
|
3045
|
+
}
|
|
3046
|
+
/**
|
|
3047
|
+
* Time Complexity: O(n)
|
|
3048
|
+
* Space Complexity: O(1)
|
|
3049
|
+
*/
|
|
3050
|
+
/**
|
|
3051
|
+
* Time Complexity: O(n)
|
|
3052
|
+
* Space Complexity: O(1)
|
|
3053
|
+
*
|
|
3054
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
3055
|
+
* each element.
|
|
3056
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
3057
|
+
* deque. It takes three parameters:
|
|
3058
|
+
*/
|
|
3059
|
+
forEach(callback) {
|
|
3060
|
+
let index = 0;
|
|
3061
|
+
for (const el of this) {
|
|
3062
|
+
callback(el, index, this);
|
|
3063
|
+
index++;
|
|
3064
|
+
}
|
|
3065
|
+
}
|
|
3066
|
+
/**
|
|
3067
|
+
* Time Complexity: O(n)
|
|
3068
|
+
* Space Complexity: O(n)
|
|
3069
|
+
*/
|
|
3070
|
+
/**
|
|
3071
|
+
* Time Complexity: O(n)
|
|
3072
|
+
* Space Complexity: O(n)
|
|
3073
|
+
*
|
|
3074
|
+
* The `filter` function creates a new deque containing only the elements that satisfy the given
|
|
3075
|
+
* predicate function.
|
|
3076
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
|
|
3077
|
+
* `index`, and `deque`.
|
|
3078
|
+
* @returns The `filter` method is returning a new `Queue` object that contains only the elements
|
|
3079
|
+
* that satisfy the given `predicate` function.
|
|
3080
|
+
*/
|
|
3081
|
+
filter(predicate) {
|
|
3082
|
+
const newDeque = new _Queue([]);
|
|
3083
|
+
let index = 0;
|
|
3084
|
+
for (const el of this) {
|
|
3085
|
+
if (predicate(el, index, this)) {
|
|
3086
|
+
newDeque.push(el);
|
|
3087
|
+
}
|
|
3088
|
+
index++;
|
|
3089
|
+
}
|
|
3090
|
+
return newDeque;
|
|
3091
|
+
}
|
|
3092
|
+
/**
|
|
3093
|
+
* Time Complexity: O(n)
|
|
3094
|
+
* Space Complexity: O(n)
|
|
3095
|
+
*/
|
|
3096
|
+
/**
|
|
3097
|
+
* Time Complexity: O(n)
|
|
3098
|
+
* Space Complexity: O(n)
|
|
2912
3099
|
*
|
|
2913
|
-
* The `
|
|
2914
|
-
*
|
|
3100
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
3101
|
+
* returning a new deque with the results.
|
|
3102
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
3103
|
+
* @returns The `map` method is returning a new `Queue` object with the transformed elements.
|
|
2915
3104
|
*/
|
|
2916
|
-
|
|
2917
|
-
|
|
3105
|
+
map(callback) {
|
|
3106
|
+
const newDeque = new _Queue([]);
|
|
3107
|
+
let index = 0;
|
|
3108
|
+
for (const el of this) {
|
|
3109
|
+
newDeque.push(callback(el, index, this));
|
|
3110
|
+
index++;
|
|
3111
|
+
}
|
|
3112
|
+
return newDeque;
|
|
2918
3113
|
}
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
3114
|
+
reduce(callback, initialValue) {
|
|
3115
|
+
let accumulator = initialValue;
|
|
3116
|
+
let index = 0;
|
|
3117
|
+
for (const el of this) {
|
|
3118
|
+
accumulator = callback(accumulator, el, index, this);
|
|
3119
|
+
index++;
|
|
2922
3120
|
}
|
|
3121
|
+
return accumulator;
|
|
2923
3122
|
}
|
|
2924
3123
|
};
|
|
2925
3124
|
|
|
@@ -3505,15 +3704,21 @@ var dataStructureTyped = (() => {
|
|
|
3505
3704
|
* Time Complexity: O(n)
|
|
3506
3705
|
* Space Complexity: O(1)
|
|
3507
3706
|
*
|
|
3508
|
-
* The `
|
|
3509
|
-
*
|
|
3510
|
-
* @param callback -
|
|
3511
|
-
*
|
|
3707
|
+
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
3708
|
+
* the callback function returns true, or undefined if no such element is found.
|
|
3709
|
+
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
3710
|
+
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
3711
|
+
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
3712
|
+
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
3512
3713
|
*/
|
|
3513
|
-
|
|
3714
|
+
find(callback) {
|
|
3514
3715
|
for (let i = 0; i < this.size; ++i) {
|
|
3515
|
-
|
|
3716
|
+
const element = this.getAt(i);
|
|
3717
|
+
if (callback(element, i, this)) {
|
|
3718
|
+
return element;
|
|
3719
|
+
}
|
|
3516
3720
|
}
|
|
3721
|
+
return void 0;
|
|
3517
3722
|
}
|
|
3518
3723
|
/**
|
|
3519
3724
|
* Time Complexity: O(n)
|
|
@@ -3523,21 +3728,20 @@ var dataStructureTyped = (() => {
|
|
|
3523
3728
|
* Time Complexity: O(n)
|
|
3524
3729
|
* Space Complexity: O(1)
|
|
3525
3730
|
*
|
|
3526
|
-
* The
|
|
3527
|
-
*
|
|
3528
|
-
* @param
|
|
3529
|
-
*
|
|
3530
|
-
* @returns The
|
|
3531
|
-
*
|
|
3731
|
+
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
3732
|
+
* or -1 if the element is not found.
|
|
3733
|
+
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
3734
|
+
* index of in the data structure.
|
|
3735
|
+
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
3736
|
+
* in the data structure. If the element is not found, it returns -1.
|
|
3532
3737
|
*/
|
|
3533
|
-
|
|
3738
|
+
indexOf(element) {
|
|
3534
3739
|
for (let i = 0; i < this.size; ++i) {
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
return element;
|
|
3740
|
+
if (this.getAt(i) === element) {
|
|
3741
|
+
return i;
|
|
3538
3742
|
}
|
|
3539
3743
|
}
|
|
3540
|
-
return
|
|
3744
|
+
return -1;
|
|
3541
3745
|
}
|
|
3542
3746
|
/**
|
|
3543
3747
|
* Time Complexity: O(n)
|
|
@@ -3559,23 +3763,39 @@ var dataStructureTyped = (() => {
|
|
|
3559
3763
|
}
|
|
3560
3764
|
/**
|
|
3561
3765
|
* Time Complexity: O(n)
|
|
3562
|
-
* Space Complexity: O(
|
|
3766
|
+
* Space Complexity: O(1)
|
|
3563
3767
|
*/
|
|
3564
3768
|
/**
|
|
3565
3769
|
* Time Complexity: O(n)
|
|
3566
|
-
* Space Complexity: O(
|
|
3770
|
+
* Space Complexity: O(1)
|
|
3567
3771
|
*
|
|
3568
|
-
* The
|
|
3569
|
-
*
|
|
3570
|
-
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
3571
|
-
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
3772
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
3773
|
+
* object to be iterated over using a for...of loop.
|
|
3572
3774
|
*/
|
|
3573
|
-
|
|
3574
|
-
const newDeque = new _Deque([], this._bucketSize);
|
|
3775
|
+
*[Symbol.iterator]() {
|
|
3575
3776
|
for (let i = 0; i < this.size; ++i) {
|
|
3576
|
-
|
|
3777
|
+
yield this.getAt(i);
|
|
3778
|
+
}
|
|
3779
|
+
}
|
|
3780
|
+
/**
|
|
3781
|
+
* Time Complexity: O(n)
|
|
3782
|
+
* Space Complexity: O(1)
|
|
3783
|
+
*/
|
|
3784
|
+
/**
|
|
3785
|
+
* Time Complexity: O(n)
|
|
3786
|
+
* Space Complexity: O(1)
|
|
3787
|
+
*
|
|
3788
|
+
* The `forEach` function iterates over each element in a deque and applies a callback function to
|
|
3789
|
+
* each element.
|
|
3790
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
3791
|
+
* deque. It takes three parameters:
|
|
3792
|
+
*/
|
|
3793
|
+
forEach(callback) {
|
|
3794
|
+
let index = 0;
|
|
3795
|
+
for (const el of this) {
|
|
3796
|
+
callback(el, index, this);
|
|
3797
|
+
index++;
|
|
3577
3798
|
}
|
|
3578
|
-
return newDeque;
|
|
3579
3799
|
}
|
|
3580
3800
|
/**
|
|
3581
3801
|
* Time Complexity: O(n)
|
|
@@ -3594,11 +3814,34 @@ var dataStructureTyped = (() => {
|
|
|
3594
3814
|
*/
|
|
3595
3815
|
filter(predicate) {
|
|
3596
3816
|
const newDeque = new _Deque([], this._bucketSize);
|
|
3597
|
-
|
|
3598
|
-
|
|
3599
|
-
if (predicate(
|
|
3600
|
-
newDeque.push(
|
|
3817
|
+
let index = 0;
|
|
3818
|
+
for (const el of this) {
|
|
3819
|
+
if (predicate(el, index, this)) {
|
|
3820
|
+
newDeque.push(el);
|
|
3601
3821
|
}
|
|
3822
|
+
index++;
|
|
3823
|
+
}
|
|
3824
|
+
return newDeque;
|
|
3825
|
+
}
|
|
3826
|
+
/**
|
|
3827
|
+
* Time Complexity: O(n)
|
|
3828
|
+
* Space Complexity: O(n)
|
|
3829
|
+
*/
|
|
3830
|
+
/**
|
|
3831
|
+
* Time Complexity: O(n)
|
|
3832
|
+
* Space Complexity: O(n)
|
|
3833
|
+
*
|
|
3834
|
+
* The `map` function takes a callback function and applies it to each element in the deque,
|
|
3835
|
+
* returning a new deque with the results.
|
|
3836
|
+
* @param callback - The `callback` parameter is a function that takes three arguments:
|
|
3837
|
+
* @returns The `map` method is returning a new `Deque` object with the transformed elements.
|
|
3838
|
+
*/
|
|
3839
|
+
map(callback) {
|
|
3840
|
+
const newDeque = new _Deque([], this._bucketSize);
|
|
3841
|
+
let index = 0;
|
|
3842
|
+
for (const el of this) {
|
|
3843
|
+
newDeque.push(callback(el, index, this));
|
|
3844
|
+
index++;
|
|
3602
3845
|
}
|
|
3603
3846
|
return newDeque;
|
|
3604
3847
|
}
|
|
@@ -3621,49 +3864,15 @@ var dataStructureTyped = (() => {
|
|
|
3621
3864
|
*/
|
|
3622
3865
|
reduce(callback, initialValue) {
|
|
3623
3866
|
let accumulator = initialValue;
|
|
3624
|
-
|
|
3625
|
-
|
|
3867
|
+
let index = 0;
|
|
3868
|
+
for (const el of this) {
|
|
3869
|
+
accumulator = callback(accumulator, el, index, this);
|
|
3870
|
+
index++;
|
|
3626
3871
|
}
|
|
3627
3872
|
return accumulator;
|
|
3628
3873
|
}
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
* Space Complexity: O(1)
|
|
3632
|
-
*/
|
|
3633
|
-
/**
|
|
3634
|
-
* Time Complexity: O(n)
|
|
3635
|
-
* Space Complexity: O(1)
|
|
3636
|
-
*
|
|
3637
|
-
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
3638
|
-
* or -1 if the element is not found.
|
|
3639
|
-
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
3640
|
-
* index of in the data structure.
|
|
3641
|
-
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
3642
|
-
* in the data structure. If the element is not found, it returns -1.
|
|
3643
|
-
*/
|
|
3644
|
-
indexOf(element) {
|
|
3645
|
-
for (let i = 0; i < this.size; ++i) {
|
|
3646
|
-
if (this.getAt(i) === element) {
|
|
3647
|
-
return i;
|
|
3648
|
-
}
|
|
3649
|
-
}
|
|
3650
|
-
return -1;
|
|
3651
|
-
}
|
|
3652
|
-
/**
|
|
3653
|
-
* Time Complexity: O(n)
|
|
3654
|
-
* Space Complexity: O(1)
|
|
3655
|
-
*/
|
|
3656
|
-
/**
|
|
3657
|
-
* Time Complexity: O(n)
|
|
3658
|
-
* Space Complexity: O(1)
|
|
3659
|
-
*
|
|
3660
|
-
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
3661
|
-
* object to be iterated over using a for...of loop.
|
|
3662
|
-
*/
|
|
3663
|
-
*[Symbol.iterator]() {
|
|
3664
|
-
for (let i = 0; i < this.size; ++i) {
|
|
3665
|
-
yield this.getAt(i);
|
|
3666
|
-
}
|
|
3874
|
+
print() {
|
|
3875
|
+
console.log([...this]);
|
|
3667
3876
|
}
|
|
3668
3877
|
/**
|
|
3669
3878
|
* Time Complexity: O(n)
|
|
@@ -3895,21 +4104,32 @@ var dataStructureTyped = (() => {
|
|
|
3895
4104
|
|
|
3896
4105
|
// src/data-structures/heap/heap.ts
|
|
3897
4106
|
var Heap = class _Heap {
|
|
3898
|
-
constructor(options) {
|
|
4107
|
+
constructor(elements, options) {
|
|
4108
|
+
__publicField(this, "options");
|
|
3899
4109
|
__publicField(this, "_elements", []);
|
|
3900
|
-
|
|
3901
|
-
|
|
3902
|
-
|
|
3903
|
-
|
|
3904
|
-
|
|
4110
|
+
const defaultComparator = (a, b) => {
|
|
4111
|
+
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4112
|
+
throw new Error("The a, b params of compare function must be number");
|
|
4113
|
+
} else {
|
|
4114
|
+
return a - b;
|
|
4115
|
+
}
|
|
4116
|
+
};
|
|
4117
|
+
if (options) {
|
|
4118
|
+
this.options = options;
|
|
4119
|
+
} else {
|
|
4120
|
+
this.options = {
|
|
4121
|
+
comparator: defaultComparator
|
|
4122
|
+
};
|
|
4123
|
+
}
|
|
4124
|
+
if (elements) {
|
|
4125
|
+
for (const el of elements) {
|
|
4126
|
+
this.push(el);
|
|
4127
|
+
}
|
|
3905
4128
|
}
|
|
3906
4129
|
}
|
|
3907
4130
|
get elements() {
|
|
3908
4131
|
return this._elements;
|
|
3909
4132
|
}
|
|
3910
|
-
get comparator() {
|
|
3911
|
-
return this._comparator;
|
|
3912
|
-
}
|
|
3913
4133
|
/**
|
|
3914
4134
|
* Get the size (number of elements) of the heap.
|
|
3915
4135
|
*/
|
|
@@ -3927,10 +4147,11 @@ var dataStructureTyped = (() => {
|
|
|
3927
4147
|
/**
|
|
3928
4148
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
3929
4149
|
* @returns A new Heap instance.
|
|
4150
|
+
* @param elements
|
|
3930
4151
|
* @param options
|
|
3931
4152
|
*/
|
|
3932
|
-
static heapify(options) {
|
|
3933
|
-
return new _Heap(options);
|
|
4153
|
+
static heapify(elements, options) {
|
|
4154
|
+
return new _Heap(elements, options);
|
|
3934
4155
|
}
|
|
3935
4156
|
/**
|
|
3936
4157
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
@@ -4090,26 +4311,27 @@ var dataStructureTyped = (() => {
|
|
|
4090
4311
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
4091
4312
|
* @returns An array containing elements traversed in the specified order.
|
|
4092
4313
|
*/
|
|
4093
|
-
dfs(order) {
|
|
4314
|
+
dfs(order = "pre") {
|
|
4094
4315
|
const result = [];
|
|
4095
|
-
const
|
|
4316
|
+
const _dfs = (index) => {
|
|
4317
|
+
const left = 2 * index + 1, right = left + 1;
|
|
4096
4318
|
if (index < this.size) {
|
|
4097
4319
|
if (order === "in") {
|
|
4098
|
-
|
|
4320
|
+
_dfs(left);
|
|
4099
4321
|
result.push(this.elements[index]);
|
|
4100
|
-
|
|
4322
|
+
_dfs(right);
|
|
4101
4323
|
} else if (order === "pre") {
|
|
4102
4324
|
result.push(this.elements[index]);
|
|
4103
|
-
|
|
4104
|
-
|
|
4325
|
+
_dfs(left);
|
|
4326
|
+
_dfs(right);
|
|
4105
4327
|
} else if (order === "post") {
|
|
4106
|
-
|
|
4107
|
-
|
|
4328
|
+
_dfs(left);
|
|
4329
|
+
_dfs(right);
|
|
4108
4330
|
result.push(this.elements[index]);
|
|
4109
4331
|
}
|
|
4110
4332
|
}
|
|
4111
4333
|
};
|
|
4112
|
-
|
|
4334
|
+
_dfs(0);
|
|
4113
4335
|
return result;
|
|
4114
4336
|
}
|
|
4115
4337
|
/**
|
|
@@ -4138,7 +4360,7 @@ var dataStructureTyped = (() => {
|
|
|
4138
4360
|
* @returns A new Heap instance containing the same elements.
|
|
4139
4361
|
*/
|
|
4140
4362
|
clone() {
|
|
4141
|
-
const clonedHeap = new _Heap(
|
|
4363
|
+
const clonedHeap = new _Heap([], this.options);
|
|
4142
4364
|
clonedHeap._elements = [...this.elements];
|
|
4143
4365
|
return clonedHeap;
|
|
4144
4366
|
}
|
|
@@ -4177,10 +4399,58 @@ var dataStructureTyped = (() => {
|
|
|
4177
4399
|
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
4178
4400
|
this._sinkDown(i, this.elements.length >> 1);
|
|
4179
4401
|
}
|
|
4402
|
+
*[Symbol.iterator]() {
|
|
4403
|
+
for (const element of this.elements) {
|
|
4404
|
+
yield element;
|
|
4405
|
+
}
|
|
4406
|
+
}
|
|
4407
|
+
forEach(callback) {
|
|
4408
|
+
let index = 0;
|
|
4409
|
+
for (const el of this) {
|
|
4410
|
+
callback(el, index, this);
|
|
4411
|
+
index++;
|
|
4412
|
+
}
|
|
4413
|
+
}
|
|
4414
|
+
filter(predicate) {
|
|
4415
|
+
const filteredHeap = new _Heap([], this.options);
|
|
4416
|
+
let index = 0;
|
|
4417
|
+
for (const el of this) {
|
|
4418
|
+
if (predicate(el, index, this)) {
|
|
4419
|
+
filteredHeap.push(el);
|
|
4420
|
+
}
|
|
4421
|
+
index++;
|
|
4422
|
+
}
|
|
4423
|
+
return filteredHeap;
|
|
4424
|
+
}
|
|
4425
|
+
map(callback, comparator) {
|
|
4426
|
+
const mappedHeap = new _Heap([], { comparator });
|
|
4427
|
+
let index = 0;
|
|
4428
|
+
for (const el of this) {
|
|
4429
|
+
mappedHeap.add(callback(el, index, this));
|
|
4430
|
+
index++;
|
|
4431
|
+
}
|
|
4432
|
+
return mappedHeap;
|
|
4433
|
+
}
|
|
4434
|
+
reduce(callback, initialValue) {
|
|
4435
|
+
let accumulator = initialValue;
|
|
4436
|
+
let index = 0;
|
|
4437
|
+
for (const el of this) {
|
|
4438
|
+
accumulator = callback(accumulator, el, index, this);
|
|
4439
|
+
index++;
|
|
4440
|
+
}
|
|
4441
|
+
return accumulator;
|
|
4442
|
+
}
|
|
4180
4443
|
/**
|
|
4181
4444
|
* Time Complexity: O(log n)
|
|
4182
4445
|
* Space Complexity: O(1)
|
|
4183
4446
|
*/
|
|
4447
|
+
print() {
|
|
4448
|
+
console.log([...this]);
|
|
4449
|
+
}
|
|
4450
|
+
/**
|
|
4451
|
+
* Time Complexity: O(n)
|
|
4452
|
+
* Space Complexity: O(1)
|
|
4453
|
+
*/
|
|
4184
4454
|
/**
|
|
4185
4455
|
* Time Complexity: O(log n)
|
|
4186
4456
|
* Space Complexity: O(1)
|
|
@@ -4193,17 +4463,13 @@ var dataStructureTyped = (() => {
|
|
|
4193
4463
|
while (index > 0) {
|
|
4194
4464
|
const parent = index - 1 >> 1;
|
|
4195
4465
|
const parentItem = this.elements[parent];
|
|
4196
|
-
if (this.
|
|
4466
|
+
if (this.options.comparator(parentItem, element) <= 0)
|
|
4197
4467
|
break;
|
|
4198
4468
|
this.elements[index] = parentItem;
|
|
4199
4469
|
index = parent;
|
|
4200
4470
|
}
|
|
4201
4471
|
this.elements[index] = element;
|
|
4202
4472
|
}
|
|
4203
|
-
/**
|
|
4204
|
-
* Time Complexity: O(n)
|
|
4205
|
-
* Space Complexity: O(1)
|
|
4206
|
-
*/
|
|
4207
4473
|
/**
|
|
4208
4474
|
* Time Complexity: O(log n)
|
|
4209
4475
|
* Space Complexity: O(1)
|
|
@@ -4218,11 +4484,11 @@ var dataStructureTyped = (() => {
|
|
|
4218
4484
|
let left = index << 1 | 1;
|
|
4219
4485
|
const right = left + 1;
|
|
4220
4486
|
let minItem = this.elements[left];
|
|
4221
|
-
if (right < this.elements.length && this.
|
|
4487
|
+
if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) {
|
|
4222
4488
|
left = right;
|
|
4223
4489
|
minItem = this.elements[right];
|
|
4224
4490
|
}
|
|
4225
|
-
if (this.
|
|
4491
|
+
if (this.options.comparator(minItem, element) >= 0)
|
|
4226
4492
|
break;
|
|
4227
4493
|
this.elements[index] = minItem;
|
|
4228
4494
|
index = left;
|
|
@@ -4251,7 +4517,7 @@ var dataStructureTyped = (() => {
|
|
|
4251
4517
|
__publicField(this, "_min");
|
|
4252
4518
|
__publicField(this, "_comparator");
|
|
4253
4519
|
this.clear();
|
|
4254
|
-
this._comparator = comparator || this.
|
|
4520
|
+
this._comparator = comparator || this._defaultComparator;
|
|
4255
4521
|
if (typeof this.comparator !== "function") {
|
|
4256
4522
|
throw new Error("FibonacciHeap constructor: given comparator should be a function.");
|
|
4257
4523
|
}
|
|
@@ -4420,7 +4686,7 @@ var dataStructureTyped = (() => {
|
|
|
4420
4686
|
this._root = void 0;
|
|
4421
4687
|
} else {
|
|
4422
4688
|
this._min = z.right;
|
|
4423
|
-
this.
|
|
4689
|
+
this._consolidate();
|
|
4424
4690
|
}
|
|
4425
4691
|
this._size--;
|
|
4426
4692
|
return z.element;
|
|
@@ -4456,27 +4722,27 @@ var dataStructureTyped = (() => {
|
|
|
4456
4722
|
this._size += heapToMerge.size;
|
|
4457
4723
|
heapToMerge.clear();
|
|
4458
4724
|
}
|
|
4725
|
+
/**
|
|
4726
|
+
* Create a new node.
|
|
4727
|
+
* @param element
|
|
4728
|
+
* @protected
|
|
4729
|
+
*/
|
|
4730
|
+
createNode(element) {
|
|
4731
|
+
return new FibonacciHeapNode(element);
|
|
4732
|
+
}
|
|
4459
4733
|
/**
|
|
4460
4734
|
* Default comparator function used by the heap.
|
|
4461
4735
|
* @param {E} a
|
|
4462
4736
|
* @param {E} b
|
|
4463
4737
|
* @protected
|
|
4464
4738
|
*/
|
|
4465
|
-
|
|
4739
|
+
_defaultComparator(a, b) {
|
|
4466
4740
|
if (a < b)
|
|
4467
4741
|
return -1;
|
|
4468
4742
|
if (a > b)
|
|
4469
4743
|
return 1;
|
|
4470
4744
|
return 0;
|
|
4471
4745
|
}
|
|
4472
|
-
/**
|
|
4473
|
-
* Create a new node.
|
|
4474
|
-
* @param element
|
|
4475
|
-
* @protected
|
|
4476
|
-
*/
|
|
4477
|
-
createNode(element) {
|
|
4478
|
-
return new FibonacciHeapNode(element);
|
|
4479
|
-
}
|
|
4480
4746
|
/**
|
|
4481
4747
|
* Time Complexity: O(1)
|
|
4482
4748
|
* Space Complexity: O(1)
|
|
@@ -4531,7 +4797,7 @@ var dataStructureTyped = (() => {
|
|
|
4531
4797
|
* @param x
|
|
4532
4798
|
* @protected
|
|
4533
4799
|
*/
|
|
4534
|
-
|
|
4800
|
+
_link(y, x) {
|
|
4535
4801
|
this.removeFromRoot(y);
|
|
4536
4802
|
y.left = y;
|
|
4537
4803
|
y.right = y;
|
|
@@ -4550,7 +4816,7 @@ var dataStructureTyped = (() => {
|
|
|
4550
4816
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
4551
4817
|
* @protected
|
|
4552
4818
|
*/
|
|
4553
|
-
|
|
4819
|
+
_consolidate() {
|
|
4554
4820
|
const A = new Array(this.size);
|
|
4555
4821
|
const elements = this.consumeLinkedList(this.root);
|
|
4556
4822
|
let x, y, d, t;
|
|
@@ -4564,7 +4830,7 @@ var dataStructureTyped = (() => {
|
|
|
4564
4830
|
x = y;
|
|
4565
4831
|
y = t;
|
|
4566
4832
|
}
|
|
4567
|
-
this.
|
|
4833
|
+
this._link(y, x);
|
|
4568
4834
|
A[d] = void 0;
|
|
4569
4835
|
d++;
|
|
4570
4836
|
}
|
|
@@ -4580,7 +4846,7 @@ var dataStructureTyped = (() => {
|
|
|
4580
4846
|
|
|
4581
4847
|
// src/data-structures/heap/max-heap.ts
|
|
4582
4848
|
var MaxHeap = class extends Heap {
|
|
4583
|
-
constructor(options = {
|
|
4849
|
+
constructor(elements, options = {
|
|
4584
4850
|
comparator: (a, b) => {
|
|
4585
4851
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4586
4852
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -4589,13 +4855,13 @@ var dataStructureTyped = (() => {
|
|
|
4589
4855
|
}
|
|
4590
4856
|
}
|
|
4591
4857
|
}) {
|
|
4592
|
-
super(options);
|
|
4858
|
+
super(elements, options);
|
|
4593
4859
|
}
|
|
4594
4860
|
};
|
|
4595
4861
|
|
|
4596
4862
|
// src/data-structures/heap/min-heap.ts
|
|
4597
4863
|
var MinHeap = class extends Heap {
|
|
4598
|
-
constructor(options = {
|
|
4864
|
+
constructor(elements, options = {
|
|
4599
4865
|
comparator: (a, b) => {
|
|
4600
4866
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4601
4867
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -4604,20 +4870,20 @@ var dataStructureTyped = (() => {
|
|
|
4604
4870
|
}
|
|
4605
4871
|
}
|
|
4606
4872
|
}) {
|
|
4607
|
-
super(options);
|
|
4873
|
+
super(elements, options);
|
|
4608
4874
|
}
|
|
4609
4875
|
};
|
|
4610
4876
|
|
|
4611
4877
|
// src/data-structures/priority-queue/priority-queue.ts
|
|
4612
4878
|
var PriorityQueue = class extends Heap {
|
|
4613
|
-
constructor(options) {
|
|
4614
|
-
super(options);
|
|
4879
|
+
constructor(elements, options) {
|
|
4880
|
+
super(elements, options);
|
|
4615
4881
|
}
|
|
4616
4882
|
};
|
|
4617
4883
|
|
|
4618
4884
|
// src/data-structures/priority-queue/min-priority-queue.ts
|
|
4619
4885
|
var MinPriorityQueue = class extends PriorityQueue {
|
|
4620
|
-
constructor(options = {
|
|
4886
|
+
constructor(elements, options = {
|
|
4621
4887
|
comparator: (a, b) => {
|
|
4622
4888
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4623
4889
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -4626,13 +4892,13 @@ var dataStructureTyped = (() => {
|
|
|
4626
4892
|
}
|
|
4627
4893
|
}
|
|
4628
4894
|
}) {
|
|
4629
|
-
super(options);
|
|
4895
|
+
super(elements, options);
|
|
4630
4896
|
}
|
|
4631
4897
|
};
|
|
4632
4898
|
|
|
4633
4899
|
// src/data-structures/priority-queue/max-priority-queue.ts
|
|
4634
4900
|
var MaxPriorityQueue = class extends PriorityQueue {
|
|
4635
|
-
constructor(options = {
|
|
4901
|
+
constructor(elements, options = {
|
|
4636
4902
|
comparator: (a, b) => {
|
|
4637
4903
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4638
4904
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -4641,7 +4907,7 @@ var dataStructureTyped = (() => {
|
|
|
4641
4907
|
}
|
|
4642
4908
|
}
|
|
4643
4909
|
}) {
|
|
4644
|
-
super(options);
|
|
4910
|
+
super(elements, options);
|
|
4645
4911
|
}
|
|
4646
4912
|
};
|
|
4647
4913
|
|
|
@@ -5224,7 +5490,7 @@ var dataStructureTyped = (() => {
|
|
|
5224
5490
|
if (vertexOrKey instanceof AbstractVertex)
|
|
5225
5491
|
distMap.set(vertexOrKey, Infinity);
|
|
5226
5492
|
}
|
|
5227
|
-
const heap = new PriorityQueue({ comparator: (a, b) => a.key - b.key });
|
|
5493
|
+
const heap = new PriorityQueue([], { comparator: (a, b) => a.key - b.key });
|
|
5228
5494
|
heap.add({ key: 0, value: srcVertex });
|
|
5229
5495
|
distMap.set(srcVertex, 0);
|
|
5230
5496
|
preMap.set(srcVertex, null);
|
|
@@ -6550,10 +6816,10 @@ var dataStructureTyped = (() => {
|
|
|
6550
6816
|
};
|
|
6551
6817
|
|
|
6552
6818
|
// src/types/data-structures/binary-tree/binary-tree.ts
|
|
6553
|
-
var IterationType = /* @__PURE__ */ ((
|
|
6554
|
-
|
|
6555
|
-
|
|
6556
|
-
return
|
|
6819
|
+
var IterationType = /* @__PURE__ */ ((IterationType3) => {
|
|
6820
|
+
IterationType3["ITERATIVE"] = "ITERATIVE";
|
|
6821
|
+
IterationType3["RECURSIVE"] = "RECURSIVE";
|
|
6822
|
+
return IterationType3;
|
|
6557
6823
|
})(IterationType || {});
|
|
6558
6824
|
var FamilyPosition = /* @__PURE__ */ ((FamilyPosition2) => {
|
|
6559
6825
|
FamilyPosition2["ROOT"] = "ROOT";
|
|
@@ -6660,17 +6926,20 @@ var dataStructureTyped = (() => {
|
|
|
6660
6926
|
* Creates a new instance of BinaryTree.
|
|
6661
6927
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
6662
6928
|
*/
|
|
6663
|
-
constructor(options) {
|
|
6664
|
-
__publicField(this, "
|
|
6929
|
+
constructor(elements, options) {
|
|
6930
|
+
__publicField(this, "iterationType", "ITERATIVE" /* ITERATIVE */);
|
|
6665
6931
|
__publicField(this, "_root");
|
|
6666
6932
|
__publicField(this, "_size");
|
|
6667
6933
|
__publicField(this, "_defaultOneParamCallback", (node) => node.key);
|
|
6668
6934
|
if (options) {
|
|
6669
|
-
|
|
6670
|
-
|
|
6671
|
-
|
|
6935
|
+
const { iterationType } = options;
|
|
6936
|
+
if (iterationType) {
|
|
6937
|
+
this.iterationType = iterationType;
|
|
6938
|
+
}
|
|
6672
6939
|
}
|
|
6673
6940
|
this._size = 0;
|
|
6941
|
+
if (elements)
|
|
6942
|
+
this.init(elements);
|
|
6674
6943
|
}
|
|
6675
6944
|
/**
|
|
6676
6945
|
* Get the root node of the binary tree.
|
|
@@ -6694,13 +6963,8 @@ var dataStructureTyped = (() => {
|
|
|
6694
6963
|
return new BinaryTreeNode(key, value);
|
|
6695
6964
|
}
|
|
6696
6965
|
createTree(options) {
|
|
6697
|
-
return new _BinaryTree(__spreadValues(
|
|
6966
|
+
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
6698
6967
|
}
|
|
6699
|
-
/**
|
|
6700
|
-
* Time Complexity: O(n)
|
|
6701
|
-
* Space Complexity: O(1)
|
|
6702
|
-
* 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.
|
|
6703
|
-
*/
|
|
6704
6968
|
/**
|
|
6705
6969
|
* Time Complexity: O(n)
|
|
6706
6970
|
* Space Complexity: O(1)
|
|
@@ -6756,8 +7020,9 @@ var dataStructureTyped = (() => {
|
|
|
6756
7020
|
return inserted;
|
|
6757
7021
|
}
|
|
6758
7022
|
/**
|
|
6759
|
-
* Time Complexity: O(
|
|
7023
|
+
* Time Complexity: O(n)
|
|
6760
7024
|
* Space Complexity: O(1)
|
|
7025
|
+
* 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.
|
|
6761
7026
|
*/
|
|
6762
7027
|
/**
|
|
6763
7028
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
@@ -6805,10 +7070,6 @@ var dataStructureTyped = (() => {
|
|
|
6805
7070
|
this.clear();
|
|
6806
7071
|
return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
|
|
6807
7072
|
}
|
|
6808
|
-
/**
|
|
6809
|
-
* Time Complexity: O(n)
|
|
6810
|
-
* Space Complexity: O(1)
|
|
6811
|
-
*/
|
|
6812
7073
|
/**
|
|
6813
7074
|
* Time Complexity: O(n)
|
|
6814
7075
|
* Space Complexity: O(1)
|
|
@@ -6899,9 +7160,8 @@ var dataStructureTyped = (() => {
|
|
|
6899
7160
|
return depth;
|
|
6900
7161
|
}
|
|
6901
7162
|
/**
|
|
6902
|
-
* Time Complexity: O(n)
|
|
6903
|
-
* Space Complexity: O(
|
|
6904
|
-
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
7163
|
+
* Time Complexity: O(n)
|
|
7164
|
+
* Space Complexity: O(1)
|
|
6905
7165
|
*/
|
|
6906
7166
|
/**
|
|
6907
7167
|
* Time Complexity: O(n)
|
|
@@ -6917,7 +7177,7 @@ var dataStructureTyped = (() => {
|
|
|
6917
7177
|
* values:
|
|
6918
7178
|
* @returns the height of the binary tree.
|
|
6919
7179
|
*/
|
|
6920
|
-
getHeight(beginRoot = this.root, iterationType = this.
|
|
7180
|
+
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
6921
7181
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
6922
7182
|
if (!beginRoot)
|
|
6923
7183
|
return -1;
|
|
@@ -6962,7 +7222,7 @@ var dataStructureTyped = (() => {
|
|
|
6962
7222
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
6963
7223
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
6964
7224
|
*/
|
|
6965
|
-
getMinHeight(beginRoot = this.root, iterationType = this.
|
|
7225
|
+
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
6966
7226
|
var _a, _b, _c;
|
|
6967
7227
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
6968
7228
|
if (!beginRoot)
|
|
@@ -7007,6 +7267,7 @@ var dataStructureTyped = (() => {
|
|
|
7007
7267
|
/**
|
|
7008
7268
|
* Time Complexity: O(n)
|
|
7009
7269
|
* Space Complexity: O(log n)
|
|
7270
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
7010
7271
|
*/
|
|
7011
7272
|
/**
|
|
7012
7273
|
* Time Complexity: O(n)
|
|
@@ -7022,10 +7283,6 @@ var dataStructureTyped = (() => {
|
|
|
7022
7283
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
7023
7284
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
7024
7285
|
}
|
|
7025
|
-
/**
|
|
7026
|
-
* Time Complexity: O(n)
|
|
7027
|
-
* Space Complexity: O(log n).
|
|
7028
|
-
*/
|
|
7029
7286
|
/**
|
|
7030
7287
|
* Time Complexity: O(n)
|
|
7031
7288
|
* Space Complexity: O(log n).
|
|
@@ -7051,7 +7308,7 @@ var dataStructureTyped = (() => {
|
|
|
7051
7308
|
* traverse the binary tree. It can have two possible values:
|
|
7052
7309
|
* @returns an array of nodes of type `N`.
|
|
7053
7310
|
*/
|
|
7054
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.
|
|
7311
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7055
7312
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7056
7313
|
callback = (node) => node;
|
|
7057
7314
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -7088,10 +7345,6 @@ var dataStructureTyped = (() => {
|
|
|
7088
7345
|
}
|
|
7089
7346
|
return ans;
|
|
7090
7347
|
}
|
|
7091
|
-
/**
|
|
7092
|
-
* Time Complexity: O(n)
|
|
7093
|
-
* Space Complexity: O(log n).
|
|
7094
|
-
*/
|
|
7095
7348
|
/**
|
|
7096
7349
|
* Time Complexity: O(n)
|
|
7097
7350
|
*
|
|
@@ -7112,15 +7365,11 @@ var dataStructureTyped = (() => {
|
|
|
7112
7365
|
* be performed in a pre-order, in-order, or post-order manner.
|
|
7113
7366
|
* @returns a boolean value.
|
|
7114
7367
|
*/
|
|
7115
|
-
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7368
|
+
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7116
7369
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7117
7370
|
callback = (node) => node;
|
|
7118
7371
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
7119
7372
|
}
|
|
7120
|
-
/**
|
|
7121
|
-
* Time Complexity: O(n)
|
|
7122
|
-
* Space Complexity: O(log n)
|
|
7123
|
-
*/
|
|
7124
7373
|
/**
|
|
7125
7374
|
* Time Complexity: O(n)
|
|
7126
7375
|
* Space Complexity: O(log n)
|
|
@@ -7142,7 +7391,7 @@ var dataStructureTyped = (() => {
|
|
|
7142
7391
|
* nodes are visited during the search.
|
|
7143
7392
|
* @returns a value of type `N | null | undefined`.
|
|
7144
7393
|
*/
|
|
7145
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7394
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7146
7395
|
var _a;
|
|
7147
7396
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7148
7397
|
callback = (node) => node;
|
|
@@ -7194,6 +7443,10 @@ var dataStructureTyped = (() => {
|
|
|
7194
7443
|
}
|
|
7195
7444
|
}
|
|
7196
7445
|
}
|
|
7446
|
+
/**
|
|
7447
|
+
* Time Complexity: O(n)
|
|
7448
|
+
* Space Complexity: O(log n)
|
|
7449
|
+
*/
|
|
7197
7450
|
/**
|
|
7198
7451
|
* The function `ensureNotKey` returns the node corresponding to the given key if it is a valid node
|
|
7199
7452
|
* key, otherwise it returns the key itself.
|
|
@@ -7208,10 +7461,6 @@ var dataStructureTyped = (() => {
|
|
|
7208
7461
|
ensureNotKey(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
7209
7462
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
7210
7463
|
}
|
|
7211
|
-
/**
|
|
7212
|
-
* Time Complexity: O(n)
|
|
7213
|
-
* Space Complexity: O(log n)
|
|
7214
|
-
*/
|
|
7215
7464
|
/**
|
|
7216
7465
|
* Time Complexity: O(n)
|
|
7217
7466
|
* Space Complexity: O(log n)
|
|
@@ -7234,12 +7483,16 @@ var dataStructureTyped = (() => {
|
|
|
7234
7483
|
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
7235
7484
|
* found, `undefined` is returned.
|
|
7236
7485
|
*/
|
|
7237
|
-
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7486
|
+
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7238
7487
|
var _a, _b;
|
|
7239
7488
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7240
7489
|
callback = (node) => node;
|
|
7241
7490
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value) != null ? _b : void 0;
|
|
7242
7491
|
}
|
|
7492
|
+
/**
|
|
7493
|
+
* Time Complexity: O(n)
|
|
7494
|
+
* Space Complexity: O(log n)
|
|
7495
|
+
*/
|
|
7243
7496
|
/**
|
|
7244
7497
|
* Clear the binary tree, removing all nodes.
|
|
7245
7498
|
*/
|
|
@@ -7254,10 +7507,6 @@ var dataStructureTyped = (() => {
|
|
|
7254
7507
|
isEmpty() {
|
|
7255
7508
|
return this.size === 0;
|
|
7256
7509
|
}
|
|
7257
|
-
/**
|
|
7258
|
-
* Time Complexity: O(log n)
|
|
7259
|
-
* Space Complexity: O(log n)
|
|
7260
|
-
*/
|
|
7261
7510
|
/**
|
|
7262
7511
|
* Time Complexity: O(log n)
|
|
7263
7512
|
* Space Complexity: O(log n)
|
|
@@ -7286,7 +7535,7 @@ var dataStructureTyped = (() => {
|
|
|
7286
7535
|
}
|
|
7287
7536
|
/**
|
|
7288
7537
|
* Time Complexity: O(log n)
|
|
7289
|
-
* Space Complexity: O(
|
|
7538
|
+
* Space Complexity: O(log n)
|
|
7290
7539
|
*/
|
|
7291
7540
|
/**
|
|
7292
7541
|
* Time Complexity: O(log n)
|
|
@@ -7302,7 +7551,7 @@ var dataStructureTyped = (() => {
|
|
|
7302
7551
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
7303
7552
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
7304
7553
|
*/
|
|
7305
|
-
getLeftMost(beginRoot = this.root, iterationType = this.
|
|
7554
|
+
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7306
7555
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7307
7556
|
if (!beginRoot)
|
|
7308
7557
|
return beginRoot;
|
|
@@ -7341,7 +7590,7 @@ var dataStructureTyped = (() => {
|
|
|
7341
7590
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
7342
7591
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
7343
7592
|
*/
|
|
7344
|
-
getRightMost(beginRoot = this.root, iterationType = this.
|
|
7593
|
+
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7345
7594
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7346
7595
|
if (!beginRoot)
|
|
7347
7596
|
return beginRoot;
|
|
@@ -7362,7 +7611,7 @@ var dataStructureTyped = (() => {
|
|
|
7362
7611
|
}
|
|
7363
7612
|
}
|
|
7364
7613
|
/**
|
|
7365
|
-
* Time Complexity: O(n)
|
|
7614
|
+
* Time Complexity: O(log n)
|
|
7366
7615
|
* Space Complexity: O(1)
|
|
7367
7616
|
*/
|
|
7368
7617
|
/**
|
|
@@ -7377,7 +7626,7 @@ var dataStructureTyped = (() => {
|
|
|
7377
7626
|
* possible values:
|
|
7378
7627
|
* @returns a boolean value.
|
|
7379
7628
|
*/
|
|
7380
|
-
isSubtreeBST(beginRoot, iterationType = this.
|
|
7629
|
+
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
7381
7630
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7382
7631
|
if (!beginRoot)
|
|
7383
7632
|
return true;
|
|
@@ -7422,15 +7671,11 @@ var dataStructureTyped = (() => {
|
|
|
7422
7671
|
* expected to be
|
|
7423
7672
|
* @returns a boolean value.
|
|
7424
7673
|
*/
|
|
7425
|
-
isBST(iterationType = this.
|
|
7674
|
+
isBST(iterationType = this.iterationType) {
|
|
7426
7675
|
if (this.root === null)
|
|
7427
7676
|
return true;
|
|
7428
7677
|
return this.isSubtreeBST(this.root, iterationType);
|
|
7429
7678
|
}
|
|
7430
|
-
/**
|
|
7431
|
-
* Time complexity: O(n)
|
|
7432
|
-
* Space complexity: O(log n)
|
|
7433
|
-
*/
|
|
7434
7679
|
/**
|
|
7435
7680
|
* Time complexity: O(n)
|
|
7436
7681
|
* Space complexity: O(log n)
|
|
@@ -7453,7 +7698,7 @@ var dataStructureTyped = (() => {
|
|
|
7453
7698
|
* the `callback` function on each node in the subtree. The type of the array elements is determined
|
|
7454
7699
|
* by the return type of the `callback` function.
|
|
7455
7700
|
*/
|
|
7456
|
-
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7701
|
+
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7457
7702
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7458
7703
|
const ans = [];
|
|
7459
7704
|
if (!beginRoot)
|
|
@@ -7490,6 +7735,10 @@ var dataStructureTyped = (() => {
|
|
|
7490
7735
|
}
|
|
7491
7736
|
return ans;
|
|
7492
7737
|
}
|
|
7738
|
+
/**
|
|
7739
|
+
* Time complexity: O(n)
|
|
7740
|
+
* Space complexity: O(log n)
|
|
7741
|
+
*/
|
|
7493
7742
|
/**
|
|
7494
7743
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
7495
7744
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -7524,10 +7773,6 @@ var dataStructureTyped = (() => {
|
|
|
7524
7773
|
isNodeKey(potentialKey) {
|
|
7525
7774
|
return typeof potentialKey === "number";
|
|
7526
7775
|
}
|
|
7527
|
-
/**
|
|
7528
|
-
* Time complexity: O(n)
|
|
7529
|
-
* Space complexity: O(n)
|
|
7530
|
-
*/
|
|
7531
7776
|
/**
|
|
7532
7777
|
* Time complexity: O(n)
|
|
7533
7778
|
* Space complexity: O(n)
|
|
@@ -7650,10 +7895,6 @@ var dataStructureTyped = (() => {
|
|
|
7650
7895
|
}
|
|
7651
7896
|
return ans;
|
|
7652
7897
|
}
|
|
7653
|
-
/**
|
|
7654
|
-
* Time complexity: O(n)
|
|
7655
|
-
* Space complexity: O(n)
|
|
7656
|
-
*/
|
|
7657
7898
|
/**
|
|
7658
7899
|
* Time complexity: O(n)
|
|
7659
7900
|
* Space complexity: O(n)
|
|
@@ -7675,7 +7916,7 @@ var dataStructureTyped = (() => {
|
|
|
7675
7916
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
7676
7917
|
* the breadth-first traversal of a binary tree.
|
|
7677
7918
|
*/
|
|
7678
|
-
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7919
|
+
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7679
7920
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7680
7921
|
if (!beginRoot)
|
|
7681
7922
|
return [];
|
|
@@ -7724,10 +7965,6 @@ var dataStructureTyped = (() => {
|
|
|
7724
7965
|
}
|
|
7725
7966
|
return ans;
|
|
7726
7967
|
}
|
|
7727
|
-
/**
|
|
7728
|
-
* Time complexity: O(n)
|
|
7729
|
-
* Space complexity: O(n)
|
|
7730
|
-
*/
|
|
7731
7968
|
/**
|
|
7732
7969
|
* Time complexity: O(n)
|
|
7733
7970
|
* Space complexity: O(n)
|
|
@@ -7749,7 +7986,7 @@ var dataStructureTyped = (() => {
|
|
|
7749
7986
|
* be excluded
|
|
7750
7987
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
7751
7988
|
*/
|
|
7752
|
-
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
7989
|
+
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7753
7990
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
7754
7991
|
const levelsNodes = [];
|
|
7755
7992
|
if (!beginRoot)
|
|
@@ -7837,10 +8074,6 @@ var dataStructureTyped = (() => {
|
|
|
7837
8074
|
}
|
|
7838
8075
|
return y;
|
|
7839
8076
|
}
|
|
7840
|
-
/**
|
|
7841
|
-
* Time complexity: O(n)
|
|
7842
|
-
* Space complexity: O(1)
|
|
7843
|
-
*/
|
|
7844
8077
|
/**
|
|
7845
8078
|
* Time complexity: O(n)
|
|
7846
8079
|
* Space complexity: O(1)
|
|
@@ -7940,6 +8173,10 @@ var dataStructureTyped = (() => {
|
|
|
7940
8173
|
}
|
|
7941
8174
|
return ans;
|
|
7942
8175
|
}
|
|
8176
|
+
/**
|
|
8177
|
+
* Time complexity: O(n)
|
|
8178
|
+
* Space complexity: O(1)
|
|
8179
|
+
*/
|
|
7943
8180
|
/**
|
|
7944
8181
|
* The `forEach` function iterates over each entry in a tree and calls a callback function with the
|
|
7945
8182
|
* entry and the tree as arguments.
|
|
@@ -7968,14 +8205,6 @@ var dataStructureTyped = (() => {
|
|
|
7968
8205
|
}
|
|
7969
8206
|
return newTree;
|
|
7970
8207
|
}
|
|
7971
|
-
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
7972
|
-
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
7973
|
-
// const newTree = this.createTree();
|
|
7974
|
-
// for (const [key, value] of this) {
|
|
7975
|
-
// newTree.add(key, callback([key, value], this));
|
|
7976
|
-
// }
|
|
7977
|
-
// return newTree;
|
|
7978
|
-
// }
|
|
7979
8208
|
/**
|
|
7980
8209
|
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
7981
8210
|
* tree.
|
|
@@ -7989,6 +8218,14 @@ var dataStructureTyped = (() => {
|
|
|
7989
8218
|
}
|
|
7990
8219
|
return newTree;
|
|
7991
8220
|
}
|
|
8221
|
+
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
8222
|
+
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
8223
|
+
// const newTree = this.createTree();
|
|
8224
|
+
// for (const [key, value] of this) {
|
|
8225
|
+
// newTree.add(key, callback([key, value], this));
|
|
8226
|
+
// }
|
|
8227
|
+
// return newTree;
|
|
8228
|
+
// }
|
|
7992
8229
|
/**
|
|
7993
8230
|
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
7994
8231
|
* entry, accumulating a single value.
|
|
@@ -8020,7 +8257,7 @@ var dataStructureTyped = (() => {
|
|
|
8020
8257
|
*[Symbol.iterator](node = this.root) {
|
|
8021
8258
|
if (!node)
|
|
8022
8259
|
return;
|
|
8023
|
-
if (this.
|
|
8260
|
+
if (this.iterationType === "ITERATIVE" /* ITERATIVE */) {
|
|
8024
8261
|
const stack = [];
|
|
8025
8262
|
let current = node;
|
|
8026
8263
|
while (current || stack.length > 0) {
|
|
@@ -8073,6 +8310,18 @@ var dataStructureTyped = (() => {
|
|
|
8073
8310
|
};
|
|
8074
8311
|
display(beginRoot);
|
|
8075
8312
|
}
|
|
8313
|
+
init(elements) {
|
|
8314
|
+
if (elements) {
|
|
8315
|
+
for (const entryOrKey of elements) {
|
|
8316
|
+
if (Array.isArray(entryOrKey)) {
|
|
8317
|
+
const [key, value] = entryOrKey;
|
|
8318
|
+
this.add(key, value);
|
|
8319
|
+
} else {
|
|
8320
|
+
this.add(entryOrKey);
|
|
8321
|
+
}
|
|
8322
|
+
}
|
|
8323
|
+
}
|
|
8324
|
+
}
|
|
8076
8325
|
_displayAux(node, options) {
|
|
8077
8326
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
8078
8327
|
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
@@ -8223,16 +8472,19 @@ var dataStructureTyped = (() => {
|
|
|
8223
8472
|
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
8224
8473
|
* for the binary search tree.
|
|
8225
8474
|
*/
|
|
8226
|
-
constructor(options) {
|
|
8227
|
-
super(options);
|
|
8228
|
-
__publicField(this, "options");
|
|
8475
|
+
constructor(elements, options) {
|
|
8476
|
+
super([], options);
|
|
8229
8477
|
__publicField(this, "_root");
|
|
8478
|
+
__publicField(this, "comparator", (a, b) => a - b);
|
|
8230
8479
|
if (options) {
|
|
8231
|
-
|
|
8232
|
-
|
|
8233
|
-
|
|
8480
|
+
const { comparator } = options;
|
|
8481
|
+
if (comparator) {
|
|
8482
|
+
this.comparator = comparator;
|
|
8483
|
+
}
|
|
8234
8484
|
}
|
|
8235
8485
|
this._root = void 0;
|
|
8486
|
+
if (elements)
|
|
8487
|
+
this.init(elements);
|
|
8236
8488
|
}
|
|
8237
8489
|
/**
|
|
8238
8490
|
* Get the root node of the binary tree.
|
|
@@ -8252,12 +8504,11 @@ var dataStructureTyped = (() => {
|
|
|
8252
8504
|
return new BSTNode(key, value);
|
|
8253
8505
|
}
|
|
8254
8506
|
createTree(options) {
|
|
8255
|
-
return new _BST(__spreadValues(
|
|
8507
|
+
return new _BST([], __spreadValues({
|
|
8508
|
+
iterationType: this.iterationType,
|
|
8509
|
+
comparator: this.comparator
|
|
8510
|
+
}, options));
|
|
8256
8511
|
}
|
|
8257
|
-
/**
|
|
8258
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8259
|
-
* Space Complexity: O(1) - Constant space is used.
|
|
8260
|
-
*/
|
|
8261
8512
|
/**
|
|
8262
8513
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8263
8514
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -8332,8 +8583,8 @@ var dataStructureTyped = (() => {
|
|
|
8332
8583
|
return inserted;
|
|
8333
8584
|
}
|
|
8334
8585
|
/**
|
|
8335
|
-
* Time Complexity: O(
|
|
8336
|
-
* Space Complexity: O(
|
|
8586
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8587
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
8337
8588
|
*/
|
|
8338
8589
|
/**
|
|
8339
8590
|
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
@@ -8355,7 +8606,7 @@ var dataStructureTyped = (() => {
|
|
|
8355
8606
|
* current instance of the binary search tree
|
|
8356
8607
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
8357
8608
|
*/
|
|
8358
|
-
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.
|
|
8609
|
+
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
8359
8610
|
function hasNoUndefined(arr) {
|
|
8360
8611
|
return arr.indexOf(void 0) === -1;
|
|
8361
8612
|
}
|
|
@@ -8423,8 +8674,8 @@ var dataStructureTyped = (() => {
|
|
|
8423
8674
|
return inserted;
|
|
8424
8675
|
}
|
|
8425
8676
|
/**
|
|
8426
|
-
* Time Complexity: O(log n) -
|
|
8427
|
-
* Space Complexity: O(
|
|
8677
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
8678
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
8428
8679
|
*/
|
|
8429
8680
|
/**
|
|
8430
8681
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -8441,7 +8692,7 @@ var dataStructureTyped = (() => {
|
|
|
8441
8692
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
8442
8693
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
8443
8694
|
*/
|
|
8444
|
-
lastKey(beginRoot = this.root, iterationType = this.
|
|
8695
|
+
lastKey(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8445
8696
|
var _a, _b, _c, _d, _e, _f;
|
|
8446
8697
|
if (this._compare(0, 1) === "lt" /* lt */)
|
|
8447
8698
|
return (_b = (_a = this.getRightMost(beginRoot, iterationType)) == null ? void 0 : _a.key) != null ? _b : 0;
|
|
@@ -8452,7 +8703,7 @@ var dataStructureTyped = (() => {
|
|
|
8452
8703
|
}
|
|
8453
8704
|
/**
|
|
8454
8705
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
8455
|
-
* Space Complexity: O(
|
|
8706
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
8456
8707
|
*/
|
|
8457
8708
|
/**
|
|
8458
8709
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -8498,6 +8749,10 @@ var dataStructureTyped = (() => {
|
|
|
8498
8749
|
}
|
|
8499
8750
|
}
|
|
8500
8751
|
}
|
|
8752
|
+
/**
|
|
8753
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
8754
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
8755
|
+
*/
|
|
8501
8756
|
/**
|
|
8502
8757
|
* The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
|
|
8503
8758
|
* otherwise it returns the key itself.
|
|
@@ -8510,10 +8765,6 @@ var dataStructureTyped = (() => {
|
|
|
8510
8765
|
ensureNotKey(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
8511
8766
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
8512
8767
|
}
|
|
8513
|
-
/**
|
|
8514
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
8515
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
8516
|
-
*/
|
|
8517
8768
|
/**
|
|
8518
8769
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
8519
8770
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -8537,7 +8788,7 @@ var dataStructureTyped = (() => {
|
|
|
8537
8788
|
* performed on the binary tree. It can have two possible values:
|
|
8538
8789
|
* @returns The method returns an array of nodes (`N[]`).
|
|
8539
8790
|
*/
|
|
8540
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.
|
|
8791
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8541
8792
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
8542
8793
|
if (!beginRoot)
|
|
8543
8794
|
return [];
|
|
@@ -8613,7 +8864,7 @@ var dataStructureTyped = (() => {
|
|
|
8613
8864
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
8614
8865
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
8615
8866
|
*/
|
|
8616
|
-
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = "lt" /* lt */, targetNode = this.root, iterationType = this.
|
|
8867
|
+
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = "lt" /* lt */, targetNode = this.root, iterationType = this.iterationType) {
|
|
8617
8868
|
targetNode = this.ensureNotKey(targetNode);
|
|
8618
8869
|
const ans = [];
|
|
8619
8870
|
if (!targetNode)
|
|
@@ -8653,17 +8904,8 @@ var dataStructureTyped = (() => {
|
|
|
8653
8904
|
}
|
|
8654
8905
|
}
|
|
8655
8906
|
/**
|
|
8656
|
-
*
|
|
8657
|
-
*
|
|
8658
|
-
* 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.
|
|
8659
|
-
*
|
|
8660
|
-
* Use Cases and Efficiency:
|
|
8661
|
-
* 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.
|
|
8662
|
-
* 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).
|
|
8663
|
-
*/
|
|
8664
|
-
/**
|
|
8665
|
-
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
8666
|
-
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
8907
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
8908
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
8667
8909
|
*/
|
|
8668
8910
|
/**
|
|
8669
8911
|
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
@@ -8676,7 +8918,7 @@ var dataStructureTyped = (() => {
|
|
|
8676
8918
|
* values:
|
|
8677
8919
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
8678
8920
|
*/
|
|
8679
|
-
perfectlyBalance(iterationType = this.
|
|
8921
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
8680
8922
|
const sorted = this.dfs((node) => node, "in"), n = sorted.length;
|
|
8681
8923
|
this.clear();
|
|
8682
8924
|
if (sorted.length < 1)
|
|
@@ -8713,8 +8955,17 @@ var dataStructureTyped = (() => {
|
|
|
8713
8955
|
}
|
|
8714
8956
|
}
|
|
8715
8957
|
/**
|
|
8716
|
-
*
|
|
8717
|
-
*
|
|
8958
|
+
* Balancing Adjustment:
|
|
8959
|
+
* 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.
|
|
8960
|
+
* 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.
|
|
8961
|
+
*
|
|
8962
|
+
* Use Cases and Efficiency:
|
|
8963
|
+
* 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.
|
|
8964
|
+
* 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).
|
|
8965
|
+
*/
|
|
8966
|
+
/**
|
|
8967
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
8968
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
8718
8969
|
*/
|
|
8719
8970
|
/**
|
|
8720
8971
|
* Time Complexity: O(n) - Visiting each node once.
|
|
@@ -8725,7 +8976,7 @@ var dataStructureTyped = (() => {
|
|
|
8725
8976
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
8726
8977
|
* @returns a boolean value.
|
|
8727
8978
|
*/
|
|
8728
|
-
isAVLBalanced(iterationType = this.
|
|
8979
|
+
isAVLBalanced(iterationType = this.iterationType) {
|
|
8729
8980
|
var _a, _b;
|
|
8730
8981
|
if (!this.root)
|
|
8731
8982
|
return true;
|
|
@@ -8768,6 +9019,22 @@ var dataStructureTyped = (() => {
|
|
|
8768
9019
|
}
|
|
8769
9020
|
return balanced;
|
|
8770
9021
|
}
|
|
9022
|
+
/**
|
|
9023
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
9024
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
9025
|
+
*/
|
|
9026
|
+
init(elements) {
|
|
9027
|
+
if (elements) {
|
|
9028
|
+
for (const entryOrKey of elements) {
|
|
9029
|
+
if (Array.isArray(entryOrKey)) {
|
|
9030
|
+
const [key, value] = entryOrKey;
|
|
9031
|
+
this.add(key, value);
|
|
9032
|
+
} else {
|
|
9033
|
+
this.add(entryOrKey);
|
|
9034
|
+
}
|
|
9035
|
+
}
|
|
9036
|
+
}
|
|
9037
|
+
}
|
|
8771
9038
|
_setRoot(v) {
|
|
8772
9039
|
if (v) {
|
|
8773
9040
|
v.parent = void 0;
|
|
@@ -8783,7 +9050,7 @@ var dataStructureTyped = (() => {
|
|
|
8783
9050
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
8784
9051
|
*/
|
|
8785
9052
|
_compare(a, b) {
|
|
8786
|
-
const compared = this.
|
|
9053
|
+
const compared = this.comparator(a, b);
|
|
8787
9054
|
if (compared > 0)
|
|
8788
9055
|
return "gt" /* gt */;
|
|
8789
9056
|
else if (compared < 0)
|
|
@@ -9234,14 +9501,10 @@ var dataStructureTyped = (() => {
|
|
|
9234
9501
|
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
9235
9502
|
* options.
|
|
9236
9503
|
*/
|
|
9237
|
-
constructor(options) {
|
|
9238
|
-
super(options);
|
|
9239
|
-
|
|
9240
|
-
|
|
9241
|
-
this.options = __spreadValues({ iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b }, options);
|
|
9242
|
-
} else {
|
|
9243
|
-
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b };
|
|
9244
|
-
}
|
|
9504
|
+
constructor(elements, options) {
|
|
9505
|
+
super([], options);
|
|
9506
|
+
if (elements)
|
|
9507
|
+
this.init(elements);
|
|
9245
9508
|
}
|
|
9246
9509
|
/**
|
|
9247
9510
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -9256,12 +9519,11 @@ var dataStructureTyped = (() => {
|
|
|
9256
9519
|
return new AVLTreeNode(key, value);
|
|
9257
9520
|
}
|
|
9258
9521
|
createTree(options) {
|
|
9259
|
-
return new _AVLTree(__spreadValues(
|
|
9522
|
+
return new _AVLTree([], __spreadValues({
|
|
9523
|
+
iterationType: this.iterationType,
|
|
9524
|
+
comparator: this.comparator
|
|
9525
|
+
}, options));
|
|
9260
9526
|
}
|
|
9261
|
-
/**
|
|
9262
|
-
* 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.
|
|
9263
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9264
|
-
*/
|
|
9265
9527
|
/**
|
|
9266
9528
|
* 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.
|
|
9267
9529
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -9283,7 +9545,7 @@ var dataStructureTyped = (() => {
|
|
|
9283
9545
|
return inserted;
|
|
9284
9546
|
}
|
|
9285
9547
|
/**
|
|
9286
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The
|
|
9548
|
+
* 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.
|
|
9287
9549
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9288
9550
|
*/
|
|
9289
9551
|
/**
|
|
@@ -9312,6 +9574,22 @@ var dataStructureTyped = (() => {
|
|
|
9312
9574
|
}
|
|
9313
9575
|
return deletedResults;
|
|
9314
9576
|
}
|
|
9577
|
+
/**
|
|
9578
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
|
|
9579
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9580
|
+
*/
|
|
9581
|
+
init(elements) {
|
|
9582
|
+
if (elements) {
|
|
9583
|
+
for (const entryOrKey of elements) {
|
|
9584
|
+
if (Array.isArray(entryOrKey)) {
|
|
9585
|
+
const [key, value] = entryOrKey;
|
|
9586
|
+
this.add(key, value);
|
|
9587
|
+
} else {
|
|
9588
|
+
this.add(entryOrKey);
|
|
9589
|
+
}
|
|
9590
|
+
}
|
|
9591
|
+
}
|
|
9592
|
+
}
|
|
9315
9593
|
/**
|
|
9316
9594
|
* The `_swap` function swaps the key, value, and height properties between two nodes in a binary
|
|
9317
9595
|
* tree.
|
|
@@ -9627,18 +9905,14 @@ var dataStructureTyped = (() => {
|
|
|
9627
9905
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
9628
9906
|
* passed to the constructor. It is used to configure the RBTree object with specific options.
|
|
9629
9907
|
*/
|
|
9630
|
-
constructor(options) {
|
|
9631
|
-
super(options);
|
|
9908
|
+
constructor(elements, options) {
|
|
9909
|
+
super([], options);
|
|
9632
9910
|
__publicField(this, "Sentinel", new RedBlackTreeNode(NaN));
|
|
9633
|
-
__publicField(this, "options");
|
|
9634
9911
|
__publicField(this, "_root");
|
|
9635
9912
|
__publicField(this, "_size", 0);
|
|
9636
|
-
if (options) {
|
|
9637
|
-
this.options = __spreadValues({ iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b }, options);
|
|
9638
|
-
} else {
|
|
9639
|
-
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b };
|
|
9640
|
-
}
|
|
9641
9913
|
this._root = this.Sentinel;
|
|
9914
|
+
if (elements)
|
|
9915
|
+
this.init(elements);
|
|
9642
9916
|
}
|
|
9643
9917
|
get root() {
|
|
9644
9918
|
return this._root;
|
|
@@ -9650,12 +9924,11 @@ var dataStructureTyped = (() => {
|
|
|
9650
9924
|
return new RedBlackTreeNode(key, value, color);
|
|
9651
9925
|
}
|
|
9652
9926
|
createTree(options) {
|
|
9653
|
-
return new _RedBlackTree(__spreadValues(
|
|
9927
|
+
return new _RedBlackTree([], __spreadValues({
|
|
9928
|
+
iterationType: this.iterationType,
|
|
9929
|
+
comparator: this.comparator
|
|
9930
|
+
}, options));
|
|
9654
9931
|
}
|
|
9655
|
-
/**
|
|
9656
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
9657
|
-
* Space Complexity: O(1)
|
|
9658
|
-
*/
|
|
9659
9932
|
/**
|
|
9660
9933
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
9661
9934
|
* Space Complexity: O(1)
|
|
@@ -9788,13 +10061,13 @@ var dataStructureTyped = (() => {
|
|
|
9788
10061
|
helper(this.root);
|
|
9789
10062
|
return ans;
|
|
9790
10063
|
}
|
|
9791
|
-
isRealNode(node) {
|
|
9792
|
-
return node !== this.Sentinel && node !== void 0;
|
|
9793
|
-
}
|
|
9794
10064
|
/**
|
|
9795
10065
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
9796
10066
|
* Space Complexity: O(1)
|
|
9797
10067
|
*/
|
|
10068
|
+
isRealNode(node) {
|
|
10069
|
+
return node !== this.Sentinel && node !== void 0;
|
|
10070
|
+
}
|
|
9798
10071
|
/**
|
|
9799
10072
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
9800
10073
|
* Space Complexity: O(1)
|
|
@@ -9816,7 +10089,7 @@ var dataStructureTyped = (() => {
|
|
|
9816
10089
|
* `getNodes` method, which is called within the `getNode` method.
|
|
9817
10090
|
* @returns a value of type `N`, `null`, or `undefined`.
|
|
9818
10091
|
*/
|
|
9819
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
10092
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9820
10093
|
var _a;
|
|
9821
10094
|
if (identifier instanceof BinaryTreeNode)
|
|
9822
10095
|
callback = (node) => node;
|
|
@@ -9871,10 +10144,26 @@ var dataStructureTyped = (() => {
|
|
|
9871
10144
|
}
|
|
9872
10145
|
return y;
|
|
9873
10146
|
}
|
|
10147
|
+
/**
|
|
10148
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10149
|
+
* Space Complexity: O(1)
|
|
10150
|
+
*/
|
|
9874
10151
|
clear() {
|
|
9875
10152
|
this._root = this.Sentinel;
|
|
9876
10153
|
this._size = 0;
|
|
9877
10154
|
}
|
|
10155
|
+
init(elements) {
|
|
10156
|
+
if (elements) {
|
|
10157
|
+
for (const entryOrKey of elements) {
|
|
10158
|
+
if (Array.isArray(entryOrKey)) {
|
|
10159
|
+
const [key, value] = entryOrKey;
|
|
10160
|
+
this.add(key, value);
|
|
10161
|
+
} else {
|
|
10162
|
+
this.add(entryOrKey);
|
|
10163
|
+
}
|
|
10164
|
+
}
|
|
10165
|
+
}
|
|
10166
|
+
}
|
|
9878
10167
|
_setRoot(v) {
|
|
9879
10168
|
if (v) {
|
|
9880
10169
|
v.parent = void 0;
|
|
@@ -10119,15 +10408,11 @@ var dataStructureTyped = (() => {
|
|
|
10119
10408
|
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
10120
10409
|
* TreeMultimap.
|
|
10121
10410
|
*/
|
|
10122
|
-
constructor(options
|
|
10123
|
-
super(options);
|
|
10124
|
-
__publicField(this, "options");
|
|
10411
|
+
constructor(elements, options) {
|
|
10412
|
+
super([], options);
|
|
10125
10413
|
__publicField(this, "_count", 0);
|
|
10126
|
-
if (
|
|
10127
|
-
this.
|
|
10128
|
-
} else {
|
|
10129
|
-
this.options = { iterationType: "ITERATIVE" /* ITERATIVE */, comparator: (a, b) => a - b };
|
|
10130
|
-
}
|
|
10414
|
+
if (elements)
|
|
10415
|
+
this.init(elements);
|
|
10131
10416
|
}
|
|
10132
10417
|
get count() {
|
|
10133
10418
|
return this._count;
|
|
@@ -10145,12 +10430,11 @@ var dataStructureTyped = (() => {
|
|
|
10145
10430
|
return new TreeMultimapNode(key, value, count);
|
|
10146
10431
|
}
|
|
10147
10432
|
createTree(options) {
|
|
10148
|
-
return new _TreeMultimap(__spreadValues(
|
|
10433
|
+
return new _TreeMultimap([], __spreadValues({
|
|
10434
|
+
iterationType: this.iterationType,
|
|
10435
|
+
comparator: this.comparator
|
|
10436
|
+
}, options));
|
|
10149
10437
|
}
|
|
10150
|
-
/**
|
|
10151
|
-
* 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.
|
|
10152
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10153
|
-
*/
|
|
10154
10438
|
/**
|
|
10155
10439
|
* 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.
|
|
10156
10440
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -10229,8 +10513,8 @@ var dataStructureTyped = (() => {
|
|
|
10229
10513
|
return inserted;
|
|
10230
10514
|
}
|
|
10231
10515
|
/**
|
|
10232
|
-
* Time Complexity: O(
|
|
10233
|
-
* Space Complexity: O(1) - constant space, as it
|
|
10516
|
+
* 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.
|
|
10517
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10234
10518
|
*/
|
|
10235
10519
|
/**
|
|
10236
10520
|
* 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.
|
|
@@ -10262,8 +10546,8 @@ var dataStructureTyped = (() => {
|
|
|
10262
10546
|
return inserted;
|
|
10263
10547
|
}
|
|
10264
10548
|
/**
|
|
10265
|
-
* Time Complexity: O(
|
|
10266
|
-
* Space Complexity: O(1) - constant space, as it
|
|
10549
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
10550
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
10267
10551
|
*/
|
|
10268
10552
|
/**
|
|
10269
10553
|
* 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.
|
|
@@ -10276,7 +10560,7 @@ var dataStructureTyped = (() => {
|
|
|
10276
10560
|
* values:
|
|
10277
10561
|
* @returns a boolean value.
|
|
10278
10562
|
*/
|
|
10279
|
-
perfectlyBalance(iterationType = this.
|
|
10563
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
10280
10564
|
const sorted = this.dfs((node) => node, "in"), n = sorted.length;
|
|
10281
10565
|
if (sorted.length < 1)
|
|
10282
10566
|
return false;
|
|
@@ -10312,8 +10596,8 @@ var dataStructureTyped = (() => {
|
|
|
10312
10596
|
}
|
|
10313
10597
|
}
|
|
10314
10598
|
/**
|
|
10315
|
-
* Time Complexity: O(
|
|
10316
|
-
* Space Complexity: O(
|
|
10599
|
+
* 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.
|
|
10600
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10317
10601
|
*/
|
|
10318
10602
|
/**
|
|
10319
10603
|
* 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.
|
|
@@ -10387,8 +10671,8 @@ var dataStructureTyped = (() => {
|
|
|
10387
10671
|
return deletedResult;
|
|
10388
10672
|
}
|
|
10389
10673
|
/**
|
|
10390
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree.
|
|
10391
|
-
* Space Complexity: O(
|
|
10674
|
+
* 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.
|
|
10675
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
10392
10676
|
*/
|
|
10393
10677
|
/**
|
|
10394
10678
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
@@ -10397,6 +10681,22 @@ var dataStructureTyped = (() => {
|
|
|
10397
10681
|
super.clear();
|
|
10398
10682
|
this._count = 0;
|
|
10399
10683
|
}
|
|
10684
|
+
/**
|
|
10685
|
+
* 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.
|
|
10686
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10687
|
+
*/
|
|
10688
|
+
init(elements) {
|
|
10689
|
+
if (elements) {
|
|
10690
|
+
for (const entryOrKey of elements) {
|
|
10691
|
+
if (Array.isArray(entryOrKey)) {
|
|
10692
|
+
const [key, value] = entryOrKey;
|
|
10693
|
+
this.add(key, value);
|
|
10694
|
+
} else {
|
|
10695
|
+
this.add(entryOrKey);
|
|
10696
|
+
}
|
|
10697
|
+
}
|
|
10698
|
+
}
|
|
10699
|
+
}
|
|
10400
10700
|
/**
|
|
10401
10701
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
10402
10702
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -11114,7 +11414,7 @@ var dataStructureTyped = (() => {
|
|
|
11114
11414
|
this.children = /* @__PURE__ */ new Map();
|
|
11115
11415
|
}
|
|
11116
11416
|
};
|
|
11117
|
-
var Trie = class {
|
|
11417
|
+
var Trie = class _Trie {
|
|
11118
11418
|
constructor(words, caseSensitive = true) {
|
|
11119
11419
|
__publicField(this, "_caseSensitive");
|
|
11120
11420
|
__publicField(this, "_root");
|
|
@@ -11397,6 +11697,53 @@ var dataStructureTyped = (() => {
|
|
|
11397
11697
|
dfs(startNode, prefix);
|
|
11398
11698
|
return words;
|
|
11399
11699
|
}
|
|
11700
|
+
*[Symbol.iterator]() {
|
|
11701
|
+
function* _dfs(node, path) {
|
|
11702
|
+
if (node.isEnd) {
|
|
11703
|
+
yield path;
|
|
11704
|
+
}
|
|
11705
|
+
for (const [char, childNode] of node.children) {
|
|
11706
|
+
yield* __yieldStar(_dfs(childNode, path + char));
|
|
11707
|
+
}
|
|
11708
|
+
}
|
|
11709
|
+
yield* __yieldStar(_dfs(this.root, ""));
|
|
11710
|
+
}
|
|
11711
|
+
forEach(callback) {
|
|
11712
|
+
let index = 0;
|
|
11713
|
+
for (const word of this) {
|
|
11714
|
+
callback(word, index, this);
|
|
11715
|
+
index++;
|
|
11716
|
+
}
|
|
11717
|
+
}
|
|
11718
|
+
filter(predicate) {
|
|
11719
|
+
const results = [];
|
|
11720
|
+
let index = 0;
|
|
11721
|
+
for (const word of this) {
|
|
11722
|
+
if (predicate(word, index, this)) {
|
|
11723
|
+
results.push(word);
|
|
11724
|
+
}
|
|
11725
|
+
index++;
|
|
11726
|
+
}
|
|
11727
|
+
return results;
|
|
11728
|
+
}
|
|
11729
|
+
map(callback) {
|
|
11730
|
+
const newTrie = new _Trie();
|
|
11731
|
+
let index = 0;
|
|
11732
|
+
for (const word of this) {
|
|
11733
|
+
newTrie.add(callback(word, index, this));
|
|
11734
|
+
index++;
|
|
11735
|
+
}
|
|
11736
|
+
return newTrie;
|
|
11737
|
+
}
|
|
11738
|
+
reduce(callback, initialValue) {
|
|
11739
|
+
let accumulator = initialValue;
|
|
11740
|
+
let index = 0;
|
|
11741
|
+
for (const word of this) {
|
|
11742
|
+
accumulator = callback(accumulator, word, index, this);
|
|
11743
|
+
index++;
|
|
11744
|
+
}
|
|
11745
|
+
return accumulator;
|
|
11746
|
+
}
|
|
11400
11747
|
/**
|
|
11401
11748
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
11402
11749
|
* Space Complexity: O(1) - Constant space.
|