avl-tree-typed 1.52.8 → 1.53.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 (27) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +22 -22
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +64 -47
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +20 -20
  4. package/dist/data-structures/binary-tree/avl-tree.js +28 -26
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +240 -141
  6. package/dist/data-structures/binary-tree/binary-tree.js +395 -269
  7. package/dist/data-structures/binary-tree/bst.d.ts +56 -56
  8. package/dist/data-structures/binary-tree/bst.js +114 -91
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -13
  10. package/dist/data-structures/binary-tree/rb-tree.js +35 -31
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +59 -48
  13. package/dist/data-structures/trie/trie.js +3 -3
  14. package/dist/interfaces/binary-tree.d.ts +5 -5
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
  17. package/package.json +2 -2
  18. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +60 -55
  19. package/src/data-structures/binary-tree/avl-tree.ts +31 -35
  20. package/src/data-structures/binary-tree/binary-tree.ts +461 -385
  21. package/src/data-structures/binary-tree/bst.ts +155 -128
  22. package/src/data-structures/binary-tree/rb-tree.ts +37 -39
  23. package/src/data-structures/binary-tree/tree-multi-map.ts +57 -60
  24. package/src/data-structures/trie/trie.ts +3 -3
  25. package/src/interfaces/binary-tree.ts +6 -6
  26. package/src/types/data-structures/binary-tree/binary-tree.ts +14 -15
  27. package/src/types/data-structures/binary-tree/bst.ts +4 -4
@@ -1,14 +1,12 @@
1
1
  import type {
2
2
  BinaryTreeDeleteResult,
3
- BTNKeyOrNodeOrEntry,
4
- BTNPredicate,
3
+ BTNRep,
5
4
  CRUD,
6
- OptBSTN,
5
+ OptNode,
7
6
  RBTNColor,
8
7
  RBTreeOptions,
9
8
  RedBlackTreeNested,
10
- RedBlackTreeNodeNested,
11
- BTNEntry
9
+ RedBlackTreeNodeNested
12
10
  } from '../../types';
13
11
  import { BST, BSTNode } from './bst';
14
12
  import { IBinaryTree } from '../../interfaces';
@@ -56,7 +54,7 @@ export class RedBlackTreeNode<
56
54
  export class RedBlackTree<
57
55
  K = any,
58
56
  V = any,
59
- R = BTNEntry<K, V>,
57
+ R = object,
60
58
  NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
61
59
  TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
62
60
  >
