data-structure-typed 1.49.5 → 1.49.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +17 -23
  3. package/README_zh-CN.md +2 -0
  4. package/benchmark/report.html +14 -23
  5. package/benchmark/report.json +158 -251
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
  7. package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js +194 -153
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
  13. package/dist/cjs/data-structures/binary-tree/bst.js +114 -91
  14. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
  19. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -51
  20. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  21. package/dist/cjs/data-structures/hash/hash-map.d.ts +24 -27
  22. package/dist/cjs/data-structures/hash/hash-map.js +35 -35
  23. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  24. package/dist/cjs/data-structures/hash/index.d.ts +0 -1
  25. package/dist/cjs/data-structures/hash/index.js +0 -1
  26. package/dist/cjs/data-structures/hash/index.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/heap.d.ts +2 -1
  28. package/dist/cjs/data-structures/heap/heap.js +13 -13
  29. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  30. package/dist/cjs/data-structures/heap/max-heap.js +1 -1
  31. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  32. package/dist/cjs/data-structures/heap/min-heap.js +1 -1
  33. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  34. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +1 -1
  35. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  36. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +1 -3
  37. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  38. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -8
  39. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +15 -18
  40. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  41. package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -7
  42. package/dist/cjs/data-structures/matrix/matrix.js +0 -7
  43. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  44. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +1 -1
  45. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  46. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +1 -1
  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.js +1 -1
  49. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  50. package/dist/cjs/data-structures/queue/deque.d.ts +2 -11
  51. package/dist/cjs/data-structures/queue/deque.js +9 -13
  52. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  53. package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
  54. package/dist/cjs/data-structures/queue/queue.js +29 -25
  55. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  56. package/dist/cjs/data-structures/stack/stack.js +2 -3
  57. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  58. package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
  59. package/dist/cjs/data-structures/trie/trie.js +9 -5
  60. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  61. package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
  62. package/dist/cjs/types/common.d.ts +3 -3
  63. package/dist/cjs/types/common.js +2 -2
  64. package/dist/cjs/types/common.js.map +1 -1
  65. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  66. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
  67. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  68. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +5 -2
  69. package/dist/cjs/types/data-structures/hash/index.d.ts +0 -1
  70. package/dist/cjs/types/data-structures/hash/index.js +0 -1
  71. package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
  72. package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
  73. package/dist/cjs/types/data-structures/linked-list/index.d.ts +1 -0
  74. package/dist/cjs/types/data-structures/linked-list/index.js +1 -0
  75. package/dist/cjs/types/data-structures/linked-list/index.js.map +1 -1
  76. package/dist/cjs/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
  77. package/dist/cjs/types/data-structures/matrix/index.d.ts +1 -0
  78. package/dist/cjs/types/data-structures/matrix/index.js +1 -0
  79. package/dist/cjs/types/data-structures/matrix/index.js.map +1 -1
  80. package/dist/cjs/types/data-structures/matrix/matrix.d.ts +7 -1
  81. package/dist/cjs/types/data-structures/queue/deque.d.ts +3 -1
  82. package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -1
  83. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
  84. package/dist/mjs/data-structures/binary-tree/avl-tree.js +55 -49
  85. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
  86. package/dist/mjs/data-structures/binary-tree/binary-tree.js +194 -153
  87. package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
  88. package/dist/mjs/data-structures/binary-tree/bst.js +114 -91
  89. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
  90. package/dist/mjs/data-structures/binary-tree/rb-tree.js +62 -59
  91. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
  92. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +58 -51
  93. package/dist/mjs/data-structures/hash/hash-map.d.ts +24 -27
  94. package/dist/mjs/data-structures/hash/hash-map.js +35 -37
  95. package/dist/mjs/data-structures/hash/index.d.ts +0 -1
  96. package/dist/mjs/data-structures/hash/index.js +0 -1
  97. package/dist/mjs/data-structures/heap/heap.d.ts +2 -1
  98. package/dist/mjs/data-structures/heap/heap.js +19 -20
  99. package/dist/mjs/data-structures/heap/max-heap.js +1 -1
  100. package/dist/mjs/data-structures/heap/min-heap.js +1 -1
  101. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +1 -1
  102. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +2 -5
  103. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -8
  104. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +16 -23
  105. package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -7
  106. package/dist/mjs/data-structures/matrix/matrix.js +0 -7
  107. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +1 -1
  108. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +1 -1
  109. package/dist/mjs/data-structures/priority-queue/priority-queue.js +1 -1
  110. package/dist/mjs/data-structures/queue/deque.d.ts +2 -11
  111. package/dist/mjs/data-structures/queue/deque.js +9 -14
  112. package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
  113. package/dist/mjs/data-structures/queue/queue.js +30 -28
  114. package/dist/mjs/data-structures/stack/stack.js +3 -5
  115. package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
  116. package/dist/mjs/data-structures/trie/trie.js +10 -9
  117. package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
  118. package/dist/mjs/types/common.d.ts +3 -3
  119. package/dist/mjs/types/common.js +2 -2
  120. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  121. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +1 -1
  122. package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  123. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +5 -2
  124. package/dist/mjs/types/data-structures/hash/index.d.ts +0 -1
  125. package/dist/mjs/types/data-structures/hash/index.js +0 -1
  126. package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
  127. package/dist/mjs/types/data-structures/linked-list/index.d.ts +1 -0
  128. package/dist/mjs/types/data-structures/linked-list/index.js +1 -0
  129. package/dist/mjs/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
  130. package/dist/mjs/types/data-structures/matrix/index.d.ts +1 -0
  131. package/dist/mjs/types/data-structures/matrix/index.js +1 -0
  132. package/dist/mjs/types/data-structures/matrix/matrix.d.ts +7 -1
  133. package/dist/mjs/types/data-structures/queue/deque.d.ts +3 -1
  134. package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -1
  135. package/dist/umd/data-structure-typed.js +620 -823
  136. package/dist/umd/data-structure-typed.min.js +2 -2
  137. package/dist/umd/data-structure-typed.min.js.map +1 -1
  138. package/package.json +1 -1
  139. package/src/data-structures/binary-tree/avl-tree.ts +58 -53
  140. package/src/data-structures/binary-tree/binary-tree.ts +255 -211
  141. package/src/data-structures/binary-tree/bst.ts +126 -107
  142. package/src/data-structures/binary-tree/rb-tree.ts +66 -64
  143. package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
  144. package/src/data-structures/hash/hash-map.ts +46 -50
  145. package/src/data-structures/hash/index.ts +0 -1
  146. package/src/data-structures/heap/heap.ts +20 -19
  147. package/src/data-structures/heap/max-heap.ts +1 -1
  148. package/src/data-structures/heap/min-heap.ts +1 -1
  149. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  150. package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
  151. package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
  152. package/src/data-structures/matrix/matrix.ts +2 -10
  153. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  154. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  155. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  156. package/src/data-structures/queue/deque.ts +11 -15
  157. package/src/data-structures/queue/queue.ts +29 -28
  158. package/src/data-structures/stack/stack.ts +3 -6
  159. package/src/data-structures/trie/trie.ts +10 -11
  160. package/src/interfaces/binary-tree.ts +3 -3
  161. package/src/types/common.ts +3 -3
  162. package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
  163. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  164. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
  165. package/src/types/data-structures/hash/hash-map.ts +6 -2
  166. package/src/types/data-structures/hash/index.ts +0 -1
  167. package/src/types/data-structures/heap/heap.ts +1 -1
  168. package/src/types/data-structures/linked-list/index.ts +1 -0
  169. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
  170. package/src/types/data-structures/matrix/index.ts +1 -0
  171. package/src/types/data-structures/matrix/matrix.ts +7 -1
  172. package/src/types/data-structures/queue/deque.ts +1 -1
  173. package/src/types/data-structures/trie/trie.ts +1 -1
  174. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
  175. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
  176. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
  177. package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
  178. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -15
  179. package/test/performance/data-structures/comparison/comparison.test.ts +13 -36
  180. package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
  181. package/test/performance/data-structures/hash/hash-map.test.ts +11 -34
  182. package/test/performance/data-structures/heap/heap.test.ts +5 -18
  183. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
  184. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
  185. package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
  186. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
  187. package/test/performance/data-structures/queue/queue.test.ts +8 -25
  188. package/test/performance/data-structures/stack/stack.test.ts +6 -18
  189. package/test/performance/data-structures/trie/trie.test.ts +2 -6
  190. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -5
  191. package/test/unit/data-structures/binary-tree/bst.test.ts +17 -1
  192. package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
  193. package/test/unit/data-structures/queue/deque.test.ts +5 -5
  194. package/test/unit/data-structures/trie/trie.test.ts +1 -1
  195. package/dist/cjs/data-structures/hash/hash-table.d.ts +0 -108
  196. package/dist/cjs/data-structures/hash/hash-table.js +0 -282
  197. package/dist/cjs/data-structures/hash/hash-table.js.map +0 -1
  198. package/dist/cjs/types/data-structures/hash/hash-table.d.ts +0 -1
  199. package/dist/cjs/types/data-structures/hash/hash-table.js +0 -3
  200. package/dist/cjs/types/data-structures/hash/hash-table.js.map +0 -1
  201. package/dist/cjs/types/data-structures/matrix/matrix2d.d.ts +0 -1
  202. package/dist/cjs/types/data-structures/matrix/matrix2d.js +0 -3
  203. package/dist/cjs/types/data-structures/matrix/matrix2d.js.map +0 -1
  204. package/dist/cjs/types/data-structures/matrix/vector2d.d.ts +0 -1
  205. package/dist/cjs/types/data-structures/matrix/vector2d.js +0 -3
  206. package/dist/cjs/types/data-structures/matrix/vector2d.js.map +0 -1
  207. package/dist/mjs/data-structures/hash/hash-table.d.ts +0 -108
  208. package/dist/mjs/data-structures/hash/hash-table.js +0 -283
  209. package/dist/mjs/types/data-structures/hash/hash-table.d.ts +0 -1
  210. package/dist/mjs/types/data-structures/hash/hash-table.js +0 -1
  211. package/dist/mjs/types/data-structures/matrix/matrix2d.d.ts +0 -1
  212. package/dist/mjs/types/data-structures/matrix/matrix2d.js +0 -1
  213. package/dist/mjs/types/data-structures/matrix/vector2d.d.ts +0 -1
  214. package/dist/mjs/types/data-structures/matrix/vector2d.js +0 -1
  215. package/src/data-structures/hash/hash-table.ts +0 -318
  216. package/src/types/data-structures/hash/hash-table.ts +0 -1
  217. package/src/types/data-structures/matrix/matrix2d.ts +0 -1
  218. package/src/types/data-structures/matrix/vector2d.ts +0 -1
  219. package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
  220. package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
  221. package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
  222. package/test/unit/data-structures/hash/hash-table.test.ts +0 -238
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNExemplar, BTNKeyOrNode } from '../../types';
8
+ import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
9
9
  import { BSTVariant, CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
@@ -45,13 +45,13 @@ export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTN
45
45
  export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>> extends BinaryTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
46
46
  /**
47
47
  * This is the constructor function for a binary search tree class in TypeScript, which initializes
48
- * the tree with optional elements and options.
49
- * @param [elements] - An optional iterable of BTNExemplar objects that will be added to the
48
+ * the tree with optional keysOrNodesOrEntries and options.
49
+ * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
50
50
  * binary search tree.
51
51
  * @param [options] - The `options` parameter is an optional object that can contain additional
52
52
  * configuration options for the binary search tree. It can have the following properties:
53
53
  */
54
- constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>);
54
+ constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: BSTOptions<K>);
55
55
  protected _root?: N;
