graph-typed 1.52.0 → 1.52.2
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/binary-tree/avl-tree-multi-map.d.ts +11 -11
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
- package/dist/data-structures/binary-tree/avl-tree.d.ts +11 -11
- package/dist/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +97 -97
- package/dist/data-structures/binary-tree/binary-tree.js +52 -52
- package/dist/data-structures/binary-tree/bst.d.ts +35 -35
- package/dist/data-structures/binary-tree/bst.js +17 -17
- package/dist/data-structures/binary-tree/rb-tree.d.ts +8 -8
- package/dist/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +10 -10
- 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/queue/deque.d.ts +7 -0
- package/dist/data-structures/queue/deque.js +16 -1
- package/dist/data-structures/queue/queue.d.ts +18 -1
- package/dist/data-structures/queue/queue.js +32 -7
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +1 -22
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +18 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +25 -24
- package/src/data-structures/binary-tree/avl-tree.ts +20 -19
- package/src/data-structures/binary-tree/binary-tree.ts +162 -157
- package/src/data-structures/binary-tree/bst.ts +54 -50
- package/src/data-structures/binary-tree/rb-tree.ts +18 -17
- package/src/data-structures/binary-tree/tree-multi-map.ts +18 -17
- package/src/data-structures/graph/abstract-graph.ts +15 -14
- package/src/data-structures/graph/directed-graph.ts +9 -7
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +18 -4
- package/src/data-structures/queue/queue.ts +38 -8
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +2 -24
- package/src/types/data-structures/binary-tree/binary-tree.ts +21 -1
- package/src/types/data-structures/binary-tree/bst.ts +7 -0
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/src/types/data-structures/queue/deque.ts +4 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/utils/utils.ts +4 -4
|
@@ -11,12 +11,13 @@ import type {
|
|
|
11
11
|
BSTNodeNested,
|
|
12
12
|
BSTOptions,
|
|
13
13
|
BTNCallback,
|
|
14
|
+
BTNKeyOrNodeOrEntry,
|
|
14
15
|
BTNPureKeyOrNodeOrEntry,
|
|
15
16
|
Comparator,
|
|
16
17
|
CP,
|
|
17
18
|
DFSOrderPattern,
|
|
18
19
|
IterationType,
|
|
19
|
-
|
|
20
|
+
OptBSTN
|
|
20
21
|
} from '../../types';
|
|
21
22
|
import { BTNEntry } from '../../types';
|
|
22
23
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
@@ -43,16 +44,16 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
43
44
|
* The function returns the value of the `_left` property.
|
|
44
45
|
* @returns The `_left` property of the current object is being returned.
|
|
45
46
|
*/
|
|
46
|
-
override get left(): NODE
|
|
47
|
+
override get left(): OptBSTN<NODE> {
|
|
47
48
|
return this._left;
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
/**
|
|
51
52
|
* The function sets the left child of a node and updates the parent reference of the child.
|
|
52
|
-
* @param {NODE
|
|
53
|
+
* @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be an
|
|
53
54
|
* instance of the `NODE` class or `undefined`.
|
|
54
55
|
*/
|
|
55
|
-
override set left(v: NODE
|
|
56
|
+
override set left(v: OptBSTN<NODE>) {
|
|
56
57
|
if (v) {
|
|
57
58
|
v.parent = this as unknown as NODE;
|
|
58
59
|
}
|
|
@@ -66,16 +67,16 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
66
67
|
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
67
68
|
* `undefined`.
|
|
68
69
|
*/
|
|
69
|
-
override get right(): NODE
|
|
70
|
+
override get right(): OptBSTN<NODE> {
|
|
70
71
|
return this._right;
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
/**
|
|
74
75
|
* The function sets the right child of a node and updates the parent reference of the child.
|
|
75
|
-
* @param {NODE
|
|
76
|
+
* @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be a
|
|
76
77
|
* `NODE` object or `undefined`.
|
|
77
78
|
*/
|
|
78
|
-
override set right(v: NODE
|
|
79
|
+
override set right(v: OptBSTN<NODE>) {
|
|
79
80
|
if (v) {
|
|
80
81
|
v.parent = this as unknown as NODE;
|
|
81
82
|
}
|
|
@@ -93,14 +94,15 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
93
94
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
94
95
|
*/
|
|
95
96
|
export class BST<
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
>
|
|
97
|
+
K = any,
|
|
98
|
+
V = any,
|
|
99
|
+
R = BTNEntry<K, V>,
|
|
100
|
+
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
101
|
+
TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
|
|
102
|
+
>
|
|
102
103
|
extends BinaryTree<K, V, R, NODE, TREE>
|
|
103
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
104
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
105
|
+
{
|
|
104
106
|
/**
|
|
105
107
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
106
108
|
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
@@ -110,7 +112,7 @@ export class BST<
|
|
|
110
112
|
* It can include a comparator function that defines the order of the elements in the tree.
|
|
111
113
|
*/
|
|
112
114
|
constructor(
|
|
113
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
115
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
114
116
|
options?: BSTOptions<K, V, R>
|
|
115
117
|
) {
|
|
116
118
|
super([], options);
|
|
@@ -129,7 +131,7 @@ export class BST<
|
|
|
129
131
|
* The function returns the root node of a tree structure.
|
|
130
132
|
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
131
133
|
*/
|
|
132
|
-
override get root(): NODE
|
|
134
|
+
override get root(): OptBSTN<NODE> {
|
|
133
135
|
return this._root;
|
|
134
136
|
}
|
|
135
137
|
|
|
@@ -162,17 +164,17 @@ export class BST<
|
|
|
162
164
|
|
|
163
165
|
/**
|
|
164
166
|
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
165
|
-
* @param {R |
|
|
166
|
-
* type R or
|
|
167
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
|
|
168
|
+
* type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
167
169
|
* element.
|
|
168
170
|
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
169
171
|
* value associated with a key in a key-value pair.
|
|
170
172
|
* @returns either a NODE object or undefined.
|
|
171
173
|
*/
|
|
172
174
|
override keyValueOrEntryOrRawElementToNode(
|
|
173
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
175
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
174
176
|
value?: V
|
|
175
|
-
): NODE
|
|
177
|
+
): OptBSTN<NODE> {
|
|
176
178
|
return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
|
|
177
179
|
}
|
|
178
180
|
|
|
@@ -182,7 +184,7 @@ export class BST<
|
|
|
182
184
|
*
|
|
183
185
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
184
186
|
* it doesn't exist.
|
|
185
|
-
* @param {R |
|
|
187
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
186
188
|
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
|
|
187
189
|
* entry, or raw element that needs to be ensured in the tree.
|
|
188
190
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
@@ -192,21 +194,21 @@ export class BST<
|
|
|
192
194
|
* not be ensured.
|
|
193
195
|
*/
|
|
194
196
|
override ensureNode(
|
|
195
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
197
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
196
198
|
iterationType: IterationType = 'ITERATIVE'
|
|
197
|
-
): NODE
|
|
199
|
+
): OptBSTN<NODE> {
|
|
198
200
|
return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
|
|
199
201
|
}
|
|
200
202
|
|
|
201
203
|
/**
|
|
202
204
|
* The function checks if the input is an instance of the BSTNode class.
|
|
203
|
-
* @param {R |
|
|
204
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
205
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
206
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
205
207
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
206
208
|
* an instance of the `BSTNode` class.
|
|
207
209
|
*/
|
|
208
210
|
override isNode(
|
|
209
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
211
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
210
212
|
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
211
213
|
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
212
214
|
}
|
|
@@ -216,13 +218,13 @@ export class BST<
|
|
|
216
218
|
* Space Complexity: O(1)
|
|
217
219
|
*
|
|
218
220
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
219
|
-
* @param {R |
|
|
220
|
-
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `
|
|
221
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
222
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
221
223
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
222
224
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
223
225
|
* @returns a boolean value.
|
|
224
226
|
*/
|
|
225
|
-
override add(keyOrNodeOrEntryOrRawElement: R |
|
|
227
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
226
228
|
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
227
229
|
if (newNode === undefined) return false;
|
|
228
230
|
|
|
@@ -284,7 +286,7 @@ export class BST<
|
|
|
284
286
|
* successfully inserted into the data structure.
|
|
285
287
|
*/
|
|
286
288
|
override addMany(
|
|
287
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
289
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
|
|
288
290
|
values?: Iterable<V | undefined>,
|
|
289
291
|
isBalanceAdd = true,
|
|
290
292
|
iterationType: IterationType = this.iterationType
|
|
@@ -308,7 +310,9 @@ export class BST<
|
|
|
308
310
|
|
|
309
311
|
const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
|
|
310
312
|
|
|
311
|
-
const isRealBTNExemplar = (
|
|
313
|
+
const isRealBTNExemplar = (
|
|
314
|
+
kve: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
315
|
+
): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
|
|
312
316
|
if (kve === undefined || kve === null) return false;
|
|
313
317
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
314
318
|
};
|
|
@@ -395,7 +399,7 @@ export class BST<
|
|
|
395
399
|
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
|
|
396
400
|
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
|
|
397
401
|
* false, all matching nodes will be returned. The default value is false.
|
|
398
|
-
* @param {R |
|
|
402
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
399
403
|
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
|
|
400
404
|
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
|
|
401
405
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
@@ -406,7 +410,7 @@ export class BST<
|
|
|
406
410
|
identifier: ReturnType<C> | undefined,
|
|
407
411
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
408
412
|
onlyOne = false,
|
|
409
|
-
beginRoot: R |
|
|
413
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
410
414
|
iterationType: IterationType = this.iterationType
|
|
411
415
|
): NODE[] {
|
|
412
416
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -496,7 +500,7 @@ export class BST<
|
|
|
496
500
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
497
501
|
beginRoot: R | BSTNKeyOrNode<K, NODE> = this.root,
|
|
498
502
|
iterationType: IterationType = this.iterationType
|
|
499
|
-
): NODE
|
|
503
|
+
): OptBSTN<NODE> {
|
|
500
504
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
501
505
|
}
|
|
502
506
|
|
|
@@ -518,7 +522,7 @@ export class BST<
|
|
|
518
522
|
* It has a default value of `'ITERATIVE'`.
|
|
519
523
|
* @returns The method is returning a NODE object or undefined.
|
|
520
524
|
*/
|
|
521
|
-
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE
|
|
525
|
+
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBSTN<NODE> {
|
|
522
526
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
523
527
|
}
|
|
524
528
|
|
|
@@ -539,7 +543,7 @@ export class BST<
|
|
|
539
543
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
540
544
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
541
545
|
* take one of the following values:
|
|
542
|
-
* @param {R |
|
|
546
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
543
547
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
544
548
|
* node entry. If not specified, the default value is the root of the tree.
|
|
545
549
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -550,7 +554,7 @@ export class BST<
|
|
|
550
554
|
override dfs<C extends BTNCallback<NODE>>(
|
|
551
555
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
552
556
|
pattern: DFSOrderPattern = 'IN',
|
|
553
|
-
beginRoot: R |
|
|
557
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
554
558
|
iterationType: IterationType = 'ITERATIVE'
|
|
555
559
|
): ReturnType<C>[] {
|
|
556
560
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
@@ -570,7 +574,7 @@ export class BST<
|
|
|
570
574
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
571
575
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
572
576
|
* node being visited, and it can return a value of any type.
|
|
573
|
-
* @param {R |
|
|
577
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
574
578
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
575
579
|
* object. If no value is provided, the default value is the root of the tree.
|
|
576
580
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -580,7 +584,7 @@ export class BST<
|
|
|
580
584
|
*/
|
|
581
585
|
override bfs<C extends BTNCallback<NODE>>(
|
|
582
586
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
583
|
-
beginRoot: R |
|
|
587
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
584
588
|
iterationType: IterationType = this.iterationType
|
|
585
589
|
): ReturnType<C>[] {
|
|
586
590
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
@@ -600,7 +604,7 @@ export class BST<
|
|
|
600
604
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
601
605
|
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
602
606
|
* tree during the iteration process.
|
|
603
|
-
* @param {R |
|
|
607
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
604
608
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
605
609
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
606
610
|
* value is provided, the root of
|
|
@@ -611,7 +615,7 @@ export class BST<
|
|
|
611
615
|
*/
|
|
612
616
|
override listLevels<C extends BTNCallback<NODE>>(
|
|
613
617
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
614
|
-
beginRoot: R |
|
|
618
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
615
619
|
iterationType: IterationType = this.iterationType
|
|
616
620
|
): ReturnType<C>[][] {
|
|
617
621
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
@@ -634,7 +638,7 @@ export class BST<
|
|
|
634
638
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
635
639
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
636
640
|
* 0, or 1, where:
|
|
637
|
-
* @param {R |
|
|
641
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
|
|
638
642
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
639
643
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
640
644
|
* `targetNode` is provided,
|
|
@@ -646,7 +650,7 @@ export class BST<
|
|
|
646
650
|
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
|
|
647
651
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
648
652
|
lesserOrGreater: CP = -1,
|
|
649
|
-
targetNode: R |
|
|
653
|
+
targetNode: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
650
654
|
iterationType: IterationType = this.iterationType
|
|
651
655
|
): ReturnType<C>[] {
|
|
652
656
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
@@ -760,7 +764,7 @@ export class BST<
|
|
|
760
764
|
let balanced = true;
|
|
761
765
|
|
|
762
766
|
if (iterationType === 'RECURSIVE') {
|
|
763
|
-
const _height = (cur: NODE
|
|
767
|
+
const _height = (cur: OptBSTN<NODE>): number => {
|
|
764
768
|
if (!cur) return 0;
|
|
765
769
|
const leftHeight = _height(cur.left),
|
|
766
770
|
rightHeight = _height(cur.right);
|
|
@@ -770,8 +774,8 @@ export class BST<
|
|
|
770
774
|
_height(this.root);
|
|
771
775
|
} else {
|
|
772
776
|
const stack: NODE[] = [];
|
|
773
|
-
let node: NODE
|
|
774
|
-
last: NODE
|
|
777
|
+
let node: OptBSTN<NODE> = this.root,
|
|
778
|
+
last: OptBSTN<NODE> = undefined;
|
|
775
779
|
const depths: Map<NODE, number> = new Map();
|
|
776
780
|
|
|
777
781
|
while (stack.length > 0 || node) {
|
|
@@ -783,8 +787,8 @@ export class BST<
|
|
|
783
787
|
if (!node.right || last === node.right) {
|
|
784
788
|
node = stack.pop();
|
|
785
789
|
if (node) {
|
|
786
|
-
const left = node.left ? depths.get(node.left) ?? -1 : -1;
|
|
787
|
-
const right = node.right ? depths.get(node.right) ?? -1 : -1;
|
|
790
|
+
const left = node.left ? (depths.get(node.left) ?? -1) : -1;
|
|
791
|
+
const right = node.right ? (depths.get(node.right) ?? -1) : -1;
|
|
788
792
|
if (Math.abs(left - right) > 1) return false;
|
|
789
793
|
depths.set(node, 1 + Math.max(left, right));
|
|
790
794
|
last = node;
|
|
@@ -827,9 +831,9 @@ export class BST<
|
|
|
827
831
|
/**
|
|
828
832
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
829
833
|
* root.
|
|
830
|
-
* @param {NODE
|
|
834
|
+
* @param {OptBSTN<NODE>} v - v is a parameter of type NODE or undefined.
|
|
831
835
|
*/
|
|
832
|
-
protected override _setRoot(v: NODE
|
|
836
|
+
protected override _setRoot(v: OptBSTN<NODE>) {
|
|
833
837
|
if (v) {
|
|
834
838
|
v.parent = undefined;
|
|
835
839
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
3
|
BTNCallback,
|
|
4
|
+
BTNKeyOrNodeOrEntry,
|
|
4
5
|
CRUD,
|
|
5
|
-
KeyOrNodeOrEntry,
|
|
6
6
|
RBTNColor,
|
|
7
7
|
RBTreeOptions,
|
|
8
8
|
RedBlackTreeNested,
|
|
@@ -53,14 +53,15 @@ export class RedBlackTreeNode<
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export class RedBlackTree<
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
>
|
|
56
|
+
K = any,
|
|
57
|
+
V = any,
|
|
58
|
+
R = BTNEntry<K, V>,
|
|
59
|
+
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
60
|
+
TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
|
|
61
|
+
>
|
|
62
62
|
extends BST<K, V, R, NODE, TREE>
|
|
63
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
63
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
64
|
+
{
|
|
64
65
|
/**
|
|
65
66
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
66
67
|
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
@@ -72,7 +73,7 @@ export class RedBlackTree<
|
|
|
72
73
|
* depend on the implementation
|
|
73
74
|
*/
|
|
74
75
|
constructor(
|
|
75
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
76
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
76
77
|
options?: RBTreeOptions<K, V, R>
|
|
77
78
|
) {
|
|
78
79
|
super([], options);
|
|
@@ -135,13 +136,13 @@ export class RedBlackTree<
|
|
|
135
136
|
* Space Complexity: O(1)
|
|
136
137
|
*
|
|
137
138
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
138
|
-
* @param {R |
|
|
139
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
139
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
140
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
140
141
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
141
142
|
* an instance of the `RedBlackTreeNode` class.
|
|
142
143
|
*/
|
|
143
144
|
override isNode(
|
|
144
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
145
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
145
146
|
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
146
147
|
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
|
|
147
148
|
}
|
|
@@ -157,11 +158,11 @@ export class RedBlackTree<
|
|
|
157
158
|
// *
|
|
158
159
|
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
|
|
159
160
|
// * valid, otherwise it returns undefined.
|
|
160
|
-
// * @param {
|
|
161
|
+
// * @param {BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
|
|
161
162
|
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
|
|
162
163
|
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
163
164
|
// */
|
|
164
|
-
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R |
|
|
165
|
+
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
165
166
|
//
|
|
166
167
|
// if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
167
168
|
// if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
@@ -210,8 +211,8 @@ export class RedBlackTree<
|
|
|
210
211
|
*
|
|
211
212
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
212
213
|
* added.
|
|
213
|
-
* @param {R |
|
|
214
|
-
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `
|
|
214
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
215
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
215
216
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
216
217
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
217
218
|
* structure.
|
|
@@ -219,7 +220,7 @@ export class RedBlackTree<
|
|
|
219
220
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
220
221
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
221
222
|
*/
|
|
222
|
-
override add(keyOrNodeOrEntryOrRawElement: R |
|
|
223
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
223
224
|
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
224
225
|
if (!this.isRealNode(newNode)) return false;
|
|
225
226
|
|
|
@@ -9,8 +9,8 @@ import type {
|
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNKeyOrNode,
|
|
11
11
|
BTNCallback,
|
|
12
|
+
BTNKeyOrNodeOrEntry,
|
|
12
13
|
IterationType,
|
|
13
|
-
KeyOrNodeOrEntry,
|
|
14
14
|
RBTNColor,
|
|
15
15
|
TreeMultiMapNested,
|
|
16
16
|
TreeMultiMapNodeNested,
|
|
@@ -63,14 +63,15 @@ export class TreeMultiMapNode<
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export class TreeMultiMap<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
>
|
|
66
|
+
K = any,
|
|
67
|
+
V = any,
|
|
68
|
+
R = BTNEntry<K, V>,
|
|
69
|
+
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
|
|
70
|
+
TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
|
|
71
|
+
>
|
|
72
72
|
extends RedBlackTree<K, V, R, NODE, TREE>
|
|
73
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
73
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
74
|
+
{
|
|
74
75
|
/**
|
|
75
76
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
76
77
|
* @param keysOrNodesOrEntriesOrRawElements - The parameter `keysOrNodesOrEntriesOrRawElements` is an
|
|
@@ -81,7 +82,7 @@ export class TreeMultiMap<
|
|
|
81
82
|
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
82
83
|
*/
|
|
83
84
|
constructor(
|
|
84
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<
|
|
85
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
85
86
|
options?: TreeMultiMapOptions<K, V, R>
|
|
86
87
|
) {
|
|
87
88
|
super([], options);
|
|
@@ -153,8 +154,8 @@ export class TreeMultiMap<
|
|
|
153
154
|
/**
|
|
154
155
|
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
|
|
155
156
|
* node based on the input.
|
|
156
|
-
* @param {R |
|
|
157
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
157
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
158
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
158
159
|
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
159
160
|
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
160
161
|
* an existing node.
|
|
@@ -163,7 +164,7 @@ export class TreeMultiMap<
|
|
|
163
164
|
* @returns either a NODE object or undefined.
|
|
164
165
|
*/
|
|
165
166
|
override keyValueOrEntryOrRawElementToNode(
|
|
166
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
167
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
167
168
|
value?: V,
|
|
168
169
|
count = 1
|
|
169
170
|
): NODE | undefined {
|
|
@@ -190,13 +191,13 @@ export class TreeMultiMap<
|
|
|
190
191
|
|
|
191
192
|
/**
|
|
192
193
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
193
|
-
* @param {R |
|
|
194
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
194
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
195
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
195
196
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
196
197
|
* an instance of the `TreeMultiMapNode` class.
|
|
197
198
|
*/
|
|
198
199
|
override isNode(
|
|
199
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
200
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
200
201
|
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
201
202
|
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
|
|
202
203
|
}
|
|
@@ -212,7 +213,7 @@ export class TreeMultiMap<
|
|
|
212
213
|
*
|
|
213
214
|
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
214
215
|
* the count and returning a boolean indicating success.
|
|
215
|
-
* @param {R |
|
|
216
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
216
217
|
* `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
|
|
217
218
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
218
219
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -222,7 +223,7 @@ export class TreeMultiMap<
|
|
|
222
223
|
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
223
224
|
* was successful, and false otherwise.
|
|
224
225
|
*/
|
|
225
|
-
override add(keyOrNodeOrEntryOrRawElement: R |
|
|
226
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
|
|
226
227
|
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
227
228
|
const orgCount = newNode?.count || 0;
|
|
228
229
|
const isSuccessAdded = super.add(newNode);
|
|
@@ -61,13 +61,14 @@ export abstract class AbstractEdge<E = any> {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
export abstract class AbstractGraph<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
>
|
|
64
|
+
V = any,
|
|
65
|
+
E = any,
|
|
66
|
+
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
67
|
+
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
68
|
+
>
|
|
69
69
|
extends IterableEntryBase<VertexKey, V | undefined>
|
|
70
|
-
implements IGraph<V, E, VO, EO>
|
|
70
|
+
implements IGraph<V, E, VO, EO>
|
|
71
|
+
{
|
|
71
72
|
constructor() {
|
|
72
73
|
super();
|
|
73
74
|
}
|
|
@@ -618,14 +619,14 @@ export abstract class AbstractGraph<
|
|
|
618
619
|
}
|
|
619
620
|
|
|
620
621
|
getMinDist &&
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
622
|
+
distMap.forEach((d, v) => {
|
|
623
|
+
if (v !== srcVertex) {
|
|
624
|
+
if (d < minDist) {
|
|
625
|
+
minDist = d;
|
|
626
|
+
if (genPaths) minDest = v;
|
|
627
|
+
}
|
|
626
628
|
}
|
|
627
|
-
}
|
|
628
|
-
});
|
|
629
|
+
});
|
|
629
630
|
|
|
630
631
|
genPaths && getPaths(minDest);
|
|
631
632
|
|
|
@@ -1069,7 +1070,7 @@ export abstract class AbstractGraph<
|
|
|
1069
1070
|
return mapped;
|
|
1070
1071
|
}
|
|
1071
1072
|
|
|
1072
|
-
protected*
|
|
1073
|
+
protected *_getIterator(): IterableIterator<[VertexKey, V | undefined]> {
|
|
1073
1074
|
for (const vertex of this._vertexMap.values()) {
|
|
1074
1075
|
yield [vertex.key, vertex.value];
|
|
1075
1076
|
}
|
|
@@ -46,13 +46,14 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export class DirectedGraph<
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
>
|
|
49
|
+
V = any,
|
|
50
|
+
E = any,
|
|
51
|
+
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
52
|
+
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
53
|
+
>
|
|
54
54
|
extends AbstractGraph<V, E, VO, EO>
|
|
55
|
-
implements IGraph<V, E, VO, EO>
|
|
55
|
+
implements IGraph<V, E, VO, EO>
|
|
56
|
+
{
|
|
56
57
|
/**
|
|
57
58
|
* The constructor function initializes an instance of a class.
|
|
58
59
|
*/
|
|
@@ -261,7 +262,8 @@ export class DirectedGraph<
|
|
|
261
262
|
if (vertex) {
|
|
262
263
|
const neighbors = this.getNeighbors(vertex);
|
|
263
264
|
for (const neighbor of neighbors) {
|
|
264
|
-
this._inEdgeMap.delete(neighbor);
|
|
265
|
+
// this._inEdgeMap.delete(neighbor);
|
|
266
|
+
this.deleteEdgeSrcToDest(vertex, neighbor);
|
|
265
267
|
}
|
|
266
268
|
this._outEdgeMap.delete(vertex);
|
|
267
269
|
this._inEdgeMap.delete(vertex);
|
|
@@ -43,13 +43,14 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
export class UndirectedGraph<
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
>
|
|
46
|
+
V = any,
|
|
47
|
+
E = any,
|
|
48
|
+
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
|
49
|
+
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
|
50
|
+
>
|
|
51
51
|
extends AbstractGraph<V, E, VO, EO>
|
|
52
|
-
implements IGraph<V, E, VO, EO>
|
|
52
|
+
implements IGraph<V, E, VO, EO>
|
|
53
|
+
{
|
|
53
54
|
/**
|
|
54
55
|
* The constructor initializes a new Map object to store edgeMap.
|
|
55
56
|
*/
|
|
@@ -322,7 +322,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
322
322
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
323
323
|
* object map.
|
|
324
324
|
*/
|
|
325
|
-
protected*
|
|
325
|
+
protected *_getIterator(): IterableIterator<[K, V]> {
|
|
326
326
|
for (const node of Object.values(this.store)) {
|
|
327
327
|
yield [node.key, node.value] as [K, V];
|
|
328
328
|
}
|
|
@@ -537,7 +537,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
537
537
|
/**
|
|
538
538
|
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
539
539
|
*/
|
|
540
|
-
*
|
|
540
|
+
*begin() {
|
|
541
541
|
let node = this.head;
|
|
542
542
|
while (node !== this._sentinel) {
|
|
543
543
|
yield [node.key, node.value];
|
|
@@ -549,7 +549,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
549
549
|
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
|
|
550
550
|
* key and value.
|
|
551
551
|
*/
|
|
552
|
-
*
|
|
552
|
+
*reverseBegin() {
|
|
553
553
|
let node = this.tail;
|
|
554
554
|
while (node !== this._sentinel) {
|
|
555
555
|
yield [node.key, node.value];
|
|
@@ -942,7 +942,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
942
942
|
*
|
|
943
943
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
944
944
|
*/
|
|
945
|
-
protected*
|
|
945
|
+
protected *_getIterator() {
|
|
946
946
|
let node = this.head;
|
|
947
947
|
while (node !== this._sentinel) {
|
|
948
948
|
yield [node.key, node.value] as [K, V];
|