queue-typed 1.53.8 → 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 (48) hide show
  1. package/dist/data-structures/base/iterable-entry-base.js +4 -4
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +62 -52
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -14
  5. package/dist/data-structures/binary-tree/avl-tree.js +22 -25
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +32 -16
  7. package/dist/data-structures/binary-tree/binary-tree.js +43 -24
  8. package/dist/data-structures/binary-tree/bst.d.ts +14 -23
  9. package/dist/data-structures/binary-tree/bst.js +28 -29
  10. package/dist/data-structures/binary-tree/red-black-tree.d.ts +26 -16
  11. package/dist/data-structures/binary-tree/red-black-tree.js +43 -59
  12. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +18 -14
  13. package/dist/data-structures/binary-tree/tree-multi-map.js +36 -23
  14. package/dist/data-structures/graph/abstract-graph.js +2 -2
  15. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  16. package/dist/data-structures/hash/hash-map.js +5 -5
  17. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +10 -10
  18. package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
  19. package/dist/data-structures/linked-list/singly-linked-list.d.ts +10 -10
  20. package/dist/data-structures/linked-list/singly-linked-list.js +16 -16
  21. package/dist/interfaces/binary-tree.d.ts +1 -1
  22. package/dist/types/data-structures/base/base.d.ts +1 -1
  23. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  24. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
  25. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  26. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
  27. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  28. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
  29. package/package.json +2 -2
  30. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  31. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +83 -64
  32. package/src/data-structures/binary-tree/avl-tree.ts +40 -34
  33. package/src/data-structures/binary-tree/binary-tree.ts +74 -30
  34. package/src/data-structures/binary-tree/bst.ts +57 -43
  35. package/src/data-structures/binary-tree/red-black-tree.ts +66 -70
  36. package/src/data-structures/binary-tree/tree-multi-map.ts +56 -30
  37. package/src/data-structures/graph/abstract-graph.ts +2 -2
  38. package/src/data-structures/hash/hash-map.ts +7 -7
  39. package/src/data-structures/linked-list/doubly-linked-list.ts +13 -13
  40. package/src/data-structures/linked-list/singly-linked-list.ts +17 -17
  41. package/src/interfaces/binary-tree.ts +4 -1
  42. package/src/types/data-structures/base/base.ts +1 -1
  43. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
  44. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  45. package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
  46. package/src/types/data-structures/binary-tree/bst.ts +3 -3
  47. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  48. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -4
@@ -5,38 +5,38 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types';
8
+ import { BSTNested, BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode, OptNodeOrNull } from '../../types';
9
9
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { Range } from '../../common';
12
12
  export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
13
13
  parent?: NODE;
14
14
  constructor(key: K, value?: V);
15
- protected _left?: NODE;
15
+ _left?: OptNodeOrNull<NODE>;
16
16
  /**
17
17
  * The function returns the value of the `_left` property.
18
18
  * @returns The `_left` property of the current object is being returned.
19
19
  */
20
- get left(): OptNode<NODE>;
20
+ get left(): OptNodeOrNull<NODE>;
21
21
  /**
22
22
  * The function sets the left child of a node and updates the parent reference of the child.
23
23
  * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
24
24
  * instance of the `NODE` class or `undefined`.
25
25
  */
26
- set left(v: OptNode<NODE>);
27
- protected _right?: NODE;
26
+ set left(v: OptNodeOrNull<NODE>);
27
+ _right?: OptNodeOrNull<NODE>;
28
28
  /**
29
29
  * The function returns the right node of a binary tree or undefined if there is no right node.
30
30
  * @returns The method is returning the value of the `_right` property, which is of type `NODE` or
31
31
  * `undefined`.
32
32
  */
33
- get right(): OptNode<NODE>;
33
+ get right(): OptNodeOrNull<NODE>;
34
34
  /**
35
35
  * The function sets the right child of a node and updates the parent reference of the child.
36
36
  * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
37
37
  * `NODE` object or `undefined`.
38
38
  */
39
- set right(v: OptNode<NODE>);
39
+ set right(v: OptNodeOrNull<NODE>);
40
40
  }
41
41
  /**
42
42
  * 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
@@ -103,7 +103,7 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
103
103
  * console.log(findLCA(5, 35)); // 15
104
104
  * console.log(findLCA(20, 30)); // 25
105
105
  */
106
- export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>> extends BinaryTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
106
+ export declare class BST<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, R, MK, MV, MR, NODE, TREE> = BST<K, V, R, MK, MV, MR, NODE, BSTNested<K, V, R, MK, MV, MR, NODE>>> extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
107
107
  /**
108
108
  * This is the constructor function for a Binary Search Tree class in TypeScript.
109
109
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
@@ -220,16 +220,6 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
220
220
  * successfully inserted into the data structure.
221
221
  */
