avl-tree-typed 1.49.5 → 1.49.7

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 (100) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
  2. package/dist/data-structures/binary-tree/avl-tree.js +55 -49
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
  4. package/dist/data-structures/binary-tree/binary-tree.js +194 -153
  5. package/dist/data-structures/binary-tree/bst.d.ts +83 -71
  6. package/dist/data-structures/binary-tree/bst.js +114 -91
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
  8. package/dist/data-structures/binary-tree/rb-tree.js +62 -59
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
  10. package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
  11. package/dist/data-structures/hash/hash-map.d.ts +24 -27
  12. package/dist/data-structures/hash/hash-map.js +35 -35
  13. package/dist/data-structures/hash/index.d.ts +0 -1
  14. package/dist/data-structures/hash/index.js +0 -1
  15. package/dist/data-structures/heap/heap.d.ts +2 -1
  16. package/dist/data-structures/heap/heap.js +13 -13
  17. package/dist/data-structures/heap/max-heap.js +1 -1
  18. package/dist/data-structures/heap/min-heap.js +1 -1
  19. package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
  20. package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
  21. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
  22. package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
  23. package/dist/data-structures/matrix/matrix.d.ts +2 -7
  24. package/dist/data-structures/matrix/matrix.js +0 -7
  25. package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
  26. package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
  27. package/dist/data-structures/priority-queue/priority-queue.js +1 -1
  28. package/dist/data-structures/queue/deque.d.ts +2 -11
  29. package/dist/data-structures/queue/deque.js +9 -13
  30. package/dist/data-structures/queue/queue.d.ts +13 -13
  31. package/dist/data-structures/queue/queue.js +29 -25
  32. package/dist/data-structures/stack/stack.js +2 -3
  33. package/dist/data-structures/trie/trie.d.ts +2 -2
  34. package/dist/data-structures/trie/trie.js +9 -5
  35. package/dist/interfaces/binary-tree.d.ts +3 -3
  36. package/dist/types/common.d.ts +3 -3
  37. package/dist/types/common.js +2 -2
  38. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  39. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  40. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  41. package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
  42. package/dist/types/data-structures/hash/index.d.ts +0 -1
  43. package/dist/types/data-structures/hash/index.js +0 -1
  44. package/dist/types/data-structures/heap/heap.d.ts +1 -1
  45. package/dist/types/data-structures/linked-list/index.d.ts +1 -0
  46. package/dist/types/data-structures/linked-list/index.js +1 -0
  47. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
  48. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  49. package/dist/types/data-structures/matrix/index.js +1 -0
  50. package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
  51. package/dist/types/data-structures/queue/deque.d.ts +3 -1
  52. package/dist/types/data-structures/trie/trie.d.ts +3 -1
  53. package/package.json +2 -2
  54. package/src/data-structures/binary-tree/avl-tree.ts +58 -53
  55. package/src/data-structures/binary-tree/binary-tree.ts +255 -211
  56. package/src/data-structures/binary-tree/bst.ts +126 -107
  57. package/src/data-structures/binary-tree/rb-tree.ts +66 -64
  58. package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
  59. package/src/data-structures/hash/hash-map.ts +46 -50
  60. package/src/data-structures/hash/index.ts +0 -1
  61. package/src/data-structures/heap/heap.ts +20 -19
  62. package/src/data-structures/heap/max-heap.ts +1 -1
  63. package/src/data-structures/heap/min-heap.ts +1 -1
  64. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  65. package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
  66. package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
  67. package/src/data-structures/matrix/matrix.ts +2 -10
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  70. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  71. package/src/data-structures/queue/deque.ts +11 -15
  72. package/src/data-structures/queue/queue.ts +29 -28
  73. package/src/data-structures/stack/stack.ts +3 -6
  74. package/src/data-structures/trie/trie.ts +10 -11
  75. package/src/interfaces/binary-tree.ts +3 -3
  76. package/src/types/common.ts +3 -3
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
  78. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  79. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
  80. package/src/types/data-structures/hash/hash-map.ts +6 -2
  81. package/src/types/data-structures/hash/index.ts +0 -1
  82. package/src/types/data-structures/heap/heap.ts +1 -1
  83. package/src/types/data-structures/linked-list/index.ts +1 -0
  84. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
  85. package/src/types/data-structures/matrix/index.ts +1 -0
  86. package/src/types/data-structures/matrix/matrix.ts +7 -1
  87. package/src/types/data-structures/queue/deque.ts +1 -1
  88. package/src/types/data-structures/trie/trie.ts +1 -1
  89. package/dist/data-structures/hash/hash-table.d.ts +0 -108
  90. package/dist/data-structures/hash/hash-table.js +0 -281
  91. package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
  92. package/dist/types/data-structures/hash/hash-table.js +0 -2
  93. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
  94. package/dist/types/data-structures/matrix/matrix2d.js +0 -2
  95. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
  96. package/dist/types/data-structures/matrix/vector2d.js +0 -2
  97. package/src/data-structures/hash/hash-table.ts +0 -318
  98. package/src/types/data-structures/hash/hash-table.ts +0 -1
  99. package/src/types/data-structures/matrix/matrix2d.ts +0 -1
  100. package/src/types/data-structures/matrix/vector2d.ts +0 -1
