data-structure-typed 1.51.8 → 1.52.0

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 (216) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +152 -152
  5. package/dist/cjs/data-structures/base/index.d.ts +2 -1
  6. package/dist/cjs/data-structures/base/index.js +2 -1
  7. package/dist/cjs/data-structures/base/index.js.map +1 -1
  8. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +171 -0
  9. package/dist/cjs/data-structures/base/iterable-element-base.js +226 -0
  10. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -0
  11. package/dist/cjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  12. package/dist/cjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +13 -190
  13. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -0
  14. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
  15. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  16. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
  18. package/dist/cjs/data-structures/binary-tree/avl-tree.js +78 -59
  19. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +318 -226
  21. package/dist/cjs/data-structures/binary-tree/binary-tree.js +475 -363
  22. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  23. package/dist/cjs/data-structures/binary-tree/bst.d.ts +192 -202
  24. package/dist/cjs/data-structures/binary-tree/bst.js +207 -249
  25. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  26. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +73 -74
  27. package/dist/cjs/data-structures/binary-tree/rb-tree.js +107 -98
  28. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  29. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
  30. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
  31. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  32. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
  33. package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
  34. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  35. package/dist/cjs/data-structures/hash/hash-map.d.ts +33 -40
  36. package/dist/cjs/data-structures/hash/hash-map.js +40 -55
  37. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  38. package/dist/cjs/data-structures/heap/heap.d.ts +43 -114
  39. package/dist/cjs/data-structures/heap/heap.js +59 -127
  40. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  41. package/dist/cjs/data-structures/heap/max-heap.d.ts +50 -4
  42. package/dist/cjs/data-structures/heap/max-heap.js +76 -10
  43. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  44. package/dist/cjs/data-structures/heap/min-heap.d.ts +51 -5
  45. package/dist/cjs/data-structures/heap/min-heap.js +68 -11
  46. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  47. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  48. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +26 -28
  49. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  50. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  51. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +29 -26
  52. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  53. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  54. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +79 -10
  55. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  56. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  57. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +71 -11
  58. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  59. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
  60. package/dist/cjs/data-structures/priority-queue/priority-queue.js +70 -1
  61. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  62. package/dist/cjs/data-structures/queue/deque.d.ts +21 -20
  63. package/dist/cjs/data-structures/queue/deque.js +29 -23
  64. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  65. package/dist/cjs/data-structures/queue/queue.d.ts +8 -28
  66. package/dist/cjs/data-structures/queue/queue.js +15 -31
  67. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  68. package/dist/cjs/data-structures/stack/stack.d.ts +17 -22
  69. package/dist/cjs/data-structures/stack/stack.js +25 -24
  70. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  71. package/dist/cjs/data-structures/trie/trie.d.ts +19 -14
  72. package/dist/cjs/data-structures/trie/trie.js +27 -16
  73. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  74. package/dist/cjs/interfaces/binary-tree.d.ts +7 -7
  75. package/dist/cjs/types/common.d.ts +1 -2
  76. package/dist/cjs/types/data-structures/base/base.d.ts +5 -2
  77. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
  78. package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
  79. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
  80. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +4 -5
  81. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
  82. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  83. package/dist/cjs/types/data-structures/heap/heap.d.ts +3 -2
  84. package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  85. package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  86. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  87. package/dist/cjs/types/data-structures/queue/deque.d.ts +3 -2
  88. package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -1
  89. package/dist/cjs/types/data-structures/stack/stack.d.ts +2 -1
  90. package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -2
  91. package/dist/cjs/utils/utils.js +3 -5
  92. package/dist/cjs/utils/utils.js.map +1 -1
  93. package/dist/mjs/data-structures/base/index.d.ts +2 -1
  94. package/dist/mjs/data-structures/base/index.js +2 -1
  95. package/dist/mjs/data-structures/base/iterable-element-base.d.ts +171 -0
  96. package/dist/mjs/data-structures/base/iterable-element-base.js +222 -0
  97. package/dist/mjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  98. package/dist/mjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +10 -186
  99. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
  100. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  101. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
  102. package/dist/mjs/data-structures/binary-tree/avl-tree.js +78 -59
  103. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +318 -226
  104. package/dist/mjs/data-structures/binary-tree/binary-tree.js +476 -364
  105. package/dist/mjs/data-structures/binary-tree/bst.d.ts +192 -202
  106. package/dist/mjs/data-structures/binary-tree/bst.js +211 -255
  107. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +73 -74
  108. package/dist/mjs/data-structures/binary-tree/rb-tree.js +107 -98
  109. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
  110. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
  111. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
  112. package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
  113. package/dist/mjs/data-structures/hash/hash-map.d.ts +33 -40
  114. package/dist/mjs/data-structures/hash/hash-map.js +41 -55
  115. package/dist/mjs/data-structures/heap/heap.d.ts +43 -114
  116. package/dist/mjs/data-structures/heap/heap.js +60 -128
  117. package/dist/mjs/data-structures/heap/max-heap.d.ts +50 -4
  118. package/dist/mjs/data-structures/heap/max-heap.js +79 -10
  119. package/dist/mjs/data-structures/heap/min-heap.d.ts +51 -5
  120. package/dist/mjs/data-structures/heap/min-heap.js +68 -11
  121. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  122. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +26 -28
  123. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  124. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +29 -26
  125. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  126. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +82 -10
  127. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  128. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +71 -11
  129. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
  130. package/dist/mjs/data-structures/priority-queue/priority-queue.js +70 -1
  131. package/dist/mjs/data-structures/queue/deque.d.ts +21 -20
  132. package/dist/mjs/data-structures/queue/deque.js +29 -23
  133. package/dist/mjs/data-structures/queue/queue.d.ts +8 -28
  134. package/dist/mjs/data-structures/queue/queue.js +15 -31
  135. package/dist/mjs/data-structures/stack/stack.d.ts +17 -22
  136. package/dist/mjs/data-structures/stack/stack.js +25 -24
  137. package/dist/mjs/data-structures/trie/trie.d.ts +19 -14
  138. package/dist/mjs/data-structures/trie/trie.js +27 -16
  139. package/dist/mjs/interfaces/binary-tree.d.ts +7 -7
  140. package/dist/mjs/types/common.d.ts +1 -2
  141. package/dist/mjs/types/data-structures/base/base.d.ts +5 -2
  142. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
  143. package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
  144. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
  145. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +4 -5
  146. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
  147. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  148. package/dist/mjs/types/data-structures/heap/heap.d.ts +3 -2
  149. package/dist/mjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  150. package/dist/mjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  151. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  152. package/dist/mjs/types/data-structures/queue/deque.d.ts +3 -2
  153. package/dist/mjs/types/data-structures/queue/queue.d.ts +2 -1
  154. package/dist/mjs/types/data-structures/stack/stack.d.ts +2 -1
  155. package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -2
  156. package/dist/mjs/utils/utils.js +3 -5
  157. package/dist/umd/data-structure-typed.js +1949 -1481
  158. package/dist/umd/data-structure-typed.min.js +2 -2
  159. package/dist/umd/data-structure-typed.min.js.map +1 -1
  160. package/package.json +6 -6
  161. package/src/data-structures/base/index.ts +2 -1
  162. package/src/data-structures/base/iterable-element-base.ts +250 -0
  163. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
  164. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
  165. package/src/data-structures/binary-tree/avl-tree.ts +96 -69
  166. package/src/data-structures/binary-tree/binary-tree.ts +535 -403
  167. package/src/data-structures/binary-tree/bst.ts +247 -277
  168. package/src/data-structures/binary-tree/rb-tree.ts +123 -103
  169. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
  170. package/src/data-structures/graph/abstract-graph.ts +10 -10
  171. package/src/data-structures/hash/hash-map.ts +46 -53
  172. package/src/data-structures/heap/heap.ts +71 -152
  173. package/src/data-structures/heap/max-heap.ts +88 -13
  174. package/src/data-structures/heap/min-heap.ts +78 -15
  175. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  176. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  177. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  178. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  179. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  180. package/src/data-structures/queue/deque.ts +37 -26
  181. package/src/data-structures/queue/queue.ts +23 -36
  182. package/src/data-structures/stack/stack.ts +31 -26
  183. package/src/data-structures/trie/trie.ts +35 -20
  184. package/src/interfaces/binary-tree.ts +9 -9
  185. package/src/types/common.ts +1 -2
  186. package/src/types/data-structures/base/base.ts +14 -6
  187. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
  188. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
  189. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
  190. package/src/types/data-structures/binary-tree/bst.ts +4 -5
  191. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
  192. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
  193. package/src/types/data-structures/heap/heap.ts +4 -1
  194. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  195. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  196. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  197. package/src/types/data-structures/queue/deque.ts +3 -1
  198. package/src/types/data-structures/queue/queue.ts +3 -1
  199. package/src/types/data-structures/stack/stack.ts +3 -1
  200. package/src/types/data-structures/trie/trie.ts +3 -1
  201. package/src/utils/utils.ts +3 -3
  202. package/test/integration/avl-tree.test.ts +2 -2
  203. package/test/integration/bst.test.ts +3 -3
  204. package/test/integration/index.html +25 -11
  205. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +68 -0
  206. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
  207. package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
  208. package/test/unit/data-structures/heap/heap.test.ts +80 -0
  209. package/test/unit/data-structures/heap/max-heap.test.ts +44 -1
  210. package/test/unit/data-structures/heap/min-heap.test.ts +18 -1
  211. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +42 -0
  212. package/test/unit/data-structures/queue/deque.test.ts +30 -0
  213. package/test/unit/data-structures/queue/queue.test.ts +56 -0
  214. package/test/utils/big-o.ts +12 -6
  215. package/tsconfig-base.json +1 -0
  216. package/dist/cjs/data-structures/base/iterable-base.js.map +0 -1