56
56
  get root(): N | undefined;
57
57
  protected _variant: BSTVariant;
@@ -74,27 +74,53 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
74
74
  */
75
75
  createTree(options?: Partial<BSTOptions<K>>): TREE;
76
76
  /**
77
- * The function checks if an exemplar is an instance of BSTNode.
78
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
79
- * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
80
- */
81
- isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
82
- /**
83
- * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
77
+ * The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
84
78
  * otherwise it returns undefined.
85
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
79
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
86
80
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
87
- * `exemplarToNode` function. It represents the value associated with the exemplar node.
81
+ * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
88
82
  * @returns a node of type N or undefined.
89
83
  */
90
- exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined;
84
+ exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined;
85
+ /**
86
+ * Time Complexity: O(log n)
87
+ * Space Complexity: O(log n)
88
+ * Average case for a balanced tree. Space for the recursive call stack in the worst case.
89
+ */
90
+ /**
91
+ * Time Complexity: O(log n)
92
+ * Space Complexity: O(log n)
93
+ *
94
+ * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
95
+ * otherwise it returns the key itself.
96
+ * @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
97
+ * `undefined`.
98
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
99
+ * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
100
+ * @returns either a node object (N) or undefined.
101
+ */
102
+ ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | undefined;
103
+ /**
104
+ * The function "isNotNodeInstance" checks if a potential key is a K.
105
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
106
+ * data type.
107
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
108
+ */
109
+ isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
110
+ /**
111
+ * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
112
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
113
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
114
+ */
115
+ isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
91
116
  /**
92
- * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
93
- * Space Complexity: O(1) - Constant space is used.
117
+ * Time Complexity: O(log n)
118
+ * Space Complexity: O(1)
119
+ * - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
94
120
  */