@@ -10,9 +10,8 @@ import {
10
10
  BinaryTreeDeleteResult,
11
11
  BSTNKeyOrNode,
12
12
  BTNCallback,
13
- BTNExemplar,
14
- BTNKeyOrNode,
15
13
  IterationType,
14
+ KeyOrNodeOrEntry,
16
15
  RBTNColor,
17
16
  RBTreeOptions,
18
17
  RedBlackTreeNested,
@@ -53,20 +52,20 @@ export class RedBlackTree<
53
52
 
54
53
  /**
55
54
  * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
56
- * initializes the tree with optional elements and options.
57
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
58
- * objects. It represents the initial elements that will be added to the RBTree during its
55
+ * initializes the tree with optional nodes and options.
56
+ * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
57
+ * objects. It represents the initial nodes that will be added to the RBTree during its
59
58
  * construction. If this parameter is provided, the `addMany` method is called to add all the
60
- * elements to the
59
+ * nodes to the
61
60
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
62
61
  * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
63
62
  * only a subset of the properties defined in the `RBTreeOptions` interface.
64
63
  */
65
- constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>) {
64
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: RBTreeOptions<K>) {
66
65
  super([], options);
67
66
 
68
67
  this._root = this.Sentinel;
69
- if (elements) super.addMany(elements);
68
+ if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
70
69
  }
71
70
 
72
71
  protected _root: N;
@@ -113,62 +112,73 @@ export class RedBlackTree<
113
112
  }
114
113
 
115
114
  /**
116
- * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
117
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
118
- * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
119
- * class.
120
- */
121
- override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
122
- return exemplar instanceof RedBlackTreeNode;
123
- }
124
-
125
- /**
126
- * The function "isNotNodeInstance" checks if a potential key is a K.
127
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
128
- * data type.
129
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
130
- */
131
- override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
132
- return !(potentialKey instanceof RedBlackTreeNode);
133
- }
134
-
135
- /**
136
- * The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
137
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
115
+ * The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
116
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
138
117
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
139
- * `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
118
+ * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
140
119
  * is provided, it will be used when creating the new node. If no value is provided, the new node
141
120
  * @returns a node of type N or undefined.
142
121
  */
