deque-typed 1.49.4 → 1.49.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/iterable-base.d.ts +1 -1
- 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 +154 -143
- package/dist/data-structures/binary-tree/binary-tree.js +211 -198
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +113 -89
- 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 -55
- package/dist/data-structures/binary-tree/tree-multimap.js +59 -94
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/data-structures/graph/abstract-graph.js +3 -2
- package/dist/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/data-structures/hash/hash-map.js +2 -2
- package/dist/data-structures/heap/heap.js +2 -3
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/index.d.ts +0 -2
- package/dist/data-structures/matrix/index.js +0 -2
- package/dist/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/data-structures/matrix/matrix.js +400 -15
- package/dist/data-structures/queue/deque.d.ts +2 -2
- package/dist/data-structures/queue/deque.js +5 -7
- package/dist/data-structures/queue/queue.d.ts +1 -1
- 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/base/base.d.ts +1 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/utils/utils.d.ts +1 -0
- package/dist/utils/utils.js +6 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -1
- package/src/data-structures/base/iterable-base.ts +7 -10
- package/src/data-structures/binary-tree/avl-tree.ts +73 -61
- package/src/data-structures/binary-tree/binary-tree.ts +301 -270
- package/src/data-structures/binary-tree/bst.ts +139 -115
- package/src/data-structures/binary-tree/rb-tree.ts +81 -73
- package/src/data-structures/binary-tree/tree-multimap.ts +72 -103
- package/src/data-structures/graph/abstract-graph.ts +13 -11
- package/src/data-structures/graph/directed-graph.ts +1 -3
- package/src/data-structures/graph/map-graph.ts +6 -1
- package/src/data-structures/graph/undirected-graph.ts +3 -6
- package/src/data-structures/hash/hash-map.ts +18 -16
- package/src/data-structures/heap/heap.ts +7 -10
- package/src/data-structures/heap/max-heap.ts +2 -1
- package/src/data-structures/heap/min-heap.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/matrix/index.ts +0 -2
- package/src/data-structures/matrix/matrix.ts +442 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
- package/src/data-structures/queue/deque.ts +18 -39
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +9 -4
- package/src/types/common.ts +5 -5
- package/src/types/data-structures/base/base.ts +14 -3
- package/src/types/data-structures/base/index.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +4 -2
- package/src/types/data-structures/hash/hash-map.ts +3 -3
- package/src/types/data-structures/heap/heap.ts +2 -2
- package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/utils/utils.ts +7 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/data-structures/matrix/matrix2d.js +0 -199
- package/dist/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/data-structures/matrix/vector2d.js +0 -290
- package/src/data-structures/matrix/matrix2d.ts +0 -211
- package/src/data-structures/matrix/vector2d.ts +0 -315
|
@@ -7,20 +7,22 @@
|
|
|
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';
|
|
20
18
|
import { IBinaryTree } from '../../interfaces';
|
|
21
19
|
import { Queue } from '../queue';
|
|
22
20
|
|
|
23
|
-
export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<
|
|
21
|
+
export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<
|
|
22
|
+
K,
|
|
23
|
+
V,
|
|
24
|
+
N
|
|
25
|
+
> {
|
|
24
26
|
override parent?: N;
|
|
25
27
|
|
|
26
28
|
constructor(key: K, value?: V) {
|
|
@@ -80,20 +82,23 @@ export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNeste
|
|
|
80
82
|
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
81
83
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
82
84
|
*/
|
|
83
|
-
export class BST<
|
|
85
|
+
export class BST<
|
|
86
|
+
K = any,
|
|
87
|
+
V = any,
|
|
88
|
+
N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
89
|
+
TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>
|
|
90
|
+
>
|
|
84
91
|
extends BinaryTree<K, V, N, TREE>
|
|
85
92
|
implements IBinaryTree<K, V, N, TREE> {
|
|
86
|
-
|
|
87
|
-
|
|
88
93
|
/**
|
|
89
94
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
90
|
-
* the tree with optional
|
|
91
|
-
* @param [
|
|
95
|
+
* the tree with optional nodes and options.
|
|
96
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
92
97
|
* binary search tree.
|
|
93
98
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
94
99
|
* configuration options for the binary search tree. It can have the following properties:
|
|
95
100
|
*/
|
|
96
|
-
constructor(
|
|
101
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BSTOptions<K>>) {
|
|
97
102
|
super([], options);
|
|
98
103
|
|
|
99
104
|
if (options) {
|
|
@@ -105,7 +110,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
105
110
|
|
|
106
111
|
this._root = undefined;
|
|
107
112
|
|
|
108
|
-
if (
|
|
113
|
+
if (nodes) this.addMany(nodes);
|
|
109
114
|
}
|
|
110
115
|
|
|
111
116
|
protected override _root?: N;
|
|
@@ -114,7 +119,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
114
119
|
return this._root;
|
|
115
120
|
}
|
|
116
121
|
|
|
117
|
-
protected _variant = BSTVariant.
|
|
122
|
+
protected _variant = BSTVariant.STANDARD;
|
|
118
123
|
|
|
119
124
|
get variant() {
|
|
120
125
|
return this._variant;
|
|
@@ -142,43 +147,34 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
142
147
|
override createTree(options?: Partial<BSTOptions<K>>): TREE {
|
|
143
148
|
return new BST<K, V, N, TREE>([], {
|
|
144
149
|
iterationType: this.iterationType,
|
|
145
|
-
variant: this.variant,
|
|
150
|
+
variant: this.variant,
|
|
151
|
+
...options
|
|
146
152
|
}) as TREE;
|
|
147
153
|
}
|
|
148
154
|
|
|
149
155
|
/**
|
|
150
|
-
* The function
|
|
151
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
152
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
153
|
-
*/
|
|
154
|
-
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
155
|
-
return exemplar instanceof BSTNode;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
156
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
161
157
|
* otherwise it returns undefined.
|
|
162
|
-
* @param
|
|
158
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
163
159
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
164
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
160
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
165
161
|
* @returns a node of type N or undefined.
|
|
166
162
|
*/
|
|
167
|
-
override exemplarToNode(
|
|
163
|
+
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
|
|
168
164
|
let node: N | undefined;
|
|
169
|
-
if (
|
|
165
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
170
166
|
return;
|
|
171
|
-
} else if (this.isNode(
|
|
172
|
-
node =
|
|
173
|
-
} else if (this.isEntry(
|
|
174
|
-
const [key, value] =
|
|
167
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
168
|
+
node = keyOrNodeOrEntry;
|
|
169
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
170
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
175
171
|
if (key === undefined || key === null) {
|
|
176
172
|
return;
|
|
177
173
|
} else {
|
|
178
174
|
node = this.createNode(key, value);
|
|
179
175
|
}
|
|
180
|
-
} else if (this.isNotNodeInstance(
|
|
181
|
-
node = this.createNode(
|
|
176
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
177
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
182
178
|
} else {
|
|
183
179
|
return;
|
|
184
180
|
}
|
|
@@ -186,13 +182,66 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
186
182
|
}
|
|
187
183
|
|
|
188
184
|
/**
|
|
189
|
-
* Time Complexity: O(log n)
|
|
190
|
-
* Space Complexity: O(
|
|
185
|
+
* Time Complexity: O(log n)
|
|
186
|
+
* Space Complexity: O(log n)
|
|
187
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
188
|
+
*/
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Time Complexity: O(log n)
|
|
192
|
+
* Space Complexity: O(log n)
|
|
193
|
+
*
|
|
194
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
195
|
+
* otherwise it returns the key itself.
|
|
196
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
197
|
+
* `undefined`.
|
|
198
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
199
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
200
|
+
* @returns either a node object (N) or undefined.
|
|
201
|
+
*/
|
|
202
|
+
override ensureNode(
|
|
203
|
+
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>,
|
|
204
|
+
iterationType = IterationType.ITERATIVE
|
|
205
|
+
): N | undefined {
|
|
206
|
+
let res: N | undefined;
|
|
207
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
208
|
+
res = keyOrNodeOrEntry;
|
|
209
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
210
|
+
if (keyOrNodeOrEntry[0]) res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
211
|
+
} else {
|
|
212
|
+
if (keyOrNodeOrEntry) res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
213
|
+
}
|
|
214
|
+
return res;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
219
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
220
|
+
* data type.
|
|
221
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
222
|
+
*/
|
|
223
|
+
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
224
|
+
return !(potentialKey instanceof BSTNode);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
229
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
230
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
231
|
+
*/
|
|
232
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
|
|
233
|
+
return keyOrNodeOrEntry instanceof BSTNode;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Time Complexity: O(log n)
|
|
238
|
+
* Space Complexity: O(1)
|
|
239
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
191
240
|
*/
|
|
192
241
|
|
|
193
242
|
/**
|
|
194
|
-
* Time Complexity: O(log n)
|
|
195
|
-
* Space Complexity: O(1)
|
|
243
|
+
* Time Complexity: O(log n)
|
|
244
|
+
* Space Complexity: O(1)
|
|
196
245
|
*
|
|
197
246
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
198
247
|
* or inserting a new node if the key is unique.
|
|
@@ -202,14 +251,14 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
202
251
|
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
203
252
|
* node was not added.
|
|
204
253
|
*/
|
|
205
|
-
override add(keyOrNodeOrEntry:
|
|
254
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
|
|
206
255
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
207
|
-
if (newNode === undefined) return;
|
|
256
|
+
if (newNode === undefined) return false;
|
|
208
257
|
|
|
209
258
|
if (this.root === undefined) {
|
|
210
259
|
this._setRoot(newNode);
|
|
211
260
|
this._size++;
|
|
212
|
-
return
|
|
261
|
+
return true;
|
|
213
262
|
}
|
|
214
263
|
|
|
215
264
|
let current = this.root;
|
|
@@ -218,7 +267,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
218
267
|
// if (current !== newNode) {
|
|
219
268
|
// The key value is the same but the reference is different, update the value of the existing node
|
|
220
269
|
this._replaceNode(current, newNode);
|
|
221
|
-
return
|
|
270
|
+
return true;
|
|
222
271
|
|
|
223
272
|
// } else {
|
|
224
273
|
// The key value is the same and the reference is the same, replace the entire node
|
|
@@ -231,7 +280,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
231
280
|
current.left = newNode;
|
|
232
281
|
newNode.parent = current;
|
|
233
282
|
this._size++;
|
|
234
|
-
return
|
|
283
|
+
return true;
|
|
235
284
|
}
|
|
236
285
|
current = current.left;
|
|
237
286
|
} else {
|
|
@@ -239,23 +288,24 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
239
288
|
current.right = newNode;
|
|
240
289
|
newNode.parent = current;
|
|
241
290
|
this._size++;
|
|
242
|
-
return
|
|
291
|
+
return true;
|
|
243
292
|
}
|
|
244
293
|
current = current.right;
|
|
245
294
|
}
|
|
246
295
|
}
|
|
247
296
|
|
|
248
|
-
return
|
|
297
|
+
return false;
|
|
249
298
|
}
|
|
250
299
|
|
|
251
300
|
/**
|
|
252
|
-
* Time Complexity: O(k log n)
|
|
253
|
-
* Space Complexity: O(k)
|
|
301
|
+
* Time Complexity: O(k log n)
|
|
302
|
+
* Space Complexity: O(k)
|
|
303
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
254
304
|
*/
|
|
255
305
|
|
|
256
306
|
/**
|
|
257
|
-
* Time Complexity: O(k log n)
|
|
258
|
-
* Space Complexity: O(k)
|
|
307
|
+
* Time Complexity: O(k log n)
|
|
308
|
+
* Space Complexity: O(k)
|
|
259
309
|
*
|
|
260
310
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
261
311
|
* balancing the tree after each addition.
|
|
@@ -266,7 +316,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
266
316
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
267
317
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
268
318
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
269
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
319
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
270
320
|
* in the order they appear in the input.
|
|
271
321
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
272
322
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
@@ -274,12 +324,12 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
274
324
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
275
325
|
*/
|
|
276
326
|
override addMany(
|
|
277
|
-
keysOrNodesOrEntries: Iterable<
|
|
327
|
+
keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>,
|
|
278
328
|
values?: Iterable<V | undefined>,
|
|
279
329
|
isBalanceAdd = true,
|
|
280
330
|
iterationType = this.iterationType
|
|
281
|
-
):
|
|
282
|
-
const inserted:
|
|
331
|
+
): boolean[] {
|
|
332
|
+
const inserted: boolean[] = [];
|
|
283
333
|
|
|
284
334
|
let valuesIterator: Iterator<V | undefined> | undefined;
|
|
285
335
|
|
|
@@ -298,10 +348,10 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
298
348
|
|
|
299
349
|
const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
|
|
300
350
|
|
|
301
|
-
const isRealBTNExemplar = (kve:
|
|
351
|
+
const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
|
|
302
352
|
if (kve === undefined || kve === null) return false;
|
|
303
353
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
304
|
-
}
|
|
354
|
+
};
|
|
305
355
|
|
|
306
356
|
for (const kve of keysOrNodesOrEntries) {
|
|
307
357
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
@@ -359,32 +409,30 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
359
409
|
return inserted;
|
|
360
410
|
}
|
|
361
411
|
|
|
362
|
-
|
|
363
412
|
/**
|
|
364
|
-
* Time Complexity: O(n log n)
|
|
365
|
-
* Space Complexity: O(n)
|
|
413
|
+
* Time Complexity: O(n log n)
|
|
414
|
+
* Space Complexity: O(n)
|
|
415
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
366
416
|
*/
|
|
367
417
|
|
|
368
418
|
/**
|
|
369
|
-
* Time Complexity: O(log n)
|
|
370
|
-
* Space Complexity: O(
|
|
419
|
+
* Time Complexity: O(n log n)
|
|
420
|
+
* Space Complexity: O(n)
|
|
371
421
|
*
|
|
372
422
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
373
423
|
* leftmost node if the comparison result is greater than.
|
|
374
424
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
375
425
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
376
426
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
377
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
378
|
-
* be performed. It can have one of the following values:
|
|
379
427
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
380
428
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
381
429
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
382
430
|
*/
|
|
383
|
-
lastKey(beginRoot:
|
|
431
|
+
lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
|
|
384
432
|
let current = this.ensureNode(beginRoot);
|
|
385
433
|
if (!current) return undefined;
|
|
386
434
|
|
|
387
|
-
if (this._variant === BSTVariant.
|
|
435
|
+
if (this._variant === BSTVariant.STANDARD) {
|
|
388
436
|
// For BSTVariant.MIN, find the rightmost node
|
|
389
437
|
while (current.right !== undefined) {
|
|
390
438
|
current = current.right;
|
|
@@ -398,15 +446,14 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
398
446
|
return current.key;
|
|
399
447
|
}
|
|
400
448
|
|
|
401
|
-
|
|
402
449
|
/**
|
|
403
|
-
* Time Complexity: O(log n)
|
|
404
|
-
* Space Complexity: O(1)
|
|
450
|
+
* Time Complexity: O(log n)
|
|
451
|
+
* Space Complexity: O(1)
|
|
405
452
|
*/
|
|
406
453
|
|
|
407
454
|
/**
|
|
408
|
-
* Time Complexity: O(log n)
|
|
409
|
-
* Space Complexity: O(
|
|
455
|
+
* Time Complexity: O(log n)
|
|
456
|
+
* Space Complexity: O(1)
|
|
410
457
|
*
|
|
411
458
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
412
459
|
* either recursive or iterative methods.
|
|
@@ -444,36 +491,14 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
444
491
|
}
|
|
445
492
|
|
|
446
493
|
/**
|
|
447
|
-
*
|
|
448
|
-
*
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
/**
|
|
457
|
-
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
458
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
459
|
-
*/
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
463
|
-
* otherwise it returns the key itself.
|
|
464
|
-
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
465
|
-
* `undefined`.
|
|
466
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
467
|
-
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
468
|
-
* @returns either a node object (N) or undefined.
|
|
469
|
-
*/
|
|
470
|
-
override ensureNode(key: BSTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | undefined {
|
|
471
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
/**
|
|
475
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
476
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
494
|
+
* Time Complexity: O(log n)
|
|
495
|
+
* Space Complexity: O(log n)
|
|
496
|
+
* 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.
|
|
497
|
+
* /
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Time Complexity: O(log n)
|
|
501
|
+
* Space Complexity: O(log n)
|
|
477
502
|
*
|
|
478
503
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
479
504
|
* recursive or iterative approach.
|
|
@@ -498,7 +523,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
498
523
|
identifier: ReturnType<C> | undefined,
|
|
499
524
|
callback: C = this._defaultOneParamCallback as C,
|
|
500
525
|
onlyOne = false,
|
|
501
|
-
beginRoot:
|
|
526
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
502
527
|
iterationType = this.iterationType
|
|
503
528
|
): N[] {
|
|
504
529
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -551,13 +576,14 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
551
576
|
}
|
|
552
577
|
|
|
553
578
|
/**
|
|
554
|
-
* Time Complexity: O(log n)
|
|
555
|
-
* Space Complexity: O(log n)
|
|
579
|
+
* Time Complexity: O(log n)
|
|
580
|
+
* Space Complexity: O(log n)
|
|
581
|
+
* 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.
|
|
556
582
|
*/
|
|
557
583
|
|
|
558
584
|
/**
|
|
559
|
-
* Time Complexity: O(log n)
|
|
560
|
-
* Space Complexity: O(log n)
|
|
585
|
+
* Time Complexity: O(log n)
|
|
586
|
+
* Space Complexity: O(log n)
|
|
561
587
|
*
|
|
562
588
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
563
589
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -579,7 +605,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
579
605
|
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
|
|
580
606
|
callback: C = this._defaultOneParamCallback as C,
|
|
581
607
|
lesserOrGreater: CP = CP.lt,
|
|
582
|
-
targetNode:
|
|
608
|
+
targetNode: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
583
609
|
iterationType = this.iterationType
|
|
584
610
|
): ReturnType<C>[] {
|
|
585
611
|
targetNode = this.ensureNode(targetNode);
|
|
@@ -618,13 +644,13 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
618
644
|
}
|
|
619
645
|
|
|
620
646
|
/**
|
|
621
|
-
* Time Complexity: O(log n)
|
|
622
|
-
* Space Complexity: O(log n)
|
|
647
|
+
* Time Complexity: O(log n)
|
|
648
|
+
* Space Complexity: O(log n)
|
|
623
649
|
*/
|
|
624
650
|
|
|
625
651
|
/**
|
|
626
|
-
* Time Complexity: O(n)
|
|
627
|
-
* Space Complexity: O(n)
|
|
652
|
+
* Time Complexity: O(log n)
|
|
653
|
+
* Space Complexity: O(log n)
|
|
628
654
|
*
|
|
629
655
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
630
656
|
* ensures the tree is perfectly balanced.
|
|
@@ -686,8 +712,8 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
686
712
|
*/
|
|
687
713
|
|
|
688
714
|
/**
|
|
689
|
-
* Time Complexity: O(n)
|
|
690
|
-
* Space Complexity: O(log n)
|
|
715
|
+
* Time Complexity: O(n)
|
|
716
|
+
* Space Complexity: O(log n)
|
|
691
717
|
*
|
|
692
718
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
693
719
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -738,7 +764,6 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
738
764
|
return balanced;
|
|
739
765
|
}
|
|
740
766
|
|
|
741
|
-
|
|
742
767
|
protected _setRoot(v: N | undefined) {
|
|
743
768
|
if (v) {
|
|
744
769
|
v.parent = undefined;
|
|
@@ -757,9 +782,8 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
757
782
|
protected _compare(a: K, b: K): CP {
|
|
758
783
|
const extractedA = this.extractor(a);
|
|
759
784
|
const extractedB = this.extractor(b);
|
|
760
|
-
const compared = this.variant === BSTVariant.
|
|
785
|
+
const compared = this.variant === BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA;
|
|
761
786
|
|
|
762
787
|
return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
|
|
763
788
|
}
|
|
764
|
-
|
|
765
789
|
}
|