red-black-tree-typed 1.53.6 → 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.d.ts +12 -0
- package/dist/common/index.js +28 -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 -331
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +131 -71
- 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 +309 -208
- package/dist/data-structures/binary-tree/binary-tree.js +382 -300
- package/dist/data-structures/binary-tree/bst.d.ts +245 -127
- package/dist/data-structures/binary-tree/bst.js +366 -163
- 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} +181 -108
- 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 +26 -9
- package/dist/data-structures/heap/heap.js +37 -17
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +64 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +92 -31
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +48 -12
- package/dist/data-structures/linked-list/singly-linked-list.js +74 -27
- 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 +111 -10
- package/dist/data-structures/trie/trie.js +123 -18
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- 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 +1 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- 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/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +25 -0
- 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 +152 -373
- package/src/data-structures/binary-tree/avl-tree.ts +164 -106
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +563 -447
- package/src/data-structures/binary-tree/bst.ts +433 -237
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +224 -146
- 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 +72 -49
- package/src/data-structures/linked-list/doubly-linked-list.ts +186 -118
- package/src/data-structures/linked-list/singly-linked-list.ts +81 -28
- 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 +123 -17
- package/src/index.ts +4 -3
- 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 +1 -6
- package/src/types/data-structures/binary-tree/bst.ts +8 -7
- 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/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -205
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
|
@@ -4,46 +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;
|
|
43
|
+
/**
|
|
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.
|
|
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']
|
|
95
|
+
*/
|
|
37
96
|
class RedBlackTree extends bst_1.BST {
|
|
38
97
|
/**
|
|
39
|
-
* This
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
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
|
|
47
107
|
*/
|
|
48
108
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
49
109
|
super([], options);
|
|
@@ -52,14 +112,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
52
112
|
this.addMany(keysNodesEntriesOrRaws);
|
|
53
113
|
}
|
|
54
114
|
}
|
|
55
|
-
/**
|
|
56
|
-
* The function returns the root node of a tree or undefined if there is no root.
|
|
57
|
-
* @returns The root node of the tree structure, or undefined if there is no root node.
|
|
58
|
-
*/
|
|
59
115
|
get root() {
|
|
60
116
|
return this._root;
|
|
61
117
|
}
|
|
62
118
|
/**
|
|
119
|
+
* Time Complexity: O(1)
|
|
120
|
+
* Space Complexity: O(1)
|
|
121
|
+
*
|
|
63
122
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
64
123
|
* @param {K} key - The key parameter represents the key value of the node being created. It is of
|
|
65
124
|
* type K, which is a generic type that can be replaced with any specific type when using the
|
|
@@ -77,62 +136,30 @@ class RedBlackTree extends bst_1.BST {
|
|
|
77
136
|
return new RedBlackTreeNode(key, this._isMapMode ? undefined : value, color);
|
|
78
137
|
}
|
|
79
138
|
/**
|
|
139
|
+
* Time Complexity: O(1)
|
|
140
|
+
* Space Complexity: O(1)
|
|
141
|
+
*
|
|
80
142
|
* The function creates a new Red-Black Tree with the specified options.
|
|
81
143
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
82
144
|
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
83
145
|
* @returns a new instance of a RedBlackTree object.
|
|
84
146
|
*/
|
|
85
147
|
createTree(options) {
|
|
86
|
-
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));
|
|
87
149
|
}
|
|
88
150
|
/**
|
|
89
151
|
* Time Complexity: O(1)
|
|
90
152
|
* Space Complexity: O(1)
|
|
91
153
|
*
|
|
92
154
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
93
|
-
* @param {BTNRep<K, V,
|
|
94
|
-
* `
|
|
95
|
-
* @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
|
|
96
158
|
* an instance of the `RedBlackTreeNode` class.
|
|
97
159
|
*/
|
|
98
|
-
isNode(
|
|
99
|
-
return
|
|
160
|
+
isNode(keyNodeOrEntry) {
|
|
161
|
+
return keyNodeOrEntry instanceof RedBlackTreeNode;
|
|
100
162
|
}
|
|
101
|
-
// /**
|
|
102
|
-
// * Time Complexity: O(1)
|
|
103
|
-
// * Space Complexity: O(1)
|
|
104
|
-
// */
|
|
105
|
-
//
|
|
106
|
-
// /**
|
|
107
|
-
// * Time Complexity: O(1)
|
|
108
|
-
// * Space Complexity: O(1)
|
|
109
|
-
// *
|
|
110
|
-
// * The function `keyValueNodeEntryRawToNodeAndValue` takes a key, value, or entry and returns a node if it is
|
|
111
|
-
// * valid, otherwise it returns undefined.
|
|
112
|
-
// * @param {BTNRep<K, V, NODE>} keyNodeEntryOrRaw - The key, value, or entry to convert.
|
|
113
|
-
// * @param {V} [value] - The value associated with the key (if `keyNodeEntryOrRaw` is a key).
|
|
114
|
-
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
115
|
-
// */
|
|
116
|
-
// override keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): NODE | undefined {
|
|
117
|
-
//
|
|
118
|
-
// if (keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === undefined) return;
|
|
119
|
-
// if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw;
|
|
120
|
-
//
|
|
121
|
-
// if (this._toEntryFn) {
|
|
122
|
-
// const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
123
|
-
// if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'RED');
|
|
124
|
-
// }
|
|
125
|
-
//
|
|
126
|
-
// if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
127
|
-
// const [key, value] = keyNodeEntryOrRaw;
|
|
128
|
-
// if (key === undefined || key === null) return;
|
|
129
|
-
// else return this.createNode(key, value, 'RED');
|
|
130
|
-
// }
|
|
131
|
-
//
|
|
132
|
-
// if (this.isKey(keyNodeEntryOrRaw)) return this.createNode(keyNodeEntryOrRaw, value, 'RED');
|
|
133
|
-
//
|
|
134
|
-
// return ;
|
|
135
|
-
// }
|
|
136
163
|
/**
|
|
137
164
|
* Time Complexity: O(1)
|
|
138
165
|
* Space Complexity: O(1)
|
|
@@ -146,12 +173,12 @@ class RedBlackTree extends bst_1.BST {
|
|
|
146
173
|
}
|
|
147
174
|
/**
|
|
148
175
|
* Time Complexity: O(log n)
|
|
149
|
-
* Space Complexity: O(
|
|
176
|
+
* Space Complexity: O(log n)
|
|
150
177
|
*
|
|
151
178
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
152
179
|
* added.
|
|
153
|
-
* @param {BTNRep<K, V,
|
|
154
|
-
* `
|
|
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>>`.
|
|
155
182
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
156
183
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
157
184
|
* structure.
|
|
@@ -159,8 +186,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
159
186
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
160
187
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
161
188
|
*/
|
|
162
|
-
add(
|
|
163
|
-
const [newNode, newValue] = this.
|
|
189
|
+
add(keyNodeOrEntry, value) {
|
|
190
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
164
191
|
if (!this.isRealNode(newNode))
|
|
165
192
|
return false;
|
|
166
193
|
const insertStatus = this._insert(newNode);
|
|
@@ -186,35 +213,37 @@ class RedBlackTree extends bst_1.BST {
|
|
|
186
213
|
}
|
|
187
214
|
/**
|
|
188
215
|
* Time Complexity: O(log n)
|
|
189
|
-
* Space Complexity: O(
|
|
216
|
+
* Space Complexity: O(log n)
|
|
190
217
|
*
|
|
191
218
|
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
192
219
|
* a given predicate and maintain the binary search tree properties.
|
|
193
|
-
* @param {BTNRep<K, V,
|
|
220
|
+
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
194
221
|
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
195
222
|
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
196
223
|
* function that determines which node(s) should be deleted.
|
|
197
|
-
* @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>>`
|
|
198
225
|
* objects. Each object in the array contains information about the deleted node and whether
|
|
199
226
|
* balancing is needed.
|
|
200
227
|
*/
|
|
201
|
-
delete(
|
|
202
|
-
if (
|
|
228
|
+
delete(keyNodeOrEntry) {
|
|
229
|
+
if (keyNodeOrEntry === null)
|
|
203
230
|
return [];
|
|
204
231
|
const results = [];
|
|
205
232
|
let nodeToDelete;
|
|
206
|
-
if (this._isPredicate(
|
|
207
|
-
nodeToDelete = this.getNode(
|
|
233
|
+
if (this._isPredicate(keyNodeOrEntry))
|
|
234
|
+
nodeToDelete = this.getNode(keyNodeOrEntry);
|
|
208
235
|
else
|
|
209
|
-
nodeToDelete = this.isRealNode(
|
|
236
|
+
nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
|
|
210
237
|
if (!nodeToDelete) {
|
|
211
238
|
return results;
|
|
212
239
|
}
|
|
213
240
|
let originalColor = nodeToDelete.color;
|
|
214
241
|
let replacementNode;
|
|
215
242
|
if (!this.isRealNode(nodeToDelete.left)) {
|
|
216
|
-
|
|
217
|
-
|
|
243
|
+
if (nodeToDelete.right !== null) {
|
|
244
|
+
replacementNode = nodeToDelete.right;
|
|
245
|
+
this._transplant(nodeToDelete, nodeToDelete.right);
|
|
246
|
+
}
|
|
218
247
|
}
|
|
219
248
|
else if (!this.isRealNode(nodeToDelete.right)) {
|
|
220
249
|
replacementNode = nodeToDelete.left;
|
|
@@ -224,15 +253,18 @@ class RedBlackTree extends bst_1.BST {
|
|
|
224
253
|
const successor = this.getLeftMost(node => node, nodeToDelete.right);
|
|
225
254
|
if (successor) {
|
|
226
255
|
originalColor = successor.color;
|
|
227
|
-
|
|
256
|
+
if (successor.right !== null)
|
|
257
|
+
replacementNode = successor.right;
|
|
228
258
|
if (successor.parent === nodeToDelete) {
|
|
229
259
|
if (this.isRealNode(replacementNode)) {
|
|
230
260
|
replacementNode.parent = successor;
|
|
231
261
|
}
|
|
232
262
|
}
|
|
233
263
|
else {
|
|
234
|
-
|
|
235
|
-
|
|
264
|
+
if (successor.right !== null) {
|
|
265
|
+
this._transplant(successor, successor.right);
|
|
266
|
+
successor.right = nodeToDelete.right;
|
|
267
|
+
}
|
|
236
268
|
if (this.isRealNode(successor.right)) {
|
|
237
269
|
successor.right.parent = successor;
|
|
238
270
|
}
|
|
@@ -255,13 +287,54 @@ class RedBlackTree extends bst_1.BST {
|
|
|
255
287
|
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
256
288
|
return results;
|
|
257
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
|
+
}
|
|
258
331
|
/**
|
|
259
332
|
* Time Complexity: O(1)
|
|
260
333
|
* Space Complexity: O(1)
|
|
261
334
|
*
|
|
262
335
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
263
336
|
* root.
|
|
264
|
-
* @param {
|
|
337
|
+
* @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined.
|
|
265
338
|
*/
|
|
266
339
|
_setRoot(v) {
|
|
267
340
|
if (v) {
|
|
@@ -274,9 +347,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
274
347
|
* Space Complexity: O(1)
|
|
275
348
|
*
|
|
276
349
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
277
|
-
* @param {
|
|
350
|
+
* @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
278
351
|
* the data structure.
|
|
279
|
-
* @param {
|
|
352
|
+
* @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a
|
|
280
353
|
* data structure.
|
|
281
354
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
282
355
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
@@ -287,11 +360,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
287
360
|
}
|
|
288
361
|
/**
|
|
289
362
|
* Time Complexity: O(log n)
|
|
290
|
-
* Space Complexity: O(
|
|
363
|
+
* Space Complexity: O(log n)
|
|
291
364
|
*
|
|
292
365
|
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
|
|
293
366
|
* maintain the red-black tree properties.
|
|
294
|
-
* @param {
|
|
367
|
+
* @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the
|
|
295
368
|
* binary search tree.
|
|
296
369
|
* @returns a string value indicating the result of the insertion operation. It can return either
|
|
297
370
|
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
|
|
@@ -303,7 +376,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
303
376
|
let parent = undefined;
|
|
304
377
|
while (this.isRealNode(current)) {
|
|
305
378
|
parent = current;
|
|
306
|
-
const compared = this.
|
|
379
|
+
const compared = this._compare(node.key, current.key);
|
|
307
380
|
if (compared < 0) {
|
|
308
381
|
current = (_a = current.left) !== null && _a !== void 0 ? _a : this.NIL;
|
|
309
382
|
}
|
|
@@ -319,7 +392,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
319
392
|
if (!parent) {
|
|
320
393
|
this._setRoot(node);
|
|
321
394
|
}
|
|
322
|
-
else if (node.key
|
|
395
|
+
else if (this._compare(node.key, parent.key) < 0) {
|
|
323
396
|
parent.left = node;
|
|
324
397
|
}
|
|
325
398
|
else {
|
|
@@ -336,9 +409,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
336
409
|
* Space Complexity: O(1)
|
|
337
410
|
*
|
|
338
411
|
* The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
|
|
339
|
-
* @param {
|
|
340
|
-
* @param {
|
|
341
|
-
* 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`.
|
|
342
415
|
*/
|
|
343
416
|
_transplant(u, v) {
|
|
344
417
|
if (!u.parent) {
|
|
@@ -359,11 +432,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
359
432
|
* Space Complexity: O(1)
|
|
360
433
|
*
|
|
361
434
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
362
|
-
* @param {
|
|
435
|
+
* @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
|
|
363
436
|
* structure. It can either be a valid node or `undefined`.
|
|
364
437
|
*/
|
|
365
438
|
_insertFixup(z) {
|
|
366
|
-
var _a, _b, _c, _d;
|
|
439
|
+
var _a, _b, _c, _d, _e;
|
|
367
440
|
// Continue fixing the tree as long as the parent of z is red
|
|
368
441
|
while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
|
|
369
442
|
// Check if the parent of z is the left child of its parent
|
|
@@ -397,7 +470,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
397
470
|
else {
|
|
398
471
|
// Symmetric case for the right child (left and right exchanged)
|
|
399
472
|
// Follow the same logic as above with left and right exchanged
|
|
400
|
-
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;
|
|
401
474
|
if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
|
|
402
475
|
z.parent.color = 'BLACK';
|
|
403
476
|
y.color = 'BLACK';
|
|
@@ -427,7 +500,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
427
500
|
*
|
|
428
501
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
429
502
|
* the colors and performing rotations.
|
|
430
|
-
* @param {
|
|
503
|
+
* @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can
|
|
431
504
|
* be either a valid node object or `undefined`.
|
|
432
505
|
* @returns The function does not return any value. It has a return type of `void`, which means it
|
|
433
506
|
* does not return anything.
|
|
@@ -513,7 +586,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
513
586
|
* Space Complexity: O(1)
|
|
514
587
|
*
|
|
515
588
|
* The `_leftRotate` function performs a left rotation on a given node in a binary tree.
|
|
516
|
-
* @param {
|
|
589
|
+
* @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
|
|
517
590
|
* node in a binary tree or `undefined` if there is no node.
|
|
518
591
|
* @returns void, which means it does not return any value.
|
|
519
592
|
*/
|
|
@@ -544,7 +617,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
544
617
|
* Space Complexity: O(1)
|
|
545
618
|
*
|
|
546
619
|
* The `_rightRotate` function performs a right rotation on a given node in a binary tree.
|
|
547
|
-
* @param {
|
|
620
|
+
* @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
|
|
548
621
|
* node in a binary tree or `undefined` if there is no node.
|
|
549
622
|
* @returns void, which means it does not return any value.
|
|
550
623
|
*/
|