directed-graph-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,26 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BTNOptKeyOrNull, TreeMultiMapOptions } from '../../types';
8
+
9
+ import type { BTNOptKeyOrNull, ElemOf, EntryCallback, RedBlackTreeOptions, TreeMultiMapOptions } from '../../types';
9
10
  import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
10
11
  import { IBinaryTree } from '../../interfaces';
11
12
 
13
+ /**
14
+ * Node used by TreeMultiMap; stores the key with a bucket of values (array).
15
+ * @remarks Time O(1), Space O(1)
16
+ * @template K
17
+ * @template V
18
+ */
12
19
  export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> {
13
20
  override parent?: TreeMultiMapNode<K, V> = undefined;
14
21
 
15
22
  /**
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`.
23
+ * Create a TreeMultiMap node with an optional value bucket.
24
+ * @remarks Time O(1), Space O(1)
25
+ * @param key - Key of the node.
26
+ * @param [value] - Initial array of values.
27
+ * @returns New TreeMultiMapNode instance.
23
28
  */
24
29
  constructor(key: K, value?: V[]) {
25
30
  super(key, value);
@@ -27,10 +32,21 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
27
32
 
28
33
  override _left?: TreeMultiMapNode<K, V> | null | undefined = undefined;
29
34
 
35
+ /**
36
+ * Get the left child pointer.
37
+ * @remarks Time O(1), Space O(1)
38
+ * @returns Left child node, or null/undefined.
39
+ */
30
40
  override get left(): TreeMultiMapNode<K, V> | null | undefined {
31
41
  return this._left;
32
42
  }
33
43
 
44
+ /**
45
+ * Set the left child and update its parent pointer.
46
+ * @remarks Time O(1), Space O(1)
47
+ * @param v - New left child node, or null/undefined.
48
+ * @returns void
49
+ */
34
50
  override set left(v: TreeMultiMapNode<K, V> | null | undefined) {
35
51
  if (v) {
36
52
  v.parent = this;
@@ -40,10 +56,21 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
40
56
 
41
57
  override _right?: TreeMultiMapNode<K, V> | null | undefined = undefined;
42
58
 
59
+ /**
60
+ * Get the right child pointer.
61
+ * @remarks Time O(1), Space O(1)
62
+ * @returns Right child node, or null/undefined.
63
+ */
43
64
  override get right(): TreeMultiMapNode<K, V> | null | undefined {
44
65
  return this._right;
45
66
  }
46
67
 
68
+ /**
69
+ * Set the right child and update its parent pointer.
70
+ * @remarks Time O(1), Space O(1)
71
+ * @param v - New right child node, or null/undefined.
72
+ * @returns void
73
+ */
47
74
  override set right(v: TreeMultiMapNode<K, V> | null | undefined) {
48
75
  if (v) {
49
76
  v.parent = this;
@@ -53,7 +80,11 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
53
80
  }
54
81
 
55
82
  /**
56
- *
83
+ * Red-Black Tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
84
+ * @remarks Time O(1), Space O(1)
85
+ * @template K
86
+ * @template V
87
+ * @template R
57
88
  * @example
58
89
  * // players ranked by score with their equipment
59
90
  * type Equipment = {
@@ -219,20 +250,16 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
219
250
  * // ]
220
251
  * // ]
221
252
  */
222
- export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object>
223
- extends RedBlackTree<K, V[], R, MK, MV[], MR>
224
- implements IBinaryTree<K, V[], R, MK, MV, MR>
253
+ export class TreeMultiMap<K = any, V = any, R extends object = object>
254
+ extends RedBlackTree<K, V[], R>
255
+ implements IBinaryTree<K, V[], R>
225
256
  {
226
257
  /**
227
- * The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data
228
- * and options.
229
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
230
- * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
231
- * TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
232
- * the RedBlackTreeMulti
233
- * @param [options] - The `options` parameter in the constructor is of type
234
- * `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
235
- * additional options for configuring the TreeMultiMap instance.
258
+ * Create a TreeMultiMap and optionally bulk-insert items.
259
+ * @remarks Time O(N log N), Space O(N)
260
+ * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
261
+ * @param [options] - Options for TreeMultiMap (comparator, reverse, map mode).
262
+ * @returns New TreeMultiMap instance.
236
263
  */
237
264
  constructor(
238
265
  keysNodesEntriesOrRaws: Iterable<
@@ -246,45 +273,7 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
246
273
  }
247
274
  }
248
275
 
249
- /**
250
- * Time Complexity: O(1)
251
- * Space Complexity: O(1)
252
- *
253
- * The `createTree` function in TypeScript overrides the default implementation to create a new
254
- * TreeMultiMap with specified options.
255
- * @param [options] - The `options` parameter in the `createTree` method is of type
256
- * `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration
257
- * options when creating a new `TreeMultiMap` instance. It includes properties such as
258
- * `iterationType`, `specifyComparable
259
- * @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial
260
- * data and the provided options merged with the existing properties of the current object.
261
- */
262
- override createTree(options?: TreeMultiMapOptions<K, V[], R>) {
263
- return new TreeMultiMap<K, V, R, MK, MV, MR>([], {
264
- iterationType: this.iterationType,
265
- specifyComparable: this._specifyComparable,
266
- toEntryFn: this._toEntryFn,
267
- isReverse: this._isReverse,
268
- isMapMode: this._isMapMode,
269
- ...options
270
- });
271
- }
272
-
273
- /**
274
- * Time Complexity: O(1)
275
- * Space Complexity: O(1)
276
- *
277
- * The function `createNode` overrides the creation of a new TreeMultiMapNode with a specified key
278
- * and value array.
279
- * @param {K} key - The `key` parameter represents the key of the node being created in the
280
- * `TreeMultiMap`.
281
- * @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
282
- * values associated with a specific key in the TreeMultiMap data structure.
283
- * @returns A new instance of `TreeMultiMapNode<K, V>` is being returned with the specified key and
284
- * value. If `_isMapMode` is true, an empty array is passed as the value, otherwise the provided
285
- * value is used.
286
- */
287
- override createNode(key: K, value: V[] = []): TreeMultiMapNode<K, V> {
276
+ override _createNode(key: K, value: V[] = []): TreeMultiMapNode<K, V> {
288
277
  return new TreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
289
278
  }
290
279
 
@@ -295,18 +284,11 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
295
284
  override add(key: K, value: V): boolean;
296
285
 
297
286
  /**
298
- * Time Complexity: O(log n)
299
- * Space Complexity: O(log n)
300
- *
301
- * The function overrides the add method to handle different types of input for a TreeMultiMap data
302
- * structure.
303
- * @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
304
- * be added to the TreeMultiMap. It can be of type `K`, which is the key type of the TreeMultiMap, or
305
- * it can be a TreeMultiMapNode containing the key and its
306
- * @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
307
- * that you want to add to the TreeMultiMap. It can contain one or more values of type `V`.
308
- * @returns The `add` method is returning a boolean value, which indicates whether the operation was
309
- * successful or not.
287
+ * Insert a value or a list of values into the multimap. If the key exists, values are appended.
288
+ * @remarks Time O(log N + M), Space O(1)
289
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry.
290
+ * @param [value] - Single value to add when a bare key is provided.
291
+ * @returns True if inserted or appended; false if ignored.
310
292
  */
311
293
  override add(
312
294
  keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
@@ -360,20 +342,11 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
360
342
  }
361
343
 
362
344
  /**
363
- * Time Complexity: O(log n)
364
- * Space Complexity: O(log n)
365
- *
366
- * The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure
367
- * and deletes the entire node if no values are left for that key.
368
- * @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
369
- * parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an
370
- * array of values, or just a key itself.
371
- * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
372
- * value that you want to remove from the multi-map data structure associated with a particular key.
373
- * The function checks if the value exists in the array of values associated with the key, and if
374
- * found, removes it from the array.
375
- * @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was
376
- * successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise.
345
+ * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
346
+ * @remarks Time O(log N), Space O(1)
347
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
348
+ * @param value - Value to remove from the bucket.
349
+ * @returns True if the value was removed; false if not found.
377
350
  */
378
351
  deleteValue(
379
352
  keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
@@ -385,7 +358,6 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
385
358
  if (index === -1) return false;
386
359
  values.splice(index, 1);
387
360
 
388
- // If no values left, remove the entire node
389
361
  if (values.length === 0) this.delete(keyNodeOrEntry);
390
362
 
391
363
  return true;
@@ -393,17 +365,79 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
393
365
  return false;
394
366
  }
395
367
 
368
+ override map<MK = K, MVArr extends unknown[] = V[], MR extends object = object>(
369
+ callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
370
+ options?: Partial<RedBlackTreeOptions<MK, MVArr, MR>>,
371
+ thisArg?: unknown
372
+ ): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
373
+
374
+ override map<MK = K, MV = V[], MR extends object = object>(
375
+ callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
376
+ options?: Partial<RedBlackTreeOptions<MK, MV, MR>>,
377
+ thisArg?: unknown
378
+ ): RedBlackTree<MK, MV, MR>;
379
+
380
+ /**
381
+ * Create a new tree by mapping each [key, values] bucket.
382
+ * @remarks Time O(N log N), Space O(N)
383
+ * @template MK
384
+ * @template MV
385
+ * @template MR
386
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
387
+ * @param [options] - Options for the output tree.
388
+ * @param [thisArg] - Value for `this` inside the callback.
389
+ * @returns A new RedBlackTree (or TreeMultiMap when mapping to array values; see overloads).
390
+ */
391
+ override map<MK, MV, MR extends object>(
392
+ callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
393
+ options?: Partial<RedBlackTreeOptions<MK, MV, MR>>,
394
+ thisArg?: unknown
395
+ ): RedBlackTree<MK, MV, MR> {
396
+ const out = this._createLike<MK, MV, MR>([], options);
397
+ let i = 0;
398
+ for (const [k, v] of this) out.add(callback.call(thisArg, k, v, i++, this));
399
+ return out;
400
+ }
401
+
396
402
  /**
397
- * Time Complexity: O(n)
398
- * Space Complexity: O(n)
399
- *
400
- * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
401
- * structure.
402
- * @returns The `cloned` object is being returned.
403
+ * (Protected) Create an empty instance of the same concrete class.
404
+ * @remarks Time O(1), Space O(1)
405
+ * @template TK
406
+ * @template TV
407
+ * @template TR
408
+ * @param [options] - Optional constructor options for the like-kind instance.
409
+ * @returns An empty like-kind instance.
403
410
  */
404
- override clone() {
405
- const cloned = this.createTree();
406
- this._clone(cloned);
407
- return cloned;
411
+ protected override _createInstance<TK = K, TV = V, TR extends object = R>(
412
+ options?: Partial<RedBlackTreeOptions<TK, TV, TR>>
413
+ ): this {
414
+ const Ctor = this.constructor as unknown as new (
415
+ iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
416
+ opts?: RedBlackTreeOptions<TK, TV, TR>
417
+ ) => RedBlackTree<TK, TV, TR>;
418
+ return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
419
+ }
420
+
421
+ /**
422
+ * (Protected) Create a like-kind instance and seed it from an iterable.
423
+ * @remarks Time O(N log N), Space O(N)
424
+ * @template TK
425
+ * @template TV
426
+ * @template TR
427
+ * @param iter - Iterable used to seed the new tree.
428
+ * @param [options] - Options merged with the current snapshot.
429
+ * @returns A like-kind RedBlackTree built from the iterable.
430
+ */
431
+ protected override _createLike<TK = K, TV = V, TR extends object = R>(
432
+ iter: Iterable<
433
+ TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
434
+ > = [],
435
+ options?: Partial<RedBlackTreeOptions<TK, TV, TR>>
436
+ ): RedBlackTree<TK, TV, TR> {
437
+ const Ctor = this.constructor as unknown as new (
438
+ iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
439
+ opts?: RedBlackTreeOptions<TK, TV, TR>
440
+ ) => RedBlackTree<TK, TV, TR>;
441
+ return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
408
442
  }
409
443
  }