queue-typed 1.53.5 → 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/README.md +5 -7
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +23 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/data-structures/binary-tree/binary-tree.js +100 -66
- package/dist/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/data-structures/binary-tree/bst.js +185 -66
- package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +6 -6
- package/dist/data-structures/heap/heap.js +6 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +49 -34
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +144 -62
- package/dist/data-structures/linked-list/singly-linked-list.js +201 -97
- package/dist/data-structures/trie/trie.d.ts +104 -4
- package/dist/data-structures/trie/trie.js +116 -12
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- 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 +139 -121
- package/src/data-structures/linked-list/singly-linked-list.ts +219 -98
- package/src/data-structures/trie/trie.ts +116 -11
- package/src/index.ts +2 -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
|
@@ -29,7 +29,7 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
29
29
|
import { isComparable, trampoline } from '../../utils';
|
|
30
30
|
import { Queue } from '../queue';
|
|
31
31
|
import { IterableEntryBase } from '../base';
|
|
32
|
-
import { DFSOperation } from '../../
|
|
32
|
+
import { DFSOperation, Range } from '../../common';
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* Represents a node in a binary tree.
|
|
@@ -233,17 +233,14 @@ export class BinaryTree<
|
|
|
233
233
|
return [this.createNode(key, finalValue), finalValue];
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
237
|
-
|
|
238
236
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
243
|
-
}
|
|
244
|
-
return [undefined, undefined];
|
|
237
|
+
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
238
|
+
const finalValue = value ?? entryValue;
|
|
239
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
245
240
|
}
|
|
246
241
|
|
|
242
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
243
|
+
|
|
247
244
|
return [undefined, undefined];
|
|
248
245
|
}
|
|
249
246
|
|
|
@@ -275,15 +272,15 @@ export class BinaryTree<
|
|
|
275
272
|
const key = keyNodeEntryOrRaw[0];
|
|
276
273
|
if (key === null) return null;
|
|
277
274
|
if (key === undefined) return;
|
|
278
|
-
return this.
|
|
275
|
+
return this.getNode(key, this._root, iterationType);
|
|
279
276
|
}
|
|
280
277
|
|
|
281
278
|
if (this._toEntryFn) {
|
|
282
279
|
const [key] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
283
|
-
if (this.isKey(key)) return this.
|
|
280
|
+
if (this.isKey(key)) return this.getNode(key);
|
|
284
281
|
}
|
|
285
282
|
|
|
286
|
-
if (this.isKey(keyNodeEntryOrRaw)) return this.
|
|
283
|
+
if (this.isKey(keyNodeEntryOrRaw)) return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
|
|
287
284
|
return;
|
|
288
285
|
}
|
|
289
286
|
|
|
@@ -302,8 +299,15 @@ export class BinaryTree<
|
|
|
302
299
|
return keyNodeEntryOrRaw instanceof BinaryTreeNode;
|
|
303
300
|
}
|
|
304
301
|
|
|
302
|
+
/**
|
|
303
|
+
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
304
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
|
|
305
|
+
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
306
|
+
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
307
|
+
* indicating that it is of type `R`.
|
|
308
|
+
*/
|
|
305
309
|
isRaw(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is R {
|
|
306
|
-
return typeof keyNodeEntryOrRaw === 'object';
|
|
310
|
+
return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
|
|
307
311
|
}
|
|
308
312
|
|
|
309
313
|
/**
|
|
@@ -345,6 +349,12 @@ export class BinaryTree<
|
|
|
345
349
|
return keyNodeEntryOrRaw === this._NIL;
|
|
346
350
|
}
|
|
347
351
|
|
|
352
|
+
isRange(
|
|
353
|
+
keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>
|
|
354
|
+
): keyNodeEntryRawOrPredicate is Range<K> {
|
|
355
|
+
return keyNodeEntryRawOrPredicate instanceof Range;
|
|
356
|
+
}
|
|
357
|
+
|
|
348
358
|
/**
|
|
349
359
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
350
360
|
* tree.
|
|
@@ -508,6 +518,18 @@ export class BinaryTree<
|
|
|
508
518
|
return inserted;
|
|
509
519
|
}
|
|
510
520
|
|
|
521
|
+
/**
|
|
522
|
+
* Time Complexity: O(k * n)
|
|
523
|
+
* Space Complexity: O(1)
|
|
524
|
+
*
|
|
525
|
+
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
526
|
+
* elements from the other tree.
|
|
527
|
+
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
528
|
+
*/
|
|
529
|
+
merge(anotherTree: BinaryTree<K, V, R, NODE, TREE>) {
|
|
530
|
+
this.addMany(anotherTree, []);
|
|
531
|
+
}
|
|
532
|
+
|
|
511
533
|
/**
|
|
512
534
|
* Time Complexity: O(k * n)
|
|
513
535
|
* Space Complexity: O(1)
|
|
@@ -589,41 +611,45 @@ export class BinaryTree<
|
|
|
589
611
|
* Time Complexity: O(n)
|
|
590
612
|
* Space Complexity: O(k + log n)
|
|
591
613
|
*
|
|
592
|
-
* The
|
|
593
|
-
* or
|
|
594
|
-
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
595
|
-
*
|
|
596
|
-
* @param [onlyOne=false] - The `onlyOne` parameter in the `
|
|
597
|
-
* determines whether
|
|
598
|
-
*
|
|
599
|
-
* @param {
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
* @
|
|
607
|
-
*
|
|
614
|
+
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
615
|
+
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
616
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
617
|
+
* `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
|
|
618
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
619
|
+
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
620
|
+
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
621
|
+
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
622
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
623
|
+
* extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
624
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
|
|
625
|
+
* used to specify the node from which the search operation should begin. It represents the starting
|
|
626
|
+
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
627
|
+
* provided, the search operation will start from the root
|
|
628
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
|
|
629
|
+
* specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
|
|
630
|
+
* two possible values:
|
|
631
|
+
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
632
|
+
* on the search algorithm implemented within the function.
|
|
608
633
|
*/
|
|
609
|
-
|
|
634
|
+
search<C extends NodeCallback<NODE>>(
|
|
610
635
|
keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
|
|
611
636
|
onlyOne = false,
|
|
637
|
+
callback: C = this._DEFAULT_NODE_CALLBACK as C,
|
|
612
638
|
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
613
639
|
iterationType: IterationType = this.iterationType
|
|
614
|
-
):
|
|
640
|
+
): ReturnType<C>[] {
|
|
615
641
|
if (keyNodeEntryRawOrPredicate === undefined) return [];
|
|
616
642
|
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
617
643
|
startNode = this.ensureNode(startNode);
|
|
618
644
|
if (!startNode) return [];
|
|
619
|
-
const
|
|
645
|
+
const predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
620
646
|
|
|
621
|
-
const ans:
|
|
647
|
+
const ans: ReturnType<C>[] = [];
|
|
622
648
|
|
|
623
649
|
if (iterationType === 'RECURSIVE') {
|
|
624
650
|
const dfs = (cur: NODE) => {
|
|
625
|
-
if (
|
|
626
|
-
ans.push(cur);
|
|
651
|
+
if (predicate(cur)) {
|
|
652
|
+
ans.push(callback(cur));
|
|
627
653
|
if (onlyOne) return;
|
|
628
654
|
}
|
|
629
655
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
@@ -637,8 +663,8 @@ export class BinaryTree<
|
|
|
637
663
|
while (stack.length > 0) {
|
|
638
664
|
const cur = stack.pop();
|
|
639
665
|
if (this.isRealNode(cur)) {
|
|
640
|
-
if (
|
|
641
|
-
ans.push(cur);
|
|
666
|
+
if (predicate(cur)) {
|
|
667
|
+
ans.push(callback(cur));
|
|
642
668
|
if (onlyOne) return ans;
|
|
643
669
|
}
|
|
644
670
|
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
@@ -650,6 +676,36 @@ export class BinaryTree<
|
|
|
650
676
|
return ans;
|
|
651
677
|
}
|
|
652
678
|
|
|
679
|
+
/**
|
|
680
|
+
* Time Complexity: O(n)
|
|
681
|
+
* Space Complexity: O(k + log n)
|
|
682
|
+
*
|
|
683
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
684
|
+
* or predicate, with options for recursive or iterative traversal.
|
|
685
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
686
|
+
* - The `getNodes` function you provided takes several parameters:
|
|
687
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
688
|
+
* determines whether to return only the first node that matches the criteria specified by the
|
|
689
|
+
* `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
690
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
691
|
+
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
692
|
+
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
693
|
+
* not provided, the default value is set to `this._root
|
|
694
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
695
|
+
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
696
|
+
* can have two possible values:
|
|
697
|
+
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
698
|
+
* based on the input parameters and the iteration type specified.
|
|
699
|
+
*/
|
|
700
|
+
getNodes(
|
|
701
|
+
keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
|
|
702
|
+
onlyOne = false,
|
|
703
|
+
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
704
|
+
iterationType: IterationType = this.iterationType
|
|
705
|
+
): NODE[] {
|
|
706
|
+
return this.search(keyNodeEntryRawOrPredicate, onlyOne, node => node, startNode, iterationType);
|
|
707
|
+
}
|
|
708
|
+
|
|
653
709
|
/**
|
|
654
710
|
* Time Complexity: O(n)
|
|
655
711
|
* Space Complexity: O(log n).
|
|
@@ -675,24 +731,7 @@ export class BinaryTree<
|
|
|
675
731
|
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
676
732
|
iterationType: IterationType = this.iterationType
|
|
677
733
|
): OptNodeOrNull<NODE> {
|
|
678
|
-
return this.
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
/**
|
|
682
|
-
* Time Complexity: O(n)
|
|
683
|
-
* Space Complexity: O(log n)
|
|
684
|
-
*
|
|
685
|
-
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
686
|
-
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
687
|
-
* structure.
|
|
688
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
689
|
-
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
690
|
-
* is an optional parameter with a default value of `this.iterationType`.
|
|
691
|
-
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
692
|
-
* (`OptNodeOrNull<NODE>`).
|
|
693
|
-
*/
|
|
694
|
-
getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptNodeOrNull<NODE> {
|
|
695
|
-
return this.getNode(key, this._root, iterationType);
|
|
734
|
+
return this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType)[0] ?? null;
|
|
696
735
|
}
|
|
697
736
|
|
|
698
737
|
/**
|
|
@@ -723,7 +762,7 @@ export class BinaryTree<
|
|
|
723
762
|
iterationType: IterationType = this.iterationType
|
|
724
763
|
): V | undefined {
|
|
725
764
|
if (this._isMapMode) {
|
|
726
|
-
const key = this.
|
|
765
|
+
const key = this._extractKey(keyNodeEntryRawOrPredicate);
|
|
727
766
|
if (key === null || key === undefined) return;
|
|
728
767
|
return this._store.get(key);
|
|
729
768
|
}
|
|
@@ -756,7 +795,7 @@ export class BinaryTree<
|
|
|
756
795
|
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
757
796
|
iterationType: IterationType = this.iterationType
|
|
758
797
|
): boolean {
|
|
759
|
-
return this.
|
|
798
|
+
return this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType).length > 0;
|
|
760
799
|
}
|
|
761
800
|
|
|
762
801
|
/**
|
|
@@ -1023,9 +1062,9 @@ export class BinaryTree<
|
|
|
1023
1062
|
* parameter.
|
|
1024
1063
|
*/
|
|
1025
1064
|
getPathToRoot<C extends NodeCallback<OptNodeOrNull<NODE>>>(
|
|
1026
|
-
callback: C = this._DEFAULT_NODE_CALLBACK as C,
|
|
1027
1065
|
beginNode: BTNRep<K, V, NODE> | R,
|
|
1028
|
-
|
|
1066
|
+
callback: C = this._DEFAULT_NODE_CALLBACK as C,
|
|
1067
|
+
isReverse = false
|
|
1029
1068
|
): ReturnType<C>[] {
|
|
1030
1069
|
const result: ReturnType<C>[] = [];
|
|
1031
1070
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
@@ -1118,7 +1157,6 @@ export class BinaryTree<
|
|
|
1118
1157
|
iterationType: IterationType = this.iterationType
|
|
1119
1158
|
): ReturnType<C> {
|
|
1120
1159
|
if (this.isNIL(startNode)) return callback(undefined);
|
|
1121
|
-
// TODO support get right most by passing key in
|
|
1122
1160
|
startNode = this.ensureNode(startNode);
|
|
1123
1161
|
if (!startNode) return callback(startNode);
|
|
1124
1162
|
|
|
@@ -2144,16 +2182,16 @@ export class BinaryTree<
|
|
|
2144
2182
|
* Time Complexity: O(1)
|
|
2145
2183
|
* Space Complexity: O(1)
|
|
2146
2184
|
*
|
|
2147
|
-
* The function `
|
|
2185
|
+
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
2148
2186
|
* entry, raw data, or null/undefined.
|
|
2149
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `
|
|
2187
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
|
|
2150
2188
|
* TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
|
|
2151
2189
|
* where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
|
|
2152
|
-
* @returns The `
|
|
2190
|
+
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
|
|
2153
2191
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
2154
2192
|
* the conditions checked in the method.
|
|
2155
2193
|
*/
|
|
2156
|
-
protected
|
|
2194
|
+
protected _extractKey(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): K | null | undefined {
|
|
2157
2195
|
if (keyNodeEntryOrRaw === null) return null;
|
|
2158
2196
|
if (keyNodeEntryOrRaw === undefined) return;
|
|
2159
2197
|
if (keyNodeEntryOrRaw === this._NIL) return;
|
|
@@ -2193,6 +2231,9 @@ export class BinaryTree<
|
|
|
2193
2231
|
}
|
|
2194
2232
|
|
|
2195
2233
|
/**
|
|
2234
|
+
* Time Complexity: O(1)
|
|
2235
|
+
* Space Complexity: O(1)
|
|
2236
|
+
*
|
|
2196
2237
|
* The _clearNodes function sets the root node to undefined and resets the size to 0.
|
|
2197
2238
|
*/
|
|
2198
2239
|
protected _clearNodes() {
|
|
@@ -2201,6 +2242,9 @@ export class BinaryTree<
|
|
|
2201
2242
|
}
|
|
2202
2243
|
|
|
2203
2244
|
/**
|
|
2245
|
+
* Time Complexity: O(1)
|
|
2246
|
+
* Space Complexity: O(1)
|
|
2247
|
+
*
|
|
2204
2248
|
* The _clearValues function clears all values stored in the _store object.
|
|
2205
2249
|
*/
|
|
2206
2250
|
protected _clearValues() {
|