95
121
  /**
96
- * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
97
- * Space Complexity: O(1) - Constant space is used.
122
+ * Time Complexity: O(log n)
123
+ * Space Complexity: O(1)
98
124
  *
99
125
  * The `add` function adds a new node to a binary tree, updating the value if the key already exists
100
126
  * or inserting a new node if the key is unique.
@@ -104,14 +130,15 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
104
130
  * @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
105
131
  * node was not added.
106
132
  */
107
- add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined;
133
+ add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean;
108
134
  /**
109
- * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
110
- * Space Complexity: O(k) - Additional space is required for the sorted array.
135
+ * Time Complexity: O(k log n)
136
+ * Space Complexity: O(k)
137
+ * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
111
138
  */
112
139
  /**
113
- * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
114
- * Space Complexity: O(k) - Additional space is required for the sorted array.
140
+ * Time Complexity: O(k log n)
141
+ * Space Complexity: O(k)
115
142
  *
116
143
  * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
117
144
  * balancing the tree after each addition.
@@ -122,41 +149,40 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
122
149
  * order. If not provided, undefined will be assigned as the value for each key or node.
123
150
  * @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
124
151
  * balanced or not. If set to true, the add operation will be balanced using a binary search tree
125
- * algorithm. If set to false, the add operation will not be balanced and the elements will be added
152
+ * algorithm. If set to false, the add operation will not be balanced and the nodes will be added
126
153
  * in the order they appear in the input.
127
154
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
128
155
  * type of iteration to use when adding multiple keys or nodes. It has a default value of
129
156
  * `this.iterationType`, which suggests that it is a property of the current object.
130
157
  * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
131
158
  */
