linked-list-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.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +6 -0
- package/dist/data-structures/binary-tree/avl-tree.js +8 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +52 -0
- package/dist/data-structures/binary-tree/binary-tree.js +106 -28
- package/dist/data-structures/binary-tree/bst.d.ts +13 -0
- package/dist/data-structures/binary-tree/bst.js +41 -21
- package/dist/data-structures/binary-tree/rb-tree.d.ts +16 -0
- package/dist/data-structures/binary-tree/rb-tree.js +54 -31
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +28 -0
- package/dist/data-structures/binary-tree/tree-multimap.js +60 -21
- package/dist/data-structures/hash/hash-map.d.ts +173 -16
- package/dist/data-structures/hash/hash-map.js +373 -37
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +9 -0
- package/src/data-structures/binary-tree/binary-tree.ts +109 -24
- package/src/data-structures/binary-tree/bst.ts +38 -19
- package/src/data-structures/binary-tree/rb-tree.ts +55 -31
- package/src/data-structures/binary-tree/tree-multimap.ts +60 -17
- package/src/data-structures/hash/hash-map.ts +400 -40
- package/src/types/data-structures/hash/hash-map.ts +2 -0
|
@@ -102,9 +102,10 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
102
102
|
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
103
103
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
104
104
|
*/
|
|
105
|
+
|
|
105
106
|
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>>
|
|
106
|
-
implements IBinaryTree<V, N, TREE> {
|
|
107
107
|
|
|
108
|
+
implements IBinaryTree<V, N, TREE> {
|
|
108
109
|
iterationType = IterationType.ITERATIVE
|
|
109
110
|
|
|
110
111
|
/**
|
|
@@ -163,6 +164,47 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
163
164
|
return new BinaryTree<V, N, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
|
|
164
165
|
}
|
|
165
166
|
|
|
167
|
+
/**
|
|
168
|
+
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
169
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
170
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
171
|
+
*/
|
|
172
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
173
|
+
return exemplar instanceof BinaryTreeNode;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
|
|
178
|
+
* object.
|
|
179
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
|
|
180
|
+
* function. It can be any type.
|
|
181
|
+
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
|
|
182
|
+
*/
|
|
183
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | null | undefined {
|
|
184
|
+
if (exemplar === undefined) return;
|
|
185
|
+
|
|
186
|
+
let node: N | null | undefined;
|
|
187
|
+
if (exemplar === null) {
|
|
188
|
+
node = null;
|
|
189
|
+
} else if (this.isEntry(exemplar)) {
|
|
190
|
+
const [key, value] = exemplar;
|
|
191
|
+
if (key === undefined) {
|
|
192
|
+
return;
|
|
193
|
+
} else if (key === null) {
|
|
194
|
+
node = null;
|
|
195
|
+
} else {
|
|
196
|
+
node = this.createNode(key, value);
|
|
197
|
+
}
|
|
198
|
+
} else if (this.isNode(exemplar)) {
|
|
199
|
+
node = exemplar;
|
|
200
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
201
|
+
node = this.createNode(exemplar);
|
|
202
|
+
} else {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
return node;
|
|
206
|
+
}
|
|
207
|
+
|
|
166
208
|
/**
|
|
167
209
|
* The function checks if a given value is an entry in a binary tree node.
|
|
168
210
|
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
@@ -188,7 +230,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
188
230
|
*/
|
|
189
231
|
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | null | undefined {
|
|
190
232
|
|
|
191
|
-
let inserted: N | null | undefined
|
|
233
|
+
let inserted: N | null | undefined;
|
|
234
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
235
|
+
if (newNode === undefined) return;
|
|
192
236
|
|
|
193
237
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
194
238
|
const queue = new Queue<N>([root]);
|
|
@@ -205,30 +249,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
205
249
|
}
|
|
206
250
|
};
|
|
207
251
|
|
|
208
|
-
if (keyOrNodeOrEntry === null) {
|
|
209
|
-
needInsert = null;
|
|
210
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
211
|
-
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
212
|
-
} else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
213
|
-
needInsert = keyOrNodeOrEntry;
|
|
214
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
215
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
216
|
-
if (key === undefined) {
|
|
217
|
-
return;
|
|
218
|
-
} else if (key === null) {
|
|
219
|
-
needInsert = null;
|
|
220
|
-
} else {
|
|
221
|
-
needInsert = this.createNode(key, value);
|
|
222
|
-
}
|
|
223
|
-
} else {
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
252
|
if (this.root) {
|
|
228
|
-
inserted = _bfs(this.root,
|
|
253
|
+
inserted = _bfs(this.root, newNode);
|
|
229
254
|
} else {
|
|
230
|
-
this._setRoot(
|
|
231
|
-
if (
|
|
255
|
+
this._setRoot(newNode);
|
|
256
|
+
if (newNode) {
|
|
232
257
|
this._size = 1;
|
|
233
258
|
} else {
|
|
234
259
|
this._size = 0;
|
|
@@ -1699,6 +1724,66 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1699
1724
|
return ans;
|
|
1700
1725
|
}
|
|
1701
1726
|
|
|
1727
|
+
/**
|
|
1728
|
+
* Time complexity: O(n)
|
|
1729
|
+
* Space complexity: O(n)
|
|
1730
|
+
*/
|
|
1731
|
+
|
|
1732
|
+
/**
|
|
1733
|
+
* Time complexity: O(n)
|
|
1734
|
+
* Space complexity: O(n)
|
|
1735
|
+
*
|
|
1736
|
+
* The function "keys" returns an array of keys from a given object.
|
|
1737
|
+
* @returns an array of BTNKey objects.
|
|
1738
|
+
*/
|
|
1739
|
+
keys(): BTNKey[] {
|
|
1740
|
+
const keys: BTNKey[] = [];
|
|
1741
|
+
for (const entry of this) {
|
|
1742
|
+
keys.push(entry[0]);
|
|
1743
|
+
}
|
|
1744
|
+
return keys;
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
/**
|
|
1748
|
+
* Time complexity: O(n)
|
|
1749
|
+
* Space complexity: O(n)
|
|
1750
|
+
*/
|
|
1751
|
+
|
|
1752
|
+
/**
|
|
1753
|
+
* Time complexity: O(n)
|
|
1754
|
+
* Space complexity: O(n)
|
|
1755
|
+
*
|
|
1756
|
+
* The function "values" returns an array of values from a map-like object.
|
|
1757
|
+
* @returns The `values()` method is returning an array of values (`V`) from the entries in the
|
|
1758
|
+
* object.
|
|
1759
|
+
*/
|
|
1760
|
+
values(): (V | undefined)[] {
|
|
1761
|
+
const values: (V | undefined)[] = [];
|
|
1762
|
+
for (const entry of this) {
|
|
1763
|
+
values.push(entry[1]);
|
|
1764
|
+
}
|
|
1765
|
+
return values;
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
/**
|
|
1769
|
+
* Time complexity: O(n)
|
|
1770
|
+
* Space complexity: O(n)
|
|
1771
|
+
*/
|
|
1772
|
+
|
|
1773
|
+
/**
|
|
1774
|
+
* Time complexity: O(n)
|
|
1775
|
+
* Space complexity: O(n)
|
|
1776
|
+
*
|
|
1777
|
+
* The `clone` function creates a new tree object and copies all the nodes from the original tree to
|
|
1778
|
+
* the new tree.
|
|
1779
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
1780
|
+
*/
|
|
1781
|
+
clone(): TREE {
|
|
1782
|
+
const cloned = this.createTree();
|
|
1783
|
+
this.bfs(node => cloned.add([node.key, node.value]));
|
|
1784
|
+
return cloned;
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1702
1787
|
/**
|
|
1703
1788
|
* Time complexity: O(n)
|
|
1704
1789
|
* Space complexity: O(1)
|
|
@@ -143,6 +143,42 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
143
143
|
}) as TREE;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
/**
|
|
147
|
+
* The function checks if an exemplar is an instance of BSTNode.
|
|
148
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
149
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
150
|
+
*/
|
|
151
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
152
|
+
return exemplar instanceof BSTNode;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
157
|
+
* is valid, otherwise it returns undefined.
|
|
158
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
159
|
+
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
160
|
+
*/
|
|
161
|
+
override exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined {
|
|
162
|
+
let node: N | undefined;
|
|
163
|
+
if (exemplar === null || exemplar === undefined) {
|
|
164
|
+
return;
|
|
165
|
+
} else if (this.isNode(exemplar)) {
|
|
166
|
+
node = exemplar;
|
|
167
|
+
} else if (this.isEntry(exemplar)) {
|
|
168
|
+
const [key, value] = exemplar;
|
|
169
|
+
if (key === undefined || key === null) {
|
|
170
|
+
return;
|
|
171
|
+
} else {
|
|
172
|
+
node = this.createNode(key, value);
|
|
173
|
+
}
|
|
174
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
175
|
+
node = this.createNode(exemplar);
|
|
176
|
+
} else {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
return node;
|
|
180
|
+
}
|
|
181
|
+
|
|
146
182
|
/**
|
|
147
183
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
148
184
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -159,25 +195,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
159
195
|
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
160
196
|
*/
|
|
161
197
|
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
let newNode: N | undefined;
|
|
167
|
-
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
168
|
-
newNode = keyOrNodeOrEntry;
|
|
169
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
170
|
-
newNode = this.createNode(keyOrNodeOrEntry);
|
|
171
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
172
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
173
|
-
if (key === undefined || key === null) {
|
|
174
|
-
return;
|
|
175
|
-
} else {
|
|
176
|
-
newNode = this.createNode(key, value);
|
|
177
|
-
}
|
|
178
|
-
} else {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
198
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
199
|
+
if (newNode === undefined) return;
|
|
181
200
|
|
|
182
201
|
if (this.root === undefined) {
|
|
183
202
|
this._setRoot(newNode);
|
|
@@ -107,38 +107,62 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
110
|
+
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
111
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
112
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
113
|
+
* class.
|
|
112
114
|
*/
|
|
113
|
-
|
|
115
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
116
|
+
return exemplar instanceof RedBlackTreeNode;
|
|
117
|
+
}
|
|
114
118
|
|
|
115
119
|
/**
|
|
116
|
-
* The function
|
|
117
|
-
*
|
|
118
|
-
* @
|
|
119
|
-
*
|
|
120
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
121
|
+
* otherwise it returns undefined.
|
|
122
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
123
|
+
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
124
|
+
* that is not a valid exemplar.
|
|
125
|
+
* @returns a variable `node` which is of type `N | undefined`.
|
|
120
126
|
*/
|
|
121
|
-
override
|
|
122
|
-
let node: N;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
} else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
126
|
-
node = keyOrNodeOrEntry;
|
|
127
|
-
} else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
127
|
+
override exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined {
|
|
128
|
+
let node: N | undefined;
|
|
129
|
+
|
|
130
|
+
if (exemplar === null || exemplar === undefined) {
|
|
128
131
|
return;
|
|
129
|
-
} else if (this.
|
|
130
|
-
|
|
132
|
+
} else if (this.isNode(exemplar)) {
|
|
133
|
+
node = exemplar;
|
|
134
|
+
} else if (this.isEntry(exemplar)) {
|
|
135
|
+
const [key, value] = exemplar;
|
|
131
136
|
if (key === undefined || key === null) {
|
|
132
137
|
return;
|
|
133
138
|
} else {
|
|
134
139
|
node = this.createNode(key, value, RBTNColor.RED);
|
|
135
140
|
}
|
|
141
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
142
|
+
node = this.createNode(exemplar, undefined, RBTNColor.RED);
|
|
136
143
|
} else {
|
|
137
144
|
return;
|
|
138
145
|
}
|
|
146
|
+
return node;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
151
|
+
* Space Complexity: O(1)
|
|
152
|
+
*/
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
156
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
157
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
158
|
+
* `undefined`.
|
|
159
|
+
*/
|
|
160
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
|
|
161
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
162
|
+
if (newNode === undefined) return;
|
|
139
163
|
|
|
140
|
-
|
|
141
|
-
|
|
164
|
+
newNode.left = this.Sentinel;
|
|
165
|
+
newNode.right = this.Sentinel;
|
|
142
166
|
|
|
143
167
|
let y: N | undefined = undefined;
|
|
144
168
|
let x: N | undefined = this.root;
|
|
@@ -146,13 +170,13 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
146
170
|
while (x !== this.Sentinel) {
|
|
147
171
|
y = x;
|
|
148
172
|
if (x) {
|
|
149
|
-
if (
|
|
173
|
+
if (newNode.key < x.key) {
|
|
150
174
|
x = x.left;
|
|
151
|
-
} else if (
|
|
175
|
+
} else if (newNode.key > x.key) {
|
|
152
176
|
x = x?.right;
|
|
153
177
|
} else {
|
|
154
|
-
if (
|
|
155
|
-
this._replaceNode(x,
|
|
178
|
+
if (newNode !== x) {
|
|
179
|
+
this._replaceNode(x, newNode)
|
|
156
180
|
}
|
|
157
181
|
return;
|
|
158
182
|
}
|
|
@@ -160,27 +184,27 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
160
184
|
|
|
161
185
|
}
|
|
162
186
|
|
|
163
|
-
|
|
187
|
+
newNode.parent = y;
|
|
164
188
|
if (y === undefined) {
|
|
165
|
-
this._setRoot(
|
|
166
|
-
} else if (
|
|
167
|
-
y.left =
|
|
189
|
+
this._setRoot(newNode);
|
|
190
|
+
} else if (newNode.key < y.key) {
|
|
191
|
+
y.left = newNode;
|
|
168
192
|
} else {
|
|
169
|
-
y.right =
|
|
193
|
+
y.right = newNode;
|
|
170
194
|
}
|
|
171
195
|
|
|
172
|
-
if (
|
|
173
|
-
|
|
196
|
+
if (newNode.parent === undefined) {
|
|
197
|
+
newNode.color = RBTNColor.BLACK;
|
|
174
198
|
this._size++;
|
|
175
199
|
return;
|
|
176
200
|
}
|
|
177
201
|
|
|
178
|
-
if (
|
|
202
|
+
if (newNode.parent.parent === undefined) {
|
|
179
203
|
this._size++;
|
|
180
204
|
return;
|
|
181
205
|
}
|
|
182
206
|
|
|
183
|
-
this._fixInsert(
|
|
207
|
+
this._fixInsert(newNode);
|
|
184
208
|
this._size++;
|
|
185
209
|
}
|
|
186
210
|
|
|
@@ -80,6 +80,45 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
80
80
|
}) as TREE;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
85
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
86
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
87
|
+
* class.
|
|
88
|
+
*/
|
|
89
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
90
|
+
return exemplar instanceof TreeMultimapNode;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
95
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
|
|
96
|
+
* the value type and `N` represents the node type.
|
|
97
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
98
|
+
* times the node should be created. If not provided, it defaults to 1.
|
|
99
|
+
* @returns a value of type `N` (the generic type parameter) or `undefined`.
|
|
100
|
+
*/
|
|
101
|
+
override exemplarToNode(exemplar: BTNodeExemplar<V, N>, count = 1): N | undefined {
|
|
102
|
+
let node: N | undefined;
|
|
103
|
+
if (exemplar === undefined || exemplar === null) {
|
|
104
|
+
return;
|
|
105
|
+
} else if (this.isNode(exemplar)) {
|
|
106
|
+
node = exemplar;
|
|
107
|
+
} else if (this.isEntry(exemplar)) {
|
|
108
|
+
const [key, value] = exemplar;
|
|
109
|
+
if (key === undefined || key === null) {
|
|
110
|
+
return;
|
|
111
|
+
} else {
|
|
112
|
+
node = this.createNode(key, value, count);
|
|
113
|
+
}
|
|
114
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
115
|
+
node = this.createNode(exemplar, undefined, count);
|
|
116
|
+
} else {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
return node;
|
|
120
|
+
}
|
|
121
|
+
|
|
83
122
|
/**
|
|
84
123
|
* 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.
|
|
85
124
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -98,23 +137,9 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
98
137
|
* @returns either a node (`N`) or `undefined`.
|
|
99
138
|
*/
|
|
100
139
|
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count = 1): N | undefined {
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
} else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
105
|
-
newNode = keyOrNodeOrEntry;
|
|
106
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
107
|
-
newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
|
|
108
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
109
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
110
|
-
if (key === undefined || key === null) {
|
|
111
|
-
return;
|
|
112
|
-
} else {
|
|
113
|
-
newNode = this.createNode(key, value, count);
|
|
114
|
-
}
|
|
115
|
-
} else {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
140
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
|
|
141
|
+
if (newNode === undefined) return;
|
|
142
|
+
|
|
118
143
|
const orgNodeCount = newNode?.count || 0;
|
|
119
144
|
const inserted = super.add(newNode);
|
|
120
145
|
if (inserted) {
|
|
@@ -293,6 +318,24 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
293
318
|
this._count = 0;
|
|
294
319
|
}
|
|
295
320
|
|
|
321
|
+
/**
|
|
322
|
+
* Time complexity: O(n)
|
|
323
|
+
* Space complexity: O(n)
|
|
324
|
+
*/
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Time complexity: O(n)
|
|
328
|
+
* Space complexity: O(n)
|
|
329
|
+
*
|
|
330
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
331
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
332
|
+
*/
|
|
333
|
+
override clone(): TREE {
|
|
334
|
+
const cloned = this.createTree();
|
|
335
|
+
this.bfs(node => cloned.add([node.key, node.value], node.count));
|
|
336
|
+
return cloned;
|
|
337
|
+
}
|
|
338
|
+
|
|
296
339
|
/**
|
|
297
340
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
298
341
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|