143
- override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined {
122
+ override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
144
123
  let node: N | undefined;
145
124
 
146
- if (exemplar === null || exemplar === undefined) {
125
+ if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
147
126
  return;
148
- } else if (this.isNode(exemplar)) {
149
- node = exemplar;
150
- } else if (this.isEntry(exemplar)) {
151
- const [key, value] = exemplar;
127
+ } else if (this.isNode(keyOrNodeOrEntry)) {
128
+ node = keyOrNodeOrEntry;
129
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
130
+ const [key, value] = keyOrNodeOrEntry;
152
131
  if (key === undefined || key === null) {
153
132
  return;
154
133
  } else {
155
134
  node = this.createNode(key, value, RBTNColor.RED);
156
135
  }
157
- } else if (this.isNotNodeInstance(exemplar)) {
158
- node = this.createNode(exemplar, value, RBTNColor.RED);
136
+ } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
137
+ node = this.createNode(keyOrNodeOrEntry, value, RBTNColor.RED);
159
138
  } else {
160
139
  return;
161
140
  }
162
141
  return node;
163
142
  }
164
143
 
144
+ /**
145
+ * The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
146
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
147
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
148
+ * class.
149
+ */
150
+ override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
151
+ return keyOrNodeOrEntry instanceof RedBlackTreeNode;
152
+ }
153
+
165
154
  /**
166
155
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
167
156
  * Space Complexity: O(1)
168
157
  */
169
158
 
159
+ override isRealNode(node: N | undefined): node is N {
160
+ if (node === this.Sentinel || node === undefined) return false;
161
+ return node instanceof RedBlackTreeNode;
162
+ }
163
+
170
164
  /**
171
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
165
+ * The function "isNotNodeInstance" checks if a potential key is a K.
166
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
167
+ * data type.
168
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
169
+ */
170
+ override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
171
+ return !(potentialKey instanceof RedBlackTreeNode);
172
+ }
173
+
174
+ /**
175
+ * Time Complexity: O(log n)
176
+ * Space Complexity: O(1)
177
+ * on average (where n is the number of nodes in the tree)
178
+ */
179
+
180
+ /**
181
+ * Time Complexity: O(log n)
172
182
  * Space Complexity: O(1)
173
183
  *
174
184
  * The `add` function adds a new node to a binary search tree and performs necessary rotations and
@@ -179,9 +189,9 @@ export class RedBlackTree<
179
189
  * being added to the binary search tree.
180
190
  * @returns The method `add` returns either the newly added node (`N`) or `undefined`.
181
191
  */
182
- override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
192
+ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
183
193
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
184
- if (newNode === undefined) return;
194
+ if (newNode === undefined) return false;
185
195
 
186
196
  newNode.left = this.Sentinel;
187
197
  newNode.right = this.Sentinel;
@@ -200,7 +210,7 @@ export class RedBlackTree<
200
210
  if (newNode !== x) {
201
211
  this._replaceNode(x, newNode);
202
212
  }
203
- return;
213
+ return false;
204
214
  }
205
215
  }
206
216
  }
@@ -217,25 +227,27 @@ export class RedBlackTree<
217
227
  if (newNode.parent === undefined) {
218
228
  newNode.color = RBTNColor.BLACK;
219
229
  this._size++;
220
- return;
230
+ return false;
221
231
  }
222
232
 
223
233
  if (newNode.parent.parent === undefined) {
224
234
  this._size++;
225
- return;
235
+ return false;
226
236
  }
227
237
 
228
238
  this._fixInsert(newNode);
229
239
  this._size++;
240
+ return true;
230
241
  }
231
242
 
232
243
  /**
233
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
244
+ * Time Complexity: O(log n)
234
245
  * Space Complexity: O(1)
246
+ * on average (where n is the number of nodes in the tree)
235
247
  */
236
248
 
237
249
  /**
238
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
250
+ * Time Complexity: O(log n)
239
251
  * Space Complexity: O(1)
240
252
  *
241
253
  * The `delete` function removes a node from a binary tree based on a given identifier and updates
@@ -310,16 +322,6 @@ export class RedBlackTree<
310
322
  return ans;
311
323
  }
312
324
 
313
- /**
314
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
315
- * Space Complexity: O(1)
316
- */
317
-
318
- override isRealNode(node: N | undefined): node is N {
319
- if (node === this.Sentinel || node === undefined) return false;
320
- return node instanceof RedBlackTreeNode;
321
- }
322
-
323
325
  getNode<C extends BTNCallback<N, K>>(
324
326
  identifier: K,
325
327
  callback?: C,
@@ -342,12 +344,12 @@ export class RedBlackTree<
342
344
  ): N | undefined;