222
222
  addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
223
- /**
224
- * Time Complexity: O(n)
225
- * Space Complexity: O(1)
226
- *
227
- * The `merge` function overrides the base class method by adding elements from another
228
- * binary search tree.
229
- * @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
230
- * value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
231
- */
232
- merge(anotherTree: BST<K, V, R, NODE, TREE>): void;
233
223
  /**
234
224
  * Time Complexity: O(log n)
235
225
  * Space Complexity: O(k + log n)
@@ -414,13 +404,13 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
414
404
  * @returns The `_comparator` property is being returned.
415
405
  */
416
406
  get comparator(): Comparator<K>;
417
- protected _extractComparable?: (key: K) => Comparable;
407
+ protected _specifyComparable?: (key: K) => Comparable;
418
408
  /**
419
- * This function returns the value of the `_extractComparable` property.
420
- * @returns The method `extractComparable()` is being returned, which is a getter method for the
421
- * `_extractComparable` property.
409
+ * This function returns the value of the `_specifyComparable` property.
410
+ * @returns The method `specifyComparable()` is being returned, which is a getter method for the
411
+ * `_specifyComparable` property.
422
412
  */
423
- get extractComparable(): ((key: K) => Comparable) | undefined;
413
+ get specifyComparable(): ((key: K) => Comparable) | undefined;
424
414
  /**
425
415
  * The function sets the root of a tree-like structure and updates the parent property of the new
426
416
  * root.
@@ -428,4 +418,5 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
428
418
  */
429
419
  protected _setRoot(v: OptNode<NODE>): void;
430
420
  protected _compare(a: K, b: K): number;
421
+ map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BSTOptions<MK, MV, MR>, thisArg?: any): BST<MK, MV, MR>;
431
422
  }
@@ -137,22 +137,22 @@ class BST extends binary_tree_1.BinaryTree {
137
137
  return -1;
138
138
  return 0;
139
139
  }
140
- if (this._extractComparable) {
141
- if (this._extractComparable(a) > this._extractComparable(b))
140
+ if (this._specifyComparable) {
141
+ if (this._specifyComparable(a) > this._specifyComparable(b))
142
142
  return 1;
143
- if (this._extractComparable(a) < this._extractComparable(b))
143
+ if (this._specifyComparable(a) < this._specifyComparable(b))
144
144
  return -1;
145
145
  return 0;
146
146
  }
147
147
  if (typeof a === 'object' || typeof b === 'object') {
148
- throw TypeError(`When comparing object types, a custom extractComparable must be defined in the constructor's options parameter.`);
148
+ throw TypeError(`When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`);
149
149
  }
150
150
  return 0;
151
151
  };
152
152
  if (options) {
153
- const { extractComparable, isReverse } = options;
154
- if (typeof extractComparable === 'function')
155
- this._extractComparable = extractComparable;
153
+ const { specifyComparable, isReverse } = options;
154
+ if (typeof specifyComparable === 'function')
155
+ this._specifyComparable = specifyComparable;
156
156
  if (isReverse !== undefined)
157
157
  this._isReverse = isReverse;
158
158
  }
@@ -193,7 +193,7 @@ class BST extends binary_tree_1.BinaryTree {
193
193
  * @returns a new instance of the BST class with the provided options.
194
194
  */
195
195
  createTree(options) {
196
- return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
196
+ return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
197
197
  }
198
198
  /**
199
199
  * The function overrides a method and converts a key, value pair or entry or raw element to a node.
@@ -248,7 +248,7 @@ class BST extends binary_tree_1.BinaryTree {
248
248
  * this._DEFAULT_COMPARATOR`.
249
249
  */
250
250
  isKey(key) {
251
- return (0, utils_1.isComparable)(key, this._extractComparable !== undefined);
251
+ return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined);
252
252
  }