@@ -59,23 +59,22 @@ export class BSTNode extends BinaryTreeNode {
59
59
  */
60
60
  export class BST extends BinaryTree {
61
61
  /**
62
- * This is the constructor function for a Binary Search Tree class in TypeScript, which initializes
63
- * the tree with keys, nodes, or entries and optional options.
64
- * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
65
- * contain keys, nodes, or entries. It is used to initialize the binary search tree with the provided
66
- * keys, nodes, or entries.
67
- * @param [options] - The `options` parameter is an optional object that can contain additional
68
- * configuration options for the binary search tree. It can have the following properties:
69
- */
70
- constructor(keysOrNodesOrEntries = [], options) {
62
+ * This is the constructor function for a Binary Search Tree class in TypeScript.
63
+ * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
64
+ * iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
65
+ * added to the binary search tree during the construction of the object.
66
+ * @param [options] - An optional object that contains additional options for the Binary Search Tree.
67
+ * It can include a comparator function that defines the order of the elements in the tree.
68
+ */
69
+ constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
71
70
  super([], options);
72
71
  if (options) {
73
72
  const { comparator } = options;
74
73
  if (comparator)
75
74
  this._comparator = comparator;
76
75
  }
77
- if (keysOrNodesOrEntries)
78
- this.addMany(keysOrNodesOrEntries);
76
+ if (keysOrNodesOrEntriesOrRawElements)
77
+ this.addMany(keysOrNodesOrEntriesOrRawElements);
79
78
  }
80
79
  _root = undefined;
81
80
  /**
@@ -85,20 +84,6 @@ export class BST extends BinaryTree {
85
84
  get root() {
86
85
  return this._root;
87
86
  }
88
- _comparator = (a, b) => {
89
- if (a > b)
90
- return 1;
91
- if (a < b)
92
- return -1;
93
- return 0;
94
- };
95
- /**
96
- * The function returns the value of the _comparator property.
97
- * @returns The `_comparator` property is being returned.
98
- */
99
- get comparator() {
100
- return this._comparator;
101
- }
102
87
  /**
103
88
  * The function creates a new BSTNode with the given key and value and returns it.
104
89
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@@ -113,10 +98,9 @@ export class BST extends BinaryTree {
113
98
  /**
114
99
  * The function creates a new binary search tree with the specified options.
115
100
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
116
- * behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
117
- * partial object of type `BSTOptions<K>`.
118
- * @returns a new instance of the BST class, with the provided options merged with the default
119
- * options. The returned value is casted as TREE.
101
+ * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
102
+ * following properties:
103
+ * @returns a new instance of the BST class with the provided options.
120
104
  */
121
105
  createTree(options) {
122
106
  return new BST([], {
@@ -126,95 +110,58 @@ export class BST extends BinaryTree {
126
110
  });
127
111
  }
128
112
  /**
129
- * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
130
- * otherwise it returns undefined.
131
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
132
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
133
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
134
- * @returns a node of type NODE or undefined.
135
- */
136
- keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
137
- let node;
138
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
139
- return;
140
- }
141
- else if (this.isNode(keyOrNodeOrEntry)) {
142
- node = keyOrNodeOrEntry;
143
- }
144
- else if (this.isEntry(keyOrNodeOrEntry)) {
145
- const [key, value] = keyOrNodeOrEntry;
146
- if (key === undefined || key === null) {
147
- return;
148
- }
149
- else {
150
- node = this.createNode(key, value);
151
- }
152
- }
153
- else if (!this.isNode(keyOrNodeOrEntry)) {
154
- node = this.createNode(keyOrNodeOrEntry, value);
155
- }
156
- else {
157
- return;
158
- }
159
- return node;
160
- }
161
- /**
162
- * Time Complexity: O(log n)
163
- * Space Complexity: O(log n)
113
+ * The function overrides a method and converts a key, value pair or entry or raw element to a node.
114
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
115
+ * type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
116
+ * element.
117
+ * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
118
+ * value associated with a key in a key-value pair.
119
+ * @returns either a NODE object or undefined.
164
120
  */
121
+ keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
122
+ return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
123
+ }
165
124
  /**
166
125
  * Time Complexity: O(log n)
167
126
  * Space Complexity: O(log n)
168
127
  *
169
- * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
170
- * otherwise it returns the key itself.
171
- * @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
172
- * `undefined`.
173
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
174
- * type of iteration to be performed. It has a default value of `'ITERATIVE'`.
175
- * @returns either a node object (NODE) or undefined.
176
- */
177
- ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
178
- if (keyOrNodeOrEntry === this.NIL)
179
- return;
180
- if (this.isRealNode(keyOrNodeOrEntry)) {
181
- return keyOrNodeOrEntry;
182
- }
183
- if (this.isEntry(keyOrNodeOrEntry)) {
184
- const key = keyOrNodeOrEntry[0];
185
- if (key === null || key === undefined)
186
- return;
187
- return this.getNodeByKey(key, iterationType);
188
- }
189
- const key = keyOrNodeOrEntry;
190
- if (key === null || key === undefined)
191
- return;
192
- return this.getNodeByKey(key, iterationType);
128
+ * The function ensures the existence of a node in a data structure and returns it, or undefined if
129
+ * it doesn't exist.
130
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
131
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
132
+ * entry, or raw element that needs to be ensured in the tree.
133
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
134
+ * parameter that specifies the type of iteration to be used when ensuring a node. It has a default
135
+ * value of `'ITERATIVE'`.
136
+ * @returns The method is returning either the node that was ensured or `undefined` if the node could
137
+ * not be ensured.
138
+ */
139
+ ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
140
+ return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
193
141
  }
