min-heap-typed 1.51.8 → 1.52.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 (106) hide show
  1. package/dist/data-structures/base/index.d.ts +2 -1
  2. package/dist/data-structures/base/index.js +2 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
  4. package/dist/data-structures/base/iterable-element-base.js +225 -0
  5. package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  6. package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
  10. package/dist/data-structures/binary-tree/avl-tree.js +78 -59
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -226
  12. package/dist/data-structures/binary-tree/binary-tree.js +475 -363
  13. package/dist/data-structures/binary-tree/bst.d.ts +192 -202
  14. package/dist/data-structures/binary-tree/bst.js +207 -249
  15. package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
  16. package/dist/data-structures/binary-tree/rb-tree.js +107 -98
  17. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
  18. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
  19. package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
  20. package/dist/data-structures/graph/abstract-graph.js +10 -15
  21. package/dist/data-structures/hash/hash-map.d.ts +33 -40
  22. package/dist/data-structures/hash/hash-map.js +40 -55
  23. package/dist/data-structures/heap/heap.d.ts +43 -114
  24. package/dist/data-structures/heap/heap.js +59 -127
  25. package/dist/data-structures/heap/max-heap.d.ts +50 -4
  26. package/dist/data-structures/heap/max-heap.js +76 -10
  27. package/dist/data-structures/heap/min-heap.d.ts +51 -5
  28. package/dist/data-structures/heap/min-heap.js +68 -11
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  32. package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
  33. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  34. package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
  35. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  36. package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
  37. package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
  38. package/dist/data-structures/priority-queue/priority-queue.js +70 -1
  39. package/dist/data-structures/queue/deque.d.ts +21 -20
  40. package/dist/data-structures/queue/deque.js +29 -23
  41. package/dist/data-structures/queue/queue.d.ts +8 -28
  42. package/dist/data-structures/queue/queue.js +15 -31
  43. package/dist/data-structures/stack/stack.d.ts +17 -22
  44. package/dist/data-structures/stack/stack.js +25 -24
  45. package/dist/data-structures/trie/trie.d.ts +19 -14
  46. package/dist/data-structures/trie/trie.js +27 -16
  47. package/dist/interfaces/binary-tree.d.ts +7 -7
  48. package/dist/types/common.d.ts +1 -2
  49. package/dist/types/data-structures/base/base.d.ts +5 -2
  50. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
  51. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
  52. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
  53. package/dist/types/data-structures/binary-tree/bst.d.ts +4 -5
  54. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
  55. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  56. package/dist/types/data-structures/heap/heap.d.ts +3 -2
  57. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  58. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  59. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  60. package/dist/types/data-structures/queue/deque.d.ts +3 -2
  61. package/dist/types/data-structures/queue/queue.d.ts +2 -1
  62. package/dist/types/data-structures/stack/stack.d.ts +2 -1
  63. package/dist/types/data-structures/trie/trie.d.ts +3 -2
  64. package/dist/utils/utils.js +3 -5
  65. package/package.json +2 -2
  66. package/src/data-structures/base/index.ts +2 -1
  67. package/src/data-structures/base/iterable-element-base.ts +250 -0
  68. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
  69. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
  70. package/src/data-structures/binary-tree/avl-tree.ts +96 -69
  71. package/src/data-structures/binary-tree/binary-tree.ts +535 -403
  72. package/src/data-structures/binary-tree/bst.ts +247 -277
  73. package/src/data-structures/binary-tree/rb-tree.ts +123 -103
  74. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
  75. package/src/data-structures/graph/abstract-graph.ts +10 -10
  76. package/src/data-structures/hash/hash-map.ts +46 -53
  77. package/src/data-structures/heap/heap.ts +71 -152
  78. package/src/data-structures/heap/max-heap.ts +88 -13
  79. package/src/data-structures/heap/min-heap.ts +78 -15
  80. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  81. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  82. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  83. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  84. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  85. package/src/data-structures/queue/deque.ts +37 -26
  86. package/src/data-structures/queue/queue.ts +23 -36
  87. package/src/data-structures/stack/stack.ts +31 -26
  88. package/src/data-structures/trie/trie.ts +35 -20
  89. package/src/interfaces/binary-tree.ts +9 -9
  90. package/src/types/common.ts +1 -2
  91. package/src/types/data-structures/base/base.ts +14 -6
  92. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
  93. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
  94. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
  95. package/src/types/data-structures/binary-tree/bst.ts +4 -5
  96. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
  97. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
  98. package/src/types/data-structures/heap/heap.ts +4 -1
  99. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  100. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  101. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  102. package/src/types/data-structures/queue/deque.ts +3 -1
  103. package/src/types/data-structures/queue/queue.ts +3 -1
  104. package/src/types/data-structures/stack/stack.ts +3 -1
  105. package/src/types/data-structures/trie/trie.ts +3 -1
  106. package/src/utils/utils.ts +3 -3
