data-structure-typed 1.50.1 → 1.50.2
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 +2 -1
- package/README.md +26 -26
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +153 -159
- package/dist/cjs/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/cjs/data-structures/binary-tree/bst.js +30 -71
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +26 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js +25 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/cjs/data-structures/hash/hash-map.js +160 -25
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +66 -6
- package/dist/cjs/data-structures/heap/heap.js +66 -6
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +70 -64
- 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 +128 -103
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +49 -19
- package/dist/cjs/data-structures/queue/deque.js +101 -47
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +39 -5
- package/dist/cjs/data-structures/queue/queue.js +47 -5
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +16 -0
- package/dist/cjs/data-structures/stack/stack.js +22 -0
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +38 -1
- package/dist/cjs/data-structures/trie/trie.js +41 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/mjs/data-structures/binary-tree/bst.js +30 -71
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +26 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/mjs/data-structures/graph/undirected-graph.js +25 -0
- package/dist/mjs/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/mjs/data-structures/hash/hash-map.js +160 -25
- package/dist/mjs/data-structures/heap/heap.d.ts +66 -6
- package/dist/mjs/data-structures/heap/heap.js +66 -6
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/queue/deque.d.ts +49 -19
- package/dist/mjs/data-structures/queue/deque.js +101 -47
- package/dist/mjs/data-structures/queue/queue.d.ts +39 -5
- package/dist/mjs/data-structures/queue/queue.js +47 -5
- package/dist/mjs/data-structures/stack/stack.d.ts +16 -0
- package/dist/mjs/data-structures/stack/stack.js +22 -0
- package/dist/mjs/data-structures/trie/trie.d.ts +38 -1
- package/dist/mjs/data-structures/trie/trie.js +41 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +1730 -1042
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +8 -3
- package/src/data-structures/base/iterable-base.ts +172 -19
- package/src/data-structures/binary-tree/avl-tree.ts +97 -97
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +89 -131
- package/src/data-structures/binary-tree/rb-tree.ts +127 -155
- package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +28 -0
- package/src/data-structures/hash/hash-map.ts +175 -34
- package/src/data-structures/heap/heap.ts +66 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
- package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/queue/deque.ts +108 -49
- package/src/data-structures/queue/queue.ts +51 -5
- package/src/data-structures/stack/stack.ts +24 -0
- package/src/data-structures/trie/trie.ts +45 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -3
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +8 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +249 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +65 -5
- package/test/unit/data-structures/queue/queue.test.ts +22 -5
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
|
@@ -317,6 +317,100 @@ var dataStructureTyped = (() => {
|
|
|
317
317
|
callbackfn.call(thisArg, value, key, index++, this);
|
|
318
318
|
}
|
|
319
319
|
}
|
|
320
|
+
/**
|
|
321
|
+
* Time Complexity: O(n)
|
|
322
|
+
* Space Complexity: O(1)
|
|
323
|
+
*/
|
|
324
|
+
/**
|
|
325
|
+
* Time Complexity: O(n)
|
|
326
|
+
* Space Complexity: O(1)
|
|
327
|
+
*
|
|
328
|
+
* The `find` function iterates over the entries of a collection and returns the first value for
|
|
329
|
+
* which the callback function returns true.
|
|
330
|
+
* @param callbackfn - The callback function that will be called for each entry in the collection. It
|
|
331
|
+
* takes three arguments: the value of the entry, the key of the entry, and the index of the entry in
|
|
332
|
+
* the collection. It should return a boolean value indicating whether the current entry matches the
|
|
333
|
+
* desired condition.
|
|
334
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
335
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
336
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
337
|
+
* @returns The method `find` returns the value of the first element in the iterable that satisfies
|
|
338
|
+
* the provided callback function. If no element satisfies the callback function, `undefined` is
|
|
339
|
+
* returned.
|
|
340
|
+
*/
|
|
341
|
+
find(callbackfn, thisArg) {
|
|
342
|
+
let index = 0;
|
|
343
|
+
for (const item of this) {
|
|
344
|
+
const [key, value] = item;
|
|
345
|
+
if (callbackfn.call(thisArg, value, key, index++, this))
|
|
346
|
+
return item;
|
|
347
|
+
}
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Time Complexity: O(n)
|
|
352
|
+
* Space Complexity: O(1)
|
|
353
|
+
*/
|
|
354
|
+
/**
|
|
355
|
+
* Time Complexity: O(n)
|
|
356
|
+
* Space Complexity: O(1)
|
|
357
|
+
*
|
|
358
|
+
* The function checks if a given key exists in a collection.
|
|
359
|
+
* @param {K} key - The parameter "key" is of type K, which means it can be any type. It represents
|
|
360
|
+
* the key that we want to check for existence in the data structure.
|
|
361
|
+
* @returns a boolean value. It returns true if the key is found in the collection, and false
|
|
362
|
+
* otherwise.
|
|
363
|
+
*/
|
|
364
|
+
has(key) {
|
|
365
|
+
for (const item of this) {
|
|
366
|
+
const [itemKey] = item;
|
|
367
|
+
if (itemKey === key)
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Time Complexity: O(n)
|
|
374
|
+
* Space Complexity: O(1)
|
|
375
|
+
*/
|
|
376
|
+
/**
|
|
377
|
+
* Time Complexity: O(n)
|
|
378
|
+
* Space Complexity: O(1)
|
|
379
|
+
*
|
|
380
|
+
* The function checks if a given value exists in a collection.
|
|
381
|
+
* @param {V} value - The parameter "value" is the value that we want to check if it exists in the
|
|
382
|
+
* collection.
|
|
383
|
+
* @returns a boolean value, either true or false.
|
|
384
|
+
*/
|
|
385
|
+
hasValue(value) {
|
|
386
|
+
for (const [, elementValue] of this) {
|
|
387
|
+
if (elementValue === value)
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Time Complexity: O(n)
|
|
394
|
+
* Space Complexity: O(1)
|
|
395
|
+
*/
|
|
396
|
+
/**
|
|
397
|
+
* Time Complexity: O(n)
|
|
398
|
+
* Space Complexity: O(1)
|
|
399
|
+
*
|
|
400
|
+
* The `get` function retrieves the value associated with a given key from a collection.
|
|
401
|
+
* @param {K} key - K (the type of the key) - This parameter represents the key that is being
|
|
402
|
+
* searched for in the collection.
|
|
403
|
+
* @returns The `get` method returns the value associated with the specified key if it exists in the
|
|
404
|
+
* collection, otherwise it returns `undefined`.
|
|
405
|
+
*/
|
|
406
|
+
get(key) {
|
|
407
|
+
for (const item of this) {
|
|
408
|
+
const [itemKey, value] = item;
|
|
409
|
+
if (itemKey === key)
|
|
410
|
+
return value;
|
|
411
|
+
}
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
320
414
|
/**
|
|
321
415
|
* Time Complexity: O(n)
|
|
322
416
|
* Space Complexity: O(1)
|
|
@@ -346,13 +440,6 @@ var dataStructureTyped = (() => {
|
|
|
346
440
|
}
|
|
347
441
|
return accumulator;
|
|
348
442
|
}
|
|
349
|
-
hasValue(value) {
|
|
350
|
-
for (const [, elementValue] of this) {
|
|
351
|
-
if (elementValue === value)
|
|
352
|
-
return true;
|
|
353
|
-
}
|
|
354
|
-
return false;
|
|
355
|
-
}
|
|
356
443
|
/**
|
|
357
444
|
* Time Complexity: O(n)
|
|
358
445
|
* Space Complexity: O(n)
|
|
@@ -470,6 +557,55 @@ var dataStructureTyped = (() => {
|
|
|
470
557
|
callbackfn.call(thisArg, item, index++, this);
|
|
471
558
|
}
|
|
472
559
|
}
|
|
560
|
+
/**
|
|
561
|
+
* Time Complexity: O(n)
|
|
562
|
+
* Space Complexity: O(1)
|
|
563
|
+
*/
|
|
564
|
+
/**
|
|
565
|
+
* Time Complexity: O(n)
|
|
566
|
+
* Space Complexity: O(1)
|
|
567
|
+
*
|
|
568
|
+
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
569
|
+
* element that satisfies the provided callback function.
|
|
570
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
571
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
572
|
+
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
573
|
+
* current element matches the desired condition.
|
|
574
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
575
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
576
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
577
|
+
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
578
|
+
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
579
|
+
*/
|
|
580
|
+
find(callbackfn, thisArg) {
|
|
581
|
+
let index = 0;
|
|
582
|
+
for (const item of this) {
|
|
583
|
+
if (callbackfn.call(thisArg, item, index++, this))
|
|
584
|
+
return item;
|
|
585
|
+
}
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Time Complexity: O(n)
|
|
590
|
+
* Space Complexity: O(1)
|
|
591
|
+
*/
|
|
592
|
+
/**
|
|
593
|
+
* Time Complexity: O(n)
|
|
594
|
+
* Space Complexity: O(1)
|
|
595
|
+
*
|
|
596
|
+
* The function checks if a given element exists in a collection.
|
|
597
|
+
* @param {E} element - The parameter "element" is of type E, which means it can be any type. It
|
|
598
|
+
* represents the element that we want to check for existence in the collection.
|
|
599
|
+
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
|
600
|
+
* otherwise.
|
|
601
|
+
*/
|
|
602
|
+
has(element) {
|
|
603
|
+
for (const ele of this) {
|
|
604
|
+
if (ele === element)
|
|
605
|
+
return true;
|
|
606
|
+
}
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
473
609
|
/**
|
|
474
610
|
* Time Complexity: O(n)
|
|
475
611
|
* Space Complexity: O(1)
|
|
@@ -619,18 +755,49 @@ var dataStructureTyped = (() => {
|
|
|
619
755
|
this.setMany(rawCollection);
|
|
620
756
|
}
|
|
621
757
|
}
|
|
758
|
+
/**
|
|
759
|
+
* The function returns the value of the _toEntryFn property.
|
|
760
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
761
|
+
*/
|
|
622
762
|
get toEntryFn() {
|
|
623
763
|
return this._toEntryFn;
|
|
624
764
|
}
|
|
765
|
+
/**
|
|
766
|
+
* The hasFn function is a function that takes in an item and returns a boolean
|
|
767
|
+
* indicating whether the item is contained within the hash table.
|
|
768
|
+
*
|
|
769
|
+
* @return The hash function
|
|
770
|
+
*/
|
|
771
|
+
get hasFn() {
|
|
772
|
+
return this._hashFn;
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* The function returns the size of an object.
|
|
776
|
+
* @returns The size of the object, which is a number.
|
|
777
|
+
*/
|
|
625
778
|
get size() {
|
|
626
779
|
return this._size;
|
|
627
780
|
}
|
|
781
|
+
/**
|
|
782
|
+
* The function checks if a given element is an array with exactly two elements.
|
|
783
|
+
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
784
|
+
* data type.
|
|
785
|
+
* @returns a boolean value.
|
|
786
|
+
*/
|
|
628
787
|
isEntry(rawElement) {
|
|
629
788
|
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
630
789
|
}
|
|
790
|
+
/**
|
|
791
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
792
|
+
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
793
|
+
*/
|
|
631
794
|
isEmpty() {
|
|
632
795
|
return this.size === 0;
|
|
633
796
|
}
|
|
797
|
+
/**
|
|
798
|
+
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
|
799
|
+
* size.
|
|
800
|
+
*/
|
|
634
801
|
clear() {
|
|
635
802
|
this._store = {};
|
|
636
803
|
this._objMap.clear();
|
|
@@ -670,7 +837,15 @@ var dataStructureTyped = (() => {
|
|
|
670
837
|
setMany(rawCollection) {
|
|
671
838
|
const results = [];
|
|
672
839
|
for (const rawEle of rawCollection) {
|
|
673
|
-
|
|
840
|
+
let key, value;
|
|
841
|
+
if (this.isEntry(rawEle)) {
|
|
842
|
+
key = rawEle[0];
|
|
843
|
+
value = rawEle[1];
|
|
844
|
+
} else {
|
|
845
|
+
const item = this.toEntryFn(rawEle);
|
|
846
|
+
key = item[0];
|
|
847
|
+
value = item[1];
|
|
848
|
+
}
|
|
674
849
|
results.push(this.set(key, value));
|
|
675
850
|
}
|
|
676
851
|
return results;
|
|
@@ -729,6 +904,16 @@ var dataStructureTyped = (() => {
|
|
|
729
904
|
return false;
|
|
730
905
|
}
|
|
731
906
|
}
|
|
907
|
+
/**
|
|
908
|
+
* The clone function creates a new HashMap with the same key-value pairs as
|
|
909
|
+
* this one. The clone function is useful for creating a copy of an existing
|
|
910
|
+
* HashMap, and then modifying that copy without affecting the original.
|
|
911
|
+
*
|
|
912
|
+
* @return A new hashmap with the same values as this one
|
|
913
|
+
*/
|
|
914
|
+
clone() {
|
|
915
|
+
return new _HashMap(this, { hashFn: this._hashFn, toEntryFn: this.toEntryFn });
|
|
916
|
+
}
|
|
732
917
|
/**
|
|
733
918
|
* Time Complexity: O(n)
|
|
734
919
|
* Space Complexity: O(n)
|
|
@@ -785,6 +970,14 @@ var dataStructureTyped = (() => {
|
|
|
785
970
|
}
|
|
786
971
|
return filteredMap;
|
|
787
972
|
}
|
|
973
|
+
/**
|
|
974
|
+
* The put function sets a value in a data structure using a specified key.
|
|
975
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key being passed
|
|
976
|
+
* to the function.
|
|
977
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
978
|
+
* specified key in the data structure.
|
|
979
|
+
* @returns The method is returning a boolean value.
|
|
980
|
+
*/
|
|
788
981
|
put(key, value) {
|
|
789
982
|
return this.set(key, value);
|
|
790
983
|
}
|
|
@@ -820,35 +1013,64 @@ var dataStructureTyped = (() => {
|
|
|
820
1013
|
}
|
|
821
1014
|
};
|
|
822
1015
|
var LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
823
|
-
|
|
1016
|
+
/**
|
|
1017
|
+
* The constructor initializes a LinkedHashMap object with an optional raw collection and options.
|
|
1018
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements. It is
|
|
1019
|
+
* used to initialize the HashMapLinked instance with key-value pairs. Each element in the
|
|
1020
|
+
* `rawCollection` is converted to a key-value pair using the `toEntryFn` function (if provided) and
|
|
1021
|
+
* then added to the HashMap
|
|
1022
|
+
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
1023
|
+
* properties:
|
|
1024
|
+
*/
|
|
1025
|
+
constructor(rawCollection = [], options) {
|
|
824
1026
|
super();
|
|
825
1027
|
__publicField(this, "_noObjMap", {});
|
|
826
1028
|
__publicField(this, "_objMap", /* @__PURE__ */ new WeakMap());
|
|
827
1029
|
__publicField(this, "_head");
|
|
828
1030
|
__publicField(this, "_tail");
|
|
829
1031
|
__publicField(this, "_sentinel");
|
|
1032
|
+
__publicField(this, "_toEntryFn", (rawElement) => {
|
|
1033
|
+
if (this.isEntry(rawElement)) {
|
|
1034
|
+
return rawElement;
|
|
1035
|
+
} else {
|
|
1036
|
+
throw new Error(
|
|
1037
|
+
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
});
|
|
830
1041
|
__publicField(this, "_size", 0);
|
|
831
|
-
/**
|
|
832
|
-
* Time Complexity: O(n)
|
|
833
|
-
* Space Complexity: O(n)
|
|
834
|
-
*/
|
|
835
1042
|
__publicField(this, "_hashFn", (key) => String(key));
|
|
836
1043
|
__publicField(this, "_objHashFn", (key) => key);
|
|
837
1044
|
this._sentinel = {};
|
|
838
1045
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
839
1046
|
if (options) {
|
|
840
|
-
const { hashFn, objHashFn } = options;
|
|
1047
|
+
const { hashFn, objHashFn, toEntryFn } = options;
|
|
841
1048
|
if (hashFn)
|
|
842
1049
|
this._hashFn = hashFn;
|
|
843
1050
|
if (objHashFn)
|
|
844
1051
|
this._objHashFn = objHashFn;
|
|
1052
|
+
if (toEntryFn) {
|
|
1053
|
+
this._toEntryFn = toEntryFn;
|
|
1054
|
+
}
|
|
845
1055
|
}
|
|
846
|
-
if (
|
|
847
|
-
for (const el of
|
|
848
|
-
this.
|
|
1056
|
+
if (rawCollection) {
|
|
1057
|
+
for (const el of rawCollection) {
|
|
1058
|
+
const [key, value] = this.toEntryFn(el);
|
|
1059
|
+
this.set(key, value);
|
|
849
1060
|
}
|
|
850
1061
|
}
|
|
851
1062
|
}
|
|
1063
|
+
/**
|
|
1064
|
+
* The function returns the value of the _toEntryFn property.
|
|
1065
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
1066
|
+
*/
|
|
1067
|
+
get toEntryFn() {
|
|
1068
|
+
return this._toEntryFn;
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* The function returns the size of an object.
|
|
1072
|
+
* @returns The size of the object.
|
|
1073
|
+
*/
|
|
852
1074
|
get size() {
|
|
853
1075
|
return this._size;
|
|
854
1076
|
}
|
|
@@ -946,6 +1168,28 @@ var dataStructureTyped = (() => {
|
|
|
946
1168
|
}
|
|
947
1169
|
return true;
|
|
948
1170
|
}
|
|
1171
|
+
/**
|
|
1172
|
+
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
1173
|
+
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
1174
|
+
* of booleans indicating the success of each set operation.
|
|
1175
|
+
* @param rawCollection - The rawCollection parameter is an iterable collection of elements of type
|
|
1176
|
+
* R.
|
|
1177
|
+
* @returns The `setMany` function returns an array of booleans.
|
|
1178
|
+
*/
|
|
1179
|
+
setMany(rawCollection) {
|
|
1180
|
+
const results = [];
|
|
1181
|
+
for (const rawEle of rawCollection) {
|
|
1182
|
+
const [key, value] = this.toEntryFn(rawEle);
|
|
1183
|
+
results.push(this.set(key, value));
|
|
1184
|
+
}
|
|
1185
|
+
return results;
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* The function checks if a given key exists in a map, using different logic depending on whether the
|
|
1189
|
+
* key is a weak key or not.
|
|
1190
|
+
* @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
|
|
1191
|
+
* @returns The method `has` is returning a boolean value.
|
|
1192
|
+
*/
|
|
949
1193
|
has(key) {
|
|
950
1194
|
if (isWeakKey(key)) {
|
|
951
1195
|
const hash = this._objHashFn(key);
|
|
@@ -955,14 +1199,6 @@ var dataStructureTyped = (() => {
|
|
|
955
1199
|
return hash in this._noObjMap;
|
|
956
1200
|
}
|
|
957
1201
|
}
|
|
958
|
-
setMany(entries) {
|
|
959
|
-
const results = [];
|
|
960
|
-
for (const entry of entries) {
|
|
961
|
-
const [key, value] = entry;
|
|
962
|
-
results.push(this.set(key, value));
|
|
963
|
-
}
|
|
964
|
-
return results;
|
|
965
|
-
}
|
|
966
1202
|
/**
|
|
967
1203
|
* Time Complexity: O(1)
|
|
968
1204
|
* Space Complexity: O(1)
|
|
@@ -991,14 +1227,14 @@ var dataStructureTyped = (() => {
|
|
|
991
1227
|
* Time Complexity: O(n), where n is the index.
|
|
992
1228
|
* Space Complexity: O(1)
|
|
993
1229
|
*
|
|
994
|
-
* The function `
|
|
1230
|
+
* The function `at` retrieves the key-value pair at a specified index in a linked list.
|
|
995
1231
|
* @param {number} index - The index parameter is a number that represents the position of the
|
|
996
1232
|
* element we want to retrieve from the data structure.
|
|
997
|
-
* @returns The method `
|
|
1233
|
+
* @returns The method `at(index: number)` is returning an array containing the key-value pair at
|
|
998
1234
|
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
999
1235
|
* where `K` is the key and `V` is the value.
|
|
1000
1236
|
*/
|
|
1001
|
-
|
|
1237
|
+
at(index) {
|
|
1002
1238
|
rangeCheck(index, 0, this._size - 1);
|
|
1003
1239
|
let node = this._head;
|
|
1004
1240
|
while (index--) {
|
|
@@ -1037,7 +1273,7 @@ var dataStructureTyped = (() => {
|
|
|
1037
1273
|
return true;
|
|
1038
1274
|
}
|
|
1039
1275
|
/**
|
|
1040
|
-
* Time Complexity: O(n)
|
|
1276
|
+
* Time Complexity: O(n)
|
|
1041
1277
|
* Space Complexity: O(1)
|
|
1042
1278
|
*
|
|
1043
1279
|
* The `deleteAt` function deletes a node at a specified index in a linked list.
|
|
@@ -1064,6 +1300,15 @@ var dataStructureTyped = (() => {
|
|
|
1064
1300
|
isEmpty() {
|
|
1065
1301
|
return this._size === 0;
|
|
1066
1302
|
}
|
|
1303
|
+
/**
|
|
1304
|
+
* The function checks if a given element is an array with exactly two elements.
|
|
1305
|
+
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
1306
|
+
* data type.
|
|
1307
|
+
* @returns a boolean value.
|
|
1308
|
+
*/
|
|
1309
|
+
isEntry(rawElement) {
|
|
1310
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
1311
|
+
}
|
|
1067
1312
|
/**
|
|
1068
1313
|
* Time Complexity: O(1)
|
|
1069
1314
|
* Space Complexity: O(1)
|
|
@@ -1075,6 +1320,19 @@ var dataStructureTyped = (() => {
|
|
|
1075
1320
|
this._size = 0;
|
|
1076
1321
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
1077
1322
|
}
|
|
1323
|
+
/**
|
|
1324
|
+
* Time Complexity: O(n)
|
|
1325
|
+
* Space Complexity: O(n)
|
|
1326
|
+
*/
|
|
1327
|
+
/**
|
|
1328
|
+
* Time Complexity: O(n)
|
|
1329
|
+
* Space Complexity: O(n)
|
|
1330
|
+
*
|
|
1331
|
+
* The `clone` function creates a new instance of a `LinkedHashMap` with the same key-value pairs as
|
|
1332
|
+
* the original.
|
|
1333
|
+
* @returns The `clone()` method is returning a new instance of `LinkedHashMap<K, V>` that is a clone
|
|
1334
|
+
* of the original `LinkedHashMap` object.
|
|
1335
|
+
*/
|
|
1078
1336
|
clone() {
|
|
1079
1337
|
const cloned = new _LinkedHashMap([], { hashFn: this._hashFn, objHashFn: this._objHashFn });
|
|
1080
1338
|
for (const entry of this) {
|
|
@@ -1137,15 +1395,27 @@ var dataStructureTyped = (() => {
|
|
|
1137
1395
|
return mappedMap;
|
|
1138
1396
|
}
|
|
1139
1397
|
/**
|
|
1140
|
-
* Time Complexity: O(
|
|
1141
|
-
* Space Complexity: O(
|
|
1398
|
+
* Time Complexity: O(1)
|
|
1399
|
+
* Space Complexity: O(1)
|
|
1400
|
+
*/
|
|
1401
|
+
/**
|
|
1402
|
+
* Time Complexity: O(1)
|
|
1403
|
+
* Space Complexity: O(1)
|
|
1404
|
+
*
|
|
1405
|
+
* The put function sets a value in a data structure using a specified key.
|
|
1406
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key being passed
|
|
1407
|
+
* to the function.
|
|
1408
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
1409
|
+
* specified key in the data structure.
|
|
1410
|
+
* @returns The method is returning a boolean value.
|
|
1142
1411
|
*/
|
|
1143
1412
|
put(key, value) {
|
|
1144
1413
|
return this.set(key, value);
|
|
1145
1414
|
}
|
|
1146
1415
|
/**
|
|
1147
|
-
* Time Complexity: O(n)
|
|
1416
|
+
* Time Complexity: O(n)
|
|
1148
1417
|
* Space Complexity: O(1)
|
|
1418
|
+
* where n is the number of entries in the LinkedHashMap.
|
|
1149
1419
|
*
|
|
1150
1420
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
1151
1421
|
*/
|
|
@@ -1197,7 +1467,10 @@ var dataStructureTyped = (() => {
|
|
|
1197
1467
|
};
|
|
1198
1468
|
var SinglyLinkedList = class _SinglyLinkedList extends IterableElementBase {
|
|
1199
1469
|
/**
|
|
1200
|
-
* The constructor initializes
|
|
1470
|
+
* The constructor initializes a new instance of a class with an optional iterable of elements.
|
|
1471
|
+
* @param elements - The `elements` parameter is an optional iterable object that contains the
|
|
1472
|
+
* initial elements to be added to the instance of the class. If no `elements` are provided, an empty
|
|
1473
|
+
* array will be used as the default value.
|
|
1201
1474
|
*/
|
|
1202
1475
|
constructor(elements = []) {
|
|
1203
1476
|
super();
|
|
@@ -1209,22 +1482,36 @@ var dataStructureTyped = (() => {
|
|
|
1209
1482
|
this.push(el);
|
|
1210
1483
|
}
|
|
1211
1484
|
}
|
|
1485
|
+
/**
|
|
1486
|
+
* The `head` function returns the first node of a singly linked list.
|
|
1487
|
+
* @returns The method is returning either a SinglyLinkedListNode object or undefined.
|
|
1488
|
+
*/
|
|
1212
1489
|
get head() {
|
|
1213
1490
|
return this._head;
|
|
1214
1491
|
}
|
|
1492
|
+
/**
|
|
1493
|
+
* The `tail` function returns the last node of a singly linked list.
|
|
1494
|
+
* @returns The method is returning either a SinglyLinkedListNode object or undefined.
|
|
1495
|
+
*/
|
|
1215
1496
|
get tail() {
|
|
1216
1497
|
return this._tail;
|
|
1217
1498
|
}
|
|
1499
|
+
/**
|
|
1500
|
+
* The function returns the size of an object.
|
|
1501
|
+
* @returns The size of the object, which is a number.
|
|
1502
|
+
*/
|
|
1218
1503
|
get size() {
|
|
1219
1504
|
return this._size;
|
|
1220
1505
|
}
|
|
1221
1506
|
/**
|
|
1222
|
-
* Time Complexity: O(n)
|
|
1223
|
-
* Space Complexity: O(n)
|
|
1507
|
+
* Time Complexity: O(n)
|
|
1508
|
+
* Space Complexity: O(n)
|
|
1509
|
+
* Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
|
|
1510
|
+
* Linear space, as it creates a new node for each element in the array.
|
|
1224
1511
|
*/
|
|
1225
1512
|
/**
|
|
1226
|
-
* Time Complexity: O(n)
|
|
1227
|
-
* Space Complexity: O(n)
|
|
1513
|
+
* Time Complexity: O(n)
|
|
1514
|
+
* Space Complexity: O(n)
|
|
1228
1515
|
*
|
|
1229
1516
|
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
1230
1517
|
* array.
|
|
@@ -1239,12 +1526,14 @@ var dataStructureTyped = (() => {
|
|
|
1239
1526
|
return singlyLinkedList;
|
|
1240
1527
|
}
|
|
1241
1528
|
/**
|
|
1242
|
-
* Time Complexity: O(1)
|
|
1243
|
-
* Space Complexity: O(1)
|
|
1529
|
+
* Time Complexity: O(1)
|
|
1530
|
+
* Space Complexity: O(1)
|
|
1531
|
+
* Constant time, as it involves basic pointer adjustments.
|
|
1532
|
+
* Constant space, as it only creates a new node.
|
|
1244
1533
|
*/
|
|
1245
1534
|
/**
|
|
1246
|
-
* Time Complexity: O(1)
|
|
1247
|
-
* Space Complexity: O(1)
|
|
1535
|
+
* Time Complexity: O(1)
|
|
1536
|
+
* Space Complexity: O(1)
|
|
1248
1537
|
*
|
|
1249
1538
|
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
1250
1539
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
@@ -1263,12 +1552,12 @@ var dataStructureTyped = (() => {
|
|
|
1263
1552
|
return true;
|
|
1264
1553
|
}
|
|
1265
1554
|
/**
|
|
1266
|
-
* Time Complexity: O(1)
|
|
1267
|
-
* Space Complexity: O(1)
|
|
1555
|
+
* Time Complexity: O(1)
|
|
1556
|
+
* Space Complexity: O(1)
|
|
1268
1557
|
*/
|
|
1269
1558
|
/**
|
|
1270
|
-
* Time Complexity: O(1)
|
|
1271
|
-
* Space Complexity: O(1)
|
|
1559
|
+
* Time Complexity: O(1)
|
|
1560
|
+
* Space Complexity: O(1)
|
|
1272
1561
|
*
|
|
1273
1562
|
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
1274
1563
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
@@ -1278,12 +1567,13 @@ var dataStructureTyped = (() => {
|
|
|
1278
1567
|
return this.push(value);
|
|
1279
1568
|
}
|
|
1280
1569
|
/**
|
|
1281
|
-
* Time Complexity: O(n)
|
|
1282
|
-
* Space Complexity: O(1)
|
|
1570
|
+
* Time Complexity: O(n)
|
|
1571
|
+
* Space Complexity: O(1)
|
|
1572
|
+
* Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
1283
1573
|
*/
|
|
1284
1574
|
/**
|
|
1285
|
-
* Time Complexity: O(n)
|
|
1286
|
-
* Space Complexity: O(1)
|
|
1575
|
+
* Time Complexity: O(n)
|
|
1576
|
+
* Space Complexity: O(1)
|
|
1287
1577
|
*
|
|
1288
1578
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
1289
1579
|
* pointers accordingly.
|
|
@@ -1311,12 +1601,12 @@ var dataStructureTyped = (() => {
|
|
|
1311
1601
|
return value;
|
|
1312
1602
|
}
|
|
1313
1603
|
/**
|
|
1314
|
-
* Time Complexity: O(n)
|
|
1315
|
-
* Space Complexity: O(1)
|
|
1604
|
+
* Time Complexity: O(n)
|
|
1605
|
+
* Space Complexity: O(1)
|
|
1316
1606
|
*/
|
|
1317
1607
|
/**
|
|
1318
|
-
* Time Complexity: O(n)
|
|
1319
|
-
* Space Complexity: O(1)
|
|
1608
|
+
* Time Complexity: O(n)
|
|
1609
|
+
* Space Complexity: O(1)
|
|
1320
1610
|
*
|
|
1321
1611
|
* The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
1322
1612
|
* pointers accordingly.
|
|
@@ -1327,12 +1617,12 @@ var dataStructureTyped = (() => {
|
|
|
1327
1617
|
return this.pop();
|
|
1328
1618
|
}
|
|
1329
1619
|
/**
|
|
1330
|
-
* Time Complexity: O(1)
|
|
1331
|
-
* Space Complexity: O(1)
|
|
1620
|
+
* Time Complexity: O(1)
|
|
1621
|
+
* Space Complexity: O(1)
|
|
1332
1622
|
*/
|
|
1333
1623
|
/**
|
|
1334
|
-
* Time Complexity: O(1)
|
|
1335
|
-
* Space Complexity: O(1)
|
|
1624
|
+
* Time Complexity: O(1)
|
|
1625
|
+
* Space Complexity: O(1)
|
|
1336
1626
|
*
|
|
1337
1627
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
1338
1628
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -1346,12 +1636,12 @@ var dataStructureTyped = (() => {
|
|
|
1346
1636
|
return removedNode.value;
|
|
1347
1637
|
}
|
|
1348
1638
|
/**
|
|
1349
|
-
* Time Complexity: O(1)
|
|
1350
|
-
* Space Complexity: O(1)
|
|
1639
|
+
* Time Complexity: O(1)
|
|
1640
|
+
* Space Complexity: O(1)
|
|
1351
1641
|
*/
|
|
1352
1642
|
/**
|
|
1353
|
-
* Time Complexity: O(1)
|
|
1354
|
-
* Space Complexity: O(1)
|
|
1643
|
+
* Time Complexity: O(1)
|
|
1644
|
+
* Space Complexity: O(1)
|
|
1355
1645
|
*
|
|
1356
1646
|
* The `pollFirst()` function removes and returns the value of the first node in a linked list.
|
|
1357
1647
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -1360,12 +1650,12 @@ var dataStructureTyped = (() => {
|
|
|
1360
1650
|
return this.shift();
|
|
1361
1651
|
}
|
|
1362
1652
|
/**
|
|
1363
|
-
* Time Complexity: O(1)
|
|
1364
|
-
* Space Complexity: O(1)
|
|
1653
|
+
* Time Complexity: O(1)
|
|
1654
|
+
* Space Complexity: O(1)
|
|
1365
1655
|
*/
|
|
1366
1656
|
/**
|
|
1367
|
-
* Time Complexity: O(1)
|
|
1368
|
-
* Space Complexity: O(1)
|
|
1657
|
+
* Time Complexity: O(1)
|
|
1658
|
+
* Space Complexity: O(1)
|
|
1369
1659
|
*
|
|
1370
1660
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
1371
1661
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
@@ -1384,12 +1674,12 @@ var dataStructureTyped = (() => {
|
|
|
1384
1674
|
return true;
|
|
1385
1675
|
}
|
|
1386
1676
|
/**
|
|
1387
|
-
* Time Complexity: O(1)
|
|
1388
|
-
* Space Complexity: O(1)
|
|
1677
|
+
* Time Complexity: O(1)
|
|
1678
|
+
* Space Complexity: O(1)
|
|
1389
1679
|
*/
|
|
1390
1680
|
/**
|
|
1391
|
-
* Time Complexity: O(1)
|
|
1392
|
-
* Space Complexity: O(1)
|
|
1681
|
+
* Time Complexity: O(1)
|
|
1682
|
+
* Space Complexity: O(1)
|
|
1393
1683
|
*
|
|
1394
1684
|
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
1395
1685
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
@@ -1399,20 +1689,21 @@ var dataStructureTyped = (() => {
|
|
|
1399
1689
|
return this.unshift(value);
|
|
1400
1690
|
}
|
|
1401
1691
|
/**
|
|
1402
|
-
* Time Complexity: O(n)
|
|
1403
|
-
* Space Complexity: O(1)
|
|
1692
|
+
* Time Complexity: O(n)
|
|
1693
|
+
* Space Complexity: O(1)
|
|
1694
|
+
* Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
1404
1695
|
*/
|
|
1405
1696
|
/**
|
|
1406
|
-
* Time Complexity: O(n)
|
|
1407
|
-
* Space Complexity: O(1)
|
|
1697
|
+
* Time Complexity: O(n)
|
|
1698
|
+
* Space Complexity: O(1)
|
|
1408
1699
|
*
|
|
1409
|
-
* The function `
|
|
1700
|
+
* The function `at` returns the value at a specified index in a linked list, or undefined if the index is out of range.
|
|
1410
1701
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
1411
1702
|
* retrieve from the list.
|
|
1412
|
-
* @returns The method `
|
|
1703
|
+
* @returns The method `at(index: number): E | undefined` returns the value at the specified index in the linked list, or
|
|
1413
1704
|
* `undefined` if the index is out of bounds.
|
|
1414
1705
|
*/
|
|
1415
|
-
|
|
1706
|
+
at(index) {
|
|
1416
1707
|
if (index < 0 || index >= this.size)
|
|
1417
1708
|
return void 0;
|
|
1418
1709
|
let current = this.head;
|
|
@@ -1422,12 +1713,12 @@ var dataStructureTyped = (() => {
|
|
|
1422
1713
|
return current.value;
|
|
1423
1714
|
}
|
|
1424
1715
|
/**
|
|
1425
|
-
* Time Complexity: O(n)
|
|
1426
|
-
* Space Complexity: O(1)
|
|
1716
|
+
* Time Complexity: O(n)
|
|
1717
|
+
* Space Complexity: O(1)
|
|
1427
1718
|
*/
|
|
1428
1719
|
/**
|
|
1429
|
-
* Time Complexity: O(n)
|
|
1430
|
-
* Space Complexity: O(1)
|
|
1720
|
+
* Time Complexity: O(n)
|
|
1721
|
+
* Space Complexity: O(1)
|
|
1431
1722
|
*
|
|
1432
1723
|
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
1433
1724
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
@@ -1443,12 +1734,12 @@ var dataStructureTyped = (() => {
|
|
|
1443
1734
|
return current;
|
|
1444
1735
|
}
|
|
1445
1736
|
/**
|
|
1446
|
-
* Time Complexity: O(n)
|
|
1447
|
-
* Space Complexity: O(1)
|
|
1737
|
+
* Time Complexity: O(n)
|
|
1738
|
+
* Space Complexity: O(1)
|
|
1448
1739
|
*/
|
|
1449
1740
|
/**
|
|
1450
|
-
* Time Complexity: O(n)
|
|
1451
|
-
* Space Complexity: O(1)
|
|
1741
|
+
* Time Complexity: O(n)
|
|
1742
|
+
* Space Complexity: O(1)
|
|
1452
1743
|
*
|
|
1453
1744
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
1454
1745
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -1474,12 +1765,12 @@ var dataStructureTyped = (() => {
|
|
|
1474
1765
|
return true;
|
|
1475
1766
|
}
|
|
1476
1767
|
/**
|
|
1477
|
-
* Time Complexity: O(n)
|
|
1478
|
-
* Space Complexity: O(1)
|
|
1768
|
+
* Time Complexity: O(n)
|
|
1769
|
+
* Space Complexity: O(1)
|
|
1479
1770
|
*/
|
|
1480
1771
|
/**
|
|
1481
|
-
* Time Complexity: O(n)
|
|
1482
|
-
* Space Complexity: O(1)
|
|
1772
|
+
* Time Complexity: O(n)
|
|
1773
|
+
* Space Complexity: O(1)
|
|
1483
1774
|
*
|
|
1484
1775
|
* The delete function removes a node with a specific value from a singly linked list.
|
|
1485
1776
|
* @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
|
|
@@ -1519,12 +1810,12 @@ var dataStructureTyped = (() => {
|
|
|
1519
1810
|
return false;
|
|
1520
1811
|
}
|
|
1521
1812
|
/**
|
|
1522
|
-
* Time Complexity: O(n)
|
|
1523
|
-
* Space Complexity: O(1)
|
|
1813
|
+
* Time Complexity: O(n)
|
|
1814
|
+
* Space Complexity: O(1)
|
|
1524
1815
|
*/
|
|
1525
1816
|
/**
|
|
1526
|
-
* Time Complexity: O(n)
|
|
1527
|
-
* Space Complexity: O(1)
|
|
1817
|
+
* Time Complexity: O(n)
|
|
1818
|
+
* Space Complexity: O(1)
|
|
1528
1819
|
*
|
|
1529
1820
|
* The `addAt` function inserts a value at a specified index in a singly linked list.
|
|
1530
1821
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
@@ -1569,12 +1860,14 @@ var dataStructureTyped = (() => {
|
|
|
1569
1860
|
this._size = 0;
|
|
1570
1861
|
}
|
|
1571
1862
|
/**
|
|
1572
|
-
* Time Complexity: O(n)
|
|
1573
|
-
* Space Complexity: O(n)
|
|
1863
|
+
* Time Complexity: O(n)
|
|
1864
|
+
* Space Complexity: O(n)
|
|
1865
|
+
* Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
|
|
1866
|
+
* Linear space, as it creates an array with the same length as the list.
|
|
1574
1867
|
*/
|
|
1575
1868
|
/**
|
|
1576
|
-
* Time Complexity: O(n)
|
|
1577
|
-
* Space Complexity: O(n)
|
|
1869
|
+
* Time Complexity: O(n)
|
|
1870
|
+
* Space Complexity: O(n)
|
|
1578
1871
|
*
|
|
1579
1872
|
* The `toArray` function converts a linked list into an array.
|
|
1580
1873
|
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
@@ -1589,12 +1882,12 @@ var dataStructureTyped = (() => {
|
|
|
1589
1882
|
return array;
|
|
1590
1883
|
}
|
|
1591
1884
|
/**
|
|
1592
|
-
* Time Complexity: O(n)
|
|
1593
|
-
* Space Complexity: O(1)
|
|
1885
|
+
* Time Complexity: O(n)
|
|
1886
|
+
* Space Complexity: O(1)
|
|
1594
1887
|
*/
|
|
1595
1888
|
/**
|
|
1596
|
-
* Time Complexity: O(n)
|
|
1597
|
-
* Space Complexity: O(1)
|
|
1889
|
+
* Time Complexity: O(n)
|
|
1890
|
+
* Space Complexity: O(1)
|
|
1598
1891
|
*
|
|
1599
1892
|
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
1600
1893
|
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
@@ -1615,36 +1908,12 @@ var dataStructureTyped = (() => {
|
|
|
1615
1908
|
return this;
|
|
1616
1909
|
}
|
|
1617
1910
|
/**
|
|
1618
|
-
* Time Complexity: O(n)
|
|
1619
|
-
* Space Complexity: O(1)
|
|
1620
|
-
*/
|
|
1621
|
-
/**
|
|
1622
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
1623
|
-
* Space Complexity: O(1) - Constant space.
|
|
1624
|
-
*
|
|
1625
|
-
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
1626
|
-
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
1627
|
-
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
1628
|
-
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
1629
|
-
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
1630
|
-
*/
|
|
1631
|
-
find(callback) {
|
|
1632
|
-
let current = this.head;
|
|
1633
|
-
while (current) {
|
|
1634
|
-
if (callback(current.value)) {
|
|
1635
|
-
return current.value;
|
|
1636
|
-
}
|
|
1637
|
-
current = current.next;
|
|
1638
|
-
}
|
|
1639
|
-
return void 0;
|
|
1640
|
-
}
|
|
1641
|
-
/**
|
|
1642
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
1643
|
-
* Space Complexity: O(1) - Constant space.
|
|
1911
|
+
* Time Complexity: O(n)
|
|
1912
|
+
* Space Complexity: O(1)
|
|
1644
1913
|
*/
|
|
1645
1914
|
/**
|
|
1646
|
-
* Time Complexity: O(n)
|
|
1647
|
-
* Space Complexity: O(1)
|
|
1915
|
+
* Time Complexity: O(n)
|
|
1916
|
+
* Space Complexity: O(1)
|
|
1648
1917
|
*
|
|
1649
1918
|
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
1650
1919
|
* @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
@@ -1664,12 +1933,12 @@ var dataStructureTyped = (() => {
|
|
|
1664
1933
|
return -1;
|
|
1665
1934
|
}
|
|
1666
1935
|
/**
|
|
1667
|
-
* Time Complexity: O(n)
|
|
1668
|
-
* Space Complexity: O(1)
|
|
1936
|
+
* Time Complexity: O(n)
|
|
1937
|
+
* Space Complexity: O(1)
|
|
1669
1938
|
*/
|
|
1670
1939
|
/**
|
|
1671
|
-
* Time Complexity: O(n)
|
|
1672
|
-
* Space Complexity: O(1)
|
|
1940
|
+
* Time Complexity: O(n)
|
|
1941
|
+
* Space Complexity: O(1)
|
|
1673
1942
|
*
|
|
1674
1943
|
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
1675
1944
|
* undefined.
|
|
@@ -1688,12 +1957,12 @@ var dataStructureTyped = (() => {
|
|
|
1688
1957
|
return void 0;
|
|
1689
1958
|
}
|
|
1690
1959
|
/**
|
|
1691
|
-
* Time Complexity: O(n)
|
|
1692
|
-
* Space Complexity: O(1)
|
|
1960
|
+
* Time Complexity: O(n)
|
|
1961
|
+
* Space Complexity: O(1)
|
|
1693
1962
|
*/
|
|
1694
1963
|
/**
|
|
1695
|
-
* Time Complexity: O(n)
|
|
1696
|
-
* Space Complexity: O(1)
|
|
1964
|
+
* Time Complexity: O(n)
|
|
1965
|
+
* Space Complexity: O(1)
|
|
1697
1966
|
*
|
|
1698
1967
|
* The `addBefore` function inserts a new value before an existing value in a singly linked list.
|
|
1699
1968
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
@@ -1729,12 +1998,12 @@ var dataStructureTyped = (() => {
|
|
|
1729
1998
|
return false;
|
|
1730
1999
|
}
|
|
1731
2000
|
/**
|
|
1732
|
-
* Time Complexity: O(n)
|
|
1733
|
-
* Space Complexity: O(1)
|
|
2001
|
+
* Time Complexity: O(n)
|
|
2002
|
+
* Space Complexity: O(1)
|
|
1734
2003
|
*/
|
|
1735
2004
|
/**
|
|
1736
|
-
* Time Complexity: O(n)
|
|
1737
|
-
* Space Complexity: O(1)
|
|
2005
|
+
* Time Complexity: O(n)
|
|
2006
|
+
* Space Complexity: O(1)
|
|
1738
2007
|
*
|
|
1739
2008
|
* The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
1740
2009
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
@@ -1763,12 +2032,12 @@ var dataStructureTyped = (() => {
|
|
|
1763
2032
|
return false;
|
|
1764
2033
|
}
|
|
1765
2034
|
/**
|
|
1766
|
-
* Time Complexity: O(n)
|
|
1767
|
-
* Space Complexity: O(1)
|
|
2035
|
+
* Time Complexity: O(n)
|
|
2036
|
+
* Space Complexity: O(1)
|
|
1768
2037
|
*/
|
|
1769
2038
|
/**
|
|
1770
|
-
* Time Complexity: O(n)
|
|
1771
|
-
* Space Complexity: O(1)
|
|
2039
|
+
* Time Complexity: O(n)
|
|
2040
|
+
* Space Complexity: O(1)
|
|
1772
2041
|
*
|
|
1773
2042
|
* The function counts the number of occurrences of a given value in a linked list.
|
|
1774
2043
|
* @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
@@ -1785,6 +2054,22 @@ var dataStructureTyped = (() => {
|
|
|
1785
2054
|
}
|
|
1786
2055
|
return count;
|
|
1787
2056
|
}
|
|
2057
|
+
/**
|
|
2058
|
+
* Time Complexity: O(n)
|
|
2059
|
+
* Space Complexity: O(n)
|
|
2060
|
+
*/
|
|
2061
|
+
/**
|
|
2062
|
+
* Time Complexity: O(n)
|
|
2063
|
+
* Space Complexity: O(n)
|
|
2064
|
+
*
|
|
2065
|
+
* The `clone` function returns a new instance of the `SinglyLinkedList` class with the same values
|
|
2066
|
+
* as the original list.
|
|
2067
|
+
* @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
|
|
2068
|
+
* is a clone of the original list.
|
|
2069
|
+
*/
|
|
2070
|
+
clone() {
|
|
2071
|
+
return new _SinglyLinkedList(this.values());
|
|
2072
|
+
}
|
|
1788
2073
|
/**
|
|
1789
2074
|
* Time Complexity: O(n)
|
|
1790
2075
|
* Space Complexity: O(n)
|
|
@@ -1844,6 +2129,9 @@ var dataStructureTyped = (() => {
|
|
|
1844
2129
|
}
|
|
1845
2130
|
return mappedList;
|
|
1846
2131
|
}
|
|
2132
|
+
/**
|
|
2133
|
+
* The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
|
|
2134
|
+
*/
|
|
1847
2135
|
*_getIterator() {
|
|
1848
2136
|
let current = this.head;
|
|
1849
2137
|
while (current) {
|
|
@@ -1871,7 +2159,10 @@ var dataStructureTyped = (() => {
|
|
|
1871
2159
|
};
|
|
1872
2160
|
var DoublyLinkedList = class _DoublyLinkedList extends IterableElementBase {
|
|
1873
2161
|
/**
|
|
1874
|
-
* The constructor initializes
|
|
2162
|
+
* The constructor initializes a linked list with optional elements.
|
|
2163
|
+
* @param elements - The `elements` parameter is an optional iterable object that contains the
|
|
2164
|
+
* initial elements to be added to the data structure. It defaults to an empty array if no elements
|
|
2165
|
+
* are provided.
|
|
1875
2166
|
*/
|
|
1876
2167
|
constructor(elements = []) {
|
|
1877
2168
|
super();
|
|
@@ -1887,21 +2178,35 @@ var dataStructureTyped = (() => {
|
|
|
1887
2178
|
}
|
|
1888
2179
|
}
|
|
1889
2180
|
}
|
|
2181
|
+
/**
|
|
2182
|
+
* The `head` function returns the first node of a doubly linked list.
|
|
2183
|
+
* @returns The method `getHead()` returns either a `DoublyLinkedListNode<E>` object or `undefined`.
|
|
2184
|
+
*/
|
|
1890
2185
|
get head() {
|
|
1891
2186
|
return this._head;
|
|
1892
2187
|
}
|
|
2188
|
+
/**
|
|
2189
|
+
* The `tail` function returns the last node of a doubly linked list.
|
|
2190
|
+
* @returns The `get tail()` method is returning either a `DoublyLinkedListNode<E>` object or
|
|
2191
|
+
* `undefined`.
|
|
2192
|
+
*/
|
|
1893
2193
|
get tail() {
|
|
1894
2194
|
return this._tail;
|
|
1895
2195
|
}
|
|
2196
|
+
/**
|
|
2197
|
+
* The function returns the size of an object.
|
|
2198
|
+
* @returns The size of the object, which is a number.
|
|
2199
|
+
*/
|
|
1896
2200
|
get size() {
|
|
1897
2201
|
return this._size;
|
|
1898
2202
|
}
|
|
1899
2203
|
/**
|
|
1900
|
-
* Time Complexity: O(n)
|
|
2204
|
+
* Time Complexity: O(n)
|
|
1901
2205
|
* Space Complexity: O(n)
|
|
2206
|
+
* where n is the number of elements in the linked list.
|
|
1902
2207
|
*/
|
|
1903
2208
|
/**
|
|
1904
|
-
* Time Complexity: O(n)
|
|
2209
|
+
* Time Complexity: O(n)
|
|
1905
2210
|
* Space Complexity: O(1)
|
|
1906
2211
|
*
|
|
1907
2212
|
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
@@ -1916,7 +2221,7 @@ var dataStructureTyped = (() => {
|
|
|
1916
2221
|
* Space Complexity: O(1)
|
|
1917
2222
|
*/
|
|
1918
2223
|
/**
|
|
1919
|
-
* Time Complexity: O(n)
|
|
2224
|
+
* Time Complexity: O(n)
|
|
1920
2225
|
* Space Complexity: O(1)
|
|
1921
2226
|
*
|
|
1922
2227
|
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
@@ -1940,11 +2245,7 @@ var dataStructureTyped = (() => {
|
|
|
1940
2245
|
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
1941
2246
|
*/
|
|
1942
2247
|
static fromArray(data) {
|
|
1943
|
-
|
|
1944
|
-
for (const item of data) {
|
|
1945
|
-
doublyLinkedList.push(item);
|
|
1946
|
-
}
|
|
1947
|
-
return doublyLinkedList;
|
|
2248
|
+
return new _DoublyLinkedList(data);
|
|
1948
2249
|
}
|
|
1949
2250
|
/**
|
|
1950
2251
|
* Time Complexity: O(1)
|
|
@@ -1997,7 +2298,7 @@ var dataStructureTyped = (() => {
|
|
|
1997
2298
|
return removedNode.value;
|
|
1998
2299
|
}
|
|
1999
2300
|
/**
|
|
2000
|
-
* Time Complexity: O(n)
|
|
2301
|
+
* Time Complexity: O(n)
|
|
2001
2302
|
* Space Complexity: O(1)
|
|
2002
2303
|
*/
|
|
2003
2304
|
/**
|
|
@@ -2023,7 +2324,7 @@ var dataStructureTyped = (() => {
|
|
|
2023
2324
|
return removedNode.value;
|
|
2024
2325
|
}
|
|
2025
2326
|
/**
|
|
2026
|
-
* Time Complexity: O(n)
|
|
2327
|
+
* Time Complexity: O(n)
|
|
2027
2328
|
* Space Complexity: O(1)
|
|
2028
2329
|
*/
|
|
2029
2330
|
/**
|
|
@@ -2048,20 +2349,20 @@ var dataStructureTyped = (() => {
|
|
|
2048
2349
|
return true;
|
|
2049
2350
|
}
|
|
2050
2351
|
/**
|
|
2051
|
-
* Time Complexity: O(n)
|
|
2352
|
+
* Time Complexity: O(n)
|
|
2052
2353
|
* Space Complexity: O(1)
|
|
2053
2354
|
*/
|
|
2054
2355
|
/**
|
|
2055
|
-
* Time Complexity: O(n)
|
|
2356
|
+
* Time Complexity: O(n)
|
|
2056
2357
|
* Space Complexity: O(1)
|
|
2057
2358
|
*
|
|
2058
|
-
* The `
|
|
2359
|
+
* The `at` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
|
|
2059
2360
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
2060
2361
|
* retrieve from the list.
|
|
2061
2362
|
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
|
2062
2363
|
* or the linked list is empty, it will return undefined.
|
|
2063
2364
|
*/
|
|
2064
|
-
|
|
2365
|
+
at(index) {
|
|
2065
2366
|
if (index < 0 || index >= this.size)
|
|
2066
2367
|
return void 0;
|
|
2067
2368
|
let current = this.head;
|
|
@@ -2071,11 +2372,11 @@ var dataStructureTyped = (() => {
|
|
|
2071
2372
|
return current.value;
|
|
2072
2373
|
}
|
|
2073
2374
|
/**
|
|
2074
|
-
* Time Complexity: O(n)
|
|
2375
|
+
* Time Complexity: O(n)
|
|
2075
2376
|
* Space Complexity: O(1)
|
|
2076
2377
|
*/
|
|
2077
2378
|
/**
|
|
2078
|
-
* Time Complexity: O(n)
|
|
2379
|
+
* Time Complexity: O(n)
|
|
2079
2380
|
* Space Complexity: O(1)
|
|
2080
2381
|
*
|
|
2081
2382
|
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or undefined if the index is out of
|
|
@@ -2095,11 +2396,11 @@ var dataStructureTyped = (() => {
|
|
|
2095
2396
|
return current;
|
|
2096
2397
|
}
|
|
2097
2398
|
/**
|
|
2098
|
-
* Time Complexity: O(n)
|
|
2399
|
+
* Time Complexity: O(n)
|
|
2099
2400
|
* Space Complexity: O(1)
|
|
2100
2401
|
*/
|
|
2101
2402
|
/**
|
|
2102
|
-
* Time Complexity: O(n)
|
|
2403
|
+
* Time Complexity: O(n)
|
|
2103
2404
|
* Space Complexity: O(1)
|
|
2104
2405
|
*
|
|
2105
2406
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
@@ -2119,11 +2420,11 @@ var dataStructureTyped = (() => {
|
|
|
2119
2420
|
return void 0;
|
|
2120
2421
|
}
|
|
2121
2422
|
/**
|
|
2122
|
-
* Time Complexity: O(n)
|
|
2423
|
+
* Time Complexity: O(n)
|
|
2123
2424
|
* Space Complexity: O(1)
|
|
2124
2425
|
*/
|
|
2125
2426
|
/**
|
|
2126
|
-
* Time Complexity: O(n)
|
|
2427
|
+
* Time Complexity: O(n)
|
|
2127
2428
|
* Space Complexity: O(1)
|
|
2128
2429
|
*
|
|
2129
2430
|
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
@@ -2156,11 +2457,12 @@ var dataStructureTyped = (() => {
|
|
|
2156
2457
|
return true;
|
|
2157
2458
|
}
|
|
2158
2459
|
/**
|
|
2159
|
-
* Time Complexity: O(n)
|
|
2460
|
+
* Time Complexity: O(n)
|
|
2160
2461
|
* Space Complexity: O(1)
|
|
2462
|
+
* where n is the number of elements in the linked list.
|
|
2161
2463
|
*/
|
|
2162
2464
|
/**
|
|
2163
|
-
* Time Complexity: O(n)
|
|
2465
|
+
* Time Complexity: O(n)
|
|
2164
2466
|
* Space Complexity: O(1)
|
|
2165
2467
|
*
|
|
2166
2468
|
* The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
@@ -2196,11 +2498,11 @@ var dataStructureTyped = (() => {
|
|
|
2196
2498
|
return false;
|
|
2197
2499
|
}
|
|
2198
2500
|
/**
|
|
2199
|
-
* Time Complexity: O(n)
|
|
2501
|
+
* Time Complexity: O(n)
|
|
2200
2502
|
* Space Complexity: O(1)
|
|
2201
2503
|
*/
|
|
2202
2504
|
/**
|
|
2203
|
-
* Time Complexity: O(n)
|
|
2505
|
+
* Time Complexity: O(n)
|
|
2204
2506
|
* Space Complexity: O(1)
|
|
2205
2507
|
*
|
|
2206
2508
|
* The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
@@ -2235,7 +2537,7 @@ var dataStructureTyped = (() => {
|
|
|
2235
2537
|
return false;
|
|
2236
2538
|
}
|
|
2237
2539
|
/**
|
|
2238
|
-
* Time Complexity: O(n)
|
|
2540
|
+
* Time Complexity: O(n)
|
|
2239
2541
|
* Space Complexity: O(1)
|
|
2240
2542
|
*
|
|
2241
2543
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
@@ -2264,7 +2566,7 @@ var dataStructureTyped = (() => {
|
|
|
2264
2566
|
return true;
|
|
2265
2567
|
}
|
|
2266
2568
|
/**
|
|
2267
|
-
* Time Complexity: O(n)
|
|
2569
|
+
* Time Complexity: O(n)
|
|
2268
2570
|
* Space Complexity: O(1)
|
|
2269
2571
|
*
|
|
2270
2572
|
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
|
|
@@ -2297,7 +2599,7 @@ var dataStructureTyped = (() => {
|
|
|
2297
2599
|
return false;
|
|
2298
2600
|
}
|
|
2299
2601
|
/**
|
|
2300
|
-
* Time Complexity: O(
|
|
2602
|
+
* Time Complexity: O(1)
|
|
2301
2603
|
* Space Complexity: O(1)
|
|
2302
2604
|
*/
|
|
2303
2605
|
/**
|
|
@@ -2308,7 +2610,7 @@ var dataStructureTyped = (() => {
|
|
|
2308
2610
|
return this.size === 0;
|
|
2309
2611
|
}
|
|
2310
2612
|
/**
|
|
2311
|
-
* Time Complexity: O(
|
|
2613
|
+
* Time Complexity: O(1)
|
|
2312
2614
|
* Space Complexity: O(1)
|
|
2313
2615
|
*/
|
|
2314
2616
|
/**
|
|
@@ -2320,35 +2622,11 @@ var dataStructureTyped = (() => {
|
|
|
2320
2622
|
this._size = 0;
|
|
2321
2623
|
}
|
|
2322
2624
|
/**
|
|
2323
|
-
* Time Complexity: O(n)
|
|
2324
|
-
* Space Complexity: O(1)
|
|
2325
|
-
*/
|
|
2326
|
-
/**
|
|
2327
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2328
|
-
* Space Complexity: O(1)
|
|
2329
|
-
*
|
|
2330
|
-
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
2331
|
-
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
2332
|
-
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
2333
|
-
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
2334
|
-
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
2335
|
-
*/
|
|
2336
|
-
find(callback) {
|
|
2337
|
-
let current = this.head;
|
|
2338
|
-
while (current) {
|
|
2339
|
-
if (callback(current.value)) {
|
|
2340
|
-
return current.value;
|
|
2341
|
-
}
|
|
2342
|
-
current = current.next;
|
|
2343
|
-
}
|
|
2344
|
-
return void 0;
|
|
2345
|
-
}
|
|
2346
|
-
/**
|
|
2347
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2625
|
+
* Time Complexity: O(n)
|
|
2348
2626
|
* Space Complexity: O(1)
|
|
2349
2627
|
*/
|
|
2350
2628
|
/**
|
|
2351
|
-
* Time Complexity: O(n)
|
|
2629
|
+
* Time Complexity: O(n)
|
|
2352
2630
|
* Space Complexity: O(1)
|
|
2353
2631
|
*
|
|
2354
2632
|
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
@@ -2370,11 +2648,11 @@ var dataStructureTyped = (() => {
|
|
|
2370
2648
|
return -1;
|
|
2371
2649
|
}
|
|
2372
2650
|
/**
|
|
2373
|
-
* Time Complexity: O(n)
|
|
2651
|
+
* Time Complexity: O(n)
|
|
2374
2652
|
* Space Complexity: O(n)
|
|
2375
2653
|
*/
|
|
2376
2654
|
/**
|
|
2377
|
-
* Time Complexity: O(n)
|
|
2655
|
+
* Time Complexity: O(n)
|
|
2378
2656
|
* Space Complexity: O(1)
|
|
2379
2657
|
*
|
|
2380
2658
|
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
@@ -2395,11 +2673,11 @@ var dataStructureTyped = (() => {
|
|
|
2395
2673
|
return void 0;
|
|
2396
2674
|
}
|
|
2397
2675
|
/**
|
|
2398
|
-
* Time Complexity: O(n)
|
|
2676
|
+
* Time Complexity: O(n)
|
|
2399
2677
|
* Space Complexity: O(n)
|
|
2400
2678
|
*/
|
|
2401
2679
|
/**
|
|
2402
|
-
* Time Complexity: O(n)
|
|
2680
|
+
* Time Complexity: O(n)
|
|
2403
2681
|
* Space Complexity: O(1)
|
|
2404
2682
|
*
|
|
2405
2683
|
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
@@ -2419,7 +2697,7 @@ var dataStructureTyped = (() => {
|
|
|
2419
2697
|
* Space Complexity: O(n)
|
|
2420
2698
|
*/
|
|
2421
2699
|
/**
|
|
2422
|
-
* Time Complexity: O(n)
|
|
2700
|
+
* Time Complexity: O(n)
|
|
2423
2701
|
* Space Complexity: O(n)
|
|
2424
2702
|
*
|
|
2425
2703
|
* The `toArray` function converts a linked list into an array.
|
|
@@ -2435,11 +2713,11 @@ var dataStructureTyped = (() => {
|
|
|
2435
2713
|
return array;
|
|
2436
2714
|
}
|
|
2437
2715
|
/**
|
|
2438
|
-
* Time Complexity: O(n)
|
|
2716
|
+
* Time Complexity: O(n)
|
|
2439
2717
|
* Space Complexity: O(n)
|
|
2440
2718
|
*/
|
|
2441
2719
|
/**
|
|
2442
|
-
* Time Complexity: O(n)
|
|
2720
|
+
* Time Complexity: O(n)
|
|
2443
2721
|
* Space Complexity: O(n)
|
|
2444
2722
|
*
|
|
2445
2723
|
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
@@ -2454,6 +2732,22 @@ var dataStructureTyped = (() => {
|
|
|
2454
2732
|
}
|
|
2455
2733
|
return array;
|
|
2456
2734
|
}
|
|
2735
|
+
/**
|
|
2736
|
+
* Time Complexity: O(n)
|
|
2737
|
+
* Space Complexity: O(n)
|
|
2738
|
+
*/
|
|
2739
|
+
/**
|
|
2740
|
+
* Time Complexity: O(n)
|
|
2741
|
+
* Space Complexity: O(n)
|
|
2742
|
+
*
|
|
2743
|
+
* The `clone` function creates a new instance of the `DoublyLinkedList` class with the same values
|
|
2744
|
+
* as the original list.
|
|
2745
|
+
* @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which
|
|
2746
|
+
* is a copy of the original list.
|
|
2747
|
+
*/
|
|
2748
|
+
clone() {
|
|
2749
|
+
return new _DoublyLinkedList(this.values());
|
|
2750
|
+
}
|
|
2457
2751
|
/**
|
|
2458
2752
|
* Time Complexity: O(1)
|
|
2459
2753
|
* Space Complexity: O(1)
|
|
@@ -2546,7 +2840,7 @@ var dataStructureTyped = (() => {
|
|
|
2546
2840
|
return this.pop();
|
|
2547
2841
|
}
|
|
2548
2842
|
/**
|
|
2549
|
-
* Time Complexity: O(n)
|
|
2843
|
+
* Time Complexity: O(n)
|
|
2550
2844
|
* Space Complexity: O(1)
|
|
2551
2845
|
*/
|
|
2552
2846
|
/**
|
|
@@ -2561,7 +2855,7 @@ var dataStructureTyped = (() => {
|
|
|
2561
2855
|
return this.shift();
|
|
2562
2856
|
}
|
|
2563
2857
|
/**
|
|
2564
|
-
* Time Complexity: O(n)
|
|
2858
|
+
* Time Complexity: O(n)
|
|
2565
2859
|
* Space Complexity: O(1)
|
|
2566
2860
|
*/
|
|
2567
2861
|
/**
|
|
@@ -2599,6 +2893,14 @@ var dataStructureTyped = (() => {
|
|
|
2599
2893
|
}
|
|
2600
2894
|
};
|
|
2601
2895
|
var SkipList = class {
|
|
2896
|
+
/**
|
|
2897
|
+
* The constructor function initializes a SkipLinkedList object with optional options and elements.
|
|
2898
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
2899
|
+
* is used to initialize the SkipLinkedList with the given key-value pairs. If no elements are
|
|
2900
|
+
* provided, the SkipLinkedList will be empty.
|
|
2901
|
+
* @param {SkipLinkedListOptions} [options] - The `options` parameter is an optional object that can
|
|
2902
|
+
* contain two properties:
|
|
2903
|
+
*/
|
|
2602
2904
|
constructor(elements = [], options) {
|
|
2603
2905
|
__publicField(this, "_head", new SkipListNode(void 0, void 0, this.maxLevel));
|
|
2604
2906
|
__publicField(this, "_level", 0);
|
|
@@ -2616,25 +2918,41 @@ var dataStructureTyped = (() => {
|
|
|
2616
2918
|
this.add(key, value);
|
|
2617
2919
|
}
|
|
2618
2920
|
}
|
|
2921
|
+
/**
|
|
2922
|
+
* The function returns the head node of a SkipList.
|
|
2923
|
+
* @returns The method is returning a SkipListNode object with generic key type K and value type V.
|
|
2924
|
+
*/
|
|
2619
2925
|
get head() {
|
|
2620
2926
|
return this._head;
|
|
2621
2927
|
}
|
|
2928
|
+
/**
|
|
2929
|
+
* The function returns the value of the private variable _level.
|
|
2930
|
+
* @returns The level property of the object.
|
|
2931
|
+
*/
|
|
2622
2932
|
get level() {
|
|
2623
2933
|
return this._level;
|
|
2624
2934
|
}
|
|
2935
|
+
/**
|
|
2936
|
+
* The function returns the maximum level.
|
|
2937
|
+
* @returns The value of the variable `_maxLevel` is being returned.
|
|
2938
|
+
*/
|
|
2625
2939
|
get maxLevel() {
|
|
2626
2940
|
return this._maxLevel;
|
|
2627
2941
|
}
|
|
2942
|
+
/**
|
|
2943
|
+
* The function returns the probability value.
|
|
2944
|
+
* @returns The probability value stored in the private variable `_probability` is being returned.
|
|
2945
|
+
*/
|
|
2628
2946
|
get probability() {
|
|
2629
2947
|
return this._probability;
|
|
2630
2948
|
}
|
|
2631
2949
|
/**
|
|
2632
|
-
* Time Complexity: O(log n)
|
|
2633
|
-
* Space Complexity: O(1)
|
|
2950
|
+
* Time Complexity: O(log n)
|
|
2951
|
+
* Space Complexity: O(1)
|
|
2634
2952
|
*/
|
|
2635
2953
|
/**
|
|
2636
|
-
* Time Complexity: O(1)
|
|
2637
|
-
* Space Complexity: O(1)
|
|
2954
|
+
* Time Complexity: O(1)
|
|
2955
|
+
* Space Complexity: O(1)
|
|
2638
2956
|
*
|
|
2639
2957
|
* Get the value of the first element (the smallest element) in the Skip List.
|
|
2640
2958
|
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
@@ -2644,12 +2962,12 @@ var dataStructureTyped = (() => {
|
|
|
2644
2962
|
return firstNode ? firstNode.value : void 0;
|
|
2645
2963
|
}
|
|
2646
2964
|
/**
|
|
2647
|
-
* Time Complexity: O(log n)
|
|
2648
|
-
* Space Complexity: O(1)
|
|
2965
|
+
* Time Complexity: O(log n)
|
|
2966
|
+
* Space Complexity: O(1)
|
|
2649
2967
|
*/
|
|
2650
2968
|
/**
|
|
2651
|
-
* Time Complexity: O(log n)
|
|
2652
|
-
* Space Complexity: O(1)
|
|
2969
|
+
* Time Complexity: O(log n)
|
|
2970
|
+
* Space Complexity: O(1)
|
|
2653
2971
|
*
|
|
2654
2972
|
* Get the value of the last element (the largest element) in the Skip List.
|
|
2655
2973
|
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
@@ -2664,12 +2982,12 @@ var dataStructureTyped = (() => {
|
|
|
2664
2982
|
return current.value;
|
|
2665
2983
|
}
|
|
2666
2984
|
/**
|
|
2667
|
-
* Time Complexity: O(log n)
|
|
2668
|
-
* Space Complexity: O(1)
|
|
2985
|
+
* Time Complexity: O(log n)
|
|
2986
|
+
* Space Complexity: O(1)
|
|
2669
2987
|
*/
|
|
2670
2988
|
/**
|
|
2671
|
-
* Time Complexity: O(log n)
|
|
2672
|
-
* Space Complexity: O(1)
|
|
2989
|
+
* Time Complexity: O(log n)
|
|
2990
|
+
* Space Complexity: O(1)
|
|
2673
2991
|
*
|
|
2674
2992
|
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
2675
2993
|
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
@@ -2695,12 +3013,12 @@ var dataStructureTyped = (() => {
|
|
|
2695
3013
|
}
|
|
2696
3014
|
}
|
|
2697
3015
|
/**
|
|
2698
|
-
* Time Complexity: O(log n)
|
|
2699
|
-
* Space Complexity: O(1)
|
|
3016
|
+
* Time Complexity: O(log n)
|
|
3017
|
+
* Space Complexity: O(1)
|
|
2700
3018
|
*/
|
|
2701
3019
|
/**
|
|
2702
|
-
* Time Complexity: O(log n)
|
|
2703
|
-
* Space Complexity: O(1)
|
|
3020
|
+
* Time Complexity: O(log n)
|
|
3021
|
+
* Space Complexity: O(1)
|
|
2704
3022
|
*
|
|
2705
3023
|
* The function `get` retrieves the value associated with a given key from a skip list data structure.
|
|
2706
3024
|
* @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
|
|
@@ -2721,23 +3039,25 @@ var dataStructureTyped = (() => {
|
|
|
2721
3039
|
return void 0;
|
|
2722
3040
|
}
|
|
2723
3041
|
/**
|
|
2724
|
-
* Time Complexity: O(
|
|
2725
|
-
* Space Complexity: O(1)
|
|
3042
|
+
* Time Complexity: O(log n)
|
|
3043
|
+
* Space Complexity: O(1)
|
|
2726
3044
|
*/
|
|
2727
3045
|
/**
|
|
2728
|
-
*
|
|
2729
|
-
*
|
|
3046
|
+
* The function checks if a key exists in a data structure.
|
|
3047
|
+
* @param {K} key - The parameter "key" is of type K, which represents the type of the key being
|
|
3048
|
+
* checked.
|
|
3049
|
+
* @returns a boolean value.
|
|
2730
3050
|
*/
|
|
2731
3051
|
has(key) {
|
|
2732
3052
|
return this.get(key) !== void 0;
|
|
2733
3053
|
}
|
|
2734
3054
|
/**
|
|
2735
|
-
* Time Complexity: O(log n)
|
|
2736
|
-
* Space Complexity: O(1)
|
|
3055
|
+
* Time Complexity: O(log n)
|
|
3056
|
+
* Space Complexity: O(1)
|
|
2737
3057
|
*/
|
|
2738
3058
|
/**
|
|
2739
|
-
* Time Complexity: O(log n)
|
|
2740
|
-
* Space Complexity: O(1)
|
|
3059
|
+
* Time Complexity: O(log n)
|
|
3060
|
+
* Space Complexity: O(1)
|
|
2741
3061
|
*
|
|
2742
3062
|
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
2743
3063
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
@@ -2769,12 +3089,12 @@ var dataStructureTyped = (() => {
|
|
|
2769
3089
|
return false;
|
|
2770
3090
|
}
|
|
2771
3091
|
/**
|
|
2772
|
-
* Time Complexity: O(log n)
|
|
2773
|
-
* Space Complexity: O(1)
|
|
3092
|
+
* Time Complexity: O(log n)
|
|
3093
|
+
* Space Complexity: O(1)
|
|
2774
3094
|
*/
|
|
2775
3095
|
/**
|
|
2776
|
-
* Time Complexity: O(log n)
|
|
2777
|
-
* Space Complexity: O(1)
|
|
3096
|
+
* Time Complexity: O(log n)
|
|
3097
|
+
* Space Complexity: O(1)
|
|
2778
3098
|
*
|
|
2779
3099
|
* Get the value of the first element in the Skip List that is greater than the given key.
|
|
2780
3100
|
* @param key - the given key.
|
|
@@ -2791,12 +3111,12 @@ var dataStructureTyped = (() => {
|
|
|
2791
3111
|
return nextNode ? nextNode.value : void 0;
|
|
2792
3112
|
}
|
|
2793
3113
|
/**
|
|
2794
|
-
* Time Complexity: O(log n)
|
|
2795
|
-
* Space Complexity: O(1)
|
|
3114
|
+
* Time Complexity: O(log n)
|
|
3115
|
+
* Space Complexity: O(1)
|
|
2796
3116
|
*/
|
|
2797
3117
|
/**
|
|
2798
|
-
* Time Complexity: O(log n)
|
|
2799
|
-
* Space Complexity: O(1)
|
|
3118
|
+
* Time Complexity: O(log n)
|
|
3119
|
+
* Space Complexity: O(1)
|
|
2800
3120
|
*
|
|
2801
3121
|
* Get the value of the last element in the Skip List that is less than the given key.
|
|
2802
3122
|
* @param key - the given key.
|
|
@@ -2816,12 +3136,13 @@ var dataStructureTyped = (() => {
|
|
|
2816
3136
|
return lastLess ? lastLess.value : void 0;
|
|
2817
3137
|
}
|
|
2818
3138
|
/**
|
|
2819
|
-
* Time Complexity: O(maxLevel)
|
|
2820
|
-
* Space Complexity: O(1)
|
|
3139
|
+
* Time Complexity: O(maxLevel)
|
|
3140
|
+
* Space Complexity: O(1)
|
|
3141
|
+
* where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
|
|
2821
3142
|
*/
|
|
2822
3143
|
/**
|
|
2823
|
-
* Time Complexity: O(maxLevel)
|
|
2824
|
-
* Space Complexity: O(1)
|
|
3144
|
+
* Time Complexity: O(maxLevel)
|
|
3145
|
+
* Space Complexity: O(1)
|
|
2825
3146
|
*
|
|
2826
3147
|
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
|
|
2827
3148
|
* @returns the level, which is a number.
|
|
@@ -2851,6 +3172,10 @@ var dataStructureTyped = (() => {
|
|
|
2851
3172
|
this.push(el);
|
|
2852
3173
|
}
|
|
2853
3174
|
}
|
|
3175
|
+
/**
|
|
3176
|
+
* The elements function returns the elements of this set.
|
|
3177
|
+
* @return An array of elements
|
|
3178
|
+
*/
|
|
2854
3179
|
get elements() {
|
|
2855
3180
|
return this._elements;
|
|
2856
3181
|
}
|
|
@@ -2934,8 +3259,26 @@ var dataStructureTyped = (() => {
|
|
|
2934
3259
|
return this.elements.pop();
|
|
2935
3260
|
}
|
|
2936
3261
|
/**
|
|
2937
|
-
*
|
|
2938
|
-
*
|
|
3262
|
+
* The delete function removes an element from the stack.
|
|
3263
|
+
* @param element: E Specify the element to be deleted
|
|
3264
|
+
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
3265
|
+
*/
|
|
3266
|
+
delete(element) {
|
|
3267
|
+
const index = this.elements.indexOf(element);
|
|
3268
|
+
return this.deleteAt(index);
|
|
3269
|
+
}
|
|
3270
|
+
/**
|
|
3271
|
+
* The deleteAt function deletes the element at a given index.
|
|
3272
|
+
* @param index: number Determine the index of the element to be deleted
|
|
3273
|
+
* @return A boolean value
|
|
3274
|
+
*/
|
|
3275
|
+
deleteAt(index) {
|
|
3276
|
+
const spliced = this.elements.splice(index, 1);
|
|
3277
|
+
return spliced.length === 1;
|
|
3278
|
+
}
|
|
3279
|
+
/**
|
|
3280
|
+
* Time Complexity: O(n)
|
|
3281
|
+
* Space Complexity: O(n)
|
|
2939
3282
|
*/
|
|
2940
3283
|
/**
|
|
2941
3284
|
* Time Complexity: O(n)
|
|
@@ -3052,9 +3395,17 @@ var dataStructureTyped = (() => {
|
|
|
3052
3395
|
this.push(el);
|
|
3053
3396
|
}
|
|
3054
3397
|
}
|
|
3398
|
+
/**
|
|
3399
|
+
* The elements function returns the elements of this set.
|
|
3400
|
+
* @return An array of the elements in the stack
|
|
3401
|
+
*/
|
|
3055
3402
|
get elements() {
|
|
3056
3403
|
return this._elements;
|
|
3057
3404
|
}
|
|
3405
|
+
/**
|
|
3406
|
+
* The offset function returns the offset of the current page.
|
|
3407
|
+
* @return The value of the private variable _offset
|
|
3408
|
+
*/
|
|
3058
3409
|
get offset() {
|
|
3059
3410
|
return this._offset;
|
|
3060
3411
|
}
|
|
@@ -3145,6 +3496,24 @@ var dataStructureTyped = (() => {
|
|
|
3145
3496
|
this._offset = 0;
|
|
3146
3497
|
return first;
|
|
3147
3498
|
}
|
|
3499
|
+
/**
|
|
3500
|
+
* The delete function removes an element from the list.
|
|
3501
|
+
* @param element: E Specify the element to be deleted
|
|
3502
|
+
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
3503
|
+
*/
|
|
3504
|
+
delete(element) {
|
|
3505
|
+
const index = this.elements.indexOf(element);
|
|
3506
|
+
return this.deleteAt(index);
|
|
3507
|
+
}
|
|
3508
|
+
/**
|
|
3509
|
+
* The deleteAt function deletes the element at a given index.
|
|
3510
|
+
* @param index: number Determine the index of the element to be deleted
|
|
3511
|
+
* @return A boolean value
|
|
3512
|
+
*/
|
|
3513
|
+
deleteAt(index) {
|
|
3514
|
+
const spliced = this.elements.splice(index, 1);
|
|
3515
|
+
return spliced.length === 1;
|
|
3516
|
+
}
|
|
3148
3517
|
/**
|
|
3149
3518
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
3150
3519
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -3213,7 +3582,7 @@ var dataStructureTyped = (() => {
|
|
|
3213
3582
|
*
|
|
3214
3583
|
* @param index
|
|
3215
3584
|
*/
|
|
3216
|
-
|
|
3585
|
+
at(index) {
|
|
3217
3586
|
return this.elements[index];
|
|
3218
3587
|
}
|
|
3219
3588
|
/**
|
|
@@ -3252,12 +3621,13 @@ var dataStructureTyped = (() => {
|
|
|
3252
3621
|
this._offset = 0;
|
|
3253
3622
|
}
|
|
3254
3623
|
/**
|
|
3255
|
-
* Time Complexity: O(n)
|
|
3256
|
-
* Space Complexity: O(n)
|
|
3624
|
+
* Time Complexity: O(n)
|
|
3625
|
+
* Space Complexity: O(n)
|
|
3626
|
+
* where n is the number of elements in the queue. It creates a shallow copy of the internal array. the space required is proportional to the number of elements in the queue.
|
|
3257
3627
|
*/
|
|
3258
3628
|
/**
|
|
3259
|
-
* Time Complexity: O(n)
|
|
3260
|
-
* Space Complexity: O(n)
|
|
3629
|
+
* Time Complexity: O(n)
|
|
3630
|
+
* Space Complexity: O(n)
|
|
3261
3631
|
*
|
|
3262
3632
|
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
3263
3633
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
@@ -3333,7 +3703,7 @@ var dataStructureTyped = (() => {
|
|
|
3333
3703
|
}
|
|
3334
3704
|
}
|
|
3335
3705
|
};
|
|
3336
|
-
var LinkedListQueue = class extends SinglyLinkedList {
|
|
3706
|
+
var LinkedListQueue = class _LinkedListQueue extends SinglyLinkedList {
|
|
3337
3707
|
/**
|
|
3338
3708
|
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
3339
3709
|
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
@@ -3363,10 +3733,36 @@ var dataStructureTyped = (() => {
|
|
|
3363
3733
|
peek() {
|
|
3364
3734
|
return this.first;
|
|
3365
3735
|
}
|
|
3736
|
+
/**
|
|
3737
|
+
* Time Complexity: O(n)
|
|
3738
|
+
* Space Complexity: O(n)
|
|
3739
|
+
*/
|
|
3740
|
+
/**
|
|
3741
|
+
* Time Complexity: O(n)
|
|
3742
|
+
* Space Complexity: O(n)
|
|
3743
|
+
* The `clone` function returns a new instance of the `LinkedListQueue` class with the same values as
|
|
3744
|
+
* the current instance.
|
|
3745
|
+
* @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
|
|
3746
|
+
* values as the original `LinkedListQueue`.
|
|
3747
|
+
*/
|
|
3748
|
+
clone() {
|
|
3749
|
+
return new _LinkedListQueue(this.values());
|
|
3750
|
+
}
|
|
3366
3751
|
};
|
|
3367
3752
|
|
|
3368
3753
|
// src/data-structures/queue/deque.ts
|
|
3369
3754
|
var Deque = class _Deque extends IterableElementBase {
|
|
3755
|
+
/**
|
|
3756
|
+
* The constructor initializes a Deque object with an optional iterable of elements and options.
|
|
3757
|
+
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
3758
|
+
* elements to be added to the deque. It can also be an object with a `length` or `size` property
|
|
3759
|
+
* that represents the number of elements in the iterable object. If no elements are provided, an
|
|
3760
|
+
* empty deque
|
|
3761
|
+
* @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain
|
|
3762
|
+
* configuration options for the deque. In this code, it is used to set the `bucketSize` option,
|
|
3763
|
+
* which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
|
|
3764
|
+
* or is not a number
|
|
3765
|
+
*/
|
|
3370
3766
|
constructor(elements = [], options) {
|
|
3371
3767
|
super();
|
|
3372
3768
|
__publicField(this, "_bucketFirst", 0);
|
|
@@ -3405,9 +3801,26 @@ var dataStructureTyped = (() => {
|
|
|
3405
3801
|
this.push(element);
|
|
3406
3802
|
}
|
|
3407
3803
|
}
|
|
3804
|
+
/**
|
|
3805
|
+
* The bucketSize function returns the size of the bucket.
|
|
3806
|
+
*
|
|
3807
|
+
* @return The size of the bucket
|
|
3808
|
+
*/
|
|
3809
|
+
get bucketSize() {
|
|
3810
|
+
return this._bucketSize;
|
|
3811
|
+
}
|
|
3812
|
+
/**
|
|
3813
|
+
* The buckets function returns the buckets property of the object.
|
|
3814
|
+
*
|
|
3815
|
+
* @return The buckets property
|
|
3816
|
+
*/
|
|
3408
3817
|
get buckets() {
|
|
3409
3818
|
return this._buckets;
|
|
3410
3819
|
}
|
|
3820
|
+
/**
|
|
3821
|
+
* The size function returns the number of items in the stack.
|
|
3822
|
+
* @return The number of values in the set
|
|
3823
|
+
*/
|
|
3411
3824
|
get size() {
|
|
3412
3825
|
return this._size;
|
|
3413
3826
|
}
|
|
@@ -3421,6 +3834,10 @@ var dataStructureTyped = (() => {
|
|
|
3421
3834
|
return;
|
|
3422
3835
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
3423
3836
|
}
|
|
3837
|
+
/**
|
|
3838
|
+
* The last function returns the last element in the queue.
|
|
3839
|
+
* @return The last element in the array
|
|
3840
|
+
*/
|
|
3424
3841
|
get last() {
|
|
3425
3842
|
if (this.size === 0)
|
|
3426
3843
|
return;
|
|
@@ -3573,7 +3990,7 @@ var dataStructureTyped = (() => {
|
|
|
3573
3990
|
*begin() {
|
|
3574
3991
|
let index = 0;
|
|
3575
3992
|
while (index < this.size) {
|
|
3576
|
-
yield this.
|
|
3993
|
+
yield this.at(index);
|
|
3577
3994
|
index++;
|
|
3578
3995
|
}
|
|
3579
3996
|
}
|
|
@@ -3584,7 +4001,7 @@ var dataStructureTyped = (() => {
|
|
|
3584
4001
|
*reverseBegin() {
|
|
3585
4002
|
let index = this.size - 1;
|
|
3586
4003
|
while (index >= 0) {
|
|
3587
|
-
yield this.
|
|
4004
|
+
yield this.at(index);
|
|
3588
4005
|
index--;
|
|
3589
4006
|
}
|
|
3590
4007
|
}
|
|
@@ -3596,13 +4013,13 @@ var dataStructureTyped = (() => {
|
|
|
3596
4013
|
* Time Complexity: O(1)
|
|
3597
4014
|
* Space Complexity: O(1)
|
|
3598
4015
|
*
|
|
3599
|
-
* The `
|
|
4016
|
+
* The `at` function retrieves an element at a specified position in an array-like data structure.
|
|
3600
4017
|
* @param {number} pos - The `pos` parameter represents the position of the element that you want to
|
|
3601
4018
|
* retrieve from the data structure. It is of type `number` and should be a valid index within the
|
|
3602
4019
|
* range of the data structure.
|
|
3603
4020
|
* @returns The element at the specified position in the data structure is being returned.
|
|
3604
4021
|
*/
|
|
3605
|
-
|
|
4022
|
+
at(pos) {
|
|
3606
4023
|
rangeCheck(pos, 0, this.size - 1);
|
|
3607
4024
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
3608
4025
|
return this._buckets[bucketIndex][indexInBucket];
|
|
@@ -3658,9 +4075,9 @@ var dataStructureTyped = (() => {
|
|
|
3658
4075
|
} else {
|
|
3659
4076
|
const arr = [];
|
|
3660
4077
|
for (let i = pos; i < this.size; ++i) {
|
|
3661
|
-
arr.push(this.
|
|
4078
|
+
arr.push(this.at(i));
|
|
3662
4079
|
}
|
|
3663
|
-
this.cut(pos - 1);
|
|
4080
|
+
this.cut(pos - 1, true);
|
|
3664
4081
|
for (let i = 0; i < num; ++i)
|
|
3665
4082
|
this.push(element);
|
|
3666
4083
|
for (let i = 0; i < arr.length; ++i)
|
|
@@ -3680,18 +4097,46 @@ var dataStructureTyped = (() => {
|
|
|
3680
4097
|
* updated size.
|
|
3681
4098
|
* @param {number} pos - The `pos` parameter represents the position at which the string should be
|
|
3682
4099
|
* cut. It is a number that indicates the index of the character where the cut should be made.
|
|
4100
|
+
* @param {boolean} isCutSelf - If true, the original deque will not be cut, and return a new deque
|
|
3683
4101
|
* @returns The method is returning the updated size of the data structure.
|
|
3684
4102
|
*/
|
|
3685
|
-
cut(pos) {
|
|
3686
|
-
if (
|
|
3687
|
-
|
|
3688
|
-
|
|
4103
|
+
cut(pos, isCutSelf = false) {
|
|
4104
|
+
if (isCutSelf) {
|
|
4105
|
+
if (pos < 0) {
|
|
4106
|
+
this.clear();
|
|
4107
|
+
return this;
|
|
4108
|
+
}
|
|
4109
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
4110
|
+
this._bucketLast = bucketIndex;
|
|
4111
|
+
this._lastInBucket = indexInBucket;
|
|
4112
|
+
this._size = pos + 1;
|
|
4113
|
+
return this;
|
|
4114
|
+
} else {
|
|
4115
|
+
const newDeque = new _Deque([], { bucketSize: this._bucketSize });
|
|
4116
|
+
for (let i = 0; i <= pos; i++) {
|
|
4117
|
+
newDeque.push(this.at(i));
|
|
4118
|
+
}
|
|
4119
|
+
return newDeque;
|
|
4120
|
+
}
|
|
4121
|
+
}
|
|
4122
|
+
cutRest(pos, isCutSelf = false) {
|
|
4123
|
+
if (isCutSelf) {
|
|
4124
|
+
if (pos < 0) {
|
|
4125
|
+
this.clear();
|
|
4126
|
+
return this;
|
|
4127
|
+
}
|
|
4128
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
4129
|
+
this._bucketFirst = bucketIndex;
|
|
4130
|
+
this._firstInBucket = indexInBucket;
|
|
4131
|
+
this._size = this._size - pos;
|
|
4132
|
+
return this;
|
|
4133
|
+
} else {
|
|
4134
|
+
const newDeque = new _Deque([], { bucketSize: this._bucketSize });
|
|
4135
|
+
for (let i = pos; i < this.size; i++) {
|
|
4136
|
+
newDeque.push(this.at(i));
|
|
4137
|
+
}
|
|
4138
|
+
return newDeque;
|
|
3689
4139
|
}
|
|
3690
|
-
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
3691
|
-
this._bucketLast = bucketIndex;
|
|
3692
|
-
this._lastInBucket = indexInBucket;
|
|
3693
|
-
this._size = pos + 1;
|
|
3694
|
-
return this.size;
|
|
3695
4140
|
}
|
|
3696
4141
|
/**
|
|
3697
4142
|
* Time Complexity: O(n)
|
|
@@ -3748,14 +4193,14 @@ var dataStructureTyped = (() => {
|
|
|
3748
4193
|
let i = 0;
|
|
3749
4194
|
let index = 0;
|
|
3750
4195
|
while (i < size) {
|
|
3751
|
-
const oldElement = this.
|
|
4196
|
+
const oldElement = this.at(i);
|
|
3752
4197
|
if (oldElement !== element) {
|
|
3753
4198
|
this.setAt(index, oldElement);
|
|
3754
4199
|
index += 1;
|
|
3755
4200
|
}
|
|
3756
4201
|
i += 1;
|
|
3757
4202
|
}
|
|
3758
|
-
this.cut(index - 1);
|
|
4203
|
+
this.cut(index - 1, true);
|
|
3759
4204
|
return true;
|
|
3760
4205
|
}
|
|
3761
4206
|
/**
|
|
@@ -3799,15 +4244,15 @@ var dataStructureTyped = (() => {
|
|
|
3799
4244
|
return this;
|
|
3800
4245
|
}
|
|
3801
4246
|
let index = 1;
|
|
3802
|
-
let prev = this.
|
|
4247
|
+
let prev = this.at(0);
|
|
3803
4248
|
for (let i = 1; i < this.size; ++i) {
|
|
3804
|
-
const cur = this.
|
|
4249
|
+
const cur = this.at(i);
|
|
3805
4250
|
if (cur !== prev) {
|
|
3806
4251
|
prev = cur;
|
|
3807
4252
|
this.setAt(index++, cur);
|
|
3808
4253
|
}
|
|
3809
4254
|
}
|
|
3810
|
-
this.cut(index - 1);
|
|
4255
|
+
this.cut(index - 1, true);
|
|
3811
4256
|
return this;
|
|
3812
4257
|
}
|
|
3813
4258
|
/**
|
|
@@ -3827,7 +4272,7 @@ var dataStructureTyped = (() => {
|
|
|
3827
4272
|
sort(comparator) {
|
|
3828
4273
|
const arr = [];
|
|
3829
4274
|
for (let i = 0; i < this.size; ++i) {
|
|
3830
|
-
arr.push(this.
|
|
4275
|
+
arr.push(this.at(i));
|
|
3831
4276
|
}
|
|
3832
4277
|
arr.sort(comparator);
|
|
3833
4278
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -3870,30 +4315,6 @@ var dataStructureTyped = (() => {
|
|
|
3870
4315
|
this._bucketLast = newBuckets.length - 1;
|
|
3871
4316
|
this._buckets = newBuckets;
|
|
3872
4317
|
}
|
|
3873
|
-
/**
|
|
3874
|
-
* Time Complexity: O(n)
|
|
3875
|
-
* Space Complexity: O(1)
|
|
3876
|
-
*/
|
|
3877
|
-
/**
|
|
3878
|
-
* Time Complexity: O(n)
|
|
3879
|
-
* Space Complexity: O(1)
|
|
3880
|
-
*
|
|
3881
|
-
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
3882
|
-
* the callback function returns true, or undefined if no such element is found.
|
|
3883
|
-
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
3884
|
-
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
3885
|
-
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
3886
|
-
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
3887
|
-
*/
|
|
3888
|
-
find(callback) {
|
|
3889
|
-
for (let i = 0; i < this.size; ++i) {
|
|
3890
|
-
const element = this.getAt(i);
|
|
3891
|
-
if (callback(element, i, this)) {
|
|
3892
|
-
return element;
|
|
3893
|
-
}
|
|
3894
|
-
}
|
|
3895
|
-
return;
|
|
3896
|
-
}
|
|
3897
4318
|
/**
|
|
3898
4319
|
* Time Complexity: O(n)
|
|
3899
4320
|
* Space Complexity: O(1)
|
|
@@ -3911,7 +4332,7 @@ var dataStructureTyped = (() => {
|
|
|
3911
4332
|
*/
|
|
3912
4333
|
indexOf(element) {
|
|
3913
4334
|
for (let i = 0; i < this.size; ++i) {
|
|
3914
|
-
if (this.
|
|
4335
|
+
if (this.at(i) === element) {
|
|
3915
4336
|
return i;
|
|
3916
4337
|
}
|
|
3917
4338
|
}
|
|
@@ -3931,6 +4352,22 @@ var dataStructureTyped = (() => {
|
|
|
3931
4352
|
toArray() {
|
|
3932
4353
|
return [...this];
|
|
3933
4354
|
}
|
|
4355
|
+
/**
|
|
4356
|
+
* Time Complexity: O(n)
|
|
4357
|
+
* Space Complexity: O(n)
|
|
4358
|
+
*/
|
|
4359
|
+
/**
|
|
4360
|
+
* Time Complexity: O(n)
|
|
4361
|
+
* Space Complexity: O(n)
|
|
4362
|
+
*
|
|
4363
|
+
* The `clone()` function returns a new instance of the `Deque` class with the same elements and
|
|
4364
|
+
* bucket size as the original instance.
|
|
4365
|
+
* @returns The `clone()` method is returning a new instance of the `Deque` class with the same
|
|
4366
|
+
* elements as the original deque (`this`) and the same bucket size.
|
|
4367
|
+
*/
|
|
4368
|
+
clone() {
|
|
4369
|
+
return new _Deque([...this], { bucketSize: this.bucketSize });
|
|
4370
|
+
}
|
|
3934
4371
|
/**
|
|
3935
4372
|
* Time Complexity: O(n)
|
|
3936
4373
|
* Space Complexity: O(n)
|
|
@@ -4048,7 +4485,7 @@ var dataStructureTyped = (() => {
|
|
|
4048
4485
|
*/
|
|
4049
4486
|
*_getIterator() {
|
|
4050
4487
|
for (let i = 0; i < this.size; ++i) {
|
|
4051
|
-
yield this.
|
|
4488
|
+
yield this.at(i);
|
|
4052
4489
|
}
|
|
4053
4490
|
}
|
|
4054
4491
|
/**
|
|
@@ -4116,6 +4553,16 @@ var dataStructureTyped = (() => {
|
|
|
4116
4553
|
|
|
4117
4554
|
// src/data-structures/heap/heap.ts
|
|
4118
4555
|
var Heap = class _Heap extends IterableElementBase {
|
|
4556
|
+
/**
|
|
4557
|
+
* The constructor initializes a heap data structure with optional elements and options.
|
|
4558
|
+
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
4559
|
+
* elements to be added to the heap. It is an optional parameter and if not provided, the heap will
|
|
4560
|
+
* be initialized as empty.
|
|
4561
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
4562
|
+
* configuration options for the heap. In this case, it is used to specify a custom comparator
|
|
4563
|
+
* function for comparing elements in the heap. The comparator function is used to determine the
|
|
4564
|
+
* order of elements in the heap.
|
|
4565
|
+
*/
|
|
4119
4566
|
constructor(elements = [], options) {
|
|
4120
4567
|
super();
|
|
4121
4568
|
__publicField(this, "_comparator", (a, b) => {
|
|
@@ -4137,9 +4584,17 @@ var dataStructureTyped = (() => {
|
|
|
4137
4584
|
}
|
|
4138
4585
|
}
|
|
4139
4586
|
}
|
|
4587
|
+
/**
|
|
4588
|
+
* The function returns the value of the _comparator property.
|
|
4589
|
+
* @returns The `_comparator` property is being returned.
|
|
4590
|
+
*/
|
|
4140
4591
|
get comparator() {
|
|
4141
4592
|
return this._comparator;
|
|
4142
4593
|
}
|
|
4594
|
+
/**
|
|
4595
|
+
* The function returns an array of elements.
|
|
4596
|
+
* @returns The elements array is being returned.
|
|
4597
|
+
*/
|
|
4143
4598
|
get elements() {
|
|
4144
4599
|
return this._elements;
|
|
4145
4600
|
}
|
|
@@ -4167,11 +4622,12 @@ var dataStructureTyped = (() => {
|
|
|
4167
4622
|
return new _Heap(elements, options);
|
|
4168
4623
|
}
|
|
4169
4624
|
/**
|
|
4170
|
-
* Time Complexity: O(log n)
|
|
4625
|
+
* Time Complexity: O(log n)
|
|
4171
4626
|
* Space Complexity: O(1)
|
|
4627
|
+
* where n is the number of elements in the heap.
|
|
4172
4628
|
*/
|
|
4173
4629
|
/**
|
|
4174
|
-
* Time Complexity: O(log n)
|
|
4630
|
+
* Time Complexity: O(log n)
|
|
4175
4631
|
* Space Complexity: O(1)
|
|
4176
4632
|
*
|
|
4177
4633
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -4182,11 +4638,12 @@ var dataStructureTyped = (() => {
|
|
|
4182
4638
|
return this._bubbleUp(this.elements.length - 1);
|
|
4183
4639
|
}
|
|
4184
4640
|
/**
|
|
4185
|
-
* Time Complexity: O(log n)
|
|
4641
|
+
* Time Complexity: O(log n)
|
|
4186
4642
|
* Space Complexity: O(1)
|
|
4643
|
+
* where n is the number of elements in the heap.
|
|
4187
4644
|
*/
|
|
4188
4645
|
/**
|
|
4189
|
-
* Time Complexity: O(log n)
|
|
4646
|
+
* Time Complexity: O(log n)
|
|
4190
4647
|
* Space Complexity: O(1)
|
|
4191
4648
|
*
|
|
4192
4649
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -4204,6 +4661,9 @@ var dataStructureTyped = (() => {
|
|
|
4204
4661
|
return value;
|
|
4205
4662
|
}
|
|
4206
4663
|
/**
|
|
4664
|
+
* Time Complexity: O(1)
|
|
4665
|
+
* Space Complexity: O(1)
|
|
4666
|
+
*
|
|
4207
4667
|
* Peek at the top element of the heap without removing it.
|
|
4208
4668
|
* @returns The top element or undefined if the heap is empty.
|
|
4209
4669
|
*/
|
|
@@ -4449,6 +4909,9 @@ var dataStructureTyped = (() => {
|
|
|
4449
4909
|
}
|
|
4450
4910
|
return mappedHeap;
|
|
4451
4911
|
}
|
|
4912
|
+
/**
|
|
4913
|
+
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
|
4914
|
+
*/
|
|
4452
4915
|
*_getIterator() {
|
|
4453
4916
|
for (const element of this.elements) {
|
|
4454
4917
|
yield element;
|
|
@@ -4506,6 +4969,16 @@ var dataStructureTyped = (() => {
|
|
|
4506
4969
|
}
|
|
4507
4970
|
};
|
|
4508
4971
|
var FibonacciHeapNode = class {
|
|
4972
|
+
/**
|
|
4973
|
+
* The constructor function initializes an object with an element and a degree, and sets the marked
|
|
4974
|
+
* property to false.
|
|
4975
|
+
* @param {E} element - The "element" parameter represents the value or data that will be stored in
|
|
4976
|
+
* the node of a data structure. It can be any type of data, such as a number, string, object, or
|
|
4977
|
+
* even another data structure.
|
|
4978
|
+
* @param [degree=0] - The degree parameter represents the degree of the element in a data structure
|
|
4979
|
+
* called a Fibonacci heap. The degree of a node is the number of children it has. By default, the
|
|
4980
|
+
* degree is set to 0 when a new node is created.
|
|
4981
|
+
*/
|
|
4509
4982
|
constructor(element, degree = 0) {
|
|
4510
4983
|
__publicField(this, "element");
|
|
4511
4984
|
__publicField(this, "degree");
|
|
@@ -4520,6 +4993,13 @@ var dataStructureTyped = (() => {
|
|
|
4520
4993
|
}
|
|
4521
4994
|
};
|
|
4522
4995
|
var FibonacciHeap = class {
|
|
4996
|
+
/**
|
|
4997
|
+
* The constructor function initializes a FibonacciHeap object with an optional comparator function.
|
|
4998
|
+
* @param [comparator] - The `comparator` parameter is an optional argument that represents a
|
|
4999
|
+
* function used to compare elements in the FibonacciHeap. If a comparator function is provided, it
|
|
5000
|
+
* will be used to determine the order of elements in the heap. If no comparator function is
|
|
5001
|
+
* provided, a default comparator function will be used.
|
|
5002
|
+
*/
|
|
4523
5003
|
constructor(comparator) {
|
|
4524
5004
|
__publicField(this, "_root");
|
|
4525
5005
|
__publicField(this, "_size", 0);
|
|
@@ -4531,15 +5011,32 @@ var dataStructureTyped = (() => {
|
|
|
4531
5011
|
throw new Error("FibonacciHeap constructor: given comparator should be a function.");
|
|
4532
5012
|
}
|
|
4533
5013
|
}
|
|
5014
|
+
/**
|
|
5015
|
+
* The function returns the root node of a Fibonacci heap.
|
|
5016
|
+
* @returns The method is returning either a FibonacciHeapNode object or undefined.
|
|
5017
|
+
*/
|
|
4534
5018
|
get root() {
|
|
4535
5019
|
return this._root;
|
|
4536
5020
|
}
|
|
5021
|
+
/**
|
|
5022
|
+
* The function returns the size of an object.
|
|
5023
|
+
* @returns The size of the object, which is a number.
|
|
5024
|
+
*/
|
|
4537
5025
|
get size() {
|
|
4538
5026
|
return this._size;
|
|
4539
5027
|
}
|
|
5028
|
+
/**
|
|
5029
|
+
* The function returns the minimum node in a Fibonacci heap.
|
|
5030
|
+
* @returns The method is returning the minimum node of the Fibonacci heap, which is of type
|
|
5031
|
+
* `FibonacciHeapNode<E>`. If there is no minimum node, it will return `undefined`.
|
|
5032
|
+
*/
|
|
4540
5033
|
get min() {
|
|
4541
5034
|
return this._min;
|
|
4542
5035
|
}
|
|
5036
|
+
/**
|
|
5037
|
+
* The function returns the comparator used for comparing elements.
|
|
5038
|
+
* @returns The `_comparator` property of the object.
|
|
5039
|
+
*/
|
|
4543
5040
|
get comparator() {
|
|
4544
5041
|
return this._comparator;
|
|
4545
5042
|
}
|
|
@@ -4815,11 +5312,11 @@ var dataStructureTyped = (() => {
|
|
|
4815
5312
|
y.parent = x;
|
|
4816
5313
|
}
|
|
4817
5314
|
/**
|
|
4818
|
-
* Time Complexity: O(n log n)
|
|
5315
|
+
* Time Complexity: O(n log n)
|
|
4819
5316
|
* Space Complexity: O(n)
|
|
4820
5317
|
*/
|
|
4821
5318
|
/**
|
|
4822
|
-
* Time Complexity: O(n log n)
|
|
5319
|
+
* Time Complexity: O(n log n)
|
|
4823
5320
|
* Space Complexity: O(n)
|
|
4824
5321
|
*
|
|
4825
5322
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -4933,6 +5430,9 @@ var dataStructureTyped = (() => {
|
|
|
4933
5430
|
get vertexMap() {
|
|
4934
5431
|
return this._vertexMap;
|
|
4935
5432
|
}
|
|
5433
|
+
set vertexMap(v) {
|
|
5434
|
+
this._vertexMap = v;
|
|
5435
|
+
}
|
|
4936
5436
|
/**
|
|
4937
5437
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
4938
5438
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
@@ -6013,7 +6513,7 @@ var dataStructureTyped = (() => {
|
|
|
6013
6513
|
this.dest = dest;
|
|
6014
6514
|
}
|
|
6015
6515
|
};
|
|
6016
|
-
var DirectedGraph = class extends AbstractGraph {
|
|
6516
|
+
var DirectedGraph = class _DirectedGraph extends AbstractGraph {
|
|
6017
6517
|
/**
|
|
6018
6518
|
* The constructor function initializes an instance of a class.
|
|
6019
6519
|
*/
|
|
@@ -6025,9 +6525,15 @@ var dataStructureTyped = (() => {
|
|
|
6025
6525
|
get outEdgeMap() {
|
|
6026
6526
|
return this._outEdgeMap;
|
|
6027
6527
|
}
|
|
6528
|
+
set outEdgeMap(v) {
|
|
6529
|
+
this._outEdgeMap = v;
|
|
6530
|
+
}
|
|
6028
6531
|
get inEdgeMap() {
|
|
6029
6532
|
return this._inEdgeMap;
|
|
6030
6533
|
}
|
|
6534
|
+
set inEdgeMap(v) {
|
|
6535
|
+
this._inEdgeMap = v;
|
|
6536
|
+
}
|
|
6031
6537
|
/**
|
|
6032
6538
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
6033
6539
|
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
@@ -6497,6 +7003,26 @@ var dataStructureTyped = (() => {
|
|
|
6497
7003
|
return void 0;
|
|
6498
7004
|
}
|
|
6499
7005
|
}
|
|
7006
|
+
/**
|
|
7007
|
+
* The isEmpty function checks if the graph is empty.
|
|
7008
|
+
*
|
|
7009
|
+
* @return A boolean value
|
|
7010
|
+
*/
|
|
7011
|
+
isEmpty() {
|
|
7012
|
+
return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
|
|
7013
|
+
}
|
|
7014
|
+
/**
|
|
7015
|
+
* The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
|
|
7016
|
+
*
|
|
7017
|
+
* @return A new instance of the directedgraph class
|
|
7018
|
+
*/
|
|
7019
|
+
clone() {
|
|
7020
|
+
const cloned = new _DirectedGraph();
|
|
7021
|
+
cloned.vertexMap = new Map(this.vertexMap);
|
|
7022
|
+
cloned.inEdgeMap = new Map(this.inEdgeMap);
|
|
7023
|
+
cloned.outEdgeMap = new Map(this.outEdgeMap);
|
|
7024
|
+
return cloned;
|
|
7025
|
+
}
|
|
6500
7026
|
/**
|
|
6501
7027
|
* Time Complexity: O(1)
|
|
6502
7028
|
* Space Complexity: O(1)
|
|
@@ -6567,7 +7093,7 @@ var dataStructureTyped = (() => {
|
|
|
6567
7093
|
this.vertexMap = [v1, v2];
|
|
6568
7094
|
}
|
|
6569
7095
|
};
|
|
6570
|
-
var UndirectedGraph = class extends AbstractGraph {
|
|
7096
|
+
var UndirectedGraph = class _UndirectedGraph extends AbstractGraph {
|
|
6571
7097
|
/**
|
|
6572
7098
|
* The constructor initializes a new Map object to store edgeMap.
|
|
6573
7099
|
*/
|
|
@@ -6579,6 +7105,9 @@ var dataStructureTyped = (() => {
|
|
|
6579
7105
|
get edgeMap() {
|
|
6580
7106
|
return this._edgeMap;
|
|
6581
7107
|
}
|
|
7108
|
+
set edgeMap(v) {
|
|
7109
|
+
this._edgeMap = v;
|
|
7110
|
+
}
|
|
6582
7111
|
/**
|
|
6583
7112
|
* The function creates a new vertex with an optional value and returns it.
|
|
6584
7113
|
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
|
|
@@ -6853,6 +7382,28 @@ var dataStructureTyped = (() => {
|
|
|
6853
7382
|
return void 0;
|
|
6854
7383
|
}
|
|
6855
7384
|
}
|
|
7385
|
+
/**
|
|
7386
|
+
* The isEmpty function checks if the graph is empty.
|
|
7387
|
+
* @return True if the graph is empty and false otherwise
|
|
7388
|
+
*/
|
|
7389
|
+
isEmpty() {
|
|
7390
|
+
return this.vertexMap.size === 0 && this.edgeMap.size === 0;
|
|
7391
|
+
}
|
|
7392
|
+
/**
|
|
7393
|
+
* The clone function creates a new UndirectedGraph object and copies the
|
|
7394
|
+
* vertexMap and edgeMap from this graph to the new one. This is done by
|
|
7395
|
+
* assigning each of these properties to their respective counterparts in the
|
|
7396
|
+
* cloned graph. The clone function returns a reference to this newly created,
|
|
7397
|
+
* cloned UndirectedGraph object.
|
|
7398
|
+
*
|
|
7399
|
+
* @return A new instance of the undirectedgraph class
|
|
7400
|
+
*/
|
|
7401
|
+
clone() {
|
|
7402
|
+
const cloned = new _UndirectedGraph();
|
|
7403
|
+
cloned.vertexMap = new Map(this.vertexMap);
|
|
7404
|
+
cloned.edgeMap = new Map(this.edgeMap);
|
|
7405
|
+
return cloned;
|
|
7406
|
+
}
|
|
6856
7407
|
/**
|
|
6857
7408
|
* Time Complexity: O(1)
|
|
6858
7409
|
* Space Complexity: O(1)
|
|
@@ -6920,7 +7471,7 @@ var dataStructureTyped = (() => {
|
|
|
6920
7471
|
super(src, dest, weight, value);
|
|
6921
7472
|
}
|
|
6922
7473
|
};
|
|
6923
|
-
var MapGraph = class extends DirectedGraph {
|
|
7474
|
+
var MapGraph = class _MapGraph extends DirectedGraph {
|
|
6924
7475
|
/**
|
|
6925
7476
|
* The constructor function initializes the originCoord and bottomRight properties of a MapGraphCoordinate object.
|
|
6926
7477
|
* @param {MapGraphCoordinate} originCoord - The `originCoord` parameter is a `MapGraphCoordinate` object that represents the
|
|
@@ -6972,6 +7523,20 @@ var dataStructureTyped = (() => {
|
|
|
6972
7523
|
createEdge(src, dest, weight, value) {
|
|
6973
7524
|
return new MapEdge(src, dest, weight, value);
|
|
6974
7525
|
}
|
|
7526
|
+
/**
|
|
7527
|
+
* The override function is used to override the default behavior of a function.
|
|
7528
|
+
* In this case, we are overriding the clone() function from Graph<V, E>.
|
|
7529
|
+
* The clone() function returns a new graph that is an exact copy of the original graph.
|
|
7530
|
+
*
|
|
7531
|
+
* @return A mapgraph<v, e, vo, eo>
|
|
7532
|
+
*/
|
|
7533
|
+
clone() {
|
|
7534
|
+
const cloned = new _MapGraph(this.originCoord, this.bottomRight);
|
|
7535
|
+
cloned.vertexMap = new Map(this.vertexMap);
|
|
7536
|
+
cloned.inEdgeMap = new Map(this.inEdgeMap);
|
|
7537
|
+
cloned.outEdgeMap = new Map(this.outEdgeMap);
|
|
7538
|
+
return cloned;
|
|
7539
|
+
}
|
|
6975
7540
|
};
|
|
6976
7541
|
|
|
6977
7542
|
// src/types/data-structures/binary-tree/rb-tree.ts
|
|
@@ -6993,10 +7558,10 @@ var dataStructureTyped = (() => {
|
|
|
6993
7558
|
CP2["gt"] = "gt";
|
|
6994
7559
|
return CP2;
|
|
6995
7560
|
})(CP || {});
|
|
6996
|
-
var IterationType = /* @__PURE__ */ ((
|
|
6997
|
-
|
|
6998
|
-
|
|
6999
|
-
return
|
|
7561
|
+
var IterationType = /* @__PURE__ */ ((IterationType2) => {
|
|
7562
|
+
IterationType2["ITERATIVE"] = "ITERATIVE";
|
|
7563
|
+
IterationType2["RECURSIVE"] = "RECURSIVE";
|
|
7564
|
+
return IterationType2;
|
|
7000
7565
|
})(IterationType || {});
|
|
7001
7566
|
var FamilyPosition = /* @__PURE__ */ ((FamilyPosition2) => {
|
|
7002
7567
|
FamilyPosition2["ROOT"] = "ROOT";
|
|
@@ -7011,6 +7576,13 @@ var dataStructureTyped = (() => {
|
|
|
7011
7576
|
|
|
7012
7577
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
7013
7578
|
var BinaryTreeNode = class {
|
|
7579
|
+
/**
|
|
7580
|
+
* The constructor function initializes an object with a key and an optional value.
|
|
7581
|
+
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
|
|
7582
|
+
* constructor. It is used to set the value of the "key" property of the object being created.
|
|
7583
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
7584
|
+
* value associated with the key in the constructor.
|
|
7585
|
+
*/
|
|
7014
7586
|
constructor(key, value) {
|
|
7015
7587
|
__publicField(this, "key");
|
|
7016
7588
|
__publicField(this, "value");
|
|
@@ -7020,18 +7592,39 @@ var dataStructureTyped = (() => {
|
|
|
7020
7592
|
this.key = key;
|
|
7021
7593
|
this.value = value;
|
|
7022
7594
|
}
|
|
7595
|
+
/**
|
|
7596
|
+
* The function returns the value of the `_left` property, which can be of type `NODE`, `null`, or
|
|
7597
|
+
* `undefined`.
|
|
7598
|
+
* @returns The left node of the current node is being returned. It can be either a NODE object,
|
|
7599
|
+
* null, or undefined.
|
|
7600
|
+
*/
|
|
7023
7601
|
get left() {
|
|
7024
7602
|
return this._left;
|
|
7025
7603
|
}
|
|
7604
|
+
/**
|
|
7605
|
+
* The function sets the left child of a node and updates its parent reference.
|
|
7606
|
+
* @param {NODE | null | undefined} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
7607
|
+
* `undefined`.
|
|
7608
|
+
*/
|
|
7026
7609
|
set left(v) {
|
|
7027
7610
|
if (v) {
|
|
7028
7611
|
v.parent = this;
|
|
7029
7612
|
}
|
|
7030
7613
|
this._left = v;
|
|
7031
7614
|
}
|
|
7615
|
+
/**
|
|
7616
|
+
* The function returns the right node of a binary tree or null if it doesn't exist.
|
|
7617
|
+
* @returns The method is returning the value of the `_right` property, which can be a `NODE` object,
|
|
7618
|
+
* `null`, or `undefined`.
|
|
7619
|
+
*/
|
|
7032
7620
|
get right() {
|
|
7033
7621
|
return this._right;
|
|
7034
7622
|
}
|
|
7623
|
+
/**
|
|
7624
|
+
* The function sets the right child of a node and updates its parent.
|
|
7625
|
+
* @param {NODE | null | undefined} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
7626
|
+
* `undefined`.
|
|
7627
|
+
*/
|
|
7035
7628
|
set right(v) {
|
|
7036
7629
|
if (v) {
|
|
7037
7630
|
v.parent = this;
|
|
@@ -7083,12 +7676,25 @@ var dataStructureTyped = (() => {
|
|
|
7083
7676
|
if (keysOrNodesOrEntries)
|
|
7084
7677
|
this.addMany(keysOrNodesOrEntries);
|
|
7085
7678
|
}
|
|
7679
|
+
/**
|
|
7680
|
+
* The function returns the value of the `_extractor` property.
|
|
7681
|
+
* @returns The `_extractor` property is being returned.
|
|
7682
|
+
*/
|
|
7086
7683
|
get extractor() {
|
|
7087
7684
|
return this._extractor;
|
|
7088
7685
|
}
|
|
7686
|
+
/**
|
|
7687
|
+
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
7688
|
+
* @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
|
|
7689
|
+
* `null`, or `undefined`.
|
|
7690
|
+
*/
|
|
7089
7691
|
get root() {
|
|
7090
7692
|
return this._root;
|
|
7091
7693
|
}
|
|
7694
|
+
/**
|
|
7695
|
+
* The function returns the size of an object.
|
|
7696
|
+
* @returns The size of the object, which is a number.
|
|
7697
|
+
*/
|
|
7092
7698
|
get size() {
|
|
7093
7699
|
return this._size;
|
|
7094
7700
|
}
|
|
@@ -7096,7 +7702,7 @@ var dataStructureTyped = (() => {
|
|
|
7096
7702
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
7097
7703
|
* @param {K} key - The key for the new node.
|
|
7098
7704
|
* @param {V} value - The value for the new node.
|
|
7099
|
-
* @returns {
|
|
7705
|
+
* @returns {NODE} - The newly created BinaryTreeNode.
|
|
7100
7706
|
*/
|
|
7101
7707
|
createNode(key, value) {
|
|
7102
7708
|
return new BinaryTreeNode(key, value);
|
|
@@ -7112,14 +7718,14 @@ var dataStructureTyped = (() => {
|
|
|
7112
7718
|
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
7113
7719
|
}
|
|
7114
7720
|
/**
|
|
7115
|
-
* The function `
|
|
7116
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
7721
|
+
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
7722
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
7117
7723
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
7118
|
-
* `
|
|
7724
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
|
|
7119
7725
|
* is provided, it will be `undefined`.
|
|
7120
|
-
* @returns a value of type
|
|
7726
|
+
* @returns a value of type NODE (node), or null, or undefined.
|
|
7121
7727
|
*/
|
|
7122
|
-
|
|
7728
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
7123
7729
|
if (keyOrNodeOrEntry === void 0)
|
|
7124
7730
|
return;
|
|
7125
7731
|
let node;
|
|
@@ -7153,7 +7759,7 @@ var dataStructureTyped = (() => {
|
|
|
7153
7759
|
*
|
|
7154
7760
|
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
7155
7761
|
* key, otherwise it returns the key itself.
|
|
7156
|
-
* @param {K |
|
|
7762
|
+
* @param {K | NODE | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`,
|
|
7157
7763
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
7158
7764
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
7159
7765
|
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
@@ -7180,25 +7786,21 @@ var dataStructureTyped = (() => {
|
|
|
7180
7786
|
}
|
|
7181
7787
|
/**
|
|
7182
7788
|
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
7183
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,
|
|
7184
|
-
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class
|
|
7789
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
|
|
7790
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class NODE.
|
|
7185
7791
|
*/
|
|
7186
7792
|
isNode(keyOrNodeOrEntry) {
|
|
7187
7793
|
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
7188
7794
|
}
|
|
7189
7795
|
/**
|
|
7190
7796
|
* The function checks if a given value is an entry in a binary tree node.
|
|
7191
|
-
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,
|
|
7192
|
-
* two type parameters V and
|
|
7797
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
7798
|
+
* two type parameters V and NODE, representing the value and node type respectively.
|
|
7193
7799
|
* @returns a boolean value.
|
|
7194
7800
|
*/
|
|
7195
7801
|
isEntry(keyOrNodeOrEntry) {
|
|
7196
7802
|
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
7197
7803
|
}
|
|
7198
|
-
/**
|
|
7199
|
-
* Time complexity: O(n)
|
|
7200
|
-
* Space complexity: O(log n)
|
|
7201
|
-
*/
|
|
7202
7804
|
/**
|
|
7203
7805
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
7204
7806
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -7225,21 +7827,21 @@ var dataStructureTyped = (() => {
|
|
|
7225
7827
|
return this.isRealNode(node) || node === null;
|
|
7226
7828
|
}
|
|
7227
7829
|
/**
|
|
7228
|
-
* Time Complexity O(
|
|
7830
|
+
* Time Complexity O(n)
|
|
7229
7831
|
* Space Complexity O(1)
|
|
7230
7832
|
*/
|
|
7231
7833
|
/**
|
|
7232
|
-
* Time Complexity O(
|
|
7834
|
+
* Time Complexity O(n)
|
|
7233
7835
|
* Space Complexity O(1)
|
|
7234
7836
|
*
|
|
7235
7837
|
* The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
|
|
7236
7838
|
* existing node with the same key.
|
|
7237
7839
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
7238
7840
|
* @param {V} [value] - The value to be inserted into the binary tree.
|
|
7239
|
-
* @returns The function `add` returns either a node (`
|
|
7841
|
+
* @returns The function `add` returns either a node (`NODE`), `null`, or `undefined`.
|
|
7240
7842
|
*/
|
|
7241
7843
|
add(keyOrNodeOrEntry, value) {
|
|
7242
|
-
const newNode = this.
|
|
7844
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
7243
7845
|
if (newNode === void 0)
|
|
7244
7846
|
return false;
|
|
7245
7847
|
if (!this.root) {
|
|
@@ -7279,19 +7881,19 @@ var dataStructureTyped = (() => {
|
|
|
7279
7881
|
return false;
|
|
7280
7882
|
}
|
|
7281
7883
|
/**
|
|
7282
|
-
* Time Complexity: O(k
|
|
7884
|
+
* Time Complexity: O(k * n)
|
|
7283
7885
|
* Space Complexity: O(1)
|
|
7284
7886
|
* 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.
|
|
7285
7887
|
*/
|
|
7286
7888
|
/**
|
|
7287
|
-
* Time Complexity: O(k
|
|
7889
|
+
* Time Complexity: O(k * n)
|
|
7288
7890
|
* Space Complexity: O(1)
|
|
7289
7891
|
*
|
|
7290
7892
|
* The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
|
|
7291
7893
|
* adds each node with its corresponding value to the data structure.
|
|
7292
7894
|
* @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
|
|
7293
7895
|
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
7294
|
-
* @returns The function `addMany` returns an array of `
|
|
7896
|
+
* @returns The function `addMany` returns an array of `NODE`, `null`, or `undefined` values.
|
|
7295
7897
|
*/
|
|
7296
7898
|
addMany(keysOrNodesOrEntries, values) {
|
|
7297
7899
|
const inserted = [];
|
|
@@ -7322,7 +7924,7 @@ var dataStructureTyped = (() => {
|
|
|
7322
7924
|
*
|
|
7323
7925
|
* The `refill` function clears the current data and adds new key-value pairs to the data structure.
|
|
7324
7926
|
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
|
|
7325
|
-
* KeyOrNodeOrEntry<K, V,
|
|
7927
|
+
* KeyOrNodeOrEntry<K, V, NODE>.
|
|
7326
7928
|
* @param [values] - The `values` parameter is an optional iterable that contains the values to be
|
|
7327
7929
|
* associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
|
|
7328
7930
|
* the values will be associated with the corresponding keys or nodes or entries in the
|
|
@@ -7333,20 +7935,25 @@ var dataStructureTyped = (() => {
|
|
|
7333
7935
|
this.addMany(keysOrNodesOrEntries, values);
|
|
7334
7936
|
}
|
|
7335
7937
|
/**
|
|
7336
|
-
|
|
7337
|
-
|
|
7338
|
-
|
|
7339
|
-
|
|
7340
|
-
|
|
7341
|
-
|
|
7342
|
-
|
|
7343
|
-
|
|
7344
|
-
|
|
7345
|
-
|
|
7346
|
-
|
|
7347
|
-
|
|
7348
|
-
|
|
7349
|
-
|
|
7938
|
+
* Time Complexity: O(n)
|
|
7939
|
+
* Space Complexity: O(1)
|
|
7940
|
+
* /
|
|
7941
|
+
|
|
7942
|
+
/**
|
|
7943
|
+
* Time Complexity: O(n)
|
|
7944
|
+
* Space Complexity: O(1)
|
|
7945
|
+
*
|
|
7946
|
+
* The function deletes a node from a binary tree and returns an array of the deleted nodes along
|
|
7947
|
+
* with the nodes that need to be balanced.
|
|
7948
|
+
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
|
|
7949
|
+
* object that you want to delete from the binary tree. It can be of any type that is compatible with
|
|
7950
|
+
* the callback function's return type. It can also be null or undefined if you want to delete a
|
|
7951
|
+
* specific node based on its value or object.
|
|
7952
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
7953
|
+
* identifier of the node to be deleted. It is optional and has a default value of
|
|
7954
|
+
* `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
|
|
7955
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
7956
|
+
*/
|
|
7350
7957
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
7351
7958
|
const deletedResult = [];
|
|
7352
7959
|
if (!this.root)
|
|
@@ -7356,36 +7963,35 @@ var dataStructureTyped = (() => {
|
|
|
7356
7963
|
const curr = this.getNode(identifier, callback);
|
|
7357
7964
|
if (!curr)
|
|
7358
7965
|
return deletedResult;
|
|
7359
|
-
const parent =
|
|
7360
|
-
let needBalanced
|
|
7966
|
+
const parent = curr == null ? void 0 : curr.parent;
|
|
7967
|
+
let needBalanced;
|
|
7361
7968
|
let orgCurrent = curr;
|
|
7362
|
-
if (!curr.left) {
|
|
7363
|
-
|
|
7364
|
-
|
|
7365
|
-
|
|
7366
|
-
|
|
7367
|
-
|
|
7368
|
-
|
|
7369
|
-
|
|
7370
|
-
|
|
7969
|
+
if (!curr.left && !curr.right && !parent) {
|
|
7970
|
+
this._setRoot(void 0);
|
|
7971
|
+
} else if (curr.left) {
|
|
7972
|
+
const leftSubTreeRightMost = this.getRightMost(curr.left);
|
|
7973
|
+
if (leftSubTreeRightMost) {
|
|
7974
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
7975
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
7976
|
+
if (parentOfLeftSubTreeMax) {
|
|
7977
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
7978
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
7979
|
+
else
|
|
7980
|
+
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
7981
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
7371
7982
|
}
|
|
7372
|
-
needBalanced = parent;
|
|
7373
7983
|
}
|
|
7374
|
-
} else {
|
|
7375
|
-
|
|
7376
|
-
|
|
7377
|
-
|
|
7378
|
-
|
|
7379
|
-
|
|
7380
|
-
if (parentOfLeftSubTreeMax) {
|
|
7381
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
7382
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
7383
|
-
else
|
|
7384
|
-
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
7385
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
7386
|
-
}
|
|
7387
|
-
}
|
|
7984
|
+
} else if (parent) {
|
|
7985
|
+
const { familyPosition: fp } = curr;
|
|
7986
|
+
if (fp === "LEFT" /* LEFT */ || fp === "ROOT_LEFT" /* ROOT_LEFT */) {
|
|
7987
|
+
parent.left = curr.right;
|
|
7988
|
+
} else if (fp === "RIGHT" /* RIGHT */ || fp === "ROOT_RIGHT" /* ROOT_RIGHT */) {
|
|
7989
|
+
parent.right = curr.right;
|
|
7388
7990
|
}
|
|
7991
|
+
needBalanced = parent;
|
|
7992
|
+
} else {
|
|
7993
|
+
this._setRoot(curr.right);
|
|
7994
|
+
curr.right = void 0;
|
|
7389
7995
|
}
|
|
7390
7996
|
this._size = this.size - 1;
|
|
7391
7997
|
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
@@ -7393,186 +7999,36 @@ var dataStructureTyped = (() => {
|
|
|
7393
7999
|
}
|
|
7394
8000
|
/**
|
|
7395
8001
|
* Time Complexity: O(n)
|
|
7396
|
-
* Space Complexity: O(
|
|
7397
|
-
*/
|
|
7398
|
-
/**
|
|
7399
|
-
* Time Complexity: O(n)
|
|
7400
|
-
* Space Complexity: O(1)
|
|
7401
|
-
*
|
|
7402
|
-
* The function calculates the depth of a given node in a binary tree.
|
|
7403
|
-
* @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
|
|
7404
|
-
* the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
|
|
7405
|
-
* `undefined`.
|
|
7406
|
-
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
7407
|
-
* from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
|
|
7408
|
-
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
7409
|
-
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
7410
|
-
*/
|
|
7411
|
-
getDepth(dist, beginRoot = this.root) {
|
|
7412
|
-
dist = this.ensureNode(dist);
|
|
7413
|
-
beginRoot = this.ensureNode(beginRoot);
|
|
7414
|
-
let depth = 0;
|
|
7415
|
-
while (dist == null ? void 0 : dist.parent) {
|
|
7416
|
-
if (dist === beginRoot) {
|
|
7417
|
-
return depth;
|
|
7418
|
-
}
|
|
7419
|
-
depth++;
|
|
7420
|
-
dist = dist.parent;
|
|
7421
|
-
}
|
|
7422
|
-
return depth;
|
|
7423
|
-
}
|
|
7424
|
-
/**
|
|
7425
|
-
* Time Complexity: O(n)
|
|
7426
|
-
* Space Complexity: O(1)
|
|
8002
|
+
* Space Complexity: O(k + log n)
|
|
7427
8003
|
*/
|
|
7428
8004
|
/**
|
|
7429
8005
|
* Time Complexity: O(n)
|
|
7430
|
-
* Space Complexity: O(log n)
|
|
8006
|
+
* Space Complexity: O(k + log n).
|
|
7431
8007
|
*
|
|
7432
|
-
* The function `
|
|
7433
|
-
*
|
|
7434
|
-
* @param {
|
|
7435
|
-
*
|
|
7436
|
-
*
|
|
7437
|
-
*
|
|
7438
|
-
*
|
|
7439
|
-
*
|
|
7440
|
-
*
|
|
8008
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
|
|
8009
|
+
* callback function.
|
|
8010
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8011
|
+
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
8012
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
|
|
8013
|
+
* specific value.
|
|
8014
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
8015
|
+
* input and returns a value of type `C`. It is used to determine if a node matches the given
|
|
8016
|
+
* identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
|
|
8017
|
+
* default
|
|
8018
|
+
* @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
|
|
8019
|
+
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
8020
|
+
* node and return that node. If set to false (default), the function will continue iterating and
|
|
8021
|
+
* return all nodes that match the identifier.
|
|
8022
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
8023
|
+
* starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
|
|
8024
|
+
* it is `null` or `undefined`, an empty array will be returned.
|
|
8025
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
8026
|
+
* traverse the binary tree. It can have two possible values:
|
|
8027
|
+
* @returns an array of nodes of type `NODE`.
|
|
7441
8028
|
*/
|
|
7442
|
-
|
|
7443
|
-
|
|
7444
|
-
|
|
7445
|
-
return -1;
|
|
7446
|
-
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
7447
|
-
const _getMaxHeight = (cur) => {
|
|
7448
|
-
if (!cur)
|
|
7449
|
-
return -1;
|
|
7450
|
-
const leftHeight = _getMaxHeight(cur.left);
|
|
7451
|
-
const rightHeight = _getMaxHeight(cur.right);
|
|
7452
|
-
return Math.max(leftHeight, rightHeight) + 1;
|
|
7453
|
-
};
|
|
7454
|
-
return _getMaxHeight(beginRoot);
|
|
7455
|
-
} else {
|
|
7456
|
-
const stack = [{ node: beginRoot, depth: 0 }];
|
|
7457
|
-
let maxHeight = 0;
|
|
7458
|
-
while (stack.length > 0) {
|
|
7459
|
-
const { node, depth } = stack.pop();
|
|
7460
|
-
if (node.left)
|
|
7461
|
-
stack.push({ node: node.left, depth: depth + 1 });
|
|
7462
|
-
if (node.right)
|
|
7463
|
-
stack.push({ node: node.right, depth: depth + 1 });
|
|
7464
|
-
maxHeight = Math.max(maxHeight, depth);
|
|
7465
|
-
}
|
|
7466
|
-
return maxHeight;
|
|
7467
|
-
}
|
|
7468
|
-
}
|
|
7469
|
-
/**
|
|
7470
|
-
* Time Complexity: O(n)
|
|
7471
|
-
* Space Complexity: O(log n)
|
|
7472
|
-
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
7473
|
-
*/
|
|
7474
|
-
/**
|
|
7475
|
-
* Time Complexity: O(n)
|
|
7476
|
-
* Space Complexity: O(log n)
|
|
7477
|
-
*
|
|
7478
|
-
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
7479
|
-
* recursive or iterative approach.
|
|
7480
|
-
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
7481
|
-
* starting node of the binary tree from which we want to calculate the minimum height. It can be of
|
|
7482
|
-
* type `K`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
|
|
7483
|
-
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
7484
|
-
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
7485
|
-
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
7486
|
-
*/
|
|
7487
|
-
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7488
|
-
var _a, _b, _c;
|
|
7489
|
-
beginRoot = this.ensureNode(beginRoot);
|
|
7490
|
-
if (!beginRoot)
|
|
7491
|
-
return -1;
|
|
7492
|
-
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
7493
|
-
const _getMinHeight = (cur) => {
|
|
7494
|
-
if (!cur)
|
|
7495
|
-
return 0;
|
|
7496
|
-
if (!cur.left && !cur.right)
|
|
7497
|
-
return 0;
|
|
7498
|
-
const leftMinHeight = _getMinHeight(cur.left);
|
|
7499
|
-
const rightMinHeight = _getMinHeight(cur.right);
|
|
7500
|
-
return Math.min(leftMinHeight, rightMinHeight) + 1;
|
|
7501
|
-
};
|
|
7502
|
-
return _getMinHeight(beginRoot);
|
|
7503
|
-
} else {
|
|
7504
|
-
const stack = [];
|
|
7505
|
-
let node = beginRoot, last = null;
|
|
7506
|
-
const depths = /* @__PURE__ */ new Map();
|
|
7507
|
-
while (stack.length > 0 || node) {
|
|
7508
|
-
if (node) {
|
|
7509
|
-
stack.push(node);
|
|
7510
|
-
node = node.left;
|
|
7511
|
-
} else {
|
|
7512
|
-
node = stack[stack.length - 1];
|
|
7513
|
-
if (!node.right || last === node.right) {
|
|
7514
|
-
node = stack.pop();
|
|
7515
|
-
if (node) {
|
|
7516
|
-
const leftMinHeight = node.left ? (_a = depths.get(node.left)) != null ? _a : -1 : -1;
|
|
7517
|
-
const rightMinHeight = node.right ? (_b = depths.get(node.right)) != null ? _b : -1 : -1;
|
|
7518
|
-
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
7519
|
-
last = node;
|
|
7520
|
-
node = null;
|
|
7521
|
-
}
|
|
7522
|
-
} else
|
|
7523
|
-
node = node.right;
|
|
7524
|
-
}
|
|
7525
|
-
}
|
|
7526
|
-
return (_c = depths.get(beginRoot)) != null ? _c : -1;
|
|
7527
|
-
}
|
|
7528
|
-
}
|
|
7529
|
-
/**
|
|
7530
|
-
* Time Complexity: O(n)
|
|
7531
|
-
* Space Complexity: O(log n)
|
|
7532
|
-
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
7533
|
-
*/
|
|
7534
|
-
/**
|
|
7535
|
-
* Time Complexity: O(n)
|
|
7536
|
-
* Space Complexity: O(log n)
|
|
7537
|
-
*
|
|
7538
|
-
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
7539
|
-
* height of the tree.
|
|
7540
|
-
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
7541
|
-
* for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
|
|
7542
|
-
* value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
|
|
7543
|
-
* @returns a boolean value.
|
|
7544
|
-
*/
|
|
7545
|
-
isPerfectlyBalanced(beginRoot = this.root) {
|
|
7546
|
-
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
7547
|
-
}
|
|
7548
|
-
/**
|
|
7549
|
-
* Time Complexity: O(n)
|
|
7550
|
-
* Space Complexity: O(log n).
|
|
7551
|
-
*
|
|
7552
|
-
* The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
|
|
7553
|
-
* callback function.
|
|
7554
|
-
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
7555
|
-
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
7556
|
-
* callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
|
|
7557
|
-
* specific value.
|
|
7558
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as
|
|
7559
|
-
* input and returns a value of type `C`. It is used to determine if a node matches the given
|
|
7560
|
-
* identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
|
|
7561
|
-
* default
|
|
7562
|
-
* @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
|
|
7563
|
-
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
7564
|
-
* node and return that node. If set to false (default), the function will continue iterating and
|
|
7565
|
-
* return all nodes that match the identifier.
|
|
7566
|
-
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
7567
|
-
* starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
|
|
7568
|
-
* it is `null` or `undefined`, an empty array will be returned.
|
|
7569
|
-
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
7570
|
-
* traverse the binary tree. It can have two possible values:
|
|
7571
|
-
* @returns an array of nodes of type `N`.
|
|
7572
|
-
*/
|
|
7573
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7574
|
-
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7575
|
-
callback = (node) => node;
|
|
8029
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8030
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
8031
|
+
callback = (node) => node;
|
|
7576
8032
|
beginRoot = this.ensureNode(beginRoot);
|
|
7577
8033
|
if (!beginRoot)
|
|
7578
8034
|
return [];
|
|
@@ -7610,29 +8066,7 @@ var dataStructureTyped = (() => {
|
|
|
7610
8066
|
/**
|
|
7611
8067
|
* Time Complexity: O(n)
|
|
7612
8068
|
* Space Complexity: O(log n).
|
|
7613
|
-
*
|
|
7614
|
-
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
7615
|
-
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
7616
|
-
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
7617
|
-
* callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
|
|
7618
|
-
* specific identifier.
|
|
7619
|
-
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
7620
|
-
* the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
|
|
7621
|
-
* function should return a boolean value indicating whether the node should be included in the
|
|
7622
|
-
* result or not.
|
|
7623
|
-
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
7624
|
-
* for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
|
|
7625
|
-
* node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
|
|
7626
|
-
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
7627
|
-
* iteration to be performed on the binary tree. It is used to specify whether the iteration should
|
|
7628
|
-
* be performed in a pre-order, in-order, or post-order manner.
|
|
7629
|
-
* @returns a boolean value.
|
|
7630
8069
|
*/
|
|
7631
|
-
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7632
|
-
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7633
|
-
callback = (node) => node;
|
|
7634
|
-
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
7635
|
-
}
|
|
7636
8070
|
/**
|
|
7637
8071
|
* Time Complexity: O(n)
|
|
7638
8072
|
* Space Complexity: O(log n)
|
|
@@ -7645,14 +8079,14 @@ var dataStructureTyped = (() => {
|
|
|
7645
8079
|
* identifier.
|
|
7646
8080
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
7647
8081
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
7648
|
-
* function should take a single parameter of type `
|
|
7649
|
-
* @param {K |
|
|
8082
|
+
* function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
|
|
8083
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
7650
8084
|
* for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
|
|
7651
8085
|
* If `null` or `undefined` is passed, the search will start from the root of the binary tree.
|
|
7652
8086
|
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
7653
8087
|
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
7654
8088
|
* nodes are visited during the search.
|
|
7655
|
-
* @returns a value of type `
|
|
8089
|
+
* @returns a value of type `NODE | null | undefined`.
|
|
7656
8090
|
*/
|
|
7657
8091
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7658
8092
|
var _a;
|
|
@@ -7675,7 +8109,7 @@ var dataStructureTyped = (() => {
|
|
|
7675
8109
|
* @param iterationType - The `iterationType` parameter is used to determine whether the search for
|
|
7676
8110
|
* the node with the given key should be performed iteratively or recursively. It has two possible
|
|
7677
8111
|
* values:
|
|
7678
|
-
* @returns The function `getNodeByKey` returns a node (`
|
|
8112
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
7679
8113
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
7680
8114
|
*/
|
|
7681
8115
|
getNodeByKey(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
@@ -7706,6 +8140,10 @@ var dataStructureTyped = (() => {
|
|
|
7706
8140
|
}
|
|
7707
8141
|
}
|
|
7708
8142
|
}
|
|
8143
|
+
/**
|
|
8144
|
+
* Time Complexity: O(n)
|
|
8145
|
+
* Space Complexity: O(log n)
|
|
8146
|
+
*/
|
|
7709
8147
|
/**
|
|
7710
8148
|
* Time Complexity: O(n)
|
|
7711
8149
|
* Space Complexity: O(log n)
|
|
@@ -7719,9 +8157,9 @@ var dataStructureTyped = (() => {
|
|
|
7719
8157
|
* the binary tree. It is used to determine whether a node matches the given identifier. The callback
|
|
7720
8158
|
* function should return a value that can be compared to the identifier to determine if it is a
|
|
7721
8159
|
* match.
|
|
7722
|
-
* @param {K |
|
|
8160
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
7723
8161
|
* for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
|
|
7724
|
-
* node), a node object of type `
|
|
8162
|
+
* node), a node object of type `NODE`, or `null`/`undefined` to start the search from the root of
|
|
7725
8163
|
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
7726
8164
|
* be performed when searching for a node in the binary tree. It is an optional parameter with a
|
|
7727
8165
|
* default value specified by `this.iterationType`.
|
|
@@ -7734,6 +8172,36 @@ var dataStructureTyped = (() => {
|
|
|
7734
8172
|
callback = (node) => node;
|
|
7735
8173
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value) != null ? _b : void 0;
|
|
7736
8174
|
}
|
|
8175
|
+
/**
|
|
8176
|
+
* Time Complexity: O(n)
|
|
8177
|
+
* Space Complexity: O(log n).
|
|
8178
|
+
*/
|
|
8179
|
+
/**
|
|
8180
|
+
* Time Complexity: O(n)
|
|
8181
|
+
* Space Complexity: O(log n).
|
|
8182
|
+
*
|
|
8183
|
+
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
8184
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8185
|
+
* that you want to search for in the binary tree. It can be of any type that is returned by the
|
|
8186
|
+
* callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
|
|
8187
|
+
* specific identifier.
|
|
8188
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8189
|
+
* the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
|
|
8190
|
+
* function should return a boolean value indicating whether the node should be included in the
|
|
8191
|
+
* result or not.
|
|
8192
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
8193
|
+
* for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
|
|
8194
|
+
* node in the binary tree), a node object (`NODE`), or `null`/`undefined` to start the search from
|
|
8195
|
+
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
8196
|
+
* iteration to be performed on the binary tree. It is used to specify whether the iteration should
|
|
8197
|
+
* be performed in a pre-order, in-order, or post-order manner.
|
|
8198
|
+
* @returns a boolean value.
|
|
8199
|
+
*/
|
|
8200
|
+
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8201
|
+
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
8202
|
+
callback = (node) => node;
|
|
8203
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
8204
|
+
}
|
|
7737
8205
|
/**
|
|
7738
8206
|
* Time Complexity: O(1)
|
|
7739
8207
|
* Space Complexity: O(1)
|
|
@@ -7763,19 +8231,231 @@ var dataStructureTyped = (() => {
|
|
|
7763
8231
|
return this.size === 0;
|
|
7764
8232
|
}
|
|
7765
8233
|
/**
|
|
7766
|
-
* Time Complexity: O(
|
|
8234
|
+
* Time Complexity: O(n)
|
|
8235
|
+
* Space Complexity: O(log n)
|
|
8236
|
+
*/
|
|
8237
|
+
/**
|
|
8238
|
+
* Time Complexity: O(n)
|
|
7767
8239
|
* Space Complexity: O(log n)
|
|
7768
8240
|
*
|
|
7769
|
-
* The function
|
|
7770
|
-
*
|
|
7771
|
-
* @param {K |
|
|
7772
|
-
*
|
|
7773
|
-
* `null`, or `undefined`.
|
|
7774
|
-
* @
|
|
7775
|
-
|
|
7776
|
-
|
|
7777
|
-
|
|
8241
|
+
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
8242
|
+
* height of the tree.
|
|
8243
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
8244
|
+
* for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
|
|
8245
|
+
* value of a binary tree node), `NODE` (a node of a binary tree), `null`, or `undefined`. If
|
|
8246
|
+
* @returns a boolean value.
|
|
8247
|
+
*/
|
|
8248
|
+
isPerfectlyBalanced(beginRoot = this.root) {
|
|
8249
|
+
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
8250
|
+
}
|
|
8251
|
+
/**
|
|
8252
|
+
* Time Complexity: O(n)
|
|
8253
|
+
* Space Complexity: O(1)
|
|
8254
|
+
*/
|
|
8255
|
+
/**
|
|
8256
|
+
* Time Complexity: O(n)
|
|
8257
|
+
* Space Complexity: O(1)
|
|
8258
|
+
*
|
|
8259
|
+
* The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
|
|
8260
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the root
|
|
8261
|
+
* node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
|
|
8262
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
8263
|
+
* type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
|
|
8264
|
+
* possible values:
|
|
8265
|
+
* @returns a boolean value.
|
|
8266
|
+
*/
|
|
8267
|
+
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8268
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8269
|
+
if (!beginRoot)
|
|
8270
|
+
return true;
|
|
8271
|
+
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
8272
|
+
const dfs = (cur, min, max) => {
|
|
8273
|
+
if (!cur)
|
|
8274
|
+
return true;
|
|
8275
|
+
const numKey = this.extractor(cur.key);
|
|
8276
|
+
if (numKey <= min || numKey >= max)
|
|
8277
|
+
return false;
|
|
8278
|
+
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
8279
|
+
};
|
|
8280
|
+
const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
8281
|
+
const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
8282
|
+
return isStandardBST || isInverseBST;
|
|
8283
|
+
} else {
|
|
8284
|
+
const checkBST = (checkMax = false) => {
|
|
8285
|
+
const stack = [];
|
|
8286
|
+
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
8287
|
+
let curr = beginRoot;
|
|
8288
|
+
while (curr || stack.length > 0) {
|
|
8289
|
+
while (curr) {
|
|
8290
|
+
stack.push(curr);
|
|
8291
|
+
curr = curr.left;
|
|
8292
|
+
}
|
|
8293
|
+
curr = stack.pop();
|
|
8294
|
+
const numKey = this.extractor(curr.key);
|
|
8295
|
+
if (!curr || !checkMax && prev >= numKey || checkMax && prev <= numKey)
|
|
8296
|
+
return false;
|
|
8297
|
+
prev = numKey;
|
|
8298
|
+
curr = curr.right;
|
|
8299
|
+
}
|
|
8300
|
+
return true;
|
|
8301
|
+
};
|
|
8302
|
+
const isStandardBST = checkBST(false), isInverseBST = checkBST(true);
|
|
8303
|
+
return isStandardBST || isInverseBST;
|
|
8304
|
+
}
|
|
8305
|
+
}
|
|
8306
|
+
/**
|
|
8307
|
+
* Time Complexity: O(n)
|
|
8308
|
+
* Space Complexity: O(1)
|
|
8309
|
+
*/
|
|
8310
|
+
/**
|
|
8311
|
+
* Time Complexity: O(n)
|
|
8312
|
+
* Space Complexity: O(1)
|
|
8313
|
+
*
|
|
8314
|
+
* The function calculates the depth of a given node in a binary tree.
|
|
8315
|
+
* @param {K | NODE | null | undefined} dist - The `dist` parameter represents the node in
|
|
8316
|
+
* the binary tree whose depth we want to find. It can be of type `K`, `NODE`, `null`, or
|
|
8317
|
+
* `undefined`.
|
|
8318
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
8319
|
+
* from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
|
|
8320
|
+
* `NODE` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
8321
|
+
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
8322
|
+
*/
|
|
8323
|
+
getDepth(dist, beginRoot = this.root) {
|
|
8324
|
+
dist = this.ensureNode(dist);
|
|
8325
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8326
|
+
let depth = 0;
|
|
8327
|
+
while (dist == null ? void 0 : dist.parent) {
|
|
8328
|
+
if (dist === beginRoot) {
|
|
8329
|
+
return depth;
|
|
8330
|
+
}
|
|
8331
|
+
depth++;
|
|
8332
|
+
dist = dist.parent;
|
|
8333
|
+
}
|
|
8334
|
+
return depth;
|
|
8335
|
+
}
|
|
8336
|
+
/**
|
|
8337
|
+
* Time Complexity: O(n)
|
|
8338
|
+
* Space Complexity: O(1)
|
|
8339
|
+
*/
|
|
8340
|
+
/**
|
|
8341
|
+
* Time Complexity: O(n)
|
|
8342
|
+
* Space Complexity: O(log n)
|
|
8343
|
+
*
|
|
8344
|
+
* The function `getHeight` calculates the maximum height of a binary tree using either recursive or
|
|
8345
|
+
* iterative traversal.
|
|
8346
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
8347
|
+
* starting node of the binary tree from which we want to calculate the height. It can be of type
|
|
8348
|
+
* `K`, `NODE`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
|
|
8349
|
+
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
8350
|
+
* height of the tree using a recursive approach or an iterative approach. It can have two possible
|
|
8351
|
+
* values:
|
|
8352
|
+
* @returns the height of the binary tree.
|
|
8353
|
+
*/
|
|
8354
|
+
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8355
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8356
|
+
if (!beginRoot)
|
|
8357
|
+
return -1;
|
|
8358
|
+
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
8359
|
+
const _getMaxHeight = (cur) => {
|
|
8360
|
+
if (!cur)
|
|
8361
|
+
return -1;
|
|
8362
|
+
const leftHeight = _getMaxHeight(cur.left);
|
|
8363
|
+
const rightHeight = _getMaxHeight(cur.right);
|
|
8364
|
+
return Math.max(leftHeight, rightHeight) + 1;
|
|
8365
|
+
};
|
|
8366
|
+
return _getMaxHeight(beginRoot);
|
|
8367
|
+
} else {
|
|
8368
|
+
const stack = [{ node: beginRoot, depth: 0 }];
|
|
8369
|
+
let maxHeight = 0;
|
|
8370
|
+
while (stack.length > 0) {
|
|
8371
|
+
const { node, depth } = stack.pop();
|
|
8372
|
+
if (node.left)
|
|
8373
|
+
stack.push({ node: node.left, depth: depth + 1 });
|
|
8374
|
+
if (node.right)
|
|
8375
|
+
stack.push({ node: node.right, depth: depth + 1 });
|
|
8376
|
+
maxHeight = Math.max(maxHeight, depth);
|
|
8377
|
+
}
|
|
8378
|
+
return maxHeight;
|
|
8379
|
+
}
|
|
8380
|
+
}
|
|
8381
|
+
/**
|
|
8382
|
+
* Time Complexity: O(n)
|
|
8383
|
+
* Space Complexity: O(log n)
|
|
8384
|
+
*/
|
|
8385
|
+
/**
|
|
8386
|
+
* Time Complexity: O(n)
|
|
8387
|
+
* Space Complexity: O(log n)
|
|
8388
|
+
*
|
|
8389
|
+
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
8390
|
+
* recursive or iterative approach.
|
|
8391
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
8392
|
+
* starting node of the binary tree from which we want to calculate the minimum height. It can be of
|
|
8393
|
+
* type `K`, `NODE`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
|
|
8394
|
+
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
8395
|
+
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
8396
|
+
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
7778
8397
|
*/
|
|
8398
|
+
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8399
|
+
var _a, _b, _c;
|
|
8400
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8401
|
+
if (!beginRoot)
|
|
8402
|
+
return -1;
|
|
8403
|
+
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
8404
|
+
const _getMinHeight = (cur) => {
|
|
8405
|
+
if (!cur)
|
|
8406
|
+
return 0;
|
|
8407
|
+
if (!cur.left && !cur.right)
|
|
8408
|
+
return 0;
|
|
8409
|
+
const leftMinHeight = _getMinHeight(cur.left);
|
|
8410
|
+
const rightMinHeight = _getMinHeight(cur.right);
|
|
8411
|
+
return Math.min(leftMinHeight, rightMinHeight) + 1;
|
|
8412
|
+
};
|
|
8413
|
+
return _getMinHeight(beginRoot);
|
|
8414
|
+
} else {
|
|
8415
|
+
const stack = [];
|
|
8416
|
+
let node = beginRoot, last = null;
|
|
8417
|
+
const depths = /* @__PURE__ */ new Map();
|
|
8418
|
+
while (stack.length > 0 || node) {
|
|
8419
|
+
if (node) {
|
|
8420
|
+
stack.push(node);
|
|
8421
|
+
node = node.left;
|
|
8422
|
+
} else {
|
|
8423
|
+
node = stack[stack.length - 1];
|
|
8424
|
+
if (!node.right || last === node.right) {
|
|
8425
|
+
node = stack.pop();
|
|
8426
|
+
if (node) {
|
|
8427
|
+
const leftMinHeight = node.left ? (_a = depths.get(node.left)) != null ? _a : -1 : -1;
|
|
8428
|
+
const rightMinHeight = node.right ? (_b = depths.get(node.right)) != null ? _b : -1 : -1;
|
|
8429
|
+
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
8430
|
+
last = node;
|
|
8431
|
+
node = null;
|
|
8432
|
+
}
|
|
8433
|
+
} else
|
|
8434
|
+
node = node.right;
|
|
8435
|
+
}
|
|
8436
|
+
}
|
|
8437
|
+
return (_c = depths.get(beginRoot)) != null ? _c : -1;
|
|
8438
|
+
}
|
|
8439
|
+
}
|
|
8440
|
+
/**
|
|
8441
|
+
* Time Complexity: O(log n)
|
|
8442
|
+
* Space Complexity: O(log n)
|
|
8443
|
+
* /
|
|
8444
|
+
|
|
8445
|
+
/**
|
|
8446
|
+
* Time Complexity: O(log n)
|
|
8447
|
+
* Space Complexity: O(log n)
|
|
8448
|
+
*
|
|
8449
|
+
* The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
|
|
8450
|
+
* structure, with the option to reverse the order of the nodes.
|
|
8451
|
+
* @param {K | NODE | null | undefined} beginNode - The `beginRoot` parameter represents the
|
|
8452
|
+
* starting node from which you want to find the path to the root. It can be of type `K`, `NODE`,
|
|
8453
|
+
* `null`, or `undefined`.
|
|
8454
|
+
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
8455
|
+
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
8456
|
+
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
|
8457
|
+
* @returns The function `getPathToRoot` returns an array of nodes (`NODE[]`).
|
|
8458
|
+
*/
|
|
7779
8459
|
getPathToRoot(beginNode, isReverse = true) {
|
|
7780
8460
|
const result = [];
|
|
7781
8461
|
beginNode = this.ensureNode(beginNode);
|
|
@@ -7798,12 +8478,12 @@ var dataStructureTyped = (() => {
|
|
|
7798
8478
|
*
|
|
7799
8479
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
|
|
7800
8480
|
* iteratively.
|
|
7801
|
-
* @param {K |
|
|
7802
|
-
* for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `
|
|
8481
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
8482
|
+
* for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `NODE` (a
|
|
7803
8483
|
* node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
|
|
7804
8484
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
7805
8485
|
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
7806
|
-
* @returns The function `getLeftMost` returns the leftmost node (`
|
|
8486
|
+
* @returns The function `getLeftMost` returns the leftmost node (`NODE`) in the binary tree. If there
|
|
7807
8487
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
7808
8488
|
*/
|
|
7809
8489
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -7836,13 +8516,13 @@ var dataStructureTyped = (() => {
|
|
|
7836
8516
|
*
|
|
7837
8517
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
7838
8518
|
* iteratively.
|
|
7839
|
-
* @param {K |
|
|
7840
|
-
* starting node from which we want to find the rightmost node. It can be of type `K`, `
|
|
8519
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
8520
|
+
* starting node from which we want to find the rightmost node. It can be of type `K`, `NODE`,
|
|
7841
8521
|
* `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
|
|
7842
8522
|
* current object.
|
|
7843
8523
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
7844
8524
|
* type of iteration to use when finding the rightmost node. It can have one of two values:
|
|
7845
|
-
* @returns The function `getRightMost` returns the rightmost node (`
|
|
8525
|
+
* @returns The function `getRightMost` returns the rightmost node (`NODE`) in a binary tree. If there
|
|
7846
8526
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
7847
8527
|
*/
|
|
7848
8528
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -7870,79 +8550,82 @@ var dataStructureTyped = (() => {
|
|
|
7870
8550
|
* Space Complexity: O(1)
|
|
7871
8551
|
*/
|
|
7872
8552
|
/**
|
|
7873
|
-
* Time Complexity: O(n)
|
|
8553
|
+
* Time Complexity: O(log n)
|
|
7874
8554
|
* Space Complexity: O(1)
|
|
7875
8555
|
*
|
|
7876
|
-
* The function
|
|
7877
|
-
* @param {
|
|
7878
|
-
*
|
|
7879
|
-
* @
|
|
7880
|
-
* type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
|
|
7881
|
-
* possible values:
|
|
7882
|
-
* @returns a boolean value.
|
|
8556
|
+
* The function returns the predecessor of a given node in a tree.
|
|
8557
|
+
* @param {NODE} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
|
|
8558
|
+
* tree.
|
|
8559
|
+
* @returns the predecessor of the given 'node'.
|
|
7883
8560
|
*/
|
|
7884
|
-
|
|
7885
|
-
|
|
7886
|
-
|
|
7887
|
-
|
|
7888
|
-
|
|
7889
|
-
|
|
7890
|
-
if (!cur)
|
|
7891
|
-
return true;
|
|
7892
|
-
const numKey = this.extractor(cur.key);
|
|
7893
|
-
if (numKey <= min || numKey >= max)
|
|
7894
|
-
return false;
|
|
7895
|
-
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
7896
|
-
};
|
|
7897
|
-
const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
7898
|
-
const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
7899
|
-
return isStandardBST || isInverseBST;
|
|
7900
|
-
} else {
|
|
7901
|
-
const checkBST = (checkMax = false) => {
|
|
7902
|
-
const stack = [];
|
|
7903
|
-
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
7904
|
-
let curr = beginRoot;
|
|
7905
|
-
while (curr || stack.length > 0) {
|
|
7906
|
-
while (curr) {
|
|
7907
|
-
stack.push(curr);
|
|
7908
|
-
curr = curr.left;
|
|
7909
|
-
}
|
|
7910
|
-
curr = stack.pop();
|
|
7911
|
-
const numKey = this.extractor(curr.key);
|
|
7912
|
-
if (!curr || !checkMax && prev >= numKey || checkMax && prev <= numKey)
|
|
7913
|
-
return false;
|
|
7914
|
-
prev = numKey;
|
|
7915
|
-
curr = curr.right;
|
|
8561
|
+
getPredecessor(node) {
|
|
8562
|
+
if (this.isRealNode(node.left)) {
|
|
8563
|
+
let predecessor = node.left;
|
|
8564
|
+
while (!this.isRealNode(predecessor) || this.isRealNode(predecessor.right) && predecessor.right !== node) {
|
|
8565
|
+
if (this.isRealNode(predecessor)) {
|
|
8566
|
+
predecessor = predecessor.right;
|
|
7916
8567
|
}
|
|
7917
|
-
|
|
7918
|
-
|
|
7919
|
-
|
|
7920
|
-
return
|
|
8568
|
+
}
|
|
8569
|
+
return predecessor;
|
|
8570
|
+
} else {
|
|
8571
|
+
return node;
|
|
8572
|
+
}
|
|
8573
|
+
}
|
|
8574
|
+
/**
|
|
8575
|
+
* Time Complexity: O(log n)
|
|
8576
|
+
* Space Complexity: O(1)
|
|
8577
|
+
*/
|
|
8578
|
+
/**
|
|
8579
|
+
* Time Complexity: O(log n)
|
|
8580
|
+
* Space Complexity: O(1)
|
|
8581
|
+
*
|
|
8582
|
+
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
8583
|
+
* @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
|
|
8584
|
+
* @returns the successor of the given node or key. The successor is the node that comes immediately
|
|
8585
|
+
* after the given node in the inorder traversal of the binary tree.
|
|
8586
|
+
*/
|
|
8587
|
+
getSuccessor(x) {
|
|
8588
|
+
x = this.ensureNode(x);
|
|
8589
|
+
if (!this.isRealNode(x))
|
|
8590
|
+
return void 0;
|
|
8591
|
+
if (this.isRealNode(x.right)) {
|
|
8592
|
+
return this.getLeftMost(x.right);
|
|
8593
|
+
}
|
|
8594
|
+
let y = x.parent;
|
|
8595
|
+
while (this.isRealNode(y) && x === y.right) {
|
|
8596
|
+
x = y;
|
|
8597
|
+
y = y.parent;
|
|
7921
8598
|
}
|
|
8599
|
+
return y;
|
|
7922
8600
|
}
|
|
7923
8601
|
/**
|
|
7924
|
-
|
|
7925
|
-
|
|
7926
|
-
|
|
7927
|
-
|
|
7928
|
-
|
|
7929
|
-
|
|
7930
|
-
|
|
7931
|
-
|
|
7932
|
-
|
|
7933
|
-
|
|
7934
|
-
|
|
7935
|
-
|
|
7936
|
-
|
|
7937
|
-
|
|
7938
|
-
|
|
7939
|
-
|
|
7940
|
-
|
|
7941
|
-
|
|
7942
|
-
|
|
7943
|
-
|
|
7944
|
-
|
|
7945
|
-
|
|
8602
|
+
* Time complexity: O(n)
|
|
8603
|
+
* Space complexity: O(n)
|
|
8604
|
+
* /
|
|
8605
|
+
|
|
8606
|
+
/**
|
|
8607
|
+
* Time complexity: O(n)
|
|
8608
|
+
* Space complexity: O(n)
|
|
8609
|
+
*
|
|
8610
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
|
|
8611
|
+
* specified pattern and iteration type, and returns an array of values obtained from applying a
|
|
8612
|
+
* callback function to each visited node.
|
|
8613
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8614
|
+
* the tree during the depth-first search. It takes a single parameter, which can be of type `NODE`,
|
|
8615
|
+
* `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
|
|
8616
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
8617
|
+
* nodes are traversed during the depth-first search. It can have one of the following values:
|
|
8618
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
8619
|
+
* for the depth-first search traversal. It can be specified as a key, a node object, or
|
|
8620
|
+
* `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
|
|
8621
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
8622
|
+
* iteration to use when traversing the tree. It can have one of the following values:
|
|
8623
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
8624
|
+
* whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
|
|
8625
|
+
* `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
|
|
8626
|
+
* `false`, null or undefined
|
|
8627
|
+
* @returns an array of values that are the return values of the callback function.
|
|
8628
|
+
*/
|
|
7946
8629
|
dfs(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root, iterationType = "ITERATIVE" /* ITERATIVE */, includeNull = false) {
|
|
7947
8630
|
beginRoot = this.ensureNode(beginRoot);
|
|
7948
8631
|
if (!beginRoot)
|
|
@@ -8042,6 +8725,10 @@ var dataStructureTyped = (() => {
|
|
|
8042
8725
|
}
|
|
8043
8726
|
return ans;
|
|
8044
8727
|
}
|
|
8728
|
+
/**
|
|
8729
|
+
* Time complexity: O(n)
|
|
8730
|
+
* Space complexity: O(n)
|
|
8731
|
+
*/
|
|
8045
8732
|
/**
|
|
8046
8733
|
* Time complexity: O(n)
|
|
8047
8734
|
* Space complexity: O(n)
|
|
@@ -8051,7 +8738,7 @@ var dataStructureTyped = (() => {
|
|
|
8051
8738
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8052
8739
|
* the breadth-first search traversal. It takes a single parameter, which is the current node being
|
|
8053
8740
|
* visited, and returns a value of any type.
|
|
8054
|
-
* @param {K |
|
|
8741
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
8055
8742
|
* starting node for the breadth-first search traversal. It can be specified as a key, a node object,
|
|
8056
8743
|
* or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
|
|
8057
8744
|
* the class is used as
|
|
@@ -8112,6 +8799,10 @@ var dataStructureTyped = (() => {
|
|
|
8112
8799
|
}
|
|
8113
8800
|
return ans;
|
|
8114
8801
|
}
|
|
8802
|
+
/**
|
|
8803
|
+
* Time complexity: O(n)
|
|
8804
|
+
* Space complexity: O(n)
|
|
8805
|
+
*/
|
|
8115
8806
|
/**
|
|
8116
8807
|
* Time complexity: O(n)
|
|
8117
8808
|
* Space complexity: O(n)
|
|
@@ -8120,10 +8811,10 @@ var dataStructureTyped = (() => {
|
|
|
8120
8811
|
* a binary tree and contains the values returned by a callback function applied to the nodes at that
|
|
8121
8812
|
* level.
|
|
8122
8813
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8123
|
-
* the tree. It takes a single parameter, which can be of type `
|
|
8814
|
+
* the tree. It takes a single parameter, which can be of type `NODE`, `null`, or `undefined`, and
|
|
8124
8815
|
* returns a value of any type.
|
|
8125
|
-
* @param {K |
|
|
8126
|
-
* starting node for traversing the tree. It can be either a node object (`
|
|
8816
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
8817
|
+
* starting node for traversing the tree. It can be either a node object (`NODE`), a key value
|
|
8127
8818
|
* (`K`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
|
|
8128
8819
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
8129
8820
|
* performed on the tree. It can have two possible values:
|
|
@@ -8179,52 +8870,6 @@ var dataStructureTyped = (() => {
|
|
|
8179
8870
|
}
|
|
8180
8871
|
return levelsNodes;
|
|
8181
8872
|
}
|
|
8182
|
-
/**
|
|
8183
|
-
* Time Complexity: O(log n)
|
|
8184
|
-
* Space Complexity: O(1)
|
|
8185
|
-
*/
|
|
8186
|
-
/**
|
|
8187
|
-
* Time Complexity: O(log n)
|
|
8188
|
-
* Space Complexity: O(1)
|
|
8189
|
-
*
|
|
8190
|
-
* The function returns the predecessor of a given node in a tree.
|
|
8191
|
-
* @param {N} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
|
|
8192
|
-
* tree.
|
|
8193
|
-
* @returns the predecessor of the given 'node'.
|
|
8194
|
-
*/
|
|
8195
|
-
getPredecessor(node) {
|
|
8196
|
-
if (this.isRealNode(node.left)) {
|
|
8197
|
-
let predecessor = node.left;
|
|
8198
|
-
while (!this.isRealNode(predecessor) || this.isRealNode(predecessor.right) && predecessor.right !== node) {
|
|
8199
|
-
if (this.isRealNode(predecessor)) {
|
|
8200
|
-
predecessor = predecessor.right;
|
|
8201
|
-
}
|
|
8202
|
-
}
|
|
8203
|
-
return predecessor;
|
|
8204
|
-
} else {
|
|
8205
|
-
return node;
|
|
8206
|
-
}
|
|
8207
|
-
}
|
|
8208
|
-
/**
|
|
8209
|
-
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
8210
|
-
* @param {K | N | null} [x] - The parameter `x` can be of type `K`, `N`, or `null`.
|
|
8211
|
-
* @returns the successor of the given node or key. The successor is the node that comes immediately
|
|
8212
|
-
* after the given node in the inorder traversal of the binary tree.
|
|
8213
|
-
*/
|
|
8214
|
-
getSuccessor(x) {
|
|
8215
|
-
x = this.ensureNode(x);
|
|
8216
|
-
if (!this.isRealNode(x))
|
|
8217
|
-
return void 0;
|
|
8218
|
-
if (this.isRealNode(x.right)) {
|
|
8219
|
-
return this.getLeftMost(x.right);
|
|
8220
|
-
}
|
|
8221
|
-
let y = x.parent;
|
|
8222
|
-
while (this.isRealNode(y) && x === y.right) {
|
|
8223
|
-
x = y;
|
|
8224
|
-
y = y.parent;
|
|
8225
|
-
}
|
|
8226
|
-
return y;
|
|
8227
|
-
}
|
|
8228
8873
|
/**
|
|
8229
8874
|
* Time complexity: O(n)
|
|
8230
8875
|
* Space complexity: O(n)
|
|
@@ -8236,12 +8881,12 @@ var dataStructureTyped = (() => {
|
|
|
8236
8881
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
8237
8882
|
* algorithm.
|
|
8238
8883
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8239
|
-
* the tree. It takes a single parameter of type `
|
|
8884
|
+
* the tree. It takes a single parameter of type `NODE` (the type of the nodes in the tree) and returns
|
|
8240
8885
|
* a value of any type.
|
|
8241
8886
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
8242
8887
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
8243
8888
|
* following values:
|
|
8244
|
-
* @param {K |
|
|
8889
|
+
* @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
8245
8890
|
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
8246
8891
|
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
8247
8892
|
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
@@ -8343,7 +8988,17 @@ var dataStructureTyped = (() => {
|
|
|
8343
8988
|
*/
|
|
8344
8989
|
clone() {
|
|
8345
8990
|
const cloned = this.createTree();
|
|
8346
|
-
this.bfs(
|
|
8991
|
+
this.bfs(
|
|
8992
|
+
(node) => {
|
|
8993
|
+
if (node === null)
|
|
8994
|
+
cloned.add(null);
|
|
8995
|
+
else
|
|
8996
|
+
cloned.add([node.key, node.value]);
|
|
8997
|
+
},
|
|
8998
|
+
this.root,
|
|
8999
|
+
this.iterationType,
|
|
9000
|
+
true
|
|
9001
|
+
);
|
|
8347
9002
|
return cloned;
|
|
8348
9003
|
}
|
|
8349
9004
|
/**
|
|
@@ -8420,7 +9075,7 @@ var dataStructureTyped = (() => {
|
|
|
8420
9075
|
* Space Complexity: O(n)
|
|
8421
9076
|
*
|
|
8422
9077
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
8423
|
-
* @param {K |
|
|
9078
|
+
* @param {K | NODE | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | NODE | null |
|
|
8424
9079
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
8425
9080
|
* following types:
|
|
8426
9081
|
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
@@ -8434,7 +9089,7 @@ var dataStructureTyped = (() => {
|
|
|
8434
9089
|
console.log(`U for undefined
|
|
8435
9090
|
`);
|
|
8436
9091
|
if (opts.isShowNull)
|
|
8437
|
-
console.log(`
|
|
9092
|
+
console.log(`NODE for null
|
|
8438
9093
|
`);
|
|
8439
9094
|
if (opts.isShowRedBlackNIL)
|
|
8440
9095
|
console.log(`S for Sentinel Node
|
|
@@ -8447,6 +9102,15 @@ var dataStructureTyped = (() => {
|
|
|
8447
9102
|
};
|
|
8448
9103
|
display(beginRoot);
|
|
8449
9104
|
}
|
|
9105
|
+
/**
|
|
9106
|
+
* The function `_getIterator` is a protected generator function that returns an iterator for the
|
|
9107
|
+
* key-value pairs in a binary search tree.
|
|
9108
|
+
* @param node - The `node` parameter represents the current node in the binary search tree. It is an
|
|
9109
|
+
* optional parameter with a default value of `this.root`, which means if no node is provided, the
|
|
9110
|
+
* root node of the tree will be used as the starting point for iteration.
|
|
9111
|
+
* @returns The function `_getIterator` returns an `IterableIterator` of key-value pairs `[K, V |
|
|
9112
|
+
* undefined]`.
|
|
9113
|
+
*/
|
|
8450
9114
|
*_getIterator(node = this.root) {
|
|
8451
9115
|
if (!node)
|
|
8452
9116
|
return;
|
|
@@ -8474,6 +9138,20 @@ var dataStructureTyped = (() => {
|
|
|
8474
9138
|
}
|
|
8475
9139
|
}
|
|
8476
9140
|
}
|
|
9141
|
+
/**
|
|
9142
|
+
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
9143
|
+
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
9144
|
+
* @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
|
|
9145
|
+
* It can be of type `NODE`, `null`, or `undefined`.
|
|
9146
|
+
* @param {BinaryTreePrintOptions} options - The `options` parameter is an object that contains the
|
|
9147
|
+
* following properties:
|
|
9148
|
+
* @returns The function `_displayAux` returns a `NodeDisplayLayout` which is an array containing the
|
|
9149
|
+
* following elements:
|
|
9150
|
+
* 1. `mergedLines`: An array of strings representing the lines of the node display.
|
|
9151
|
+
* 2. `totalWidth`: The total width of the node display.
|
|
9152
|
+
* 3. `totalHeight`: The total height of the node display.
|
|
9153
|
+
* 4. `middleIndex`: The index of the middle character
|
|
9154
|
+
*/
|
|
8477
9155
|
_displayAux(node, options) {
|
|
8478
9156
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
8479
9157
|
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
@@ -8492,7 +9170,7 @@ var dataStructureTyped = (() => {
|
|
|
8492
9170
|
this._displayAux(node.right, options)
|
|
8493
9171
|
);
|
|
8494
9172
|
} else {
|
|
8495
|
-
const line = node === void 0 ? "U" : "
|
|
9173
|
+
const line = node === void 0 ? "U" : "NODE", width = line.length;
|
|
8496
9174
|
return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
|
|
8497
9175
|
}
|
|
8498
9176
|
function _buildNodeDisplay(line, width, left, right) {
|
|
@@ -8516,9 +9194,9 @@ var dataStructureTyped = (() => {
|
|
|
8516
9194
|
}
|
|
8517
9195
|
/**
|
|
8518
9196
|
* Swap the data of two nodes in the binary tree.
|
|
8519
|
-
* @param {
|
|
8520
|
-
* @param {
|
|
8521
|
-
* @returns {
|
|
9197
|
+
* @param {NODE} srcNode - The source node to swap.
|
|
9198
|
+
* @param {NODE} destNode - The destination node to swap.
|
|
9199
|
+
* @returns {NODE} - The destination node after the swap.
|
|
8522
9200
|
*/
|
|
8523
9201
|
_swapProperties(srcNode, destNode) {
|
|
8524
9202
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -8538,9 +9216,9 @@ var dataStructureTyped = (() => {
|
|
|
8538
9216
|
}
|
|
8539
9217
|
/**
|
|
8540
9218
|
* The function replaces an old node with a new node in a binary tree.
|
|
8541
|
-
* @param {
|
|
9219
|
+
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
8542
9220
|
* tree.
|
|
8543
|
-
* @param {
|
|
9221
|
+
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
8544
9222
|
* tree.
|
|
8545
9223
|
* @returns The method is returning the newNode.
|
|
8546
9224
|
*/
|
|
@@ -8563,8 +9241,8 @@ var dataStructureTyped = (() => {
|
|
|
8563
9241
|
/**
|
|
8564
9242
|
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
8565
9243
|
* it also sets the parent property of the value to undefined.
|
|
8566
|
-
* @param {
|
|
8567
|
-
* type `
|
|
9244
|
+
* @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`, which means it can either be of
|
|
9245
|
+
* type `NODE` or `null`.
|
|
8568
9246
|
*/
|
|
8569
9247
|
_setRoot(v) {
|
|
8570
9248
|
if (v) {
|
|
@@ -8585,32 +9263,18 @@ var dataStructureTyped = (() => {
|
|
|
8585
9263
|
this._left = void 0;
|
|
8586
9264
|
this._right = void 0;
|
|
8587
9265
|
}
|
|
8588
|
-
/**
|
|
8589
|
-
* Get the left child node.
|
|
8590
|
-
*/
|
|
8591
9266
|
get left() {
|
|
8592
9267
|
return this._left;
|
|
8593
9268
|
}
|
|
8594
|
-
/**
|
|
8595
|
-
* Set the left child node.
|
|
8596
|
-
* @param {N | undefined} v - The left child node.
|
|
8597
|
-
*/
|
|
8598
9269
|
set left(v) {
|
|
8599
9270
|
if (v) {
|
|
8600
9271
|
v.parent = this;
|
|
8601
9272
|
}
|
|
8602
9273
|
this._left = v;
|
|
8603
9274
|
}
|
|
8604
|
-
/**
|
|
8605
|
-
* Get the right child node.
|
|
8606
|
-
*/
|
|
8607
9275
|
get right() {
|
|
8608
9276
|
return this._right;
|
|
8609
9277
|
}
|
|
8610
|
-
/**
|
|
8611
|
-
* Set the right child node.
|
|
8612
|
-
* @param {N | undefined} v - The right child node.
|
|
8613
|
-
*/
|
|
8614
9278
|
set right(v) {
|
|
8615
9279
|
if (v) {
|
|
8616
9280
|
v.parent = this;
|
|
@@ -8671,14 +9335,14 @@ var dataStructureTyped = (() => {
|
|
|
8671
9335
|
}, options));
|
|
8672
9336
|
}
|
|
8673
9337
|
/**
|
|
8674
|
-
* The function `
|
|
9338
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
8675
9339
|
* otherwise it returns undefined.
|
|
8676
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
9340
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
8677
9341
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
8678
|
-
* `
|
|
8679
|
-
* @returns a node of type
|
|
9342
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
9343
|
+
* @returns a node of type NODE or undefined.
|
|
8680
9344
|
*/
|
|
8681
|
-
|
|
9345
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
8682
9346
|
let node;
|
|
8683
9347
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
8684
9348
|
return;
|
|
@@ -8701,7 +9365,6 @@ var dataStructureTyped = (() => {
|
|
|
8701
9365
|
/**
|
|
8702
9366
|
* Time Complexity: O(log n)
|
|
8703
9367
|
* Space Complexity: O(log n)
|
|
8704
|
-
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
8705
9368
|
*/
|
|
8706
9369
|
/**
|
|
8707
9370
|
* Time Complexity: O(log n)
|
|
@@ -8709,11 +9372,11 @@ var dataStructureTyped = (() => {
|
|
|
8709
9372
|
*
|
|
8710
9373
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
8711
9374
|
* otherwise it returns the key itself.
|
|
8712
|
-
* @param {K |
|
|
9375
|
+
* @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
|
|
8713
9376
|
* `undefined`.
|
|
8714
9377
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
8715
9378
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
8716
|
-
* @returns either a node object (
|
|
9379
|
+
* @returns either a node object (NODE) or undefined.
|
|
8717
9380
|
*/
|
|
8718
9381
|
ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
8719
9382
|
let res;
|
|
@@ -8730,7 +9393,7 @@ var dataStructureTyped = (() => {
|
|
|
8730
9393
|
}
|
|
8731
9394
|
/**
|
|
8732
9395
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
8733
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,
|
|
9396
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
8734
9397
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
8735
9398
|
*/
|
|
8736
9399
|
isNode(keyOrNodeOrEntry) {
|
|
@@ -8739,7 +9402,6 @@ var dataStructureTyped = (() => {
|
|
|
8739
9402
|
/**
|
|
8740
9403
|
* Time Complexity: O(log n)
|
|
8741
9404
|
* Space Complexity: O(1)
|
|
8742
|
-
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8743
9405
|
*/
|
|
8744
9406
|
/**
|
|
8745
9407
|
* Time Complexity: O(log n)
|
|
@@ -8754,7 +9416,7 @@ var dataStructureTyped = (() => {
|
|
|
8754
9416
|
* node was not added.
|
|
8755
9417
|
*/
|
|
8756
9418
|
add(keyOrNodeOrEntry, value) {
|
|
8757
|
-
const newNode = this.
|
|
9419
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
8758
9420
|
if (newNode === void 0)
|
|
8759
9421
|
return false;
|
|
8760
9422
|
if (this.root === void 0) {
|
|
@@ -8770,7 +9432,6 @@ var dataStructureTyped = (() => {
|
|
|
8770
9432
|
} else if (this._compare(current.key, newNode.key) === "gt" /* gt */) {
|
|
8771
9433
|
if (current.left === void 0) {
|
|
8772
9434
|
current.left = newNode;
|
|
8773
|
-
newNode.parent = current;
|
|
8774
9435
|
this._size++;
|
|
8775
9436
|
return true;
|
|
8776
9437
|
}
|
|
@@ -8778,7 +9439,6 @@ var dataStructureTyped = (() => {
|
|
|
8778
9439
|
} else {
|
|
8779
9440
|
if (current.right === void 0) {
|
|
8780
9441
|
current.right = newNode;
|
|
8781
|
-
newNode.parent = current;
|
|
8782
9442
|
this._size++;
|
|
8783
9443
|
return true;
|
|
8784
9444
|
}
|
|
@@ -8789,12 +9449,11 @@ var dataStructureTyped = (() => {
|
|
|
8789
9449
|
}
|
|
8790
9450
|
/**
|
|
8791
9451
|
* Time Complexity: O(k log n)
|
|
8792
|
-
* Space Complexity: O(k)
|
|
8793
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
9452
|
+
* Space Complexity: O(k + log n)
|
|
8794
9453
|
*/
|
|
8795
9454
|
/**
|
|
8796
9455
|
* Time Complexity: O(k log n)
|
|
8797
|
-
* Space Complexity: O(k)
|
|
9456
|
+
* Space Complexity: O(k + log n)
|
|
8798
9457
|
*
|
|
8799
9458
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
8800
9459
|
* balancing the tree after each addition.
|
|
@@ -8810,7 +9469,7 @@ var dataStructureTyped = (() => {
|
|
|
8810
9469
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
8811
9470
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
8812
9471
|
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
8813
|
-
* @returns The function `addMany` returns an array of nodes (`
|
|
9472
|
+
* @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
|
|
8814
9473
|
*/
|
|
8815
9474
|
addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
8816
9475
|
const inserted = [];
|
|
@@ -8900,7 +9559,7 @@ var dataStructureTyped = (() => {
|
|
|
8900
9559
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
8901
9560
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
8902
9561
|
* values:
|
|
8903
|
-
* @returns The function `getNodeByKey` returns a node (`
|
|
9562
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
8904
9563
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
8905
9564
|
*/
|
|
8906
9565
|
getNodeByKey(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
@@ -8935,32 +9594,31 @@ var dataStructureTyped = (() => {
|
|
|
8935
9594
|
}
|
|
8936
9595
|
/**
|
|
8937
9596
|
* Time Complexity: O(log n)
|
|
8938
|
-
* Space Complexity: O(log n)
|
|
8939
|
-
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
9597
|
+
* Space Complexity: O(k + log n)
|
|
8940
9598
|
* /
|
|
8941
9599
|
|
|
8942
9600
|
/**
|
|
8943
9601
|
* Time Complexity: O(log n)
|
|
8944
|
-
* Space Complexity: O(log n)
|
|
9602
|
+
* Space Complexity: O(k + log n)
|
|
8945
9603
|
*
|
|
8946
9604
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
8947
9605
|
* recursive or iterative approach.
|
|
8948
9606
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
8949
9607
|
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
8950
9608
|
* callback function `C`.
|
|
8951
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
9609
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
|
|
8952
9610
|
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
8953
|
-
* function type that extends the `BTNCallback<
|
|
9611
|
+
* function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
|
|
8954
9612
|
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
8955
9613
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
8956
9614
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
8957
9615
|
* searching for all nodes that match the identifier and return an array containing
|
|
8958
|
-
* @param {K |
|
|
9616
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
8959
9617
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
8960
9618
|
* traversal will start from the root of the tree.
|
|
8961
9619
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
8962
9620
|
* performed on the binary tree. It can have two possible values:
|
|
8963
|
-
* @returns The method returns an array of nodes (`
|
|
9621
|
+
* @returns The method returns an array of nodes (`NODE[]`).
|
|
8964
9622
|
*/
|
|
8965
9623
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8966
9624
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -9013,25 +9671,6 @@ var dataStructureTyped = (() => {
|
|
|
9013
9671
|
}
|
|
9014
9672
|
return ans;
|
|
9015
9673
|
}
|
|
9016
|
-
// /**
|
|
9017
|
-
// * The function overrides the subTreeTraverse method and returns the result of calling the super
|
|
9018
|
-
// * method with the provided arguments.
|
|
9019
|
-
// * @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
9020
|
-
// * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
|
|
9021
|
-
// * the tree. The return type of the callback function can be any type.
|
|
9022
|
-
// * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
|
|
9023
|
-
// * can be either a key, a node, or an entry.
|
|
9024
|
-
// * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
9025
|
-
// * be performed during the traversal of the subtree. It can have one of the following values:
|
|
9026
|
-
// * @returns The method is returning an array of the return type of the callback function.
|
|
9027
|
-
// */
|
|
9028
|
-
// override subTreeTraverse<C extends BTNCallback<N>>(
|
|
9029
|
-
// callback: C = this._defaultOneParamCallback as C,
|
|
9030
|
-
// beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
9031
|
-
// iterationType = this.iterationType
|
|
9032
|
-
// ): ReturnType<C>[] {
|
|
9033
|
-
// return super.subTreeTraverse(callback, beginRoot, iterationType, false);
|
|
9034
|
-
// }
|
|
9035
9674
|
/**
|
|
9036
9675
|
* Time complexity: O(n)
|
|
9037
9676
|
* Space complexity: O(n)
|
|
@@ -9093,7 +9732,7 @@ var dataStructureTyped = (() => {
|
|
|
9093
9732
|
* The function overrides the listLevels method and returns an array of arrays containing the return
|
|
9094
9733
|
* type of the callback function for each level of the tree.
|
|
9095
9734
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
9096
|
-
* `BTNCallback<
|
|
9735
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
|
|
9097
9736
|
* during the level listing process.
|
|
9098
9737
|
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
|
|
9099
9738
|
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
|
|
@@ -9108,18 +9747,17 @@ var dataStructureTyped = (() => {
|
|
|
9108
9747
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
9109
9748
|
}
|
|
9110
9749
|
/**
|
|
9111
|
-
* Time Complexity: O(
|
|
9112
|
-
* Space Complexity: O(
|
|
9113
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
9750
|
+
* Time Complexity: O(log n)
|
|
9751
|
+
* Space Complexity: O(1)
|
|
9114
9752
|
*/
|
|
9115
9753
|
/**
|
|
9116
|
-
* Time Complexity: O(
|
|
9117
|
-
* Space Complexity: O(
|
|
9754
|
+
* Time Complexity: O(log n)
|
|
9755
|
+
* Space Complexity: O(1)
|
|
9118
9756
|
*
|
|
9119
9757
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
9120
9758
|
* leftmost node if the comparison result is greater than.
|
|
9121
|
-
* @param {K |
|
|
9122
|
-
* type `K`, `
|
|
9759
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
9760
|
+
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
9123
9761
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
9124
9762
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
9125
9763
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
@@ -9143,7 +9781,6 @@ var dataStructureTyped = (() => {
|
|
|
9143
9781
|
/**
|
|
9144
9782
|
* Time Complexity: O(log n)
|
|
9145
9783
|
* Space Complexity: O(log n)
|
|
9146
|
-
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
9147
9784
|
*/
|
|
9148
9785
|
/**
|
|
9149
9786
|
* Time Complexity: O(log n)
|
|
@@ -9153,12 +9790,12 @@ var dataStructureTyped = (() => {
|
|
|
9153
9790
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
9154
9791
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9155
9792
|
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
|
|
9156
|
-
* parameter of type `
|
|
9793
|
+
* parameter of type `NODE` (the node type) and returns a value of any type.
|
|
9157
9794
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
9158
9795
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
9159
9796
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
9160
9797
|
* `lesserOrGreater` are
|
|
9161
|
-
* @param {K |
|
|
9798
|
+
* @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
9162
9799
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
9163
9800
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
9164
9801
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -9265,8 +9902,8 @@ var dataStructureTyped = (() => {
|
|
|
9265
9902
|
* 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).
|
|
9266
9903
|
*/
|
|
9267
9904
|
/**
|
|
9268
|
-
* Time Complexity: O(n)
|
|
9269
|
-
* Space Complexity: O(n)
|
|
9905
|
+
* Time Complexity: O(n)
|
|
9906
|
+
* Space Complexity: O(log n)
|
|
9270
9907
|
*/
|
|
9271
9908
|
/**
|
|
9272
9909
|
* Time Complexity: O(n)
|
|
@@ -9779,7 +10416,7 @@ var dataStructureTyped = (() => {
|
|
|
9779
10416
|
var AVLTree = class _AVLTree extends BST {
|
|
9780
10417
|
/**
|
|
9781
10418
|
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
|
|
9782
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
10419
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
9783
10420
|
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
9784
10421
|
* initialization.
|
|
9785
10422
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
@@ -9797,7 +10434,7 @@ var dataStructureTyped = (() => {
|
|
|
9797
10434
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
9798
10435
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
9799
10436
|
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
9800
|
-
* node type `
|
|
10437
|
+
* node type `NODE`.
|
|
9801
10438
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
9802
10439
|
*/
|
|
9803
10440
|
createNode(key, value) {
|
|
@@ -9818,7 +10455,7 @@ var dataStructureTyped = (() => {
|
|
|
9818
10455
|
}
|
|
9819
10456
|
/**
|
|
9820
10457
|
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
9821
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
10458
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
9822
10459
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
9823
10460
|
*/
|
|
9824
10461
|
isNode(keyOrNodeOrEntry) {
|
|
@@ -9865,8 +10502,8 @@ var dataStructureTyped = (() => {
|
|
|
9865
10502
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9866
10503
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
9867
10504
|
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single
|
|
9868
|
-
* parameter of type `
|
|
9869
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<
|
|
10505
|
+
* parameter of type `NODE
|
|
10506
|
+
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
9870
10507
|
*/
|
|
9871
10508
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
9872
10509
|
if (identifier instanceof AVLTreeNode)
|
|
@@ -9882,9 +10519,9 @@ var dataStructureTyped = (() => {
|
|
|
9882
10519
|
/**
|
|
9883
10520
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
9884
10521
|
* tree.
|
|
9885
|
-
* @param {K |
|
|
9886
|
-
* needs to be swapped with the destination node. It can be of type `K`, `
|
|
9887
|
-
* @param {K |
|
|
10522
|
+
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
10523
|
+
* needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
|
|
10524
|
+
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
9888
10525
|
* node where the values from the source node will be swapped to.
|
|
9889
10526
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
9890
10527
|
* if either `srcNode` or `destNode` is undefined.
|
|
@@ -9911,14 +10548,13 @@ var dataStructureTyped = (() => {
|
|
|
9911
10548
|
/**
|
|
9912
10549
|
* Time Complexity: O(1)
|
|
9913
10550
|
* Space Complexity: O(1)
|
|
9914
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
9915
10551
|
*/
|
|
9916
10552
|
/**
|
|
9917
10553
|
* Time Complexity: O(1)
|
|
9918
10554
|
* Space Complexity: O(1)
|
|
9919
10555
|
*
|
|
9920
10556
|
* The function calculates the balance factor of a node in a binary tree.
|
|
9921
|
-
* @param {
|
|
10557
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
9922
10558
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
9923
10559
|
* height of the left subtree from the height of the right subtree.
|
|
9924
10560
|
*/
|
|
@@ -9933,7 +10569,6 @@ var dataStructureTyped = (() => {
|
|
|
9933
10569
|
/**
|
|
9934
10570
|
* Time Complexity: O(1)
|
|
9935
10571
|
* Space Complexity: O(1)
|
|
9936
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
9937
10572
|
*/
|
|
9938
10573
|
/**
|
|
9939
10574
|
* Time Complexity: O(1)
|
|
@@ -9941,7 +10576,7 @@ var dataStructureTyped = (() => {
|
|
|
9941
10576
|
*
|
|
9942
10577
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
9943
10578
|
* right children.
|
|
9944
|
-
* @param {
|
|
10579
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
9945
10580
|
*/
|
|
9946
10581
|
_updateHeight(node) {
|
|
9947
10582
|
if (!node.left && !node.right)
|
|
@@ -9954,58 +10589,16 @@ var dataStructureTyped = (() => {
|
|
|
9954
10589
|
else
|
|
9955
10590
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
9956
10591
|
}
|
|
9957
|
-
/**
|
|
9958
|
-
* Time Complexity: O(log n)
|
|
9959
|
-
* Space Complexity: O(1)
|
|
9960
|
-
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
|
|
9961
|
-
*/
|
|
9962
|
-
/**
|
|
9963
|
-
* Time Complexity: O(log n)
|
|
9964
|
-
* Space Complexity: O(1)
|
|
9965
|
-
*
|
|
9966
|
-
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
9967
|
-
* to restore balance in an AVL tree after inserting a node.
|
|
9968
|
-
* @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
9969
|
-
* AVL tree that needs to be balanced.
|
|
9970
|
-
*/
|
|
9971
|
-
_balancePath(node) {
|
|
9972
|
-
node = this.ensureNode(node);
|
|
9973
|
-
const path = this.getPathToRoot(node, false);
|
|
9974
|
-
for (let i = 0; i < path.length; i++) {
|
|
9975
|
-
const A = path[i];
|
|
9976
|
-
this._updateHeight(A);
|
|
9977
|
-
switch (this._balanceFactor(A)) {
|
|
9978
|
-
case -2:
|
|
9979
|
-
if (A && A.left) {
|
|
9980
|
-
if (this._balanceFactor(A.left) <= 0) {
|
|
9981
|
-
this._balanceLL(A);
|
|
9982
|
-
} else {
|
|
9983
|
-
this._balanceLR(A);
|
|
9984
|
-
}
|
|
9985
|
-
}
|
|
9986
|
-
break;
|
|
9987
|
-
case 2:
|
|
9988
|
-
if (A && A.right) {
|
|
9989
|
-
if (this._balanceFactor(A.right) >= 0) {
|
|
9990
|
-
this._balanceRR(A);
|
|
9991
|
-
} else {
|
|
9992
|
-
this._balanceRL(A);
|
|
9993
|
-
}
|
|
9994
|
-
}
|
|
9995
|
-
}
|
|
9996
|
-
}
|
|
9997
|
-
}
|
|
9998
10592
|
/**
|
|
9999
10593
|
* Time Complexity: O(1)
|
|
10000
10594
|
* Space Complexity: O(1)
|
|
10001
|
-
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
10002
10595
|
*/
|
|
10003
10596
|
/**
|
|
10004
10597
|
* Time Complexity: O(1)
|
|
10005
10598
|
* Space Complexity: O(1)
|
|
10006
10599
|
*
|
|
10007
10600
|
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
10008
|
-
* @param {
|
|
10601
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
10009
10602
|
*/
|
|
10010
10603
|
_balanceLL(A) {
|
|
10011
10604
|
const parentOfA = A.parent;
|
|
@@ -10044,7 +10637,7 @@ var dataStructureTyped = (() => {
|
|
|
10044
10637
|
* Space Complexity: O(1)
|
|
10045
10638
|
*
|
|
10046
10639
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
10047
|
-
* @param {
|
|
10640
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
10048
10641
|
*/
|
|
10049
10642
|
_balanceLR(A) {
|
|
10050
10643
|
const parentOfA = A.parent;
|
|
@@ -10098,7 +10691,7 @@ var dataStructureTyped = (() => {
|
|
|
10098
10691
|
* Space Complexity: O(1)
|
|
10099
10692
|
*
|
|
10100
10693
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
10101
|
-
* @param {
|
|
10694
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
10102
10695
|
*/
|
|
10103
10696
|
_balanceRR(A) {
|
|
10104
10697
|
const parentOfA = A.parent;
|
|
@@ -10138,7 +10731,7 @@ var dataStructureTyped = (() => {
|
|
|
10138
10731
|
* Space Complexity: O(1)
|
|
10139
10732
|
*
|
|
10140
10733
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
10141
|
-
* @param {
|
|
10734
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
10142
10735
|
*/
|
|
10143
10736
|
_balanceRL(A) {
|
|
10144
10737
|
const parentOfA = A.parent;
|
|
@@ -10183,6 +10776,47 @@ var dataStructureTyped = (() => {
|
|
|
10183
10776
|
B && this._updateHeight(B);
|
|
10184
10777
|
C && this._updateHeight(C);
|
|
10185
10778
|
}
|
|
10779
|
+
/**
|
|
10780
|
+
* Time Complexity: O(log n)
|
|
10781
|
+
* Space Complexity: O(1)
|
|
10782
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10783
|
+
*/
|
|
10784
|
+
/**
|
|
10785
|
+
* Time Complexity: O(log n)
|
|
10786
|
+
* Space Complexity: O(1)
|
|
10787
|
+
*
|
|
10788
|
+
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
10789
|
+
* to restore balance in an AVL tree after inserting a node.
|
|
10790
|
+
* @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
10791
|
+
* AVL tree that needs to be balanced.
|
|
10792
|
+
*/
|
|
10793
|
+
_balancePath(node) {
|
|
10794
|
+
node = this.ensureNode(node);
|
|
10795
|
+
const path = this.getPathToRoot(node, false);
|
|
10796
|
+
for (let i = 0; i < path.length; i++) {
|
|
10797
|
+
const A = path[i];
|
|
10798
|
+
this._updateHeight(A);
|
|
10799
|
+
switch (this._balanceFactor(A)) {
|
|
10800
|
+
case -2:
|
|
10801
|
+
if (A && A.left) {
|
|
10802
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
10803
|
+
this._balanceLL(A);
|
|
10804
|
+
} else {
|
|
10805
|
+
this._balanceLR(A);
|
|
10806
|
+
}
|
|
10807
|
+
}
|
|
10808
|
+
break;
|
|
10809
|
+
case 2:
|
|
10810
|
+
if (A && A.right) {
|
|
10811
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
10812
|
+
this._balanceRR(A);
|
|
10813
|
+
} else {
|
|
10814
|
+
this._balanceRL(A);
|
|
10815
|
+
}
|
|
10816
|
+
}
|
|
10817
|
+
}
|
|
10818
|
+
}
|
|
10819
|
+
}
|
|
10186
10820
|
_replaceNode(oldNode, newNode) {
|
|
10187
10821
|
newNode.height = oldNode.height;
|
|
10188
10822
|
return super._replaceNode(oldNode, newNode);
|
|
@@ -10201,7 +10835,7 @@ var dataStructureTyped = (() => {
|
|
|
10201
10835
|
/**
|
|
10202
10836
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
10203
10837
|
* initializes the tree with optional nodes and options.
|
|
10204
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
10838
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
10205
10839
|
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
10206
10840
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
10207
10841
|
* nodes to the
|
|
@@ -10252,14 +10886,14 @@ var dataStructureTyped = (() => {
|
|
|
10252
10886
|
}, options));
|
|
10253
10887
|
}
|
|
10254
10888
|
/**
|
|
10255
|
-
* The function `
|
|
10256
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
10889
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
10890
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
10257
10891
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
10258
|
-
* `
|
|
10892
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
10259
10893
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
10260
|
-
* @returns a node of type
|
|
10894
|
+
* @returns a node of type NODE or undefined.
|
|
10261
10895
|
*/
|
|
10262
|
-
|
|
10896
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
10263
10897
|
let node;
|
|
10264
10898
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
10265
10899
|
return;
|
|
@@ -10281,17 +10915,13 @@ var dataStructureTyped = (() => {
|
|
|
10281
10915
|
}
|
|
10282
10916
|
/**
|
|
10283
10917
|
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
10284
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
10918
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
10285
10919
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
10286
10920
|
* class.
|
|
10287
10921
|
*/
|
|
10288
10922
|
isNode(keyOrNodeOrEntry) {
|
|
10289
10923
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
10290
10924
|
}
|
|
10291
|
-
/**
|
|
10292
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10293
|
-
* Space Complexity: O(1)
|
|
10294
|
-
*/
|
|
10295
10925
|
isRealNode(node) {
|
|
10296
10926
|
if (node === this.Sentinel || node === void 0)
|
|
10297
10927
|
return false;
|
|
@@ -10300,7 +10930,7 @@ var dataStructureTyped = (() => {
|
|
|
10300
10930
|
/**
|
|
10301
10931
|
* Time Complexity: O(log n)
|
|
10302
10932
|
* Space Complexity: O(1)
|
|
10303
|
-
*
|
|
10933
|
+
* On average (where n is the number of nodes in the tree)
|
|
10304
10934
|
*/
|
|
10305
10935
|
/**
|
|
10306
10936
|
* Time Complexity: O(log n)
|
|
@@ -10312,10 +10942,10 @@ var dataStructureTyped = (() => {
|
|
|
10312
10942
|
* entry.
|
|
10313
10943
|
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
10314
10944
|
* being added to the binary search tree.
|
|
10315
|
-
* @returns The method `add` returns either the newly added node (`
|
|
10945
|
+
* @returns The method `add` returns either the newly added node (`NODE`) or `undefined`.
|
|
10316
10946
|
*/
|
|
10317
10947
|
add(keyOrNodeOrEntry, value) {
|
|
10318
|
-
const newNode = this.
|
|
10948
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
10319
10949
|
if (newNode === void 0)
|
|
10320
10950
|
return false;
|
|
10321
10951
|
newNode.left = this.Sentinel;
|
|
@@ -10361,7 +10991,6 @@ var dataStructureTyped = (() => {
|
|
|
10361
10991
|
/**
|
|
10362
10992
|
* Time Complexity: O(log n)
|
|
10363
10993
|
* Space Complexity: O(1)
|
|
10364
|
-
* on average (where n is the number of nodes in the tree)
|
|
10365
10994
|
*/
|
|
10366
10995
|
/**
|
|
10367
10996
|
* Time Complexity: O(log n)
|
|
@@ -10373,10 +11002,10 @@ var dataStructureTyped = (() => {
|
|
|
10373
11002
|
* that you want to use to identify the node that you want to delete from the binary tree. It can be
|
|
10374
11003
|
* of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
10375
11004
|
* you don't want to
|
|
10376
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
11005
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` and
|
|
10377
11006
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
10378
11007
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
10379
|
-
* @returns an array of `BinaryTreeDeleteResult<
|
|
11008
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
10380
11009
|
*/
|
|
10381
11010
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
10382
11011
|
const ans = [];
|
|
@@ -10448,14 +11077,14 @@ var dataStructureTyped = (() => {
|
|
|
10448
11077
|
* node that matches the other criteria
|
|
10449
11078
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
10450
11079
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
10451
|
-
* function should take a single parameter of type `
|
|
10452
|
-
* @param {K |
|
|
11080
|
+
* function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
|
|
11081
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
10453
11082
|
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
10454
11083
|
* provided, the search will start from the root of the binary tree.
|
|
10455
11084
|
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
10456
11085
|
* iteration to be performed when searching for nodes in the binary tree. It is used in the
|
|
10457
11086
|
* `getNodes` method, which is called within the `getNode` method.
|
|
10458
|
-
* @returns a value of type `
|
|
11087
|
+
* @returns a value of type `NODE`, `null`, or `undefined`.
|
|
10459
11088
|
*/
|
|
10460
11089
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
10461
11090
|
var _a;
|
|
@@ -10464,6 +11093,14 @@ var dataStructureTyped = (() => {
|
|
|
10464
11093
|
beginRoot = this.ensureNode(beginRoot);
|
|
10465
11094
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
|
|
10466
11095
|
}
|
|
11096
|
+
/**
|
|
11097
|
+
* Time Complexity: O(1)
|
|
11098
|
+
* Space Complexity: O(1)
|
|
11099
|
+
*/
|
|
11100
|
+
clear() {
|
|
11101
|
+
this._root = this.Sentinel;
|
|
11102
|
+
this._size = 0;
|
|
11103
|
+
}
|
|
10467
11104
|
/**
|
|
10468
11105
|
* Time Complexity: O(log n)
|
|
10469
11106
|
* Space Complexity: O(1)
|
|
@@ -10488,14 +11125,6 @@ var dataStructureTyped = (() => {
|
|
|
10488
11125
|
}
|
|
10489
11126
|
return y;
|
|
10490
11127
|
}
|
|
10491
|
-
/**
|
|
10492
|
-
* Time Complexity: O(1)
|
|
10493
|
-
* Space Complexity: O(1)
|
|
10494
|
-
*/
|
|
10495
|
-
clear() {
|
|
10496
|
-
this._root = this.Sentinel;
|
|
10497
|
-
this._size = 0;
|
|
10498
|
-
}
|
|
10499
11128
|
_setRoot(v) {
|
|
10500
11129
|
if (v) {
|
|
10501
11130
|
v.parent = void 0;
|
|
@@ -10511,7 +11140,7 @@ var dataStructureTyped = (() => {
|
|
|
10511
11140
|
* Space Complexity: O(1)
|
|
10512
11141
|
*
|
|
10513
11142
|
* The function performs a left rotation on a binary tree node.
|
|
10514
|
-
* @param {RedBlackTreeNode} x - The parameter `x` is of type `
|
|
11143
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `NODE`, which likely represents a node in a binary tree.
|
|
10515
11144
|
*/
|
|
10516
11145
|
_leftRotate(x) {
|
|
10517
11146
|
if (x.right) {
|
|
@@ -10553,17 +11182,71 @@ var dataStructureTyped = (() => {
|
|
|
10553
11182
|
if (y.right)
|
|
10554
11183
|
y.right.parent = x;
|
|
10555
11184
|
}
|
|
10556
|
-
y.parent = x.parent;
|
|
10557
|
-
if (x.parent === void 0) {
|
|
10558
|
-
this._setRoot(y);
|
|
10559
|
-
} else if (x === x.parent.right) {
|
|
10560
|
-
x.parent.right = y;
|
|
10561
|
-
} else {
|
|
10562
|
-
x.parent.left = y;
|
|
11185
|
+
y.parent = x.parent;
|
|
11186
|
+
if (x.parent === void 0) {
|
|
11187
|
+
this._setRoot(y);
|
|
11188
|
+
} else if (x === x.parent.right) {
|
|
11189
|
+
x.parent.right = y;
|
|
11190
|
+
} else {
|
|
11191
|
+
x.parent.left = y;
|
|
11192
|
+
}
|
|
11193
|
+
y.right = x;
|
|
11194
|
+
x.parent = y;
|
|
11195
|
+
}
|
|
11196
|
+
}
|
|
11197
|
+
/**
|
|
11198
|
+
* Time Complexity: O(log n)
|
|
11199
|
+
* Space Complexity: O(1)
|
|
11200
|
+
*/
|
|
11201
|
+
/**
|
|
11202
|
+
* Time Complexity: O(log n)
|
|
11203
|
+
* Space Complexity: O(1)
|
|
11204
|
+
*
|
|
11205
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
11206
|
+
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
11207
|
+
* red-black tree.
|
|
11208
|
+
*/
|
|
11209
|
+
_fixInsert(k) {
|
|
11210
|
+
let u;
|
|
11211
|
+
while (k.parent && k.parent.color === 1) {
|
|
11212
|
+
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
11213
|
+
u = k.parent.parent.left;
|
|
11214
|
+
if (u && u.color === 1) {
|
|
11215
|
+
u.color = 0 /* BLACK */;
|
|
11216
|
+
k.parent.color = 0 /* BLACK */;
|
|
11217
|
+
k.parent.parent.color = 1 /* RED */;
|
|
11218
|
+
k = k.parent.parent;
|
|
11219
|
+
} else {
|
|
11220
|
+
if (k === k.parent.left) {
|
|
11221
|
+
k = k.parent;
|
|
11222
|
+
this._rightRotate(k);
|
|
11223
|
+
}
|
|
11224
|
+
k.parent.color = 0 /* BLACK */;
|
|
11225
|
+
k.parent.parent.color = 1 /* RED */;
|
|
11226
|
+
this._leftRotate(k.parent.parent);
|
|
11227
|
+
}
|
|
11228
|
+
} else {
|
|
11229
|
+
u = k.parent.parent.right;
|
|
11230
|
+
if (u && u.color === 1) {
|
|
11231
|
+
u.color = 0 /* BLACK */;
|
|
11232
|
+
k.parent.color = 0 /* BLACK */;
|
|
11233
|
+
k.parent.parent.color = 1 /* RED */;
|
|
11234
|
+
k = k.parent.parent;
|
|
11235
|
+
} else {
|
|
11236
|
+
if (k === k.parent.right) {
|
|
11237
|
+
k = k.parent;
|
|
11238
|
+
this._leftRotate(k);
|
|
11239
|
+
}
|
|
11240
|
+
k.parent.color = 0 /* BLACK */;
|
|
11241
|
+
k.parent.parent.color = 1 /* RED */;
|
|
11242
|
+
this._rightRotate(k.parent.parent);
|
|
11243
|
+
}
|
|
11244
|
+
}
|
|
11245
|
+
if (k === this.root) {
|
|
11246
|
+
break;
|
|
10563
11247
|
}
|
|
10564
|
-
y.right = x;
|
|
10565
|
-
x.parent = y;
|
|
10566
11248
|
}
|
|
11249
|
+
this.root.color = 0 /* BLACK */;
|
|
10567
11250
|
}
|
|
10568
11251
|
/**
|
|
10569
11252
|
* Time Complexity: O(log n)
|
|
@@ -10659,65 +11342,11 @@ var dataStructureTyped = (() => {
|
|
|
10659
11342
|
}
|
|
10660
11343
|
v.parent = u.parent;
|
|
10661
11344
|
}
|
|
10662
|
-
/**
|
|
10663
|
-
* Time Complexity: O(log n)
|
|
10664
|
-
* Space Complexity: O(1)
|
|
10665
|
-
*/
|
|
10666
|
-
/**
|
|
10667
|
-
* Time Complexity: O(log n)
|
|
10668
|
-
* Space Complexity: O(1)
|
|
10669
|
-
*
|
|
10670
|
-
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
10671
|
-
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
10672
|
-
* red-black tree.
|
|
10673
|
-
*/
|
|
10674
|
-
_fixInsert(k) {
|
|
10675
|
-
let u;
|
|
10676
|
-
while (k.parent && k.parent.color === 1) {
|
|
10677
|
-
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
10678
|
-
u = k.parent.parent.left;
|
|
10679
|
-
if (u && u.color === 1) {
|
|
10680
|
-
u.color = 0 /* BLACK */;
|
|
10681
|
-
k.parent.color = 0 /* BLACK */;
|
|
10682
|
-
k.parent.parent.color = 1 /* RED */;
|
|
10683
|
-
k = k.parent.parent;
|
|
10684
|
-
} else {
|
|
10685
|
-
if (k === k.parent.left) {
|
|
10686
|
-
k = k.parent;
|
|
10687
|
-
this._rightRotate(k);
|
|
10688
|
-
}
|
|
10689
|
-
k.parent.color = 0 /* BLACK */;
|
|
10690
|
-
k.parent.parent.color = 1 /* RED */;
|
|
10691
|
-
this._leftRotate(k.parent.parent);
|
|
10692
|
-
}
|
|
10693
|
-
} else {
|
|
10694
|
-
u = k.parent.parent.right;
|
|
10695
|
-
if (u && u.color === 1) {
|
|
10696
|
-
u.color = 0 /* BLACK */;
|
|
10697
|
-
k.parent.color = 0 /* BLACK */;
|
|
10698
|
-
k.parent.parent.color = 1 /* RED */;
|
|
10699
|
-
k = k.parent.parent;
|
|
10700
|
-
} else {
|
|
10701
|
-
if (k === k.parent.right) {
|
|
10702
|
-
k = k.parent;
|
|
10703
|
-
this._leftRotate(k);
|
|
10704
|
-
}
|
|
10705
|
-
k.parent.color = 0 /* BLACK */;
|
|
10706
|
-
k.parent.parent.color = 1 /* RED */;
|
|
10707
|
-
this._rightRotate(k.parent.parent);
|
|
10708
|
-
}
|
|
10709
|
-
}
|
|
10710
|
-
if (k === this.root) {
|
|
10711
|
-
break;
|
|
10712
|
-
}
|
|
10713
|
-
}
|
|
10714
|
-
this.root.color = 0 /* BLACK */;
|
|
10715
|
-
}
|
|
10716
11345
|
/**
|
|
10717
11346
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
10718
|
-
* @param {
|
|
10719
|
-
* data structure. It is of type `
|
|
10720
|
-
* @param {
|
|
11347
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
11348
|
+
* data structure. It is of type `NODE`, which is the type of the nodes in the data structure.
|
|
11349
|
+
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
10721
11350
|
* data structure.
|
|
10722
11351
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
10723
11352
|
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
@@ -10763,7 +11392,7 @@ var dataStructureTyped = (() => {
|
|
|
10763
11392
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
10764
11393
|
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
10765
11394
|
* distinguish one node from another in the tree.
|
|
10766
|
-
* @param {
|
|
11395
|
+
* @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
|
10767
11396
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
10768
11397
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
10769
11398
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
@@ -10778,17 +11407,17 @@ var dataStructureTyped = (() => {
|
|
|
10778
11407
|
}, options));
|
|
10779
11408
|
}
|
|
10780
11409
|
/**
|
|
10781
|
-
* The function `
|
|
10782
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
11410
|
+
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
11411
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
|
|
10783
11412
|
* can be one of the following:
|
|
10784
11413
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
10785
11414
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
10786
11415
|
* it defaults to `undefined`.
|
|
10787
11416
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
10788
11417
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
10789
|
-
* @returns a node of type `
|
|
11418
|
+
* @returns a node of type `NODE` or `undefined`.
|
|
10790
11419
|
*/
|
|
10791
|
-
|
|
11420
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
10792
11421
|
let node;
|
|
10793
11422
|
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
10794
11423
|
return;
|
|
@@ -10810,7 +11439,7 @@ var dataStructureTyped = (() => {
|
|
|
10810
11439
|
}
|
|
10811
11440
|
/**
|
|
10812
11441
|
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
10813
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
11442
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
10814
11443
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
10815
11444
|
* class.
|
|
10816
11445
|
*/
|
|
@@ -10820,7 +11449,6 @@ var dataStructureTyped = (() => {
|
|
|
10820
11449
|
/**
|
|
10821
11450
|
* Time Complexity: O(log n)
|
|
10822
11451
|
* Space Complexity: O(1)
|
|
10823
|
-
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10824
11452
|
*/
|
|
10825
11453
|
/**
|
|
10826
11454
|
* Time Complexity: O(log n)
|
|
@@ -10839,7 +11467,7 @@ var dataStructureTyped = (() => {
|
|
|
10839
11467
|
* was not successful.
|
|
10840
11468
|
*/
|
|
10841
11469
|
add(keyOrNodeOrEntry, value, count = 1) {
|
|
10842
|
-
const newNode = this.
|
|
11470
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
|
|
10843
11471
|
if (newNode === void 0)
|
|
10844
11472
|
return false;
|
|
10845
11473
|
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
@@ -10850,81 +11478,11 @@ var dataStructureTyped = (() => {
|
|
|
10850
11478
|
return true;
|
|
10851
11479
|
}
|
|
10852
11480
|
/**
|
|
10853
|
-
* Time Complexity: O(
|
|
10854
|
-
* Space Complexity: O(1)
|
|
10855
|
-
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10856
|
-
*/
|
|
10857
|
-
/**
|
|
10858
|
-
* Time Complexity: O(k log n)
|
|
10859
|
-
* Space Complexity: O(1)
|
|
10860
|
-
*
|
|
10861
|
-
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
10862
|
-
* structure.
|
|
10863
|
-
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
10864
|
-
* either keys, nodes, or entries.
|
|
10865
|
-
* @returns The method is returning an array of type `N | undefined`.
|
|
10866
|
-
*/
|
|
10867
|
-
addMany(keysOrNodesOrEntries) {
|
|
10868
|
-
return super.addMany(keysOrNodesOrEntries);
|
|
10869
|
-
}
|
|
10870
|
-
/**
|
|
10871
|
-
* Time Complexity: O(n log n)
|
|
10872
|
-
* Space Complexity: O(n)
|
|
10873
|
-
* 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. linear space, as it creates an array to store the sorted nodes.
|
|
10874
|
-
*/
|
|
10875
|
-
/**
|
|
10876
|
-
* Time Complexity: O(n log n)
|
|
10877
|
-
* Space Complexity: O(n)
|
|
10878
|
-
*
|
|
10879
|
-
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
10880
|
-
* tree using either a recursive or iterative approach.
|
|
10881
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
10882
|
-
* type of iteration to use when building the balanced binary search tree. It can have two possible
|
|
10883
|
-
* values:
|
|
10884
|
-
* @returns a boolean value.
|
|
10885
|
-
*/
|
|
10886
|
-
perfectlyBalance(iterationType = this.iterationType) {
|
|
10887
|
-
const sorted = this.dfs((node) => node, "in"), n = sorted.length;
|
|
10888
|
-
if (sorted.length < 1)
|
|
10889
|
-
return false;
|
|
10890
|
-
this.clear();
|
|
10891
|
-
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
10892
|
-
const buildBalanceBST = (l, r) => {
|
|
10893
|
-
if (l > r)
|
|
10894
|
-
return;
|
|
10895
|
-
const m = l + Math.floor((r - l) / 2);
|
|
10896
|
-
const midNode = sorted[m];
|
|
10897
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
10898
|
-
buildBalanceBST(l, m - 1);
|
|
10899
|
-
buildBalanceBST(m + 1, r);
|
|
10900
|
-
};
|
|
10901
|
-
buildBalanceBST(0, n - 1);
|
|
10902
|
-
return true;
|
|
10903
|
-
} else {
|
|
10904
|
-
const stack = [[0, n - 1]];
|
|
10905
|
-
while (stack.length > 0) {
|
|
10906
|
-
const popped = stack.pop();
|
|
10907
|
-
if (popped) {
|
|
10908
|
-
const [l, r] = popped;
|
|
10909
|
-
if (l <= r) {
|
|
10910
|
-
const m = l + Math.floor((r - l) / 2);
|
|
10911
|
-
const midNode = sorted[m];
|
|
10912
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
10913
|
-
stack.push([m + 1, r]);
|
|
10914
|
-
stack.push([l, m - 1]);
|
|
10915
|
-
}
|
|
10916
|
-
}
|
|
10917
|
-
}
|
|
10918
|
-
return true;
|
|
10919
|
-
}
|
|
10920
|
-
}
|
|
10921
|
-
/**
|
|
10922
|
-
* Time Complexity: O(k log n)
|
|
11481
|
+
* Time Complexity: O(log n)
|
|
10923
11482
|
* Space Complexity: O(1)
|
|
10924
|
-
* 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. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10925
11483
|
*/
|
|
10926
11484
|
/**
|
|
10927
|
-
* Time Complexity: O(
|
|
11485
|
+
* Time Complexity: O(log n)
|
|
10928
11486
|
* Space Complexity: O(1)
|
|
10929
11487
|
*
|
|
10930
11488
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
@@ -10940,7 +11498,7 @@ var dataStructureTyped = (() => {
|
|
|
10940
11498
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
10941
11499
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
10942
11500
|
* decremented by 1 and
|
|
10943
|
-
* @returns an array of `BinaryTreeDeleteResult<
|
|
11501
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
10944
11502
|
*/
|
|
10945
11503
|
delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
|
|
10946
11504
|
var _a;
|
|
@@ -11008,6 +11566,56 @@ var dataStructureTyped = (() => {
|
|
|
11008
11566
|
super.clear();
|
|
11009
11567
|
this._count = 0;
|
|
11010
11568
|
}
|
|
11569
|
+
/**
|
|
11570
|
+
* Time Complexity: O(n log n)
|
|
11571
|
+
* Space Complexity: O(log n)
|
|
11572
|
+
*/
|
|
11573
|
+
/**
|
|
11574
|
+
* Time Complexity: O(n log n)
|
|
11575
|
+
* Space Complexity: O(log n)
|
|
11576
|
+
*
|
|
11577
|
+
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
11578
|
+
* tree using either a recursive or iterative approach.
|
|
11579
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
11580
|
+
* type of iteration to use when building the balanced binary search tree. It can have two possible
|
|
11581
|
+
* values:
|
|
11582
|
+
* @returns a boolean value.
|
|
11583
|
+
*/
|
|
11584
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
11585
|
+
const sorted = this.dfs((node) => node, "in"), n = sorted.length;
|
|
11586
|
+
if (sorted.length < 1)
|
|
11587
|
+
return false;
|
|
11588
|
+
this.clear();
|
|
11589
|
+
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
11590
|
+
const buildBalanceBST = (l, r) => {
|
|
11591
|
+
if (l > r)
|
|
11592
|
+
return;
|
|
11593
|
+
const m = l + Math.floor((r - l) / 2);
|
|
11594
|
+
const midNode = sorted[m];
|
|
11595
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
11596
|
+
buildBalanceBST(l, m - 1);
|
|
11597
|
+
buildBalanceBST(m + 1, r);
|
|
11598
|
+
};
|
|
11599
|
+
buildBalanceBST(0, n - 1);
|
|
11600
|
+
return true;
|
|
11601
|
+
} else {
|
|
11602
|
+
const stack = [[0, n - 1]];
|
|
11603
|
+
while (stack.length > 0) {
|
|
11604
|
+
const popped = stack.pop();
|
|
11605
|
+
if (popped) {
|
|
11606
|
+
const [l, r] = popped;
|
|
11607
|
+
if (l <= r) {
|
|
11608
|
+
const m = l + Math.floor((r - l) / 2);
|
|
11609
|
+
const midNode = sorted[m];
|
|
11610
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
11611
|
+
stack.push([m + 1, r]);
|
|
11612
|
+
stack.push([l, m - 1]);
|
|
11613
|
+
}
|
|
11614
|
+
}
|
|
11615
|
+
}
|
|
11616
|
+
return true;
|
|
11617
|
+
}
|
|
11618
|
+
}
|
|
11011
11619
|
/**
|
|
11012
11620
|
* Time complexity: O(n)
|
|
11013
11621
|
* Space complexity: O(n)
|
|
@@ -11026,9 +11634,9 @@ var dataStructureTyped = (() => {
|
|
|
11026
11634
|
}
|
|
11027
11635
|
/**
|
|
11028
11636
|
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
11029
|
-
* @param {K |
|
|
11030
|
-
* which the values will be swapped. It can be of type `K`, `
|
|
11031
|
-
* @param {K |
|
|
11637
|
+
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
11638
|
+
* which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
|
|
11639
|
+
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
11032
11640
|
* node where the values from the source node will be swapped to.
|
|
11033
11641
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
11034
11642
|
* if either `srcNode` or `destNode` is undefined.
|
|
@@ -11180,21 +11788,45 @@ var dataStructureTyped = (() => {
|
|
|
11180
11788
|
}
|
|
11181
11789
|
}
|
|
11182
11790
|
}
|
|
11791
|
+
/**
|
|
11792
|
+
* The function returns the number of rows.
|
|
11793
|
+
* @returns The number of rows.
|
|
11794
|
+
*/
|
|
11183
11795
|
get rows() {
|
|
11184
11796
|
return this._rows;
|
|
11185
11797
|
}
|
|
11798
|
+
/**
|
|
11799
|
+
* The function returns the value of the private variable _cols.
|
|
11800
|
+
* @returns The number of columns.
|
|
11801
|
+
*/
|
|
11186
11802
|
get cols() {
|
|
11187
11803
|
return this._cols;
|
|
11188
11804
|
}
|
|
11805
|
+
/**
|
|
11806
|
+
* The function returns a two-dimensional array of numbers.
|
|
11807
|
+
* @returns The data property, which is a two-dimensional array of numbers.
|
|
11808
|
+
*/
|
|
11189
11809
|
get data() {
|
|
11190
11810
|
return this._data;
|
|
11191
11811
|
}
|
|
11812
|
+
/**
|
|
11813
|
+
* The above function returns the value of the _addFn property.
|
|
11814
|
+
* @returns The value of the property `_addFn` is being returned.
|
|
11815
|
+
*/
|
|
11192
11816
|
get addFn() {
|
|
11193
11817
|
return this._addFn;
|
|
11194
11818
|
}
|
|
11819
|
+
/**
|
|
11820
|
+
* The function returns the value of the _subtractFn property.
|
|
11821
|
+
* @returns The `_subtractFn` property is being returned.
|
|
11822
|
+
*/
|
|
11195
11823
|
get subtractFn() {
|
|
11196
11824
|
return this._subtractFn;
|
|
11197
11825
|
}
|
|
11826
|
+
/**
|
|
11827
|
+
* The function returns the value of the _multiplyFn property.
|
|
11828
|
+
* @returns The `_multiplyFn` property is being returned.
|
|
11829
|
+
*/
|
|
11198
11830
|
get multiplyFn() {
|
|
11199
11831
|
return this._multiplyFn;
|
|
11200
11832
|
}
|
|
@@ -11460,17 +12092,6 @@ var dataStructureTyped = (() => {
|
|
|
11460
12092
|
multiplyFn: this.multiplyFn
|
|
11461
12093
|
});
|
|
11462
12094
|
}
|
|
11463
|
-
_addFn(a, b) {
|
|
11464
|
-
if (a === void 0)
|
|
11465
|
-
return b;
|
|
11466
|
-
return a + b;
|
|
11467
|
-
}
|
|
11468
|
-
_subtractFn(a, b) {
|
|
11469
|
-
return a - b;
|
|
11470
|
-
}
|
|
11471
|
-
_multiplyFn(a, b) {
|
|
11472
|
-
return a * b;
|
|
11473
|
-
}
|
|
11474
12095
|
/**
|
|
11475
12096
|
* The function checks if a given row and column index is valid within a specified range.
|
|
11476
12097
|
* @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
|
|
@@ -11482,6 +12103,32 @@ var dataStructureTyped = (() => {
|
|
|
11482
12103
|
isValidIndex(row, col) {
|
|
11483
12104
|
return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
|
|
11484
12105
|
}
|
|
12106
|
+
/**
|
|
12107
|
+
* The `clone` function returns a new instance of the Matrix class with the same data and properties
|
|
12108
|
+
* as the original instance.
|
|
12109
|
+
* @returns The `clone()` method is returning a new instance of the `Matrix` class with the same data
|
|
12110
|
+
* and properties as the current instance.
|
|
12111
|
+
*/
|
|
12112
|
+
clone() {
|
|
12113
|
+
return new _Matrix(this.data, {
|
|
12114
|
+
rows: this.rows,
|
|
12115
|
+
cols: this.cols,
|
|
12116
|
+
addFn: this.addFn,
|
|
12117
|
+
subtractFn: this.subtractFn,
|
|
12118
|
+
multiplyFn: this.multiplyFn
|
|
12119
|
+
});
|
|
12120
|
+
}
|
|
12121
|
+
_addFn(a, b) {
|
|
12122
|
+
if (a === void 0)
|
|
12123
|
+
return b;
|
|
12124
|
+
return a + b;
|
|
12125
|
+
}
|
|
12126
|
+
_subtractFn(a, b) {
|
|
12127
|
+
return a - b;
|
|
12128
|
+
}
|
|
12129
|
+
_multiplyFn(a, b) {
|
|
12130
|
+
return a * b;
|
|
12131
|
+
}
|
|
11485
12132
|
/**
|
|
11486
12133
|
* The function `_swapRows` swaps the positions of two rows in an array.
|
|
11487
12134
|
* @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
|
|
@@ -11653,6 +12300,12 @@ var dataStructureTyped = (() => {
|
|
|
11653
12300
|
}
|
|
11654
12301
|
};
|
|
11655
12302
|
var Trie = class _Trie extends IterableElementBase {
|
|
12303
|
+
/**
|
|
12304
|
+
* The constructor function for the Trie class.
|
|
12305
|
+
* @param words: Iterable string Initialize the trie with a set of words
|
|
12306
|
+
* @param options?: TrieOptions Allow the user to pass in options for the trie
|
|
12307
|
+
* @return This
|
|
12308
|
+
*/
|
|
11656
12309
|
constructor(words = [], options) {
|
|
11657
12310
|
super();
|
|
11658
12311
|
__publicField(this, "_size", 0);
|
|
@@ -11668,12 +12321,25 @@ var dataStructureTyped = (() => {
|
|
|
11668
12321
|
this.add(word);
|
|
11669
12322
|
}
|
|
11670
12323
|
}
|
|
12324
|
+
/**
|
|
12325
|
+
* The size function returns the size of the stack.
|
|
12326
|
+
* @return The number of elements in the list
|
|
12327
|
+
*/
|
|
11671
12328
|
get size() {
|
|
11672
12329
|
return this._size;
|
|
11673
12330
|
}
|
|
12331
|
+
/**
|
|
12332
|
+
* The caseSensitive function is a getter that returns the value of the private _caseSensitive property.
|
|
12333
|
+
*
|
|
12334
|
+
* @return The value of the _casesensitive private variable
|
|
12335
|
+
*/
|
|
11674
12336
|
get caseSensitive() {
|
|
11675
12337
|
return this._caseSensitive;
|
|
11676
12338
|
}
|
|
12339
|
+
/**
|
|
12340
|
+
* The root function returns the root node of the tree.
|
|
12341
|
+
* @return The root node
|
|
12342
|
+
*/
|
|
11677
12343
|
get root() {
|
|
11678
12344
|
return this._root;
|
|
11679
12345
|
}
|
|
@@ -11731,6 +12397,13 @@ var dataStructureTyped = (() => {
|
|
|
11731
12397
|
}
|
|
11732
12398
|
return cur.isEnd;
|
|
11733
12399
|
}
|
|
12400
|
+
/**
|
|
12401
|
+
* The isEmpty function checks if the size of the queue is 0.
|
|
12402
|
+
* @return True if the size of the queue is 0
|
|
12403
|
+
*/
|
|
12404
|
+
isEmpty() {
|
|
12405
|
+
return this.size === 0;
|
|
12406
|
+
}
|
|
11734
12407
|
/**
|
|
11735
12408
|
* Time Complexity: O(M), where M is the length of the word being deleted.
|
|
11736
12409
|
* Space Complexity: O(M) - Due to the recursive DFS approach.
|
|
@@ -11950,6 +12623,21 @@ var dataStructureTyped = (() => {
|
|
|
11950
12623
|
dfs(startNode, prefix);
|
|
11951
12624
|
return words;
|
|
11952
12625
|
}
|
|
12626
|
+
/**
|
|
12627
|
+
* Time Complexity: O(n)
|
|
12628
|
+
* Space Complexity: O(n)
|
|
12629
|
+
*/
|
|
12630
|
+
/**
|
|
12631
|
+
* Time Complexity: O(n)
|
|
12632
|
+
* Space Complexity: O(n)
|
|
12633
|
+
*
|
|
12634
|
+
* The `clone` function returns a new instance of the Trie class with the same values and case
|
|
12635
|
+
* sensitivity as the original Trie.
|
|
12636
|
+
* @returns A new instance of the Trie class is being returned.
|
|
12637
|
+
*/
|
|
12638
|
+
clone() {
|
|
12639
|
+
return new _Trie(this.values(), { caseSensitive: this.caseSensitive });
|
|
12640
|
+
}
|
|
11953
12641
|
/**
|
|
11954
12642
|
* Time Complexity: O(n)
|
|
11955
12643
|
* Space Complexity: O(n)
|