priority-queue-typed 1.47.4 → 1.47.6

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 (72) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +8 -8
  2. package/dist/data-structures/binary-tree/avl-tree.js +23 -15
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
  4. package/dist/data-structures/binary-tree/binary-tree.js +66 -82
  5. package/dist/data-structures/binary-tree/bst.d.ts +38 -37
  6. package/dist/data-structures/binary-tree/bst.js +56 -40
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
  8. package/dist/data-structures/binary-tree/rb-tree.js +26 -17
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  10. package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
  11. package/dist/data-structures/graph/abstract-graph.js +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +3 -3
  13. package/dist/data-structures/hash/hash-map.js +10 -4
  14. package/dist/data-structures/hash/hash-table.d.ts +9 -4
  15. package/dist/data-structures/hash/hash-table.js +50 -5
  16. package/dist/data-structures/heap/heap.d.ts +25 -22
  17. package/dist/data-structures/heap/heap.js +101 -41
  18. package/dist/data-structures/heap/max-heap.d.ts +2 -5
  19. package/dist/data-structures/heap/max-heap.js +2 -2
  20. package/dist/data-structures/heap/min-heap.d.ts +2 -5
  21. package/dist/data-structures/heap/min-heap.js +2 -2
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +125 -119
  24. package/dist/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  25. package/dist/data-structures/linked-list/singly-linked-list.js +65 -60
  26. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  27. package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
  28. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  29. package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
  30. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
  31. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  32. package/dist/data-structures/queue/deque.d.ts +50 -49
  33. package/dist/data-structures/queue/deque.js +81 -71
  34. package/dist/data-structures/queue/queue.d.ts +46 -0
  35. package/dist/data-structures/queue/queue.js +80 -0
  36. package/dist/data-structures/stack/stack.d.ts +20 -6
  37. package/dist/data-structures/stack/stack.js +65 -8
  38. package/dist/data-structures/trie/trie.d.ts +5 -0
  39. package/dist/data-structures/trie/trie.js +47 -0
  40. package/dist/interfaces/binary-tree.d.ts +3 -1
  41. package/dist/types/common.d.ts +2 -0
  42. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  43. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  44. package/dist/types/data-structures/heap/heap.d.ts +4 -1
  45. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  46. package/package.json +2 -2
  47. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  48. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  49. package/src/data-structures/binary-tree/bst.ts +67 -47
  50. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  51. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  52. package/src/data-structures/graph/abstract-graph.ts +1 -1
  53. package/src/data-structures/hash/hash-map.ts +13 -7
  54. package/src/data-structures/hash/hash-table.ts +59 -9
  55. package/src/data-structures/heap/heap.ts +115 -46
  56. package/src/data-structures/heap/max-heap.ts +5 -5
  57. package/src/data-structures/heap/min-heap.ts +5 -5
  58. package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
  59. package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
  60. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  61. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  62. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  63. package/src/data-structures/queue/deque.ts +86 -75
  64. package/src/data-structures/queue/queue.ts +88 -0
  65. package/src/data-structures/stack/stack.ts +75 -10
  66. package/src/data-structures/trie/trie.ts +53 -0
  67. package/src/interfaces/binary-tree.ts +13 -1
  68. package/src/types/common.ts +5 -1
  69. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  70. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  71. package/src/types/data-structures/heap/heap.ts +3 -1
  72. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
