directed-graph-typed 1.51.8 → 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 +104 -66
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +80 -60
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +316 -224
- package/dist/data-structures/binary-tree/binary-tree.js +471 -361
- package/dist/data-structures/binary-tree/bst.d.ts +198 -200
- package/dist/data-structures/binary-tree/bst.js +215 -249
- package/dist/data-structures/binary-tree/rb-tree.d.ts +71 -72
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
- 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/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 +6 -6
- package/dist/types/common.d.ts +1 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
- package/src/data-structures/binary-tree/avl-tree.ts +94 -66
- package/src/data-structures/binary-tree/binary-tree.ts +530 -398
- package/src/data-structures/binary-tree/bst.ts +251 -270
- package/src/data-structures/binary-tree/rb-tree.ts +121 -100
- package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- 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 +8 -7
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/utils/utils.ts +3 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { BinaryTreeDeleteResult, BTNCallback, Comparable, CRUD, KeyOrNodeOrEntry, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
2
|
+
import { BTNEntry } from '../../types';
|
|
2
3
|
import { BST, BSTNode } from './bst';
|
|
3
4
|
import { IBinaryTree } from '../../interfaces';
|
|
4
5
|
export declare class RedBlackTreeNode<K extends Comparable, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
@@ -26,18 +27,18 @@ export declare class RedBlackTreeNode<K extends Comparable, V = any, NODE extend
|
|
|
26
27
|
*/
|
|
27
28
|
set color(value: RBTNColor);
|
|
28
29
|
}
|
|
29
|
-
export declare class RedBlackTree<K extends Comparable, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>> extends BST<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
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> {
|
|
30
31
|
/**
|
|
31
32
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
32
|
-
* @param
|
|
33
|
-
* contain keys, nodes, or
|
|
34
|
-
*
|
|
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.
|
|
35
36
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
36
|
-
* constructor. It
|
|
37
|
-
*
|
|
38
|
-
*
|
|
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
|
|
39
40
|
*/
|
|
40
|
-
constructor(
|
|
41
|
+
constructor(keysOrNodesOrEntriesOrRawElements?: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K, V, R>);
|
|
41
42
|
protected _root: NODE | undefined;
|
|
42
43
|
/**
|
|
43
44
|
* The function returns the root node of a tree or undefined if there is no root.
|
|
@@ -46,54 +47,41 @@ export declare class RedBlackTree<K extends Comparable, V = any, NODE extends Re
|
|
|
46
47
|
get root(): NODE | undefined;
|
|
47
48
|
/**
|
|
48
49
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
49
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of
|
|
50
|
-
* 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.
|
|
51
53
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
52
|
-
* associated with the key in the node. It is not required and can be omitted if
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* can
|
|
56
|
-
*
|
|
57
|
-
* 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.
|
|
58
61
|
*/
|
|
59
62
|
createNode(key: K, value?: V, color?: RBTNColor): NODE;
|
|
60
63
|
/**
|
|
61
|
-
* The function creates a Red-Black Tree with the
|
|
62
|
-
* @param [options] - The `options` parameter is an optional object that contains
|
|
63
|
-
* options for creating the Red-Black Tree. It
|
|
64
|
-
* 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:
|
|
65
67
|
* @returns a new instance of a RedBlackTree object.
|
|
66
68
|
*/
|
|
67
|
-
createTree(options?: RBTreeOptions<K>): TREE;
|
|
69
|
+
createTree(options?: RBTreeOptions<K, V, R>): TREE;
|
|
68
70
|
/**
|
|
69
71
|
* Time Complexity: O(1)
|
|
70
72
|
* Space Complexity: O(1)
|
|
71
73
|
*/
|
|
72
74
|
/**
|
|
73
|
-
* Time Complexity: O(1)
|
|
74
|
-
* Space Complexity: O(1)
|
|
75
|
-
*
|
|
76
|
-
* The function `keyValueOrEntryToNode` takes a key, value, or entry and returns a node if it is
|
|
77
|
-
* valid, otherwise it returns undefined.
|
|
78
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The key, value, or entry to convert.
|
|
79
|
-
* @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntry` is a key).
|
|
80
|
-
* @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
81
|
-
*/
|
|
82
|
-
keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined;
|
|
83
|
-
/**
|
|
84
|
-
* Time Complexity: O(1)
|
|
85
|
-
* Space Complexity: O(1)
|
|
86
|
-
* /
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
75
|
* Time Complexity: O(1)
|
|
90
76
|
* Space Complexity: O(1)
|
|
91
77
|
*
|
|
92
78
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
93
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>}
|
|
94
|
-
*
|
|
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.
|
|
95
83
|
*/
|
|
96
|
-
isNode(
|
|
84
|
+
isNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE;
|
|
97
85
|
/**
|
|
98
86
|
* Time Complexity: O(1)
|
|
99
87
|
* Space Complexity: O(1)
|
|
@@ -114,16 +102,18 @@ export declare class RedBlackTree<K extends Comparable, V = any, NODE extends Re
|
|
|
114
102
|
* Time Complexity: O(log n)
|
|
115
103
|
* Space Complexity: O(1)
|
|
116
104
|
*
|
|
117
|
-
* The function adds a new node to a
|
|
118
|
-
*
|
|
119
|
-
* @param
|
|
120
|
-
*
|
|
121
|
-
* @param {V} [value] - The `value` parameter is
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
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.
|
|
125
115
|
*/
|
|
126
|
-
add(
|
|
116
|
+
add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
|
|
127
117
|
/**
|
|
128
118
|
* Time Complexity: O(log n)
|
|
129
119
|
* Space Complexity: O(1)
|
|
@@ -132,20 +122,27 @@ export declare class RedBlackTree<K extends Comparable, V = any, NODE extends Re
|
|
|
132
122
|
* Time Complexity: O(log n)
|
|
133
123
|
* Space Complexity: O(1)
|
|
134
124
|
*
|
|
135
|
-
* The function
|
|
136
|
-
*
|
|
137
|
-
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
* @param {C} callback - The `callback` parameter is a function that is used to
|
|
142
|
-
*
|
|
143
|
-
* `_DEFAULT_CALLBACK
|
|
144
|
-
*
|
|
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
|
|
145
135
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
146
136
|
*/
|
|
147
137
|
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
148
138
|
/**
|
|
139
|
+
* Time Complexity: O(1)
|
|
140
|
+
* Space Complexity: O(1)
|
|
141
|
+
*/
|
|
142
|
+
/**
|
|
143
|
+
* Time Complexity: O(1)
|
|
144
|
+
* Space Complexity: O(1)
|
|
145
|
+
*
|
|
149
146
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
150
147
|
* root.
|
|
151
148
|
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
@@ -162,8 +159,8 @@ export declare class RedBlackTree<K extends Comparable, V = any, NODE extends Re
|
|
|
162
159
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
163
160
|
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
164
161
|
* the data structure.
|
|
165
|
-
* @param {NODE} newNode - The `newNode` parameter is
|
|
166
|
-
*
|
|
162
|
+
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
|
|
163
|
+
* data structure.
|
|
167
164
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
168
165
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
169
166
|
*/
|
|
@@ -176,12 +173,13 @@ export declare class RedBlackTree<K extends Comparable, V = any, NODE extends Re
|
|
|
176
173
|
* Time Complexity: O(log n)
|
|
177
174
|
* Space Complexity: O(1)
|
|
178
175
|
*
|
|
179
|
-
* The `_insert` function inserts
|
|
180
|
-
*
|
|
181
|
-
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into
|
|
182
|
-
* binary search tree.
|
|
183
|
-
*
|
|
184
|
-
*
|
|
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.
|
|
185
183
|
*/
|
|
186
184
|
protected _insert(node: NODE): CRUD;
|
|
187
185
|
/**
|
|
@@ -207,8 +205,8 @@ export declare class RedBlackTree<K extends Comparable, V = any, NODE extends Re
|
|
|
207
205
|
* Space Complexity: O(1)
|
|
208
206
|
*
|
|
209
207
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
210
|
-
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree
|
|
211
|
-
* 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`.
|
|
212
210
|
*/
|
|
213
211
|
protected _insertFixup(z: NODE | undefined): void;
|
|
214
212
|
/**
|
|
@@ -221,9 +219,10 @@ export declare class RedBlackTree<K extends Comparable, V = any, NODE extends Re
|
|
|
221
219
|
*
|
|
222
220
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
223
221
|
* the colors and performing rotations.
|
|
224
|
-
* @param {NODE | undefined} node - The `node` parameter represents a node in a
|
|
225
|
-
*
|
|
226
|
-
* @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.
|
|
227
226
|
*/
|
|
228
227
|
protected _deleteFixup(node: NODE | undefined): void;
|
|
229
228
|
/**
|
|
@@ -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
|
-
*
|
|
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
173
|
*/
|
|
174
|
-
add(
|
|
175
|
-
const newNode = this.
|
|
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;
|
|
@@ -378,8 +386,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
378
386
|
* Space Complexity: O(1)
|
|
379
387
|
*
|
|
380
388
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
381
|
-
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree
|
|
382
|
-
* 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`.
|
|
383
391
|
*/
|
|
384
392
|
_insertFixup(z) {
|
|
385
393
|
var _a, _b, _c, _d;
|
|
@@ -450,9 +458,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
450
458
|
*
|
|
451
459
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
452
460
|
* the colors and performing rotations.
|
|
453
|
-
* @param {NODE | undefined} node - The `node` parameter represents a node in a
|
|
454
|
-
*
|
|
455
|
-
* @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.
|
|
456
465
|
*/
|
|
457
466
|
_deleteFixup(node) {
|
|
458
467
|
var _a, _b, _c, _d;
|