data-structure-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 (162) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/benchmark/report.html +2 -2
  3. package/benchmark/report.json +12 -18
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
  11. package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  17. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  19. package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
  20. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  21. package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
  22. package/dist/cjs/data-structures/hash/hash-map.js +10 -4
  23. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  24. package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
  25. package/dist/cjs/data-structures/hash/hash-table.js +50 -5
  26. package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/heap.d.ts +25 -22
  28. package/dist/cjs/data-structures/heap/heap.js +101 -41
  29. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  30. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
  31. package/dist/cjs/data-structures/heap/max-heap.js +2 -2
  32. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  33. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
  34. package/dist/cjs/data-structures/heap/min-heap.js +2 -2
  35. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  36. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  37. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +125 -119
  38. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  39. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  40. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +65 -60
  41. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  42. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  43. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  44. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  45. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  46. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  47. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  48. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  49. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  50. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  51. package/dist/cjs/data-structures/queue/deque.d.ts +50 -49
  52. package/dist/cjs/data-structures/queue/deque.js +81 -71
  53. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  54. package/dist/cjs/data-structures/queue/queue.d.ts +46 -0
  55. package/dist/cjs/data-structures/queue/queue.js +80 -0
  56. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  57. package/dist/cjs/data-structures/stack/stack.d.ts +20 -6
  58. package/dist/cjs/data-structures/stack/stack.js +65 -8
  59. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  60. package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
  61. package/dist/cjs/data-structures/trie/trie.js +47 -0
  62. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  63. package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
  64. package/dist/cjs/types/common.d.ts +2 -0
  65. package/dist/cjs/types/common.js.map +1 -1
  66. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  67. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  68. package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
  69. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  70. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
  71. package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
  72. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
  73. package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
  74. package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
  75. package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
  76. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
  77. package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
  78. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  79. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
  80. package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
  81. package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
  82. package/dist/mjs/data-structures/hash/hash-map.js +10 -4
  83. package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
  84. package/dist/mjs/data-structures/hash/hash-table.js +50 -5
  85. package/dist/mjs/data-structures/heap/heap.d.ts +25 -22
  86. package/dist/mjs/data-structures/heap/heap.js +102 -42
  87. package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
  88. package/dist/mjs/data-structures/heap/max-heap.js +2 -2
  89. package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
  90. package/dist/mjs/data-structures/heap/min-heap.js +2 -2
  91. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  92. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -119
  93. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  94. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +65 -60
  95. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  96. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
  97. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  98. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
  99. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
  100. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  101. package/dist/mjs/data-structures/queue/deque.d.ts +50 -49
  102. package/dist/mjs/data-structures/queue/deque.js +81 -71
  103. package/dist/mjs/data-structures/queue/queue.d.ts +46 -0
  104. package/dist/mjs/data-structures/queue/queue.js +80 -0
  105. package/dist/mjs/data-structures/stack/stack.d.ts +20 -6
  106. package/dist/mjs/data-structures/stack/stack.js +65 -8
  107. package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
  108. package/dist/mjs/data-structures/trie/trie.js +47 -0
  109. package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
  110. package/dist/mjs/types/common.d.ts +2 -0
  111. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  112. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  113. package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
  114. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  115. package/dist/umd/data-structure-typed.js +856 -509
  116. package/dist/umd/data-structure-typed.min.js +2 -2
  117. package/dist/umd/data-structure-typed.min.js.map +1 -1
  118. package/package.json +1 -1
  119. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  120. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  121. package/src/data-structures/binary-tree/bst.ts +67 -47
  122. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  123. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  124. package/src/data-structures/graph/abstract-graph.ts +1 -1
  125. package/src/data-structures/hash/hash-map.ts +13 -7
  126. package/src/data-structures/hash/hash-table.ts +59 -9
  127. package/src/data-structures/heap/heap.ts +115 -46
  128. package/src/data-structures/heap/max-heap.ts +5 -5
  129. package/src/data-structures/heap/min-heap.ts +5 -5
  130. package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
  131. package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
  132. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  133. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  134. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  135. package/src/data-structures/queue/deque.ts +86 -75
  136. package/src/data-structures/queue/queue.ts +88 -0
  137. package/src/data-structures/stack/stack.ts +75 -10
  138. package/src/data-structures/trie/trie.ts +53 -0
  139. package/src/interfaces/binary-tree.ts +13 -1
  140. package/src/types/common.ts +5 -1
  141. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  142. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  143. package/src/types/data-structures/heap/heap.ts +3 -1
  144. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
  145. package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
  146. package/test/performance/data-structures/heap/heap.test.ts +14 -5
  147. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
  148. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
  149. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
  150. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
  151. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
  152. package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
  153. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  154. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  155. package/test/unit/data-structures/heap/min-heap.test.ts +49 -1
  156. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
  157. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
  158. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
  159. package/test/unit/data-structures/queue/queue.test.ts +37 -0
  160. package/test/unit/data-structures/stack/stack.test.ts +55 -5
  161. package/test/unit/data-structures/trie/trie.test.ts +33 -0
  162. package/test/unit/unrestricted-interconversion.test.ts +100 -0
