bst-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 (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 +602 -873
  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 +196 -217
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
  72. package/src/data-structures/binary-tree/avl-tree.ts +237 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +665 -896
  74. package/src/data-structures/binary-tree/bst.ts +565 -572
  75. package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
  76. package/src/data-structures/binary-tree/tree-counter.ts +195 -219
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
  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,30 +5,60 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { TreeMultiMapOptions } from '../../types';
8
+ import type { ElemOf, EntryCallback, RedBlackTreeOptions, TreeMultiMapOptions } from '../../types';
9
9
  import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
10
10
  import { IBinaryTree } from '../../interfaces';
11
+ /**
12
+ * Node used by TreeMultiMap; stores the key with a bucket of values (array).
13
+ * @remarks Time O(1), Space O(1)
14
+ * @template K
15
+ * @template V
16
+ */
11
17
  export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> {
12
18
  parent?: TreeMultiMapNode<K, V>;
13
19
  /**
14
- * This TypeScript constructor initializes an object with a key of type K and an array of values of
15
- * type V.
16
- * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
17
- * data being stored in the data structure. It helps in quickly accessing or retrieving the
18
- * associated value in the data structure.
19
- * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
20
- * type `V`.
20
+ * Create a TreeMultiMap node with an optional value bucket.
21
+ * @remarks Time O(1), Space O(1)
22
+ * @param key - Key of the node.
23
+ * @param [value] - Initial array of values.
24
+ * @returns New TreeMultiMapNode instance.
21
25
  */
22
26
  constructor(key: K, value?: V[]);
23
27
  _left?: TreeMultiMapNode<K, V> | null | undefined;
28
+ /**
29
+ * Get the left child pointer.
30
+ * @remarks Time O(1), Space O(1)
31
+ * @returns Left child node, or null/undefined.
32
+ */
24
33
  get left(): TreeMultiMapNode<K, V> | null | undefined;
34
+ /**
35
+ * Set the left child and update its parent pointer.
36
+ * @remarks Time O(1), Space O(1)
37
+ * @param v - New left child node, or null/undefined.
38
+ * @returns void
39
+ */
25
40
  set left(v: TreeMultiMapNode<K, V> | null | undefined);
26
41
  _right?: TreeMultiMapNode<K, V> | null | undefined;
42
+ /**
43
+ * Get the right child pointer.
44
+ * @remarks Time O(1), Space O(1)
45
+ * @returns Right child node, or null/undefined.
46
+ */
27
47
  get right(): TreeMultiMapNode<K, V> | null | undefined;
48
+ /**
49
+ * Set the right child and update its parent pointer.
50
+ * @remarks Time O(1), Space O(1)
51
+ * @param v - New right child node, or null/undefined.
52
+ * @returns void
53
+ */
28
54
  set right(v: TreeMultiMapNode<K, V> | null | undefined);
29
55
  }
30
56
  /**
31
- *
57
+ * Red-Black Tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
58
+ * @remarks Time O(1), Space O(1)
59
+ * @template K
60
+ * @template V
61
+ * @template R
32
62
  * @example
33
63
  * // players ranked by score with their equipment
34
64
  * type Equipment = {
@@ -194,74 +224,47 @@ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode
194
224
  * // ]
195
225
  * // ]
196
226
  */
197
- export declare class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends RedBlackTree<K, V[], R, MK, MV[], MR> implements IBinaryTree<K, V[], R, MK, MV, MR> {
227
+ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[], R> implements IBinaryTree<K, V[], R> {
198
228
  /**
199
- * The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data
200
- * and options.
201
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
202
- * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
203
- * TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
204
- * the RedBlackTreeMulti
205
- * @param [options] - The `options` parameter in the constructor is of type
206
- * `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
207
- * additional options for configuring the TreeMultiMap instance.
229
+ * Create a TreeMultiMap and optionally bulk-insert items.
230
+ * @remarks Time O(N log N), Space O(N)
231
+ * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
232
+ * @param [options] - Options for TreeMultiMap (comparator, reverse, map mode).
233
+ * @returns New TreeMultiMap instance.
208
234
  */
209
235
  constructor(keysNodesEntriesOrRaws?: Iterable<K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: TreeMultiMapOptions<K, V[], R>);
210
- /**
211
- * Time Complexity: O(1)
212
- * Space Complexity: O(1)
213
- *
214
- * The `createTree` function in TypeScript overrides the default implementation to create a new
215
- * TreeMultiMap with specified options.
216
- * @param [options] - The `options` parameter in the `createTree` method is of type
217
- * `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration
218
- * options when creating a new `TreeMultiMap` instance. It includes properties such as
219
- * `iterationType`, `specifyComparable
220
- * @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial
221
- * data and the provided options merged with the existing properties of the current object.
222
- */
223
- createTree(options?: TreeMultiMapOptions<K, V[], R>): TreeMultiMap<K, V, R, MK, MV, MR>;
224
- /**
225
- * Time Complexity: O(1)
226
- * Space Complexity: O(1)
227
- *
228
- * The function `createNode` overrides the creation of a new TreeMultiMapNode with a specified key
229
- * and value array.
230
- * @param {K} key - The `key` parameter represents the key of the node being created in the
231
- * `TreeMultiMap`.
232
- * @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
233
- * values associated with a specific key in the TreeMultiMap data structure.
234
- * @returns A new instance of `TreeMultiMapNode<K, V>` is being returned with the specified key and
235
- * value. If `_isMapMode` is true, an empty array is passed as the value, otherwise the provided
236
- * value is used.
237
- */
238
- createNode(key: K, value?: V[]): TreeMultiMapNode<K, V>;
236
+ _createNode(key: K, value?: V[]): TreeMultiMapNode<K, V>;
239
237
  add(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
240
238
  add(key: K, value: V): boolean;
241
239
  /**
242
- * Time Complexity: O(log n)
243
- * Space Complexity: O(log n)
244
- *
245
- * The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure
246
- * and deletes the entire node if no values are left for that key.
247
- * @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
248
- * parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an
249
- * array of values, or just a key itself.
250
- * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
251
- * value that you want to remove from the multi-map data structure associated with a particular key.
252
- * The function checks if the value exists in the array of values associated with the key, and if
253
- * found, removes it from the array.
254
- * @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was
255
- * successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise.
240
+ * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
241
+ * @remarks Time O(log N), Space O(1)
242
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
243
+ * @param value - Value to remove from the bucket.
244
+ * @returns True if the value was removed; false if not found.
256
245
  */
257
246
  deleteValue(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
247
+ map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<RedBlackTreeOptions<MK, MVArr, MR>>, thisArg?: unknown): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
248
+ map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<RedBlackTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
249
+ /**
250
+ * (Protected) Create an empty instance of the same concrete class.
251
+ * @remarks Time O(1), Space O(1)
252
+ * @template TK
253
+ * @template TV
254
+ * @template TR
255
+ * @param [options] - Optional constructor options for the like-kind instance.
256
+ * @returns An empty like-kind instance.
257
+ */
258
+ protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
258
259
  /**
259
- * Time Complexity: O(n)
260
- * Space Complexity: O(n)
261
- *
262
- * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
263
- * structure.
264
- * @returns The `cloned` object is being returned.
260
+ * (Protected) Create a like-kind instance and seed it from an iterable.
261
+ * @remarks Time O(N log N), Space O(N)
262
+ * @template TK
263
+ * @template TV
264
+ * @template TR
265
+ * @param iter - Iterable used to seed the new tree.
266
+ * @param [options] - Options merged with the current snapshot.
267
+ * @returns A like-kind RedBlackTree built from the iterable.
265
268
  */
266
- clone(): TreeMultiMap<K, V, R, MK, MV, MR>;
269
+ protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
267
270
  }
@@ -1,16 +1,27 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Pablo Zeng
6
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.TreeMultiMap = exports.TreeMultiMapNode = void 0;
4
11
  const red_black_tree_1 = require("./red-black-tree");
12
+ /**
13
+ * Node used by TreeMultiMap; stores the key with a bucket of values (array).
14
+ * @remarks Time O(1), Space O(1)
15
+ * @template K
16
+ * @template V
17
+ */
5
18
  class TreeMultiMapNode extends red_black_tree_1.RedBlackTreeNode {
6
19
  /**
7
- * This TypeScript constructor initializes an object with a key of type K and an array of values of
8
- * type V.
9
- * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
10
- * data being stored in the data structure. It helps in quickly accessing or retrieving the
11
- * associated value in the data structure.
12
- * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
13
- * type `V`.
20
+ * Create a TreeMultiMap node with an optional value bucket.
21
+ * @remarks Time O(1), Space O(1)
22
+ * @param key - Key of the node.
23
+ * @param [value] - Initial array of values.
24
+ * @returns New TreeMultiMapNode instance.
14
25
  */
15
26
  constructor(key, value) {
16
27
  super(key, value);
@@ -18,18 +29,40 @@ class TreeMultiMapNode extends red_black_tree_1.RedBlackTreeNode {
18
29
  this._left = undefined;
19
30
  this._right = undefined;
20
31
  }
32
+ /**
33
+ * Get the left child pointer.
34
+ * @remarks Time O(1), Space O(1)
35
+ * @returns Left child node, or null/undefined.
36
+ */
21
37
  get left() {
22
38
  return this._left;
23
39
  }
40
+ /**
41
+ * Set the left child and update its parent pointer.
42
+ * @remarks Time O(1), Space O(1)
43
+ * @param v - New left child node, or null/undefined.
44
+ * @returns void
45
+ */
24
46
  set left(v) {
25
47
  if (v) {
26
48
  v.parent = this;
27
49
  }
28
50
  this._left = v;
29
51
  }
52
+ /**
53
+ * Get the right child pointer.
54
+ * @remarks Time O(1), Space O(1)
55
+ * @returns Right child node, or null/undefined.
56
+ */
30
57
  get right() {
31
58
  return this._right;
32
59
  }
60
+ /**
61
+ * Set the right child and update its parent pointer.
62
+ * @remarks Time O(1), Space O(1)
63
+ * @param v - New right child node, or null/undefined.
64
+ * @returns void
65
+ */
33
66
  set right(v) {
34
67
  if (v) {
35
68
  v.parent = this;
@@ -39,7 +72,11 @@ class TreeMultiMapNode extends red_black_tree_1.RedBlackTreeNode {
39
72
  }
40
73
  exports.TreeMultiMapNode = TreeMultiMapNode;
41
74
  /**
42
- *
75
+ * Red-Black Tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
76
+ * @remarks Time O(1), Space O(1)
77
+ * @template K
78
+ * @template V
79
+ * @template R
43
80
  * @example
44
81
  * // players ranked by score with their equipment
45
82
  * type Equipment = {
@@ -207,15 +244,11 @@ exports.TreeMultiMapNode = TreeMultiMapNode;
207
244
  */
208
245
  class TreeMultiMap extends red_black_tree_1.RedBlackTree {
209
246
  /**
210
- * The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data
211
- * and options.
212
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
213
- * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
214
- * TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
215
- * the RedBlackTreeMulti
216
- * @param [options] - The `options` parameter in the constructor is of type
217
- * `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
218
- * additional options for configuring the TreeMultiMap instance.
247
+ * Create a TreeMultiMap and optionally bulk-insert items.
248
+ * @remarks Time O(N log N), Space O(N)
249
+ * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
250
+ * @param [options] - Options for TreeMultiMap (comparator, reverse, map mode).
251
+ * @returns New TreeMultiMap instance.
219
252
  */
220
253
  constructor(keysNodesEntriesOrRaws = [], options) {
221
254
  super([], Object.assign({}, options));
@@ -223,52 +256,15 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
223
256
  this.addMany(keysNodesEntriesOrRaws);
224
257
  }
225
258
  }
226
- /**
227
- * Time Complexity: O(1)
228
- * Space Complexity: O(1)
229
- *
230
- * The `createTree` function in TypeScript overrides the default implementation to create a new
231
- * TreeMultiMap with specified options.
232
- * @param [options] - The `options` parameter in the `createTree` method is of type
233
- * `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration
234
- * options when creating a new `TreeMultiMap` instance. It includes properties such as
235
- * `iterationType`, `specifyComparable
236
- * @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial
237
- * data and the provided options merged with the existing properties of the current object.
238
- */
239
- createTree(options) {
240
- return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse, isMapMode: this._isMapMode }, options));
241
- }
242
- /**
243
- * Time Complexity: O(1)
244
- * Space Complexity: O(1)
245
- *
246
- * The function `createNode` overrides the creation of a new TreeMultiMapNode with a specified key
247
- * and value array.
248
- * @param {K} key - The `key` parameter represents the key of the node being created in the
249
- * `TreeMultiMap`.
250
- * @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
251
- * values associated with a specific key in the TreeMultiMap data structure.
252
- * @returns A new instance of `TreeMultiMapNode<K, V>` is being returned with the specified key and
253
- * value. If `_isMapMode` is true, an empty array is passed as the value, otherwise the provided
254
- * value is used.
255
- */
256
- createNode(key, value = []) {
259
+ _createNode(key, value = []) {
257
260
  return new TreeMultiMapNode(key, this._isMapMode ? [] : value);
258
261
  }
259
262
  /**
260
- * Time Complexity: O(log n)
261
- * Space Complexity: O(log n)
262
- *
263
- * The function overrides the add method to handle different types of input for a TreeMultiMap data
264
- * structure.
265
- * @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
266
- * be added to the TreeMultiMap. It can be of type `K`, which is the key type of the TreeMultiMap, or
267
- * it can be a TreeMultiMapNode containing the key and its
268
- * @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
269
- * that you want to add to the TreeMultiMap. It can contain one or more values of type `V`.
270
- * @returns The `add` method is returning a boolean value, which indicates whether the operation was
271
- * successful or not.
263
+ * Insert a value or a list of values into the multimap. If the key exists, values are appended.
264
+ * @remarks Time O(log N + M), Space O(1)
265
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry.
266
+ * @param [value] - Single value to add when a bare key is provided.
267
+ * @returns True if inserted or appended; false if ignored.
272
268
  */
273
269
  add(keyNodeOrEntry, value) {
274
270
  if (this.isRealNode(keyNodeOrEntry))
@@ -318,20 +314,11 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
318
314
  return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
319
315
  }
320
316
  /**
321
- * Time Complexity: O(log n)
322
- * Space Complexity: O(log n)
323
- *
324
- * The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure
325
- * and deletes the entire node if no values are left for that key.
326
- * @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
327
- * parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an
328
- * array of values, or just a key itself.
329
- * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
330
- * value that you want to remove from the multi-map data structure associated with a particular key.
331
- * The function checks if the value exists in the array of values associated with the key, and if
332
- * found, removes it from the array.
333
- * @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was
334
- * successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise.
317
+ * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
318
+ * @remarks Time O(log N), Space O(1)
319
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
320
+ * @param value - Value to remove from the bucket.
321
+ * @returns True if the value was removed; false if not found.
335
322
  */
336
323
  deleteValue(keyNodeOrEntry, value) {
337
324
  const values = this.get(keyNodeOrEntry);
@@ -340,7 +327,6 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
340
327
  if (index === -1)
341
328
  return false;
342
329
  values.splice(index, 1);
343
- // If no values left, remove the entire node
344
330
  if (values.length === 0)
345
331
  this.delete(keyNodeOrEntry);
346
332
  return true;
@@ -348,17 +334,51 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
348
334
  return false;
349
335
  }
350
336
  /**
351
- * Time Complexity: O(n)
352
- * Space Complexity: O(n)
353
- *
354
- * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
355
- * structure.
356
- * @returns The `cloned` object is being returned.
337
+ * Create a new tree by mapping each [key, values] bucket.
338
+ * @remarks Time O(N log N), Space O(N)
339
+ * @template MK
340
+ * @template MV
341
+ * @template MR
342
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
343
+ * @param [options] - Options for the output tree.
344
+ * @param [thisArg] - Value for `this` inside the callback.
345
+ * @returns A new RedBlackTree (or TreeMultiMap when mapping to array values; see overloads).
346
+ */
347
+ map(callback, options, thisArg) {
348
+ const out = this._createLike([], options);
349
+ let i = 0;
350
+ for (const [k, v] of this)
351
+ out.add(callback.call(thisArg, k, v, i++, this));
352
+ return out;
353
+ }
354
+ /**
355
+ * (Protected) Create an empty instance of the same concrete class.
356
+ * @remarks Time O(1), Space O(1)
357
+ * @template TK
358
+ * @template TV
359
+ * @template TR
360
+ * @param [options] - Optional constructor options for the like-kind instance.
361
+ * @returns An empty like-kind instance.
362
+ */
363
+ _createInstance(options) {
364
+ var _a, _b;
365
+ const Ctor = this.constructor;
366
+ return new Ctor([], Object.assign(Object.assign({}, ((_b = (_a = this._snapshotOptions) === null || _a === void 0 ? void 0 : _a.call(this)) !== null && _b !== void 0 ? _b : {})), (options !== null && options !== void 0 ? options : {})));
367
+ }
368
+ /**
369
+ * (Protected) Create a like-kind instance and seed it from an iterable.
370
+ * @remarks Time O(N log N), Space O(N)
371
+ * @template TK
372
+ * @template TV
373
+ * @template TR
374
+ * @param iter - Iterable used to seed the new tree.
375
+ * @param [options] - Options merged with the current snapshot.
376
+ * @returns A like-kind RedBlackTree built from the iterable.
357
377
  */
358
- clone() {
359
- const cloned = this.createTree();
360
- this._clone(cloned);
361
- return cloned;
378
+ _createLike(iter = [], options) {
379
+ var _a, _b;
380
+ const Ctor = this.constructor;
381
+ return new Ctor(iter, Object.assign(Object.assign({}, ((_b = (_a = this._snapshotOptions) === null || _a === void 0 ? void 0 : _a.call(this)) !== null && _b !== void 0 ? _b : {})), (options !== null && options !== void 0 ? options : {})));
362
382
  }
363
383
  }
364
384
  exports.TreeMultiMap = TreeMultiMap;