@@ -86,15 +86,18 @@ class BinaryTree {
86
86
  * Creates a new instance of BinaryTree.
87
87
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
88
88
  */
89
- constructor(options) {
89
+ constructor(elements, options) {
90
+ this.iterationType = types_1.IterationType.ITERATIVE;
90
91
  this._defaultOneParamCallback = (node) => node.key;
91
92
  if (options) {
92
- this.options = Object.assign({ iterationType: types_1.IterationType.ITERATIVE }, options);
93
- }
94
- else {
95
- this.options = { iterationType: types_1.IterationType.ITERATIVE };
93
+ const { iterationType } = options;
94
+ if (iterationType) {
95
+ this.iterationType = iterationType;
96
+ }
96
97
  }
97
98
  this._size = 0;
99
+ if (elements)
100
+ this.init(elements);
98
101
  }
99
102
  /**
100
103
  * Get the root node of the binary tree.
@@ -118,13 +121,8 @@ class BinaryTree {
118
121
  return new BinaryTreeNode(key, value);
119
122
  }
120
123
  createTree(options) {
121
- return new BinaryTree(Object.assign(Object.assign({}, this.options), options));
124
+ return new BinaryTree([], Object.assign({ iterationType: this.iterationType }, options));
122
125
  }
123
- /**
124
- * Time Complexity: O(n)
125
- * Space Complexity: O(1)
126
- * Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
127
- */
128
126
  /**
129
127
  * Time Complexity: O(n)
130
128
  * Space Complexity: O(1)
@@ -185,8 +183,9 @@ class BinaryTree {
185
183
  return inserted;
186
184
  }
187
185
  /**
188
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
186
+ * Time Complexity: O(n)
189
187
  * Space Complexity: O(1)
188
+ * Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
190
189
  */
191
190
  /**
192
191
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
@@ -235,10 +234,6 @@ class BinaryTree {
235
234
  this.clear();
236
235
  return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
237
236
  }
238
- /**
239
- * Time Complexity: O(n)
240
- * Space Complexity: O(1)
241
- */
242
237
  /**
243
238
  * Time Complexity: O(n)
244
239
  * Space Complexity: O(1)
@@ -334,8 +329,7 @@ class BinaryTree {
334
329
  }
335
330
  /**
336
331
  * Time Complexity: O(n)
337
- * Space Complexity: O(log n)
338
- * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
332
+ * Space Complexity: O(1)
339
333
  */
340
334
  /**
341
335
  * Time Complexity: O(n)
@@ -351,7 +345,7 @@ class BinaryTree {
351
345
  * values:
352
346
  * @returns the height of the binary tree.
353
347
  */
354
- getHeight(beginRoot = this.root, iterationType = this.options.iterationType) {
348
+ getHeight(beginRoot = this.root, iterationType = this.iterationType) {
355
349
  beginRoot = this.ensureNotKey(beginRoot);
356
350
  if (!beginRoot)
357
351
  return -1;
@@ -397,7 +391,7 @@ class BinaryTree {
397
391
  * to calculate the minimum height of a binary tree. It can have two possible values:
398
392
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
399
393
  */
400
- getMinHeight(beginRoot = this.root, iterationType = this.options.iterationType) {
394
+ getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
401
395
  var _a, _b, _c;
402
396
  beginRoot = this.ensureNotKey(beginRoot);
403
397
  if (!beginRoot)
@@ -445,6 +439,7 @@ class BinaryTree {
445
439
  /**
446
440
  * Time Complexity: O(n)
447
441
  * Space Complexity: O(log n)
442
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
448
443
  */
449
444
  /**
450
445
  * Time Complexity: O(n)
@@ -460,10 +455,6 @@ class BinaryTree {
460
455
  isPerfectlyBalanced(beginRoot = this.root) {
461
456
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
462
457
  }
463
- /**
464
- * Time Complexity: O(n)
465
- * Space Complexity: O(log n).
466
- */
467
458
  /**
468
459
  * Time Complexity: O(n)
469
460
  * Space Complexity: O(log n).
@@ -489,7 +480,7 @@ class BinaryTree {
489
480
  * traverse the binary tree. It can have two possible values:
490
481
  * @returns an array of nodes of type `N`.
491
482
  */
492
- getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.options.iterationType) {
483
+ getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
493
484
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
494
485
  callback = (node => node);
495
486
  beginRoot = this.ensureNotKey(beginRoot);
@@ -527,10 +518,6 @@ class BinaryTree {
527
518
  }
528
519
  return ans;
529
520
  }
530
- /**
531
- * Time Complexity: O(n)
532
- * Space Complexity: O(log n).
533
- */
534
521
  /**
535
522
  * Time Complexity: O(n)
536
523
  *
@@ -551,15 +538,11 @@ class BinaryTree {
551
538
  * be performed in a pre-order, in-order, or post-order manner.
552
539
  * @returns a boolean value.
553
540
  */
554
- has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
541
+ has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
555
542
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
556
543
  callback = (node => node);
557
544
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
558
545
  }
559
- /**
560
- * Time Complexity: O(n)
561
- * Space Complexity: O(log n)
562
- */
563
546
  /**
564
547
  * Time Complexity: O(n)
565
548
  * Space Complexity: O(log n)
@@ -581,7 +564,7 @@ class BinaryTree {
581
564
  * nodes are visited during the search.
582
565
  * @returns a value of type `N | null | undefined`.
583
566
  */
584
- getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
567
+ getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
585
568
  var _a;
586
569
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
587
570
  callback = (node => node);
@@ -634,6 +617,10 @@ class BinaryTree {
634
617
  }
635
618
  }
636
619
  }
620
+ /**
621
+ * Time Complexity: O(n)
622
+ * Space Complexity: O(log n)
623
+ */
637
624
  /**
638
625
  * The function `ensureNotKey` returns the node corresponding to the given key if it is a valid node
639
626
  * key, otherwise it returns the key itself.
@@ -648,10 +635,6 @@ class BinaryTree {
648
635
  ensureNotKey(key, iterationType = types_1.IterationType.ITERATIVE) {
649
636
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
650
637
  }
651
- /**
652
- * Time Complexity: O(n)
653
- * Space Complexity: O(log n)
654
- */
655
638
  /**
656
639
  * Time Complexity: O(n)
657
640
  * Space Complexity: O(log n)
@@ -674,12 +657,16 @@ class BinaryTree {
674
657
  * @returns The value of the node with the given identifier is being returned. If the node is not
675
658
  * found, `undefined` is returned.
676
659
  */
677
- get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
660
+ get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
678
661
  var _a, _b;
679
662
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
680
663
  callback = (node => node);
681
664
  return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined;
682
665
  }
666
+ /**
667
+ * Time Complexity: O(n)
668
+ * Space Complexity: O(log n)
669
+ */
683
670
  /**
684
671
  * Clear the binary tree, removing all nodes.
685
672
  */
@@ -694,10 +681,6 @@ class BinaryTree {
694
681
  isEmpty() {
695
682
  return this.size === 0;
696
683
  }
697
- /**
698
- * Time Complexity: O(log n)
699
- * Space Complexity: O(log n)
700
- */
701
684
  /**
702
685
  * Time Complexity: O(log n)
703
686
  * Space Complexity: O(log n)
@@ -729,7 +712,7 @@ class BinaryTree {
729
712
  }
730
713
  /**
731
714
  * Time Complexity: O(log n)
732
- * Space Complexity: O(1)
715
+ * Space Complexity: O(log n)
733
716
  */
734
717
  /**
735
718
  * Time Complexity: O(log n)
@@ -745,7 +728,7 @@ class BinaryTree {
745
728
  * @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
746
729
  * is no leftmost node, it returns `null` or `undefined` depending on the input.
747
730
  */
748
- getLeftMost(beginRoot = this.root, iterationType = this.options.iterationType) {
731
+ getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
749
732
  beginRoot = this.ensureNotKey(beginRoot);
750
733
  if (!beginRoot)
751
734
  return beginRoot;
@@ -786,7 +769,7 @@ class BinaryTree {
786
769
  * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
787
770
  * is no rightmost node, it returns `null` or `undefined`, depending on the input.
788
771
  */
789
- getRightMost(beginRoot = this.root, iterationType = this.options.iterationType) {
772
+ getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
790
773
  // TODO support get right most by passing key in
791
774
  beginRoot = this.ensureNotKey(beginRoot);
792
775
  if (!beginRoot)
@@ -810,7 +793,7 @@ class BinaryTree {
810
793
  }
811
794
  }
812
795
  /**
813
- * Time Complexity: O(n)
796
+ * Time Complexity: O(log n)
814
797
  * Space Complexity: O(1)
815
798
  */
816
799
  /**
@@ -825,7 +808,7 @@ class BinaryTree {
825
808
  * possible values:
826
809
  * @returns a boolean value.
827
810
  */
828
- isSubtreeBST(beginRoot, iterationType = this.options.iterationType) {
811
+ isSubtreeBST(beginRoot, iterationType = this.iterationType) {
829
812
  // TODO there is a bug
830
813
  beginRoot = this.ensureNotKey(beginRoot);
831
814
  if (!beginRoot)
@@ -872,15 +855,11 @@ class BinaryTree {
872
855
  * expected to be
873
856
  * @returns a boolean value.
874
857
  */
875
- isBST(iterationType = this.options.iterationType) {
858
+ isBST(iterationType = this.iterationType) {
876
859
  if (this.root === null)
877
860
  return true;
878
861
  return this.isSubtreeBST(this.root, iterationType);
879
862
  }
880
- /**
881
- * Time complexity: O(n)
882
- * Space complexity: O(log n)
883
- */
884
863
  /**
885
864
  * Time complexity: O(n)
886
865
  * Space complexity: O(log n)
@@ -903,7 +882,7 @@ class BinaryTree {
903
882
  * the `callback` function on each node in the subtree. The type of the array elements is determined
904
883
  * by the return type of the `callback` function.
905
884
  */
906
- subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
885
+ subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
907
886
  beginRoot = this.ensureNotKey(beginRoot);
908
887
  const ans = [];
909
888
  if (!beginRoot)
@@ -943,6 +922,10 @@ class BinaryTree {
943
922
  }
944
923
  return ans;
945
924
  }
925
+ /**
926
+ * Time complexity: O(n)
927
+ * Space complexity: O(log n)
928
+ */
946
929
  /**
947
930
  * The function checks if a given node is a real node by verifying if it is an instance of
948
931
  * BinaryTreeNode and its key is not NaN.
@@ -977,10 +960,6 @@ class BinaryTree {
977
960
  isNodeKey(potentialKey) {
978
961
  return typeof potentialKey === 'number';
979
962
  }
980
- /**
981
- * Time complexity: O(n)
982
- * Space complexity: O(n)
983
- */
984
963
  /**
985
964
  * Time complexity: O(n)
986
965
  * Space complexity: O(n)
@@ -1110,10 +1089,6 @@ class BinaryTree {
1110
1089
  }
1111
1090
  return ans;
1112
1091
  }
1113
- /**
1114
- * Time complexity: O(n)
1115
- * Space complexity: O(n)
1116
- */
1117
1092
  /**
1118
1093
  * Time complexity: O(n)
1119
1094
  * Space complexity: O(n)
@@ -1135,7 +1110,7 @@ class BinaryTree {
1135
1110
  * @returns an array of values that are the result of invoking the callback function on each node in
1136
1111
  * the breadth-first traversal of a binary tree.
1137
1112
  */
1138
- bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
1113
+ bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1139
1114
  beginRoot = this.ensureNotKey(beginRoot);
1140
1115
  if (!beginRoot)
1141
1116
  return [];
@@ -1187,10 +1162,6 @@ class BinaryTree {
1187
1162
  }
1188
1163
  return ans;
1189
1164
  }
1190
- /**
1191
- * Time complexity: O(n)
1192
- * Space complexity: O(n)
1193
- */
1194
1165
  /**
1195
1166
  * Time complexity: O(n)
1196
1167
  * Space complexity: O(n)
@@ -1212,7 +1183,7 @@ class BinaryTree {
1212
1183
  * be excluded
1213
1184
  * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1214
1185
  */
1215
- listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
1186
+ listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1216
1187
  beginRoot = this.ensureNotKey(beginRoot);
1217
1188
  const levelsNodes = [];
1218
1189
  if (!beginRoot)
@@ -1304,10 +1275,6 @@ class BinaryTree {
1304
1275
  }
1305
1276
  return y;
1306
1277
  }
1307
- /**
1308
- * Time complexity: O(n)
1309
- * Space complexity: O(1)
1310
- */
1311
1278
  /**
1312
1279
  * Time complexity: O(n)
1313
1280
  * Space complexity: O(1)
@@ -1411,6 +1378,10 @@ class BinaryTree {
1411
1378
  }
1412
1379
  return ans;
1413
1380
  }
1381
+ /**
1382
+ * Time complexity: O(n)
1383
+ * Space complexity: O(1)
1384
+ */
1414
1385
  /**
1415
1386
  * The `forEach` function iterates over each entry in a tree and calls a callback function with the
1416
1387
  * entry and the tree as arguments.
@@ -1439,14 +1410,6 @@ class BinaryTree {
1439
1410
  }
1440
1411
  return newTree;
1441
1412
  }
1442
- // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1443
- // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1444
- // const newTree = this.createTree();
1445
- // for (const [key, value] of this) {
1446
- // newTree.add(key, callback([key, value], this));
1447
- // }
1448
- // return newTree;
1449
- // }
1450
1413
  /**
1451
1414
  * The `map` function creates a new tree by applying a callback function to each entry in the current
1452
1415
  * tree.
@@ -1460,6 +1423,14 @@ class BinaryTree {
1460
1423
  }
1461
1424
  return newTree;
1462
1425
  }
1426
+ // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1427
+ // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1428
+ // const newTree = this.createTree();
1429
+ // for (const [key, value] of this) {
1430
+ // newTree.add(key, callback([key, value], this));
1431
+ // }
1432
+ // return newTree;
1433
+ // }
1463
1434
  /**
1464
1435
  * The `reduce` function iterates over the entries of a tree and applies a callback function to each
1465
1436
  * entry, accumulating a single value.
@@ -1491,7 +1462,7 @@ class BinaryTree {
1491
1462
  *[Symbol.iterator](node = this.root) {
1492
1463
  if (!node)
1493
1464
  return;
1494
- if (this.options.iterationType === types_1.IterationType.ITERATIVE) {
1465
+ if (this.iterationType === types_1.IterationType.ITERATIVE) {
1495
1466
  const stack = [];
1496
1467
  let current = node;
1497
1468
  while (current || stack.length > 0) {
@@ -1545,6 +1516,19 @@ class BinaryTree {
1545
1516
  };
1546
1517
  display(beginRoot);
1547
1518
  }
1519
+ init(elements) {
1520
+ if (elements) {
1521
+ for (const entryOrKey of elements) {
1522
+ if (Array.isArray(entryOrKey)) {
1523
+ const [key, value] = entryOrKey;
1524
+ this.add(key, value);
1525
+ }
1526
+ else {
1527
+ this.add(entryOrKey);
1528
+ }
1529
+ }
1530
+ }
1531
+ }
1548
1532
  _displayAux(node, options) {
1549
1533
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
1550
1534
  const emptyDisplayLayout = [['─'], 1, 0, 0];
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, BTNKey } from '../../types';
9
- import { CP, IterationType } from '../../types';
8
+ import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, BTNKey, Comparator } from '../../types';
9
+ import { CP, IterableEntriesOrKeys, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
12
12
  export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
@@ -34,18 +34,18 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
34
34
  set right(v: N | undefined);
35
35
  }
36
36
  export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>, TREE extends BST<V, N, TREE> = BST<V, N, BSTNested<V, N>>> extends BinaryTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
37
- options: BSTOptions;
38
37
  /**
39
38
  * The constructor function initializes a binary search tree with an optional comparator function.
40
39
  * @param {BSTOptions} [options] - An optional object that contains additional configuration options
41
40
  * for the binary search tree.
42
41
  */
43
- constructor(options?: BSTOptions);
42
+ constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<BSTOptions>);
44
43
  protected _root?: N;
45
44
  /**
46
45
  * Get the root node of the binary tree.
47
46
  */
48
47
  get root(): N | undefined;
48
+ comparator: Comparator<BTNKey>;
49
49
  /**
50
50
  * The function creates a new binary search tree node with the given key and value.
51
51
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
@@ -55,11 +55,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
55
55
  * @returns a new instance of the BSTNode class with the specified key and value.
56
56
  */
57
57
  createNode(key: BTNKey, value?: V): N;
58
- createTree(options?: BSTOptions): TREE;
59
- /**
60
- * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
61
- * Space Complexity: O(1) - Constant space is used.
62
- */
58
+ createTree(options?: Partial<BSTOptions>): TREE;
63
59
  /**
64
60
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
65
61
  * Space Complexity: O(1) - Constant space is used.
@@ -74,8 +70,8 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
74
70
  */
75
71
  add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
76
72
  /**
77
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
78
- * Space Complexity: O(n) - Additional space is required for the sorted array.
73
+ * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
74
+ * Space Complexity: O(1) - Constant space is used.
79
75
  */
80
76
  /**
81
77
  * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
@@ -97,10 +93,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
97
93
  * current instance of the binary search tree
98
94
  * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
99
95
  */
100
- addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType | undefined): (N | undefined)[];
96
+ addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
101
97
  /**
102
- * Time Complexity: O(log n) - Average case for a balanced tree.
103
- * Space Complexity: O(1) - Constant space is used.
98
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
99
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
104
100
  */
105
101
  /**
106
102
  * Time Complexity: O(log n) - Average case for a balanced tree.
@@ -117,10 +113,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
117
113
  * the key of the leftmost node if the comparison result is greater than, and the key of the
118
114
  * rightmost node otherwise. If no node is found, it returns 0.
119
115
  */
120
- lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType | undefined): BTNKey;
116
+ lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): BTNKey;
121
117
  /**
122
118
  * Time Complexity: O(log n) - Average case for a balanced tree.
123
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
119
+ * Space Complexity: O(1) - Constant space is used.
124
120
  */
125
121
  /**
126
122
  * Time Complexity: O(log n) - Average case for a balanced tree.
@@ -137,6 +133,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
137
133
  * found in the binary tree. If no node is found, it returns `undefined`.
138
134
  */
139
135
  getNodeByKey(key: BTNKey, iterationType?: IterationType): N | undefined;
136
+ /**
137
+ * Time Complexity: O(log n) - Average case for a balanced tree.
138
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
139
+ */
140
140
  /**
141
141
  * The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
142
142
  * otherwise it returns the key itself.
@@ -147,10 +147,6 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
147
147
  * @returns either a node object (N) or undefined.
148
148
  */
149
149
  ensureNotKey(key: BTNKey | N | undefined, iterationType?: IterationType): N | undefined;
150
- /**
151
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
152
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
153
- */
154
150
  /**
155
151
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
156
152
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -174,7 +170,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
174
170
  * performed on the binary tree. It can have two possible values:
175
171
  * @returns The method returns an array of nodes (`N[]`).
176
172
  */
177
- getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType | undefined): N[];
173
+ getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): N[];
178
174
  /**
179
175
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
180
176
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -200,19 +196,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
200
196
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
201
197
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
202
198
  */