194
142
  /**
195
- * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
196
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
197
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
143
+ * The function checks if the input is an instance of the BSTNode class.
144
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
145
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
146
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
147
+ * an instance of the `BSTNode` class.
198
148
  */
199
- isNode(keyOrNodeOrEntry) {
200
- return keyOrNodeOrEntry instanceof BSTNode;
149
+ isNode(keyOrNodeOrEntryOrRawElement) {
150
+ return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
201
151
  }
202
- /**
203
- * Time Complexity: O(log n)
204
- * Space Complexity: O(1)
205
- */
206
152
  /**
207
153
  * Time Complexity: O(log n)
208
154
  * Space Complexity: O(1)
209
155
  *
210
- * The `add` function in TypeScript adds a new node to a binary search tree based on the key value,
211
- * updating the value if the key already exists.
212
- * @param keyOrNodeOrEntry - It is a parameter that can accept three types of values:
213
- * @param {V} [value] - The value to be added to the binary search tree.
214
- * @returns The method returns a boolean value.
215
- */
216
- add(keyOrNodeOrEntry, value) {
217
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
156
+ * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
157
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
158
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
159
+ * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
160
+ * key in the binary search tree. If provided, it will be stored in the node along with the key.
161
+ * @returns a boolean value.
162
+ */
163
+ add(keyOrNodeOrEntryOrRawElement, value) {
164
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
218
165
  if (newNode === undefined)
219
166
  return false;
220
167
  if (this.root === undefined) {
@@ -225,15 +172,8 @@ export class BST extends BinaryTree {
225
172
  let current = this.root;
226
173
  while (current !== undefined) {
227
174
  if (this.comparator(current.key, newNode.key) === 0) {
228
- // if (current !== newNode) {
229
- // The key value is the same but the reference is different, update the value of the existing node
230
175
  this._replaceNode(current, newNode);
231
176
  return true;
232
- // } else {
233
- // The key value is the same and the reference is the same, replace the entire node
234
- // this._replaceNode(current, newNode);
235
- // return;
236
- // }
237
177
  }
238
178
  else if (this.comparator(current.key, newNode.key) > 0) {
239
179
  if (current.left === undefined) {
@@ -255,18 +195,17 @@ export class BST extends BinaryTree {
255
195
  return false;
256
196
  }
257
197
  /**
258
- * Time Complexity: O(k log n)
259
- * Space Complexity: O(k + log n)
198
+ * Time Complexity: O(log n)
199
+ * Space Complexity: O(log n)
260
200
  */
261
201
  /**
262
202
  * Time Complexity: O(k log n)
263
203
  * Space Complexity: O(k + log n)
264
204
  *
265
- * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure, balancing
266
- * the structure if specified, and returns an array indicating whether each key or node was
267
- * successfully inserted.
268
- * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
269
- * data structure.
205
+ * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
206
+ * an array indicating whether each key or node was successfully inserted.
207
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
208
+ * elements to be added to the data structure.
270
209
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
271
210
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
272
211
  * order. If not provided, undefined will be assigned as the value for each key or node.
@@ -275,20 +214,19 @@ export class BST extends BinaryTree {
275
214
  * algorithm. If set to false, the elements will be added without balancing the tree. The default
276
215
  * value is true.
277
216
  * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
278
- * specifies the type of iteration to use when adding multiple keys or nodes to the binary tree. It
279
- * has a default value of `this.iterationType`, which means it will use the iteration type specified
280
- * in the binary tree instance.
281
- * @returns The function `addMany` returns an array of booleans indicating whether each key or node
282
- * or entry was successfully inserted into the data structure.
217
+ * specifies the type of iteration to use when adding multiple keys or nodes to the binary search
218
+ * tree. It can have two possible values:
219
+ * @returns The function `addMany` returns an array of booleans indicating whether each element was
220
+ * successfully inserted into the data structure.
283
221
  */
284
- addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
222
+ addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
285
223
  const inserted = [];
286
224
  let valuesIterator;
287
225
  if (values) {
288
226
  valuesIterator = values[Symbol.iterator]();
289
227
  }
290
228
  if (!isBalanceAdd) {
291
- for (const kve of keysOrNodesOrEntries) {
229
+ for (const kve of keysOrNodesOrEntriesOrRawElements) {
292
230
  const value = valuesIterator?.next().value;
293
231
  const nn = this.add(kve, value);
294
232
  inserted.push(nn);
@@ -301,25 +239,32 @@ export class BST extends BinaryTree {
301
239
  return false;
302
240
  return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
303
241
  };
304
- for (const kve of keysOrNodesOrEntries) {
242
+ for (const kve of keysOrNodesOrEntriesOrRawElements) {
305
243
  isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
306
244
  }
307
245
  let sorted = [];
308
246
  sorted = realBTNExemplars.sort((a, b) => {
309
247
  let keyA, keyB;
310
- if (this.isEntry(a)) {
248
+ if (this.isEntry(a))
311
249
  keyA = a[0];
312
- }
313
250
  else if (this.isRealNode(a))
314
251
  keyA = a.key;
315
- else
252
+ else if (this.toEntryFn) {
253
+ keyA = this.toEntryFn(a)[0];
254
+ }
255
+ else {
316
256
  keyA = a;
257
+ }
317
258
  if (this.isEntry(b))
318
259
  keyB = b[0];
319
260
  else if (this.isRealNode(b))
320
261
  keyB = b.key;
321
- else
262
+ else if (this.toEntryFn) {
263
+ keyB = this.toEntryFn(b)[0];
264
+ }
265
+ else {
322
266
  keyB = b;
267
+ }
323
268
  if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
324
269
  return this.comparator(keyA, keyB);
325
270
  }
@@ -360,32 +305,26 @@ export class BST extends BinaryTree {
360
305
  return inserted;
361
306
  }
362
307
  /**
363
- * Time Complexity: O(log n)
364
- * Space Complexity: O(k + log n)
365
- * /
366
-
367
- /**
368
308
  * Time Complexity: O(log n)
369
309
  * Space Complexity: O(k + log n)
370
310
  *
371
- * The function `getNodes` returns an array of nodes that match a given identifier, using either a
372
- * recursive or iterative approach.
311
+ * The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
312
+ * identifier and callback function.
373
313
  * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
374
- * want to search for in the nodes of the binary tree. It can be of any type that is returned by the
375
- * callback function `C`.
376
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
377
- * argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
378
- * function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
379
- * @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
380
- * first node that matches the identifier. If set to true, the function will return an array
381
- * containing only the first matching node. If set to false (default), the function will continue
382
- * searching for all nodes that match the identifier and return an array containing
383
- * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
384
- * for the traversal. It can be either a key value or a node object. If it is undefined, the
385
- * traversal will start from the root of the tree.
386
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
387
- * performed on the binary tree. It can have two possible values:
388
- * @returns The method returns an array of nodes (`NODE[]`).
314
+ * want to search for in the binary tree. It can be of any type that is returned by the callback
315
+ * function.
316
+ * @param {C} callback - The `callback` parameter is a function that takes a node as input and
317
+ * returns a value. This value is used to identify the nodes that match the given identifier. The
318
+ * `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
319
+ * @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
320
+ * or all matching nodes. If set to true, only the first matching node will be returned. If set to
321
+ * false, all matching nodes will be returned. The default value is false.
322
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
323
+ * point for the search in the binary tree. It can be either a node object, a key-value pair, or an
324
+ * entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
325
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
326
+ * iteration to be performed. It can have two possible values:
327
+ * @returns The method `getNodes` returns an array of `NODE` objects.
389
328
  */
390
329
  getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
391
330
  beginRoot = this.ensureNode(beginRoot);
@@ -456,50 +395,49 @@ export class BST extends BinaryTree {
456
395
  * Time Complexity: O(log n)
457
396
  * Space Complexity: O(1)
458
397
  *
459
- * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
460
- * callback function.
461
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
462
- * that you want to search for in the binary search tree. It can be of any type that is compatible
463
- * with the type of nodes in the tree.
464
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
465
- * the tree. It is used to determine whether a node matches the given identifier. The `callback`
466
- * function should take a node as its parameter and return a value that can be compared to the
467
- * `identifier` parameter.
398
+ * The function `getNode` returns the first node that matches the given identifier and callback
399
+ * function in a binary search tree.
400
+ * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
401
+ * want to search for in the binary search tree. It can be of any type that is compatible with the
402
+ * type returned by the callback function.
403
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
404
+ * node matches the desired criteria. It should be a function that takes a node as an argument and
405
+ * returns a boolean value indicating whether the node matches the criteria or not. If no callback is
406
+ * provided, the default callback will be
468
407
  * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
469
- * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
470
- * using the `ensureNode` method. If it is not provided, the `root`
471
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
472
- * be performed when searching for nodes in the binary search tree. It is an optional parameter and
473
- * its default value is taken from the `iterationType` property of the class.
474
- * @returns The method is returning a value of type `NODE | null | undefined`.
408
+ * search tree. It can be either a key or a node. If it is a key, the search will start from the node
409
+ * with that key. If it is a node, the search will start from that node.
410
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
411
+ * of iteration to be performed when searching for nodes in the binary search tree. It can have one
412
+ * of the following values:
413
+ * @returns The method is returning a NODE object or undefined.
475
414
  */
476
415
  getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
477
416
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
478
417
  }
479
418
  /**
480
- * Time Complexity: O(log n)
481
- * Space Complexity: O(1)
419
+ * Time Complexity: O(k log n)
420
+ * Space Complexity: O(k + log n)
482
421
  */
483
422
  /**
484
423
  * Time Complexity: O(log n)
485
424
  * Space Complexity: O(1)
486
425
  *
487
- * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
488
- * either recursive or iterative methods.
489
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
490
- * It is used to identify the node that we want to retrieve.
491
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
492
- * type of iteration to use when searching for a node in the binary tree. It can have two possible
493
- * values:
494
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
495
- * found in the binary tree. If no node is found, it returns `undefined`.
426
+ * The function `getNodeByKey` returns a node with a specific key from a tree data structure.
427
+ * @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
428
+ * is typically a unique identifier or a value that can be used to determine the position of the node
429
+ * in the tree structure.
430
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
431
+ * parameter that specifies the type of iteration to be used when searching for a node in the tree.
432
+ * It has a default value of `'ITERATIVE'`.
433
+ * @returns The method is returning a NODE object or undefined.
496
434
  */
497
435
  getNodeByKey(key, iterationType = 'ITERATIVE') {
498
436
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
499
437
  }
500
438
  /**
501
- * Time complexity: O(n)
502
- * Space complexity: O(n)
439
+ * Time Complexity: O(log n)
440
+ * Space Complexity: O(k + log n)
503
441
  */
504
442
  /**
505
443
  * Time complexity: O(n)
@@ -508,15 +446,16 @@ export class BST extends BinaryTree {
508
446
  * The function overrides the depth-first search method and returns an array of the return types of
509
447
  * the callback function.
510
448
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
511
- * during the depth-first search traversal. It is an optional parameter and if not provided, a
512
- * default callback function will be used.
513
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
514
- * nodes are visited during the depth-first search. It can have one of the following values:
515
- * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
516
- * Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
517
- * value is provided, the DFS traversal will start from the root of the tree.
518
- * @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
519
- * iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
449
+ * during the depth-first search traversal. It is an optional parameter and defaults to
450
+ * `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
451
+ * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
452
+ * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
453
+ * take one of the following values:
454
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
455
+ * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
456
+ * node entry. If not specified, the default value is the root of the tree.
457
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
458
+ * type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
520
459
  * following values:
521
460
  * @returns The method is returning an array of the return type of the callback function.
522
461
  */
@@ -524,8 +463,8 @@ export class BST extends BinaryTree {
524
463
  return super.dfs(callback, pattern, beginRoot, iterationType, false);
525
464
  }
526
465
  /**
527
- * Time complexity: O(n)
528
- * Space complexity: O(n)
466
+ * Time Complexity: O(log n)
467
+ * Space Complexity: O(1)
529
468
  */
530
469
  /**
531
470
  * Time complexity: O(n)
@@ -534,38 +473,38 @@ export class BST extends BinaryTree {
534
473
  * The function overrides the breadth-first search method and returns an array of the return types of
535
474
  * the callback function.
536
475
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
537
- * visited during the breadth-first search traversal. It is an optional parameter and if not
538
- * provided, a default callback function will be used.
539
- * @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
540
- * traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
541
- * the tree is used as the starting point.
542
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
543
- * be performed during the breadth-first search (BFS) traversal. It determines the order in which the
544
- * nodes are visited.
545
- * @returns The method is returning an array of the return type of the callback function.
476
+ * visited during the breadth-first search. It should take a single argument, which is the current
477
+ * node being visited, and it can return a value of any type.
478
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
479
+ * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
480
+ * object. If no value is provided, the default value is the root of the tree.
481
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
482
+ * of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
483
+ * the following values:
484
+ * @returns an array of the return type of the callback function.
546
485
  */
547
486
  bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
548
487
  return super.bfs(callback, beginRoot, iterationType, false);
549
488
  }
550
489
  /**
551
- * Time complexity: O(n)
552
- * Space complexity: O(n)
490
+ * Time Complexity: O(log n)
491
+ * Space Complexity: O(1)
553
492
  */
554
493
  /**
555
494
  * Time complexity: O(n)
556
495
  * Space complexity: O(n)
557
496
  *
558
- * The function overrides the listLevels method and returns an array of arrays containing the return
559
- * type of the callback function for each level of the tree.
497
+ * The function overrides the listLevels method from the superclass and returns an array of arrays
498
+ * containing the results of the callback function applied to each level of the tree.
560
499
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
561
- * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
562
- * during the level listing process.
563
- * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
564
- * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
565
- * provided, the root of the binary tree is used as the starting point.
566
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
567
- * be performed on the tree. It determines the order in which the nodes are visited during the
568
- * iteration.
500
+ * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
501
+ * tree during the iteration process.
502
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
503
+ * point for listing the levels of the binary tree. It can be either a root node of the tree, a
504
+ * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
505
+ * value is provided, the root of
506
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
507
+ * of iteration to be performed on the tree. It can have one of the following values:
569
508
  * @returns The method is returning a two-dimensional array of the return type of the callback
570
509
  * function.
571
510
  */
@@ -573,27 +512,27 @@ export class BST extends BinaryTree {
573
512
  return super.listLevels(callback, beginRoot, iterationType, false);
574
513
  }
575
514
  /**
576
- * Time Complexity: O(log n)
577
- * Space Complexity: O(log n)
515
+ * Time complexity: O(n)
516
+ * Space complexity: O(n)
578
517
  */
579
518
  /**
580
- * Time Complexity: O(log n)
581
- * Space Complexity: O(log n)
519
+ * Time complexity: O(n)
520
+ * Space complexity: O(n)
582
521
  *
583
- * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
584
- * are either lesser or greater than a target node, depending on the specified comparison type.
522
+ * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
523
+ * each node that meets a certain condition based on a target node and a comparison value.
585
524
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
586
- * that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
587
- * parameter of type `NODE` (the node type) and returns a value of any type.
525
+ * that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
526
+ * which is the current node being traversed, and returns a value of any type.
588
527
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
589
- * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
590
- * `CP`, which is a custom type representing the comparison operator. The possible values for
591
- * `lesserOrGreater` are
592
- * @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
593
- * binary tree that you want to traverse from. It can be specified either by its key, by the node
594
- * object itself, or it can be left undefined to start the traversal from the root of the tree.
595
- * @param iterationType - The `iterationType` parameter determines the type of traversal to be
596
- * performed on the binary tree. It can have two possible values:
528
+ * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
529
+ * 0, or 1, where:
530
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
531
+ * the binary tree that you want to start traversing from. It can be specified either by providing
532
+ * the key of the node, the node itself, or an entry containing the key and value of the node. If no
533
+ * `targetNode` is provided,
534
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
535
+ * traversal to be performed on the binary tree. It can have two possible values:
597
536
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
598
537
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
599
538
  */
@@ -636,18 +575,19 @@ export class BST extends BinaryTree {
636
575
  }
637
576
  }
638
577
  /**
639
- * Time Complexity: O(log n)
640
- * Space Complexity: O(log n)
578
+ * Time complexity: O(n)
579
+ * Space complexity: O(n)
641
580
  */
642
581
  /**
643
- * Time Complexity: O(log n)
644
- * Space Complexity: O(log n)
582
+ * Time complexity: O(n)
583
+ * Space complexity: O(n)
645
584
  *
646
- * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
647
- * ensures the tree is perfectly balanced.
648
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
649
- * type of iteration to use when building a balanced binary search tree. It can have two possible
650
- * values:
585
+ * The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
586
+ * the binary search tree is perfectly balanced, otherwise it returns `false`.
587
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
588
+ * specifies the type of iteration to use when building a balanced binary search tree. It has a
589
+ * default value of `this.iterationType`, which means it will use the iteration type specified in the
590
+ * current instance of the class.
651
591
  * @returns The function `perfectlyBalance` returns a boolean value.
652
592
  */
653
593
  perfectlyBalance(iterationType = this.iterationType) {
@@ -687,25 +627,19 @@ export class BST extends BinaryTree {
687
627
  }
688
628
  }
689
629
  /**
690
- * Balancing Adjustment:
691
- * 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.
692
- * 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.
693
- *
694
- * Use Cases and Efficiency:
695
- * 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.
696
- * 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).
697
- */
698
- /**
699
- * Time Complexity: O(n)
700
- * Space Complexity: O(log n)
630
+ * Time complexity: O(n)
631
+ * Space complexity: O(n)
701
632
  */
702
633
  /**
703
634
  * Time Complexity: O(n)
704
635
  * Space Complexity: O(log n)
705
636
  *
706
- * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
707
- * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
708
- * to check if the AVL tree is balanced. It can have two possible values:
637
+ * The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
638
+ * iterative approach.
639
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
640
+ * specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
641
+ * value of `this.iterationType`, which means it will use the iteration type specified in the current
642
+ * instance of the AVL tree.
709
643
  * @returns a boolean value.
710
644
  */
711
645
  isAVLBalanced(iterationType = this.iterationType) {
@@ -753,10 +687,32 @@ export class BST extends BinaryTree {
753
687
  }
754
688
  return balanced;
755
689
  }
690
+ _DEFAULT_COMPARATOR = (a, b) => {
691
+ if (typeof a === 'object' || typeof b === 'object') {
692
+ throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`);
693
+ }
694
+ if (a > b)
695
+ return 1;
696
+ if (a < b)
697
+ return -1;
698
+ return 0;
699
+ };
700
+ _comparator = this._DEFAULT_COMPARATOR;
701
+ /**
702
+ * Time Complexity: O(n)
703
+ * Space Complexity: O(log n)
704
+ */
705
+ /**
706
+ * The function returns the value of the _comparator property.
707
+ * @returns The `_comparator` property is being returned.
708
+ */
709
+ get comparator() {
710
+ return this._comparator;
711
+ }
756
712
  /**
757
- * The function sets the root property of an object and updates the parent property of the new root.
758
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
759
- * can either be an object of type `NODE` or it can be `undefined`.
713
+ * The function sets the root of a tree-like structure and updates the parent property of the new
714
+ * root.
715
+ * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
760
716
  */
761
717
  _setRoot(v) {
762
718
  if (v) {