132
- addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
159
+ addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
133
160
  /**
134
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
135
- * Space Complexity: O(n) - Additional space is required for the sorted array.
161
+ * Time Complexity: O(n log n)
162
+ * Space Complexity: O(n)
163
+ * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
136
164
  */
137
165
  /**
138
- * Time Complexity: O(log n) - Average case for a balanced tree.
139
- * Space Complexity: O(1) - Constant space is used.
166
+ * Time Complexity: O(n log n)
167
+ * Space Complexity: O(n)
140
168
  *
141
169
  * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
142
170
  * leftmost node if the comparison result is greater than.
143
171
  * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
144
172
  * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
145
173
  * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
146
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
147
- * be performed. It can have one of the following values:
148
174
  * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
149
175
  * the key of the leftmost node if the comparison result is greater than, and the key of the
150
176
  * rightmost node otherwise. If no node is found, it returns 0.
151
177
  */
152
- lastKey(beginRoot?: BSTNKeyOrNode<K, N>): K | undefined;
178
+ lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, N>): K | undefined;
153
179
  /**
154
- * Time Complexity: O(log n) - Average case for a balanced tree.
155
- * Space Complexity: O(1) - Constant space is used.
180
+ * Time Complexity: O(log n)
181
+ * Space Complexity: O(1)
156
182
  */
