avl-tree-typed 2.0.5 → 2.1.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 (104) hide show
  1. package/dist/common/index.js +1 -1
  2. package/dist/constants/index.js +1 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  4. package/dist/data-structures/base/iterable-element-base.js +149 -107
  5. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  6. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  7. package/dist/data-structures/base/linear-base.d.ts +250 -192
  8. package/dist/data-structures/base/linear-base.js +137 -274
  9. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  10. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  11. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  12. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  13. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  14. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  15. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  16. package/dist/data-structures/binary-tree/binary-tree.js +602 -873
  17. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  18. package/dist/data-structures/binary-tree/bst.js +505 -481
  19. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  20. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  21. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  22. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  23. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  24. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  25. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  26. package/dist/data-structures/graph/abstract-graph.js +267 -237
  27. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  28. package/dist/data-structures/graph/directed-graph.js +146 -233
  29. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  30. package/dist/data-structures/graph/map-graph.js +56 -59
  31. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  32. package/dist/data-structures/graph/undirected-graph.js +129 -149
  33. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  34. package/dist/data-structures/hash/hash-map.js +270 -457
  35. package/dist/data-structures/heap/heap.d.ts +214 -289
  36. package/dist/data-structures/heap/heap.js +340 -349
  37. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  38. package/dist/data-structures/heap/max-heap.js +11 -66
  39. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  40. package/dist/data-structures/heap/min-heap.js +11 -66
  41. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  42. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  43. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  44. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  45. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  46. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  47. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  48. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  49. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  50. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  51. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  52. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  53. package/dist/data-structures/queue/deque.d.ts +227 -254
  54. package/dist/data-structures/queue/deque.js +309 -348
  55. package/dist/data-structures/queue/queue.d.ts +180 -201
  56. package/dist/data-structures/queue/queue.js +265 -248
  57. package/dist/data-structures/stack/stack.d.ts +124 -102
  58. package/dist/data-structures/stack/stack.js +181 -125
  59. package/dist/data-structures/trie/trie.d.ts +164 -165
  60. package/dist/data-structures/trie/trie.js +189 -172
  61. package/dist/interfaces/binary-tree.d.ts +56 -6
  62. package/dist/interfaces/graph.d.ts +16 -0
  63. package/dist/types/data-structures/base/base.d.ts +1 -1
  64. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  65. package/dist/types/utils/utils.d.ts +1 -0
  66. package/dist/utils/number.js +1 -2
  67. package/dist/utils/utils.d.ts +1 -1
  68. package/dist/utils/utils.js +9 -8
  69. package/package.json +15 -15
  70. package/src/data-structures/base/iterable-element-base.ts +238 -115
  71. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  72. package/src/data-structures/base/linear-base.ts +271 -277
  73. package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
  74. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
  75. package/src/data-structures/binary-tree/avl-tree.ts +237 -206
  76. package/src/data-structures/binary-tree/binary-tree.ts +665 -896
  77. package/src/data-structures/binary-tree/bst.ts +565 -572
  78. package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
  79. package/src/data-structures/binary-tree/tree-counter.ts +195 -219
  80. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
  81. package/src/data-structures/graph/abstract-graph.ts +339 -264
  82. package/src/data-structures/graph/directed-graph.ts +146 -236
  83. package/src/data-structures/graph/map-graph.ts +63 -60
  84. package/src/data-structures/graph/undirected-graph.ts +129 -152
  85. package/src/data-structures/hash/hash-map.ts +274 -496
  86. package/src/data-structures/heap/heap.ts +389 -402
  87. package/src/data-structures/heap/max-heap.ts +12 -76
  88. package/src/data-structures/heap/min-heap.ts +13 -76
  89. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  90. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  91. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  92. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  93. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  94. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  95. package/src/data-structures/queue/deque.ts +381 -357
  96. package/src/data-structures/queue/queue.ts +310 -264
  97. package/src/data-structures/stack/stack.ts +217 -131
  98. package/src/data-structures/trie/trie.ts +240 -175
  99. package/src/interfaces/binary-tree.ts +240 -6
  100. package/src/interfaces/graph.ts +37 -0
  101. package/src/types/data-structures/base/base.ts +5 -5
  102. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  103. package/src/types/utils/utils.ts +2 -0
  104. package/src/utils/utils.ts +9 -14
@@ -5,21 +5,33 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { AVLTreeMultiMapOptions, BTNOptKeyOrNull } from '../../types';
8
+
9
+ import type {
10
+ AVLTreeMultiMapOptions,
11
+ AVLTreeOptions,
12
+ BTNOptKeyOrNull,
13
+ ElemOf,
14
+ EntryCallback,
15
+ IterationType
16
+ } from '../../types';
9
17
  import { AVLTree, AVLTreeNode } from './avl-tree';
