queue-typed 1.51.8 → 1.51.9

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 (50) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +80 -60
  4. package/dist/data-structures/binary-tree/avl-tree.js +78 -59
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +316 -224
  6. package/dist/data-structures/binary-tree/binary-tree.js +471 -361
  7. package/dist/data-structures/binary-tree/bst.d.ts +198 -200
  8. package/dist/data-structures/binary-tree/bst.js +215 -249
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +71 -72
  10. package/dist/data-structures/binary-tree/rb-tree.js +107 -98
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
  13. package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
  14. package/dist/data-structures/graph/abstract-graph.js +10 -15
  15. package/dist/data-structures/hash/hash-map.d.ts +31 -38
  16. package/dist/data-structures/hash/hash-map.js +40 -55
  17. package/dist/data-structures/queue/deque.d.ts +2 -3
  18. package/dist/data-structures/queue/deque.js +2 -3
  19. package/dist/data-structures/trie/trie.d.ts +1 -1
  20. package/dist/data-structures/trie/trie.js +1 -1
  21. package/dist/interfaces/binary-tree.d.ts +6 -6
  22. package/dist/types/common.d.ts +1 -2
  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 +5 -4
  26. package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
  27. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
  28. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
  29. package/dist/utils/utils.js +3 -5
  30. package/package.json +2 -2
  31. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
  32. package/src/data-structures/binary-tree/avl-tree.ts +94 -66
  33. package/src/data-structures/binary-tree/binary-tree.ts +530 -398
  34. package/src/data-structures/binary-tree/bst.ts +251 -270
  35. package/src/data-structures/binary-tree/rb-tree.ts +121 -100
  36. package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
  37. package/src/data-structures/graph/abstract-graph.ts +10 -10
  38. package/src/data-structures/hash/hash-map.ts +42 -49
  39. package/src/data-structures/queue/deque.ts +2 -2
  40. package/src/data-structures/queue/queue.ts +1 -1
  41. package/src/data-structures/trie/trie.ts +2 -2
  42. package/src/interfaces/binary-tree.ts +8 -7
  43. package/src/types/common.ts +1 -2
  44. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
  45. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  46. package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
  47. package/src/types/data-structures/binary-tree/bst.ts +4 -4
  48. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
  49. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
  50. package/src/utils/utils.ts +3 -3
@@ -9,6 +9,7 @@ import type {
9
9
  RedBlackTreeNested,
10
10
  RedBlackTreeNodeNested
11
11
  } from '../../types';
12
+ import { BTNEntry } from '../../types';
12
13
  import { BST, BSTNode } from './bst';
13
14
  import { IBinaryTree } from '../../interfaces';
14
15
 
@@ -55,28 +56,32 @@ export class RedBlackTreeNode<
55
56
  export class RedBlackTree<
56
57
  K extends Comparable,
57
58
  V = any,
59
+ R = BTNEntry<K, V>,
58
60
  NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
59
- TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>
61
+ TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
60
62
  >
