data-structure-typed 1.51.7 → 1.51.9

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 (123) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +1 -37
  4. package/benchmark/report.json +15 -387
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.js +90 -71
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +318 -233
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +492 -392
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/bst.d.ts +204 -251
  15. package/dist/cjs/data-structures/binary-tree/bst.js +256 -358
  16. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +74 -85
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.js +111 -119
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
  21. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
  22. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
  24. package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
  25. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  26. package/dist/cjs/data-structures/hash/hash-map.d.ts +31 -38
  27. package/dist/cjs/data-structures/hash/hash-map.js +40 -55
  28. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  29. package/dist/cjs/data-structures/heap/heap.d.ts +1 -3
  30. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  31. package/dist/cjs/data-structures/queue/deque.d.ts +2 -3
  32. package/dist/cjs/data-structures/queue/deque.js +2 -3
  33. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  34. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  35. package/dist/cjs/data-structures/trie/trie.d.ts +1 -1
  36. package/dist/cjs/data-structures/trie/trie.js +1 -1
  37. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  38. package/dist/cjs/interfaces/binary-tree.d.ts +7 -7
  39. package/dist/cjs/types/common.d.ts +2 -3
  40. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
  41. package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
  42. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
  43. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +6 -5
  44. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
  45. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
  46. package/dist/cjs/types/utils/utils.d.ts +10 -1
  47. package/dist/cjs/utils/utils.d.ts +2 -1
  48. package/dist/cjs/utils/utils.js +27 -1
  49. package/dist/cjs/utils/utils.js.map +1 -1
  50. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
  51. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
  52. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
  53. package/dist/mjs/data-structures/binary-tree/avl-tree.js +90 -71
  54. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +318 -233
  55. package/dist/mjs/data-structures/binary-tree/binary-tree.js +493 -393
  56. package/dist/mjs/data-structures/binary-tree/bst.d.ts +204 -251
  57. package/dist/mjs/data-structures/binary-tree/bst.js +252 -357
  58. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +74 -85
  59. package/dist/mjs/data-structures/binary-tree/rb-tree.js +111 -119
  60. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
  61. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
  62. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
  63. package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
  64. package/dist/mjs/data-structures/hash/hash-map.d.ts +31 -38
  65. package/dist/mjs/data-structures/hash/hash-map.js +41 -55
  66. package/dist/mjs/data-structures/heap/heap.d.ts +1 -3
  67. package/dist/mjs/data-structures/queue/deque.d.ts +2 -3
  68. package/dist/mjs/data-structures/queue/deque.js +2 -3
  69. package/dist/mjs/data-structures/trie/trie.d.ts +1 -1
  70. package/dist/mjs/data-structures/trie/trie.js +1 -1
  71. package/dist/mjs/interfaces/binary-tree.d.ts +7 -7
  72. package/dist/mjs/types/common.d.ts +2 -3
  73. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
  74. package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
  75. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
  76. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +6 -5
  77. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
  78. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
  79. package/dist/mjs/types/utils/utils.d.ts +10 -1
  80. package/dist/mjs/utils/utils.d.ts +2 -1
  81. package/dist/mjs/utils/utils.js +25 -0
  82. package/dist/umd/data-structure-typed.js +1427 -1343
  83. package/dist/umd/data-structure-typed.min.js +4 -4
  84. package/dist/umd/data-structure-typed.min.js.map +1 -1
  85. package/package.json +6 -6
  86. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
  87. package/src/data-structures/binary-tree/avl-tree.ts +109 -80
  88. package/src/data-structures/binary-tree/binary-tree.ts +556 -433
  89. package/src/data-structures/binary-tree/bst.ts +286 -375
  90. package/src/data-structures/binary-tree/rb-tree.ts +132 -125
  91. package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
  92. package/src/data-structures/graph/abstract-graph.ts +10 -10
  93. package/src/data-structures/hash/hash-map.ts +42 -49
  94. package/src/data-structures/heap/heap.ts +1 -1
  95. package/src/data-structures/queue/deque.ts +2 -2
  96. package/src/data-structures/queue/queue.ts +1 -1
  97. package/src/data-structures/trie/trie.ts +2 -2
  98. package/src/interfaces/binary-tree.ts +11 -9
  99. package/src/types/common.ts +2 -3
  100. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
  101. package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
  102. package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
  103. package/src/types/data-structures/binary-tree/bst.ts +6 -5
  104. package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
  105. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
  106. package/src/types/utils/utils.ts +14 -1
  107. package/src/utils/utils.ts +20 -1
  108. package/test/integration/all-in-one.test.ts +2 -2
  109. package/test/integration/avl-tree.test.ts +2 -2
  110. package/test/integration/bst.test.ts +3 -3
  111. package/test/integration/index.html +95 -11
  112. package/test/performance/reportor.ts +2 -3
  113. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +75 -7
  114. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
  115. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
  116. package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
  117. package/test/unit/data-structures/binary-tree/overall.test.ts +24 -20
  118. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +6 -6
  119. package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
  120. package/test/unit/utils/utils.test.ts +5 -0
  121. package/test/utils/big-o.ts +12 -6
  122. package/test/utils/console.ts +1 -1
  123. package/tsconfig-base.json +1 -0
