binary-tree-typed 1.53.7 → 1.54.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/common/index.js +5 -0
  2. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +63 -53
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -14
  6. package/dist/data-structures/binary-tree/avl-tree.js +22 -25
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +33 -17
  8. package/dist/data-structures/binary-tree/binary-tree.js +45 -26
  9. package/dist/data-structures/binary-tree/bst.d.ts +67 -46
  10. package/dist/data-structures/binary-tree/bst.js +87 -54
  11. package/dist/data-structures/binary-tree/index.d.ts +1 -1
  12. package/dist/data-structures/binary-tree/index.js +1 -1
  13. package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +75 -16
  14. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +93 -60
  15. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +20 -16
  16. package/dist/data-structures/binary-tree/tree-multi-map.js +41 -28
  17. package/dist/data-structures/graph/abstract-graph.js +2 -2
  18. package/dist/data-structures/hash/hash-map.d.ts +31 -1
  19. package/dist/data-structures/hash/hash-map.js +35 -5
  20. package/dist/data-structures/heap/heap.d.ts +20 -3
  21. package/dist/data-structures/heap/heap.js +31 -11
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
  24. package/dist/data-structures/linked-list/singly-linked-list.d.ts +44 -11
  25. package/dist/data-structures/linked-list/singly-linked-list.js +70 -26
  26. package/dist/data-structures/queue/deque.d.ts +37 -8
  27. package/dist/data-structures/queue/deque.js +73 -29
  28. package/dist/data-structures/queue/queue.d.ts +41 -1
  29. package/dist/data-structures/queue/queue.js +51 -9
  30. package/dist/data-structures/stack/stack.d.ts +27 -10
  31. package/dist/data-structures/stack/stack.js +39 -20
  32. package/dist/data-structures/trie/trie.d.ts +8 -3
  33. package/dist/data-structures/trie/trie.js +8 -3
  34. package/dist/interfaces/binary-tree.d.ts +1 -1
  35. package/dist/types/data-structures/base/base.d.ts +1 -1
  36. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  37. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
  38. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  39. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
  40. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  41. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
  42. package/package.json +2 -2
  43. package/src/common/index.ts +7 -1
  44. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  45. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +84 -65
  46. package/src/data-structures/binary-tree/avl-tree.ts +40 -34
  47. package/src/data-structures/binary-tree/binary-tree.ts +76 -32
  48. package/src/data-structures/binary-tree/bst.ts +121 -68
  49. package/src/data-structures/binary-tree/index.ts +1 -1
  50. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +116 -71
  51. package/src/data-structures/binary-tree/tree-multi-map.ts +59 -33
  52. package/src/data-structures/graph/abstract-graph.ts +2 -2
  53. package/src/data-structures/hash/hash-map.ts +37 -7
  54. package/src/data-structures/heap/heap.ts +33 -10
  55. package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
  56. package/src/data-structures/linked-list/singly-linked-list.ts +77 -27
  57. package/src/data-structures/queue/deque.ts +72 -28
  58. package/src/data-structures/queue/queue.ts +50 -7
  59. package/src/data-structures/stack/stack.ts +39 -20
  60. package/src/data-structures/trie/trie.ts +8 -3
  61. package/src/interfaces/binary-tree.ts +4 -1
  62. package/src/types/data-structures/base/base.ts +1 -1
  63. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
  64. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  65. package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
  66. package/src/types/data-structures/binary-tree/bst.ts +3 -3
  67. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  68. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -4
@@ -1,4 +1,4 @@
1
- import type { BinaryTreeDeleteResult, BTNRep, CRUD, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
1
+ import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
2
2
  import { BST, BSTNode } from './bst';
3
3
  import { IBinaryTree } from '../../interfaces';
4
4
  export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
@@ -14,34 +14,72 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
14
14
  * Tree Node. It is an optional parameter with a default value of `'BLACK'`.
15
15
  */
16
16
  constructor(key: K, value?: V, color?: RBTNColor);
17
- protected _color: RBTNColor;
18
- /**
19
- * The function returns the color value of a variable.
20
- * @returns The color value stored in the private variable `_color`.
21
- */
22
- get color(): RBTNColor;
23
- /**
24
- * The function sets the color property to the specified value.
25
- * @param {RBTNColor} value - The value parameter is of type RBTNColor.
26
- */
27
- set color(value: RBTNColor);
28
17
  }