61
- extends BST<K, V, NODE, TREE>
62
- implements IBinaryTree<K, V, NODE, TREE> {
63
+ extends BST<K, V, R, NODE, TREE>
64
+ implements IBinaryTree<K, V, R, NODE, TREE> {
63
65
  /**
64
66
  * This is the constructor function for a Red-Black Tree data structure in TypeScript.
65
- * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
66
- * contain keys, nodes, or entries. It is used to initialize the RBTree with the provided keys,
67
- * nodes, or entries.
67
+ * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
68
+ * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
69
+ * initialize the RBTree with the provided elements.
68
70
  * @param [options] - The `options` parameter is an optional object that can be passed to the
69
- * constructor. It allows you to customize the behavior of the RBTree. It can include properties such
70
- * as `compareKeys`, `compareValues`, `allowDuplicates`, etc. These properties define how the RBTree
71
- * should compare keys and
71
+ * constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
72
+ * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
73
+ * depend on the implementation
72
74
  */
73
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
75
+ constructor(
76
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
77
+ options?: RBTreeOptions<K, V, R>
78
+ ) {
74
79
  super([], options);
75
80
 
76
81
  this._root = this.NIL;
77
82
 
78
- if (keysOrNodesOrEntries) {
79
- this.addMany(keysOrNodesOrEntries);
83
+ if (keysOrNodesOrEntriesOrRawElements) {
84
+ this.addMany(keysOrNodesOrEntriesOrRawElements);
80
85
  }
81
86
  }
82
87
 
@@ -92,29 +97,30 @@ export class RedBlackTree<
92
97
 
93
98
  /**
94
99
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
95
- * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
96
- * which is a generic type representing the key's data type.
100
+ * @param {K} key - The key parameter represents the key value of the node being created. It is of
101
+ * type K, which is a generic type that can be replaced with any specific type when using the
102
+ * function.
97
103
  * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
98
- * associated with the key in the node. It is not required and can be omitted if not needed.
99
- * @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
100
- * Red-Black Tree. It is an optional parameter with a default value of "'BLACK'". The color
101
- * can be either "'RED'" or "'BLACK'".
102
- * @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
103
- * value, and color.
104
+ * associated with the key in the node. It is not required and can be omitted if you only need to
105
+ * create a node with a key.
106
+ * @param {RBTNColor} [color=BLACK] - The "color" parameter is used to specify the color of the node
107
+ * in a Red-Black Tree. It can have two possible values: "RED" or "BLACK". By default, the color is
108
+ * set to "BLACK" if not specified.
109
+ * @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
110
+ * returned.
104
111
  */
105
112
  override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): NODE {
106
113
  return new RedBlackTreeNode<K, V, NODE>(key, value, color) as NODE;
107
114
  }
108
115
 
109
116
  /**
110
- * The function creates a Red-Black Tree with the given options and returns it.
111
- * @param [options] - The `options` parameter is an optional object that contains configuration
112
- * options for creating the Red-Black Tree. It is of type `RBTreeOptions<K>`, where `K` represents
113
- * the type of keys in the tree.
117
+ * The function creates a new Red-Black Tree with the specified options.
118
+ * @param [options] - The `options` parameter is an optional object that contains additional
119
+ * configuration options for creating the Red-Black Tree. It has the following properties:
114
120
  * @returns a new instance of a RedBlackTree object.
115
121
  */
116
- override createTree(options?: RBTreeOptions<K>): TREE {
117
- return new RedBlackTree<K, V, NODE, TREE>([], {
122
+ override createTree(options?: RBTreeOptions<K, V, R>): TREE {
123
+ return new RedBlackTree<K, V, R, NODE, TREE>([], {
118
124
  iterationType: this.iterationType,
119
125
  ...options
120
126
  }) as TREE;
@@ -126,54 +132,57 @@ export class RedBlackTree<
126
132
  */
127
133
 
128
134
  /**
129
- * Time Complexity: O(1)
130
- * Space Complexity: O(1)
131
- *
132
- * The function `keyValueOrEntryToNode` takes a key, value, or entry and returns a node if it is
133
- * valid, otherwise it returns undefined.
134
- * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The key, value, or entry to convert.
135
- * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntry` is a key).
136
- * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
137
- */
138
- override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
139
- let node: NODE | undefined;
140
-
141
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
142
- return;
143
- } else if (this.isNode(keyOrNodeOrEntry)) {
144
- node = keyOrNodeOrEntry;
145
- } else if (this.isEntry(keyOrNodeOrEntry)) {
146
- const [key, value] = keyOrNodeOrEntry;
147
- if (key === undefined || key === null) {
148
- return;
149
- } else {
150
- node = this.createNode(key, value, 'RED');
151
- }
152
- } else if (!this.isNode(keyOrNodeOrEntry)) {
153
- node = this.createNode(keyOrNodeOrEntry, value, 'RED');
154
- } else {
155
- return;
156
- }
157
- return node;
158
- }
159
-
160
- /**
161
- * Time Complexity: O(1)
162
- * Space Complexity: O(1)
163
- * /
164
-
165
- /**
166
135
  * Time Complexity: O(1)
167
136
  * Space Complexity: O(1)
168
137
  *
169
138
  * The function checks if the input is an instance of the RedBlackTreeNode class.
170
- * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The object to check.
171
- * @returns {boolean} - `true` if the object is a Red-Black Tree node, `false` otherwise.
139
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
140
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
141
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
142
+ * an instance of the `RedBlackTreeNode` class.
172
143
  */
173
- override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
174
- return keyOrNodeOrEntry instanceof RedBlackTreeNode;
144
+ override isNode(
145
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
146
+ ): keyOrNodeOrEntryOrRawElement is NODE {
147
+ return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
175
148
  }
176
149
 
150
+ // /**
151
+ // * Time Complexity: O(1)
152
+ // * Space Complexity: O(1)
153
+ // */
154
+ //
155
+ // /**
156
+ // * Time Complexity: O(1)
157
+ // * Space Complexity: O(1)
158
+ // *
159
+ // * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
160
+ // * valid, otherwise it returns undefined.
161
+ // * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
162
+ // * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
163
+ // * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
164
+ // */
165
+ // override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
166
+ //
167
+ // if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
168
+ // if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
169
+ //
170
+ // if (this.toEntryFn) {
171
+ // const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
172
+ // if (key) return this.createNode(key, entryValue ?? value, 'RED');
173
+ // }
174
+ //
175
+ // if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
176
+ // const [key, value] = keyOrNodeOrEntryOrRawElement;
177
+ // if (key === undefined || key === null) return;
178
+ // else return this.createNode(key, value, 'RED');
179
+ // }
180
+ //
181
+ // if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'RED');
182
+ //
183
+ // return ;
184
+ // }
185
+
177
186
  /**
178
187
  * Time Complexity: O(1)
179
188
  * Space Complexity: O(1)
@@ -200,17 +209,19 @@ export class RedBlackTree<
200
209
  * Time Complexity: O(log n)
201
210
  * Space Complexity: O(1)
202
211
  *
203
- * The function adds a new node to a Red-Black Tree data structure and returns a boolean indicating
204
- * whether the operation was successful.
205
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
206
- * entry.
207
- * @param {V} [value] - The `value` parameter is the value associated with the key that is being
208
- * added to the tree.
209
- * @returns The method is returning a boolean value. It returns true if the node was successfully
210
- * added or updated, and false otherwise.
212
+ * The function adds a new node to a binary search tree and returns true if the node was successfully
213
+ * added.
214
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
215
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
216
+ * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
217
+ * the key in the data structure. It represents the value that you want to add or update in the data
218
+ * structure.
219
+ * @returns The method is returning a boolean value. If a new node is successfully added to the tree,
220
+ * the method returns true. If the node already exists and its value is updated, the method also
221
+ * returns true. If the node cannot be added or updated, the method returns false.
211
222
  */
212
- override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
213
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
223
+ override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
224
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
214
225
  if (!this.isRealNode(newNode)) return false;
215
226
 
216
227
  const insertStatus = this._insert(newNode);
@@ -236,16 +247,16 @@ export class RedBlackTree<
236
247
  * Time Complexity: O(log n)
237
248
  * Space Complexity: O(1)
238
249
  *
239
- * The function `delete` in a binary tree class deletes a node from the tree and fixes the tree if
240
- * necessary.
241
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
242
- * identifier of the node that needs to be deleted from the binary tree. It can be of any type that
243
- * is returned by the callback function `C`. It can also be `null` or `undefined` if the node to be
244
- * deleted is not found.
245
- * @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
246
- * the binary tree based on its identifier. It is an optional parameter and if not provided, the
247
- * `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should
248
- * return the identifier of the node to
250
+ * The function overrides the delete method of a binary tree data structure, allowing for the
251
+ * deletion of a node and maintaining the balance of the tree.
252
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
253
+ * that identifies the node to be deleted from the binary tree. It can be of any type that is
254
+ * returned by the callback function `C`. It can also be `null` or `undefined` if there is no node to
255
+ * delete.
256
+ * @param {C} callback - The `callback` parameter is a function that is used to determine the
257
+ * equality of nodes in the binary tree. It is optional and has a default value of
258
+ * `this._DEFAULT_CALLBACK`. The type of the `callback` parameter is `C`, which is a generic type
259
+ * that extends the `BTNCallback
249
260
  * @returns an array of BinaryTreeDeleteResult<NODE> objects.
250
261
  */
251
262
  override delete<C extends BTNCallback<NODE>>(
@@ -309,6 +320,14 @@ export class RedBlackTree<
309
320
  }
310
321
 
311
322
  /**
323
+ * Time Complexity: O(1)
324
+ * Space Complexity: O(1)
325
+ */
326
+
327
+ /**
328
+ * Time Complexity: O(1)
329
+ * Space Complexity: O(1)
330
+ *
312
331
  * The function sets the root of a tree-like structure and updates the parent property of the new
313
332
  * root.
314
333
  * @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
@@ -332,8 +351,8 @@ export class RedBlackTree<
332
351
  * The function replaces an old node with a new node while preserving the color of the old node.
333
352
  * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
334
353
  * the data structure.
335
- * @param {NODE} newNode - The `newNode` parameter is the new node that will replace the old node in
336
- * the data structure.
354
+ * @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
355
+ * data structure.
337
356
  * @returns The method is returning the result of calling the `_replaceNode` method from the
338
357
  * superclass, with the `oldNode` and `newNode` parameters.
339
358
  */
@@ -352,12 +371,13 @@ export class RedBlackTree<
352
371
  * Time Complexity: O(log n)
353
372
  * Space Complexity: O(1)
354
373
  *
355
- * The `_insert` function inserts or updates a node in a binary search tree and performs necessary
356
- * fix-ups to maintain the red-black tree properties.
357
- * @param {NODE} node - The `node` parameter represents the node that needs to be inserted into a
358
- * binary search tree. It contains a `key` property that is used to determine the position of the
359
- * node in the tree.
360
- * @returns {'inserted' | 'updated'} - The result of the insertion.
374
+ * The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
375
+ * maintain the red-black tree properties.
376
+ * @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the
377
+ * binary search tree.
378
+ * @returns a string value indicating the result of the insertion operation. It can return either
379
+ * 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
380
+ * was created and inserted into the tree.
361
381
  */
362
382
  protected _insert(node: NODE): CRUD {
363
383
  let current = this.root;
@@ -432,8 +452,8 @@ export class RedBlackTree<
432
452
  * Space Complexity: O(1)
433
453
  *
434
454
  * The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
435
- * @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree. It can
436
- * either be a valid node object or `undefined`.
455
+ * @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
456
+ * structure. It can either be a valid node or `undefined`.
437
457
  */
438
458
  protected _insertFixup(z: NODE | undefined): void {
439
459
  // Continue fixing the tree as long as the parent of z is red
@@ -504,9 +524,10 @@ export class RedBlackTree<
504
524
  *
505
525
  * The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
506
526
  * the colors and performing rotations.
507
- * @param {NODE | undefined} node - The `node` parameter represents a node in a Red-Black Tree data
508
- * structure. It can be either a valid node object or `undefined`.
509
- * @returns The function does not return any value. It has a return type of `void`.
527
+ * @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can
528
+ * be either a valid node object or `undefined`.
529
+ * @returns The function does not return any value. It has a return type of `void`, which means it
530
+ * does not return anything.
510
531
  */
511
532
  protected _deleteFixup(node: NODE | undefined): void {
512
533
  // Early exit condition