priority-queue-typed 1.48.9 → 1.49.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 (82) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +16 -11
  2. package/dist/data-structures/binary-tree/avl-tree.js +12 -4
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +70 -61
  4. package/dist/data-structures/binary-tree/binary-tree.js +25 -21
  5. package/dist/data-structures/binary-tree/bst.d.ts +20 -13
  6. package/dist/data-structures/binary-tree/bst.js +12 -3
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +19 -25
  8. package/dist/data-structures/binary-tree/rb-tree.js +21 -35
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +20 -13
  10. package/dist/data-structures/binary-tree/tree-multimap.js +12 -3
  11. package/dist/data-structures/graph/abstract-graph.d.ts +9 -3
  12. package/dist/data-structures/graph/abstract-graph.js +27 -31
  13. package/dist/data-structures/graph/directed-graph.d.ts +8 -1
  14. package/dist/data-structures/graph/directed-graph.js +1 -8
  15. package/dist/data-structures/graph/map-graph.d.ts +1 -1
  16. package/dist/data-structures/graph/undirected-graph.d.ts +8 -1
  17. package/dist/data-structures/graph/undirected-graph.js +1 -8
  18. package/dist/data-structures/hash/hash-map.d.ts +14 -2
  19. package/dist/data-structures/hash/hash-map.js +19 -8
  20. package/dist/data-structures/hash/hash-table.d.ts +2 -2
  21. package/dist/data-structures/heap/heap.d.ts +14 -3
  22. package/dist/data-structures/heap/heap.js +12 -0
  23. package/dist/data-structures/heap/max-heap.d.ts +11 -1
  24. package/dist/data-structures/heap/max-heap.js +10 -7
  25. package/dist/data-structures/heap/min-heap.d.ts +11 -1
  26. package/dist/data-structures/heap/min-heap.js +10 -7
  27. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +8 -2
  28. package/dist/data-structures/linked-list/doubly-linked-list.js +6 -7
  29. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  30. package/dist/data-structures/linked-list/singly-linked-list.js +0 -7
  31. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +0 -7
  33. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
  34. package/dist/data-structures/priority-queue/min-priority-queue.js +0 -7
  35. package/dist/data-structures/priority-queue/priority-queue.d.ts +9 -1
  36. package/dist/data-structures/priority-queue/priority-queue.js +8 -7
  37. package/dist/data-structures/queue/deque.d.ts +6 -5
  38. package/dist/data-structures/queue/deque.js +6 -12
  39. package/dist/data-structures/queue/queue.d.ts +18 -3
  40. package/dist/data-structures/queue/queue.js +16 -6
  41. package/dist/data-structures/stack/stack.d.ts +15 -5
  42. package/dist/data-structures/stack/stack.js +7 -4
  43. package/dist/data-structures/trie/trie.d.ts +13 -3
  44. package/dist/data-structures/trie/trie.js +11 -8
  45. package/dist/interfaces/binary-tree.d.ts +4 -4
  46. package/dist/types/common.d.ts +32 -8
  47. package/dist/types/common.js +22 -1
  48. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
  49. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -22
  50. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
  51. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  52. package/package.json +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +23 -13
  54. package/src/data-structures/binary-tree/binary-tree.ts +93 -97
  55. package/src/data-structures/binary-tree/bst.ts +26 -15
  56. package/src/data-structures/binary-tree/rb-tree.ts +33 -48
  57. package/src/data-structures/binary-tree/tree-multimap.ts +32 -14
  58. package/src/data-structures/graph/abstract-graph.ts +35 -25
  59. package/src/data-structures/graph/directed-graph.ts +2 -2
  60. package/src/data-structures/graph/map-graph.ts +1 -1
  61. package/src/data-structures/graph/undirected-graph.ts +2 -2
  62. package/src/data-structures/hash/hash-map.ts +20 -3
  63. package/src/data-structures/hash/hash-table.ts +3 -3
  64. package/src/data-structures/heap/heap.ts +14 -3
  65. package/src/data-structures/heap/max-heap.ts +11 -2
  66. package/src/data-structures/heap/min-heap.ts +11 -2
  67. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -3
  68. package/src/data-structures/linked-list/singly-linked-list.ts +3 -3
  69. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  70. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  71. package/src/data-structures/priority-queue/priority-queue.ts +9 -2
  72. package/src/data-structures/queue/deque.ts +7 -9
  73. package/src/data-structures/queue/queue.ts +18 -3
  74. package/src/data-structures/stack/stack.ts +16 -6
  75. package/src/data-structures/trie/trie.ts +13 -4
  76. package/src/interfaces/binary-tree.ts +5 -5
  77. package/src/types/common.ts +37 -12
  78. package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
  79. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
  80. package/src/types/data-structures/binary-tree/bst.ts +0 -1
  81. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  82. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNodeExemplar } from '../../types';
