min-heap-typed 1.50.0 → 1.50.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/data-structures/binary-tree/bst.js +30 -71
- package/dist/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/data-structures/graph/abstract-graph.js +3 -0
- package/dist/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/data-structures/graph/directed-graph.js +26 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/data-structures/graph/undirected-graph.js +25 -0
- package/dist/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/data-structures/hash/hash-map.js +160 -25
- package/dist/data-structures/heap/heap.d.ts +66 -6
- package/dist/data-structures/heap/heap.js +66 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/queue/deque.d.ts +49 -19
- package/dist/data-structures/queue/deque.js +101 -47
- package/dist/data-structures/queue/queue.d.ts +39 -5
- package/dist/data-structures/queue/queue.js +47 -5
- package/dist/data-structures/stack/stack.d.ts +16 -0
- package/dist/data-structures/stack/stack.js +22 -0
- package/dist/data-structures/trie/trie.d.ts +38 -1
- package/dist/data-structures/trie/trie.js +41 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +172 -19
- package/src/data-structures/binary-tree/avl-tree.ts +97 -97
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +89 -131
- package/src/data-structures/binary-tree/rb-tree.ts +127 -155
- package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +28 -0
- package/src/data-structures/hash/hash-map.ts +175 -34
- package/src/data-structures/heap/heap.ts +66 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
- package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/queue/deque.ts +108 -49
- package/src/data-structures/queue/queue.ts +51 -5
- package/src/data-structures/stack/stack.ts +24 -0
- package/src/data-structures/trie/trie.ts +45 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BinaryTreeDeleteResult,
|
|
8
|
+
import { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
export declare class RedBlackTreeNode<K = any, V = any,
|
|
11
|
+
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
12
12
|
color: RBTNColor;
|
|
13
13
|
constructor(key: K, value?: V, color?: RBTNColor);
|
|
14
14
|
}
|
|
@@ -19,12 +19,12 @@ export declare class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNo
|
|
|
19
19
|
* 4. Red nodes must have black children.
|
|
20
20
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
21
21
|
*/
|
|
22
|
-
export declare class RedBlackTree<K = any, V = any,
|
|
23
|
-
Sentinel:
|
|
22
|
+
export declare class RedBlackTree<K = any, 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> {
|
|
23
|
+
Sentinel: NODE;
|
|
24
24
|
/**
|
|
25
25
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
26
26
|
* initializes the tree with optional nodes and options.
|
|
27
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
27
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
28
28
|
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
29
29
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
30
30
|
* nodes to the
|
|
@@ -32,9 +32,9 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
32
32
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
33
33
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
34
34
|
*/
|
|
35
|
-
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V,
|
|
36
|
-
protected _root:
|
|
37
|
-
get root():
|
|
35
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K>);
|
|
36
|
+
protected _root: NODE;
|
|
37
|
+
get root(): NODE;
|
|
38
38
|
protected _size: number;
|
|
39
39
|
get size(): number;
|
|
40
40
|
/**
|
|
@@ -49,7 +49,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
49
49
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
50
50
|
* value, and color.
|
|
51
51
|
*/
|
|
52
|
-
createNode(key: K, value?: V, color?: RBTNColor):
|
|
52
|
+
createNode(key: K, value?: V, color?: RBTNColor): NODE;
|
|
53
53
|
/**
|
|
54
54
|
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
55
55
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
@@ -59,30 +59,26 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
59
59
|
*/
|
|
60
60
|
createTree(options?: RBTreeOptions<K>): TREE;
|
|
61
61
|
/**
|
|
62
|
-
* The function `
|
|
63
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
62
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
63
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
64
64
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
65
|
-
* `
|
|
65
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
66
66
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
67
|
-
* @returns a node of type
|
|
67
|
+
* @returns a node of type NODE or undefined.
|
|
68
68
|
*/
|
|
69
|
-
|
|
69
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined;
|
|
70
70
|
/**
|
|
71
71
|
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
72
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
72
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
73
73
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
74
74
|
* class.
|
|
75
75
|
*/
|
|
76
|
-
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
77
|
-
|
|
78
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
79
|
-
* Space Complexity: O(1)
|
|
80
|
-
*/
|
|
81
|
-
isRealNode(node: N | undefined): node is N;
|
|
76
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
|
|
77
|
+
isRealNode(node: NODE | undefined): node is NODE;
|
|
82
78
|
/**
|
|
83
79
|
* Time Complexity: O(log n)
|
|
84
80
|
* Space Complexity: O(1)
|
|
85
|
-
*
|
|
81
|
+
* On average (where n is the number of nodes in the tree)
|
|
86
82
|
*/
|
|
87
83
|
/**
|
|
88
84
|
* Time Complexity: O(log n)
|
|
@@ -94,13 +90,12 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
94
90
|
* entry.
|
|
95
91
|
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
96
92
|
* being added to the binary search tree.
|
|
97
|
-
* @returns The method `add` returns either the newly added node (`
|
|
93
|
+
* @returns The method `add` returns either the newly added node (`NODE`) or `undefined`.
|
|
98
94
|
*/
|
|
99
|
-
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
95
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
|
|
100
96
|
/**
|
|
101
97
|
* Time Complexity: O(log n)
|
|
102
98
|
* Space Complexity: O(1)
|
|
103
|
-
* on average (where n is the number of nodes in the tree)
|
|
104
99
|
*/
|
|
105
100
|
/**
|
|
106
101
|
* Time Complexity: O(log n)
|
|
@@ -112,15 +107,12 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
112
107
|
* that you want to use to identify the node that you want to delete from the binary tree. It can be
|
|
113
108
|
* of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
114
109
|
* you don't want to
|
|
115
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
110
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` and
|
|
116
111
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
117
112
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
118
|
-
* @returns an array of `BinaryTreeDeleteResult<
|
|
113
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
119
114
|
*/
|
|
120
|
-
delete<C extends BTNCallback<
|
|
121
|
-
getNode<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
122
|
-
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
123
|
-
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
115
|
+
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
124
116
|
/**
|
|
125
117
|
* Time Complexity: O(log n)
|
|
126
118
|
* Space Complexity: O(1)
|
|
@@ -129,18 +121,44 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
129
121
|
* Time Complexity: O(log n)
|
|
130
122
|
* Space Complexity: O(1)
|
|
131
123
|
*
|
|
132
|
-
* The function
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
|
|
137
|
-
|
|
124
|
+
* The function `getNode` retrieves a single node from a binary tree based on a given identifier and
|
|
125
|
+
* callback function.
|
|
126
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value used to
|
|
127
|
+
* identify the node you want to retrieve. It can be of any type that is the return type of the `C`
|
|
128
|
+
* callback function. If the `identifier` is `undefined`, it means you want to retrieve the first
|
|
129
|
+
* node that matches the other criteria
|
|
130
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
131
|
+
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
132
|
+
* function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
|
|
133
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
134
|
+
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
135
|
+
* provided, the search will start from the root of the binary tree.
|
|
136
|
+
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
137
|
+
* iteration to be performed when searching for nodes in the binary tree. It is used in the
|
|
138
|
+
* `getNodes` method, which is called within the `getNode` method.
|
|
139
|
+
* @returns a value of type `NODE`, `null`, or `undefined`.
|
|
140
|
+
*/
|
|
141
|
+
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: import("../../types").IterationType): NODE | null | undefined;
|
|
138
142
|
/**
|
|
139
143
|
* Time Complexity: O(1)
|
|
140
144
|
* Space Complexity: O(1)
|
|
141
145
|
*/
|
|
142
146
|
clear(): void;
|
|
143
|
-
|
|
147
|
+
/**
|
|
148
|
+
* Time Complexity: O(log n)
|
|
149
|
+
* Space Complexity: O(1)
|
|
150
|
+
*/
|
|
151
|
+
/**
|
|
152
|
+
* Time Complexity: O(log n)
|
|
153
|
+
* Space Complexity: O(1)
|
|
154
|
+
*
|
|
155
|
+
* The function returns the predecessor of a given node in a red-black tree.
|
|
156
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `RedBlackTreeNode`, which represents a node in a
|
|
157
|
+
* Red-Black Tree.
|
|
158
|
+
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
159
|
+
*/
|
|
160
|
+
getPredecessor(x: NODE): NODE;
|
|
161
|
+
protected _setRoot(v: NODE): void;
|
|
144
162
|
/**
|
|
145
163
|
* Time Complexity: O(1)
|
|
146
164
|
* Space Complexity: O(1)
|
|
@@ -150,9 +168,9 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
150
168
|
* Space Complexity: O(1)
|
|
151
169
|
*
|
|
152
170
|
* The function performs a left rotation on a binary tree node.
|
|
153
|
-
* @param {RedBlackTreeNode} x - The parameter `x` is of type `
|
|
171
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `NODE`, which likely represents a node in a binary tree.
|
|
154
172
|
*/
|
|
155
|
-
protected _leftRotate(x:
|
|
173
|
+
protected _leftRotate(x: NODE): void;
|
|
156
174
|
/**
|
|
157
175
|
* Time Complexity: O(1)
|
|
158
176
|
* Space Complexity: O(1)
|
|
@@ -165,7 +183,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
165
183
|
* @param {RedBlackTreeNode} x - x is a RedBlackTreeNode, which represents the node that needs to be right
|
|
166
184
|
* rotated.
|
|
167
185
|
*/
|
|
168
|
-
protected _rightRotate(x:
|
|
186
|
+
protected _rightRotate(x: NODE): void;
|
|
169
187
|
/**
|
|
170
188
|
* Time Complexity: O(log n)
|
|
171
189
|
* Space Complexity: O(1)
|
|
@@ -174,44 +192,44 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
174
192
|
* Time Complexity: O(log n)
|
|
175
193
|
* Space Complexity: O(1)
|
|
176
194
|
*
|
|
177
|
-
* The
|
|
178
|
-
* @param {RedBlackTreeNode}
|
|
195
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
196
|
+
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
197
|
+
* red-black tree.
|
|
179
198
|
*/
|
|
180
|
-
protected
|
|
199
|
+
protected _fixInsert(k: NODE): void;
|
|
181
200
|
/**
|
|
182
|
-
* Time Complexity: O(
|
|
201
|
+
* Time Complexity: O(log n)
|
|
183
202
|
* Space Complexity: O(1)
|
|
184
203
|
*/
|
|
185
204
|
/**
|
|
186
|
-
* Time Complexity: O(
|
|
205
|
+
* Time Complexity: O(log n)
|
|
187
206
|
* Space Complexity: O(1)
|
|
188
207
|
*
|
|
189
|
-
* The function `
|
|
190
|
-
* @param {RedBlackTreeNode}
|
|
191
|
-
* @param {RedBlackTreeNode} v - The parameter "v" is a RedBlackTreeNode object.
|
|
208
|
+
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
209
|
+
* @param {RedBlackTreeNode} x - The parameter `x` represents a node in a Red-Black Tree (RBT).
|
|
192
210
|
*/
|
|
193
|
-
protected
|
|
211
|
+
protected _fixDelete(x: NODE): void;
|
|
194
212
|
/**
|
|
195
|
-
* Time Complexity: O(
|
|
213
|
+
* Time Complexity: O(1)
|
|
196
214
|
* Space Complexity: O(1)
|
|
197
215
|
*/
|
|
198
216
|
/**
|
|
199
|
-
* Time Complexity: O(
|
|
217
|
+
* Time Complexity: O(1)
|
|
200
218
|
* Space Complexity: O(1)
|
|
201
219
|
*
|
|
202
|
-
* The `
|
|
203
|
-
* @param {RedBlackTreeNode}
|
|
204
|
-
*
|
|
220
|
+
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
221
|
+
* @param {RedBlackTreeNode} u - The parameter "u" represents a RedBlackTreeNode object.
|
|
222
|
+
* @param {RedBlackTreeNode} v - The parameter "v" is a RedBlackTreeNode object.
|
|
205
223
|
*/
|
|
206
|
-
protected
|
|
224
|
+
protected _rbTransplant(u: NODE, v: NODE): void;
|
|
207
225
|
/**
|
|
208
226
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
209
|
-
* @param {
|
|
210
|
-
* data structure. It is of type `
|
|
211
|
-
* @param {
|
|
227
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
228
|
+
* data structure. It is of type `NODE`, which is the type of the nodes in the data structure.
|
|
229
|
+
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
212
230
|
* data structure.
|
|
213
231
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
214
232
|
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
215
233
|
*/
|
|
216
|
-
protected _replaceNode(oldNode:
|
|
234
|
+
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
|
|
217
235
|
}
|
|
@@ -28,7 +28,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
28
28
|
/**
|
|
29
29
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
30
30
|
* initializes the tree with optional nodes and options.
|
|
31
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
31
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
32
32
|
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
33
33
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
34
34
|
* nodes to the
|
|
@@ -76,14 +76,14 @@ class RedBlackTree extends bst_1.BST {
|
|
|
76
76
|
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
79
|
-
* The function `
|
|
80
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
79
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
80
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
81
81
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
82
|
-
* `
|
|
82
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
83
83
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
84
|
-
* @returns a node of type
|
|
84
|
+
* @returns a node of type NODE or undefined.
|
|
85
85
|
*/
|
|
86
|
-
|
|
86
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
87
87
|
let node;
|
|
88
88
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
89
89
|
return;
|
|
@@ -110,17 +110,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
112
|
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
113
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
113
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
114
114
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
115
115
|
* class.
|
|
116
116
|
*/
|
|
117
117
|
isNode(keyOrNodeOrEntry) {
|
|
118
118
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
119
119
|
}
|
|
120
|
-
/**
|
|
121
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
122
|
-
* Space Complexity: O(1)
|
|
123
|
-
*/
|
|
124
120
|
isRealNode(node) {
|
|
125
121
|
if (node === this.Sentinel || node === undefined)
|
|
126
122
|
return false;
|
|
@@ -129,7 +125,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
129
125
|
/**
|
|
130
126
|
* Time Complexity: O(log n)
|
|
131
127
|
* Space Complexity: O(1)
|
|
132
|
-
*
|
|
128
|
+
* On average (where n is the number of nodes in the tree)
|
|
133
129
|
*/
|
|
134
130
|
/**
|
|
135
131
|
* Time Complexity: O(log n)
|
|
@@ -141,10 +137,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
141
137
|
* entry.
|
|
142
138
|
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
143
139
|
* being added to the binary search tree.
|
|
144
|
-
* @returns The method `add` returns either the newly added node (`
|
|
140
|
+
* @returns The method `add` returns either the newly added node (`NODE`) or `undefined`.
|
|
145
141
|
*/
|
|
146
142
|
add(keyOrNodeOrEntry, value) {
|
|
147
|
-
const newNode = this.
|
|
143
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
148
144
|
if (newNode === undefined)
|
|
149
145
|
return false;
|
|
150
146
|
newNode.left = this.Sentinel;
|
|
@@ -194,7 +190,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
194
190
|
/**
|
|
195
191
|
* Time Complexity: O(log n)
|
|
196
192
|
* Space Complexity: O(1)
|
|
197
|
-
* on average (where n is the number of nodes in the tree)
|
|
198
193
|
*/
|
|
199
194
|
/**
|
|
200
195
|
* Time Complexity: O(log n)
|
|
@@ -206,10 +201,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
206
201
|
* that you want to use to identify the node that you want to delete from the binary tree. It can be
|
|
207
202
|
* of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
208
203
|
* you don't want to
|
|
209
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
204
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` and
|
|
210
205
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
211
206
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
212
|
-
* @returns an array of `BinaryTreeDeleteResult<
|
|
207
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
213
208
|
*/
|
|
214
209
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
215
210
|
const ans = [];
|
|
@@ -285,14 +280,14 @@ class RedBlackTree extends bst_1.BST {
|
|
|
285
280
|
* node that matches the other criteria
|
|
286
281
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
287
282
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
288
|
-
* function should take a single parameter of type `
|
|
289
|
-
* @param {K |
|
|
283
|
+
* function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
|
|
284
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
290
285
|
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
291
286
|
* provided, the search will start from the root of the binary tree.
|
|
292
287
|
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
293
288
|
* iteration to be performed when searching for nodes in the binary tree. It is used in the
|
|
294
289
|
* `getNodes` method, which is called within the `getNode` method.
|
|
295
|
-
* @returns a value of type `
|
|
290
|
+
* @returns a value of type `NODE`, `null`, or `undefined`.
|
|
296
291
|
*/
|
|
297
292
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
298
293
|
var _a;
|
|
@@ -301,6 +296,14 @@ class RedBlackTree extends bst_1.BST {
|
|
|
301
296
|
beginRoot = this.ensureNode(beginRoot);
|
|
302
297
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
303
298
|
}
|
|
299
|
+
/**
|
|
300
|
+
* Time Complexity: O(1)
|
|
301
|
+
* Space Complexity: O(1)
|
|
302
|
+
*/
|
|
303
|
+
clear() {
|
|
304
|
+
this._root = this.Sentinel;
|
|
305
|
+
this._size = 0;
|
|
306
|
+
}
|
|
304
307
|
/**
|
|
305
308
|
* Time Complexity: O(log n)
|
|
306
309
|
* Space Complexity: O(1)
|
|
@@ -325,14 +328,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
325
328
|
}
|
|
326
329
|
return y;
|
|
327
330
|
}
|
|
328
|
-
/**
|
|
329
|
-
* Time Complexity: O(1)
|
|
330
|
-
* Space Complexity: O(1)
|
|
331
|
-
*/
|
|
332
|
-
clear() {
|
|
333
|
-
this._root = this.Sentinel;
|
|
334
|
-
this._size = 0;
|
|
335
|
-
}
|
|
336
331
|
_setRoot(v) {
|
|
337
332
|
if (v) {
|
|
338
333
|
v.parent = undefined;
|
|
@@ -348,7 +343,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
348
343
|
* Space Complexity: O(1)
|
|
349
344
|
*
|
|
350
345
|
* The function performs a left rotation on a binary tree node.
|
|
351
|
-
* @param {RedBlackTreeNode} x - The parameter `x` is of type `
|
|
346
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `NODE`, which likely represents a node in a binary tree.
|
|
352
347
|
*/
|
|
353
348
|
_leftRotate(x) {
|
|
354
349
|
if (x.right) {
|
|
@@ -406,6 +401,63 @@ class RedBlackTree extends bst_1.BST {
|
|
|
406
401
|
x.parent = y;
|
|
407
402
|
}
|
|
408
403
|
}
|
|
404
|
+
/**
|
|
405
|
+
* Time Complexity: O(log n)
|
|
406
|
+
* Space Complexity: O(1)
|
|
407
|
+
*/
|
|
408
|
+
/**
|
|
409
|
+
* Time Complexity: O(log n)
|
|
410
|
+
* Space Complexity: O(1)
|
|
411
|
+
*
|
|
412
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
413
|
+
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
414
|
+
* red-black tree.
|
|
415
|
+
*/
|
|
416
|
+
_fixInsert(k) {
|
|
417
|
+
let u;
|
|
418
|
+
while (k.parent && k.parent.color === 1) {
|
|
419
|
+
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
420
|
+
u = k.parent.parent.left;
|
|
421
|
+
if (u && u.color === 1) {
|
|
422
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
423
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
424
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
425
|
+
k = k.parent.parent;
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
if (k === k.parent.left) {
|
|
429
|
+
k = k.parent;
|
|
430
|
+
this._rightRotate(k);
|
|
431
|
+
}
|
|
432
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
433
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
434
|
+
this._leftRotate(k.parent.parent);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
u = k.parent.parent.right;
|
|
439
|
+
if (u && u.color === 1) {
|
|
440
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
441
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
442
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
443
|
+
k = k.parent.parent;
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
if (k === k.parent.right) {
|
|
447
|
+
k = k.parent;
|
|
448
|
+
this._leftRotate(k);
|
|
449
|
+
}
|
|
450
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
451
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
452
|
+
this._rightRotate(k.parent.parent);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
if (k === this.root) {
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
this.root.color = types_1.RBTNColor.BLACK;
|
|
460
|
+
}
|
|
409
461
|
/**
|
|
410
462
|
* Time Complexity: O(log n)
|
|
411
463
|
* Space Complexity: O(1)
|
|
@@ -505,68 +557,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
505
557
|
}
|
|
506
558
|
v.parent = u.parent;
|
|
507
559
|
}
|
|
508
|
-
/**
|
|
509
|
-
* Time Complexity: O(log n)
|
|
510
|
-
* Space Complexity: O(1)
|
|
511
|
-
*/
|
|
512
|
-
/**
|
|
513
|
-
* Time Complexity: O(log n)
|
|
514
|
-
* Space Complexity: O(1)
|
|
515
|
-
*
|
|
516
|
-
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
517
|
-
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
518
|
-
* red-black tree.
|
|
519
|
-
*/
|
|
520
|
-
_fixInsert(k) {
|
|
521
|
-
let u;
|
|
522
|
-
while (k.parent && k.parent.color === 1) {
|
|
523
|
-
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
524
|
-
u = k.parent.parent.left;
|
|
525
|
-
if (u && u.color === 1) {
|
|
526
|
-
u.color = types_1.RBTNColor.BLACK;
|
|
527
|
-
k.parent.color = types_1.RBTNColor.BLACK;
|
|
528
|
-
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
529
|
-
k = k.parent.parent;
|
|
530
|
-
}
|
|
531
|
-
else {
|
|
532
|
-
if (k === k.parent.left) {
|
|
533
|
-
k = k.parent;
|
|
534
|
-
this._rightRotate(k);
|
|
535
|
-
}
|
|
536
|
-
k.parent.color = types_1.RBTNColor.BLACK;
|
|
537
|
-
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
538
|
-
this._leftRotate(k.parent.parent);
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
else {
|
|
542
|
-
u = k.parent.parent.right;
|
|
543
|
-
if (u && u.color === 1) {
|
|
544
|
-
u.color = types_1.RBTNColor.BLACK;
|
|
545
|
-
k.parent.color = types_1.RBTNColor.BLACK;
|
|
546
|
-
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
547
|
-
k = k.parent.parent;
|
|
548
|
-
}
|
|
549
|
-
else {
|
|
550
|
-
if (k === k.parent.right) {
|
|
551
|
-
k = k.parent;
|
|
552
|
-
this._leftRotate(k);
|
|
553
|
-
}
|
|
554
|
-
k.parent.color = types_1.RBTNColor.BLACK;
|
|
555
|
-
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
556
|
-
this._rightRotate(k.parent.parent);
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
if (k === this.root) {
|
|
560
|
-
break;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
this.root.color = types_1.RBTNColor.BLACK;
|
|
564
|
-
}
|
|
565
560
|
/**
|
|
566
561
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
567
|
-
* @param {
|
|
568
|
-
* data structure. It is of type `
|
|
569
|
-
* @param {
|
|
562
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
563
|
+
* data structure. It is of type `NODE`, which is the type of the nodes in the data structure.
|
|
564
|
+
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
570
565
|
* data structure.
|
|
571
566
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
572
567
|
* superclass, passing in the `oldNode` and `newNode` as arguments.
|