directed-graph-typed 1.53.6 → 1.53.8

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 (61) hide show
  1. package/dist/common/index.d.ts +12 -0
  2. package/dist/common/index.js +28 -0
  3. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  4. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +9 -12
  5. package/dist/data-structures/binary-tree/avl-tree.js +2 -2
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +55 -20
  7. package/dist/data-structures/binary-tree/binary-tree.js +102 -68
  8. package/dist/data-structures/binary-tree/bst.d.ts +131 -37
  9. package/dist/data-structures/binary-tree/bst.js +222 -69
  10. package/dist/data-structures/binary-tree/index.d.ts +1 -1
  11. package/dist/data-structures/binary-tree/index.js +1 -1
  12. package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +53 -0
  13. package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +56 -3
  14. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  15. package/dist/data-structures/binary-tree/tree-multi-map.js +7 -7
  16. package/dist/data-structures/hash/hash-map.d.ts +30 -0
  17. package/dist/data-structures/hash/hash-map.js +30 -0
  18. package/dist/data-structures/heap/heap.d.ts +26 -9
  19. package/dist/data-structures/heap/heap.js +37 -17
  20. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +54 -9
  21. package/dist/data-structures/linked-list/doubly-linked-list.js +80 -19
  22. package/dist/data-structures/linked-list/singly-linked-list.d.ts +35 -2
  23. package/dist/data-structures/linked-list/singly-linked-list.js +55 -11
  24. package/dist/data-structures/queue/deque.d.ts +37 -8
  25. package/dist/data-structures/queue/deque.js +73 -29
  26. package/dist/data-structures/queue/queue.d.ts +41 -1
  27. package/dist/data-structures/queue/queue.js +51 -9
  28. package/dist/data-structures/stack/stack.d.ts +27 -10
  29. package/dist/data-structures/stack/stack.js +39 -20
  30. package/dist/data-structures/trie/trie.d.ts +111 -6
  31. package/dist/data-structures/trie/trie.js +123 -14
  32. package/dist/index.d.ts +2 -1
  33. package/dist/index.js +2 -1
  34. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  35. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  36. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  37. package/dist/types/utils/utils.d.ts +10 -6
  38. package/dist/utils/utils.js +4 -2
  39. package/package.json +2 -2
  40. package/src/common/index.ts +25 -0
  41. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -11
  42. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  43. package/src/data-structures/binary-tree/binary-tree.ts +110 -66
  44. package/src/data-structures/binary-tree/bst.ts +232 -72
  45. package/src/data-structures/binary-tree/index.ts +1 -1
  46. package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +56 -3
  47. package/src/data-structures/binary-tree/tree-multi-map.ts +6 -6
  48. package/src/data-structures/hash/hash-map.ts +30 -0
  49. package/src/data-structures/heap/heap.ts +72 -49
  50. package/src/data-structures/linked-list/doubly-linked-list.ts +173 -105
  51. package/src/data-structures/linked-list/singly-linked-list.ts +61 -11
  52. package/src/data-structures/queue/deque.ts +72 -28
  53. package/src/data-structures/queue/queue.ts +50 -7
  54. package/src/data-structures/stack/stack.ts +39 -20
  55. package/src/data-structures/trie/trie.ts +123 -13
  56. package/src/index.ts +2 -1
  57. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  58. package/src/types/data-structures/binary-tree/bst.ts +3 -2
  59. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  60. package/src/types/utils/utils.ts +16 -10
  61. package/src/utils/utils.ts +4 -2