8
+ import type { BSTNested, BSTNKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNExemplar, BTNKeyOrNode } from '../../types';
9
9
  import { BSTVariant, CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
@@ -46,12 +46,12 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
46
46
  /**
47
47
  * This is the constructor function for a binary search tree class in TypeScript, which initializes
48
48
  * the tree with optional elements and options.
49
- * @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
49
+ * @param [elements] - An optional iterable of BTNExemplar objects that will be added to the
50
50
  * binary search tree.
51
51
  * @param [options] - The `options` parameter is an optional object that can contain additional
52
52
  * configuration options for the binary search tree. It can have the following properties:
53
53
  */
54
- constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>);
54
+ constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>);
55
55
  protected _root?: N;
56
56
  get root(): N | undefined;
57
57
  protected _variant: BSTVariant;
@@ -75,19 +75,19 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
75
75
  createTree(options?: Partial<BSTOptions<K>>): TREE;
76
76
  /**
77
77
  * The function checks if an exemplar is an instance of BSTNode.
78
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
78
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
79
79
  * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
80
80
  */
81
- isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
81
+ isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
82
82
  /**
83
83
  * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
84
84
  * otherwise it returns undefined.
85
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
85
+ * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
86
86
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
87
87
  * `exemplarToNode` function. It represents the value associated with the exemplar node.
88
88
  * @returns a node of type N or undefined.
89
89
  */
90
- exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
90
+ exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined;
91
91
  /**
92
92
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
93
93
  * Space Complexity: O(1) - Constant space is used.
@@ -104,7 +104,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
104
104
  * @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
105
105
  * node was not added.
106
106
  */
107
- add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
107
+ add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined;
108
108
  /**
109
109
  * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
110
110
  * Space Complexity: O(k) - Additional space is required for the sorted array.
@@ -129,7 +129,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
129
129
  * `this.iterationType`, which suggests that it is a property of the current object.
130
130
  * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
131
131
  */
132
- addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
132
+ addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
133
133
  /**
134
134
  * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
135
135
  * Space Complexity: O(n) - Additional space is required for the sorted array.
@@ -149,7 +149,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
149
149
  * the key of the leftmost node if the comparison result is greater than, and the key of the
150
150
  * rightmost node otherwise. If no node is found, it returns 0.
151
151
  */
152
- lastKey(beginRoot?: BSTNodeKeyOrNode<K, N>): K | undefined;
152
+ lastKey(beginRoot?: BSTNKeyOrNode<K, N>): K | undefined;
153
153
  /**
154
154
  * Time Complexity: O(log n) - Average case for a balanced tree.
155
155
  * Space Complexity: O(1) - Constant space is used.
@@ -169,6 +169,13 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
169
169
  * found in the binary tree. If no node is found, it returns `undefined`.
170
170
  */
171
171
  getNodeByKey(key: K, iterationType?: IterationType): N | undefined;
172
+ /**
173
+ * The function "isNotNodeInstance" checks if a potential key is a K.
174
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
175
+ * data type.
176
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
177
+ */
178
+ isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K;
172
179
  /**
173
180
  * Time Complexity: O(log n) - Average case for a balanced tree.
174
181
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -182,7 +189,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
182
189
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
183
190
  * @returns either a node object (N) or undefined.
184
191
  */
