data-structure-typed 1.53.6 → 1.53.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +16 -25
- package/benchmark/report.html +32 -5
- package/benchmark/report.json +326 -23
- package/dist/cjs/common/index.d.ts +12 -0
- package/dist/cjs/common/index.js +24 -0
- package/dist/cjs/common/index.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +100 -66
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/cjs/data-structures/binary-tree/bst.js +185 -66
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -6
- package/dist/cjs/data-structures/heap/heap.js +6 -6
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +18 -8
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +24 -10
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +110 -4
- package/dist/cjs/data-structures/trie/trie.js +122 -12
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/cjs/types/utils/utils.d.ts +10 -6
- package/dist/cjs/utils/utils.js +4 -2
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/common/index.d.ts +12 -0
- package/dist/mjs/common/index.js +24 -0
- package/dist/mjs/common/index.js.map +1 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +8 -10
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +3 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +95 -61
- package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/mjs/data-structures/binary-tree/bst.js +187 -66
- package/dist/mjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -6
- package/dist/mjs/data-structures/heap/heap.js +6 -6
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +18 -8
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +24 -10
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/trie/trie.d.ts +110 -4
- package/dist/mjs/data-structures/trie/trie.js +122 -12
- package/dist/mjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/index.d.ts +1 -1
- package/dist/mjs/index.js +1 -1
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/mjs/types/utils/utils.d.ts +10 -6
- package/dist/mjs/utils/utils.js +4 -2
- package/dist/mjs/utils/utils.js.map +1 -1
- package/dist/umd/data-structure-typed.js +299 -156
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/common/index.ts +19 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +108 -64
- package/src/data-structures/binary-tree/bst.ts +190 -69
- package/src/data-structures/binary-tree/rb-tree.ts +6 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +39 -39
- package/src/data-structures/linked-list/doubly-linked-list.ts +111 -97
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/trie/trie.ts +116 -11
- package/src/index.ts +1 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +3 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -1
- package/test/performance/reportor.ts +38 -33
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -3
- package/test/unit/data-structures/binary-tree/overall.test.ts +14 -22
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +100 -1
- package/test/unit/data-structures/trie/trie.test.ts +151 -0
- package/test/unit/utils/utils.test.ts +6 -6
- package/dist/cjs/constants/index.d.ts +0 -4
- package/dist/cjs/constants/index.js +0 -9
- package/dist/cjs/constants/index.js.map +0 -1
- package/dist/mjs/constants/index.d.ts +0 -4
- package/dist/mjs/constants/index.js +0 -6
- package/dist/mjs/constants/index.js.map +0 -1
- package/src/constants/index.ts +0 -4
|
@@ -131,6 +131,7 @@ var dataStructureTyped = (() => {
|
|
|
131
131
|
Navigator: () => Navigator,
|
|
132
132
|
PriorityQueue: () => PriorityQueue,
|
|
133
133
|
Queue: () => Queue,
|
|
134
|
+
Range: () => Range,
|
|
134
135
|
RedBlackTree: () => RedBlackTree,
|
|
135
136
|
RedBlackTreeNode: () => RedBlackTreeNode,
|
|
136
137
|
SegmentTree: () => SegmentTree,
|
|
@@ -670,7 +671,7 @@ var dataStructureTyped = (() => {
|
|
|
670
671
|
};
|
|
671
672
|
function isPrimitiveComparable(value) {
|
|
672
673
|
const valueType = typeof value;
|
|
673
|
-
if (valueType === "number") return
|
|
674
|
+
if (valueType === "number") return true;
|
|
674
675
|
return valueType === "bigint" || valueType === "string" || valueType === "boolean";
|
|
675
676
|
}
|
|
676
677
|
function tryObjectToPrimitive(obj) {
|
|
@@ -691,7 +692,7 @@ var dataStructureTyped = (() => {
|
|
|
691
692
|
if (value === null || value === void 0) return false;
|
|
692
693
|
if (isPrimitiveComparable(value)) return true;
|
|
693
694
|
if (typeof value !== "object") return false;
|
|
694
|
-
if (value instanceof Date) return
|
|
695
|
+
if (value instanceof Date) return true;
|
|
695
696
|
if (isForceObjectComparable) return true;
|
|
696
697
|
const comparableValue = tryObjectToPrimitive(value);
|
|
697
698
|
if (comparableValue === null || comparableValue === void 0) return false;
|
|
@@ -1659,7 +1660,7 @@ var dataStructureTyped = (() => {
|
|
|
1659
1660
|
* @returns The `get` method returns the value of the first node in the singly linked list that
|
|
1660
1661
|
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
1661
1662
|
*/
|
|
1662
|
-
|
|
1663
|
+
search(elementNodeOrPredicate) {
|
|
1663
1664
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1664
1665
|
let current = this.head;
|
|
1665
1666
|
while (current) {
|
|
@@ -2216,6 +2217,16 @@ var dataStructureTyped = (() => {
|
|
|
2216
2217
|
}
|
|
2217
2218
|
};
|
|
2218
2219
|
var DoublyLinkedList = class _DoublyLinkedList extends IterableElementBase {
|
|
2220
|
+
/**
|
|
2221
|
+
* This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
|
|
2222
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
|
|
2223
|
+
* iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
|
|
2224
|
+
* with the elements provided in the iterable. If no elements are provided, the default value is an
|
|
2225
|
+
* empty iterable.
|
|
2226
|
+
* @param [options] - The `options` parameter in the constructor is of type
|
|
2227
|
+
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
2228
|
+
* configuration options to customize the behavior of the DoublyLinkedList.
|
|
2229
|
+
*/
|
|
2219
2230
|
constructor(elements = [], options) {
|
|
2220
2231
|
super(options);
|
|
2221
2232
|
__publicField(this, "_head");
|
|
@@ -2495,7 +2506,7 @@ var dataStructureTyped = (() => {
|
|
|
2495
2506
|
* node was not found.
|
|
2496
2507
|
*/
|
|
2497
2508
|
addBefore(existingElementOrNode, newElementOrNode) {
|
|
2498
|
-
const existingNode = this.getNode(existingElementOrNode);
|
|
2509
|
+
const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
|
|
2499
2510
|
if (existingNode) {
|
|
2500
2511
|
const newNode = this._ensureNode(newElementOrNode);
|
|
2501
2512
|
newNode.prev = existingNode.prev;
|
|
@@ -2529,7 +2540,7 @@ var dataStructureTyped = (() => {
|
|
|
2529
2540
|
* was not found in the linked list.
|
|
2530
2541
|
*/
|
|
2531
2542
|
addAfter(existingElementOrNode, newElementOrNode) {
|
|
2532
|
-
const existingNode = this.getNode(existingElementOrNode);
|
|
2543
|
+
const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
|
|
2533
2544
|
if (existingNode) {
|
|
2534
2545
|
const newNode = this._ensureNode(newElementOrNode);
|
|
2535
2546
|
newNode.next = existingNode.next;
|
|
@@ -2662,7 +2673,7 @@ var dataStructureTyped = (() => {
|
|
|
2662
2673
|
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
2663
2674
|
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
2664
2675
|
*/
|
|
2665
|
-
|
|
2676
|
+
search(elementNodeOrPredicate) {
|
|
2666
2677
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
2667
2678
|
let current = this.head;
|
|
2668
2679
|
while (current) {
|
|
@@ -7097,12 +7108,26 @@ var dataStructureTyped = (() => {
|
|
|
7097
7108
|
}
|
|
7098
7109
|
};
|
|
7099
7110
|
|
|
7100
|
-
// src/
|
|
7111
|
+
// src/common/index.ts
|
|
7101
7112
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
7102
7113
|
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
7103
7114
|
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
7104
7115
|
return DFSOperation2;
|
|
7105
7116
|
})(DFSOperation || {});
|
|
7117
|
+
var Range = class {
|
|
7118
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
7119
|
+
this.low = low;
|
|
7120
|
+
this.high = high;
|
|
7121
|
+
this.includeLow = includeLow;
|
|
7122
|
+
this.includeHigh = includeHigh;
|
|
7123
|
+
}
|
|
7124
|
+
// Determine whether a key is within the range
|
|
7125
|
+
isInRange(key, comparator) {
|
|
7126
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
7127
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
7128
|
+
return lowCheck && highCheck;
|
|
7129
|
+
}
|
|
7130
|
+
};
|
|
7106
7131
|
|
|
7107
7132
|
// src/data-structures/binary-tree/binary-tree.ts
|
|
7108
7133
|
var BinaryTreeNode = class {
|
|
@@ -7246,15 +7271,12 @@ var dataStructureTyped = (() => {
|
|
|
7246
7271
|
const finalValue = value != null ? value : entryValue;
|
|
7247
7272
|
return [this.createNode(key, finalValue), finalValue];
|
|
7248
7273
|
}
|
|
7249
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
7250
7274
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
7251
|
-
|
|
7252
|
-
|
|
7253
|
-
|
|
7254
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
7255
|
-
}
|
|
7256
|
-
return [void 0, void 0];
|
|
7275
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
7276
|
+
const finalValue = value != null ? value : entryValue;
|
|
7277
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
7257
7278
|
}
|
|
7279
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
7258
7280
|
return [void 0, void 0];
|
|
7259
7281
|
}
|
|
7260
7282
|
/**
|
|
@@ -7281,13 +7303,13 @@ var dataStructureTyped = (() => {
|
|
|
7281
7303
|
const key = keyNodeEntryOrRaw[0];
|
|
7282
7304
|
if (key === null) return null;
|
|
7283
7305
|
if (key === void 0) return;
|
|
7284
|
-
return this.
|
|
7306
|
+
return this.getNode(key, this._root, iterationType);
|
|
7285
7307
|
}
|
|
7286
7308
|
if (this._toEntryFn) {
|
|
7287
7309
|
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
7288
|
-
if (this.isKey(key)) return this.
|
|
7310
|
+
if (this.isKey(key)) return this.getNode(key);
|
|
7289
7311
|
}
|
|
7290
|
-
if (this.isKey(keyNodeEntryOrRaw)) return this.
|
|
7312
|
+
if (this.isKey(keyNodeEntryOrRaw)) return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
|
|
7291
7313
|
return;
|
|
7292
7314
|
}
|
|
7293
7315
|
/**
|
|
@@ -7304,8 +7326,15 @@ var dataStructureTyped = (() => {
|
|
|
7304
7326
|
isNode(keyNodeEntryOrRaw) {
|
|
7305
7327
|
return keyNodeEntryOrRaw instanceof BinaryTreeNode;
|
|
7306
7328
|
}
|
|
7329
|
+
/**
|
|
7330
|
+
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
7331
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
|
|
7332
|
+
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
7333
|
+
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
7334
|
+
* indicating that it is of type `R`.
|
|
7335
|
+
*/
|
|
7307
7336
|
isRaw(keyNodeEntryOrRaw) {
|
|
7308
|
-
return typeof keyNodeEntryOrRaw === "object";
|
|
7337
|
+
return this._toEntryFn !== void 0 && typeof keyNodeEntryOrRaw === "object";
|
|
7309
7338
|
}
|
|
7310
7339
|
/**
|
|
7311
7340
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
@@ -7343,6 +7372,9 @@ var dataStructureTyped = (() => {
|
|
|
7343
7372
|
isNIL(keyNodeEntryOrRaw) {
|
|
7344
7373
|
return keyNodeEntryOrRaw === this._NIL;
|
|
7345
7374
|
}
|
|
7375
|
+
isRange(keyNodeEntryRawOrPredicate) {
|
|
7376
|
+
return keyNodeEntryRawOrPredicate instanceof Range;
|
|
7377
|
+
}
|
|
7346
7378
|
/**
|
|
7347
7379
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
7348
7380
|
* tree.
|
|
@@ -7481,6 +7513,17 @@ var dataStructureTyped = (() => {
|
|
|
7481
7513
|
}
|
|
7482
7514
|
return inserted;
|
|
7483
7515
|
}
|
|
7516
|
+
/**
|
|
7517
|
+
* Time Complexity: O(k * n)
|
|
7518
|
+
* Space Complexity: O(1)
|
|
7519
|
+
*
|
|
7520
|
+
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
7521
|
+
* elements from the other tree.
|
|
7522
|
+
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
7523
|
+
*/
|
|
7524
|
+
merge(anotherTree) {
|
|
7525
|
+
this.addMany(anotherTree, []);
|
|
7526
|
+
}
|
|
7484
7527
|
/**
|
|
7485
7528
|
* Time Complexity: O(k * n)
|
|
7486
7529
|
* Space Complexity: O(1)
|
|
@@ -7555,34 +7598,37 @@ var dataStructureTyped = (() => {
|
|
|
7555
7598
|
* Time Complexity: O(n)
|
|
7556
7599
|
* Space Complexity: O(k + log n)
|
|
7557
7600
|
*
|
|
7558
|
-
* The
|
|
7559
|
-
* or
|
|
7560
|
-
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
7561
|
-
*
|
|
7562
|
-
* @param [onlyOne=false] - The `onlyOne` parameter in the `
|
|
7563
|
-
* determines whether
|
|
7564
|
-
*
|
|
7565
|
-
* @param {
|
|
7566
|
-
*
|
|
7567
|
-
*
|
|
7568
|
-
*
|
|
7569
|
-
*
|
|
7570
|
-
*
|
|
7571
|
-
*
|
|
7572
|
-
* @
|
|
7573
|
-
*
|
|
7574
|
-
|
|
7575
|
-
|
|
7601
|
+
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
7602
|
+
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
7603
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
7604
|
+
* `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
|
|
7605
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
7606
|
+
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
7607
|
+
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
7608
|
+
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
7609
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
7610
|
+
* extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
7611
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
|
|
7612
|
+
* used to specify the node from which the search operation should begin. It represents the starting
|
|
7613
|
+
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
7614
|
+
* provided, the search operation will start from the root
|
|
7615
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
|
|
7616
|
+
* specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
|
|
7617
|
+
* two possible values:
|
|
7618
|
+
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
7619
|
+
* on the search algorithm implemented within the function.
|
|
7620
|
+
*/
|
|
7621
|
+
search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
7576
7622
|
if (keyNodeEntryRawOrPredicate === void 0) return [];
|
|
7577
7623
|
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
7578
7624
|
startNode = this.ensureNode(startNode);
|
|
7579
7625
|
if (!startNode) return [];
|
|
7580
|
-
const
|
|
7626
|
+
const predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
7581
7627
|
const ans = [];
|
|
7582
7628
|
if (iterationType === "RECURSIVE") {
|
|
7583
7629
|
const dfs = (cur) => {
|
|
7584
|
-
if (
|
|
7585
|
-
ans.push(cur);
|
|
7630
|
+
if (predicate(cur)) {
|
|
7631
|
+
ans.push(callback(cur));
|
|
7586
7632
|
if (onlyOne) return;
|
|
7587
7633
|
}
|
|
7588
7634
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
@@ -7595,8 +7641,8 @@ var dataStructureTyped = (() => {
|
|
|
7595
7641
|
while (stack.length > 0) {
|
|
7596
7642
|
const cur = stack.pop();
|
|
7597
7643
|
if (this.isRealNode(cur)) {
|
|
7598
|
-
if (
|
|
7599
|
-
ans.push(cur);
|
|
7644
|
+
if (predicate(cur)) {
|
|
7645
|
+
ans.push(callback(cur));
|
|
7600
7646
|
if (onlyOne) return ans;
|
|
7601
7647
|
}
|
|
7602
7648
|
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
@@ -7606,6 +7652,30 @@ var dataStructureTyped = (() => {
|
|
|
7606
7652
|
}
|
|
7607
7653
|
return ans;
|
|
7608
7654
|
}
|
|
7655
|
+
/**
|
|
7656
|
+
* Time Complexity: O(n)
|
|
7657
|
+
* Space Complexity: O(k + log n)
|
|
7658
|
+
*
|
|
7659
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
7660
|
+
* or predicate, with options for recursive or iterative traversal.
|
|
7661
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
7662
|
+
* - The `getNodes` function you provided takes several parameters:
|
|
7663
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
7664
|
+
* determines whether to return only the first node that matches the criteria specified by the
|
|
7665
|
+
* `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
7666
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
7667
|
+
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
7668
|
+
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
7669
|
+
* not provided, the default value is set to `this._root
|
|
7670
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
7671
|
+
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
7672
|
+
* can have two possible values:
|
|
7673
|
+
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
7674
|
+
* based on the input parameters and the iteration type specified.
|
|
7675
|
+
*/
|
|
7676
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
7677
|
+
return this.search(keyNodeEntryRawOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
7678
|
+
}
|
|
7609
7679
|
/**
|
|
7610
7680
|
* Time Complexity: O(n)
|
|
7611
7681
|
* Space Complexity: O(log n).
|
|
@@ -7628,23 +7698,7 @@ var dataStructureTyped = (() => {
|
|
|
7628
7698
|
*/
|
|
7629
7699
|
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7630
7700
|
var _a;
|
|
7631
|
-
return (_a = this.
|
|
7632
|
-
}
|
|
7633
|
-
/**
|
|
7634
|
-
* Time Complexity: O(n)
|
|
7635
|
-
* Space Complexity: O(log n)
|
|
7636
|
-
*
|
|
7637
|
-
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
7638
|
-
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
7639
|
-
* structure.
|
|
7640
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
7641
|
-
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
7642
|
-
* is an optional parameter with a default value of `this.iterationType`.
|
|
7643
|
-
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
7644
|
-
* (`OptNodeOrNull<NODE>`).
|
|
7645
|
-
*/
|
|
7646
|
-
getNodeByKey(key, iterationType = this.iterationType) {
|
|
7647
|
-
return this.getNode(key, this._root, iterationType);
|
|
7701
|
+
return (_a = this.search(keyNodeEntryRawOrPredicate, true, (node) => node, startNode, iterationType)[0]) != null ? _a : null;
|
|
7648
7702
|
}
|
|
7649
7703
|
/**
|
|
7650
7704
|
* Time Complexity: O(n)
|
|
@@ -7671,7 +7725,7 @@ var dataStructureTyped = (() => {
|
|
|
7671
7725
|
get(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7672
7726
|
var _a;
|
|
7673
7727
|
if (this._isMapMode) {
|
|
7674
|
-
const key = this.
|
|
7728
|
+
const key = this._extractKey(keyNodeEntryRawOrPredicate);
|
|
7675
7729
|
if (key === null || key === void 0) return;
|
|
7676
7730
|
return this._store.get(key);
|
|
7677
7731
|
}
|
|
@@ -7699,7 +7753,7 @@ var dataStructureTyped = (() => {
|
|
|
7699
7753
|
* Otherwise, it returns `false`.
|
|
7700
7754
|
*/
|
|
7701
7755
|
has(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
7702
|
-
return this.
|
|
7756
|
+
return this.search(keyNodeEntryRawOrPredicate, true, (node) => node, startNode, iterationType).length > 0;
|
|
7703
7757
|
}
|
|
7704
7758
|
/**
|
|
7705
7759
|
* Time Complexity: O(1)
|
|
@@ -7938,7 +7992,7 @@ var dataStructureTyped = (() => {
|
|
|
7938
7992
|
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
7939
7993
|
* parameter.
|
|
7940
7994
|
*/
|
|
7941
|
-
getPathToRoot(callback = this._DEFAULT_NODE_CALLBACK,
|
|
7995
|
+
getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
|
|
7942
7996
|
const result = [];
|
|
7943
7997
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
7944
7998
|
if (!beginNodeEnsured) return result;
|
|
@@ -8863,16 +8917,16 @@ var dataStructureTyped = (() => {
|
|
|
8863
8917
|
* Time Complexity: O(1)
|
|
8864
8918
|
* Space Complexity: O(1)
|
|
8865
8919
|
*
|
|
8866
|
-
* The function `
|
|
8920
|
+
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
8867
8921
|
* entry, raw data, or null/undefined.
|
|
8868
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `
|
|
8922
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
|
|
8869
8923
|
* TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
|
|
8870
8924
|
* where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
|
|
8871
|
-
* @returns The `
|
|
8925
|
+
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
|
|
8872
8926
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
8873
8927
|
* the conditions checked in the method.
|
|
8874
8928
|
*/
|
|
8875
|
-
|
|
8929
|
+
_extractKey(keyNodeEntryOrRaw) {
|
|
8876
8930
|
if (keyNodeEntryOrRaw === null) return null;
|
|
8877
8931
|
if (keyNodeEntryOrRaw === void 0) return;
|
|
8878
8932
|
if (keyNodeEntryOrRaw === this._NIL) return;
|
|
@@ -8907,6 +8961,9 @@ var dataStructureTyped = (() => {
|
|
|
8907
8961
|
return this._store.set(key, value);
|
|
8908
8962
|
}
|
|
8909
8963
|
/**
|
|
8964
|
+
* Time Complexity: O(1)
|
|
8965
|
+
* Space Complexity: O(1)
|
|
8966
|
+
*
|
|
8910
8967
|
* The _clearNodes function sets the root node to undefined and resets the size to 0.
|
|
8911
8968
|
*/
|
|
8912
8969
|
_clearNodes() {
|
|
@@ -8914,6 +8971,9 @@ var dataStructureTyped = (() => {
|
|
|
8914
8971
|
this._size = 0;
|
|
8915
8972
|
}
|
|
8916
8973
|
/**
|
|
8974
|
+
* Time Complexity: O(1)
|
|
8975
|
+
* Space Complexity: O(1)
|
|
8976
|
+
*
|
|
8917
8977
|
* The _clearValues function clears all values stored in the _store object.
|
|
8918
8978
|
*/
|
|
8919
8979
|
_clearValues() {
|
|
@@ -8982,20 +9042,30 @@ var dataStructureTyped = (() => {
|
|
|
8982
9042
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
8983
9043
|
super([], options);
|
|
8984
9044
|
__publicField(this, "_root");
|
|
8985
|
-
__publicField(this, "
|
|
9045
|
+
__publicField(this, "_isReverse", false);
|
|
9046
|
+
__publicField(this, "_comparator", (a, b) => {
|
|
9047
|
+
if (isComparable(a) && isComparable(b)) {
|
|
9048
|
+
if (a > b) return 1;
|
|
9049
|
+
if (a < b) return -1;
|
|
9050
|
+
return 0;
|
|
9051
|
+
}
|
|
9052
|
+
if (this._extractComparable) {
|
|
9053
|
+
if (this._extractComparable(a) > this._extractComparable(b)) return 1;
|
|
9054
|
+
if (this._extractComparable(a) < this._extractComparable(b)) return -1;
|
|
9055
|
+
return 0;
|
|
9056
|
+
}
|
|
8986
9057
|
if (typeof a === "object" || typeof b === "object") {
|
|
8987
9058
|
throw TypeError(
|
|
8988
|
-
`When comparing object types, a custom
|
|
9059
|
+
`When comparing object types, a custom extractComparable must be defined in the constructor's options parameter.`
|
|
8989
9060
|
);
|
|
8990
9061
|
}
|
|
8991
|
-
if (a > b) return 1;
|
|
8992
|
-
if (a < b) return -1;
|
|
8993
9062
|
return 0;
|
|
8994
9063
|
});
|
|
8995
|
-
__publicField(this, "
|
|
9064
|
+
__publicField(this, "_extractComparable");
|
|
8996
9065
|
if (options) {
|
|
8997
|
-
const {
|
|
8998
|
-
if (
|
|
9066
|
+
const { extractComparable, isReverse } = options;
|
|
9067
|
+
if (typeof extractComparable === "function") this._extractComparable = extractComparable;
|
|
9068
|
+
if (isReverse !== void 0) this._isReverse = isReverse;
|
|
8999
9069
|
}
|
|
9000
9070
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
9001
9071
|
}
|
|
@@ -9006,6 +9076,14 @@ var dataStructureTyped = (() => {
|
|
|
9006
9076
|
get root() {
|
|
9007
9077
|
return this._root;
|
|
9008
9078
|
}
|
|
9079
|
+
/**
|
|
9080
|
+
* The above function is a getter method in TypeScript that returns the value of the private property
|
|
9081
|
+
* `_isReverse`.
|
|
9082
|
+
* @returns The `isReverse` property of the object, which is a boolean value.
|
|
9083
|
+
*/
|
|
9084
|
+
get isReverse() {
|
|
9085
|
+
return this._isReverse;
|
|
9086
|
+
}
|
|
9009
9087
|
/**
|
|
9010
9088
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
9011
9089
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -9028,8 +9106,9 @@ var dataStructureTyped = (() => {
|
|
|
9028
9106
|
return new _BST([], __spreadValues({
|
|
9029
9107
|
iterationType: this.iterationType,
|
|
9030
9108
|
isMapMode: this._isMapMode,
|
|
9031
|
-
|
|
9032
|
-
toEntryFn: this._toEntryFn
|
|
9109
|
+
extractComparable: this._extractComparable,
|
|
9110
|
+
toEntryFn: this._toEntryFn,
|
|
9111
|
+
isReverse: this._isReverse
|
|
9033
9112
|
}, options));
|
|
9034
9113
|
}
|
|
9035
9114
|
/**
|
|
@@ -9080,11 +9159,11 @@ var dataStructureTyped = (() => {
|
|
|
9080
9159
|
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
|
|
9081
9160
|
* type `K`.
|
|
9082
9161
|
* @returns The `override isKey(key: any): key is K` function is returning a boolean value based on
|
|
9083
|
-
* the result of the `isComparable` function with the condition `this.
|
|
9162
|
+
* the result of the `isComparable` function with the condition `this._compare !==
|
|
9084
9163
|
* this._DEFAULT_COMPARATOR`.
|
|
9085
9164
|
*/
|
|
9086
9165
|
isKey(key) {
|
|
9087
|
-
return isComparable(key, this.
|
|
9166
|
+
return isComparable(key, this._extractComparable !== void 0);
|
|
9088
9167
|
}
|
|
9089
9168
|
/**
|
|
9090
9169
|
* Time Complexity: O(log n)
|
|
@@ -9108,11 +9187,11 @@ var dataStructureTyped = (() => {
|
|
|
9108
9187
|
}
|
|
9109
9188
|
let current = this._root;
|
|
9110
9189
|
while (current !== void 0) {
|
|
9111
|
-
if (this.
|
|
9190
|
+
if (this._compare(current.key, newNode.key) === 0) {
|
|
9112
9191
|
this._replaceNode(current, newNode);
|
|
9113
9192
|
if (this._isMapMode) this._setValue(current.key, newValue);
|
|
9114
9193
|
return true;
|
|
9115
|
-
} else if (this.
|
|
9194
|
+
} else if (this._compare(current.key, newNode.key) > 0) {
|
|
9116
9195
|
if (current.left === void 0) {
|
|
9117
9196
|
current.left = newNode;
|
|
9118
9197
|
if (this._isMapMode) this._setValue(newNode == null ? void 0 : newNode.key, newValue);
|
|
@@ -9190,7 +9269,7 @@ var dataStructureTyped = (() => {
|
|
|
9190
9269
|
keyB = b;
|
|
9191
9270
|
}
|
|
9192
9271
|
if (keyA !== void 0 && keyA !== null && keyB !== void 0 && keyB !== null) {
|
|
9193
|
-
return this.
|
|
9272
|
+
return this._compare(keyA, keyB);
|
|
9194
9273
|
}
|
|
9195
9274
|
return 0;
|
|
9196
9275
|
});
|
|
@@ -9226,48 +9305,92 @@ var dataStructureTyped = (() => {
|
|
|
9226
9305
|
}
|
|
9227
9306
|
return inserted;
|
|
9228
9307
|
}
|
|
9308
|
+
/**
|
|
9309
|
+
* Time Complexity: O(n)
|
|
9310
|
+
* Space Complexity: O(1)
|
|
9311
|
+
*
|
|
9312
|
+
* The `merge` function overrides the base class method by adding elements from another
|
|
9313
|
+
* binary search tree.
|
|
9314
|
+
* @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
|
|
9315
|
+
* value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
|
|
9316
|
+
*/
|
|
9317
|
+
merge(anotherTree) {
|
|
9318
|
+
this.addMany(anotherTree, [], false);
|
|
9319
|
+
}
|
|
9229
9320
|
/**
|
|
9230
9321
|
* Time Complexity: O(log n)
|
|
9231
9322
|
* Space Complexity: O(k + log n)
|
|
9232
9323
|
*
|
|
9233
|
-
* The function `
|
|
9234
|
-
*
|
|
9235
|
-
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
9236
|
-
* parameter in the `
|
|
9237
|
-
*
|
|
9238
|
-
*
|
|
9239
|
-
*
|
|
9240
|
-
*
|
|
9241
|
-
*
|
|
9242
|
-
*
|
|
9243
|
-
*
|
|
9244
|
-
*
|
|
9245
|
-
*
|
|
9246
|
-
*
|
|
9247
|
-
*
|
|
9248
|
-
*
|
|
9249
|
-
*
|
|
9250
|
-
*
|
|
9324
|
+
* The function `search` in TypeScript overrides the search behavior in a binary tree structure based
|
|
9325
|
+
* on specified criteria.
|
|
9326
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
9327
|
+
* `keyNodeEntryRawOrPredicate` parameter in the `override search` method can accept one of the
|
|
9328
|
+
* following types:
|
|
9329
|
+
* @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
|
|
9330
|
+
* search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
|
|
9331
|
+
* search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
|
|
9332
|
+
* @param {C} callback - The `callback` parameter in the `override search` function is a function
|
|
9333
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
9334
|
+
* extends `NodeCallback<NODE>`. The callback function should accept a node of type `NODE` as its
|
|
9335
|
+
* argument and
|
|
9336
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `override search`
|
|
9337
|
+
* method represents the node from which the search operation will begin. It is the starting point
|
|
9338
|
+
* for searching within the tree data structure. The method ensures that the `startNode` is a valid
|
|
9339
|
+
* node before proceeding with the search operation. If the `
|
|
9340
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `override search`
|
|
9341
|
+
* function determines the type of iteration to be used during the search operation. It can have two
|
|
9342
|
+
* possible values:
|
|
9343
|
+
* @returns The `override search` method returns an array of values that match the search criteria
|
|
9344
|
+
* specified by the input parameters. The method performs a search operation on a binary tree
|
|
9345
|
+
* structure based on the provided key, predicate, and other options. The search results are
|
|
9346
|
+
* collected in an array and returned as the output of the method.
|
|
9251
9347
|
*/
|
|
9252
|
-
|
|
9348
|
+
search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
9253
9349
|
if (keyNodeEntryRawOrPredicate === void 0) return [];
|
|
9254
9350
|
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
9255
9351
|
startNode = this.ensureNode(startNode);
|
|
9256
9352
|
if (!startNode) return [];
|
|
9257
|
-
|
|
9353
|
+
let predicate;
|
|
9354
|
+
const isRange = this.isRange(keyNodeEntryRawOrPredicate);
|
|
9355
|
+
if (isRange) {
|
|
9356
|
+
predicate = (node) => keyNodeEntryRawOrPredicate.isInRange(node.key, this._comparator);
|
|
9357
|
+
} else {
|
|
9358
|
+
predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
9359
|
+
}
|
|
9360
|
+
const isToLeftByRange = (cur) => {
|
|
9361
|
+
if (isRange) {
|
|
9362
|
+
const range = keyNodeEntryRawOrPredicate;
|
|
9363
|
+
const leftS = this.isReverse ? range.high : range.low;
|
|
9364
|
+
const leftI = this.isReverse ? range.includeHigh : range.includeLow;
|
|
9365
|
+
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
9366
|
+
}
|
|
9367
|
+
return false;
|
|
9368
|
+
};
|
|
9369
|
+
const isToRightByRange = (cur) => {
|
|
9370
|
+
if (isRange) {
|
|
9371
|
+
const range = keyNodeEntryRawOrPredicate;
|
|
9372
|
+
const rightS = this.isReverse ? range.low : range.high;
|
|
9373
|
+
const rightI = this.isReverse ? range.includeLow : range.includeLow;
|
|
9374
|
+
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
9375
|
+
}
|
|
9376
|
+
return false;
|
|
9377
|
+
};
|
|
9258
9378
|
const ans = [];
|
|
9259
9379
|
if (iterationType === "RECURSIVE") {
|
|
9260
9380
|
const dfs = (cur) => {
|
|
9261
|
-
if (
|
|
9262
|
-
ans.push(cur);
|
|
9381
|
+
if (predicate(cur)) {
|
|
9382
|
+
ans.push(callback(cur));
|
|
9263
9383
|
if (onlyOne) return;
|
|
9264
9384
|
}
|
|
9265
9385
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
9266
|
-
if (
|
|
9267
|
-
|
|
9268
|
-
if (this.isRealNode(cur.
|
|
9386
|
+
if (isRange) {
|
|
9387
|
+
if (this.isRealNode(cur.left) && isToLeftByRange(cur)) dfs(cur.left);
|
|
9388
|
+
if (this.isRealNode(cur.right) && isToRightByRange(cur)) dfs(cur.right);
|
|
9389
|
+
} else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
9390
|
+
const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate);
|
|
9391
|
+
if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) > 0)
|
|
9269
9392
|
dfs(cur.left);
|
|
9270
|
-
if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this.
|
|
9393
|
+
if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) < 0)
|
|
9271
9394
|
dfs(cur.right);
|
|
9272
9395
|
} else {
|
|
9273
9396
|
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
@@ -9279,15 +9402,18 @@ var dataStructureTyped = (() => {
|
|
|
9279
9402
|
const stack = [startNode];
|
|
9280
9403
|
while (stack.length > 0) {
|
|
9281
9404
|
const cur = stack.pop();
|
|
9282
|
-
if (
|
|
9283
|
-
ans.push(cur);
|
|
9405
|
+
if (predicate(cur)) {
|
|
9406
|
+
ans.push(callback(cur));
|
|
9284
9407
|
if (onlyOne) return ans;
|
|
9285
9408
|
}
|
|
9286
|
-
if (
|
|
9287
|
-
|
|
9288
|
-
if (this.isRealNode(cur.right) &&
|
|
9409
|
+
if (isRange) {
|
|
9410
|
+
if (this.isRealNode(cur.left) && isToLeftByRange(cur)) stack.push(cur.left);
|
|
9411
|
+
if (this.isRealNode(cur.right) && isToRightByRange(cur)) stack.push(cur.right);
|
|
9412
|
+
} else if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
9413
|
+
const benchmarkKey = this._extractKey(keyNodeEntryRawOrPredicate);
|
|
9414
|
+
if (this.isRealNode(cur.right) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) < 0)
|
|
9289
9415
|
stack.push(cur.right);
|
|
9290
|
-
if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this.
|
|
9416
|
+
if (this.isRealNode(cur.left) && benchmarkKey !== null && benchmarkKey !== void 0 && this._compare(cur.key, benchmarkKey) > 0)
|
|
9291
9417
|
stack.push(cur.left);
|
|
9292
9418
|
} else {
|
|
9293
9419
|
if (this.isRealNode(cur.right)) stack.push(cur.right);
|
|
@@ -9321,22 +9447,6 @@ var dataStructureTyped = (() => {
|
|
|
9321
9447
|
var _a;
|
|
9322
9448
|
return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
|
|
9323
9449
|
}
|
|
9324
|
-
/**
|
|
9325
|
-
* Time Complexity: O(log n)
|
|
9326
|
-
* Space Complexity: O(1)
|
|
9327
|
-
*
|
|
9328
|
-
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
9329
|
-
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
9330
|
-
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
9331
|
-
* in the tree structure.
|
|
9332
|
-
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
9333
|
-
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
9334
|
-
* It has a default value of `'ITERATIVE'`.
|
|
9335
|
-
* @returns The method is returning a NODE object or undefined.
|
|
9336
|
-
*/
|
|
9337
|
-
getNodeByKey(key, iterationType = this.iterationType) {
|
|
9338
|
-
return this.getNode(key, this._root, iterationType);
|
|
9339
|
-
}
|
|
9340
9450
|
/**
|
|
9341
9451
|
* Time complexity: O(n)
|
|
9342
9452
|
* Space complexity: O(n)
|
|
@@ -9430,7 +9540,7 @@ var dataStructureTyped = (() => {
|
|
|
9430
9540
|
const targetKey = targetNodeEnsured.key;
|
|
9431
9541
|
if (iterationType === "RECURSIVE") {
|
|
9432
9542
|
const dfs = (cur) => {
|
|
9433
|
-
const compared = this.
|
|
9543
|
+
const compared = this._compare(cur.key, targetKey);
|
|
9434
9544
|
if (Math.sign(compared) === lesserOrGreater) ans.push(callback(cur));
|
|
9435
9545
|
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
9436
9546
|
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
@@ -9442,7 +9552,7 @@ var dataStructureTyped = (() => {
|
|
|
9442
9552
|
while (queue.size > 0) {
|
|
9443
9553
|
const cur = queue.shift();
|
|
9444
9554
|
if (this.isRealNode(cur)) {
|
|
9445
|
-
const compared = this.
|
|
9555
|
+
const compared = this._compare(cur.key, targetKey);
|
|
9446
9556
|
if (Math.sign(compared) === lesserOrGreater) ans.push(callback(cur));
|
|
9447
9557
|
if (this.isRealNode(cur.left)) queue.push(cur.left);
|
|
9448
9558
|
if (this.isRealNode(cur.right)) queue.push(cur.right);
|
|
@@ -9554,6 +9664,14 @@ var dataStructureTyped = (() => {
|
|
|
9554
9664
|
get comparator() {
|
|
9555
9665
|
return this._comparator;
|
|
9556
9666
|
}
|
|
9667
|
+
/**
|
|
9668
|
+
* This function returns the value of the `_extractComparable` property.
|
|
9669
|
+
* @returns The method `extractComparable()` is being returned, which is a getter method for the
|
|
9670
|
+
* `_extractComparable` property.
|
|
9671
|
+
*/
|
|
9672
|
+
get extractComparable() {
|
|
9673
|
+
return this._extractComparable;
|
|
9674
|
+
}
|
|
9557
9675
|
/**
|
|
9558
9676
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
9559
9677
|
* root.
|
|
@@ -9565,6 +9683,9 @@ var dataStructureTyped = (() => {
|
|
|
9565
9683
|
}
|
|
9566
9684
|
this._root = v;
|
|
9567
9685
|
}
|
|
9686
|
+
_compare(a, b) {
|
|
9687
|
+
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
|
|
9688
|
+
}
|
|
9568
9689
|
};
|
|
9569
9690
|
|
|
9570
9691
|
// src/data-structures/binary-tree/binary-indexed-tree.ts
|
|
@@ -10200,8 +10321,9 @@ var dataStructureTyped = (() => {
|
|
|
10200
10321
|
return new _AVLTree([], __spreadValues({
|
|
10201
10322
|
iterationType: this.iterationType,
|
|
10202
10323
|
isMapMode: this._isMapMode,
|
|
10203
|
-
|
|
10204
|
-
toEntryFn: this._toEntryFn
|
|
10324
|
+
extractComparable: this._extractComparable,
|
|
10325
|
+
toEntryFn: this._toEntryFn,
|
|
10326
|
+
isReverse: this._isReverse
|
|
10205
10327
|
}, options));
|
|
10206
10328
|
}
|
|
10207
10329
|
/**
|
|
@@ -10487,7 +10609,7 @@ var dataStructureTyped = (() => {
|
|
|
10487
10609
|
*/
|
|
10488
10610
|
_balancePath(node) {
|
|
10489
10611
|
node = this.ensureNode(node);
|
|
10490
|
-
const path = this.getPathToRoot((node2) => node2,
|
|
10612
|
+
const path = this.getPathToRoot(node, (node2) => node2, false);
|
|
10491
10613
|
for (let i = 0; i < path.length; i++) {
|
|
10492
10614
|
const A = path[i];
|
|
10493
10615
|
if (A) {
|
|
@@ -10619,7 +10741,7 @@ var dataStructureTyped = (() => {
|
|
|
10619
10741
|
return new _RedBlackTree([], __spreadValues({
|
|
10620
10742
|
iterationType: this.iterationType,
|
|
10621
10743
|
isMapMode: this._isMapMode,
|
|
10622
|
-
|
|
10744
|
+
extractComparable: this._extractComparable,
|
|
10623
10745
|
toEntryFn: this._toEntryFn
|
|
10624
10746
|
}, options));
|
|
10625
10747
|
}
|
|
@@ -10828,7 +10950,7 @@ var dataStructureTyped = (() => {
|
|
|
10828
10950
|
let parent = void 0;
|
|
10829
10951
|
while (this.isRealNode(current)) {
|
|
10830
10952
|
parent = current;
|
|
10831
|
-
const compared = this.
|
|
10953
|
+
const compared = this._compare(node.key, current.key);
|
|
10832
10954
|
if (compared < 0) {
|
|
10833
10955
|
current = (_a = current.left) != null ? _a : this.NIL;
|
|
10834
10956
|
} else if (compared > 0) {
|
|
@@ -11143,8 +11265,9 @@ var dataStructureTyped = (() => {
|
|
|
11143
11265
|
return new _AVLTreeMultiMap([], __spreadValues({
|
|
11144
11266
|
iterationType: this.iterationType,
|
|
11145
11267
|
isMapMode: this._isMapMode,
|
|
11146
|
-
|
|
11147
|
-
toEntryFn: this._toEntryFn
|
|
11268
|
+
extractComparable: this._extractComparable,
|
|
11269
|
+
toEntryFn: this._toEntryFn,
|
|
11270
|
+
isReverse: this._isReverse
|
|
11148
11271
|
}, options));
|
|
11149
11272
|
}
|
|
11150
11273
|
/**
|
|
@@ -11178,15 +11301,12 @@ var dataStructureTyped = (() => {
|
|
|
11178
11301
|
const finalValue = value != null ? value : entryValue;
|
|
11179
11302
|
return [this.createNode(key, finalValue, count), finalValue];
|
|
11180
11303
|
}
|
|
11181
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
11182
11304
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
11183
|
-
|
|
11184
|
-
|
|
11185
|
-
|
|
11186
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
11187
|
-
}
|
|
11188
|
-
return [void 0, void 0];
|
|
11305
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
11306
|
+
const finalValue = value != null ? value : entryValue;
|
|
11307
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
11189
11308
|
}
|
|
11309
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
11190
11310
|
return [void 0, void 0];
|
|
11191
11311
|
}
|
|
11192
11312
|
/**
|
|
@@ -11505,7 +11625,7 @@ var dataStructureTyped = (() => {
|
|
|
11505
11625
|
return new _TreeMultiMap([], __spreadValues({
|
|
11506
11626
|
iterationType: this.iterationType,
|
|
11507
11627
|
isMapMode: this._isMapMode,
|
|
11508
|
-
|
|
11628
|
+
extractComparable: this._extractComparable,
|
|
11509
11629
|
toEntryFn: this._toEntryFn
|
|
11510
11630
|
}, options));
|
|
11511
11631
|
}
|
|
@@ -11530,7 +11650,7 @@ var dataStructureTyped = (() => {
|
|
|
11530
11650
|
const finalValue = value != null ? value : entryValue;
|
|
11531
11651
|
if (this.isKey(key)) return [this.createNode(key, finalValue, "BLACK", count), finalValue];
|
|
11532
11652
|
}
|
|
11533
|
-
if (this.
|
|
11653
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
11534
11654
|
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
11535
11655
|
const finalValue = value != null ? value : entryValue;
|
|
11536
11656
|
if (this.isKey(key)) return [this.createNode(key, finalValue, "BLACK", count), finalValue];
|
|
@@ -12649,10 +12769,16 @@ var dataStructureTyped = (() => {
|
|
|
12649
12769
|
};
|
|
12650
12770
|
var Trie = class _Trie extends IterableElementBase {
|
|
12651
12771
|
/**
|
|
12652
|
-
* The constructor
|
|
12653
|
-
*
|
|
12654
|
-
* @param
|
|
12655
|
-
*
|
|
12772
|
+
* The constructor initializes a Trie data structure with optional options and words provided as
|
|
12773
|
+
* input.
|
|
12774
|
+
* @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the constructor is an
|
|
12775
|
+
* iterable containing either strings or elements of type `R`. It is used to initialize the Trie with
|
|
12776
|
+
* a list of words or elements. If no `words` are provided, an empty iterable is used as the default
|
|
12777
|
+
* value.
|
|
12778
|
+
* @param [options] - The `options` parameter in the constructor is an optional object that can
|
|
12779
|
+
* contain configuration options for the Trie data structure. One of the options it can have is
|
|
12780
|
+
* `caseSensitive`, which is a boolean value indicating whether the Trie should be case-sensitive or
|
|
12781
|
+
* not. If `caseSensitive` is set to `
|
|
12656
12782
|
*/
|
|
12657
12783
|
constructor(words = [], options) {
|
|
12658
12784
|
super(options);
|
|
@@ -12664,13 +12790,7 @@ var dataStructureTyped = (() => {
|
|
|
12664
12790
|
if (caseSensitive !== void 0) this._caseSensitive = caseSensitive;
|
|
12665
12791
|
}
|
|
12666
12792
|
if (words) {
|
|
12667
|
-
|
|
12668
|
-
if (this.toElementFn) {
|
|
12669
|
-
this.add(this.toElementFn(word));
|
|
12670
|
-
} else {
|
|
12671
|
-
this.add(word);
|
|
12672
|
-
}
|
|
12673
|
-
}
|
|
12793
|
+
this.addMany(words);
|
|
12674
12794
|
}
|
|
12675
12795
|
}
|
|
12676
12796
|
/**
|
|
@@ -12721,6 +12841,29 @@ var dataStructureTyped = (() => {
|
|
|
12721
12841
|
}
|
|
12722
12842
|
return isNewWord;
|
|
12723
12843
|
}
|
|
12844
|
+
/**
|
|
12845
|
+
* Time Complexity: O(n * l)
|
|
12846
|
+
* Space Complexity: O(1)
|
|
12847
|
+
*
|
|
12848
|
+
* The `addMany` function in TypeScript takes an iterable of strings or elements of type R, converts
|
|
12849
|
+
* them using a provided function if available, and adds them to a data structure while returning an
|
|
12850
|
+
* array of boolean values indicating success.
|
|
12851
|
+
* @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the `addMany` function is
|
|
12852
|
+
* an iterable that contains either strings or elements of type `R`.
|
|
12853
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each word in
|
|
12854
|
+
* the input iterable was successfully added to the data structure.
|
|
12855
|
+
*/
|
|
12856
|
+
addMany(words = []) {
|
|
12857
|
+
const ans = [];
|
|
12858
|
+
for (const word of words) {
|
|
12859
|
+
if (this.toElementFn) {
|
|
12860
|
+
ans.push(this.add(this.toElementFn(word)));
|
|
12861
|
+
} else {
|
|
12862
|
+
ans.push(this.add(word));
|
|
12863
|
+
}
|
|
12864
|
+
}
|
|
12865
|
+
return ans;
|
|
12866
|
+
}
|
|
12724
12867
|
/**
|
|
12725
12868
|
* Time Complexity: O(l), where l is the length of the input word.
|
|
12726
12869
|
* Space Complexity: O(1) - Constant space.
|