data-structure-typed 1.53.6 → 1.53.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +16 -25
- package/benchmark/report.html +32 -5
- package/benchmark/report.json +326 -23
- package/dist/cjs/common/index.d.ts +12 -0
- package/dist/cjs/common/index.js +29 -0
- package/dist/cjs/common/index.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +9 -12
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +55 -20
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +102 -68
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +131 -37
- package/dist/cjs/data-structures/binary-tree/bst.js +222 -69
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/index.js +1 -1
- package/dist/cjs/data-structures/binary-tree/index.js.map +1 -1
- package/dist/{mjs/data-structures/binary-tree/rb-tree.d.ts → cjs/data-structures/binary-tree/red-black-tree.d.ts} +56 -0
- package/dist/cjs/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +60 -4
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +7 -7
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +30 -0
- package/dist/cjs/data-structures/hash/hash-map.js +30 -0
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +26 -9
- package/dist/cjs/data-structures/heap/heap.js +37 -17
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +54 -9
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +80 -19
- 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 +35 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +55 -11
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +37 -8
- package/dist/cjs/data-structures/queue/deque.js +73 -29
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +41 -1
- package/dist/cjs/data-structures/queue/queue.js +51 -9
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +27 -10
- package/dist/cjs/data-structures/stack/stack.js +39 -20
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +111 -6
- package/dist/cjs/data-structures/trie/trie.js +123 -14
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/cjs/types/utils/utils.d.ts +10 -6
- package/dist/cjs/utils/utils.js +4 -2
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/common/index.d.ts +12 -0
- package/dist/mjs/common/index.js +29 -0
- package/dist/mjs/common/index.js.map +1 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +10 -12
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +3 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +55 -20
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +97 -63
- package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +131 -37
- package/dist/mjs/data-structures/binary-tree/bst.js +224 -69
- package/dist/mjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/index.js +1 -1
- package/dist/mjs/data-structures/binary-tree/index.js.map +1 -1
- package/dist/{cjs/data-structures/binary-tree/rb-tree.d.ts → mjs/data-structures/binary-tree/red-black-tree.d.ts} +56 -0
- package/dist/mjs/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +60 -4
- package/dist/mjs/data-structures/binary-tree/red-black-tree.js.map +1 -0
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +30 -0
- package/dist/mjs/data-structures/hash/hash-map.js +30 -0
- package/dist/mjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/mjs/data-structures/heap/heap.d.ts +26 -9
- package/dist/mjs/data-structures/heap/heap.js +37 -17
- package/dist/mjs/data-structures/heap/heap.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +54 -9
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +80 -19
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +35 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +55 -11
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +37 -8
- package/dist/mjs/data-structures/queue/deque.js +73 -29
- package/dist/mjs/data-structures/queue/deque.js.map +1 -1
- package/dist/mjs/data-structures/queue/queue.d.ts +41 -1
- package/dist/mjs/data-structures/queue/queue.js +51 -9
- package/dist/mjs/data-structures/queue/queue.js.map +1 -1
- package/dist/mjs/data-structures/stack/stack.d.ts +27 -10
- package/dist/mjs/data-structures/stack/stack.js +39 -20
- package/dist/mjs/data-structures/stack/stack.js.map +1 -1
- package/dist/mjs/data-structures/trie/trie.d.ts +111 -6
- package/dist/mjs/data-structures/trie/trie.js +123 -14
- package/dist/mjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/index.d.ts +1 -1
- package/dist/mjs/index.js +1 -1
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/mjs/types/utils/utils.d.ts +10 -6
- package/dist/mjs/utils/utils.js +4 -2
- package/dist/mjs/utils/utils.js.map +1 -1
- package/dist/umd/data-structure-typed.js +671 -246
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/common/index.ts +25 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -11
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +110 -66
- package/src/data-structures/binary-tree/bst.ts +232 -72
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +56 -3
- package/src/data-structures/binary-tree/tree-multi-map.ts +6 -6
- package/src/data-structures/hash/hash-map.ts +30 -0
- package/src/data-structures/heap/heap.ts +72 -49
- package/src/data-structures/linked-list/doubly-linked-list.ts +173 -105
- package/src/data-structures/linked-list/singly-linked-list.ts +61 -11
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +123 -13
- package/src/index.ts +1 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +3 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -1
- package/test/performance/reportor.ts +38 -33
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +47 -47
- package/test/unit/data-structures/binary-tree/bst.test.ts +114 -44
- package/test/unit/data-structures/binary-tree/overall.test.ts +14 -22
- package/test/unit/data-structures/binary-tree/{rb-tree.test.ts → red-black-tree.test.ts} +55 -1
- package/test/unit/data-structures/trie/trie.test.ts +151 -0
- package/test/unit/utils/utils.test.ts +6 -6
- package/dist/cjs/constants/index.d.ts +0 -4
- package/dist/cjs/constants/index.js +0 -9
- package/dist/cjs/constants/index.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +0 -1
- package/dist/mjs/constants/index.d.ts +0 -4
- package/dist/mjs/constants/index.js +0 -6
- package/dist/mjs/constants/index.js.map +0 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.js.map +0 -1
- package/src/constants/index.ts +0 -4
|
@@ -131,6 +131,7 @@ var dataStructureTyped = (() => {
|
|
|
131
131
|
Navigator: () => Navigator,
|
|
132
132
|
PriorityQueue: () => PriorityQueue,
|
|
133
133
|
Queue: () => Queue,
|
|
134
|
+
Range: () => Range,
|
|
134
135
|
RedBlackTree: () => RedBlackTree,
|
|
135
136
|
RedBlackTreeNode: () => RedBlackTreeNode,
|
|
136
137
|
SegmentTree: () => SegmentTree,
|
|
@@ -670,7 +671,7 @@ var dataStructureTyped = (() => {
|
|
|
670
671
|
};
|
|
671
672
|
function isPrimitiveComparable(value) {
|
|
672
673
|
const valueType = typeof value;
|
|
673
|
-
if (valueType === "number") return
|
|
674
|
+
if (valueType === "number") return true;
|
|
674
675
|
return valueType === "bigint" || valueType === "string" || valueType === "boolean";
|
|
675
676
|
}
|
|
676
677
|
function tryObjectToPrimitive(obj) {
|
|
@@ -691,7 +692,7 @@ var dataStructureTyped = (() => {
|
|
|
691
692
|
if (value === null || value === void 0) return false;
|
|
692
693
|
if (isPrimitiveComparable(value)) return true;
|
|
693
694
|
if (typeof value !== "object") return false;
|
|
694
|
-
if (value instanceof Date) return
|
|
695
|
+
if (value instanceof Date) return true;
|
|
695
696
|
if (isForceObjectComparable) return true;
|
|
696
697
|
const comparableValue = tryObjectToPrimitive(value);
|
|
697
698
|
if (comparableValue === null || comparableValue === void 0) return false;
|
|
@@ -770,6 +771,9 @@ var dataStructureTyped = (() => {
|
|
|
770
771
|
return this._hashFn;
|
|
771
772
|
}
|
|
772
773
|
/**
|
|
774
|
+
* Time Complexity: O(1)
|
|
775
|
+
* Space Complexity: O(1)
|
|
776
|
+
*
|
|
773
777
|
* The function checks if a given element is an array with exactly two elements.
|
|
774
778
|
* @param {any} rawElement - The `rawElement` parameter is of type `any`, which means it can be any
|
|
775
779
|
* data type.
|
|
@@ -779,6 +783,9 @@ var dataStructureTyped = (() => {
|
|
|
779
783
|
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
780
784
|
}
|
|
781
785
|
/**
|
|
786
|
+
* Time Complexity: O(1)
|
|
787
|
+
* Space Complexity: O(1)
|
|
788
|
+
*
|
|
782
789
|
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
783
790
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
784
791
|
*/
|
|
@@ -786,6 +793,9 @@ var dataStructureTyped = (() => {
|
|
|
786
793
|
return this._size === 0;
|
|
787
794
|
}
|
|
788
795
|
/**
|
|
796
|
+
* Time Complexity: O(1)
|
|
797
|
+
* Space Complexity: O(1)
|
|
798
|
+
*
|
|
789
799
|
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
|
790
800
|
* size.
|
|
791
801
|
*/
|
|
@@ -795,6 +805,9 @@ var dataStructureTyped = (() => {
|
|
|
795
805
|
this._size = 0;
|
|
796
806
|
}
|
|
797
807
|
/**
|
|
808
|
+
* Time Complexity: O(1)
|
|
809
|
+
* Space Complexity: O(1)
|
|
810
|
+
*
|
|
798
811
|
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
799
812
|
* the key is not already present.
|
|
800
813
|
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
@@ -819,6 +832,9 @@ var dataStructureTyped = (() => {
|
|
|
819
832
|
return true;
|
|
820
833
|
}
|
|
821
834
|
/**
|
|
835
|
+
* Time Complexity: O(k)
|
|
836
|
+
* Space Complexity: O(k)
|
|
837
|
+
*
|
|
822
838
|
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
823
839
|
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
824
840
|
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
@@ -842,6 +858,9 @@ var dataStructureTyped = (() => {
|
|
|
842
858
|
return results;
|
|
843
859
|
}
|
|
844
860
|
/**
|
|
861
|
+
* Time Complexity: O(1)
|
|
862
|
+
* Space Complexity: O(1)
|
|
863
|
+
*
|
|
845
864
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
846
865
|
* a string map.
|
|
847
866
|
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
@@ -859,6 +878,9 @@ var dataStructureTyped = (() => {
|
|
|
859
878
|
}
|
|
860
879
|
}
|
|
861
880
|
/**
|
|
881
|
+
* Time Complexity: O(1)
|
|
882
|
+
* Space Complexity: O(1)
|
|
883
|
+
*
|
|
862
884
|
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
863
885
|
* is an object key or not.
|
|
864
886
|
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
@@ -873,6 +895,9 @@ var dataStructureTyped = (() => {
|
|
|
873
895
|
}
|
|
874
896
|
}
|
|
875
897
|
/**
|
|
898
|
+
* Time Complexity: O(1)
|
|
899
|
+
* Space Complexity: O(1)
|
|
900
|
+
*
|
|
876
901
|
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
877
902
|
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
878
903
|
* data structure.
|
|
@@ -1194,6 +1219,9 @@ var dataStructureTyped = (() => {
|
|
|
1194
1219
|
return true;
|
|
1195
1220
|
}
|
|
1196
1221
|
/**
|
|
1222
|
+
* Time Complexity: O(k)
|
|
1223
|
+
* Space Complexity: O(k)
|
|
1224
|
+
*
|
|
1197
1225
|
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
1198
1226
|
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
1199
1227
|
* of booleans indicating the success of each set operation.
|
|
@@ -1218,6 +1246,9 @@ var dataStructureTyped = (() => {
|
|
|
1218
1246
|
return results;
|
|
1219
1247
|
}
|
|
1220
1248
|
/**
|
|
1249
|
+
* Time Complexity: O(1)
|
|
1250
|
+
* Space Complexity: O(1)
|
|
1251
|
+
*
|
|
1221
1252
|
* The function checks if a given key exists in a map, using different logic depending on whether the
|
|
1222
1253
|
* key is a weak key or not.
|
|
1223
1254
|
* @param {K} key - The `key` parameter is the key that is being checked for existence in the map.
|
|
@@ -1513,15 +1544,7 @@ var dataStructureTyped = (() => {
|
|
|
1513
1544
|
__publicField(this, "_head");
|
|
1514
1545
|
__publicField(this, "_tail");
|
|
1515
1546
|
__publicField(this, "_size", 0);
|
|
1516
|
-
|
|
1517
|
-
for (const el of elements) {
|
|
1518
|
-
if (this.toElementFn) {
|
|
1519
|
-
this.push(this.toElementFn(el));
|
|
1520
|
-
} else {
|
|
1521
|
-
this.push(el);
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
}
|
|
1547
|
+
this.pushMany(elements);
|
|
1525
1548
|
}
|
|
1526
1549
|
/**
|
|
1527
1550
|
* The `head` function returns the first node of a singly linked list.
|
|
@@ -1647,6 +1670,53 @@ var dataStructureTyped = (() => {
|
|
|
1647
1670
|
this._size++;
|
|
1648
1671
|
return true;
|
|
1649
1672
|
}
|
|
1673
|
+
/**
|
|
1674
|
+
* Time Complexity: O(k)
|
|
1675
|
+
* Space Complexity: O(k)
|
|
1676
|
+
*
|
|
1677
|
+
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a
|
|
1678
|
+
* transformation function if provided.
|
|
1679
|
+
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
|
|
1680
|
+
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
|
|
1681
|
+
* or `SinglyLinkedListNode<E>`.
|
|
1682
|
+
* @returns The `pushMany` function returns an array of boolean values indicating whether each
|
|
1683
|
+
* element was successfully pushed into the data structure.
|
|
1684
|
+
*/
|
|
1685
|
+
pushMany(elements) {
|
|
1686
|
+
const ans = [];
|
|
1687
|
+
for (const el of elements) {
|
|
1688
|
+
if (this.toElementFn) {
|
|
1689
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
1690
|
+
continue;
|
|
1691
|
+
}
|
|
1692
|
+
ans.push(this.push(el));
|
|
1693
|
+
}
|
|
1694
|
+
return ans;
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* Time Complexity: O(k)
|
|
1698
|
+
* Space Complexity: O(k)
|
|
1699
|
+
*
|
|
1700
|
+
* The function `unshiftMany` iterates over elements and adds them to a data structure, optionally
|
|
1701
|
+
* converting them using a provided function.
|
|
1702
|
+
* @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
|
|
1703
|
+
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
|
|
1704
|
+
* `R`, or `SinglyLinkedListNode<E>`. The function iterates over each element in the iterable and
|
|
1705
|
+
* performs an `unshift` operation on the linked list for each
|
|
1706
|
+
* @returns The `unshiftMany` function is returning an array of boolean values, where each value
|
|
1707
|
+
* represents the result of calling the `unshift` method on the current instance of the class.
|
|
1708
|
+
*/
|
|
1709
|
+
unshiftMany(elements) {
|
|
1710
|
+
const ans = [];
|
|
1711
|
+
for (const el of elements) {
|
|
1712
|
+
if (this.toElementFn) {
|
|
1713
|
+
ans.push(this.unshift(this.toElementFn(el)));
|
|
1714
|
+
continue;
|
|
1715
|
+
}
|
|
1716
|
+
ans.push(this.unshift(el));
|
|
1717
|
+
}
|
|
1718
|
+
return ans;
|
|
1719
|
+
}
|
|
1650
1720
|
/**
|
|
1651
1721
|
* Time Complexity: O(n)
|
|
1652
1722
|
* Space Complexity: O(1)
|
|
@@ -1659,7 +1729,7 @@ var dataStructureTyped = (() => {
|
|
|
1659
1729
|
* @returns The `get` method returns the value of the first node in the singly linked list that
|
|
1660
1730
|
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
1661
1731
|
*/
|
|
1662
|
-
|
|
1732
|
+
search(elementNodeOrPredicate) {
|
|
1663
1733
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1664
1734
|
let current = this.head;
|
|
1665
1735
|
while (current) {
|
|
@@ -1818,6 +1888,9 @@ var dataStructureTyped = (() => {
|
|
|
1818
1888
|
return true;
|
|
1819
1889
|
}
|
|
1820
1890
|
/**
|
|
1891
|
+
* Time Complexity: O(1)
|
|
1892
|
+
* Space Complexity: O(1)
|
|
1893
|
+
*
|
|
1821
1894
|
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
1822
1895
|
* whether it is empty or not.
|
|
1823
1896
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
@@ -1826,6 +1899,9 @@ var dataStructureTyped = (() => {
|
|
|
1826
1899
|
return this._size === 0;
|
|
1827
1900
|
}
|
|
1828
1901
|
/**
|
|
1902
|
+
* Time Complexity: O(1)
|
|
1903
|
+
* Space Complexity: O(1)
|
|
1904
|
+
*
|
|
1829
1905
|
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
1830
1906
|
*/
|
|
1831
1907
|
clear() {
|
|
@@ -2216,6 +2292,16 @@ var dataStructureTyped = (() => {
|
|
|
2216
2292
|
}
|
|
2217
2293
|
};
|
|
2218
2294
|
var DoublyLinkedList = class _DoublyLinkedList extends IterableElementBase {
|
|
2295
|
+
/**
|
|
2296
|
+
* This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
|
|
2297
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
|
|
2298
|
+
* iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
|
|
2299
|
+
* with the elements provided in the iterable. If no elements are provided, the default value is an
|
|
2300
|
+
* empty iterable.
|
|
2301
|
+
* @param [options] - The `options` parameter in the constructor is of type
|
|
2302
|
+
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
2303
|
+
* configuration options to customize the behavior of the DoublyLinkedList.
|
|
2304
|
+
*/
|
|
2219
2305
|
constructor(elements = [], options) {
|
|
2220
2306
|
super(options);
|
|
2221
2307
|
__publicField(this, "_head");
|
|
@@ -2224,13 +2310,7 @@ var dataStructureTyped = (() => {
|
|
|
2224
2310
|
this._head = void 0;
|
|
2225
2311
|
this._tail = void 0;
|
|
2226
2312
|
this._size = 0;
|
|
2227
|
-
|
|
2228
|
-
for (const el of elements) {
|
|
2229
|
-
if (this.toElementFn) {
|
|
2230
|
-
this.push(this.toElementFn(el));
|
|
2231
|
-
} else this.push(el);
|
|
2232
|
-
}
|
|
2233
|
-
}
|
|
2313
|
+
this.pushMany(elements);
|
|
2234
2314
|
}
|
|
2235
2315
|
/**
|
|
2236
2316
|
* The `head` function returns the first node of a doubly linked list.
|
|
@@ -2378,6 +2458,55 @@ var dataStructureTyped = (() => {
|
|
|
2378
2458
|
this._size++;
|
|
2379
2459
|
return true;
|
|
2380
2460
|
}
|
|
2461
|
+
/**
|
|
2462
|
+
* Time Complexity: O(k)
|
|
2463
|
+
* Space Complexity: O(k)
|
|
2464
|
+
*
|
|
2465
|
+
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a
|
|
2466
|
+
* transformation function if provided.
|
|
2467
|
+
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
|
|
2468
|
+
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
|
|
2469
|
+
* or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and pushes
|
|
2470
|
+
* it onto the linked list. If a transformation function `to
|
|
2471
|
+
* @returns The `pushMany` function is returning an array of boolean values (`ans`) which indicate
|
|
2472
|
+
* the success or failure of pushing each element into the data structure.
|
|
2473
|
+
*/
|
|
2474
|
+
pushMany(elements) {
|
|
2475
|
+
const ans = [];
|
|
2476
|
+
for (const el of elements) {
|
|
2477
|
+
if (this.toElementFn) {
|
|
2478
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
2479
|
+
continue;
|
|
2480
|
+
}
|
|
2481
|
+
ans.push(this.push(el));
|
|
2482
|
+
}
|
|
2483
|
+
return ans;
|
|
2484
|
+
}
|
|
2485
|
+
/**
|
|
2486
|
+
* Time Complexity: O(k)
|
|
2487
|
+
* Space Complexity: O(k)
|
|
2488
|
+
*
|
|
2489
|
+
* The function `unshiftMany` iterates through a collection of elements and adds them to the
|
|
2490
|
+
* beginning of a Doubly Linked List, returning an array of boolean values indicating the success of
|
|
2491
|
+
* each insertion.
|
|
2492
|
+
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
|
|
2493
|
+
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
|
|
2494
|
+
* `R`, or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and
|
|
2495
|
+
* performs an `unshift` operation on the doubly linked list
|
|
2496
|
+
* @returns The `unshiftMany` function returns an array of boolean values indicating the success of
|
|
2497
|
+
* each unshift operation performed on the elements passed as input.
|
|
2498
|
+
*/
|
|
2499
|
+
unshiftMany(elements) {
|
|
2500
|
+
const ans = [];
|
|
2501
|
+
for (const el of elements) {
|
|
2502
|
+
if (this.toElementFn) {
|
|
2503
|
+
ans.push(this.unshift(this.toElementFn(el)));
|
|
2504
|
+
continue;
|
|
2505
|
+
}
|
|
2506
|
+
ans.push(this.unshift(el));
|
|
2507
|
+
}
|
|
2508
|
+
return ans;
|
|
2509
|
+
}
|
|
2381
2510
|
/**
|
|
2382
2511
|
* Time Complexity: O(n)
|
|
2383
2512
|
* Space Complexity: O(1)
|
|
@@ -2495,7 +2624,7 @@ var dataStructureTyped = (() => {
|
|
|
2495
2624
|
* node was not found.
|
|
2496
2625
|
*/
|
|
2497
2626
|
addBefore(existingElementOrNode, newElementOrNode) {
|
|
2498
|
-
const existingNode = this.getNode(existingElementOrNode);
|
|
2627
|
+
const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
|
|
2499
2628
|
if (existingNode) {
|
|
2500
2629
|
const newNode = this._ensureNode(newElementOrNode);
|
|
2501
2630
|
newNode.prev = existingNode.prev;
|
|
@@ -2529,7 +2658,7 @@ var dataStructureTyped = (() => {
|
|
|
2529
2658
|
* was not found in the linked list.
|
|
2530
2659
|
*/
|
|
2531
2660
|
addAfter(existingElementOrNode, newElementOrNode) {
|
|
2532
|
-
const existingNode = this.getNode(existingElementOrNode);
|
|
2661
|
+
const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
|
|
2533
2662
|
if (existingNode) {
|
|
2534
2663
|
const newNode = this._ensureNode(newElementOrNode);
|
|
2535
2664
|
newNode.next = existingNode.next;
|
|
@@ -2662,7 +2791,7 @@ var dataStructureTyped = (() => {
|
|
|
2662
2791
|
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
2663
2792
|
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
2664
2793
|
*/
|
|
2665
|
-
|
|
2794
|
+
search(elementNodeOrPredicate) {
|
|
2666
2795
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
2667
2796
|
let current = this.head;
|
|
2668
2797
|
while (current) {
|
|
@@ -2814,6 +2943,12 @@ var dataStructureTyped = (() => {
|
|
|
2814
2943
|
* Time Complexity: O(n)
|
|
2815
2944
|
* Space Complexity: O(1)
|
|
2816
2945
|
*
|
|
2946
|
+
* The function `countOccurrences` iterates through a doubly linked list and counts the occurrences
|
|
2947
|
+
* of a specified element or nodes that satisfy a given predicate.
|
|
2948
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementOrNode
|
|
2949
|
+
* - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
|
|
2950
|
+
* @returns The `countOccurrences` method returns the number of occurrences of the specified element,
|
|
2951
|
+
* node, or predicate function in the doubly linked list.
|
|
2817
2952
|
*/
|
|
2818
2953
|
countOccurrences(elementOrNode) {
|
|
2819
2954
|
const predicate = this._ensurePredicate(elementOrNode);
|
|
@@ -3133,15 +3268,7 @@ var dataStructureTyped = (() => {
|
|
|
3133
3268
|
constructor(elements = [], options) {
|
|
3134
3269
|
super(options);
|
|
3135
3270
|
__publicField(this, "_elements", []);
|
|
3136
|
-
|
|
3137
|
-
for (const el of elements) {
|
|
3138
|
-
if (this.toElementFn) {
|
|
3139
|
-
this.push(this.toElementFn(el));
|
|
3140
|
-
} else {
|
|
3141
|
-
this.push(el);
|
|
3142
|
-
}
|
|
3143
|
-
}
|
|
3144
|
-
}
|
|
3271
|
+
this.pushMany(elements);
|
|
3145
3272
|
}
|
|
3146
3273
|
/**
|
|
3147
3274
|
* The elements function returns the elements of this set.
|
|
@@ -3157,10 +3284,6 @@ var dataStructureTyped = (() => {
|
|
|
3157
3284
|
get size() {
|
|
3158
3285
|
return this.elements.length;
|
|
3159
3286
|
}
|
|
3160
|
-
/**
|
|
3161
|
-
* Time Complexity: O(n)
|
|
3162
|
-
* Space Complexity: O(n)
|
|
3163
|
-
*/
|
|
3164
3287
|
/**
|
|
3165
3288
|
* Time Complexity: O(n)
|
|
3166
3289
|
* Space Complexity: O(n)
|
|
@@ -3174,6 +3297,9 @@ var dataStructureTyped = (() => {
|
|
|
3174
3297
|
return new _Stack(elements);
|
|
3175
3298
|
}
|
|
3176
3299
|
/**
|
|
3300
|
+
* Time Complexity: O(1)
|
|
3301
|
+
* Space Complexity: O(1)
|
|
3302
|
+
*
|
|
3177
3303
|
* The function checks if an array is empty and returns a boolean value.
|
|
3178
3304
|
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
3179
3305
|
*/
|
|
@@ -3216,18 +3342,46 @@ var dataStructureTyped = (() => {
|
|
|
3216
3342
|
return this.elements.pop();
|
|
3217
3343
|
}
|
|
3218
3344
|
/**
|
|
3219
|
-
*
|
|
3220
|
-
*
|
|
3221
|
-
*
|
|
3345
|
+
* Time Complexity: O(k)
|
|
3346
|
+
* Space Complexity: O(1)
|
|
3347
|
+
*
|
|
3348
|
+
* The function `pushMany` iterates over elements and pushes them into an array after applying a
|
|
3349
|
+
* transformation function if provided.
|
|
3350
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
3351
|
+
* is an iterable containing elements of type `E` or `R`. The function iterates over each element in
|
|
3352
|
+
* the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
|
|
3353
|
+
* provided, it is used to
|
|
3354
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
3355
|
+
* element was successfully pushed into the data structure.
|
|
3356
|
+
*/
|
|
3357
|
+
pushMany(elements) {
|
|
3358
|
+
const ans = [];
|
|
3359
|
+
for (const el of elements) {
|
|
3360
|
+
if (this.toElementFn) {
|
|
3361
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
3362
|
+
} else {
|
|
3363
|
+
ans.push(this.push(el));
|
|
3364
|
+
}
|
|
3365
|
+
}
|
|
3366
|
+
return ans;
|
|
3367
|
+
}
|
|
3368
|
+
/**
|
|
3369
|
+
* Time Complexity: O(n)
|
|
3370
|
+
* Space Complexity: O(1)
|
|
3371
|
+
*
|
|
3372
|
+
* The toArray function returns a copy of the elements in an array.
|
|
3373
|
+
* @returns An array of type E.
|
|
3222
3374
|
*/
|
|
3223
3375
|
delete(element) {
|
|
3224
3376
|
const index = this.elements.indexOf(element);
|
|
3225
3377
|
return this.deleteAt(index);
|
|
3226
3378
|
}
|
|
3227
3379
|
/**
|
|
3228
|
-
*
|
|
3229
|
-
*
|
|
3230
|
-
*
|
|
3380
|
+
* Time Complexity: O(n)
|
|
3381
|
+
* Space Complexity: O(1)
|
|
3382
|
+
*
|
|
3383
|
+
* The toArray function returns a copy of the elements in an array.
|
|
3384
|
+
* @returns An array of type E.
|
|
3231
3385
|
*/
|
|
3232
3386
|
deleteAt(index) {
|
|
3233
3387
|
const spliced = this.elements.splice(index, 1);
|
|
@@ -3340,12 +3494,7 @@ var dataStructureTyped = (() => {
|
|
|
3340
3494
|
const { autoCompactRatio = 0.5 } = options;
|
|
3341
3495
|
this._autoCompactRatio = autoCompactRatio;
|
|
3342
3496
|
}
|
|
3343
|
-
|
|
3344
|
-
for (const el of elements) {
|
|
3345
|
-
if (this.toElementFn) this.push(this.toElementFn(el));
|
|
3346
|
-
else this.push(el);
|
|
3347
|
-
}
|
|
3348
|
-
}
|
|
3497
|
+
this.pushMany(elements);
|
|
3349
3498
|
}
|
|
3350
3499
|
/**
|
|
3351
3500
|
* The elements function returns the elements of this set.
|
|
@@ -3430,6 +3579,25 @@ var dataStructureTyped = (() => {
|
|
|
3430
3579
|
this.elements.push(element);
|
|
3431
3580
|
return true;
|
|
3432
3581
|
}
|
|
3582
|
+
/**
|
|
3583
|
+
* Time Complexity: O(k)
|
|
3584
|
+
* Space Complexity: O(k)
|
|
3585
|
+
*
|
|
3586
|
+
* The `pushMany` function iterates over elements and pushes them into an array after applying a
|
|
3587
|
+
* transformation function if provided.
|
|
3588
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
3589
|
+
* is an iterable containing elements of type `E` or `R`.
|
|
3590
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
3591
|
+
* element was successfully pushed into the data structure.
|
|
3592
|
+
*/
|
|
3593
|
+
pushMany(elements) {
|
|
3594
|
+
const ans = [];
|
|
3595
|
+
for (const el of elements) {
|
|
3596
|
+
if (this.toElementFn) ans.push(this.push(this.toElementFn(el)));
|
|
3597
|
+
else ans.push(this.push(el));
|
|
3598
|
+
}
|
|
3599
|
+
return ans;
|
|
3600
|
+
}
|
|
3433
3601
|
/**
|
|
3434
3602
|
* Time Complexity: O(1)
|
|
3435
3603
|
* Space Complexity: O(1)
|
|
@@ -3446,6 +3614,9 @@ var dataStructureTyped = (() => {
|
|
|
3446
3614
|
return first;
|
|
3447
3615
|
}
|
|
3448
3616
|
/**
|
|
3617
|
+
* Time Complexity: O(n)
|
|
3618
|
+
* Space Complexity: O(1)
|
|
3619
|
+
*
|
|
3449
3620
|
* The delete function removes an element from the list.
|
|
3450
3621
|
* @param {E} element - Specify the element to be deleted
|
|
3451
3622
|
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
@@ -3455,6 +3626,9 @@ var dataStructureTyped = (() => {
|
|
|
3455
3626
|
return this.deleteAt(index);
|
|
3456
3627
|
}
|
|
3457
3628
|
/**
|
|
3629
|
+
* Time Complexity: O(n)
|
|
3630
|
+
* Space Complexity: O(1)
|
|
3631
|
+
*
|
|
3458
3632
|
* The deleteAt function deletes the element at a given index.
|
|
3459
3633
|
* @param {number} index - Determine the index of the element to be deleted
|
|
3460
3634
|
* @return A boolean value
|
|
@@ -3467,7 +3641,12 @@ var dataStructureTyped = (() => {
|
|
|
3467
3641
|
* Time Complexity: O(1)
|
|
3468
3642
|
* Space Complexity: O(1)
|
|
3469
3643
|
*
|
|
3470
|
-
*
|
|
3644
|
+
* The `at` function returns the element at a specified index adjusted by an offset, or `undefined`
|
|
3645
|
+
* if the index is out of bounds.
|
|
3646
|
+
* @param {number} index - The `index` parameter represents the position of the element you want to
|
|
3647
|
+
* retrieve from the data structure.
|
|
3648
|
+
* @returns The `at` method is returning the element at the specified index adjusted by the offset
|
|
3649
|
+
* `_offset`.
|
|
3471
3650
|
*/
|
|
3472
3651
|
at(index) {
|
|
3473
3652
|
return this.elements[index + this._offset];
|
|
@@ -3503,6 +3682,9 @@ var dataStructureTyped = (() => {
|
|
|
3503
3682
|
this._offset = 0;
|
|
3504
3683
|
}
|
|
3505
3684
|
/**
|
|
3685
|
+
* Time Complexity: O(n)
|
|
3686
|
+
* Space Complexity: O(1)
|
|
3687
|
+
*
|
|
3506
3688
|
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
3507
3689
|
* offset to zero.
|
|
3508
3690
|
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
@@ -3552,6 +3734,20 @@ var dataStructureTyped = (() => {
|
|
|
3552
3734
|
/**
|
|
3553
3735
|
* Time Complexity: O(n)
|
|
3554
3736
|
* Space Complexity: O(n)
|
|
3737
|
+
*
|
|
3738
|
+
* The `map` function in TypeScript creates a new Queue by applying a callback function to each
|
|
3739
|
+
* element in the original Queue.
|
|
3740
|
+
* @param callback - The `callback` parameter is a function that will be applied to each element in
|
|
3741
|
+
* the queue. It takes the current element, its index, and the queue itself as arguments, and returns
|
|
3742
|
+
* a new element.
|
|
3743
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to
|
|
3744
|
+
* convert a raw element of type `RM` to a new element of type `EM`. This function is used within the
|
|
3745
|
+
* `map` method to transform each raw element before passing it to the `callback` function. If
|
|
3746
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the
|
|
3747
|
+
* value of `this` when executing the `callback` function. It allows you to set the context (the
|
|
3748
|
+
* value of `this`) within the callback function. If `thisArg` is provided, it will be
|
|
3749
|
+
* @returns A new Queue object containing elements of type EM, which are the result of applying the
|
|
3750
|
+
* callback function to each element in the original Queue object.
|
|
3555
3751
|
*/
|
|
3556
3752
|
map(callback, toElementFn, thisArg) {
|
|
3557
3753
|
const newDeque = new _Queue([], { toElementFn });
|
|
@@ -3632,13 +3828,7 @@ var dataStructureTyped = (() => {
|
|
|
3632
3828
|
const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
|
|
3633
3829
|
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
3634
3830
|
this._firstInBucket = this._lastInBucket = this._bucketSize - _size % this._bucketSize >> 1;
|
|
3635
|
-
|
|
3636
|
-
if (this.toElementFn) {
|
|
3637
|
-
this.push(this.toElementFn(el));
|
|
3638
|
-
} else {
|
|
3639
|
-
this.push(el);
|
|
3640
|
-
}
|
|
3641
|
-
}
|
|
3831
|
+
this.pushMany(elements);
|
|
3642
3832
|
}
|
|
3643
3833
|
/**
|
|
3644
3834
|
* The bucketSize function returns the size of the bucket.
|
|
@@ -3776,6 +3966,32 @@ var dataStructureTyped = (() => {
|
|
|
3776
3966
|
this._size -= 1;
|
|
3777
3967
|
return element;
|
|
3778
3968
|
}
|
|
3969
|
+
/**
|
|
3970
|
+
* Time Complexity: O(1)
|
|
3971
|
+
* Space Complexity: O(1)
|
|
3972
|
+
*
|
|
3973
|
+
* The `shift()` function removes and returns the first element from a data structure, updating the
|
|
3974
|
+
* internal state variables accordingly.
|
|
3975
|
+
* @returns The element that is being removed from the beginning of the data structure is being
|
|
3976
|
+
* returned.
|
|
3977
|
+
*/
|
|
3978
|
+
shift() {
|
|
3979
|
+
if (this._size === 0) return;
|
|
3980
|
+
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
3981
|
+
if (this._size !== 1) {
|
|
3982
|
+
if (this._firstInBucket < this._bucketSize - 1) {
|
|
3983
|
+
this._firstInBucket += 1;
|
|
3984
|
+
} else if (this._bucketFirst < this._bucketCount - 1) {
|
|
3985
|
+
this._bucketFirst += 1;
|
|
3986
|
+
this._firstInBucket = 0;
|
|
3987
|
+
} else {
|
|
3988
|
+
this._bucketFirst = 0;
|
|
3989
|
+
this._firstInBucket = 0;
|
|
3990
|
+
}
|
|
3991
|
+
}
|
|
3992
|
+
this._size -= 1;
|
|
3993
|
+
return element;
|
|
3994
|
+
}
|
|
3779
3995
|
/**
|
|
3780
3996
|
* Time Complexity: Amortized O(1)
|
|
3781
3997
|
* Space Complexity: O(n)
|
|
@@ -3805,30 +4021,53 @@ var dataStructureTyped = (() => {
|
|
|
3805
4021
|
return true;
|
|
3806
4022
|
}
|
|
3807
4023
|
/**
|
|
3808
|
-
* Time Complexity: O(
|
|
3809
|
-
* Space Complexity: O(
|
|
4024
|
+
* Time Complexity: O(k)
|
|
4025
|
+
* Space Complexity: O(k)
|
|
3810
4026
|
*
|
|
3811
|
-
* The `
|
|
3812
|
-
*
|
|
3813
|
-
* @
|
|
3814
|
-
*
|
|
4027
|
+
* The function `pushMany` iterates over elements and pushes them into an array after applying a
|
|
4028
|
+
* transformation function if provided.
|
|
4029
|
+
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
4030
|
+
* parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
|
|
4031
|
+
* or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
|
|
4032
|
+
* function iterates over each element
|
|
4033
|
+
* @returns The `pushMany` function is returning an array of boolean values, where each value
|
|
4034
|
+
* represents the result of calling the `push` method on the current object instance with the
|
|
4035
|
+
* corresponding element from the input `elements` iterable.
|
|
3815
4036
|
*/
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
const
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
this._firstInBucket += 1;
|
|
3822
|
-
} else if (this._bucketFirst < this._bucketCount - 1) {
|
|
3823
|
-
this._bucketFirst += 1;
|
|
3824
|
-
this._firstInBucket = 0;
|
|
4037
|
+
pushMany(elements) {
|
|
4038
|
+
const ans = [];
|
|
4039
|
+
for (const el of elements) {
|
|
4040
|
+
if (this.toElementFn) {
|
|
4041
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
3825
4042
|
} else {
|
|
3826
|
-
this.
|
|
3827
|
-
this._firstInBucket = 0;
|
|
4043
|
+
ans.push(this.push(el));
|
|
3828
4044
|
}
|
|
3829
4045
|
}
|
|
3830
|
-
|
|
3831
|
-
|
|
4046
|
+
return ans;
|
|
4047
|
+
}
|
|
4048
|
+
/**
|
|
4049
|
+
* Time Complexity: O(k)
|
|
4050
|
+
* Space Complexity: O(k)
|
|
4051
|
+
*
|
|
4052
|
+
* The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of
|
|
4053
|
+
* an array, optionally converting them using a provided function.
|
|
4054
|
+
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
4055
|
+
* parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
|
|
4056
|
+
* can be an array or any other iterable data structure that has a known size or length. The function
|
|
4057
|
+
* iterates over each element in the `elements` iterable and
|
|
4058
|
+
* @returns The `unshiftMany` function returns an array of boolean values indicating whether each
|
|
4059
|
+
* element was successfully added to the beginning of the array.
|
|
4060
|
+
*/
|
|
4061
|
+
unshiftMany(elements = []) {
|
|
4062
|
+
const ans = [];
|
|
4063
|
+
for (const el of elements) {
|
|
4064
|
+
if (this.toElementFn) {
|
|
4065
|
+
ans.push(this.unshift(this.toElementFn(el)));
|
|
4066
|
+
} else {
|
|
4067
|
+
ans.push(this.unshift(el));
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
return ans;
|
|
3832
4071
|
}
|
|
3833
4072
|
/**
|
|
3834
4073
|
* Time Complexity: O(1)
|
|
@@ -4345,12 +4584,7 @@ var dataStructureTyped = (() => {
|
|
|
4345
4584
|
const { comparator } = options;
|
|
4346
4585
|
if (comparator) this._comparator = comparator;
|
|
4347
4586
|
}
|
|
4348
|
-
|
|
4349
|
-
for (const el of elements) {
|
|
4350
|
-
if (this.toElementFn) this.add(this.toElementFn(el));
|
|
4351
|
-
else this.add(el);
|
|
4352
|
-
}
|
|
4353
|
-
}
|
|
4587
|
+
this.addMany(elements);
|
|
4354
4588
|
}
|
|
4355
4589
|
/**
|
|
4356
4590
|
* The function returns an array of elements.
|
|
@@ -4386,13 +4620,40 @@ var dataStructureTyped = (() => {
|
|
|
4386
4620
|
* Time Complexity: O(log n)
|
|
4387
4621
|
* Space Complexity: O(1)
|
|
4388
4622
|
*
|
|
4389
|
-
*
|
|
4390
|
-
* @param element - The element to
|
|
4623
|
+
* The add function pushes an element into an array and then triggers a bubble-up operation.
|
|
4624
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the
|
|
4625
|
+
* data structure.
|
|
4626
|
+
* @returns The `add` method is returning a boolean value, which is the result of calling the
|
|
4627
|
+
* `_bubbleUp` method with the index `this.elements.length - 1` as an argument.
|
|
4391
4628
|
*/
|
|
4392
4629
|
add(element) {
|
|
4393
4630
|
this._elements.push(element);
|
|
4394
4631
|
return this._bubbleUp(this.elements.length - 1);
|
|
4395
4632
|
}
|
|
4633
|
+
/**
|
|
4634
|
+
* Time Complexity: O(k log n)
|
|
4635
|
+
* Space Complexity: O(1)
|
|
4636
|
+
*
|
|
4637
|
+
* The `addMany` function iterates over elements and adds them to a collection, returning an array of
|
|
4638
|
+
* boolean values indicating success or failure.
|
|
4639
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `addMany` method is
|
|
4640
|
+
* an iterable containing elements of type `E` or `R`. The method iterates over each element in the
|
|
4641
|
+
* iterable and adds them to the data structure. If a transformation function `_toElementFn` is
|
|
4642
|
+
* provided, it transforms the element
|
|
4643
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each element
|
|
4644
|
+
* in the input iterable was successfully added to the data structure.
|
|
4645
|
+
*/
|
|
4646
|
+
addMany(elements) {
|
|
4647
|
+
const ans = [];
|
|
4648
|
+
for (const el of elements) {
|
|
4649
|
+
if (this._toElementFn) {
|
|
4650
|
+
ans.push(this.add(this._toElementFn(el)));
|
|
4651
|
+
continue;
|
|
4652
|
+
}
|
|
4653
|
+
ans.push(this.add(el));
|
|
4654
|
+
}
|
|
4655
|
+
return ans;
|
|
4656
|
+
}
|
|
4396
4657
|
/**
|
|
4397
4658
|
* Time Complexity: O(log n)
|
|
4398
4659
|
* Space Complexity: O(1)
|
|
@@ -4586,7 +4847,7 @@ var dataStructureTyped = (() => {
|
|
|
4586
4847
|
return filteredList;
|
|
4587
4848
|
}
|
|
4588
4849
|
/**
|
|
4589
|
-
* Time Complexity: O(n
|
|
4850
|
+
* Time Complexity: O(n)
|
|
4590
4851
|
* Space Complexity: O(n)
|
|
4591
4852
|
*
|
|
4592
4853
|
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
@@ -7097,12 +7358,28 @@ var dataStructureTyped = (() => {
|
|
|
7097
7358
|
}
|
|
7098
7359
|
};
|
|
7099
7360
|
|
|
7100
|
-
// src/
|
|
7361
|
+
// src/common/index.ts
|
|
7101
7362
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
7102
7363
|
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
7103
7364
|
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
7104
7365
|
return DFSOperation2;
|
|
7105
7366
|
})(DFSOperation || {});
|
|
7367
|
+
var Range = class {
|
|
7368
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
7369
|
+
this.low = low;
|
|
7370
|
+
this.high = high;
|
|
7371
|
+
this.includeLow = includeLow;
|
|
7372
|
+
this.includeHigh = includeHigh;
|
|
7373
|
+
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
7374
|
+
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
7375
|
+
}
|
|
7376
|
+
// Determine whether a key is within the range
|
|
7377
|
+
isInRange(key, comparator) {
|
|
7378
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
7379
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
7380
|
+
return lowCheck && highCheck;
|
|
7381
|
+
}
|
|
7382
|
+
};
|
|
7106
7383
|
|
|
7107
7384
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
7108
7385
|
var BinaryTreeNode = class {
|
|
@@ -7235,7 +7512,7 @@ var dataStructureTyped = (() => {
|
|
|
7235
7512
|
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
7236
7513
|
* value.
|
|
7237
7514
|
*/
|
|
7238
|
-
|
|
7515
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
7239
7516
|
if (keyNodeEntryOrRaw === void 0) return [void 0, void 0];
|
|
7240
7517
|
if (keyNodeEntryOrRaw === null) return [null, void 0];
|
|
7241
7518
|
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
@@ -7246,15 +7523,12 @@ var dataStructureTyped = (() => {
|
|
|
7246
7523
|
const finalValue = value != null ? value : entryValue;
|
|
7247
7524
|
return [this.createNode(key, finalValue), finalValue];
|
|
7248
7525
|
}
|
|
7249
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
7250
7526
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
7251
|
-
|
|
7252
|
-
|
|
7253
|
-
|
|
7254
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
7255
|
-
}
|
|
7256
|
-
return [void 0, void 0];
|
|
7527
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
7528
|
+
const finalValue = value != null ? value : entryValue;
|
|
7529
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
7257
7530
|
}
|
|
7531
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
7258
7532
|
return [void 0, void 0];
|
|
7259
7533
|
}
|
|
7260
7534
|
/**
|
|
@@ -7281,13 +7555,13 @@ var dataStructureTyped = (() => {
|
|
|
7281
7555
|
const key = keyNodeEntryOrRaw[0];
|
|
7282
7556
|
if (key === null) return null;
|
|
7283
7557
|
if (key === void 0) return;
|
|
7284
|
-
return this.
|
|
7558
|
+
return this.getNode(key, this._root, iterationType);
|
|
7285
7559
|
}
|
|
7286
7560
|
if (this._toEntryFn) {
|
|
7287
7561
|
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
7288
|
-
if (this.isKey(key)) return this.
|
|
7562
|
+
if (this.isKey(key)) return this.getNode(key);
|
|
7289
7563
|
}
|
|
7290
|
-
if (this.isKey(keyNodeEntryOrRaw)) return this.
|
|
7564
|
+
if (this.isKey(keyNodeEntryOrRaw)) return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
|
|
7291
7565
|
return;
|
|
7292
7566
|
}
|
|
7293
7567
|
/**
|
|
@@ -7304,8 +7578,15 @@ var dataStructureTyped = (() => {
|
|
|
7304
7578
|
isNode(keyNodeEntryOrRaw) {
|
|
7305
7579
|
return keyNodeEntryOrRaw instanceof BinaryTreeNode;
|
|
7306
7580
|
}
|
|
7581
|
+
/**
|
|
7582
|
+
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
7583
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
|
|
7584
|
+
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
7585
|
+
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
7586
|
+
* indicating that it is of type `R`.
|
|
7587
|
+
*/
|
|
7307
7588
|
isRaw(keyNodeEntryOrRaw) {
|
|
7308
|
-
return typeof keyNodeEntryOrRaw === "object";
|
|
7589
|
+
return this._toEntryFn !== void 0 && typeof keyNodeEntryOrRaw === "object";
|
|
7309
7590
|
}
|
|
7310
7591
|
/**
|
|
7311
7592
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
@@ -7343,6 +7624,9 @@ var dataStructureTyped = (() => {
|
|
|
7343
7624
|
isNIL(keyNodeEntryOrRaw) {
|
|
7344
7625
|
return keyNodeEntryOrRaw === this._NIL;
|
|
7345
7626
|
}
|
|
7627
|
+
isRange(keyNodeEntryRawOrPredicate) {
|
|
7628
|
+
return keyNodeEntryRawOrPredicate instanceof Range;
|
|
7629
|
+
}
|
|
7346
7630
|
/**
|
|
7347
7631
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
7348
7632
|
* tree.
|
|
@@ -7405,7 +7689,7 @@ var dataStructureTyped = (() => {
|
|
|
7405
7689
|
* key was found and the node was replaced instead of inserted.
|
|
7406
7690
|
*/
|
|
7407
7691
|
add(keyNodeEntryOrRaw, value) {
|
|
7408
|
-
const [newNode, newValue] = this.
|
|
7692
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
7409
7693
|
if (newNode === void 0) return false;
|
|
7410
7694
|
if (!this._root) {
|
|
7411
7695
|
this._setRoot(newNode);
|
|
@@ -7481,6 +7765,17 @@ var dataStructureTyped = (() => {
|
|
|
7481
7765
|
}
|
|
7482
7766
|
return inserted;
|
|
7483
7767
|
}
|
|
7768
|
+
/**
|
|
7769
|
+
* Time Complexity: O(k * n)
|
|
7770
|
+
* Space Complexity: O(1)
|
|
7771
|
+
*
|
|
7772
|
+
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
7773
|
+
* elements from the other tree.
|
|
7774
|
+
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
7775
|
+
*/
|
|
7776
|
+
merge(anotherTree) {
|
|
7777
|
+
this.addMany(anotherTree, []);
|
|
7778
|
+
}
|
|
7484
7779
|
/**
|
|
7485
7780
|
* Time Complexity: O(k * n)
|
|
7486
7781
|
* Space Complexity: O(1)
|
|
@@ -7555,34 +7850,37 @@ var dataStructureTyped = (() => {
|
|
|
7555
7850
|
* Time Complexity: O(n)
|
|
7556
7851
|
* Space Complexity: O(k + log n)
|
|
7557
7852
|
*
|
|
7558
|
-
* The
|
|
7559
|
-
* or
|
|
7560
|
-
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
7561
|
-
*
|
|
7562
|
-
* @param [onlyOne=false] - The `onlyOne` parameter in the `
|
|
7563
|
-
* determines whether
|
|
7564
|
-
*
|
|
7565
|
-
* @param {
|
|
7566
|
-
*
|
|
7567
|
-
*
|
|
7568
|
-
*
|
|
7569
|
-
*
|
|
7570
|
-
*
|
|
7571
|
-
*
|
|
7572
|
-
* @
|
|
7573
|
-
*
|
|
7574
|
-
|
|
7575
|
-
|
|
7853
|
+
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
7854
|
+
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
7855
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
7856
|
+
* `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
|
|
7857
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
7858
|
+
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
7859
|
+
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
7860
|
+
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
7861
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
7862
|
+
* extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
7863
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
|
|
7864
|
+
* used to specify the node from which the search operation should begin. It represents the starting
|
|
7865
|
+
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
7866
|
+
* provided, the search operation will start from the root
|
|
7867
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
|
|
7868
|
+
* specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
|
|
7869
|
+
* two possible values:
|
|
7870
|
+
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
7871
|
+
* on the search algorithm implemented within the function.
|
|
7872
|
+
*/
|
|
7873
|
+
search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
7576
7874
|
if (keyNodeEntryRawOrPredicate === void 0) return [];
|
|
7577
7875
|
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
7578
7876
|
startNode = this.ensureNode(startNode);
|
|
7579
7877
|
if (!startNode) return [];
|
|
7580
|
-
const
|
|
7878
|
+
const predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
7581
7879
|
const ans = [];
|
|
7582
7880
|
if (iterationType === "RECURSIVE") {
|
|
7583
7881
|
const dfs = (cur) => {
|
|
7584
|
-
if (
|
|
7585
|
-
ans.push(cur);
|
|
7882
|
+
if (predicate(cur)) {
|
|
7883
|
+
ans.push(callback(cur));
|
|
7586
7884
|
if (onlyOne) return;
|
|
7587
7885
|
}
|
|
7588
7886
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
@@ -7595,8 +7893,8 @@ var dataStructureTyped = (() => {
|
|
|
7595
7893
|
while (stack.length > 0) {
|
|
7596
7894
|
const cur = stack.pop();
|
|
7597
7895
|
if (this.isRealNode(cur)) {
|
|
7598
|
-
if (
|
|
7599
|
-
ans.push(cur);
|
|
7896
|
+
if (predicate(cur)) {
|
|
7897
|
+
ans.push(callback(cur));
|
|
7600
7898
|
if (onlyOne) return ans;
|
|
7601
7899
|
}
|
|
7602
7900
|
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
@@ -7606,6 +7904,30 @@ var dataStructureTyped = (() => {
|
|
|
7606
7904
|
}
|
|
7607
7905
|
return ans;
|
|
7608
7906
|
}
|
|
7907
|
+
/**
|
|
7908
|
+
* Time Complexity: O(n)
|
|
7909
|
+
* Space Complexity: O(k + log n)
|
|
7910
|
+
*
|
|
7911
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
7912
|
+
* or predicate, with options for recursive or iterative traversal.
|
|
7913
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
7914
|
+
* - The `getNodes` function you provided takes several parameters:
|
|
7915
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
7916
|
+
* determines whether to return only the first node that matches the criteria specified by the
|
|
7917
|
+
* `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
7918
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
7919
|
+
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
7920
|
+
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
7921
|
+
* not provided, the default value is set to `this._root
|
|
7922
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
7923
|
+
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
7924
|
+
* can have two possible values:
|
|
7925
|
+
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
7926
|
+
* based on the input parameters and the iteration type specified.
|
|
7927
|
+
*/
|
|
7928
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
7929
|
+
return this.search(keyNodeEntryRawOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
7930
|
+
}
|
|
7609
7931
|
/**
|
|
7610
7932
|
* Time Complexity: O(n)
|
|
7611
7933
|
* Space Complexity: O(log n).
|
|
@@ -7628,23 +7950,7 @@ var dataStructureTyped = (() => {
|
|
|
7628
7950
|
*/
|
|
7629
7951
|
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7630
7952
|
var _a;
|
|
7631
|
-
return (_a = this.
|
|
7632
|
-
}
|
|
7633
|
-
/**
|
|
7634
|
-
* Time Complexity: O(n)
|
|
7635
|
-
* Space Complexity: O(log n)
|
|
7636
|
-
*
|
|
7637
|
-
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
7638
|
-
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
7639
|
-
* structure.
|
|
7640
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
7641
|
-
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
7642
|
-
* is an optional parameter with a default value of `this.iterationType`.
|
|
7643
|
-
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
7644
|
-
* (`OptNodeOrNull<NODE>`).
|
|
7645
|
-
*/
|
|
7646
|
-
getNodeByKey(key, iterationType = this.iterationType) {
|
|
7647
|
-
return this.getNode(key, this._root, iterationType);
|
|
7953
|
+
return (_a = this.search(keyNodeEntryRawOrPredicate, true, (node) => node, startNode, iterationType)[0]) != null ? _a : null;
|
|
7648
7954
|
}
|
|
7649
7955
|
/**
|
|
7650
7956
|
* Time Complexity: O(n)
|
|
@@ -7671,7 +7977,7 @@ var dataStructureTyped = (() => {
|
|
|
7671
7977
|
get(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7672
7978
|
var _a;
|
|
7673
7979
|
if (this._isMapMode) {
|
|
7674
|
-
const key = this.
|
|
7980
|
+
const key = this._extractKey(keyNodeEntryRawOrPredicate);
|
|
7675
7981
|
if (key === null || key === void 0) return;
|
|
7676
7982
|
return this._store.get(key);
|
|
7677
7983
|
}
|
|
@@ -7699,7 +8005,7 @@ var dataStructureTyped = (() => {
|
|
|
7699
8005
|
* Otherwise, it returns `false`.
|
|
7700
8006
|
*/
|
|
7701
8007
|
has(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7702
|
-
return this.
|
|
8008
|
+
return this.search(keyNodeEntryRawOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
7703
8009
|
}
|
|
7704
8010
|
/**
|
|
7705
8011
|
* Time Complexity: O(1)
|
|
@@ -7938,7 +8244,7 @@ var dataStructureTyped = (() => {
|
|
|
7938
8244
|
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
7939
8245
|
* parameter.
|
|
7940
8246
|
*/
|
|
7941
|
-
getPathToRoot(callback = this._DEFAULT_NODE_CALLBACK,
|
|
8247
|
+
getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
|
|
7942
8248
|
const result = [];
|
|
7943
8249
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
7944
8250
|
if (!beginNodeEnsured) return result;
|
|
@@ -8863,16 +9169,16 @@ var dataStructureTyped = (() => {
|
|
|
8863
9169
|
* Time Complexity: O(1)
|
|
8864
9170
|
* Space Complexity: O(1)
|
|
8865
9171
|
*
|
|
8866
|
-
* The function `
|
|
9172
|
+
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
8867
9173
|
* entry, raw data, or null/undefined.
|
|
8868
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `
|
|
9174
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
|
|
8869
9175
|
* TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
|
|
8870
9176
|
* where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
|
|
8871
|
-
* @returns The `
|
|
9177
|
+
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
|
|
8872
9178
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
8873
9179
|
* the conditions checked in the method.
|
|
8874
9180
|
*/
|
|
8875
|
-
|
|
9181
|
+
_extractKey(keyNodeEntryOrRaw) {
|
|
8876
9182
|
if (keyNodeEntryOrRaw === null) return null;
|
|
8877
9183
|
if (keyNodeEntryOrRaw === void 0) return;
|
|
8878
9184
|
if (keyNodeEntryOrRaw === this._NIL) return;
|
|
@@ -8907,6 +9213,9 @@ var dataStructureTyped = (() => {
|
|
|
8907
9213
|
return this._store.set(key, value);
|
|
8908
9214
|
}
|
|
8909
9215
|
/**
|
|
9216
|
+
* Time Complexity: O(1)
|
|
9217
|
+
* Space Complexity: O(1)
|
|
9218
|
+
*
|
|
8910
9219
|
* The _clearNodes function sets the root node to undefined and resets the size to 0.
|
|
8911
9220
|
*/
|
|
8912
9221
|
_clearNodes() {
|
|
@@ -8914,6 +9223,9 @@ var dataStructureTyped = (() => {
|
|
|
8914
9223
|
this._size = 0;
|
|
8915
9224
|
}
|
|
8916
9225
|
/**
|
|
9226
|
+
* Time Complexity: O(1)
|
|
9227
|
+
* Space Complexity: O(1)
|
|
9228
|
+
*
|
|
8917
9229
|
* The _clearValues function clears all values stored in the _store object.
|
|
8918
9230
|
*/
|
|
8919
9231
|
_clearValues() {
|
|
@@ -8982,20 +9294,30 @@ var dataStructureTyped = (() => {
|
|
|
8982
9294
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
8983
9295
|
super([], options);
|
|
8984
9296
|
__publicField(this, "_root");
|
|
8985
|
-
__publicField(this, "
|
|
9297
|
+
__publicField(this, "_isReverse", false);
|
|
9298
|
+
__publicField(this, "_comparator", (a, b) => {
|
|
9299
|
+
if (isComparable(a) && isComparable(b)) {
|
|
9300
|
+
if (a > b) return 1;
|
|
9301
|
+
if (a < b) return -1;
|
|
9302
|
+
return 0;
|
|
9303
|
+
}
|
|
9304
|
+
if (this._extractComparable) {
|
|
9305
|
+
if (this._extractComparable(a) > this._extractComparable(b)) return 1;
|
|
9306
|
+
if (this._extractComparable(a) < this._extractComparable(b)) return -1;
|
|
9307
|
+
return 0;
|
|
9308
|
+
}
|
|
8986
9309
|
if (typeof a === "object" || typeof b === "object") {
|
|
8987
9310
|
throw TypeError(
|
|
8988
|
-
`When comparing object types, a custom
|
|
9311
|
+
`When comparing object types, a custom extractComparable must be defined in the constructor's options parameter.`
|
|
8989
9312
|
);
|
|
8990
9313
|
}
|
|
8991
|
-
if (a > b) return 1;
|
|
8992
|
-
if (a < b) return -1;
|
|
8993
9314
|
return 0;
|
|
8994
9315
|
});
|
|
8995
|
-
__publicField(this, "
|
|
9316
|
+
__publicField(this, "_extractComparable");
|
|
8996
9317
|
if (options) {
|
|
8997
|
-
const {
|
|
8998
|
-
if (
|
|
9318
|
+
const { extractComparable, isReverse } = options;
|
|
9319
|
+
if (typeof extractComparable === "function") this._extractComparable = extractComparable;
|
|
9320
|
+
if (isReverse !== void 0) this._isReverse = isReverse;
|
|
8999
9321
|
}
|
|
9000
9322
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
9001
9323
|
}
|
|
@@ -9006,6 +9328,14 @@ var dataStructureTyped = (() => {
|
|
|
9006
9328
|
get root() {
|
|
9007
9329
|
return this._root;
|
|
9008
9330
|
}
|
|
9331
|
+
/**
|
|
9332
|
+
* The above function is a getter method in TypeScript that returns the value of the private property
|
|
9333
|
+
* `_isReverse`.
|
|
9334
|
+
* @returns The `isReverse` property of the object, which is a boolean value.
|
|
9335
|
+
*/
|
|
9336
|
+
get isReverse() {
|
|
9337
|
+
return this._isReverse;
|
|
9338
|
+
}
|
|
9009
9339
|
/**
|
|
9010
9340
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
9011
9341
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -9028,8 +9358,9 @@ var dataStructureTyped = (() => {
|
|
|
9028
9358
|
return new _BST([], __spreadValues({
|
|
9029
9359
|
iterationType: this.iterationType,
|
|
9030
9360
|
isMapMode: this._isMapMode,
|
|
9031
|
-
|
|
9032
|
-
toEntryFn: this._toEntryFn
|
|
9361
|
+
extractComparable: this._extractComparable,
|
|
9362
|
+
toEntryFn: this._toEntryFn,
|
|
9363
|
+
isReverse: this._isReverse
|
|
9033
9364
|
}, options));
|
|
9034
9365
|
}
|
|
9035
9366
|
/**
|
|
@@ -9041,8 +9372,8 @@ var dataStructureTyped = (() => {
|
|
|
9041
9372
|
* value associated with a key in a key-value pair.
|
|
9042
9373
|
* @returns either a NODE object or undefined.
|
|
9043
9374
|
*/
|
|
9044
|
-
|
|
9045
|
-
const [node, entryValue] = super.
|
|
9375
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
9376
|
+
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
9046
9377
|
if (node === null) return [void 0, void 0];
|
|
9047
9378
|
return [node, value != null ? value : entryValue];
|
|
9048
9379
|
}
|
|
@@ -9080,11 +9411,11 @@ var dataStructureTyped = (() => {
|
|
|
9080
9411
|
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
|
|
9081
9412
|
* type `K`.
|
|
9082
9413
|
* @returns The `override isKey(key: any): key is K` function is returning a boolean value based on
|
|
9083
|
-
* the result of the `isComparable` function with the condition `this.
|
|
9414
|
+
* the result of the `isComparable` function with the condition `this._compare !==
|
|
9084
9415
|
* this._DEFAULT_COMPARATOR`.
|
|
9085
9416
|
*/
|
|
9086
9417
|
isKey(key) {
|
|
9087
|
-
return isComparable(key, this.
|
|
9418
|
+
return isComparable(key, this._extractComparable !== void 0);
|
|
9088
9419
|
}
|
|
9089
9420
|
/**
|
|
9090
9421
|
* Time Complexity: O(log n)
|
|
@@ -9098,7 +9429,7 @@ var dataStructureTyped = (() => {
|
|
|
9098
9429
|
* @returns a boolean value.
|
|
9099
9430
|
*/
|
|
9100
9431
|
add(keyNodeEntryOrRaw, value) {
|
|
9101
|
-
const [newNode, newValue] = this.
|
|
9432
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
9102
9433
|
if (newNode === void 0) return false;
|
|
9103
9434
|
if (this._root === void 0) {
|
|
9104
9435
|
this._setRoot(newNode);
|
|
@@ -9108,11 +9439,11 @@ var dataStructureTyped = (() => {
|
|
|
9108
9439
|
}
|
|
9109
9440
|
let current = this._root;
|
|
9110
9441
|
while (current !== void 0) {
|
|
9111
|
-
if (this.
|
|
9442
|
+
if (this._compare(current.key, newNode.key) === 0) {
|
|
9112
9443
|
this._replaceNode(current, newNode);
|
|
9113
9444
|
if (this._isMapMode) this._setValue(current.key, newValue);
|
|
9114
9445
|
return true;
|
|
9115
|
-
} else if (this.
|
|
9446
|
+
} else if (this._compare(current.key, newNode.key) > 0) {
|
|
9116
9447
|
if (current.left === void 0) {
|
|
9117
9448
|
current.left = newNode;
|
|
9118
9449
|
if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
|
|
@@ -9190,7 +9521,7 @@ var dataStructureTyped = (() => {
|
|
|
9190
9521
|
keyB = b;
|
|
9191
9522
|
}
|
|
9192
9523
|
if (keyA !== void 0 && keyA !== null && keyB !== void 0 && keyB !== null) {
|
|
9193
|
-
return this.
|
|
9524
|
+
return this._compare(keyA, keyB);
|
|
9194
9525
|
}
|
|
9195
9526
|
return 0;
|
|
9196
9527
|
});
|
|
@@ -9226,48 +9557,92 @@ var dataStructureTyped = (() => {
|
|
|
9226
9557
|
}
|
|
9227
9558
|
return inserted;
|
|
9228
9559
|
}
|
|
9560
|
+
/**
|
|
9561
|
+
* Time Complexity: O(n)
|
|
9562
|
+
* Space Complexity: O(1)
|
|
9563
|
+
*
|
|
9564
|
+
* The `merge` function overrides the base class method by adding elements from another
|
|
9565
|
+
* binary search tree.
|
|
9566
|
+
* @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
|
|
9567
|
+
* value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
|
|
9568
|
+
*/
|
|
9569
|
+
merge(anotherTree) {
|
|
9570
|
+
this.addMany(anotherTree, [], false);
|
|
9571
|
+
}
|
|
9229
9572
|
/**
|
|
9230
9573
|
* Time Complexity: O(log n)
|
|
9231
9574
|
* Space Complexity: O(k + log n)
|
|
9232
9575
|
*
|
|
9233
|
-
* The function `
|
|
9234
|
-
*
|
|
9235
|
-
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
9236
|
-
* parameter in the `
|
|
9237
|
-
*
|
|
9238
|
-
*
|
|
9239
|
-
*
|
|
9240
|
-
*
|
|
9241
|
-
*
|
|
9242
|
-
*
|
|
9243
|
-
*
|
|
9244
|
-
*
|
|
9245
|
-
*
|
|
9246
|
-
*
|
|
9247
|
-
*
|
|
9248
|
-
*
|
|
9249
|
-
*
|
|
9250
|
-
*
|
|
9576
|
+
* The function `search` in TypeScript overrides the search behavior in a binary tree structure based
|
|
9577
|
+
* on specified criteria.
|
|
9578
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
9579
|
+
* `keyNodeEntryRawOrPredicate` parameter in the `override search` method can accept one of the
|
|
9580
|
+
* following types:
|
|
9581
|
+
* @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
|
|
9582
|
+
* search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
|
|
9583
|
+
* search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
|
|
9584
|
+
* @param {C} callback - The `callback` parameter in the `override search` function is a function
|
|
9585
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
9586
|
+
* extends `NodeCallback<NODE>`. The callback function should accept a node of type `NODE` as its
|
|
9587
|
+
* argument and
|
|
9588
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `override search`
|
|
9589
|
+
* method represents the node from which the search operation will begin. It is the starting point
|
|
9590
|
+
* for searching within the tree data structure. The method ensures that the `startNode` is a valid
|
|
9591
|
+
* node before proceeding with the search operation. If the `
|
|
9592
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `override search`
|
|
9593
|
+
* function determines the type of iteration to be used during the search operation. It can have two
|
|
9594
|
+
* possible values:
|
|
9595
|
+
* @returns The `override search` method returns an array of values that match the search criteria
|
|
9596
|
+
* specified by the input parameters. The method performs a search operation on a binary tree
|
|
9597
|
+
* structure based on the provided key, predicate, and other options. The search results are
|
|
9598
|
+
* collected in an array and returned as the output of the method.
|
|
9251
9599
|
*/
|
|
9252
|
-
|
|
9600
|
+
search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
9253
9601
|
if (keyNodeEntryRawOrPredicate === void 0) return [];
|
|
9254
9602
|
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
9255
9603
|
startNode = this.ensureNode(startNode);
|
|
9256
9604
|
if (!startNode) return [];
|
|
9257
|
-
|
|
9605
|
+
let predicate;
|
|
9606
|
+
const isRange = this.isRange(keyNodeEntryRawOrPredicate);
|
|
9607
|
+
if (isRange) {
|
|
9608
|
+
predicate = (node) => keyNodeEntryRawOrPredicate.isInRange(node.key, this._comparator);
|
|
9609
|
+
} else {
|
|
9610
|
+
predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
9611
|
+
}
|
|
9612
|
+
const isToLeftByRange = (cur) => {
|
|
9613
|
+
if (isRange) {
|
|
9614
|
+
const range = keyNodeEntryRawOrPredicate;
|
|
9615
|
+
const leftS = this.isReverse ? range.high : range.low;
|
|
9616
|
+
const leftI = this.isReverse ? range.includeHigh : range.includeLow;
|
|
9617
|
+
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
9618
|
+
}
|
|
9619
|
+
return false;
|
|
9620
|
+
};
|
|
9621
|
+
const isToRightByRange = (cur) => {
|
|
9622
|
+
if (isRange) {
|
|
9623
|
+
const range = keyNodeEntryRawOrPredicate;
|
|
9624
|
+
const rightS = this.isReverse ? range.low : range.high;
|
|
9625
|
+
const rightI = this.isReverse ? range.includeLow : range.includeLow;
|
|
9626
|
+
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
9627
|
+
}
|
|
9628
|
+
return false;
|
|
9629
|
+
};
|
|
9258
9630
|
const ans = [];
|
|
9259
9631
|
if (iterationType === "RECURSIVE") {
|
|
9260
9632
|
const dfs = (cur) => {
|
|
9261
|
-
if (
|
|
9262
|
-
ans.push(cur);
|
|
9633
|
+
if (predicate(cur)) {
|
|
9634
|
+
ans.push(callback(cur));
|
|
9263
9635
|
if (onlyOne) return;
|
|
9264
9636
|
}
|
|
9265
9637
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
9266
|
-
if (
|
|
9267
|
-
|
|
9268
|
-
if (this.isRealNode(cur.
|
|
9638
|
+
if (isRange) {
|
|
9639
|
+
if (this.isRealNode(cur.left) && isToLeftByRange(cur)) dfs(cur.left);
|
|
9640
|
+
if (this.isRealNode(cur.right) && isToRightByRange(cur)) dfs(cur.right);
|
|
9641
|
+
} else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
9642
|
+
const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate);
|
|
9643
|
+
if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) > 0)
|
|
9269
9644
|
dfs(cur.left);
|
|
9270
|
-
if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this.
|
|
9645
|
+
if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) < 0)
|
|
9271
9646
|
dfs(cur.right);
|
|
9272
9647
|
} else {
|
|
9273
9648
|
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
@@ -9279,15 +9654,18 @@ var dataStructureTyped = (() => {
|
|
|
9279
9654
|
const stack = [startNode];
|
|
9280
9655
|
while (stack.length > 0) {
|
|
9281
9656
|
const cur = stack.pop();
|
|
9282
|
-
if (
|
|
9283
|
-
ans.push(cur);
|
|
9657
|
+
if (predicate(cur)) {
|
|
9658
|
+
ans.push(callback(cur));
|
|
9284
9659
|
if (onlyOne) return ans;
|
|
9285
9660
|
}
|
|
9286
|
-
if (
|
|
9287
|
-
|
|
9288
|
-
if (this.isRealNode(cur.right) &&
|
|
9661
|
+
if (isRange) {
|
|
9662
|
+
if (this.isRealNode(cur.left) && isToLeftByRange(cur)) stack.push(cur.left);
|
|
9663
|
+
if (this.isRealNode(cur.right) && isToRightByRange(cur)) stack.push(cur.right);
|
|
9664
|
+
} else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
9665
|
+
const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate);
|
|
9666
|
+
if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) < 0)
|
|
9289
9667
|
stack.push(cur.right);
|
|
9290
|
-
if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this.
|
|
9668
|
+
if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) > 0)
|
|
9291
9669
|
stack.push(cur.left);
|
|
9292
9670
|
} else {
|
|
9293
9671
|
if (this.isRealNode(cur.right)) stack.push(cur.right);
|
|
@@ -9297,6 +9675,31 @@ var dataStructureTyped = (() => {
|
|
|
9297
9675
|
}
|
|
9298
9676
|
return ans;
|
|
9299
9677
|
}
|
|
9678
|
+
/**
|
|
9679
|
+
* Time Complexity: O(log n)
|
|
9680
|
+
* Space Complexity: O(n)
|
|
9681
|
+
*
|
|
9682
|
+
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
|
|
9683
|
+
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
|
|
9684
|
+
* either a `Range` object or an array of two elements representing the range boundaries.
|
|
9685
|
+
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
|
|
9686
|
+
* function that is used to process each node that is found within the specified range during the
|
|
9687
|
+
* search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the
|
|
9688
|
+
* data structure.
|
|
9689
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch`
|
|
9690
|
+
* function represents the node from which the search for nodes within the specified range will
|
|
9691
|
+
* begin. It is the starting point for the range search operation.
|
|
9692
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
|
|
9693
|
+
* is used to specify the type of iteration to be performed during the search operation. It has a
|
|
9694
|
+
* default value of `this.iterationType`, which suggests that it is likely a property of the class or
|
|
9695
|
+
* object that the `rangeSearch`
|
|
9696
|
+
* @returns The `rangeSearch` function is returning the result of calling the `search` method with
|
|
9697
|
+
* the specified parameters.
|
|
9698
|
+
*/
|
|
9699
|
+
rangeSearch(range, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
9700
|
+
const searchRange = range instanceof Range ? range : new Range(range[0], range[1]);
|
|
9701
|
+
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
9702
|
+
}
|
|
9300
9703
|
/**
|
|
9301
9704
|
* Time Complexity: O(log n)
|
|
9302
9705
|
* Space Complexity: O(1)
|
|
@@ -9321,22 +9724,6 @@ var dataStructureTyped = (() => {
|
|
|
9321
9724
|
var _a;
|
|
9322
9725
|
return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
|
|
9323
9726
|
}
|
|
9324
|
-
/**
|
|
9325
|
-
* Time Complexity: O(log n)
|
|
9326
|
-
* Space Complexity: O(1)
|
|
9327
|
-
*
|
|
9328
|
-
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
9329
|
-
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
9330
|
-
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
9331
|
-
* in the tree structure.
|
|
9332
|
-
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
9333
|
-
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
9334
|
-
* It has a default value of `'ITERATIVE'`.
|
|
9335
|
-
* @returns The method is returning a NODE object or undefined.
|
|
9336
|
-
*/
|
|
9337
|
-
getNodeByKey(key, iterationType = this.iterationType) {
|
|
9338
|
-
return this.getNode(key, this._root, iterationType);
|
|
9339
|
-
}
|
|
9340
9727
|
/**
|
|
9341
9728
|
* Time complexity: O(n)
|
|
9342
9729
|
* Space complexity: O(n)
|
|
@@ -9430,7 +9817,7 @@ var dataStructureTyped = (() => {
|
|
|
9430
9817
|
const targetKey = targetNodeEnsured.key;
|
|
9431
9818
|
if (iterationType === "RECURSIVE") {
|
|
9432
9819
|
const dfs = (cur) => {
|
|
9433
|
-
const compared = this.
|
|
9820
|
+
const compared = this._compare(cur.key, targetKey);
|
|
9434
9821
|
if (Math.sign(compared) === lesserOrGreater) ans.push(callback(cur));
|
|
9435
9822
|
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
9436
9823
|
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
@@ -9442,7 +9829,7 @@ var dataStructureTyped = (() => {
|
|
|
9442
9829
|
while (queue.size > 0) {
|
|
9443
9830
|
const cur = queue.shift();
|
|
9444
9831
|
if (this.isRealNode(cur)) {
|
|
9445
|
-
const compared = this.
|
|
9832
|
+
const compared = this._compare(cur.key, targetKey);
|
|
9446
9833
|
if (Math.sign(compared) === lesserOrGreater) ans.push(callback(cur));
|
|
9447
9834
|
if (this.isRealNode(cur.left)) queue.push(cur.left);
|
|
9448
9835
|
if (this.isRealNode(cur.right)) queue.push(cur.right);
|
|
@@ -9554,6 +9941,14 @@ var dataStructureTyped = (() => {
|
|
|
9554
9941
|
get comparator() {
|
|
9555
9942
|
return this._comparator;
|
|
9556
9943
|
}
|
|
9944
|
+
/**
|
|
9945
|
+
* This function returns the value of the `_extractComparable` property.
|
|
9946
|
+
* @returns The method `extractComparable()` is being returned, which is a getter method for the
|
|
9947
|
+
* `_extractComparable` property.
|
|
9948
|
+
*/
|
|
9949
|
+
get extractComparable() {
|
|
9950
|
+
return this._extractComparable;
|
|
9951
|
+
}
|
|
9557
9952
|
/**
|
|
9558
9953
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
9559
9954
|
* root.
|
|
@@ -9565,6 +9960,9 @@ var dataStructureTyped = (() => {
|
|
|
9565
9960
|
}
|
|
9566
9961
|
this._root = v;
|
|
9567
9962
|
}
|
|
9963
|
+
_compare(a, b) {
|
|
9964
|
+
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
|
|
9965
|
+
}
|
|
9568
9966
|
};
|
|
9569
9967
|
|
|
9570
9968
|
// src/data-structures/binary-tree/binary-indexed-tree.ts
|
|
@@ -10200,8 +10598,9 @@ var dataStructureTyped = (() => {
|
|
|
10200
10598
|
return new _AVLTree([], __spreadValues({
|
|
10201
10599
|
iterationType: this.iterationType,
|
|
10202
10600
|
isMapMode: this._isMapMode,
|
|
10203
|
-
|
|
10204
|
-
toEntryFn: this._toEntryFn
|
|
10601
|
+
extractComparable: this._extractComparable,
|
|
10602
|
+
toEntryFn: this._toEntryFn,
|
|
10603
|
+
isReverse: this._isReverse
|
|
10205
10604
|
}, options));
|
|
10206
10605
|
}
|
|
10207
10606
|
/**
|
|
@@ -10487,7 +10886,7 @@ var dataStructureTyped = (() => {
|
|
|
10487
10886
|
*/
|
|
10488
10887
|
_balancePath(node) {
|
|
10489
10888
|
node = this.ensureNode(node);
|
|
10490
|
-
const path = this.getPathToRoot((node2) => node2,
|
|
10889
|
+
const path = this.getPathToRoot(node, (node2) => node2, false);
|
|
10491
10890
|
for (let i = 0; i < path.length; i++) {
|
|
10492
10891
|
const A = path[i];
|
|
10493
10892
|
if (A) {
|
|
@@ -10533,7 +10932,7 @@ var dataStructureTyped = (() => {
|
|
|
10533
10932
|
}
|
|
10534
10933
|
};
|
|
10535
10934
|
|
|
10536
|
-
// src/data-structures/binary-tree/
|
|
10935
|
+
// src/data-structures/binary-tree/red-black-tree.ts
|
|
10537
10936
|
var RedBlackTreeNode = class extends BSTNode {
|
|
10538
10937
|
/**
|
|
10539
10938
|
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
@@ -10619,7 +11018,7 @@ var dataStructureTyped = (() => {
|
|
|
10619
11018
|
return new _RedBlackTree([], __spreadValues({
|
|
10620
11019
|
iterationType: this.iterationType,
|
|
10621
11020
|
isMapMode: this._isMapMode,
|
|
10622
|
-
|
|
11021
|
+
extractComparable: this._extractComparable,
|
|
10623
11022
|
toEntryFn: this._toEntryFn
|
|
10624
11023
|
}, options));
|
|
10625
11024
|
}
|
|
@@ -10698,7 +11097,7 @@ var dataStructureTyped = (() => {
|
|
|
10698
11097
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
10699
11098
|
*/
|
|
10700
11099
|
add(keyNodeEntryOrRaw, value) {
|
|
10701
|
-
const [newNode, newValue] = this.
|
|
11100
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
10702
11101
|
if (!this.isRealNode(newNode)) return false;
|
|
10703
11102
|
const insertStatus = this._insert(newNode);
|
|
10704
11103
|
if (insertStatus === "CREATED") {
|
|
@@ -10828,7 +11227,7 @@ var dataStructureTyped = (() => {
|
|
|
10828
11227
|
let parent = void 0;
|
|
10829
11228
|
while (this.isRealNode(current)) {
|
|
10830
11229
|
parent = current;
|
|
10831
|
-
const compared = this.
|
|
11230
|
+
const compared = this._compare(node.key, current.key);
|
|
10832
11231
|
if (compared < 0) {
|
|
10833
11232
|
current = (_a = current.left) != null ? _a : this.NIL;
|
|
10834
11233
|
} else if (compared > 0) {
|
|
@@ -11143,8 +11542,9 @@ var dataStructureTyped = (() => {
|
|
|
11143
11542
|
return new _AVLTreeMultiMap([], __spreadValues({
|
|
11144
11543
|
iterationType: this.iterationType,
|
|
11145
11544
|
isMapMode: this._isMapMode,
|
|
11146
|
-
|
|
11147
|
-
toEntryFn: this._toEntryFn
|
|
11545
|
+
extractComparable: this._extractComparable,
|
|
11546
|
+
toEntryFn: this._toEntryFn,
|
|
11547
|
+
isReverse: this._isReverse
|
|
11148
11548
|
}, options));
|
|
11149
11549
|
}
|
|
11150
11550
|
/**
|
|
@@ -11169,7 +11569,7 @@ var dataStructureTyped = (() => {
|
|
|
11169
11569
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
11170
11570
|
* @returns either a NODE object or undefined.
|
|
11171
11571
|
*/
|
|
11172
|
-
|
|
11572
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
11173
11573
|
if (keyNodeEntryOrRaw === void 0 || keyNodeEntryOrRaw === null) return [void 0, void 0];
|
|
11174
11574
|
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
11175
11575
|
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
@@ -11178,15 +11578,12 @@ var dataStructureTyped = (() => {
|
|
|
11178
11578
|
const finalValue = value != null ? value : entryValue;
|
|
11179
11579
|
return [this.createNode(key, finalValue, count), finalValue];
|
|
11180
11580
|
}
|
|
11181
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
11182
11581
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
11183
|
-
|
|
11184
|
-
|
|
11185
|
-
|
|
11186
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
11187
|
-
}
|
|
11188
|
-
return [void 0, void 0];
|
|
11582
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
11583
|
+
const finalValue = value != null ? value : entryValue;
|
|
11584
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
11189
11585
|
}
|
|
11586
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
11190
11587
|
return [void 0, void 0];
|
|
11191
11588
|
}
|
|
11192
11589
|
/**
|
|
@@ -11207,7 +11604,7 @@ var dataStructureTyped = (() => {
|
|
|
11207
11604
|
* @returns a boolean value.
|
|
11208
11605
|
*/
|
|
11209
11606
|
add(keyNodeEntryOrRaw, value, count = 1) {
|
|
11210
|
-
const [newNode, newValue] = this.
|
|
11607
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
11211
11608
|
if (newNode === void 0) return false;
|
|
11212
11609
|
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
11213
11610
|
const inserted = super.add(newNode, newValue);
|
|
@@ -11505,7 +11902,7 @@ var dataStructureTyped = (() => {
|
|
|
11505
11902
|
return new _TreeMultiMap([], __spreadValues({
|
|
11506
11903
|
iterationType: this.iterationType,
|
|
11507
11904
|
isMapMode: this._isMapMode,
|
|
11508
|
-
|
|
11905
|
+
extractComparable: this._extractComparable,
|
|
11509
11906
|
toEntryFn: this._toEntryFn
|
|
11510
11907
|
}, options));
|
|
11511
11908
|
}
|
|
@@ -11521,7 +11918,7 @@ var dataStructureTyped = (() => {
|
|
|
11521
11918
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
11522
11919
|
* @returns either a NODE object or undefined.
|
|
11523
11920
|
*/
|
|
11524
|
-
|
|
11921
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
11525
11922
|
if (keyNodeEntryOrRaw === void 0 || keyNodeEntryOrRaw === null) return [void 0, void 0];
|
|
11526
11923
|
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
11527
11924
|
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
@@ -11530,7 +11927,7 @@ var dataStructureTyped = (() => {
|
|
|
11530
11927
|
const finalValue = value != null ? value : entryValue;
|
|
11531
11928
|
if (this.isKey(key)) return [this.createNode(key, finalValue, "BLACK", count), finalValue];
|
|
11532
11929
|
}
|
|
11533
|
-
if (this.
|
|
11930
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
11534
11931
|
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
11535
11932
|
const finalValue = value != null ? value : entryValue;
|
|
11536
11933
|
if (this.isKey(key)) return [this.createNode(key, finalValue, "BLACK", count), finalValue];
|
|
@@ -11565,7 +11962,7 @@ var dataStructureTyped = (() => {
|
|
|
11565
11962
|
* was successful, and false otherwise.
|
|
11566
11963
|
*/
|
|
11567
11964
|
add(keyNodeEntryOrRaw, value, count = 1) {
|
|
11568
|
-
const [newNode, newValue] = this.
|
|
11965
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
11569
11966
|
const orgCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
11570
11967
|
const isSuccessAdded = super.add(newNode, newValue);
|
|
11571
11968
|
if (isSuccessAdded) {
|
|
@@ -12649,10 +13046,16 @@ var dataStructureTyped = (() => {
|
|
|
12649
13046
|
};
|
|
12650
13047
|
var Trie = class _Trie extends IterableElementBase {
|
|
12651
13048
|
/**
|
|
12652
|
-
* The constructor
|
|
12653
|
-
*
|
|
12654
|
-
* @param
|
|
12655
|
-
*
|
|
13049
|
+
* The constructor initializes a Trie data structure with optional options and words provided as
|
|
13050
|
+
* input.
|
|
13051
|
+
* @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the constructor is an
|
|
13052
|
+
* iterable containing either strings or elements of type `R`. It is used to initialize the Trie with
|
|
13053
|
+
* a list of words or elements. If no `words` are provided, an empty iterable is used as the default
|
|
13054
|
+
* value.
|
|
13055
|
+
* @param [options] - The `options` parameter in the constructor is an optional object that can
|
|
13056
|
+
* contain configuration options for the Trie data structure. One of the options it can have is
|
|
13057
|
+
* `caseSensitive`, which is a boolean value indicating whether the Trie should be case-sensitive or
|
|
13058
|
+
* not. If `caseSensitive` is set to `
|
|
12656
13059
|
*/
|
|
12657
13060
|
constructor(words = [], options) {
|
|
12658
13061
|
super(options);
|
|
@@ -12664,13 +13067,7 @@ var dataStructureTyped = (() => {
|
|
|
12664
13067
|
if (caseSensitive !== void 0) this._caseSensitive = caseSensitive;
|
|
12665
13068
|
}
|
|
12666
13069
|
if (words) {
|
|
12667
|
-
|
|
12668
|
-
if (this.toElementFn) {
|
|
12669
|
-
this.add(this.toElementFn(word));
|
|
12670
|
-
} else {
|
|
12671
|
-
this.add(word);
|
|
12672
|
-
}
|
|
12673
|
-
}
|
|
13070
|
+
this.addMany(words);
|
|
12674
13071
|
}
|
|
12675
13072
|
}
|
|
12676
13073
|
/**
|
|
@@ -12721,6 +13118,29 @@ var dataStructureTyped = (() => {
|
|
|
12721
13118
|
}
|
|
12722
13119
|
return isNewWord;
|
|
12723
13120
|
}
|
|
13121
|
+
/**
|
|
13122
|
+
* Time Complexity: O(n * l)
|
|
13123
|
+
* Space Complexity: O(1)
|
|
13124
|
+
*
|
|
13125
|
+
* The `addMany` function in TypeScript takes an iterable of strings or elements of type R, converts
|
|
13126
|
+
* them using a provided function if available, and adds them to a data structure while returning an
|
|
13127
|
+
* array of boolean values indicating success.
|
|
13128
|
+
* @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the `addMany` function is
|
|
13129
|
+
* an iterable that contains either strings or elements of type `R`.
|
|
13130
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each word in
|
|
13131
|
+
* the input iterable was successfully added to the data structure.
|
|
13132
|
+
*/
|
|
13133
|
+
addMany(words) {
|
|
13134
|
+
const ans = [];
|
|
13135
|
+
for (const word of words) {
|
|
13136
|
+
if (this.toElementFn) {
|
|
13137
|
+
ans.push(this.add(this.toElementFn(word)));
|
|
13138
|
+
} else {
|
|
13139
|
+
ans.push(this.add(word));
|
|
13140
|
+
}
|
|
13141
|
+
}
|
|
13142
|
+
return ans;
|
|
13143
|
+
}
|
|
12724
13144
|
/**
|
|
12725
13145
|
* Time Complexity: O(l), where l is the length of the input word.
|
|
12726
13146
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -12802,9 +13222,14 @@ var dataStructureTyped = (() => {
|
|
|
12802
13222
|
return isDeleted;
|
|
12803
13223
|
}
|
|
12804
13224
|
/**
|
|
12805
|
-
* Time Complexity: O(n)
|
|
12806
|
-
* Space Complexity: O(1)
|
|
13225
|
+
* Time Complexity: O(n)
|
|
13226
|
+
* Space Complexity: O(1)
|
|
12807
13227
|
*
|
|
13228
|
+
* The function `getHeight` calculates the height of a trie data structure starting from the root
|
|
13229
|
+
* node.
|
|
13230
|
+
* @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
|
|
13231
|
+
* the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
|
|
13232
|
+
* tree and returns the maximum depth found.
|
|
12808
13233
|
*/
|
|
12809
13234
|
getHeight() {
|
|
12810
13235
|
const startNode = this.root;
|