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.
Files changed (121) hide show
  1. package/README.md +52 -0
  2. package/dist/common/index.d.ts +12 -0
  3. package/dist/common/index.js +28 -0
  4. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  5. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
  6. package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -331
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
  10. package/dist/data-structures/binary-tree/avl-tree.js +131 -71
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +309 -208
  14. package/dist/data-structures/binary-tree/binary-tree.js +382 -300
  15. package/dist/data-structures/binary-tree/bst.d.ts +245 -127
  16. package/dist/data-structures/binary-tree/bst.js +366 -163
  17. package/dist/data-structures/binary-tree/index.d.ts +3 -1
  18. package/dist/data-structures/binary-tree/index.js +3 -1
  19. package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
  20. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +181 -108
  21. package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
  22. package/dist/data-structures/binary-tree/tree-counter.js +444 -0
  23. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
  24. package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
  25. package/dist/data-structures/graph/abstract-graph.js +2 -2
  26. package/dist/data-structures/graph/directed-graph.d.ts +3 -0
  27. package/dist/data-structures/graph/directed-graph.js +3 -0
  28. package/dist/data-structures/graph/map-graph.d.ts +3 -0
  29. package/dist/data-structures/graph/map-graph.js +3 -0
  30. package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
  31. package/dist/data-structures/graph/undirected-graph.js +3 -0
  32. package/dist/data-structures/hash/hash-map.d.ts +31 -1
  33. package/dist/data-structures/hash/hash-map.js +35 -5
  34. package/dist/data-structures/heap/heap.d.ts +26 -9
  35. package/dist/data-structures/heap/heap.js +37 -17
  36. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +64 -19
  37. package/dist/data-structures/linked-list/doubly-linked-list.js +92 -31
  38. package/dist/data-structures/linked-list/singly-linked-list.d.ts +48 -12
  39. package/dist/data-structures/linked-list/singly-linked-list.js +74 -27
  40. package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
  41. package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
  42. package/dist/data-structures/matrix/matrix.d.ts +3 -0
  43. package/dist/data-structures/matrix/matrix.js +3 -0
  44. package/dist/data-structures/matrix/navigator.d.ts +3 -0
  45. package/dist/data-structures/matrix/navigator.js +3 -0
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
  50. package/dist/data-structures/queue/deque.d.ts +37 -8
  51. package/dist/data-structures/queue/deque.js +73 -29
  52. package/dist/data-structures/queue/queue.d.ts +41 -1
  53. package/dist/data-structures/queue/queue.js +51 -9
  54. package/dist/data-structures/stack/stack.d.ts +27 -10
  55. package/dist/data-structures/stack/stack.js +39 -20
  56. package/dist/data-structures/trie/trie.d.ts +111 -10
  57. package/dist/data-structures/trie/trie.js +123 -18
  58. package/dist/index.d.ts +2 -1
  59. package/dist/index.js +2 -1
  60. package/dist/interfaces/binary-tree.d.ts +8 -8
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  63. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
  64. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
  65. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
  66. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -4
  67. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
  68. package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
  69. package/dist/types/data-structures/binary-tree/index.js +2 -0
  70. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
  71. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  72. package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
  73. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
  74. package/dist/types/utils/utils.d.ts +10 -6
  75. package/dist/utils/utils.js +4 -2
  76. package/package.json +2 -2
  77. package/src/common/index.ts +25 -0
  78. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  79. package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
  80. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +152 -373
  81. package/src/data-structures/binary-tree/avl-tree.ts +164 -106
  82. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
  83. package/src/data-structures/binary-tree/binary-tree.ts +563 -447
  84. package/src/data-structures/binary-tree/bst.ts +433 -237
  85. package/src/data-structures/binary-tree/index.ts +3 -1
  86. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +224 -146
  87. package/src/data-structures/binary-tree/tree-counter.ts +504 -0
  88. package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
  89. package/src/data-structures/graph/abstract-graph.ts +2 -2
  90. package/src/data-structures/graph/directed-graph.ts +3 -0
  91. package/src/data-structures/graph/map-graph.ts +3 -0
  92. package/src/data-structures/graph/undirected-graph.ts +3 -0
  93. package/src/data-structures/hash/hash-map.ts +37 -7
  94. package/src/data-structures/heap/heap.ts +72 -49
  95. package/src/data-structures/linked-list/doubly-linked-list.ts +186 -118
  96. package/src/data-structures/linked-list/singly-linked-list.ts +81 -28
  97. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  98. package/src/data-structures/matrix/matrix.ts +3 -0
  99. package/src/data-structures/matrix/navigator.ts +3 -0
  100. package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
  101. package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
  102. package/src/data-structures/queue/deque.ts +72 -28
  103. package/src/data-structures/queue/queue.ts +50 -7
  104. package/src/data-structures/stack/stack.ts +39 -20
  105. package/src/data-structures/trie/trie.ts +123 -17
  106. package/src/index.ts +4 -3
  107. package/src/interfaces/binary-tree.ts +10 -21
  108. package/src/types/data-structures/base/base.ts +1 -1
  109. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  110. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
  111. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
  112. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -6
  113. package/src/types/data-structures/binary-tree/bst.ts +8 -7
  114. package/src/types/data-structures/binary-tree/index.ts +3 -1
  115. package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
  116. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  117. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
  118. package/src/types/utils/utils.ts +16 -10
  119. package/src/utils/utils.ts +4 -2
  120. package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -205
  121. 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 function initializes a Red-Black Tree Node with a key, an optional value, and a
