data-structure-typed 1.47.9 → 1.48.1

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 (78) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +49 -48
  3. package/benchmark/report.html +16 -16
  4. package/benchmark/report.json +172 -292
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js +8 -0
  7. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +52 -0
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js +106 -28
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/bst.d.ts +13 -0
  13. package/dist/cjs/data-structures/binary-tree/bst.js +41 -21
  14. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js +54 -31
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
  19. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +28 -0
  20. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +60 -21
  21. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  22. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  24. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  25. package/dist/cjs/data-structures/hash/hash-map.d.ts +173 -16
  26. package/dist/cjs/data-structures/hash/hash-map.js +373 -37
  27. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  28. package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
  29. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  30. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  31. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  32. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  33. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  34. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  35. package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
  36. package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
  37. package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
  38. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  39. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  40. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  41. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  42. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  43. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  44. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  45. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -0
  46. package/dist/cjs/utils/utils.js.map +1 -1
  47. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
  48. package/dist/mjs/data-structures/binary-tree/avl-tree.js +8 -0
  49. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +52 -0
  50. package/dist/mjs/data-structures/binary-tree/binary-tree.js +106 -28
  51. package/dist/mjs/data-structures/binary-tree/bst.d.ts +13 -0
  52. package/dist/mjs/data-structures/binary-tree/bst.js +41 -21
  53. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
  54. package/dist/mjs/data-structures/binary-tree/rb-tree.js +54 -31
  55. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +28 -0
  56. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +60 -21
  57. package/dist/mjs/data-structures/hash/hash-map.d.ts +173 -16
  58. package/dist/mjs/data-structures/hash/hash-map.js +369 -35
  59. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -0
  60. package/dist/umd/data-structure-typed.js +615 -120
  61. package/dist/umd/data-structure-typed.min.js +2 -2
  62. package/dist/umd/data-structure-typed.min.js.map +1 -1
  63. package/package.json +2 -2
  64. package/src/data-structures/binary-tree/avl-tree.ts +9 -0
  65. package/src/data-structures/binary-tree/binary-tree.ts +109 -24
  66. package/src/data-structures/binary-tree/bst.ts +38 -19
  67. package/src/data-structures/binary-tree/rb-tree.ts +55 -31
  68. package/src/data-structures/binary-tree/tree-multimap.ts +60 -17
  69. package/src/data-structures/hash/hash-map.ts +400 -40
  70. package/src/types/data-structures/hash/hash-map.ts +2 -0
  71. package/test/integration/avl-tree.test.ts +2 -2
  72. package/test/integration/bst.test.ts +21 -25
  73. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +17 -12
  74. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +16 -0
  75. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +16 -1
  76. package/test/unit/data-structures/binary-tree/bst.test.ts +16 -0
  77. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +82 -1
  78. package/test/unit/data-structures/hash/hash-map.test.ts +312 -18
