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,8 +1,8 @@
|
|
|
1
|
-
import type { BinaryTreeDeleteResult, BTNCallback, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
2
|
-
import {
|
|
1
|
+
import type { BinaryTreeDeleteResult, BTNCallback, Comparable, CRUD, KeyOrNodeOrEntry, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
2
|
+
import { BTNEntry } from '../../types';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
4
|
import { IBinaryTree } from '../../interfaces';
|
|
5
|
-
export declare class RedBlackTreeNode<K
|
|
5
|
+
export declare class RedBlackTreeNode<K extends Comparable, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
6
6
|
/**
|
|
7
7
|
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
8
8
|
* color.
|
|
@@ -27,18 +27,18 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
27
27
|
*/
|
|
28
28
|
set color(value: RBTNColor);
|
|
29
29
|
}
|
|
30
|
-
export declare class RedBlackTree<K
|
|
30
|
+
export declare class RedBlackTree<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
31
31
|
/**
|
|
32
32
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
33
|
-
* @param
|
|
34
|
-
* contain keys, nodes, or
|
|
35
|
-
*
|
|
33
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
34
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
35
|
+
* initialize the RBTree with the provided elements.
|
|
36
36
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
37
|
-
* constructor. It
|
|
38
|
-
*
|
|
39
|
-
*
|
|
37
|
+
* constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
|
|
38
|
+
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
39
|
+
* depend on the implementation
|
|
40
40
|
*/
|
|
41
|
-
constructor(
|
|
41
|
+
constructor(keysOrNodesOrEntriesOrRawElements?: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K, V, R>);
|
|
42
42
|
protected _root: NODE | undefined;
|
|
43
43
|
/**
|
|
44
44
|
* The function returns the root node of a tree or undefined if there is no root.
|
|
@@ -47,54 +47,41 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
47
47
|
get root(): NODE | undefined;
|
|
48
48
|
/**
|
|
49
49
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
50
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of
|
|
51
|
-
* which is a generic type
|
|
50
|
+
* @param {K} key - The key parameter represents the key value of the node being created. It is of
|
|
51
|
+
* type K, which is a generic type that can be replaced with any specific type when using the
|
|
52
|
+
* function.
|
|
52
53
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
53
|
-
* associated with the key in the node. It is not required and can be omitted if
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* can
|
|
57
|
-
*
|
|
58
|
-
* value, and color
|
|
54
|
+
* associated with the key in the node. It is not required and can be omitted if you only need to
|
|
55
|
+
* create a node with a key.
|
|
56
|
+
* @param {RBTNColor} [color=BLACK] - The "color" parameter is used to specify the color of the node
|
|
57
|
+
* in a Red-Black Tree. It can have two possible values: "RED" or "BLACK". By default, the color is
|
|
58
|
+
* set to "BLACK" if not specified.
|
|
59
|
+
* @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
|
|
60
|
+
* returned.
|
|
59
61
|
*/
|
|
60
62
|
createNode(key: K, value?: V, color?: RBTNColor): NODE;
|
|
61
63
|
/**
|
|
62
|
-
* The function creates a Red-Black Tree with the
|
|
63
|
-
* @param [options] - The `options` parameter is an optional object that contains
|
|
64
|
-
* options for creating the Red-Black Tree. It
|
|
65
|
-
* the type of keys in the tree.
|
|
64
|
+
* The function creates a new Red-Black Tree with the specified options.
|
|
65
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
66
|
+
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
66
67
|
* @returns a new instance of a RedBlackTree object.
|
|
67
68
|
*/
|
|
68
|
-
createTree(options?: RBTreeOptions<K>): TREE;
|
|
69
|
+
createTree(options?: RBTreeOptions<K, V, R>): TREE;
|
|
69
70
|
/**
|
|
70
71
|
* Time Complexity: O(1)
|
|
71
72
|
* Space Complexity: O(1)
|
|
72
73
|
*/
|
|
73
74
|
/**
|
|
74
|
-
* Time Complexity: O(1)
|
|
75
|
-
* Space Complexity: O(1)
|
|
76
|
-
*
|
|
77
|
-
* The function `keyValueOrEntryToNode` takes a key, value, or entry and returns a node if it is
|
|
78
|
-
* valid, otherwise it returns undefined.
|
|
79
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The key, value, or entry to convert.
|
|
80
|
-
* @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntry` is a key).
|
|
81
|
-
* @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
82
|
-
*/
|
|
83
|
-
keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined;
|
|
84
|
-
/**
|
|
85
|
-
* Time Complexity: O(1)
|
|
86
|
-
* Space Complexity: O(1)
|
|
87
|
-
* /
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
75
|
* Time Complexity: O(1)
|
|
91
76
|
* Space Complexity: O(1)
|
|
92
77
|
*
|
|
93
78
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
94
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>}
|
|
95
|
-
*
|
|
79
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
80
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
81
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
82
|
+
* an instance of the `RedBlackTreeNode` class.
|
|
96
83
|
*/
|
|
97
|
-
isNode(
|
|
84
|
+
isNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE;
|
|
98
85
|
/**
|
|
99
86
|
* Time Complexity: O(1)
|
|
100
87
|
* Space Complexity: O(1)
|
|
@@ -115,16 +102,18 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
115
102
|
* Time Complexity: O(log n)
|
|
116
103
|
* Space Complexity: O(1)
|
|
117
104
|
*
|
|
118
|
-
* The function adds a new node to a
|
|
119
|
-
*
|
|
120
|
-
* @param
|
|
121
|
-
*
|
|
122
|
-
* @param {V} [value] - The `value` parameter is
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
|
|
127
|
-
|
|
105
|
+
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
106
|
+
* added.
|
|
107
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
108
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
109
|
+
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
110
|
+
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
111
|
+
* structure.
|
|
112
|
+
* @returns The method is returning a boolean value. If a new node is successfully added to the tree,
|
|
113
|
+
* the method returns true. If the node already exists and its value is updated, the method also
|
|
114
|
+
* returns true. If the node cannot be added or updated, the method returns false.
|
|
115
|
+
*/
|
|
116
|
+
add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
|
|
128
117
|
/**
|
|
129
118
|
* Time Complexity: O(log n)
|
|
130
119
|
* Space Complexity: O(1)
|
|
@@ -133,20 +122,27 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
133
122
|
* Time Complexity: O(log n)
|
|
134
123
|
* Space Complexity: O(1)
|
|
135
124
|
*
|
|
136
|
-
* The function
|
|
137
|
-
*
|
|
138
|
-
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
* @param {C} callback - The `callback` parameter is a function that is used to
|
|
143
|
-
*
|
|
144
|
-
* `_DEFAULT_CALLBACK
|
|
145
|
-
*
|
|
125
|
+
* The function overrides the delete method of a binary tree data structure, allowing for the
|
|
126
|
+
* deletion of a node and maintaining the balance of the tree.
|
|
127
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
128
|
+
* that identifies the node to be deleted from the binary tree. It can be of any type that is
|
|
129
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if there is no node to
|
|
130
|
+
* delete.
|
|
131
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
132
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
133
|
+
* `this._DEFAULT_CALLBACK`. The type of the `callback` parameter is `C`, which is a generic type
|
|
134
|
+
* that extends the `BTNCallback
|
|
146
135
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
147
136
|
*/
|
|
148
137
|
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
149
138
|
/**
|
|
139
|
+
* Time Complexity: O(1)
|
|
140
|
+
* Space Complexity: O(1)
|
|
141
|
+
*/
|
|
142
|
+
/**
|
|
143
|
+
* Time Complexity: O(1)
|
|
144
|
+
* Space Complexity: O(1)
|
|
145
|
+
*
|
|
150
146
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
151
147
|
* root.
|
|
152
148
|
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
@@ -163,8 +159,8 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
163
159
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
164
160
|
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
165
161
|
* the data structure.
|
|
166
|
-
* @param {NODE} newNode - The `newNode` parameter is
|
|
167
|
-
*
|
|
162
|
+
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
|
|
163
|
+
* data structure.
|
|
168
164
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
169
165
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
170
166
|
*/
|
|
@@ -177,12 +173,13 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
177
173
|
* Time Complexity: O(log n)
|
|
178
174
|
* Space Complexity: O(1)
|
|
179
175
|
*
|
|
180
|
-
* The `_insert` function inserts
|
|
181
|
-
*
|
|
182
|
-
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into
|
|
183
|
-
* binary search tree.
|
|
184
|
-
*
|
|
185
|
-
*
|
|
176
|
+
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
|
|
177
|
+
* maintain the red-black tree properties.
|
|
178
|
+
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the
|
|
179
|
+
* binary search tree.
|
|
180
|
+
* @returns a string value indicating the result of the insertion operation. It can return either
|
|
181
|
+
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
|
|
182
|
+
* was created and inserted into the tree.
|
|
186
183
|
*/
|
|
187
184
|
protected _insert(node: NODE): CRUD;
|
|
188
185
|
/**
|
|
@@ -208,8 +205,8 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
208
205
|
* Space Complexity: O(1)
|
|
209
206
|
*
|
|
210
207
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
211
|
-
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree
|
|
212
|
-
* either be a valid node
|
|
208
|
+
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
|
|
209
|
+
* structure. It can either be a valid node or `undefined`.
|
|
213
210
|
*/
|
|
214
211
|
protected _insertFixup(z: NODE | undefined): void;
|
|
215
212
|
/**
|
|
@@ -222,9 +219,10 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
222
219
|
*
|
|
223
220
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
224
221
|
* the colors and performing rotations.
|
|
225
|
-
* @param {NODE | undefined} node - The `node` parameter represents a node in a
|
|
226
|
-
*
|
|
227
|
-
* @returns The function does not return any value. It has a return type of `void
|
|
222
|
+
* @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can
|
|
223
|
+
* be either a valid node object or `undefined`.
|
|
224
|
+
* @returns The function does not return any value. It has a return type of `void`, which means it
|
|
225
|
+
* does not return anything.
|
|
228
226
|
*/
|
|
229
227
|
protected _deleteFixup(node: NODE | undefined): void;
|
|
230
228
|
/**
|
|
@@ -255,13 +253,4 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
255
253
|
* @returns void, which means it does not return any value.
|
|
256
254
|
*/
|
|
257
255
|
protected _rightRotate(y: NODE | undefined): void;
|
|
258
|
-
/**
|
|
259
|
-
* The function compares two values using a comparator function and returns whether the first value
|
|
260
|
-
* is greater than, less than, or equal to the second value.
|
|
261
|
-
* @param {K} a - The parameter "a" is of type K.
|
|
262
|
-
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
263
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
264
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
265
|
-
*/
|
|
266
|
-
protected _compare(a: K, b: K): CP;
|
|
267
256
|
}
|
|
@@ -37,19 +37,19 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
|
37
37
|
class RedBlackTree extends bst_1.BST {
|
|
38
38
|
/**
|
|
39
39
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
40
|
-
* @param
|
|
41
|
-
* contain keys, nodes, or
|
|
42
|
-
*
|
|
40
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
41
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
42
|
+
* initialize the RBTree with the provided elements.
|
|
43
43
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
44
|
-
* constructor. It
|
|
45
|
-
*
|
|
46
|
-
*
|
|
44
|
+
* constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
|
|
45
|
+
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
46
|
+
* depend on the implementation
|
|
47
47
|
*/
|
|
48
|
-
constructor(
|
|
48
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
49
49
|
super([], options);
|
|
50
50
|
this._root = this.NIL;
|
|
51
|
-
if (
|
|
52
|
-
this.addMany(
|
|
51
|
+
if (keysOrNodesOrEntriesOrRawElements) {
|
|
52
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
@@ -61,24 +61,25 @@ class RedBlackTree extends bst_1.BST {
|
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
63
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
64
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of
|
|
65
|
-
* which is a generic type
|
|
64
|
+
* @param {K} key - The key parameter represents the key value of the node being created. It is of
|
|
65
|
+
* type K, which is a generic type that can be replaced with any specific type when using the
|
|
66
|
+
* function.
|
|
66
67
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
67
|
-
* associated with the key in the node. It is not required and can be omitted if
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* can
|
|
71
|
-
*
|
|
72
|
-
* value, and color
|
|
68
|
+
* associated with the key in the node. It is not required and can be omitted if you only need to
|
|
69
|
+
* create a node with a key.
|
|
70
|
+
* @param {RBTNColor} [color=BLACK] - The "color" parameter is used to specify the color of the node
|
|
71
|
+
* in a Red-Black Tree. It can have two possible values: "RED" or "BLACK". By default, the color is
|
|
72
|
+
* set to "BLACK" if not specified.
|
|
73
|
+
* @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
|
|
74
|
+
* returned.
|
|
73
75
|
*/
|
|
74
76
|
createNode(key, value, color = 'BLACK') {
|
|
75
77
|
return new RedBlackTreeNode(key, value, color);
|
|
76
78
|
}
|
|
77
79
|
/**
|
|
78
|
-
* The function creates a Red-Black Tree with the
|
|
79
|
-
* @param [options] - The `options` parameter is an optional object that contains
|
|
80
|
-
* options for creating the Red-Black Tree. It
|
|
81
|
-
* the type of keys in the tree.
|
|
80
|
+
* The function creates a new Red-Black Tree with the specified options.
|
|
81
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
82
|
+
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
82
83
|
* @returns a new instance of a RedBlackTree object.
|
|
83
84
|
*/
|
|
84
85
|
createTree(options) {
|
|
@@ -89,56 +90,53 @@ class RedBlackTree extends bst_1.BST {
|
|
|
89
90
|
* Space Complexity: O(1)
|
|
90
91
|
*/
|
|
91
92
|
/**
|
|
92
|
-
* Time Complexity: O(1)
|
|
93
|
-
* Space Complexity: O(1)
|
|
94
|
-
*
|
|
95
|
-
* The function `keyValueOrEntryToNode` takes a key, value, or entry and returns a node if it is
|
|
96
|
-
* valid, otherwise it returns undefined.
|
|
97
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The key, value, or entry to convert.
|
|
98
|
-
* @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntry` is a key).
|
|
99
|
-
* @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
100
|
-
*/
|
|
101
|
-
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
102
|
-
let node;
|
|
103
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
107
|
-
node = keyOrNodeOrEntry;
|
|
108
|
-
}
|
|
109
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
110
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
111
|
-
if (key === undefined || key === null) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
node = this.createNode(key, value, 'RED');
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
119
|
-
node = this.createNode(keyOrNodeOrEntry, value, 'RED');
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
return node;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Time Complexity: O(1)
|
|
128
|
-
* Space Complexity: O(1)
|
|
129
|
-
* /
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
93
|
* Time Complexity: O(1)
|
|
133
94
|
* Space Complexity: O(1)
|
|
134
95
|
*
|
|
135
96
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
136
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>}
|
|
137
|
-
*
|
|
97
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
98
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
99
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
100
|
+
* an instance of the `RedBlackTreeNode` class.
|
|
138
101
|
*/
|
|
139
|
-
isNode(
|
|
140
|
-
return
|
|
102
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
103
|
+
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
|
|
141
104
|
}
|
|
105
|
+
// /**
|
|
106
|
+
// * Time Complexity: O(1)
|
|
107
|
+
// * Space Complexity: O(1)
|
|
108
|
+
// */
|
|
109
|
+
//
|
|
110
|
+
// /**
|
|
111
|
+
// * Time Complexity: O(1)
|
|
112
|
+
// * Space Complexity: O(1)
|
|
113
|
+
// *
|
|
114
|
+
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
|
|
115
|
+
// * valid, otherwise it returns undefined.
|
|
116
|
+
// * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
|
|
117
|
+
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
|
|
118
|
+
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
119
|
+
// */
|
|
120
|
+
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
121
|
+
//
|
|
122
|
+
// if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
123
|
+
// if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
124
|
+
//
|
|
125
|
+
// if (this.toEntryFn) {
|
|
126
|
+
// const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
|
|
127
|
+
// if (key) return this.createNode(key, entryValue ?? value, 'RED');
|
|
128
|
+
// }
|
|
129
|
+
//
|
|
130
|
+
// if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
131
|
+
// const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
132
|
+
// if (key === undefined || key === null) return;
|
|
133
|
+
// else return this.createNode(key, value, 'RED');
|
|
134
|
+
// }
|
|
135
|
+
//
|
|
136
|
+
// if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'RED');
|
|
137
|
+
//
|
|
138
|
+
// return ;
|
|
139
|
+
// }
|
|
142
140
|
/**
|
|
143
141
|
* Time Complexity: O(1)
|
|
144
142
|
* Space Complexity: O(1)
|
|
@@ -162,17 +160,19 @@ class RedBlackTree extends bst_1.BST {
|
|
|
162
160
|
* Time Complexity: O(log n)
|
|
163
161
|
* Space Complexity: O(1)
|
|
164
162
|
*
|
|
165
|
-
* The function adds a new node to a
|
|
166
|
-
*
|
|
167
|
-
* @param
|
|
168
|
-
*
|
|
169
|
-
* @param {V} [value] - The `value` parameter is
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
163
|
+
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
164
|
+
* added.
|
|
165
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
166
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
167
|
+
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
168
|
+
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
169
|
+
* structure.
|
|
170
|
+
* @returns The method is returning a boolean value. If a new node is successfully added to the tree,
|
|
171
|
+
* the method returns true. If the node already exists and its value is updated, the method also
|
|
172
|
+
* returns true. If the node cannot be added or updated, the method returns false.
|
|
173
|
+
*/
|
|
174
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
175
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
176
176
|
if (!this.isRealNode(newNode))
|
|
177
177
|
return false;
|
|
178
178
|
const insertStatus = this._insert(newNode);
|
|
@@ -198,16 +198,16 @@ class RedBlackTree extends bst_1.BST {
|
|
|
198
198
|
* Time Complexity: O(log n)
|
|
199
199
|
* Space Complexity: O(1)
|
|
200
200
|
*
|
|
201
|
-
* The function
|
|
202
|
-
*
|
|
203
|
-
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
* @param {C} callback - The `callback` parameter is a function that is used to
|
|
208
|
-
*
|
|
209
|
-
* `_DEFAULT_CALLBACK
|
|
210
|
-
*
|
|
201
|
+
* The function overrides the delete method of a binary tree data structure, allowing for the
|
|
202
|
+
* deletion of a node and maintaining the balance of the tree.
|
|
203
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
204
|
+
* that identifies the node to be deleted from the binary tree. It can be of any type that is
|
|
205
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if there is no node to
|
|
206
|
+
* delete.
|
|
207
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
208
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
209
|
+
* `this._DEFAULT_CALLBACK`. The type of the `callback` parameter is `C`, which is a generic type
|
|
210
|
+
* that extends the `BTNCallback
|
|
211
211
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
212
212
|
*/
|
|
213
213
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
@@ -263,6 +263,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
263
263
|
return results;
|
|
264
264
|
}
|
|
265
265
|
/**
|
|
266
|
+
* Time Complexity: O(1)
|
|
267
|
+
* Space Complexity: O(1)
|
|
268
|
+
*/
|
|
269
|
+
/**
|
|
270
|
+
* Time Complexity: O(1)
|
|
271
|
+
* Space Complexity: O(1)
|
|
272
|
+
*
|
|
266
273
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
267
274
|
* root.
|
|
268
275
|
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
@@ -284,8 +291,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
284
291
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
285
292
|
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
286
293
|
* the data structure.
|
|
287
|
-
* @param {NODE} newNode - The `newNode` parameter is
|
|
288
|
-
*
|
|
294
|
+
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
|
|
295
|
+
* data structure.
|
|
289
296
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
290
297
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
291
298
|
*/
|
|
@@ -301,12 +308,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
301
308
|
* Time Complexity: O(log n)
|
|
302
309
|
* Space Complexity: O(1)
|
|
303
310
|
*
|
|
304
|
-
* The `_insert` function inserts
|
|
305
|
-
*
|
|
306
|
-
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into
|
|
307
|
-
* binary search tree.
|
|
308
|
-
*
|
|
309
|
-
*
|
|
311
|
+
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
|
|
312
|
+
* maintain the red-black tree properties.
|
|
313
|
+
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the
|
|
314
|
+
* binary search tree.
|
|
315
|
+
* @returns a string value indicating the result of the insertion operation. It can return either
|
|
316
|
+
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
|
|
317
|
+
* was created and inserted into the tree.
|
|
310
318
|
*/
|
|
311
319
|
_insert(node) {
|
|
312
320
|
var _a, _b;
|
|
@@ -314,10 +322,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
314
322
|
let parent = undefined;
|
|
315
323
|
while (this.isRealNode(current)) {
|
|
316
324
|
parent = current;
|
|
317
|
-
|
|
325
|
+
const compared = this.comparator(node.key, current.key);
|
|
326
|
+
if (compared < 0) {
|
|
318
327
|
current = (_a = current.left) !== null && _a !== void 0 ? _a : this.NIL;
|
|
319
328
|
}
|
|
320
|
-
else if (
|
|
329
|
+
else if (compared > 0) {
|
|
321
330
|
current = (_b = current.right) !== null && _b !== void 0 ? _b : this.NIL;
|
|
322
331
|
}
|
|
323
332
|
else {
|
|
@@ -377,8 +386,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
377
386
|
* Space Complexity: O(1)
|
|
378
387
|
*
|
|
379
388
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
380
|
-
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree
|
|
381
|
-
* either be a valid node
|
|
389
|
+
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
|
|
390
|
+
* structure. It can either be a valid node or `undefined`.
|
|
382
391
|
*/
|
|
383
392
|
_insertFixup(z) {
|
|
384
393
|
var _a, _b, _c, _d;
|
|
@@ -449,9 +458,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
449
458
|
*
|
|
450
459
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
451
460
|
* the colors and performing rotations.
|
|
452
|
-
* @param {NODE | undefined} node - The `node` parameter represents a node in a
|
|
453
|
-
*
|
|
454
|
-
* @returns The function does not return any value. It has a return type of `void
|
|
461
|
+
* @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can
|
|
462
|
+
* be either a valid node object or `undefined`.
|
|
463
|
+
* @returns The function does not return any value. It has a return type of `void`, which means it
|
|
464
|
+
* does not return anything.
|
|
455
465
|
*/
|
|
456
466
|
_deleteFixup(node) {
|
|
457
467
|
var _a, _b, _c, _d;
|
|
@@ -599,23 +609,5 @@ class RedBlackTree extends bst_1.BST {
|
|
|
599
609
|
x.right = y;
|
|
600
610
|
y.parent = x;
|
|
601
611
|
}
|
|
602
|
-
/**
|
|
603
|
-
* The function compares two values using a comparator function and returns whether the first value
|
|
604
|
-
* is greater than, less than, or equal to the second value.
|
|
605
|
-
* @param {K} a - The parameter "a" is of type K.
|
|
606
|
-
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
607
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
608
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
609
|
-
*/
|
|
610
|
-
_compare(a, b) {
|
|
611
|
-
const extractedA = this.extractor(a);
|
|
612
|
-
const extractedB = this.extractor(b);
|
|
613
|
-
const compared = extractedA - extractedB;
|
|
614
|
-
if (compared > 0)
|
|
615
|
-
return 'GT';
|
|
616
|
-
if (compared < 0)
|
|
617
|
-
return 'LT';
|
|
618
|
-
return 'EQ';
|
|
619
|
-
}
|
|
620
612
|
}
|
|
621
613
|
exports.RedBlackTree = RedBlackTree;
|