8
- * color.
9
- * @param {K} key - The key parameter is of type K and represents the key of the node in the
10
- * Red-Black Tree.
11
- * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
12
- * associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
13
- * creating a new instance of the Red-Black Tree Node.
14
- * @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
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
- * The function returns the color value of a variable.
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
- * The function sets the color property to the specified value.
30
- * @param {RBTNColor} value - The value parameter is of type RBTNColor.
31
- */
32
- set color(value) {
33
- this._color = value;
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 is the constructor function for a Red-Black Tree data structure in TypeScript.
40
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
41
- * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
42
- * initialize the RBTree with the provided elements.
43
- * @param [options] - The `options` parameter is an optional object that can be passed to the
44
- * constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
45
- * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
46
- * depend on the implementation
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, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
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, NODE> | R} keyNodeEntryOrRaw - The parameter
94
- * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
95
- * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
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(keyNodeEntryOrRaw) {
99
- return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
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(1)
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, NODE> | R} keyNodeEntryOrRaw - The parameter
154
- * `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
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(keyNodeEntryOrRaw, value) {
163
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
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(1)
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, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
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<NODE>`
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(keyNodeEntryOrRaw) {
202
- if (keyNodeEntryOrRaw === null)
228
+ delete(keyNodeOrEntry) {
229
+ if (keyNodeOrEntry === null)
203
230
  return [];
204
231
  const results = [];
205
232
  let nodeToDelete;
206
- if (this._isPredicate(keyNodeEntryOrRaw))
207
- nodeToDelete = this.getNode(keyNodeEntryOrRaw);
233
+ if (this._isPredicate(keyNodeOrEntry))
234
+ nodeToDelete = this.getNode(keyNodeOrEntry);
208
235
  else
209
- nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
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
- replacementNode = nodeToDelete.right;
217
- this._transplant(nodeToDelete, nodeToDelete.right);
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
- replacementNode = successor.right;
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
- this._transplant(successor, successor.right);
235
- successor.right = nodeToDelete.right;
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 {NODE | undefined} v - v is a parameter of type NODE or undefined.
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 {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
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 {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
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(1)
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 {NODE} node - The `node` parameter represents the node that needs to be inserted into the
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.comparator(node.key, current.key);
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 < parent.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 {NODE} u - The parameter "u" represents a node in a binary tree.
340
- * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can
341
- * either be a `NODE` object or `undefined`.
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 {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
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 {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can
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 {NODE | undefined} x - The parameter `x` is of type `NODE | undefined`. It represents a
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 {NODE | undefined} y - The parameter `y` is of type `NODE | undefined`. It represents a
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
  */