@@ -92,19 +92,21 @@ export class BinaryTreeNode {
92
92
  * @template N - The type of the binary tree's nodes.
93
93
  */
94
94
  export class BinaryTree {
95
- options;
95
+ iterationType = IterationType.ITERATIVE;
96
96
  /**
97
97
  * Creates a new instance of BinaryTree.
98
98
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
99
99
  */
100
- constructor(options) {
100
+ constructor(elements, options) {
101
101
  if (options) {
102
- this.options = { iterationType: IterationType.ITERATIVE, ...options };
103
- }
104
- else {
105
- this.options = { iterationType: IterationType.ITERATIVE };
102
+ const { iterationType } = options;
103
+ if (iterationType) {
104
+ this.iterationType = iterationType;
105
+ }
106
106
  }
107
107
  this._size = 0;
108
+ if (elements)
109
+ this.init(elements);
108
110
  }
109
111
  _root;
110
112
  /**
@@ -130,13 +132,8 @@ export class BinaryTree {
130
132
  return new BinaryTreeNode(key, value);
131
133
  }
132
134
  createTree(options) {
133
- return new BinaryTree({ ...this.options, ...options });
135
+ return new BinaryTree([], { iterationType: this.iterationType, ...options });
134
136
  }
135
- /**
136
- * Time Complexity: O(n)
137
- * Space Complexity: O(1)
138
- * 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.
139
- */
140
137
  /**
141
138
  * Time Complexity: O(n)
142
139
  * Space Complexity: O(1)
@@ -197,8 +194,9 @@ export class BinaryTree {
197
194
  return inserted;
198
195
  }
199
196
  /**
200
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
197
+ * Time Complexity: O(n)
201
198
  * Space Complexity: O(1)
199
+ * 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.
202
200
  */
203
201
  /**
204
202
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
@@ -247,10 +245,6 @@ export class BinaryTree {
247
245
  this.clear();
248
246
  return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
249
247
  }
250
- /**
251
- * Time Complexity: O(n)
252
- * Space Complexity: O(1)
253
- */
254
248
  /**
255
249
  * Time Complexity: O(n)
256
250
  * Space Complexity: O(1)
@@ -346,8 +340,7 @@ export class BinaryTree {
346
340
  }
347
341
  /**
348
342
  * Time Complexity: O(n)
349
- * Space Complexity: O(log n)
350
- * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
343
+ * Space Complexity: O(1)
351
344
  */
352
345
  /**
353
346
  * Time Complexity: O(n)
@@ -363,7 +356,7 @@ export class BinaryTree {
363
356
  * values:
364
357
  * @returns the height of the binary tree.
365
358
  */
366
- getHeight(beginRoot = this.root, iterationType = this.options.iterationType) {
359
+ getHeight(beginRoot = this.root, iterationType = this.iterationType) {
367
360
  beginRoot = this.ensureNotKey(beginRoot);
368
361
  if (!beginRoot)
369
362
  return -1;
@@ -409,7 +402,7 @@ export class BinaryTree {
409
402
  * to calculate the minimum height of a binary tree. It can have two possible values:
410
403
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
411
404
  */
412
- getMinHeight(beginRoot = this.root, iterationType = this.options.iterationType) {
405
+ getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
413
406
  beginRoot = this.ensureNotKey(beginRoot);
414
407
  if (!beginRoot)
415
408
  return -1;
@@ -456,6 +449,7 @@ export class BinaryTree {
456
449
  /**
457
450
  * Time Complexity: O(n)
458
451
  * Space Complexity: O(log n)
452
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
459
453
  */
460
454
  /**
461
455
  * Time Complexity: O(n)
@@ -471,10 +465,6 @@ export class BinaryTree {
471
465
  isPerfectlyBalanced(beginRoot = this.root) {
472
466
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
473
467
  }
474
- /**
475
- * Time Complexity: O(n)
476
- * Space Complexity: O(log n).
477
- */
478
468
  /**
479
469
  * Time Complexity: O(n)
480
470
  * Space Complexity: O(log n).
@@ -500,7 +490,7 @@ export class BinaryTree {
500
490
  * traverse the binary tree. It can have two possible values:
501
491
  * @returns an array of nodes of type `N`.
502
492
  */
503
- getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.options.iterationType) {
493
+ getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
504
494
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
505
495
  callback = (node => node);
506
496
  beginRoot = this.ensureNotKey(beginRoot);
@@ -538,10 +528,6 @@ export class BinaryTree {
538
528
  }
539
529
  return ans;
540
530
  }
541
- /**
542
- * Time Complexity: O(n)
543
- * Space Complexity: O(log n).
544
- */
545
531
  /**
546
532
  * Time Complexity: O(n)
547
533
  *
@@ -562,15 +548,11 @@ export class BinaryTree {
562
548
  * be performed in a pre-order, in-order, or post-order manner.
563
549
  * @returns a boolean value.
564
550
  */
565
- has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
551
+ has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
566
552
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
567
553
  callback = (node => node);
568
554
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
569
555
  }
570
- /**
571
- * Time Complexity: O(n)
572
- * Space Complexity: O(log n)
573
- */
574
556
  /**
575
557
  * Time Complexity: O(n)
576
558
  * Space Complexity: O(log n)
@@ -592,7 +574,7 @@ export class BinaryTree {
592
574
  * nodes are visited during the search.
593
575
  * @returns a value of type `N | null | undefined`.
594
576
  */
595
- getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
577
+ getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
596
578
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
597
579
  callback = (node => node);
598
580
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
@@ -644,6 +626,10 @@ export class BinaryTree {
644
626
  }
645
627
  }
646
628
  }
629
+ /**
630
+ * Time Complexity: O(n)
631
+ * Space Complexity: O(log n)
632
+ */
647
633
  /**
648
634
  * The function `ensureNotKey` returns the node corresponding to the given key if it is a valid node
649
635
  * key, otherwise it returns the key itself.
@@ -658,10 +644,6 @@ export class BinaryTree {
658
644
  ensureNotKey(key, iterationType = IterationType.ITERATIVE) {
659
645
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
660
646
  }
661
- /**
662
- * Time Complexity: O(n)
663
- * Space Complexity: O(log n)
664
- */
665
647
  /**
666
648
  * Time Complexity: O(n)
667
649
  * Space Complexity: O(log n)
@@ -684,11 +666,15 @@ export class BinaryTree {
684
666
  * @returns The value of the node with the given identifier is being returned. If the node is not
685
667
  * found, `undefined` is returned.
686
668
  */
687
- get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
669
+ get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
688
670
  if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
689
671
  callback = (node => node);
690
672
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
691
673
  }
674
+ /**
675
+ * Time Complexity: O(n)
676
+ * Space Complexity: O(log n)
677
+ */
692
678
  /**
693
679
  * Clear the binary tree, removing all nodes.
694
680
  */
@@ -703,10 +689,6 @@ export class BinaryTree {
703
689
  isEmpty() {
704
690
  return this.size === 0;
705
691
  }
706
- /**
707
- * Time Complexity: O(log n)
708
- * Space Complexity: O(log n)
709
- */
710
692
  /**
711
693
  * Time Complexity: O(log n)
712
694
  * Space Complexity: O(log n)
@@ -738,7 +720,7 @@ export class BinaryTree {
738
720
  }
739
721
  /**
740
722
  * Time Complexity: O(log n)
741
- * Space Complexity: O(1)
723
+ * Space Complexity: O(log n)
742
724
  */
743
725
  /**
744
726
  * Time Complexity: O(log n)
@@ -754,7 +736,7 @@ export class BinaryTree {
754
736
  * @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
755
737
  * is no leftmost node, it returns `null` or `undefined` depending on the input.
756
738
  */
757
- getLeftMost(beginRoot = this.root, iterationType = this.options.iterationType) {
739
+ getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
758
740
  beginRoot = this.ensureNotKey(beginRoot);
759
741
  if (!beginRoot)
760
742
  return beginRoot;
@@ -795,7 +777,7 @@ export class BinaryTree {
795
777
  * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
796
778
  * is no rightmost node, it returns `null` or `undefined`, depending on the input.
797
779
  */
798
- getRightMost(beginRoot = this.root, iterationType = this.options.iterationType) {
780
+ getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
799
781
  // TODO support get right most by passing key in
800
782
  beginRoot = this.ensureNotKey(beginRoot);
801
783
  if (!beginRoot)
@@ -819,7 +801,7 @@ export class BinaryTree {
819
801
  }
820
802
  }
821
803
  /**
822
- * Time Complexity: O(n)
804
+ * Time Complexity: O(log n)
823
805
  * Space Complexity: O(1)
824
806
  */
825
807
  /**
@@ -834,7 +816,7 @@ export class BinaryTree {
834
816
  * possible values:
835
817
  * @returns a boolean value.
836
818
  */
837
- isSubtreeBST(beginRoot, iterationType = this.options.iterationType) {
819
+ isSubtreeBST(beginRoot, iterationType = this.iterationType) {
838
820
  // TODO there is a bug
839
821
  beginRoot = this.ensureNotKey(beginRoot);
840
822
  if (!beginRoot)
@@ -881,15 +863,11 @@ export class BinaryTree {
881
863
  * expected to be
882
864
  * @returns a boolean value.
883
865
  */
884
- isBST(iterationType = this.options.iterationType) {
866
+ isBST(iterationType = this.iterationType) {
885
867
  if (this.root === null)
886
868
  return true;
887
869
  return this.isSubtreeBST(this.root, iterationType);
888
870
  }
889
- /**
890
- * Time complexity: O(n)
891
- * Space complexity: O(log n)
892
- */
893
871
  /**
894
872
  * Time complexity: O(n)
895
873
  * Space complexity: O(log n)
@@ -912,7 +890,7 @@ export class BinaryTree {
912
890
  * the `callback` function on each node in the subtree. The type of the array elements is determined
913
891
  * by the return type of the `callback` function.
914
892
  */
915
- subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
893
+ subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
916
894
  beginRoot = this.ensureNotKey(beginRoot);
917
895
  const ans = [];
918
896
  if (!beginRoot)
@@ -952,6 +930,10 @@ export class BinaryTree {
952
930
  }
953
931
  return ans;
954
932
  }
933
+ /**
934
+ * Time complexity: O(n)
935
+ * Space complexity: O(log n)
936
+ */
955
937
  /**
956
938
  * The function checks if a given node is a real node by verifying if it is an instance of
957
939
  * BinaryTreeNode and its key is not NaN.
@@ -986,10 +968,6 @@ export class BinaryTree {
986
968
  isNodeKey(potentialKey) {
987
969
  return typeof potentialKey === 'number';
988
970
  }
989
- /**
990
- * Time complexity: O(n)
991
- * Space complexity: O(n)
992
- */
993
971
  /**
994
972
  * Time complexity: O(n)
995
973
  * Space complexity: O(n)
@@ -1119,10 +1097,6 @@ export class BinaryTree {
1119
1097
  }
1120
1098
  return ans;
1121
1099
  }
1122
- /**
1123
- * Time complexity: O(n)
1124
- * Space complexity: O(n)
1125
- */
1126
1100
  /**
1127
1101
  * Time complexity: O(n)
1128
1102
  * Space complexity: O(n)
@@ -1144,7 +1118,7 @@ export class BinaryTree {
1144
1118
  * @returns an array of values that are the result of invoking the callback function on each node in
1145
1119
  * the breadth-first traversal of a binary tree.
1146
1120
  */
1147
- bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
1121
+ bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1148
1122
  beginRoot = this.ensureNotKey(beginRoot);
1149
1123
  if (!beginRoot)
1150
1124
  return [];
@@ -1196,10 +1170,6 @@ export class BinaryTree {
1196
1170
  }
1197
1171
  return ans;
1198
1172
  }
1199
- /**
1200
- * Time complexity: O(n)
1201
- * Space complexity: O(n)
1202
- */
1203
1173
  /**
1204
1174
  * Time complexity: O(n)
1205
1175
  * Space complexity: O(n)
@@ -1221,7 +1191,7 @@ export class BinaryTree {
1221
1191
  * be excluded
1222
1192
  * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1223
1193
  */
1224
- listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType, includeNull = false) {
1194
+ listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1225
1195
  beginRoot = this.ensureNotKey(beginRoot);
1226
1196
  const levelsNodes = [];
1227
1197
  if (!beginRoot)
@@ -1313,10 +1283,6 @@ export class BinaryTree {
1313
1283
  }
1314
1284
  return y;
1315
1285
  }
1316
- /**
1317
- * Time complexity: O(n)
1318
- * Space complexity: O(1)
1319
- */
1320
1286
  /**
1321
1287
  * Time complexity: O(n)
1322
1288
  * Space complexity: O(1)
@@ -1420,6 +1386,10 @@ export class BinaryTree {
1420
1386
  }
1421
1387
  return ans;
1422
1388
  }
1389
+ /**
1390
+ * Time complexity: O(n)
1391
+ * Space complexity: O(1)
1392
+ */
1423
1393
  /**
1424
1394
  * The `forEach` function iterates over each entry in a tree and calls a callback function with the
1425
1395
  * entry and the tree as arguments.
@@ -1448,14 +1418,6 @@ export class BinaryTree {
1448
1418
  }
1449
1419
  return newTree;
1450
1420
  }
1451
- // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1452
- // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1453
- // const newTree = this.createTree();
1454
- // for (const [key, value] of this) {
1455
- // newTree.add(key, callback([key, value], this));
1456
- // }
1457
- // return newTree;
1458
- // }
1459
1421
  /**
1460
1422
  * The `map` function creates a new tree by applying a callback function to each entry in the current
1461
1423
  * tree.
@@ -1469,6 +1431,14 @@ export class BinaryTree {
1469
1431
  }
1470
1432
  return newTree;
1471
1433
  }
1434
+ // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1435
+ // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1436
+ // const newTree = this.createTree();
1437
+ // for (const [key, value] of this) {
1438
+ // newTree.add(key, callback([key, value], this));
1439
+ // }
1440
+ // return newTree;
1441
+ // }
1472
1442
  /**
1473
1443
  * The `reduce` function iterates over the entries of a tree and applies a callback function to each
1474
1444
  * entry, accumulating a single value.
@@ -1500,7 +1470,7 @@ export class BinaryTree {
1500
1470
  *[Symbol.iterator](node = this.root) {
1501
1471
  if (!node)
1502
1472
  return;
1503
- if (this.options.iterationType === IterationType.ITERATIVE) {
1473
+ if (this.iterationType === IterationType.ITERATIVE) {
1504
1474
  const stack = [];
1505
1475
  let current = node;
1506
1476
  while (current || stack.length > 0) {
@@ -1554,6 +1524,19 @@ export class BinaryTree {
1554
1524
  };
1555
1525
  display(beginRoot);
1556
1526
  }
1527
+ init(elements) {
1528
+ if (elements) {
1529
+ for (const entryOrKey of elements) {
1530
+ if (Array.isArray(entryOrKey)) {
1531
+ const [key, value] = entryOrKey;
1532
+ this.add(key, value);
1533
+ }
1534
+ else {
1535
+ this.add(entryOrKey);
1536
+ }
1537
+ }
1538
+ }
1539
+ }
1557
1540
  _displayAux(node, options) {
1558
1541
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
1559
1542
  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