@@ -59,25 +59,24 @@ export class BSTNode extends BinaryTreeNode {
59
59
  */
60
60
  export class BST extends BinaryTree {
61
61
  /**
62
- * This is the constructor function for a TypeScript class that initializes a binary search tree with
63
- * optional keys or nodes or entries and options.
64
- * @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
65
- * to initialize the binary search tree with the provided keys, nodes, or entries.
66
- * @param [options] - The `options` parameter is an optional object that can contain additional
67
- * configuration options for the binary search tree. It can have the following properties:
68
- */
69
- 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) {
70
70
  super([], options);
71
71
  if (options) {
72
- const { variant } = options;
73
- if (variant)
74
- this._variant = variant;
72
+ const { comparator } = options;
73
+ if (comparator)
74
+ this._comparator = comparator;
75
75
  }
76
- this._root = undefined;
77
- if (keysOrNodesOrEntries)
78
- this.addMany(keysOrNodesOrEntries);
76
+ if (keysOrNodesOrEntriesOrRawElements)
77
+ this.addMany(keysOrNodesOrEntriesOrRawElements);
79
78
  }
80
- _root;
79
+ _root = undefined;
81
80
  /**
82
81
  * The function returns the root node of a tree structure.
83
82
  * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
@@ -85,14 +84,6 @@ export class BST extends BinaryTree {
85
84
  get root() {
86
85
  return this._root;
87
86
  }
88
- _variant = 'STANDARD';
89
- /**
90
- * The function returns the value of the _variant property.
91
- * @returns The value of the `_variant` property.
92
- */
93
- get variant() {
94
- return this._variant;
95
- }
96
87
  /**
97
88
  * The function creates a new BSTNode with the given key and value and returns it.
98
89
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@@ -107,110 +98,70 @@ export class BST extends BinaryTree {
107
98
  /**
108
99
  * The function creates a new binary search tree with the specified options.
109
100
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
110
- * behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
111
- * partial object of type `BSTOptions<K>`.
112
- * @returns a new instance of the BST class, with the provided options merged with the default
113
- * 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.
114
104
  */
115
105
  createTree(options) {
116
106
  return new BST([], {
117
107
  iterationType: this.iterationType,
118
- variant: this.variant,
108
+ comparator: this.comparator,
119
109
  ...options
120
110
  });
121
111
  }
122
112
  /**
123
- * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
124
- * otherwise it returns undefined.
125
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
126
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
127
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
128
- * @returns a node of type NODE or undefined.
129
- */
130
- keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
131
- let node;
132
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
133
- return;
134
- }
135
- else if (this.isNode(keyOrNodeOrEntry)) {
136
- node = keyOrNodeOrEntry;
137
- }
138
- else if (this.isEntry(keyOrNodeOrEntry)) {
139
- const [key, value] = keyOrNodeOrEntry;
140
- if (key === undefined || key === null) {
141
- return;
142
- }
143
- else {
144
- node = this.createNode(key, value);
145
- }
146
- }
147
- else if (!this.isNode(keyOrNodeOrEntry)) {
148
- node = this.createNode(keyOrNodeOrEntry, value);
149
- }
150
- else {
151
- return;
152
- }
153
- return node;
154
- }
155
- /**
156
- * Time Complexity: O(log n)
157
- * 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.
158
120
  */
