data-structure-typed 1.51.7 → 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 +15 -387
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- 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 +82 -62
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +492 -392
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/cjs/data-structures/binary-tree/bst.js +256 -358
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +111 -119
- 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 +92 -76
- 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/heap/heap.d.ts +1 -3
- package/dist/cjs/data-structures/heap/heap.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 +7 -7
- package/dist/cjs/types/common.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +10 -1
- package/dist/cjs/utils/utils.d.ts +2 -1
- package/dist/cjs/utils/utils.js +27 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +493 -393
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/mjs/data-structures/binary-tree/bst.js +252 -357
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- 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/heap/heap.d.ts +1 -3
- 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 +7 -7
- package/dist/mjs/types/common.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +10 -1
- package/dist/mjs/utils/utils.d.ts +2 -1
- package/dist/mjs/utils/utils.js +25 -0
- package/dist/umd/data-structure-typed.js +1427 -1343
- package/dist/umd/data-structure-typed.min.js +4 -4
- 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 -100
- package/src/data-structures/binary-tree/avl-tree.ts +109 -80
- package/src/data-structures/binary-tree/binary-tree.ts +556 -433
- package/src/data-structures/binary-tree/bst.ts +286 -375
- package/src/data-structures/binary-tree/rb-tree.ts +132 -125
- package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/heap/heap.ts +1 -1
- 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 +11 -9
- package/src/types/common.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
- package/test/integration/all-in-one.test.ts +2 -2
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +3 -3
- package/test/integration/index.html +95 -11
- package/test/performance/reportor.ts +2 -3
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +75 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/overall.test.ts +24 -20
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +6 -6
- package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
- package/test/unit/utils/utils.test.ts +5 -0
- package/test/utils/big-o.ts +12 -6
- package/test/utils/console.ts +1 -1
- package/tsconfig-base.json +1 -0
|
@@ -160,6 +160,7 @@ var dataStructureTyped = (() => {
|
|
|
160
160
|
arrayRemove: () => arrayRemove,
|
|
161
161
|
calcMinUnitsRequired: () => calcMinUnitsRequired,
|
|
162
162
|
getMSB: () => getMSB,
|
|
163
|
+
isComparable: () => isComparable,
|
|
163
164
|
isThunk: () => isThunk,
|
|
164
165
|
isWeakKey: () => isWeakKey,
|
|
165
166
|
rangeCheck: () => rangeCheck,
|
|
@@ -711,6 +712,29 @@ var dataStructureTyped = (() => {
|
|
|
711
712
|
const multiplier = Math.pow(10, digit);
|
|
712
713
|
return Math.round(num * multiplier) / multiplier;
|
|
713
714
|
};
|
|
715
|
+
function isComparable(key) {
|
|
716
|
+
const keyType = typeof key;
|
|
717
|
+
if (keyType === "number")
|
|
718
|
+
return !isNaN(key);
|
|
719
|
+
if (keyType === "string")
|
|
720
|
+
return true;
|
|
721
|
+
if (keyType === "bigint")
|
|
722
|
+
return true;
|
|
723
|
+
if (keyType === "boolean")
|
|
724
|
+
return true;
|
|
725
|
+
if (keyType === "symbol")
|
|
726
|
+
return false;
|
|
727
|
+
if (keyType === "undefined")
|
|
728
|
+
return false;
|
|
729
|
+
if (keyType === "function")
|
|
730
|
+
return isComparable(key());
|
|
731
|
+
if (keyType === "object") {
|
|
732
|
+
if (key === null)
|
|
733
|
+
return true;
|
|
734
|
+
return false;
|
|
735
|
+
}
|
|
736
|
+
return false;
|
|
737
|
+
}
|
|
714
738
|
|
|
715
739
|
// src/utils/number.ts
|
|
716
740
|
function toBinaryString(num, digit = 32) {
|
|
@@ -724,23 +748,15 @@ var dataStructureTyped = (() => {
|
|
|
724
748
|
/**
|
|
725
749
|
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
726
750
|
* options.
|
|
727
|
-
* @param
|
|
751
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
728
752
|
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
729
753
|
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
730
754
|
*/
|
|
731
|
-
constructor(
|
|
755
|
+
constructor(entryOrRawElements = [], options) {
|
|
732
756
|
super();
|
|
733
757
|
__publicField(this, "_store", {});
|
|
734
758
|
__publicField(this, "_objMap", /* @__PURE__ */ new Map());
|
|
735
|
-
__publicField(this, "_toEntryFn"
|
|
736
|
-
if (this.isEntry(rawElement)) {
|
|
737
|
-
return rawElement;
|
|
738
|
-
} else {
|
|
739
|
-
throw new Error(
|
|
740
|
-
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
741
|
-
);
|
|
742
|
-
}
|
|
743
|
-
});
|
|
759
|
+
__publicField(this, "_toEntryFn");
|
|
744
760
|
__publicField(this, "_size", 0);
|
|
745
761
|
__publicField(this, "_hashFn", (key) => String(key));
|
|
746
762
|
if (options) {
|
|
@@ -752,8 +768,8 @@ var dataStructureTyped = (() => {
|
|
|
752
768
|
this._toEntryFn = toEntryFn;
|
|
753
769
|
}
|
|
754
770
|
}
|
|
755
|
-
if (
|
|
756
|
-
this.setMany(
|
|
771
|
+
if (entryOrRawElements) {
|
|
772
|
+
this.setMany(entryOrRawElements);
|
|
757
773
|
}
|
|
758
774
|
}
|
|
759
775
|
/**
|
|
@@ -847,23 +863,24 @@ var dataStructureTyped = (() => {
|
|
|
847
863
|
/**
|
|
848
864
|
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
849
865
|
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
850
|
-
* @param
|
|
866
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements of a type
|
|
851
867
|
* `T`.
|
|
852
868
|
* @returns The `setMany` function is returning an array of booleans.
|
|
853
869
|
*/
|
|
854
|
-
setMany(
|
|
870
|
+
setMany(entryOrRawElements) {
|
|
855
871
|
const results = [];
|
|
856
|
-
for (const rawEle of
|
|
872
|
+
for (const rawEle of entryOrRawElements) {
|
|
857
873
|
let key, value;
|
|
858
874
|
if (this.isEntry(rawEle)) {
|
|
859
875
|
key = rawEle[0];
|
|
860
876
|
value = rawEle[1];
|
|
861
|
-
} else {
|
|
877
|
+
} else if (this.toEntryFn) {
|
|
862
878
|
const item = this.toEntryFn(rawEle);
|
|
863
879
|
key = item[0];
|
|
864
880
|
value = item[1];
|
|
865
881
|
}
|
|
866
|
-
|
|
882
|
+
if (key !== void 0 && value !== void 0)
|
|
883
|
+
results.push(this.set(key, value));
|
|
867
884
|
}
|
|
868
885
|
return results;
|
|
869
886
|
}
|
|
@@ -1044,14 +1061,14 @@ var dataStructureTyped = (() => {
|
|
|
1044
1061
|
var LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
1045
1062
|
/**
|
|
1046
1063
|
* The constructor initializes a LinkedHashMap object with an optional raw collection and options.
|
|
1047
|
-
* @param
|
|
1064
|
+
* @param entryOrRawElements - The `entryOrRawElements` parameter is an iterable collection of elements. It is
|
|
1048
1065
|
* used to initialize the HashMapLinked instance with key-value pairs. Each element in the
|
|
1049
|
-
* `
|
|
1066
|
+
* `entryOrRawElements` is converted to a key-value pair using the `toEntryFn` function (if provided) and
|
|
1050
1067
|
* then added to the HashMap
|
|
1051
1068
|
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
1052
1069
|
* properties:
|
|
1053
1070
|
*/
|
|
1054
|
-
constructor(
|
|
1071
|
+
constructor(entryOrRawElements = [], options) {
|
|
1055
1072
|
super();
|
|
1056
1073
|
__publicField(this, "_sentinel");
|
|
1057
1074
|
__publicField(this, "_hashFn", (key) => String(key));
|
|
@@ -1065,7 +1082,7 @@ var dataStructureTyped = (() => {
|
|
|
1065
1082
|
return rawElement;
|
|
1066
1083
|
} else {
|
|
1067
1084
|
throw new Error(
|
|
1068
|
-
"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."
|
|
1069
1086
|
);
|
|
1070
1087
|
}
|
|
1071
1088
|
});
|
|
@@ -1082,8 +1099,8 @@ var dataStructureTyped = (() => {
|
|
|
1082
1099
|
this._toEntryFn = toEntryFn;
|
|
1083
1100
|
}
|
|
1084
1101
|
}
|
|
1085
|
-
if (
|
|
1086
|
-
for (const el of
|
|
1102
|
+
if (entryOrRawElements) {
|
|
1103
|
+
for (const el of entryOrRawElements) {
|
|
1087
1104
|
const [key, value] = this.toEntryFn(el);
|
|
1088
1105
|
this.set(key, value);
|
|
1089
1106
|
}
|
|
@@ -1122,7 +1139,7 @@ var dataStructureTyped = (() => {
|
|
|
1122
1139
|
/**
|
|
1123
1140
|
* The function returns the head node of a HashMapLinkedNode.
|
|
1124
1141
|
* @returns The method `getHead()` is returning a `HashMapLinkedNode` object with key type `K` and
|
|
1125
|
-
* value type `V | undefined`.
|
|
1142
|
+
* a value type `V | undefined`.
|
|
1126
1143
|
*/
|
|
1127
1144
|
get head() {
|
|
1128
1145
|
return this._head;
|
|
@@ -1258,13 +1275,13 @@ var dataStructureTyped = (() => {
|
|
|
1258
1275
|
* The function `setMany` takes an iterable collection, converts each element into a key-value pair
|
|
1259
1276
|
* using a provided function, and sets each key-value pair in the current object, returning an array
|
|
1260
1277
|
* of booleans indicating the success of each set operation.
|
|
1261
|
-
* @param
|
|
1278
|
+
* @param entryOrRawElements - The entryOrRawElements parameter is an iterable collection of elements of type
|
|
1262
1279
|
* R.
|
|
1263
1280
|
* @returns The `setMany` function returns an array of booleans.
|
|
1264
1281
|
*/
|
|
1265
|
-
setMany(
|
|
1282
|
+
setMany(entryOrRawElements) {
|
|
1266
1283
|
const results = [];
|
|
1267
|
-
for (const rawEle of
|
|
1284
|
+
for (const rawEle of entryOrRawElements) {
|
|
1268
1285
|
const [key, value] = this.toEntryFn(rawEle);
|
|
1269
1286
|
results.push(this.set(key, value));
|
|
1270
1287
|
}
|
|
@@ -1314,21 +1331,20 @@ var dataStructureTyped = (() => {
|
|
|
1314
1331
|
}
|
|
1315
1332
|
}
|
|
1316
1333
|
/**
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
*/
|
|
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
|
+
*/
|
|
1332
1348
|
at(index) {
|
|
1333
1349
|
rangeCheck(index, 0, this._size - 1);
|
|
1334
1350
|
let node = this.head;
|
|
@@ -1338,20 +1354,19 @@ var dataStructureTyped = (() => {
|
|
|
1338
1354
|
return node.value;
|
|
1339
1355
|
}
|
|
1340
1356
|
/**
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
*/
|
|
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
|
+
*/
|
|
1355
1370
|
delete(key) {
|
|
1356
1371
|
let node;
|
|
1357
1372
|
if (isWeakKey(key)) {
|
|
@@ -1373,19 +1388,18 @@ var dataStructureTyped = (() => {
|
|
|
1373
1388
|
return true;
|
|
1374
1389
|
}
|
|
1375
1390
|
/**
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
*/
|
|
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
|
+
*/
|
|
1389
1403
|
deleteAt(index) {
|
|
1390
1404
|
rangeCheck(index, 0, this._size - 1);
|
|
1391
1405
|
let node = this.head;
|
|
@@ -1395,18 +1409,17 @@ var dataStructureTyped = (() => {
|
|
|
1395
1409
|
return this._deleteNode(node);
|
|
1396
1410
|
}
|
|
1397
1411
|
/**
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
*/
|
|
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
|
+
*/
|
|
1410
1423
|
isEmpty() {
|
|
1411
1424
|
return this._size === 0;
|
|
1412
1425
|
}
|
|
@@ -1420,16 +1433,15 @@ var dataStructureTyped = (() => {
|
|
|
1420
1433
|
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
1421
1434
|
}
|
|
1422
1435
|
/**
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
*/
|
|
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
|
+
*/
|
|
1433
1445
|
clear() {
|
|
1434
1446
|
this._noObjMap = {};
|
|
1435
1447
|
this._size = 0;
|
|
@@ -1457,25 +1469,24 @@ var dataStructureTyped = (() => {
|
|
|
1457
1469
|
return cloned;
|
|
1458
1470
|
}
|
|
1459
1471
|
/**
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
*/
|
|
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
|
+
*/
|
|
1479
1490
|
filter(predicate, thisArg) {
|
|
1480
1491
|
const filteredMap = new _LinkedHashMap();
|
|
1481
1492
|
let index = 0;
|
|
@@ -1488,27 +1499,26 @@ var dataStructureTyped = (() => {
|
|
|
1488
1499
|
return filteredMap;
|
|
1489
1500
|
}
|
|
1490
1501
|
/**
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
*/
|
|
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
|
+
*/
|
|
1512
1522
|
map(callback, thisArg) {
|
|
1513
1523
|
const mappedMap = new _LinkedHashMap();
|
|
1514
1524
|
let index = 0;
|
|
@@ -1538,18 +1548,17 @@ var dataStructureTyped = (() => {
|
|
|
1538
1548
|
return this.set(key, value);
|
|
1539
1549
|
}
|
|
1540
1550
|
/**
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
*/
|
|
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
|
+
*/
|
|
1553
1562
|
*_getIterator() {
|
|
1554
1563
|
let node = this.head;
|
|
1555
1564
|
while (node !== this._sentinel) {
|
|
@@ -4552,17 +4561,16 @@ var dataStructureTyped = (() => {
|
|
|
4552
4561
|
return newDeque;
|
|
4553
4562
|
}
|
|
4554
4563
|
/**
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4563
|
-
|
|
4564
|
-
|
|
4565
|
-
*/
|
|
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
|
+
*/
|
|
4566
4574
|
*_getIterator() {
|
|
4567
4575
|
for (let i = 0; i < this.size; ++i) {
|
|
4568
4576
|
yield this.at(i);
|
|
@@ -5859,13 +5867,12 @@ var dataStructureTyped = (() => {
|
|
|
5859
5867
|
}
|
|
5860
5868
|
}
|
|
5861
5869
|
/**
|
|
5862
|
-
|
|
5863
|
-
|
|
5864
|
-
|
|
5865
|
-
|
|
5866
|
-
|
|
5867
|
-
|
|
5868
|
-
*/
|
|
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
|
+
*/
|
|
5869
5876
|
/**
|
|
5870
5877
|
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
5871
5878
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
@@ -5980,18 +5987,17 @@ var dataStructureTyped = (() => {
|
|
|
5980
5987
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
5981
5988
|
}
|
|
5982
5989
|
/**
|
|
5983
|
-
|
|
5984
|
-
|
|
5985
|
-
|
|
5986
|
-
|
|
5987
|
-
|
|
5988
|
-
|
|
5989
|
-
|
|
5990
|
-
|
|
5991
|
-
|
|
5992
|
-
|
|
5993
|
-
|
|
5994
|
-
*/
|
|
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
|
+
*/
|
|
5995
6001
|
/**
|
|
5996
6002
|
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
5997
6003
|
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
@@ -6104,29 +6110,28 @@ var dataStructureTyped = (() => {
|
|
|
6104
6110
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
6105
6111
|
}
|
|
6106
6112
|
/**
|
|
6107
|
-
|
|
6108
|
-
|
|
6109
|
-
|
|
6110
|
-
|
|
6111
|
-
|
|
6112
|
-
|
|
6113
|
-
|
|
6114
|
-
|
|
6115
|
-
|
|
6116
|
-
|
|
6117
|
-
|
|
6118
|
-
|
|
6119
|
-
|
|
6120
|
-
|
|
6121
|
-
|
|
6122
|
-
|
|
6123
|
-
|
|
6124
|
-
|
|
6125
|
-
|
|
6126
|
-
|
|
6127
|
-
|
|
6128
|
-
|
|
6129
|
-
*/
|
|
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
|
+
*/
|
|
6130
6135
|
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
6131
6136
|
if (getMin === void 0)
|
|
6132
6137
|
getMin = false;
|
|
@@ -6212,13 +6217,12 @@ var dataStructureTyped = (() => {
|
|
|
6212
6217
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
6213
6218
|
}
|
|
6214
6219
|
/**
|
|
6215
|
-
|
|
6216
|
-
|
|
6217
|
-
|
|
6218
|
-
|
|
6219
|
-
|
|
6220
|
-
|
|
6221
|
-
*/
|
|
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
|
+
*/
|
|
6222
6226
|
/**
|
|
6223
6227
|
* BellmanFord time:O(VE) space:O(VO)
|
|
6224
6228
|
* one to rest pairs
|
|
@@ -6226,27 +6230,26 @@ var dataStructureTyped = (() => {
|
|
|
6226
6230
|
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
6227
6231
|
*/
|
|
6228
6232
|
/**
|
|
6229
|
-
|
|
6230
|
-
|
|
6231
|
-
|
|
6232
|
-
|
|
6233
|
-
|
|
6234
|
-
|
|
6235
|
-
|
|
6236
|
-
|
|
6237
|
-
|
|
6238
|
-
|
|
6239
|
-
|
|
6240
|
-
|
|
6241
|
-
|
|
6242
|
-
|
|
6243
|
-
|
|
6244
|
-
|
|
6245
|
-
|
|
6246
|
-
|
|
6247
|
-
|
|
6248
|
-
|
|
6249
|
-
*/
|
|
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
|
+
*/
|
|
6250
6253
|
floydWarshall() {
|
|
6251
6254
|
var _a;
|
|
6252
6255
|
const idAndVertices = [...this._vertexMap];
|
|
@@ -7734,39 +7737,31 @@ var dataStructureTyped = (() => {
|
|
|
7734
7737
|
};
|
|
7735
7738
|
var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
7736
7739
|
/**
|
|
7737
|
-
* The constructor function initializes a binary tree object with optional
|
|
7738
|
-
* @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
|
|
7739
7742
|
* nodes to be added to the binary tree.
|
|
7740
7743
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
7741
7744
|
* configuration options for the binary tree. In this case, it is of type
|
|
7742
7745
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
7743
7746
|
* required.
|
|
7744
7747
|
*/
|
|
7745
|
-
constructor(
|
|
7748
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
7746
7749
|
super();
|
|
7747
7750
|
__publicField(this, "iterationType", "ITERATIVE");
|
|
7748
|
-
__publicField(this, "_extractor", (key) => typeof key === "number" ? key : Number(key));
|
|
7749
7751
|
__publicField(this, "_root");
|
|
7750
|
-
__publicField(this, "_size");
|
|
7752
|
+
__publicField(this, "_size", 0);
|
|
7751
7753
|
__publicField(this, "_NIL", new BinaryTreeNode(NaN));
|
|
7754
|
+
__publicField(this, "_toEntryFn");
|
|
7752
7755
|
__publicField(this, "_DEFAULT_CALLBACK", (node) => node ? node.key : void 0);
|
|
7753
7756
|
if (options) {
|
|
7754
|
-
const { iterationType,
|
|
7757
|
+
const { iterationType, toEntryFn } = options;
|
|
7755
7758
|
if (iterationType)
|
|
7756
7759
|
this.iterationType = iterationType;
|
|
7757
|
-
if (
|
|
7758
|
-
this.
|
|
7760
|
+
if (typeof toEntryFn === "function")
|
|
7761
|
+
this._toEntryFn = toEntryFn;
|
|
7759
7762
|
}
|
|
7760
|
-
|
|
7761
|
-
|
|
7762
|
-
this.addMany(keysOrNodesOrEntries);
|
|
7763
|
-
}
|
|
7764
|
-
/**
|
|
7765
|
-
* The function returns the value of the `_extractor` property.
|
|
7766
|
-
* @returns The `_extractor` property is being returned.
|
|
7767
|
-
*/
|
|
7768
|
-
get extractor() {
|
|
7769
|
-
return this._extractor;
|
|
7763
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
7764
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
7770
7765
|
}
|
|
7771
7766
|
/**
|
|
7772
7767
|
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
@@ -7790,6 +7785,13 @@ var dataStructureTyped = (() => {
|
|
|
7790
7785
|
get NIL() {
|
|
7791
7786
|
return this._NIL;
|
|
7792
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
|
+
}
|
|
7793
7795
|
/**
|
|
7794
7796
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
7795
7797
|
* @param {K} key - The key for the new node.
|
|
@@ -7810,36 +7812,42 @@ var dataStructureTyped = (() => {
|
|
|
7810
7812
|
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
7811
7813
|
}
|
|
7812
7814
|
/**
|
|
7813
|
-
* The function `
|
|
7814
|
-
*
|
|
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>`.
|
|
7815
7819
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
7816
|
-
* `
|
|
7817
|
-
*
|
|
7818
|
-
* @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`.
|
|
7819
7824
|
*/
|
|
7820
|
-
|
|
7821
|
-
if (
|
|
7825
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
7826
|
+
if (keyOrNodeOrEntryOrRawElement === void 0)
|
|
7822
7827
|
return;
|
|
7823
|
-
|
|
7824
|
-
|
|
7825
|
-
|
|
7826
|
-
|
|
7827
|
-
|
|
7828
|
-
|
|
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
|
|
7829
7837
|
return;
|
|
7830
|
-
} else if (key === null) {
|
|
7831
|
-
node = null;
|
|
7832
|
-
} else {
|
|
7833
|
-
node = this.createNode(key, value2);
|
|
7834
|
-
}
|
|
7835
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
7836
|
-
node = keyOrNodeOrEntry;
|
|
7837
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
7838
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
7839
|
-
} else {
|
|
7840
|
-
return;
|
|
7841
7838
|
}
|
|
7842
|
-
|
|
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;
|
|
7843
7851
|
}
|
|
7844
7852
|
/**
|
|
7845
7853
|
* Time Complexity: O(n)
|
|
@@ -7849,56 +7857,56 @@ var dataStructureTyped = (() => {
|
|
|
7849
7857
|
* Time Complexity: O(n)
|
|
7850
7858
|
* Space Complexity: O(log n)
|
|
7851
7859
|
*
|
|
7852
|
-
* The
|
|
7853
|
-
*
|
|
7854
|
-
* @param {
|
|
7855
|
-
* `
|
|
7856
|
-
*
|
|
7857
|
-
*
|
|
7858
|
-
*
|
|
7859
|
-
*
|
|
7860
|
-
*
|
|
7861
|
-
*/
|
|
7862
|
-
ensureNode(
|
|
7863
|
-
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)
|
|
7864
7874
|
return;
|
|
7865
|
-
if (this.
|
|
7866
|
-
return
|
|
7867
|
-
|
|
7868
|
-
|
|
7869
|
-
|
|
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];
|
|
7870
7886
|
if (key === null)
|
|
7871
7887
|
return null;
|
|
7872
7888
|
if (key === void 0)
|
|
7873
7889
|
return;
|
|
7874
7890
|
return this.getNodeByKey(key, iterationType);
|
|
7875
7891
|
}
|
|
7876
|
-
if (
|
|
7877
|
-
return
|
|
7878
|
-
|
|
7879
|
-
return;
|
|
7880
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
7881
|
-
}
|
|
7882
|
-
/**
|
|
7883
|
-
* The function checks if a given node is a real node or null.
|
|
7884
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7885
|
-
* @returns a boolean value.
|
|
7886
|
-
*/
|
|
7887
|
-
isNodeOrNull(node) {
|
|
7888
|
-
return this.isRealNode(node) || node === null;
|
|
7892
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
7893
|
+
return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
|
|
7894
|
+
return;
|
|
7889
7895
|
}
|
|
7890
7896
|
/**
|
|
7891
|
-
* The function
|
|
7892
|
-
* @param
|
|
7893
|
-
*
|
|
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.
|
|
7894
7902
|
*/
|
|
7895
|
-
isNode(
|
|
7896
|
-
return
|
|
7903
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
7904
|
+
return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
|
|
7897
7905
|
}
|
|
7898
7906
|
/**
|
|
7899
|
-
* The function checks if a given node is a
|
|
7900
|
-
*
|
|
7901
|
-
*
|
|
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>`.
|
|
7902
7910
|
* @returns a boolean value.
|
|
7903
7911
|
*/
|
|
7904
7912
|
isRealNode(node) {
|
|
@@ -7907,21 +7915,64 @@ var dataStructureTyped = (() => {
|
|
|
7907
7915
|
return this.isNode(node);
|
|
7908
7916
|
}
|
|
7909
7917
|
/**
|
|
7910
|
-
* The function checks if a given node is a
|
|
7911
|
-
* @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>`.
|
|
7912
7930
|
* @returns a boolean value.
|
|
7913
7931
|
*/
|
|
7914
7932
|
isNIL(node) {
|
|
7915
7933
|
return node === this.NIL;
|
|
7916
7934
|
}
|
|
7917
7935
|
/**
|
|
7918
|
-
* The function checks if
|
|
7919
|
-
*
|
|
7920
|
-
*
|
|
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().
|
|
7921
7953
|
* @returns a boolean value.
|
|
7922
7954
|
*/
|
|
7923
|
-
|
|
7924
|
-
|
|
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;
|
|
7925
7976
|
}
|
|
7926
7977
|
/**
|
|
7927
7978
|
* Time Complexity O(n)
|
|
@@ -7931,14 +7982,20 @@ var dataStructureTyped = (() => {
|
|
|
7931
7982
|
* Time Complexity O(n)
|
|
7932
7983
|
* Space Complexity O(1)
|
|
7933
7984
|
*
|
|
7934
|
-
* The `add` function
|
|
7935
|
-
*
|
|
7936
|
-
* @param
|
|
7937
|
-
*
|
|
7938
|
-
*
|
|
7939
|
-
|
|
7940
|
-
|
|
7941
|
-
|
|
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);
|
|
7942
7999
|
if (newNode === void 0)
|
|
7943
8000
|
return false;
|
|
7944
8001
|
if (!this.root) {
|
|
@@ -7986,19 +8043,23 @@ var dataStructureTyped = (() => {
|
|
|
7986
8043
|
* Time Complexity: O(k * n)
|
|
7987
8044
|
* Space Complexity: O(1)
|
|
7988
8045
|
*
|
|
7989
|
-
* The `addMany` function takes in
|
|
7990
|
-
* adds each node with its corresponding value to
|
|
7991
|
-
*
|
|
7992
|
-
* @param
|
|
7993
|
-
*
|
|
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.
|
|
7994
8055
|
*/
|
|
7995
|
-
addMany(
|
|
8056
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values) {
|
|
7996
8057
|
const inserted = [];
|
|
7997
8058
|
let valuesIterator;
|
|
7998
8059
|
if (values) {
|
|
7999
8060
|
valuesIterator = values[Symbol.iterator]();
|
|
8000
8061
|
}
|
|
8001
|
-
for (const
|
|
8062
|
+
for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
|
|
8002
8063
|
let value = void 0;
|
|
8003
8064
|
if (valuesIterator) {
|
|
8004
8065
|
const valueResult = valuesIterator.next();
|
|
@@ -8006,7 +8067,7 @@ var dataStructureTyped = (() => {
|
|
|
8006
8067
|
value = valueResult.value;
|
|
8007
8068
|
}
|
|
8008
8069
|
}
|
|
8009
|
-
inserted.push(this.add(
|
|
8070
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
|
|
8010
8071
|
}
|
|
8011
8072
|
return inserted;
|
|
8012
8073
|
}
|
|
@@ -8019,38 +8080,36 @@ var dataStructureTyped = (() => {
|
|
|
8019
8080
|
* Time Complexity: O(k * n)
|
|
8020
8081
|
* Space Complexity: O(1)
|
|
8021
8082
|
*
|
|
8022
|
-
* The `refill` function clears the current data and adds new
|
|
8023
|
-
* @param
|
|
8024
|
-
* KeyOrNodeOrEntry<K, V, NODE
|
|
8025
|
-
* @param [values] - The `values` parameter is an optional iterable
|
|
8026
|
-
*
|
|
8027
|
-
*
|
|
8028
|
-
* `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`.
|
|
8029
8089
|
*/
|
|
8030
|
-
refill(
|
|
8090
|
+
refill(keysOrNodesOrEntriesOrRawElements, values) {
|
|
8031
8091
|
this.clear();
|
|
8032
|
-
this.addMany(
|
|
8033
|
-
}
|
|
8034
|
-
/**
|
|
8035
|
-
|
|
8036
|
-
|
|
8037
|
-
|
|
8038
|
-
|
|
8039
|
-
|
|
8040
|
-
|
|
8041
|
-
|
|
8042
|
-
|
|
8043
|
-
|
|
8044
|
-
|
|
8045
|
-
|
|
8046
|
-
|
|
8047
|
-
|
|
8048
|
-
|
|
8049
|
-
|
|
8050
|
-
|
|
8051
|
-
|
|
8052
|
-
|
|
8053
|
-
*/
|
|
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
|
+
*/
|
|
8054
8113
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
8055
8114
|
const deletedResult = [];
|
|
8056
8115
|
if (!this.root)
|
|
@@ -8099,28 +8158,27 @@ var dataStructureTyped = (() => {
|
|
|
8099
8158
|
*/
|
|
8100
8159
|
/**
|
|
8101
8160
|
* Time Complexity: O(n)
|
|
8102
|
-
* Space Complexity: O(k + log n)
|
|
8161
|
+
* Space Complexity: O(k + log n)
|
|
8103
8162
|
*
|
|
8104
|
-
* The function `getNodes`
|
|
8105
|
-
*
|
|
8163
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
8164
|
+
* recursive or iterative approach.
|
|
8106
8165
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8107
|
-
* that
|
|
8108
|
-
* callback function
|
|
8109
|
-
*
|
|
8110
|
-
*
|
|
8111
|
-
*
|
|
8112
|
-
*
|
|
8113
|
-
*
|
|
8114
|
-
*
|
|
8115
|
-
*
|
|
8116
|
-
*
|
|
8117
|
-
*
|
|
8118
|
-
*
|
|
8119
|
-
*
|
|
8120
|
-
*
|
|
8121
|
-
*
|
|
8122
|
-
*
|
|
8123
|
-
* @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.
|
|
8124
8182
|
*/
|
|
8125
8183
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8126
8184
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -8164,24 +8222,21 @@ var dataStructureTyped = (() => {
|
|
|
8164
8222
|
*/
|
|
8165
8223
|
/**
|
|
8166
8224
|
* Time Complexity: O(n)
|
|
8167
|
-
* Space Complexity: O(log n)
|
|
8225
|
+
* Space Complexity: O(log n).
|
|
8168
8226
|
*
|
|
8169
|
-
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
8170
|
-
*
|
|
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.
|
|
8171
8229
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8172
|
-
* used to identify the node you want to retrieve. It can be of any type that is
|
|
8173
|
-
* callback function
|
|
8174
|
-
*
|
|
8175
|
-
*
|
|
8176
|
-
*
|
|
8177
|
-
*
|
|
8178
|
-
*
|
|
8179
|
-
*
|
|
8180
|
-
*
|
|
8181
|
-
* @
|
|
8182
|
-
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
8183
|
-
* nodes are visited during the search.
|
|
8184
|
-
* @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.
|
|
8185
8240
|
*/
|
|
8186
8241
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8187
8242
|
var _a;
|
|
@@ -8195,15 +8250,13 @@ var dataStructureTyped = (() => {
|
|
|
8195
8250
|
* Time Complexity: O(n)
|
|
8196
8251
|
* Space Complexity: O(log n)
|
|
8197
8252
|
*
|
|
8198
|
-
* The function `getNodeByKey`
|
|
8199
|
-
*
|
|
8200
|
-
*
|
|
8201
|
-
*
|
|
8202
|
-
*
|
|
8203
|
-
*
|
|
8204
|
-
*
|
|
8205
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
8206
|
-
* 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.
|
|
8207
8260
|
*/
|
|
8208
8261
|
getNodeByKey(key, iterationType = "ITERATIVE") {
|
|
8209
8262
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
@@ -8216,23 +8269,22 @@ var dataStructureTyped = (() => {
|
|
|
8216
8269
|
* Time Complexity: O(n)
|
|
8217
8270
|
* Space Complexity: O(log n)
|
|
8218
8271
|
*
|
|
8219
|
-
* The function `get`
|
|
8220
|
-
*
|
|
8272
|
+
* The function `get` in TypeScript overrides the base class method and returns the value associated
|
|
8273
|
+
* with the given identifier.
|
|
8221
8274
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8222
|
-
* 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
|
|
8223
8276
|
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
8224
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
8225
|
-
* the
|
|
8226
|
-
*
|
|
8227
|
-
*
|
|
8228
|
-
*
|
|
8229
|
-
*
|
|
8230
|
-
*
|
|
8231
|
-
*
|
|
8232
|
-
*
|
|
8233
|
-
*
|
|
8234
|
-
*
|
|
8235
|
-
* 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.
|
|
8236
8288
|
*/
|
|
8237
8289
|
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8238
8290
|
var _a;
|
|
@@ -8240,28 +8292,27 @@ var dataStructureTyped = (() => {
|
|
|
8240
8292
|
}
|
|
8241
8293
|
/**
|
|
8242
8294
|
* Time Complexity: O(n)
|
|
8243
|
-
* Space Complexity: O(log n)
|
|
8295
|
+
* Space Complexity: O(log n)
|
|
8244
8296
|
*/
|
|
8245
8297
|
/**
|
|
8246
8298
|
* Time Complexity: O(n)
|
|
8247
|
-
* Space Complexity: O(log n)
|
|
8299
|
+
* Space Complexity: O(log n)
|
|
8248
8300
|
*
|
|
8249
|
-
* 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.
|
|
8250
8303
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
8251
|
-
*
|
|
8252
|
-
* callback function `C`. It can also be `null` or `undefined` if
|
|
8253
|
-
*
|
|
8254
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
8255
|
-
*
|
|
8256
|
-
*
|
|
8257
|
-
*
|
|
8258
|
-
*
|
|
8259
|
-
*
|
|
8260
|
-
*
|
|
8261
|
-
*
|
|
8262
|
-
*
|
|
8263
|
-
* be performed in a pre-order, in-order, or post-order manner.
|
|
8264
|
-
* @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.
|
|
8265
8316
|
*/
|
|
8266
8317
|
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8267
8318
|
callback = this._ensureCallback(identifier, callback);
|
|
@@ -8305,9 +8356,10 @@ var dataStructureTyped = (() => {
|
|
|
8305
8356
|
*
|
|
8306
8357
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
8307
8358
|
* height of the tree.
|
|
8308
|
-
* @param {
|
|
8309
|
-
*
|
|
8310
|
-
*
|
|
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
|
|
8311
8363
|
* @returns a boolean value.
|
|
8312
8364
|
*/
|
|
8313
8365
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
@@ -8321,12 +8373,14 @@ var dataStructureTyped = (() => {
|
|
|
8321
8373
|
* Time Complexity: O(n)
|
|
8322
8374
|
* Space Complexity: O(1)
|
|
8323
8375
|
*
|
|
8324
|
-
* The function `
|
|
8325
|
-
* @param {
|
|
8326
|
-
*
|
|
8327
|
-
*
|
|
8328
|
-
*
|
|
8329
|
-
*
|
|
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:
|
|
8330
8384
|
* @returns a boolean value.
|
|
8331
8385
|
*/
|
|
8332
8386
|
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -8337,7 +8391,7 @@ var dataStructureTyped = (() => {
|
|
|
8337
8391
|
const dfs = (cur, min, max) => {
|
|
8338
8392
|
if (!this.isRealNode(cur))
|
|
8339
8393
|
return true;
|
|
8340
|
-
const numKey =
|
|
8394
|
+
const numKey = Number(cur.key);
|
|
8341
8395
|
if (numKey <= min || numKey >= max)
|
|
8342
8396
|
return false;
|
|
8343
8397
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
@@ -8356,7 +8410,7 @@ var dataStructureTyped = (() => {
|
|
|
8356
8410
|
curr = curr.left;
|
|
8357
8411
|
}
|
|
8358
8412
|
curr = stack.pop();
|
|
8359
|
-
const numKey =
|
|
8413
|
+
const numKey = Number(curr.key);
|
|
8360
8414
|
if (!this.isRealNode(curr) || !checkMax && prev >= numKey || checkMax && prev <= numKey)
|
|
8361
8415
|
return false;
|
|
8362
8416
|
prev = numKey;
|
|
@@ -8376,25 +8430,26 @@ var dataStructureTyped = (() => {
|
|
|
8376
8430
|
* Time Complexity: O(n)
|
|
8377
8431
|
* Space Complexity: O(1)
|
|
8378
8432
|
*
|
|
8379
|
-
* The function calculates the depth of a given node in a
|
|
8380
|
-
* @param {
|
|
8381
|
-
*
|
|
8382
|
-
*
|
|
8383
|
-
* @param {
|
|
8384
|
-
* from which
|
|
8385
|
-
*
|
|
8386
|
-
*
|
|
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.
|
|
8387
8442
|
*/
|
|
8388
8443
|
getDepth(dist, beginRoot = this.root) {
|
|
8389
|
-
|
|
8390
|
-
|
|
8444
|
+
let distEnsured = this.ensureNode(dist);
|
|
8445
|
+
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
8391
8446
|
let depth = 0;
|
|
8392
|
-
while (
|
|
8393
|
-
if (
|
|
8447
|
+
while (distEnsured == null ? void 0 : distEnsured.parent) {
|
|
8448
|
+
if (distEnsured === beginRootEnsured) {
|
|
8394
8449
|
return depth;
|
|
8395
8450
|
}
|
|
8396
8451
|
depth++;
|
|
8397
|
-
|
|
8452
|
+
distEnsured = distEnsured.parent;
|
|
8398
8453
|
}
|
|
8399
8454
|
return depth;
|
|
8400
8455
|
}
|
|
@@ -8404,17 +8459,16 @@ var dataStructureTyped = (() => {
|
|
|
8404
8459
|
*/
|
|
8405
8460
|
/**
|
|
8406
8461
|
* Time Complexity: O(n)
|
|
8407
|
-
* Space Complexity: O(
|
|
8462
|
+
* Space Complexity: O(1)
|
|
8408
8463
|
*
|
|
8409
|
-
* The
|
|
8410
|
-
* iterative
|
|
8411
|
-
* @param {
|
|
8412
|
-
* starting
|
|
8413
|
-
*
|
|
8414
|
-
* @param iterationType - The `iterationType` parameter
|
|
8415
|
-
*
|
|
8416
|
-
*
|
|
8417
|
-
* @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.
|
|
8418
8472
|
*/
|
|
8419
8473
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8420
8474
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -8453,12 +8507,15 @@ var dataStructureTyped = (() => {
|
|
|
8453
8507
|
*
|
|
8454
8508
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
8455
8509
|
* recursive or iterative approach.
|
|
8456
|
-
* @param {
|
|
8457
|
-
* starting
|
|
8458
|
-
*
|
|
8459
|
-
*
|
|
8460
|
-
*
|
|
8461
|
-
*
|
|
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.
|
|
8462
8519
|
*/
|
|
8463
8520
|
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8464
8521
|
var _a, _b, _c;
|
|
@@ -8503,34 +8560,32 @@ var dataStructureTyped = (() => {
|
|
|
8503
8560
|
}
|
|
8504
8561
|
}
|
|
8505
8562
|
/**
|
|
8506
|
-
|
|
8507
|
-
|
|
8508
|
-
|
|
8509
|
-
|
|
8510
|
-
|
|
8511
|
-
|
|
8512
|
-
|
|
8513
|
-
|
|
8514
|
-
|
|
8515
|
-
|
|
8516
|
-
|
|
8517
|
-
|
|
8518
|
-
|
|
8519
|
-
|
|
8520
|
-
|
|
8521
|
-
|
|
8522
|
-
* @returns The function `getPathToRoot` returns an array of nodes (`NODE[]`).
|
|
8523
|
-
*/
|
|
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
|
+
*/
|
|
8524
8579
|
getPathToRoot(beginNode, isReverse = true) {
|
|
8525
8580
|
const result = [];
|
|
8526
|
-
|
|
8527
|
-
if (!
|
|
8581
|
+
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
8582
|
+
if (!beginNodeEnsured)
|
|
8528
8583
|
return result;
|
|
8529
|
-
while (
|
|
8530
|
-
result.push(
|
|
8531
|
-
|
|
8584
|
+
while (beginNodeEnsured.parent) {
|
|
8585
|
+
result.push(beginNodeEnsured);
|
|
8586
|
+
beginNodeEnsured = beginNodeEnsured.parent;
|
|
8532
8587
|
}
|
|
8533
|
-
result.push(
|
|
8588
|
+
result.push(beginNodeEnsured);
|
|
8534
8589
|
return isReverse ? result.reverse() : result;
|
|
8535
8590
|
}
|
|
8536
8591
|
/**
|
|
@@ -8541,15 +8596,14 @@ var dataStructureTyped = (() => {
|
|
|
8541
8596
|
* Time Complexity: O(log n)
|
|
8542
8597
|
* Space Complexity: O(1)
|
|
8543
8598
|
*
|
|
8544
|
-
* The
|
|
8545
|
-
*
|
|
8546
|
-
* @param {
|
|
8547
|
-
* for finding the leftmost node in a binary tree. It can be either a `
|
|
8548
|
-
*
|
|
8549
|
-
* @param iterationType - The `iterationType` parameter is used to
|
|
8550
|
-
*
|
|
8551
|
-
* @returns The function `getLeftMost` returns the leftmost node
|
|
8552
|
-
* 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.
|
|
8553
8607
|
*/
|
|
8554
8608
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8555
8609
|
if (this.isNIL(beginRoot))
|
|
@@ -8581,16 +8635,15 @@ var dataStructureTyped = (() => {
|
|
|
8581
8635
|
* Time Complexity: O(log n)
|
|
8582
8636
|
* Space Complexity: O(1)
|
|
8583
8637
|
*
|
|
8584
|
-
* The
|
|
8638
|
+
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
|
|
8585
8639
|
* iteratively.
|
|
8586
|
-
* @param {
|
|
8587
|
-
* starting
|
|
8588
|
-
* `
|
|
8589
|
-
*
|
|
8590
|
-
*
|
|
8591
|
-
*
|
|
8592
|
-
* @returns The function `getRightMost` returns
|
|
8593
|
-
* 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`.
|
|
8594
8647
|
*/
|
|
8595
8648
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8596
8649
|
if (this.isNIL(beginRoot))
|
|
@@ -8622,10 +8675,10 @@ var dataStructureTyped = (() => {
|
|
|
8622
8675
|
* Time Complexity: O(log n)
|
|
8623
8676
|
* Space Complexity: O(1)
|
|
8624
8677
|
*
|
|
8625
|
-
* The function returns the predecessor of a given node in a tree.
|
|
8626
|
-
* @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
|
|
8627
8680
|
* tree.
|
|
8628
|
-
* @returns the predecessor of the given
|
|
8681
|
+
* @returns the predecessor node of the given node.
|
|
8629
8682
|
*/
|
|
8630
8683
|
getPredecessor(node) {
|
|
8631
8684
|
if (this.isRealNode(node.left)) {
|
|
@@ -8650,8 +8703,8 @@ var dataStructureTyped = (() => {
|
|
|
8650
8703
|
*
|
|
8651
8704
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
8652
8705
|
* @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
|
|
8653
|
-
* @returns
|
|
8654
|
-
*
|
|
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.
|
|
8655
8708
|
*/
|
|
8656
8709
|
getSuccessor(x) {
|
|
8657
8710
|
x = this.ensureNode(x);
|
|
@@ -8668,33 +8721,32 @@ var dataStructureTyped = (() => {
|
|
|
8668
8721
|
return y;
|
|
8669
8722
|
}
|
|
8670
8723
|
/**
|
|
8671
|
-
|
|
8672
|
-
|
|
8673
|
-
|
|
8674
|
-
|
|
8675
|
-
|
|
8676
|
-
|
|
8677
|
-
|
|
8678
|
-
|
|
8679
|
-
|
|
8680
|
-
|
|
8681
|
-
|
|
8682
|
-
|
|
8683
|
-
|
|
8684
|
-
|
|
8685
|
-
|
|
8686
|
-
|
|
8687
|
-
|
|
8688
|
-
|
|
8689
|
-
|
|
8690
|
-
|
|
8691
|
-
|
|
8692
|
-
|
|
8693
|
-
|
|
8694
|
-
|
|
8695
|
-
|
|
8696
|
-
|
|
8697
|
-
*/
|
|
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
|
+
*/
|
|
8698
8750
|
dfs(callback = this._DEFAULT_CALLBACK, pattern = "IN", beginRoot = this.root, iterationType = "ITERATIVE", includeNull = false) {
|
|
8699
8751
|
beginRoot = this.ensureNode(beginRoot);
|
|
8700
8752
|
if (!beginRoot)
|
|
@@ -8802,22 +8854,23 @@ var dataStructureTyped = (() => {
|
|
|
8802
8854
|
* Time complexity: O(n)
|
|
8803
8855
|
* Space complexity: O(n)
|
|
8804
8856
|
*
|
|
8805
|
-
* The `bfs` function performs a breadth-first search
|
|
8806
|
-
*
|
|
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.
|
|
8807
8859
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8808
|
-
* 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
|
|
8809
8861
|
* visited, and returns a value of any type.
|
|
8810
|
-
* @param {
|
|
8811
|
-
* starting
|
|
8812
|
-
* or
|
|
8813
|
-
*
|
|
8814
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
8815
|
-
*
|
|
8816
|
-
* @param [includeNull=false] - The `includeNull` parameter is a boolean
|
|
8817
|
-
* to include null values in the breadth-first search traversal. If
|
|
8818
|
-
* `true`, null values will be included in the traversal
|
|
8819
|
-
*
|
|
8820
|
-
*
|
|
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.
|
|
8821
8874
|
*/
|
|
8822
8875
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
8823
8876
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -8877,18 +8930,18 @@ var dataStructureTyped = (() => {
|
|
|
8877
8930
|
* Space complexity: O(n)
|
|
8878
8931
|
*
|
|
8879
8932
|
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
8880
|
-
* a binary tree and contains the
|
|
8881
|
-
* level.
|
|
8933
|
+
* a binary tree and contains the results of applying a callback function to the nodes at that level.
|
|
8882
8934
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8883
|
-
* the tree. It takes a
|
|
8884
|
-
*
|
|
8885
|
-
* @param {
|
|
8886
|
-
* starting
|
|
8887
|
-
*
|
|
8888
|
-
*
|
|
8889
|
-
*
|
|
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:
|
|
8890
8943
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
8891
|
-
* 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`,
|
|
8892
8945
|
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
8893
8946
|
* be excluded
|
|
8894
8947
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
@@ -8950,17 +9003,17 @@ var dataStructureTyped = (() => {
|
|
|
8950
9003
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
8951
9004
|
* algorithm.
|
|
8952
9005
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
8953
|
-
* the tree. It takes a single
|
|
8954
|
-
*
|
|
8955
|
-
*
|
|
8956
|
-
*
|
|
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
|
|
8957
9011
|
* following values:
|
|
8958
|
-
* @param {
|
|
8959
|
-
* for the traversal. It can be
|
|
8960
|
-
* the root of the tree
|
|
8961
|
-
* @returns The function `morris` returns an array of values that are the
|
|
8962
|
-
*
|
|
8963
|
-
* 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`.
|
|
8964
9017
|
*/
|
|
8965
9018
|
morris(callback = this._DEFAULT_CALLBACK, pattern = "IN", beginRoot = this.root) {
|
|
8966
9019
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -9051,8 +9104,7 @@ var dataStructureTyped = (() => {
|
|
|
9051
9104
|
* Time complexity: O(n)
|
|
9052
9105
|
* Space complexity: O(n)
|
|
9053
9106
|
*
|
|
9054
|
-
* The `clone` function creates a
|
|
9055
|
-
* the new tree.
|
|
9107
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
9056
9108
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
9057
9109
|
*/
|
|
9058
9110
|
clone() {
|
|
@@ -9078,16 +9130,16 @@ var dataStructureTyped = (() => {
|
|
|
9078
9130
|
* Time Complexity: O(n)
|
|
9079
9131
|
* Space Complexity: O(n)
|
|
9080
9132
|
*
|
|
9081
|
-
* The `filter` function creates a new tree
|
|
9082
|
-
*
|
|
9083
|
-
*
|
|
9084
|
-
*
|
|
9085
|
-
*
|
|
9086
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
9087
|
-
*
|
|
9088
|
-
*
|
|
9089
|
-
* @returns The `filter` method is returning a new tree object that contains the
|
|
9090
|
-
*
|
|
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.
|
|
9091
9143
|
*/
|
|
9092
9144
|
filter(predicate, thisArg) {
|
|
9093
9145
|
const newTree = this.createTree();
|
|
@@ -9107,15 +9159,15 @@ var dataStructureTyped = (() => {
|
|
|
9107
9159
|
* Time Complexity: O(n)
|
|
9108
9160
|
* Space Complexity: O(n)
|
|
9109
9161
|
*
|
|
9110
|
-
* The `map` function creates a new tree by applying a callback function to each
|
|
9111
|
-
*
|
|
9112
|
-
* @param callback - The callback parameter is a function that will be called for each
|
|
9113
|
-
*
|
|
9114
|
-
* the
|
|
9115
|
-
*
|
|
9116
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
9117
|
-
*
|
|
9118
|
-
*
|
|
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
|
|
9119
9171
|
* @returns The `map` method is returning a new tree object.
|
|
9120
9172
|
*/
|
|
9121
9173
|
map(callback, thisArg) {
|
|
@@ -9143,11 +9195,15 @@ var dataStructureTyped = (() => {
|
|
|
9143
9195
|
* Time Complexity: O(n)
|
|
9144
9196
|
* Space Complexity: O(n)
|
|
9145
9197
|
*
|
|
9146
|
-
* The `print` function
|
|
9147
|
-
* @param {
|
|
9148
|
-
*
|
|
9149
|
-
*
|
|
9150
|
-
*
|
|
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.
|
|
9151
9207
|
*/
|
|
9152
9208
|
print(beginRoot = this.root, options) {
|
|
9153
9209
|
const opts = __spreadValues({ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false }, options);
|
|
@@ -9158,10 +9214,10 @@ var dataStructureTyped = (() => {
|
|
|
9158
9214
|
console.log(`U for undefined
|
|
9159
9215
|
`);
|
|
9160
9216
|
if (opts.isShowNull)
|
|
9161
|
-
console.log(`
|
|
9217
|
+
console.log(`N for null
|
|
9162
9218
|
`);
|
|
9163
9219
|
if (opts.isShowRedBlackNIL)
|
|
9164
|
-
console.log(`S for Sentinel Node
|
|
9220
|
+
console.log(`S for Sentinel Node(NIL)
|
|
9165
9221
|
`);
|
|
9166
9222
|
const display = (root) => {
|
|
9167
9223
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
@@ -9172,13 +9228,18 @@ var dataStructureTyped = (() => {
|
|
|
9172
9228
|
display(beginRoot);
|
|
9173
9229
|
}
|
|
9174
9230
|
/**
|
|
9175
|
-
*
|
|
9176
|
-
*
|
|
9177
|
-
|
|
9178
|
-
|
|
9179
|
-
*
|
|
9180
|
-
*
|
|
9181
|
-
*
|
|
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]>.
|
|
9182
9243
|
*/
|
|
9183
9244
|
*_getIterator(node = this.root) {
|
|
9184
9245
|
if (!node)
|
|
@@ -9187,27 +9248,34 @@ var dataStructureTyped = (() => {
|
|
|
9187
9248
|
const stack = [];
|
|
9188
9249
|
let current = node;
|
|
9189
9250
|
while (current || stack.length > 0) {
|
|
9190
|
-
while (
|
|
9251
|
+
while (this.isRealNode(current)) {
|
|
9191
9252
|
stack.push(current);
|
|
9192
9253
|
current = current.left;
|
|
9193
9254
|
}
|
|
9194
9255
|
current = stack.pop();
|
|
9195
|
-
if (
|
|
9256
|
+
if (this.isRealNode(current)) {
|
|
9196
9257
|
yield [current.key, current.value];
|
|
9197
9258
|
current = current.right;
|
|
9198
9259
|
}
|
|
9199
9260
|
}
|
|
9200
9261
|
} else {
|
|
9201
|
-
if (node.left &&
|
|
9262
|
+
if (node.left && this.isRealNode(node)) {
|
|
9202
9263
|
yield* __yieldStar(this[Symbol.iterator](node.left));
|
|
9203
9264
|
}
|
|
9204
9265
|
yield [node.key, node.value];
|
|
9205
|
-
if (node.right &&
|
|
9266
|
+
if (node.right && this.isRealNode(node)) {
|
|
9206
9267
|
yield* __yieldStar(this[Symbol.iterator](node.right));
|
|
9207
9268
|
}
|
|
9208
9269
|
}
|
|
9209
9270
|
}
|
|
9210
9271
|
/**
|
|
9272
|
+
* Time Complexity: O(n)
|
|
9273
|
+
* Space Complexity: O(n)
|
|
9274
|
+
*/
|
|
9275
|
+
/**
|
|
9276
|
+
* Time Complexity: O(n)
|
|
9277
|
+
* Space Complexity: O(n)
|
|
9278
|
+
*
|
|
9211
9279
|
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
9212
9280
|
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
9213
9281
|
* @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
|
|
@@ -9228,10 +9296,10 @@ var dataStructureTyped = (() => {
|
|
|
9228
9296
|
return emptyDisplayLayout;
|
|
9229
9297
|
} else if (node === void 0 && !isShowUndefined) {
|
|
9230
9298
|
return emptyDisplayLayout;
|
|
9231
|
-
} else if (
|
|
9299
|
+
} else if (this.isNIL(node) && !isShowRedBlackNIL) {
|
|
9232
9300
|
return emptyDisplayLayout;
|
|
9233
9301
|
} else if (node !== null && node !== void 0) {
|
|
9234
|
-
const key = node.key, line =
|
|
9302
|
+
const key = node.key, line = this.isNIL(node) ? "S" : key.toString(), width = line.length;
|
|
9235
9303
|
return _buildNodeDisplay(
|
|
9236
9304
|
line,
|
|
9237
9305
|
width,
|
|
@@ -9262,10 +9330,21 @@ var dataStructureTyped = (() => {
|
|
|
9262
9330
|
}
|
|
9263
9331
|
}
|
|
9264
9332
|
/**
|
|
9265
|
-
*
|
|
9266
|
-
*
|
|
9267
|
-
|
|
9268
|
-
|
|
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.
|
|
9269
9348
|
*/
|
|
9270
9349
|
_swapProperties(srcNode, destNode) {
|
|
9271
9350
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -9284,12 +9363,20 @@ var dataStructureTyped = (() => {
|
|
|
9284
9363
|
return void 0;
|
|
9285
9364
|
}
|
|
9286
9365
|
/**
|
|
9287
|
-
*
|
|
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.
|
|
9288
9375
|
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
9289
9376
|
* tree.
|
|
9290
9377
|
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
9291
9378
|
* tree.
|
|
9292
|
-
* @returns
|
|
9379
|
+
* @returns the newNode.
|
|
9293
9380
|
*/
|
|
9294
9381
|
_replaceNode(oldNode, newNode) {
|
|
9295
9382
|
if (oldNode.parent) {
|
|
@@ -9308,10 +9395,17 @@ var dataStructureTyped = (() => {
|
|
|
9308
9395
|
return newNode;
|
|
9309
9396
|
}
|
|
9310
9397
|
/**
|
|
9311
|
-
*
|
|
9312
|
-
*
|
|
9313
|
-
|
|
9314
|
-
|
|
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`.
|
|
9315
9409
|
*/
|
|
9316
9410
|
_setRoot(v) {
|
|
9317
9411
|
if (v) {
|
|
@@ -9319,6 +9413,23 @@ var dataStructureTyped = (() => {
|
|
|
9319
9413
|
}
|
|
9320
9414
|
this._root = v;
|
|
9321
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
|
+
*/
|
|
9322
9433
|
_ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
9323
9434
|
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
9324
9435
|
callback = (node) => node;
|
|
@@ -9378,25 +9489,44 @@ var dataStructureTyped = (() => {
|
|
|
9378
9489
|
};
|
|
9379
9490
|
var BST = class _BST extends BinaryTree {
|
|
9380
9491
|
/**
|
|
9381
|
-
* This is the constructor function for a
|
|
9382
|
-
*
|
|
9383
|
-
*
|
|
9384
|
-
* to
|
|
9385
|
-
* @param [options] -
|
|
9386
|
-
*
|
|
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.
|
|
9387
9498
|
*/
|
|
9388
|
-
constructor(
|
|
9499
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
9389
9500
|
super([], options);
|
|
9390
9501
|
__publicField(this, "_root");
|
|
9391
|
-
|
|
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
|
+
}
|
|
9512
|
+
if (a > b)
|
|
9513
|
+
return 1;
|
|
9514
|
+
if (a < b)
|
|
9515
|
+
return -1;
|
|
9516
|
+
return 0;
|
|
9517
|
+
});
|
|
9518
|
+
/**
|
|
9519
|
+
* Time complexity: O(n)
|
|
9520
|
+
* Space complexity: O(n)
|
|
9521
|
+
*/
|
|
9522
|
+
__publicField(this, "_comparator", this._DEFAULT_COMPARATOR);
|
|
9392
9523
|
if (options) {
|
|
9393
|
-
const {
|
|
9394
|
-
if (
|
|
9395
|
-
this.
|
|
9524
|
+
const { comparator } = options;
|
|
9525
|
+
if (comparator)
|
|
9526
|
+
this._comparator = comparator;
|
|
9396
9527
|
}
|
|
9397
|
-
|
|
9398
|
-
|
|
9399
|
-
this.addMany(keysOrNodesOrEntries);
|
|
9528
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
9529
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
9400
9530
|
}
|
|
9401
9531
|
/**
|
|
9402
9532
|
* The function returns the root node of a tree structure.
|
|
@@ -9405,13 +9535,6 @@ var dataStructureTyped = (() => {
|
|
|
9405
9535
|
get root() {
|
|
9406
9536
|
return this._root;
|
|
9407
9537
|
}
|
|
9408
|
-
/**
|
|
9409
|
-
* The function returns the value of the _variant property.
|
|
9410
|
-
* @returns The value of the `_variant` property.
|
|
9411
|
-
*/
|
|
9412
|
-
get variant() {
|
|
9413
|
-
return this._variant;
|
|
9414
|
-
}
|
|
9415
9538
|
/**
|
|
9416
9539
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
9417
9540
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -9426,104 +9549,71 @@ var dataStructureTyped = (() => {
|
|
|
9426
9549
|
/**
|
|
9427
9550
|
* The function creates a new binary search tree with the specified options.
|
|
9428
9551
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
9429
|
-
* behavior of the `createTree` method. It
|
|
9430
|
-
*
|
|
9431
|
-
* @returns a new instance of the BST class
|
|
9432
|
-
* 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.
|
|
9433
9555
|
*/
|
|
9434
9556
|
createTree(options) {
|
|
9435
9557
|
return new _BST([], __spreadValues({
|
|
9436
9558
|
iterationType: this.iterationType,
|
|
9437
|
-
|
|
9559
|
+
comparator: this.comparator
|
|
9438
9560
|
}, options));
|
|
9439
9561
|
}
|
|
9440
9562
|
/**
|
|
9441
|
-
* The function
|
|
9442
|
-
*
|
|
9443
|
-
*
|
|
9444
|
-
*
|
|
9445
|
-
* `
|
|
9446
|
-
*
|
|
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.
|
|
9447
9570
|
*/
|
|
9448
|
-
|
|
9449
|
-
|
|
9450
|
-
|
|
9451
|
-
return;
|
|
9452
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
9453
|
-
node = keyOrNodeOrEntry;
|
|
9454
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9455
|
-
const [key, value2] = keyOrNodeOrEntry;
|
|
9456
|
-
if (key === void 0 || key === null) {
|
|
9457
|
-
return;
|
|
9458
|
-
} else {
|
|
9459
|
-
node = this.createNode(key, value2);
|
|
9460
|
-
}
|
|
9461
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
9462
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
9463
|
-
} else {
|
|
9464
|
-
return;
|
|
9465
|
-
}
|
|
9466
|
-
return node;
|
|
9571
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
9572
|
+
var _a;
|
|
9573
|
+
return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value)) != null ? _a : void 0;
|
|
9467
9574
|
}
|
|
9468
|
-
/**
|
|
9469
|
-
* Time Complexity: O(log n)
|
|
9470
|
-
* Space Complexity: O(log n)
|
|
9471
|
-
*/
|
|
9472
9575
|
/**
|
|
9473
9576
|
* Time Complexity: O(log n)
|
|
9474
9577
|
* Space Complexity: O(log n)
|
|
9475
9578
|
*
|
|
9476
|
-
* The function
|
|
9477
|
-
*
|
|
9478
|
-
* @param {
|
|
9479
|
-
* `
|
|
9480
|
-
*
|
|
9481
|
-
*
|
|
9482
|
-
*
|
|
9483
|
-
|
|
9484
|
-
|
|
9485
|
-
|
|
9486
|
-
|
|
9487
|
-
|
|
9488
|
-
|
|
9489
|
-
|
|
9490
|
-
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9491
|
-
const key2 = keyOrNodeOrEntry[0];
|
|
9492
|
-
if (key2 === null || key2 === void 0)
|
|
9493
|
-
return;
|
|
9494
|
-
return this.getNodeByKey(key2, iterationType);
|
|
9495
|
-
}
|
|
9496
|
-
const key = keyOrNodeOrEntry;
|
|
9497
|
-
if (key === null || key === void 0)
|
|
9498
|
-
return;
|
|
9499
|
-
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;
|
|
9500
9593
|
}
|
|
9501
9594
|
/**
|
|
9502
|
-
* The function checks if
|
|
9503
|
-
* @param
|
|
9504
|
-
*
|
|
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.
|
|
9505
9600
|
*/
|
|
9506
|
-
isNode(
|
|
9507
|
-
return
|
|
9601
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
9602
|
+
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
9508
9603
|
}
|
|
9509
|
-
/**
|
|
9510
|
-
* Time Complexity: O(log n)
|
|
9511
|
-
* Space Complexity: O(1)
|
|
9512
|
-
*/
|
|
9513
9604
|
/**
|
|
9514
9605
|
* Time Complexity: O(log n)
|
|
9515
9606
|
* Space Complexity: O(1)
|
|
9516
9607
|
*
|
|
9517
|
-
* The `add` function adds a new node to a binary tree
|
|
9518
|
-
*
|
|
9519
|
-
*
|
|
9520
|
-
* @param {V} [value] - The `value` parameter
|
|
9521
|
-
*
|
|
9522
|
-
* @returns
|
|
9523
|
-
* node was not added.
|
|
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.
|
|
9524
9614
|
*/
|
|
9525
|
-
add(
|
|
9526
|
-
const newNode = this.
|
|
9615
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
9616
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
9527
9617
|
if (newNode === void 0)
|
|
9528
9618
|
return false;
|
|
9529
9619
|
if (this.root === void 0) {
|
|
@@ -9533,10 +9623,10 @@ var dataStructureTyped = (() => {
|
|
|
9533
9623
|
}
|
|
9534
9624
|
let current = this.root;
|
|
9535
9625
|
while (current !== void 0) {
|
|
9536
|
-
if (this.
|
|
9626
|
+
if (this.comparator(current.key, newNode.key) === 0) {
|
|
9537
9627
|
this._replaceNode(current, newNode);
|
|
9538
9628
|
return true;
|
|
9539
|
-
} else if (this.
|
|
9629
|
+
} else if (this.comparator(current.key, newNode.key) > 0) {
|
|
9540
9630
|
if (current.left === void 0) {
|
|
9541
9631
|
current.left = newNode;
|
|
9542
9632
|
this._size++;
|
|
@@ -9555,37 +9645,38 @@ var dataStructureTyped = (() => {
|
|
|
9555
9645
|
return false;
|
|
9556
9646
|
}
|
|
9557
9647
|
/**
|
|
9558
|
-
* Time Complexity: O(
|
|
9559
|
-
* Space Complexity: O(
|
|
9648
|
+
* Time Complexity: O(log n)
|
|
9649
|
+
* Space Complexity: O(log n)
|
|
9560
9650
|
*/
|
|
9561
9651
|
/**
|
|
9562
9652
|
* Time Complexity: O(k log n)
|
|
9563
9653
|
* Space Complexity: O(k + log n)
|
|
9564
9654
|
*
|
|
9565
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a
|
|
9566
|
-
*
|
|
9567
|
-
* @param
|
|
9568
|
-
* the
|
|
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.
|
|
9569
9659
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
9570
9660
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
9571
9661
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
9572
|
-
* @param [isBalanceAdd=true] - A boolean flag indicating whether the
|
|
9573
|
-
*
|
|
9574
|
-
* algorithm. If set to false, the
|
|
9575
|
-
*
|
|
9576
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
9577
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
9578
|
-
*
|
|
9579
|
-
* @returns The function `addMany` returns an array of
|
|
9580
|
-
|
|
9581
|
-
|
|
9662
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
9663
|
+
* adding the elements. If set to true, the tree will be balanced using a binary search tree
|
|
9664
|
+
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
9665
|
+
* value is true.
|
|
9666
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
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.
|
|
9671
|
+
*/
|
|
9672
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
9582
9673
|
const inserted = [];
|
|
9583
9674
|
let valuesIterator;
|
|
9584
9675
|
if (values) {
|
|
9585
9676
|
valuesIterator = values[Symbol.iterator]();
|
|
9586
9677
|
}
|
|
9587
9678
|
if (!isBalanceAdd) {
|
|
9588
|
-
for (const kve of
|
|
9679
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
9589
9680
|
const value = valuesIterator == null ? void 0 : valuesIterator.next().value;
|
|
9590
9681
|
const nn = this.add(kve, value);
|
|
9591
9682
|
inserted.push(nn);
|
|
@@ -9598,25 +9689,34 @@ var dataStructureTyped = (() => {
|
|
|
9598
9689
|
return false;
|
|
9599
9690
|
return !(this.isEntry(kve) && (kve[0] === void 0 || kve[0] === null));
|
|
9600
9691
|
};
|
|
9601
|
-
for (const kve of
|
|
9692
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
9602
9693
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
9603
9694
|
}
|
|
9604
9695
|
let sorted = [];
|
|
9605
9696
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
9606
|
-
let
|
|
9697
|
+
let keyA, keyB;
|
|
9607
9698
|
if (this.isEntry(a))
|
|
9608
|
-
|
|
9699
|
+
keyA = a[0];
|
|
9609
9700
|
else if (this.isRealNode(a))
|
|
9610
|
-
|
|
9611
|
-
else
|
|
9612
|
-
|
|
9701
|
+
keyA = a.key;
|
|
9702
|
+
else if (this.toEntryFn) {
|
|
9703
|
+
keyA = this.toEntryFn(a)[0];
|
|
9704
|
+
} else {
|
|
9705
|
+
keyA = a;
|
|
9706
|
+
}
|
|
9613
9707
|
if (this.isEntry(b))
|
|
9614
|
-
|
|
9708
|
+
keyB = b[0];
|
|
9615
9709
|
else if (this.isRealNode(b))
|
|
9616
|
-
|
|
9617
|
-
else
|
|
9618
|
-
|
|
9619
|
-
|
|
9710
|
+
keyB = b.key;
|
|
9711
|
+
else if (this.toEntryFn) {
|
|
9712
|
+
keyB = this.toEntryFn(b)[0];
|
|
9713
|
+
} else {
|
|
9714
|
+
keyB = b;
|
|
9715
|
+
}
|
|
9716
|
+
if (keyA !== void 0 && keyA !== null && keyB !== void 0 && keyB !== null) {
|
|
9717
|
+
return this.comparator(keyA, keyB);
|
|
9718
|
+
}
|
|
9719
|
+
return 0;
|
|
9620
9720
|
});
|
|
9621
9721
|
const _dfs = (arr) => {
|
|
9622
9722
|
if (arr.length === 0)
|
|
@@ -9652,33 +9752,27 @@ var dataStructureTyped = (() => {
|
|
|
9652
9752
|
return inserted;
|
|
9653
9753
|
}
|
|
9654
9754
|
/**
|
|
9655
|
-
|
|
9656
|
-
|
|
9657
|
-
|
|
9658
|
-
|
|
9659
|
-
|
|
9660
|
-
|
|
9661
|
-
|
|
9662
|
-
|
|
9663
|
-
|
|
9664
|
-
|
|
9665
|
-
|
|
9666
|
-
|
|
9667
|
-
|
|
9668
|
-
|
|
9669
|
-
|
|
9670
|
-
|
|
9671
|
-
|
|
9672
|
-
|
|
9673
|
-
|
|
9674
|
-
|
|
9675
|
-
|
|
9676
|
-
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
9677
|
-
* traversal will start from the root of the tree.
|
|
9678
|
-
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
9679
|
-
* performed on the binary tree. It can have two possible values:
|
|
9680
|
-
* @returns The method returns an array of nodes (`NODE[]`).
|
|
9681
|
-
*/
|
|
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
|
+
*/
|
|
9682
9776
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9683
9777
|
beginRoot = this.ensureNode(beginRoot);
|
|
9684
9778
|
if (!beginRoot)
|
|
@@ -9696,9 +9790,9 @@ var dataStructureTyped = (() => {
|
|
|
9696
9790
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
9697
9791
|
return;
|
|
9698
9792
|
if (callback === this._DEFAULT_CALLBACK) {
|
|
9699
|
-
if (this.isRealNode(cur.left) && this.
|
|
9793
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
9700
9794
|
dfs(cur.left);
|
|
9701
|
-
if (this.isRealNode(cur.right) && this.
|
|
9795
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
9702
9796
|
dfs(cur.right);
|
|
9703
9797
|
} else {
|
|
9704
9798
|
this.isRealNode(cur.left) && dfs(cur.left);
|
|
@@ -9717,9 +9811,9 @@ var dataStructureTyped = (() => {
|
|
|
9717
9811
|
return ans;
|
|
9718
9812
|
}
|
|
9719
9813
|
if (callback === this._DEFAULT_CALLBACK) {
|
|
9720
|
-
if (this.isRealNode(cur.right) && this.
|
|
9814
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
9721
9815
|
stack.push(cur.right);
|
|
9722
|
-
if (this.isRealNode(cur.left) && this.
|
|
9816
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
9723
9817
|
stack.push(cur.left);
|
|
9724
9818
|
} else {
|
|
9725
9819
|
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
@@ -9737,51 +9831,50 @@ var dataStructureTyped = (() => {
|
|
|
9737
9831
|
* Time Complexity: O(log n)
|
|
9738
9832
|
* Space Complexity: O(1)
|
|
9739
9833
|
*
|
|
9740
|
-
* The `getNode`
|
|
9741
|
-
*
|
|
9742
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value
|
|
9743
|
-
*
|
|
9744
|
-
*
|
|
9745
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
9746
|
-
* the
|
|
9747
|
-
*
|
|
9748
|
-
*
|
|
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
|
|
9749
9843
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
9750
|
-
* search tree. It can be either a key or a node. If it is a key,
|
|
9751
|
-
*
|
|
9752
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
9753
|
-
* be performed when searching for nodes in the binary search tree. It
|
|
9754
|
-
*
|
|
9755
|
-
* @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.
|
|
9756
9850
|
*/
|
|
9757
9851
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9758
9852
|
var _a;
|
|
9759
9853
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
|
|
9760
9854
|
}
|
|
9761
9855
|
/**
|
|
9762
|
-
* Time Complexity: O(log n)
|
|
9763
|
-
* Space Complexity: O(
|
|
9856
|
+
* Time Complexity: O(k log n)
|
|
9857
|
+
* Space Complexity: O(k + log n)
|
|
9764
9858
|
*/
|
|
9765
9859
|
/**
|
|
9766
9860
|
* Time Complexity: O(log n)
|
|
9767
9861
|
* Space Complexity: O(1)
|
|
9768
9862
|
*
|
|
9769
|
-
* The function `getNodeByKey`
|
|
9770
|
-
*
|
|
9771
|
-
*
|
|
9772
|
-
*
|
|
9773
|
-
* @param iterationType - The `iterationType` parameter is an optional
|
|
9774
|
-
* type of iteration to
|
|
9775
|
-
*
|
|
9776
|
-
* @returns The
|
|
9777
|
-
* 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.
|
|
9778
9871
|
*/
|
|
9779
9872
|
getNodeByKey(key, iterationType = "ITERATIVE") {
|
|
9780
9873
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
9781
9874
|
}
|
|
9782
9875
|
/**
|
|
9783
|
-
* Time
|
|
9784
|
-
* Space
|
|
9876
|
+
* Time Complexity: O(log n)
|
|
9877
|
+
* Space Complexity: O(k + log n)
|
|
9785
9878
|
*/
|
|
9786
9879
|
/**
|
|
9787
9880
|
* Time complexity: O(n)
|
|
@@ -9790,15 +9883,16 @@ var dataStructureTyped = (() => {
|
|
|
9790
9883
|
* The function overrides the depth-first search method and returns an array of the return types of
|
|
9791
9884
|
* the callback function.
|
|
9792
9885
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9793
|
-
* during the depth-first search traversal. It is an optional parameter and
|
|
9794
|
-
*
|
|
9795
|
-
* @param {DFSOrderPattern} [pattern=
|
|
9796
|
-
*
|
|
9797
|
-
*
|
|
9798
|
-
*
|
|
9799
|
-
*
|
|
9800
|
-
*
|
|
9801
|
-
*
|
|
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
|
|
9802
9896
|
* following values:
|
|
9803
9897
|
* @returns The method is returning an array of the return type of the callback function.
|
|
9804
9898
|
*/
|
|
@@ -9806,8 +9900,8 @@ var dataStructureTyped = (() => {
|
|
|
9806
9900
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
9807
9901
|
}
|
|
9808
9902
|
/**
|
|
9809
|
-
* Time
|
|
9810
|
-
* Space
|
|
9903
|
+
* Time Complexity: O(log n)
|
|
9904
|
+
* Space Complexity: O(1)
|
|
9811
9905
|
*/
|
|
9812
9906
|
/**
|
|
9813
9907
|
* Time complexity: O(n)
|
|
@@ -9816,38 +9910,38 @@ var dataStructureTyped = (() => {
|
|
|
9816
9910
|
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
9817
9911
|
* the callback function.
|
|
9818
9912
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9819
|
-
* visited during the breadth-first search
|
|
9820
|
-
*
|
|
9821
|
-
* @param beginRoot - The `beginRoot` parameter is the starting
|
|
9822
|
-
*
|
|
9823
|
-
* the
|
|
9824
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
9825
|
-
* be performed during the breadth-first search (BFS) traversal. It
|
|
9826
|
-
*
|
|
9827
|
-
* @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.
|
|
9828
9922
|
*/
|
|
9829
9923
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9830
9924
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
9831
9925
|
}
|
|
9832
9926
|
/**
|
|
9833
|
-
* Time
|
|
9834
|
-
* Space
|
|
9927
|
+
* Time Complexity: O(log n)
|
|
9928
|
+
* Space Complexity: O(1)
|
|
9835
9929
|
*/
|
|
9836
9930
|
/**
|
|
9837
9931
|
* Time complexity: O(n)
|
|
9838
9932
|
* Space complexity: O(n)
|
|
9839
9933
|
*
|
|
9840
|
-
* The function overrides the listLevels method and returns an array of arrays
|
|
9841
|
-
*
|
|
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.
|
|
9842
9936
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
9843
|
-
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
9844
|
-
* during the
|
|
9845
|
-
* @param beginRoot - The `beginRoot` parameter is
|
|
9846
|
-
* levels of
|
|
9847
|
-
*
|
|
9848
|
-
*
|
|
9849
|
-
*
|
|
9850
|
-
* 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:
|
|
9851
9945
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
9852
9946
|
* function.
|
|
9853
9947
|
*/
|
|
@@ -9855,74 +9949,42 @@ var dataStructureTyped = (() => {
|
|
|
9855
9949
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
9856
9950
|
}
|
|
9857
9951
|
/**
|
|
9858
|
-
* Time
|
|
9859
|
-
* Space
|
|
9860
|
-
*/
|
|
9861
|
-
/**
|
|
9862
|
-
* Time Complexity: O(log n)
|
|
9863
|
-
* Space Complexity: O(1)
|
|
9864
|
-
*
|
|
9865
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
9866
|
-
* leftmost node if the comparison result is greater than.
|
|
9867
|
-
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
9868
|
-
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
9869
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
9870
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
9871
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
9872
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
9873
|
-
*/
|
|
9874
|
-
lastKey(beginRoot = this.root) {
|
|
9875
|
-
let current = this.ensureNode(beginRoot);
|
|
9876
|
-
if (!current)
|
|
9877
|
-
return void 0;
|
|
9878
|
-
if (this._variant === "STANDARD") {
|
|
9879
|
-
while (current.right !== void 0) {
|
|
9880
|
-
current = current.right;
|
|
9881
|
-
}
|
|
9882
|
-
} else {
|
|
9883
|
-
while (current.left !== void 0) {
|
|
9884
|
-
current = current.left;
|
|
9885
|
-
}
|
|
9886
|
-
}
|
|
9887
|
-
return current.key;
|
|
9888
|
-
}
|
|
9889
|
-
/**
|
|
9890
|
-
* Time Complexity: O(log n)
|
|
9891
|
-
* Space Complexity: O(log n)
|
|
9952
|
+
* Time complexity: O(n)
|
|
9953
|
+
* Space complexity: O(n)
|
|
9892
9954
|
*/
|
|
9893
9955
|
/**
|
|
9894
|
-
* Time
|
|
9895
|
-
* Space
|
|
9956
|
+
* Time complexity: O(n)
|
|
9957
|
+
* Space complexity: O(n)
|
|
9896
9958
|
*
|
|
9897
|
-
* The `lesserOrGreaterTraverse` function traverses a binary tree and
|
|
9898
|
-
*
|
|
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.
|
|
9899
9961
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
9900
|
-
* that
|
|
9901
|
-
*
|
|
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.
|
|
9902
9964
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
9903
|
-
* traverse nodes that are lesser
|
|
9904
|
-
*
|
|
9905
|
-
* `
|
|
9906
|
-
*
|
|
9907
|
-
*
|
|
9908
|
-
*
|
|
9909
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
9910
|
-
* 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:
|
|
9911
9973
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
9912
9974
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
9913
9975
|
*/
|
|
9914
|
-
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater =
|
|
9915
|
-
|
|
9976
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = -1, targetNode = this.root, iterationType = this.iterationType) {
|
|
9977
|
+
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
9916
9978
|
const ans = [];
|
|
9917
|
-
if (!
|
|
9979
|
+
if (!targetNodeEnsured)
|
|
9918
9980
|
return ans;
|
|
9919
9981
|
if (!this.root)
|
|
9920
9982
|
return ans;
|
|
9921
|
-
const targetKey =
|
|
9983
|
+
const targetKey = targetNodeEnsured.key;
|
|
9922
9984
|
if (iterationType === "RECURSIVE") {
|
|
9923
9985
|
const dfs = (cur) => {
|
|
9924
|
-
const compared = this.
|
|
9925
|
-
if (compared === lesserOrGreater)
|
|
9986
|
+
const compared = this.comparator(cur.key, targetKey);
|
|
9987
|
+
if (Math.sign(compared) === lesserOrGreater)
|
|
9926
9988
|
ans.push(callback(cur));
|
|
9927
9989
|
if (this.isRealNode(cur.left))
|
|
9928
9990
|
dfs(cur.left);
|
|
@@ -9936,8 +9998,8 @@ var dataStructureTyped = (() => {
|
|
|
9936
9998
|
while (queue.size > 0) {
|
|
9937
9999
|
const cur = queue.shift();
|
|
9938
10000
|
if (this.isRealNode(cur)) {
|
|
9939
|
-
const compared = this.
|
|
9940
|
-
if (compared === lesserOrGreater)
|
|
10001
|
+
const compared = this.comparator(cur.key, targetKey);
|
|
10002
|
+
if (Math.sign(compared) === lesserOrGreater)
|
|
9941
10003
|
ans.push(callback(cur));
|
|
9942
10004
|
if (this.isRealNode(cur.left))
|
|
9943
10005
|
queue.push(cur.left);
|
|
@@ -9949,18 +10011,19 @@ var dataStructureTyped = (() => {
|
|
|
9949
10011
|
}
|
|
9950
10012
|
}
|
|
9951
10013
|
/**
|
|
9952
|
-
* Time
|
|
9953
|
-
* Space
|
|
10014
|
+
* Time complexity: O(n)
|
|
10015
|
+
* Space complexity: O(n)
|
|
9954
10016
|
*/
|
|
9955
10017
|
/**
|
|
9956
|
-
* Time
|
|
9957
|
-
* Space
|
|
10018
|
+
* Time complexity: O(n)
|
|
10019
|
+
* Space complexity: O(n)
|
|
9958
10020
|
*
|
|
9959
|
-
* The `perfectlyBalance` function
|
|
9960
|
-
*
|
|
9961
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
9962
|
-
* type of iteration to use when building a balanced binary search tree. It
|
|
9963
|
-
*
|
|
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.
|
|
9964
10027
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
9965
10028
|
*/
|
|
9966
10029
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
@@ -9999,25 +10062,19 @@ var dataStructureTyped = (() => {
|
|
|
9999
10062
|
}
|
|
10000
10063
|
}
|
|
10001
10064
|
/**
|
|
10002
|
-
*
|
|
10003
|
-
*
|
|
10004
|
-
* 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.
|
|
10005
|
-
*
|
|
10006
|
-
* Use Cases and Efficiency:
|
|
10007
|
-
* 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.
|
|
10008
|
-
* 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).
|
|
10009
|
-
*/
|
|
10010
|
-
/**
|
|
10011
|
-
* Time Complexity: O(n)
|
|
10012
|
-
* Space Complexity: O(log n)
|
|
10065
|
+
* Time complexity: O(n)
|
|
10066
|
+
* Space complexity: O(n)
|
|
10013
10067
|
*/
|
|
10014
10068
|
/**
|
|
10015
10069
|
* Time Complexity: O(n)
|
|
10016
10070
|
* Space Complexity: O(log n)
|
|
10017
10071
|
*
|
|
10018
|
-
* The function checks if a binary tree is AVL balanced using either recursive or
|
|
10019
|
-
*
|
|
10020
|
-
*
|
|
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.
|
|
10021
10078
|
* @returns a boolean value.
|
|
10022
10079
|
*/
|
|
10023
10080
|
isAVLBalanced(iterationType = this.iterationType) {
|
|
@@ -10064,60 +10121,26 @@ var dataStructureTyped = (() => {
|
|
|
10064
10121
|
return balanced;
|
|
10065
10122
|
}
|
|
10066
10123
|
/**
|
|
10067
|
-
*
|
|
10068
|
-
*
|
|
10069
|
-
* can either be an object of type `NODE` or it can be `undefined`.
|
|
10124
|
+
* Time Complexity: O(n)
|
|
10125
|
+
* Space Complexity: O(log n)
|
|
10070
10126
|
*/
|
|
10071
|
-
_setRoot(v) {
|
|
10072
|
-
if (v) {
|
|
10073
|
-
v.parent = void 0;
|
|
10074
|
-
}
|
|
10075
|
-
this._root = v;
|
|
10076
|
-
}
|
|
10077
10127
|
/**
|
|
10078
|
-
* The function
|
|
10079
|
-
*
|
|
10080
|
-
* @param {K} a - The parameter "a" is of type K.
|
|
10081
|
-
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
10082
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
10083
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
10084
|
-
*/
|
|
10085
|
-
_compare(a, b) {
|
|
10086
|
-
const extractedA = this.extractor(a);
|
|
10087
|
-
const extractedB = this.extractor(b);
|
|
10088
|
-
const compared = this.variant === "STANDARD" ? extractedA - extractedB : extractedB - extractedA;
|
|
10089
|
-
if (compared > 0)
|
|
10090
|
-
return "GT";
|
|
10091
|
-
if (compared < 0)
|
|
10092
|
-
return "LT";
|
|
10093
|
-
return "EQ";
|
|
10094
|
-
}
|
|
10095
|
-
/**
|
|
10096
|
-
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
10097
|
-
* `a` is less than `b` based on the specified variant.
|
|
10098
|
-
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
10099
|
-
* first value to be compared in the function.
|
|
10100
|
-
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
10101
|
-
* of the arguments for the comparison in the `_lt` function.
|
|
10102
|
-
* @returns a boolean value.
|
|
10128
|
+
* The function returns the value of the _comparator property.
|
|
10129
|
+
* @returns The `_comparator` property is being returned.
|
|
10103
10130
|
*/
|
|
10104
|
-
|
|
10105
|
-
|
|
10106
|
-
const extractedB = this.extractor(b);
|
|
10107
|
-
return this.variant === "STANDARD" ? extractedA < extractedB : extractedA > extractedB;
|
|
10131
|
+
get comparator() {
|
|
10132
|
+
return this._comparator;
|
|
10108
10133
|
}
|
|
10109
10134
|
/**
|
|
10110
|
-
* The function
|
|
10111
|
-
*
|
|
10112
|
-
* @param {
|
|
10113
|
-
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
10114
|
-
* of the arguments for the comparison in the function.
|
|
10115
|
-
* @returns a boolean value.
|
|
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.
|
|
10116
10138
|
*/
|
|
10117
|
-
|
|
10118
|
-
|
|
10119
|
-
|
|
10120
|
-
|
|
10139
|
+
_setRoot(v) {
|
|
10140
|
+
if (v) {
|
|
10141
|
+
v.parent = void 0;
|
|
10142
|
+
}
|
|
10143
|
+
this._root = v;
|
|
10121
10144
|
}
|
|
10122
10145
|
};
|
|
10123
10146
|
|
|
@@ -10719,27 +10742,29 @@ var dataStructureTyped = (() => {
|
|
|
10719
10742
|
};
|
|
10720
10743
|
var AVLTree = class _AVLTree extends BST {
|
|
10721
10744
|
/**
|
|
10722
|
-
*
|
|
10723
|
-
*
|
|
10724
|
-
*
|
|
10725
|
-
*
|
|
10726
|
-
*
|
|
10727
|
-
*
|
|
10728
|
-
*
|
|
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` (
|
|
10729
10754
|
*/
|
|
10730
|
-
constructor(
|
|
10755
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
10731
10756
|
super([], options);
|
|
10732
|
-
if (
|
|
10733
|
-
super.addMany(
|
|
10757
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
10758
|
+
super.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
10734
10759
|
}
|
|
10735
10760
|
/**
|
|
10736
|
-
* The function creates a new AVL tree node with the
|
|
10737
|
-
* @param {K} key - The key parameter is the key
|
|
10738
|
-
*
|
|
10739
|
-
* @param [value] - The parameter
|
|
10740
|
-
*
|
|
10741
|
-
*
|
|
10742
|
-
*
|
|
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.
|
|
10743
10768
|
*/
|
|
10744
10769
|
createNode(key, value) {
|
|
10745
10770
|
return new AVLTreeNode(key, value);
|
|
@@ -10754,16 +10779,18 @@ var dataStructureTyped = (() => {
|
|
|
10754
10779
|
createTree(options) {
|
|
10755
10780
|
return new _AVLTree([], __spreadValues({
|
|
10756
10781
|
iterationType: this.iterationType,
|
|
10757
|
-
|
|
10782
|
+
comparator: this.comparator
|
|
10758
10783
|
}, options));
|
|
10759
10784
|
}
|
|
10760
10785
|
/**
|
|
10761
|
-
* The function checks if
|
|
10762
|
-
* @param
|
|
10763
|
-
*
|
|
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.
|
|
10764
10791
|
*/
|
|
10765
|
-
isNode(
|
|
10766
|
-
return
|
|
10792
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
10793
|
+
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
|
|
10767
10794
|
}
|
|
10768
10795
|
/**
|
|
10769
10796
|
* Time Complexity: O(log n)
|
|
@@ -10774,20 +10801,21 @@ var dataStructureTyped = (() => {
|
|
|
10774
10801
|
* Time Complexity: O(log n)
|
|
10775
10802
|
* Space Complexity: O(1)
|
|
10776
10803
|
*
|
|
10777
|
-
* The function overrides the add method of a
|
|
10778
|
-
*
|
|
10779
|
-
* @param
|
|
10780
|
-
*
|
|
10781
|
-
*
|
|
10782
|
-
*
|
|
10783
|
-
*
|
|
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.
|
|
10784
10812
|
*/
|
|
10785
|
-
add(
|
|
10786
|
-
if (
|
|
10813
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
10814
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
10787
10815
|
return false;
|
|
10788
|
-
const inserted = super.add(
|
|
10816
|
+
const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
|
|
10789
10817
|
if (inserted)
|
|
10790
|
-
this._balancePath(
|
|
10818
|
+
this._balancePath(keyOrNodeOrEntryOrRawElement);
|
|
10791
10819
|
return inserted;
|
|
10792
10820
|
}
|
|
10793
10821
|
/**
|
|
@@ -10798,16 +10826,14 @@ var dataStructureTyped = (() => {
|
|
|
10798
10826
|
* Time Complexity: O(log n)
|
|
10799
10827
|
* Space Complexity: O(1)
|
|
10800
10828
|
*
|
|
10801
|
-
* The function overrides the delete method of a binary tree
|
|
10802
|
-
*
|
|
10829
|
+
* The function overrides the delete method of a binary tree class and performs additional operations
|
|
10830
|
+
* to balance the tree after deletion.
|
|
10803
10831
|
* @param identifier - The `identifier` parameter is the value or condition used to identify the
|
|
10804
|
-
* node(s) to be deleted from the binary tree. It can be of any type
|
|
10805
|
-
*
|
|
10806
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
10807
|
-
*
|
|
10808
|
-
*
|
|
10809
|
-
* parameter of type `NODE
|
|
10810
|
-
* @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.
|
|
10811
10837
|
*/
|
|
10812
10838
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
10813
10839
|
const deletedResults = super.delete(identifier, callback);
|
|
@@ -10819,31 +10845,38 @@ var dataStructureTyped = (() => {
|
|
|
10819
10845
|
return deletedResults;
|
|
10820
10846
|
}
|
|
10821
10847
|
/**
|
|
10822
|
-
*
|
|
10823
|
-
*
|
|
10824
|
-
|
|
10825
|
-
|
|
10826
|
-
*
|
|
10827
|
-
*
|
|
10828
|
-
*
|
|
10829
|
-
*
|
|
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`.
|
|
10830
10863
|
*/
|
|
10831
10864
|
_swapProperties(srcNode, destNode) {
|
|
10832
|
-
|
|
10833
|
-
|
|
10834
|
-
if (
|
|
10835
|
-
const { key, value, height } =
|
|
10865
|
+
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
10866
|
+
const destNodeEnsured = this.ensureNode(destNode);
|
|
10867
|
+
if (srcNodeEnsured && destNodeEnsured) {
|
|
10868
|
+
const { key, value, height } = destNodeEnsured;
|
|
10836
10869
|
const tempNode = this.createNode(key, value);
|
|
10837
10870
|
if (tempNode) {
|
|
10838
10871
|
tempNode.height = height;
|
|
10839
|
-
|
|
10840
|
-
|
|
10841
|
-
|
|
10842
|
-
|
|
10843
|
-
|
|
10844
|
-
|
|
10872
|
+
destNodeEnsured.key = srcNodeEnsured.key;
|
|
10873
|
+
destNodeEnsured.value = srcNodeEnsured.value;
|
|
10874
|
+
destNodeEnsured.height = srcNodeEnsured.height;
|
|
10875
|
+
srcNodeEnsured.key = tempNode.key;
|
|
10876
|
+
srcNodeEnsured.value = tempNode.value;
|
|
10877
|
+
srcNodeEnsured.height = tempNode.height;
|
|
10845
10878
|
}
|
|
10846
|
-
return
|
|
10879
|
+
return destNodeEnsured;
|
|
10847
10880
|
}
|
|
10848
10881
|
return void 0;
|
|
10849
10882
|
}
|
|
@@ -10856,7 +10889,8 @@ var dataStructureTyped = (() => {
|
|
|
10856
10889
|
* Space Complexity: O(1)
|
|
10857
10890
|
*
|
|
10858
10891
|
* The function calculates the balance factor of a node in a binary tree.
|
|
10859
|
-
* @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.
|
|
10860
10894
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
10861
10895
|
* height of the left subtree from the height of the right subtree.
|
|
10862
10896
|
*/
|
|
@@ -10899,7 +10933,7 @@ var dataStructureTyped = (() => {
|
|
|
10899
10933
|
* Time Complexity: O(1)
|
|
10900
10934
|
* Space Complexity: O(1)
|
|
10901
10935
|
*
|
|
10902
|
-
* The
|
|
10936
|
+
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
10903
10937
|
* @param {NODE} A - A is a node in a binary tree.
|
|
10904
10938
|
*/
|
|
10905
10939
|
_balanceLL(A) {
|
|
@@ -11089,8 +11123,8 @@ var dataStructureTyped = (() => {
|
|
|
11089
11123
|
*
|
|
11090
11124
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
11091
11125
|
* to restore balance in an AVL tree after inserting a node.
|
|
11092
|
-
* @param {NODE} node - The `node` parameter
|
|
11093
|
-
*
|
|
11126
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
|
|
11127
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
11094
11128
|
*/
|
|
11095
11129
|
_balancePath(node) {
|
|
11096
11130
|
node = this.ensureNode(node);
|
|
@@ -11120,13 +11154,21 @@ var dataStructureTyped = (() => {
|
|
|
11120
11154
|
}
|
|
11121
11155
|
}
|
|
11122
11156
|
/**
|
|
11123
|
-
*
|
|
11124
|
-
*
|
|
11125
|
-
|
|
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.
|
|
11126
11168
|
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
11127
11169
|
* the data structure.
|
|
11128
|
-
* @returns the result of calling the `_replaceNode` method
|
|
11129
|
-
* `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.
|
|
11130
11172
|
*/
|
|
11131
11173
|
_replaceNode(oldNode, newNode) {
|
|
11132
11174
|
newNode.height = oldNode.height;
|
|
@@ -11170,20 +11212,20 @@ var dataStructureTyped = (() => {
|
|
|
11170
11212
|
var RedBlackTree = class _RedBlackTree extends BST {
|
|
11171
11213
|
/**
|
|
11172
11214
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
11173
|
-
* @param
|
|
11174
|
-
* contain keys, nodes, or
|
|
11175
|
-
*
|
|
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.
|
|
11176
11218
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
11177
|
-
* constructor. It
|
|
11178
|
-
*
|
|
11179
|
-
*
|
|
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
|
|
11180
11222
|
*/
|
|
11181
|
-
constructor(
|
|
11223
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
11182
11224
|
super([], options);
|
|
11183
11225
|
__publicField(this, "_root");
|
|
11184
11226
|
this._root = this.NIL;
|
|
11185
|
-
if (
|
|
11186
|
-
this.addMany(
|
|
11227
|
+
if (keysOrNodesOrEntriesOrRawElements) {
|
|
11228
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
11187
11229
|
}
|
|
11188
11230
|
}
|
|
11189
11231
|
/**
|
|
@@ -11195,24 +11237,25 @@ var dataStructureTyped = (() => {
|
|
|
11195
11237
|
}
|
|
11196
11238
|
/**
|
|
11197
11239
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
11198
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of
|
|
11199
|
-
* 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.
|
|
11200
11243
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
11201
|
-
* associated with the key in the node. It is not required and can be omitted if
|
|
11202
|
-
*
|
|
11203
|
-
*
|
|
11204
|
-
* can
|
|
11205
|
-
*
|
|
11206
|
-
* 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.
|
|
11207
11251
|
*/
|
|
11208
11252
|
createNode(key, value, color = "BLACK") {
|
|
11209
11253
|
return new RedBlackTreeNode(key, value, color);
|
|
11210
11254
|
}
|
|
11211
11255
|
/**
|
|
11212
|
-
* The function creates a Red-Black Tree with the
|
|
11213
|
-
* @param [options] - The `options` parameter is an optional object that contains
|
|
11214
|
-
* options for creating the Red-Black Tree. It
|
|
11215
|
-
* 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:
|
|
11216
11259
|
* @returns a new instance of a RedBlackTree object.
|
|
11217
11260
|
*/
|
|
11218
11261
|
createTree(options) {
|
|
@@ -11228,48 +11271,50 @@ var dataStructureTyped = (() => {
|
|
|
11228
11271
|
* Time Complexity: O(1)
|
|
11229
11272
|
* Space Complexity: O(1)
|
|
11230
11273
|
*
|
|
11231
|
-
* The function
|
|
11232
|
-
*
|
|
11233
|
-
*
|
|
11234
|
-
* @
|
|
11235
|
-
*
|
|
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.
|
|
11236
11279
|
*/
|
|
11237
|
-
|
|
11238
|
-
|
|
11239
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
11240
|
-
return;
|
|
11241
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
11242
|
-
node = keyOrNodeOrEntry;
|
|
11243
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
11244
|
-
const [key, value2] = keyOrNodeOrEntry;
|
|
11245
|
-
if (key === void 0 || key === null) {
|
|
11246
|
-
return;
|
|
11247
|
-
} else {
|
|
11248
|
-
node = this.createNode(key, value2, "RED");
|
|
11249
|
-
}
|
|
11250
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
11251
|
-
node = this.createNode(keyOrNodeOrEntry, value, "RED");
|
|
11252
|
-
} else {
|
|
11253
|
-
return;
|
|
11254
|
-
}
|
|
11255
|
-
return node;
|
|
11256
|
-
}
|
|
11257
|
-
/**
|
|
11258
|
-
* Time Complexity: O(1)
|
|
11259
|
-
* Space Complexity: O(1)
|
|
11260
|
-
* /
|
|
11261
|
-
|
|
11262
|
-
/**
|
|
11263
|
-
* Time Complexity: O(1)
|
|
11264
|
-
* Space Complexity: O(1)
|
|
11265
|
-
*
|
|
11266
|
-
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
11267
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The object to check.
|
|
11268
|
-
* @returns {boolean} - `true` if the object is a Red-Black Tree node, `false` otherwise.
|
|
11269
|
-
*/
|
|
11270
|
-
isNode(keyOrNodeOrEntry) {
|
|
11271
|
-
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
11280
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
11281
|
+
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
|
|
11272
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
|
+
// }
|
|
11273
11318
|
/**
|
|
11274
11319
|
* Time Complexity: O(1)
|
|
11275
11320
|
* Space Complexity: O(1)
|
|
@@ -11293,17 +11338,19 @@ var dataStructureTyped = (() => {
|
|
|
11293
11338
|
* Time Complexity: O(log n)
|
|
11294
11339
|
* Space Complexity: O(1)
|
|
11295
11340
|
*
|
|
11296
|
-
* The function adds a new node to a
|
|
11297
|
-
*
|
|
11298
|
-
* @param
|
|
11299
|
-
*
|
|
11300
|
-
* @param {V} [value] - The `value` parameter is
|
|
11301
|
-
*
|
|
11302
|
-
*
|
|
11303
|
-
*
|
|
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.
|
|
11304
11351
|
*/
|
|
11305
|
-
add(
|
|
11306
|
-
const newNode = this.
|
|
11352
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
11353
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
11307
11354
|
if (!this.isRealNode(newNode))
|
|
11308
11355
|
return false;
|
|
11309
11356
|
const insertStatus = this._insert(newNode);
|
|
@@ -11326,16 +11373,16 @@ var dataStructureTyped = (() => {
|
|
|
11326
11373
|
* Time Complexity: O(log n)
|
|
11327
11374
|
* Space Complexity: O(1)
|
|
11328
11375
|
*
|
|
11329
|
-
* The function
|
|
11330
|
-
*
|
|
11331
|
-
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
|
|
11332
|
-
*
|
|
11333
|
-
*
|
|
11334
|
-
*
|
|
11335
|
-
* @param {C} callback - The `callback` parameter is a function that is used to
|
|
11336
|
-
*
|
|
11337
|
-
* `_DEFAULT_CALLBACK
|
|
11338
|
-
*
|
|
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
|
|
11339
11386
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
11340
11387
|
*/
|
|
11341
11388
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
@@ -11387,6 +11434,13 @@ var dataStructureTyped = (() => {
|
|
|
11387
11434
|
return results;
|
|
11388
11435
|
}
|
|
11389
11436
|
/**
|
|
11437
|
+
* Time Complexity: O(1)
|
|
11438
|
+
* Space Complexity: O(1)
|
|
11439
|
+
*/
|
|
11440
|
+
/**
|
|
11441
|
+
* Time Complexity: O(1)
|
|
11442
|
+
* Space Complexity: O(1)
|
|
11443
|
+
*
|
|
11390
11444
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
11391
11445
|
* root.
|
|
11392
11446
|
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
@@ -11408,8 +11462,8 @@ var dataStructureTyped = (() => {
|
|
|
11408
11462
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
11409
11463
|
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
11410
11464
|
* the data structure.
|
|
11411
|
-
* @param {NODE} newNode - The `newNode` parameter is
|
|
11412
|
-
*
|
|
11465
|
+
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
|
|
11466
|
+
* data structure.
|
|
11413
11467
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
11414
11468
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
11415
11469
|
*/
|
|
@@ -11425,12 +11479,13 @@ var dataStructureTyped = (() => {
|
|
|
11425
11479
|
* Time Complexity: O(log n)
|
|
11426
11480
|
* Space Complexity: O(1)
|
|
11427
11481
|
*
|
|
11428
|
-
* The `_insert` function inserts
|
|
11429
|
-
*
|
|
11430
|
-
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into
|
|
11431
|
-
* binary search tree.
|
|
11432
|
-
*
|
|
11433
|
-
*
|
|
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.
|
|
11434
11489
|
*/
|
|
11435
11490
|
_insert(node) {
|
|
11436
11491
|
var _a, _b;
|
|
@@ -11438,9 +11493,10 @@ var dataStructureTyped = (() => {
|
|
|
11438
11493
|
let parent = void 0;
|
|
11439
11494
|
while (this.isRealNode(current)) {
|
|
11440
11495
|
parent = current;
|
|
11441
|
-
|
|
11496
|
+
const compared = this.comparator(node.key, current.key);
|
|
11497
|
+
if (compared < 0) {
|
|
11442
11498
|
current = (_a = current.left) != null ? _a : this.NIL;
|
|
11443
|
-
} else if (
|
|
11499
|
+
} else if (compared > 0) {
|
|
11444
11500
|
current = (_b = current.right) != null ? _b : this.NIL;
|
|
11445
11501
|
} else {
|
|
11446
11502
|
this._replaceNode(current, node);
|
|
@@ -11495,8 +11551,8 @@ var dataStructureTyped = (() => {
|
|
|
11495
11551
|
* Space Complexity: O(1)
|
|
11496
11552
|
*
|
|
11497
11553
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
11498
|
-
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree
|
|
11499
|
-
* 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`.
|
|
11500
11556
|
*/
|
|
11501
11557
|
_insertFixup(z) {
|
|
11502
11558
|
var _a, _b, _c, _d;
|
|
@@ -11552,9 +11608,10 @@ var dataStructureTyped = (() => {
|
|
|
11552
11608
|
*
|
|
11553
11609
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
11554
11610
|
* the colors and performing rotations.
|
|
11555
|
-
* @param {NODE | undefined} node - The `node` parameter represents a node in a
|
|
11556
|
-
*
|
|
11557
|
-
* @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.
|
|
11558
11615
|
*/
|
|
11559
11616
|
_deleteFixup(node) {
|
|
11560
11617
|
var _a, _b, _c, _d;
|
|
@@ -11686,24 +11743,6 @@ var dataStructureTyped = (() => {
|
|
|
11686
11743
|
x.right = y;
|
|
11687
11744
|
y.parent = x;
|
|
11688
11745
|
}
|
|
11689
|
-
/**
|
|
11690
|
-
* The function compares two values using a comparator function and returns whether the first value
|
|
11691
|
-
* is greater than, less than, or equal to the second value.
|
|
11692
|
-
* @param {K} a - The parameter "a" is of type K.
|
|
11693
|
-
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
11694
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
11695
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
11696
|
-
*/
|
|
11697
|
-
_compare(a, b) {
|
|
11698
|
-
const extractedA = this.extractor(a);
|
|
11699
|
-
const extractedB = this.extractor(b);
|
|
11700
|
-
const compared = extractedA - extractedB;
|
|
11701
|
-
if (compared > 0)
|
|
11702
|
-
return "GT";
|
|
11703
|
-
if (compared < 0)
|
|
11704
|
-
return "LT";
|
|
11705
|
-
return "EQ";
|
|
11706
|
-
}
|
|
11707
11746
|
};
|
|
11708
11747
|
|
|
11709
11748
|
// src/data-structures/binary-tree/avl-tree-multi-map.ts
|
|
@@ -11740,13 +11779,20 @@ var dataStructureTyped = (() => {
|
|
|
11740
11779
|
}
|
|
11741
11780
|
};
|
|
11742
11781
|
var AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
11743
|
-
|
|
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) {
|
|
11744
11791
|
super([], options);
|
|
11745
11792
|
__publicField(this, "_count", 0);
|
|
11746
|
-
if (
|
|
11747
|
-
this.addMany(
|
|
11793
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
11794
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
11748
11795
|
}
|
|
11749
|
-
// TODO the _count is not accurate after nodes count modified
|
|
11750
11796
|
/**
|
|
11751
11797
|
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
11752
11798
|
* search.
|
|
@@ -11773,13 +11819,15 @@ var dataStructureTyped = (() => {
|
|
|
11773
11819
|
return sum;
|
|
11774
11820
|
}
|
|
11775
11821
|
/**
|
|
11776
|
-
* The function creates a new
|
|
11777
|
-
* @param {K} key - The key parameter
|
|
11778
|
-
*
|
|
11779
|
-
* @param {
|
|
11780
|
-
*
|
|
11781
|
-
*
|
|
11782
|
-
*
|
|
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.
|
|
11783
11831
|
*/
|
|
11784
11832
|
createNode(key, value, count) {
|
|
11785
11833
|
return new AVLTreeMultiMapNode(key, value, count);
|
|
@@ -11787,57 +11835,58 @@ var dataStructureTyped = (() => {
|
|
|
11787
11835
|
/**
|
|
11788
11836
|
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
11789
11837
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
11790
|
-
* configuration options for creating the
|
|
11791
|
-
*
|
|
11792
|
-
*
|
|
11793
|
-
* @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
|
|
11794
|
-
* default options. The returned value is casted as `TREE`.
|
|
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.
|
|
11795
11841
|
*/
|
|
11796
11842
|
createTree(options) {
|
|
11797
11843
|
return new _AVLTreeMultiMap([], __spreadValues({
|
|
11798
11844
|
iterationType: this.iterationType,
|
|
11799
|
-
|
|
11845
|
+
comparator: this.comparator
|
|
11800
11846
|
}, options));
|
|
11801
11847
|
}
|
|
11802
11848
|
/**
|
|
11803
|
-
* The function
|
|
11804
|
-
* @param
|
|
11805
|
-
* can be
|
|
11806
|
-
* @
|
|
11807
|
-
*
|
|
11808
|
-
|
|
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`.
|
|
11809
11866
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
11810
|
-
* times the value should be added to the
|
|
11811
|
-
* @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.
|
|
11812
11869
|
*/
|
|
11813
|
-
|
|
11814
|
-
|
|
11815
|
-
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
11870
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
11871
|
+
if (keyOrNodeOrEntryOrRawElement === void 0 || keyOrNodeOrEntryOrRawElement === null)
|
|
11816
11872
|
return;
|
|
11817
|
-
|
|
11818
|
-
|
|
11819
|
-
|
|
11820
|
-
const [key,
|
|
11821
|
-
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)
|
|
11822
11883
|
return;
|
|
11823
|
-
|
|
11824
|
-
|
|
11825
|
-
}
|
|
11826
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
11827
|
-
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
11828
|
-
} else {
|
|
11829
|
-
return;
|
|
11884
|
+
else
|
|
11885
|
+
return this.createNode(key, value2, count);
|
|
11830
11886
|
}
|
|
11831
|
-
|
|
11832
|
-
|
|
11833
|
-
|
|
11834
|
-
* The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
|
|
11835
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
11836
|
-
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
|
|
11837
|
-
* class.
|
|
11838
|
-
*/
|
|
11839
|
-
isNode(keyOrNodeOrEntry) {
|
|
11840
|
-
return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
11887
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
11888
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
11889
|
+
return;
|
|
11841
11890
|
}
|
|
11842
11891
|
/**
|
|
11843
11892
|
* Time Complexity: O(log n)
|
|
@@ -11847,20 +11896,21 @@ var dataStructureTyped = (() => {
|
|
|
11847
11896
|
* Time Complexity: O(log n)
|
|
11848
11897
|
* Space Complexity: O(1)
|
|
11849
11898
|
*
|
|
11850
|
-
* The function overrides the add method of a
|
|
11851
|
-
*
|
|
11852
|
-
*
|
|
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
|
|
11853
11905
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
11854
|
-
*
|
|
11855
|
-
* method.
|
|
11906
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
11856
11907
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
11857
|
-
* be added to the
|
|
11858
|
-
* added once. However, you can specify a different value for `count` if you want to add
|
|
11859
|
-
* @returns
|
|
11860
|
-
* 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.
|
|
11861
11911
|
*/
|
|
11862
|
-
add(
|
|
11863
|
-
const newNode = this.
|
|
11912
|
+
add(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
11913
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
11864
11914
|
if (newNode === void 0)
|
|
11865
11915
|
return false;
|
|
11866
11916
|
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
@@ -11878,19 +11928,19 @@ var dataStructureTyped = (() => {
|
|
|
11878
11928
|
* Time Complexity: O(log n)
|
|
11879
11929
|
* Space Complexity: O(1)
|
|
11880
11930
|
*
|
|
11881
|
-
* The `delete` function in
|
|
11882
|
-
*
|
|
11883
|
-
* @param identifier - The identifier is the value
|
|
11884
|
-
*
|
|
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
|
|
11885
11935
|
* function.
|
|
11886
|
-
* @param {C} callback - The `callback` parameter is a function that is used to determine
|
|
11887
|
-
*
|
|
11888
|
-
* function takes
|
|
11889
|
-
*
|
|
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
|
|
11890
11940
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
11891
11941
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
11892
|
-
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
11893
|
-
*
|
|
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
|
|
11894
11944
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
11895
11945
|
*/
|
|
11896
11946
|
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
@@ -11954,7 +12004,8 @@ var dataStructureTyped = (() => {
|
|
|
11954
12004
|
* Time Complexity: O(1)
|
|
11955
12005
|
* Space Complexity: O(1)
|
|
11956
12006
|
*
|
|
11957
|
-
* The clear
|
|
12007
|
+
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
12008
|
+
* zero.
|
|
11958
12009
|
*/
|
|
11959
12010
|
clear() {
|
|
11960
12011
|
super.clear();
|
|
@@ -11967,13 +12018,14 @@ var dataStructureTyped = (() => {
|
|
|
11967
12018
|
/**
|
|
11968
12019
|
* Time Complexity: O(n log n)
|
|
11969
12020
|
* Space Complexity: O(log n)
|
|
11970
|
-
*
|
|
11971
12021
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
11972
12022
|
* tree using either a recursive or iterative approach.
|
|
11973
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
11974
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
11975
|
-
*
|
|
11976
|
-
*
|
|
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.
|
|
11977
12029
|
*/
|
|
11978
12030
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
11979
12031
|
const sorted = this.dfs((node) => node, "IN"), n = sorted.length;
|
|
@@ -12018,7 +12070,7 @@ var dataStructureTyped = (() => {
|
|
|
12018
12070
|
* Time complexity: O(n)
|
|
12019
12071
|
* Space complexity: O(n)
|
|
12020
12072
|
*
|
|
12021
|
-
* The
|
|
12073
|
+
* The function overrides the clone method to create a deep copy of a tree object.
|
|
12022
12074
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
12023
12075
|
*/
|
|
12024
12076
|
clone() {
|
|
@@ -12027,13 +12079,21 @@ var dataStructureTyped = (() => {
|
|
|
12027
12079
|
return cloned;
|
|
12028
12080
|
}
|
|
12029
12081
|
/**
|
|
12030
|
-
*
|
|
12031
|
-
*
|
|
12032
|
-
|
|
12033
|
-
|
|
12034
|
-
*
|
|
12035
|
-
*
|
|
12036
|
-
*
|
|
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`.
|
|
12037
12097
|
*/
|
|
12038
12098
|
_swapProperties(srcNode, destNode) {
|
|
12039
12099
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -12057,12 +12117,19 @@ var dataStructureTyped = (() => {
|
|
|
12057
12117
|
return void 0;
|
|
12058
12118
|
}
|
|
12059
12119
|
/**
|
|
12120
|
+
* Time Complexity: O(1)
|
|
12121
|
+
* Space Complexity: O(1)
|
|
12122
|
+
*/
|
|
12123
|
+
/**
|
|
12124
|
+
* Time Complexity: O(1)
|
|
12125
|
+
* Space Complexity: O(1)
|
|
12126
|
+
*
|
|
12060
12127
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
12061
|
-
* @param {NODE} oldNode - The
|
|
12062
|
-
*
|
|
12063
|
-
* @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.
|
|
12064
12131
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
12065
|
-
* superclass,
|
|
12132
|
+
* superclass, which is of type `NODE`.
|
|
12066
12133
|
*/
|
|
12067
12134
|
_replaceNode(oldNode, newNode) {
|
|
12068
12135
|
newNode.count = oldNode.count + newNode.count;
|
|
@@ -12107,19 +12174,19 @@ var dataStructureTyped = (() => {
|
|
|
12107
12174
|
};
|
|
12108
12175
|
var TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
12109
12176
|
/**
|
|
12110
|
-
* The constructor function initializes a
|
|
12111
|
-
*
|
|
12112
|
-
*
|
|
12113
|
-
*
|
|
12114
|
-
*
|
|
12115
|
-
*
|
|
12116
|
-
*
|
|
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.
|
|
12117
12184
|
*/
|
|
12118
|
-
constructor(
|
|
12185
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
12119
12186
|
super([], options);
|
|
12120
12187
|
__publicField(this, "_count", 0);
|
|
12121
|
-
if (
|
|
12122
|
-
this.addMany(
|
|
12188
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
12189
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
12123
12190
|
}
|
|
12124
12191
|
// TODO the _count is not accurate after nodes count modified
|
|
12125
12192
|
/**
|
|
@@ -12149,16 +12216,15 @@ var dataStructureTyped = (() => {
|
|
|
12149
12216
|
/**
|
|
12150
12217
|
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
12151
12218
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
12152
|
-
* which is a generic type representing the
|
|
12153
|
-
* @param {V} [value] - The `value` parameter
|
|
12154
|
-
* node. It is
|
|
12155
|
-
* 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.
|
|
12156
12222
|
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
12157
12223
|
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
12158
12224
|
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
12159
12225
|
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
12160
12226
|
* with a key in the tree.
|
|
12161
|
-
* @returns A new instance of the TreeMultiMapNode class
|
|
12227
|
+
* @returns A new instance of the TreeMultiMapNode class, casted as NODE.
|
|
12162
12228
|
*/
|
|
12163
12229
|
createNode(key, value, color = "BLACK", count) {
|
|
12164
12230
|
return new TreeMultiMapNode(key, value, count, color);
|
|
@@ -12166,10 +12232,10 @@ var dataStructureTyped = (() => {
|
|
|
12166
12232
|
/**
|
|
12167
12233
|
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
12168
12234
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
12169
|
-
* configuration options for creating the `TreeMultiMap`. It
|
|
12170
|
-
*
|
|
12235
|
+
* configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
|
|
12236
|
+
* R>`.
|
|
12171
12237
|
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
12172
|
-
* existing `iterationType`
|
|
12238
|
+
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
12173
12239
|
*/
|
|
12174
12240
|
createTree(options) {
|
|
12175
12241
|
return new _TreeMultiMap([], __spreadValues({
|
|
@@ -12177,46 +12243,47 @@ var dataStructureTyped = (() => {
|
|
|
12177
12243
|
}, options));
|
|
12178
12244
|
}
|
|
12179
12245
|
/**
|
|
12180
|
-
* The function `
|
|
12181
|
-
*
|
|
12182
|
-
* @param
|
|
12183
|
-
*
|
|
12184
|
-
* @param {V} [value] - The `value` parameter is an optional value
|
|
12185
|
-
*
|
|
12186
|
-
*
|
|
12187
|
-
*
|
|
12188
|
-
*
|
|
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.
|
|
12189
12256
|
*/
|
|
12190
|
-
|
|
12191
|
-
|
|
12192
|
-
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
12257
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
12258
|
+
if (keyOrNodeOrEntryOrRawElement === void 0 || keyOrNodeOrEntryOrRawElement === null)
|
|
12193
12259
|
return;
|
|
12194
|
-
|
|
12195
|
-
|
|
12196
|
-
|
|
12197
|
-
const [key
|
|
12198
|
-
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)
|
|
12199
12270
|
return;
|
|
12200
|
-
|
|
12201
|
-
|
|
12202
|
-
}
|
|
12203
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
12204
|
-
node = this.createNode(keyOrNodeOrEntry, value, "BLACK", count);
|
|
12205
|
-
} else {
|
|
12206
|
-
return;
|
|
12271
|
+
else
|
|
12272
|
+
return this.createNode(key, value2, "BLACK", count);
|
|
12207
12273
|
}
|
|
12208
|
-
|
|
12274
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
12275
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value, "BLACK", count);
|
|
12276
|
+
return;
|
|
12209
12277
|
}
|
|
12210
12278
|
/**
|
|
12211
|
-
* The function
|
|
12212
|
-
*
|
|
12213
|
-
*
|
|
12214
|
-
*
|
|
12215
|
-
*
|
|
12216
|
-
* 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.
|
|
12217
12284
|
*/
|
|
12218
|
-
isNode(
|
|
12219
|
-
return
|
|
12285
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
12286
|
+
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
|
|
12220
12287
|
}
|
|
12221
12288
|
/**
|
|
12222
12289
|
* Time Complexity: O(log n)
|
|
@@ -12226,17 +12293,20 @@ var dataStructureTyped = (() => {
|
|
|
12226
12293
|
* Time Complexity: O(log n)
|
|
12227
12294
|
* Space Complexity: O(1)
|
|
12228
12295
|
*
|
|
12229
|
-
* The function overrides the add method
|
|
12230
|
-
*
|
|
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:
|
|
12231
12300
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
12232
|
-
* data structure.
|
|
12301
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
12233
12302
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
12234
|
-
* be added to the data structure. By default, it is set to 1, meaning that
|
|
12235
|
-
*
|
|
12236
|
-
* @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.
|
|
12237
12307
|
*/
|
|
12238
|
-
add(
|
|
12239
|
-
const newNode = this.
|
|
12308
|
+
add(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
12309
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
12240
12310
|
const orgCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
12241
12311
|
const isSuccessAdded = super.add(newNode);
|
|
12242
12312
|
if (isSuccessAdded) {
|
|
@@ -12254,20 +12324,18 @@ var dataStructureTyped = (() => {
|
|
|
12254
12324
|
* Time Complexity: O(log n)
|
|
12255
12325
|
* Space Complexity: O(1)
|
|
12256
12326
|
*
|
|
12257
|
-
* The `delete`
|
|
12258
|
-
*
|
|
12259
|
-
*
|
|
12260
|
-
*
|
|
12261
|
-
*
|
|
12262
|
-
*
|
|
12263
|
-
*
|
|
12264
|
-
*
|
|
12265
|
-
*
|
|
12266
|
-
*
|
|
12267
|
-
*
|
|
12268
|
-
*
|
|
12269
|
-
* and the node will be deleted regardless of its count. If set to false (default), the count of the
|
|
12270
|
-
* 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.
|
|
12271
12339
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
12272
12340
|
*/
|
|
12273
12341
|
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
@@ -12375,10 +12443,12 @@ var dataStructureTyped = (() => {
|
|
|
12375
12443
|
*
|
|
12376
12444
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
12377
12445
|
* tree using either a recursive or iterative approach.
|
|
12378
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
12379
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
12380
|
-
*
|
|
12381
|
-
*
|
|
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.
|
|
12382
12452
|
*/
|
|
12383
12453
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
12384
12454
|
const sorted = this.dfs((node) => node, "IN"), n = sorted.length;
|
|
@@ -12432,15 +12502,22 @@ var dataStructureTyped = (() => {
|
|
|
12432
12502
|
return cloned;
|
|
12433
12503
|
}
|
|
12434
12504
|
/**
|
|
12435
|
-
*
|
|
12436
|
-
*
|
|
12437
|
-
|
|
12438
|
-
|
|
12439
|
-
*
|
|
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.
|
|
12440
12519
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
12441
|
-
* If
|
|
12442
|
-
* and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
|
|
12443
|
-
* If either `srcNode` or `destNode` is
|
|
12520
|
+
* If either `srcNode` or `destNode` is undefined, it returns undefined.
|
|
12444
12521
|
*/
|
|
12445
12522
|
_swapProperties(srcNode, destNode) {
|
|
12446
12523
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -12464,12 +12541,19 @@ var dataStructureTyped = (() => {
|
|
|
12464
12541
|
return void 0;
|
|
12465
12542
|
}
|
|
12466
12543
|
/**
|
|
12544
|
+
* Time Complexity: O(1)
|
|
12545
|
+
* Space Complexity: O(1)
|
|
12546
|
+
*/
|
|
12547
|
+
/**
|
|
12548
|
+
* Time Complexity: O(1)
|
|
12549
|
+
* Space Complexity: O(1)
|
|
12550
|
+
*
|
|
12467
12551
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
12468
|
-
* @param {NODE} oldNode - The `oldNode` parameter is
|
|
12469
|
-
*
|
|
12470
|
-
* @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.
|
|
12471
12555
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
12472
|
-
* superclass,
|
|
12556
|
+
* superclass, which is of type `NODE`.
|
|
12473
12557
|
*/
|
|
12474
12558
|
_replaceNode(oldNode, newNode) {
|
|
12475
12559
|
newNode.count = oldNode.count + newNode.count;
|