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.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +16 -11
- package/dist/data-structures/binary-tree/avl-tree.js +12 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +70 -61
- package/dist/data-structures/binary-tree/binary-tree.js +25 -21
- package/dist/data-structures/binary-tree/bst.d.ts +20 -13
- package/dist/data-structures/binary-tree/bst.js +12 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +19 -25
- package/dist/data-structures/binary-tree/rb-tree.js +21 -35
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +20 -13
- package/dist/data-structures/binary-tree/tree-multimap.js +12 -3
- package/dist/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/data-structures/graph/abstract-graph.js +27 -31
- package/dist/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/data-structures/graph/directed-graph.js +1 -8
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -8
- package/dist/data-structures/hash/hash-map.d.ts +14 -2
- package/dist/data-structures/hash/hash-map.js +19 -8
- package/dist/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +14 -3
- package/dist/data-structures/heap/heap.js +12 -0
- package/dist/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/data-structures/heap/max-heap.js +10 -7
- package/dist/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/data-structures/heap/min-heap.js +10 -7
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +8 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +6 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/data-structures/priority-queue/priority-queue.js +8 -7
- package/dist/data-structures/queue/deque.d.ts +6 -5
- package/dist/data-structures/queue/deque.js +6 -12
- package/dist/data-structures/queue/queue.d.ts +18 -3
- package/dist/data-structures/queue/queue.js +16 -6
- package/dist/data-structures/stack/stack.d.ts +15 -5
- package/dist/data-structures/stack/stack.js +7 -4
- package/dist/data-structures/trie/trie.d.ts +13 -3
- package/dist/data-structures/trie/trie.js +11 -8
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/common.d.ts +32 -8
- package/dist/types/common.js +22 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -22
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +23 -13
- package/src/data-structures/binary-tree/binary-tree.ts +93 -97
- package/src/data-structures/binary-tree/bst.ts +26 -15
- package/src/data-structures/binary-tree/rb-tree.ts +33 -48
- package/src/data-structures/binary-tree/tree-multimap.ts +32 -14
- package/src/data-structures/graph/abstract-graph.ts +35 -25
- package/src/data-structures/graph/directed-graph.ts +2 -2
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +20 -3
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +14 -3
- package/src/data-structures/heap/max-heap.ts +11 -2
- package/src/data-structures/heap/min-heap.ts +11 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +9 -2
- package/src/data-structures/queue/deque.ts +7 -9
- package/src/data-structures/queue/queue.ts +18 -3
- package/src/data-structures/stack/stack.ts +16 -6
- package/src/data-structures/trie/trie.ts +13 -4
- package/src/interfaces/binary-tree.ts +5 -5
- package/src/types/common.ts +37 -12
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
- package/src/types/data-structures/binary-tree/bst.ts +0 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
9
|
BSTNested,
|
|
10
|
-
|
|
10
|
+
BSTNKeyOrNode,
|
|
11
11
|
BSTNodeNested,
|
|
12
12
|
BSTOptions,
|
|
13
13
|
BTNCallback,
|
|
14
|
-
|
|
14
|
+
BTNExemplar,
|
|
15
|
+
BTNKeyOrNode,
|
|
15
16
|
BTNodePureExemplar
|
|
16
17
|
} from '../../types';
|
|
17
18
|
import { BSTVariant, CP, IterationType } from '../../types';
|
|
@@ -87,12 +88,12 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
87
88
|
/**
|
|
88
89
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
89
90
|
* the tree with optional elements and options.
|
|
90
|
-
* @param [elements] - An optional iterable of
|
|
91
|
+
* @param [elements] - An optional iterable of BTNExemplar objects that will be added to the
|
|
91
92
|
* binary search tree.
|
|
92
93
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
93
94
|
* configuration options for the binary search tree. It can have the following properties:
|
|
94
95
|
*/
|
|
95
|
-
constructor(elements?: Iterable<
|
|
96
|
+
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>) {
|
|
96
97
|
super([], options);
|
|
97
98
|
|
|
98
99
|
if (options) {
|
|
@@ -147,10 +148,10 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
147
148
|
|
|
148
149
|
/**
|
|
149
150
|
* The function checks if an exemplar is an instance of BSTNode.
|
|
150
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `
|
|
151
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
151
152
|
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
152
153
|
*/
|
|
153
|
-
override isNode(exemplar:
|
|
154
|
+
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
154
155
|
return exemplar instanceof BSTNode;
|
|
155
156
|
}
|
|
156
157
|
|
|
@@ -158,12 +159,12 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
158
159
|
/**
|
|
159
160
|
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
160
161
|
* otherwise it returns undefined.
|
|
161
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
162
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
162
163
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
163
164
|
* `exemplarToNode` function. It represents the value associated with the exemplar node.
|
|
164
165
|
* @returns a node of type N or undefined.
|
|
165
166
|
*/
|
|
166
|
-
override exemplarToNode(exemplar:
|
|
167
|
+
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
|
167
168
|
let node: N | undefined;
|
|
168
169
|
if (exemplar === null || exemplar === undefined) {
|
|
169
170
|
return;
|
|
@@ -201,7 +202,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
201
202
|
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
202
203
|
* node was not added.
|
|
203
204
|
*/
|
|
204
|
-
override add(keyOrNodeOrEntry:
|
|
205
|
+
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
|
205
206
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
206
207
|
if (newNode === undefined) return;
|
|
207
208
|
|
|
@@ -273,7 +274,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
273
274
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
274
275
|
*/
|
|
275
276
|
override addMany(
|
|
276
|
-
keysOrNodesOrEntries: Iterable<
|
|
277
|
+
keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>,
|
|
277
278
|
values?: Iterable<V | undefined>,
|
|
278
279
|
isBalanceAdd = true,
|
|
279
280
|
iterationType = this.iterationType
|
|
@@ -297,7 +298,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
297
298
|
|
|
298
299
|
const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
|
|
299
300
|
|
|
300
|
-
const isRealBTNExemplar = (kve:
|
|
301
|
+
const isRealBTNExemplar = (kve: BTNExemplar<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
|
|
301
302
|
if (kve === undefined || kve === null) return false;
|
|
302
303
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
303
304
|
}
|
|
@@ -379,7 +380,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
379
380
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
380
381
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
381
382
|
*/
|
|
382
|
-
lastKey(beginRoot:
|
|
383
|
+
lastKey(beginRoot: BSTNKeyOrNode<K, N> = this.root): K | undefined {
|
|
383
384
|
let current = this.ensureNode(beginRoot);
|
|
384
385
|
if (!current) return undefined;
|
|
385
386
|
|
|
@@ -442,6 +443,16 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
442
443
|
}
|
|
443
444
|
}
|
|
444
445
|
|
|
446
|
+
/**
|
|
447
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
448
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
449
|
+
* data type.
|
|
450
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
451
|
+
*/
|
|
452
|
+
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
453
|
+
return !(potentialKey instanceof BSTNode)
|
|
454
|
+
}
|
|
455
|
+
|
|
445
456
|
/**
|
|
446
457
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
447
458
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -456,7 +467,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
456
467
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
457
468
|
* @returns either a node object (N) or undefined.
|
|
458
469
|
*/
|
|
459
|
-
override ensureNode(key:
|
|
470
|
+
override ensureNode(key: BSTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | undefined {
|
|
460
471
|
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
461
472
|
}
|
|
462
473
|
|
|
@@ -487,7 +498,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
487
498
|
identifier: ReturnType<C> | undefined,
|
|
488
499
|
callback: C = this._defaultOneParamCallback as C,
|
|
489
500
|
onlyOne = false,
|
|
490
|
-
beginRoot:
|
|
501
|
+
beginRoot: BSTNKeyOrNode<K, N> = this.root,
|
|
491
502
|
iterationType = this.iterationType
|
|
492
503
|
): N[] {
|
|
493
504
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -568,7 +579,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
568
579
|
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
|
|
569
580
|
callback: C = this._defaultOneParamCallback as C,
|
|
570
581
|
lesserOrGreater: CP = CP.lt,
|
|
571
|
-
targetNode:
|
|
582
|
+
targetNode: BSTNKeyOrNode<K, N> = this.root,
|
|
572
583
|
iterationType = this.iterationType
|
|
573
584
|
): ReturnType<C>[] {
|
|
574
585
|
targetNode = this.ensureNode(targetNode);
|
|
@@ -7,10 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
BinaryTreeDeleteResult,
|
|
11
|
+
BSTNKeyOrNode,
|
|
12
12
|
BTNCallback,
|
|
13
|
-
|
|
13
|
+
BTNExemplar,
|
|
14
|
+
BTNKeyOrNode,
|
|
14
15
|
IterationType,
|
|
15
16
|
RBTNColor,
|
|
16
17
|
RBTreeOptions,
|
|
@@ -19,7 +20,6 @@ import {
|
|
|
19
20
|
} from '../../types';
|
|
20
21
|
import { BST, BSTNode } from './bst';
|
|
21
22
|
import { IBinaryTree } from '../../interfaces';
|
|
22
|
-
import { BinaryTreeNode } from './binary-tree';
|
|
23
23
|
|
|
24
24
|
export class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<
|
|
25
25
|
K, V,
|
|
@@ -48,7 +48,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
48
48
|
/**
|
|
49
49
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
50
50
|
* initializes the tree with optional elements and options.
|
|
51
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `
|
|
51
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
|
|
52
52
|
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
53
53
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
54
54
|
* elements to the
|
|
@@ -56,7 +56,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
56
56
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
57
57
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
58
58
|
*/
|
|
59
|
-
constructor(elements?: Iterable<
|
|
59
|
+
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>) {
|
|
60
60
|
super([], options);
|
|
61
61
|
|
|
62
62
|
this._root = this.Sentinel;
|
|
@@ -107,23 +107,33 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
107
107
|
|
|
108
108
|
/**
|
|
109
109
|
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
110
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
110
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
111
111
|
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
112
112
|
* class.
|
|
113
113
|
*/
|
|
114
|
-
override isNode(exemplar:
|
|
114
|
+
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
115
115
|
return exemplar instanceof RedBlackTreeNode;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
+
/**
|
|
119
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
120
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
121
|
+
* data type.
|
|
122
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
123
|
+
*/
|
|
124
|
+
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
125
|
+
return !(potentialKey instanceof RedBlackTreeNode)
|
|
126
|
+
}
|
|
127
|
+
|
|
118
128
|
/**
|
|
119
129
|
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
120
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
130
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
121
131
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
122
132
|
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
123
133
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
124
134
|
* @returns a node of type N or undefined.
|
|
125
135
|
*/
|
|
126
|
-
override exemplarToNode(exemplar:
|
|
136
|
+
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
|
127
137
|
let node: N | undefined;
|
|
128
138
|
|
|
129
139
|
if (exemplar === null || exemplar === undefined) {
|
|
@@ -162,7 +172,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
162
172
|
* being added to the binary search tree.
|
|
163
173
|
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
164
174
|
*/
|
|
165
|
-
override add(keyOrNodeOrEntry:
|
|
175
|
+
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
|
166
176
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
167
177
|
if (newNode === undefined) return;
|
|
168
178
|
|
|
@@ -231,13 +241,13 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
231
241
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` and
|
|
232
242
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
233
243
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
234
|
-
* @returns an array of `
|
|
244
|
+
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
235
245
|
*/
|
|
236
246
|
delete<C extends BTNCallback<N>>(
|
|
237
247
|
identifier: ReturnType<C> | null | undefined,
|
|
238
248
|
callback: C = this._defaultOneParamCallback as C
|
|
239
|
-
):
|
|
240
|
-
const ans:
|
|
249
|
+
): BinaryTreeDeleteResult<N>[] {
|
|
250
|
+
const ans: BinaryTreeDeleteResult<N>[] = [];
|
|
241
251
|
if (identifier === null) return ans;
|
|
242
252
|
const helper = (node: N | undefined): void => {
|
|
243
253
|
let z: N = this.Sentinel;
|
|
@@ -300,7 +310,8 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
300
310
|
*/
|
|
301
311
|
|
|
302
312
|
override isRealNode(node: N | undefined): node is N {
|
|
303
|
-
|
|
313
|
+
if (node === this.Sentinel || node === undefined) return false;
|
|
314
|
+
return node instanceof RedBlackTreeNode;
|
|
304
315
|
}
|
|
305
316
|
|
|
306
317
|
getNode<C extends BTNCallback<N, K>>(
|
|
@@ -353,47 +364,21 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
353
364
|
getNode<C extends BTNCallback<N>>(
|
|
354
365
|
identifier: ReturnType<C> | undefined,
|
|
355
366
|
callback: C = this._defaultOneParamCallback as C,
|
|
356
|
-
beginRoot:
|
|
367
|
+
beginRoot: BSTNKeyOrNode<K, N> = this.root,
|
|
357
368
|
iterationType = this.iterationType
|
|
358
369
|
): N | null | undefined {
|
|
359
|
-
if ((identifier as any) instanceof
|
|
370
|
+
if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
|
|
360
371
|
beginRoot = this.ensureNode(beginRoot);
|
|
361
372
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
362
373
|
}
|
|
363
374
|
|
|
364
375
|
/**
|
|
365
|
-
* Time Complexity: O(log n)
|
|
366
|
-
* Space Complexity: O(1)
|
|
367
|
-
*/
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
371
|
-
* Space Complexity: O(1)
|
|
372
|
-
*
|
|
373
|
-
* The function returns the successor of a given node in a red-black tree.
|
|
374
|
-
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
375
|
-
* @returns the successor of the given RedBlackTreeNode.
|
|
376
|
-
*/
|
|
377
|
-
override getSuccessor(x: N): N | undefined {
|
|
378
|
-
if (x.right !== this.Sentinel) {
|
|
379
|
-
return this.getLeftMost(x.right) ?? undefined;
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
let y: N | undefined = x.parent;
|
|
383
|
-
while (y !== this.Sentinel && y !== undefined && x === y.right) {
|
|
384
|
-
x = y;
|
|
385
|
-
y = y.parent;
|
|
386
|
-
}
|
|
387
|
-
return y;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
376
|
+
* Time Complexity: O(log n)
|
|
392
377
|
* Space Complexity: O(1)
|
|
393
378
|
*/
|
|
394
379
|
|
|
395
380
|
/**
|
|
396
|
-
* Time Complexity: O(log n)
|
|
381
|
+
* Time Complexity: O(log n)
|
|
397
382
|
* Space Complexity: O(1)
|
|
398
383
|
*
|
|
399
384
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
@@ -402,12 +387,12 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
402
387
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
403
388
|
*/
|
|
404
389
|
override getPredecessor(x: N): N {
|
|
405
|
-
if (x.left
|
|
406
|
-
return this.getRightMost(x.left
|
|
390
|
+
if (this.isRealNode(x.left)) {
|
|
391
|
+
return this.getRightMost(x.left)!;
|
|
407
392
|
}
|
|
408
393
|
|
|
409
394
|
let y: N | undefined = x.parent;
|
|
410
|
-
while (
|
|
395
|
+
while (this.isRealNode(y) && x === y.left) {
|
|
411
396
|
x = y!;
|
|
412
397
|
y = y!.parent;
|
|
413
398
|
}
|
|
@@ -5,8 +5,17 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
|
|
8
|
+
import type {
|
|
9
|
+
BinaryTreeDeleteResult,
|
|
10
|
+
BSTNKeyOrNode,
|
|
11
|
+
BTNCallback,
|
|
12
|
+
BTNExemplar,
|
|
13
|
+
BTNKeyOrNode,
|
|
14
|
+
TreeMultimapNested,
|
|
15
|
+
TreeMultimapNodeNested,
|
|
16
|
+
TreeMultimapOptions
|
|
17
|
+
} from '../../types';
|
|
18
|
+
import { FamilyPosition, IterationType } from '../../types';
|
|
10
19
|
import { IBinaryTree } from '../../interfaces';
|
|
11
20
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
21
|
|
|
@@ -41,7 +50,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
41
50
|
extends AVLTree<K, V, N, TREE>
|
|
42
51
|
implements IBinaryTree<K, V, N, TREE> {
|
|
43
52
|
|
|
44
|
-
constructor(elements?: Iterable<
|
|
53
|
+
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) {
|
|
45
54
|
super([], options);
|
|
46
55
|
if (elements) this.addMany(elements);
|
|
47
56
|
}
|
|
@@ -77,18 +86,27 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
77
86
|
|
|
78
87
|
/**
|
|
79
88
|
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
80
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
89
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
81
90
|
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
82
91
|
* class.
|
|
83
92
|
*/
|
|
84
|
-
override isNode(exemplar:
|
|
93
|
+
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
85
94
|
return exemplar instanceof TreeMultimapNode;
|
|
86
95
|
}
|
|
87
96
|
|
|
97
|
+
/**
|
|
98
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
99
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
100
|
+
* data type.
|
|
101
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
102
|
+
*/
|
|
103
|
+
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
104
|
+
return !(potentialKey instanceof TreeMultimapNode)
|
|
105
|
+
}
|
|
88
106
|
|
|
89
107
|
/**
|
|
90
108
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
91
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
109
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
92
110
|
* can be one of the following:
|
|
93
111
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
94
112
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -97,7 +115,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
97
115
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
98
116
|
* @returns a node of type `N` or `undefined`.
|
|
99
117
|
*/
|
|
100
|
-
override exemplarToNode(exemplar:
|
|
118
|
+
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined {
|
|
101
119
|
let node: N | undefined;
|
|
102
120
|
if (exemplar === undefined || exemplar === null) {
|
|
103
121
|
return;
|
|
@@ -139,7 +157,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
139
157
|
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
|
|
140
158
|
* was not successful.
|
|
141
159
|
*/
|
|
142
|
-
override add(keyOrNodeOrEntry:
|
|
160
|
+
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined {
|
|
143
161
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
144
162
|
if (newNode === undefined) return;
|
|
145
163
|
|
|
@@ -166,7 +184,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
166
184
|
* either keys, nodes, or entries.
|
|
167
185
|
* @returns The method is returning an array of type `N | undefined`.
|
|
168
186
|
*/
|
|
169
|
-
override addMany(keysOrNodesOrEntries: Iterable<
|
|
187
|
+
override addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>): (N | undefined)[] {
|
|
170
188
|
return super.addMany(keysOrNodesOrEntries);
|
|
171
189
|
}
|
|
172
190
|
|
|
@@ -246,14 +264,14 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
246
264
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
247
265
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
248
266
|
* decremented by 1 and
|
|
249
|
-
* @returns an array of `
|
|
267
|
+
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
250
268
|
*/
|
|
251
269
|
override delete<C extends BTNCallback<N>>(
|
|
252
270
|
identifier: ReturnType<C>,
|
|
253
271
|
callback: C = this._defaultOneParamCallback as C,
|
|
254
272
|
ignoreCount = false
|
|
255
|
-
):
|
|
256
|
-
const deletedResult:
|
|
273
|
+
): BinaryTreeDeleteResult<N>[] {
|
|
274
|
+
const deletedResult: BinaryTreeDeleteResult<N>[] = [];
|
|
257
275
|
if (!this.root) return deletedResult;
|
|
258
276
|
|
|
259
277
|
const curr: N | undefined = this.getNode(identifier, callback) ?? undefined;
|
|
@@ -354,7 +372,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
354
372
|
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
355
373
|
* added, or `undefined` if no node was added.
|
|
356
374
|
*/
|
|
357
|
-
protected override _addTo(newNode: N | undefined, parent:
|
|
375
|
+
protected override _addTo(newNode: N | undefined, parent: BSTNKeyOrNode<K, N>): N | undefined {
|
|
358
376
|
parent = this.ensureNode(parent);
|
|
359
377
|
if (parent) {
|
|
360
378
|
if (parent.left === undefined) {
|
|
@@ -389,7 +407,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
389
407
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
390
408
|
* if either `srcNode` or `destNode` is undefined.
|
|
391
409
|
*/
|
|
392
|
-
protected override _swapProperties(srcNode:
|
|
410
|
+
protected override _swapProperties(srcNode: BSTNKeyOrNode<K, N>, destNode: BSTNKeyOrNode<K, N>): N | undefined {
|
|
393
411
|
srcNode = this.ensureNode(srcNode);
|
|
394
412
|
destNode = this.ensureNode(destNode);
|
|
395
413
|
if (srcNode && destNode) {
|
|
@@ -5,13 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
import type { DijkstraResult, EntryCallback, VertexKey } from '../../types';
|
|
8
9
|
import { uuidV4 } from '../../utils';
|
|
9
|
-
import {
|
|
10
|
-
import type { DijkstraResult, VertexKey } from '../../types';
|
|
11
|
-
import { EntryCallback } from "../../types";
|
|
10
|
+
import { IterableEntryBase } from "../base";
|
|
12
11
|
import { IGraph } from '../../interfaces';
|
|
12
|
+
import { Heap } from '../heap';
|
|
13
13
|
import { Queue } from '../queue';
|
|
14
|
-
import { IterableEntryBase } from "../base";
|
|
15
14
|
|
|
16
15
|
export abstract class AbstractVertex<V = any> {
|
|
17
16
|
key: VertexKey;
|
|
@@ -527,14 +526,10 @@ export abstract class AbstractGraph<
|
|
|
527
526
|
*/
|
|
528
527
|
dijkstraWithoutHeap(
|
|
529
528
|
src: VO | VertexKey,
|
|
530
|
-
dest
|
|
531
|
-
getMinDist
|
|
532
|
-
genPaths
|
|
529
|
+
dest: VO | VertexKey | undefined = undefined,
|
|
530
|
+
getMinDist: boolean = false,
|
|
531
|
+
genPaths: boolean = false
|
|
533
532
|
): DijkstraResult<VO> {
|
|
534
|
-
if (getMinDist === undefined) getMinDist = false;
|
|
535
|
-
if (genPaths === undefined) genPaths = false;
|
|
536
|
-
|
|
537
|
-
if (dest === undefined) dest = undefined;
|
|
538
533
|
let minDist = Infinity;
|
|
539
534
|
let minDest: VO | undefined = undefined;
|
|
540
535
|
let minPath: VO[] = [];
|
|
@@ -675,14 +670,11 @@ export abstract class AbstractGraph<
|
|
|
675
670
|
*/
|
|
676
671
|
dijkstra(
|
|
677
672
|
src: VO | VertexKey,
|
|
678
|
-
dest
|
|
679
|
-
getMinDist
|
|
680
|
-
genPaths
|
|
673
|
+
dest: VO | VertexKey | undefined = undefined,
|
|
674
|
+
getMinDist: boolean = false,
|
|
675
|
+
genPaths: boolean = false
|
|
681
676
|
): DijkstraResult<VO> {
|
|
682
|
-
if (getMinDist === undefined) getMinDist = false;
|
|
683
|
-
if (genPaths === undefined) genPaths = false;
|
|
684
677
|
|
|
685
|
-
if (dest === undefined) dest = undefined;
|
|
686
678
|
let minDist = Infinity;
|
|
687
679
|
let minDest: VO | undefined = undefined;
|
|
688
680
|
let minPath: VO[] = [];
|
|
@@ -702,7 +694,7 @@ export abstract class AbstractGraph<
|
|
|
702
694
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
703
695
|
}
|
|
704
696
|
|
|
705
|
-
const heap = new
|
|
697
|
+
const heap = new Heap<{ key: number; value: VO }>([], { comparator: (a, b) => a.key - b.key });
|
|
706
698
|
heap.add({ key: 0, value: srcVertex });
|
|
707
699
|
|
|
708
700
|
distMap.set(srcVertex, 0);
|
|
@@ -1094,15 +1086,33 @@ export abstract class AbstractGraph<
|
|
|
1094
1086
|
}
|
|
1095
1087
|
|
|
1096
1088
|
const cycles: Map<number, VO[]> = new Map();
|
|
1089
|
+
|
|
1097
1090
|
if (needCycles) {
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1091
|
+
const visitedMap: Map<VO, boolean> = new Map();
|
|
1092
|
+
const stack: VO[] = [];
|
|
1093
|
+
const findCyclesDFS = (cur: VO, parent: VO | undefined) => {
|
|
1094
|
+
visitedMap.set(cur, true);
|
|
1095
|
+
stack.push(cur);
|
|
1096
|
+
|
|
1097
|
+
const neighbors = this.getNeighbors(cur);
|
|
1098
|
+
|
|
1099
|
+
for (const neighbor of neighbors) {
|
|
1100
|
+
if (!visitedMap.get(neighbor)) {
|
|
1101
|
+
findCyclesDFS(neighbor, cur);
|
|
1102
|
+
} else if (stack.includes(neighbor) && neighbor !== parent) {
|
|
1103
|
+
const cycleStartIndex = stack.indexOf(neighbor);
|
|
1104
|
+
const cycle = stack.slice(cycleStartIndex);
|
|
1105
|
+
const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
|
|
1106
|
+
cycles.set(cycleLow, cycle);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
stack.pop();
|
|
1111
|
+
};
|
|
1102
1112
|
|
|
1103
|
-
|
|
1104
|
-
if (
|
|
1105
|
-
|
|
1113
|
+
vertexMap.forEach(v => {
|
|
1114
|
+
if (!visitedMap.get(v)) {
|
|
1115
|
+
findCyclesDFS(v, undefined);
|
|
1106
1116
|
}
|
|
1107
1117
|
});
|
|
1108
1118
|
}
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { arrayRemove } from '../../utils';
|
|
9
|
-
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
10
8
|
import type { TopologicalStatus, VertexKey } from '../../types';
|
|
9
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
11
10
|
import { IGraph } from '../../interfaces';
|
|
11
|
+
import { arrayRemove } from '../../utils';
|
|
12
12
|
|
|
13
13
|
export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
14
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MapGraphCoordinate, VertexKey } from '../../types';
|
|
1
|
+
import type { MapGraphCoordinate, VertexKey } from '../../types';
|
|
2
2
|
import { DirectedEdge, DirectedGraph, DirectedVertex } from './directed-graph';
|
|
3
3
|
|
|
4
4
|
export class MapVertex<V = any> extends DirectedVertex<V> {
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { arrayRemove } from '../../utils';
|
|
9
|
-
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
10
8
|
import type { VertexKey } from '../../types';
|
|
11
9
|
import { IGraph } from '../../interfaces';
|
|
10
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
11
|
+
import { arrayRemove } from '../../utils';
|
|
12
12
|
|
|
13
13
|
export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
14
|
/**
|
|
@@ -5,11 +5,16 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
9
|
+
import { IterableEntryBase } from '../base';
|
|
9
10
|
import { isWeakKey, rangeCheck } from '../../utils';
|
|
10
|
-
import { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
11
|
-
import { IterableEntryBase } from "../base";
|
|
12
11
|
|
|
12
|
+
/**
|
|
13
|
+
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
14
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
|
|
15
|
+
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
16
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
|
|
17
|
+
*/
|
|
13
18
|
export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
14
19
|
protected _store: { [key: string]: HashMapStoreItem<K, V> } = {};
|
|
15
20
|
protected _objMap: Map<object, V> = new Map();
|
|
@@ -248,6 +253,11 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
248
253
|
}
|
|
249
254
|
}
|
|
250
255
|
|
|
256
|
+
/**
|
|
257
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which elements are inserted. Therefore, when you traverse it, elements will be returned in the order they were inserted into the map.
|
|
258
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of elements through the linked list.
|
|
259
|
+
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
260
|
+
*/
|
|
251
261
|
export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
252
262
|
|
|
253
263
|
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
|
|
@@ -401,6 +411,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
401
411
|
}
|
|
402
412
|
}
|
|
403
413
|
|
|
414
|
+
hasValue(value: V): boolean {
|
|
415
|
+
for (const [, elementValue] of this) {
|
|
416
|
+
if (elementValue === value) return true;
|
|
417
|
+
}
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
|
|
404
421
|
setMany(entries: Iterable<[K, V]>): void {
|
|
405
422
|
for (const entry of entries) {
|
|
406
423
|
const [key, value] = entry;
|
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
import type { HashFunction } from '../../types';
|
|
10
|
+
|
|
11
|
+
export class HashTableNode<K = any, V = any> {
|
|
10
12
|
key: K;
|
|
11
13
|
value: V;
|
|
12
14
|
next: HashTableNode<K, V> | undefined;
|
|
@@ -18,8 +20,6 @@ export class HashTableNode<K, V> {
|
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
import { HashFunction } from '../../types';
|
|
22
|
-
|
|
23
23
|
export class HashTable<K = any, V = any> {
|
|
24
24
|
protected static readonly DEFAULT_CAPACITY = 16;
|
|
25
25
|
protected static readonly LOAD_FACTOR = 0.75;
|