stack-typed 2.0.5 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +598 -869
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +1 -0
  64. package/dist/utils/utils.d.ts +1 -1
  65. package/dist/utils/utils.js +2 -1
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  72. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +664 -893
  74. package/src/data-structures/binary-tree/bst.ts +568 -570
  75. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  76. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +2 -0
  101. 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,22 @@ 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>
96
+ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
97
+ extends AVLTree<K, V[], R>
98
+ implements IBinaryTree<K, V[], R>
61
99
  {
62
100
  /**
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.
101
+ * Create an AVLTreeMultiMap and optionally bulk-insert items.
102
+ * @remarks Time O(N log N), Space O(N)
103
+ * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
104
+ * @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
105
+ * @returns New AVLTreeMultiMap instance.
72
106
  */
73
107
  constructor(
74
108
  keysNodesEntriesOrRaws: Iterable<
@@ -82,45 +116,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
82
116
  }
83
117
  }
84
118
 
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> {
119
+ override _createNode(key: K, value: V[] = []): AVLTreeMultiMapNode<K, V> {
124
120
  return new AVLTreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
125
121
  }
126
122
 
@@ -131,22 +127,14 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
131
127
  override add(key: K, value: V): boolean;
132
128
 
133
129
  /**
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.
130
+ * Insert a value or a list of values into the multimap. If the key exists, values are appended.
131
+ * @remarks Time O(log N + M), Space O(1)
132
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry.
133
+ * @param [value] - Single value to add when a bare key is provided.
134
+ * @returns True if inserted or appended; false if ignored.
147
135
  */
148
136
  override add(
149
- keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
137
+ keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
150
138
  value?: V
151
139
  ): boolean {
152
140
  if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
@@ -197,24 +185,14 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
197
185
  }
198
186
 
199
187
  /**
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`.
188
+ * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
189
+ * @remarks Time O(log N), Space O(1)
190
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
191
+ * @param value - Value to remove from the bucket.
192
+ * @returns True if the value was removed; false if not found.
215
193
  */
216
194
  deleteValue(
217
- keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
195
+ keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
218
196
  value: V
219
197
  ): boolean {
220
198
  const values = this.get(keyNodeOrEntry);
@@ -223,7 +201,6 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
223
201
  if (index === -1) return false;
224
202
  values.splice(index, 1);
225
203
 
226
- // If no values left, remove the entire node
227
204
  if (values.length === 0) this.delete(keyNodeOrEntry);
228
205
 
229
206
  return true;
@@ -232,16 +209,130 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
232
209
  }
233
210
 
234
211
  /**
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.
212
+ * Rebuild the tree into a perfectly balanced form using in-order nodes.
213
+ * @remarks Time O(N), Space O(N)
214
+ * @param [iterationType] - Traversal style to use when constructing the balanced tree.
215
+ * @returns True if rebalancing succeeded (tree not empty).
216
+ */
217
+ override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
218
+ const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
219
+ const n = nodes.length;
220
+ if (n === 0) return false;
221
+
222
+ this._clearNodes();
223
+
224
+ const build = (l: number, r: number, parent?: any): any | undefined => {
225
+ if (l > r) return undefined;
226
+ const m = l + ((r - l) >> 1);
227
+ const root = nodes[m];
228
+ root.left = build(l, m - 1, root);
229
+ root.right = build(m + 1, r, root);
230
+ root.parent = parent;
231
+ const lh = root.left ? root.left.height : -1;
232
+ const rh = root.right ? root.right.height : -1;
233
+ root.height = Math.max(lh, rh) + 1;
234
+ return root;
235
+ };
236
+
237
+ const newRoot = build(0, n - 1, undefined);
238
+ this._setRoot(newRoot);
239
+ this._size = n;
240
+ return true;
241
+ }
242
+
243
+ /**
244
+ * Create a new tree by mapping each [key, values] bucket.
245
+ * @remarks Time O(N log N), Space O(N)
246
+ * @template MK
247
+ * @template MVArr
248
+ * @template MR
249
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
250
+ * @param [options] - Options for the output tree.
251
+ * @param [thisArg] - Value for `this` inside the callback.
252
+ * @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
253
+ */
254
+ override map<MK = K, MVArr extends unknown[] = V[], MR extends object = object>(
255
+ callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
256
+ options?: Partial<AVLTreeOptions<MK, MVArr, MR>>,
257
+ thisArg?: unknown
258
+ ): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
259
+
260
+ /**
261
+ * Create a new tree by mapping each [key, values] bucket.
262
+ * @remarks Time O(N log N), Space O(N)
263
+ * @template MK
264
+ * @template MV
265
+ * @template MR
266
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
267
+ * @param [options] - Options for the output tree.
268
+ * @param [thisArg] - Value for `this` inside the callback.
269
+ * @returns A new AVLTree when mapping to non-array values; see overloads.
270
+ */
271
+ override map<MK = K, MV = V[], MR extends object = object>(
272
+ callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
273
+ options?: Partial<AVLTreeOptions<MK, MV, MR>>,
274
+ thisArg?: unknown
275
+ ): AVLTree<MK, MV, MR>;
276
+
277
+ /**
278
+ * Create a new tree by mapping each [key, values] bucket.
279
+ * @remarks Time O(N log N), Space O(N)
280
+ * @template MK
281
+ * @template MV
282
+ * @template MR
283
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
284
+ * @param [options] - Options for the output tree.
285
+ * @param [thisArg] - Value for `this` inside the callback.
286
+ * @returns The mapped AVLTree or AVLTreeMultiMap depending on MV; see overloads.
287
+ */
288
+ override map<MK, MV, MR extends object>(
289
+ callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
290
+ options?: Partial<AVLTreeOptions<MK, MV, MR>>,
291
+ thisArg?: unknown
292
+ ): AVLTree<MK, MV, MR> {
293
+ const out = this._createLike<MK, MV, MR>([], options);
294
+ let i = 0;
295
+ for (const [k, v] of this) out.add(callback.call(thisArg, k, v, i++, this));
296
+ return out;
297
+ }
298
+
299
+ /**
300
+ * (Protected) Create an empty instance of the same concrete class.
301
+ * @remarks Time O(1), Space O(1)
302
+ * @template TK
303
+ * @template TV
304
+ * @template TR
305
+ * @param [options] - Optional constructor options for the like-kind instance.
306
+ * @returns An empty like-kind instance.
307
+ */
308
+ protected override _createInstance<TK = K, TV = V, TR extends object = R>(
309
+ options?: Partial<AVLTreeOptions<TK, TV, TR>>
310
+ ): this {
311
+ const Ctor = this.constructor as unknown as new (
312
+ iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
313
+ opts?: AVLTreeOptions<TK, TV, TR>
314
+ ) => AVLTree<TK, TV, TR>;
315
+ return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
316
+ }
317
+
318
+ /**
319
+ * (Protected) Create a like-kind instance and seed it from an iterable.
320
+ * @remarks Time O(N log N), Space O(N)
321
+ * @template TK
322
+ * @template TV
323
+ * @template TR
324
+ * @param iter - Iterable used to seed the new tree.
325
+ * @param [options] - Options merged with the current snapshot.
326
+ * @returns A like-kind AVLTree built from the iterable.
241
327
  */
242
- override clone() {
243
- const cloned = this.createTree();
244
- this._clone(cloned);
245
- return cloned;
328
+ protected override _createLike<TK = K, TV = V, TR extends object = R>(
329
+ iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
330
+ options?: Partial<AVLTreeOptions<TK, TV, TR>>
331
+ ): AVLTree<TK, TV, TR> {
332
+ const Ctor = this.constructor as unknown as new (
333
+ iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
334
+ opts?: AVLTreeOptions<TK, TV, TR>
335
+ ) => AVLTree<TK, TV, TR>;
336
+ return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
246
337
  }
247
338
  }