185
- ensureNode(key: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): N | undefined;
192
+ ensureNode(key: BSTNKeyOrNode<K, N>, iterationType?: IterationType): N | undefined;
186
193
  /**
187
194
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
188
195
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -206,7 +213,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
206
213
  * performed on the binary tree. It can have two possible values:
207
214
  * @returns The method returns an array of nodes (`N[]`).
208
215
  */
209
- getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): N[];
216
+ getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNKeyOrNode<K, N>, iterationType?: IterationType): N[];
210
217
  /**
211
218
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
212
219
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -232,7 +239,7 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
232
239
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
233
240
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
234
241
  */
235
- lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): ReturnType<C>[];
242
+ lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNKeyOrNode<K, N>, iterationType?: IterationType): ReturnType<C>[];
236
243
  /**
237
244
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
238
245
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -58,7 +58,7 @@ class BST extends binary_tree_1.BinaryTree {
58
58
  /**
59
59
  * This is the constructor function for a binary search tree class in TypeScript, which initializes
60
60
  * the tree with optional elements and options.
61
- * @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
61
+ * @param [elements] - An optional iterable of BTNExemplar objects that will be added to the
62
62
  * binary search tree.
63
63
  * @param [options] - The `options` parameter is an optional object that can contain additional
64
64
  * configuration options for the binary search tree. It can have the following properties:
@@ -105,7 +105,7 @@ class BST extends binary_tree_1.BinaryTree {
105
105
  }
106
106
  /**
107
107
  * The function checks if an exemplar is an instance of BSTNode.
108
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
108
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
109
109
  * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
110
110
  */
111
111
  isNode(exemplar) {
@@ -114,7 +114,7 @@ class BST extends binary_tree_1.BinaryTree {
114
114
  /**
115
115
  * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
116
116
  * otherwise it returns undefined.
117
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
117
+ * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
118
118
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
119
119
  * `exemplarToNode` function. It represents the value associated with the exemplar node.
120
120
  * @returns a node of type N or undefined.
@@ -387,6 +387,15 @@ class BST extends binary_tree_1.BinaryTree {
387
387
  }
388
388
  }
389
389
  }
390
+ /**
391
+ * The function "isNotNodeInstance" checks if a potential key is a K.
392
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
393
+ * data type.
394
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
395
+ */
396
+ isNotNodeInstance(potentialKey) {
397
+ return !(potentialKey instanceof BSTNode);
398
+ }
390
399
  /**
391
400
  * Time Complexity: O(log n) - Average case for a balanced tree.
392
401
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { BiTreeDeleteResult, BTNCallback, BTNodeExemplar, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
8
+ import { BinaryTreeDeleteResult, BTNCallback, BTNExemplar, BTNKeyOrNode, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
9
9
  import { BST, BSTNode } from './bst';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  export declare class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
@@ -24,7 +24,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
24
24
  /**
25
25
  * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
26
26
  * initializes the tree with optional elements and options.
27
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
27
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
28
28
  * objects. It represents the initial elements that will be added to the RBTree during its
29
29
  * construction. If this parameter is provided, the `addMany` method is called to add all the
30
30
  * elements to the
@@ -32,7 +32,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
32
32
  * behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
33
33
  * only a subset of the properties defined in the `RBTreeOptions` interface.
34
34
  */
35
- constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>);
35
+ constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>);
36
36
  protected _root: N;
37
37
  get root(): N;
38
38
  protected _size: number;
@@ -60,20 +60,27 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
60
60
  createTree(options?: RBTreeOptions<K>): TREE;
61
61
  /**
62
62
  * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
63
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
63
+ * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
64
64
  * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
65
65
  * class.
66
66
  */