203
- lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType | undefined): ReturnType<C>[];
204
- /**
205
- * Balancing Adjustment:
206
- * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
207
- * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
208
- *
209
- * Use Cases and Efficiency:
210
- * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
211
- * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
212
- */
199
+ lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType): ReturnType<C>[];
213
200
  /**
214
- * Time Complexity: O(n) - Building a balanced tree from a sorted array.
215
- * Space Complexity: O(n) - Additional space is required for the sorted array.
201
+ * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
202
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
216
203
  */
217
204
  /**
218
205
  * Time Complexity: O(n) - Building a balanced tree from a sorted array.
@@ -225,10 +212,19 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
225
212
  * values:
226
213
  * @returns The function `perfectlyBalance` returns a boolean value.
227
214
  */
228
- perfectlyBalance(iterationType?: IterationType | undefined): boolean;
215
+ perfectlyBalance(iterationType?: IterationType): boolean;
229
216
  /**
230
- * Time Complexity: O(n) - Visiting each node once.
231
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
217
+ * Balancing Adjustment:
218
+ * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
219
+ * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
220
+ *
221
+ * Use Cases and Efficiency:
222
+ * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
223
+ * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
224
+ */
225
+ /**
226
+ * Time Complexity: O(n) - Building a balanced tree from a sorted array.
227
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
232
228
  */
233
229
  /**
234
230
  * Time Complexity: O(n) - Visiting each node once.
@@ -239,7 +235,12 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
239
235
  * to check if the AVL tree is balanced. It can have two possible values:
240
236
  * @returns a boolean value.
241
237
  */
242
- isAVLBalanced(iterationType?: IterationType | undefined): boolean;
238
+ isAVLBalanced(iterationType?: IterationType): boolean;
239
+ /**
240
+ * Time Complexity: O(n) - Visiting each node once.
241
+ * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
242
+ */
243
+ init(elements: IterableEntriesOrKeys<V>): void;
243
244
  protected _setRoot(v: N | undefined): void;
244
245
  /**
245
246
  * The function compares two values using a comparator function and returns whether the first value