linked-list-typed 2.0.5 → 2.1.0
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/README.md +14 -14
- package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/data-structures/base/iterable-element-base.js +149 -107
- package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/data-structures/base/linear-base.d.ts +250 -192
- package/dist/data-structures/base/linear-base.js +137 -274
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/data-structures/binary-tree/binary-tree.js +598 -869
- package/dist/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/data-structures/binary-tree/bst.js +505 -481
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/data-structures/graph/abstract-graph.js +267 -237
- package/dist/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/data-structures/graph/directed-graph.js +146 -233
- package/dist/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/data-structures/graph/map-graph.js +56 -59
- package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/data-structures/graph/undirected-graph.js +129 -149
- package/dist/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/data-structures/hash/hash-map.js +270 -457
- package/dist/data-structures/heap/heap.d.ts +214 -289
- package/dist/data-structures/heap/heap.js +340 -349
- package/dist/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/data-structures/heap/max-heap.js +11 -66
- package/dist/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/data-structures/heap/min-heap.js +11 -66
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/data-structures/queue/deque.d.ts +227 -254
- package/dist/data-structures/queue/deque.js +309 -348
- package/dist/data-structures/queue/queue.d.ts +180 -201
- package/dist/data-structures/queue/queue.js +265 -248
- package/dist/data-structures/stack/stack.d.ts +124 -102
- package/dist/data-structures/stack/stack.js +181 -125
- package/dist/data-structures/trie/trie.d.ts +164 -165
- package/dist/data-structures/trie/trie.js +189 -172
- package/dist/interfaces/binary-tree.d.ts +56 -6
- package/dist/interfaces/graph.d.ts +16 -0
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/types/utils/utils.d.ts +1 -0
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.js +2 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +664 -893
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +2 -0
- package/src/utils/utils.ts +9 -14
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Pablo Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.RedBlackTree = exports.RedBlackTreeNode = void 0;
|
|
4
11
|
const bst_1 = require("./bst");
|
|
5
12
|
class RedBlackTreeNode extends bst_1.BSTNode {
|
|
6
13
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
9
|
-
*
|
|
10
|
-
* @param
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
13
|
-
* color of the node in a Red-Black Tree. It has a default value of 'BLACK' if not provided
|
|
14
|
-
* explicitly.
|
|
14
|
+
* Create a Red-Black Tree and optionally bulk-insert items.
|
|
15
|
+
* @remarks Time O(n log n), Space O(n)
|
|
16
|
+
* @param key - See parameter type for details.
|
|
17
|
+
* @param [value]- See parameter type for details.
|
|
18
|
+
* @param color - See parameter type for details.
|
|
19
|
+
* @returns New RedBlackTree instance.
|
|
15
20
|
*/
|
|
16
21
|
constructor(key, value, color = 'BLACK') {
|
|
17
22
|
super(key, value);
|
|
@@ -20,18 +25,40 @@ class RedBlackTreeNode extends bst_1.BSTNode {
|
|
|
20
25
|
this._right = undefined;
|
|
21
26
|
this._color = color;
|
|
22
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Get the left child pointer.
|
|
30
|
+
* @remarks Time O(1), Space O(1)
|
|
31
|
+
* @returns Left child node, or null/undefined.
|
|
32
|
+
*/
|
|
23
33
|
get left() {
|
|
24
34
|
return this._left;
|
|
25
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Set the left child and update its parent pointer.
|
|
38
|
+
* @remarks Time O(1), Space O(1)
|
|
39
|
+
* @param v - New left node, or null/undefined.
|
|
40
|
+
* @returns void
|
|
41
|
+
*/
|
|
26
42
|
set left(v) {
|
|
27
43
|
if (v) {
|
|
28
44
|
v.parent = this;
|
|
29
45
|
}
|
|
30
46
|
this._left = v;
|
|
31
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Get the right child pointer.
|
|
50
|
+
* @remarks Time O(1), Space O(1)
|
|
51
|
+
* @returns Right child node, or null/undefined.
|
|
52
|
+
*/
|
|
32
53
|
get right() {
|
|
33
54
|
return this._right;
|
|
34
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Set the right child and update its parent pointer.
|
|
58
|
+
* @remarks Time O(1), Space O(1)
|
|
59
|
+
* @param v - New right node, or null/undefined.
|
|
60
|
+
* @returns void
|
|
61
|
+
*/
|
|
35
62
|
set right(v) {
|
|
36
63
|
if (v) {
|
|
37
64
|
v.parent = this;
|
|
@@ -41,6 +68,11 @@ class RedBlackTreeNode extends bst_1.BSTNode {
|
|
|
41
68
|
}
|
|
42
69
|
exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
43
70
|
/**
|
|
71
|
+
* RRRRed-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
|
|
72
|
+
* @remarks Time O(1), Space O(1)
|
|
73
|
+
* @template K
|
|
74
|
+
* @template V
|
|
75
|
+
* @template R
|
|
44
76
|
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
|
|
45
77
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
46
78
|
* @example
|
|
@@ -88,17 +120,6 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
|
88
120
|
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
|
|
89
121
|
*/
|
|
90
122
|
class RedBlackTree extends bst_1.BST {
|
|
91
|
-
/**
|
|
92
|
-
* This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or
|
|
93
|
-
* raw data.
|
|
94
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
95
|
-
* iterable that can contain either `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined` objects or `R` objects. It
|
|
96
|
-
* is used to initialize the Red-Black Tree with keys, nodes, entries, or
|
|
97
|
-
* @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K,
|
|
98
|
-
* V, R>`. It is an optional parameter that allows you to specify additional options for the
|
|
99
|
-
* RedBlackTree class. These options could include configuration settings, behavior customization, or
|
|
100
|
-
* any other parameters that are specific to
|
|
101
|
-
*/
|
|
102
123
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
103
124
|
super([], options);
|
|
104
125
|
this._root = this.NIL;
|
|
@@ -106,79 +127,49 @@ class RedBlackTree extends bst_1.BST {
|
|
|
106
127
|
this.addMany(keysNodesEntriesOrRaws);
|
|
107
128
|
}
|
|
108
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Get the current root node.
|
|
132
|
+
* @remarks Time O(1), Space O(1)
|
|
133
|
+
* @returns Root node, or undefined.
|
|
134
|
+
*/
|
|
109
135
|
get root() {
|
|
110
136
|
return this._root;
|
|
111
137
|
}
|
|
112
138
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* @param
|
|
118
|
-
*
|
|
119
|
-
* function.
|
|
120
|
-
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
121
|
-
* associated with the key in the node. It is not required and can be omitted if you only need to
|
|
122
|
-
* create a node with a key.
|
|
123
|
-
* @param {RBTNColor} [color=BLACK] - The "color" parameter is used to specify the color of the node
|
|
124
|
-
* in a Red-Black Tree. It can have two possible values: "RED" or "BLACK". By default, the color is
|
|
125
|
-
* set to "BLACK" if not specified.
|
|
126
|
-
* @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
|
|
127
|
-
* returned.
|
|
139
|
+
* Create a red-black node for the given key/value (value ignored in map mode).
|
|
140
|
+
* @remarks Time O(1), Space O(1)
|
|
141
|
+
* @param key - See parameter type for details.
|
|
142
|
+
* @param [value] - See parameter type for details.
|
|
143
|
+
* @param color - See parameter type for details.
|
|
144
|
+
* @returns A new RedBlackTreeNode instance.
|
|
128
145
|
*/
|
|
129
|
-
|
|
146
|
+
_createNode(key, value, color = 'BLACK') {
|
|
130
147
|
return new RedBlackTreeNode(key, this._isMapMode ? undefined : value, color);
|
|
131
148
|
}
|
|
132
149
|
/**
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
138
|
-
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
139
|
-
* @returns a new instance of a RedBlackTree object.
|
|
140
|
-
*/
|
|
141
|
-
createTree(options) {
|
|
142
|
-
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options));
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Time Complexity: O(1)
|
|
146
|
-
* Space Complexity: O(1)
|
|
147
|
-
*
|
|
148
|
-
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
149
|
-
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
150
|
-
* `keyNodeOrEntry` can be of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
151
|
-
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
152
|
-
* an instance of the `RedBlackTreeNode` class.
|
|
150
|
+
* Type guard: check whether the input is a RedBlackTreeNode.
|
|
151
|
+
* @remarks Time O(1), Space O(1)
|
|
152
|
+
* @param keyNodeOrEntry - See parameter type for details.
|
|
153
|
+
* @returns True if the value is a RedBlackTreeNode.
|
|
153
154
|
*/
|
|
154
155
|
isNode(keyNodeOrEntry) {
|
|
155
156
|
return keyNodeOrEntry instanceof RedBlackTreeNode;
|
|
156
157
|
}
|
|
157
158
|
/**
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
* The "clear" function sets the root node of a data structure to a sentinel value and resets the
|
|
162
|
-
* size counter to zero.
|
|
159
|
+
* Remove all nodes and clear the key→value store (if in map mode).
|
|
160
|
+
* @remarks Time O(n), Space O(1)
|
|
161
|
+
* @returns void
|
|
163
162
|
*/
|
|
164
163
|
clear() {
|
|
165
164
|
super.clear();
|
|
166
165
|
this._root = this.NIL;
|
|
167
166
|
}
|
|
168
167
|
/**
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
175
|
-
* `keyNodeOrEntry` can accept a value of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
176
|
-
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
177
|
-
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
178
|
-
* structure.
|
|
179
|
-
* @returns The method is returning a boolean value. If a new node is successfully added to the tree,
|
|
180
|
-
* the method returns true. If the node already exists and its value is updated, the method also
|
|
181
|
-
* returns true. If the node cannot be added or updated, the method returns false.
|
|
168
|
+
* Insert or replace an entry using BST order and red-black fix-up.
|
|
169
|
+
* @remarks Time O(log n), Space O(1)
|
|
170
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
|
|
171
|
+
* @param [value]- See parameter type for details.
|
|
172
|
+
* @returns True if inserted or updated; false if ignored.
|
|
182
173
|
*/
|
|
183
174
|
add(keyNodeOrEntry, value) {
|
|
184
175
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
@@ -186,7 +177,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
186
177
|
return false;
|
|
187
178
|
const insertStatus = this._insert(newNode);
|
|
188
179
|
if (insertStatus === 'CREATED') {
|
|
189
|
-
// Ensure the root is black
|
|
190
180
|
if (this.isRealNode(this._root)) {
|
|
191
181
|
this._root.color = 'BLACK';
|
|
192
182
|
}
|
|
@@ -206,18 +196,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
206
196
|
return false;
|
|
207
197
|
}
|
|
208
198
|
/**
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* a given predicate and maintain the binary search tree properties.
|
|
214
|
-
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
215
|
-
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
216
|
-
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
217
|
-
* function that determines which node(s) should be deleted.
|
|
218
|
-
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>`
|
|
219
|
-
* objects. Each object in the array contains information about the deleted node and whether
|
|
220
|
-
* balancing is needed.
|
|
199
|
+
* Delete a node by key/node/entry and rebalance as needed.
|
|
200
|
+
* @remarks Time O(log n), Space O(1)
|
|
201
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node to delete.
|
|
202
|
+
* @returns Array with deletion metadata (removed node, rebalancing hint if any).
|
|
221
203
|
*/
|
|
222
204
|
delete(keyNodeOrEntry) {
|
|
223
205
|
if (keyNodeOrEntry === null)
|
|
@@ -274,7 +256,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
274
256
|
if (this._isMapMode)
|
|
275
257
|
this._store.delete(nodeToDelete.key);
|
|
276
258
|
this._size--;
|
|
277
|
-
// If the original color was black, fix the tree
|
|
278
259
|
if (originalColor === 'BLACK') {
|
|
279
260
|
this._deleteFixup(replacementNode);
|
|
280
261
|
}
|
|
@@ -282,87 +263,47 @@ class RedBlackTree extends bst_1.BST {
|
|
|
282
263
|
return results;
|
|
283
264
|
}
|
|
284
265
|
/**
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
* @param callback -
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
* @
|
|
294
|
-
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
295
|
-
* Tree that will be created during the mapping process. These options could include things like
|
|
296
|
-
* custom comparators
|
|
297
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
298
|
-
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
299
|
-
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
300
|
-
* or
|
|
301
|
-
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
302
|
-
* provided callback function.
|
|
266
|
+
* Transform entries into a like-kind red-black tree with possibly different key/value types.
|
|
267
|
+
* @remarks Time O(n), Space O(n)
|
|
268
|
+
* @template MK
|
|
269
|
+
* @template MV
|
|
270
|
+
* @template MR
|
|
271
|
+
* @param callback - Mapping function from (key, value, index, tree) to a new [key, value].
|
|
272
|
+
* @param [options] - See parameter type for details.
|
|
273
|
+
* @param [thisArg] - See parameter type for details.
|
|
274
|
+
* @returns A new RedBlackTree with mapped entries.
|
|
303
275
|
*/
|
|
304
276
|
map(callback, options, thisArg) {
|
|
305
|
-
const
|
|
277
|
+
const out = this._createLike([], options);
|
|
306
278
|
let index = 0;
|
|
307
279
|
for (const [key, value] of this) {
|
|
308
|
-
|
|
280
|
+
out.add(callback.call(thisArg, key, value, index++, this));
|
|
309
281
|
}
|
|
310
|
-
return
|
|
282
|
+
return out;
|
|
311
283
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
*/
|
|
320
|
-
clone() {
|
|
321
|
-
const cloned = this.createTree();
|
|
322
|
-
this._clone(cloned);
|
|
323
|
-
return cloned;
|
|
284
|
+
_createInstance(options) {
|
|
285
|
+
const Ctor = this.constructor;
|
|
286
|
+
return new Ctor([], Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
|
|
287
|
+
}
|
|
288
|
+
_createLike(iter = [], options) {
|
|
289
|
+
const Ctor = this.constructor;
|
|
290
|
+
return new Ctor(iter, Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
|
|
324
291
|
}
|
|
325
|
-
/**
|
|
326
|
-
* Time Complexity: O(1)
|
|
327
|
-
* Space Complexity: O(1)
|
|
328
|
-
*
|
|
329
|
-
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
330
|
-
* root.
|
|
331
|
-
* @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined.
|
|
332
|
-
*/
|
|
333
292
|
_setRoot(v) {
|
|
334
293
|
if (v) {
|
|
335
294
|
v.parent = undefined;
|
|
336
295
|
}
|
|
337
296
|
this._root = v;
|
|
338
297
|
}
|
|
339
|
-
/**
|
|
340
|
-
* Time Complexity: O(1)
|
|
341
|
-
* Space Complexity: O(1)
|
|
342
|
-
*
|
|
343
|
-
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
344
|
-
* @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
345
|
-
* the data structure.
|
|
346
|
-
* @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a
|
|
347
|
-
* data structure.
|
|
348
|
-
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
349
|
-
* superclass, with the `oldNode` and `newNode` parameters.
|
|
350
|
-
*/
|
|
351
298
|
_replaceNode(oldNode, newNode) {
|
|
352
299
|
newNode.color = oldNode.color;
|
|
353
300
|
return super._replaceNode(oldNode, newNode);
|
|
354
301
|
}
|
|
355
302
|
/**
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
* maintain the red-black tree properties.
|
|
361
|
-
* @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the
|
|
362
|
-
* binary search tree.
|
|
363
|
-
* @returns a string value indicating the result of the insertion operation. It can return either
|
|
364
|
-
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
|
|
365
|
-
* was created and inserted into the tree.
|
|
303
|
+
* (Protected) Standard BST insert followed by red-black fix-up.
|
|
304
|
+
* @remarks Time O(log n), Space O(1)
|
|
305
|
+
* @param node - Node to insert.
|
|
306
|
+
* @returns Status string: 'CREATED' or 'UPDATED'.
|
|
366
307
|
*/
|
|
367
308
|
_insert(node) {
|
|
368
309
|
var _a, _b;
|
|
@@ -399,13 +340,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
399
340
|
return 'CREATED';
|
|
400
341
|
}
|
|
401
342
|
/**
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
*
|
|
406
|
-
* @
|
|
407
|
-
* @param {RedBlackTreeNode<K, V> | undefined} v - The parameter `v` is of type `RedBlackTreeNode<K, V> | undefined`, which means it can
|
|
408
|
-
* either be a `RedBlackTreeNode<K, V>` object or `undefined`.
|
|
343
|
+
* (Protected) Transplant a subtree in place of another during deletion.
|
|
344
|
+
* @remarks Time O(1), Space O(1)
|
|
345
|
+
* @param u - Node to replace.
|
|
346
|
+
* @param v - Replacement subtree root (may be undefined).
|
|
347
|
+
* @returns void
|
|
409
348
|
*/
|
|
410
349
|
_transplant(u, v) {
|
|
411
350
|
if (!u.parent) {
|
|
@@ -422,38 +361,27 @@ class RedBlackTree extends bst_1.BST {
|
|
|
422
361
|
}
|
|
423
362
|
}
|
|
424
363
|
/**
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
* @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
|
|
430
|
-
* structure. It can either be a valid node or `undefined`.
|
|
364
|
+
* (Protected) Restore red-black properties after insertion (recolor/rotate).
|
|
365
|
+
* @remarks Time O(log n), Space O(1)
|
|
366
|
+
* @param z - Recently inserted node.
|
|
367
|
+
* @returns void
|
|
431
368
|
*/
|
|
432
369
|
_insertFixup(z) {
|
|
433
370
|
var _a, _b, _c, _d, _e;
|
|
434
|
-
// Continue fixing the tree as long as the parent of z is red
|
|
435
371
|
while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
|
|
436
|
-
// Check if the parent of z is the left child of its parent
|
|
437
372
|
if (z.parent === ((_b = z.parent.parent) === null || _b === void 0 ? void 0 : _b.left)) {
|
|
438
|
-
// Case 1: The uncle (y) of z is red
|
|
439
373
|
const y = z.parent.parent.right;
|
|
440
374
|
if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
|
|
441
|
-
// Set colors to restore properties of Red-Black Tree
|
|
442
375
|
z.parent.color = 'BLACK';
|
|
443
376
|
y.color = 'BLACK';
|
|
444
377
|
z.parent.parent.color = 'RED';
|
|
445
|
-
// Move up the tree to continue fixing
|
|
446
378
|
z = z.parent.parent;
|
|
447
379
|
}
|
|
448
380
|
else {
|
|
449
|
-
// Case 2: The uncle (y) of z is black, and z is a right child
|
|
450
381
|
if (z === z.parent.right) {
|
|
451
|
-
// Perform a left rotation to transform the case into Case 3
|
|
452
382
|
z = z.parent;
|
|
453
383
|
this._leftRotate(z);
|
|
454
384
|
}
|
|
455
|
-
// Case 3: The uncle (y) of z is black, and z is a left child
|
|
456
|
-
// Adjust colors and perform a right rotation
|
|
457
385
|
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
458
386
|
z.parent.color = 'BLACK';
|
|
459
387
|
z.parent.parent.color = 'RED';
|
|
@@ -462,8 +390,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
462
390
|
}
|
|
463
391
|
}
|
|
464
392
|
else {
|
|
465
|
-
// Symmetric case for the right child (left and right exchanged)
|
|
466
|
-
// Follow the same logic as above with left and right exchanged
|
|
467
393
|
const y = (_e = (_d = (_c = z === null || z === void 0 ? void 0 : z.parent) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.left) !== null && _e !== void 0 ? _e : undefined;
|
|
468
394
|
if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
|
|
469
395
|
z.parent.color = 'BLACK';
|
|
@@ -484,52 +410,42 @@ class RedBlackTree extends bst_1.BST {
|
|
|
484
410
|
}
|
|
485
411
|
}
|
|
486
412
|
}
|
|
487
|
-
// Ensure that the root is black after fixing
|
|
488
413
|
if (this.isRealNode(this._root))
|
|
489
414
|
this._root.color = 'BLACK';
|
|
490
415
|
}
|
|
491
416
|
/**
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
* the colors and performing rotations.
|
|
497
|
-
* @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can
|
|
498
|
-
* be either a valid node object or `undefined`.
|
|
499
|
-
* @returns The function does not return any value. It has a return type of `void`, which means it
|
|
500
|
-
* does not return anything.
|
|
417
|
+
* (Protected) Restore red-black properties after deletion (recolor/rotate).
|
|
418
|
+
* @remarks Time O(log n), Space O(1)
|
|
419
|
+
* @param node - Child that replaced the deleted node (may be undefined).
|
|
420
|
+
* @returns void
|
|
501
421
|
*/
|
|
502
422
|
_deleteFixup(node) {
|
|
503
423
|
var _a, _b, _c, _d;
|
|
504
|
-
// Early exit condition
|
|
505
424
|
if (!node || node === this.root || node.color === 'BLACK') {
|
|
506
425
|
if (node) {
|
|
507
|
-
node.color = 'BLACK';
|
|
426
|
+
node.color = 'BLACK';
|
|
508
427
|
}
|
|
509
428
|
return;
|
|
510
429
|
}
|
|
511
430
|
while (node && node !== this.root && node.color === 'BLACK') {
|
|
512
431
|
const parent = node.parent;
|
|
513
432
|
if (!parent) {
|
|
514
|
-
break;
|
|
433
|
+
break;
|
|
515
434
|
}
|
|
516
435
|
if (node === parent.left) {
|
|
517
436
|
let sibling = parent.right;
|
|
518
|
-
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
519
437
|
if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) === 'RED') {
|
|
520
438
|
sibling.color = 'BLACK';
|
|
521
439
|
parent.color = 'RED';
|
|
522
440
|
this._leftRotate(parent);
|
|
523
441
|
sibling = parent.right;
|
|
524
442
|
}
|
|
525
|
-
// Case 3: Sibling's left child is black
|
|
526
443
|
if (((_b = (_a = sibling === null || sibling === void 0 ? void 0 : sibling.left) === null || _a === void 0 ? void 0 : _a.color) !== null && _b !== void 0 ? _b : 'BLACK') === 'BLACK') {
|
|
527
444
|
if (sibling)
|
|
528
445
|
sibling.color = 'RED';
|
|
529
446
|
node = parent;
|
|
530
447
|
}
|
|
531
448
|
else {
|
|
532
|
-
// Case 4: Adjust colors and perform a right rotation
|
|
533
449
|
if (sibling === null || sibling === void 0 ? void 0 : sibling.left)
|
|
534
450
|
sibling.left.color = 'BLACK';
|
|
535
451
|
if (sibling)
|
|
@@ -540,9 +456,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
540
456
|
}
|
|
541
457
|
}
|
|
542
458
|
else {
|
|
543
|
-
// Symmetric case for the right child (left and right exchanged)
|
|
544
459
|
let sibling = parent.left;
|
|
545
|
-
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
546
460
|
if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) === 'RED') {
|
|
547
461
|
sibling.color = 'BLACK';
|
|
548
462
|
if (parent)
|
|
@@ -551,14 +465,12 @@ class RedBlackTree extends bst_1.BST {
|
|
|
551
465
|
if (parent)
|
|
552
466
|
sibling = parent.left;
|
|
553
467
|
}
|
|
554
|
-
// Case 3: Sibling's left child is black
|
|
555
468
|
if (((_d = (_c = sibling === null || sibling === void 0 ? void 0 : sibling.right) === null || _c === void 0 ? void 0 : _c.color) !== null && _d !== void 0 ? _d : 'BLACK') === 'BLACK') {
|
|
556
469
|
if (sibling)
|
|
557
470
|
sibling.color = 'RED';
|
|
558
471
|
node = parent;
|
|
559
472
|
}
|
|
560
473
|
else {
|
|
561
|
-
// Case 4: Adjust colors and perform a left rotation
|
|
562
474
|
if (sibling === null || sibling === void 0 ? void 0 : sibling.right)
|
|
563
475
|
sibling.right.color = 'BLACK';
|
|
564
476
|
if (sibling)
|
|
@@ -570,19 +482,15 @@ class RedBlackTree extends bst_1.BST {
|
|
|
570
482
|
}
|
|
571
483
|
}
|
|
572
484
|
}
|
|
573
|
-
// Ensure that the final node (possibly the root) is black
|
|
574
485
|
if (node) {
|
|
575
486
|
node.color = 'BLACK';
|
|
576
487
|
}
|
|
577
488
|
}
|
|
578
489
|
/**
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
* @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
|
|
584
|
-
* node in a binary tree or `undefined` if there is no node.
|
|
585
|
-
* @returns void, which means it does not return any value.
|
|
490
|
+
* (Protected) Perform a left rotation around x.
|
|
491
|
+
* @remarks Time O(1), Space O(1)
|
|
492
|
+
* @param x - Pivot node to rotate around.
|
|
493
|
+
* @returns void
|
|
586
494
|
*/
|
|
587
495
|
_leftRotate(x) {
|
|
588
496
|
if (!x || !x.right) {
|
|
@@ -607,13 +515,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
607
515
|
x.parent = y;
|
|
608
516
|
}
|
|
609
517
|
/**
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
614
|
-
* @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
|
|
615
|
-
* node in a binary tree or `undefined` if there is no node.
|
|
616
|
-
* @returns void, which means it does not return any value.
|
|
518
|
+
* (Protected) Perform a right rotation around y.
|
|
519
|
+
* @remarks Time O(1), Space O(1)
|
|
520
|
+
* @param y - Pivot node to rotate around.
|
|
521
|
+
* @returns void
|
|
617
522
|
*/
|
|
618
523
|
_rightRotate(y) {
|
|
619
524
|
if (!y || !y.left) {
|