67
- isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
67
+ isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
68
+ /**
69
+ * The function "isNotNodeInstance" checks if a potential key is a K.
70
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
71
+ * data type.
72
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
73
+ */
74
+ isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K;
68
75
  /**
69
76
  * The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
70
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
77
+ * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
71
78
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
72
79
  * `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
73
80
  * is provided, it will be used when creating the new node. If no value is provided, the new node
74
81
  * @returns a node of type N or undefined.
75
82
  */
76
- exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
83
+ exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined;
77
84
  /**
78
85
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
79
86
  * Space Complexity: O(1)
@@ -90,7 +97,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
90
97
  * being added to the binary search tree.
91
98
  * @returns The method `add` returns either the newly added node (`N`) or `undefined`.
92
99
  */
93
- add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
100
+ add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined;
94
101
  /**
95
102
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
96
103
  * Space Complexity: O(1)
@@ -108,9 +115,9 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
108
115
  * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` and
109
116
  * returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
110
117
  * on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
111
- * @returns an array of `BiTreeDeleteResult<N>`.
118
+ * @returns an array of `BinaryTreeDeleteResult<N>`.
112
119
  */
113
- delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BiTreeDeleteResult<N>[];
120
+ delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeleteResult<N>[];
114
121
  /**
115
122
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
116
123
  * Space Complexity: O(1)
@@ -120,24 +127,11 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
120
127
  getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
121
128
  getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
122
129
  /**
123
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
130
+ * Time Complexity: O(log n)
124
131
  * Space Complexity: O(1)
125
132
  */
126
133
  /**
127
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
128
- * Space Complexity: O(1)
129
- *
130
- * The function returns the successor of a given node in a red-black tree.
131
- * @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
132
- * @returns the successor of the given RedBlackTreeNode.
133
- */
134
- getSuccessor(x: N): N | undefined;
135
- /**
136
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
137
- * Space Complexity: O(1)
138
- */
139
- /**
140
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
134
+ * Time Complexity: O(log n)
141
135
  * Space Complexity: O(1)
142
136
  *
143
137
  * The function returns the predecessor of a given node in a red-black tree.
@@ -10,7 +10,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.RedBlackTree = exports.RedBlackTreeNode = void 0;
11
11
  const types_1 = require("../../types");
12
12
  const bst_1 = require("./bst");
13
- const binary_tree_1 = require("./binary-tree");
14
13
  class RedBlackTreeNode extends bst_1.BSTNode {
15
14
  constructor(key, value, color = types_1.RBTNColor.BLACK) {
16
15
  super(key, value);
@@ -29,7 +28,7 @@ class RedBlackTree extends bst_1.BST {
29
28
  /**
30
29
  * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
31
30
  * initializes the tree with optional elements and options.
32
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
31
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
33
32
  * objects. It represents the initial elements that will be added to the RBTree during its
34
33
  * construction. If this parameter is provided, the `addMany` method is called to add all the
35
34
  * elements to the
@@ -78,16 +77,25 @@ class RedBlackTree extends bst_1.BST {
78
77
  }
79
78
  /**
80
79
  * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
81
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
80
+ * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
82
81
  * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
83
82
  * class.
84
83
  */
85
84
  isNode(exemplar) {
86
85
  return exemplar instanceof RedBlackTreeNode;
87
86
  }
87
+ /**
88
+ * The function "isNotNodeInstance" checks if a potential key is a K.
89
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
90
+ * data type.
91
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
92
+ */
93
+ isNotNodeInstance(potentialKey) {
94
+ return !(potentialKey instanceof RedBlackTreeNode);
95
+ }
88
96
  /**
89
97
  * The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
90
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
98
+ * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
91
99
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
92
100
  * `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
93
101
  * is provided, it will be used when creating the new node. If no value is provided, the new node
@@ -198,7 +206,7 @@ class RedBlackTree extends bst_1.BST {
198
206
  * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` and
199
207
  * returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
200
208
  * on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
201
- * @returns an array of `BiTreeDeleteResult<N>`.
209
+ * @returns an array of `BinaryTreeDeleteResult<N>`.
202
210
  */
