priority-queue-typed 1.54.1 → 1.54.3

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.
Files changed (46) hide show
  1. package/LICENSE +1 -1
  2. package/coverage/lcov-report/index.ts.html +2 -2
  3. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
  4. package/dist/data-structures/binary-tree/avl-tree-counter.js +8 -7
  5. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -12
  6. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +2 -2
  7. package/dist/data-structures/binary-tree/avl-tree.d.ts +25 -21
  8. package/dist/data-structures/binary-tree/avl-tree.js +12 -8
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +173 -225
  10. package/dist/data-structures/binary-tree/binary-tree.js +239 -144
  11. package/dist/data-structures/binary-tree/bst.d.ts +62 -56
  12. package/dist/data-structures/binary-tree/bst.js +78 -122
  13. package/dist/data-structures/binary-tree/red-black-tree.d.ts +19 -25
  14. package/dist/data-structures/binary-tree/red-black-tree.js +7 -13
  15. package/dist/data-structures/binary-tree/tree-counter.d.ts +19 -19
  16. package/dist/data-structures/binary-tree/tree-counter.js +12 -12
  17. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +14 -14
  18. package/dist/data-structures/binary-tree/tree-multi-map.js +4 -4
  19. package/dist/index.d.ts +2 -2
  20. package/dist/index.js +2 -2
  21. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  22. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  23. package/dist/types/data-structures/binary-tree/index.d.ts +1 -1
  24. package/dist/types/data-structures/binary-tree/index.js +1 -1
  25. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
  26. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  27. package/dist/utils/utils.d.ts +2 -2
  28. package/package.json +3 -3
  29. package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
  30. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +25 -15
  31. package/src/data-structures/binary-tree/avl-tree.ts +35 -29
  32. package/src/data-structures/binary-tree/binary-tree.ts +469 -252
  33. package/src/data-structures/binary-tree/bst.ts +141 -143
  34. package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
  35. package/src/data-structures/binary-tree/tree-counter.ts +33 -27
  36. package/src/data-structures/binary-tree/tree-multi-map.ts +25 -17
  37. package/src/index.ts +2 -2
  38. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
  39. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  40. package/src/types/data-structures/binary-tree/index.ts +1 -1
  41. package/src/types/data-structures/binary-tree/tree-counter.ts +1 -1
  42. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  43. package/src/utils/utils.ts +2 -2
  44. /package/dist/types/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +0 -0
  45. /package/dist/types/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +0 -0
  46. /package/src/types/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +0 -0
@@ -75,9 +75,9 @@ exports.BSTNode = BSTNode;
75
75
  * @example
76
76
  * // Find elements in a range
77
77
  * const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
78
- * console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
79
- * console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
80
- * console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
78
+ * console.log(bst.search(new Range(5, 10))); // [5, 7, 10]
79
+ * console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['5', '7', '10', '12']
80
+ * console.log(bst.search(new Range(4, 12, true, false))); // [5, 7, 10]
81
81
  * console.log(bst.rangeSearch([15, 20])); // [15, 18]
82
82
  * console.log(bst.search(new Range(15, 20, false))); // [18]
83
83
  * @example
