min-heap-typed 1.42.8 → 1.42.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.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/data-structures/binary-tree/binary-tree.js +388 -201
- package/dist/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/data-structures/binary-tree/bst.js +121 -67
- package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/data-structures/graph/abstract-graph.js +147 -36
- package/dist/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/data-structures/graph/directed-graph.js +126 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/data-structures/graph/undirected-graph.js +63 -0
- package/dist/data-structures/heap/heap.d.ts +175 -12
- package/dist/data-structures/heap/heap.js +175 -12
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/data-structures/queue/deque.d.ts +113 -3
- package/dist/data-structures/queue/deque.js +113 -3
- package/dist/data-structures/queue/queue.d.ts +87 -0
- package/dist/data-structures/queue/queue.js +87 -0
- package/dist/data-structures/stack/stack.d.ts +42 -0
- package/dist/data-structures/stack/stack.js +42 -0
- package/dist/data-structures/trie/trie.d.ts +76 -0
- package/dist/data-structures/trie/trie.js +76 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +97 -23
- package/src/data-structures/binary-tree/binary-tree.ts +419 -204
- package/src/data-structures/binary-tree/bst.ts +130 -68
- package/src/data-structures/binary-tree/rb-tree.ts +106 -19
- package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
- package/src/data-structures/graph/abstract-graph.ts +133 -7
- package/src/data-structures/graph/directed-graph.ts +145 -1
- package/src/data-structures/graph/undirected-graph.ts +72 -0
- package/src/data-structures/heap/heap.ts +201 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
- package/src/data-structures/queue/deque.ts +127 -3
- package/src/data-structures/queue/queue.ts +99 -0
- package/src/data-structures/stack/stack.ts +48 -0
- package/src/data-structures/trie/trie.ts +87 -4
|
@@ -66,11 +66,11 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
66
66
|
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
67
67
|
extends BinaryTree<V, N>
|
|
68
68
|
implements IBinaryTree<V, N> {
|
|
69
|
+
|
|
69
70
|
/**
|
|
70
|
-
* The constructor function initializes a binary search tree
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* binary search tree.
|
|
71
|
+
* The constructor function initializes a binary search tree with an optional comparator function.
|
|
72
|
+
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
73
|
+
* for the binary search tree.
|
|
74
74
|
*/
|
|
75
75
|
constructor(options?: BSTOptions) {
|
|
76
76
|
super(options);
|
|
@@ -105,14 +105,22 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
*
|
|
115
|
-
*
|
|
108
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
109
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
115
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
116
|
+
*
|
|
117
|
+
* The `add` function adds a new node to a binary search tree based on the provided key and value.
|
|
118
|
+
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
119
|
+
* following types:
|
|
120
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
121
|
+
* key or node being added to the binary search tree.
|
|
122
|
+
* @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
|
|
123
|
+
* no node was inserted, it returns `undefined`.
|
|
116
124
|
*/
|
|
117
125
|
override add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined {
|
|
118
126
|
if (keyOrNode === null) return undefined;
|
|
@@ -182,17 +190,29 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
182
190
|
}
|
|
183
191
|
|
|
184
192
|
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
* @
|
|
193
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
194
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
195
|
+
*/
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
199
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
200
|
+
*
|
|
201
|
+
* The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
|
|
202
|
+
* to a binary search tree.
|
|
203
|
+
* @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
|
|
204
|
+
* binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
|
|
205
|
+
* node), or `undefined`.
|
|
206
|
+
* @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
|
|
207
|
+
* nodes being added. If provided, the length of the `data` array must be the same as the length of
|
|
208
|
+
* the `keysOrNodes` array.
|
|
209
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
210
|
+
* adding the nodes. The default value is `true`.
|
|
211
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
212
|
+
* type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
|
|
213
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
214
|
+
* current instance of the binary search tree
|
|
215
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
196
216
|
*/
|
|
197
217
|
override addMany(
|
|
198
218
|
keysOrNodes: (BTNKey | N | undefined)[],
|
|
@@ -274,16 +294,21 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
274
294
|
}
|
|
275
295
|
|
|
276
296
|
/**
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
*
|
|
283
|
-
*
|
|
297
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
298
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
299
|
+
*/
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
303
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
304
|
+
*
|
|
305
|
+
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
306
|
+
* leftmost node if the comparison result is greater than.
|
|
307
|
+
* @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
308
|
+
* type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
309
|
+
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
284
310
|
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
285
|
-
* be performed
|
|
286
|
-
* pre-order, in-order, or post-order.
|
|
311
|
+
* be performed. It can have one of the following values:
|
|
287
312
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
288
313
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
289
314
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
@@ -295,10 +320,18 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
295
320
|
}
|
|
296
321
|
|
|
297
322
|
/**
|
|
323
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
324
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
325
|
+
*/
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
329
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
330
|
+
*
|
|
298
331
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
299
332
|
* either recursive or iterative methods.
|
|
300
333
|
* @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
301
|
-
* It is used to
|
|
334
|
+
* It is used to identify the node that we want to retrieve.
|
|
302
335
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
303
336
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
304
337
|
* values:
|
|
@@ -344,25 +377,32 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
344
377
|
}
|
|
345
378
|
|
|
346
379
|
/**
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
* a
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
* @param {
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
* @param
|
|
364
|
-
*
|
|
365
|
-
*
|
|
380
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
381
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
382
|
+
*/
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
386
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
387
|
+
*
|
|
388
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
389
|
+
* recursive or iterative approach.
|
|
390
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
391
|
+
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
392
|
+
* callback function `C`.
|
|
393
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
|
|
394
|
+
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
395
|
+
* function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
|
|
396
|
+
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
397
|
+
* first node that matches the identifier. If set to true, the function will return an array
|
|
398
|
+
* containing only the first matching node. If set to false (default), the function will continue
|
|
399
|
+
* searching for all nodes that match the identifier and return an array containing
|
|
400
|
+
* @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
401
|
+
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
402
|
+
* traversal will start from the root of the tree.
|
|
403
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
404
|
+
* performed on the binary tree. It can have two possible values:
|
|
405
|
+
* @returns The method returns an array of nodes (`N[]`).
|
|
366
406
|
*/
|
|
367
407
|
override getNodes<C extends BTNCallback<N>>(
|
|
368
408
|
identifier: ReturnType<C> | undefined,
|
|
@@ -420,24 +460,31 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
420
460
|
return ans;
|
|
421
461
|
}
|
|
422
462
|
|
|
423
|
-
|
|
463
|
+
/**
|
|
464
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
465
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
466
|
+
*/
|
|
424
467
|
|
|
425
468
|
/**
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
*
|
|
469
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
470
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
471
|
+
*
|
|
472
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
473
|
+
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
474
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
475
|
+
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
|
|
476
|
+
* parameter of type `N` (the node type) and returns a value of any type.
|
|
431
477
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
432
|
-
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It
|
|
433
|
-
*
|
|
434
|
-
*
|
|
435
|
-
*
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
* @param iterationType - The `iterationType` parameter determines
|
|
439
|
-
*
|
|
440
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of
|
|
478
|
+
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
479
|
+
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
480
|
+
* `lesserOrGreater` are
|
|
481
|
+
* @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
482
|
+
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
483
|
+
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
484
|
+
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
485
|
+
* performed on the binary tree. It can have two possible values:
|
|
486
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
487
|
+
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
441
488
|
*/
|
|
442
489
|
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
|
|
443
490
|
callback: C = this._defaultOneParamCallback as C,
|
|
@@ -491,6 +538,14 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
491
538
|
*/
|
|
492
539
|
|
|
493
540
|
/**
|
|
541
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
542
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
543
|
+
*/
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
547
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
548
|
+
*
|
|
494
549
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
495
550
|
* ensures the tree is perfectly balanced.
|
|
496
551
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
@@ -537,6 +592,14 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
537
592
|
}
|
|
538
593
|
|
|
539
594
|
/**
|
|
595
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
596
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
597
|
+
*/
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
601
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
602
|
+
*
|
|
540
603
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
541
604
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
542
605
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
@@ -610,5 +673,4 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
610
673
|
else return CP.eq;
|
|
611
674
|
}
|
|
612
675
|
|
|
613
|
-
// --- end additional functions
|
|
614
676
|
}
|
|
@@ -39,6 +39,11 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
39
39
|
implements IBinaryTree<V, N>
|
|
40
40
|
{
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* The constructor function initializes a Red-Black Tree with an optional set of options.
|
|
44
|
+
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
45
|
+
* passed to the constructor. It is used to configure the RBTree object with specific options.
|
|
46
|
+
*/
|
|
42
47
|
constructor(options?: RBTreeOptions) {
|
|
43
48
|
super(options);
|
|
44
49
|
this._root = this.NIL;
|
|
@@ -59,6 +64,14 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
59
64
|
NIL: N = new RedBlackTreeNode<V>(NaN) as unknown as N;
|
|
60
65
|
|
|
61
66
|
/**
|
|
67
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
68
|
+
* Space Complexity: O(1)
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
73
|
+
* Space Complexity: O(1)
|
|
74
|
+
*
|
|
62
75
|
* The `add` function adds a new node to a Red-Black Tree data structure.
|
|
63
76
|
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
64
77
|
* following types:
|
|
@@ -122,8 +135,16 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
122
135
|
override createNode(key: BTNKey, value?: V, color: RBTNColor = RBTNColor.BLACK): N {
|
|
123
136
|
return new RedBlackTreeNode<V, N>(key, value, color) as N;
|
|
124
137
|
}
|
|
125
|
-
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
141
|
+
* Space Complexity: O(1)
|
|
142
|
+
*/
|
|
143
|
+
|
|
126
144
|
/**
|
|
145
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
146
|
+
* Space Complexity: O(1)
|
|
147
|
+
*
|
|
127
148
|
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
128
149
|
* the tree accordingly.
|
|
129
150
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
@@ -222,19 +243,30 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
222
243
|
): N | undefined;
|
|
223
244
|
|
|
224
245
|
/**
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
* @param
|
|
236
|
-
*
|
|
237
|
-
*
|
|
246
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
247
|
+
* Space Complexity: O(1)
|
|
248
|
+
*/
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
252
|
+
* Space Complexity: O(1)
|
|
253
|
+
*
|
|
254
|
+
* The function `getNode` retrieves a single node from a binary tree based on a given identifier and
|
|
255
|
+
* callback function.
|
|
256
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value used to
|
|
257
|
+
* identify the node you want to retrieve. It can be of any type that is the return type of the `C`
|
|
258
|
+
* callback function. If the `identifier` is `undefined`, it means you want to retrieve the first
|
|
259
|
+
* node that matches the other criteria
|
|
260
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
261
|
+
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
262
|
+
* function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
|
|
263
|
+
* @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
264
|
+
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
265
|
+
* provided, the search will start from the root of the binary tree.
|
|
266
|
+
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
267
|
+
* iteration to be performed when searching for nodes in the binary tree. It is used in the
|
|
268
|
+
* `getNodes` method, which is called within the `getNode` method.
|
|
269
|
+
* @returns a value of type `N`, `null`, or `undefined`.
|
|
238
270
|
*/
|
|
239
271
|
getNode<C extends BTNCallback<N>>(
|
|
240
272
|
identifier: ReturnType<C> | undefined,
|
|
@@ -248,6 +280,14 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
248
280
|
}
|
|
249
281
|
|
|
250
282
|
/**
|
|
283
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
284
|
+
* Space Complexity: O(1)
|
|
285
|
+
*/
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
289
|
+
* Space Complexity: O(1)
|
|
290
|
+
*
|
|
251
291
|
* The function returns the successor of a given node in a red-black tree.
|
|
252
292
|
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
253
293
|
* @returns the successor of the given RedBlackTreeNode.
|
|
@@ -266,6 +306,14 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
266
306
|
}
|
|
267
307
|
|
|
268
308
|
/**
|
|
309
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
310
|
+
* Space Complexity: O(1)
|
|
311
|
+
*/
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
315
|
+
* Space Complexity: O(1)
|
|
316
|
+
*
|
|
269
317
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
270
318
|
* @param {RedBlackTreeNode} x - The parameter `x` is of type `RedBlackTreeNode`, which represents a node in a
|
|
271
319
|
* Red-Black Tree.
|
|
@@ -298,8 +346,16 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
298
346
|
}
|
|
299
347
|
|
|
300
348
|
/**
|
|
301
|
-
*
|
|
302
|
-
*
|
|
349
|
+
* Time Complexity: O(1)
|
|
350
|
+
* Space Complexity: O(1)
|
|
351
|
+
*/
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Time Complexity: O(1)
|
|
355
|
+
* Space Complexity: O(1)
|
|
356
|
+
*
|
|
357
|
+
* The function performs a left rotation on a binary tree node.
|
|
358
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `N`, which likely represents a node in a binary tree.
|
|
303
359
|
*/
|
|
304
360
|
protected _leftRotate(x: N): void {
|
|
305
361
|
if (x.right) {
|
|
@@ -322,6 +378,14 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
322
378
|
}
|
|
323
379
|
|
|
324
380
|
/**
|
|
381
|
+
* Time Complexity: O(1)
|
|
382
|
+
* Space Complexity: O(1)
|
|
383
|
+
*/
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Time Complexity: O(1)
|
|
387
|
+
* Space Complexity: O(1)
|
|
388
|
+
*
|
|
325
389
|
* The function performs a right rotation on a red-black tree node.
|
|
326
390
|
* @param {RedBlackTreeNode} x - x is a RedBlackTreeNode, which represents the node that needs to be right
|
|
327
391
|
* rotated.
|
|
@@ -347,9 +411,16 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
347
411
|
}
|
|
348
412
|
|
|
349
413
|
/**
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
|
|
414
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
415
|
+
* Space Complexity: O(1)
|
|
416
|
+
*/
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
420
|
+
* Space Complexity: O(1)
|
|
421
|
+
*
|
|
422
|
+
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
423
|
+
* @param {RedBlackTreeNode} x - The parameter `x` represents a node in a Red-Black Tree (RBT).
|
|
353
424
|
*/
|
|
354
425
|
protected _fixDelete(x: N): void {
|
|
355
426
|
let s: N | undefined;
|
|
@@ -412,6 +483,14 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
412
483
|
}
|
|
413
484
|
|
|
414
485
|
/**
|
|
486
|
+
* Time Complexity: O(1)
|
|
487
|
+
* Space Complexity: O(1)
|
|
488
|
+
*/
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Time Complexity: O(1)
|
|
492
|
+
* Space Complexity: O(1)
|
|
493
|
+
*
|
|
415
494
|
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
416
495
|
* @param {RedBlackTreeNode} u - The parameter "u" represents a RedBlackTreeNode object.
|
|
417
496
|
* @param {RedBlackTreeNode} v - The parameter "v" is a RedBlackTreeNode object.
|
|
@@ -428,6 +507,14 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
428
507
|
}
|
|
429
508
|
|
|
430
509
|
/**
|
|
510
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
511
|
+
* Space Complexity: O(1)
|
|
512
|
+
*/
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
516
|
+
* Space Complexity: O(1)
|
|
517
|
+
*
|
|
431
518
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
432
519
|
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
433
520
|
* red-black tree.
|