directed-graph-typed 1.51.9 → 1.52.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.
- package/dist/data-structures/base/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +13 -13
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
- package/dist/data-structures/binary-tree/avl-tree.d.ts +13 -13
- package/dist/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +99 -99
- package/dist/data-structures/binary-tree/binary-tree.js +54 -52
- package/dist/data-structures/binary-tree/bst.d.ts +37 -45
- package/dist/data-structures/binary-tree/bst.js +17 -25
- package/dist/data-structures/binary-tree/rb-tree.d.ts +10 -10
- package/dist/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
- package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/data-structures/graph/directed-graph.js +2 -1
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +27 -18
- package/dist/data-structures/queue/deque.js +43 -21
- package/dist/data-structures/queue/queue.d.ts +8 -29
- package/dist/data-structures/queue/queue.js +15 -32
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +18 -13
- package/dist/data-structures/trie/trie.js +26 -15
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -1
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/common.d.ts +1 -22
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +20 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +5 -3
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +4 -2
- package/dist/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +14 -15
- package/src/data-structures/binary-tree/avl-tree.ts +13 -14
- package/src/data-structures/binary-tree/binary-tree.ts +156 -152
- package/src/data-structures/binary-tree/bst.ts +52 -60
- package/src/data-structures/binary-tree/rb-tree.ts +12 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +12 -13
- package/src/data-structures/graph/directed-graph.ts +2 -1
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +50 -25
- package/src/data-structures/queue/queue.ts +23 -37
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +33 -18
- package/src/index.ts +2 -1
- package/src/interfaces/binary-tree.ts +4 -5
- package/src/types/common.ts +2 -24
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +24 -5
- package/src/types/data-structures/binary-tree/bst.ts +9 -3
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +6 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
|
@@ -11,24 +11,24 @@ import type {
|
|
|
11
11
|
BSTNodeNested,
|
|
12
12
|
BSTOptions,
|
|
13
13
|
BTNCallback,
|
|
14
|
+
BTNKeyOrNodeOrEntry,
|
|
14
15
|
BTNPureKeyOrNodeOrEntry,
|
|
15
|
-
Comparable,
|
|
16
16
|
Comparator,
|
|
17
17
|
CP,
|
|
18
18
|
DFSOrderPattern,
|
|
19
19
|
IterationType,
|
|
20
|
-
|
|
20
|
+
OptBSTN
|
|
21
21
|
} from '../../types';
|
|
22
22
|
import { BTNEntry } from '../../types';
|
|
23
23
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
24
24
|
import { IBinaryTree } from '../../interfaces';
|
|
25
25
|
import { Queue } from '../queue';
|
|
26
26
|
|
|
27
|
-
export class BSTNode<
|
|
28
|
-
K
|
|
29
|
-
V
|
|
30
|
-
NODE
|
|
31
|
-
>
|
|
27
|
+
export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<
|
|
28
|
+
K,
|
|
29
|
+
V,
|
|
30
|
+
NODE
|
|
31
|
+
> {
|
|
32
32
|
override parent?: NODE;
|
|
33
33
|
|
|
34
34
|
constructor(key: K, value?: V) {
|
|
@@ -44,16 +44,16 @@ export class BSTNode<
|
|
|
44
44
|
* The function returns the value of the `_left` property.
|
|
45
45
|
* @returns The `_left` property of the current object is being returned.
|
|
46
46
|
*/
|
|
47
|
-
override get left(): NODE
|
|
47
|
+
override get left(): OptBSTN<NODE> {
|
|
48
48
|
return this._left;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
52
|
* The function sets the left child of a node and updates the parent reference of the child.
|
|
53
|
-
* @param {NODE
|
|
53
|
+
* @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be an
|
|
54
54
|
* instance of the `NODE` class or `undefined`.
|
|
55
55
|
*/
|
|
56
|
-
override set left(v: NODE
|
|
56
|
+
override set left(v: OptBSTN<NODE>) {
|
|
57
57
|
if (v) {
|
|
58
58
|
v.parent = this as unknown as NODE;
|
|
59
59
|
}
|
|
@@ -67,16 +67,16 @@ export class BSTNode<
|
|
|
67
67
|
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
68
68
|
* `undefined`.
|
|
69
69
|
*/
|
|
70
|
-
override get right(): NODE
|
|
70
|
+
override get right(): OptBSTN<NODE> {
|
|
71
71
|
return this._right;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
75
|
* The function sets the right child of a node and updates the parent reference of the child.
|
|
76
|
-
* @param {NODE
|
|
76
|
+
* @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be a
|
|
77
77
|
* `NODE` object or `undefined`.
|
|
78
78
|
*/
|
|
79
|
-
override set right(v: NODE
|
|
79
|
+
override set right(v: OptBSTN<NODE>) {
|
|
80
80
|
if (v) {
|
|
81
81
|
v.parent = this as unknown as NODE;
|
|
82
82
|
}
|
|
@@ -94,7 +94,7 @@ export class BSTNode<
|
|
|
94
94
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
95
95
|
*/
|
|
96
96
|
export class BST<
|
|
97
|
-
K
|
|
97
|
+
K = any,
|
|
98
98
|
V = any,
|
|
99
99
|
R = BTNEntry<K, V>,
|
|
100
100
|
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
@@ -111,7 +111,7 @@ export class BST<
|
|
|
111
111
|
* It can include a comparator function that defines the order of the elements in the tree.
|
|
112
112
|
*/
|
|
113
113
|
constructor(
|
|
114
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
114
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
115
115
|
options?: BSTOptions<K, V, R>
|
|
116
116
|
) {
|
|
117
117
|
super([], options);
|
|
@@ -130,7 +130,7 @@ export class BST<
|
|
|
130
130
|
* The function returns the root node of a tree structure.
|
|
131
131
|
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
132
132
|
*/
|
|
133
|
-
override get root(): NODE
|
|
133
|
+
override get root(): OptBSTN<NODE> {
|
|
134
134
|
return this._root;
|
|
135
135
|
}
|
|
136
136
|
|
|
@@ -163,17 +163,17 @@ export class BST<
|
|
|
163
163
|
|
|
164
164
|
/**
|
|
165
165
|
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
166
|
-
* @param {R |
|
|
167
|
-
* type R or
|
|
166
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
|
|
167
|
+
* type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
168
168
|
* element.
|
|
169
169
|
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
170
170
|
* value associated with a key in a key-value pair.
|
|
171
171
|
* @returns either a NODE object or undefined.
|
|
172
172
|
*/
|
|
173
173
|
override keyValueOrEntryOrRawElementToNode(
|
|
174
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
174
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
175
175
|
value?: V
|
|
176
|
-
): NODE
|
|
176
|
+
): OptBSTN<NODE> {
|
|
177
177
|
return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
|
|
178
178
|
}
|
|
179
179
|
|
|
@@ -183,7 +183,7 @@ export class BST<
|
|
|
183
183
|
*
|
|
184
184
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
185
185
|
* it doesn't exist.
|
|
186
|
-
* @param {R |
|
|
186
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
187
187
|
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
|
|
188
188
|
* entry, or raw element that needs to be ensured in the tree.
|
|
189
189
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
@@ -193,21 +193,21 @@ export class BST<
|
|
|
193
193
|
* not be ensured.
|
|
194
194
|
*/
|
|
195
195
|
override ensureNode(
|
|
196
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
196
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
197
197
|
iterationType: IterationType = 'ITERATIVE'
|
|
198
|
-
): NODE
|
|
198
|
+
): OptBSTN<NODE> {
|
|
199
199
|
return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
|
|
200
200
|
}
|
|
201
201
|
|
|
202
202
|
/**
|
|
203
203
|
* The function checks if the input is an instance of the BSTNode class.
|
|
204
|
-
* @param {R |
|
|
205
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
204
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
205
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
206
206
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
207
207
|
* an instance of the `BSTNode` class.
|
|
208
208
|
*/
|
|
209
209
|
override isNode(
|
|
210
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
210
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
211
211
|
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
212
212
|
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
213
213
|
}
|
|
@@ -217,13 +217,13 @@ export class BST<
|
|
|
217
217
|
* Space Complexity: O(1)
|
|
218
218
|
*
|
|
219
219
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
220
|
-
* @param {R |
|
|
221
|
-
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `
|
|
220
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
221
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
222
222
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
223
223
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
224
224
|
* @returns a boolean value.
|
|
225
225
|
*/
|
|
226
|
-
override add(keyOrNodeOrEntryOrRawElement: R |
|
|
226
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
227
227
|
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
228
228
|
if (newNode === undefined) return false;
|
|
229
229
|
|
|
@@ -285,7 +285,7 @@ export class BST<
|
|
|
285
285
|
* successfully inserted into the data structure.
|
|
286
286
|
*/
|
|
287
287
|
override addMany(
|
|
288
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
288
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
|
|
289
289
|
values?: Iterable<V | undefined>,
|
|
290
290
|
isBalanceAdd = true,
|
|
291
291
|
iterationType: IterationType = this.iterationType
|
|
@@ -309,7 +309,9 @@ export class BST<
|
|
|
309
309
|
|
|
310
310
|
const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
|
|
311
311
|
|
|
312
|
-
const isRealBTNExemplar = (
|
|
312
|
+
const isRealBTNExemplar = (
|
|
313
|
+
kve: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
314
|
+
): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
|
|
313
315
|
if (kve === undefined || kve === null) return false;
|
|
314
316
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
315
317
|
};
|
|
@@ -396,7 +398,7 @@ export class BST<
|
|
|
396
398
|
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
|
|
397
399
|
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
|
|
398
400
|
* false, all matching nodes will be returned. The default value is false.
|
|
399
|
-
* @param {R |
|
|
401
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
400
402
|
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
|
|
401
403
|
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
|
|
402
404
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
@@ -407,7 +409,7 @@ export class BST<
|
|
|
407
409
|
identifier: ReturnType<C> | undefined,
|
|
408
410
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
409
411
|
onlyOne = false,
|
|
410
|
-
beginRoot: R |
|
|
412
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
411
413
|
iterationType: IterationType = this.iterationType
|
|
412
414
|
): NODE[] {
|
|
413
415
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -497,7 +499,7 @@ export class BST<
|
|
|
497
499
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
498
500
|
beginRoot: R | BSTNKeyOrNode<K, NODE> = this.root,
|
|
499
501
|
iterationType: IterationType = this.iterationType
|
|
500
|
-
): NODE
|
|
502
|
+
): OptBSTN<NODE> {
|
|
501
503
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
502
504
|
}
|
|
503
505
|
|
|
@@ -519,7 +521,7 @@ export class BST<
|
|
|
519
521
|
* It has a default value of `'ITERATIVE'`.
|
|
520
522
|
* @returns The method is returning a NODE object or undefined.
|
|
521
523
|
*/
|
|
522
|
-
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE
|
|
524
|
+
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBSTN<NODE> {
|
|
523
525
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
524
526
|
}
|
|
525
527
|
|
|
@@ -540,7 +542,7 @@ export class BST<
|
|
|
540
542
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
541
543
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
542
544
|
* take one of the following values:
|
|
543
|
-
* @param {R |
|
|
545
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
544
546
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
545
547
|
* node entry. If not specified, the default value is the root of the tree.
|
|
546
548
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -551,7 +553,7 @@ export class BST<
|
|
|
551
553
|
override dfs<C extends BTNCallback<NODE>>(
|
|
552
554
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
553
555
|
pattern: DFSOrderPattern = 'IN',
|
|
554
|
-
beginRoot: R |
|
|
556
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
555
557
|
iterationType: IterationType = 'ITERATIVE'
|
|
556
558
|
): ReturnType<C>[] {
|
|
557
559
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
@@ -571,7 +573,7 @@ export class BST<
|
|
|
571
573
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
572
574
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
573
575
|
* node being visited, and it can return a value of any type.
|
|
574
|
-
* @param {R |
|
|
576
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
575
577
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
576
578
|
* object. If no value is provided, the default value is the root of the tree.
|
|
577
579
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -581,7 +583,7 @@ export class BST<
|
|
|
581
583
|
*/
|
|
582
584
|
override bfs<C extends BTNCallback<NODE>>(
|
|
583
585
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
584
|
-
beginRoot: R |
|
|
586
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
585
587
|
iterationType: IterationType = this.iterationType
|
|
586
588
|
): ReturnType<C>[] {
|
|
587
589
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
@@ -601,7 +603,7 @@ export class BST<
|
|
|
601
603
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
602
604
|
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
603
605
|
* tree during the iteration process.
|
|
604
|
-
* @param {R |
|
|
606
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
605
607
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
606
608
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
607
609
|
* value is provided, the root of
|
|
@@ -612,7 +614,7 @@ export class BST<
|
|
|
612
614
|
*/
|
|
613
615
|
override listLevels<C extends BTNCallback<NODE>>(
|
|
614
616
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
615
|
-
beginRoot: R |
|
|
617
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
616
618
|
iterationType: IterationType = this.iterationType
|
|
617
619
|
): ReturnType<C>[][] {
|
|
618
620
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
@@ -635,7 +637,7 @@ export class BST<
|
|
|
635
637
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
636
638
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
637
639
|
* 0, or 1, where:
|
|
638
|
-
* @param {R |
|
|
640
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
|
|
639
641
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
640
642
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
641
643
|
* `targetNode` is provided,
|
|
@@ -647,7 +649,7 @@ export class BST<
|
|
|
647
649
|
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
|
|
648
650
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
649
651
|
lesserOrGreater: CP = -1,
|
|
650
|
-
targetNode: R |
|
|
652
|
+
targetNode: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
651
653
|
iterationType: IterationType = this.iterationType
|
|
652
654
|
): ReturnType<C>[] {
|
|
653
655
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
@@ -761,7 +763,7 @@ export class BST<
|
|
|
761
763
|
let balanced = true;
|
|
762
764
|
|
|
763
765
|
if (iterationType === 'RECURSIVE') {
|
|
764
|
-
const _height = (cur: NODE
|
|
766
|
+
const _height = (cur: OptBSTN<NODE>): number => {
|
|
765
767
|
if (!cur) return 0;
|
|
766
768
|
const leftHeight = _height(cur.left),
|
|
767
769
|
rightHeight = _height(cur.right);
|
|
@@ -771,8 +773,8 @@ export class BST<
|
|
|
771
773
|
_height(this.root);
|
|
772
774
|
} else {
|
|
773
775
|
const stack: NODE[] = [];
|
|
774
|
-
let node: NODE
|
|
775
|
-
last: NODE
|
|
776
|
+
let node: OptBSTN<NODE> = this.root,
|
|
777
|
+
last: OptBSTN<NODE> = undefined;
|
|
776
778
|
const depths: Map<NODE, number> = new Map();
|
|
777
779
|
|
|
778
780
|
while (stack.length > 0 || node) {
|
|
@@ -799,15 +801,10 @@ export class BST<
|
|
|
799
801
|
return balanced;
|
|
800
802
|
}
|
|
801
803
|
|
|
802
|
-
/**
|
|
803
|
-
* Time complexity: O(n)
|
|
804
|
-
* Space complexity: O(n)
|
|
805
|
-
*/
|
|
806
|
-
|
|
807
804
|
protected _DEFAULT_COMPARATOR = (a: K, b: K): number => {
|
|
808
|
-
if (typeof a === 'object'
|
|
805
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
809
806
|
throw TypeError(
|
|
810
|
-
|
|
807
|
+
`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
|
|
811
808
|
);
|
|
812
809
|
}
|
|
813
810
|
if (a > b) return 1;
|
|
@@ -815,11 +812,6 @@ export class BST<
|
|
|
815
812
|
return 0;
|
|
816
813
|
};
|
|
817
814
|
|
|
818
|
-
/**
|
|
819
|
-
* Time complexity: O(n)
|
|
820
|
-
* Space complexity: O(n)
|
|
821
|
-
*/
|
|
822
|
-
|
|
823
815
|
protected _comparator: Comparator<K> = this._DEFAULT_COMPARATOR;
|
|
824
816
|
|
|
825
817
|
/**
|
|
@@ -838,9 +830,9 @@ export class BST<
|
|
|
838
830
|
/**
|
|
839
831
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
840
832
|
* root.
|
|
841
|
-
* @param {NODE
|
|
833
|
+
* @param {OptBSTN<NODE>} v - v is a parameter of type NODE or undefined.
|
|
842
834
|
*/
|
|
843
|
-
protected override _setRoot(v: NODE
|
|
835
|
+
protected override _setRoot(v: OptBSTN<NODE>) {
|
|
844
836
|
if (v) {
|
|
845
837
|
v.parent = undefined;
|
|
846
838
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
3
|
BTNCallback,
|
|
4
|
-
|
|
4
|
+
BTNKeyOrNodeOrEntry,
|
|
5
5
|
CRUD,
|
|
6
|
-
KeyOrNodeOrEntry,
|
|
7
6
|
RBTNColor,
|
|
8
7
|
RBTreeOptions,
|
|
9
8
|
RedBlackTreeNested,
|
|
@@ -14,7 +13,7 @@ import { BST, BSTNode } from './bst';
|
|
|
14
13
|
import { IBinaryTree } from '../../interfaces';
|
|
15
14
|
|
|
16
15
|
export class RedBlackTreeNode<
|
|
17
|
-
K
|
|
16
|
+
K = any,
|
|
18
17
|
V = any,
|
|
19
18
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
|
|
20
19
|
> extends BSTNode<K, V, NODE> {
|
|
@@ -54,7 +53,7 @@ export class RedBlackTreeNode<
|
|
|
54
53
|
}
|
|
55
54
|
|
|
56
55
|
export class RedBlackTree<
|
|
57
|
-
K
|
|
56
|
+
K = any,
|
|
58
57
|
V = any,
|
|
59
58
|
R = BTNEntry<K, V>,
|
|
60
59
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
@@ -73,7 +72,7 @@ export class RedBlackTree<
|
|
|
73
72
|
* depend on the implementation
|
|
74
73
|
*/
|
|
75
74
|
constructor(
|
|
76
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
75
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
77
76
|
options?: RBTreeOptions<K, V, R>
|
|
78
77
|
) {
|
|
79
78
|
super([], options);
|
|
@@ -136,13 +135,13 @@ export class RedBlackTree<
|
|
|
136
135
|
* Space Complexity: O(1)
|
|
137
136
|
*
|
|
138
137
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
139
|
-
* @param {R |
|
|
140
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
138
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
139
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
141
140
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
142
141
|
* an instance of the `RedBlackTreeNode` class.
|
|
143
142
|
*/
|
|
144
143
|
override isNode(
|
|
145
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
144
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
146
145
|
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
147
146
|
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
|
|
148
147
|
}
|
|
@@ -158,11 +157,11 @@ export class RedBlackTree<
|
|
|
158
157
|
// *
|
|
159
158
|
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
|
|
160
159
|
// * valid, otherwise it returns undefined.
|
|
161
|
-
// * @param {
|
|
160
|
+
// * @param {BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
|
|
162
161
|
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
|
|
163
162
|
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
164
163
|
// */
|
|
165
|
-
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R |
|
|
164
|
+
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
166
165
|
//
|
|
167
166
|
// if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
168
167
|
// if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
@@ -211,8 +210,8 @@ export class RedBlackTree<
|
|
|
211
210
|
*
|
|
212
211
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
213
212
|
* added.
|
|
214
|
-
* @param {R |
|
|
215
|
-
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `
|
|
213
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
214
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
216
215
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
217
216
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
218
217
|
* structure.
|
|
@@ -220,7 +219,7 @@ export class RedBlackTree<
|
|
|
220
219
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
221
220
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
222
221
|
*/
|
|
223
|
-
override add(keyOrNodeOrEntryOrRawElement: R |
|
|
222
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
224
223
|
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
225
224
|
if (!this.isRealNode(newNode)) return false;
|
|
226
225
|
|
|
@@ -9,9 +9,8 @@ import type {
|
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNKeyOrNode,
|
|
11
11
|
BTNCallback,
|
|
12
|
-
|
|
12
|
+
BTNKeyOrNodeOrEntry,
|
|
13
13
|
IterationType,
|
|
14
|
-
KeyOrNodeOrEntry,
|
|
15
14
|
RBTNColor,
|
|
16
15
|
TreeMultiMapNested,
|
|
17
16
|
TreeMultiMapNodeNested,
|
|
@@ -22,7 +21,7 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
22
21
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
23
22
|
|
|
24
23
|
export class TreeMultiMapNode<
|
|
25
|
-
K
|
|
24
|
+
K = any,
|
|
26
25
|
V = any,
|
|
27
26
|
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>
|
|
28
27
|
> extends RedBlackTreeNode<K, V, NODE> {
|
|
@@ -64,7 +63,7 @@ export class TreeMultiMapNode<
|
|
|
64
63
|
}
|
|
65
64
|
|
|
66
65
|
export class TreeMultiMap<
|
|
67
|
-
K
|
|
66
|
+
K = any,
|
|
68
67
|
V = any,
|
|
69
68
|
R = BTNEntry<K, V>,
|
|
70
69
|
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
|
|
@@ -82,7 +81,7 @@ export class TreeMultiMap<
|
|
|
82
81
|
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
83
82
|
*/
|
|
84
83
|
constructor(
|
|
85
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<
|
|
84
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
86
85
|
options?: TreeMultiMapOptions<K, V, R>
|
|
87
86
|
) {
|
|
88
87
|
super([], options);
|
|
@@ -154,8 +153,8 @@ export class TreeMultiMap<
|
|
|
154
153
|
/**
|
|
155
154
|
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
|
|
156
155
|
* node based on the input.
|
|
157
|
-
* @param {R |
|
|
158
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
156
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
157
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
159
158
|
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
160
159
|
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
161
160
|
* an existing node.
|
|
@@ -164,7 +163,7 @@ export class TreeMultiMap<
|
|
|
164
163
|
* @returns either a NODE object or undefined.
|
|
165
164
|
*/
|
|
166
165
|
override keyValueOrEntryOrRawElementToNode(
|
|
167
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
166
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
168
167
|
value?: V,
|
|
169
168
|
count = 1
|
|
170
169
|
): NODE | undefined {
|
|
@@ -191,13 +190,13 @@ export class TreeMultiMap<
|
|
|
191
190
|
|
|
192
191
|
/**
|
|
193
192
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
194
|
-
* @param {R |
|
|
195
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
193
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
194
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
196
195
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
197
196
|
* an instance of the `TreeMultiMapNode` class.
|
|
198
197
|
*/
|
|
199
198
|
override isNode(
|
|
200
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
199
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
201
200
|
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
202
201
|
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
|
|
203
202
|
}
|
|
@@ -213,7 +212,7 @@ export class TreeMultiMap<
|
|
|
213
212
|
*
|
|
214
213
|
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
215
214
|
* the count and returning a boolean indicating success.
|
|
216
|
-
* @param {R |
|
|
215
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
217
216
|
* `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
|
|
218
217
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
219
218
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -223,7 +222,7 @@ export class TreeMultiMap<
|
|
|
223
222
|
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
224
223
|
* was successful, and false otherwise.
|
|
225
224
|
*/
|
|
226
|
-
override add(keyOrNodeOrEntryOrRawElement: R |
|
|
225
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
|
|
227
226
|
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
228
227
|
const orgCount = newNode?.count || 0;
|
|
229
228
|
const isSuccessAdded = super.add(newNode);
|
|
@@ -261,7 +261,8 @@ export class DirectedGraph<
|
|
|
261
261
|
if (vertex) {
|
|
262
262
|
const neighbors = this.getNeighbors(vertex);
|
|
263
263
|
for (const neighbor of neighbors) {
|
|
264
|
-
this._inEdgeMap.delete(neighbor);
|
|
264
|
+
// this._inEdgeMap.delete(neighbor);
|
|
265
|
+
this.deleteEdgeSrcToDest(vertex, neighbor);
|
|
265
266
|
}
|
|
266
267
|
this._outEdgeMap.delete(vertex);
|
|
267
268
|
this._inEdgeMap.delete(vertex);
|
|
@@ -270,8 +270,8 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
270
270
|
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
|
271
271
|
* the provided callback function.
|
|
272
272
|
*/
|
|
273
|
-
map<
|
|
274
|
-
const resultMap = new HashMap<K,
|
|
273
|
+
map<VM>(callbackfn: EntryCallback<K, V, VM>, thisArg?: any): HashMap<K, VM> {
|
|
274
|
+
const resultMap = new HashMap<K, VM>();
|
|
275
275
|
let index = 0;
|
|
276
276
|
for (const [key, value] of this) {
|
|
277
277
|
resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
|
@@ -898,8 +898,8 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
898
898
|
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
|
899
899
|
* function.
|
|
900
900
|
*/
|
|
901
|
-
map<
|
|
902
|
-
const mappedMap = new LinkedHashMap<K,
|
|
901
|
+
map<VM>(callback: EntryCallback<K, V, VM>, thisArg?: any): LinkedHashMap<K, VM> {
|
|
902
|
+
const mappedMap = new LinkedHashMap<K, VM>();
|
|
903
903
|
let index = 0;
|
|
904
904
|
for (const [key, value] of this) {
|
|
905
905
|
const newValue = callback.call(thisArg, value, key, index, this);
|