@@ -111,7 +111,7 @@ class BST extends binary_tree_1.BinaryTree {
111
111
  * This TypeScript constructor initializes a binary search tree with optional options and adds
112
112
  * elements if provided.
113
113
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
114
- * iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to
114
+ * iterable that can contain elements of type `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It is used to
115
115
  * initialize the binary search tree with keys, nodes, entries, or raw data.
116
116
  * @param [options] - The `options` parameter is an optional object that can contain the following
117
117
  * properties:
@@ -195,7 +195,7 @@ class BST extends binary_tree_1.BinaryTree {
195
195
  *
196
196
  * The function ensures the existence of a node in a data structure and returns it, or undefined if
197
197
  * it doesn't exist.
198
- * @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
198
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
199
199
  * `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node,
200
200
  * entry, or raw element that needs to be ensured in the tree.
201
201
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
@@ -213,8 +213,8 @@ class BST extends binary_tree_1.BinaryTree {
213
213
  * Space Complexity: O(1)
214
214
  *
215
215
  * The function checks if the input is an instance of the BSTNode class.
216
- * @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
217
- * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`.
216
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
217
+ * `keyNodeOrEntry` can be of type `R` or `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
218
218
  * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
219
219
  * an instance of the `BSTNode` class.
220
220
  */
@@ -240,8 +240,8 @@ class BST extends binary_tree_1.BinaryTree {
240
240
  * Space Complexity: O(log n)
241
241
  *
242
242
  * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
243
- * @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
244
- * `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`.
243
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
244
+ * `keyNodeOrEntry` can accept a value of type `R` or `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
245
245
  * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
246
246
  * key in the binary search tree. If provided, it will be stored in the node along with the key.
247
247
  * @returns a boolean value.
@@ -412,7 +412,7 @@ class BST extends binary_tree_1.BinaryTree {
412
412
  *
413
413
  * The function `search` in TypeScript overrides the search behavior in a binary tree structure based
414
414
  * on specified criteria.
415
- * @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The
415
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The
416
416
  * `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the
417
417
  * following types:
418
418
  * @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
@@ -420,9 +420,9 @@ class BST extends binary_tree_1.BinaryTree {
420
420
  * search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
421
421
  * @param {C} callback - The `callback` parameter in the `override search` function is a function
422
422
  * that will be called on each node that matches the search criteria. It is of type `C`, which
423
- * extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its
423
+ * extends `NodeCallback<BSTNode<K, V> | null>`. The callback function should accept a node of type `BSTNode<K, V>` as its
424
424
  * argument and
425
- * @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search`
425
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `override search`
426
426
  * method represents the node from which the search operation will begin. It is the starting point
427
427
  * for searching within the tree data structure. The method ensures that the `startNode` is a valid
428
428
  * node before proceeding with the search operation. If the `
@@ -446,104 +446,54 @@ class BST extends binary_tree_1.BinaryTree {
446
446
  const isRange = this.isRange(keyNodeEntryOrPredicate);
447
447
  // Set predicate based on parameter type
448
448
  if (isRange) {
449
- predicate = node => keyNodeEntryOrPredicate.isInRange(node.key, this._comparator);
449
+ predicate = node => {
450
+ if (!node)
451
+ return false;
452
+ return keyNodeEntryOrPredicate.isInRange(node.key, this._comparator);
453
+ };
450
454
  }
451
455
  else {
452
456
  predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
453
457
  }
454
- const isToLeftByRange = (cur) => {
458
+ const shouldVisitLeft = (cur) => {
459
+ if (!cur)
460
+ return false;
461
+ if (!this.isRealNode(cur.left))
462
+ return false;
455
463
  if (isRange) {
456
464
  const range = keyNodeEntryOrPredicate;
457
465
  const leftS = this.isReverse ? range.high : range.low;
458
466
  const leftI = this.isReverse ? range.includeHigh : range.includeLow;
459
467
  return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
460
468
  }
461
- return false;
469
+ if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
470
+ const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
471
+ return benchmarkKey !== null && benchmarkKey !== undefined && this._compare(cur.key, benchmarkKey) > 0;
472
+ }
473
+ return true;
462
474
  };
463
- const isToRightByRange = (cur) => {
475
+ const shouldVisitRight = (cur) => {
476
+ if (!cur)
477
+ return false;
478
+ if (!this.isRealNode(cur.right))
479
+ return false;
464
480
  if (isRange) {
465
481
  const range = keyNodeEntryOrPredicate;
466
482
  const rightS = this.isReverse ? range.low : range.high;
467
483
  const rightI = this.isReverse ? range.includeLow : range.includeLow;
468
484
  return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
469
485
  }
470
- return false;
471
- };
472
- const ans = [];
473
- if (iterationType === 'RECURSIVE') {
474
- const dfs = (cur) => {
475
- if (predicate(cur)) {
476
- ans.push(callback(cur));
477
- if (onlyOne)
478
- return;
479
- }
480
- if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
481
- return;
482
- if (isRange) {
483
- if (this.isRealNode(cur.left) && isToLeftByRange(cur))
484
- dfs(cur.left);
485
- if (this.isRealNode(cur.right) && isToRightByRange(cur))
486
- dfs(cur.right);
487
- }
488
- else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
489
- const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
490
- if (this.isRealNode(cur.left) &&
491
- benchmarkKey !== null &&
492
- benchmarkKey !== undefined &&
493
- this._compare(cur.key, benchmarkKey) > 0)
494
- dfs(cur.left);
495
- if (this.isRealNode(cur.right) &&
496
- benchmarkKey !== null &&
497
- benchmarkKey !== undefined &&
498
- this._compare(cur.key, benchmarkKey) < 0)
499
- dfs(cur.right);
500
- }
501
- else {
502
- if (this.isRealNode(cur.left))
503
- dfs(cur.left);
504
- if (this.isRealNode(cur.right))
505
- dfs(cur.right);
506
- }
507
- };
508
- dfs(startNode);
509
- }
510
- else {
511
- const stack = [startNode];
512
- while (stack.length > 0) {
513
- const cur = stack.pop();
514
- if (predicate(cur)) {
515
- ans.push(callback(cur));
516
- if (onlyOne)
517
- return ans;
518
- }
519
- if (isRange) {
520
- if (this.isRealNode(cur.left) && isToLeftByRange(cur))
521
- stack.push(cur.left);
522
- if (this.isRealNode(cur.right) && isToRightByRange(cur))
523
- stack.push(cur.right);
524
- }
525
- else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
526
- const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
527
- if (this.isRealNode(cur.right) &&
528
- benchmarkKey !== null &&
529
- benchmarkKey !== undefined &&
530
- this._compare(cur.key, benchmarkKey) < 0)
531
- stack.push(cur.right);
532
- if (this.isRealNode(cur.left) &&
533
- benchmarkKey !== null &&
534
- benchmarkKey !== undefined &&
535
- this._compare(cur.key, benchmarkKey) > 0)
536
- stack.push(cur.left);
537
- }
538
- else {
539
- if (this.isRealNode(cur.right))
540
- stack.push(cur.right);
541
- if (this.isRealNode(cur.left))
542
- stack.push(cur.left);
543
- }
486
+ if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
487
+ const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
488
+ return benchmarkKey !== null && benchmarkKey !== undefined && this._compare(cur.key, benchmarkKey) < 0;
544
489
  }
545
- }
546
- return ans;
490
+ return true;
491
+ };
492
+ return super._dfs(callback, 'IN', onlyOne, startNode, iterationType, false, shouldVisitLeft, shouldVisitRight, () => true, cur => {
493
+ if (cur)
494
+ return predicate(cur);
495
+ return false;
496
+ });
547
497
  }
548
498
  /**
549
499
  * Time Complexity: O(log n)
@@ -554,9 +504,9 @@ class BST extends binary_tree_1.BinaryTree {
554
504
  * either a `Range` object or an array of two elements representing the range boundaries.
555
505
  * @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
556
506
  * function that is used to process each node that is found within the specified range during the
557
- * search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the
507
+ * search operation. It is of type `NodeCallback<BSTNode<K, V> | null>`, where `BSTNode<K, V>` is the type of nodes in the
558
508
  * data structure.
559
- * @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch`
509
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `rangeSearch`
560
510
  * function represents the node from which the search for nodes within the specified range will
561
511
  * begin. It is the starting point for the range search operation.
562
512
  * @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
@@ -575,8 +525,8 @@ class BST extends binary_tree_1.BinaryTree {
575
525
  * Space Complexity: O(log n)
576
526
  *
577
527
  * This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure.
578
- * @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate`
579
- * parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`.
528
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate`
529
+ * parameter can be of type `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `R`, or `NodePredicate<BSTNode<K, V>>`.
580
530
  * @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method
581
531
  * is used to specify the starting point for searching nodes in the binary search tree. If no
582
532
  * specific starting point is provided, the default value is set to `this._root`, which is the root
@@ -598,24 +548,30 @@ class BST extends binary_tree_1.BinaryTree {
598
548
  * Time complexity: O(n)
599
549
  * Space complexity: O(n)
600
550
  *
601
- * The function overrides the depth-first search method and returns an array of the return types of
602
- * the callback function.
551
+ * The function `dfs` in TypeScript overrides the base class method with default parameters and
552
+ * returns the result of the super class `dfs` method.
603
553
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
604
- * during the depth-first search traversal. It is an optional parameter and defaults to
605
- * `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
606
- * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
607
- * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
608
- * take one of the following values:
609
- * @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
610
- * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
611
- * node entry. If not specified, the default value is the root of the tree.
612
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
613
- * type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
614
- * following values:
615
- * @returns The method is returning an array of the return type of the callback function.
554
+ * visited during the Depth-First Search traversal. It is a generic type `C` that extends the
555
+ * `NodeCallback` interface for `BSTNode<K, V>`. The default value for `callback` is `this._
556
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `override dfs` method
557
+ * specifies the order in which the Depth-First Search (DFS) traversal should be performed on the
558
+ * Binary Search Tree (BST). The possible values for the `pattern` parameter are:
559
+ * @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `override dfs` method is a
560
+ * boolean flag that indicates whether you want to stop the depth-first search traversal after
561
+ * finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set
562
+ * to `true`, the traversal will stop after finding
563
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} startNode -
564
+ * The `startNode` parameter in the `override dfs` method can be one of the following types:
565
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `override dfs` method
566
+ * specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
567
+ * Binary Search Tree (BST). It is used to determine the order in which nodes are visited during the
568
+ * traversal. The possible values for `
569
+ * @returns The `override` function is returning the result of calling the `dfs` method from the
570
+ * superclass, with the provided arguments `callback`, `pattern`, `onlyOne`, `startNode`, and
571
+ * `iterationType`. The return type is an array of the return type of the callback function `C`.
616
572
  */
617
- dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType) {
618
- return super.dfs(callback, pattern, startNode, iterationType);
573
+ dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
574
+ return super.dfs(callback, pattern, onlyOne, startNode, iterationType);
619
575
  }
620
576
  /**
621
577
  * Time complexity: O(n)
@@ -626,7 +582,7 @@ class BST extends binary_tree_1.BinaryTree {
626
582
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
627
583
  * visited during the breadth-first search. It should take a single argument, which is the current
628
584
  * node being visited, and it can return a value of any type.
629
- * @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
585
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
630
586
  * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
631
587
  * object. If no value is provided, the default value is the root of the tree.
632
588
  * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@@ -644,9 +600,9 @@ class BST extends binary_tree_1.BinaryTree {
644
600
  * The function overrides the listLevels method from the superclass and returns an array of arrays
645
601
  * containing the results of the callback function applied to each level of the tree.
646
602
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
647
- * `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the
603
+ * `NodeCallback<BSTNode<K, V> | null>`. It represents a callback function that will be called for each node in the
648
604
  * tree during the iteration process.
649
- * @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
605
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
650
606
  * point for listing the levels of the binary tree. It can be either a root node of the tree, a
651
607
  * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
652
608
  * value is provided, the root of
@@ -670,7 +626,7 @@ class BST extends binary_tree_1.BinaryTree {
670
626
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
671
627
  * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
672
628
  * 0, or 1, where:
673
- * @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in
629
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } targetNode - The `targetNode` parameter is the node in
674
630
  * the binary tree that you want to start traversing from. It can be specified either by providing
675
631
  * the key of the node, the node itself, or an entry containing the key and value of the node. If no
676
632
  * `targetNode` is provided,
@@ -741,9 +697,9 @@ class BST extends binary_tree_1.BinaryTree {
741
697
  return;
742
698
  const m = l + Math.floor((r - l) / 2);
743
699
  const midNode = sorted[m];
744
- if (this._isMapMode)
700
+ if (this._isMapMode && midNode !== null)
745
701
  this.add(midNode.key);
746
- else
702
+ else if (midNode !== null)
747
703
  this.add([midNode.key, midNode.value]);
748
704
  buildBalanceBST(l, m - 1);
749
705
  buildBalanceBST(m + 1, r);
@@ -760,9 +716,9 @@ class BST extends binary_tree_1.BinaryTree {
760
716
  if (l <= r) {
761
717
  const m = l + Math.floor((r - l) / 2);
762
718
  const midNode = sorted[m];
763
- if (this._isMapMode)
719
+ if (this._isMapMode && midNode !== null)
764
720
  this.add(midNode.key);
765
- else
721
+ else if (midNode !== null)
766
722
  this.add([midNode.key, midNode.value]);
767
723
  stack.push([m + 1, r]);
768
724
  stack.push([l, m - 1]);
@@ -875,8 +831,8 @@ class BST extends binary_tree_1.BinaryTree {
875
831
  * Space Complexity: O(1)
876
832
  *
877
833
  * The function overrides a method and converts a key, value pair or entry or raw element to a node.
878
- * @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of
879
- * type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw
834
+ * @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - A variable that can be of
835
+ * type R or K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined . It represents either a key, a node, an entry, or a raw
880
836
  * element.
881
837
  * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
882
838
  * value associated with a key in a key-value pair.
@@ -1,7 +1,8 @@
1
- import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, OptNodeOrNull, RBTNColor, RedBlackTreeOptions } from '../../types';
1
+ import type { BinaryTreeDeleteResult, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions } from '../../types';
2
2
  import { BST, BSTNode } from './bst';
3
3
  import { IBinaryTree } from '../../interfaces';
4
4
  export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
5
+ parent?: RedBlackTreeNode<K, V>;
5
6
  /**
6
7
  * The constructor initializes a node with a key, value, and color for a Red-Black Tree.
7
8
  * @param {K} key - The `key` parameter is a key of type `K` that is used to identify the node in a
@@ -13,24 +14,17 @@ export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
13
14
  * explicitly.
14
15
  */
15
16
  constructor(key: K, value?: V, color?: RBTNColor);
16
- parent?: RedBlackTreeNode<K, V>;
17
- _left?: OptNodeOrNull<RedBlackTreeNode<K, V>>;
18
- get left(): OptNodeOrNull<RedBlackTreeNode<K, V>>;
19
- set left(v: OptNodeOrNull<RedBlackTreeNode<K, V>>);
20
- _right?: OptNodeOrNull<RedBlackTreeNode<K, V>>;
21
- get right(): OptNodeOrNull<RedBlackTreeNode<K, V>>;
22
- set right(v: OptNodeOrNull<RedBlackTreeNode<K, V>>);
17
+ _left?: RedBlackTreeNode<K, V> | null | undefined;
18
+ get left(): RedBlackTreeNode<K, V> | null | undefined;
19
+ set left(v: RedBlackTreeNode<K, V> | null | undefined);
20
+ _right?: RedBlackTreeNode<K, V> | null | undefined;
21
+ get right(): RedBlackTreeNode<K, V> | null | undefined;
22
+ set right(v: RedBlackTreeNode<K, V> | null | undefined);
23
23
  }