10
18
  import { IBinaryTree } from '../../interfaces';
11
19
 
20
+ /**
21
+ * Node used by AVLTreeMultiMap; stores the key with a bucket of values (array).
22
+ * @remarks Time O(1), Space O(1)
23
+ * @template K
24
+ * @template V
25
+ */
12
26
  export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
13
27
  override parent?: AVLTreeMultiMapNode<K, V> = undefined;
14
28
 
15
29
  /**
16
- * This TypeScript constructor initializes an object with a key of type K and an array of values of
17
- * type V.
18
- * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
19
- * data being stored in the data structure. It helps in quickly accessing or retrieving the
20
- * associated value in the data structure.
21
- * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
22
- * type `V`.
30
+ * Create an AVLTreeMultiMap node with a value bucket.
31
+ * @remarks Time O(1), Space O(1)
32
+ * @param key - Key of the node.
33
+ * @param value - Initial array of values.
34
+ * @returns New AVLTreeMultiMapNode instance.
23
35
  */
24
36
  constructor(key: K, value: V[]) {
25
37
  super(key, value);
@@ -27,10 +39,21 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
27
39
 
28
40
  override _left?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
29
41
 
42
+ /**
43
+ * Get the left child pointer.
44
+ * @remarks Time O(1), Space O(1)
45
+ * @returns Left child node, or null/undefined.
46
+ */
30
47
  override get left(): AVLTreeMultiMapNode<K, V> | null | undefined {
31
48
  return this._left;
32
49
  }
33
50
 
51
+ /**
52
+ * Set the left child and update its parent pointer.
53
+ * @remarks Time O(1), Space O(1)
54
+ * @param v - New left child node, or null/undefined.
55
+ * @returns void
56
+ */
34
57
  override set left(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
35
58
  if (v) {
36
59
  v.parent = this;
@@ -40,10 +63,21 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
40
63
 
41
64
  override _right?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
42
65
 
66
+ /**
67
+ * Get the right child pointer.
68
+ * @remarks Time O(1), Space O(1)
69
+ * @returns Right child node, or null/undefined.
70
+ */
43
71
  override get right(): AVLTreeMultiMapNode<K, V> | null | undefined {
44
72
  return this._right;
45
73
  }
46
74
 
75
+ /**
76
+ * Set the right child and update its parent pointer.
77
+ * @remarks Time O(1), Space O(1)
78
+ * @param v - New right child node, or null/undefined.
79
+ * @returns void
80
+ */
47
81
  override set right(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
48
82
  if (v) {
49
83
  v.parent = this;
@@ -53,22 +87,19 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
53
87
  }
54
88
 
55
89
  /**
56
- *
90
+ * AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
91
+ * @remarks Time O(1), Space O(1)
92
+ * @template K
93
+ * @template V
94
+ * @template R
57
95
  */
58
- export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object>
59
- extends AVLTree<K, V[], R, MK, MV[], MR>
60
- implements IBinaryTree<K, V[], R, MK, MV, MR>
61
- {
96
+ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[], R> implements IBinaryTree<K, V[], R> {
62
97
  /**
63
- * The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data
64
- * and options.
65
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
66
- * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
67
- * AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
68
- * the AVLTreeMulti
69
- * @param [options] - The `options` parameter in the constructor is of type
70
- * `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
71
- * additional options for configuring the AVLTreeMultiMap instance.
98
+ * Create an AVLTreeMultiMap and optionally bulk-insert items.
99
+ * @remarks Time O(N log N), Space O(N)
100
+ * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
101
+ * @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
102
+ * @returns New AVLTreeMultiMap instance.
72
103
  */
73
104
  constructor(
74
105
  keysNodesEntriesOrRaws: Iterable<
@@ -82,45 +113,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
82
113
  }
83
114
  }
84
115
 
85
- /**
86
- * Time Complexity: O(1)
87
- * Space Complexity: O(1)
88
- *
89
- * The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with
90
- * specified options.
91
- * @param [options] - The `options` parameter in the `createTree` function is of type
92
- * `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type
93
- * `K`, `V[]`, and `R`. The function creates a new `AVL
94
- * @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the
95
- * provided options.
96
- */
97
- override createTree(options?: AVLTreeMultiMapOptions<K, V[], R>) {
98
- return new AVLTreeMultiMap<K, V, R, MK, MV, MR>([], {
99
- iterationType: this.iterationType,
100
- specifyComparable: this._specifyComparable,
101
- toEntryFn: this._toEntryFn,
102
- isReverse: this._isReverse,
103
- isMapMode: this._isMapMode,
104
- ...options
105
- });
106
- }
107
-
108
- /**
109
- * Time Complexity: O(1)
110
- * Space Complexity: O(1)
111
- *
112
- * The `createNode` function in TypeScript overrides the default implementation to create a new
113
- * AVLTreeMultiMapNode with a specified key and value array.
114
- * @param {K} key - The `key` parameter represents the key of the node being created in the
115
- * AVLTreeMultiMap.
116
- * @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
117
- * values associated with a specific key in the AVLTreeMultiMapNode. If no value is provided when
118
- * calling the method, an empty array `[]` is used as the default value.
119
- * @returns An AVLTreeMultiMapNode object is being returned, with the specified key and value. If the
120
- * AVLTreeMultiMap is in map mode, an empty array is used as the value, otherwise the provided value
121
- * array is used.
122
- */
123
- override createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
116
+ override _createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
124
117
  return new AVLTreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
125
118
  }
126
119
 
@@ -131,22 +124,14 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
131
124
  override add(key: K, value: V): boolean;
132
125
 
133
126
  /**
134
- * Time Complexity: O(log n)
135
- * Space Complexity: O(log n)
136
- *
137
- * The function `add` in this TypeScript code overrides the superclass method to add key-value pairs
138
- * to an AVLTreeMultiMap, handling different input types and scenarios.
139
- * @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
140
- * be added to the AVLTreeMultiMap. It can be of type `K`, which is the key type of the map. The key
141
- * can be a single key value, a node of the AVLTree
142
- * @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
143
- * that you want to add to the AVLTreeMultiMap. It can contain one or more values associated with a
144
- * specific key.
145
- * @returns The `add` method is returning a boolean value, which indicates whether the operation was
146
- * successful or not.
127
+ * Insert a value or a list of values into the multimap. If the key exists, values are appended.
128
+ * @remarks Time O(log N + M), Space O(1)
129
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry.
130
+ * @param [value] - Single value to add when a bare key is provided.
131
+ * @returns True if inserted or appended; false if ignored.
147
132
  */
148
133
  override add(
149
- keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
134
+ keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
150
135
  value?: V
151
136
  ): boolean {
152
137
  if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
@@ -197,24 +182,14 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
197
182
  }
198
183
 
199
184
  /**
200
- * Time Complexity: O(log n)
201
- * Space Complexity: O(log n)
202
- *
203
- * The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
204
- * structure and deletes the entire node if no values are left for that key.
205
- * @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
206
- * parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
207
- * pair in the AVLTreeMultiMapNode, or just the key itself.
208
- * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
209
- * value that you want to delete from the multi-map data structure associated with a particular key.
210
- * The function checks if the value exists in the array of values associated with the key, and if
211
- * found, removes it from the array.
212
- * @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified
213
- * `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
214
- * the value was not found in the array, it returns `false`.
185
+ * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
186
+ * @remarks Time O(log N), Space O(1)
187
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
188
+ * @param value - Value to remove from the bucket.
189
+ * @returns True if the value was removed; false if not found.
215
190
  */
216
191
  deleteValue(
217
- keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
192
+ keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
218
193
  value: V
219
194
  ): boolean {
220
195
  const values = this.get(keyNodeOrEntry);
@@ -223,7 +198,6 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
223
198
  if (index === -1) return false;
224
199
  values.splice(index, 1);
225
200
 
226
- // If no values left, remove the entire node
227
201
  if (values.length === 0) this.delete(keyNodeOrEntry);
228
202
 
229
203
  return true;
@@ -232,16 +206,128 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
232
206
  }
233
207
 
234
208
  /**
235
- * Time Complexity: O(n)
236
- * Space Complexity: O(n)
237
- *
238
- * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
239
- * structure.
240
- * @returns A cloned tree object is being returned.
209
+ * Rebuild the tree into a perfectly balanced form using in-order nodes.
210
+ * @remarks Time O(N), Space O(N)
211
+ * @param [iterationType] - Traversal style to use when constructing the balanced tree.
212
+ * @returns True if rebalancing succeeded (tree not empty).
213
+ */
214
+ override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
215
+ const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
216
+ const n = nodes.length;
217
+ if (n === 0) return false;
218
+
219
+ this._clearNodes();
220
+
221
+ const build = (l: number, r: number, parent?: any): any | undefined => {
222
+ if (l > r) return undefined;
223
+ const m = l + ((r - l) >> 1);
224
+ const root = nodes[m];
225
+ root.left = build(l, m - 1, root);
226
+ root.right = build(m + 1, r, root);
227
+ root.parent = parent;
228
+ const lh = root.left ? root.left.height : -1;
229
+ const rh = root.right ? root.right.height : -1;
230
+ root.height = Math.max(lh, rh) + 1;
231
+ return root;
232
+ };
233
+
234
+ const newRoot = build(0, n - 1, undefined);
235
+ this._setRoot(newRoot);
236
+ this._size = n;
237
+ return true;
238
+ }
239
+
240
+ /**
241
+ * Create a new tree by mapping each [key, values] bucket.
242
+ * @remarks Time O(N log N), Space O(N)
243
+ * @template MK
244
+ * @template MVArr
245
+ * @template MR
246
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
247
+ * @param [options] - Options for the output tree.
248
+ * @param [thisArg] - Value for `this` inside the callback.
249
+ * @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
250
+ */
251
+ override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
252
+ callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
253
+ options?: Partial<AVLTreeOptions<MK, MVArr, MR>>,
254
+ thisArg?: unknown
255
+ ): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
256
+
257
+ /**
258
+ * Create a new tree by mapping each [key, values] bucket.
259
+ * @remarks Time O(N log N), Space O(N)
260
+ * @template MK
261
+ * @template MV
262
+ * @template MR
263
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
264
+ * @param [options] - Options for the output tree.
265
+ * @param [thisArg] - Value for `this` inside the callback.
266
+ * @returns A new AVLTree when mapping to non-array values; see overloads.
267
+ */
268
+ override map<MK = K, MV = V[], MR = any>(
269
+ callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
270
+ options?: Partial<AVLTreeOptions<MK, MV, MR>>,
271
+ thisArg?: unknown
272
+ ): AVLTree<MK, MV, MR>;
273
+
274
+ /**
275
+ * Create a new tree by mapping each [key, values] bucket.
276
+ * @remarks Time O(N log N), Space O(N)
277
+ * @template MK
278
+ * @template MV
279
+ * @template MR
280
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
281
+ * @param [options] - Options for the output tree.
282
+ * @param [thisArg] - Value for `this` inside the callback.
283
+ * @returns The mapped AVLTree or AVLTreeMultiMap depending on MV; see overloads.
284
+ */
285
+ override map<MK, MV, MR extends object>(
286
+ callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
287
+ options?: Partial<AVLTreeOptions<MK, MV, MR>>,
288
+ thisArg?: unknown
289
+ ): AVLTree<MK, MV, MR> {
290
+ const out = this._createLike<MK, MV, MR>([], options);
291
+ let i = 0;
292
+ for (const [k, v] of this) out.add(callback.call(thisArg, k, v, i++, this));
293
+ return out;
294
+ }
295
+
296
+ /**
297
+ * (Protected) Create an empty instance of the same concrete class.
298
+ * @remarks Time O(1), Space O(1)
299
+ * @template TK
300
+ * @template TV
301
+ * @template TR
302
+ * @param [options] - Optional constructor options for the like-kind instance.
303
+ * @returns An empty like-kind instance.
304
+ */
305
+ protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this {
306
+ const Ctor = this.constructor as unknown as new (
307
+ iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
308
+ opts?: AVLTreeOptions<TK, TV, TR>
309
+ ) => AVLTree<TK, TV, TR>;
310
+ return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
311
+ }
312
+
313
+ /**
314
+ * (Protected) Create a like-kind instance and seed it from an iterable.
315
+ * @remarks Time O(N log N), Space O(N)
316
+ * @template TK
317
+ * @template TV
318
+ * @template TR
319
+ * @param iter - Iterable used to seed the new tree.
320
+ * @param [options] - Options merged with the current snapshot.
321
+ * @returns A like-kind AVLTree built from the iterable.
241
322
  */
242
- override clone() {
243
- const cloned = this.createTree();
244
- this._clone(cloned);
245
- return cloned;
323
+ protected override _createLike<TK = K, TV = V, TR = R>(
324
+ iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
325
+ options?: Partial<AVLTreeOptions<TK, TV, TR>>
326
+ ): AVLTree<TK, TV, TR> {
327
+ const Ctor = this.constructor as unknown as new (
328
+ iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
329
+ opts?: AVLTreeOptions<TK, TV, TR>
330
+ ) => AVLTree<TK, TV, TR>;
331
+ return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
246
332
  }
247
333
  }