121
+ keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
122
+ return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
123
+ }
159
124
  /**
160
125
  * Time Complexity: O(log n)
161
126
  * Space Complexity: O(log n)
162
127
  *
163
- * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
164
- * otherwise it returns the key itself.
165
- * @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
166
- * `undefined`.
167
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
168
- * type of iteration to be performed. It has a default value of `'ITERATIVE'`.
169
- * @returns either a node object (NODE) or undefined.
170
- */
171
- ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
172
- if (keyOrNodeOrEntry === this.NIL)
173
- return;
174
- if (this.isRealNode(keyOrNodeOrEntry)) {
175
- return keyOrNodeOrEntry;
176
- }
177
- if (this.isEntry(keyOrNodeOrEntry)) {
178
- const key = keyOrNodeOrEntry[0];
179
- if (key === null || key === undefined)
180
- return;
181
- return this.getNodeByKey(key, iterationType);
182
- }
183
- const key = keyOrNodeOrEntry;
184
- if (key === null || key === undefined)
185
- return;
186
- 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;
187
141
  }
188
142
  /**
189
- * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
190
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
191
- * @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.
192
148
  */
193
- isNode(keyOrNodeOrEntry) {
194
- return keyOrNodeOrEntry instanceof BSTNode;
149
+ isNode(keyOrNodeOrEntryOrRawElement) {
150
+ return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
195
151
  }
196
- /**
197
- * Time Complexity: O(log n)
198
- * Space Complexity: O(1)
199
- */
200
152
  /**
201
153
  * Time Complexity: O(log n)
202
154
  * Space Complexity: O(1)
203
155
  *
204
- * The `add` function adds a new node to a binary tree, updating the value if the key already exists
205
- * or inserting a new node if the key is unique.
206
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
207
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
208
- * being added to the binary tree.
209
- * @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
210
- * node was not added.
211
- */
212
- add(keyOrNodeOrEntry, value) {
213
- 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);
214
165
  if (newNode === undefined)
215
166
  return false;
216
167
  if (this.root === undefined) {
@@ -220,18 +171,11 @@ export class BST extends BinaryTree {
220
171
  }
221
172
  let current = this.root;
222
173
  while (current !== undefined) {
223
- if (this._compare(current.key, newNode.key) === 'EQ') {
224
- // if (current !== newNode) {
225
- // The key value is the same but the reference is different, update the value of the existing node
174
+ if (this.comparator(current.key, newNode.key) === 0) {
226
175
  this._replaceNode(current, newNode);
227
176
  return true;
228
- // } else {
229
- // The key value is the same and the reference is the same, replace the entire node
230
- // this._replaceNode(current, newNode);
231
- // return;
232
- // }
233
177
  }
234
- else if (this._compare(current.key, newNode.key) === 'GT') {
178
+ else if (this.comparator(current.key, newNode.key) > 0) {
235
179
  if (current.left === undefined) {
236
180
  current.left = newNode;
237
181
  this._size++;
@@ -251,37 +195,38 @@ export class BST extends BinaryTree {
251
195
  return false;
252
196
  }
253
197
  /**
254
- * Time Complexity: O(k log n)
255
- * Space Complexity: O(k + log n)
198
+ * Time Complexity: O(log n)
199
+ * Space Complexity: O(log n)
256
200
  */
257
201
  /**
258
202
  * Time Complexity: O(k log n)
259
203
  * Space Complexity: O(k + log n)
260
204
  *
261
- * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
262
- * balancing the tree after each addition.
263
- * @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
264
- * the binary tree.
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.
265
209
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
266
210
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
267
211
  * order. If not provided, undefined will be assigned as the value for each key or node.
268
- * @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
269
- * balanced or not. If set to true, the add operation will be balanced using a binary search tree
270
- * algorithm. If set to false, the add operation will not be balanced and the nodes will be added
271
- * in the order they appear in the input.
272
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
273
- * type of iteration to use when adding multiple keys or nodes. It has a default value of
274
- * `this.iterationType`, which suggests that it is a property of the current object.
275
- * @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
276
- */
277
- addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
212
+ * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
213
+ * adding the elements. If set to true, the tree will be balanced using a binary search tree
214
+ * algorithm. If set to false, the elements will be added without balancing the tree. The default
215
+ * value is true.
216
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
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.
221
+ */
222
+ addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
278
223
  const inserted = [];
279
224
  let valuesIterator;
280
225
  if (values) {
281
226
  valuesIterator = values[Symbol.iterator]();
282
227
  }
283
228
  if (!isBalanceAdd) {
284
- for (const kve of keysOrNodesOrEntries) {
229
+ for (const kve of keysOrNodesOrEntriesOrRawElements) {
285
230
  const value = valuesIterator?.next().value;
286
231
  const nn = this.add(kve, value);
287
232
  inserted.push(nn);
@@ -294,25 +239,36 @@ export class BST extends BinaryTree {
294
239
  return false;
295
240
  return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
296
241
  };
297
- for (const kve of keysOrNodesOrEntries) {
242
+ for (const kve of keysOrNodesOrEntriesOrRawElements) {
298
243
  isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
299
244
  }
300
245
  let sorted = [];
301
246
  sorted = realBTNExemplars.sort((a, b) => {
302
- let aR, bR;
247
+ let keyA, keyB;
303
248
  if (this.isEntry(a))
304
- aR = this.extractor(a[0]);
249
+ keyA = a[0];
305
250
  else if (this.isRealNode(a))
306
- aR = this.extractor(a.key);
307
- else
308
- aR = this.extractor(a);
251
+ keyA = a.key;
252
+ else if (this.toEntryFn) {
253
+ keyA = this.toEntryFn(a)[0];
254
+ }
255
+ else {
256
+ keyA = a;
257
+ }
309
258
  if (this.isEntry(b))
310
- bR = this.extractor(b[0]);
259
+ keyB = b[0];
311
260
  else if (this.isRealNode(b))
312
- bR = this.extractor(b.key);
313
- else
314
- bR = this.extractor(b);
315
- return aR - bR;
261
+ keyB = b.key;
262
+ else if (this.toEntryFn) {
263
+ keyB = this.toEntryFn(b)[0];
264
+ }
265
+ else {
266
+ keyB = b;
267
+ }
268
+ if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
269
+ return this.comparator(keyA, keyB);
270
+ }
271
+ return 0;
316
272
  });
317
273
  const _dfs = (arr) => {
318
274
  if (arr.length === 0)
@@ -349,32 +305,26 @@ export class BST extends BinaryTree {
349
305
  return inserted;
350
306
  }
351
307
  /**
352
- * Time Complexity: O(log n)
353
- * Space Complexity: O(k + log n)
354
- * /
355
-
356
- /**
357
308
  * Time Complexity: O(log n)
358
309
  * Space Complexity: O(k + log n)
359
310
  *
360
- * The function `getNodes` returns an array of nodes that match a given identifier, using either a
361
- * 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.
362
313
  * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
363
- * want to search for in the nodes of the binary tree. It can be of any type that is returned by the
364
- * callback function `C`.
365
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
366
- * argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
367
- * function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
368
- * @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
369
- * first node that matches the identifier. If set to true, the function will return an array
370
- * containing only the first matching node. If set to false (default), the function will continue
371
- * searching for all nodes that match the identifier and return an array containing
372
- * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
373
- * for the traversal. It can be either a key value or a node object. If it is undefined, the
374
- * traversal will start from the root of the tree.
375
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
376
- * performed on the binary tree. It can have two possible values:
377
- * @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.
378
328
  */
379
329
  getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
380
330
  beginRoot = this.ensureNode(beginRoot);
@@ -394,9 +344,9 @@ export class BST extends BinaryTree {
394
344
  return;
395
345
  // TODO potential bug
396
346
  if (callback === this._DEFAULT_CALLBACK) {
397
- if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
347
+ if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
398
348
  dfs(cur.left);
399
- if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
349
+ if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
400
350
  dfs(cur.right);
401
351
  }
402
352
  else {
@@ -418,9 +368,9 @@ export class BST extends BinaryTree {
418
368
  }
419
369
  // TODO potential bug
420
370
  if (callback === this._DEFAULT_CALLBACK) {
421
- if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
371
+ if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
422
372
  stack.push(cur.right);
423
- if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
373
+ if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
424
374
  stack.push(cur.left);
425
375
  // if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
426
376
  // if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
@@ -445,50 +395,49 @@ export class BST extends BinaryTree {
445
395
  * Time Complexity: O(log n)
446
396
  * Space Complexity: O(1)
447
397
  *
448
- * The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
449
- * callback function.
450
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
451
- * that you want to search for in the binary search tree. It can be of any type that is compatible
452
- * with the type of nodes in the tree.
453
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
454
- * the tree. It is used to determine whether a node matches the given identifier. The `callback`
455
- * function should take a node as its parameter and return a value that can be compared to the
456
- * `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
457
407
  * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
458
- * search tree. It can be either a key or a node. If it is a key, it will be converted to a node
459
- * using the `ensureNode` method. If it is not provided, the `root`
460
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
461
- * be performed when searching for nodes in the binary search tree. It is an optional parameter and
462
- * its default value is taken from the `iterationType` property of the class.
463
- * @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.
464
414
  */
465
415
  getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
466
416
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
467
417
  }
468
418
  /**
469
- * Time Complexity: O(log n)
470
- * Space Complexity: O(1)
419
+ * Time Complexity: O(k log n)
420
+ * Space Complexity: O(k + log n)
471
421
  */
472
422
  /**
473
423
  * Time Complexity: O(log n)
474
424
  * Space Complexity: O(1)
475
425
  *
476
- * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
477
- * either recursive or iterative methods.
478
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
479
- * It is used to identify the node that we want to retrieve.
480
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
481
- * type of iteration to use when searching for a node in the binary tree. It can have two possible
482
- * values:
483
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
484
- * 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.
485
434
  */
486
435
  getNodeByKey(key, iterationType = 'ITERATIVE') {
487
436
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
488
437
  }
489
438
  /**
490
- * Time complexity: O(n)
491
- * Space complexity: O(n)
439
+ * Time Complexity: O(log n)
440
+ * Space Complexity: O(k + log n)
492
441
  */
493
442
  /**
494
443
  * Time complexity: O(n)
@@ -497,15 +446,16 @@ export class BST extends BinaryTree {
497
446
  * The function overrides the depth-first search method and returns an array of the return types of
498
447
  * the callback function.
499
448
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
500
- * during the depth-first search traversal. It is an optional parameter and if not provided, a
501
- * default callback function will be used.
502
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
503
- * nodes are visited during the depth-first search. It can have one of the following values:
504
- * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
505
- * Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
506
- * value is provided, the DFS traversal will start from the root of the tree.
507
- * @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
508
- * 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
509
459
  * following values:
510
460
  * @returns The method is returning an array of the return type of the callback function.
511
461
  */
@@ -513,8 +463,8 @@ export class BST extends BinaryTree {
513
463
  return super.dfs(callback, pattern, beginRoot, iterationType, false);
514
464
  }
515
465
  /**
516
- * Time complexity: O(n)
517
- * Space complexity: O(n)
466
+ * Time Complexity: O(log n)
467
+ * Space Complexity: O(1)
518
468
  */
519
469
  /**
520
470
  * Time complexity: O(n)
@@ -523,38 +473,38 @@ export class BST extends BinaryTree {
523
473
  * The function overrides the breadth-first search method and returns an array of the return types of
524
474
  * the callback function.
525
475
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
526
- * visited during the breadth-first search traversal. It is an optional parameter and if not
527
- * provided, a default callback function will be used.
528
- * @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
529
- * traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
530
- * the tree is used as the starting point.
531
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
532
- * be performed during the breadth-first search (BFS) traversal. It determines the order in which the
533
- * nodes are visited.
534
- * @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.
535
485
  */
536
486
  bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
537
487
  return super.bfs(callback, beginRoot, iterationType, false);
538
488
  }
539
489
  /**
540
- * Time complexity: O(n)
541
- * Space complexity: O(n)
490
+ * Time Complexity: O(log n)
491
+ * Space Complexity: O(1)
542
492
  */
543
493
  /**
544
494
  * Time complexity: O(n)
545
495
  * Space complexity: O(n)
546
496
  *
547
- * The function overrides the listLevels method and returns an array of arrays containing the return
548
- * 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.
549
499
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
550
- * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
551
- * during the level listing process.
552
- * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
553
- * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
554
- * provided, the root of the binary tree is used as the starting point.
555
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
556
- * be performed on the tree. It determines the order in which the nodes are visited during the
557
- * 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:
558
508
  * @returns The method is returning a two-dimensional array of the return type of the callback
559
509
  * function.
560
510
  */
@@ -562,77 +512,42 @@ export class BST extends BinaryTree {
562
512
  return super.listLevels(callback, beginRoot, iterationType, false);
563
513
  }
564
514
  /**
565
- * Time Complexity: O(log n)
566
- * Space Complexity: O(1)
567
- */
568
- /**
569
- * Time Complexity: O(log n)
570
- * Space Complexity: O(1)
571
- *
572
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
573
- * leftmost node if the comparison result is greater than.
574
- * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
575
- * type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
576
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
577
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
578
- * the key of the leftmost node if the comparison result is greater than, and the key of the
579
- * rightmost node otherwise. If no node is found, it returns 0.
580
- */
581
- lastKey(beginRoot = this.root) {
582
- let current = this.ensureNode(beginRoot);
583
- if (!current)
584
- return undefined;
585
- if (this._variant === 'STANDARD') {
586
- // For 'STANDARD', find the rightmost node
587
- while (current.right !== undefined) {
588
- current = current.right;
589
- }
590
- }
591
- else {
592
- // For BSTVariant.MAX, find the leftmost node
593
- while (current.left !== undefined) {
594
- current = current.left;
595
- }
596
- }
597
- return current.key;
598
- }
599
- /**
600
- * Time Complexity: O(log n)
601
- * Space Complexity: O(log n)
515
+ * Time complexity: O(n)
516
+ * Space complexity: O(n)
602
517
  */
603
518
  /**
604
- * Time Complexity: O(log n)
605
- * Space Complexity: O(log n)
519
+ * Time complexity: O(n)
520
+ * Space complexity: O(n)
606
521
  *
607
- * The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
608
- * 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.
609
524
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
610
- * that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
611
- * 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.
612
527
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
613
- * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
614
- * `CP`, which is a custom type representing the comparison operator. The possible values for
615
- * `lesserOrGreater` are
616
- * @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
617
- * binary tree that you want to traverse from. It can be specified either by its key, by the node
618
- * object itself, or it can be left undefined to start the traversal from the root of the tree.
619
- * @param iterationType - The `iterationType` parameter determines the type of traversal to be
620
- * 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:
621
536
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
622
537
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
623
538
  */
624
- lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = 'LT', targetNode = this.root, iterationType = this.iterationType) {
625
- targetNode = this.ensureNode(targetNode);
539
+ lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = -1, targetNode = this.root, iterationType = this.iterationType) {
540
+ const targetNodeEnsured = this.ensureNode(targetNode);
626
541
  const ans = [];
627
- if (!targetNode)
542
+ if (!targetNodeEnsured)
628
543
  return ans;
629
544
  if (!this.root)
630
545
  return ans;
631
- const targetKey = targetNode.key;
546
+ const targetKey = targetNodeEnsured.key;
632
547
  if (iterationType === 'RECURSIVE') {
633
548
  const dfs = (cur) => {
634
- const compared = this._compare(cur.key, targetKey);
635
- if (compared === lesserOrGreater)
549
+ const compared = this.comparator(cur.key, targetKey);
550
+ if (Math.sign(compared) === lesserOrGreater)
636
551
  ans.push(callback(cur));
637
552
  if (this.isRealNode(cur.left))
638
553
  dfs(cur.left);
@@ -647,8 +562,8 @@ export class BST extends BinaryTree {
647
562
  while (queue.size > 0) {
648
563
  const cur = queue.shift();
649
564
  if (this.isRealNode(cur)) {
650
- const compared = this._compare(cur.key, targetKey);
651
- if (compared === lesserOrGreater)
565
+ const compared = this.comparator(cur.key, targetKey);
566
+ if (Math.sign(compared) === lesserOrGreater)
652
567
  ans.push(callback(cur));
653
568
  if (this.isRealNode(cur.left))
654
569
  queue.push(cur.left);
@@ -660,18 +575,19 @@ export class BST extends BinaryTree {
660
575
  }
661
576
  }
662
577
  /**
663
- * Time Complexity: O(log n)
664
- * Space Complexity: O(log n)
578
+ * Time complexity: O(n)
579
+ * Space complexity: O(n)
665
580
  */
666
581
  /**
667
- * Time Complexity: O(log n)
668
- * Space Complexity: O(log n)
582
+ * Time complexity: O(n)
583
+ * Space complexity: O(n)
669
584
  *
670
- * The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
671
- * ensures the tree is perfectly balanced.
672
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
673
- * type of iteration to use when building a balanced binary search tree. It can have two possible
674
- * 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.
675
591
  * @returns The function `perfectlyBalance` returns a boolean value.
676
592
  */
677
593
  perfectlyBalance(iterationType = this.iterationType) {
@@ -711,25 +627,19 @@ export class BST extends BinaryTree {
711
627
  }
712
628
  }
713
629
  /**
714
- * Balancing Adjustment:
715
- * 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.
716
- * 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.
717
- *
718
- * Use Cases and Efficiency:
719
- * 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.
720
- * 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).
721
- */
722
- /**
723
- * Time Complexity: O(n)
724
- * Space Complexity: O(log n)
630
+ * Time complexity: O(n)
631
+ * Space complexity: O(n)
725
632
  */
726
633
  /**
727
634
  * Time Complexity: O(n)
728
635
  * Space Complexity: O(log n)
729
636
  *
730
- * The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
731
- * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
732
- * 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.
733
643
  * @returns a boolean value.
734
644
  */
735
645
  isAVLBalanced(iterationType = this.iterationType) {
@@ -778,59 +688,44 @@ export class BST extends BinaryTree {
778
688
  return balanced;
779
689
  }
780
690
  /**
781
- * The function sets the root property of an object and updates the parent property of the new root.
782
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
783
- * can either be an object of type `NODE` or it can be `undefined`.
691
+ * Time complexity: O(n)
692
+ * Space complexity: O(n)
784
693
  */
785
- _setRoot(v) {
786
- if (v) {
787
- v.parent = undefined;
694
+ _DEFAULT_COMPARATOR = (a, b) => {
695
+ if (typeof a === 'object' && typeof b === 'object' && this.comparator === this._DEFAULT_COMPARATOR) {
696
+ throw TypeError('When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.');
788
697
  }
789
- this._root = v;
790
- }
698
+ if (a > b)
699
+ return 1;
700
+ if (a < b)
701
+ return -1;
702
+ return 0;
703
+ };
791
704
  /**
792
- * The function compares two values using a comparator function and returns whether the first value
793
- * is greater than, less than, or equal to the second value.
794
- * @param {K} a - The parameter "a" is of type K.
795
- * @param {K} b - The parameter "b" in the above code represents a K.
796
- * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
797
- * than), 'LT' (less than), or 'EQ' (equal).
798
- */
799
- _compare(a, b) {
800
- const extractedA = this.extractor(a);
801
- const extractedB = this.extractor(b);
802
- const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
803
- if (compared > 0)
804
- return 'GT';
805
- if (compared < 0)
806
- return 'LT';
807
- return 'EQ';
808
- }
705
+ * Time complexity: O(n)
706
+ * Space complexity: O(n)
707
+ */
708
+ _comparator = this._DEFAULT_COMPARATOR;
809
709
  /**
810
- * The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
811
- * `a` is less than `b` based on the specified variant.
812
- * @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
813
- * first value to be compared in the function.
814
- * @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
815
- * of the arguments for the comparison in the `_lt` function.
816
- * @returns a boolean value.
710
+ * Time Complexity: O(n)
711
+ * Space Complexity: O(log n)
817
712
  */
818
- _lt(a, b) {
819
- const extractedA = this.extractor(a);
820
- const extractedB = this.extractor(b);
821
- return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
713
+ /**
714
+ * The function returns the value of the _comparator property.
715
+ * @returns The `_comparator` property is being returned.
716
+ */
717
+ get comparator() {
718
+ return this._comparator;
822
719
  }
823
720
  /**
824
- * The function compares two values using a custom extractor function and returns true if the first
825
- * value is greater than the second value.
826
- * @param {K} a - The parameter "a" is of type K, which means it can be any type.
827
- * @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
828
- * of the arguments for the comparison in the function.
829
- * @returns a boolean value.
721
+ * The function sets the root of a tree-like structure and updates the parent property of the new
722
+ * root.
723
+ * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
830
724
  */
831
- _gt(a, b) {
832
- const extractedA = this.extractor(a);
833
- const extractedB = this.extractor(b);
834
- return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
725
+ _setRoot(v) {
726
+ if (v) {
727
+ v.parent = undefined;
728
+ }
729
+ this._root = v;
835
730
  }
836
731
  }