undirected-graph-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.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/data-structures/binary-tree/binary-tree.js +492 -392
- package/dist/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/data-structures/binary-tree/bst.js +256 -358
- package/dist/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +1 -3
- package/dist/data-structures/queue/deque.d.ts +2 -3
- package/dist/data-structures/queue/deque.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +1 -1
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +10 -1
- package/dist/utils/utils.d.ts +2 -1
- package/dist/utils/utils.js +27 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
- package/src/data-structures/binary-tree/avl-tree.ts +109 -80
- package/src/data-structures/binary-tree/binary-tree.ts +556 -433
- package/src/data-structures/binary-tree/bst.ts +286 -375
- package/src/data-structures/binary-tree/rb-tree.ts +132 -125
- package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +11 -9
- package/src/types/common.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
3
|
BTNCallback,
|
|
4
|
+
Comparable,
|
|
5
|
+
CRUD,
|
|
4
6
|
KeyOrNodeOrEntry,
|
|
7
|
+
RBTNColor,
|
|
5
8
|
RBTreeOptions,
|
|
6
9
|
RedBlackTreeNested,
|
|
7
10
|
RedBlackTreeNodeNested
|
|
8
11
|
} from '../../types';
|
|
9
|
-
import {
|
|
12
|
+
import { BTNEntry } from '../../types';
|
|
10
13
|
import { BST, BSTNode } from './bst';
|
|
11
14
|
import { IBinaryTree } from '../../interfaces';
|
|
12
15
|
|
|
13
16
|
export class RedBlackTreeNode<
|
|
14
|
-
K
|
|
17
|
+
K extends Comparable,
|
|
15
18
|
V = any,
|
|
16
19
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
|
|
17
20
|
> extends BSTNode<K, V, NODE> {
|
|
@@ -51,30 +54,34 @@ export class RedBlackTreeNode<
|
|
|
51
54
|
}
|
|
52
55
|
|
|
53
56
|
export class RedBlackTree<
|
|
54
|
-
K
|
|
57
|
+
K extends Comparable,
|
|
55
58
|
V = any,
|
|
59
|
+
R = BTNEntry<K, V>,
|
|
56
60
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
57
|
-
TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>
|
|
61
|
+
TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
|
|
58
62
|
>
|
|
59
|
-
extends BST<K, V, NODE, TREE>
|
|
60
|
-
implements IBinaryTree<K, V, NODE, TREE> {
|
|
63
|
+
extends BST<K, V, R, NODE, TREE>
|
|
64
|
+
implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
61
65
|
/**
|
|
62
66
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
63
|
-
* @param
|
|
64
|
-
* contain keys, nodes, or
|
|
65
|
-
*
|
|
67
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
68
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
69
|
+
* initialize the RBTree with the provided elements.
|
|
66
70
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
67
|
-
* constructor. It
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*/
|
|
71
|
-
constructor(
|
|
71
|
+
* constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
|
|
72
|
+
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
73
|
+
* depend on the implementation
|
|
74
|
+
*/
|
|
75
|
+
constructor(
|
|
76
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
77
|
+
options?: RBTreeOptions<K, V, R>
|
|
78
|
+
) {
|
|
72
79
|
super([], options);
|
|
73
80
|
|
|
74
81
|
this._root = this.NIL;
|
|
75
82
|
|
|
76
|
-
if (
|
|
77
|
-
this.addMany(
|
|
83
|
+
if (keysOrNodesOrEntriesOrRawElements) {
|
|
84
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
78
85
|
}
|
|
79
86
|
}
|
|
80
87
|
|
|
@@ -90,29 +97,30 @@ export class RedBlackTree<
|
|
|
90
97
|
|
|
91
98
|
/**
|
|
92
99
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
93
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of
|
|
94
|
-
* which is a generic type
|
|
100
|
+
* @param {K} key - The key parameter represents the key value of the node being created. It is of
|
|
101
|
+
* type K, which is a generic type that can be replaced with any specific type when using the
|
|
102
|
+
* function.
|
|
95
103
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
96
|
-
* associated with the key in the node. It is not required and can be omitted if
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* can
|
|
100
|
-
*
|
|
101
|
-
* value, and color
|
|
104
|
+
* associated with the key in the node. It is not required and can be omitted if you only need to
|
|
105
|
+
* create a node with a key.
|
|
106
|
+
* @param {RBTNColor} [color=BLACK] - The "color" parameter is used to specify the color of the node
|
|
107
|
+
* in a Red-Black Tree. It can have two possible values: "RED" or "BLACK". By default, the color is
|
|
108
|
+
* set to "BLACK" if not specified.
|
|
109
|
+
* @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
|
|
110
|
+
* returned.
|
|
102
111
|
*/
|
|
103
112
|
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): NODE {
|
|
104
113
|
return new RedBlackTreeNode<K, V, NODE>(key, value, color) as NODE;
|
|
105
114
|
}
|
|
106
115
|
|
|
107
116
|
/**
|
|
108
|
-
* The function creates a Red-Black Tree with the
|
|
109
|
-
* @param [options] - The `options` parameter is an optional object that contains
|
|
110
|
-
* options for creating the Red-Black Tree. It
|
|
111
|
-
* the type of keys in the tree.
|
|
117
|
+
* The function creates a new Red-Black Tree with the specified options.
|
|
118
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
119
|
+
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
112
120
|
* @returns a new instance of a RedBlackTree object.
|
|
113
121
|
*/
|
|
114
|
-
override createTree(options?: RBTreeOptions<K>): TREE {
|
|
115
|
-
return new RedBlackTree<K, V, NODE, TREE>([], {
|
|
122
|
+
override createTree(options?: RBTreeOptions<K, V, R>): TREE {
|
|
123
|
+
return new RedBlackTree<K, V, R, NODE, TREE>([], {
|
|
116
124
|
iterationType: this.iterationType,
|
|
117
125
|
...options
|
|
118
126
|
}) as TREE;
|
|
@@ -124,54 +132,57 @@ export class RedBlackTree<
|
|
|
124
132
|
*/
|
|
125
133
|
|
|
126
134
|
/**
|
|
127
|
-
* Time Complexity: O(1)
|
|
128
|
-
* Space Complexity: O(1)
|
|
129
|
-
*
|
|
130
|
-
* The function `keyValueOrEntryToNode` takes a key, value, or entry and returns a node if it is
|
|
131
|
-
* valid, otherwise it returns undefined.
|
|
132
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The key, value, or entry to convert.
|
|
133
|
-
* @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntry` is a key).
|
|
134
|
-
* @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
135
|
-
*/
|
|
136
|
-
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
137
|
-
let node: NODE | undefined;
|
|
138
|
-
|
|
139
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
140
|
-
return;
|
|
141
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
142
|
-
node = keyOrNodeOrEntry;
|
|
143
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
144
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
145
|
-
if (key === undefined || key === null) {
|
|
146
|
-
return;
|
|
147
|
-
} else {
|
|
148
|
-
node = this.createNode(key, value, 'RED');
|
|
149
|
-
}
|
|
150
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
151
|
-
node = this.createNode(keyOrNodeOrEntry, value, 'RED');
|
|
152
|
-
} else {
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
return node;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Time Complexity: O(1)
|
|
160
|
-
* Space Complexity: O(1)
|
|
161
|
-
* /
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
135
|
* Time Complexity: O(1)
|
|
165
136
|
* Space Complexity: O(1)
|
|
166
137
|
*
|
|
167
138
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
168
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>}
|
|
169
|
-
*
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
139
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
140
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
141
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
142
|
+
* an instance of the `RedBlackTreeNode` class.
|
|
143
|
+
*/
|
|
144
|
+
override isNode(
|
|
145
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
|
|
146
|
+
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
147
|
+
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
|
|
173
148
|
}
|
|
174
149
|
|
|
150
|
+
// /**
|
|
151
|
+
// * Time Complexity: O(1)
|
|
152
|
+
// * Space Complexity: O(1)
|
|
153
|
+
// */
|
|
154
|
+
//
|
|
155
|
+
// /**
|
|
156
|
+
// * Time Complexity: O(1)
|
|
157
|
+
// * Space Complexity: O(1)
|
|
158
|
+
// *
|
|
159
|
+
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
|
|
160
|
+
// * valid, otherwise it returns undefined.
|
|
161
|
+
// * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
|
|
162
|
+
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
|
|
163
|
+
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
164
|
+
// */
|
|
165
|
+
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
166
|
+
//
|
|
167
|
+
// if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
168
|
+
// if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
169
|
+
//
|
|
170
|
+
// if (this.toEntryFn) {
|
|
171
|
+
// const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
|
|
172
|
+
// if (key) return this.createNode(key, entryValue ?? value, 'RED');
|
|
173
|
+
// }
|
|
174
|
+
//
|
|
175
|
+
// if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
176
|
+
// const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
177
|
+
// if (key === undefined || key === null) return;
|
|
178
|
+
// else return this.createNode(key, value, 'RED');
|
|
179
|
+
// }
|
|
180
|
+
//
|
|
181
|
+
// if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'RED');
|
|
182
|
+
//
|
|
183
|
+
// return ;
|
|
184
|
+
// }
|
|
185
|
+
|
|
175
186
|
/**
|
|
176
187
|
* Time Complexity: O(1)
|
|
177
188
|
* Space Complexity: O(1)
|
|
@@ -198,17 +209,19 @@ export class RedBlackTree<
|
|
|
198
209
|
* Time Complexity: O(log n)
|
|
199
210
|
* Space Complexity: O(1)
|
|
200
211
|
*
|
|
201
|
-
* The function adds a new node to a
|
|
202
|
-
*
|
|
203
|
-
* @param
|
|
204
|
-
*
|
|
205
|
-
* @param {V} [value] - The `value` parameter is
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
+
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
213
|
+
* added.
|
|
214
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
215
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
216
|
+
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
217
|
+
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
218
|
+
* structure.
|
|
219
|
+
* @returns The method is returning a boolean value. If a new node is successfully added to the tree,
|
|
220
|
+
* the method returns true. If the node already exists and its value is updated, the method also
|
|
221
|
+
* returns true. If the node cannot be added or updated, the method returns false.
|
|
222
|
+
*/
|
|
223
|
+
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
224
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
212
225
|
if (!this.isRealNode(newNode)) return false;
|
|
213
226
|
|
|
214
227
|
const insertStatus = this._insert(newNode);
|
|
@@ -234,16 +247,16 @@ export class RedBlackTree<
|
|
|
234
247
|
* Time Complexity: O(log n)
|
|
235
248
|
* Space Complexity: O(1)
|
|
236
249
|
*
|
|
237
|
-
* The function
|
|
238
|
-
*
|
|
239
|
-
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
* @param {C} callback - The `callback` parameter is a function that is used to
|
|
244
|
-
*
|
|
245
|
-
* `_DEFAULT_CALLBACK
|
|
246
|
-
*
|
|
250
|
+
* The function overrides the delete method of a binary tree data structure, allowing for the
|
|
251
|
+
* deletion of a node and maintaining the balance of the tree.
|
|
252
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
253
|
+
* that identifies the node to be deleted from the binary tree. It can be of any type that is
|
|
254
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if there is no node to
|
|
255
|
+
* delete.
|
|
256
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
257
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
258
|
+
* `this._DEFAULT_CALLBACK`. The type of the `callback` parameter is `C`, which is a generic type
|
|
259
|
+
* that extends the `BTNCallback
|
|
247
260
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
248
261
|
*/
|
|
249
262
|
override delete<C extends BTNCallback<NODE>>(
|
|
@@ -307,6 +320,14 @@ export class RedBlackTree<
|
|
|
307
320
|
}
|
|
308
321
|
|
|
309
322
|
/**
|
|
323
|
+
* Time Complexity: O(1)
|
|
324
|
+
* Space Complexity: O(1)
|
|
325
|
+
*/
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Time Complexity: O(1)
|
|
329
|
+
* Space Complexity: O(1)
|
|
330
|
+
*
|
|
310
331
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
311
332
|
* root.
|
|
312
333
|
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
@@ -330,8 +351,8 @@ export class RedBlackTree<
|
|
|
330
351
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
331
352
|
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
332
353
|
* the data structure.
|
|
333
|
-
* @param {NODE} newNode - The `newNode` parameter is
|
|
334
|
-
*
|
|
354
|
+
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
|
|
355
|
+
* data structure.
|
|
335
356
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
336
357
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
337
358
|
*/
|
|
@@ -350,12 +371,13 @@ export class RedBlackTree<
|
|
|
350
371
|
* Time Complexity: O(log n)
|
|
351
372
|
* Space Complexity: O(1)
|
|
352
373
|
*
|
|
353
|
-
* The `_insert` function inserts
|
|
354
|
-
*
|
|
355
|
-
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into
|
|
356
|
-
* binary search tree.
|
|
357
|
-
*
|
|
358
|
-
*
|
|
374
|
+
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
|
|
375
|
+
* maintain the red-black tree properties.
|
|
376
|
+
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the
|
|
377
|
+
* binary search tree.
|
|
378
|
+
* @returns a string value indicating the result of the insertion operation. It can return either
|
|
379
|
+
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
|
|
380
|
+
* was created and inserted into the tree.
|
|
359
381
|
*/
|
|
360
382
|
protected _insert(node: NODE): CRUD {
|
|
361
383
|
let current = this.root;
|
|
@@ -363,9 +385,10 @@ export class RedBlackTree<
|
|
|
363
385
|
|
|
364
386
|
while (this.isRealNode(current)) {
|
|
365
387
|
parent = current;
|
|
366
|
-
|
|
388
|
+
const compared = this.comparator(node.key, current.key);
|
|
389
|
+
if (compared < 0) {
|
|
367
390
|
current = current.left ?? this.NIL;
|
|
368
|
-
} else if (
|
|
391
|
+
} else if (compared > 0) {
|
|
369
392
|
current = current.right ?? this.NIL;
|
|
370
393
|
} else {
|
|
371
394
|
this._replaceNode(current, node);
|
|
@@ -429,8 +452,8 @@ export class RedBlackTree<
|
|
|
429
452
|
* Space Complexity: O(1)
|
|
430
453
|
*
|
|
431
454
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
432
|
-
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree
|
|
433
|
-
* either be a valid node
|
|
455
|
+
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
|
|
456
|
+
* structure. It can either be a valid node or `undefined`.
|
|
434
457
|
*/
|
|
435
458
|
protected _insertFixup(z: NODE | undefined): void {
|
|
436
459
|
// Continue fixing the tree as long as the parent of z is red
|
|
@@ -501,9 +524,10 @@ export class RedBlackTree<
|
|
|
501
524
|
*
|
|
502
525
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
503
526
|
* the colors and performing rotations.
|
|
504
|
-
* @param {NODE | undefined} node - The `node` parameter represents a node in a
|
|
505
|
-
*
|
|
506
|
-
* @returns The function does not return any value. It has a return type of `void
|
|
527
|
+
* @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can
|
|
528
|
+
* be either a valid node object or `undefined`.
|
|
529
|
+
* @returns The function does not return any value. It has a return type of `void`, which means it
|
|
530
|
+
* does not return anything.
|
|
507
531
|
*/
|
|
508
532
|
protected _deleteFixup(node: NODE | undefined): void {
|
|
509
533
|
// Early exit condition
|
|
@@ -656,21 +680,4 @@ export class RedBlackTree<
|
|
|
656
680
|
x.right = y;
|
|
657
681
|
y.parent = x;
|
|
658
682
|
}
|
|
659
|
-
|
|
660
|
-
/**
|
|
661
|
-
* The function compares two values using a comparator function and returns whether the first value
|
|
662
|
-
* is greater than, less than, or equal to the second value.
|
|
663
|
-
* @param {K} a - The parameter "a" is of type K.
|
|
664
|
-
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
665
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
666
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
667
|
-
*/
|
|
668
|
-
protected override _compare(a: K, b: K): CP {
|
|
669
|
-
const extractedA = this.extractor(a);
|
|
670
|
-
const extractedB = this.extractor(b);
|
|
671
|
-
const compared = extractedA - extractedB;
|
|
672
|
-
if (compared > 0) return 'GT';
|
|
673
|
-
if (compared < 0) return 'LT';
|
|
674
|
-
return 'EQ';
|
|
675
|
-
}
|
|
676
683
|
}
|