@@ -12,15 +12,15 @@ import type {
12
12
  BinaryTreeDeleteResult,
13
13
  BSTNKeyOrNode,
14
14
  BTNCallback,
15
- Comparable,
16
15
  IterationType,
17
16
  KeyOrNodeOrEntry
18
17
  } from '../../types';
18
+ import { BTNEntry } from '../../types';
19
19
  import { IBinaryTree } from '../../interfaces';
20
20
  import { AVLTree, AVLTreeNode } from './avl-tree';
21
21
 
22
22
  export class AVLTreeMultiMapNode<
23
- K extends Comparable,
23
+ K = any,
24
24
  V = any,
25
25
  NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>
26
26
  > extends AVLTreeNode<K, V, NODE> {
@@ -63,21 +63,38 @@ export class AVLTreeMultiMapNode<
63
63
  * The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
64
64
  */
65
65
  export class AVLTreeMultiMap<
66
- K extends Comparable,
66
+ K = any,
67
67
  V = any,
68
+ R = BTNEntry<K, V>,
68
69
  NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
69
- TREE extends AVLTreeMultiMap<K, V, NODE, TREE> = AVLTreeMultiMap<K, V, NODE, AVLTreeMultiMapNested<K, V, NODE>>
70
+ TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
71
+ K,
72
+ V,
73
+ R,
74
+ NODE,
75
+ AVLTreeMultiMapNested<K, V, R, NODE>
76
+ >
70
77
  >
71
- extends AVLTree<K, V, NODE, TREE>
72
- implements IBinaryTree<K, V, NODE, TREE> {
73
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeMultiMapOptions<K>) {
78
+ extends AVLTree<K, V, R, NODE, TREE>
79
+ implements IBinaryTree<K, V, R, NODE, TREE> {
80
+ /**
81
+ * The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
82
+ * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
83
+ * iterable object that can contain either keys, nodes, entries, or raw elements.
84
+ * @param [options] - The `options` parameter is an optional object that can be used to customize the
85
+ * behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
86
+ * `compareValues` functions to define custom comparison logic for keys and values, respectively.
87
+ */
88
+ constructor(
89
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
90
+ options?: AVLTreeMultiMapOptions<K, V, R>
91
+ ) {
74
92
  super([], options);
75
- if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
93
+ if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
76
94
  }
77
95
 
78
96
  protected _count = 0;
79
97
 
80
- // TODO the _count is not accurate after nodes count modified
81
98
  /**
82
99
  * The function calculates the sum of the count property of all nodes in a tree using depth-first
83
100
  * search.
@@ -107,20 +124,29 @@ export class AVLTreeMultiMap<
107
124
  }
108
125
 
109
126
  /**
110
- * The function creates a new BSTNode with the given key, value, and count.
111
- * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
112
- * distinguish one node from another in the tree.
113
- * @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
114
- * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
115
- * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
116
- * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
127
+ * The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
128
+ * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
129
+ * which is a generic type that can be replaced with any specific type when using the function.
130
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
131
+ * associated with the key in the node. It is of type `V`, which can be any data type.
132
+ * @param {number} [count] - The `count` parameter represents the number of occurrences of a
133
+ * key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
134
+ * calling the `createNode` method. If provided, it specifies the initial count for the node.
135
+ * @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
117
136
  */
118
137
  override createNode(key: K, value?: V, count?: number): NODE {
119
138
  return new AVLTreeMultiMapNode(key, value, count) as NODE;
120
139
  }
121
140
 
122
- override createTree(options?: AVLTreeMultiMapOptions<K>): TREE {
123
- return new AVLTreeMultiMap<K, V, NODE, TREE>([], {
141
+ /**
142
+ * The function creates a new AVLTreeMultiMap object with the specified options and returns it.
143
+ * @param [options] - The `options` parameter is an optional object that contains additional
144
+ * configuration options for creating the AVLTreeMultiMap. It can have the following properties:
145
+ * @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
146
+ * object.
147
+ */
148
+ override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
149
+ return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
124
150
  iterationType: this.iterationType,
125
151
  comparator: this.comparator,
126
152
  ...options
@@ -128,49 +154,52 @@ export class AVLTreeMultiMap<
128
154
  }
129
155
 
130
156
  /**
131
- * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
132
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
133
- * can be one of the following:
134
- * @param {V} [value] - The `value` parameter is an optional argument that represents the value
135
- * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
136
- * it defaults to `undefined`.
157
+ * The function checks if the input is an instance of AVLTreeMultiMapNode.
158
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
159
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
160
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
161
+ * an instance of the `AVLTreeMultiMapNode` class.
162
+ */
163
+ override isNode(
164
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
165
+ ): keyOrNodeOrEntryOrRawElement is NODE {
166
+ return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode;
167
+ }
168
+
169
+ /**
170
+ * The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
171
+ * a node object.
172
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
173
+ * `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
174
+ * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
175
+ * `override` function. It represents the value associated with the key in the data structure. If no
176
+ * value is provided, it will default to `undefined`.
137
177
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
138
- * times the value should be added to the node. If not provided, it defaults to 1.
139
- * @returns a node of type `NODE` or `undefined`.
178
+ * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
179
+ * @returns either a NODE object or undefined.
140
180
  */
141
- override keyValueOrEntryToNode(
142
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
181
+ override keyValueOrEntryOrRawElementToNode(
182
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
143
183
  value?: V,
144
184
  count = 1
145
185
  ): NODE | undefined {
146
- let node: NODE | undefined;
147
- if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
148
- return;
149
- } else if (this.isNode(keyOrNodeOrEntry)) {
150
- node = keyOrNodeOrEntry;
151
- } else if (this.isEntry(keyOrNodeOrEntry)) {
152
- const [key, value] = keyOrNodeOrEntry;
153
- if (key === undefined || key === null) {
154
- return;
155
- } else {
156
- node = this.createNode(key, value, count);
157
- }
158
- } else if (!this.isNode(keyOrNodeOrEntry)) {
159
- node = this.createNode(keyOrNodeOrEntry, value, count);
160
- } else {
161
- return;
186
+ if (keyOrNodeOrEntryOrRawElement === undefined || keyOrNodeOrEntryOrRawElement === null) return;
187
+ if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
188
+
189
+ if (this.toEntryFn) {
190
+ const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
191
+ if (key) return this.createNode(key, entryValue ?? value, count);
162
192
  }
163
- return node;
164
- }
165
193
 
166
- /**
167
- * The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
168
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
169
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
170
- * class.
171
- */
172
- override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
173
- return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
194
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
195
+ const [key, value] = keyOrNodeOrEntryOrRawElement;
196
+ if (key === undefined || key === null) return;
197
+ else return this.createNode(key, value, count);
198
+ }
199
+
200
+ if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value, count);
201
+
202
+ return;
174
203
  }
175
204
 
176
205
  /**
@@ -182,20 +211,21 @@ export class AVLTreeMultiMap<
182
211
  * Time Complexity: O(log n)
183
212
  * Space Complexity: O(1)
184
213
  *
185
- * The function overrides the add method of a binary tree node and adds a new node to the tree.
186
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
187
- * entry. It represents the key, node, or entry that you want to add to the binary tree.
214
+ * The function overrides the add method of a TypeScript class to add a new node to a data structure
215
+ * and update the count.
216
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
217
+ * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It
218
+ * can also accept a value of type `KeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
219
+ * entry, or raw element
188
220
  * @param {V} [value] - The `value` parameter represents the value associated with the key in the
189
- * binary tree node. It is an optional parameter, meaning it can be omitted when calling the `add`
190
- * method.
221
+ * data structure. It is an optional parameter, so it can be omitted if not needed.
191
222
  * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
192
- * be added to the binary tree. By default, it is set to 1, meaning that the key-value pair will be
193
- * added once. However, you can specify a different value for `count` if you want to add
194
- * @returns The method is returning either the newly inserted node or `undefined` if the insertion
195
- * was not successful.
223
+ * be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
224
+ * be added once. However, you can specify a different value for `count` if you want to add
225
+ * @returns a boolean value.
196
226
  */
197
- override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
198
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
227
+ override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
228
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
199
229
  if (newNode === undefined) return false;
200
230
 
201
231
  const orgNodeCount = newNode?.count || 0;
@@ -215,19 +245,19 @@ export class AVLTreeMultiMap<
215
245
  * Time Complexity: O(log n)
216
246
  * Space Complexity: O(1)
217
247
  *
218
- * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
219
- * account the count of the node and balancing the tree if necessary.
220
- * @param identifier - The identifier is the value or key that is used to identify the node that
221
- * needs to be deleted from the binary tree. It can be of any type that is returned by the callback
248
+ * The `delete` function in a binary tree data structure deletes a node based on its identifier and
249
+ * returns the deleted node along with the parent node that needs to be balanced.
250
+ * @param identifier - The identifier parameter is the value used to identify the node that needs to
251
+ * be deleted from the binary tree. It can be of any type and is the return type of the callback
222
252
  * function.
223
- * @param {C} callback - The `callback` parameter is a function that is used to determine if a node
224
- * should be deleted. It is optional and defaults to a default callback function. The `callback`
225
- * function takes one parameter, which is the identifier of the node, and returns a value that is
226
- * used to identify the node to
253
+ * @param {C} callback - The `callback` parameter is a function that is used to determine the
254
+ * equality of nodes in the binary tree. It is optional and has a default value of
255
+ * `this._DEFAULT_CALLBACK`. The `callback` function takes a single argument, which is the identifier
256
+ * of a node, and returns a value that
227
257
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
228
258
  * being deleted. If set to true, the count of the node will not be considered and the node will be
229
- * deleted regardless of its count. If set to false (default), the count of the node will be
230
- * decremented by 1 and
259
+ * deleted regardless of its count. If set to false (default), the count of the node will be taken
260
+ * into account and the node
231
261
  * @returns an array of `BinaryTreeDeleteResult<NODE>`.
232
262
  */
233
263
  override delete<C extends BTNCallback<NODE>>(
@@ -300,7 +330,8 @@ export class AVLTreeMultiMap<
300
330
  * Time Complexity: O(1)
301
331
  * Space Complexity: O(1)
302
332
  *
303
- * The clear() function clears the contents of a data structure and sets the count to zero.
333
+ * The "clear" function overrides the parent class's "clear" function and also resets the count to
334
+ * zero.
304
335
  */
305
336
  override clear() {
306
337
  super.clear();
@@ -315,13 +346,14 @@ export class AVLTreeMultiMap<
315
346
  /**
316
347
  * Time Complexity: O(n log n)
317
348
  * Space Complexity: O(log n)
318
- *
319
349
  * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
320
350
  * tree using either a recursive or iterative approach.
321
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
322
- * type of iteration to use when building the balanced binary search tree. It can have two possible
323
- * values:
324
- * @returns a boolean value.
351
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
352
+ * specifies the type of iteration to use when building the balanced binary search tree. It has a
353
+ * default value of `this.iterationType`, which means it will use the iteration type currently set in
354
+ * the object.
355
+ * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
356
+ * balancing operation is successful, and `false` if there are no nodes to balance.
325
357
  */
326
358
  override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
327
359
  const sorted = this.dfs(node => node, 'IN'),
@@ -370,7 +402,7 @@ export class AVLTreeMultiMap<
370
402
  * Time complexity: O(n)
371
403
  * Space complexity: O(n)
372
404
  *
373
- * The `clone` function creates a deep copy of a tree object.
405
+ * The function overrides the clone method to create a deep copy of a tree object.
374
406
  * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
375
407
  */
376
408
  override clone(): TREE {
@@ -380,17 +412,26 @@ export class AVLTreeMultiMap<
380
412
  }
381
413
 
382
414
  /**
383
- * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
384
- * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
385
- * which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
386
- * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
387
- * node where the values from the source node will be swapped to.
388
- * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
389
- * if either `srcNode` or `destNode` is undefined.
415
+ * Time Complexity: O(1)
416
+ * Space Complexity: O(1)
417
+ */
418
+
419
+ /**
420
+ * Time Complexity: O(1)
421
+ * Space Complexity: O(1)
422
+ *
423
+ * The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
424
+ * in a binary search tree.
425
+ * @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
426
+ * that will be swapped with the `destNode`.
427
+ * @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
428
+ * node where the properties will be swapped with the source node.
429
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
430
+ * If either `srcNode` or `destNode` is undefined, it returns `undefined`.
390
431
  */
391
432
  protected override _swapProperties(
392
- srcNode: BSTNKeyOrNode<K, NODE>,
393
- destNode: BSTNKeyOrNode<K, NODE>
433
+ srcNode: R | BSTNKeyOrNode<K, NODE>,
434
+ destNode: R | BSTNKeyOrNode<K, NODE>
394
435
  ): NODE | undefined {
395
436
  srcNode = this.ensureNode(srcNode);
396
437
  destNode = this.ensureNode(destNode);
@@ -417,12 +458,20 @@ export class AVLTreeMultiMap<
417
458
  }
418
459
 
419
460
  /**
461
+ * Time Complexity: O(1)
462
+ * Space Complexity: O(1)
463
+ */
464
+
465
+ /**
466
+ * Time Complexity: O(1)
467
+ * Space Complexity: O(1)
468
+ *
420
469
  * The function replaces an old node with a new node and updates the count property of the new node.
421
- * @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
422
- * needs to be replaced in a data structure.
423
- * @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
470
+ * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
471
+ * data structure. It is of type NODE.
472
+ * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
424
473
  * @returns The method is returning the result of calling the `_replaceNode` method from the
425
- * superclass, after updating the `count` property of the `newNode` object.
474
+ * superclass, which is of type `NODE`.
426
475
  */
427
476
  protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
428
477
  newNode.count = oldNode.count + newNode.count;
@@ -13,13 +13,13 @@ import type {
13
13
  BinaryTreeDeleteResult,
14
14
  BSTNKeyOrNode,
15
15
  BTNCallback,
16
- Comparable,
17
16
  KeyOrNodeOrEntry
18
17
  } from '../../types';
18
+ import { BTNEntry } from '../../types';
19
19
  import { IBinaryTree } from '../../interfaces';
20
20
 
21
21
  export class AVLTreeNode<
22
- K extends Comparable,
22
+ K = any,
23
23
  V = any,
24
24
  NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>
25
25
  > extends BSTNode<K, V, NODE> {
@@ -66,35 +66,41 @@ export class AVLTreeNode<
66
66
  * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
67
67
  */
68
68
  export class AVLTree<
69
- K extends Comparable,
69
+ K = any,
70
70
  V = any,
71
+ R = BTNEntry<K, V>,
71
72
  NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
72
- TREE extends AVLTree<K, V, NODE, TREE> = AVLTree<K, V, NODE, AVLTreeNested<K, V, NODE>>
73
+ TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>
73
74
  >
74
- extends BST<K, V, NODE, TREE>
75
- implements IBinaryTree<K, V, NODE, TREE> {
75
+ extends BST<K, V, R, NODE, TREE>
76
+ implements IBinaryTree<K, V, R, NODE, TREE> {
76
77
  /**
77
- * The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
78
- * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
79
- * objects. It represents a collection of nodes that will be added to the AVL tree during
80
- * initialization.
81
- * @param [options] - The `options` parameter is an optional object that allows you to customize the
82
- * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
83
- * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
78
+ * This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
79
+ * entries, or raw elements.
80
+ * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
81
+ * iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
82
+ * be used to initialize the AVLTree.
83
+ * @param [options] - The `options` parameter is an optional object that can be used to customize the
84
+ * behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare
85
+ * keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
86
+ * `nodeBuilder` (
84
87
  */
85
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeOptions<K>) {
88
+ constructor(
89
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
90
+ options?: AVLTreeOptions<K, V, R>
91
+ ) {
86
92
  super([], options);
87
- if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
93
+ if (keysOrNodesOrEntriesOrRawElements) super.addMany(keysOrNodesOrEntriesOrRawElements);
88
94
  }
89
95
 
90
96
  /**
91
- * The function creates a new AVL tree node with the specified key and value.
92
- * @param {K} key - The key parameter is the key value that will be associated with
93
- * the new node. It is used to determine the position of the node in the binary search tree.
94
- * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
95
- * type `V`, which means it can be any value that is assignable to the `value` property of the
96
- * node type `NODE`.
97
- * @returns a new AVLTreeNode object with the specified key and value.
97
+ * The function creates a new AVL tree node with the given key and value.
98
+ * @param {K} key - The key parameter is of type K, which represents the key of the node being
99
+ * created.
100
+ * @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
101
+ * value associated with the key in the node being created.
102
+ * @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
103
+ * type NODE.
98
104
  */
99
105
  override createNode(key: K, value?: V): NODE {
100
106
  return new AVLTreeNode<K, V, NODE>(key, value) as NODE;
@@ -107,8 +113,8 @@ export class AVLTree<
107
113
  * being created.
108
114
  * @returns a new AVLTree object.
109
115
  */
110
- override createTree(options?: AVLTreeOptions<K>): TREE {
111
- return new AVLTree<K, V, NODE, TREE>([], {
116
+ override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
117
+ return new AVLTree<K, V, R, NODE, TREE>([], {
112
118
  iterationType: this.iterationType,
113
119
  comparator: this.comparator,
114
120
  ...options
@@ -116,12 +122,16 @@ export class AVLTree<
116
122
  }
117
123
 
118
124
  /**
119
- * The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
120
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
121
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
125
+ * The function checks if the input is an instance of AVLTreeNode.
126
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
127
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
128
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
129
+ * an instance of the `AVLTreeNode` class.
122
130
  */
123
- override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
124
- return keyOrNodeOrEntry instanceof AVLTreeNode;
131
+ override isNode(
132
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
133
+ ): keyOrNodeOrEntryOrRawElement is NODE {
134
+ return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
125
135
  }
126
136
 
127
137
  /**
@@ -134,18 +144,19 @@ export class AVLTree<
134
144
  * Time Complexity: O(log n)
135
145
  * Space Complexity: O(1)
136
146
  *
137
- * The function overrides the add method of a binary tree node and balances the tree after inserting
138
- * a new node.
139
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
140
- * entry.
141
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
142
- * being added to the binary tree.
143
- * @returns The method is returning either the inserted node or undefined.
147
+ * The function overrides the add method of a class and inserts a key-value pair into a data
148
+ * structure, then balances the path.
149
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
150
+ * `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
151
+ * `RawElement`.
152
+ * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
153
+ * the key or node being added to the data structure.
154
+ * @returns The method is returning a boolean value.
144
155
  */
145
- override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
146
- if (keyOrNodeOrEntry === null) return false;
147
- const inserted = super.add(keyOrNodeOrEntry, value);
148
- if (inserted) this._balancePath(keyOrNodeOrEntry);
156
+ override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
157
+ if (keyOrNodeOrEntryOrRawElement === null) return false;
158
+ const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
159
+ if (inserted) this._balancePath(keyOrNodeOrEntryOrRawElement);
149
160
  return inserted;
150
161
  }
151
162
 
@@ -158,16 +169,14 @@ export class AVLTree<
158
169
  * Time Complexity: O(log n)
159
170
  * Space Complexity: O(1)
160
171
  *
161
- * The function overrides the delete method of a binary tree, performs the deletion, and then
162
- * balances the tree if necessary.
172
+ * The function overrides the delete method of a binary tree class and performs additional operations
173
+ * to balance the tree after deletion.
163
174
  * @param identifier - The `identifier` parameter is the value or condition used to identify the
164
- * node(s) to be deleted from the binary tree. It can be of any type and is the return type of the
165
- * `callback` function.
166
- * @param {C} callback - The `callback` parameter is a function that will be called for each node
167
- * that is deleted from the binary tree. It is an optional parameter and if not provided, it will
168
- * default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
169
- * parameter of type `NODE
170
- * @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
175
+ * node(s) to be deleted from the binary tree. It can be of any type that is compatible with the
176
+ * binary tree's node type.
177
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
178
+ * node should be deleted or not. It is optional and has a default value of `this._DEFAULT_CALLBACK`.
179
+ * @returns The method is returning an array of BinaryTreeDeleteResult<NODE> objects.
171
180
  */
172
181
  override delete<C extends BTNCallback<NODE>>(
173
182
  identifier: ReturnType<C>,
@@ -183,18 +192,26 @@ export class AVLTree<
183
192
  }
184
193
 
185
194
  /**
186
- * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
187
- * tree.
188
- * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
189
- * needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
190
- * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
191
- * node where the values from the source node will be swapped to.
192
- * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
193
- * if either `srcNode` or `destNode` is undefined.
195
+ * Time Complexity: O(1)
196
+ * Space Complexity: O(1)
197
+ */
198
+
199
+ /**
200
+ * Time Complexity: O(1)
201
+ * Space Complexity: O(1)
202
+ *
203
+ * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
204
+ * binary search tree.
205
+ * @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
206
+ * object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
207
+ * @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
208
+ * `R` or an instance of `BSTNKeyOrNode<K, NODE>`.
209
+ * @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
210
+ * `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
194
211
  */
195
212
  protected override _swapProperties(
196
- srcNode: BSTNKeyOrNode<K, NODE>,
197
- destNode: BSTNKeyOrNode<K, NODE>
213
+ srcNode: R | BSTNKeyOrNode<K, NODE>,
214
+ destNode: R | BSTNKeyOrNode<K, NODE>
198
215
  ): NODE | undefined {
199
216
  const srcNodeEnsured = this.ensureNode(srcNode);
200
217
  const destNodeEnsured = this.ensureNode(destNode);
@@ -230,7 +247,8 @@ export class AVLTree<
230
247
  * Space Complexity: O(1)
231
248
  *
232
249
  * The function calculates the balance factor of a node in a binary tree.
233
- * @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
250
+ * @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a
251
+ * binary tree data structure.
234
252
  * @returns the balance factor of a given node. The balance factor is calculated by subtracting the
235
253
  * height of the left subtree from the height of the right subtree.
236
254
  */
@@ -275,7 +293,7 @@ export class AVLTree<
275
293
  * Time Complexity: O(1)
276
294
  * Space Complexity: O(1)
277
295
  *
278
- * The function `_balanceLL` performs a left-left rotation to balance a binary tree.
296
+ * The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
279
297
  * @param {NODE} A - A is a node in a binary tree.
280
298
  */
281
299
  protected _balanceLL(A: NODE): void {
@@ -470,10 +488,10 @@ export class AVLTree<
470
488
  *
471
489
  * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
472
490
  * to restore balance in an AVL tree after inserting a node.
473
- * @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
474
- * AVL tree that needs to be balanced.
491
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
492
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
475
493
  */
476
- protected _balancePath(node: KeyOrNodeOrEntry<K, V, NODE>): void {
494
+ protected _balancePath(node: R | KeyOrNodeOrEntry<K, V, NODE>): void {
477
495
  node = this.ensureNode(node);
478
496
  const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
479
497
  for (let i = 0; i < path.length; i++) {
@@ -514,13 +532,22 @@ export class AVLTree<
514
532
  }
515
533
 
516
534
  /**
517
- * The function replaces an old node with a new node while preserving the height of the old node.
518
- * @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace with the
519
- * `newNode`.
535
+ * Time Complexity: O(1)
536
+ * Space Complexity: O(1)
537
+ */
538
+
539
+ /**
540
+ * Time Complexity: O(1)
541
+ * Space Complexity: O(1)
542
+ *
543
+ * The function replaces an old node with a new node and sets the height of the new node to be the
544
+ * same as the old node.
545
+ * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
546
+ * the data structure.
520
547
  * @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
521
548
  * the data structure.
522
- * @returns the result of calling the `_replaceNode` method on the superclass, passing in the
523
- * `oldNode` and `newNode` as arguments.
549
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
550
+ * superclass, with the `oldNode` and `newNode` as arguments.
524
551
  */
525
552
  protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
526
553
  newNode.height = oldNode.height;