203
211
  delete(identifier, callback = this._defaultOneParamCallback) {
204
212
  const ans = [];
@@ -263,7 +271,9 @@ class RedBlackTree extends bst_1.BST {
263
271
  * Space Complexity: O(1)
264
272
  */
265
273
  isRealNode(node) {
266
- return node !== this.Sentinel && node !== undefined;
274
+ if (node === this.Sentinel || node === undefined)
275
+ return false;
276
+ return node instanceof RedBlackTreeNode;
267
277
  }
268
278
  /**
269
279
  * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
@@ -292,41 +302,17 @@ class RedBlackTree extends bst_1.BST {
292
302
  */
293
303
  getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
294
304
  var _a;
295
- if (identifier instanceof binary_tree_1.BinaryTreeNode)
305
+ if (identifier instanceof RedBlackTreeNode)
296
306
  callback = (node => node);
297
307
  beginRoot = this.ensureNode(beginRoot);
298
308
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
299
309
  }
300
310
  /**
301
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
302
- * Space Complexity: O(1)
303
- */
304
- /**
305
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
306
- * Space Complexity: O(1)
307
- *
308
- * The function returns the successor of a given node in a red-black tree.
309
- * @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
310
- * @returns the successor of the given RedBlackTreeNode.
311
- */
312
- getSuccessor(x) {
313
- var _a;
314
- if (x.right !== this.Sentinel) {
315
- return (_a = this.getLeftMost(x.right)) !== null && _a !== void 0 ? _a : undefined;
316
- }
317
- let y = x.parent;
318
- while (y !== this.Sentinel && y !== undefined && x === y.right) {
319
- x = y;
320
- y = y.parent;
321
- }
322
- return y;
323
- }
324
- /**
325
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
311
+ * Time Complexity: O(log n)
326
312
  * Space Complexity: O(1)
327
313
  */
328
314
  /**
329
- * Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
315
+ * Time Complexity: O(log n)
330
316
  * Space Complexity: O(1)
331
317
  *
332
318
  * The function returns the predecessor of a given node in a red-black tree.
@@ -335,11 +321,11 @@ class RedBlackTree extends bst_1.BST {
335
321
  * @returns the predecessor of the given RedBlackTreeNode 'x'.
336
322
  */
337
323
  getPredecessor(x) {
338
- if (x.left !== this.Sentinel) {
324
+ if (this.isRealNode(x.left)) {
339
325
  return this.getRightMost(x.left);
340
326
  }
341
327
  let y = x.parent;
342
- while (y !== this.Sentinel && x === y.left) {
328
+ while (this.isRealNode(y) && x === y.left) {
343
329
  x = y;
344
330
  y = y.parent;
345
331
  }
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNodeKeyOrNode, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
9
- import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
8
+ import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, BTNExemplar, BTNKeyOrNode, TreeMultimapNested, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
9
+ import { IterationType } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
12
  export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, N> {
@@ -27,7 +27,7 @@ export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNo
27
27
  * The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
28
28
  */
29
29
  export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
30
- constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>);
30
+ constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>);
31
31
  private _count;
32
32
  get count(): number;
33
33
  /**
@@ -43,14 +43,21 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
43
43
  createTree(options?: TreeMultimapOptions<K>): TREE;
44
44
  /**
45
45
  * The function checks if an exemplar is an instance of the TreeMultimapNode class.
46
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
46
+ * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
47
47
  * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
48
48
  * class.
49
49
  */
50
- isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
50
+ isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
51
+ /**
52
+ * The function "isNotNodeInstance" checks if a potential key is a K.
53
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
54
+ * data type.
55
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
56
+ */
57
+ isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K;
51
58
  /**
52
59
  * The function `exemplarToNode` converts an exemplar object into a node object.
53
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, which means it
60
+ * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
54
61
  * can be one of the following:
55
62
  * @param {V} [value] - The `value` parameter is an optional argument that represents the value
56
63
  * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
@@ -59,7 +66,7 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
59
66
  * times the value should be added to the node. If not provided, it defaults to 1.
60
67
  * @returns a node of type `N` or `undefined`.
61
68
  */
62
- exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | undefined;
69
+ exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V, count?: number): N | undefined;
63
70
  /**
64
71
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
65
72
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -80,7 +87,7 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
80
87
  * @returns The method is returning either the newly inserted node or `undefined` if the insertion
81
88
  * was not successful.
82
89
  */
83
- add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | undefined;
90
+ add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count?: number): N | undefined;
84
91
  /**
85
92
  * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
86
93
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -95,7 +102,7 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
95
102
  * either keys, nodes, or entries.
96
103
  * @returns The method is returning an array of type `N | undefined`.
97
104
  */
98
- addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>): (N | undefined)[];
105
+ addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>): (N | undefined)[];
99
106
  /**
100
107
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
101
108
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -133,9 +140,9 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
133
140
  * being deleted. If set to true, the count of the node will not be considered and the node will be
134
141
  * deleted regardless of its count. If set to false (default), the count of the node will be
135
142
  * decremented by 1 and
136
- * @returns an array of `BiTreeDeleteResult<N>`.
143
+ * @returns an array of `BinaryTreeDeleteResult<N>`.
137
144
  */