29
18
  /**
30
19
  * 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.
31
20
  * 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
21
+ * @example
22
+ * // Find elements in a range
23
+ * const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]);
24
+ * console.log(bst.search(new Range(5, 10))); // [5, 10, 7]
25
+ * console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7]
26
+ * console.log(bst.search(new Range(15, 20))); // [15, 18]
27
+ * @example
28
+ * // using Red-Black Tree as a price-based index for stock data
29
+ * // Define the structure of individual stock records
30
+ * interface StockRecord {
31
+ * price: number; // Stock price (key for indexing)
32
+ * symbol: string; // Stock ticker symbol
33
+ * volume: number; // Trade volume
34
+ * }
35
+ *
36
+ * // Simulate stock market data as it might come from an external feed
37
+ * const marketStockData: StockRecord[] = [
38
+ * { price: 142.5, symbol: 'AAPL', volume: 1000000 },
39
+ * { price: 335.2, symbol: 'MSFT', volume: 800000 },
40
+ * { price: 3285.04, symbol: 'AMZN', volume: 500000 },
41
+ * { price: 267.98, symbol: 'META', volume: 750000 },
42
+ * { price: 234.57, symbol: 'GOOGL', volume: 900000 }
43
+ * ];
44
+ *
45
+ * // Extend the stock record type to include metadata for database usage
46
+ * type StockTableRecord = StockRecord & { lastUpdated: Date };
47
+ *
48
+ * // Create a Red-Black Tree to index stock records by price
49
+ * // Simulates a database index with stock price as the key for quick lookups
50
+ * const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
51
+ * toEntryFn: stockRecord => [
52
+ * stockRecord.price, // Use stock price as the key
53
+ * {
54
+ * ...stockRecord,
55
+ * lastUpdated: new Date() // Add a timestamp for when the record was indexed
56
+ * }
57
+ * ]
58
+ * });
59
+ *
60
+ * // Query the stock with the highest price
61
+ * const highestPricedStock = priceIndex.getRightMost();
62
+ * console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
63
+ *
64
+ * // Query stocks within a specific price range (200 to 400)
65
+ * const stocksInRange = priceIndex.rangeSearch(
66
+ * [200, 400], // Price range
67
+ * node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
68
+ * );
69
+ * console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
32
70
  */
33
- export declare class RedBlackTree<K = any, V = any, R = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
71
+ export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE> = RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTreeNested<K, V, R, MK, MV, MR, NODE>>> extends BST<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
34
72
  /**
35
73
  * This is the constructor function for a Red-Black Tree data structure in TypeScript.
36
74
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
37
75
  * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
38
76
  * initialize the RBTree with the provided elements.
39
77
  * @param [options] - The `options` parameter is an optional object that can be passed to the
40
- * constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
78
+ * constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
41
79
  * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
42
80
  * depend on the implementation
43
81
  */
44
- constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: RBTreeOptions<K, V, R>);
82
+ constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: RedBlackTreeOptions<K, V, R>);
45
83
  protected _root: NODE | undefined;
46
84
  /**
47
85
  * The function returns the root node of a tree or undefined if there is no root.
@@ -69,7 +107,7 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
69
107
  * configuration options for creating the Red-Black Tree. It has the following properties:
70
108
  * @returns a new instance of a RedBlackTree object.
71
109
  */
72
- createTree(options?: RBTreeOptions<K, V, R>): TREE;
110
+ createTree(options?: RedBlackTreeOptions<K, V, R>): TREE;
73
111
  /**
74
112
  * Time Complexity: O(1)
75
113
  * Space Complexity: O(1)
@@ -206,4 +244,25 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
206
244
  * @returns void, which means it does not return any value.
207
245
  */
208
246
  protected _rightRotate(y: NODE | undefined): void;
247
+ /**
248
+ * Time Complexity: O(n)
249
+ * Space Complexity: O(n)
250
+ *
251
+ * The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
252
+ * applying a callback to each entry in the original tree.
253
+ * @param callback - A function that will be called for each entry in the tree, with parameters
254
+ * representing the key, value, index, and the tree itself. It should return an entry for the new
255
+ * tree.
256
+ * @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
257
+ * MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
258
+ * Tree that will be created during the mapping process. These options could include things like
259
+ * custom comparators
260
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
261
+ * the value of `this` when executing the `callback` function. It allows you to set the context
262
+ * (value of `this`) for the callback function. This can be useful when you want to access properties
263
+ * or
264
+ * @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
265
+ * provided callback function.
266
+ */
267
+ map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: RedBlackTreeOptions<MK, MV, MR>, thisArg?: any): RedBlackTree<MK, MV, MR>;
209
268
  }
