deque-typed 1.49.5 → 1.49.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +194 -153
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +114 -91
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/data-structures/hash/hash-map.js +35 -35
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -1
- package/dist/data-structures/heap/heap.d.ts +2 -1
- package/dist/data-structures/heap/heap.js +13 -13
- package/dist/data-structures/heap/max-heap.js +1 -1
- package/dist/data-structures/heap/min-heap.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/data-structures/matrix/matrix.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/data-structures/queue/deque.d.ts +2 -11
- package/dist/data-structures/queue/deque.js +9 -13
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +29 -25
- package/dist/data-structures/stack/stack.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +9 -5
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/index.js +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/types/data-structures/linked-list/index.js +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +1 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +255 -211
- package/src/data-structures/binary-tree/bst.ts +126 -107
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/data-structures/hash/hash-table.js +0 -281
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/hash-table.js +0 -2
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/matrix2d.js +0 -2
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/vector2d.js +0 -2
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
|
@@ -7,13 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
9
|
BSTNested,
|
|
10
|
-
BSTNKeyOrNode,
|
|
11
10
|
BSTNodeNested,
|
|
12
11
|
BSTOptions,
|
|
13
12
|
BTNCallback,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
BTNodePureExemplar
|
|
13
|
+
BTNodePureExemplar,
|
|
14
|
+
KeyOrNodeOrEntry
|
|
17
15
|
} from '../../types';
|
|
18
16
|
import { BSTVariant, CP, IterationType } from '../../types';
|
|
19
17
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
@@ -94,25 +92,23 @@ export class BST<
|
|
|
94
92
|
implements IBinaryTree<K, V, N, TREE> {
|
|
95
93
|
/**
|
|
96
94
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
97
|
-
* the tree with optional
|
|
98
|
-
* @param [
|
|
95
|
+
* the tree with optional keysOrNodesOrEntries and options.
|
|
96
|
+
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
99
97
|
* binary search tree.
|
|
100
98
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
101
99
|
* configuration options for the binary search tree. It can have the following properties:
|
|
102
100
|
*/
|
|
103
|
-
constructor(
|
|
101
|
+
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: BSTOptions<K>) {
|
|
104
102
|
super([], options);
|
|
105
103
|
|
|
106
104
|
if (options) {
|
|
107
105
|
const { variant } = options;
|
|
108
|
-
if (variant)
|
|
109
|
-
this._variant = variant;
|
|
110
|
-
}
|
|
106
|
+
if (variant) this._variant = variant;
|
|
111
107
|
}
|
|
112
108
|
|
|
113
109
|
this._root = undefined;
|
|
114
110
|
|
|
115
|
-
if (
|
|
111
|
+
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
116
112
|
}
|
|
117
113
|
|
|
118
114
|
protected override _root?: N;
|
|
@@ -121,7 +117,7 @@ export class BST<
|
|
|
121
117
|
return this._root;
|
|
122
118
|
}
|
|
123
119
|
|
|
124
|
-
protected _variant = BSTVariant.
|
|
120
|
+
protected _variant = BSTVariant.STANDARD;
|
|
125
121
|
|
|
126
122
|
get variant() {
|
|
127
123
|
return this._variant;
|
|
@@ -155,37 +151,28 @@ export class BST<
|
|
|
155
151
|
}
|
|
156
152
|
|
|
157
153
|
/**
|
|
158
|
-
* The function
|
|
159
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
160
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
161
|
-
*/
|
|
162
|
-
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
163
|
-
return exemplar instanceof BSTNode;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
154
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
168
155
|
* otherwise it returns undefined.
|
|
169
|
-
* @param
|
|
156
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
170
157
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
171
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
158
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
172
159
|
* @returns a node of type N or undefined.
|
|
173
160
|
*/
|
|
174
|
-
override exemplarToNode(
|
|
161
|
+
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
|
|
175
162
|
let node: N | undefined;
|
|
176
|
-
if (
|
|
163
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
177
164
|
return;
|
|
178
|
-
} else if (this.isNode(
|
|
179
|
-
node =
|
|
180
|
-
} else if (this.isEntry(
|
|
181
|
-
const [key, value] =
|
|
165
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
166
|
+
node = keyOrNodeOrEntry;
|
|
167
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
168
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
182
169
|
if (key === undefined || key === null) {
|
|
183
170
|
return;
|
|
184
171
|
} else {
|
|
185
172
|
node = this.createNode(key, value);
|
|
186
173
|
}
|
|
187
|
-
} else if (this.isNotNodeInstance(
|
|
188
|
-
node = this.createNode(
|
|
174
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
175
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
189
176
|
} else {
|
|
190
177
|
return;
|
|
191
178
|
}
|
|
@@ -193,13 +180,66 @@ export class BST<
|
|
|
193
180
|
}
|
|
194
181
|
|
|
195
182
|
/**
|
|
196
|
-
* Time Complexity: O(log n)
|
|
197
|
-
* Space Complexity: O(
|
|
183
|
+
* Time Complexity: O(log n)
|
|
184
|
+
* Space Complexity: O(log n)
|
|
185
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
186
|
+
*/
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Time Complexity: O(log n)
|
|
190
|
+
* Space Complexity: O(log n)
|
|
191
|
+
*
|
|
192
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
193
|
+
* otherwise it returns the key itself.
|
|
194
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
195
|
+
* `undefined`.
|
|
196
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
197
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
198
|
+
* @returns either a node object (N) or undefined.
|
|
199
|
+
*/
|
|
200
|
+
override ensureNode(
|
|
201
|
+
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>,
|
|
202
|
+
iterationType = IterationType.ITERATIVE
|
|
203
|
+
): N | undefined {
|
|
204
|
+
let res: N | undefined;
|
|
205
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
206
|
+
res = keyOrNodeOrEntry;
|
|
207
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
208
|
+
if (keyOrNodeOrEntry[0]) res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
209
|
+
} else {
|
|
210
|
+
if (keyOrNodeOrEntry) res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
211
|
+
}
|
|
212
|
+
return res;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
217
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
218
|
+
* data type.
|
|
219
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
220
|
+
*/
|
|
221
|
+
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
222
|
+
return !(potentialKey instanceof BSTNode);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
227
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
228
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
229
|
+
*/
|
|
230
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
|
|
231
|
+
return keyOrNodeOrEntry instanceof BSTNode;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Time Complexity: O(log n)
|
|
236
|
+
* Space Complexity: O(1)
|
|
237
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
198
238
|
*/
|
|
199
239
|
|
|
200
240
|
/**
|
|
201
|
-
* Time Complexity: O(log n)
|
|
202
|
-
* Space Complexity: O(1)
|
|
241
|
+
* Time Complexity: O(log n)
|
|
242
|
+
* Space Complexity: O(1)
|
|
203
243
|
*
|
|
204
244
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
205
245
|
* or inserting a new node if the key is unique.
|
|
@@ -209,14 +249,14 @@ export class BST<
|
|
|
209
249
|
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
210
250
|
* node was not added.
|
|
211
251
|
*/
|
|
212
|
-
override add(keyOrNodeOrEntry:
|
|
252
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
|
|
213
253
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
214
|
-
if (newNode === undefined) return;
|
|
254
|
+
if (newNode === undefined) return false;
|
|
215
255
|
|
|
216
256
|
if (this.root === undefined) {
|
|
217
257
|
this._setRoot(newNode);
|
|
218
258
|
this._size++;
|
|
219
|
-
return
|
|
259
|
+
return true;
|
|
220
260
|
}
|
|
221
261
|
|
|
222
262
|
let current = this.root;
|
|
@@ -225,7 +265,7 @@ export class BST<
|
|
|
225
265
|
// if (current !== newNode) {
|
|
226
266
|
// The key value is the same but the reference is different, update the value of the existing node
|
|
227
267
|
this._replaceNode(current, newNode);
|
|
228
|
-
return
|
|
268
|
+
return true;
|
|
229
269
|
|
|
230
270
|
// } else {
|
|
231
271
|
// The key value is the same and the reference is the same, replace the entire node
|
|
@@ -238,7 +278,7 @@ export class BST<
|
|
|
238
278
|
current.left = newNode;
|
|
239
279
|
newNode.parent = current;
|
|
240
280
|
this._size++;
|
|
241
|
-
return
|
|
281
|
+
return true;
|
|
242
282
|
}
|
|
243
283
|
current = current.left;
|
|
244
284
|
} else {
|
|
@@ -246,23 +286,24 @@ export class BST<
|
|
|
246
286
|
current.right = newNode;
|
|
247
287
|
newNode.parent = current;
|
|
248
288
|
this._size++;
|
|
249
|
-
return
|
|
289
|
+
return true;
|
|
250
290
|
}
|
|
251
291
|
current = current.right;
|
|
252
292
|
}
|
|
253
293
|
}
|
|
254
294
|
|
|
255
|
-
return
|
|
295
|
+
return false;
|
|
256
296
|
}
|
|
257
297
|
|
|
258
298
|
/**
|
|
259
|
-
* Time Complexity: O(k log n)
|
|
260
|
-
* Space Complexity: O(k)
|
|
299
|
+
* Time Complexity: O(k log n)
|
|
300
|
+
* Space Complexity: O(k)
|
|
301
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
261
302
|
*/
|
|
262
303
|
|
|
263
304
|
/**
|
|
264
|
-
* Time Complexity: O(k log n)
|
|
265
|
-
* Space Complexity: O(k)
|
|
305
|
+
* Time Complexity: O(k log n)
|
|
306
|
+
* Space Complexity: O(k)
|
|
266
307
|
*
|
|
267
308
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
268
309
|
* balancing the tree after each addition.
|
|
@@ -273,7 +314,7 @@ export class BST<
|
|
|
273
314
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
274
315
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
275
316
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
276
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
317
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
277
318
|
* in the order they appear in the input.
|
|
278
319
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
279
320
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
@@ -281,12 +322,12 @@ export class BST<
|
|
|
281
322
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
282
323
|
*/
|
|
283
324
|
override addMany(
|
|
284
|
-
keysOrNodesOrEntries: Iterable<
|
|
325
|
+
keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>,
|
|
285
326
|
values?: Iterable<V | undefined>,
|
|
286
327
|
isBalanceAdd = true,
|
|
287
328
|
iterationType = this.iterationType
|
|
288
|
-
):
|
|
289
|
-
const inserted:
|
|
329
|
+
): boolean[] {
|
|
330
|
+
const inserted: boolean[] = [];
|
|
290
331
|
|
|
291
332
|
let valuesIterator: Iterator<V | undefined> | undefined;
|
|
292
333
|
|
|
@@ -305,7 +346,7 @@ export class BST<
|
|
|
305
346
|
|
|
306
347
|
const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
|
|
307
348
|
|
|
308
|
-
const isRealBTNExemplar = (kve:
|
|
349
|
+
const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
|
|
309
350
|
if (kve === undefined || kve === null) return false;
|
|
310
351
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
311
352
|
};
|
|
@@ -367,30 +408,29 @@ export class BST<
|
|
|
367
408
|
}
|
|
368
409
|
|
|
369
410
|
/**
|
|
370
|
-
* Time Complexity: O(n log n)
|
|
371
|
-
* Space Complexity: O(n)
|
|
411
|
+
* Time Complexity: O(n log n)
|
|
412
|
+
* Space Complexity: O(n)
|
|
413
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
372
414
|
*/
|
|
373
415
|
|
|
374
416
|
/**
|
|
375
|
-
* Time Complexity: O(log n)
|
|
376
|
-
* Space Complexity: O(
|
|
417
|
+
* Time Complexity: O(n log n)
|
|
418
|
+
* Space Complexity: O(n)
|
|
377
419
|
*
|
|
378
420
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
379
421
|
* leftmost node if the comparison result is greater than.
|
|
380
422
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
381
423
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
382
424
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
383
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
384
|
-
* be performed. It can have one of the following values:
|
|
385
425
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
386
426
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
387
427
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
388
428
|
*/
|
|
389
|
-
lastKey(beginRoot:
|
|
429
|
+
lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
|
|
390
430
|
let current = this.ensureNode(beginRoot);
|
|
391
431
|
if (!current) return undefined;
|
|
392
432
|
|
|
393
|
-
if (this._variant === BSTVariant.
|
|
433
|
+
if (this._variant === BSTVariant.STANDARD) {
|
|
394
434
|
// For BSTVariant.MIN, find the rightmost node
|
|
395
435
|
while (current.right !== undefined) {
|
|
396
436
|
current = current.right;
|
|
@@ -405,13 +445,13 @@ export class BST<
|
|
|
405
445
|
}
|
|
406
446
|
|
|
407
447
|
/**
|
|
408
|
-
* Time Complexity: O(log n)
|
|
409
|
-
* Space Complexity: O(1)
|
|
448
|
+
* Time Complexity: O(log n)
|
|
449
|
+
* Space Complexity: O(1)
|
|
410
450
|
*/
|
|
411
451
|
|
|
412
452
|
/**
|
|
413
|
-
* Time Complexity: O(log n)
|
|
414
|
-
* Space Complexity: O(
|
|
453
|
+
* Time Complexity: O(log n)
|
|
454
|
+
* Space Complexity: O(1)
|
|
415
455
|
*
|
|
416
456
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
417
457
|
* either recursive or iterative methods.
|
|
@@ -449,36 +489,14 @@ export class BST<
|
|
|
449
489
|
}
|
|
450
490
|
|
|
451
491
|
/**
|
|
452
|
-
*
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
*
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
463
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
464
|
-
*/
|
|
465
|
-
|
|
466
|
-
/**
|
|
467
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
468
|
-
* otherwise it returns the key itself.
|
|
469
|
-
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
470
|
-
* `undefined`.
|
|
471
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
472
|
-
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
473
|
-
* @returns either a node object (N) or undefined.
|
|
474
|
-
*/
|
|
475
|
-
override ensureNode(key: BSTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | undefined {
|
|
476
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
/**
|
|
480
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
481
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
492
|
+
* Time Complexity: O(log n)
|
|
493
|
+
* Space Complexity: O(log n)
|
|
494
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
495
|
+
* /
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Time Complexity: O(log n)
|
|
499
|
+
* Space Complexity: O(log n)
|
|
482
500
|
*
|
|
483
501
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
484
502
|
* recursive or iterative approach.
|
|
@@ -503,7 +521,7 @@ export class BST<
|
|
|
503
521
|
identifier: ReturnType<C> | undefined,
|
|
504
522
|
callback: C = this._defaultOneParamCallback as C,
|
|
505
523
|
onlyOne = false,
|
|
506
|
-
beginRoot:
|
|
524
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
507
525
|
iterationType = this.iterationType
|
|
508
526
|
): N[] {
|
|
509
527
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -556,13 +574,14 @@ export class BST<
|
|
|
556
574
|
}
|
|
557
575
|
|
|
558
576
|
/**
|
|
559
|
-
* Time Complexity: O(log n)
|
|
560
|
-
* Space Complexity: O(log n)
|
|
577
|
+
* Time Complexity: O(log n)
|
|
578
|
+
* Space Complexity: O(log n)
|
|
579
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
561
580
|
*/
|
|
562
581
|
|
|
563
582
|
/**
|
|
564
|
-
* Time Complexity: O(log n)
|
|
565
|
-
* Space Complexity: O(log n)
|
|
583
|
+
* Time Complexity: O(log n)
|
|
584
|
+
* Space Complexity: O(log n)
|
|
566
585
|
*
|
|
567
586
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
568
587
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -584,7 +603,7 @@ export class BST<
|
|
|
584
603
|
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
|
|
585
604
|
callback: C = this._defaultOneParamCallback as C,
|
|
586
605
|
lesserOrGreater: CP = CP.lt,
|
|
587
|
-
targetNode:
|
|
606
|
+
targetNode: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
588
607
|
iterationType = this.iterationType
|
|
589
608
|
): ReturnType<C>[] {
|
|
590
609
|
targetNode = this.ensureNode(targetNode);
|
|
@@ -623,13 +642,13 @@ export class BST<
|
|
|
623
642
|
}
|
|
624
643
|
|
|
625
644
|
/**
|
|
626
|
-
* Time Complexity: O(log n)
|
|
627
|
-
* Space Complexity: O(log n)
|
|
645
|
+
* Time Complexity: O(log n)
|
|
646
|
+
* Space Complexity: O(log n)
|
|
628
647
|
*/
|
|
629
648
|
|
|
630
649
|
/**
|
|
631
|
-
* Time Complexity: O(n)
|
|
632
|
-
* Space Complexity: O(n)
|
|
650
|
+
* Time Complexity: O(log n)
|
|
651
|
+
* Space Complexity: O(log n)
|
|
633
652
|
*
|
|
634
653
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
635
654
|
* ensures the tree is perfectly balanced.
|
|
@@ -691,8 +710,8 @@ export class BST<
|
|
|
691
710
|
*/
|
|
692
711
|
|
|
693
712
|
/**
|
|
694
|
-
* Time Complexity: O(n)
|
|
695
|
-
* Space Complexity: O(log n)
|
|
713
|
+
* Time Complexity: O(n)
|
|
714
|
+
* Space Complexity: O(log n)
|
|
696
715
|
*
|
|
697
716
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
698
717
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -761,7 +780,7 @@ export class BST<
|
|
|
761
780
|
protected _compare(a: K, b: K): CP {
|
|
762
781
|
const extractedA = this.extractor(a);
|
|
763
782
|
const extractedB = this.extractor(b);
|
|
764
|
-
const compared = this.variant === BSTVariant.
|
|
783
|
+
const compared = this.variant === BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA;
|
|
765
784
|
|
|
766
785
|
return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
|
|
767
786
|
}
|