138
- delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BiTreeDeleteResult<N>[];
145
+ delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<N>[];
139
146
  /**
140
147
  * Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
141
148
  * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
@@ -171,7 +178,7 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
171
178
  * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
172
179
  * added, or `undefined` if no node was added.
173
180
  */
174
- protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<K, N>): N | undefined;
181
+ protected _addTo(newNode: N | undefined, parent: BSTNKeyOrNode<K, N>): N | undefined;
175
182
  /**
176
183
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
177
184
  * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
@@ -181,6 +188,6 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
181
188
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
182
189
  * if either `srcNode` or `destNode` is undefined.
183
190
  */
184
- protected _swapProperties(srcNode: BSTNodeKeyOrNode<K, N>, destNode: BSTNodeKeyOrNode<K, N>): N | undefined;
191
+ protected _swapProperties(srcNode: BSTNKeyOrNode<K, N>, destNode: BSTNKeyOrNode<K, N>): N | undefined;
185
192
  protected _replaceNode(oldNode: N, newNode: N): N;
186
193
  }
@@ -53,16 +53,25 @@ class TreeMultimap extends avl_tree_1.AVLTree {
53
53
  }
54
54
  /**
55
55
  * The function checks if an exemplar is an instance of the TreeMultimapNode class.
56
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
56
+ * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
57
57
  * @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
58
58
  * class.
59
59
  */
60
60
  isNode(exemplar) {
61
61
  return exemplar instanceof TreeMultimapNode;
62
62
  }
63
+ /**
64
+ * The function "isNotNodeInstance" checks if a potential key is a K.
65
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
66
+ * data type.
67
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
68
+ */
69
+ isNotNodeInstance(potentialKey) {
70
+ return !(potentialKey instanceof TreeMultimapNode);
71
+ }
63
72
  /**
64
73
  * The function `exemplarToNode` converts an exemplar object into a node object.
65
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, which means it
74
+ * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
66
75
  * can be one of the following:
67
76
  * @param {V} [value] - The `value` parameter is an optional argument that represents the value
68
77
  * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
@@ -216,7 +225,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
216
225
  * being deleted. If set to true, the count of the node will not be considered and the node will be
217
226
  * deleted regardless of its count. If set to false (default), the count of the node will be
218
227
  * decremented by 1 and
219
- * @returns an array of `BiTreeDeleteResult<N>`.
228
+ * @returns an array of `BinaryTreeDeleteResult<N>`.
220
229
  */
221
230
  delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
222
231
  var _a;
@@ -1,7 +1,13 @@
1
- import type { DijkstraResult, VertexKey } from '../../types';
2
- import { EntryCallback } from "../../types";
3
- import { IGraph } from '../../interfaces';
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type { DijkstraResult, EntryCallback, VertexKey } from '../../types';
4
9
  import { IterableEntryBase } from "../base";