24
24
  /**
25
25
  * 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
26
26
  * 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
27
27
  * @example
28
- * // Find elements in a range
29
- * const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]);
30
- * console.log(bst.search(new Range(5, 10))); // [5, 10, 7]
31
- * console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7]
32
- * console.log(bst.search(new Range(15, 20))); // [15, 18]
33
- * @example
34
28
  * // using Red-Black Tree as a price-based index for stock data
35
29
  * // Define the structure of individual stock records
36
30
  * interface StockRecord {
@@ -72,21 +66,21 @@ export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
72
66
  * [200, 400], // Price range
73
67
  * node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
74
68
  * );
75
- * console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
69
+ * console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
76
70
  */
77
71
  export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends BST<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> {
78
72
  /**
79
73
  * This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or
80
74
  * raw data.
81
75
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
82
- * iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It
76
+ * iterable that can contain either `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined` objects or `R` objects. It
83
77
  * is used to initialize the Red-Black Tree with keys, nodes, entries, or
84
78
  * @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K,
85
79
  * V, R>`. It is an optional parameter that allows you to specify additional options for the
86
80
  * RedBlackTree class. These options could include configuration settings, behavior customization, or
87
81
  * any other parameters that are specific to
88
82
  */
89
- constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, RedBlackTreeNode<K, V>> | R>, options?: RedBlackTreeOptions<K, V, R>);
83
+ constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>);
90
84
  protected _root: RedBlackTreeNode<K, V> | undefined;