343
345
 
344
346
  /**
345
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
347
+ * Time Complexity: O(log n)
346
348
  * Space Complexity: O(1)
347
349
  */
348
350
 
349
351
  /**
350
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
352
+ * Time Complexity: O(log n)
351
353
  * Space Complexity: O(1)
352
354
  *
353
355
  * The function `getNode` retrieves a single node from a binary tree based on a given identifier and
@@ -407,7 +409,7 @@ export class RedBlackTree<
407
409
  }
408
410
 
409
411
  /**
410
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
412
+ * Time Complexity: O(1)
411
413
  * Space Complexity: O(1)
412
414
  */
413
415
 
@@ -489,12 +491,12 @@ export class RedBlackTree<
489
491
  }
490
492
 
491
493
  /**
492
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
494
+ * Time Complexity: O(log n)
493
495
  * Space Complexity: O(1)
494
496
  */
495
497
 
496
498
  /**
497
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
499
+ * Time Complexity: O(log n)
498
500
  * Space Complexity: O(1)
499
501
  *
500
502
  * The function `_fixDelete` is used to fix the red-black tree after a node deletion.
@@ -585,12 +587,12 @@ export class RedBlackTree<
585
587
  }
586
588
 
587
589
  /**
588
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
590
+ * Time Complexity: O(log n)
589
591
  * Space Complexity: O(1)
590
592
  */
591
593
 
592
594
  /**
593
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
595
+ * Time Complexity: O(log n)
594
596
  * Space Complexity: O(1)
595
597
  *
596
598
  * The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
@@ -9,8 +9,7 @@ import type {
9
9
  BinaryTreeDeleteResult,
10
10
  BSTNKeyOrNode,
11
11
  BTNCallback,
12
- BTNExemplar,
13
- BTNKeyOrNode,
12
+ KeyOrNodeOrEntry,
14
13
  TreeMultimapNested,
15
14
  TreeMultimapNodeNested,
16
15
  TreeMultimapOptions
@@ -53,9 +52,9 @@ export class TreeMultimap<
53
52
  >
54
53
  extends AVLTree<K, V, N, TREE>
55
54
  implements IBinaryTree<K, V, N, TREE> {
56
- constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) {
55
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: TreeMultimapOptions<K>) {
57
56
  super([], options);
58
- if (elements) this.addMany(elements);
57
+ if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
59
58
  }
60
59
 
61
60
  private _count = 0;
@@ -89,28 +88,8 @@ export class TreeMultimap<
89
88
  }
90
89
 
91
90
  /**
92
- * The function checks if an exemplar is an instance of the TreeMultimapNode class.
93
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
94
- * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
95
- * class.
96
- */
97
- override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
98
- return exemplar instanceof TreeMultimapNode;
99
- }
100
-
101
- /**
102
- * The function "isNotNodeInstance" checks if a potential key is a K.
103
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
104
- * data type.
105
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
106
- */
107
- override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
108
- return !(potentialKey instanceof TreeMultimapNode);
109
- }
110
-
111
- /**
112
- * The function `exemplarToNode` converts an exemplar object into a node object.
113
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
91
+ * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
92
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
114
93
  * can be one of the following:
115
94
  * @param {V} [value] - The `value` parameter is an optional argument that represents the value
116
95
  * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
@@ -119,21 +98,21 @@ export class TreeMultimap<
119
98
  * times the value should be added to the node. If not provided, it defaults to 1.
120
99
  * @returns a node of type `N` or `undefined`.
121
100
  */