@@ -5,9 +5,10 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparator, CP, DFSOrderPattern, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types';
8
+ import type { BSTNested, BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types';
9
9
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
10
10
  import { IBinaryTree } from '../../interfaces';
11
+ import { Range } from '../../common';
11
12
  export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
12
13
  parent?: NODE;
13
14
  constructor(key: K, value?: V);
@@ -45,6 +46,62 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
45
46
  * 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
46
47
  * 6. Balance Variability: Can become unbalanced; special types maintain balance.
47
48
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
49
+ * @example
50
+ * // Merge 3 sorted datasets
51
+ * const dataset1 = new BST<number, string>([
52
+ * [1, 'A'],
53
+ * [7, 'G']
54
+ * ]);
55
+ * const dataset2 = [
56
+ * [2, 'B'],
57
+ * [6, 'F']
58
+ * ];
59
+ * const dataset3 = new BST<number, string>([
60
+ * [3, 'C'],
61
+ * [5, 'E'],
62
+ * [4, 'D']
63
+ * ]);
64
+ *
65
+ * // Merge datasets into a single BinarySearchTree
66
+ * const merged = new BST<number, string>(dataset1);
67
+ * merged.addMany(dataset2);
68
+ * merged.merge(dataset3);
69
+ *
70
+ * // Verify merged dataset is in sorted order
71
+ * console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
72
+ * @example
73
+ * // Find elements in a range
74
+ * const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
75
+ * console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
76
+ * console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
77
+ * console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
78
+ * console.log(bst.rangeSearch([15, 20])); // [15, 18]
79
+ * console.log(bst.search(new Range(15, 20, false))); // [18]
80
+ * @example
81
+ * // Find lowest common ancestor
82
+ * const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
83
+ *
84
+ * // LCA helper function
85
+ * const findLCA = (num1: number, num2: number): number | undefined => {
86
+ * const path1 = bst.getPathToRoot(num1);
87
+ * const path2 = bst.getPathToRoot(num2);
88
+ * // Find the first common ancestor
89
+ * return findFirstCommon(path1, path2);
90
+ * };
91
+ *
92
+ * function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
93
+ * for (const num of arr1) {
94
+ * if (arr2.indexOf(num) !== -1) {
95
+ * return num;
96
+ * }
97
+ * }
98
+ * return undefined;
99
+ * }
100
+ *
101
+ * // Assertions
102
+ * console.log(findLCA(3, 10)); // 7
103
+ * console.log(findLCA(5, 35)); // 15
104
+ * console.log(findLCA(20, 30)); // 25
48
105
  */
49
106
  export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>> extends BinaryTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
50
107
  /**
@@ -62,6 +119,13 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
62
119
  * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
63
120
  */
64
121
  get root(): OptNode<NODE>;
122
+ protected _isReverse: boolean;
123
+ /**
124
+ * The above function is a getter method in TypeScript that returns the value of the private property
125
+ * `_isReverse`.
126
+ * @returns The `isReverse` property of the object, which is a boolean value.
127
+ */
128
+ get isReverse(): boolean;
65
129
  /**
66
130
  * The function creates a new BSTNode with the given key and value and returns it.
67
131
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@@ -88,7 +152,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
88
152
  * value associated with a key in a key-value pair.
89
153
  * @returns either a NODE object or undefined.
90
154
  */
91
- keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined];
155
+ protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined];
92
156
  /**
93
157
  * Time Complexity: O(log n)
94
158
  * Space Complexity: O(log n)
@@ -118,7 +182,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
118
182
  * @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
119
183
  * type `K`.
120
184
  * @returns The `override isKey(key: any): key is K` function is returning a boolean value based on
121
- * the result of the `isComparable` function with the condition `this.comparator !==
185
+ * the result of the `isComparable` function with the condition `this._compare !==
122
186
  * this._DEFAULT_COMPARATOR`.
123
187
  */
124
188
  isKey(key: any): key is K;
@@ -156,30 +220,67 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
156
220
  * successfully inserted into the data structure.
157
221
  */
158
222
  addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
223
+ /**
224
+ * Time Complexity: O(n)
225
+ * Space Complexity: O(1)
226
+ *
227
+ * The `merge` function overrides the base class method by adding elements from another
228
+ * binary search tree.
229
+ * @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
230
+ * value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
231
+ */
232
+ merge(anotherTree: BST<K, V, R, NODE, TREE>): void;
159
233
  /**
160
234
  * Time Complexity: O(log n)
161
235
  * Space Complexity: O(k + log n)
162
236
  *
163
- * The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
164
- * given keyNodeEntryRawOrPredicate and iteration type.
165
- * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
166
- * parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
167
- * key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
168
- * included in the result.
169
- * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
170
- * determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
171
- * that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
172
- * and
173
- * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
174
- * `getNodes` method is used to specify the starting point for traversing the tree when searching for
175
- * nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
176
- * should begin. If not explicitly provided, the default value for `begin
177
- * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
178
- * specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
179
- * have two possible values:
180
- * @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
237
+ * The function `search` in TypeScript overrides the search behavior in a binary tree structure based
238
+ * on specified criteria.
239
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
240
+ * `keyNodeEntryRawOrPredicate` parameter in the `override search` method can accept one of the
241
+ * following types:
242
+ * @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
243
+ * search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
244
+ * search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
245
+ * @param {C} callback - The `callback` parameter in the `override search` function is a function
246
+ * that will be called on each node that matches the search criteria. It is of type `C`, which
247
+ * extends `NodeCallback<NODE>`. The callback function should accept a node of type `NODE` as its
248
+ * argument and
249
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `override search`
250
+ * method represents the node from which the search operation will begin. It is the starting point
251
+ * for searching within the tree data structure. The method ensures that the `startNode` is a valid
252
+ * node before proceeding with the search operation. If the `
253
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `override search`
254
+ * function determines the type of iteration to be used during the search operation. It can have two
255
+ * possible values:
256
+ * @returns The `override search` method returns an array of values that match the search criteria
257
+ * specified by the input parameters. The method performs a search operation on a binary tree
258
+ * structure based on the provided key, predicate, and other options. The search results are
259
+ * collected in an array and returned as the output of the method.
181
260
  */
