red-black-tree-typed 1.53.7 → 1.54.1
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 +52 -0
- package/dist/common/index.js +5 -0
- package/dist/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -328
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +130 -70
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +268 -202
- package/dist/data-structures/binary-tree/binary-tree.js +311 -263
- package/dist/data-structures/binary-tree/bst.d.ts +193 -139
- package/dist/data-structures/binary-tree/bst.js +248 -164
- package/dist/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/data-structures/binary-tree/index.js +3 -1
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +176 -107
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
- package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/hash/hash-map.d.ts +31 -1
- package/dist/data-structures/hash/hash-map.js +35 -5
- package/dist/data-structures/heap/heap.d.ts +20 -3
- package/dist/data-structures/heap/heap.js +31 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
- package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +47 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +73 -26
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +8 -7
- package/dist/data-structures/trie/trie.js +8 -7
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/index.js +2 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/package.json +2 -2
- package/src/common/index.ts +7 -1
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +151 -370
- package/src/data-structures/binary-tree/avl-tree.ts +162 -105
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +488 -416
- package/src/data-structures/binary-tree/bst.ts +326 -251
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +219 -145
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/hash/hash-map.ts +37 -7
- package/src/data-structures/heap/heap.ts +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
- package/src/data-structures/linked-list/singly-linked-list.ts +80 -27
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +8 -7
- package/src/index.ts +2 -2
- package/src/interfaces/binary-tree.ts +10 -21
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -209
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
|
@@ -4,50 +4,106 @@ exports.RedBlackTree = exports.RedBlackTreeNode = void 0;
|
|
|
4
4
|
const bst_1 = require("./bst");
|
|
5
5
|
class RedBlackTreeNode extends bst_1.BSTNode {
|
|
6
6
|
/**
|
|
7
|
-
* The constructor
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* Tree Node. It is an optional parameter with a default value of `'BLACK'`.
|
|
7
|
+
* The constructor initializes a node with a key, value, and color for a Red-Black Tree.
|
|
8
|
+
* @param {K} key - The `key` parameter is a key of type `K` that is used to identify the node in a
|
|
9
|
+
* Red-Black Tree data structure.
|
|
10
|
+
* @param {V} [value] - The `value` parameter in the constructor is an optional parameter of type
|
|
11
|
+
* `V`. It represents the value associated with the key in the data structure being constructed.
|
|
12
|
+
* @param {RBTNColor} [color=BLACK] - The `color` parameter in the constructor is used to specify the
|
|
13
|
+
* color of the node in a Red-Black Tree. It has a default value of 'BLACK' if not provided
|
|
14
|
+
* explicitly.
|
|
16
15
|
*/
|
|
17
16
|
constructor(key, value, color = 'BLACK') {
|
|
18
17
|
super(key, value);
|
|
18
|
+
this.parent = undefined;
|
|
19
|
+
this._left = undefined;
|
|
20
|
+
this._right = undefined;
|
|
19
21
|
this._color = color;
|
|
20
22
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
* @returns The color value stored in the private variable `_color`.
|
|
24
|
-
*/
|
|
25
|
-
get color() {
|
|
26
|
-
return this._color;
|
|
23
|
+
get left() {
|
|
24
|
+
return this._left;
|
|
27
25
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
set left(v) {
|
|
27
|
+
if (v) {
|
|
28
|
+
v.parent = this;
|
|
29
|
+
}
|
|
30
|
+
this._left = v;
|
|
31
|
+
}
|
|
32
|
+
get right() {
|
|
33
|
+
return this._right;
|
|
34
|
+
}
|
|
35
|
+
set right(v) {
|
|
36
|
+
if (v) {
|
|
37
|
+
v.parent = this;
|
|
38
|
+
}
|
|
39
|
+
this._right = v;
|
|
34
40
|
}
|
|
35
41
|
}
|
|
36
42
|
exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
37
43
|
/**
|
|
38
44
|
* 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.
|
|
39
45
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
46
|
+
* @example
|
|
47
|
+
* // Find elements in a range
|
|
48
|
+
* const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
49
|
+
* console.log(bst.search(new Range(5, 10))); // [5, 10, 7]
|
|
50
|
+
* console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7]
|
|
51
|
+
* console.log(bst.search(new Range(15, 20))); // [15, 18]
|
|
52
|
+
* @example
|
|
53
|
+
* // using Red-Black Tree as a price-based index for stock data
|
|
54
|
+
* // Define the structure of individual stock records
|
|
55
|
+
* interface StockRecord {
|
|
56
|
+
* price: number; // Stock price (key for indexing)
|
|
57
|
+
* symbol: string; // Stock ticker symbol
|
|
58
|
+
* volume: number; // Trade volume
|
|
59
|
+
* }
|
|
60
|
+
*
|
|
61
|
+
* // Simulate stock market data as it might come from an external feed
|
|
62
|
+
* const marketStockData: StockRecord[] = [
|
|
63
|
+
* { price: 142.5, symbol: 'AAPL', volume: 1000000 },
|
|
64
|
+
* { price: 335.2, symbol: 'MSFT', volume: 800000 },
|
|
65
|
+
* { price: 3285.04, symbol: 'AMZN', volume: 500000 },
|
|
66
|
+
* { price: 267.98, symbol: 'META', volume: 750000 },
|
|
67
|
+
* { price: 234.57, symbol: 'GOOGL', volume: 900000 }
|
|
68
|
+
* ];
|
|
69
|
+
*
|
|
70
|
+
* // Extend the stock record type to include metadata for database usage
|
|
71
|
+
* type StockTableRecord = StockRecord & { lastUpdated: Date };
|
|
72
|
+
*
|
|
73
|
+
* // Create a Red-Black Tree to index stock records by price
|
|
74
|
+
* // Simulates a database index with stock price as the key for quick lookups
|
|
75
|
+
* const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
|
|
76
|
+
* toEntryFn: stockRecord => [
|
|
77
|
+
* stockRecord.price, // Use stock price as the key
|
|
78
|
+
* {
|
|
79
|
+
* ...stockRecord,
|
|
80
|
+
* lastUpdated: new Date() // Add a timestamp for when the record was indexed
|
|
81
|
+
* }
|
|
82
|
+
* ]
|
|
83
|
+
* });
|
|
84
|
+
*
|
|
85
|
+
* // Query the stock with the highest price
|
|
86
|
+
* const highestPricedStock = priceIndex.getRightMost();
|
|
87
|
+
* console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
|
|
88
|
+
*
|
|
89
|
+
* // Query stocks within a specific price range (200 to 400)
|
|
90
|
+
* const stocksInRange = priceIndex.rangeSearch(
|
|
91
|
+
* [200, 400], // Price range
|
|
92
|
+
* node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
|
|
93
|
+
* );
|
|
94
|
+
* console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
|
|
40
95
|
*/
|
|
41
96
|
class RedBlackTree extends bst_1.BST {
|
|
42
97
|
/**
|
|
43
|
-
* This
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
98
|
+
* This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or
|
|
99
|
+
* raw data.
|
|
100
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
101
|
+
* iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It
|
|
102
|
+
* is used to initialize the Red-Black Tree with keys, nodes, entries, or
|
|
103
|
+
* @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K,
|
|
104
|
+
* V, R>`. It is an optional parameter that allows you to specify additional options for the
|
|
105
|
+
* RedBlackTree class. These options could include configuration settings, behavior customization, or
|
|
106
|
+
* any other parameters that are specific to
|
|
51
107
|
*/
|
|
52
108
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
53
109
|
super([], options);
|
|
@@ -56,14 +112,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
56
112
|
this.addMany(keysNodesEntriesOrRaws);
|
|
57
113
|
}
|
|
58
114
|
}
|
|
59
|
-
/**
|
|
60
|
-
* The function returns the root node of a tree or undefined if there is no root.
|
|
61
|
-
* @returns The root node of the tree structure, or undefined if there is no root node.
|
|
62
|
-
*/
|
|
63
115
|
get root() {
|
|
64
116
|
return this._root;
|
|
65
117
|
}
|
|
66
118
|
/**
|
|
119
|
+
* Time Complexity: O(1)
|
|
120
|
+
* Space Complexity: O(1)
|
|
121
|
+
*
|
|
67
122
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
68
123
|
* @param {K} key - The key parameter represents the key value of the node being created. It is of
|
|
69
124
|
* type K, which is a generic type that can be replaced with any specific type when using the
|
|
@@ -81,62 +136,30 @@ class RedBlackTree extends bst_1.BST {
|
|
|
81
136
|
return new RedBlackTreeNode(key, this._isMapMode ? undefined : value, color);
|
|
82
137
|
}
|
|
83
138
|
/**
|
|
139
|
+
* Time Complexity: O(1)
|
|
140
|
+
* Space Complexity: O(1)
|
|
141
|
+
*
|
|
84
142
|
* The function creates a new Red-Black Tree with the specified options.
|
|
85
143
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
86
144
|
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
87
145
|
* @returns a new instance of a RedBlackTree object.
|
|
88
146
|
*/
|
|
89
147
|
createTree(options) {
|
|
90
|
-
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
148
|
+
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options));
|
|
91
149
|
}
|
|
92
150
|
/**
|
|
93
151
|
* Time Complexity: O(1)
|
|
94
152
|
* Space Complexity: O(1)
|
|
95
153
|
*
|
|
96
154
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
97
|
-
* @param {BTNRep<K, V,
|
|
98
|
-
* `
|
|
99
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
155
|
+
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
156
|
+
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
|
|
157
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
100
158
|
* an instance of the `RedBlackTreeNode` class.
|
|
101
159
|
*/
|
|
102
|
-
isNode(
|
|
103
|
-
return
|
|
160
|
+
isNode(keyNodeOrEntry) {
|
|
161
|
+
return keyNodeOrEntry instanceof RedBlackTreeNode;
|
|
104
162
|
}
|
|
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 `keyValueNodeEntryRawToNodeAndValue` takes a key, value, or entry and returns a node if it is
|
|
115
|
-
// * valid, otherwise it returns undefined.
|
|
116
|
-
// * @param {BTNRep<K, V, NODE>} keyNodeEntryOrRaw - The key, value, or entry to convert.
|
|
117
|
-
// * @param {V} [value] - The value associated with the key (if `keyNodeEntryOrRaw` is a key).
|
|
118
|
-
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
119
|
-
// */
|
|
120
|
-
// override keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): NODE | undefined {
|
|
121
|
-
//
|
|
122
|
-
// if (keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === undefined) return;
|
|
123
|
-
// if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw;
|
|
124
|
-
//
|
|
125
|
-
// if (this._toEntryFn) {
|
|
126
|
-
// const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
127
|
-
// if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'RED');
|
|
128
|
-
// }
|
|
129
|
-
//
|
|
130
|
-
// if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
131
|
-
// const [key, value] = keyNodeEntryOrRaw;
|
|
132
|
-
// if (key === undefined || key === null) return;
|
|
133
|
-
// else return this.createNode(key, value, 'RED');
|
|
134
|
-
// }
|
|
135
|
-
//
|
|
136
|
-
// if (this.isKey(keyNodeEntryOrRaw)) return this.createNode(keyNodeEntryOrRaw, value, 'RED');
|
|
137
|
-
//
|
|
138
|
-
// return ;
|
|
139
|
-
// }
|
|
140
163
|
/**
|
|
141
164
|
* Time Complexity: O(1)
|
|
142
165
|
* Space Complexity: O(1)
|
|
@@ -150,12 +173,12 @@ class RedBlackTree extends bst_1.BST {
|
|
|
150
173
|
}
|
|
151
174
|
/**
|
|
152
175
|
* Time Complexity: O(log n)
|
|
153
|
-
* Space Complexity: O(
|
|
176
|
+
* Space Complexity: O(log n)
|
|
154
177
|
*
|
|
155
178
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
156
179
|
* added.
|
|
157
|
-
* @param {BTNRep<K, V,
|
|
158
|
-
* `
|
|
180
|
+
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
181
|
+
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
|
|
159
182
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
160
183
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
161
184
|
* structure.
|
|
@@ -163,8 +186,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
163
186
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
164
187
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
165
188
|
*/
|
|
166
|
-
add(
|
|
167
|
-
const [newNode, newValue] = this.
|
|
189
|
+
add(keyNodeOrEntry, value) {
|
|
190
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
168
191
|
if (!this.isRealNode(newNode))
|
|
169
192
|
return false;
|
|
170
193
|
const insertStatus = this._insert(newNode);
|
|
@@ -190,35 +213,37 @@ class RedBlackTree extends bst_1.BST {
|
|
|
190
213
|
}
|
|
191
214
|
/**
|
|
192
215
|
* Time Complexity: O(log n)
|
|
193
|
-
* Space Complexity: O(
|
|
216
|
+
* Space Complexity: O(log n)
|
|
194
217
|
*
|
|
195
218
|
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
196
219
|
* a given predicate and maintain the binary search tree properties.
|
|
197
|
-
* @param {BTNRep<K, V,
|
|
220
|
+
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
198
221
|
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
199
222
|
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
200
223
|
* function that determines which node(s) should be deleted.
|
|
201
|
-
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<
|
|
224
|
+
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>`
|
|
202
225
|
* objects. Each object in the array contains information about the deleted node and whether
|
|
203
226
|
* balancing is needed.
|
|
204
227
|
*/
|
|
205
|
-
delete(
|
|
206
|
-
if (
|
|
228
|
+
delete(keyNodeOrEntry) {
|
|
229
|
+
if (keyNodeOrEntry === null)
|
|
207
230
|
return [];
|
|
208
231
|
const results = [];
|
|
209
232
|
let nodeToDelete;
|
|
210
|
-
if (this._isPredicate(
|
|
211
|
-
nodeToDelete = this.getNode(
|
|
233
|
+
if (this._isPredicate(keyNodeOrEntry))
|
|
234
|
+
nodeToDelete = this.getNode(keyNodeOrEntry);
|
|
212
235
|
else
|
|
213
|
-
nodeToDelete = this.isRealNode(
|
|
236
|
+
nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
|
|
214
237
|
if (!nodeToDelete) {
|
|
215
238
|
return results;
|
|
216
239
|
}
|
|
217
240
|
let originalColor = nodeToDelete.color;
|
|
218
241
|
let replacementNode;
|
|
219
242
|
if (!this.isRealNode(nodeToDelete.left)) {
|
|
220
|
-
|
|
221
|
-
|
|
243
|
+
if (nodeToDelete.right !== null) {
|
|
244
|
+
replacementNode = nodeToDelete.right;
|
|
245
|
+
this._transplant(nodeToDelete, nodeToDelete.right);
|
|
246
|
+
}
|
|
222
247
|
}
|
|
223
248
|
else if (!this.isRealNode(nodeToDelete.right)) {
|
|
224
249
|
replacementNode = nodeToDelete.left;
|
|
@@ -228,15 +253,18 @@ class RedBlackTree extends bst_1.BST {
|
|
|
228
253
|
const successor = this.getLeftMost(node => node, nodeToDelete.right);
|
|
229
254
|
if (successor) {
|
|
230
255
|
originalColor = successor.color;
|
|
231
|
-
|
|
256
|
+
if (successor.right !== null)
|
|
257
|
+
replacementNode = successor.right;
|
|
232
258
|
if (successor.parent === nodeToDelete) {
|
|
233
259
|
if (this.isRealNode(replacementNode)) {
|
|
234
260
|
replacementNode.parent = successor;
|
|
235
261
|
}
|
|
236
262
|
}
|
|
237
263
|
else {
|
|
238
|
-
|
|
239
|
-
|
|
264
|
+
if (successor.right !== null) {
|
|
265
|
+
this._transplant(successor, successor.right);
|
|
266
|
+
successor.right = nodeToDelete.right;
|
|
267
|
+
}
|
|
240
268
|
if (this.isRealNode(successor.right)) {
|
|
241
269
|
successor.right.parent = successor;
|
|
242
270
|
}
|
|
@@ -259,13 +287,54 @@ class RedBlackTree extends bst_1.BST {
|
|
|
259
287
|
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
260
288
|
return results;
|
|
261
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* Time Complexity: O(n)
|
|
292
|
+
* Space Complexity: O(n)
|
|
293
|
+
*
|
|
294
|
+
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
295
|
+
* applying a callback to each entry in the original tree.
|
|
296
|
+
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
297
|
+
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
298
|
+
* tree.
|
|
299
|
+
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
300
|
+
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
301
|
+
* Tree that will be created during the mapping process. These options could include things like
|
|
302
|
+
* custom comparators
|
|
303
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
304
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
305
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
306
|
+
* or
|
|
307
|
+
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
308
|
+
* provided callback function.
|
|
309
|
+
*/
|
|
310
|
+
map(callback, options, thisArg) {
|
|
311
|
+
const newTree = new RedBlackTree([], options);
|
|
312
|
+
let index = 0;
|
|
313
|
+
for (const [key, value] of this) {
|
|
314
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
315
|
+
}
|
|
316
|
+
return newTree;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Time Complexity: O(n)
|
|
320
|
+
* Space Complexity: O(n)
|
|
321
|
+
*
|
|
322
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
323
|
+
* structure.
|
|
324
|
+
* @returns The `cloned` object is being returned.
|
|
325
|
+
*/
|
|
326
|
+
clone() {
|
|
327
|
+
const cloned = this.createTree();
|
|
328
|
+
this._clone(cloned);
|
|
329
|
+
return cloned;
|
|
330
|
+
}
|
|
262
331
|
/**
|
|
263
332
|
* Time Complexity: O(1)
|
|
264
333
|
* Space Complexity: O(1)
|
|
265
334
|
*
|
|
266
335
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
267
336
|
* root.
|
|
268
|
-
* @param {
|
|
337
|
+
* @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined.
|
|
269
338
|
*/
|
|
270
339
|
_setRoot(v) {
|
|
271
340
|
if (v) {
|
|
@@ -278,9 +347,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
278
347
|
* Space Complexity: O(1)
|
|
279
348
|
*
|
|
280
349
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
281
|
-
* @param {
|
|
350
|
+
* @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
282
351
|
* the data structure.
|
|
283
|
-
* @param {
|
|
352
|
+
* @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a
|
|
284
353
|
* data structure.
|
|
285
354
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
286
355
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
@@ -291,11 +360,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
291
360
|
}
|
|
292
361
|
/**
|
|
293
362
|
* Time Complexity: O(log n)
|
|
294
|
-
* Space Complexity: O(
|
|
363
|
+
* Space Complexity: O(log n)
|
|
295
364
|
*
|
|
296
365
|
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
|
|
297
366
|
* maintain the red-black tree properties.
|
|
298
|
-
* @param {
|
|
367
|
+
* @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the
|
|
299
368
|
* binary search tree.
|
|
300
369
|
* @returns a string value indicating the result of the insertion operation. It can return either
|
|
301
370
|
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
|
|
@@ -323,7 +392,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
323
392
|
if (!parent) {
|
|
324
393
|
this._setRoot(node);
|
|
325
394
|
}
|
|
326
|
-
else if (node.key
|
|
395
|
+
else if (this._compare(node.key, parent.key) < 0) {
|
|
327
396
|
parent.left = node;
|
|
328
397
|
}
|
|
329
398
|
else {
|
|
@@ -340,9 +409,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
340
409
|
* Space Complexity: O(1)
|
|
341
410
|
*
|
|
342
411
|
* The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
|
|
343
|
-
* @param {
|
|
344
|
-
* @param {
|
|
345
|
-
* either be a `
|
|
412
|
+
* @param {RedBlackTreeNode<K, V>} u - The parameter "u" represents a node in a binary tree.
|
|
413
|
+
* @param {RedBlackTreeNode<K, V> | undefined} v - The parameter `v` is of type `RedBlackTreeNode<K, V> | undefined`, which means it can
|
|
414
|
+
* either be a `RedBlackTreeNode<K, V>` object or `undefined`.
|
|
346
415
|
*/
|
|
347
416
|
_transplant(u, v) {
|
|
348
417
|
if (!u.parent) {
|
|
@@ -363,11 +432,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
363
432
|
* Space Complexity: O(1)
|
|
364
433
|
*
|
|
365
434
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
366
|
-
* @param {
|
|
435
|
+
* @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
|
|
367
436
|
* structure. It can either be a valid node or `undefined`.
|
|
368
437
|
*/
|
|
369
438
|
_insertFixup(z) {
|
|
370
|
-
var _a, _b, _c, _d;
|
|
439
|
+
var _a, _b, _c, _d, _e;
|
|
371
440
|
// Continue fixing the tree as long as the parent of z is red
|
|
372
441
|
while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
|
|
373
442
|
// Check if the parent of z is the left child of its parent
|
|
@@ -401,7 +470,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
401
470
|
else {
|
|
402
471
|
// Symmetric case for the right child (left and right exchanged)
|
|
403
472
|
// Follow the same logic as above with left and right exchanged
|
|
404
|
-
const y = (_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;
|
|
473
|
+
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;
|
|
405
474
|
if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
|
|
406
475
|
z.parent.color = 'BLACK';
|
|
407
476
|
y.color = 'BLACK';
|
|
@@ -431,7 +500,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
431
500
|
*
|
|
432
501
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
433
502
|
* the colors and performing rotations.
|
|
434
|
-
* @param {
|
|
503
|
+
* @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can
|
|
435
504
|
* be either a valid node object or `undefined`.
|
|
436
505
|
* @returns The function does not return any value. It has a return type of `void`, which means it
|
|
437
506
|
* does not return anything.
|
|
@@ -517,7 +586,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
517
586
|
* Space Complexity: O(1)
|
|
518
587
|
*
|
|
519
588
|
* The `_leftRotate` function performs a left rotation on a given node in a binary tree.
|
|
520
|
-
* @param {
|
|
589
|
+
* @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
|
|
521
590
|
* node in a binary tree or `undefined` if there is no node.
|
|
522
591
|
* @returns void, which means it does not return any value.
|
|
523
592
|
*/
|
|
@@ -548,7 +617,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
548
617
|
* Space Complexity: O(1)
|
|
549
618
|
*
|
|
550
619
|
* The `_rightRotate` function performs a right rotation on a given node in a binary tree.
|
|
551
|
-
* @param {
|
|
620
|
+
* @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
|
|
552
621
|
* node in a binary tree or `undefined` if there is no node.
|
|
553
622
|
* @returns void, which means it does not return any value.
|
|
554
623
|
*/
|