122
- override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined {
101
+ override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): N | undefined {
123
102
  let node: N | undefined;
124
- if (exemplar === undefined || exemplar === null) {
103
+ if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
125
104
  return;
126
- } else if (this.isNode(exemplar)) {
127
- node = exemplar;
128
- } else if (this.isEntry(exemplar)) {
129
- const [key, value] = exemplar;
105
+ } else if (this.isNode(keyOrNodeOrEntry)) {
106
+ node = keyOrNodeOrEntry;
107
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
108
+ const [key, value] = keyOrNodeOrEntry;
130
109
  if (key === undefined || key === null) {
131
110
  return;
132
111
  } else {
133
112
  node = this.createNode(key, value, count);
134
113
  }
135
- } else if (this.isNotNodeInstance(exemplar)) {
136
- node = this.createNode(exemplar, value, count);
114
+ } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
115
+ node = this.createNode(keyOrNodeOrEntry, value, count);
137
116
  } else {
138
117
  return;
139
118
  }
@@ -141,13 +120,34 @@ export class TreeMultimap<
141
120
  }
142
121
 
143
122
  /**
144
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
145
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
123
+ * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
124
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
125
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
126
+ * class.
146
127
  */
128
+ override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
129
+ return keyOrNodeOrEntry instanceof TreeMultimapNode;
130
+ }
131
+
132
+ /**
133
+ * The function "isNotNodeInstance" checks if a potential key is a K.
134
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
135
+ * data type.
136
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
137
+ */
138
+ override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
139
+ return !(potentialKey instanceof TreeMultimapNode);
140
+ }
147
141
 
148
142
  /**
149
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
150
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
143
+ * Time Complexity: O(log n)
144
+ * Space Complexity: O(1)
145
+ * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
146
+ */
147
+
148
+ /**
149
+ * Time Complexity: O(log n)
150
+ * Space Complexity: O(1)
151
151
  *
152
152
  * The function overrides the add method of a binary tree node and adds a new node to the tree.
153
153
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
@@ -161,26 +161,27 @@ export class TreeMultimap<
161
161
  * @returns The method is returning either the newly inserted node or `undefined` if the insertion
162
162
  * was not successful.
163
163
  */
164
- override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined {
164
+ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): boolean {
165
165
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
166
- if (newNode === undefined) return;
166
+ if (newNode === undefined) return false;
167
167
 
168
168
  const orgNodeCount = newNode?.count || 0;
169
169
  const inserted = super.add(newNode);
170
170
  if (inserted) {
171
171
  this._count += orgNodeCount;
172
172
  }
173
- return inserted;
173
+ return true;
174
174
  }
175
175
 
176
176
  /**
177
- * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
178
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
177
+ * Time Complexity: O(k log n)
178
+ * Space Complexity: O(1)
179
+ * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
179
180
  */
180
181
 
181
182
  /**
182
- * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
183
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
183
+ * Time Complexity: O(k log n)
184
+ * Space Complexity: O(1)
184
185
  *
185
186
  * The function overrides the addMany method to add multiple keys, nodes, or entries to a data
186
187
  * structure.
@@ -188,18 +189,19 @@ export class TreeMultimap<
188
189
  * either keys, nodes, or entries.
189
190
  * @returns The method is returning an array of type `N | undefined`.
190
191
  */
191
- override addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>): (N | undefined)[] {
192
+ override addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>): boolean[] {
192
193
  return super.addMany(keysOrNodesOrEntries);
193
194
  }
194
195
 
195
196
  /**
196
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
197
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
197
+ * Time Complexity: O(n log n)
198
+ * Space Complexity: O(n)
199
+ * logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
198
200
  */
199
201
 