@@ -18,25 +18,60 @@ class RedBlackTreeNode extends bst_1.BSTNode {
18
18
  super(key, value);
19
19
  this._color = color;
20
20
  }
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;
27
- }
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;
34
- }
35
21
  }
36
22
  exports.RedBlackTreeNode = RedBlackTreeNode;
37
23
  /**
38
24
  * 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
25
  * 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
26
+ * @example
27
+ * // Find elements in a range
28
+ * const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]);
29
+ * console.log(bst.search(new Range(5, 10))); // [5, 10, 7]
30
+ * console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7]
31
+ * console.log(bst.search(new Range(15, 20))); // [15, 18]
32
+ * @example
33
+ * // using Red-Black Tree as a price-based index for stock data
34
+ * // Define the structure of individual stock records
35
+ * interface StockRecord {
36
+ * price: number; // Stock price (key for indexing)
37
+ * symbol: string; // Stock ticker symbol
38
+ * volume: number; // Trade volume
39
+ * }
40
+ *
41
+ * // Simulate stock market data as it might come from an external feed
42
+ * const marketStockData: StockRecord[] = [
43
+ * { price: 142.5, symbol: 'AAPL', volume: 1000000 },
44
+ * { price: 335.2, symbol: 'MSFT', volume: 800000 },
45
+ * { price: 3285.04, symbol: 'AMZN', volume: 500000 },
46
+ * { price: 267.98, symbol: 'META', volume: 750000 },
47
+ * { price: 234.57, symbol: 'GOOGL', volume: 900000 }
48
+ * ];
49
+ *
50
+ * // Extend the stock record type to include metadata for database usage
51
+ * type StockTableRecord = StockRecord & { lastUpdated: Date };
52
+ *
53
+ * // Create a Red-Black Tree to index stock records by price
54
+ * // Simulates a database index with stock price as the key for quick lookups
55
+ * const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
56
+ * toEntryFn: stockRecord => [
57
+ * stockRecord.price, // Use stock price as the key
58
+ * {
59
+ * ...stockRecord,
60
+ * lastUpdated: new Date() // Add a timestamp for when the record was indexed
61
+ * }
62
+ * ]
63
+ * });
64
+ *
65
+ * // Query the stock with the highest price
66
+ * const highestPricedStock = priceIndex.getRightMost();
67
+ * console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
68
+ *
69
+ * // Query stocks within a specific price range (200 to 400)
70
+ * const stocksInRange = priceIndex.rangeSearch(
71
+ * [200, 400], // Price range
72
+ * node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
73
+ * );
74
+ * console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
40
75
  */
41
76
  class RedBlackTree extends bst_1.BST {
42
77
  /**
@@ -45,7 +80,7 @@ class RedBlackTree extends bst_1.BST {
45
80
  * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
46
81
  * initialize the RBTree with the provided elements.
47
82
  * @param [options] - The `options` parameter is an optional object that can be passed to the
48
- * constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
83
+ * constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
49
84
  * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
50
85
  * depend on the implementation
51
86
  */
@@ -87,7 +122,7 @@ class RedBlackTree extends bst_1.BST {
87
122
  * @returns a new instance of a RedBlackTree object.
88
123
  */
89
124
  createTree(options) {
90
- return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn }, options));
125
+ return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options));
91
126
  }
92
127
  /**
93
128
  * Time Complexity: O(1)
@@ -102,41 +137,6 @@ class RedBlackTree extends bst_1.BST {
102
137
  isNode(keyNodeEntryOrRaw) {
103
138
  return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
104
139
  }
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
140
  /**
141
141
  * Time Complexity: O(1)
142
142
  * Space Complexity: O(1)
@@ -164,7 +164,7 @@ class RedBlackTree extends bst_1.BST {
164
164
  * returns true. If the node cannot be added or updated, the method returns false.
165
165
  */