@@ -65,7 +63,7 @@ export class RedBlackTree<
65
63
  {
66
64
  /**
67
65
  * This is the constructor function for a Red-Black Tree data structure in TypeScript.
68
- * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
66
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
69
67
  * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
70
68
  * initialize the RBTree with the provided elements.
71
69
  * @param [options] - The `options` parameter is an optional object that can be passed to the
@@ -73,16 +71,13 @@ export class RedBlackTree<
73
71
  * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
74
72
  * depend on the implementation
75
73
  */
76
- constructor(
77
- keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
78
- options?: RBTreeOptions<K, V, R>
79
- ) {
74
+ constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: RBTreeOptions<K, V, R>) {
80
75
  super([], options);
81
76
 
82
77
  this._root = this.NIL;
83
78
 
84
- if (keysOrNodesOrEntriesOrRaws) {
85
- this.addMany(keysOrNodesOrEntriesOrRaws);
79
+ if (keysNodesEntriesOrRaws) {
80
+ this.addMany(keysNodesEntriesOrRaws);
86
81
  }
87
82
  }
88
83
 
@@ -123,6 +118,7 @@ export class RedBlackTree<
123
118
  override createTree(options?: RBTreeOptions<K, V, R>): TREE {
124
119
  return new RedBlackTree<K, V, R, NODE, TREE>([], {
125
120
  iterationType: this.iterationType,
121
+ isMapMode: this._isMapMode,
126
122
  comparator: this._comparator,
127
123
  toEntryFn: this._toEntryFn,
128
124
  ...options
@@ -134,13 +130,13 @@ export class RedBlackTree<
134
130
  * Space Complexity: O(1)
135
131
  *
136
132
  * The function checks if the input is an instance of the RedBlackTreeNode class.
137
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
138
- * `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
139
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
133
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
134
+ * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
135
+ * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
140
136
  * an instance of the `RedBlackTreeNode` class.
141
137
  */
142
- override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
143
- return keyOrNodeOrEntryOrRaw instanceof RedBlackTreeNode;
138
+ override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
139
+ return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
144
140
  }
145
141
 
146
142
  // /**
@@ -152,29 +148,29 @@ export class RedBlackTree<
152
148
  // * Time Complexity: O(1)
153
149
  // * Space Complexity: O(1)
154
150
  // *
155
- // * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
151
+ // * The function `keyValueNodeEntryRawToNodeAndValue` takes a key, value, or entry and returns a node if it is
156
152
  // * valid, otherwise it returns undefined.
157
- // * @param {BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRaw - The key, value, or entry to convert.
158
- // * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRaw` is a key).
153
+ // * @param {BTNRep<K, V, NODE>} keyNodeEntryOrRaw - The key, value, or entry to convert.
154
+ // * @param {V} [value] - The value associated with the key (if `keyNodeEntryOrRaw` is a key).
159
155
  // * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
160
156
  // */
161
- // override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): NODE | undefined {
157
+ // override keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): NODE | undefined {
162
158
  //
163
- // if (keyOrNodeOrEntryOrRaw === null || keyOrNodeOrEntryOrRaw === undefined) return;
164
- // if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
159
+ // if (keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === undefined) return;
160
+ // if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw;
165
161
  //
166
162
  // if (this._toEntryFn) {
167
- // const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
168
- // if (this.isKey(key)) return this.createNode(key, entryValue ?? value, 'RED');
163
+ // const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
164
+ // if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'RED');
169
165
  // }
170
166
  //
171
- // if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
172
- // const [key, value] = keyOrNodeOrEntryOrRaw;
167
+ // if (this.isEntry(keyNodeEntryOrRaw)) {
168
+ // const [key, value] = keyNodeEntryOrRaw;
173
169
  // if (key === undefined || key === null) return;
174
170
  // else return this.createNode(key, value, 'RED');
175
171
  // }
176
172
  //
177
- // if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.createNode(keyOrNodeOrEntryOrRaw, value, 'RED');
173
+ // if (this.isKey(keyNodeEntryOrRaw)) return this.createNode(keyNodeEntryOrRaw, value, 'RED');
178
174
  //
179
175
  // return ;
180
176
  // }
@@ -197,8 +193,8 @@ export class RedBlackTree<
197
193
  *
198
194
  * The function adds a new node to a binary search tree and returns true if the node was successfully
199
195
  * added.
200
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
201
- * `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
196
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
197
+ * `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
202
198
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
203
199
  * the key in the data structure. It represents the value that you want to add or update in the data
204
200
  * structure.
@@ -206,8 +202,8 @@ export class RedBlackTree<
206
202
  * the method returns true. If the node already exists and its value is updated, the method also
207
203
  * returns true. If the node cannot be added or updated, the method returns false.
208
204
  */
209
- override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean {
210
- const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
205
+ override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean {
206
+ const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
211
207
  if (!this.isRealNode(newNode)) return false;
212
208
 
213
209
  const insertStatus = this._insert(newNode);
@@ -219,6 +215,7 @@ export class RedBlackTree<
219
215
  } else {
220
216
  return false;
221
217
  }
218
+ if (this._isMapMode) this._setValue(newNode.key, newValue);
222
219
  this._size++;
223
220
  return true;
224
221
  } else return insertStatus === 'UPDATED';
@@ -230,7 +227,7 @@ export class RedBlackTree<
230
227
  *
231
228
  * The function overrides the delete method in a binary tree data structure to remove a node based on
232
229
  * a given predicate and maintain the binary search tree properties.
233
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
230
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
234
231
  * parameter in the `override delete` method is used to specify the condition or key based on which a
235
232
  * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
236
233
  * function that determines which node(s) should be deleted.
@@ -238,13 +235,13 @@ export class RedBlackTree<
238
235
  * objects. Each object in the array contains information about the deleted node and whether
239
236
  * balancing is needed.
240
237
  */
241
- override delete(predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>): BinaryTreeDeleteResult<NODE>[] {
242
- if (predicate === null) return [];
238
+ override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
239
+ if (keyNodeEntryOrRaw === null) return [];
243
240
 
244
241
  const results: BinaryTreeDeleteResult<NODE>[] = [];
245
- let nodeToDelete: OptBSTN<NODE>;
246
- if (this._isPredicated(predicate)) nodeToDelete = this.getNode(predicate);
247
- else nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate);
242
+ let nodeToDelete: OptNode<NODE>;
243
+ if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw);
244
+ else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
248
245
 
249
246
  if (!nodeToDelete) {
250
247
  return results;
@@ -285,6 +282,7 @@ export class RedBlackTree<
285
282
  successor.color = nodeToDelete.color;
286
283
  }
287
284
  }
285
+ if (this._isMapMode) this._store.delete(nodeToDelete.key);
288
286
  this._size--;
289
287
 
290
288
  // If the original color was black, fix the tree
@@ -7,16 +7,14 @@
7
7
  */
8
8
  import type {
9
9
  BinaryTreeDeleteResult,
10
- BSTNKeyOrNode,
11
- BTNKeyOrNodeOrEntry,
12
- BTNPredicate,
10
+ BSTNOptKeyOrNode,
11
+ BTNRep,
13
12
  IterationType,
14
- OptBSTN,
13
+ OptNode,
15
14
  RBTNColor,
16
15
  TreeMultiMapNested,
17
16
  TreeMultiMapNodeNested,
18
- TreeMultiMapOptions,
19
- BTNEntry
17
+ TreeMultiMapOptions
20
18
  } from '../../types';
21
19
  import { IBinaryTree } from '../../interfaces';
22
20
  import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
@@ -66,7 +64,7 @@ export class TreeMultiMapNode<
66
64
  export class TreeMultiMap<
67
65
  K = any,
68
66
  V = any,
69
- R = BTNEntry<K, V>,
67
+ R = object,
70
68
  NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
71
69
  TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
72
70
  >
@@ -75,19 +73,16 @@ export class TreeMultiMap<
75
73
  {
76
74
  /**
77
75
  * The constructor function initializes a TreeMultiMap object with optional initial data.
78
- * @param keysOrNodesOrEntriesOrRaws - The parameter `keysOrNodesOrEntriesOrRaws` is an
76
+ * @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` is an
79
77
  * iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
80
78
  * TreeMultiMap with initial data.
81
79
  * @param [options] - The `options` parameter is an optional object that can be used to customize the
82
80
  * behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
83
81
  * `compareValues`, which are functions used to compare keys and values respectively.
84
82
  */
85
- constructor(
86
- keysOrNodesOrEntriesOrRaws: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
87
- options?: TreeMultiMapOptions<K, V, R>
88
- ) {
83
+ constructor(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, NODE>> = [], options?: TreeMultiMapOptions<K, V, R>) {
89
84
  super([], options);
90
- if (keysOrNodesOrEntriesOrRaws) this.addMany(keysOrNodesOrEntriesOrRaws);
85
+ if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
91
86
  }
92
87
 
93
88
  protected _count = 0;
@@ -143,6 +138,7 @@ export class TreeMultiMap<
143
138
  override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE {
144
139
  return new TreeMultiMap<K, V, R, NODE, TREE>([], {
145
140
  iterationType: this.iterationType,
141
+ isMapMode: this._isMapMode,
146
142
  comparator: this._comparator,
147
143
  toEntryFn: this._toEntryFn,
148
144
  ...options
@@ -150,10 +146,10 @@ export class TreeMultiMap<
150
146
  }
151
147
 
152
148
  /**
153
- * The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
149
+ * The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
154
150
  * node based on the input.
155
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
156
- * `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
151
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
152
+ * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
157
153
  * @param {V} [value] - The `value` parameter is an optional value that represents the value
158
154
  * associated with the key in the node. It is used when creating a new node or updating the value of
159
155
  * an existing node.
@@ -161,40 +157,42 @@ export class TreeMultiMap<
161
157
  * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
162
158
  * @returns either a NODE object or undefined.
163
159
  */
164
- override keyValueOrEntryOrRawElementToNode(
165
- keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
160
+ override keyValueNodeEntryRawToNodeAndValue(
161
+ keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
166
162
  value?: V,
167
163
  count = 1
168
- ): NODE | undefined {
169
- if (keyOrNodeOrEntryOrRaw === undefined || keyOrNodeOrEntryOrRaw === null) return;
164
+ ): [NODE | undefined, V | undefined] {
165
+ if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
170
166
 
171
- if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
167
+ if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
172
168
 
173
- if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
174
- const [key, entryValue] = keyOrNodeOrEntryOrRaw;
175
- if (key === undefined || key === null) return;
176
- if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'BLACK', count);
169
+ if (this.isEntry(keyNodeEntryOrRaw)) {
170
+ const [key, entryValue] = keyNodeEntryOrRaw;
171
+ if (key === undefined || key === null) return [undefined, undefined];
172
+ const finalValue = value ?? entryValue;
173
+ if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
177
174
  }
178
175
 
179
176
  if (this._toEntryFn) {
180
- const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
181
- if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'BLACK', count);
177
+ const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
178
+ const finalValue = value ?? entryValue;
179
+ if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
182
180
  }
183
181
 
184
- if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.createNode(keyOrNodeOrEntryOrRaw, value, 'BLACK', count);
182
+ if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
185
183
 
186
- return;
184
+ return [undefined, undefined];
187
185
  }
188
186
 
189
187
  /**
190
188
  * The function checks if the input is an instance of the TreeMultiMapNode class.
191
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
192
- * `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
193
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
189
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
190
+ * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
191
+ * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
194
192
  * an instance of the `TreeMultiMapNode` class.
195
193
  */
196
- override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
197
- return keyOrNodeOrEntryOrRaw instanceof TreeMultiMapNode;
194
+ override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
195
+ return keyNodeEntryOrRaw instanceof TreeMultiMapNode;
198
196
  }
199
197
 
200
198
  /**
@@ -203,8 +201,8 @@ export class TreeMultiMap<
203
201
  *
204
202
  * The function overrides the add method of a class and adds a new node to a data structure, updating
205
203
  * the count and returning a boolean indicating success.
206
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
207
- * `keyOrNodeOrEntryOrRaw` parameter can accept one of the following types:
204
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
205
+ * `keyNodeEntryOrRaw` parameter can accept one of the following types:
208
206
  * @param {V} [value] - The `value` parameter represents the value associated with the key in the
209
207
  * data structure. It is an optional parameter, so it can be omitted if not needed.
210
208
  * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
@@ -213,10 +211,10 @@ export class TreeMultiMap<
213
211
  * @returns The method is returning a boolean value. It returns true if the addition of the new node
214
212
  * was successful, and false otherwise.
215
213
  */
216
- override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V, count = 1): boolean {
217
- const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value, count);
214
+ override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean {
215
+ const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
218
216
  const orgCount = newNode?.count || 0;
219
- const isSuccessAdded = super.add(newNode);
217
+ const isSuccessAdded = super.add(newNode, newValue);
220
218
 
221
219
  if (isSuccessAdded) {
222
220
  this._count += orgCount;
@@ -232,27 +230,23 @@ export class TreeMultiMap<
232
230
  *
233
231
  * The function `delete` in TypeScript overrides the deletion operation in a binary tree data
234
232
  * structure, handling cases where nodes have children and maintaining balance in the tree.
235
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
233
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `predicate`
236
234
  * parameter in the `delete` method is used to specify the condition or key based on which a node
237
- * should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
238
- * function.
235
+ * should be deleted from the binary tree. It can be a key, a node, or an entry.
239
236
  * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
240
237
  * boolean flag that determines whether to ignore the count of nodes when performing deletion. If
241
238
  * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
242
239
  * `ignoreCount` is `false
243
240
  * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
244
241
  */
245
- override delete(
246
- predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
247
- ignoreCount = false
248
- ): BinaryTreeDeleteResult<NODE>[] {
249
- if (predicate === null) return [];
242
+ override delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, ignoreCount = false): BinaryTreeDeleteResult<NODE>[] {
243
+ if (keyNodeEntryOrRaw === null) return [];
250
244
 
251
245
  const results: BinaryTreeDeleteResult<NODE>[] = [];
252
246
 
253
- let nodeToDelete: OptBSTN<NODE>;
254
- if (this._isPredicated(predicate)) nodeToDelete = this.getNode(predicate);
255
- else nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate);
247
+ let nodeToDelete: OptNode<NODE>;
248
+ if (this._isPredicate(keyNodeEntryOrRaw)) nodeToDelete = this.getNode(keyNodeEntryOrRaw);
249
+ else nodeToDelete = this.isRealNode(keyNodeEntryOrRaw) ? keyNodeEntryOrRaw : this.getNode(keyNodeEntryOrRaw);
256
250
 
257
251
  if (!nodeToDelete) {
258
252
  return results;
@@ -373,7 +367,8 @@ export class TreeMultiMap<
373
367
  if (l > r) return;
374
368
  const m = l + Math.floor((r - l) / 2);
375
369
  const midNode = sorted[m];
376
- this.add(midNode.key, midNode.value, midNode.count);
370
+ if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
371
+ else this.add(midNode.key, midNode.value, midNode.count);
377
372
  buildBalanceBST(l, m - 1);
378
373
  buildBalanceBST(m + 1, r);
379
374
  };
@@ -389,7 +384,8 @@ export class TreeMultiMap<
389
384
  if (l <= r) {
390
385
  const m = l + Math.floor((r - l) / 2);
391
386
  const midNode = sorted[m];
392
- this.add(midNode.key, midNode.value, midNode.count);
387
+ if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
388
+ else this.add(midNode.key, midNode.value, midNode.count);
393
389
  stack.push([m + 1, r]);
394
390
  stack.push([l, m - 1]);
395
391
  }
@@ -408,7 +404,8 @@ export class TreeMultiMap<
408
404
  */
409
405
  override clone(): TREE {
410
406
  const cloned = this.createTree();
411
- this.bfs(node => cloned.add(node.key, node.value, node.count));
407
+ this.bfs(node => cloned.add(node.key, undefined, node.count));
408
+ if (this._isMapMode) cloned._store = this._store;
412
409
  return cloned;
413
410
  }
414
411
 
@@ -418,17 +415,17 @@ export class TreeMultiMap<
418
415
  *
419
416
  * The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
420
417
  * in a binary search tree.
421
- * @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
418
+ * @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
422
419
  * that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
423
- * instance of the `BSTNKeyOrNode<K, NODE>` class.
424
- * @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
420
+ * instance of the `BSTNOptKeyOrNode<K, NODE>` class.
421
+ * @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
425
422
  * node where the properties will be swapped with the source node.
426
423
  * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
427
424
  * If either `srcNode` or `destNode` is undefined, it returns undefined.
428
425
  */
429
426
  protected override _swapProperties(
430
- srcNode: R | BSTNKeyOrNode<K, NODE>,
431
- destNode: R | BSTNKeyOrNode<K, NODE>
427
+ srcNode: R | BSTNOptKeyOrNode<K, NODE>,
428
+ destNode: R | BSTNOptKeyOrNode<K, NODE>
432
429
  ): NODE | undefined {
433
430
  srcNode = this.ensureNode(srcNode);
434
431
  destNode = this.ensureNode(destNode);
@@ -439,12 +436,12 @@ export class TreeMultiMap<
439
436
  tempNode.color = color;
440
437
 
441
438
  destNode.key = srcNode.key;
442
- destNode.value = srcNode.value;
439
+ if (!this._isMapMode) destNode.value = srcNode.value;
443
440
  destNode.count = srcNode.count;
444
441
  destNode.color = srcNode.color;
445
442
 
446
443
  srcNode.key = tempNode.key;
447
- srcNode.value = tempNode.value;
444
+ if (!this._isMapMode) srcNode.value = tempNode.value;
448
445
  srcNode.count = tempNode.count;
449
446
  srcNode.color = tempNode.color;
450
447
  }
@@ -266,9 +266,9 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
266
266
  *
267
267
  */
268
268
  getHeight(): number {
269
- const beginRoot = this.root;
269
+ const startNode = this.root;
270
270
  let maxDepth = 0;
271
- if (beginRoot) {
271
+ if (startNode) {
272
272
  const bfs = (node: TrieNode, level: number) => {
273
273
  if (level > maxDepth) {
274
274
  maxDepth = level;
@@ -280,7 +280,7 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
280
280
  }
281
281
  }
282
282
  };
283
- bfs(beginRoot, 0);
283
+ bfs(startNode, 0);
284
284
  }
285
285
  return maxDepth;
286
286
  }
@@ -4,14 +4,14 @@ import type {
4
4
  BinaryTreeNested,
5
5
  BinaryTreeNodeNested,
6
6
  BinaryTreeOptions,
7
- BTNKeyOrNodeOrEntry,
8
- BTNPredicate
7
+ BTNRep,
8
+ NodePredicate
9
9
  } from '../types';
10
10
 
11
11
  export interface IBinaryTree<
12
12
  K = any,
13
13
  V = any,
14
- R = [K, V],
14
+ R = object,
15
15
  NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>,
16
16
  TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTreeNested<K, V, R, NODE>
17
17
  > {
@@ -19,9 +19,9 @@ export interface IBinaryTree<
19
19
 
20
20
  createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE;
21
21
 
22
- add(keyOrNodeOrEntryOrRawElement: BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
22
+ add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, NODE>, value?: V, count?: number): boolean;
23
23
 
24
- addMany(nodes: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
24
+ addMany(nodes: Iterable<BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
25
25
 
26
- delete(predicate: R | BTNKeyOrNodeOrEntry<K, V, NODE> | BTNPredicate<NODE>): BinaryTreeDeleteResult<NODE>[];
26
+ delete(predicate: R | BTNRep<K, V, NODE> | NodePredicate<NODE>): BinaryTreeDeleteResult<NODE>[];
27
27
  }
@@ -6,31 +6,30 @@ export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K,
6
6
 
7
7
  export type BinaryTreeNested<K, V, R, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
8
8
 
9
+ export type ToEntryFn<K, V, R> = (rawElement: R) => BTNEntry<K, V>;
10
+
9
11
  export type BinaryTreeOptions<K, V, R> = {
10
- iterationType?: IterationType;
11
- toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
12
+ iterationType?: IterationType;
13
+ toEntryFn?: ToEntryFn<K, V, R>;
14
+ isMapMode?: boolean;
12
15
  }
13
16
 
14
17
  export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean };
15
18
 
16
- export type OptBTNOrNull<NODE> = NODE | null | undefined;
17
-
18
- export type OptBTNKeyOrNull<K> = K | null | undefined;
19
-
20
- export type BTNEntry<K, V> = [OptBTNKeyOrNull<K>, OptValue<V>];
19
+ export type OptNodeOrNull<NODE> = NODE | null | undefined;
21
20
 
22
- export type BTNKeyOrNode<K, NODE> = OptBTNKeyOrNull<K> | NODE;
21
+ export type BTNOptKeyOrNull<K> = K | null | undefined;
23
22
 
24
- export type BTNKeyOrNodeOrEntry<K, V, NODE> = BTNEntry<K, V> | BTNKeyOrNode<K, NODE>;
23
+ export type BTNEntry<K, V> = [BTNOptKeyOrNull<K>, OptValue<V>];
25
24
 
26
- export type BTNPureKeyOrNode<K, NODE> = K | NODE;
25
+ export type BTNOptKeyNodeOrNull<K, NODE> = BTNOptKeyOrNull<K> | NODE;
27
26
 
28
- export type BTNPureKeyOrNodeOrEntry<K, V, NODE> = [K, OptValue<V>] | BTNPureKeyOrNode<K, NODE>;
27
+ export type BTNRep<K, V, NODE> = BTNEntry<K, V> | BTNOptKeyNodeOrNull<K, NODE>;
29
28
 
30
- export type BinaryTreeDeleteResult<NODE> = { deleted: OptBTNOrNull<NODE>; needBalanced: OptBTNOrNull<NODE> };
29
+ export type BinaryTreeDeleteResult<NODE> = { deleted: OptNodeOrNull<NODE>; needBalanced: OptNodeOrNull<NODE> };
31
30
 
32
- export type BTNCallback<NODE, D = any> = (node: NODE) => D;
31
+ export type NodeCallback<NODE, D = any> = (node: NODE) => D;
33
32
 
34
- export type BTNPredicate<NODE> = (node: NODE) => boolean;
33
+ export type NodePredicate<NODE> = (node: NODE) => boolean;
35
34
 
36
- export type DFSStackItem<NODE> = { opt: DFSOperation; node: OptBTNOrNull<NODE> }
35
+ export type DFSStackItem<NODE> = { opt: DFSOperation; node: OptNodeOrNull<NODE> }
@@ -7,12 +7,12 @@ export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTN
7
7
  export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
8
8
 
9
9
  export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
10
- comparator?: Comparator<K>
10
+ comparator?: Comparator<K>
11
11
  }
12
12
 
13
- export type OptBSTNKey<K> = K | undefined;
13
+ export type BSTNOptKey<K> = K | undefined;
14
14
 
15
- export type OptBSTN<NODE> = NODE | undefined;
15
+ export type OptNode<NODE> = NODE | undefined;
16
16
 
17
- export type BSTNKeyOrNode<K, NODE> = OptBSTNKey<K> | NODE;
17
+ export type BSTNOptKeyOrNode<K, NODE> = BSTNOptKey<K> | NODE;
18
18