182
- getNodes(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, onlyOne?: boolean, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): NODE[];
261
+ search<C extends NodeCallback<NODE>>(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[];
262
+ /**
263
+ * Time Complexity: O(log n)
264
+ * Space Complexity: O(n)
265
+ *
266
+ * The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
267
+ * @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
268
+ * either a `Range` object or an array of two elements representing the range boundaries.
269
+ * @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
270
+ * function that is used to process each node that is found within the specified range during the
271
+ * search operation. It is of type `NodeCallback<NODE>`, where `NODE` is the type of nodes in the
272
+ * data structure.
273
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `rangeSearch`
274
+ * function represents the node from which the search for nodes within the specified range will
275
+ * begin. It is the starting point for the range search operation.
276
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
277
+ * is used to specify the type of iteration to be performed during the search operation. It has a
278
+ * default value of `this.iterationType`, which suggests that it is likely a property of the class or
279
+ * object that the `rangeSearch`
280
+ * @returns The `rangeSearch` function is returning the result of calling the `search` method with
281
+ * the specified parameters.
282
+ */
283
+ rangeSearch<C extends NodeCallback<NODE>>(range: Range<K> | [K, K], callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[];
183
284
  /**
184
285
  * Time Complexity: O(log n)
185
286
  * Space Complexity: O(1)
@@ -201,20 +302,6 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
201
302
  * returns the first node found or `undefined` if no node is found.
202
303
  */
203
304
  getNode(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, startNode?: R | BSTNOptKeyOrNode<K, NODE>, iterationType?: IterationType): OptNode<NODE>;
204
- /**
205
- * Time Complexity: O(log n)
206
- * Space Complexity: O(1)
207
- *
208
- * The function `getNodeByKey` returns a node with a specific key from a tree data structure.
209
- * @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
210
- * is typically a unique identifier or a value that can be used to determine the position of the node
211
- * in the tree structure.
212
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
213
- * parameter that specifies the type of iteration to be used when searching for a node in the tree.
214
- * It has a default value of `'ITERATIVE'`.
215
- * @returns The method is returning a NODE object or undefined.
216
- */
217
- getNodeByKey(key: K, iterationType?: IterationType): OptNode<NODE>;
218
305
  /**
219
306
  * Time complexity: O(n)
220
307
  * Space complexity: O(n)
@@ -321,17 +408,24 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
321
408
  * @returns a boolean value.
322
409
  */
323
410
  isAVLBalanced(iterationType?: IterationType): boolean;
324
- protected _DEFAULT_COMPARATOR: (a: K, b: K) => number;
325
411
  protected _comparator: Comparator<K>;
326
412
  /**
327
413
  * The function returns the value of the _comparator property.
328
414
  * @returns The `_comparator` property is being returned.
329
415
  */
330
416
  get comparator(): Comparator<K>;
417
+ protected _extractComparable?: (key: K) => Comparable;
418
+ /**
419
+ * This function returns the value of the `_extractComparable` property.
420
+ * @returns The method `extractComparable()` is being returned, which is a getter method for the
421
+ * `_extractComparable` property.
422
+ */
423
+ get extractComparable(): ((key: K) => Comparable) | undefined;
331
424
  /**
332
425
  * The function sets the root of a tree-like structure and updates the parent property of the new
333
426
  * root.
334
427
  * @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
335
428
  */
336
429
  protected _setRoot(v: OptNode<NODE>): void;
430
+ protected _compare(a: K, b: K): number;
337
431
  }