data-structure-typed 1.47.6 → 1.47.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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +188 -32
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/cjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- 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 +21 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +13 -13
- package/dist/cjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/cjs/data-structures/stack/stack.js +7 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +6 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +30 -30
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/mjs/data-structures/graph/directed-graph.js +28 -28
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/mjs/data-structures/graph/undirected-graph.js +18 -18
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +13 -13
- package/dist/mjs/data-structures/stack/stack.d.ts +6 -6
- package/dist/mjs/data-structures/stack/stack.js +7 -7
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +6 -1
- package/dist/mjs/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/umd/data-structure-typed.js +583 -627
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/graph/abstract-graph.ts +46 -46
- package/src/data-structures/graph/directed-graph.ts +40 -40
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +158 -2
- package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/data-structures/graph/abstract-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +10 -10
- package/test/unit/data-structures/graph/undirected-graph.test.ts +3 -3
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -3
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +61 -5
- package/tsconfig-cjs.json +1 -1
|
@@ -517,13 +517,7 @@ var dataStructureTyped = (() => {
|
|
|
517
517
|
|
|
518
518
|
// src/data-structures/hash/hash-map.ts
|
|
519
519
|
var HashMap = class _HashMap {
|
|
520
|
-
|
|
521
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
522
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
523
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
524
|
-
*/
|
|
525
|
-
constructor(options = {
|
|
526
|
-
elements: [],
|
|
520
|
+
constructor(elements, options = {
|
|
527
521
|
hashFn: (key) => String(key),
|
|
528
522
|
objHashFn: (key) => key
|
|
529
523
|
}) {
|
|
@@ -537,7 +531,7 @@ var dataStructureTyped = (() => {
|
|
|
537
531
|
__publicField(this, "_size", 0);
|
|
538
532
|
this._sentinel = {};
|
|
539
533
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
540
|
-
const {
|
|
534
|
+
const { hashFn, objHashFn } = options;
|
|
541
535
|
this._hashFn = hashFn;
|
|
542
536
|
this._objHashFn = objHashFn;
|
|
543
537
|
if (elements) {
|
|
@@ -844,6 +838,9 @@ var dataStructureTyped = (() => {
|
|
|
844
838
|
node = node.next;
|
|
845
839
|
}
|
|
846
840
|
}
|
|
841
|
+
print() {
|
|
842
|
+
console.log([...this]);
|
|
843
|
+
}
|
|
847
844
|
/**
|
|
848
845
|
* Time Complexity: O(1)
|
|
849
846
|
* Space Complexity: O(1)
|
|
@@ -871,7 +868,7 @@ var dataStructureTyped = (() => {
|
|
|
871
868
|
// src/data-structures/linked-list/singly-linked-list.ts
|
|
872
869
|
var SinglyLinkedListNode = class {
|
|
873
870
|
/**
|
|
874
|
-
* The constructor function initializes an instance of a class with a given value and sets the next property to
|
|
871
|
+
* The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
|
|
875
872
|
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
876
873
|
* will be stored in the node of a linked list.
|
|
877
874
|
*/
|
|
@@ -879,7 +876,7 @@ var dataStructureTyped = (() => {
|
|
|
879
876
|
__publicField(this, "value");
|
|
880
877
|
__publicField(this, "next");
|
|
881
878
|
this.value = value;
|
|
882
|
-
this.next =
|
|
879
|
+
this.next = void 0;
|
|
883
880
|
}
|
|
884
881
|
};
|
|
885
882
|
var SinglyLinkedList = class _SinglyLinkedList {
|
|
@@ -890,8 +887,8 @@ var dataStructureTyped = (() => {
|
|
|
890
887
|
__publicField(this, "_head");
|
|
891
888
|
__publicField(this, "_tail");
|
|
892
889
|
__publicField(this, "_length");
|
|
893
|
-
this._head =
|
|
894
|
-
this._tail =
|
|
890
|
+
this._head = void 0;
|
|
891
|
+
this._tail = void 0;
|
|
895
892
|
this._length = 0;
|
|
896
893
|
if (elements) {
|
|
897
894
|
for (const el of elements)
|
|
@@ -976,15 +973,15 @@ var dataStructureTyped = (() => {
|
|
|
976
973
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
977
974
|
* pointers accordingly.
|
|
978
975
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
979
|
-
* the linked list is empty, it returns `
|
|
976
|
+
* the linked list is empty, it returns `undefined`.
|
|
980
977
|
*/
|
|
981
978
|
pop() {
|
|
982
979
|
if (!this.head)
|
|
983
980
|
return void 0;
|
|
984
981
|
if (this.head === this.tail) {
|
|
985
982
|
const value2 = this.head.value;
|
|
986
|
-
this._head =
|
|
987
|
-
this._tail =
|
|
983
|
+
this._head = void 0;
|
|
984
|
+
this._tail = void 0;
|
|
988
985
|
this._length--;
|
|
989
986
|
return value2;
|
|
990
987
|
}
|
|
@@ -993,7 +990,7 @@ var dataStructureTyped = (() => {
|
|
|
993
990
|
current = current.next;
|
|
994
991
|
}
|
|
995
992
|
const value = this.tail.value;
|
|
996
|
-
current.next =
|
|
993
|
+
current.next = void 0;
|
|
997
994
|
this._tail = current;
|
|
998
995
|
this._length--;
|
|
999
996
|
return value;
|
|
@@ -1009,7 +1006,7 @@ var dataStructureTyped = (() => {
|
|
|
1009
1006
|
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
1010
1007
|
* pointers accordingly.
|
|
1011
1008
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
1012
|
-
* the linked list is empty, it returns `
|
|
1009
|
+
* the linked list is empty, it returns `undefined`.
|
|
1013
1010
|
*/
|
|
1014
1011
|
popLast() {
|
|
1015
1012
|
return this.pop();
|
|
@@ -1093,11 +1090,11 @@ var dataStructureTyped = (() => {
|
|
|
1093
1090
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
1094
1091
|
* Space Complexity: O(1) - Constant space.
|
|
1095
1092
|
*
|
|
1096
|
-
* The function `getAt` returns the value at a specified index in a linked list, or
|
|
1093
|
+
* The function `getAt` returns the value at a specified index in a linked list, or undefined if the index is out of range.
|
|
1097
1094
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
1098
1095
|
* retrieve from the list.
|
|
1099
|
-
* @returns The method `getAt(index: number): E |
|
|
1100
|
-
* `
|
|
1096
|
+
* @returns The method `getAt(index: number): E | undefined` returns the value at the specified index in the linked list, or
|
|
1097
|
+
* `undefined` if the index is out of bounds.
|
|
1101
1098
|
*/
|
|
1102
1099
|
getAt(index) {
|
|
1103
1100
|
if (index < 0 || index >= this.length)
|
|
@@ -1120,7 +1117,7 @@ var dataStructureTyped = (() => {
|
|
|
1120
1117
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
1121
1118
|
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
|
1122
1119
|
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<E>` object if the node at the
|
|
1123
|
-
* specified index exists, or `
|
|
1120
|
+
* specified index exists, or `undefined` if the index is out of bounds.
|
|
1124
1121
|
*/
|
|
1125
1122
|
getNodeAt(index) {
|
|
1126
1123
|
let current = this.head;
|
|
@@ -1140,7 +1137,7 @@ var dataStructureTyped = (() => {
|
|
|
1140
1137
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
1141
1138
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
1142
1139
|
* data structure. It is of type number.
|
|
1143
|
-
* @returns The method `deleteAt` returns the value of the node that was deleted, or `
|
|
1140
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
1144
1141
|
* bounds.
|
|
1145
1142
|
*/
|
|
1146
1143
|
deleteAt(index) {
|
|
@@ -1179,13 +1176,13 @@ var dataStructureTyped = (() => {
|
|
|
1179
1176
|
} else {
|
|
1180
1177
|
value = valueOrNode;
|
|
1181
1178
|
}
|
|
1182
|
-
let current = this.head, prev =
|
|
1179
|
+
let current = this.head, prev = void 0;
|
|
1183
1180
|
while (current) {
|
|
1184
1181
|
if (current.value === value) {
|
|
1185
|
-
if (prev ===
|
|
1182
|
+
if (prev === void 0) {
|
|
1186
1183
|
this._head = current.next;
|
|
1187
1184
|
if (current === this.tail) {
|
|
1188
|
-
this._tail =
|
|
1185
|
+
this._tail = void 0;
|
|
1189
1186
|
}
|
|
1190
1187
|
} else {
|
|
1191
1188
|
prev.next = current.next;
|
|
@@ -1244,11 +1241,11 @@ var dataStructureTyped = (() => {
|
|
|
1244
1241
|
return this.length === 0;
|
|
1245
1242
|
}
|
|
1246
1243
|
/**
|
|
1247
|
-
* The `clear` function resets the linked list by setting the head, tail, and length to
|
|
1244
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
1248
1245
|
*/
|
|
1249
1246
|
clear() {
|
|
1250
|
-
this._head =
|
|
1251
|
-
this._tail =
|
|
1247
|
+
this._head = void 0;
|
|
1248
|
+
this._tail = void 0;
|
|
1252
1249
|
this._length = 0;
|
|
1253
1250
|
}
|
|
1254
1251
|
/**
|
|
@@ -1285,9 +1282,9 @@ var dataStructureTyped = (() => {
|
|
|
1285
1282
|
reverse() {
|
|
1286
1283
|
if (!this.head || this.head === this.tail)
|
|
1287
1284
|
return;
|
|
1288
|
-
let prev =
|
|
1285
|
+
let prev = void 0;
|
|
1289
1286
|
let current = this.head;
|
|
1290
|
-
let next =
|
|
1287
|
+
let next = void 0;
|
|
1291
1288
|
while (current) {
|
|
1292
1289
|
next = current.next;
|
|
1293
1290
|
current.next = prev;
|
|
@@ -1308,7 +1305,7 @@ var dataStructureTyped = (() => {
|
|
|
1308
1305
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
1309
1306
|
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
1310
1307
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
1311
|
-
* the callback function. If no element satisfies the condition, it returns `
|
|
1308
|
+
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
1312
1309
|
*/
|
|
1313
1310
|
find(callback) {
|
|
1314
1311
|
let current = this.head;
|
|
@@ -1318,7 +1315,7 @@ var dataStructureTyped = (() => {
|
|
|
1318
1315
|
}
|
|
1319
1316
|
current = current.next;
|
|
1320
1317
|
}
|
|
1321
|
-
return
|
|
1318
|
+
return void 0;
|
|
1322
1319
|
}
|
|
1323
1320
|
/**
|
|
1324
1321
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
@@ -1354,10 +1351,10 @@ var dataStructureTyped = (() => {
|
|
|
1354
1351
|
* Space Complexity: O(1) - Constant space.
|
|
1355
1352
|
*
|
|
1356
1353
|
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
1357
|
-
*
|
|
1354
|
+
* undefined.
|
|
1358
1355
|
* @param {E} value - The value parameter is the value that we want to search for in the linked list.
|
|
1359
1356
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
1360
|
-
* the specified value is found, the function returns `
|
|
1357
|
+
* the specified value is found, the function returns `undefined`.
|
|
1361
1358
|
*/
|
|
1362
1359
|
getNode(value) {
|
|
1363
1360
|
let current = this.head;
|
|
@@ -1367,7 +1364,7 @@ var dataStructureTyped = (() => {
|
|
|
1367
1364
|
}
|
|
1368
1365
|
current = current.next;
|
|
1369
1366
|
}
|
|
1370
|
-
return
|
|
1367
|
+
return void 0;
|
|
1371
1368
|
}
|
|
1372
1369
|
/**
|
|
1373
1370
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
@@ -1589,8 +1586,8 @@ var dataStructureTyped = (() => {
|
|
|
1589
1586
|
__publicField(this, "next");
|
|
1590
1587
|
__publicField(this, "prev");
|
|
1591
1588
|
this.value = value;
|
|
1592
|
-
this.next =
|
|
1593
|
-
this.prev =
|
|
1589
|
+
this.next = void 0;
|
|
1590
|
+
this.prev = void 0;
|
|
1594
1591
|
}
|
|
1595
1592
|
};
|
|
1596
1593
|
var DoublyLinkedList = class _DoublyLinkedList {
|
|
@@ -1601,8 +1598,8 @@ var dataStructureTyped = (() => {
|
|
|
1601
1598
|
__publicField(this, "_head");
|
|
1602
1599
|
__publicField(this, "_tail");
|
|
1603
1600
|
__publicField(this, "_length");
|
|
1604
|
-
this._head =
|
|
1605
|
-
this._tail =
|
|
1601
|
+
this._head = void 0;
|
|
1602
|
+
this._tail = void 0;
|
|
1606
1603
|
this._length = 0;
|
|
1607
1604
|
if (elements) {
|
|
1608
1605
|
for (const el of elements) {
|
|
@@ -1689,18 +1686,18 @@ var dataStructureTyped = (() => {
|
|
|
1689
1686
|
*
|
|
1690
1687
|
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
1691
1688
|
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
1692
|
-
* list is empty, it returns
|
|
1689
|
+
* list is empty, it returns undefined.
|
|
1693
1690
|
*/
|
|
1694
1691
|
pop() {
|
|
1695
1692
|
if (!this.tail)
|
|
1696
1693
|
return void 0;
|
|
1697
1694
|
const removedNode = this.tail;
|
|
1698
1695
|
if (this.head === this.tail) {
|
|
1699
|
-
this._head =
|
|
1700
|
-
this._tail =
|
|
1696
|
+
this._head = void 0;
|
|
1697
|
+
this._tail = void 0;
|
|
1701
1698
|
} else {
|
|
1702
1699
|
this._tail = removedNode.prev;
|
|
1703
|
-
this.tail.next =
|
|
1700
|
+
this.tail.next = void 0;
|
|
1704
1701
|
}
|
|
1705
1702
|
this._length--;
|
|
1706
1703
|
return removedNode.value;
|
|
@@ -1715,7 +1712,7 @@ var dataStructureTyped = (() => {
|
|
|
1715
1712
|
*
|
|
1716
1713
|
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
1717
1714
|
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
1718
|
-
* list is empty, it returns
|
|
1715
|
+
* list is empty, it returns undefined.
|
|
1719
1716
|
*/
|
|
1720
1717
|
popLast() {
|
|
1721
1718
|
return this.pop();
|
|
@@ -1737,11 +1734,11 @@ var dataStructureTyped = (() => {
|
|
|
1737
1734
|
return void 0;
|
|
1738
1735
|
const removedNode = this.head;
|
|
1739
1736
|
if (this.head === this.tail) {
|
|
1740
|
-
this._head =
|
|
1741
|
-
this._tail =
|
|
1737
|
+
this._head = void 0;
|
|
1738
|
+
this._tail = void 0;
|
|
1742
1739
|
} else {
|
|
1743
1740
|
this._head = removedNode.next;
|
|
1744
|
-
this.head.prev =
|
|
1741
|
+
this.head.prev = void 0;
|
|
1745
1742
|
}
|
|
1746
1743
|
this._length--;
|
|
1747
1744
|
return removedNode.value;
|
|
@@ -1808,8 +1805,8 @@ var dataStructureTyped = (() => {
|
|
|
1808
1805
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1809
1806
|
* Space Complexity: O(1)
|
|
1810
1807
|
*
|
|
1811
|
-
* The `getFirst` function returns the first node in a doubly linked list, or
|
|
1812
|
-
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `
|
|
1808
|
+
* The `getFirst` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
1809
|
+
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
1813
1810
|
*/
|
|
1814
1811
|
getFirst() {
|
|
1815
1812
|
var _a;
|
|
@@ -1823,8 +1820,8 @@ var dataStructureTyped = (() => {
|
|
|
1823
1820
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1824
1821
|
* Space Complexity: O(1)
|
|
1825
1822
|
*
|
|
1826
|
-
* The `getLast` function returns the last node in a doubly linked list, or
|
|
1827
|
-
* @returns The method `getLast()` returns the last node of the doubly linked list, or `
|
|
1823
|
+
* The `getLast` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
1824
|
+
* @returns The method `getLast()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
1828
1825
|
*/
|
|
1829
1826
|
getLast() {
|
|
1830
1827
|
var _a;
|
|
@@ -1838,11 +1835,11 @@ var dataStructureTyped = (() => {
|
|
|
1838
1835
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1839
1836
|
* Space Complexity: O(1)
|
|
1840
1837
|
*
|
|
1841
|
-
* The `getAt` function returns the value at a specified index in a linked list, or
|
|
1838
|
+
* The `getAt` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
|
|
1842
1839
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
1843
1840
|
* retrieve from the list.
|
|
1844
1841
|
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
|
1845
|
-
* or the linked list is empty, it will return
|
|
1842
|
+
* or the linked list is empty, it will return undefined.
|
|
1846
1843
|
*/
|
|
1847
1844
|
getAt(index) {
|
|
1848
1845
|
if (index < 0 || index >= this.length)
|
|
@@ -1861,16 +1858,16 @@ var dataStructureTyped = (() => {
|
|
|
1861
1858
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
1862
1859
|
* Space Complexity: O(1)
|
|
1863
1860
|
*
|
|
1864
|
-
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or
|
|
1861
|
+
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or undefined if the index is out of
|
|
1865
1862
|
* range.
|
|
1866
1863
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
1867
1864
|
* retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
|
|
1868
1865
|
* @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<E>` object if the index is within the
|
|
1869
|
-
* valid range of the linked list, otherwise it returns `
|
|
1866
|
+
* valid range of the linked list, otherwise it returns `undefined`.
|
|
1870
1867
|
*/
|
|
1871
1868
|
getNodeAt(index) {
|
|
1872
1869
|
if (index < 0 || index >= this.length)
|
|
1873
|
-
return
|
|
1870
|
+
return void 0;
|
|
1874
1871
|
let current = this.head;
|
|
1875
1872
|
for (let i = 0; i < index; i++) {
|
|
1876
1873
|
current = current.next;
|
|
@@ -1886,10 +1883,10 @@ var dataStructureTyped = (() => {
|
|
|
1886
1883
|
* Space Complexity: O(1)
|
|
1887
1884
|
*
|
|
1888
1885
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
1889
|
-
* node if found, otherwise it returns
|
|
1886
|
+
* node if found, otherwise it returns undefined.
|
|
1890
1887
|
* @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
|
|
1891
1888
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
|
|
1892
|
-
* is found in the linked list. If no such node is found, it returns `
|
|
1889
|
+
* is found in the linked list. If no such node is found, it returns `undefined`.
|
|
1893
1890
|
*/
|
|
1894
1891
|
getNode(value) {
|
|
1895
1892
|
let current = this.head;
|
|
@@ -1899,7 +1896,7 @@ var dataStructureTyped = (() => {
|
|
|
1899
1896
|
}
|
|
1900
1897
|
current = current.next;
|
|
1901
1898
|
}
|
|
1902
|
-
return
|
|
1899
|
+
return void 0;
|
|
1903
1900
|
}
|
|
1904
1901
|
/**
|
|
1905
1902
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2028,7 +2025,7 @@ var dataStructureTyped = (() => {
|
|
|
2028
2025
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
2029
2026
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
2030
2027
|
* data structure. It is of type number.
|
|
2031
|
-
* @returns The method `deleteAt` returns the value of the node that was deleted, or `
|
|
2028
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
2032
2029
|
* bounds.
|
|
2033
2030
|
*/
|
|
2034
2031
|
deleteAt(index) {
|
|
@@ -2091,11 +2088,11 @@ var dataStructureTyped = (() => {
|
|
|
2091
2088
|
return this.length === 0;
|
|
2092
2089
|
}
|
|
2093
2090
|
/**
|
|
2094
|
-
* The `clear` function resets the linked list by setting the head, tail, and length to
|
|
2091
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
2095
2092
|
*/
|
|
2096
2093
|
clear() {
|
|
2097
|
-
this._head =
|
|
2098
|
-
this._tail =
|
|
2094
|
+
this._head = void 0;
|
|
2095
|
+
this._tail = void 0;
|
|
2099
2096
|
this._length = 0;
|
|
2100
2097
|
}
|
|
2101
2098
|
/**
|
|
@@ -2110,7 +2107,7 @@ var dataStructureTyped = (() => {
|
|
|
2110
2107
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
2111
2108
|
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
2112
2109
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
2113
|
-
* the callback function. If no element satisfies the condition, it returns `
|
|
2110
|
+
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
2114
2111
|
*/
|
|
2115
2112
|
find(callback) {
|
|
2116
2113
|
let current = this.head;
|
|
@@ -2120,7 +2117,7 @@ var dataStructureTyped = (() => {
|
|
|
2120
2117
|
}
|
|
2121
2118
|
current = current.next;
|
|
2122
2119
|
}
|
|
2123
|
-
return
|
|
2120
|
+
return void 0;
|
|
2124
2121
|
}
|
|
2125
2122
|
/**
|
|
2126
2123
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2157,11 +2154,11 @@ var dataStructureTyped = (() => {
|
|
|
2157
2154
|
* Space Complexity: O(1)
|
|
2158
2155
|
*
|
|
2159
2156
|
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
2160
|
-
* value that satisfies the given callback function, or
|
|
2157
|
+
* value that satisfies the given callback function, or undefined if no value satisfies the callback.
|
|
2161
2158
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
2162
2159
|
* function is used to determine whether a given value satisfies a certain condition.
|
|
2163
2160
|
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
2164
|
-
* the callback function. If no value satisfies the condition, it returns `
|
|
2161
|
+
* the callback function. If no value satisfies the condition, it returns `undefined`.
|
|
2165
2162
|
*/
|
|
2166
2163
|
findBackward(callback) {
|
|
2167
2164
|
let current = this.tail;
|
|
@@ -2171,7 +2168,7 @@ var dataStructureTyped = (() => {
|
|
|
2171
2168
|
}
|
|
2172
2169
|
current = current.prev;
|
|
2173
2170
|
}
|
|
2174
|
-
return
|
|
2171
|
+
return void 0;
|
|
2175
2172
|
}
|
|
2176
2173
|
/**
|
|
2177
2174
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2366,7 +2363,7 @@ var dataStructureTyped = (() => {
|
|
|
2366
2363
|
__publicField(this, "_level");
|
|
2367
2364
|
__publicField(this, "_maxLevel");
|
|
2368
2365
|
__publicField(this, "_probability");
|
|
2369
|
-
this._head = new SkipListNode(
|
|
2366
|
+
this._head = new SkipListNode(void 0, void 0, maxLevel);
|
|
2370
2367
|
this._level = 0;
|
|
2371
2368
|
this._maxLevel = maxLevel;
|
|
2372
2369
|
this._probability = probability;
|
|
@@ -2410,7 +2407,7 @@ var dataStructureTyped = (() => {
|
|
|
2410
2407
|
newNode.forward[i] = update[i].forward[i];
|
|
2411
2408
|
update[i].forward[i] = newNode;
|
|
2412
2409
|
}
|
|
2413
|
-
if (newNode.forward[0]
|
|
2410
|
+
if (!newNode.forward[0]) {
|
|
2414
2411
|
this._level = Math.max(this.level, newNode.forward.length);
|
|
2415
2412
|
}
|
|
2416
2413
|
}
|
|
@@ -2481,7 +2478,7 @@ var dataStructureTyped = (() => {
|
|
|
2481
2478
|
}
|
|
2482
2479
|
update[i].forward[i] = current.forward[i];
|
|
2483
2480
|
}
|
|
2484
|
-
while (this.level > 0 && this.head.forward[this.level - 1]
|
|
2481
|
+
while (this.level > 0 && !this.head.forward[this.level - 1]) {
|
|
2485
2482
|
this._level--;
|
|
2486
2483
|
}
|
|
2487
2484
|
return true;
|
|
@@ -2559,7 +2556,7 @@ var dataStructureTyped = (() => {
|
|
|
2559
2556
|
*/
|
|
2560
2557
|
lower(key) {
|
|
2561
2558
|
let current = this.head;
|
|
2562
|
-
let lastLess =
|
|
2559
|
+
let lastLess = void 0;
|
|
2563
2560
|
for (let i = this.level - 1; i >= 0; i--) {
|
|
2564
2561
|
while (current.forward[i] && current.forward[i].key < key) {
|
|
2565
2562
|
current = current.forward[i];
|
|
@@ -2648,12 +2645,12 @@ var dataStructureTyped = (() => {
|
|
|
2648
2645
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
2649
2646
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
2650
2647
|
*
|
|
2651
|
-
* The `peek` function returns the last element of an array, or
|
|
2652
|
-
* @returns The `peek()` function returns the last element of the `_elements` array, or `
|
|
2648
|
+
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
2649
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
2653
2650
|
*/
|
|
2654
2651
|
peek() {
|
|
2655
2652
|
if (this.isEmpty())
|
|
2656
|
-
return
|
|
2653
|
+
return void 0;
|
|
2657
2654
|
return this.elements[this.elements.length - 1];
|
|
2658
2655
|
}
|
|
2659
2656
|
/**
|
|
@@ -2680,14 +2677,14 @@ var dataStructureTyped = (() => {
|
|
|
2680
2677
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
2681
2678
|
* Space Complexity: O(1), as it does not use any additional space.
|
|
2682
2679
|
*
|
|
2683
|
-
* The `pop` function removes and returns the last element from an array, or returns
|
|
2680
|
+
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
2684
2681
|
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
2685
|
-
* array is empty, it returns `
|
|
2682
|
+
* array is empty, it returns `undefined`.
|
|
2686
2683
|
*/
|
|
2687
2684
|
pop() {
|
|
2688
2685
|
if (this.isEmpty())
|
|
2689
|
-
return
|
|
2690
|
-
return this.elements.pop() ||
|
|
2686
|
+
return void 0;
|
|
2687
|
+
return this.elements.pop() || void 0;
|
|
2691
2688
|
}
|
|
2692
2689
|
/**
|
|
2693
2690
|
* Time Complexity: O(n)
|
|
@@ -2787,8 +2784,8 @@ var dataStructureTyped = (() => {
|
|
|
2787
2784
|
this.push(value);
|
|
2788
2785
|
}
|
|
2789
2786
|
/**
|
|
2790
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns
|
|
2791
|
-
* @returns The method is returning the element at the front of the queue, or
|
|
2787
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
2788
|
+
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
2792
2789
|
*/
|
|
2793
2790
|
dequeue() {
|
|
2794
2791
|
return this.shift();
|
|
@@ -2872,7 +2869,7 @@ var dataStructureTyped = (() => {
|
|
|
2872
2869
|
*
|
|
2873
2870
|
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
2874
2871
|
* necessary to optimize performance.
|
|
2875
|
-
* @returns The function `shift()` returns either the first element in the queue or `
|
|
2872
|
+
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
2876
2873
|
*/
|
|
2877
2874
|
shift() {
|
|
2878
2875
|
if (this.size === 0)
|
|
@@ -2893,9 +2890,9 @@ var dataStructureTyped = (() => {
|
|
|
2893
2890
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
2894
2891
|
* Space Complexity: O(1) - no additional space is used.
|
|
2895
2892
|
*
|
|
2896
|
-
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `
|
|
2893
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
2897
2894
|
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
2898
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `
|
|
2895
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
2899
2896
|
*/
|
|
2900
2897
|
getFirst() {
|
|
2901
2898
|
return this.size > 0 ? this.nodes[this.offset] : void 0;
|
|
@@ -2908,9 +2905,9 @@ var dataStructureTyped = (() => {
|
|
|
2908
2905
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
2909
2906
|
* Space Complexity: O(1) - no additional space is used.
|
|
2910
2907
|
*
|
|
2911
|
-
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `
|
|
2908
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
2912
2909
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
2913
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `
|
|
2910
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
2914
2911
|
*/
|
|
2915
2912
|
peek() {
|
|
2916
2913
|
return this.getFirst();
|
|
@@ -2923,9 +2920,9 @@ var dataStructureTyped = (() => {
|
|
|
2923
2920
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
2924
2921
|
* Space Complexity: O(1) - no additional space is used.
|
|
2925
2922
|
*
|
|
2926
|
-
* The `getLast` function returns the last element in an array-like data structure, or
|
|
2923
|
+
* The `getLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
2927
2924
|
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
2928
|
-
* array is empty, it returns `
|
|
2925
|
+
* array is empty, it returns `undefined`.
|
|
2929
2926
|
*/
|
|
2930
2927
|
getLast() {
|
|
2931
2928
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : void 0;
|
|
@@ -2938,9 +2935,9 @@ var dataStructureTyped = (() => {
|
|
|
2938
2935
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
2939
2936
|
* Space Complexity: O(1) - no additional space is used.
|
|
2940
2937
|
*
|
|
2941
|
-
* The `peekLast` function returns the last element in an array-like data structure, or
|
|
2938
|
+
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
2942
2939
|
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
2943
|
-
* array is empty, it returns `
|
|
2940
|
+
* array is empty, it returns `undefined`.
|
|
2944
2941
|
*/
|
|
2945
2942
|
peekLast() {
|
|
2946
2943
|
return this.getLast();
|
|
@@ -2967,8 +2964,8 @@ var dataStructureTyped = (() => {
|
|
|
2967
2964
|
* Time Complexity: O(n) - same as shift().
|
|
2968
2965
|
* Space Complexity: O(1) - same as shift().
|
|
2969
2966
|
*
|
|
2970
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns
|
|
2971
|
-
* @returns The method is returning a value of type E or
|
|
2967
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
2968
|
+
* @returns The method is returning a value of type E or undefined.
|
|
2972
2969
|
*/
|
|
2973
2970
|
dequeue() {
|
|
2974
2971
|
return this.shift();
|
|
@@ -4968,14 +4965,14 @@ var dataStructureTyped = (() => {
|
|
|
4968
4965
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
4969
4966
|
* Space Complexity: O(1) - Constant space, as it creates only a few variables.
|
|
4970
4967
|
*
|
|
4971
|
-
* The function "getVertex" returns the vertex with the specified ID or
|
|
4968
|
+
* The function "getVertex" returns the vertex with the specified ID or undefined if it doesn't exist.
|
|
4972
4969
|
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
4973
4970
|
* the `_vertices` map.
|
|
4974
4971
|
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertices`
|
|
4975
|
-
* map. If the vertex does not exist, it returns `
|
|
4972
|
+
* map. If the vertex does not exist, it returns `undefined`.
|
|
4976
4973
|
*/
|
|
4977
4974
|
getVertex(vertexKey) {
|
|
4978
|
-
return this._vertices.get(vertexKey) ||
|
|
4975
|
+
return this._vertices.get(vertexKey) || void 0;
|
|
4979
4976
|
}
|
|
4980
4977
|
/**
|
|
4981
4978
|
* Time Complexity: O(1) - Constant time for Map lookup.
|
|
@@ -5207,7 +5204,7 @@ var dataStructureTyped = (() => {
|
|
|
5207
5204
|
const vertex2 = this._getVertex(v2);
|
|
5208
5205
|
const vertex1 = this._getVertex(v1);
|
|
5209
5206
|
if (!(vertex1 && vertex2)) {
|
|
5210
|
-
return
|
|
5207
|
+
return void 0;
|
|
5211
5208
|
}
|
|
5212
5209
|
const visited = /* @__PURE__ */ new Map();
|
|
5213
5210
|
const queue = new Queue([vertex1]);
|
|
@@ -5231,7 +5228,7 @@ var dataStructureTyped = (() => {
|
|
|
5231
5228
|
}
|
|
5232
5229
|
cost++;
|
|
5233
5230
|
}
|
|
5234
|
-
return
|
|
5231
|
+
return void 0;
|
|
5235
5232
|
}
|
|
5236
5233
|
}
|
|
5237
5234
|
/**
|
|
@@ -5255,7 +5252,7 @@ var dataStructureTyped = (() => {
|
|
|
5255
5252
|
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
5256
5253
|
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
5257
5254
|
* @returns The function `getMinPathBetween` returns an array of vertices (`VO[]`) representing the minimum path between
|
|
5258
|
-
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `
|
|
5255
|
+
* two vertices (`v1` and `v2`). If there is no path between the vertices, it returns `undefined`.
|
|
5259
5256
|
*/
|
|
5260
5257
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
5261
5258
|
var _a, _b;
|
|
@@ -5275,7 +5272,7 @@ var dataStructureTyped = (() => {
|
|
|
5275
5272
|
}
|
|
5276
5273
|
index++;
|
|
5277
5274
|
}
|
|
5278
|
-
return allPaths[minIndex] ||
|
|
5275
|
+
return allPaths[minIndex] || void 0;
|
|
5279
5276
|
} else {
|
|
5280
5277
|
return (_b = (_a = this.dijkstra(v1, v2, true, true)) == null ? void 0 : _a.minPath) != null ? _b : [];
|
|
5281
5278
|
}
|
|
@@ -5321,9 +5318,9 @@ var dataStructureTyped = (() => {
|
|
|
5321
5318
|
* a graph without using a heap data structure.
|
|
5322
5319
|
* @param {VO | VertexKey} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
|
|
5323
5320
|
* vertex object or a vertex ID.
|
|
5324
|
-
* @param {VO | VertexKey |
|
|
5321
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
5325
5322
|
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
5326
|
-
* identifier. If no destination is provided, the value is set to `
|
|
5323
|
+
* identifier. If no destination is provided, the value is set to `undefined`.
|
|
5327
5324
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
5328
5325
|
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
5329
5326
|
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
@@ -5338,9 +5335,9 @@ var dataStructureTyped = (() => {
|
|
|
5338
5335
|
if (genPaths === void 0)
|
|
5339
5336
|
genPaths = false;
|
|
5340
5337
|
if (dest === void 0)
|
|
5341
|
-
dest =
|
|
5338
|
+
dest = void 0;
|
|
5342
5339
|
let minDist = Infinity;
|
|
5343
|
-
let minDest =
|
|
5340
|
+
let minDest = void 0;
|
|
5344
5341
|
let minPath = [];
|
|
5345
5342
|
const paths = [];
|
|
5346
5343
|
const vertices = this._vertices;
|
|
@@ -5348,9 +5345,9 @@ var dataStructureTyped = (() => {
|
|
|
5348
5345
|
const seen = /* @__PURE__ */ new Set();
|
|
5349
5346
|
const preMap = /* @__PURE__ */ new Map();
|
|
5350
5347
|
const srcVertex = this._getVertex(src);
|
|
5351
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
5348
|
+
const destVertex = dest ? this._getVertex(dest) : void 0;
|
|
5352
5349
|
if (!srcVertex) {
|
|
5353
|
-
return
|
|
5350
|
+
return void 0;
|
|
5354
5351
|
}
|
|
5355
5352
|
for (const vertex of vertices) {
|
|
5356
5353
|
const vertexOrKey = vertex[1];
|
|
@@ -5358,10 +5355,10 @@ var dataStructureTyped = (() => {
|
|
|
5358
5355
|
distMap.set(vertexOrKey, Infinity);
|
|
5359
5356
|
}
|
|
5360
5357
|
distMap.set(srcVertex, 0);
|
|
5361
|
-
preMap.set(srcVertex,
|
|
5358
|
+
preMap.set(srcVertex, void 0);
|
|
5362
5359
|
const getMinOfNoSeen = () => {
|
|
5363
5360
|
let min = Infinity;
|
|
5364
|
-
let minV =
|
|
5361
|
+
let minV = void 0;
|
|
5365
5362
|
for (const [key, value] of distMap) {
|
|
5366
5363
|
if (!seen.has(key)) {
|
|
5367
5364
|
if (value < min) {
|
|
@@ -5454,7 +5451,7 @@ var dataStructureTyped = (() => {
|
|
|
5454
5451
|
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
5455
5452
|
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
5456
5453
|
* start. It can be either a vertex object or a vertex ID.
|
|
5457
|
-
* @param {VO | VertexKey |
|
|
5454
|
+
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
5458
5455
|
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
5459
5456
|
* will calculate the shortest paths to all other vertices from the source vertex.
|
|
5460
5457
|
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
@@ -5472,9 +5469,9 @@ var dataStructureTyped = (() => {
|
|
|
5472
5469
|
if (genPaths === void 0)
|
|
5473
5470
|
genPaths = false;
|
|
5474
5471
|
if (dest === void 0)
|
|
5475
|
-
dest =
|
|
5472
|
+
dest = void 0;
|
|
5476
5473
|
let minDist = Infinity;
|
|
5477
|
-
let minDest =
|
|
5474
|
+
let minDest = void 0;
|
|
5478
5475
|
let minPath = [];
|
|
5479
5476
|
const paths = [];
|
|
5480
5477
|
const vertices = this._vertices;
|
|
@@ -5482,9 +5479,9 @@ var dataStructureTyped = (() => {
|
|
|
5482
5479
|
const seen = /* @__PURE__ */ new Set();
|
|
5483
5480
|
const preMap = /* @__PURE__ */ new Map();
|
|
5484
5481
|
const srcVertex = this._getVertex(src);
|
|
5485
|
-
const destVertex = dest ? this._getVertex(dest) :
|
|
5482
|
+
const destVertex = dest ? this._getVertex(dest) : void 0;
|
|
5486
5483
|
if (!srcVertex)
|
|
5487
|
-
return
|
|
5484
|
+
return void 0;
|
|
5488
5485
|
for (const vertex of vertices) {
|
|
5489
5486
|
const vertexOrKey = vertex[1];
|
|
5490
5487
|
if (vertexOrKey instanceof AbstractVertex)
|
|
@@ -5493,7 +5490,7 @@ var dataStructureTyped = (() => {
|
|
|
5493
5490
|
const heap = new PriorityQueue([], { comparator: (a, b) => a.key - b.key });
|
|
5494
5491
|
heap.add({ key: 0, value: srcVertex });
|
|
5495
5492
|
distMap.set(srcVertex, 0);
|
|
5496
|
-
preMap.set(srcVertex,
|
|
5493
|
+
preMap.set(srcVertex, void 0);
|
|
5497
5494
|
const getPaths = (minV) => {
|
|
5498
5495
|
for (const vertex of vertices) {
|
|
5499
5496
|
const vertexOrKey = vertex[1];
|
|
@@ -5627,7 +5624,7 @@ var dataStructureTyped = (() => {
|
|
|
5627
5624
|
}
|
|
5628
5625
|
}
|
|
5629
5626
|
}
|
|
5630
|
-
let minDest =
|
|
5627
|
+
let minDest = void 0;
|
|
5631
5628
|
if (getMin) {
|
|
5632
5629
|
distMap.forEach((d, v) => {
|
|
5633
5630
|
if (v !== srcVertex) {
|
|
@@ -5703,7 +5700,7 @@ var dataStructureTyped = (() => {
|
|
|
5703
5700
|
* graph.
|
|
5704
5701
|
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
5705
5702
|
* property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
|
|
5706
|
-
* `predecessor` property is a 2D array of vertices (or `
|
|
5703
|
+
* `predecessor` property is a 2D array of vertices (or `undefined`) representing the predecessor vertices in the shortest
|
|
5707
5704
|
* path between vertices in the
|
|
5708
5705
|
*/
|
|
5709
5706
|
floydWarshall() {
|
|
@@ -5716,7 +5713,7 @@ var dataStructureTyped = (() => {
|
|
|
5716
5713
|
costs[i] = [];
|
|
5717
5714
|
predecessor[i] = [];
|
|
5718
5715
|
for (let j = 0; j < n; j++) {
|
|
5719
|
-
predecessor[i][j] =
|
|
5716
|
+
predecessor[i][j] = void 0;
|
|
5720
5717
|
}
|
|
5721
5718
|
}
|
|
5722
5719
|
for (let i = 0; i < n; i++) {
|
|
@@ -5827,7 +5824,7 @@ var dataStructureTyped = (() => {
|
|
|
5827
5824
|
}
|
|
5828
5825
|
}
|
|
5829
5826
|
};
|
|
5830
|
-
dfs(root,
|
|
5827
|
+
dfs(root, void 0);
|
|
5831
5828
|
let SCCs = /* @__PURE__ */ new Map();
|
|
5832
5829
|
const getSCCs = () => {
|
|
5833
5830
|
const SCCs2 = /* @__PURE__ */ new Map();
|
|
@@ -5920,7 +5917,7 @@ var dataStructureTyped = (() => {
|
|
|
5920
5917
|
}
|
|
5921
5918
|
_getVertex(vertexOrKey) {
|
|
5922
5919
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
5923
|
-
return this._vertices.get(vertexKey) ||
|
|
5920
|
+
return this._vertices.get(vertexKey) || void 0;
|
|
5924
5921
|
}
|
|
5925
5922
|
_getVertexKey(vertexOrKey) {
|
|
5926
5923
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
@@ -6017,15 +6014,15 @@ var dataStructureTyped = (() => {
|
|
|
6017
6014
|
* Space Complexity: O(1)
|
|
6018
6015
|
*
|
|
6019
6016
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
6020
|
-
* @param {VO | VertexKey |
|
|
6021
|
-
* @param {VO | VertexKey |
|
|
6022
|
-
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `
|
|
6017
|
+
* @param {VO | VertexKey | undefined} srcOrKey - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
6018
|
+
* @param {VO | VertexKey | undefined} destOrKey - The `destOrKey` parameter in the `getEdge` function represents the
|
|
6019
|
+
* destination vertex of the edge. It can be either a vertex object (`VO`), a vertex ID (`VertexKey`), or `undefined` if the
|
|
6023
6020
|
* destination is not specified.
|
|
6024
|
-
* @returns the first edge found between the source and destination vertices, or
|
|
6021
|
+
* @returns the first edge found between the source and destination vertices, or undefined if no such edge is found.
|
|
6025
6022
|
*/
|
|
6026
6023
|
getEdge(srcOrKey, destOrKey) {
|
|
6027
6024
|
let edges = [];
|
|
6028
|
-
if (srcOrKey !==
|
|
6025
|
+
if (srcOrKey !== void 0 && destOrKey !== void 0) {
|
|
6029
6026
|
const src = this._getVertex(srcOrKey);
|
|
6030
6027
|
const dest = this._getVertex(destOrKey);
|
|
6031
6028
|
if (src && dest) {
|
|
@@ -6035,7 +6032,7 @@ var dataStructureTyped = (() => {
|
|
|
6035
6032
|
}
|
|
6036
6033
|
}
|
|
6037
6034
|
}
|
|
6038
|
-
return edges[0] ||
|
|
6035
|
+
return edges[0] || void 0;
|
|
6039
6036
|
}
|
|
6040
6037
|
/**
|
|
6041
6038
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
@@ -6048,14 +6045,14 @@ var dataStructureTyped = (() => {
|
|
|
6048
6045
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
6049
6046
|
* @param {VO | VertexKey} srcOrKey - The source vertex or its ID.
|
|
6050
6047
|
* @param {VO | VertexKey} destOrKey - The `destOrKey` parameter represents the destination vertex or its ID.
|
|
6051
|
-
* @returns the removed edge (EO) if it exists, or
|
|
6048
|
+
* @returns the removed edge (EO) if it exists, or undefined if either the source or destination vertex does not exist.
|
|
6052
6049
|
*/
|
|
6053
6050
|
deleteEdgeSrcToDest(srcOrKey, destOrKey) {
|
|
6054
6051
|
const src = this._getVertex(srcOrKey);
|
|
6055
6052
|
const dest = this._getVertex(destOrKey);
|
|
6056
|
-
let removed =
|
|
6053
|
+
let removed = void 0;
|
|
6057
6054
|
if (!src || !dest) {
|
|
6058
|
-
return
|
|
6055
|
+
return void 0;
|
|
6059
6056
|
}
|
|
6060
6057
|
const srcOutEdges = this._outEdgeMap.get(src);
|
|
6061
6058
|
if (srcOutEdges) {
|
|
@@ -6063,7 +6060,7 @@ var dataStructureTyped = (() => {
|
|
|
6063
6060
|
}
|
|
6064
6061
|
const destInEdges = this._inEdgeMap.get(dest);
|
|
6065
6062
|
if (destInEdges) {
|
|
6066
|
-
removed = arrayRemove(destInEdges, (edge) => edge.src === src.key)[0] ||
|
|
6063
|
+
removed = arrayRemove(destInEdges, (edge) => edge.src === src.key)[0] || void 0;
|
|
6067
6064
|
}
|
|
6068
6065
|
return removed;
|
|
6069
6066
|
}
|
|
@@ -6075,13 +6072,13 @@ var dataStructureTyped = (() => {
|
|
|
6075
6072
|
* Time Complexity: O(|E|) where |E| is the number of edges
|
|
6076
6073
|
* Space Complexity: O(1)
|
|
6077
6074
|
*
|
|
6078
|
-
* The function removes an edge from a graph and returns the removed edge, or
|
|
6075
|
+
* The function removes an edge from a graph and returns the removed edge, or undefined if the edge was not found.
|
|
6079
6076
|
* @param {EO} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
6080
6077
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
6081
|
-
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `
|
|
6078
|
+
* @returns The method `deleteEdge` returns the removed edge (`EO`) if it exists, or `undefined` if the edge does not exist.
|
|
6082
6079
|
*/
|
|
6083
6080
|
deleteEdge(edge) {
|
|
6084
|
-
let removed =
|
|
6081
|
+
let removed = void 0;
|
|
6085
6082
|
const src = this._getVertex(edge.src);
|
|
6086
6083
|
const dest = this._getVertex(edge.dest);
|
|
6087
6084
|
if (src && dest) {
|
|
@@ -6229,9 +6226,9 @@ var dataStructureTyped = (() => {
|
|
|
6229
6226
|
* Time Complexity: O(1)
|
|
6230
6227
|
* Space Complexity: O(1)
|
|
6231
6228
|
*
|
|
6232
|
-
* The function "getEdgeSrc" returns the source vertex of an edge, or
|
|
6229
|
+
* The function "getEdgeSrc" returns the source vertex of an edge, or undefined if the edge does not exist.
|
|
6233
6230
|
* @param {EO} e - The parameter "e" is of type EO, which represents an edge in a graph.
|
|
6234
|
-
* @returns either a vertex object (VO) or
|
|
6231
|
+
* @returns either a vertex object (VO) or undefined.
|
|
6235
6232
|
*/
|
|
6236
6233
|
getEdgeSrc(e) {
|
|
6237
6234
|
return this._getVertex(e.src);
|
|
@@ -6246,7 +6243,7 @@ var dataStructureTyped = (() => {
|
|
|
6246
6243
|
*
|
|
6247
6244
|
* The function "getEdgeDest" returns the destination vertex of an edge.
|
|
6248
6245
|
* @param {EO} e - The parameter "e" is of type "EO", which represents an edge in a graph.
|
|
6249
|
-
* @returns either a vertex object of type VO or
|
|
6246
|
+
* @returns either a vertex object of type VO or undefined.
|
|
6250
6247
|
*/
|
|
6251
6248
|
getEdgeDest(e) {
|
|
6252
6249
|
return this._getVertex(e.dest);
|
|
@@ -6260,12 +6257,12 @@ var dataStructureTyped = (() => {
|
|
|
6260
6257
|
* Space Complexity: O(1)
|
|
6261
6258
|
*
|
|
6262
6259
|
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
6263
|
-
* @param {VO | VertexKey |
|
|
6264
|
-
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `
|
|
6260
|
+
* @param {VO | VertexKey | undefined} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
6261
|
+
* find the destinations. It can be either a `VO` object, a `VertexKey` value, or `undefined`.
|
|
6265
6262
|
* @returns an array of vertices (VO[]).
|
|
6266
6263
|
*/
|
|
6267
6264
|
getDestinations(vertex) {
|
|
6268
|
-
if (vertex ===
|
|
6265
|
+
if (vertex === void 0) {
|
|
6269
6266
|
return [];
|
|
6270
6267
|
}
|
|
6271
6268
|
const destinations = [];
|
|
@@ -6287,11 +6284,11 @@ var dataStructureTyped = (() => {
|
|
|
6287
6284
|
* Space Complexity: O(|V|)
|
|
6288
6285
|
*
|
|
6289
6286
|
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
6290
|
-
* in the sorted order, or
|
|
6287
|
+
* in the sorted order, or undefined if the graph contains a cycle.
|
|
6291
6288
|
* @param {'vertex' | 'key'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
6292
6289
|
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'key'. If 'vertex' is
|
|
6293
6290
|
* specified, the vertices themselves will be used for sorting. If 'key' is specified, the ids of
|
|
6294
|
-
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns
|
|
6291
|
+
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns undefined.
|
|
6295
6292
|
*/
|
|
6296
6293
|
topologicalSort(propertyName) {
|
|
6297
6294
|
propertyName = propertyName != null ? propertyName : "key";
|
|
@@ -6321,7 +6318,7 @@ var dataStructureTyped = (() => {
|
|
|
6321
6318
|
}
|
|
6322
6319
|
}
|
|
6323
6320
|
if (hasCycle)
|
|
6324
|
-
return
|
|
6321
|
+
return void 0;
|
|
6325
6322
|
if (propertyName === "key")
|
|
6326
6323
|
sorted = sorted.map((vertex) => vertex instanceof DirectedVertex ? vertex.key : vertex);
|
|
6327
6324
|
return sorted.reverse();
|
|
@@ -6380,21 +6377,21 @@ var dataStructureTyped = (() => {
|
|
|
6380
6377
|
* Space Complexity: O(1)
|
|
6381
6378
|
*
|
|
6382
6379
|
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
6383
|
-
* otherwise it returns
|
|
6380
|
+
* otherwise it returns undefined.
|
|
6384
6381
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph.
|
|
6385
6382
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
6386
|
-
* graph. If the edge does not exist, it returns `
|
|
6383
|
+
* graph. If the edge does not exist, it returns `undefined`.
|
|
6387
6384
|
*/
|
|
6388
6385
|
getEndsOfEdge(edge) {
|
|
6389
6386
|
if (!this.hasEdge(edge.src, edge.dest)) {
|
|
6390
|
-
return
|
|
6387
|
+
return void 0;
|
|
6391
6388
|
}
|
|
6392
6389
|
const v1 = this._getVertex(edge.src);
|
|
6393
6390
|
const v2 = this._getVertex(edge.dest);
|
|
6394
6391
|
if (v1 && v2) {
|
|
6395
6392
|
return [v1, v2];
|
|
6396
6393
|
} else {
|
|
6397
|
-
return
|
|
6394
|
+
return void 0;
|
|
6398
6395
|
}
|
|
6399
6396
|
}
|
|
6400
6397
|
/**
|
|
@@ -6512,24 +6509,24 @@ var dataStructureTyped = (() => {
|
|
|
6512
6509
|
* Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
|
|
6513
6510
|
* Space Complexity: O(1)
|
|
6514
6511
|
*
|
|
6515
|
-
* The function `getEdge` returns the first edge that connects two vertices, or
|
|
6516
|
-
* @param {VO | VertexKey |
|
|
6517
|
-
* object), `
|
|
6518
|
-
* @param {VO | VertexKey |
|
|
6519
|
-
* object), `
|
|
6520
|
-
* @returns an edge (EO) or
|
|
6512
|
+
* The function `getEdge` returns the first edge that connects two vertices, or undefined if no such edge exists.
|
|
6513
|
+
* @param {VO | VertexKey | undefined} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
6514
|
+
* object), `undefined`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
6515
|
+
* @param {VO | VertexKey | undefined} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
6516
|
+
* object), `undefined`, or `VertexKey` (vertex ID).
|
|
6517
|
+
* @returns an edge (EO) or undefined.
|
|
6521
6518
|
*/
|
|
6522
6519
|
getEdge(v1, v2) {
|
|
6523
6520
|
var _a;
|
|
6524
6521
|
let edges = [];
|
|
6525
|
-
if (v1 !==
|
|
6522
|
+
if (v1 !== void 0 && v2 !== void 0) {
|
|
6526
6523
|
const vertex1 = this._getVertex(v1);
|
|
6527
6524
|
const vertex2 = this._getVertex(v2);
|
|
6528
6525
|
if (vertex1 && vertex2) {
|
|
6529
6526
|
edges = (_a = this._edges.get(vertex1)) == null ? void 0 : _a.filter((e) => e.vertices.includes(vertex2.key));
|
|
6530
6527
|
}
|
|
6531
6528
|
}
|
|
6532
|
-
return edges ? edges[0] ||
|
|
6529
|
+
return edges ? edges[0] || void 0 : void 0;
|
|
6533
6530
|
}
|
|
6534
6531
|
/**
|
|
6535
6532
|
* Time Complexity: O(|E|), where |E| is the number of edges incident to the given vertex.
|
|
@@ -6543,18 +6540,18 @@ var dataStructureTyped = (() => {
|
|
|
6543
6540
|
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
6544
6541
|
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
6545
6542
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
6546
|
-
* @returns the removed edge (EO) if it exists, or
|
|
6543
|
+
* @returns the removed edge (EO) if it exists, or undefined if either of the vertices (VO) does not exist.
|
|
6547
6544
|
*/
|
|
6548
6545
|
deleteEdgeBetween(v1, v2) {
|
|
6549
6546
|
const vertex1 = this._getVertex(v1);
|
|
6550
6547
|
const vertex2 = this._getVertex(v2);
|
|
6551
6548
|
if (!vertex1 || !vertex2) {
|
|
6552
|
-
return
|
|
6549
|
+
return void 0;
|
|
6553
6550
|
}
|
|
6554
6551
|
const v1Edges = this._edges.get(vertex1);
|
|
6555
|
-
let removed =
|
|
6552
|
+
let removed = void 0;
|
|
6556
6553
|
if (v1Edges) {
|
|
6557
|
-
removed = arrayRemove(v1Edges, (e) => e.vertices.includes(vertex2.key))[0] ||
|
|
6554
|
+
removed = arrayRemove(v1Edges, (e) => e.vertices.includes(vertex2.key))[0] || void 0;
|
|
6558
6555
|
}
|
|
6559
6556
|
const v2Edges = this._edges.get(vertex2);
|
|
6560
6557
|
if (v2Edges) {
|
|
@@ -6572,7 +6569,7 @@ var dataStructureTyped = (() => {
|
|
|
6572
6569
|
*
|
|
6573
6570
|
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
6574
6571
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
6575
|
-
* @returns The method is returning either the removed edge (of type EO) or
|
|
6572
|
+
* @returns The method is returning either the removed edge (of type EO) or undefined if the edge was not found.
|
|
6576
6573
|
*/
|
|
6577
6574
|
deleteEdge(edge) {
|
|
6578
6575
|
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
@@ -6677,21 +6674,21 @@ var dataStructureTyped = (() => {
|
|
|
6677
6674
|
* Space Complexity: O(1)
|
|
6678
6675
|
*
|
|
6679
6676
|
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
6680
|
-
* it returns
|
|
6677
|
+
* it returns undefined.
|
|
6681
6678
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
6682
6679
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
6683
|
-
* graph. If the edge does not exist, it returns `
|
|
6680
|
+
* graph. If the edge does not exist, it returns `undefined`.
|
|
6684
6681
|
*/
|
|
6685
6682
|
getEndsOfEdge(edge) {
|
|
6686
6683
|
if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
|
|
6687
|
-
return
|
|
6684
|
+
return void 0;
|
|
6688
6685
|
}
|
|
6689
6686
|
const v1 = this._getVertex(edge.vertices[0]);
|
|
6690
6687
|
const v2 = this._getVertex(edge.vertices[1]);
|
|
6691
6688
|
if (v1 && v2) {
|
|
6692
6689
|
return [v1, v2];
|
|
6693
6690
|
} else {
|
|
6694
|
-
return
|
|
6691
|
+
return void 0;
|
|
6695
6692
|
}
|
|
6696
6693
|
}
|
|
6697
6694
|
/**
|
|
@@ -6709,7 +6706,7 @@ var dataStructureTyped = (() => {
|
|
|
6709
6706
|
_addEdgeOnly(edge) {
|
|
6710
6707
|
for (const end of edge.vertices) {
|
|
6711
6708
|
const endVertex = this._getVertex(end);
|
|
6712
|
-
if (endVertex ===
|
|
6709
|
+
if (endVertex === void 0)
|
|
6713
6710
|
return false;
|
|
6714
6711
|
if (endVertex) {
|
|
6715
6712
|
const edges = this._edges.get(endVertex);
|
|
@@ -6849,55 +6846,27 @@ var dataStructureTyped = (() => {
|
|
|
6849
6846
|
|
|
6850
6847
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
6851
6848
|
var BinaryTreeNode = class {
|
|
6852
|
-
/**
|
|
6853
|
-
* Creates a new instance of BinaryTreeNode.
|
|
6854
|
-
* @param {BTNKey} key - The key associated with the node.
|
|
6855
|
-
* @param {V} value - The value stored in the node.
|
|
6856
|
-
*/
|
|
6857
6849
|
constructor(key, value) {
|
|
6858
|
-
/**
|
|
6859
|
-
* The key associated with the node.
|
|
6860
|
-
*/
|
|
6861
6850
|
__publicField(this, "key");
|
|
6862
|
-
/**
|
|
6863
|
-
* The value stored in the node.
|
|
6864
|
-
*/
|
|
6865
6851
|
__publicField(this, "value");
|
|
6866
|
-
/**
|
|
6867
|
-
* The parent node of the current node.
|
|
6868
|
-
*/
|
|
6869
6852
|
__publicField(this, "parent");
|
|
6870
6853
|
__publicField(this, "_left");
|
|
6871
6854
|
__publicField(this, "_right");
|
|
6872
6855
|
this.key = key;
|
|
6873
6856
|
this.value = value;
|
|
6874
6857
|
}
|
|
6875
|
-
/**
|
|
6876
|
-
* Get the left child node.
|
|
6877
|
-
*/
|
|
6878
6858
|
get left() {
|
|
6879
6859
|
return this._left;
|
|
6880
6860
|
}
|
|
6881
|
-
/**
|
|
6882
|
-
* Set the left child node.
|
|
6883
|
-
* @param {N | null | undefined} v - The left child node.
|
|
6884
|
-
*/
|
|
6885
6861
|
set left(v) {
|
|
6886
6862
|
if (v) {
|
|
6887
6863
|
v.parent = this;
|
|
6888
6864
|
}
|
|
6889
6865
|
this._left = v;
|
|
6890
6866
|
}
|
|
6891
|
-
/**
|
|
6892
|
-
* Get the right child node.
|
|
6893
|
-
*/
|
|
6894
6867
|
get right() {
|
|
6895
6868
|
return this._right;
|
|
6896
6869
|
}
|
|
6897
|
-
/**
|
|
6898
|
-
* Set the right child node.
|
|
6899
|
-
* @param {N | null | undefined} v - The right child node.
|
|
6900
|
-
*/
|
|
6901
6870
|
set right(v) {
|
|
6902
6871
|
if (v) {
|
|
6903
6872
|
v.parent = this;
|
|
@@ -6923,8 +6892,13 @@ var dataStructureTyped = (() => {
|
|
|
6923
6892
|
};
|
|
6924
6893
|
var BinaryTree = class _BinaryTree {
|
|
6925
6894
|
/**
|
|
6926
|
-
*
|
|
6927
|
-
* @param
|
|
6895
|
+
* The constructor function initializes a binary tree object with optional elements and options.
|
|
6896
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
|
|
6897
|
+
* elements to be added to the binary tree.
|
|
6898
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
6899
|
+
* configuration options for the binary tree. In this case, it is of type
|
|
6900
|
+
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
6901
|
+
* required.
|
|
6928
6902
|
*/
|
|
6929
6903
|
constructor(elements, options) {
|
|
6930
6904
|
__publicField(this, "iterationType", "ITERATIVE" /* ITERATIVE */);
|
|
@@ -6939,17 +6913,11 @@ var dataStructureTyped = (() => {
|
|
|
6939
6913
|
}
|
|
6940
6914
|
this._size = 0;
|
|
6941
6915
|
if (elements)
|
|
6942
|
-
this.
|
|
6916
|
+
this.addMany(elements);
|
|
6943
6917
|
}
|
|
6944
|
-
/**
|
|
6945
|
-
* Get the root node of the binary tree.
|
|
6946
|
-
*/
|
|
6947
6918
|
get root() {
|
|
6948
6919
|
return this._root;
|
|
6949
6920
|
}
|
|
6950
|
-
/**
|
|
6951
|
-
* Get the number of nodes in the binary tree.
|
|
6952
|
-
*/
|
|
6953
6921
|
get size() {
|
|
6954
6922
|
return this._size;
|
|
6955
6923
|
}
|
|
@@ -6962,30 +6930,46 @@ var dataStructureTyped = (() => {
|
|
|
6962
6930
|
createNode(key, value) {
|
|
6963
6931
|
return new BinaryTreeNode(key, value);
|
|
6964
6932
|
}
|
|
6933
|
+
/**
|
|
6934
|
+
* The function creates a binary tree with the given options.
|
|
6935
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
6936
|
+
* behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
|
|
6937
|
+
* you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
|
|
6938
|
+
* @returns a new instance of a binary tree.
|
|
6939
|
+
*/
|
|
6965
6940
|
createTree(options) {
|
|
6966
6941
|
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
6967
6942
|
}
|
|
6968
6943
|
/**
|
|
6969
|
-
*
|
|
6970
|
-
*
|
|
6944
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
6945
|
+
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
6946
|
+
* two type parameters V and N, representing the value and node type respectively.
|
|
6947
|
+
* @returns a boolean value.
|
|
6948
|
+
*/
|
|
6949
|
+
isEntry(kne) {
|
|
6950
|
+
return Array.isArray(kne) && kne.length === 2;
|
|
6951
|
+
}
|
|
6952
|
+
/**
|
|
6953
|
+
* Time Complexity O(log n) - O(n)
|
|
6954
|
+
* Space Complexity O(1)
|
|
6955
|
+
*/
|
|
6956
|
+
/**
|
|
6957
|
+
* Time Complexity O(log n) - O(n)
|
|
6958
|
+
* Space Complexity O(1)
|
|
6971
6959
|
*
|
|
6972
|
-
* The `add` function adds a new node
|
|
6973
|
-
*
|
|
6974
|
-
* @
|
|
6975
|
-
* following types:
|
|
6976
|
-
* @param {V} [value] - The value to be associated with the key or node being added to the binary
|
|
6977
|
-
* tree.
|
|
6978
|
-
* @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
|
|
6979
|
-
* tree, or `null` or `undefined` if the insertion was not successful.
|
|
6960
|
+
* The `add` function adds a new node to a binary tree, either by key or by providing a node object.
|
|
6961
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be one of the following:
|
|
6962
|
+
* @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
|
|
6980
6963
|
*/
|
|
6981
|
-
add(
|
|
6964
|
+
add(keyOrNodeOrEntry) {
|
|
6965
|
+
let inserted, needInsert;
|
|
6982
6966
|
const _bfs = (root, newNode) => {
|
|
6983
6967
|
const queue = new Queue([root]);
|
|
6984
6968
|
while (queue.size > 0) {
|
|
6985
6969
|
const cur = queue.shift();
|
|
6986
6970
|
if (newNode && cur.key === newNode.key) {
|
|
6987
|
-
cur
|
|
6988
|
-
return;
|
|
6971
|
+
this._replaceNode(cur, newNode);
|
|
6972
|
+
return newNode;
|
|
6989
6973
|
}
|
|
6990
6974
|
const inserted2 = this._addTo(newNode, cur);
|
|
6991
6975
|
if (inserted2 !== void 0)
|
|
@@ -6996,13 +6980,21 @@ var dataStructureTyped = (() => {
|
|
|
6996
6980
|
queue.push(cur.right);
|
|
6997
6981
|
}
|
|
6998
6982
|
};
|
|
6999
|
-
|
|
7000
|
-
if (keyOrNode === null) {
|
|
6983
|
+
if (keyOrNodeOrEntry === null) {
|
|
7001
6984
|
needInsert = null;
|
|
7002
|
-
} else if (this.isNodeKey(
|
|
7003
|
-
needInsert = this.createNode(
|
|
7004
|
-
} else if (
|
|
7005
|
-
needInsert =
|
|
6985
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
6986
|
+
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
6987
|
+
} else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
6988
|
+
needInsert = keyOrNodeOrEntry;
|
|
6989
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
6990
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
6991
|
+
if (key === void 0) {
|
|
6992
|
+
return;
|
|
6993
|
+
} else if (key === null) {
|
|
6994
|
+
needInsert = null;
|
|
6995
|
+
} else {
|
|
6996
|
+
needInsert = this.createNode(key, value);
|
|
6997
|
+
}
|
|
7006
6998
|
} else {
|
|
7007
6999
|
return;
|
|
7008
7000
|
}
|
|
@@ -7020,35 +7012,27 @@ var dataStructureTyped = (() => {
|
|
|
7020
7012
|
return inserted;
|
|
7021
7013
|
}
|
|
7022
7014
|
/**
|
|
7023
|
-
* Time Complexity: O(n)
|
|
7015
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
7024
7016
|
* Space Complexity: O(1)
|
|
7025
7017
|
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
7026
7018
|
*/
|
|
7027
7019
|
/**
|
|
7028
|
-
* Time Complexity: O(k
|
|
7020
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
7029
7021
|
* Space Complexity: O(1)
|
|
7030
7022
|
*
|
|
7031
|
-
* The `addMany`
|
|
7032
|
-
*
|
|
7033
|
-
* @param
|
|
7034
|
-
*
|
|
7035
|
-
*
|
|
7036
|
-
*
|
|
7037
|
-
* correspond to the keys or nodes being added. If provided, the values will be associated with the
|
|
7038
|
-
* keys or nodes during the add operation.
|
|
7039
|
-
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
7023
|
+
* The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
|
|
7024
|
+
* current instance, and returns an array of the inserted nodes.
|
|
7025
|
+
* @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
|
|
7026
|
+
* `BTNodeExemplar<V, N>` objects.
|
|
7027
|
+
* @returns The function `addMany` returns an array of values, where each value is either of type
|
|
7028
|
+
* `N`, `null`, or `undefined`.
|
|
7040
7029
|
*/
|
|
7041
|
-
addMany(
|
|
7042
|
-
|
|
7043
|
-
|
|
7044
|
-
|
|
7045
|
-
|
|
7046
|
-
|
|
7047
|
-
return this.add(null);
|
|
7048
|
-
}
|
|
7049
|
-
const value = values == null ? void 0 : values[i];
|
|
7050
|
-
return this.add(keyOrNode, value);
|
|
7051
|
-
});
|
|
7030
|
+
addMany(nodes) {
|
|
7031
|
+
const inserted = [];
|
|
7032
|
+
for (const kne of nodes) {
|
|
7033
|
+
inserted.push(this.add(kne));
|
|
7034
|
+
}
|
|
7035
|
+
return inserted;
|
|
7052
7036
|
}
|
|
7053
7037
|
/**
|
|
7054
7038
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
@@ -7058,17 +7042,13 @@ var dataStructureTyped = (() => {
|
|
|
7058
7042
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
7059
7043
|
* Space Complexity: O(1)
|
|
7060
7044
|
*
|
|
7061
|
-
* The `refill` function clears the
|
|
7062
|
-
* @param
|
|
7063
|
-
* `
|
|
7064
|
-
* @param {N[] | Array<V>} [values] - The `data` parameter is an optional array of values that will be assigned to
|
|
7065
|
-
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
7066
|
-
* array. Each value in the `data` array will be assigned to the
|
|
7067
|
-
* @returns The method is returning a boolean value.
|
|
7045
|
+
* The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
|
|
7046
|
+
* @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
|
|
7047
|
+
* contain either `BTNodeExemplar` objects, keys, or entries.
|
|
7068
7048
|
*/
|
|
7069
|
-
refill(
|
|
7049
|
+
refill(nodesOrKeysOrEntries) {
|
|
7070
7050
|
this.clear();
|
|
7071
|
-
|
|
7051
|
+
this.addMany(nodesOrKeysOrEntries);
|
|
7072
7052
|
}
|
|
7073
7053
|
/**
|
|
7074
7054
|
* Time Complexity: O(n)
|
|
@@ -7114,7 +7094,7 @@ var dataStructureTyped = (() => {
|
|
|
7114
7094
|
const leftSubTreeRightMost = this.getRightMost(curr.left);
|
|
7115
7095
|
if (leftSubTreeRightMost) {
|
|
7116
7096
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
7117
|
-
orgCurrent = this.
|
|
7097
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
7118
7098
|
if (parentOfLeftSubTreeMax) {
|
|
7119
7099
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
7120
7100
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -7147,8 +7127,8 @@ var dataStructureTyped = (() => {
|
|
|
7147
7127
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
7148
7128
|
*/
|
|
7149
7129
|
getDepth(distNode, beginRoot = this.root) {
|
|
7150
|
-
distNode = this.
|
|
7151
|
-
beginRoot = this.
|
|
7130
|
+
distNode = this.ensureNode(distNode);
|
|
7131
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7152
7132
|
let depth = 0;
|
|
7153
7133
|
while (distNode == null ? void 0 : distNode.parent) {
|
|
7154
7134
|
if (distNode === beginRoot) {
|
|
@@ -7178,7 +7158,7 @@ var dataStructureTyped = (() => {
|
|
|
7178
7158
|
* @returns the height of the binary tree.
|
|
7179
7159
|
*/
|
|
7180
7160
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7181
|
-
beginRoot = this.
|
|
7161
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7182
7162
|
if (!beginRoot)
|
|
7183
7163
|
return -1;
|
|
7184
7164
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7224,7 +7204,7 @@ var dataStructureTyped = (() => {
|
|
|
7224
7204
|
*/
|
|
7225
7205
|
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7226
7206
|
var _a, _b, _c;
|
|
7227
|
-
beginRoot = this.
|
|
7207
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7228
7208
|
if (!beginRoot)
|
|
7229
7209
|
return -1;
|
|
7230
7210
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7311,7 +7291,7 @@ var dataStructureTyped = (() => {
|
|
|
7311
7291
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
7312
7292
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
7313
7293
|
callback = (node) => node;
|
|
7314
|
-
beginRoot = this.
|
|
7294
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7315
7295
|
if (!beginRoot)
|
|
7316
7296
|
return [];
|
|
7317
7297
|
const ans = [];
|
|
@@ -7448,7 +7428,7 @@ var dataStructureTyped = (() => {
|
|
|
7448
7428
|
* Space Complexity: O(log n)
|
|
7449
7429
|
*/
|
|
7450
7430
|
/**
|
|
7451
|
-
* The function `
|
|
7431
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
7452
7432
|
* key, otherwise it returns the key itself.
|
|
7453
7433
|
* @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
|
|
7454
7434
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
@@ -7458,7 +7438,7 @@ var dataStructureTyped = (() => {
|
|
|
7458
7438
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
7459
7439
|
* itself if it is not a valid node key.
|
|
7460
7440
|
*/
|
|
7461
|
-
|
|
7441
|
+
ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
7462
7442
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
7463
7443
|
}
|
|
7464
7444
|
/**
|
|
@@ -7523,7 +7503,7 @@ var dataStructureTyped = (() => {
|
|
|
7523
7503
|
*/
|
|
7524
7504
|
getPathToRoot(beginRoot, isReverse = true) {
|
|
7525
7505
|
const result = [];
|
|
7526
|
-
beginRoot = this.
|
|
7506
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7527
7507
|
if (!beginRoot)
|
|
7528
7508
|
return result;
|
|
7529
7509
|
while (beginRoot.parent) {
|
|
@@ -7552,7 +7532,7 @@ var dataStructureTyped = (() => {
|
|
|
7552
7532
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
7553
7533
|
*/
|
|
7554
7534
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7555
|
-
beginRoot = this.
|
|
7535
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7556
7536
|
if (!beginRoot)
|
|
7557
7537
|
return beginRoot;
|
|
7558
7538
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7591,7 +7571,7 @@ var dataStructureTyped = (() => {
|
|
|
7591
7571
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
7592
7572
|
*/
|
|
7593
7573
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
7594
|
-
beginRoot = this.
|
|
7574
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7595
7575
|
if (!beginRoot)
|
|
7596
7576
|
return beginRoot;
|
|
7597
7577
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7627,7 +7607,7 @@ var dataStructureTyped = (() => {
|
|
|
7627
7607
|
* @returns a boolean value.
|
|
7628
7608
|
*/
|
|
7629
7609
|
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
7630
|
-
beginRoot = this.
|
|
7610
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7631
7611
|
if (!beginRoot)
|
|
7632
7612
|
return true;
|
|
7633
7613
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
@@ -7699,7 +7679,7 @@ var dataStructureTyped = (() => {
|
|
|
7699
7679
|
* by the return type of the `callback` function.
|
|
7700
7680
|
*/
|
|
7701
7681
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7702
|
-
beginRoot = this.
|
|
7682
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7703
7683
|
const ans = [];
|
|
7704
7684
|
if (!beginRoot)
|
|
7705
7685
|
return ans;
|
|
@@ -7797,7 +7777,7 @@ var dataStructureTyped = (() => {
|
|
|
7797
7777
|
* @returns an array of values that are the return values of the callback function.
|
|
7798
7778
|
*/
|
|
7799
7779
|
dfs(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root, iterationType = "ITERATIVE" /* ITERATIVE */, includeNull = false) {
|
|
7800
|
-
beginRoot = this.
|
|
7780
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7801
7781
|
if (!beginRoot)
|
|
7802
7782
|
return [];
|
|
7803
7783
|
const ans = [];
|
|
@@ -7917,7 +7897,7 @@ var dataStructureTyped = (() => {
|
|
|
7917
7897
|
* the breadth-first traversal of a binary tree.
|
|
7918
7898
|
*/
|
|
7919
7899
|
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7920
|
-
beginRoot = this.
|
|
7900
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7921
7901
|
if (!beginRoot)
|
|
7922
7902
|
return [];
|
|
7923
7903
|
const ans = [];
|
|
@@ -7987,7 +7967,7 @@ var dataStructureTyped = (() => {
|
|
|
7987
7967
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
7988
7968
|
*/
|
|
7989
7969
|
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
7990
|
-
beginRoot = this.
|
|
7970
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
7991
7971
|
const levelsNodes = [];
|
|
7992
7972
|
if (!beginRoot)
|
|
7993
7973
|
return levelsNodes;
|
|
@@ -8039,7 +8019,7 @@ var dataStructureTyped = (() => {
|
|
|
8039
8019
|
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
8040
8020
|
*/
|
|
8041
8021
|
getPredecessor(node) {
|
|
8042
|
-
node = this.
|
|
8022
|
+
node = this.ensureNode(node);
|
|
8043
8023
|
if (!this.isRealNode(node))
|
|
8044
8024
|
return void 0;
|
|
8045
8025
|
if (node.left) {
|
|
@@ -8061,7 +8041,7 @@ var dataStructureTyped = (() => {
|
|
|
8061
8041
|
* after the given node in the inorder traversal of the binary tree.
|
|
8062
8042
|
*/
|
|
8063
8043
|
getSuccessor(x) {
|
|
8064
|
-
x = this.
|
|
8044
|
+
x = this.ensureNode(x);
|
|
8065
8045
|
if (!x)
|
|
8066
8046
|
return void 0;
|
|
8067
8047
|
if (x.right) {
|
|
@@ -8093,7 +8073,7 @@ var dataStructureTyped = (() => {
|
|
|
8093
8073
|
* by the return type of the `callback` function.
|
|
8094
8074
|
*/
|
|
8095
8075
|
morris(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root) {
|
|
8096
|
-
beginRoot = this.
|
|
8076
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8097
8077
|
if (beginRoot === null)
|
|
8098
8078
|
return [];
|
|
8099
8079
|
const ans = [];
|
|
@@ -8200,7 +8180,7 @@ var dataStructureTyped = (() => {
|
|
|
8200
8180
|
const newTree = this.createTree();
|
|
8201
8181
|
for (const [key, value] of this) {
|
|
8202
8182
|
if (predicate([key, value], this)) {
|
|
8203
|
-
newTree.add(key, value);
|
|
8183
|
+
newTree.add([key, value]);
|
|
8204
8184
|
}
|
|
8205
8185
|
}
|
|
8206
8186
|
return newTree;
|
|
@@ -8214,7 +8194,7 @@ var dataStructureTyped = (() => {
|
|
|
8214
8194
|
map(callback) {
|
|
8215
8195
|
const newTree = this.createTree();
|
|
8216
8196
|
for (const [key, value] of this) {
|
|
8217
|
-
newTree.add(key, callback([key, value], this));
|
|
8197
|
+
newTree.add([key, callback([key, value], this)]);
|
|
8218
8198
|
}
|
|
8219
8199
|
return newTree;
|
|
8220
8200
|
}
|
|
@@ -8290,7 +8270,7 @@ var dataStructureTyped = (() => {
|
|
|
8290
8270
|
*/
|
|
8291
8271
|
print(beginRoot = this.root, options) {
|
|
8292
8272
|
const opts = __spreadValues({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
|
|
8293
|
-
beginRoot = this.
|
|
8273
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8294
8274
|
if (!beginRoot)
|
|
8295
8275
|
return;
|
|
8296
8276
|
if (opts.isShowUndefined)
|
|
@@ -8310,18 +8290,6 @@ var dataStructureTyped = (() => {
|
|
|
8310
8290
|
};
|
|
8311
8291
|
display(beginRoot);
|
|
8312
8292
|
}
|
|
8313
|
-
init(elements) {
|
|
8314
|
-
if (elements) {
|
|
8315
|
-
for (const entryOrKey of elements) {
|
|
8316
|
-
if (Array.isArray(entryOrKey)) {
|
|
8317
|
-
const [key, value] = entryOrKey;
|
|
8318
|
-
this.add(key, value);
|
|
8319
|
-
} else {
|
|
8320
|
-
this.add(entryOrKey);
|
|
8321
|
-
}
|
|
8322
|
-
}
|
|
8323
|
-
}
|
|
8324
|
-
}
|
|
8325
8293
|
_displayAux(node, options) {
|
|
8326
8294
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
8327
8295
|
const emptyDisplayLayout = [["\u2500"], 1, 0, 0];
|
|
@@ -8358,9 +8326,9 @@ var dataStructureTyped = (() => {
|
|
|
8358
8326
|
* @param {N} destNode - The destination node to swap.
|
|
8359
8327
|
* @returns {N} - The destination node after the swap.
|
|
8360
8328
|
*/
|
|
8361
|
-
|
|
8362
|
-
srcNode = this.
|
|
8363
|
-
destNode = this.
|
|
8329
|
+
_swapProperties(srcNode, destNode) {
|
|
8330
|
+
srcNode = this.ensureNode(srcNode);
|
|
8331
|
+
destNode = this.ensureNode(destNode);
|
|
8364
8332
|
if (srcNode && destNode) {
|
|
8365
8333
|
const { key, value } = destNode;
|
|
8366
8334
|
const tempNode = this.createNode(key, value);
|
|
@@ -8374,6 +8342,30 @@ var dataStructureTyped = (() => {
|
|
|
8374
8342
|
}
|
|
8375
8343
|
return void 0;
|
|
8376
8344
|
}
|
|
8345
|
+
/**
|
|
8346
|
+
* The function replaces an old node with a new node in a binary tree.
|
|
8347
|
+
* @param {N} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
8348
|
+
* tree.
|
|
8349
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
8350
|
+
* tree.
|
|
8351
|
+
* @returns The method is returning the newNode.
|
|
8352
|
+
*/
|
|
8353
|
+
_replaceNode(oldNode, newNode) {
|
|
8354
|
+
if (oldNode.parent) {
|
|
8355
|
+
if (oldNode.parent.left === oldNode) {
|
|
8356
|
+
oldNode.parent.left = newNode;
|
|
8357
|
+
} else if (oldNode.parent.right === oldNode) {
|
|
8358
|
+
oldNode.parent.right = newNode;
|
|
8359
|
+
}
|
|
8360
|
+
}
|
|
8361
|
+
newNode.left = oldNode.left;
|
|
8362
|
+
newNode.right = oldNode.right;
|
|
8363
|
+
newNode.parent = oldNode.parent;
|
|
8364
|
+
if (this.root === oldNode) {
|
|
8365
|
+
this._root = newNode;
|
|
8366
|
+
}
|
|
8367
|
+
return newNode;
|
|
8368
|
+
}
|
|
8377
8369
|
/**
|
|
8378
8370
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
8379
8371
|
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|
|
@@ -8468,9 +8460,12 @@ var dataStructureTyped = (() => {
|
|
|
8468
8460
|
};
|
|
8469
8461
|
var BST = class _BST extends BinaryTree {
|
|
8470
8462
|
/**
|
|
8471
|
-
*
|
|
8472
|
-
*
|
|
8473
|
-
*
|
|
8463
|
+
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
8464
|
+
* the tree with optional elements and options.
|
|
8465
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
|
|
8466
|
+
* binary search tree.
|
|
8467
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
8468
|
+
* configuration options for the binary search tree. It can have the following properties:
|
|
8474
8469
|
*/
|
|
8475
8470
|
constructor(elements, options) {
|
|
8476
8471
|
super([], options);
|
|
@@ -8484,11 +8479,8 @@ var dataStructureTyped = (() => {
|
|
|
8484
8479
|
}
|
|
8485
8480
|
this._root = void 0;
|
|
8486
8481
|
if (elements)
|
|
8487
|
-
this.
|
|
8482
|
+
this.addMany(elements);
|
|
8488
8483
|
}
|
|
8489
|
-
/**
|
|
8490
|
-
* Get the root node of the binary tree.
|
|
8491
|
-
*/
|
|
8492
8484
|
get root() {
|
|
8493
8485
|
return this._root;
|
|
8494
8486
|
}
|
|
@@ -8503,151 +8495,145 @@ var dataStructureTyped = (() => {
|
|
|
8503
8495
|
createNode(key, value) {
|
|
8504
8496
|
return new BSTNode(key, value);
|
|
8505
8497
|
}
|
|
8498
|
+
/**
|
|
8499
|
+
* The function creates a new binary search tree with the specified options.
|
|
8500
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
8501
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
|
|
8502
|
+
* that defines various options for creating a binary search tree.
|
|
8503
|
+
* @returns a new instance of the BST class with the specified options.
|
|
8504
|
+
*/
|
|
8506
8505
|
createTree(options) {
|
|
8507
8506
|
return new _BST([], __spreadValues({
|
|
8508
8507
|
iterationType: this.iterationType,
|
|
8509
8508
|
comparator: this.comparator
|
|
8510
8509
|
}, options));
|
|
8511
8510
|
}
|
|
8511
|
+
/**
|
|
8512
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8513
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
8514
|
+
*/
|
|
8512
8515
|
/**
|
|
8513
8516
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8514
8517
|
* Space Complexity: O(1) - Constant space is used.
|
|
8515
8518
|
*
|
|
8516
|
-
* The `add` function adds a new node to a binary search tree
|
|
8517
|
-
*
|
|
8518
|
-
* following
|
|
8519
|
-
* @
|
|
8520
|
-
*
|
|
8521
|
-
* @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
|
|
8522
|
-
* no node was inserted, it returns `undefined`.
|
|
8519
|
+
* The `add` function adds a new node to a binary search tree, either by key or by providing a node
|
|
8520
|
+
* object.
|
|
8521
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
8522
|
+
* @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
|
|
8523
|
+
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
8523
8524
|
*/
|
|
8524
|
-
add(
|
|
8525
|
-
if (
|
|
8525
|
+
add(keyOrNodeOrEntry) {
|
|
8526
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
8526
8527
|
return void 0;
|
|
8527
|
-
|
|
8528
|
+
}
|
|
8528
8529
|
let newNode;
|
|
8529
|
-
if (
|
|
8530
|
-
newNode =
|
|
8531
|
-
} else if (this.isNodeKey(
|
|
8532
|
-
newNode = this.createNode(
|
|
8530
|
+
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
8531
|
+
newNode = keyOrNodeOrEntry;
|
|
8532
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
8533
|
+
newNode = this.createNode(keyOrNodeOrEntry);
|
|
8534
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
8535
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
8536
|
+
if (key === void 0 || key === null) {
|
|
8537
|
+
return;
|
|
8538
|
+
} else {
|
|
8539
|
+
newNode = this.createNode(key, value);
|
|
8540
|
+
}
|
|
8533
8541
|
} else {
|
|
8534
|
-
|
|
8542
|
+
return;
|
|
8535
8543
|
}
|
|
8536
8544
|
if (this.root === void 0) {
|
|
8537
8545
|
this._setRoot(newNode);
|
|
8538
|
-
this._size
|
|
8539
|
-
|
|
8540
|
-
}
|
|
8541
|
-
|
|
8542
|
-
|
|
8543
|
-
|
|
8544
|
-
|
|
8545
|
-
|
|
8546
|
-
|
|
8547
|
-
|
|
8548
|
-
|
|
8549
|
-
|
|
8550
|
-
|
|
8551
|
-
|
|
8552
|
-
if (cur.left === void 0) {
|
|
8553
|
-
if (newNode) {
|
|
8554
|
-
newNode.parent = cur;
|
|
8555
|
-
}
|
|
8556
|
-
cur.left = newNode;
|
|
8557
|
-
this._size = this.size + 1;
|
|
8558
|
-
traversing = false;
|
|
8559
|
-
inserted = cur.left;
|
|
8560
|
-
} else {
|
|
8561
|
-
if (cur.left)
|
|
8562
|
-
cur = cur.left;
|
|
8563
|
-
}
|
|
8564
|
-
} else if (this._compare(cur.key, newNode.key) === "lt" /* lt */) {
|
|
8565
|
-
if (cur.right === void 0) {
|
|
8566
|
-
if (newNode) {
|
|
8567
|
-
newNode.parent = cur;
|
|
8568
|
-
}
|
|
8569
|
-
cur.right = newNode;
|
|
8570
|
-
this._size = this.size + 1;
|
|
8571
|
-
traversing = false;
|
|
8572
|
-
inserted = cur.right;
|
|
8573
|
-
} else {
|
|
8574
|
-
if (cur.right)
|
|
8575
|
-
cur = cur.right;
|
|
8576
|
-
}
|
|
8577
|
-
}
|
|
8578
|
-
} else {
|
|
8579
|
-
traversing = false;
|
|
8546
|
+
this._size++;
|
|
8547
|
+
return this.root;
|
|
8548
|
+
}
|
|
8549
|
+
let current = this.root;
|
|
8550
|
+
while (current !== void 0) {
|
|
8551
|
+
if (this._compare(current.key, newNode.key) === "eq" /* eq */) {
|
|
8552
|
+
this._replaceNode(current, newNode);
|
|
8553
|
+
return newNode;
|
|
8554
|
+
} else if (this._compare(current.key, newNode.key) === "gt" /* gt */) {
|
|
8555
|
+
if (current.left === void 0) {
|
|
8556
|
+
current.left = newNode;
|
|
8557
|
+
newNode.parent = current;
|
|
8558
|
+
this._size++;
|
|
8559
|
+
return newNode;
|
|
8580
8560
|
}
|
|
8561
|
+
current = current.left;
|
|
8562
|
+
} else {
|
|
8563
|
+
if (current.right === void 0) {
|
|
8564
|
+
current.right = newNode;
|
|
8565
|
+
newNode.parent = current;
|
|
8566
|
+
this._size++;
|
|
8567
|
+
return newNode;
|
|
8568
|
+
}
|
|
8569
|
+
current = current.right;
|
|
8581
8570
|
}
|
|
8582
8571
|
}
|
|
8583
|
-
return
|
|
8572
|
+
return void 0;
|
|
8584
8573
|
}
|
|
8585
8574
|
/**
|
|
8586
|
-
* Time Complexity: O(log n) -
|
|
8587
|
-
* Space Complexity: O(
|
|
8575
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
8576
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
8588
8577
|
*/
|
|
8589
8578
|
/**
|
|
8590
|
-
* Time Complexity: O(
|
|
8591
|
-
* Space Complexity: O(
|
|
8579
|
+
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
8580
|
+
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
8592
8581
|
*
|
|
8593
|
-
* The `addMany` function
|
|
8594
|
-
*
|
|
8595
|
-
* @param
|
|
8596
|
-
* binary
|
|
8597
|
-
* node), or `undefined`.
|
|
8598
|
-
* @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
|
|
8599
|
-
* nodes being added. If provided, the length of the `data` array must be the same as the length of
|
|
8600
|
-
* the `keysOrNodes` array.
|
|
8582
|
+
* The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
|
|
8583
|
+
* unbalanced manner, and returns an array of the inserted nodes.
|
|
8584
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
|
|
8585
|
+
* binary tree.
|
|
8601
8586
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
8602
|
-
* adding the nodes. The default value is
|
|
8587
|
+
* adding the nodes. The default value is true.
|
|
8603
8588
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
8604
|
-
* type of iteration to use when adding multiple keys or nodes to the binary
|
|
8605
|
-
*
|
|
8606
|
-
*
|
|
8607
|
-
* @returns The
|
|
8589
|
+
* type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
|
|
8590
|
+
* value of `this.iterationType`, which means it will use the iteration type specified by the binary
|
|
8591
|
+
* tree instance.
|
|
8592
|
+
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
8608
8593
|
*/
|
|
8609
|
-
addMany(
|
|
8610
|
-
function hasNoUndefined(arr) {
|
|
8611
|
-
return arr.indexOf(void 0) === -1;
|
|
8612
|
-
}
|
|
8613
|
-
if (!isBalanceAdd || !hasNoUndefined(keysOrNodes)) {
|
|
8614
|
-
return super.addMany(keysOrNodes, data).map((n) => n != null ? n : void 0);
|
|
8615
|
-
}
|
|
8594
|
+
addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
8616
8595
|
const inserted = [];
|
|
8617
|
-
|
|
8618
|
-
(
|
|
8619
|
-
|
|
8620
|
-
|
|
8621
|
-
|
|
8622
|
-
|
|
8623
|
-
if (keyOrNode instanceof BSTNode)
|
|
8624
|
-
return true;
|
|
8625
|
-
return false;
|
|
8596
|
+
if (!isBalanceAdd) {
|
|
8597
|
+
for (const kve of keysOrNodesOrEntries) {
|
|
8598
|
+
const nn = this.add(kve);
|
|
8599
|
+
inserted.push(nn);
|
|
8600
|
+
}
|
|
8601
|
+
return inserted;
|
|
8626
8602
|
}
|
|
8627
|
-
const
|
|
8628
|
-
|
|
8629
|
-
|
|
8630
|
-
|
|
8631
|
-
return
|
|
8603
|
+
const realBTNExemplars = [];
|
|
8604
|
+
const isRealBTNExemplar = (kve) => {
|
|
8605
|
+
if (kve === void 0 || kve === null)
|
|
8606
|
+
return false;
|
|
8607
|
+
return !(this.isEntry(kve) && (kve[0] === void 0 || kve[0] === null));
|
|
8632
8608
|
};
|
|
8633
|
-
|
|
8634
|
-
|
|
8635
|
-
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
8636
|
-
} else if (_isBinaryTreeKeyOrNullTuple(combinedArr)) {
|
|
8637
|
-
sorted = combinedArr.sort((a, b) => a[0] - b[0]);
|
|
8638
|
-
} else {
|
|
8639
|
-
throw new Error("Invalid input keysOrNodes");
|
|
8609
|
+
for (const kve of keysOrNodesOrEntries) {
|
|
8610
|
+
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
8640
8611
|
}
|
|
8641
|
-
|
|
8642
|
-
|
|
8643
|
-
|
|
8612
|
+
let sorted = [];
|
|
8613
|
+
sorted = realBTNExemplars.sort((a, b) => {
|
|
8614
|
+
let aR, bR;
|
|
8615
|
+
if (this.isEntry(a))
|
|
8616
|
+
aR = a[0];
|
|
8617
|
+
else if (this.isRealNode(a))
|
|
8618
|
+
aR = a.key;
|
|
8619
|
+
else
|
|
8620
|
+
aR = a;
|
|
8621
|
+
if (this.isEntry(b))
|
|
8622
|
+
bR = b[0];
|
|
8623
|
+
else if (this.isRealNode(b))
|
|
8624
|
+
bR = b.key;
|
|
8625
|
+
else
|
|
8626
|
+
bR = b;
|
|
8627
|
+
return aR - bR;
|
|
8628
|
+
});
|
|
8629
|
+
const _dfs = (arr) => {
|
|
8644
8630
|
if (arr.length === 0)
|
|
8645
8631
|
return;
|
|
8646
8632
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
8647
|
-
const newNode = this.add(arr[mid]
|
|
8633
|
+
const newNode = this.add(arr[mid]);
|
|
8648
8634
|
inserted.push(newNode);
|
|
8649
|
-
_dfs(arr.slice(0, mid)
|
|
8650
|
-
_dfs(arr.slice(mid + 1)
|
|
8635
|
+
_dfs(arr.slice(0, mid));
|
|
8636
|
+
_dfs(arr.slice(mid + 1));
|
|
8651
8637
|
};
|
|
8652
8638
|
const _iterate = () => {
|
|
8653
8639
|
const n = sorted.length;
|
|
@@ -8658,7 +8644,7 @@ var dataStructureTyped = (() => {
|
|
|
8658
8644
|
const [l, r] = popped;
|
|
8659
8645
|
if (l <= r) {
|
|
8660
8646
|
const m = l + Math.floor((r - l) / 2);
|
|
8661
|
-
const newNode = this.add(
|
|
8647
|
+
const newNode = this.add(sorted[m]);
|
|
8662
8648
|
inserted.push(newNode);
|
|
8663
8649
|
stack.push([m + 1, r]);
|
|
8664
8650
|
stack.push([l, m - 1]);
|
|
@@ -8667,7 +8653,7 @@ var dataStructureTyped = (() => {
|
|
|
8667
8653
|
}
|
|
8668
8654
|
};
|
|
8669
8655
|
if (iterationType === "RECURSIVE" /* RECURSIVE */) {
|
|
8670
|
-
_dfs(
|
|
8656
|
+
_dfs(sorted);
|
|
8671
8657
|
} else {
|
|
8672
8658
|
_iterate();
|
|
8673
8659
|
}
|
|
@@ -8754,7 +8740,7 @@ var dataStructureTyped = (() => {
|
|
|
8754
8740
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
8755
8741
|
*/
|
|
8756
8742
|
/**
|
|
8757
|
-
* The function `
|
|
8743
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
8758
8744
|
* otherwise it returns the key itself.
|
|
8759
8745
|
* @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
|
|
8760
8746
|
* `undefined`.
|
|
@@ -8762,7 +8748,7 @@ var dataStructureTyped = (() => {
|
|
|
8762
8748
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
8763
8749
|
* @returns either a node object (N) or undefined.
|
|
8764
8750
|
*/
|
|
8765
|
-
|
|
8751
|
+
ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
8766
8752
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
8767
8753
|
}
|
|
8768
8754
|
/**
|
|
@@ -8789,7 +8775,7 @@ var dataStructureTyped = (() => {
|
|
|
8789
8775
|
* @returns The method returns an array of nodes (`N[]`).
|
|
8790
8776
|
*/
|
|
8791
8777
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8792
|
-
beginRoot = this.
|
|
8778
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8793
8779
|
if (!beginRoot)
|
|
8794
8780
|
return [];
|
|
8795
8781
|
const ans = [];
|
|
@@ -8865,7 +8851,7 @@ var dataStructureTyped = (() => {
|
|
|
8865
8851
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
8866
8852
|
*/
|
|
8867
8853
|
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = "lt" /* lt */, targetNode = this.root, iterationType = this.iterationType) {
|
|
8868
|
-
targetNode = this.
|
|
8854
|
+
targetNode = this.ensureNode(targetNode);
|
|
8869
8855
|
const ans = [];
|
|
8870
8856
|
if (!targetNode)
|
|
8871
8857
|
return ans;
|
|
@@ -8929,7 +8915,7 @@ var dataStructureTyped = (() => {
|
|
|
8929
8915
|
return;
|
|
8930
8916
|
const m = l + Math.floor((r - l) / 2);
|
|
8931
8917
|
const midNode = sorted[m];
|
|
8932
|
-
this.add(midNode.key, midNode.value);
|
|
8918
|
+
this.add([midNode.key, midNode.value]);
|
|
8933
8919
|
buildBalanceBST(l, m - 1);
|
|
8934
8920
|
buildBalanceBST(m + 1, r);
|
|
8935
8921
|
};
|
|
@@ -8945,7 +8931,7 @@ var dataStructureTyped = (() => {
|
|
|
8945
8931
|
const m = l + Math.floor((r - l) / 2);
|
|
8946
8932
|
const midNode = sorted[m];
|
|
8947
8933
|
debugger;
|
|
8948
|
-
this.add(midNode.key, midNode.value);
|
|
8934
|
+
this.add([midNode.key, midNode.value]);
|
|
8949
8935
|
stack.push([m + 1, r]);
|
|
8950
8936
|
stack.push([l, m - 1]);
|
|
8951
8937
|
}
|
|
@@ -9019,22 +9005,6 @@ var dataStructureTyped = (() => {
|
|
|
9019
9005
|
}
|
|
9020
9006
|
return balanced;
|
|
9021
9007
|
}
|
|
9022
|
-
/**
|
|
9023
|
-
* Time Complexity: O(n) - Visiting each node once.
|
|
9024
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
9025
|
-
*/
|
|
9026
|
-
init(elements) {
|
|
9027
|
-
if (elements) {
|
|
9028
|
-
for (const entryOrKey of elements) {
|
|
9029
|
-
if (Array.isArray(entryOrKey)) {
|
|
9030
|
-
const [key, value] = entryOrKey;
|
|
9031
|
-
this.add(key, value);
|
|
9032
|
-
} else {
|
|
9033
|
-
this.add(entryOrKey);
|
|
9034
|
-
}
|
|
9035
|
-
}
|
|
9036
|
-
}
|
|
9037
|
-
}
|
|
9038
9008
|
_setRoot(v) {
|
|
9039
9009
|
if (v) {
|
|
9040
9010
|
v.parent = void 0;
|
|
@@ -9327,14 +9297,14 @@ var dataStructureTyped = (() => {
|
|
|
9327
9297
|
constructor(start, end, sum, value) {
|
|
9328
9298
|
__publicField(this, "start", 0);
|
|
9329
9299
|
__publicField(this, "end", 0);
|
|
9330
|
-
__publicField(this, "value"
|
|
9300
|
+
__publicField(this, "value");
|
|
9331
9301
|
__publicField(this, "sum", 0);
|
|
9332
|
-
__publicField(this, "left"
|
|
9333
|
-
__publicField(this, "right"
|
|
9302
|
+
__publicField(this, "left");
|
|
9303
|
+
__publicField(this, "right");
|
|
9334
9304
|
this.start = start;
|
|
9335
9305
|
this.end = end;
|
|
9336
9306
|
this.sum = sum;
|
|
9337
|
-
this.value = value ||
|
|
9307
|
+
this.value = value || void 0;
|
|
9338
9308
|
}
|
|
9339
9309
|
};
|
|
9340
9310
|
var SegmentTree = class {
|
|
@@ -9360,7 +9330,7 @@ var dataStructureTyped = (() => {
|
|
|
9360
9330
|
if (values.length > 0) {
|
|
9361
9331
|
this._root = this.build(start, end);
|
|
9362
9332
|
} else {
|
|
9363
|
-
this._root =
|
|
9333
|
+
this._root = void 0;
|
|
9364
9334
|
this._values = [];
|
|
9365
9335
|
}
|
|
9366
9336
|
}
|
|
@@ -9411,7 +9381,7 @@ var dataStructureTyped = (() => {
|
|
|
9411
9381
|
* @returns The function does not return anything.
|
|
9412
9382
|
*/
|
|
9413
9383
|
updateNode(index, sum, value) {
|
|
9414
|
-
const root = this.root ||
|
|
9384
|
+
const root = this.root || void 0;
|
|
9415
9385
|
if (!root) {
|
|
9416
9386
|
return;
|
|
9417
9387
|
}
|
|
@@ -9446,7 +9416,7 @@ var dataStructureTyped = (() => {
|
|
|
9446
9416
|
* @returns The function `querySumByRange` returns a number.
|
|
9447
9417
|
*/
|
|
9448
9418
|
querySumByRange(indexA, indexB) {
|
|
9449
|
-
const root = this.root ||
|
|
9419
|
+
const root = this.root || void 0;
|
|
9450
9420
|
if (!root) {
|
|
9451
9421
|
return 0;
|
|
9452
9422
|
}
|
|
@@ -9496,15 +9466,18 @@ var dataStructureTyped = (() => {
|
|
|
9496
9466
|
};
|
|
9497
9467
|
var AVLTree = class _AVLTree extends BST {
|
|
9498
9468
|
/**
|
|
9499
|
-
*
|
|
9500
|
-
* @param
|
|
9501
|
-
*
|
|
9502
|
-
*
|
|
9469
|
+
* The constructor function initializes an AVLTree object with optional elements and options.
|
|
9470
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
9471
|
+
* objects. It represents a collection of elements that will be added to the AVL tree during
|
|
9472
|
+
* initialization.
|
|
9473
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
9474
|
+
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
9475
|
+
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
9503
9476
|
*/
|
|
9504
9477
|
constructor(elements, options) {
|
|
9505
9478
|
super([], options);
|
|
9506
9479
|
if (elements)
|
|
9507
|
-
|
|
9480
|
+
super.addMany(elements);
|
|
9508
9481
|
}
|
|
9509
9482
|
/**
|
|
9510
9483
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -9518,28 +9491,37 @@ var dataStructureTyped = (() => {
|
|
|
9518
9491
|
createNode(key, value) {
|
|
9519
9492
|
return new AVLTreeNode(key, value);
|
|
9520
9493
|
}
|
|
9494
|
+
/**
|
|
9495
|
+
* The function creates a new AVL tree with the specified options and returns it.
|
|
9496
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
9497
|
+
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
9498
|
+
* being created.
|
|
9499
|
+
* @returns a new AVLTree object.
|
|
9500
|
+
*/
|
|
9521
9501
|
createTree(options) {
|
|
9522
9502
|
return new _AVLTree([], __spreadValues({
|
|
9523
9503
|
iterationType: this.iterationType,
|
|
9524
9504
|
comparator: this.comparator
|
|
9525
9505
|
}, options));
|
|
9526
9506
|
}
|
|
9507
|
+
/**
|
|
9508
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
9509
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9510
|
+
*/
|
|
9527
9511
|
/**
|
|
9528
9512
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
9529
9513
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9530
9514
|
*
|
|
9531
|
-
* The function overrides the add method of a
|
|
9532
|
-
*
|
|
9533
|
-
* @param
|
|
9534
|
-
*
|
|
9535
|
-
* @
|
|
9536
|
-
* added to the binary search tree.
|
|
9537
|
-
* @returns The method is returning either a node (N) or undefined.
|
|
9515
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
9516
|
+
* a new node.
|
|
9517
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
|
|
9518
|
+
* entry.
|
|
9519
|
+
* @returns The method is returning either the inserted node or `undefined`.
|
|
9538
9520
|
*/
|
|
9539
|
-
add(
|
|
9540
|
-
if (
|
|
9521
|
+
add(keyOrNodeOrEntry) {
|
|
9522
|
+
if (keyOrNodeOrEntry === null)
|
|
9541
9523
|
return void 0;
|
|
9542
|
-
const inserted = super.add(
|
|
9524
|
+
const inserted = super.add(keyOrNodeOrEntry);
|
|
9543
9525
|
if (inserted)
|
|
9544
9526
|
this._balancePath(inserted);
|
|
9545
9527
|
return inserted;
|
|
@@ -9575,23 +9557,7 @@ var dataStructureTyped = (() => {
|
|
|
9575
9557
|
return deletedResults;
|
|
9576
9558
|
}
|
|
9577
9559
|
/**
|
|
9578
|
-
*
|
|
9579
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
9580
|
-
*/
|
|
9581
|
-
init(elements) {
|
|
9582
|
-
if (elements) {
|
|
9583
|
-
for (const entryOrKey of elements) {
|
|
9584
|
-
if (Array.isArray(entryOrKey)) {
|
|
9585
|
-
const [key, value] = entryOrKey;
|
|
9586
|
-
this.add(key, value);
|
|
9587
|
-
} else {
|
|
9588
|
-
this.add(entryOrKey);
|
|
9589
|
-
}
|
|
9590
|
-
}
|
|
9591
|
-
}
|
|
9592
|
-
}
|
|
9593
|
-
/**
|
|
9594
|
-
* The `_swap` function swaps the key, value, and height properties between two nodes in a binary
|
|
9560
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
9595
9561
|
* tree.
|
|
9596
9562
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
9597
9563
|
* needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
@@ -9600,9 +9566,9 @@ var dataStructureTyped = (() => {
|
|
|
9600
9566
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
9601
9567
|
* if either `srcNode` or `destNode` is undefined.
|
|
9602
9568
|
*/
|
|
9603
|
-
|
|
9604
|
-
srcNode = this.
|
|
9605
|
-
destNode = this.
|
|
9569
|
+
_swapProperties(srcNode, destNode) {
|
|
9570
|
+
srcNode = this.ensureNode(srcNode);
|
|
9571
|
+
destNode = this.ensureNode(destNode);
|
|
9606
9572
|
if (srcNode && destNode) {
|
|
9607
9573
|
const { key, value, height } = destNode;
|
|
9608
9574
|
const tempNode = this.createNode(key, value);
|
|
@@ -9889,6 +9855,10 @@ var dataStructureTyped = (() => {
|
|
|
9889
9855
|
B && this._updateHeight(B);
|
|
9890
9856
|
C && this._updateHeight(C);
|
|
9891
9857
|
}
|
|
9858
|
+
_replaceNode(oldNode, newNode) {
|
|
9859
|
+
newNode.height = oldNode.height;
|
|
9860
|
+
return super._replaceNode(oldNode, newNode);
|
|
9861
|
+
}
|
|
9892
9862
|
};
|
|
9893
9863
|
|
|
9894
9864
|
// src/data-structures/binary-tree/rb-tree.ts
|
|
@@ -9901,9 +9871,15 @@ var dataStructureTyped = (() => {
|
|
|
9901
9871
|
};
|
|
9902
9872
|
var RedBlackTree = class _RedBlackTree extends BST {
|
|
9903
9873
|
/**
|
|
9904
|
-
*
|
|
9905
|
-
*
|
|
9906
|
-
*
|
|
9874
|
+
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
9875
|
+
* initializes the tree with optional elements and options.
|
|
9876
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
9877
|
+
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
9878
|
+
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
9879
|
+
* elements to the
|
|
9880
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
9881
|
+
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
9882
|
+
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
9907
9883
|
*/
|
|
9908
9884
|
constructor(elements, options) {
|
|
9909
9885
|
super([], options);
|
|
@@ -9912,7 +9888,7 @@ var dataStructureTyped = (() => {
|
|
|
9912
9888
|
__publicField(this, "_size", 0);
|
|
9913
9889
|
this._root = this.Sentinel;
|
|
9914
9890
|
if (elements)
|
|
9915
|
-
|
|
9891
|
+
super.addMany(elements);
|
|
9916
9892
|
}
|
|
9917
9893
|
get root() {
|
|
9918
9894
|
return this._root;
|
|
@@ -9920,9 +9896,28 @@ var dataStructureTyped = (() => {
|
|
|
9920
9896
|
get size() {
|
|
9921
9897
|
return this._size;
|
|
9922
9898
|
}
|
|
9899
|
+
/**
|
|
9900
|
+
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
9901
|
+
* @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
|
|
9902
|
+
* identify and compare nodes in the Red-Black Tree.
|
|
9903
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
9904
|
+
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
9905
|
+
* specific type when using the `createNode` method.
|
|
9906
|
+
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
9907
|
+
* Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
|
|
9908
|
+
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
9909
|
+
* value, and color.
|
|
9910
|
+
*/
|
|
9923
9911
|
createNode(key, value, color = 0 /* BLACK */) {
|
|
9924
9912
|
return new RedBlackTreeNode(key, value, color);
|
|
9925
9913
|
}
|
|
9914
|
+
/**
|
|
9915
|
+
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
9916
|
+
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
9917
|
+
* passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
|
|
9918
|
+
* class.
|
|
9919
|
+
* @returns a new instance of a RedBlackTree object.
|
|
9920
|
+
*/
|
|
9926
9921
|
createTree(options) {
|
|
9927
9922
|
return new _RedBlackTree([], __spreadValues({
|
|
9928
9923
|
iterationType: this.iterationType,
|
|
@@ -9932,24 +9927,28 @@ var dataStructureTyped = (() => {
|
|
|
9932
9927
|
/**
|
|
9933
9928
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
9934
9929
|
* Space Complexity: O(1)
|
|
9935
|
-
*
|
|
9936
|
-
* The `add` function adds a new node to a Red-Black Tree data structure.
|
|
9937
|
-
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
9938
|
-
* following types:
|
|
9939
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
9940
|
-
* key in the node being added to the Red-Black Tree.
|
|
9941
|
-
* @returns The method returns either a node (`N`) or `undefined`.
|
|
9942
9930
|
*/
|
|
9943
|
-
|
|
9931
|
+
/**
|
|
9932
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
9933
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
9934
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
9935
|
+
* `undefined`.
|
|
9936
|
+
*/
|
|
9937
|
+
add(keyOrNodeOrEntry) {
|
|
9944
9938
|
let node;
|
|
9945
|
-
if (this.isNodeKey(
|
|
9946
|
-
node = this.createNode(
|
|
9947
|
-
} else if (
|
|
9948
|
-
node =
|
|
9949
|
-
} else if (
|
|
9950
|
-
return;
|
|
9951
|
-
} else if (keyOrNode === void 0) {
|
|
9939
|
+
if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
9940
|
+
node = this.createNode(keyOrNodeOrEntry, void 0, 1 /* RED */);
|
|
9941
|
+
} else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
9942
|
+
node = keyOrNodeOrEntry;
|
|
9943
|
+
} else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
9952
9944
|
return;
|
|
9945
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9946
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
9947
|
+
if (key === void 0 || key === null) {
|
|
9948
|
+
return;
|
|
9949
|
+
} else {
|
|
9950
|
+
node = this.createNode(key, value, 1 /* RED */);
|
|
9951
|
+
}
|
|
9953
9952
|
} else {
|
|
9954
9953
|
return;
|
|
9955
9954
|
}
|
|
@@ -9965,6 +9964,9 @@ var dataStructureTyped = (() => {
|
|
|
9965
9964
|
} else if (node.key > x.key) {
|
|
9966
9965
|
x = x == null ? void 0 : x.right;
|
|
9967
9966
|
} else {
|
|
9967
|
+
if (node !== x) {
|
|
9968
|
+
this._replaceNode(x, node);
|
|
9969
|
+
}
|
|
9968
9970
|
return;
|
|
9969
9971
|
}
|
|
9970
9972
|
}
|
|
@@ -10068,6 +10070,10 @@ var dataStructureTyped = (() => {
|
|
|
10068
10070
|
isRealNode(node) {
|
|
10069
10071
|
return node !== this.Sentinel && node !== void 0;
|
|
10070
10072
|
}
|
|
10073
|
+
/**
|
|
10074
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10075
|
+
* Space Complexity: O(1)
|
|
10076
|
+
*/
|
|
10071
10077
|
/**
|
|
10072
10078
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10073
10079
|
* Space Complexity: O(1)
|
|
@@ -10093,7 +10099,7 @@ var dataStructureTyped = (() => {
|
|
|
10093
10099
|
var _a;
|
|
10094
10100
|
if (identifier instanceof BinaryTreeNode)
|
|
10095
10101
|
callback = (node) => node;
|
|
10096
|
-
beginRoot = this.
|
|
10102
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
10097
10103
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
|
|
10098
10104
|
}
|
|
10099
10105
|
/**
|
|
@@ -10152,18 +10158,6 @@ var dataStructureTyped = (() => {
|
|
|
10152
10158
|
this._root = this.Sentinel;
|
|
10153
10159
|
this._size = 0;
|
|
10154
10160
|
}
|
|
10155
|
-
init(elements) {
|
|
10156
|
-
if (elements) {
|
|
10157
|
-
for (const entryOrKey of elements) {
|
|
10158
|
-
if (Array.isArray(entryOrKey)) {
|
|
10159
|
-
const [key, value] = entryOrKey;
|
|
10160
|
-
this.add(key, value);
|
|
10161
|
-
} else {
|
|
10162
|
-
this.add(entryOrKey);
|
|
10163
|
-
}
|
|
10164
|
-
}
|
|
10165
|
-
}
|
|
10166
|
-
}
|
|
10167
10161
|
_setRoot(v) {
|
|
10168
10162
|
if (v) {
|
|
10169
10163
|
v.parent = void 0;
|
|
@@ -10381,6 +10375,19 @@ var dataStructureTyped = (() => {
|
|
|
10381
10375
|
}
|
|
10382
10376
|
this.root.color = 0 /* BLACK */;
|
|
10383
10377
|
}
|
|
10378
|
+
/**
|
|
10379
|
+
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
10380
|
+
* @param {N} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
10381
|
+
* data structure. It is of type `N`, which is the type of the nodes in the data structure.
|
|
10382
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
10383
|
+
* data structure.
|
|
10384
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
10385
|
+
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
10386
|
+
*/
|
|
10387
|
+
_replaceNode(oldNode, newNode) {
|
|
10388
|
+
newNode.color = oldNode.color;
|
|
10389
|
+
return super._replaceNode(oldNode, newNode);
|
|
10390
|
+
}
|
|
10384
10391
|
};
|
|
10385
10392
|
|
|
10386
10393
|
// src/data-structures/binary-tree/tree-multimap.ts
|
|
@@ -10402,20 +10409,17 @@ var dataStructureTyped = (() => {
|
|
|
10402
10409
|
}
|
|
10403
10410
|
};
|
|
10404
10411
|
var TreeMultimap = class _TreeMultimap extends AVLTree {
|
|
10405
|
-
/**
|
|
10406
|
-
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
10407
|
-
* merge duplicated values.
|
|
10408
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
10409
|
-
* TreeMultimap.
|
|
10410
|
-
*/
|
|
10411
10412
|
constructor(elements, options) {
|
|
10412
10413
|
super([], options);
|
|
10413
10414
|
__publicField(this, "_count", 0);
|
|
10414
10415
|
if (elements)
|
|
10415
|
-
this.
|
|
10416
|
+
this.addMany(elements);
|
|
10416
10417
|
}
|
|
10418
|
+
// TODO the _count is not accurate after nodes count modified
|
|
10417
10419
|
get count() {
|
|
10418
|
-
|
|
10420
|
+
let sum = 0;
|
|
10421
|
+
this.subTreeTraverse((node) => sum += node.count);
|
|
10422
|
+
return sum;
|
|
10419
10423
|
}
|
|
10420
10424
|
/**
|
|
10421
10425
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
@@ -10435,115 +10439,63 @@ var dataStructureTyped = (() => {
|
|
|
10435
10439
|
comparator: this.comparator
|
|
10436
10440
|
}, options));
|
|
10437
10441
|
}
|
|
10442
|
+
/**
|
|
10443
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10444
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10445
|
+
*/
|
|
10438
10446
|
/**
|
|
10439
10447
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10440
10448
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10441
10449
|
*
|
|
10442
|
-
* The `add` function
|
|
10443
|
-
*
|
|
10444
|
-
* @param
|
|
10445
|
-
* following types:
|
|
10446
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
10447
|
-
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
10450
|
+
* The `add` function overrides the base class `add` function to add a new node to the tree multimap
|
|
10451
|
+
* and update the count.
|
|
10452
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
10448
10453
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
10449
|
-
* times the key
|
|
10450
|
-
*
|
|
10454
|
+
* times the key or node or entry should be added to the multimap. If not provided, the default value
|
|
10455
|
+
* is 1.
|
|
10456
|
+
* @returns either a node (`N`) or `undefined`.
|
|
10451
10457
|
*/
|
|
10452
|
-
add(
|
|
10453
|
-
|
|
10454
|
-
|
|
10455
|
-
|
|
10456
|
-
if (
|
|
10457
|
-
newNode =
|
|
10458
|
-
} else if (
|
|
10459
|
-
newNode = void 0;
|
|
10458
|
+
add(keyOrNodeOrEntry, count = 1) {
|
|
10459
|
+
let newNode;
|
|
10460
|
+
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
10461
|
+
return;
|
|
10462
|
+
} else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
10463
|
+
newNode = keyOrNodeOrEntry;
|
|
10464
|
+
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
10465
|
+
newNode = this.createNode(keyOrNodeOrEntry, void 0, count);
|
|
10466
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
10467
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
10468
|
+
if (key === void 0 || key === null) {
|
|
10469
|
+
return;
|
|
10470
|
+
} else {
|
|
10471
|
+
newNode = this.createNode(key, value, count);
|
|
10472
|
+
}
|
|
10460
10473
|
} else {
|
|
10461
|
-
|
|
10474
|
+
return;
|
|
10462
10475
|
}
|
|
10463
|
-
|
|
10464
|
-
|
|
10465
|
-
|
|
10466
|
-
|
|
10467
|
-
this._count += newNode.count;
|
|
10468
|
-
inserted = this.root;
|
|
10469
|
-
} else {
|
|
10470
|
-
let cur = this.root;
|
|
10471
|
-
let traversing = true;
|
|
10472
|
-
while (traversing) {
|
|
10473
|
-
if (cur) {
|
|
10474
|
-
if (newNode) {
|
|
10475
|
-
if (this._compare(cur.key, newNode.key) === "eq" /* eq */) {
|
|
10476
|
-
cur.value = newNode.value;
|
|
10477
|
-
cur.count += newNode.count;
|
|
10478
|
-
this._count += newNode.count;
|
|
10479
|
-
traversing = false;
|
|
10480
|
-
inserted = cur;
|
|
10481
|
-
} else if (this._compare(cur.key, newNode.key) === "gt" /* gt */) {
|
|
10482
|
-
if (cur.left === void 0) {
|
|
10483
|
-
cur.left = newNode;
|
|
10484
|
-
this._size = this.size + 1;
|
|
10485
|
-
this._count += newNode.count;
|
|
10486
|
-
traversing = false;
|
|
10487
|
-
inserted = cur.left;
|
|
10488
|
-
} else {
|
|
10489
|
-
if (cur.left)
|
|
10490
|
-
cur = cur.left;
|
|
10491
|
-
}
|
|
10492
|
-
} else if (this._compare(cur.key, newNode.key) === "lt" /* lt */) {
|
|
10493
|
-
if (cur.right === void 0) {
|
|
10494
|
-
cur.right = newNode;
|
|
10495
|
-
this._size = this.size + 1;
|
|
10496
|
-
this._count += newNode.count;
|
|
10497
|
-
traversing = false;
|
|
10498
|
-
inserted = cur.right;
|
|
10499
|
-
} else {
|
|
10500
|
-
if (cur.right)
|
|
10501
|
-
cur = cur.right;
|
|
10502
|
-
}
|
|
10503
|
-
}
|
|
10504
|
-
} else {
|
|
10505
|
-
}
|
|
10506
|
-
} else {
|
|
10507
|
-
traversing = false;
|
|
10508
|
-
}
|
|
10509
|
-
}
|
|
10476
|
+
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
10477
|
+
const inserted = super.add(newNode);
|
|
10478
|
+
if (inserted) {
|
|
10479
|
+
this._count += orgNodeCount;
|
|
10510
10480
|
}
|
|
10511
|
-
if (inserted)
|
|
10512
|
-
this._balancePath(inserted);
|
|
10513
10481
|
return inserted;
|
|
10514
10482
|
}
|
|
10515
10483
|
/**
|
|
10516
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10484
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10517
10485
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10518
10486
|
*/
|
|
10519
10487
|
/**
|
|
10520
|
-
* Time Complexity: O(k log n) - logarithmic time
|
|
10488
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10521
10489
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10522
10490
|
*
|
|
10523
|
-
* The function
|
|
10524
|
-
*
|
|
10525
|
-
* @param
|
|
10526
|
-
*
|
|
10527
|
-
* @
|
|
10528
|
-
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
10529
|
-
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
10530
|
-
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
10491
|
+
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
10492
|
+
* structure.
|
|
10493
|
+
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
10494
|
+
* either keys, nodes, or entries.
|
|
10495
|
+
* @returns The method is returning an array of type `N | undefined`.
|
|
10531
10496
|
*/
|
|
10532
|
-
addMany(
|
|
10533
|
-
|
|
10534
|
-
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
10535
|
-
const keyOrNode = keysOrNodes[i];
|
|
10536
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
10537
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
10538
|
-
continue;
|
|
10539
|
-
}
|
|
10540
|
-
if (keyOrNode === void 0) {
|
|
10541
|
-
inserted.push(this.add(NaN, void 0, 0));
|
|
10542
|
-
continue;
|
|
10543
|
-
}
|
|
10544
|
-
inserted.push(this.add(keyOrNode, data == null ? void 0 : data[i], 1));
|
|
10545
|
-
}
|
|
10546
|
-
return inserted;
|
|
10497
|
+
addMany(keysOrNodesOrEntries) {
|
|
10498
|
+
return super.addMany(keysOrNodesOrEntries);
|
|
10547
10499
|
}
|
|
10548
10500
|
/**
|
|
10549
10501
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
@@ -10571,7 +10523,7 @@ var dataStructureTyped = (() => {
|
|
|
10571
10523
|
return;
|
|
10572
10524
|
const m = l + Math.floor((r - l) / 2);
|
|
10573
10525
|
const midNode = sorted[m];
|
|
10574
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
10526
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
10575
10527
|
buildBalanceBST(l, m - 1);
|
|
10576
10528
|
buildBalanceBST(m + 1, r);
|
|
10577
10529
|
};
|
|
@@ -10586,7 +10538,7 @@ var dataStructureTyped = (() => {
|
|
|
10586
10538
|
if (l <= r) {
|
|
10587
10539
|
const m = l + Math.floor((r - l) / 2);
|
|
10588
10540
|
const midNode = sorted[m];
|
|
10589
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
10541
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
10590
10542
|
stack.push([m + 1, r]);
|
|
10591
10543
|
stack.push([l, m - 1]);
|
|
10592
10544
|
}
|
|
@@ -10649,7 +10601,7 @@ var dataStructureTyped = (() => {
|
|
|
10649
10601
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : void 0;
|
|
10650
10602
|
if (leftSubTreeRightMost) {
|
|
10651
10603
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
10652
|
-
orgCurrent = this.
|
|
10604
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
10653
10605
|
if (parentOfLeftSubTreeMax) {
|
|
10654
10606
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
10655
10607
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -10681,22 +10633,6 @@ var dataStructureTyped = (() => {
|
|
|
10681
10633
|
super.clear();
|
|
10682
10634
|
this._count = 0;
|
|
10683
10635
|
}
|
|
10684
|
-
/**
|
|
10685
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
10686
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
10687
|
-
*/
|
|
10688
|
-
init(elements) {
|
|
10689
|
-
if (elements) {
|
|
10690
|
-
for (const entryOrKey of elements) {
|
|
10691
|
-
if (Array.isArray(entryOrKey)) {
|
|
10692
|
-
const [key, value] = entryOrKey;
|
|
10693
|
-
this.add(key, value);
|
|
10694
|
-
} else {
|
|
10695
|
-
this.add(entryOrKey);
|
|
10696
|
-
}
|
|
10697
|
-
}
|
|
10698
|
-
}
|
|
10699
|
-
}
|
|
10700
10636
|
/**
|
|
10701
10637
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
10702
10638
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -10713,7 +10649,7 @@ var dataStructureTyped = (() => {
|
|
|
10713
10649
|
* added, or `undefined` if no node was added.
|
|
10714
10650
|
*/
|
|
10715
10651
|
_addTo(newNode, parent) {
|
|
10716
|
-
parent = this.
|
|
10652
|
+
parent = this.ensureNode(parent);
|
|
10717
10653
|
if (parent) {
|
|
10718
10654
|
if (parent.left === void 0) {
|
|
10719
10655
|
parent.left = newNode;
|
|
@@ -10737,7 +10673,7 @@ var dataStructureTyped = (() => {
|
|
|
10737
10673
|
}
|
|
10738
10674
|
}
|
|
10739
10675
|
/**
|
|
10740
|
-
* The `
|
|
10676
|
+
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
10741
10677
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
10742
10678
|
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
10743
10679
|
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
@@ -10745,9 +10681,9 @@ var dataStructureTyped = (() => {
|
|
|
10745
10681
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
10746
10682
|
* if either `srcNode` or `destNode` is undefined.
|
|
10747
10683
|
*/
|
|
10748
|
-
|
|
10749
|
-
srcNode = this.
|
|
10750
|
-
destNode = this.
|
|
10684
|
+
_swapProperties(srcNode, destNode) {
|
|
10685
|
+
srcNode = this.ensureNode(srcNode);
|
|
10686
|
+
destNode = this.ensureNode(destNode);
|
|
10751
10687
|
if (srcNode && destNode) {
|
|
10752
10688
|
const { key, value, count, height } = destNode;
|
|
10753
10689
|
const tempNode = this.createNode(key, value, count);
|
|
@@ -10766,6 +10702,10 @@ var dataStructureTyped = (() => {
|
|
|
10766
10702
|
}
|
|
10767
10703
|
return void 0;
|
|
10768
10704
|
}
|
|
10705
|
+
_replaceNode(oldNode, newNode) {
|
|
10706
|
+
newNode.count = oldNode.count + newNode.count;
|
|
10707
|
+
return super._replaceNode(oldNode, newNode);
|
|
10708
|
+
}
|
|
10769
10709
|
};
|
|
10770
10710
|
|
|
10771
10711
|
// src/data-structures/tree/tree.ts
|
|
@@ -11416,16 +11356,21 @@ var dataStructureTyped = (() => {
|
|
|
11416
11356
|
};
|
|
11417
11357
|
var Trie = class _Trie {
|
|
11418
11358
|
constructor(words, caseSensitive = true) {
|
|
11359
|
+
__publicField(this, "_size");
|
|
11419
11360
|
__publicField(this, "_caseSensitive");
|
|
11420
11361
|
__publicField(this, "_root");
|
|
11421
11362
|
this._root = new TrieNode("");
|
|
11422
11363
|
this._caseSensitive = caseSensitive;
|
|
11364
|
+
this._size = 0;
|
|
11423
11365
|
if (words) {
|
|
11424
|
-
for (const
|
|
11425
|
-
this.add(
|
|
11366
|
+
for (const word of words) {
|
|
11367
|
+
this.add(word);
|
|
11426
11368
|
}
|
|
11427
11369
|
}
|
|
11428
11370
|
}
|
|
11371
|
+
get size() {
|
|
11372
|
+
return this._size;
|
|
11373
|
+
}
|
|
11429
11374
|
get caseSensitive() {
|
|
11430
11375
|
return this._caseSensitive;
|
|
11431
11376
|
}
|
|
@@ -11447,6 +11392,7 @@ var dataStructureTyped = (() => {
|
|
|
11447
11392
|
add(word) {
|
|
11448
11393
|
word = this._caseProcess(word);
|
|
11449
11394
|
let cur = this.root;
|
|
11395
|
+
let isNewWord = false;
|
|
11450
11396
|
for (const c of word) {
|
|
11451
11397
|
let nodeC = cur.children.get(c);
|
|
11452
11398
|
if (!nodeC) {
|
|
@@ -11455,8 +11401,12 @@ var dataStructureTyped = (() => {
|
|
|
11455
11401
|
}
|
|
11456
11402
|
cur = nodeC;
|
|
11457
11403
|
}
|
|
11458
|
-
cur.isEnd
|
|
11459
|
-
|
|
11404
|
+
if (!cur.isEnd) {
|
|
11405
|
+
isNewWord = true;
|
|
11406
|
+
cur.isEnd = true;
|
|
11407
|
+
this._size++;
|
|
11408
|
+
}
|
|
11409
|
+
return isNewWord;
|
|
11460
11410
|
}
|
|
11461
11411
|
/**
|
|
11462
11412
|
* Time Complexity: O(M), where M is the length of the input word.
|
|
@@ -11522,6 +11472,9 @@ var dataStructureTyped = (() => {
|
|
|
11522
11472
|
return false;
|
|
11523
11473
|
};
|
|
11524
11474
|
dfs(this.root, 0);
|
|
11475
|
+
if (isDeleted) {
|
|
11476
|
+
this._size--;
|
|
11477
|
+
}
|
|
11525
11478
|
return isDeleted;
|
|
11526
11479
|
}
|
|
11527
11480
|
/**
|
|
@@ -11744,6 +11697,9 @@ var dataStructureTyped = (() => {
|
|
|
11744
11697
|
}
|
|
11745
11698
|
return accumulator;
|
|
11746
11699
|
}
|
|
11700
|
+
print() {
|
|
11701
|
+
console.log([...this]);
|
|
11702
|
+
}
|
|
11747
11703
|
/**
|
|
11748
11704
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
11749
11705
|
* Space Complexity: O(1) - Constant space.
|