157
183
  /**
158
- * Time Complexity: O(log n) - Average case for a balanced tree.
159
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
184
+ * Time Complexity: O(log n)
185
+ * Space Complexity: O(1)
160
186
  *
161
187
  * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
162
188
  * either recursive or iterative methods.
@@ -170,29 +196,14 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
170
196
  */
171
197
  getNodeByKey(key: K, iterationType?: IterationType): N | undefined;
172
198
  /**
173
- * The function "isNotNodeInstance" checks if a potential key is a K.
174
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
175
- * data type.
176
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
177
- */
178
- isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K;
179
- /**
180
- * Time Complexity: O(log n) - Average case for a balanced tree.
181
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
182
- */
183
- /**
184
- * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
185
- * otherwise it returns the key itself.
186
- * @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
187
- * `undefined`.
188
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
189
- * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
190
- * @returns either a node object (N) or undefined.
191
- */
192
- ensureNode(key: BSTNKeyOrNode<K, N>, iterationType?: IterationType): N | undefined;
193
- /**
194
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
195
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
199
+ * Time Complexity: O(log n)
200
+ * Space Complexity: O(log n)
201
+ * Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
202
+ * /
203
+
204
+ /**
205
+ * Time Complexity: O(log n)
206
+ * Space Complexity: O(log n)
196
207
  *
197
208
  * The function `getNodes` returns an array of nodes that match a given identifier, using either a
198
209
  * recursive or iterative approach.
@@ -213,14 +224,15 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
213
224
  * performed on the binary tree. It can have two possible values:
214
225
  * @returns The method returns an array of nodes (`N[]`).
215
226
  */
216
- getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNKeyOrNode<K, N>, iterationType?: IterationType): N[];
227
+ getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N[];
217
228
  /**
218
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
219
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
229
+ * Time Complexity: O(log n)
230
+ * Space Complexity: O(log n)
231
+ * Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
220
232
  */
221
233
  /**
222
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
223
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
234
+ * Time Complexity: O(log n)
235
+ * Space Complexity: O(log n)
224
236
  *
225
237
  * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
226
238
  * are either lesser or greater than a target node, depending on the specified comparison type.
@@ -239,14 +251,14 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
239
251
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
240
252
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
241
253
  */
242
- lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNKeyOrNode<K, N>, iterationType?: IterationType): ReturnType<C>[];
254
+ lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[];
243
255
  /**
244
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
245
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
256
+ * Time Complexity: O(log n)
257
+ * Space Complexity: O(log n)
246
258
  */
247
259
  /**
248
- * Time Complexity: O(n) - Building a balanced tree from a sorted array.
249
- * Space Complexity: O(n) - Additional space is required for the sorted array.
260
+ * Time Complexity: O(log n)
261
+ * Space Complexity: O(log n)
250
262
  *
251
263
  * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
252
264
  * ensures the tree is perfectly balanced.
@@ -270,8 +282,8 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
270
282
  * Space Complexity: O(n) - Additional space is required for the sorted array.
271
283
  */
272
284
  /**
273
- * Time Complexity: O(n) - Visiting each node once.
274
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
285
+ * Time Complexity: O(n)
286
+ * Space Complexity: O(log n)
275
287
  *
276
288
  * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
277
289
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
@@ -56,29 +56,28 @@ export class BSTNode extends BinaryTreeNode {
56
56
  export class BST extends BinaryTree {
57
57
  /**
58
58
  * This is the constructor function for a binary search tree class in TypeScript, which initializes
59
- * the tree with optional elements and options.
60
- * @param [elements] - An optional iterable of BTNExemplar objects that will be added to the
59
+ * the tree with optional keysOrNodesOrEntries and options.
60
+ * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
61
61
  * binary search tree.
62
62
  * @param [options] - The `options` parameter is an optional object that can contain additional
63
63
  * configuration options for the binary search tree. It can have the following properties:
64
64
  */