253
253
  /**
254
254
  * Time Complexity: O(log n)
@@ -288,7 +288,8 @@ class BST extends binary_tree_1.BinaryTree {
288
288
  this._size++;
289
289
  return true;
290
290
  }
291
- current = current.left;
291
+ if (current.left !== null)
292
+ current = current.left;
292
293
  }
293
294
  else {
294
295
  if (current.right === undefined) {
@@ -298,7 +299,8 @@ class BST extends binary_tree_1.BinaryTree {
298
299
  this._size++;
299
300
  return true;
300
301
  }
301
- current = current.right;
302
+ if (current.right !== null)
303
+ current = current.right;
302
304
  }
303
305
  }
304
306
  return false;
@@ -405,18 +407,6 @@ class BST extends binary_tree_1.BinaryTree {
405
407
  }
406
408
  return inserted;
407
409
  }
408
- /**
409
- * Time Complexity: O(n)
410
- * Space Complexity: O(1)
411
- *
412
- * The `merge` function overrides the base class method by adding elements from another
413
- * binary search tree.
414
- * @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
415
- * value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
416
- */
417
- merge(anotherTree) {
418
- this.addMany(anotherTree, [], false);
419
- }
420
410
  /**
421
411
  * Time Complexity: O(log n)
422
412
  * Space Complexity: O(k + log n)
@@ -817,7 +807,8 @@ class BST extends binary_tree_1.BinaryTree {
817
807
  while (stack.length > 0 || node) {
818
808
  if (node) {
819
809
  stack.push(node);
820
- node = node.left;
810
+ if (node.left !== null)
811
+ node = node.left;
821
812
  }
822
813
  else {
823
814
  node = stack[stack.length - 1];
@@ -848,12 +839,12 @@ class BST extends binary_tree_1.BinaryTree {
848
839
  return this._comparator;
849
840
  }
850
841
  /**
851
- * This function returns the value of the `_extractComparable` property.
852
- * @returns The method `extractComparable()` is being returned, which is a getter method for the
853
- * `_extractComparable` property.
842
+ * This function returns the value of the `_specifyComparable` property.
843
+ * @returns The method `specifyComparable()` is being returned, which is a getter method for the
844
+ * `_specifyComparable` property.
854
845
  */
855
- get extractComparable() {
856
- return this._extractComparable;
846
+ get specifyComparable() {
847
+ return this._specifyComparable;
857
848
  }
858
849
  /**
859
850
  * The function sets the root of a tree-like structure and updates the parent property of the new
@@ -869,5 +860,13 @@ class BST extends binary_tree_1.BinaryTree {
869
860
  _compare(a, b) {
870
861
  return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
871
862
  }
863
+ map(callback, options, thisArg) {
864
+ const newTree = new BST([], options);
865
+ let index = 0;
866
+ for (const [key, value] of this) {
867
+ newTree.add(callback.call(thisArg, key, value, index++, this));
868
+ }
869
+ return newTree;
870
+ }
872
871
  }
873
872
  exports.BST = BST;
@@ -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,17 +14,6 @@ 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.
@@ -79,18 +68,18 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
79
68
  * );
80
69
  * console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
81
70
  */
82
- 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> {
83
72
  /**
84
73
  * This is the constructor function for a Red-Black Tree data structure in TypeScript.
85
74
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
86
75
  * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
87
76
  * initialize the RBTree with the provided elements.
88
77
  * @param [options] - The `options` parameter is an optional object that can be passed to the
89
- * 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
90
79
  * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
91
80
  * depend on the implementation
92
81
  */
93
- 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>);
94
83
  protected _root: NODE | undefined;
95
84
  /**
96
85
  * The function returns the root node of a tree or undefined if there is no root.
@@ -118,7 +107,7 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
118
107
  * configuration options for creating the Red-Black Tree. It has the following properties:
119
108
  * @returns a new instance of a RedBlackTree object.
120
109
  */
121
- createTree(options?: RBTreeOptions<K, V, R>): TREE;
110
+ createTree(options?: RedBlackTreeOptions<K, V, R>): TREE;
122
111
  /**
123
112
  * Time Complexity: O(1)
124
113
  * Space Complexity: O(1)
@@ -255,4 +244,25 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
255
244
  * @returns void, which means it does not return any value.
256
245
  */
257
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>;
258
268
  }
@@ -18,20 +18,6 @@ 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
  /**
@@ -94,7 +80,7 @@ class RedBlackTree extends bst_1.BST {
94
80
  * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
95
81
  * initialize the RBTree with the provided elements.
96
82
  * @param [options] - The `options` parameter is an optional object that can be passed to the
97
- * 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
98
84
  * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
99
85
  * depend on the implementation
100
86
  */
@@ -136,7 +122,7 @@ class RedBlackTree extends bst_1.BST {
136
122
  * @returns a new instance of a RedBlackTree object.
137
123
  */
138
124
  createTree(options) {
139
- 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));
140
126
  }