10
+ import { IGraph } from '../../interfaces';
5
11
  export declare abstract class AbstractVertex<V = any> {
6
12
  key: VertexKey;
7
13
  value: V | undefined;
@@ -1,17 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
4
- /**
5
- * data-structure-typed
6
- *
7
- * @author Tyler Zeng
8
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
- * @license MIT License
10
- */
11
4
  const utils_1 = require("../../utils");
12
- const priority_queue_1 = require("../priority-queue");
13
- const queue_1 = require("../queue");
14
5
  const base_1 = require("../base");
6
+ const heap_1 = require("../heap");
7
+ const queue_1 = require("../queue");
15
8
  class AbstractVertex {
16
9
  /**
17
10
  * The function is a protected constructor that takes an key and an optional value as parameters.
@@ -440,13 +433,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
440
433
  * shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
441
434
  * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
442
435
  */
443
- dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
444
- if (getMinDist === undefined)
445
- getMinDist = false;
446
- if (genPaths === undefined)
447
- genPaths = false;
448
- if (dest === undefined)
449
- dest = undefined;
436
+ dijkstraWithoutHeap(src, dest = undefined, getMinDist = false, genPaths = false) {
450
437
  let minDist = Infinity;
451
438
  let minDest = undefined;
452
439
  let minPath = [];
@@ -575,14 +562,8 @@ class AbstractGraph extends base_1.IterableEntryBase {
575
562
  * shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
576
563
  * @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
577
564
  */
578
- dijkstra(src, dest, getMinDist, genPaths) {
565
+ dijkstra(src, dest = undefined, getMinDist = false, genPaths = false) {
579
566
  var _a;
580
- if (getMinDist === undefined)
581
- getMinDist = false;
582
- if (genPaths === undefined)
583
- genPaths = false;
584
- if (dest === undefined)
585
- dest = undefined;
586
567
  let minDist = Infinity;
587
568
  let minDest = undefined;
588
569
  let minPath = [];
@@ -600,7 +581,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
600
581
  if (vertexOrKey instanceof AbstractVertex)
601
582
  distMap.set(vertexOrKey, Infinity);
602
583
  }
603
- const heap = new priority_queue_1.PriorityQueue([], { comparator: (a, b) => a.key - b.key });
584
+ const heap = new heap_1.Heap([], { comparator: (a, b) => a.key - b.key });
604
585
  heap.add({ key: 0, value: srcVertex });
605
586
  distMap.set(srcVertex, 0);
606
587
  preMap.set(srcVertex, undefined);
@@ -969,13 +950,28 @@ class AbstractGraph extends base_1.IterableEntryBase {
969
950
  }
970
951
  const cycles = new Map();
971
952
  if (needCycles) {
972
- let SCCs = new Map();
973
- if (SCCs.size < 1) {
974
- SCCs = getSCCs();
975
- }
976
- SCCs.forEach((SCC, low) => {
977
- if (SCC.length > 1) {
978
- cycles.set(low, SCC);
953
+ const visitedMap = new Map();
954
+ const stack = [];
955
+ const findCyclesDFS = (cur, parent) => {
956
+ visitedMap.set(cur, true);
957
+ stack.push(cur);
958
+ const neighbors = this.getNeighbors(cur);
959
+ for (const neighbor of neighbors) {
960
+ if (!visitedMap.get(neighbor)) {
961
+ findCyclesDFS(neighbor, cur);
962
+ }
963
+ else if (stack.includes(neighbor) && neighbor !== parent) {
964
+ const cycleStartIndex = stack.indexOf(neighbor);
965
+ const cycle = stack.slice(cycleStartIndex);
966
+ const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
967
+ cycles.set(cycleLow, cycle);
968
+ }
969
+ }
970
+ stack.pop();
971
+ };
972
+ vertexMap.forEach(v => {
973
+ if (!visitedMap.get(v)) {
974
+ findCyclesDFS(v, undefined);
979
975
  }
980
976
  });
981
977
  }