@@ -119,6 +119,51 @@ export class BinaryTree {
119
119
  createTree(options) {
120
120
  return new BinaryTree([], { iterationType: this.iterationType, ...options });
121
121
  }
122
+ /**
123
+ * The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
124
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
125
+ * @returns a boolean value indicating whether the exemplar is an instance of the class N.
126
+ */
127
+ isNode(exemplar) {
128
+ return exemplar instanceof BinaryTreeNode;
129
+ }
130
+ /**
131
+ * The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
132
+ * object.
133
+ * @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
134
+ * function. It can be any type.
135
+ * @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
136
+ */
137
+ exemplarToNode(exemplar) {
138
+ if (exemplar === undefined)
139
+ return;
140
+ let node;
141
+ if (exemplar === null) {
142
+ node = null;
143
+ }
144
+ else if (this.isEntry(exemplar)) {
145
+ const [key, value] = exemplar;
146
+ if (key === undefined) {
147
+ return;
148
+ }
149
+ else if (key === null) {
150
+ node = null;
151
+ }
152
+ else {
153
+ node = this.createNode(key, value);
154
+ }
155
+ }
156
+ else if (this.isNode(exemplar)) {
157
+ node = exemplar;
158
+ }
159
+ else if (this.isNodeKey(exemplar)) {
160
+ node = this.createNode(exemplar);
161
+ }
162
+ else {
163
+ return;
164
+ }
165
+ return node;
166
+ }
122
167
  /**
123
168
  * The function checks if a given value is an entry in a binary tree node.
124
169
  * @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
@@ -141,7 +186,10 @@ export class BinaryTree {
141
186
  * @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
142
187
  */
143
188
  add(keyOrNodeOrEntry) {
144
- let inserted, needInsert;
189
+ let inserted;
190
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry);
191
+ if (newNode === undefined)
192
+ return;
145
193
  const _bfs = (root, newNode) => {
146
194
  const queue = new Queue([root]);
147
195
  while (queue.size > 0) {
@@ -159,36 +207,12 @@ export class BinaryTree {
159
207
  queue.push(cur.right);
160
208
  }
161
209
  };
162
- if (keyOrNodeOrEntry === null) {
163
- needInsert = null;
164
- }
165
- else if (this.isNodeKey(keyOrNodeOrEntry)) {
166
- needInsert = this.createNode(keyOrNodeOrEntry);
167
- }
168
- else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
169
- needInsert = keyOrNodeOrEntry;
170
- }
171
- else if (this.isEntry(keyOrNodeOrEntry)) {
172
- const [key, value] = keyOrNodeOrEntry;
173
- if (key === undefined) {
174
- return;
175
- }
176
- else if (key === null) {
177
- needInsert = null;
178
- }
179
- else {
180
- needInsert = this.createNode(key, value);
181
- }
182
- }
183
- else {
184
- return;
185
- }
186
210
  if (this.root) {
187
- inserted = _bfs(this.root, needInsert);
211
+ inserted = _bfs(this.root, newNode);
188
212
  }
189
213
  else {
190
- this._setRoot(needInsert);
191
- if (needInsert) {
214
+ this._setRoot(newNode);
215
+ if (newNode) {
192
216
  this._size = 1;
193
217
  }
194
218
  else {
@@ -1379,6 +1403,60 @@ export class BinaryTree {
1379
1403
  }
1380
1404
  return ans;
1381
1405
  }
1406
+ /**
1407
+ * Time complexity: O(n)
1408
+ * Space complexity: O(n)
1409
+ */
1410
+ /**
1411
+ * Time complexity: O(n)
1412
+ * Space complexity: O(n)
1413
+ *
1414
+ * The function "keys" returns an array of keys from a given object.
1415
+ * @returns an array of BTNKey objects.
1416
+ */
1417
+ keys() {
1418
+ const keys = [];
1419
+ for (const entry of this) {
1420
+ keys.push(entry[0]);
1421
+ }
1422
+ return keys;
1423
+ }
1424
+ /**
1425
+ * Time complexity: O(n)
1426
+ * Space complexity: O(n)
1427
+ */
1428
+ /**
1429
+ * Time complexity: O(n)
1430
+ * Space complexity: O(n)
1431
+ *
1432
+ * The function "values" returns an array of values from a map-like object.
1433
+ * @returns The `values()` method is returning an array of values (`V`) from the entries in the
1434
+ * object.
1435
+ */
1436
+ values() {
1437
+ const values = [];
1438
+ for (const entry of this) {
1439
+ values.push(entry[1]);
1440
+ }
1441
+ return values;
1442
+ }
1443
+ /**
1444
+ * Time complexity: O(n)
1445
+ * Space complexity: O(n)
1446
+ */
1447
+ /**
1448
+ * Time complexity: O(n)
1449
+ * Space complexity: O(n)
1450
+ *
1451
+ * The `clone` function creates a new tree object and copies all the nodes from the original tree to
1452
+ * the new tree.
1453
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
1454
+ */
1455
+ clone() {
1456
+ const cloned = this.createTree();
1457
+ this.bfs(node => cloned.add([node.key, node.value]));
1458
+ return cloned;
1459
+ }
1382
1460
  /**
1383
1461
  * Time complexity: O(n)
1384
1462
  * Space complexity: O(1)
@@ -72,6 +72,19 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
72
72
  * @returns a new instance of the BST class with the specified options.
73
73
  */
74
74
  createTree(options?: Partial<BSTOptions>): TREE;
75
+ /**
76
+ * The function checks if an exemplar is an instance of BSTNode.
77
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
78
+ * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
79
+ */
80
+ isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
81
+ /**
82
+ * The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
83
+ * is valid, otherwise it returns undefined.
84
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
85
+ * @returns a variable `node` which is of type `N` or `undefined`.
86
+ */
87
+ exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
75
88
  /**
76
89
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
77
90
  * Space Complexity: O(1) - Constant space is used.
@@ -103,6 +103,45 @@ export class BST extends BinaryTree {
103
103
  comparator: this.comparator, ...options
104
104
  });
105
105
  }
106
+ /**
107
+ * The function checks if an exemplar is an instance of BSTNode.
108
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
109
+ * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
110
+ */
111
+ isNode(exemplar) {
112
+ return exemplar instanceof BSTNode;
113
+ }
114
+ /**
115
+ * The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
116
+ * is valid, otherwise it returns undefined.
117
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
118
+ * @returns a variable `node` which is of type `N` or `undefined`.
119
+ */
120
+ exemplarToNode(exemplar) {
121
+ let node;
122
+ if (exemplar === null || exemplar === undefined) {
123
+ return;
124
+ }
125
+ else if (this.isNode(exemplar)) {
126
+ node = exemplar;
127
+ }
128
+ else if (this.isEntry(exemplar)) {
129
+ const [key, value] = exemplar;
130
+ if (key === undefined || key === null) {
131
+ return;
132
+ }
133
+ else {
134
+ node = this.createNode(key, value);
135
+ }
136
+ }
137
+ else if (this.isNodeKey(exemplar)) {
138
+ node = this.createNode(exemplar);
139
+ }
140
+ else {
141
+ return;
142
+ }
143
+ return node;
144
+ }
106
145
  /**
107
146
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
108
147
  * Space Complexity: O(1) - Constant space is used.
@@ -118,28 +157,9 @@ export class BST extends BinaryTree {
118
157
  * (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
119
158
  */
120
159
  add(keyOrNodeOrEntry) {
121
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
122
- return undefined;
123
- }
124
- let newNode;
125
- if (keyOrNodeOrEntry instanceof BSTNode) {
126
- newNode = keyOrNodeOrEntry;
127
- }
128
- else if (this.isNodeKey(keyOrNodeOrEntry)) {
129
- newNode = this.createNode(keyOrNodeOrEntry);
130
- }
131
- else if (this.isEntry(keyOrNodeOrEntry)) {
132
- const [key, value] = keyOrNodeOrEntry;
133
- if (key === undefined || key === null) {
134
- return;
135
- }
136
- else {
137
- newNode = this.createNode(key, value);
138
- }
139
- }
140
- else {
160
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry);
161
+ if (newNode === undefined)
141
162
  return;
142
- }
143
163
  if (this.root === undefined) {
144
164
  this._setRoot(newNode);
145
165
  this._size++;
@@ -58,6 +58,22 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
58
58
  * @returns a new instance of a RedBlackTree object.
59
59
  */
60
60
  createTree(options?: RBTreeOptions): TREE;
61
+ /**
62
+ * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
63
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
64
+ * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
65
+ * class.
66
+ */
67
+ isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
68
+ /**
69
+ * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
70
+ * otherwise it returns undefined.
71
+ * @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
72
+ * node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
73
+ * that is not a valid exemplar.
74
+ * @returns a variable `node` which is of type `N | undefined`.
75
+ */
76
+ exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
61
77
  /**
62
78
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
63
79
  * Space Complexity: O(1)
@@ -78,28 +78,32 @@ export class RedBlackTree extends BST {
78
78
  });
79
79
  }
80
80
  /**
81
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
82
- * Space Complexity: O(1)
81
+ * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
82
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
83
+ * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
84
+ * class.
83
85
  */
86
+ isNode(exemplar) {
87
+ return exemplar instanceof RedBlackTreeNode;
88
+ }
84
89
  /**
85
- * The function adds a node to a Red-Black Tree data structure.
86
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
87
- * @returns The method `add` returns either an instance of `N` (the node that was added) or
88
- * `undefined`.
90
+ * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
91
+ * otherwise it returns undefined.
92
+ * @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
93
+ * node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
94
+ * that is not a valid exemplar.
95
+ * @returns a variable `node` which is of type `N | undefined`.
89
96
  */
90
- add(keyOrNodeOrEntry) {
97
+ exemplarToNode(exemplar) {
91
98
  let node;
92
- if (this.isNodeKey(keyOrNodeOrEntry)) {
93
- node = this.createNode(keyOrNodeOrEntry, undefined, RBTNColor.RED);
94
- }
95
- else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
96
- node = keyOrNodeOrEntry;
97
- }
98
- else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
99
+ if (exemplar === null || exemplar === undefined) {
99
100
  return;
100
101
  }
101
- else if (this.isEntry(keyOrNodeOrEntry)) {
102
- const [key, value] = keyOrNodeOrEntry;
102
+ else if (this.isNode(exemplar)) {
103
+ node = exemplar;
104
+ }
105
+ else if (this.isEntry(exemplar)) {
106
+ const [key, value] = exemplar;
103
107
  if (key === undefined || key === null) {
104
108
  return;
105
109
  }
@@ -107,50 +111,69 @@ export class RedBlackTree extends BST {
107
111
  node = this.createNode(key, value, RBTNColor.RED);
108
112
  }
109
113
  }
114
+ else if (this.isNodeKey(exemplar)) {
115
+ node = this.createNode(exemplar, undefined, RBTNColor.RED);
116
+ }
110
117
  else {
111
118
  return;
112
119
  }
113
- node.left = this.Sentinel;
114
- node.right = this.Sentinel;
120
+ return node;
121
+ }
122
+ /**
123
+ * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
124
+ * Space Complexity: O(1)
125
+ */
126
+ /**
127
+ * The function adds a node to a Red-Black Tree data structure.
128
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
129
+ * @returns The method `add` returns either an instance of `N` (the node that was added) or
130
+ * `undefined`.
131
+ */
132
+ add(keyOrNodeOrEntry) {
133
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry);
134
+ if (newNode === undefined)
135
+ return;
136
+ newNode.left = this.Sentinel;
137
+ newNode.right = this.Sentinel;
115
138
  let y = undefined;
116
139
  let x = this.root;
117
140
  while (x !== this.Sentinel) {
118
141
  y = x;
119
142
  if (x) {
120
- if (node.key < x.key) {
143
+ if (newNode.key < x.key) {
121
144
  x = x.left;
122
145
  }
123
- else if (node.key > x.key) {
146
+ else if (newNode.key > x.key) {
124
147
  x = x?.right;
125
148
  }
126
149
  else {
127
- if (node !== x) {
128
- this._replaceNode(x, node);
150
+ if (newNode !== x) {
151
+ this._replaceNode(x, newNode);
129
152
  }
130
153
  return;
131
154
  }
132
155
  }
133
156
  }
134
- node.parent = y;
157
+ newNode.parent = y;
135
158
  if (y === undefined) {
136
- this._setRoot(node);
159
+ this._setRoot(newNode);
137
160
  }
138
- else if (node.key < y.key) {
139
- y.left = node;
161
+ else if (newNode.key < y.key) {
162
+ y.left = newNode;
140
163
  }
141
164
  else {
142
- y.right = node;
165
+ y.right = newNode;
143
166
  }
144
- if (node.parent === undefined) {
145
- node.color = RBTNColor.BLACK;
167
+ if (newNode.parent === undefined) {
168
+ newNode.color = RBTNColor.BLACK;
146
169
  this._size++;
147
170
  return;
148
171
  }
149
- if (node.parent.parent === undefined) {
172
+ if (newNode.parent.parent === undefined) {
150
173
  this._size++;
151
174
  return;
152
175
  }
153
- this._fixInsert(node);
176
+ this._fixInsert(newNode);
154
177
  this._size++;
155
178
  }
156
179
  /**
@@ -41,6 +41,22 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
41
41
  */
42
42
  createNode(key: BTNKey, value?: V, count?: number): N;
43
43
  createTree(options?: TreeMultimapOptions): TREE;
44
+ /**
45
+ * The function checks if an exemplar is an instance of the TreeMultimapNode class.
46
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
47
+ * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
48
+ * class.
49
+ */
50
+ isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
51
+ /**
52
+ * The function `exemplarToNode` converts an exemplar object into a node object.
53
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
54
+ * the value type and `N` represents the node type.
55
+ * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
56
+ * times the node should be created. If not provided, it defaults to 1.
57
+ * @returns a value of type `N` (the generic type parameter) or `undefined`.
58
+ */
59
+ exemplarToNode(exemplar: BTNodeExemplar<V, N>, count?: number): N | undefined;
44
60
  /**
45
61
  * 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.
46
62
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -121,6 +137,18 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
121
137
  * The clear() function clears the contents of a data structure and sets the count to zero.
122
138
  */
123
139
  clear(): void;
140
+ /**
141
+ * Time complexity: O(n)
142
+ * Space complexity: O(n)
143
+ */
144
+ /**
145
+ * Time complexity: O(n)
146
+ * Space complexity: O(n)
147
+ *
148
+ * The `clone` function creates a deep copy of a tree object.
149
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
150
+ */
151
+ clone(): TREE;
124
152
  /**
125
153
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
126
154
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -51,6 +51,48 @@ export class TreeMultimap extends AVLTree {
51
51
  comparator: this.comparator, ...options
52
52
  });
53
53
  }
54
+ /**
55
+ * The function checks if an exemplar is an instance of the TreeMultimapNode class.
56
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
57
+ * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
58
+ * class.
59
+ */
60
+ isNode(exemplar) {
61
+ return exemplar instanceof TreeMultimapNode;
62
+ }
63
+ /**
64
+ * The function `exemplarToNode` converts an exemplar object into a node object.
65
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
66
+ * the value type and `N` represents the node type.
67
+ * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
68
+ * times the node should be created. If not provided, it defaults to 1.
69
+ * @returns a value of type `N` (the generic type parameter) or `undefined`.
70
+ */
71
+ exemplarToNode(exemplar, count = 1) {
72
+ let node;
73
+ if (exemplar === undefined || exemplar === null) {
74
+ return;
75
+ }
76
+ else if (this.isNode(exemplar)) {
77
+ node = exemplar;
78
+ }
79
+ else if (this.isEntry(exemplar)) {
80
+ const [key, value] = exemplar;
81
+ if (key === undefined || key === null) {
82
+ return;
83
+ }
84
+ else {
85
+ node = this.createNode(key, value, count);
86
+ }
87
+ }
88
+ else if (this.isNodeKey(exemplar)) {
89
+ node = this.createNode(exemplar, undefined, count);
90
+ }
91
+ else {
92
+ return;
93
+ }
94
+ return node;
95
+ }
54
96
  /**
55
97
  * 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.
56
98
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -68,28 +110,9 @@ export class TreeMultimap extends AVLTree {
68
110
  * @returns either a node (`N`) or `undefined`.
69
111
  */
70
112
  add(keyOrNodeOrEntry, count = 1) {
71
- let newNode;
72
- if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
113
+ const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
114
+ if (newNode === undefined)
73
115
  return;
74
- }
75
- else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
76
- newNode = keyOrNodeOrEntry;
77
- }
78
- else if (this.isNodeKey(keyOrNodeOrEntry)) {
79
- newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
80
- }
81
- else if (this.isEntry(keyOrNodeOrEntry)) {
82
- const [key, value] = keyOrNodeOrEntry;
83
- if (key === undefined || key === null) {
84
- return;
85
- }
86
- else {
87
- newNode = this.createNode(key, value, count);
88
- }
89
- }
90
- else {
91
- return;
92
- }
93
116
  const orgNodeCount = newNode?.count || 0;
94
117
  const inserted = super.add(newNode);
95
118
  if (inserted) {
@@ -256,6 +279,22 @@ export class TreeMultimap extends AVLTree {
256
279
  super.clear();
257
280
  this._count = 0;
258
281
  }
282
+ /**
283
+ * Time complexity: O(n)
284
+ * Space complexity: O(n)
285
+ */
286
+ /**
287
+ * Time complexity: O(n)
288
+ * Space complexity: O(n)
289
+ *
290
+ * The `clone` function creates a deep copy of a tree object.
291
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
292
+ */
293
+ clone() {
294
+ const cloned = this.createTree();
295
+ this.bfs(node => cloned.add([node.key, node.value], node.count));
296
+ return cloned;
297
+ }
259
298
  /**
260
299
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
261
300
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.