200
202
  /**
201
- * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
202
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
203
+ * Time Complexity: O(n log n)
204
+ * Space Complexity: O(n)
203
205
  *
204
206
  * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
205
207
  * tree using either a recursive or iterative approach.
@@ -247,13 +249,14 @@ export class TreeMultimap<
247
249
  }
248
250
 
249
251
  /**
250
- * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
251
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
252
+ * Time Complexity: O(k log n)
253
+ * Space Complexity: O(1)
254
+ * logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
252
255
  */
253
256
 
254
257
  /**
255
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
256
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
258
+ * Time Complexity: O(k log n)
259
+ * Space Complexity: O(1)
257
260
  *
258
261
  * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
259
262
  * account the count of the node and balancing the tree if necessary.
@@ -331,11 +334,14 @@ export class TreeMultimap<
331
334
  }
332
335
 
333
336
  /**
334
- * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
335
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
337
+ * Time Complexity: O(1)
338
+ * Space Complexity: O(1)
336
339
  */
337
340
 
338
341
  /**
342
+ * Time Complexity: O(1)
343
+ * Space Complexity: O(1)
344
+ *
339
345
  * The clear() function clears the contents of a data structure and sets the count to zero.
340
346
  */
341
347
  override clear() {
@@ -5,34 +5,35 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
8
+ import type {
9
+ EntryCallback,
10
+ HashMapLinkedNode,
11
+ HashMapOptions,
12
+ HashMapStoreItem,
13
+ LinkedHashMapOptions
14
+ } from '../../types';
9
15
  import { IterableEntryBase } from '../base';
10
16
  import { isWeakKey, rangeCheck } from '../../utils';
11
17
 
12
18
  /**
13
19
  * 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
14
- * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
20
+ * 2. Fast Lookup: It's used when you need to quickly find, insert, or delete entries based on a key.
15
21
  * 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
16
- * 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
22
+ * 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
17
23
  */
18
24
  export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
19
25
  protected _store: { [key: string]: HashMapStoreItem<K, V> } = {};
20
26
  protected _objMap: Map<object, V> = new Map();
21
27
 
22
28
  /**
23
- * The constructor function initializes a new instance of a class with optional elements and options.
24
- * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
29
+ * The constructor function initializes a new instance of a class with optional entries and options.
30
+ * @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
25
31
  * is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
26
32
  * key-value pairs.
27
33
  * @param [options] - The `options` parameter is an optional object that can contain additional
28
34
  * configuration options for the constructor. In this case, it has one property:
29
35
  */
30
- constructor(
31
- elements: Iterable<[K, V]> = [],
32
- options?: {
33
- hashFn: (key: K) => string;
34
- }
35
- ) {
36
+ constructor(entries: Iterable<[K, V]> = [], options?: HashMapOptions<K>) {
36
37
  super();
37
38
  if (options) {
38
39
  const { hashFn } = options;
@@ -40,8 +41,8 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
40
41
  this._hashFn = hashFn;
41
42
  }
42
43
  }
43
- if (elements) {
44
- this.setMany(elements);
44
+ if (entries) {
45
+ this.setMany(entries);
45
46
  }
46
47
  }
47
48
 
@@ -88,12 +89,12 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
88
89
 
89
90
  /**
90
91
  * The function "setMany" sets multiple key-value pairs in a map.
91
- * @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
92
- * key-value pair is represented as an array with two elements: the key and the value.
92
+ * @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
93
+ * key-value pair is represented as an array with two entries: the key and the value.
93
94
  */
94
- setMany(elements: Iterable<[K, V]>): boolean[] {
95
+ setMany(entries: Iterable<[K, V]>): boolean[] {
95
96
  const results: boolean[] = [];
96
- for (const [key, value] of elements) results.push(this.set(key, value));
97
+ for (const [key, value] of entries) results.push(this.set(key, value));
97
98
  return results;
98
99
  }
99
100
 
@@ -214,10 +215,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
214
215
  return filteredMap;
215
216
  }
216
217
 
217
- print(): void {
218
- console.log([...this.entries()]);
219
- }
220
-
221
218
  put(key: K, value: V): boolean {
222
219
  return this.set(key, value);
223
220
  }
@@ -261,8 +258,8 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
261
258
  }
262
259
 
263
260
  /**
264
- * 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which elements are inserted. Therefore, when you traverse it, elements will be returned in the order they were inserted into the map.
265
- * 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of elements through the linked list.
261
+ * 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which entries are inserted. Therefore, when you traverse it, entries will be returned in the order they were inserted into the map.
262
+ * 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of entries through the linked list.
266
263
  * 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
267
264
  */
268
265
  export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
@@ -271,25 +268,20 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
271
268
  protected _head: HashMapLinkedNode<K, V | undefined>;
272
269
  protected _tail: HashMapLinkedNode<K, V | undefined>;
273
270
  protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
274
- protected _hashFn: (key: K) => string;
275
- protected _objHashFn: (key: K) => object;
276
-
277
- constructor(
278
- elements?: Iterable<[K, V]>,
279
- options: HashMapOptions<K> = {
280
- hashFn: (key: K) => String(key),
281
- objHashFn: (key: K) => <object>key
282
- }
283
- ) {
271
+
272
+ constructor(entries?: Iterable<[K, V]>, options?: LinkedHashMapOptions<K>) {
284
273
  super();
285
274
  this._sentinel = <HashMapLinkedNode<K, V>>{};
286
275
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
287
276
 
288
- const { hashFn, objHashFn } = options;
289
- this._hashFn = hashFn;
290
- this._objHashFn = objHashFn;
291
- if (elements) {
292
- for (const el of elements) {
277
+ if (options) {
278
+ const { hashFn, objHashFn } = options;
279
+ if (hashFn) this._hashFn = hashFn;
280
+ if (objHashFn) this._objHashFn = objHashFn;
281
+ }
282
+
283
+ if (entries) {
284
+ for (const el of entries) {
293
285
  this.set(el[0], el[1]);
294
286
  }
295
287
  }
@@ -547,7 +539,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
547
539
  * Time Complexity: O(1)
548
540
  * Space Complexity: O(1)
549
541
  *
550
- * The `clear` function clears all the elements in a data structure and resets its properties.
542
+ * The `clear` function clears all the entries in a data structure and resets its properties.
551
543
  */
552
544
  clear(): void {
553
545
  this._noObjMap = {};
@@ -564,11 +556,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
564
556
  return cloned;
565
557
  }
566
558
 
567
- /**
568
- * Time Complexity: O(n)
569
- * Space Complexity: O(n)
570
- */
571
-
572
559
  /**
573
560
  * Time Complexity: O(n)
574
561
  * Space Complexity: O(n)
@@ -596,11 +583,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
596
583
  return filteredMap;
597
584
  }
598
585
 
599
- /**
600
- * Time Complexity: O(n)
601
- * Space Complexity: O(n)
602
- */
603
-
604
586
  /**
605
587
  * Time Complexity: O(n)
606
588
  * Space Complexity: O(n)
@@ -629,12 +611,26 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
629
611
  return mappedMap;
630
612
  }
631
613
 
614
+ /**
615
+ * Time Complexity: O(n)
616
+ * Space Complexity: O(n)
617
+ */
618
+
632
619
  put(key: K, value: V): boolean {
633
620
  return this.set(key, value);
634
621
  }
635
622
 
636
623
  /**
637
- * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
624
+ * Time Complexity: O(n)
625
+ * Space Complexity: O(n)
626
+ */
627
+
628
+ protected _hashFn: (key: K) => string = (key: K) => String(key);
629
+
630
+ protected _objHashFn: (key: K) => object = (key: K) => <object>key;
631
+
632
+ /**
633
+ * Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
638
634
  * Space Complexity: O(1)
639
635
  *
640
636
  * The above function is an iterator that yields key-value pairs from a linked list.
@@ -1,2 +1 @@
1
- export * from './hash-table';
2
1
  export * from './hash-map';