166
166
  add(keyNodeEntryOrRaw, value) {
167
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
167
+ const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
168
168
  if (!this.isRealNode(newNode))
169
169
  return false;
170
170
  const insertStatus = this._insert(newNode);
@@ -217,8 +217,10 @@ class RedBlackTree extends bst_1.BST {
217
217
  let originalColor = nodeToDelete.color;
218
218
  let replacementNode;
219
219
  if (!this.isRealNode(nodeToDelete.left)) {
220
- replacementNode = nodeToDelete.right;
221
- this._transplant(nodeToDelete, nodeToDelete.right);
220
+ if (nodeToDelete.right !== null) {
221
+ replacementNode = nodeToDelete.right;
222
+ this._transplant(nodeToDelete, nodeToDelete.right);
223
+ }
222
224
  }
223
225
  else if (!this.isRealNode(nodeToDelete.right)) {
224
226
  replacementNode = nodeToDelete.left;
@@ -228,15 +230,18 @@ class RedBlackTree extends bst_1.BST {
228
230
  const successor = this.getLeftMost(node => node, nodeToDelete.right);
229
231
  if (successor) {
230
232
  originalColor = successor.color;
231
- replacementNode = successor.right;
233
+ if (successor.right !== null)
234
+ replacementNode = successor.right;
232
235
  if (successor.parent === nodeToDelete) {
233
236
  if (this.isRealNode(replacementNode)) {
234
237
  replacementNode.parent = successor;
235
238
  }
236
239
  }
237
240
  else {
238
- this._transplant(successor, successor.right);
239
- successor.right = nodeToDelete.right;
241
+ if (successor.right !== null) {
242
+ this._transplant(successor, successor.right);
243
+ successor.right = nodeToDelete.right;
244
+ }
240
245
  if (this.isRealNode(successor.right)) {
241
246
  successor.right.parent = successor;
242
247
  }
@@ -323,7 +328,7 @@ class RedBlackTree extends bst_1.BST {
323
328
  if (!parent) {
324
329
  this._setRoot(node);
325
330
  }
326
- else if (node.key < parent.key) {
331
+ else if (this._compare(node.key, parent.key) < 0) {
327
332
  parent.left = node;
328
333
  }
329
334
  else {
@@ -367,7 +372,7 @@ class RedBlackTree extends bst_1.BST {
367
372
  * structure. It can either be a valid node or `undefined`.
368
373
  */
369
374
  _insertFixup(z) {
370
- var _a, _b, _c, _d;
375
+ var _a, _b, _c, _d, _e;
371
376
  // Continue fixing the tree as long as the parent of z is red
372
377
  while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
373
378
  // Check if the parent of z is the left child of its parent
@@ -401,7 +406,7 @@ class RedBlackTree extends bst_1.BST {
401
406
  else {
402
407
  // Symmetric case for the right child (left and right exchanged)
403
408
  // 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;
409
+ 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
410
  if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
406
411
  z.parent.color = 'BLACK';
407
412
  y.color = 'BLACK';
@@ -574,5 +579,33 @@ class RedBlackTree extends bst_1.BST {
574
579
  x.right = y;
575
580
  y.parent = x;
576
581
  }
582
+ /**
583
+ * Time Complexity: O(n)
584
+ * Space Complexity: O(n)
585
+ *
586
+ * The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
587
+ * applying a callback to each entry in the original tree.
588
+ * @param callback - A function that will be called for each entry in the tree, with parameters
589
+ * representing the key, value, index, and the tree itself. It should return an entry for the new
590
+ * tree.
591
+ * @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
592
+ * MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
593
+ * Tree that will be created during the mapping process. These options could include things like
594
+ * custom comparators
595
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
596
+ * the value of `this` when executing the `callback` function. It allows you to set the context
597
+ * (value of `this`) for the callback function. This can be useful when you want to access properties
598
+ * or
599
+ * @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
600
+ * provided callback function.
601
+ */
602
+ map(callback, options, thisArg) {
603
+ const newTree = new RedBlackTree([], options);
604
+ let index = 0;
605
+ for (const [key, value] of this) {
606
+ newTree.add(callback.call(thisArg, key, value, index++, this));
607
+ }
608
+ return newTree;
609
+ }
577
610
  }
578
611
  exports.RedBlackTree = RedBlackTree;
@@ -5,9 +5,9 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, IterationType, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
8
+ import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
9
9
  import { IBinaryTree } from '../../interfaces';
10
- import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
10
+ import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
11
11
  export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
12
12
  /**
13
13
  * The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
@@ -22,20 +22,8 @@ export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMa
22
22
  * in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
23
23
  */
24
24
  constructor(key: K, value?: V, count?: number, color?: RBTNColor);
25
- protected _count: number;
26
- /**
27
- * The function returns the value of the private variable _count.
28
- * @returns The count property of the object, which is of type number.
29
- */
30
- get count(): number;
31
- /**
32
- * The above function sets the value of the count property.
33
- * @param {number} value - The value parameter is of type number, which means it can accept any
34
- * numeric value.
35
- */
36
- set count(value: number);
37
25
  }
38
- export declare class TreeMultiMap<K = any, V = any, R = object, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>> extends RedBlackTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
26
+ export declare class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = TreeMultiMap<K, V, R, MK, MV, MR, NODE, TreeMultiMapNested<K, V, R, MK, MV, MR, NODE>>> extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
39
27
  /**
40
28
  * The constructor function initializes a TreeMultiMap object with optional initial data.
41
29
  * @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
@@ -96,7 +84,7 @@ export declare class TreeMultiMap<K = any, V = any, R = object, NODE extends Tre
96
84
  * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
97
85
  * @returns either a NODE object or undefined.
98
86
  */
99
- keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
87
+ protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
100
88
  /**
101
89
  * The function checks if the input is an instance of the TreeMultiMapNode class.
102
90
  * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
@@ -195,4 +183,20 @@ export declare class TreeMultiMap<K = any, V = any, R = object, NODE extends Tre
195
183
  * superclass, which is of type `NODE`.
196
184
  */
197
185
  protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
186
+ /**
187
+ * The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
188
+ * modified entries based on a provided callback.
189
+ * @param callback - The `callback` parameter is a function that will be called for each entry in the
190
+ * map. It takes four arguments:
191
+ * @param [options] - The `options` parameter in the `override map` function is of type
192
+ * `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
193
+ * options when creating a new `TreeMultiMap` instance within the `map` function. These options could
194
+ * include things like
195
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
196
+ * the value of `this` when executing the `callback` function. It allows you to set the context
197
+ * (value of `this`) for the callback function when it is called within the `map` function. This
198
+ * @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
199
+ * by the provided callback function.
200
+ */
201
+ map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: TreeMultiMapOptions<MK, MV, MR>, thisArg?: any): TreeMultiMap<MK, MV, MR>;
198
202
  }
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TreeMultiMap = exports.TreeMultiMapNode = void 0;
4
- const rb_tree_1 = require("./rb-tree");
5
- class TreeMultiMapNode extends rb_tree_1.RedBlackTreeNode {
4
+ const red_black_tree_1 = require("./red-black-tree");
5
+ class TreeMultiMapNode extends red_black_tree_1.RedBlackTreeNode {
6
6
  /**
7
7
  * The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
8
8
  * @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
@@ -17,27 +17,11 @@ class TreeMultiMapNode extends rb_tree_1.RedBlackTreeNode {
17
17
  */
18
18
  constructor(key, value, count = 1, color = 'BLACK') {
19
19
  super(key, value, color);
20
- this._count = 1;
21
20
  this.count = count;
22
21
  }
23
- /**
24
- * The function returns the value of the private variable _count.
25
- * @returns The count property of the object, which is of type number.
26
- */
27
- get count() {
28
- return this._count;
29
- }
30
- /**
31
- * The above function sets the value of the count property.
32
- * @param {number} value - The value parameter is of type number, which means it can accept any
33
- * numeric value.
34
- */
35
- set count(value) {
36
- this._count = value;
37
- }
38
22
  }
39
23
  exports.TreeMultiMapNode = TreeMultiMapNode;
40
- class TreeMultiMap extends rb_tree_1.RedBlackTree {
24
+ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
41
25
  /**
42
26
  * The constructor function initializes a TreeMultiMap object with optional initial data.
43
27
  * @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
@@ -99,7 +83,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
99
83
  * existing `iterationType` property. The returned value is casted as `TREE`.
100
84
  */
101
85
  createTree(options) {
102
- return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn }, options));
86
+ return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn }, options));
103
87
  }
104
88
  /**
105
89
  * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
@@ -113,7 +97,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
113
97
  * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
114
98
  * @returns either a NODE object or undefined.
115
99
  */
116
- keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
100
+ _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
117
101
  if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
118
102
  return [undefined, undefined];
119
103
  if (this.isNode(keyNodeEntryOrRaw))
@@ -163,7 +147,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
163
147
  * was successful, and false otherwise.
164
148
  */
165
149
  add(keyNodeEntryOrRaw, value, count = 1) {
166
- const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
150
+ const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
167
151
  const orgCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
168
152
  const isSuccessAdded = super.add(newNode, newValue);
169
153
  if (isSuccessAdded) {
@@ -204,10 +188,13 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
204
188
  let originalColor = nodeToDelete.color;
205
189
  let replacementNode;
206
190
  if (!this.isRealNode(nodeToDelete.left)) {
207
- replacementNode = nodeToDelete.right;
191
+ if (nodeToDelete.right !== null)
192
+ replacementNode = nodeToDelete.right;
208
193
  if (ignoreCount || nodeToDelete.count <= 1) {
209
- this._transplant(nodeToDelete, nodeToDelete.right);
210
- this._count -= nodeToDelete.count;
194
+ if (nodeToDelete.right !== null) {
195
+ this._transplant(nodeToDelete, nodeToDelete.right);
196
+ this._count -= nodeToDelete.count;
197
+ }
211
198
  }
212
199
  else {
213
200
  nodeToDelete.count--;
@@ -233,7 +220,8 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
233
220
  const successor = this.getLeftMost(node => node, nodeToDelete.right);
234
221
  if (successor) {
235
222
  originalColor = successor.color;
236
- replacementNode = successor.right;
223
+ if (successor.right !== null)
224
+ replacementNode = successor.right;
237
225
  if (successor.parent === nodeToDelete) {
238
226
  if (this.isRealNode(replacementNode)) {
239
227
  replacementNode.parent = successor;
@@ -241,8 +229,10 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
241
229
  }
242
230
  else {
243
231
  if (ignoreCount || nodeToDelete.count <= 1) {
244
- this._transplant(successor, successor.right);
245
- this._count -= nodeToDelete.count;
232
+ if (successor.right !== null) {
233
+ this._transplant(successor, successor.right);
234
+ this._count -= nodeToDelete.count;
235
+ }
246
236
  }
247
237
  else {
248
238
  nodeToDelete.count--;
@@ -412,5 +402,28 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
412
402
  newNode.count = oldNode.count + newNode.count;
413
403
  return super._replaceNode(oldNode, newNode);
414
404
  }
405
+ /**
406
+ * The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
407
+ * modified entries based on a provided callback.
408
+ * @param callback - The `callback` parameter is a function that will be called for each entry in the
409
+ * map. It takes four arguments:
410
+ * @param [options] - The `options` parameter in the `override map` function is of type
411
+ * `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
412
+ * options when creating a new `TreeMultiMap` instance within the `map` function. These options could
413
+ * include things like
414
+ * @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
415
+ * the value of `this` when executing the `callback` function. It allows you to set the context
416
+ * (value of `this`) for the callback function when it is called within the `map` function. This
417
+ * @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
418
+ * by the provided callback function.
419
+ */
420
+ map(callback, options, thisArg) {
421
+ const newTree = new TreeMultiMap([], options);
422
+ let index = 0;
423
+ for (const [key, value] of this) {
424
+ newTree.add(callback.call(thisArg, key, value, index++, this));
425
+ }
426
+ return newTree;
427
+ }
415
428
  }
416
429
  exports.TreeMultiMap = TreeMultiMap;
@@ -813,7 +813,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
813
813
  const filtered = [];
814
814
  let index = 0;
815
815
  for (const [key, value] of this) {
816
- if (predicate.call(thisArg, value, key, index, this)) {
816
+ if (predicate.call(thisArg, key, value, index, this)) {
817
817
  filtered.push([key, value]);
818
818
  }
819
819
  index++;
@@ -837,7 +837,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
837
837
  const mapped = [];
838
838
  let index = 0;
839
839
  for (const [key, value] of this) {
840
- mapped.push(callback.call(thisArg, value, key, index, this));
840
+ mapped.push(callback.call(thisArg, key, value, index, this));
841
841
  index++;
842
842
  }
843
843
  return mapped;