141
127
  /**
142
128
  * Time Complexity: O(1)
@@ -151,41 +137,6 @@ class RedBlackTree extends bst_1.BST {
151
137
  isNode(keyNodeEntryOrRaw) {
152
138
  return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
153
139
  }
154
- // /**
155
- // * Time Complexity: O(1)
156
- // * Space Complexity: O(1)
157
- // */
158
- //
159
- // /**
160
- // * Time Complexity: O(1)
161
- // * Space Complexity: O(1)
162
- // *
163
- // * The function `keyValueNodeEntryRawToNodeAndValue` takes a key, value, or entry and returns a node if it is
164
- // * valid, otherwise it returns undefined.
165
- // * @param {BTNRep<K, V, NODE>} keyNodeEntryOrRaw - The key, value, or entry to convert.
166
- // * @param {V} [value] - The value associated with the key (if `keyNodeEntryOrRaw` is a key).
167
- // * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
168
- // */
169
- // override keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): NODE | undefined {
170
- //
171
- // if (keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === undefined) return;
172
- // if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw;
173
- //
174
- // if (this._toEntryFn) {
175
- // const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
176
- // if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'RED');
177
- // }
178
- //
179
- // if (this.isEntry(keyNodeEntryOrRaw)) {
180
- // const [key, value] = keyNodeEntryOrRaw;
181
- // if (key === undefined || key === null) return;
182
- // else return this.createNode(key, value, 'RED');
183
- // }
184
- //
185
- // if (this.isKey(keyNodeEntryOrRaw)) return this.createNode(keyNodeEntryOrRaw, value, 'RED');
186
- //
187
- // return ;
188
- // }
189
140
  /**
190
141
  * Time Complexity: O(1)
191
142
  * Space Complexity: O(1)
@@ -266,8 +217,10 @@ class RedBlackTree extends bst_1.BST {
266
217
  let originalColor = nodeToDelete.color;
267
218
  let replacementNode;
268
219
  if (!this.isRealNode(nodeToDelete.left)) {
269
- replacementNode = nodeToDelete.right;
270
- this._transplant(nodeToDelete, nodeToDelete.right);
220
+ if (nodeToDelete.right !== null) {
221
+ replacementNode = nodeToDelete.right;
222
+ this._transplant(nodeToDelete, nodeToDelete.right);
223
+ }
271
224
  }
272
225
  else if (!this.isRealNode(nodeToDelete.right)) {
273
226
  replacementNode = nodeToDelete.left;
@@ -277,15 +230,18 @@ class RedBlackTree extends bst_1.BST {
277
230
  const successor = this.getLeftMost(node => node, nodeToDelete.right);
278
231
  if (successor) {
279
232
  originalColor = successor.color;
280
- replacementNode = successor.right;
233
+ if (successor.right !== null)
234
+ replacementNode = successor.right;
281
235
  if (successor.parent === nodeToDelete) {
282
236
  if (this.isRealNode(replacementNode)) {
283
237
  replacementNode.parent = successor;
284
238
  }
285
239
  }
286
240
  else {
287
- this._transplant(successor, successor.right);
288
- successor.right = nodeToDelete.right;
241
+ if (successor.right !== null) {
242
+ this._transplant(successor, successor.right);
243
+ successor.right = nodeToDelete.right;
244
+ }
289
245
  if (this.isRealNode(successor.right)) {
290
246
  successor.right.parent = successor;
291
247
  }
@@ -372,7 +328,7 @@ class RedBlackTree extends bst_1.BST {
372
328
  if (!parent) {
373
329
  this._setRoot(node);
374
330
  }
375
- else if (node.key < parent.key) {
331
+ else if (this._compare(node.key, parent.key) < 0) {
376
332
  parent.left = node;
377
333
  }
378
334
  else {
@@ -416,7 +372,7 @@ class RedBlackTree extends bst_1.BST {
416
372
  * structure. It can either be a valid node or `undefined`.
417
373
  */
418
374
  _insertFixup(z) {
419
- var _a, _b, _c, _d;
375
+ var _a, _b, _c, _d, _e;
420
376
  // Continue fixing the tree as long as the parent of z is red
421
377
  while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
422
378
  // Check if the parent of z is the left child of its parent
@@ -450,7 +406,7 @@ class RedBlackTree extends bst_1.BST {
450
406
  else {
451
407
  // Symmetric case for the right child (left and right exchanged)
452
408
  // Follow the same logic as above with left and right exchanged
453
- 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;
454
410
  if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
455
411
  z.parent.color = 'BLACK';
456
412
  y.color = 'BLACK';
@@ -623,5 +579,33 @@ class RedBlackTree extends bst_1.BST {
623
579
  x.right = y;
624
580
  y.parent = x;
625
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
+ }
626
610
  }
627
611
  exports.RedBlackTree = RedBlackTree;
@@ -5,7 +5,7 @@
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
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> {
@@ -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
@@ -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
  }
@@ -17,24 +17,8 @@ class TreeMultiMapNode extends red_black_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
24
  class TreeMultiMap extends red_black_tree_1.RedBlackTree {
@@ -99,7 +83,7 @@ class TreeMultiMap extends red_black_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
@@ -204,10 +188,13 @@ class TreeMultiMap extends red_black_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 red_black_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 red_black_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 red_black_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;