65
- constructor(elements, options) {
65
+ constructor(keysOrNodesOrEntries = [], options) {
66
66
  super([], options);
67
67
  if (options) {
68
68
  const { variant } = options;
69
- if (variant) {
69
+ if (variant)
70
70
  this._variant = variant;
71
- }
72
71
  }
73
72
  this._root = undefined;
74
- if (elements)
75
- this.addMany(elements);
73
+ if (keysOrNodesOrEntries)
74
+ this.addMany(keysOrNodesOrEntries);
76
75
  }
77
76
  _root;
78
77
  get root() {
79
78
  return this._root;
80
79
  }
81
- _variant = BSTVariant.MIN;
80
+ _variant = BSTVariant.STANDARD;
82
81
  get variant() {
83
82
  return this._variant;
84
83
  }
@@ -108,31 +107,23 @@ export class BST extends BinaryTree {
108
107
  });
109
108
  }
110
109
  /**
111
- * The function checks if an exemplar is an instance of BSTNode.
112
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
113
- * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
114
- */
115
- isNode(exemplar) {
116
- return exemplar instanceof BSTNode;
117
- }
118
- /**
119
- * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
110
+ * The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
120
111
  * otherwise it returns undefined.
121
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
112
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
122
113
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
123
- * `exemplarToNode` function. It represents the value associated with the exemplar node.
114
+ * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
124
115
  * @returns a node of type N or undefined.
125
116
  */
