data-structure-typed 1.51.8 → 1.51.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +1 -37
- package/benchmark/report.json +13 -385
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +80 -60
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +316 -224
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +471 -361
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +198 -200
- package/dist/cjs/data-structures/binary-tree/bst.js +215 -249
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +71 -72
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/cjs/data-structures/hash/hash-map.js +40 -55
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -3
- package/dist/cjs/data-structures/queue/deque.js +2 -3
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +1 -1
- package/dist/cjs/data-structures/trie/trie.js +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +6 -6
- package/dist/cjs/types/common.d.ts +1 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/cjs/utils/utils.js +3 -5
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +80 -60
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +316 -224
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +472 -362
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +198 -200
- package/dist/mjs/data-structures/binary-tree/bst.js +219 -255
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +71 -72
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/mjs/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/mjs/data-structures/hash/hash-map.js +41 -55
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -3
- package/dist/mjs/data-structures/queue/deque.js +2 -3
- package/dist/mjs/data-structures/trie/trie.d.ts +1 -1
- package/dist/mjs/data-structures/trie/trie.js +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +6 -6
- package/dist/mjs/types/common.d.ts +1 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/mjs/utils/utils.js +3 -5
- package/dist/umd/data-structure-typed.js +1340 -1181
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
- package/src/data-structures/binary-tree/avl-tree.ts +94 -66
- package/src/data-structures/binary-tree/binary-tree.ts +530 -398
- package/src/data-structures/binary-tree/bst.ts +251 -270
- package/src/data-structures/binary-tree/rb-tree.ts +121 -100
- package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +8 -7
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/utils/utils.ts +3 -3
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +3 -3
- package/test/integration/index.html +25 -11
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +68 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
- package/test/utils/big-o.ts +12 -6
- package/tsconfig-base.json +1 -0
|
@@ -715,7 +715,7 @@ var dataStructureTyped = (() => {
|
|
|
715
715
|
function isComparable(key) {
|
|
716
716
|
const keyType = typeof key;
|
|
717
717
|
if (keyType === "number")
|
|
718
|
-
return isNaN(key);
|
|
718
|
+
return !isNaN(key);
|
|
719
719
|
if (keyType === "string")
|
|
720
720
|
return true;
|
|
721
721
|
if (keyType === "bigint")
|
|
@@ -731,10 +731,6 @@ var dataStructureTyped = (() => {
|
|
|
731
731
|
if (keyType === "object") {
|
|
732
732
|
if (key === null)
|
|
733
733
|
return true;
|
|
734
|
-
if (typeof key.valueOf === "function")
|
|
735
|
-
return isComparable(key.valueOf());
|
|
736
|
-
if (typeof key.toString === "function")
|
|
737
|
-
return isComparable(key.toString());
|
|
738
734
|
return false;
|
|
739
735
|
}
|
|
740
736
|
return false;
|
|
@@ -752,23 +748,15 @@ var dataStructureTyped = (() => {
|
|
|
752
748
|
/**
|
|
753
749
|
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
754
750
|
* options.
|
|
755
|
-
* @param
|
|
751
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
756
752
|
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
757
753
|
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
758
754
|
*/
|
|
759
|
-
constructor(
|
|
755
|
+
constructor(entryOrRawElements = [], options) {
|
|
760
756
|
super();
|
|
761
757
|
__publicField(this, "_store", {});
|
|
762
758
|
__publicField(this, "_objMap", /* @__PURE__ */ new Map());
|
|
763
|
-
__publicField(this, "_toEntryFn"
|
|
764
|
-
if (this.isEntry(rawElement)) {
|
|
765
|
-
return rawElement;
|
|
766
|
-
} else {
|
|
767
|
-
throw new Error(
|
|
768
|
-
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
769
|
-
);
|
|
770
|
-
}
|
|
771
|
-
});
|
|
759
|
+
__publicField(this, "_toEntryFn");
|
|
772
760
|
__publicField(this, "_size", 0);
|
|
773
761
|
__publicField(this, "_hashFn", (key) => String(key));
|
|
774
762
|
if (options) {
|
|
@@ -780,8 +768,8 @@ var dataStructureTyped = (() => {
|
|
|
780
768
|
this._toEntryFn = toEntryFn;
|
|
781
769
|
}
|
|
782
770
|
}
|
|
783
|
-
if (
|
|
784
|
-
this.setMany(
|
|
771
|
+
if (entryOrRawElements) {
|
|
772
|
+
this.setMany(entryOrRawElements);
|
|
785
773
|
}
|
|
786
774
|
}
|
|
787
775
|
/**
|
|
@@ -875,23 +863,24 @@ var dataStructureTyped = (() => {
|
|
|
875
863
|
/**
|
|
876
864
|
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
877
865
|
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
878
|
-
* @param
|
|
866
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
879
867
|
* `T`.
|
|
880
868
|
* @returns The `setMany` function is returning an array of booleans.
|
|
881
869
|
*/
|
|
882
|
-
setMany(
|
|
870
|
+
setMany(entryOrRawElements) {
|
|
883
871
|
const results = [];
|
|
884
|
-
for (const rawEle of
|
|
872
|
+
for (const rawEle of entryOrRawElements) {
|
|
885
873
|
let key, value;
|
|
886
874
|
if (this.isEntry(rawEle)) {
|
|
887
875
|
key = rawEle[0];
|
|
888
876
|
value = rawEle[1];
|
|
889
|
-
} else {
|
|
877
|
+
} else if (this.toEntryFn) {
|
|
890
878
|
const item = this.toEntryFn(rawEle);
|
|
891
879
|
key = item[0];
|
|
892
880
|
value = item[1];
|
|
893
881
|
}
|
|
894
|
-
|
|
882
|
+
if (key !== void 0 && value !== void 0)
|
|
883
|
+
results.push(this.set(key, value));
|
|
895
884
|
}
|
|
896
885
|
return results;
|
|
897
886
|
}
|
|
@@ -1072,14 +1061,14 @@ var dataStructureTyped = (() => {
|
|
|
1072
1061
|
var LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
1073
1062
|
/**
|
|
1074
1063
|
* The constructor initializes a LinkedHashMap object with an optional raw collection and options.
|
|
1075
|
-
* @param
|
|
1064
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements. It is
|
|
1076
1065
|
* used to initialize the HashMapLinked instance with key-value pairs. Each element in the
|
|
1077
|
-
* `
|
|
1066
|
+
* `entryOrRawElements` is converted to a key-value pair using the `toEntryFn` function (if provided) and
|
|
1078
1067
|
* then added to the HashMap
|
|
1079
1068
|
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
1080
1069
|
* properties:
|
|
1081
1070
|
*/
|
|
1082
|
-
constructor(
|
|
1071
|
+
constructor(entryOrRawElements = [], options) {
|
|
1083
1072
|
super();
|
|
1084
1073
|
__publicField(this, "_sentinel");
|
|
1085
1074
|
__publicField(this, "_hashFn", (key) => String(key));
|
|
@@ -1093,7 +1082,7 @@ var dataStructureTyped = (() => {
|
|
|
1093
1082
|
return rawElement;
|
|
1094
1083
|
} else {
|
|
1095
1084
|
throw new Error(
|
|
1096
|
-
"If the provided
|
|
1085
|
+
"If the provided entryOrRawElements does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
1097
1086
|
);
|
|
1098
1087
|
}
|
|
1099
1088
|
});
|
|
@@ -1110,8 +1099,8 @@ var dataStructureTyped = (() => {
|
|
|
1110
1099
|
this._toEntryFn = toEntryFn;
|
|
1111
1100
|
}
|
|
1112
1101
|
}
|
|
1113
|
-
if (
|
|
1114
|
-
for (const el of
|
|
1102
|
+
if (entryOrRawElements) {
|
|
1103
|
+
for (const el of entryOrRawElements) {
|
|
1115
1104
|
const [key, value] = this.toEntryFn(el);
|
|
1116
1105
|
this.set(key, value);
|
|
1117
1106
|
}
|
|
@@ -1150,7 +1139,7 @@ var dataStructureTyped = (() => {
|
|
|
1150
1139
|
/**
|
|
1151
1140
|
* The function returns the head node of a HashMapLinkedNode.
|
|
1152
1141
|
* @returns The method `getHead()` is returning a `HashMapLinkedNode` object with key type `K` and
|
|
1153
|
-
* value type `V | undefined`.
|
|
1142
|
+
* a value type `V | undefined`.
|
|
1154
1143
|
*/
|
|
1155
1144
|
get head() {
|
|
1156
1145
|
return this._head;
|
|
@@ -1286,13 +1275,13 @@ var dataStructureTyped = (() => {
|
|
|
1286
1275
|
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
1287
1276
|
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
1288
1277
|
* of booleans indicating the success of each set operation.
|
|
1289
|
-
* @param
|
|
1278
|
+
* @param entryOrRawElements - The entryOrRawElements parameter is an iterable collection of elements of type
|
|
1290
1279
|
* R.
|
|
1291
1280
|
* @returns The `setMany` function returns an array of booleans.
|
|
1292
1281
|
*/
|
|
1293
|
-
setMany(
|
|
1282
|
+
setMany(entryOrRawElements) {
|
|
1294
1283
|
const results = [];
|
|
1295
|
-
for (const rawEle of
|
|
1284
|
+
for (const rawEle of entryOrRawElements) {
|
|
1296
1285
|
const [key, value] = this.toEntryFn(rawEle);
|
|
1297
1286
|
results.push(this.set(key, value));
|
|
1298
1287
|
}
|
|
@@ -1342,21 +1331,20 @@ var dataStructureTyped = (() => {
|
|
|
1342
1331
|
}
|
|
1343
1332
|
}
|
|
1344
1333
|
/**
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
*/
|
|
1334
|
+
* Time Complexity: O(n)
|
|
1335
|
+
* Space Complexity: O(1)
|
|
1336
|
+
*/
|
|
1337
|
+
/**
|
|
1338
|
+
* Time Complexity: O(n)
|
|
1339
|
+
* Space Complexity: O(1)
|
|
1340
|
+
*
|
|
1341
|
+
* The function `at` retrieves the key-value pair at a specified index in a linked list.
|
|
1342
|
+
* @param {number} index - The index parameter is a number that represents the position of the
|
|
1343
|
+
* element we want to retrieve from the data structure.
|
|
1344
|
+
* @returns The method `at(index: number)` is returning an array containing the key-value pair at
|
|
1345
|
+
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
1346
|
+
* where `K` is the key and `V` is the value.
|
|
1347
|
+
*/
|
|
1360
1348
|
at(index) {
|
|
1361
1349
|
rangeCheck(index, 0, this._size - 1);
|
|
1362
1350
|
let node = this.head;
|
|
@@ -1366,20 +1354,19 @@ var dataStructureTyped = (() => {
|
|
|
1366
1354
|
return node.value;
|
|
1367
1355
|
}
|
|
1368
1356
|
/**
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
*/
|
|
1357
|
+
* Time Complexity: O(1)
|
|
1358
|
+
* Space Complexity: O(1)
|
|
1359
|
+
*/
|
|
1360
|
+
/**
|
|
1361
|
+
* Time Complexity: O(1)
|
|
1362
|
+
* Space Complexity: O(1)
|
|
1363
|
+
*
|
|
1364
|
+
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
1365
|
+
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
1366
|
+
* It can be of any type, but typically it is a string or an object.
|
|
1367
|
+
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
1368
|
+
* was not found.
|
|
1369
|
+
*/
|
|
1383
1370
|
delete(key) {
|
|
1384
1371
|
let node;
|
|
1385
1372
|
if (isWeakKey(key)) {
|
|
@@ -1401,19 +1388,18 @@ var dataStructureTyped = (() => {
|
|
|
1401
1388
|
return true;
|
|
1402
1389
|
}
|
|
1403
1390
|
/**
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
*/
|
|
1391
|
+
* Time Complexity: O(n)
|
|
1392
|
+
* Space Complexity: O(1)
|
|
1393
|
+
*/
|
|
1394
|
+
/**
|
|
1395
|
+
* Time Complexity: O(n)
|
|
1396
|
+
* Space Complexity: O(1)
|
|
1397
|
+
*
|
|
1398
|
+
* The `deleteAt` function deletes a node at a specified index in a linked list.
|
|
1399
|
+
* @param {number} index - The index parameter represents the position at which the node should be
|
|
1400
|
+
* deleted in the linked list.
|
|
1401
|
+
* @returns The size of the list after deleting the element at the specified index.
|
|
1402
|
+
*/
|
|
1417
1403
|
deleteAt(index) {
|
|
1418
1404
|
rangeCheck(index, 0, this._size - 1);
|
|
1419
1405
|
let node = this.head;
|
|
@@ -1423,18 +1409,17 @@ var dataStructureTyped = (() => {
|
|
|
1423
1409
|
return this._deleteNode(node);
|
|
1424
1410
|
}
|
|
1425
1411
|
/**
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
*/
|
|
1412
|
+
* Time Complexity: O(1)
|
|
1413
|
+
* Space Complexity: O(1)
|
|
1414
|
+
*/
|
|
1415
|
+
/**
|
|
1416
|
+
* Time Complexity: O(1)
|
|
1417
|
+
* Space Complexity: O(1)
|
|
1418
|
+
*
|
|
1419
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
1420
|
+
* @returns The method is returning a boolean value indicating whether the size of the object is 0 or
|
|
1421
|
+
* not.
|
|
1422
|
+
*/
|
|
1438
1423
|
isEmpty() {
|
|
1439
1424
|
return this._size === 0;
|
|
1440
1425
|
}
|
|
@@ -1448,16 +1433,15 @@ var dataStructureTyped = (() => {
|
|
|
1448
1433
|
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
1449
1434
|
}
|
|
1450
1435
|
/**
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
*/
|
|
1436
|
+
* Time Complexity: O(1)
|
|
1437
|
+
* Space Complexity: O(1)
|
|
1438
|
+
*/
|
|
1439
|
+
/**
|
|
1440
|
+
* Time Complexity: O(1)
|
|
1441
|
+
* Space Complexity: O(1)
|
|
1442
|
+
*
|
|
1443
|
+
* The `clear` function clears all the entries in a data structure and resets its properties.
|
|
1444
|
+
*/
|
|
1461
1445
|
clear() {
|
|
1462
1446
|
this._noObjMap = {};
|
|
1463
1447
|
this._size = 0;
|
|
@@ -1485,25 +1469,24 @@ var dataStructureTyped = (() => {
|
|
|
1485
1469
|
return cloned;
|
|
1486
1470
|
}
|
|
1487
1471
|
/**
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
*/
|
|
1472
|
+
* Time Complexity: O(n)
|
|
1473
|
+
* Space Complexity: O(n)
|
|
1474
|
+
*/
|
|
1475
|
+
/**
|
|
1476
|
+
* Time Complexity: O(n)
|
|
1477
|
+
* Space Complexity: O(n)
|
|
1478
|
+
*
|
|
1479
|
+
* The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original
|
|
1480
|
+
* map that satisfy a given predicate function.
|
|
1481
|
+
* @param predicate - The `predicate` parameter is a callback function that takes four arguments:
|
|
1482
|
+
* `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the
|
|
1483
|
+
* current element should be included in the filtered map or not.
|
|
1484
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1485
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
1486
|
+
* specific object as the context for the `predicate` function. If `thisArg` is not provided, `this
|
|
1487
|
+
* @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
|
|
1488
|
+
* `LinkedHashMap` object that satisfy the given predicate function.
|
|
1489
|
+
*/
|
|
1507
1490
|
filter(predicate, thisArg) {
|
|
1508
1491
|
const filteredMap = new _LinkedHashMap();
|
|
1509
1492
|
let index = 0;
|
|
@@ -1516,27 +1499,26 @@ var dataStructureTyped = (() => {
|
|
|
1516
1499
|
return filteredMap;
|
|
1517
1500
|
}
|
|
1518
1501
|
/**
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
*/
|
|
1502
|
+
* Time Complexity: O(n)
|
|
1503
|
+
* Space Complexity: O(n)
|
|
1504
|
+
*/
|
|
1505
|
+
/**
|
|
1506
|
+
* Time Complexity: O(n)
|
|
1507
|
+
* Space Complexity: O(n)
|
|
1508
|
+
*
|
|
1509
|
+
* The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to
|
|
1510
|
+
* each key-value pair in the original map.
|
|
1511
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
1512
|
+
* in the map. It takes four arguments: the value of the current key-value pair, the key of the
|
|
1513
|
+
* current key-value pair, the index of the current key-value pair, and the map itself. The callback
|
|
1514
|
+
* function should
|
|
1515
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1516
|
+
* specify the value of `this` within the callback function. If provided, the callback function will
|
|
1517
|
+
* be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current
|
|
1518
|
+
* map
|
|
1519
|
+
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
|
1520
|
+
* function.
|
|
1521
|
+
*/
|
|
1540
1522
|
map(callback, thisArg) {
|
|
1541
1523
|
const mappedMap = new _LinkedHashMap();
|
|
1542
1524
|
let index = 0;
|
|
@@ -1566,18 +1548,17 @@ var dataStructureTyped = (() => {
|
|
|
1566
1548
|
return this.set(key, value);
|
|
1567
1549
|
}
|
|
1568
1550
|
/**
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
*/
|
|
1551
|
+
* Time Complexity: O(n)
|
|
1552
|
+
* Space Complexity: O(1)
|
|
1553
|
+
* where n is the number of entries in the LinkedHashMap.
|
|
1554
|
+
*/
|
|
1555
|
+
/**
|
|
1556
|
+
* Time Complexity: O(n)
|
|
1557
|
+
* Space Complexity: O(1)
|
|
1558
|
+
* where n is the number of entries in the LinkedHashMap.
|
|
1559
|
+
*
|
|
1560
|
+
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
1561
|
+
*/
|
|
1581
1562
|
*_getIterator() {
|
|
1582
1563
|
let node = this.head;
|
|
1583
1564
|
while (node !== this._sentinel) {
|
|
@@ -4580,17 +4561,16 @@ var dataStructureTyped = (() => {
|
|
|
4580
4561
|
return newDeque;
|
|
4581
4562
|
}
|
|
4582
4563
|
/**
|
|
4583
|
-
|
|
4584
|
-
|
|
4585
|
-
|
|
4586
|
-
|
|
4587
|
-
|
|
4588
|
-
|
|
4589
|
-
|
|
4590
|
-
|
|
4591
|
-
|
|
4592
|
-
|
|
4593
|
-
*/
|
|
4564
|
+
* Time Complexity: O(n)
|
|
4565
|
+
* Space Complexity: O(1)
|
|
4566
|
+
*/
|
|
4567
|
+
/**
|
|
4568
|
+
* Time Complexity: O(n)
|
|
4569
|
+
* Space Complexity: O(1)
|
|
4570
|
+
*
|
|
4571
|
+
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
4572
|
+
* object to be iterated over using a for...of loop.
|
|
4573
|
+
*/
|
|
4594
4574
|
*_getIterator() {
|
|
4595
4575
|
for (let i = 0; i < this.size; ++i) {
|
|
4596
4576
|
yield this.at(i);
|
|
@@ -5887,13 +5867,12 @@ var dataStructureTyped = (() => {
|
|
|
5887
5867
|
}
|
|
5888
5868
|
}
|
|
5889
5869
|
/**
|
|
5890
|
-
|
|
5891
|
-
|
|
5892
|
-
|
|
5893
|
-
|
|
5894
|
-
|
|
5895
|
-
|
|
5896
|
-
*/
|
|
5870
|
+
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
5871
|
+
*/
|
|
5872
|
+
/**
|
|
5873
|
+
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
5874
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
5875
|
+
*/
|
|
5897
5876
|
/**
|
|
5898
5877
|
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
5899
5878
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
@@ -6008,18 +5987,17 @@ var dataStructureTyped = (() => {
|
|
|
6008
5987
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
6009
5988
|
}
|
|
6010
5989
|
/**
|
|
6011
|
-
|
|
6012
|
-
|
|
6013
|
-
|
|
6014
|
-
|
|
6015
|
-
|
|
6016
|
-
|
|
6017
|
-
|
|
6018
|
-
|
|
6019
|
-
|
|
6020
|
-
|
|
6021
|
-
|
|
6022
|
-
*/
|
|
5990
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
5991
|
+
*
|
|
5992
|
+
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
5993
|
+
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edgeMap.
|
|
5994
|
+
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(VO^3), where VO is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
5995
|
+
*
|
|
5996
|
+
*/
|
|
5997
|
+
/**
|
|
5998
|
+
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
5999
|
+
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
6000
|
+
*/
|
|
6023
6001
|
/**
|
|
6024
6002
|
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
6025
6003
|
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
@@ -6132,29 +6110,28 @@ var dataStructureTyped = (() => {
|
|
|
6132
6110
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
6133
6111
|
}
|
|
6134
6112
|
/**
|
|
6135
|
-
|
|
6136
|
-
|
|
6137
|
-
|
|
6138
|
-
|
|
6139
|
-
|
|
6140
|
-
|
|
6141
|
-
|
|
6142
|
-
|
|
6143
|
-
|
|
6144
|
-
|
|
6145
|
-
|
|
6146
|
-
|
|
6147
|
-
|
|
6148
|
-
|
|
6149
|
-
|
|
6150
|
-
|
|
6151
|
-
|
|
6152
|
-
|
|
6153
|
-
|
|
6154
|
-
|
|
6155
|
-
|
|
6156
|
-
|
|
6157
|
-
*/
|
|
6113
|
+
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
6114
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
6115
|
+
* one to rest pairs
|
|
6116
|
+
*/
|
|
6117
|
+
/**
|
|
6118
|
+
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
6119
|
+
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
6120
|
+
*
|
|
6121
|
+
* one to rest pairs
|
|
6122
|
+
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
6123
|
+
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
6124
|
+
* all other vertexMap in a graph, and optionally detects negative cycles and generates the minimum path.
|
|
6125
|
+
* @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
6126
|
+
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
6127
|
+
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
6128
|
+
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
6129
|
+
* calculate the minimum distance from the source vertex to all other vertexMap in the graph. If `getMin` is set to
|
|
6130
|
+
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
6131
|
+
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertexMap from the source
|
|
6132
|
+
* vertex.
|
|
6133
|
+
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
6134
|
+
*/
|
|
6158
6135
|
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
6159
6136
|
if (getMin === void 0)
|
|
6160
6137
|
getMin = false;
|
|
@@ -6240,13 +6217,12 @@ var dataStructureTyped = (() => {
|
|
|
6240
6217
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
6241
6218
|
}
|
|
6242
6219
|
/**
|
|
6243
|
-
|
|
6244
|
-
|
|
6245
|
-
|
|
6246
|
-
|
|
6247
|
-
|
|
6248
|
-
|
|
6249
|
-
*/
|
|
6220
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
6221
|
+
*/
|
|
6222
|
+
/**
|
|
6223
|
+
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
6224
|
+
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
6225
|
+
*/
|
|
6250
6226
|
/**
|
|
6251
6227
|
* BellmanFord time:O(VE) space:O(VO)
|
|
6252
6228
|
* one to rest pairs
|
|
@@ -6254,27 +6230,26 @@ var dataStructureTyped = (() => {
|
|
|
6254
6230
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
6255
6231
|
*/
|
|
6256
6232
|
/**
|
|
6257
|
-
|
|
6258
|
-
|
|
6259
|
-
|
|
6260
|
-
|
|
6261
|
-
|
|
6262
|
-
|
|
6263
|
-
|
|
6264
|
-
|
|
6265
|
-
|
|
6266
|
-
|
|
6267
|
-
|
|
6268
|
-
|
|
6269
|
-
|
|
6270
|
-
|
|
6271
|
-
|
|
6272
|
-
|
|
6273
|
-
|
|
6274
|
-
|
|
6275
|
-
|
|
6276
|
-
|
|
6277
|
-
*/
|
|
6233
|
+
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
6234
|
+
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
6235
|
+
* Not support graph with negative weight cycle
|
|
6236
|
+
* all pairs
|
|
6237
|
+
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
|
|
6238
|
+
*/
|
|
6239
|
+
/**
|
|
6240
|
+
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
6241
|
+
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
6242
|
+
*
|
|
6243
|
+
* Not support graph with negative weight cycle
|
|
6244
|
+
* all pairs
|
|
6245
|
+
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
|
|
6246
|
+
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertexMap in a
|
|
6247
|
+
* graph.
|
|
6248
|
+
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
6249
|
+
* property is a 2D array of numbers representing the shortest path costs between vertexMap in a graph. The
|
|
6250
|
+
* `predecessor` property is a 2D array of vertexMap (or `undefined`) representing the predecessor vertexMap in the shortest
|
|
6251
|
+
* path between vertexMap in the
|
|
6252
|
+
*/
|
|
6278
6253
|
floydWarshall() {
|
|
6279
6254
|
var _a;
|
|
6280
6255
|
const idAndVertices = [...this._vertexMap];
|
|
@@ -7762,29 +7737,31 @@ var dataStructureTyped = (() => {
|
|
|
7762
7737
|
};
|
|
7763
7738
|
var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
7764
7739
|
/**
|
|
7765
|
-
* The constructor function initializes a binary tree object with optional
|
|
7766
|
-
* @param [
|
|
7740
|
+
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
|
|
7741
|
+
* @param [keysOrNodesOrEntriesOrRawElements] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
7767
7742
|
* nodes to be added to the binary tree.
|
|
7768
7743
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
7769
7744
|
* configuration options for the binary tree. In this case, it is of type
|
|
7770
7745
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
7771
7746
|
* required.
|
|
7772
7747
|
*/
|
|
7773
|
-
constructor(
|
|
7748
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
7774
7749
|
super();
|
|
7775
7750
|
__publicField(this, "iterationType", "ITERATIVE");
|
|
7776
7751
|
__publicField(this, "_root");
|
|
7777
|
-
__publicField(this, "_size");
|
|
7752
|
+
__publicField(this, "_size", 0);
|
|
7778
7753
|
__publicField(this, "_NIL", new BinaryTreeNode(NaN));
|
|
7754
|
+
__publicField(this, "_toEntryFn");
|
|
7779
7755
|
__publicField(this, "_DEFAULT_CALLBACK", (node) => node ? node.key : void 0);
|
|
7780
7756
|
if (options) {
|
|
7781
|
-
const { iterationType } = options;
|
|
7757
|
+
const { iterationType, toEntryFn } = options;
|
|
7782
7758
|
if (iterationType)
|
|
7783
7759
|
this.iterationType = iterationType;
|
|
7760
|
+
if (typeof toEntryFn === "function")
|
|
7761
|
+
this._toEntryFn = toEntryFn;
|
|
7784
7762
|
}
|
|
7785
|
-
|
|
7786
|
-
|
|
7787
|
-
this.addMany(keysOrNodesOrEntries);
|
|
7763
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
7764
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
7788
7765
|
}
|
|
7789
7766
|
/**
|
|
7790
7767
|
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
@@ -7808,6 +7785,13 @@ var dataStructureTyped = (() => {
|
|
|
7808
7785
|
get NIL() {
|
|
7809
7786
|
return this._NIL;
|
|
7810
7787
|
}
|
|
7788
|
+
/**
|
|
7789
|
+
* The function returns the value of the _toEntryFn property.
|
|
7790
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
7791
|
+
*/
|
|
7792
|
+
get toEntryFn() {
|
|
7793
|
+
return this._toEntryFn;
|
|
7794
|
+
}
|
|
7811
7795
|
/**
|
|
7812
7796
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
7813
7797
|
* @param {K} key - The key for the new node.
|
|
@@ -7828,36 +7812,42 @@ var dataStructureTyped = (() => {
|
|
|
7828
7812
|
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
7829
7813
|
}
|
|
7830
7814
|
/**
|
|
7831
|
-
* The function `
|
|
7832
|
-
*
|
|
7815
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
|
|
7816
|
+
* into a node object.
|
|
7817
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
7818
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
7833
7819
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
7834
|
-
* `
|
|
7835
|
-
*
|
|
7836
|
-
* @returns
|
|
7820
|
+
* `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
|
|
7821
|
+
* key-value pair. If provided, it will be used to create a node with the specified key and value.
|
|
7822
|
+
* @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
|
|
7823
|
+
* or `undefined`.
|
|
7837
7824
|
*/
|
|
7838
|
-
|
|
7839
|
-
if (
|
|
7825
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
7826
|
+
if (keyOrNodeOrEntryOrRawElement === void 0)
|
|
7840
7827
|
return;
|
|
7841
|
-
|
|
7842
|
-
|
|
7843
|
-
|
|
7844
|
-
|
|
7845
|
-
|
|
7846
|
-
|
|
7828
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
7829
|
+
return null;
|
|
7830
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
7831
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
7832
|
+
if (this.toEntryFn) {
|
|
7833
|
+
const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
7834
|
+
if (key)
|
|
7835
|
+
return this.createNode(key, entryValue != null ? entryValue : value);
|
|
7836
|
+
else
|
|
7847
7837
|
return;
|
|
7848
|
-
} else if (key === null) {
|
|
7849
|
-
node = null;
|
|
7850
|
-
} else {
|
|
7851
|
-
node = this.createNode(key, value2);
|
|
7852
|
-
}
|
|
7853
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
7854
|
-
node = keyOrNodeOrEntry;
|
|
7855
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
7856
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
7857
|
-
} else {
|
|
7858
|
-
return;
|
|
7859
7838
|
}
|
|
7860
|
-
|
|
7839
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
7840
|
+
const [key, value2] = keyOrNodeOrEntryOrRawElement;
|
|
7841
|
+
if (key === void 0)
|
|
7842
|
+
return;
|
|
7843
|
+
else if (key === null)
|
|
7844
|
+
return null;
|
|
7845
|
+
else
|
|
7846
|
+
return this.createNode(key, value2);
|
|
7847
|
+
}
|
|
7848
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
7849
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value);
|
|
7850
|
+
return;
|
|
7861
7851
|
}
|
|
7862
7852
|
/**
|
|
7863
7853
|
* Time Complexity: O(n)
|
|
@@ -7867,56 +7857,56 @@ var dataStructureTyped = (() => {
|
|
|
7867
7857
|
* Time Complexity: O(n)
|
|
7868
7858
|
* Space Complexity: O(log n)
|
|
7869
7859
|
*
|
|
7870
|
-
* The
|
|
7871
|
-
*
|
|
7872
|
-
* @param {
|
|
7873
|
-
* `
|
|
7874
|
-
*
|
|
7875
|
-
*
|
|
7876
|
-
*
|
|
7877
|
-
*
|
|
7878
|
-
*
|
|
7879
|
-
*/
|
|
7880
|
-
ensureNode(
|
|
7881
|
-
if (
|
|
7860
|
+
* The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
|
|
7861
|
+
* node if it is a key or entry.
|
|
7862
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
7863
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
|
|
7864
|
+
* a raw element.
|
|
7865
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
7866
|
+
* parameter that specifies the type of iteration to be used when searching for a node. It has a
|
|
7867
|
+
* default value of `'ITERATIVE'`.
|
|
7868
|
+
* @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
|
|
7869
|
+
*/
|
|
7870
|
+
ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = "ITERATIVE") {
|
|
7871
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
7872
|
+
return null;
|
|
7873
|
+
if (keyOrNodeOrEntryOrRawElement === void 0)
|
|
7882
7874
|
return;
|
|
7883
|
-
if (this.
|
|
7884
|
-
return
|
|
7885
|
-
|
|
7886
|
-
|
|
7887
|
-
|
|
7875
|
+
if (keyOrNodeOrEntryOrRawElement === this.NIL)
|
|
7876
|
+
return;
|
|
7877
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
7878
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
7879
|
+
if (this.toEntryFn) {
|
|
7880
|
+
const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
7881
|
+
if (key)
|
|
7882
|
+
return this.getNodeByKey(key);
|
|
7883
|
+
}
|
|
7884
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
7885
|
+
const key = keyOrNodeOrEntryOrRawElement[0];
|
|
7888
7886
|
if (key === null)
|
|
7889
7887
|
return null;
|
|
7890
7888
|
if (key === void 0)
|
|
7891
7889
|
return;
|
|
7892
7890
|
return this.getNodeByKey(key, iterationType);
|
|
7893
7891
|
}
|
|
7894
|
-
if (
|
|
7895
|
-
return
|
|
7896
|
-
|
|
7897
|
-
return;
|
|
7898
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
7899
|
-
}
|
|
7900
|
-
/**
|
|
7901
|
-
* The function checks if a given node is a real node or null.
|
|
7902
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7903
|
-
* @returns a boolean value.
|
|
7904
|
-
*/
|
|
7905
|
-
isNodeOrNull(node) {
|
|
7906
|
-
return this.isRealNode(node) || node === null;
|
|
7892
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
7893
|
+
return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
|
|
7894
|
+
return;
|
|
7907
7895
|
}
|
|
7908
7896
|
/**
|
|
7909
|
-
* The function
|
|
7910
|
-
* @param
|
|
7911
|
-
*
|
|
7897
|
+
* The function checks if the input is an instance of the BinaryTreeNode class.
|
|
7898
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
7899
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
7900
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
7901
|
+
* an instance of the `BinaryTreeNode` class.
|
|
7912
7902
|
*/
|
|
7913
|
-
isNode(
|
|
7914
|
-
return
|
|
7903
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
7904
|
+
return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
|
|
7915
7905
|
}
|
|
7916
7906
|
/**
|
|
7917
|
-
* The function checks if a given node is a
|
|
7918
|
-
*
|
|
7919
|
-
*
|
|
7907
|
+
* The function checks if a given node is a valid node in a binary search tree.
|
|
7908
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
7909
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
7920
7910
|
* @returns a boolean value.
|
|
7921
7911
|
*/
|
|
7922
7912
|
isRealNode(node) {
|
|
@@ -7925,21 +7915,64 @@ var dataStructureTyped = (() => {
|
|
|
7925
7915
|
return this.isNode(node);
|
|
7926
7916
|
}
|
|
7927
7917
|
/**
|
|
7928
|
-
* The function checks if a given node is a
|
|
7929
|
-
* @param {
|
|
7918
|
+
* The function checks if a given node is a real node or null.
|
|
7919
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
7920
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
7921
|
+
* @returns a boolean value.
|
|
7922
|
+
*/
|
|
7923
|
+
isNodeOrNull(node) {
|
|
7924
|
+
return this.isRealNode(node) || node === null;
|
|
7925
|
+
}
|
|
7926
|
+
/**
|
|
7927
|
+
* The function checks if a given node is equal to the NIL value.
|
|
7928
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
7929
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
7930
7930
|
* @returns a boolean value.
|
|
7931
7931
|
*/
|
|
7932
7932
|
isNIL(node) {
|
|
7933
7933
|
return node === this.NIL;
|
|
7934
7934
|
}
|
|
7935
7935
|
/**
|
|
7936
|
-
* The function checks if
|
|
7937
|
-
*
|
|
7938
|
-
*
|
|
7936
|
+
* The function checks if the input is an array with two elements, indicating it is a binary tree
|
|
7937
|
+
* node entry.
|
|
7938
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
7939
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
7940
|
+
* @returns a boolean value.
|
|
7941
|
+
*/
|
|
7942
|
+
isEntry(keyOrNodeOrEntryOrRawElement) {
|
|
7943
|
+
return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
|
|
7944
|
+
}
|
|
7945
|
+
/**
|
|
7946
|
+
* The function checks if a given value is a valid key by evaluating its type and value.
|
|
7947
|
+
* @param {any} key - The `key` parameter can be of any type. It is the value that we want to check
|
|
7948
|
+
* if it is a valid key.
|
|
7949
|
+
* @param [isCheckValueOf=true] - The `isCheckValueOf` parameter is a boolean flag that determines
|
|
7950
|
+
* whether the function should check the valueOf() method of an object when the key is of type
|
|
7951
|
+
* 'object'. If `isCheckValueOf` is true, the function will recursively call itself with the value
|
|
7952
|
+
* returned by key.valueOf().
|
|
7939
7953
|
* @returns a boolean value.
|
|
7940
7954
|
*/
|
|
7941
|
-
|
|
7942
|
-
|
|
7955
|
+
isKey(key, isCheckValueOf = true) {
|
|
7956
|
+
if (key === null)
|
|
7957
|
+
return true;
|
|
7958
|
+
const keyType = typeof key;
|
|
7959
|
+
if (keyType === "string" || keyType === "bigint" || keyType === "boolean")
|
|
7960
|
+
return true;
|
|
7961
|
+
if (keyType === "number")
|
|
7962
|
+
return !isNaN(key);
|
|
7963
|
+
if (keyType === "symbol" || keyType === "undefined")
|
|
7964
|
+
return false;
|
|
7965
|
+
if (keyType === "function")
|
|
7966
|
+
return this.isKey(key());
|
|
7967
|
+
if (keyType === "object") {
|
|
7968
|
+
if (typeof key.toString === "function")
|
|
7969
|
+
return true;
|
|
7970
|
+
if (isCheckValueOf && typeof key.valueOf === "function") {
|
|
7971
|
+
this.isKey(key.valueOf(), false);
|
|
7972
|
+
}
|
|
7973
|
+
return false;
|
|
7974
|
+
}
|
|
7975
|
+
return false;
|
|
7943
7976
|
}
|
|
7944
7977
|
/**
|
|
7945
7978
|
* Time Complexity O(n)
|
|
@@ -7949,14 +7982,20 @@ var dataStructureTyped = (() => {
|
|
|
7949
7982
|
* Time Complexity O(n)
|
|
7950
7983
|
* Space Complexity O(1)
|
|
7951
7984
|
*
|
|
7952
|
-
* The `add` function
|
|
7953
|
-
*
|
|
7954
|
-
* @param
|
|
7955
|
-
*
|
|
7956
|
-
*
|
|
7957
|
-
|
|
7958
|
-
|
|
7959
|
-
|
|
7985
|
+
* The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
|
|
7986
|
+
* and finding the appropriate insertion position.
|
|
7987
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
7988
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
|
|
7989
|
+
* node, entry, or raw element to be added to the tree. It can also accept a value of type
|
|
7990
|
+
* `KeyOrNodeOrEntry<K, V, NODE>
|
|
7991
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
7992
|
+
* key being added to the tree. It represents the value that will be stored in the tree for the given
|
|
7993
|
+
* key.
|
|
7994
|
+
* @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
7995
|
+
* insertion position cannot be found or if there are duplicate keys.
|
|
7996
|
+
*/
|
|
7997
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
7998
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
7960
7999
|
if (newNode === void 0)
|
|
7961
8000
|
return false;
|
|
7962
8001
|
if (!this.root) {
|
|
@@ -8004,19 +8043,23 @@ var dataStructureTyped = (() => {
|
|
|
8004
8043
|
* Time Complexity: O(k * n)
|
|
8005
8044
|
* Space Complexity: O(1)
|
|
8006
8045
|
*
|
|
8007
|
-
* The `addMany` function takes in
|
|
8008
|
-
* adds each node with its corresponding value to
|
|
8009
|
-
*
|
|
8010
|
-
* @param
|
|
8011
|
-
*
|
|
8046
|
+
* The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
|
|
8047
|
+
* optional iterable of values, and adds each key or node or entry with its corresponding value to a
|
|
8048
|
+
* data structure, returning an array of booleans indicating whether each insertion was successful.
|
|
8049
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
8050
|
+
* elements. These elements will be added to the data structure.
|
|
8051
|
+
* @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
|
|
8052
|
+
* in the `keysOrNodesOrEntriesOrRawElements` parameter.
|
|
8053
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
8054
|
+
* successfully added to the data structure.
|
|
8012
8055
|
*/
|
|
8013
|
-
addMany(
|
|
8056
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values) {
|
|
8014
8057
|
const inserted = [];
|
|
8015
8058
|
let valuesIterator;
|
|
8016
8059
|
if (values) {
|
|
8017
8060
|
valuesIterator = values[Symbol.iterator]();
|
|
8018
8061
|
}
|
|
8019
|
-
for (const
|
|
8062
|
+
for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
|
|
8020
8063
|
let value = void 0;
|
|
8021
8064
|
if (valuesIterator) {
|
|
8022
8065
|
const valueResult = valuesIterator.next();
|
|
@@ -8024,7 +8067,7 @@ var dataStructureTyped = (() => {
|
|
|
8024
8067
|
value = valueResult.value;
|
|
8025
8068
|
}
|
|
8026
8069
|
}
|
|
8027
|
-
inserted.push(this.add(
|
|
8070
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
|
|
8028
8071
|
}
|
|
8029
8072
|
return inserted;
|
|
8030
8073
|
}
|
|
@@ -8037,38 +8080,36 @@ var dataStructureTyped = (() => {
|
|
|
8037
8080
|
* Time Complexity: O(k * n)
|
|
8038
8081
|
* Space Complexity: O(1)
|
|
8039
8082
|
*
|
|
8040
|
-
* The `refill` function clears the current data and adds new
|
|
8041
|
-
* @param
|
|
8042
|
-
* KeyOrNodeOrEntry<K, V, NODE
|
|
8043
|
-
* @param [values] - The `values` parameter is an optional iterable
|
|
8044
|
-
*
|
|
8045
|
-
*
|
|
8046
|
-
* `keysOrNodesOrEntries` iterable
|
|
8083
|
+
* The `refill` function clears the current data and adds new data to the collection.
|
|
8084
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
|
|
8085
|
+
* elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry<K, V, NODE>).
|
|
8086
|
+
* @param [values] - The `values` parameter is an optional iterable of values that will be associated
|
|
8087
|
+
* with the keys or nodes being added. If provided, the values will be assigned to the corresponding
|
|
8088
|
+
* keys or nodes. If not provided, the values will be set to `undefined`.
|
|
8047
8089
|
*/
|
|
8048
|
-
refill(
|
|
8090
|
+
refill(keysOrNodesOrEntriesOrRawElements, values) {
|
|
8049
8091
|
this.clear();
|
|
8050
|
-
this.addMany(
|
|
8051
|
-
}
|
|
8052
|
-
/**
|
|
8053
|
-
|
|
8054
|
-
|
|
8055
|
-
|
|
8056
|
-
|
|
8057
|
-
|
|
8058
|
-
|
|
8059
|
-
|
|
8060
|
-
|
|
8061
|
-
|
|
8062
|
-
|
|
8063
|
-
|
|
8064
|
-
|
|
8065
|
-
|
|
8066
|
-
|
|
8067
|
-
|
|
8068
|
-
|
|
8069
|
-
|
|
8070
|
-
|
|
8071
|
-
*/
|
|
8092
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements, values);
|
|
8093
|
+
}
|
|
8094
|
+
/**
|
|
8095
|
+
* Time Complexity: O(n)
|
|
8096
|
+
* Space Complexity: O(1)
|
|
8097
|
+
*/
|
|
8098
|
+
/**
|
|
8099
|
+
* Time Complexity: O(n)
|
|
8100
|
+
* Space Complexity: O(1)
|
|
8101
|
+
*
|
|
8102
|
+
* The above function is a TypeScript implementation of deleting a node from a binary tree, returning
|
|
8103
|
+
* the deleted node and the node that needs to be balanced.
|
|
8104
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8105
|
+
* used to identify the node that needs to be deleted from the binary tree. It can be of any type
|
|
8106
|
+
* that is returned by the callback function.
|
|
8107
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
8108
|
+
* identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
|
|
8109
|
+
* interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
|
|
8110
|
+
* type `NODE
|
|
8111
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
8112
|
+
*/
|
|
8072
8113
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
8073
8114
|
const deletedResult = [];
|
|
8074
8115
|
if (!this.root)
|
|
@@ -8117,28 +8158,27 @@ var dataStructureTyped = (() => {
|
|
|
8117
8158
|
*/
|
|
8118
8159
|
/**
|
|
8119
8160
|
* Time Complexity: O(n)
|
|
8120
|
-
* Space Complexity: O(k + log n)
|
|
8161
|
+
* Space Complexity: O(k + log n)
|
|
8121
8162
|
*
|
|
8122
|
-
* The function `getNodes`
|
|
8123
|
-
*
|
|
8163
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
8164
|
+
* recursive or iterative approach.
|
|
8124
8165
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8125
|
-
* that
|
|
8126
|
-
* callback function
|
|
8127
|
-
*
|
|
8128
|
-
*
|
|
8129
|
-
*
|
|
8130
|
-
*
|
|
8131
|
-
*
|
|
8132
|
-
*
|
|
8133
|
-
*
|
|
8134
|
-
*
|
|
8135
|
-
*
|
|
8136
|
-
*
|
|
8137
|
-
*
|
|
8138
|
-
*
|
|
8139
|
-
*
|
|
8140
|
-
*
|
|
8141
|
-
* @returns an array of nodes of type `NODE`.
|
|
8166
|
+
* that is used to identify the nodes. It can be of any type and is used to match against the result
|
|
8167
|
+
* of the callback function for each node.
|
|
8168
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
8169
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
8170
|
+
* `callback` function is optional and defaults to a default callback function
|
|
8171
|
+
* (`this._DEFAULT_CALLBACK`) if not provided.
|
|
8172
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
|
|
8173
|
+
* the identifier or all nodes that match the identifier. If set to true, only the first matching
|
|
8174
|
+
* node will be returned. If set to false, all matching nodes will be returned. The default value is
|
|
8175
|
+
* false.
|
|
8176
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
8177
|
+
* point for the search. It can be either a node object, a key-value pair, or a key. If it is not
|
|
8178
|
+
* provided, the `root` of the data structure is used as the starting point.
|
|
8179
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
8180
|
+
* iteration to be performed on the nodes of a binary tree. It can have two possible values:
|
|
8181
|
+
* @returns an array of NODE objects.
|
|
8142
8182
|
*/
|
|
8143
8183
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8144
8184
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -8182,24 +8222,21 @@ var dataStructureTyped = (() => {
|
|
|
8182
8222
|
*/
|
|
8183
8223
|
/**
|
|
8184
8224
|
* Time Complexity: O(n)
|
|
8185
|
-
* Space Complexity: O(log n)
|
|
8225
|
+
* Space Complexity: O(log n).
|
|
8186
8226
|
*
|
|
8187
|
-
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
8188
|
-
*
|
|
8227
|
+
* The function `getNode` returns the first node that matches the given identifier and callback,
|
|
8228
|
+
* starting from the specified root node and using the specified iteration type.
|
|
8189
8229
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8190
|
-
* used to identify the node you want to retrieve. It can be of any type that is
|
|
8191
|
-
* callback function
|
|
8192
|
-
*
|
|
8193
|
-
*
|
|
8194
|
-
*
|
|
8195
|
-
*
|
|
8196
|
-
*
|
|
8197
|
-
*
|
|
8198
|
-
*
|
|
8199
|
-
* @
|
|
8200
|
-
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
8201
|
-
* nodes are visited during the search.
|
|
8202
|
-
* @returns a value of type `NODE | null | undefined`.
|
|
8230
|
+
* used to identify the node you want to retrieve. It can be of any type that is the return type of
|
|
8231
|
+
* the `C` callback function, or it can be `null` or `undefined`.
|
|
8232
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
8233
|
+
* node matches the desired criteria. It should return a value that can be used to identify the node.
|
|
8234
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
8235
|
+
* point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
|
|
8236
|
+
* a node entry. If not provided, the search will start from the root of the tree.
|
|
8237
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
8238
|
+
* of iteration to be performed when searching for nodes. It can have one of the following values:
|
|
8239
|
+
* @returns The method is returning a NODE object, or null, or undefined.
|
|
8203
8240
|
*/
|
|
8204
8241
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8205
8242
|
var _a;
|
|
@@ -8213,15 +8250,13 @@ var dataStructureTyped = (() => {
|
|
|
8213
8250
|
* Time Complexity: O(n)
|
|
8214
8251
|
* Space Complexity: O(log n)
|
|
8215
8252
|
*
|
|
8216
|
-
* The function `getNodeByKey`
|
|
8217
|
-
*
|
|
8218
|
-
*
|
|
8219
|
-
*
|
|
8220
|
-
*
|
|
8221
|
-
*
|
|
8222
|
-
*
|
|
8223
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
8224
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
8253
|
+
* The function `getNodeByKey` returns a node with a specific key value from a tree structure.
|
|
8254
|
+
* @param {K} key - The key parameter is the value that you want to search for in the tree. It is
|
|
8255
|
+
* used to find the node with the matching key value.
|
|
8256
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
8257
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
8258
|
+
* It has a default value of `'ITERATIVE'`.
|
|
8259
|
+
* @returns a value of type NODE, null, or undefined.
|
|
8225
8260
|
*/
|
|
8226
8261
|
getNodeByKey(key, iterationType = "ITERATIVE") {
|
|
8227
8262
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
@@ -8234,23 +8269,22 @@ var dataStructureTyped = (() => {
|
|
|
8234
8269
|
* Time Complexity: O(n)
|
|
8235
8270
|
* Space Complexity: O(log n)
|
|
8236
8271
|
*
|
|
8237
|
-
* The function `get`
|
|
8238
|
-
*
|
|
8272
|
+
* The function `get` in TypeScript overrides the base class method and returns the value associated
|
|
8273
|
+
* with the given identifier.
|
|
8239
8274
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8240
|
-
* used to identify the node in the binary tree. It can be of any type that is
|
|
8275
|
+
* used to identify the node in the binary tree. It can be of any type that is returned by the
|
|
8241
8276
|
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
8242
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
8243
|
-
* the
|
|
8244
|
-
*
|
|
8245
|
-
*
|
|
8246
|
-
*
|
|
8247
|
-
*
|
|
8248
|
-
*
|
|
8249
|
-
*
|
|
8250
|
-
*
|
|
8251
|
-
*
|
|
8252
|
-
*
|
|
8253
|
-
* found, `undefined` is returned.
|
|
8277
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
8278
|
+
* node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
8279
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
8280
|
+
* point for the search in the binary tree. It can be either a root node of the tree or a key, node,
|
|
8281
|
+
* or entry object that exists in the tree. If no specific starting point is provided, the search
|
|
8282
|
+
* will begin from the root of the
|
|
8283
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
8284
|
+
* of iteration to be performed when searching for a node in the tree. It can have one of the
|
|
8285
|
+
* following values:
|
|
8286
|
+
* @returns The method is returning the value associated with the specified identifier in the binary
|
|
8287
|
+
* tree.
|
|
8254
8288
|
*/
|
|
8255
8289
|
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8256
8290
|
var _a;
|
|
@@ -8258,28 +8292,27 @@ var dataStructureTyped = (() => {
|
|
|
8258
8292
|
}
|
|
8259
8293
|
/**
|
|
8260
8294
|
* Time Complexity: O(n)
|
|
8261
|
-
* Space Complexity: O(log n)
|
|
8295
|
+
* Space Complexity: O(log n)
|
|
8262
8296
|
*/
|
|
8263
8297
|
/**
|
|
8264
8298
|
* Time Complexity: O(n)
|
|
8265
|
-
* Space Complexity: O(log n)
|
|
8299
|
+
* Space Complexity: O(log n)
|
|
8266
8300
|
*
|
|
8267
|
-
* The function checks if a
|
|
8301
|
+
* The `has` function checks if a given identifier exists in the data structure and returns a boolean
|
|
8302
|
+
* value.
|
|
8268
8303
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8269
|
-
*
|
|
8270
|
-
* callback function `C`. It can also be `null` or `undefined` if
|
|
8271
|
-
*
|
|
8272
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
8273
|
-
*
|
|
8274
|
-
*
|
|
8275
|
-
*
|
|
8276
|
-
*
|
|
8277
|
-
*
|
|
8278
|
-
*
|
|
8279
|
-
*
|
|
8280
|
-
*
|
|
8281
|
-
* be performed in a pre-order, in-order, or post-order manner.
|
|
8282
|
-
* @returns a boolean value.
|
|
8304
|
+
* used to identify a specific node or entry in the data structure. It can be of any type that is
|
|
8305
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if no specific
|
|
8306
|
+
* identifier is provided.
|
|
8307
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine
|
|
8308
|
+
* whether a node should be included in the result or not. It is of type `C`, which extends the
|
|
8309
|
+
* `BTNCallback<NODE>` type.
|
|
8310
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
8311
|
+
* point for the iteration in the data structure. It can be either a root node, a key-value pair, or
|
|
8312
|
+
* a node entry. If not specified, it defaults to the root of the data structure.
|
|
8313
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
8314
|
+
* of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
|
|
8315
|
+
* @returns The method is returning a boolean value.
|
|
8283
8316
|
*/
|
|
8284
8317
|
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8285
8318
|
callback = this._ensureCallback(identifier, callback);
|
|
@@ -8323,9 +8356,10 @@ var dataStructureTyped = (() => {
|
|
|
8323
8356
|
*
|
|
8324
8357
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
8325
8358
|
* height of the tree.
|
|
8326
|
-
* @param {
|
|
8327
|
-
*
|
|
8328
|
-
*
|
|
8359
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
|
|
8360
|
+
* has a default value of `this.root`. It represents the starting point for checking if the tree is
|
|
8361
|
+
* perfectly balanced. It can be either a root node (`R`), a key or node or entry
|
|
8362
|
+
* (`KeyOrNodeOrEntry<K, V, NODE
|
|
8329
8363
|
* @returns a boolean value.
|
|
8330
8364
|
*/
|
|
8331
8365
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
@@ -8339,12 +8373,14 @@ var dataStructureTyped = (() => {
|
|
|
8339
8373
|
* Time Complexity: O(n)
|
|
8340
8374
|
* Space Complexity: O(1)
|
|
8341
8375
|
*
|
|
8342
|
-
* The function `
|
|
8343
|
-
* @param {
|
|
8344
|
-
*
|
|
8345
|
-
*
|
|
8346
|
-
*
|
|
8347
|
-
*
|
|
8376
|
+
* The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
|
|
8377
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
8378
|
+
* starting point for checking if a binary search tree (BST) is valid. It can be either a root node
|
|
8379
|
+
* of the BST, a key value of a node in the BST, or an entry object containing both the key and value
|
|
8380
|
+
* of a node in the BST
|
|
8381
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
8382
|
+
* of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
|
|
8383
|
+
* two possible values:
|
|
8348
8384
|
* @returns a boolean value.
|
|
8349
8385
|
*/
|
|
8350
8386
|
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -8394,14 +8430,15 @@ var dataStructureTyped = (() => {
|
|
|
8394
8430
|
* Time Complexity: O(n)
|
|
8395
8431
|
* Space Complexity: O(1)
|
|
8396
8432
|
*
|
|
8397
|
-
* The function calculates the depth of a given node in a
|
|
8398
|
-
* @param {
|
|
8399
|
-
*
|
|
8400
|
-
*
|
|
8401
|
-
* @param {
|
|
8402
|
-
* from which
|
|
8403
|
-
*
|
|
8404
|
-
*
|
|
8433
|
+
* The function calculates the depth of a given node or key in a tree-like data structure.
|
|
8434
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
|
|
8435
|
+
* (representing a root node), or a `KeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
|
|
8436
|
+
* entry).
|
|
8437
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
|
|
8438
|
+
* represents the starting point from which to calculate the depth. It can be either a reference to a
|
|
8439
|
+
* node in the tree or a key-value pair or an entry object. If not provided, the default value is
|
|
8440
|
+
* `this.root`, which refers to the root node
|
|
8441
|
+
* @returns the depth of a node in a tree structure.
|
|
8405
8442
|
*/
|
|
8406
8443
|
getDepth(dist, beginRoot = this.root) {
|
|
8407
8444
|
let distEnsured = this.ensureNode(dist);
|
|
@@ -8422,17 +8459,16 @@ var dataStructureTyped = (() => {
|
|
|
8422
8459
|
*/
|
|
8423
8460
|
/**
|
|
8424
8461
|
* Time Complexity: O(n)
|
|
8425
|
-
* Space Complexity: O(
|
|
8462
|
+
* Space Complexity: O(1)
|
|
8426
8463
|
*
|
|
8427
|
-
* The
|
|
8428
|
-
* iterative
|
|
8429
|
-
* @param {
|
|
8430
|
-
* starting
|
|
8431
|
-
*
|
|
8432
|
-
* @param iterationType - The `iterationType` parameter
|
|
8433
|
-
*
|
|
8434
|
-
*
|
|
8435
|
-
* @returns the height of the binary tree.
|
|
8464
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
8465
|
+
* or iterative approach.
|
|
8466
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
8467
|
+
* starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
|
|
8468
|
+
* node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
|
|
8469
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
8470
|
+
* iteration used to calculate the height of the tree. It can have two possible values:
|
|
8471
|
+
* @returns the maximum height of the binary tree.
|
|
8436
8472
|
*/
|
|
8437
8473
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8438
8474
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -8471,12 +8507,15 @@ var dataStructureTyped = (() => {
|
|
|
8471
8507
|
*
|
|
8472
8508
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
8473
8509
|
* recursive or iterative approach.
|
|
8474
|
-
* @param {
|
|
8475
|
-
* starting
|
|
8476
|
-
*
|
|
8477
|
-
*
|
|
8478
|
-
*
|
|
8479
|
-
*
|
|
8510
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
8511
|
+
* starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
|
|
8512
|
+
* key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
|
|
8513
|
+
* tree.
|
|
8514
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
8515
|
+
* iteration to be used when calculating the minimum height of the tree. It can have two possible
|
|
8516
|
+
* values:
|
|
8517
|
+
* @returns The function `getMinHeight` returns a number, which represents the minimum height of the
|
|
8518
|
+
* binary tree.
|
|
8480
8519
|
*/
|
|
8481
8520
|
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8482
8521
|
var _a, _b, _c;
|
|
@@ -8521,24 +8560,22 @@ var dataStructureTyped = (() => {
|
|
|
8521
8560
|
}
|
|
8522
8561
|
}
|
|
8523
8562
|
/**
|
|
8524
|
-
|
|
8525
|
-
|
|
8526
|
-
|
|
8527
|
-
|
|
8528
|
-
|
|
8529
|
-
|
|
8530
|
-
|
|
8531
|
-
|
|
8532
|
-
|
|
8533
|
-
|
|
8534
|
-
|
|
8535
|
-
|
|
8536
|
-
|
|
8537
|
-
|
|
8538
|
-
|
|
8539
|
-
|
|
8540
|
-
* @returns The function `getPathToRoot` returns an array of nodes (`NODE[]`).
|
|
8541
|
-
*/
|
|
8563
|
+
* Time Complexity: O(log n)
|
|
8564
|
+
* Space Complexity: O(log n)
|
|
8565
|
+
*/
|
|
8566
|
+
/**
|
|
8567
|
+
* Time Complexity: O(log n)
|
|
8568
|
+
* Space Complexity: O(log n)
|
|
8569
|
+
*
|
|
8570
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
8571
|
+
* up to the root node, with an option to reverse the order of the nodes.
|
|
8572
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
|
|
8573
|
+
* type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
8574
|
+
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
8575
|
+
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
8576
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
8577
|
+
* @returns The function `getPathToRoot` returns an array of `NODE` objects.
|
|
8578
|
+
*/
|
|
8542
8579
|
getPathToRoot(beginNode, isReverse = true) {
|
|
8543
8580
|
const result = [];
|
|
8544
8581
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
@@ -8559,15 +8596,14 @@ var dataStructureTyped = (() => {
|
|
|
8559
8596
|
* Time Complexity: O(log n)
|
|
8560
8597
|
* Space Complexity: O(1)
|
|
8561
8598
|
*
|
|
8562
|
-
* The
|
|
8563
|
-
*
|
|
8564
|
-
* @param {
|
|
8565
|
-
* for finding the leftmost node in a binary tree. It can be either a `
|
|
8566
|
-
*
|
|
8567
|
-
* @param iterationType - The `iterationType` parameter is used to
|
|
8568
|
-
*
|
|
8569
|
-
* @returns The function `getLeftMost` returns the leftmost node
|
|
8570
|
-
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
8599
|
+
* The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
|
|
8600
|
+
* iterative traversal.
|
|
8601
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
8602
|
+
* starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
|
|
8603
|
+
* a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
8604
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
8605
|
+
* of iteration to be performed. It can have two possible values:
|
|
8606
|
+
* @returns The function `getLeftMost` returns the leftmost node in a binary tree.
|
|
8571
8607
|
*/
|
|
8572
8608
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8573
8609
|
if (this.isNIL(beginRoot))
|
|
@@ -8599,16 +8635,15 @@ var dataStructureTyped = (() => {
|
|
|
8599
8635
|
* Time Complexity: O(log n)
|
|
8600
8636
|
* Space Complexity: O(1)
|
|
8601
8637
|
*
|
|
8602
|
-
* The
|
|
8638
|
+
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
|
|
8603
8639
|
* iteratively.
|
|
8604
|
-
* @param {
|
|
8605
|
-
* starting
|
|
8606
|
-
* `
|
|
8607
|
-
*
|
|
8608
|
-
*
|
|
8609
|
-
*
|
|
8610
|
-
* @returns The function `getRightMost` returns
|
|
8611
|
-
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
8640
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
8641
|
+
* starting point for finding the rightmost node in a binary tree. It can be either a root node
|
|
8642
|
+
* (`R`), a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
8643
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
8644
|
+
* of iteration to be performed when finding the rightmost node in a binary tree. It can have two
|
|
8645
|
+
* possible values:
|
|
8646
|
+
* @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
|
|
8612
8647
|
*/
|
|
8613
8648
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8614
8649
|
if (this.isNIL(beginRoot))
|
|
@@ -8640,10 +8675,10 @@ var dataStructureTyped = (() => {
|
|
|
8640
8675
|
* Time Complexity: O(log n)
|
|
8641
8676
|
* Space Complexity: O(1)
|
|
8642
8677
|
*
|
|
8643
|
-
* The function returns the predecessor of a given node in a tree.
|
|
8644
|
-
* @param {NODE} node - The parameter
|
|
8678
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
8679
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
|
|
8645
8680
|
* tree.
|
|
8646
|
-
* @returns the predecessor of the given
|
|
8681
|
+
* @returns the predecessor node of the given node.
|
|
8647
8682
|
*/
|
|
8648
8683
|
getPredecessor(node) {
|
|
8649
8684
|
if (this.isRealNode(node.left)) {
|
|
@@ -8668,8 +8703,8 @@ var dataStructureTyped = (() => {
|
|
|
8668
8703
|
*
|
|
8669
8704
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
8670
8705
|
* @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
|
|
8671
|
-
* @returns
|
|
8672
|
-
*
|
|
8706
|
+
* @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
|
|
8707
|
+
* there is no successor, and `undefined` if the input `x` is not a valid node.
|
|
8673
8708
|
*/
|
|
8674
8709
|
getSuccessor(x) {
|
|
8675
8710
|
x = this.ensureNode(x);
|
|
@@ -8686,41 +8721,40 @@ var dataStructureTyped = (() => {
|
|
|
8686
8721
|
return y;
|
|
8687
8722
|
}
|
|
8688
8723
|
/**
|
|
8689
|
-
|
|
8690
|
-
|
|
8691
|
-
|
|
8692
|
-
|
|
8693
|
-
|
|
8694
|
-
|
|
8695
|
-
|
|
8696
|
-
|
|
8697
|
-
|
|
8698
|
-
|
|
8699
|
-
|
|
8700
|
-
|
|
8701
|
-
|
|
8702
|
-
|
|
8703
|
-
|
|
8704
|
-
|
|
8705
|
-
|
|
8706
|
-
|
|
8707
|
-
|
|
8708
|
-
|
|
8709
|
-
|
|
8710
|
-
|
|
8711
|
-
|
|
8712
|
-
|
|
8713
|
-
|
|
8714
|
-
|
|
8715
|
-
|
|
8716
|
-
|
|
8717
|
-
|
|
8718
|
-
|
|
8719
|
-
|
|
8720
|
-
|
|
8721
|
-
|
|
8722
|
-
|
|
8723
|
-
switch (pattern) {
|
|
8724
|
+
* Time complexity: O(n)
|
|
8725
|
+
* Space complexity: O(n)
|
|
8726
|
+
*/
|
|
8727
|
+
/**
|
|
8728
|
+
* Time complexity: O(n)
|
|
8729
|
+
* Space complexity: O(n)
|
|
8730
|
+
*
|
|
8731
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
8732
|
+
* function on each node according to a specified pattern and iteration type.
|
|
8733
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
8734
|
+
* visited during the depth-first search. It takes a node as an argument and returns a value. The
|
|
8735
|
+
* return type of the callback function is determined by the generic type `C`.
|
|
8736
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
|
|
8737
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
8738
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
8739
|
+
* point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
|
|
8740
|
+
* is a key or key-value pair, the method will find the corresponding node in the tree and start the
|
|
8741
|
+
* search from there.
|
|
8742
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
|
|
8743
|
+
* type of iteration to use during the depth-first search. It can have two possible values:
|
|
8744
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
8745
|
+
* whether or not to include null values in the depth-first search traversal. If `includeNull` is set
|
|
8746
|
+
* to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
|
|
8747
|
+
* values will
|
|
8748
|
+
* @returns an array of the return types of the callback function.
|
|
8749
|
+
*/
|
|
8750
|
+
dfs(callback = this._DEFAULT_CALLBACK, pattern = "IN", beginRoot = this.root, iterationType = "ITERATIVE", includeNull = false) {
|
|
8751
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
8752
|
+
if (!beginRoot)
|
|
8753
|
+
return [];
|
|
8754
|
+
const ans = [];
|
|
8755
|
+
if (iterationType === "RECURSIVE") {
|
|
8756
|
+
const dfs = (node) => {
|
|
8757
|
+
switch (pattern) {
|
|
8724
8758
|
case "IN":
|
|
8725
8759
|
if (includeNull) {
|
|
8726
8760
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
@@ -8820,22 +8854,23 @@ var dataStructureTyped = (() => {
|
|
|
8820
8854
|
* Time complexity: O(n)
|
|
8821
8855
|
* Space complexity: O(n)
|
|
8822
8856
|
*
|
|
8823
|
-
* The `bfs` function performs a breadth-first search
|
|
8824
|
-
*
|
|
8857
|
+
* The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
|
|
8858
|
+
* on each node and returning an array of the results.
|
|
8825
8859
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8826
|
-
* the breadth-first search traversal. It takes a single
|
|
8860
|
+
* the breadth-first search traversal. It takes a single argument, which is the current node being
|
|
8827
8861
|
* visited, and returns a value of any type.
|
|
8828
|
-
* @param {
|
|
8829
|
-
* starting
|
|
8830
|
-
* or
|
|
8831
|
-
*
|
|
8832
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
8833
|
-
*
|
|
8834
|
-
* @param [includeNull=false] - The `includeNull` parameter is a boolean
|
|
8835
|
-
* to include null values in the breadth-first search traversal. If
|
|
8836
|
-
* `true`, null values will be included in the traversal
|
|
8837
|
-
*
|
|
8838
|
-
*
|
|
8862
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
8863
|
+
* starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
|
|
8864
|
+
* or entry object. If no value is provided, the `root` property of the class is used as the default
|
|
8865
|
+
* starting point.
|
|
8866
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
8867
|
+
* iteration to be performed. It can have two possible values:
|
|
8868
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
8869
|
+
* whether or not to include null values in the breadth-first search (BFS) traversal. If
|
|
8870
|
+
* `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
|
|
8871
|
+
* set to `false
|
|
8872
|
+
* @returns The function `bfs` returns an array of values that are the result of invoking the
|
|
8873
|
+
* `callback` function on each node in the breadth-first order traversal of the binary tree.
|
|
8839
8874
|
*/
|
|
8840
8875
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
8841
8876
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -8895,18 +8930,18 @@ var dataStructureTyped = (() => {
|
|
|
8895
8930
|
* Space complexity: O(n)
|
|
8896
8931
|
*
|
|
8897
8932
|
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
8898
|
-
* a binary tree and contains the
|
|
8899
|
-
* level.
|
|
8933
|
+
* a binary tree and contains the results of applying a callback function to the nodes at that level.
|
|
8900
8934
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8901
|
-
* the tree. It takes a
|
|
8902
|
-
*
|
|
8903
|
-
* @param {
|
|
8904
|
-
* starting
|
|
8905
|
-
*
|
|
8906
|
-
*
|
|
8907
|
-
*
|
|
8935
|
+
* the tree. It takes a node as an argument and returns a value. The return type of the callback
|
|
8936
|
+
* function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
|
|
8937
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
8938
|
+
* starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
|
|
8939
|
+
* entry. If no value is provided, the `root` property of the class is used as the default starting
|
|
8940
|
+
* point.
|
|
8941
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
8942
|
+
* iteration to be performed on the binary tree. It can have two possible values:
|
|
8908
8943
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
8909
|
-
* whether to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
8944
|
+
* whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
8910
8945
|
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
8911
8946
|
* be excluded
|
|
8912
8947
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
@@ -8968,17 +9003,17 @@ var dataStructureTyped = (() => {
|
|
|
8968
9003
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
8969
9004
|
* algorithm.
|
|
8970
9005
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8971
|
-
* the tree. It takes a single
|
|
8972
|
-
*
|
|
8973
|
-
*
|
|
8974
|
-
*
|
|
9006
|
+
* the tree. It takes a single argument, which is the current node, and can return any value. The
|
|
9007
|
+
* return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
|
|
9008
|
+
* the return
|
|
9009
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
|
|
9010
|
+
* to specify the order in which the nodes of a binary tree are traversed. It can take one of the
|
|
8975
9011
|
* following values:
|
|
8976
|
-
* @param {
|
|
8977
|
-
* for the traversal. It can be
|
|
8978
|
-
* the root of the tree
|
|
8979
|
-
* @returns The function `morris` returns an array of values that are the
|
|
8980
|
-
*
|
|
8981
|
-
* by the return type of the `callback` function.
|
|
9012
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
9013
|
+
* point for the traversal. It can be either a node object, a key, or an entry object. If no value is
|
|
9014
|
+
* provided, the `root` of the tree is used as the starting point.
|
|
9015
|
+
* @returns The function `morris` returns an array of values that are the return values of the
|
|
9016
|
+
* callback function `callback`.
|
|
8982
9017
|
*/
|
|
8983
9018
|
morris(callback = this._DEFAULT_CALLBACK, pattern = "IN", beginRoot = this.root) {
|
|
8984
9019
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -9069,8 +9104,7 @@ var dataStructureTyped = (() => {
|
|
|
9069
9104
|
* Time complexity: O(n)
|
|
9070
9105
|
* Space complexity: O(n)
|
|
9071
9106
|
*
|
|
9072
|
-
* The `clone` function creates a
|
|
9073
|
-
* the new tree.
|
|
9107
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
9074
9108
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
9075
9109
|
*/
|
|
9076
9110
|
clone() {
|
|
@@ -9096,16 +9130,16 @@ var dataStructureTyped = (() => {
|
|
|
9096
9130
|
* Time Complexity: O(n)
|
|
9097
9131
|
* Space Complexity: O(n)
|
|
9098
9132
|
*
|
|
9099
|
-
* The `filter` function creates a new tree
|
|
9100
|
-
*
|
|
9101
|
-
*
|
|
9102
|
-
*
|
|
9103
|
-
*
|
|
9104
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
9105
|
-
*
|
|
9106
|
-
*
|
|
9107
|
-
* @returns The `filter` method is returning a new tree object that contains the
|
|
9108
|
-
*
|
|
9133
|
+
* The `filter` function creates a new tree with entries that pass a given predicate function.
|
|
9134
|
+
* @param predicate - The `predicate` parameter is a callback function that is used to test each
|
|
9135
|
+
* element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
|
|
9136
|
+
* represents the value of the current element being processed, the `key` argument represents the key
|
|
9137
|
+
* of the
|
|
9138
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
9139
|
+
* specify the value of `this` within the `predicate` function. When the `predicate` function is
|
|
9140
|
+
* called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
|
|
9141
|
+
* @returns The `filter` method is returning a new tree object that contains the entries that pass
|
|
9142
|
+
* the given predicate function.
|
|
9109
9143
|
*/
|
|
9110
9144
|
filter(predicate, thisArg) {
|
|
9111
9145
|
const newTree = this.createTree();
|
|
@@ -9125,15 +9159,15 @@ var dataStructureTyped = (() => {
|
|
|
9125
9159
|
* Time Complexity: O(n)
|
|
9126
9160
|
* Space Complexity: O(n)
|
|
9127
9161
|
*
|
|
9128
|
-
* The `map` function creates a new tree by applying a callback function to each
|
|
9129
|
-
*
|
|
9130
|
-
* @param callback - The callback parameter is a function that will be called for each
|
|
9131
|
-
*
|
|
9132
|
-
* the
|
|
9133
|
-
*
|
|
9134
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
9135
|
-
*
|
|
9136
|
-
*
|
|
9162
|
+
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
9163
|
+
* tree.
|
|
9164
|
+
* @param callback - The callback parameter is a function that will be called for each entry in the
|
|
9165
|
+
* tree. It takes three arguments: value, key, and index. The value argument represents the value of
|
|
9166
|
+
* the current entry, the key argument represents the key of the current entry, and the index
|
|
9167
|
+
* argument represents the index of the
|
|
9168
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
9169
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
9170
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
9137
9171
|
* @returns The `map` method is returning a new tree object.
|
|
9138
9172
|
*/
|
|
9139
9173
|
map(callback, thisArg) {
|
|
@@ -9161,11 +9195,15 @@ var dataStructureTyped = (() => {
|
|
|
9161
9195
|
* Time Complexity: O(n)
|
|
9162
9196
|
* Space Complexity: O(n)
|
|
9163
9197
|
*
|
|
9164
|
-
* The `print` function
|
|
9165
|
-
* @param {
|
|
9166
|
-
*
|
|
9167
|
-
*
|
|
9168
|
-
*
|
|
9198
|
+
* The `print` function in TypeScript prints the binary tree structure with customizable options.
|
|
9199
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
9200
|
+
* point for printing the binary tree. It can be either a node of the binary tree or a key or entry
|
|
9201
|
+
* that exists in the binary tree. If no value is provided, the root of the binary tree will be used
|
|
9202
|
+
* as the starting point.
|
|
9203
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
|
|
9204
|
+
* allows you to customize the printing behavior. It has the following properties:
|
|
9205
|
+
* @returns Nothing is being returned. The function has a return type of `void`, which means it does
|
|
9206
|
+
* not return any value.
|
|
9169
9207
|
*/
|
|
9170
9208
|
print(beginRoot = this.root, options) {
|
|
9171
9209
|
const opts = __spreadValues({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
|
|
@@ -9190,13 +9228,18 @@ var dataStructureTyped = (() => {
|
|
|
9190
9228
|
display(beginRoot);
|
|
9191
9229
|
}
|
|
9192
9230
|
/**
|
|
9193
|
-
*
|
|
9194
|
-
*
|
|
9195
|
-
|
|
9196
|
-
|
|
9197
|
-
*
|
|
9198
|
-
*
|
|
9199
|
-
*
|
|
9231
|
+
* Time Complexity: O(1)
|
|
9232
|
+
* Space Complexity: O(1)
|
|
9233
|
+
*/
|
|
9234
|
+
/**
|
|
9235
|
+
* Time Complexity: O(1)
|
|
9236
|
+
* Space Complexity: O(1)
|
|
9237
|
+
*
|
|
9238
|
+
* The function `_getIterator` is a generator function that returns an iterator for the key-value
|
|
9239
|
+
* pairs in a binary search tree.
|
|
9240
|
+
* @param node - The `node` parameter represents the current node in the binary search tree. It is
|
|
9241
|
+
* initially set to the root node of the tree.
|
|
9242
|
+
* @returns an IterableIterator<[K, V | undefined]>.
|
|
9200
9243
|
*/
|
|
9201
9244
|
*_getIterator(node = this.root) {
|
|
9202
9245
|
if (!node)
|
|
@@ -9226,6 +9269,13 @@ var dataStructureTyped = (() => {
|
|
|
9226
9269
|
}
|
|
9227
9270
|
}
|
|
9228
9271
|
/**
|
|
9272
|
+
* Time Complexity: O(n)
|
|
9273
|
+
* Space Complexity: O(n)
|
|
9274
|
+
*/
|
|
9275
|
+
/**
|
|
9276
|
+
* Time Complexity: O(n)
|
|
9277
|
+
* Space Complexity: O(n)
|
|
9278
|
+
*
|
|
9229
9279
|
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
9230
9280
|
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
9231
9281
|
* @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
|
|
@@ -9280,10 +9330,21 @@ var dataStructureTyped = (() => {
|
|
|
9280
9330
|
}
|
|
9281
9331
|
}
|
|
9282
9332
|
/**
|
|
9283
|
-
*
|
|
9284
|
-
*
|
|
9285
|
-
|
|
9286
|
-
|
|
9333
|
+
* Time Complexity: O(1)
|
|
9334
|
+
* Space Complexity: O(1)
|
|
9335
|
+
*/
|
|
9336
|
+
/**
|
|
9337
|
+
* Time Complexity: O(1)
|
|
9338
|
+
* Space Complexity: O(1)
|
|
9339
|
+
*
|
|
9340
|
+
* The function `_swapProperties` swaps the key-value properties between two nodes.
|
|
9341
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
|
|
9342
|
+
* destination node. It can be either an instance of the class `R`, or an object of type
|
|
9343
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
9344
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
|
|
9345
|
+
* the properties will be swapped with the `srcNode`.
|
|
9346
|
+
* @returns either the `destNode` object with its properties swapped with the `srcNode` object's
|
|
9347
|
+
* properties, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
9287
9348
|
*/
|
|
9288
9349
|
_swapProperties(srcNode, destNode) {
|
|
9289
9350
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -9302,12 +9363,20 @@ var dataStructureTyped = (() => {
|
|
|
9302
9363
|
return void 0;
|
|
9303
9364
|
}
|
|
9304
9365
|
/**
|
|
9305
|
-
*
|
|
9366
|
+
* Time Complexity: O(1)
|
|
9367
|
+
* Space Complexity: O(1)
|
|
9368
|
+
*/
|
|
9369
|
+
/**
|
|
9370
|
+
* Time Complexity: O(1)
|
|
9371
|
+
* Space Complexity: O(1)
|
|
9372
|
+
*
|
|
9373
|
+
* The function replaces a node in a binary tree with a new node, updating the parent, left child,
|
|
9374
|
+
* right child, and root if necessary.
|
|
9306
9375
|
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
9307
9376
|
* tree.
|
|
9308
9377
|
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
9309
9378
|
* tree.
|
|
9310
|
-
* @returns
|
|
9379
|
+
* @returns the newNode.
|
|
9311
9380
|
*/
|
|
9312
9381
|
_replaceNode(oldNode, newNode) {
|
|
9313
9382
|
if (oldNode.parent) {
|
|
@@ -9326,10 +9395,17 @@ var dataStructureTyped = (() => {
|
|
|
9326
9395
|
return newNode;
|
|
9327
9396
|
}
|
|
9328
9397
|
/**
|
|
9329
|
-
*
|
|
9330
|
-
*
|
|
9331
|
-
|
|
9332
|
-
|
|
9398
|
+
* Time Complexity: O(1)
|
|
9399
|
+
* Space Complexity: O(1)
|
|
9400
|
+
*/
|
|
9401
|
+
/**
|
|
9402
|
+
* Time Complexity: O(1)
|
|
9403
|
+
* Space Complexity: O(1)
|
|
9404
|
+
*
|
|
9405
|
+
* The function sets the root property of an object to the provided value, and also updates the
|
|
9406
|
+
* parent property of the new root.
|
|
9407
|
+
* @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`. This
|
|
9408
|
+
* means that it can accept a value of type `NODE`, `null`, or `undefined`.
|
|
9333
9409
|
*/
|
|
9334
9410
|
_setRoot(v) {
|
|
9335
9411
|
if (v) {
|
|
@@ -9337,6 +9413,23 @@ var dataStructureTyped = (() => {
|
|
|
9337
9413
|
}
|
|
9338
9414
|
this._root = v;
|
|
9339
9415
|
}
|
|
9416
|
+
/**
|
|
9417
|
+
* Time Complexity: O(1)
|
|
9418
|
+
* Space Complexity: O(1)
|
|
9419
|
+
*/
|
|
9420
|
+
/**
|
|
9421
|
+
* Time Complexity: O(1)
|
|
9422
|
+
* Space Complexity: O(1)
|
|
9423
|
+
*
|
|
9424
|
+
* The function `_ensureCallback` ensures that a callback function is provided and returns it.
|
|
9425
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
|
|
9426
|
+
* `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
|
|
9427
|
+
* the generic type `C`, or it can be `null` or `undefined`.
|
|
9428
|
+
* @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
|
|
9429
|
+
* and returns a value. It is of type `C`, which is a generic type that extends the
|
|
9430
|
+
* `BTNCallback<NODE>` type.
|
|
9431
|
+
* @returns the callback parameter.
|
|
9432
|
+
*/
|
|
9340
9433
|
_ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
9341
9434
|
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
9342
9435
|
callback = (node) => node;
|
|
@@ -9396,31 +9489,44 @@ var dataStructureTyped = (() => {
|
|
|
9396
9489
|
};
|
|
9397
9490
|
var BST = class _BST extends BinaryTree {
|
|
9398
9491
|
/**
|
|
9399
|
-
* This is the constructor function for a Binary Search Tree class in TypeScript
|
|
9400
|
-
*
|
|
9401
|
-
*
|
|
9402
|
-
*
|
|
9403
|
-
*
|
|
9404
|
-
*
|
|
9405
|
-
* configuration options for the binary search tree. It can have the following properties:
|
|
9492
|
+
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
9493
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
9494
|
+
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
9495
|
+
* added to the binary search tree during the construction of the object.
|
|
9496
|
+
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
9497
|
+
* It can include a comparator function that defines the order of the elements in the tree.
|
|
9406
9498
|
*/
|
|
9407
|
-
constructor(
|
|
9499
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
9408
9500
|
super([], options);
|
|
9409
9501
|
__publicField(this, "_root");
|
|
9410
|
-
|
|
9502
|
+
/**
|
|
9503
|
+
* Time complexity: O(n)
|
|
9504
|
+
* Space complexity: O(n)
|
|
9505
|
+
*/
|
|
9506
|
+
__publicField(this, "_DEFAULT_COMPARATOR", (a, b) => {
|
|
9507
|
+
if (typeof a === "object" && typeof b === "object" && this.comparator === this._DEFAULT_COMPARATOR) {
|
|
9508
|
+
throw TypeError(
|
|
9509
|
+
"When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure."
|
|
9510
|
+
);
|
|
9511
|
+
}
|
|
9411
9512
|
if (a > b)
|
|
9412
9513
|
return 1;
|
|
9413
9514
|
if (a < b)
|
|
9414
9515
|
return -1;
|
|
9415
9516
|
return 0;
|
|
9416
9517
|
});
|
|
9518
|
+
/**
|
|
9519
|
+
* Time complexity: O(n)
|
|
9520
|
+
* Space complexity: O(n)
|
|
9521
|
+
*/
|
|
9522
|
+
__publicField(this, "_comparator", this._DEFAULT_COMPARATOR);
|
|
9417
9523
|
if (options) {
|
|
9418
9524
|
const { comparator } = options;
|
|
9419
9525
|
if (comparator)
|
|
9420
9526
|
this._comparator = comparator;
|
|
9421
9527
|
}
|
|
9422
|
-
if (
|
|
9423
|
-
this.addMany(
|
|
9528
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
9529
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
9424
9530
|
}
|
|
9425
9531
|
/**
|
|
9426
9532
|
* The function returns the root node of a tree structure.
|
|
@@ -9429,13 +9535,6 @@ var dataStructureTyped = (() => {
|
|
|
9429
9535
|
get root() {
|
|
9430
9536
|
return this._root;
|
|
9431
9537
|
}
|
|
9432
|
-
/**
|
|
9433
|
-
* The function returns the value of the _comparator property.
|
|
9434
|
-
* @returns The `_comparator` property is being returned.
|
|
9435
|
-
*/
|
|
9436
|
-
get comparator() {
|
|
9437
|
-
return this._comparator;
|
|
9438
|
-
}
|
|
9439
9538
|
/**
|
|
9440
9539
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
9441
9540
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -9450,10 +9549,9 @@ var dataStructureTyped = (() => {
|
|
|
9450
9549
|
/**
|
|
9451
9550
|
* The function creates a new binary search tree with the specified options.
|
|
9452
9551
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
9453
|
-
* behavior of the `createTree` method. It
|
|
9454
|
-
*
|
|
9455
|
-
* @returns a new instance of the BST class
|
|
9456
|
-
* options. The returned value is casted as TREE.
|
|
9552
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
9553
|
+
* following properties:
|
|
9554
|
+
* @returns a new instance of the BST class with the provided options.
|
|
9457
9555
|
*/
|
|
9458
9556
|
createTree(options) {
|
|
9459
9557
|
return new _BST([], __spreadValues({
|
|
@@ -9462,90 +9560,60 @@ var dataStructureTyped = (() => {
|
|
|
9462
9560
|
}, options));
|
|
9463
9561
|
}
|
|
9464
9562
|
/**
|
|
9465
|
-
* The function
|
|
9466
|
-
*
|
|
9467
|
-
*
|
|
9468
|
-
*
|
|
9469
|
-
* `
|
|
9470
|
-
*
|
|
9563
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
9564
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
|
|
9565
|
+
* type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
9566
|
+
* element.
|
|
9567
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
9568
|
+
* value associated with a key in a key-value pair.
|
|
9569
|
+
* @returns either a NODE object or undefined.
|
|
9471
9570
|
*/
|
|
9472
|
-
|
|
9473
|
-
|
|
9474
|
-
|
|
9475
|
-
return;
|
|
9476
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
9477
|
-
node = keyOrNodeOrEntry;
|
|
9478
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9479
|
-
const [key, value2] = keyOrNodeOrEntry;
|
|
9480
|
-
if (key === void 0 || key === null) {
|
|
9481
|
-
return;
|
|
9482
|
-
} else {
|
|
9483
|
-
node = this.createNode(key, value2);
|
|
9484
|
-
}
|
|
9485
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
9486
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
9487
|
-
} else {
|
|
9488
|
-
return;
|
|
9489
|
-
}
|
|
9490
|
-
return node;
|
|
9571
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
9572
|
+
var _a;
|
|
9573
|
+
return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value)) != null ? _a : void 0;
|
|
9491
9574
|
}
|
|
9492
|
-
/**
|
|
9493
|
-
* Time Complexity: O(log n)
|
|
9494
|
-
* Space Complexity: O(log n)
|
|
9495
|
-
*/
|
|
9496
9575
|
/**
|
|
9497
9576
|
* Time Complexity: O(log n)
|
|
9498
9577
|
* Space Complexity: O(log n)
|
|
9499
9578
|
*
|
|
9500
|
-
* The function
|
|
9501
|
-
*
|
|
9502
|
-
* @param {
|
|
9503
|
-
* `
|
|
9504
|
-
*
|
|
9505
|
-
*
|
|
9506
|
-
*
|
|
9507
|
-
|
|
9508
|
-
|
|
9509
|
-
|
|
9510
|
-
|
|
9511
|
-
|
|
9512
|
-
|
|
9513
|
-
|
|
9514
|
-
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9515
|
-
const key2 = keyOrNodeOrEntry[0];
|
|
9516
|
-
if (key2 === null || key2 === void 0)
|
|
9517
|
-
return;
|
|
9518
|
-
return this.getNodeByKey(key2, iterationType);
|
|
9519
|
-
}
|
|
9520
|
-
const key = keyOrNodeOrEntry;
|
|
9521
|
-
if (key === null || key === void 0)
|
|
9522
|
-
return;
|
|
9523
|
-
return this.getNodeByKey(key, iterationType);
|
|
9579
|
+
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
9580
|
+
* it doesn't exist.
|
|
9581
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
9582
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
|
|
9583
|
+
* entry, or raw element that needs to be ensured in the tree.
|
|
9584
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
9585
|
+
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
9586
|
+
* value of `'ITERATIVE'`.
|
|
9587
|
+
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
9588
|
+
* not be ensured.
|
|
9589
|
+
*/
|
|
9590
|
+
ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = "ITERATIVE") {
|
|
9591
|
+
var _a;
|
|
9592
|
+
return (_a = super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType)) != null ? _a : void 0;
|
|
9524
9593
|
}
|
|
9525
9594
|
/**
|
|
9526
|
-
* The function checks if
|
|
9527
|
-
* @param
|
|
9528
|
-
*
|
|
9595
|
+
* The function checks if the input is an instance of the BSTNode class.
|
|
9596
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
9597
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
9598
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
9599
|
+
* an instance of the `BSTNode` class.
|
|
9529
9600
|
*/
|
|
9530
|
-
isNode(
|
|
9531
|
-
return
|
|
9601
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
9602
|
+
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
9532
9603
|
}
|
|
9533
|
-
/**
|
|
9534
|
-
* Time Complexity: O(log n)
|
|
9535
|
-
* Space Complexity: O(1)
|
|
9536
|
-
*/
|
|
9537
9604
|
/**
|
|
9538
9605
|
* Time Complexity: O(log n)
|
|
9539
9606
|
* Space Complexity: O(1)
|
|
9540
9607
|
*
|
|
9541
|
-
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value
|
|
9542
|
-
*
|
|
9543
|
-
*
|
|
9544
|
-
* @param {V} [value] - The value
|
|
9545
|
-
*
|
|
9608
|
+
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
9609
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
9610
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
9611
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
9612
|
+
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
9613
|
+
* @returns a boolean value.
|
|
9546
9614
|
*/
|
|
9547
|
-
add(
|
|
9548
|
-
const newNode = this.
|
|
9615
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
9616
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
9549
9617
|
if (newNode === void 0)
|
|
9550
9618
|
return false;
|
|
9551
9619
|
if (this.root === void 0) {
|
|
@@ -9577,18 +9645,17 @@ var dataStructureTyped = (() => {
|
|
|
9577
9645
|
return false;
|
|
9578
9646
|
}
|
|
9579
9647
|
/**
|
|
9580
|
-
* Time Complexity: O(
|
|
9581
|
-
* Space Complexity: O(
|
|
9648
|
+
* Time Complexity: O(log n)
|
|
9649
|
+
* Space Complexity: O(log n)
|
|
9582
9650
|
*/
|
|
9583
9651
|
/**
|
|
9584
9652
|
* Time Complexity: O(k log n)
|
|
9585
9653
|
* Space Complexity: O(k + log n)
|
|
9586
9654
|
*
|
|
9587
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure
|
|
9588
|
-
*
|
|
9589
|
-
*
|
|
9590
|
-
*
|
|
9591
|
-
* data structure.
|
|
9655
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
9656
|
+
* an array indicating whether each key or node was successfully inserted.
|
|
9657
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
9658
|
+
* elements to be added to the data structure.
|
|
9592
9659
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
9593
9660
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
9594
9661
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
@@ -9597,20 +9664,19 @@ var dataStructureTyped = (() => {
|
|
|
9597
9664
|
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
9598
9665
|
* value is true.
|
|
9599
9666
|
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
9600
|
-
* specifies the type of iteration to use when adding multiple keys or nodes to the binary
|
|
9601
|
-
*
|
|
9602
|
-
*
|
|
9603
|
-
*
|
|
9604
|
-
* or entry was successfully inserted into the data structure.
|
|
9667
|
+
* specifies the type of iteration to use when adding multiple keys or nodes to the binary search
|
|
9668
|
+
* tree. It can have two possible values:
|
|
9669
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
9670
|
+
* successfully inserted into the data structure.
|
|
9605
9671
|
*/
|
|
9606
|
-
addMany(
|
|
9672
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
9607
9673
|
const inserted = [];
|
|
9608
9674
|
let valuesIterator;
|
|
9609
9675
|
if (values) {
|
|
9610
9676
|
valuesIterator = values[Symbol.iterator]();
|
|
9611
9677
|
}
|
|
9612
9678
|
if (!isBalanceAdd) {
|
|
9613
|
-
for (const kve of
|
|
9679
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
9614
9680
|
const value = valuesIterator == null ? void 0 : valuesIterator.next().value;
|
|
9615
9681
|
const nn = this.add(kve, value);
|
|
9616
9682
|
inserted.push(nn);
|
|
@@ -9623,24 +9689,30 @@ var dataStructureTyped = (() => {
|
|
|
9623
9689
|
return false;
|
|
9624
9690
|
return !(this.isEntry(kve) && (kve[0] === void 0 || kve[0] === null));
|
|
9625
9691
|
};
|
|
9626
|
-
for (const kve of
|
|
9692
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
9627
9693
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
9628
9694
|
}
|
|
9629
9695
|
let sorted = [];
|
|
9630
9696
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
9631
9697
|
let keyA, keyB;
|
|
9632
|
-
if (this.isEntry(a))
|
|
9698
|
+
if (this.isEntry(a))
|
|
9633
9699
|
keyA = a[0];
|
|
9634
|
-
|
|
9700
|
+
else if (this.isRealNode(a))
|
|
9635
9701
|
keyA = a.key;
|
|
9636
|
-
else
|
|
9702
|
+
else if (this.toEntryFn) {
|
|
9703
|
+
keyA = this.toEntryFn(a)[0];
|
|
9704
|
+
} else {
|
|
9637
9705
|
keyA = a;
|
|
9706
|
+
}
|
|
9638
9707
|
if (this.isEntry(b))
|
|
9639
9708
|
keyB = b[0];
|
|
9640
9709
|
else if (this.isRealNode(b))
|
|
9641
9710
|
keyB = b.key;
|
|
9642
|
-
else
|
|
9711
|
+
else if (this.toEntryFn) {
|
|
9712
|
+
keyB = this.toEntryFn(b)[0];
|
|
9713
|
+
} else {
|
|
9643
9714
|
keyB = b;
|
|
9715
|
+
}
|
|
9644
9716
|
if (keyA !== void 0 && keyA !== null && keyB !== void 0 && keyB !== null) {
|
|
9645
9717
|
return this.comparator(keyA, keyB);
|
|
9646
9718
|
}
|
|
@@ -9680,33 +9752,27 @@ var dataStructureTyped = (() => {
|
|
|
9680
9752
|
return inserted;
|
|
9681
9753
|
}
|
|
9682
9754
|
/**
|
|
9683
|
-
|
|
9684
|
-
|
|
9685
|
-
|
|
9686
|
-
|
|
9687
|
-
|
|
9688
|
-
|
|
9689
|
-
|
|
9690
|
-
|
|
9691
|
-
|
|
9692
|
-
|
|
9693
|
-
|
|
9694
|
-
|
|
9695
|
-
|
|
9696
|
-
|
|
9697
|
-
|
|
9698
|
-
|
|
9699
|
-
|
|
9700
|
-
|
|
9701
|
-
|
|
9702
|
-
|
|
9703
|
-
|
|
9704
|
-
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
9705
|
-
* traversal will start from the root of the tree.
|
|
9706
|
-
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
9707
|
-
* performed on the binary tree. It can have two possible values:
|
|
9708
|
-
* @returns The method returns an array of nodes (`NODE[]`).
|
|
9709
|
-
*/
|
|
9755
|
+
* Time Complexity: O(log n)
|
|
9756
|
+
* Space Complexity: O(k + log n)
|
|
9757
|
+
*
|
|
9758
|
+
* The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
|
|
9759
|
+
* identifier and callback function.
|
|
9760
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
9761
|
+
* want to search for in the binary tree. It can be of any type that is returned by the callback
|
|
9762
|
+
* function.
|
|
9763
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
9764
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
9765
|
+
* `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
9766
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
|
|
9767
|
+
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
|
|
9768
|
+
* false, all matching nodes will be returned. The default value is false.
|
|
9769
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
9770
|
+
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
|
|
9771
|
+
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
|
|
9772
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
9773
|
+
* iteration to be performed. It can have two possible values:
|
|
9774
|
+
* @returns The method `getNodes` returns an array of `NODE` objects.
|
|
9775
|
+
*/
|
|
9710
9776
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9711
9777
|
beginRoot = this.ensureNode(beginRoot);
|
|
9712
9778
|
if (!beginRoot)
|
|
@@ -9765,51 +9831,50 @@ var dataStructureTyped = (() => {
|
|
|
9765
9831
|
* Time Complexity: O(log n)
|
|
9766
9832
|
* Space Complexity: O(1)
|
|
9767
9833
|
*
|
|
9768
|
-
* The `getNode`
|
|
9769
|
-
*
|
|
9770
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value
|
|
9771
|
-
*
|
|
9772
|
-
*
|
|
9773
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
9774
|
-
* the
|
|
9775
|
-
*
|
|
9776
|
-
*
|
|
9834
|
+
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
9835
|
+
* function in a binary search tree.
|
|
9836
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
9837
|
+
* want to search for in the binary search tree. It can be of any type that is compatible with the
|
|
9838
|
+
* type returned by the callback function.
|
|
9839
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
9840
|
+
* node matches the desired criteria. It should be a function that takes a node as an argument and
|
|
9841
|
+
* returns a boolean value indicating whether the node matches the criteria or not. If no callback is
|
|
9842
|
+
* provided, the default callback will be
|
|
9777
9843
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
9778
|
-
* search tree. It can be either a key or a node. If it is a key,
|
|
9779
|
-
*
|
|
9780
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
9781
|
-
* be performed when searching for nodes in the binary search tree. It
|
|
9782
|
-
*
|
|
9783
|
-
* @returns The method is returning a
|
|
9844
|
+
* search tree. It can be either a key or a node. If it is a key, the search will start from the node
|
|
9845
|
+
* with that key. If it is a node, the search will start from that node.
|
|
9846
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
9847
|
+
* of iteration to be performed when searching for nodes in the binary search tree. It can have one
|
|
9848
|
+
* of the following values:
|
|
9849
|
+
* @returns The method is returning a NODE object or undefined.
|
|
9784
9850
|
*/
|
|
9785
9851
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9786
9852
|
var _a;
|
|
9787
9853
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
|
|
9788
9854
|
}
|
|
9789
9855
|
/**
|
|
9790
|
-
* Time Complexity: O(log n)
|
|
9791
|
-
* Space Complexity: O(
|
|
9856
|
+
* Time Complexity: O(k log n)
|
|
9857
|
+
* Space Complexity: O(k + log n)
|
|
9792
9858
|
*/
|
|
9793
9859
|
/**
|
|
9794
9860
|
* Time Complexity: O(log n)
|
|
9795
9861
|
* Space Complexity: O(1)
|
|
9796
9862
|
*
|
|
9797
|
-
* The function `getNodeByKey`
|
|
9798
|
-
*
|
|
9799
|
-
*
|
|
9800
|
-
*
|
|
9801
|
-
* @param iterationType - The `iterationType` parameter is an optional
|
|
9802
|
-
* type of iteration to
|
|
9803
|
-
*
|
|
9804
|
-
* @returns The
|
|
9805
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
9863
|
+
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
9864
|
+
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
9865
|
+
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
9866
|
+
* in the tree structure.
|
|
9867
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
9868
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
9869
|
+
* It has a default value of `'ITERATIVE'`.
|
|
9870
|
+
* @returns The method is returning a NODE object or undefined.
|
|
9806
9871
|
*/
|
|
9807
9872
|
getNodeByKey(key, iterationType = "ITERATIVE") {
|
|
9808
9873
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
9809
9874
|
}
|
|
9810
9875
|
/**
|
|
9811
|
-
* Time
|
|
9812
|
-
* Space
|
|
9876
|
+
* Time Complexity: O(log n)
|
|
9877
|
+
* Space Complexity: O(k + log n)
|
|
9813
9878
|
*/
|
|
9814
9879
|
/**
|
|
9815
9880
|
* Time complexity: O(n)
|
|
@@ -9818,15 +9883,16 @@ var dataStructureTyped = (() => {
|
|
|
9818
9883
|
* The function overrides the depth-first search method and returns an array of the return types of
|
|
9819
9884
|
* the callback function.
|
|
9820
9885
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9821
|
-
* during the depth-first search traversal. It is an optional parameter and
|
|
9822
|
-
*
|
|
9823
|
-
* @param {DFSOrderPattern} [pattern=
|
|
9824
|
-
*
|
|
9825
|
-
*
|
|
9826
|
-
*
|
|
9827
|
-
*
|
|
9828
|
-
*
|
|
9829
|
-
*
|
|
9886
|
+
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
9887
|
+
* `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
|
|
9888
|
+
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
9889
|
+
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
9890
|
+
* take one of the following values:
|
|
9891
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
9892
|
+
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
9893
|
+
* node entry. If not specified, the default value is the root of the tree.
|
|
9894
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
9895
|
+
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
9830
9896
|
* following values:
|
|
9831
9897
|
* @returns The method is returning an array of the return type of the callback function.
|
|
9832
9898
|
*/
|
|
@@ -9834,8 +9900,8 @@ var dataStructureTyped = (() => {
|
|
|
9834
9900
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
9835
9901
|
}
|
|
9836
9902
|
/**
|
|
9837
|
-
* Time
|
|
9838
|
-
* Space
|
|
9903
|
+
* Time Complexity: O(log n)
|
|
9904
|
+
* Space Complexity: O(1)
|
|
9839
9905
|
*/
|
|
9840
9906
|
/**
|
|
9841
9907
|
* Time complexity: O(n)
|
|
@@ -9844,38 +9910,38 @@ var dataStructureTyped = (() => {
|
|
|
9844
9910
|
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
9845
9911
|
* the callback function.
|
|
9846
9912
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9847
|
-
* visited during the breadth-first search
|
|
9848
|
-
*
|
|
9849
|
-
* @param beginRoot - The `beginRoot` parameter is the starting
|
|
9850
|
-
*
|
|
9851
|
-
* the
|
|
9852
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
9853
|
-
* be performed during the breadth-first search (BFS) traversal. It
|
|
9854
|
-
*
|
|
9855
|
-
* @returns
|
|
9913
|
+
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
9914
|
+
* node being visited, and it can return a value of any type.
|
|
9915
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
9916
|
+
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
9917
|
+
* object. If no value is provided, the default value is the root of the tree.
|
|
9918
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
9919
|
+
* of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
|
|
9920
|
+
* the following values:
|
|
9921
|
+
* @returns an array of the return type of the callback function.
|
|
9856
9922
|
*/
|
|
9857
9923
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9858
9924
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
9859
9925
|
}
|
|
9860
9926
|
/**
|
|
9861
|
-
* Time
|
|
9862
|
-
* Space
|
|
9927
|
+
* Time Complexity: O(log n)
|
|
9928
|
+
* Space Complexity: O(1)
|
|
9863
9929
|
*/
|
|
9864
9930
|
/**
|
|
9865
9931
|
* Time complexity: O(n)
|
|
9866
9932
|
* Space complexity: O(n)
|
|
9867
9933
|
*
|
|
9868
|
-
* The function overrides the listLevels method and returns an array of arrays
|
|
9869
|
-
*
|
|
9934
|
+
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
9935
|
+
* containing the results of the callback function applied to each level of the tree.
|
|
9870
9936
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
9871
|
-
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
9872
|
-
* during the
|
|
9873
|
-
* @param beginRoot - The `beginRoot` parameter is
|
|
9874
|
-
* levels of
|
|
9875
|
-
*
|
|
9876
|
-
*
|
|
9877
|
-
*
|
|
9878
|
-
* iteration.
|
|
9937
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
9938
|
+
* tree during the iteration process.
|
|
9939
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
9940
|
+
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
9941
|
+
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
9942
|
+
* value is provided, the root of
|
|
9943
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
9944
|
+
* of iteration to be performed on the tree. It can have one of the following values:
|
|
9879
9945
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
9880
9946
|
* function.
|
|
9881
9947
|
*/
|
|
@@ -9883,27 +9949,27 @@ var dataStructureTyped = (() => {
|
|
|
9883
9949
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
9884
9950
|
}
|
|
9885
9951
|
/**
|
|
9886
|
-
* Time
|
|
9887
|
-
* Space
|
|
9952
|
+
* Time complexity: O(n)
|
|
9953
|
+
* Space complexity: O(n)
|
|
9888
9954
|
*/
|
|
9889
9955
|
/**
|
|
9890
|
-
* Time
|
|
9891
|
-
* Space
|
|
9956
|
+
* Time complexity: O(n)
|
|
9957
|
+
* Space complexity: O(n)
|
|
9892
9958
|
*
|
|
9893
|
-
* The `lesserOrGreaterTraverse` function traverses a binary tree and
|
|
9894
|
-
*
|
|
9959
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
9960
|
+
* each node that meets a certain condition based on a target node and a comparison value.
|
|
9895
9961
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9896
|
-
* that
|
|
9897
|
-
*
|
|
9962
|
+
* that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
|
|
9963
|
+
* which is the current node being traversed, and returns a value of any type.
|
|
9898
9964
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
9899
|
-
* traverse nodes that are lesser
|
|
9900
|
-
*
|
|
9901
|
-
* `
|
|
9902
|
-
*
|
|
9903
|
-
*
|
|
9904
|
-
*
|
|
9905
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
9906
|
-
* performed on the binary tree. It can have two possible values:
|
|
9965
|
+
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
9966
|
+
* 0, or 1, where:
|
|
9967
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
|
|
9968
|
+
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
9969
|
+
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
9970
|
+
* `targetNode` is provided,
|
|
9971
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
9972
|
+
* traversal to be performed on the binary tree. It can have two possible values:
|
|
9907
9973
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
9908
9974
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
9909
9975
|
*/
|
|
@@ -9945,18 +10011,19 @@ var dataStructureTyped = (() => {
|
|
|
9945
10011
|
}
|
|
9946
10012
|
}
|
|
9947
10013
|
/**
|
|
9948
|
-
* Time
|
|
9949
|
-
* Space
|
|
10014
|
+
* Time complexity: O(n)
|
|
10015
|
+
* Space complexity: O(n)
|
|
9950
10016
|
*/
|
|
9951
10017
|
/**
|
|
9952
|
-
* Time
|
|
9953
|
-
* Space
|
|
10018
|
+
* Time complexity: O(n)
|
|
10019
|
+
* Space complexity: O(n)
|
|
9954
10020
|
*
|
|
9955
|
-
* The `perfectlyBalance` function
|
|
9956
|
-
*
|
|
9957
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
9958
|
-
* type of iteration to use when building a balanced binary search tree. It
|
|
9959
|
-
*
|
|
10021
|
+
* The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
|
|
10022
|
+
* the binary search tree is perfectly balanced, otherwise it returns `false`.
|
|
10023
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
10024
|
+
* specifies the type of iteration to use when building a balanced binary search tree. It has a
|
|
10025
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
10026
|
+
* current instance of the class.
|
|
9960
10027
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
9961
10028
|
*/
|
|
9962
10029
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
@@ -9995,25 +10062,19 @@ var dataStructureTyped = (() => {
|
|
|
9995
10062
|
}
|
|
9996
10063
|
}
|
|
9997
10064
|
/**
|
|
9998
|
-
*
|
|
9999
|
-
*
|
|
10000
|
-
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
10001
|
-
*
|
|
10002
|
-
* Use Cases and Efficiency:
|
|
10003
|
-
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
10004
|
-
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
10005
|
-
*/
|
|
10006
|
-
/**
|
|
10007
|
-
* Time Complexity: O(n)
|
|
10008
|
-
* Space Complexity: O(log n)
|
|
10065
|
+
* Time complexity: O(n)
|
|
10066
|
+
* Space complexity: O(n)
|
|
10009
10067
|
*/
|
|
10010
10068
|
/**
|
|
10011
10069
|
* Time Complexity: O(n)
|
|
10012
10070
|
* Space Complexity: O(log n)
|
|
10013
10071
|
*
|
|
10014
|
-
* The function checks if a binary tree is AVL balanced using either recursive or
|
|
10015
|
-
*
|
|
10016
|
-
*
|
|
10072
|
+
* The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
|
|
10073
|
+
* iterative approach.
|
|
10074
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
10075
|
+
* specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
|
|
10076
|
+
* value of `this.iterationType`, which means it will use the iteration type specified in the current
|
|
10077
|
+
* instance of the AVL tree.
|
|
10017
10078
|
* @returns a boolean value.
|
|
10018
10079
|
*/
|
|
10019
10080
|
isAVLBalanced(iterationType = this.iterationType) {
|
|
@@ -10060,9 +10121,20 @@ var dataStructureTyped = (() => {
|
|
|
10060
10121
|
return balanced;
|
|
10061
10122
|
}
|
|
10062
10123
|
/**
|
|
10063
|
-
*
|
|
10064
|
-
*
|
|
10065
|
-
|
|
10124
|
+
* Time Complexity: O(n)
|
|
10125
|
+
* Space Complexity: O(log n)
|
|
10126
|
+
*/
|
|
10127
|
+
/**
|
|
10128
|
+
* The function returns the value of the _comparator property.
|
|
10129
|
+
* @returns The `_comparator` property is being returned.
|
|
10130
|
+
*/
|
|
10131
|
+
get comparator() {
|
|
10132
|
+
return this._comparator;
|
|
10133
|
+
}
|
|
10134
|
+
/**
|
|
10135
|
+
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
10136
|
+
* root.
|
|
10137
|
+
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
10066
10138
|
*/
|
|
10067
10139
|
_setRoot(v) {
|
|
10068
10140
|
if (v) {
|
|
@@ -10670,27 +10742,29 @@ var dataStructureTyped = (() => {
|
|
|
10670
10742
|
};
|
|
10671
10743
|
var AVLTree = class _AVLTree extends BST {
|
|
10672
10744
|
/**
|
|
10673
|
-
*
|
|
10674
|
-
*
|
|
10675
|
-
*
|
|
10676
|
-
*
|
|
10677
|
-
*
|
|
10678
|
-
*
|
|
10679
|
-
*
|
|
10745
|
+
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
10746
|
+
* entries, or raw elements.
|
|
10747
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
10748
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
10749
|
+
* be used to initialize the AVLTree.
|
|
10750
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
10751
|
+
* behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare
|
|
10752
|
+
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
10753
|
+
* `nodeBuilder` (
|
|
10680
10754
|
*/
|
|
10681
|
-
constructor(
|
|
10755
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
10682
10756
|
super([], options);
|
|
10683
|
-
if (
|
|
10684
|
-
super.addMany(
|
|
10757
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
10758
|
+
super.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
10685
10759
|
}
|
|
10686
10760
|
/**
|
|
10687
|
-
* The function creates a new AVL tree node with the
|
|
10688
|
-
* @param {K} key - The key parameter is the key
|
|
10689
|
-
*
|
|
10690
|
-
* @param [value] - The parameter
|
|
10691
|
-
*
|
|
10692
|
-
*
|
|
10693
|
-
*
|
|
10761
|
+
* The function creates a new AVL tree node with the given key and value.
|
|
10762
|
+
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
10763
|
+
* created.
|
|
10764
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
10765
|
+
* value associated with the key in the node being created.
|
|
10766
|
+
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
10767
|
+
* type NODE.
|
|
10694
10768
|
*/
|
|
10695
10769
|
createNode(key, value) {
|
|
10696
10770
|
return new AVLTreeNode(key, value);
|
|
@@ -10709,12 +10783,14 @@ var dataStructureTyped = (() => {
|
|
|
10709
10783
|
}, options));
|
|
10710
10784
|
}
|
|
10711
10785
|
/**
|
|
10712
|
-
* The function checks if
|
|
10713
|
-
* @param
|
|
10714
|
-
*
|
|
10786
|
+
* The function checks if the input is an instance of AVLTreeNode.
|
|
10787
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
10788
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
10789
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
10790
|
+
* an instance of the `AVLTreeNode` class.
|
|
10715
10791
|
*/
|
|
10716
|
-
isNode(
|
|
10717
|
-
return
|
|
10792
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
10793
|
+
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
|
|
10718
10794
|
}
|
|
10719
10795
|
/**
|
|
10720
10796
|
* Time Complexity: O(log n)
|
|
@@ -10725,20 +10801,21 @@ var dataStructureTyped = (() => {
|
|
|
10725
10801
|
* Time Complexity: O(log n)
|
|
10726
10802
|
* Space Complexity: O(1)
|
|
10727
10803
|
*
|
|
10728
|
-
* The function overrides the add method of a
|
|
10729
|
-
*
|
|
10730
|
-
* @param
|
|
10731
|
-
*
|
|
10732
|
-
*
|
|
10733
|
-
*
|
|
10734
|
-
*
|
|
10804
|
+
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
10805
|
+
* structure, then balances the path.
|
|
10806
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
10807
|
+
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
|
|
10808
|
+
* `RawElement`.
|
|
10809
|
+
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
10810
|
+
* the key or node being added to the data structure.
|
|
10811
|
+
* @returns The method is returning a boolean value.
|
|
10735
10812
|
*/
|
|
10736
|
-
add(
|
|
10737
|
-
if (
|
|
10813
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
10814
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
10738
10815
|
return false;
|
|
10739
|
-
const inserted = super.add(
|
|
10816
|
+
const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
|
|
10740
10817
|
if (inserted)
|
|
10741
|
-
this._balancePath(
|
|
10818
|
+
this._balancePath(keyOrNodeOrEntryOrRawElement);
|
|
10742
10819
|
return inserted;
|
|
10743
10820
|
}
|
|
10744
10821
|
/**
|
|
@@ -10749,16 +10826,14 @@ var dataStructureTyped = (() => {
|
|
|
10749
10826
|
* Time Complexity: O(log n)
|
|
10750
10827
|
* Space Complexity: O(1)
|
|
10751
10828
|
*
|
|
10752
|
-
* The function overrides the delete method of a binary tree
|
|
10753
|
-
*
|
|
10829
|
+
* The function overrides the delete method of a binary tree class and performs additional operations
|
|
10830
|
+
* to balance the tree after deletion.
|
|
10754
10831
|
* @param identifier - The `identifier` parameter is the value or condition used to identify the
|
|
10755
|
-
* node(s) to be deleted from the binary tree. It can be of any type
|
|
10756
|
-
*
|
|
10757
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
10758
|
-
*
|
|
10759
|
-
*
|
|
10760
|
-
* parameter of type `NODE
|
|
10761
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
10832
|
+
* node(s) to be deleted from the binary tree. It can be of any type that is compatible with the
|
|
10833
|
+
* binary tree's node type.
|
|
10834
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
10835
|
+
* node should be deleted or not. It is optional and has a default value of `this._DEFAULT_CALLBACK`.
|
|
10836
|
+
* @returns The method is returning an array of BinaryTreeDeleteResult<NODE> objects.
|
|
10762
10837
|
*/
|
|
10763
10838
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
10764
10839
|
const deletedResults = super.delete(identifier, callback);
|
|
@@ -10770,14 +10845,21 @@ var dataStructureTyped = (() => {
|
|
|
10770
10845
|
return deletedResults;
|
|
10771
10846
|
}
|
|
10772
10847
|
/**
|
|
10773
|
-
*
|
|
10774
|
-
*
|
|
10775
|
-
|
|
10776
|
-
|
|
10777
|
-
*
|
|
10778
|
-
*
|
|
10779
|
-
*
|
|
10780
|
-
*
|
|
10848
|
+
* Time Complexity: O(1)
|
|
10849
|
+
* Space Complexity: O(1)
|
|
10850
|
+
*/
|
|
10851
|
+
/**
|
|
10852
|
+
* Time Complexity: O(1)
|
|
10853
|
+
* Space Complexity: O(1)
|
|
10854
|
+
*
|
|
10855
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
10856
|
+
* binary search tree.
|
|
10857
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
10858
|
+
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
10859
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
10860
|
+
* `R` or an instance of `BSTNKeyOrNode<K, NODE>`.
|
|
10861
|
+
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
10862
|
+
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
10781
10863
|
*/
|
|
10782
10864
|
_swapProperties(srcNode, destNode) {
|
|
10783
10865
|
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
@@ -10807,7 +10889,8 @@ var dataStructureTyped = (() => {
|
|
|
10807
10889
|
* Space Complexity: O(1)
|
|
10808
10890
|
*
|
|
10809
10891
|
* The function calculates the balance factor of a node in a binary tree.
|
|
10810
|
-
* @param {NODE} node - The parameter "node" represents a node in a
|
|
10892
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a
|
|
10893
|
+
* binary tree data structure.
|
|
10811
10894
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
10812
10895
|
* height of the left subtree from the height of the right subtree.
|
|
10813
10896
|
*/
|
|
@@ -10850,7 +10933,7 @@ var dataStructureTyped = (() => {
|
|
|
10850
10933
|
* Time Complexity: O(1)
|
|
10851
10934
|
* Space Complexity: O(1)
|
|
10852
10935
|
*
|
|
10853
|
-
* The
|
|
10936
|
+
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
10854
10937
|
* @param {NODE} A - A is a node in a binary tree.
|
|
10855
10938
|
*/
|
|
10856
10939
|
_balanceLL(A) {
|
|
@@ -11040,8 +11123,8 @@ var dataStructureTyped = (() => {
|
|
|
11040
11123
|
*
|
|
11041
11124
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
11042
11125
|
* to restore balance in an AVL tree after inserting a node.
|
|
11043
|
-
* @param {NODE} node - The `node` parameter
|
|
11044
|
-
*
|
|
11126
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
|
|
11127
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
11045
11128
|
*/
|
|
11046
11129
|
_balancePath(node) {
|
|
11047
11130
|
node = this.ensureNode(node);
|
|
@@ -11071,13 +11154,21 @@ var dataStructureTyped = (() => {
|
|
|
11071
11154
|
}
|
|
11072
11155
|
}
|
|
11073
11156
|
/**
|
|
11074
|
-
*
|
|
11075
|
-
*
|
|
11076
|
-
|
|
11157
|
+
* Time Complexity: O(1)
|
|
11158
|
+
* Space Complexity: O(1)
|
|
11159
|
+
*/
|
|
11160
|
+
/**
|
|
11161
|
+
* Time Complexity: O(1)
|
|
11162
|
+
* Space Complexity: O(1)
|
|
11163
|
+
*
|
|
11164
|
+
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
11165
|
+
* same as the old node.
|
|
11166
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
11167
|
+
* the data structure.
|
|
11077
11168
|
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
11078
11169
|
* the data structure.
|
|
11079
|
-
* @returns the result of calling the `_replaceNode` method
|
|
11080
|
-
* `oldNode` and `newNode` as arguments.
|
|
11170
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
11171
|
+
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
11081
11172
|
*/
|
|
11082
11173
|
_replaceNode(oldNode, newNode) {
|
|
11083
11174
|
newNode.height = oldNode.height;
|
|
@@ -11121,20 +11212,20 @@ var dataStructureTyped = (() => {
|
|
|
11121
11212
|
var RedBlackTree = class _RedBlackTree extends BST {
|
|
11122
11213
|
/**
|
|
11123
11214
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
11124
|
-
* @param
|
|
11125
|
-
* contain keys, nodes, or
|
|
11126
|
-
*
|
|
11215
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
11216
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
11217
|
+
* initialize the RBTree with the provided elements.
|
|
11127
11218
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
11128
|
-
* constructor. It
|
|
11129
|
-
*
|
|
11130
|
-
*
|
|
11219
|
+
* constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
|
|
11220
|
+
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
11221
|
+
* depend on the implementation
|
|
11131
11222
|
*/
|
|
11132
|
-
constructor(
|
|
11223
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
11133
11224
|
super([], options);
|
|
11134
11225
|
__publicField(this, "_root");
|
|
11135
11226
|
this._root = this.NIL;
|
|
11136
|
-
if (
|
|
11137
|
-
this.addMany(
|
|
11227
|
+
if (keysOrNodesOrEntriesOrRawElements) {
|
|
11228
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
11138
11229
|
}
|
|
11139
11230
|
}
|
|
11140
11231
|
/**
|
|
@@ -11146,24 +11237,25 @@ var dataStructureTyped = (() => {
|
|
|
11146
11237
|
}
|
|
11147
11238
|
/**
|
|
11148
11239
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
11149
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of
|
|
11150
|
-
* which is a generic type
|
|
11240
|
+
* @param {K} key - The key parameter represents the key value of the node being created. It is of
|
|
11241
|
+
* type K, which is a generic type that can be replaced with any specific type when using the
|
|
11242
|
+
* function.
|
|
11151
11243
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
11152
|
-
* associated with the key in the node. It is not required and can be omitted if
|
|
11153
|
-
*
|
|
11154
|
-
*
|
|
11155
|
-
* can
|
|
11156
|
-
*
|
|
11157
|
-
* value, and color
|
|
11244
|
+
* associated with the key in the node. It is not required and can be omitted if you only need to
|
|
11245
|
+
* create a node with a key.
|
|
11246
|
+
* @param {RBTNColor} [color=BLACK] - The "color" parameter is used to specify the color of the node
|
|
11247
|
+
* in a Red-Black Tree. It can have two possible values: "RED" or "BLACK". By default, the color is
|
|
11248
|
+
* set to "BLACK" if not specified.
|
|
11249
|
+
* @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
|
|
11250
|
+
* returned.
|
|
11158
11251
|
*/
|
|
11159
11252
|
createNode(key, value, color = "BLACK") {
|
|
11160
11253
|
return new RedBlackTreeNode(key, value, color);
|
|
11161
11254
|
}
|
|
11162
11255
|
/**
|
|
11163
|
-
* The function creates a Red-Black Tree with the
|
|
11164
|
-
* @param [options] - The `options` parameter is an optional object that contains
|
|
11165
|
-
* options for creating the Red-Black Tree. It
|
|
11166
|
-
* the type of keys in the tree.
|
|
11256
|
+
* The function creates a new Red-Black Tree with the specified options.
|
|
11257
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
11258
|
+
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
11167
11259
|
* @returns a new instance of a RedBlackTree object.
|
|
11168
11260
|
*/
|
|
11169
11261
|
createTree(options) {
|
|
@@ -11179,48 +11271,50 @@ var dataStructureTyped = (() => {
|
|
|
11179
11271
|
* Time Complexity: O(1)
|
|
11180
11272
|
* Space Complexity: O(1)
|
|
11181
11273
|
*
|
|
11182
|
-
* The function
|
|
11183
|
-
*
|
|
11184
|
-
*
|
|
11185
|
-
* @
|
|
11186
|
-
*
|
|
11274
|
+
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
11275
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
11276
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
11277
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
11278
|
+
* an instance of the `RedBlackTreeNode` class.
|
|
11187
11279
|
*/
|
|
11188
|
-
|
|
11189
|
-
|
|
11190
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
11191
|
-
return;
|
|
11192
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
11193
|
-
node = keyOrNodeOrEntry;
|
|
11194
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
11195
|
-
const [key, value2] = keyOrNodeOrEntry;
|
|
11196
|
-
if (key === void 0 || key === null) {
|
|
11197
|
-
return;
|
|
11198
|
-
} else {
|
|
11199
|
-
node = this.createNode(key, value2, "RED");
|
|
11200
|
-
}
|
|
11201
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
11202
|
-
node = this.createNode(keyOrNodeOrEntry, value, "RED");
|
|
11203
|
-
} else {
|
|
11204
|
-
return;
|
|
11205
|
-
}
|
|
11206
|
-
return node;
|
|
11207
|
-
}
|
|
11208
|
-
/**
|
|
11209
|
-
* Time Complexity: O(1)
|
|
11210
|
-
* Space Complexity: O(1)
|
|
11211
|
-
* /
|
|
11212
|
-
|
|
11213
|
-
/**
|
|
11214
|
-
* Time Complexity: O(1)
|
|
11215
|
-
* Space Complexity: O(1)
|
|
11216
|
-
*
|
|
11217
|
-
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
11218
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The object to check.
|
|
11219
|
-
* @returns {boolean} - `true` if the object is a Red-Black Tree node, `false` otherwise.
|
|
11220
|
-
*/
|
|
11221
|
-
isNode(keyOrNodeOrEntry) {
|
|
11222
|
-
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
11280
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
11281
|
+
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
|
|
11223
11282
|
}
|
|
11283
|
+
// /**
|
|
11284
|
+
// * Time Complexity: O(1)
|
|
11285
|
+
// * Space Complexity: O(1)
|
|
11286
|
+
// */
|
|
11287
|
+
//
|
|
11288
|
+
// /**
|
|
11289
|
+
// * Time Complexity: O(1)
|
|
11290
|
+
// * Space Complexity: O(1)
|
|
11291
|
+
// *
|
|
11292
|
+
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
|
|
11293
|
+
// * valid, otherwise it returns undefined.
|
|
11294
|
+
// * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
|
|
11295
|
+
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
|
|
11296
|
+
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
11297
|
+
// */
|
|
11298
|
+
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
11299
|
+
//
|
|
11300
|
+
// if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
11301
|
+
// if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
11302
|
+
//
|
|
11303
|
+
// if (this.toEntryFn) {
|
|
11304
|
+
// const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
|
|
11305
|
+
// if (key) return this.createNode(key, entryValue ?? value, 'RED');
|
|
11306
|
+
// }
|
|
11307
|
+
//
|
|
11308
|
+
// if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
11309
|
+
// const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
11310
|
+
// if (key === undefined || key === null) return;
|
|
11311
|
+
// else return this.createNode(key, value, 'RED');
|
|
11312
|
+
// }
|
|
11313
|
+
//
|
|
11314
|
+
// if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'RED');
|
|
11315
|
+
//
|
|
11316
|
+
// return ;
|
|
11317
|
+
// }
|
|
11224
11318
|
/**
|
|
11225
11319
|
* Time Complexity: O(1)
|
|
11226
11320
|
* Space Complexity: O(1)
|
|
@@ -11244,17 +11338,19 @@ var dataStructureTyped = (() => {
|
|
|
11244
11338
|
* Time Complexity: O(log n)
|
|
11245
11339
|
* Space Complexity: O(1)
|
|
11246
11340
|
*
|
|
11247
|
-
* The function adds a new node to a
|
|
11248
|
-
*
|
|
11249
|
-
* @param
|
|
11250
|
-
*
|
|
11251
|
-
* @param {V} [value] - The `value` parameter is
|
|
11252
|
-
*
|
|
11253
|
-
*
|
|
11254
|
-
*
|
|
11341
|
+
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
11342
|
+
* added.
|
|
11343
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
11344
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
11345
|
+
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
11346
|
+
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
11347
|
+
* structure.
|
|
11348
|
+
* @returns The method is returning a boolean value. If a new node is successfully added to the tree,
|
|
11349
|
+
* the method returns true. If the node already exists and its value is updated, the method also
|
|
11350
|
+
* returns true. If the node cannot be added or updated, the method returns false.
|
|
11255
11351
|
*/
|
|
11256
|
-
add(
|
|
11257
|
-
const newNode = this.
|
|
11352
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
11353
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
11258
11354
|
if (!this.isRealNode(newNode))
|
|
11259
11355
|
return false;
|
|
11260
11356
|
const insertStatus = this._insert(newNode);
|
|
@@ -11277,16 +11373,16 @@ var dataStructureTyped = (() => {
|
|
|
11277
11373
|
* Time Complexity: O(log n)
|
|
11278
11374
|
* Space Complexity: O(1)
|
|
11279
11375
|
*
|
|
11280
|
-
* The function
|
|
11281
|
-
*
|
|
11282
|
-
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
|
|
11283
|
-
*
|
|
11284
|
-
*
|
|
11285
|
-
*
|
|
11286
|
-
* @param {C} callback - The `callback` parameter is a function that is used to
|
|
11287
|
-
*
|
|
11288
|
-
* `_DEFAULT_CALLBACK
|
|
11289
|
-
*
|
|
11376
|
+
* The function overrides the delete method of a binary tree data structure, allowing for the
|
|
11377
|
+
* deletion of a node and maintaining the balance of the tree.
|
|
11378
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
11379
|
+
* that identifies the node to be deleted from the binary tree. It can be of any type that is
|
|
11380
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if there is no node to
|
|
11381
|
+
* delete.
|
|
11382
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
11383
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
11384
|
+
* `this._DEFAULT_CALLBACK`. The type of the `callback` parameter is `C`, which is a generic type
|
|
11385
|
+
* that extends the `BTNCallback
|
|
11290
11386
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
11291
11387
|
*/
|
|
11292
11388
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
@@ -11338,6 +11434,13 @@ var dataStructureTyped = (() => {
|
|
|
11338
11434
|
return results;
|
|
11339
11435
|
}
|
|
11340
11436
|
/**
|
|
11437
|
+
* Time Complexity: O(1)
|
|
11438
|
+
* Space Complexity: O(1)
|
|
11439
|
+
*/
|
|
11440
|
+
/**
|
|
11441
|
+
* Time Complexity: O(1)
|
|
11442
|
+
* Space Complexity: O(1)
|
|
11443
|
+
*
|
|
11341
11444
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
11342
11445
|
* root.
|
|
11343
11446
|
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
@@ -11359,8 +11462,8 @@ var dataStructureTyped = (() => {
|
|
|
11359
11462
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
11360
11463
|
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
11361
11464
|
* the data structure.
|
|
11362
|
-
* @param {NODE} newNode - The `newNode` parameter is
|
|
11363
|
-
*
|
|
11465
|
+
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
|
|
11466
|
+
* data structure.
|
|
11364
11467
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
11365
11468
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
11366
11469
|
*/
|
|
@@ -11376,12 +11479,13 @@ var dataStructureTyped = (() => {
|
|
|
11376
11479
|
* Time Complexity: O(log n)
|
|
11377
11480
|
* Space Complexity: O(1)
|
|
11378
11481
|
*
|
|
11379
|
-
* The `_insert` function inserts
|
|
11380
|
-
*
|
|
11381
|
-
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into
|
|
11382
|
-
* binary search tree.
|
|
11383
|
-
*
|
|
11384
|
-
*
|
|
11482
|
+
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
|
|
11483
|
+
* maintain the red-black tree properties.
|
|
11484
|
+
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the
|
|
11485
|
+
* binary search tree.
|
|
11486
|
+
* @returns a string value indicating the result of the insertion operation. It can return either
|
|
11487
|
+
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
|
|
11488
|
+
* was created and inserted into the tree.
|
|
11385
11489
|
*/
|
|
11386
11490
|
_insert(node) {
|
|
11387
11491
|
var _a, _b;
|
|
@@ -11447,8 +11551,8 @@ var dataStructureTyped = (() => {
|
|
|
11447
11551
|
* Space Complexity: O(1)
|
|
11448
11552
|
*
|
|
11449
11553
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
11450
|
-
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree
|
|
11451
|
-
* either be a valid node
|
|
11554
|
+
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
|
|
11555
|
+
* structure. It can either be a valid node or `undefined`.
|
|
11452
11556
|
*/
|
|
11453
11557
|
_insertFixup(z) {
|
|
11454
11558
|
var _a, _b, _c, _d;
|
|
@@ -11504,9 +11608,10 @@ var dataStructureTyped = (() => {
|
|
|
11504
11608
|
*
|
|
11505
11609
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
11506
11610
|
* the colors and performing rotations.
|
|
11507
|
-
* @param {NODE | undefined} node - The `node` parameter represents a node in a
|
|
11508
|
-
*
|
|
11509
|
-
* @returns The function does not return any value. It has a return type of `void
|
|
11611
|
+
* @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can
|
|
11612
|
+
* be either a valid node object or `undefined`.
|
|
11613
|
+
* @returns The function does not return any value. It has a return type of `void`, which means it
|
|
11614
|
+
* does not return anything.
|
|
11510
11615
|
*/
|
|
11511
11616
|
_deleteFixup(node) {
|
|
11512
11617
|
var _a, _b, _c, _d;
|
|
@@ -11674,13 +11779,20 @@ var dataStructureTyped = (() => {
|
|
|
11674
11779
|
}
|
|
11675
11780
|
};
|
|
11676
11781
|
var AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
11677
|
-
|
|
11782
|
+
/**
|
|
11783
|
+
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
11784
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
11785
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements.
|
|
11786
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
11787
|
+
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
|
|
11788
|
+
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
11789
|
+
*/
|
|
11790
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
11678
11791
|
super([], options);
|
|
11679
11792
|
__publicField(this, "_count", 0);
|
|
11680
|
-
if (
|
|
11681
|
-
this.addMany(
|
|
11793
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
11794
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
11682
11795
|
}
|
|
11683
|
-
// TODO the _count is not accurate after nodes count modified
|
|
11684
11796
|
/**
|
|
11685
11797
|
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
11686
11798
|
* search.
|
|
@@ -11707,17 +11819,26 @@ var dataStructureTyped = (() => {
|
|
|
11707
11819
|
return sum;
|
|
11708
11820
|
}
|
|
11709
11821
|
/**
|
|
11710
|
-
* The function creates a new
|
|
11711
|
-
* @param {K} key - The key parameter
|
|
11712
|
-
*
|
|
11713
|
-
* @param {
|
|
11714
|
-
*
|
|
11715
|
-
*
|
|
11716
|
-
*
|
|
11822
|
+
* The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
|
|
11823
|
+
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
11824
|
+
* which is a generic type that can be replaced with any specific type when using the function.
|
|
11825
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
11826
|
+
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
11827
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
|
|
11828
|
+
* key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
|
|
11829
|
+
* calling the `createNode` method. If provided, it specifies the initial count for the node.
|
|
11830
|
+
* @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
|
|
11717
11831
|
*/
|
|
11718
11832
|
createNode(key, value, count) {
|
|
11719
11833
|
return new AVLTreeMultiMapNode(key, value, count);
|
|
11720
11834
|
}
|
|
11835
|
+
/**
|
|
11836
|
+
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
11837
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
11838
|
+
* configuration options for creating the AVLTreeMultiMap. It can have the following properties:
|
|
11839
|
+
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
11840
|
+
* object.
|
|
11841
|
+
*/
|
|
11721
11842
|
createTree(options) {
|
|
11722
11843
|
return new _AVLTreeMultiMap([], __spreadValues({
|
|
11723
11844
|
iterationType: this.iterationType,
|
|
@@ -11725,44 +11846,47 @@ var dataStructureTyped = (() => {
|
|
|
11725
11846
|
}, options));
|
|
11726
11847
|
}
|
|
11727
11848
|
/**
|
|
11728
|
-
* The function
|
|
11729
|
-
* @param
|
|
11730
|
-
* can be
|
|
11731
|
-
* @
|
|
11732
|
-
*
|
|
11733
|
-
|
|
11849
|
+
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
11850
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
11851
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
11852
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
11853
|
+
* an instance of the `AVLTreeMultiMapNode` class.
|
|
11854
|
+
*/
|
|
11855
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
11856
|
+
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode;
|
|
11857
|
+
}
|
|
11858
|
+
/**
|
|
11859
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
|
|
11860
|
+
* a node object.
|
|
11861
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
11862
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
11863
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
11864
|
+
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
11865
|
+
* value is provided, it will default to `undefined`.
|
|
11734
11866
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
11735
|
-
* times the value should be added to the
|
|
11736
|
-
* @returns a
|
|
11867
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
11868
|
+
* @returns either a NODE object or undefined.
|
|
11737
11869
|
*/
|
|
11738
|
-
|
|
11739
|
-
|
|
11740
|
-
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
11870
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
11871
|
+
if (keyOrNodeOrEntryOrRawElement === void 0 || keyOrNodeOrEntryOrRawElement === null)
|
|
11741
11872
|
return;
|
|
11742
|
-
|
|
11743
|
-
|
|
11744
|
-
|
|
11745
|
-
const [key,
|
|
11746
|
-
if (key
|
|
11873
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
11874
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
11875
|
+
if (this.toEntryFn) {
|
|
11876
|
+
const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
11877
|
+
if (key)
|
|
11878
|
+
return this.createNode(key, entryValue != null ? entryValue : value, count);
|
|
11879
|
+
}
|
|
11880
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
11881
|
+
const [key, value2] = keyOrNodeOrEntryOrRawElement;
|
|
11882
|
+
if (key === void 0 || key === null)
|
|
11747
11883
|
return;
|
|
11748
|
-
|
|
11749
|
-
|
|
11750
|
-
}
|
|
11751
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
11752
|
-
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
11753
|
-
} else {
|
|
11754
|
-
return;
|
|
11884
|
+
else
|
|
11885
|
+
return this.createNode(key, value2, count);
|
|
11755
11886
|
}
|
|
11756
|
-
|
|
11757
|
-
|
|
11758
|
-
|
|
11759
|
-
* The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
|
|
11760
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
11761
|
-
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
|
|
11762
|
-
* class.
|
|
11763
|
-
*/
|
|
11764
|
-
isNode(keyOrNodeOrEntry) {
|
|
11765
|
-
return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
11887
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
11888
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
11889
|
+
return;
|
|
11766
11890
|
}
|
|
11767
11891
|
/**
|
|
11768
11892
|
* Time Complexity: O(log n)
|
|
@@ -11772,20 +11896,21 @@ var dataStructureTyped = (() => {
|
|
|
11772
11896
|
* Time Complexity: O(log n)
|
|
11773
11897
|
* Space Complexity: O(1)
|
|
11774
11898
|
*
|
|
11775
|
-
* The function overrides the add method of a
|
|
11776
|
-
*
|
|
11777
|
-
*
|
|
11899
|
+
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
11900
|
+
* and update the count.
|
|
11901
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
11902
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It
|
|
11903
|
+
* can also accept a value of type `KeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
|
|
11904
|
+
* entry, or raw element
|
|
11778
11905
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
11779
|
-
*
|
|
11780
|
-
* method.
|
|
11906
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
11781
11907
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
11782
|
-
* be added to the
|
|
11783
|
-
* added once. However, you can specify a different value for `count` if you want to add
|
|
11784
|
-
* @returns
|
|
11785
|
-
* was not successful.
|
|
11908
|
+
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
|
|
11909
|
+
* be added once. However, you can specify a different value for `count` if you want to add
|
|
11910
|
+
* @returns a boolean value.
|
|
11786
11911
|
*/
|
|
11787
|
-
add(
|
|
11788
|
-
const newNode = this.
|
|
11912
|
+
add(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
11913
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
11789
11914
|
if (newNode === void 0)
|
|
11790
11915
|
return false;
|
|
11791
11916
|
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
@@ -11803,19 +11928,19 @@ var dataStructureTyped = (() => {
|
|
|
11803
11928
|
* Time Complexity: O(log n)
|
|
11804
11929
|
* Space Complexity: O(1)
|
|
11805
11930
|
*
|
|
11806
|
-
* The `delete` function in
|
|
11807
|
-
*
|
|
11808
|
-
* @param identifier - The identifier is the value
|
|
11809
|
-
*
|
|
11931
|
+
* The `delete` function in a binary tree data structure deletes a node based on its identifier and
|
|
11932
|
+
* returns the deleted node along with the parent node that needs to be balanced.
|
|
11933
|
+
* @param identifier - The identifier parameter is the value used to identify the node that needs to
|
|
11934
|
+
* be deleted from the binary tree. It can be of any type and is the return type of the callback
|
|
11810
11935
|
* function.
|
|
11811
|
-
* @param {C} callback - The `callback` parameter is a function that is used to determine
|
|
11812
|
-
*
|
|
11813
|
-
* function takes
|
|
11814
|
-
*
|
|
11936
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
11937
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
11938
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function takes a single argument, which is the identifier
|
|
11939
|
+
* of a node, and returns a value that
|
|
11815
11940
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
11816
11941
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
11817
|
-
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
11818
|
-
*
|
|
11942
|
+
* deleted regardless of its count. If set to false (default), the count of the node will be taken
|
|
11943
|
+
* into account and the node
|
|
11819
11944
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
11820
11945
|
*/
|
|
11821
11946
|
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
@@ -11879,7 +12004,8 @@ var dataStructureTyped = (() => {
|
|
|
11879
12004
|
* Time Complexity: O(1)
|
|
11880
12005
|
* Space Complexity: O(1)
|
|
11881
12006
|
*
|
|
11882
|
-
* The clear
|
|
12007
|
+
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
12008
|
+
* zero.
|
|
11883
12009
|
*/
|
|
11884
12010
|
clear() {
|
|
11885
12011
|
super.clear();
|
|
@@ -11892,13 +12018,14 @@ var dataStructureTyped = (() => {
|
|
|
11892
12018
|
/**
|
|
11893
12019
|
* Time Complexity: O(n log n)
|
|
11894
12020
|
* Space Complexity: O(log n)
|
|
11895
|
-
*
|
|
11896
12021
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
11897
12022
|
* tree using either a recursive or iterative approach.
|
|
11898
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
11899
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
11900
|
-
*
|
|
11901
|
-
*
|
|
12023
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
12024
|
+
* specifies the type of iteration to use when building the balanced binary search tree. It has a
|
|
12025
|
+
* default value of `this.iterationType`, which means it will use the iteration type currently set in
|
|
12026
|
+
* the object.
|
|
12027
|
+
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
|
|
12028
|
+
* balancing operation is successful, and `false` if there are no nodes to balance.
|
|
11902
12029
|
*/
|
|
11903
12030
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
11904
12031
|
const sorted = this.dfs((node) => node, "IN"), n = sorted.length;
|
|
@@ -11943,7 +12070,7 @@ var dataStructureTyped = (() => {
|
|
|
11943
12070
|
* Time complexity: O(n)
|
|
11944
12071
|
* Space complexity: O(n)
|
|
11945
12072
|
*
|
|
11946
|
-
* The
|
|
12073
|
+
* The function overrides the clone method to create a deep copy of a tree object.
|
|
11947
12074
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
11948
12075
|
*/
|
|
11949
12076
|
clone() {
|
|
@@ -11952,13 +12079,21 @@ var dataStructureTyped = (() => {
|
|
|
11952
12079
|
return cloned;
|
|
11953
12080
|
}
|
|
11954
12081
|
/**
|
|
11955
|
-
*
|
|
11956
|
-
*
|
|
11957
|
-
|
|
11958
|
-
|
|
11959
|
-
*
|
|
11960
|
-
*
|
|
11961
|
-
*
|
|
12082
|
+
* Time Complexity: O(1)
|
|
12083
|
+
* Space Complexity: O(1)
|
|
12084
|
+
*/
|
|
12085
|
+
/**
|
|
12086
|
+
* Time Complexity: O(1)
|
|
12087
|
+
* Space Complexity: O(1)
|
|
12088
|
+
*
|
|
12089
|
+
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
|
|
12090
|
+
* in a binary search tree.
|
|
12091
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
12092
|
+
* that will be swapped with the `destNode`.
|
|
12093
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
12094
|
+
* node where the properties will be swapped with the source node.
|
|
12095
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
12096
|
+
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
11962
12097
|
*/
|
|
11963
12098
|
_swapProperties(srcNode, destNode) {
|
|
11964
12099
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -11982,12 +12117,19 @@ var dataStructureTyped = (() => {
|
|
|
11982
12117
|
return void 0;
|
|
11983
12118
|
}
|
|
11984
12119
|
/**
|
|
12120
|
+
* Time Complexity: O(1)
|
|
12121
|
+
* Space Complexity: O(1)
|
|
12122
|
+
*/
|
|
12123
|
+
/**
|
|
12124
|
+
* Time Complexity: O(1)
|
|
12125
|
+
* Space Complexity: O(1)
|
|
12126
|
+
*
|
|
11985
12127
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
11986
|
-
* @param {NODE} oldNode - The
|
|
11987
|
-
*
|
|
11988
|
-
* @param {NODE} newNode - The `newNode` parameter is an
|
|
12128
|
+
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
12129
|
+
* data structure. It is of type NODE.
|
|
12130
|
+
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
|
|
11989
12131
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
11990
|
-
* superclass,
|
|
12132
|
+
* superclass, which is of type `NODE`.
|
|
11991
12133
|
*/
|
|
11992
12134
|
_replaceNode(oldNode, newNode) {
|
|
11993
12135
|
newNode.count = oldNode.count + newNode.count;
|
|
@@ -12032,19 +12174,19 @@ var dataStructureTyped = (() => {
|
|
|
12032
12174
|
};
|
|
12033
12175
|
var TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
12034
12176
|
/**
|
|
12035
|
-
* The constructor function initializes a
|
|
12036
|
-
*
|
|
12037
|
-
*
|
|
12038
|
-
*
|
|
12039
|
-
*
|
|
12040
|
-
*
|
|
12041
|
-
*
|
|
12177
|
+
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
12178
|
+
* @param keysOrNodesOrEntriesOrRawElements - The parameter `keysOrNodesOrEntriesOrRawElements` is an
|
|
12179
|
+
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
|
|
12180
|
+
* TreeMultiMap with initial data.
|
|
12181
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
12182
|
+
* behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
|
|
12183
|
+
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
12042
12184
|
*/
|
|
12043
|
-
constructor(
|
|
12185
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
12044
12186
|
super([], options);
|
|
12045
12187
|
__publicField(this, "_count", 0);
|
|
12046
|
-
if (
|
|
12047
|
-
this.addMany(
|
|
12188
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
12189
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
12048
12190
|
}
|
|
12049
12191
|
// TODO the _count is not accurate after nodes count modified
|
|
12050
12192
|
/**
|
|
@@ -12074,16 +12216,15 @@ var dataStructureTyped = (() => {
|
|
|
12074
12216
|
/**
|
|
12075
12217
|
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
12076
12218
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
12077
|
-
* which is a generic type representing the
|
|
12078
|
-
* @param {V} [value] - The `value` parameter
|
|
12079
|
-
* node. It is
|
|
12080
|
-
* function. If provided, it should be of type `V`.
|
|
12219
|
+
* which is a generic type representing the type of keys in the tree.
|
|
12220
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
12221
|
+
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
12081
12222
|
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
12082
12223
|
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
12083
12224
|
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
12084
12225
|
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
12085
12226
|
* with a key in the tree.
|
|
12086
|
-
* @returns A new instance of the TreeMultiMapNode class
|
|
12227
|
+
* @returns A new instance of the TreeMultiMapNode class, casted as NODE.
|
|
12087
12228
|
*/
|
|
12088
12229
|
createNode(key, value, color = "BLACK", count) {
|
|
12089
12230
|
return new TreeMultiMapNode(key, value, count, color);
|
|
@@ -12091,10 +12232,10 @@ var dataStructureTyped = (() => {
|
|
|
12091
12232
|
/**
|
|
12092
12233
|
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
12093
12234
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
12094
|
-
* configuration options for creating the `TreeMultiMap`. It
|
|
12095
|
-
*
|
|
12235
|
+
* configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
|
|
12236
|
+
* R>`.
|
|
12096
12237
|
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
12097
|
-
* existing `iterationType`
|
|
12238
|
+
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
12098
12239
|
*/
|
|
12099
12240
|
createTree(options) {
|
|
12100
12241
|
return new _TreeMultiMap([], __spreadValues({
|
|
@@ -12102,46 +12243,47 @@ var dataStructureTyped = (() => {
|
|
|
12102
12243
|
}, options));
|
|
12103
12244
|
}
|
|
12104
12245
|
/**
|
|
12105
|
-
* The function `
|
|
12106
|
-
*
|
|
12107
|
-
* @param
|
|
12108
|
-
*
|
|
12109
|
-
* @param {V} [value] - The `value` parameter is an optional value
|
|
12110
|
-
*
|
|
12111
|
-
*
|
|
12112
|
-
*
|
|
12113
|
-
*
|
|
12246
|
+
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
|
|
12247
|
+
* node based on the input.
|
|
12248
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
12249
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
12250
|
+
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
12251
|
+
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
12252
|
+
* an existing node.
|
|
12253
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
12254
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
12255
|
+
* @returns either a NODE object or undefined.
|
|
12114
12256
|
*/
|
|
12115
|
-
|
|
12116
|
-
|
|
12117
|
-
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
12257
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
12258
|
+
if (keyOrNodeOrEntryOrRawElement === void 0 || keyOrNodeOrEntryOrRawElement === null)
|
|
12118
12259
|
return;
|
|
12119
|
-
|
|
12120
|
-
|
|
12121
|
-
|
|
12122
|
-
const [key
|
|
12123
|
-
if (key
|
|
12260
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
12261
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
12262
|
+
if (this.toEntryFn) {
|
|
12263
|
+
const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
12264
|
+
if (key)
|
|
12265
|
+
return this.getNodeByKey(key);
|
|
12266
|
+
}
|
|
12267
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
12268
|
+
const [key, value2] = keyOrNodeOrEntryOrRawElement;
|
|
12269
|
+
if (key === void 0 || key === null)
|
|
12124
12270
|
return;
|
|
12125
|
-
|
|
12126
|
-
|
|
12127
|
-
}
|
|
12128
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
12129
|
-
node = this.createNode(keyOrNodeOrEntry, value, "BLACK", count);
|
|
12130
|
-
} else {
|
|
12131
|
-
return;
|
|
12271
|
+
else
|
|
12272
|
+
return this.createNode(key, value2, "BLACK", count);
|
|
12132
12273
|
}
|
|
12133
|
-
|
|
12274
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
12275
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value, "BLACK", count);
|
|
12276
|
+
return;
|
|
12134
12277
|
}
|
|
12135
12278
|
/**
|
|
12136
|
-
* The function
|
|
12137
|
-
*
|
|
12138
|
-
*
|
|
12139
|
-
*
|
|
12140
|
-
*
|
|
12141
|
-
* of the `TreeMultiMapNode` class.
|
|
12279
|
+
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
12280
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
12281
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
12282
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
12283
|
+
* an instance of the `TreeMultiMapNode` class.
|
|
12142
12284
|
*/
|
|
12143
|
-
isNode(
|
|
12144
|
-
return
|
|
12285
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
12286
|
+
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
|
|
12145
12287
|
}
|
|
12146
12288
|
/**
|
|
12147
12289
|
* Time Complexity: O(log n)
|
|
@@ -12151,17 +12293,20 @@ var dataStructureTyped = (() => {
|
|
|
12151
12293
|
* Time Complexity: O(log n)
|
|
12152
12294
|
* Space Complexity: O(1)
|
|
12153
12295
|
*
|
|
12154
|
-
* The function overrides the add method
|
|
12155
|
-
*
|
|
12296
|
+
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
12297
|
+
* the count and returning a boolean indicating success.
|
|
12298
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
12299
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
|
|
12156
12300
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
12157
|
-
* data structure.
|
|
12301
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
12158
12302
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
12159
|
-
* be added to the data structure. By default, it is set to 1, meaning that
|
|
12160
|
-
*
|
|
12161
|
-
* @returns a boolean value.
|
|
12303
|
+
* be added to the data structure. By default, it is set to 1, meaning that if no value is provided
|
|
12304
|
+
* for `count`, the key-value pair will be added once.
|
|
12305
|
+
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
12306
|
+
* was successful, and false otherwise.
|
|
12162
12307
|
*/
|
|
12163
|
-
add(
|
|
12164
|
-
const newNode = this.
|
|
12308
|
+
add(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
12309
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
12165
12310
|
const orgCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
12166
12311
|
const isSuccessAdded = super.add(newNode);
|
|
12167
12312
|
if (isSuccessAdded) {
|
|
@@ -12179,20 +12324,18 @@ var dataStructureTyped = (() => {
|
|
|
12179
12324
|
* Time Complexity: O(log n)
|
|
12180
12325
|
* Space Complexity: O(1)
|
|
12181
12326
|
*
|
|
12182
|
-
* The `delete`
|
|
12183
|
-
*
|
|
12184
|
-
*
|
|
12185
|
-
*
|
|
12186
|
-
*
|
|
12187
|
-
*
|
|
12188
|
-
*
|
|
12189
|
-
*
|
|
12190
|
-
*
|
|
12191
|
-
*
|
|
12192
|
-
*
|
|
12193
|
-
*
|
|
12194
|
-
* and the node will be deleted regardless of its count. If set to false (default), the count of the
|
|
12195
|
-
* target node will be decremented
|
|
12327
|
+
* The function `delete` is used to remove a node from a binary tree and fix the tree if necessary.
|
|
12328
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value or
|
|
12329
|
+
* key that is used to identify the node that needs to be deleted from the binary tree. It can be of
|
|
12330
|
+
* any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
12331
|
+
* the node to be deleted
|
|
12332
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
12333
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
12334
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function is used to compare nodes when searching for a
|
|
12335
|
+
* specific node or when performing other operations on the
|
|
12336
|
+
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
12337
|
+
* being deleted. If set to true, the count of the node will not be taken into account when deleting
|
|
12338
|
+
* it. If set to false, the count of the node will be decremented by 1 before deleting it.
|
|
12196
12339
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
12197
12340
|
*/
|
|
12198
12341
|
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
@@ -12300,10 +12443,12 @@ var dataStructureTyped = (() => {
|
|
|
12300
12443
|
*
|
|
12301
12444
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
12302
12445
|
* tree using either a recursive or iterative approach.
|
|
12303
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
12304
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
12305
|
-
*
|
|
12306
|
-
*
|
|
12446
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
12447
|
+
* specifies the type of iteration to use when building the balanced binary search tree. It has a
|
|
12448
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified by the
|
|
12449
|
+
* `iterationType` property of the current object.
|
|
12450
|
+
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
|
|
12451
|
+
* balancing operation is successful, and `false` if there are no nodes to balance.
|
|
12307
12452
|
*/
|
|
12308
12453
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
12309
12454
|
const sorted = this.dfs((node) => node, "IN"), n = sorted.length;
|
|
@@ -12357,15 +12502,22 @@ var dataStructureTyped = (() => {
|
|
|
12357
12502
|
return cloned;
|
|
12358
12503
|
}
|
|
12359
12504
|
/**
|
|
12360
|
-
*
|
|
12361
|
-
*
|
|
12362
|
-
|
|
12363
|
-
|
|
12364
|
-
*
|
|
12505
|
+
* Time Complexity: O(1)
|
|
12506
|
+
* Space Complexity: O(1)
|
|
12507
|
+
*/
|
|
12508
|
+
/**
|
|
12509
|
+
* Time Complexity: O(1)
|
|
12510
|
+
* Space Complexity: O(1)
|
|
12511
|
+
*
|
|
12512
|
+
* The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
|
|
12513
|
+
* in a binary search tree.
|
|
12514
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
12515
|
+
* that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
|
|
12516
|
+
* instance of the `BSTNKeyOrNode<K, NODE>` class.
|
|
12517
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
12518
|
+
* node where the properties will be swapped with the source node.
|
|
12365
12519
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
12366
|
-
* If
|
|
12367
|
-
* and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
|
|
12368
|
-
* If either `srcNode` or `destNode` is
|
|
12520
|
+
* If either `srcNode` or `destNode` is undefined, it returns undefined.
|
|
12369
12521
|
*/
|
|
12370
12522
|
_swapProperties(srcNode, destNode) {
|
|
12371
12523
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -12389,12 +12541,19 @@ var dataStructureTyped = (() => {
|
|
|
12389
12541
|
return void 0;
|
|
12390
12542
|
}
|
|
12391
12543
|
/**
|
|
12544
|
+
* Time Complexity: O(1)
|
|
12545
|
+
* Space Complexity: O(1)
|
|
12546
|
+
*/
|
|
12547
|
+
/**
|
|
12548
|
+
* Time Complexity: O(1)
|
|
12549
|
+
* Space Complexity: O(1)
|
|
12550
|
+
*
|
|
12392
12551
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
12393
|
-
* @param {NODE} oldNode - The `oldNode` parameter is
|
|
12394
|
-
*
|
|
12395
|
-
* @param {NODE} newNode - The `newNode` parameter is an
|
|
12552
|
+
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
|
|
12553
|
+
* structure.
|
|
12554
|
+
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
|
|
12396
12555
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
12397
|
-
* superclass,
|
|
12556
|
+
* superclass, which is of type `NODE`.
|
|
12398
12557
|
*/
|
|
12399
12558
|
_replaceNode(oldNode, newNode) {
|
|
12400
12559
|
newNode.count = oldNode.count + newNode.count;
|