91
85
  get root(): RedBlackTreeNode<K, V> | undefined;
92
86
  /**
@@ -122,12 +116,12 @@ export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = a
122
116
  * Space Complexity: O(1)
123
117
  *
124
118
  * The function checks if the input is an instance of the RedBlackTreeNode class.
125
- * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
126
- * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
119
+ * @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
120
+ * `keyNodeOrEntry` can be of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
127
121
  * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
128
122
  * an instance of the `RedBlackTreeNode` class.
129
123
  */
130
- isNode(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>): keyNodeOrEntry is RedBlackTreeNode<K, V>;
124
+ isNode(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is RedBlackTreeNode<K, V>;
131
125
  /**
132
126
  * Time Complexity: O(1)
133
127
  * Space Complexity: O(1)
@@ -142,8 +136,8 @@ export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = a
142
136
  *
143
137
  * The function adds a new node to a binary search tree and returns true if the node was successfully
144
138
  * added.
145
- * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
146
- * `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
139
+ * @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
140
+ * `keyNodeOrEntry` can accept a value of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
147
141
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
148
142
  * the key in the data structure. It represents the value that you want to add or update in the data
149
143
  * structure.
@@ -151,14 +145,14 @@ export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = a
151
145
  * the method returns true. If the node already exists and its value is updated, the method also
152
146
  * returns true. If the node cannot be added or updated, the method returns false.
153
147
  */
154
- add(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>, value?: V): boolean;
148
+ add(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
155
149
  /**
156
150
  * Time Complexity: O(log n)
157
151
  * Space Complexity: O(log n)
158
152
  *
159
153
  * The function overrides the delete method in a binary tree data structure to remove a node based on
160
154
  * a given predicate and maintain the binary search tree properties.
161
- * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
155
+ * @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
162
156
  * parameter in the `override delete` method is used to specify the condition or key based on which a
163
157
  * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
164
158
  * function that determines which node(s) should be deleted.
@@ -166,7 +160,7 @@ export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = a
166
160
  * objects. Each object in the array contains information about the deleted node and whether
167
161
  * balancing is needed.
168
162
  */
169
- delete(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[];
163
+ delete(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[];
170
164
  /**
171
165
  * Time Complexity: O(n)
172
166
  * Space Complexity: O(n)
@@ -44,12 +44,6 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
44
44
  * 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
45
45
  * 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
46
46
  * @example
47
- * // Find elements in a range
48
- * const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]);
49
- * console.log(bst.search(new Range(5, 10))); // [5, 10, 7]
50
- * console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7]
51
- * console.log(bst.search(new Range(15, 20))); // [15, 18]
52
- * @example
53
47
  * // using Red-Black Tree as a price-based index for stock data
54
48
  * // Define the structure of individual stock records
55
49
  * interface StockRecord {
@@ -91,14 +85,14 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
91
85
  * [200, 400], // Price range
92
86
  * node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
93
87
  * );
94
- * console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
88
+ * console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
95
89
  */
96
90
  class RedBlackTree extends bst_1.BST {
97
91
  /**
98
92
  * This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or
99
93
  * raw data.
100
94
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
101
- * iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It
95
+ * iterable that can contain either `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined` objects or `R` objects. It
102
96
  * is used to initialize the Red-Black Tree with keys, nodes, entries, or
103
97
  * @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K,
104
98
  * V, R>`. It is an optional parameter that allows you to specify additional options for the
@@ -152,8 +146,8 @@ class RedBlackTree extends bst_1.BST {
152
146
  * Space Complexity: O(1)
153
147
  *
154
148
  * The function checks if the input is an instance of the RedBlackTreeNode class.
155
- * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
156
- * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
149
+ * @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
150
+ * `keyNodeOrEntry` can be of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
157
151
  * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
158
152
  * an instance of the `RedBlackTreeNode` class.
159
153
  */
@@ -177,8 +171,8 @@ class RedBlackTree extends bst_1.BST {
177
171
  *
178
172
  * The function adds a new node to a binary search tree and returns true if the node was successfully
179
173
  * added.
180
- * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
181
- * `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
174
+ * @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
175
+ * `keyNodeOrEntry` can accept a value of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
182
176
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
183
177
  * the key in the data structure. It represents the value that you want to add or update in the data
184
178
  * structure.
@@ -217,7 +211,7 @@ class RedBlackTree extends bst_1.BST {
217
211
  *
218
212
  * The function overrides the delete method in a binary tree data structure to remove a node based on
219
213
  * a given predicate and maintain the binary search tree properties.
220
- * @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
214
+ * @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
221
215
  * parameter in the `override delete` method is used to specify the condition or key based on which a
222
216
  * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
223
217
  * function that determines which node(s) should be deleted.