126
- exemplarToNode(exemplar, value) {
117
+ exemplarToNode(keyOrNodeOrEntry, value) {
127
118
  let node;
128
- if (exemplar === null || exemplar === undefined) {
119
+ if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
129
120
  return;
130
121
  }
131
- else if (this.isNode(exemplar)) {
132
- node = exemplar;
122
+ else if (this.isNode(keyOrNodeOrEntry)) {
123
+ node = keyOrNodeOrEntry;
133
124
  }
134
- else if (this.isEntry(exemplar)) {
135
- const [key, value] = exemplar;
125
+ else if (this.isEntry(keyOrNodeOrEntry)) {
126
+ const [key, value] = keyOrNodeOrEntry;
136
127
  if (key === undefined || key === null) {
137
128
  return;
138
129
  }
@@ -140,8 +131,8 @@ export class BST extends BinaryTree {
140
131
  node = this.createNode(key, value);
141
132
  }
142
133
  }
143
- else if (this.isNotNodeInstance(exemplar)) {
144
- node = this.createNode(exemplar, value);
134
+ else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
135
+ node = this.createNode(keyOrNodeOrEntry, value);
145
136
  }
146
137
  else {
147
138
  return;
@@ -149,12 +140,62 @@ export class BST extends BinaryTree {
149
140
  return node;
150
141
  }
151
142
  /**
152
- * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
153
- * Space Complexity: O(1) - Constant space is used.
143
+ * Time Complexity: O(log n)
144
+ * Space Complexity: O(log n)
145
+ * Average case for a balanced tree. Space for the recursive call stack in the worst case.
146
+ */
147
+ /**
148
+ * Time Complexity: O(log n)
149
+ * Space Complexity: O(log n)
150
+ *
151
+ * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
152
+ * otherwise it returns the key itself.
153
+ * @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
154
+ * `undefined`.
155
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
156
+ * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
157
+ * @returns either a node object (N) or undefined.
158
+ */
159
+ ensureNode(keyOrNodeOrEntry, iterationType = IterationType.ITERATIVE) {
160
+ let res;
161
+ if (this.isRealNode(keyOrNodeOrEntry)) {
162
+ res = keyOrNodeOrEntry;
163
+ }
164
+ else if (this.isEntry(keyOrNodeOrEntry)) {
165
+ if (keyOrNodeOrEntry[0])
166
+ res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
167
+ }
168
+ else {
169
+ if (keyOrNodeOrEntry)
170
+ res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
171
+ }
172
+ return res;
173
+ }
174
+ /**
175
+ * The function "isNotNodeInstance" checks if a potential key is a K.
176
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
177
+ * data type.
178
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
179
+ */
180
+ isNotNodeInstance(potentialKey) {
181
+ return !(potentialKey instanceof BSTNode);
182
+ }
183
+ /**
184
+ * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
185
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
186
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
187
+ */
188
+ isNode(keyOrNodeOrEntry) {
189
+ return keyOrNodeOrEntry instanceof BSTNode;
190
+ }
191
+ /**
192
+ * Time Complexity: O(log n)
193
+ * Space Complexity: O(1)
194
+ * - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
154
195
  */
155
196
  /**
156
- * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
157
- * Space Complexity: O(1) - Constant space is used.
197
+ * Time Complexity: O(log n)
198
+ * Space Complexity: O(1)
158
199
  *
159
200
  * The `add` function adds a new node to a binary tree, updating the value if the key already exists
160
201
  * or inserting a new node if the key is unique.
@@ -167,11 +208,11 @@ export class BST extends BinaryTree {
167
208
  add(keyOrNodeOrEntry, value) {
168
209
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
169
210
  if (newNode === undefined)
170
- return;
211
+ return false;
171
212
  if (this.root === undefined) {
172
213
  this._setRoot(newNode);
173
214
  this._size++;
174
- return this.root;
215
+ return true;
175
216
  }
176
217
  let current = this.root;
177
218
  while (current !== undefined) {
@@ -179,7 +220,7 @@ export class BST extends BinaryTree {
179
220
  // if (current !== newNode) {
180
221
  // The key value is the same but the reference is different, update the value of the existing node
181
222
  this._replaceNode(current, newNode);
182
- return newNode;
223
+ return true;
183
224
  // } else {
184
225
  // The key value is the same and the reference is the same, replace the entire node
185
226
  // this._replaceNode(current, newNode);
@@ -191,7 +232,7 @@ export class BST extends BinaryTree {
191
232
  current.left = newNode;
192
233
  newNode.parent = current;
193
234
  this._size++;
194
- return newNode;
235
+ return true;
195
236
  }
196
237
  current = current.left;
197
238
  }
@@ -200,20 +241,21 @@ export class BST extends BinaryTree {
200
241
  current.right = newNode;
201
242
  newNode.parent = current;
202
243
  this._size++;
203
- return newNode;
244
+ return true;
204
245
  }
205
246
  current = current.right;
206
247
  }
207
248
  }
208
- return undefined;
249
+ return false;
209
250
  }
210
251
  /**
211
- * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
212
- * Space Complexity: O(k) - Additional space is required for the sorted array.
252
+ * Time Complexity: O(k log n)
253
+ * Space Complexity: O(k)
254
+ * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
213
255
  */
214
256
  /**
215
- * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
216
- * Space Complexity: O(k) - Additional space is required for the sorted array.
257
+ * Time Complexity: O(k log n)
258
+ * Space Complexity: O(k)
217
259
  *
218
260
  * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
219
261
  * balancing the tree after each addition.
@@ -224,7 +266,7 @@ export class BST extends BinaryTree {
224
266
  * order. If not provided, undefined will be assigned as the value for each key or node.
225
267
  * @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
226
268
  * balanced or not. If set to true, the add operation will be balanced using a binary search tree
227
- * algorithm. If set to false, the add operation will not be balanced and the elements will be added
269
+ * algorithm. If set to false, the add operation will not be balanced and the nodes will be added
228
270
  * in the order they appear in the input.
229
271
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
230
272
  * type of iteration to use when adding multiple keys or nodes. It has a default value of
@@ -306,20 +348,19 @@ export class BST extends BinaryTree {
306
348
  return inserted;
307
349
  }
308
350
  /**
309
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
310
- * Space Complexity: O(n) - Additional space is required for the sorted array.
351
+ * Time Complexity: O(n log n)
352
+ * Space Complexity: O(n)
353
+ * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
311
354
  */
312
355
  /**
313
- * Time Complexity: O(log n) - Average case for a balanced tree.
314
- * Space Complexity: O(1) - Constant space is used.
356
+ * Time Complexity: O(n log n)
357
+ * Space Complexity: O(n)
315
358
  *
316
359
  * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
317
360
  * leftmost node if the comparison result is greater than.
318
361
  * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
319
362
  * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
320
363
  * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
321
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
322
- * be performed. It can have one of the following values:
323
364
  * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
324
365
  * the key of the leftmost node if the comparison result is greater than, and the key of the
325
366
  * rightmost node otherwise. If no node is found, it returns 0.
@@ -328,7 +369,7 @@ export class BST extends BinaryTree {
328
369
  let current = this.ensureNode(beginRoot);
329
370
  if (!current)
330
371
  return undefined;
331
- if (this._variant === BSTVariant.MIN) {
372
+ if (this._variant === BSTVariant.STANDARD) {
332
373
  // For BSTVariant.MIN, find the rightmost node
333
374
  while (current.right !== undefined) {
334
375
  current = current.right;
@@ -343,12 +384,12 @@ export class BST extends BinaryTree {
343
384
  return current.key;
344
385
  }
345
386
  /**
346
- * Time Complexity: O(log n) - Average case for a balanced tree.
347
- * Space Complexity: O(1) - Constant space is used.
387
+ * Time Complexity: O(log n)
388
+ * Space Complexity: O(1)
348
389
  */
349
390
  /**
350
- * Time Complexity: O(log n) - Average case for a balanced tree.
351
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
391
+ * Time Complexity: O(log n)
392
+ * Space Complexity: O(1)
352
393
  *
353
394
  * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
354
395
  * either recursive or iterative methods.
@@ -392,33 +433,14 @@ export class BST extends BinaryTree {
392
433
  }
393
434
  }
394
435
  /**
395
- * The function "isNotNodeInstance" checks if a potential key is a K.
396
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
397
- * data type.
398
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
399
- */
400
- isNotNodeInstance(potentialKey) {
401
- return !(potentialKey instanceof BSTNode);
402
- }
403
- /**
404
- * Time Complexity: O(log n) - Average case for a balanced tree.
405
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
406
- */
407
- /**
408
- * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
409
- * otherwise it returns the key itself.
410
- * @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
411
- * `undefined`.
412
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
413
- * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
414
- * @returns either a node object (N) or undefined.
415
- */
416
- ensureNode(key, iterationType = IterationType.ITERATIVE) {
417
- return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
418
- }
419
- /**
420
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
421
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
436
+ * Time Complexity: O(log n)
437
+ * Space Complexity: O(log n)
438
+ * Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
439
+ * /
440
+
441
+ /**
442
+ * Time Complexity: O(log n)
443
+ * Space Complexity: O(log n)
422
444
  *
423
445
  * The function `getNodes` returns an array of nodes that match a given identifier, using either a
424
446
  * recursive or iterative approach.
@@ -496,12 +518,13 @@ export class BST extends BinaryTree {
496
518
  return ans;
497
519
  }
498
520
  /**
499
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
500
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
521
+ * Time Complexity: O(log n)
522
+ * Space Complexity: O(log n)
523
+ * Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
501
524
  */
502
525
  /**
503
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
504
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
526
+ * Time Complexity: O(log n)
527
+ * Space Complexity: O(log n)
505
528
  *
506
529
  * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
507
530
  * are either lesser or greater than a target node, depending on the specified comparison type.
@@ -561,12 +584,12 @@ export class BST extends BinaryTree {
561
584
  }
562
585
  }
563
586
  /**
564
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
565
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
587
+ * Time Complexity: O(log n)
588
+ * Space Complexity: O(log n)
566
589
  */
567
590
  /**
568
- * Time Complexity: O(n) - Building a balanced tree from a sorted array.
569
- * Space Complexity: O(n) - Additional space is required for the sorted array.
591
+ * Time Complexity: O(log n)
592
+ * Space Complexity: O(log n)
570
593
  *
571
594
  * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
572
595
  * ensures the tree is perfectly balanced.
@@ -625,8 +648,8 @@ export class BST extends BinaryTree {
625
648
  * Space Complexity: O(n) - Additional space is required for the sorted array.
626
649
  */
627
650
  /**
628
- * Time Complexity: O(n) - Visiting each node once.
629
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
651
+ * Time Complexity: O(n)
652
+ * Space Complexity: O(log n)
630
653
  *
631
654
  * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
632
655
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
@@ -695,7 +718,7 @@ export class BST extends BinaryTree {
695
718
  _compare(a, b) {
696
719
  const extractedA = this.extractor(a);
697
720
  const extractedB = this.extractor(b);
698
- const compared = this.variant === BSTVariant.MIN ? extractedA - extractedB : extractedB - extractedA;
721
+ const compared = this.variant === BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA;
699
722
  return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
700
723
  }
701
724
  }