directed-graph-typed 1.52.5 → 1.52.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants/index.d.ts +4 -0
- package/dist/constants/index.js +8 -0
- package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-element-base.js +10 -1
- package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/data-structures/binary-tree/binary-tree.js +669 -598
- package/dist/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/data-structures/binary-tree/bst.js +115 -113
- package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.js +7 -4
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +4 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/heap/heap.js +3 -3
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.js +4 -2
- package/dist/data-structures/queue/deque.d.ts +3 -3
- package/dist/data-structures/queue/deque.js +29 -29
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/stack/stack.d.ts +2 -2
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/package.json +2 -2
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +698 -726
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +2 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
|
-
BTNCallback,
|
|
4
3
|
BTNKeyOrNodeOrEntry,
|
|
4
|
+
BTNPredicate,
|
|
5
5
|
CRUD,
|
|
6
|
+
OptBSTN,
|
|
6
7
|
RBTNColor,
|
|
7
8
|
RBTreeOptions,
|
|
8
9
|
RedBlackTreeNested,
|
|
9
|
-
RedBlackTreeNodeNested
|
|
10
|
+
RedBlackTreeNodeNested,
|
|
11
|
+
BTNEntry
|
|
10
12
|
} from '../../types';
|
|
11
|
-
import { BTNEntry } from '../../types';
|
|
12
13
|
import { BST, BSTNode } from './bst';
|
|
13
14
|
import { IBinaryTree } from '../../interfaces';
|
|
14
15
|
|
|
@@ -64,7 +65,7 @@ export class RedBlackTree<
|
|
|
64
65
|
{
|
|
65
66
|
/**
|
|
66
67
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
67
|
-
* @param
|
|
68
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
|
|
68
69
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
69
70
|
* initialize the RBTree with the provided elements.
|
|
70
71
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -73,15 +74,15 @@ export class RedBlackTree<
|
|
|
73
74
|
* depend on the implementation
|
|
74
75
|
*/
|
|
75
76
|
constructor(
|
|
76
|
-
|
|
77
|
+
keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
77
78
|
options?: RBTreeOptions<K, V, R>
|
|
78
79
|
) {
|
|
79
80
|
super([], options);
|
|
80
81
|
|
|
81
82
|
this._root = this.NIL;
|
|
82
83
|
|
|
83
|
-
if (
|
|
84
|
-
this.addMany(
|
|
84
|
+
if (keysOrNodesOrEntriesOrRaws) {
|
|
85
|
+
this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
|
|
@@ -122,6 +123,8 @@ export class RedBlackTree<
|
|
|
122
123
|
override createTree(options?: RBTreeOptions<K, V, R>): TREE {
|
|
123
124
|
return new RedBlackTree<K, V, R, NODE, TREE>([], {
|
|
124
125
|
iterationType: this.iterationType,
|
|
126
|
+
comparator: this._comparator,
|
|
127
|
+
toEntryFn: this._toEntryFn,
|
|
125
128
|
...options
|
|
126
129
|
}) as TREE;
|
|
127
130
|
}
|
|
@@ -131,15 +134,13 @@ export class RedBlackTree<
|
|
|
131
134
|
* Space Complexity: O(1)
|
|
132
135
|
*
|
|
133
136
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
134
|
-
* @param {
|
|
135
|
-
* `
|
|
136
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
137
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
138
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
139
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
137
140
|
* an instance of the `RedBlackTreeNode` class.
|
|
138
141
|
*/
|
|
139
|
-
override isNode(
|
|
140
|
-
|
|
141
|
-
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
142
|
-
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
|
|
142
|
+
override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
|
|
143
|
+
return keyOrNodeOrEntryOrRaw instanceof RedBlackTreeNode;
|
|
143
144
|
}
|
|
144
145
|
|
|
145
146
|
// /**
|
|
@@ -153,27 +154,27 @@ export class RedBlackTree<
|
|
|
153
154
|
// *
|
|
154
155
|
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
|
|
155
156
|
// * valid, otherwise it returns undefined.
|
|
156
|
-
// * @param {BTNKeyOrNodeOrEntry<K, V, NODE>}
|
|
157
|
-
// * @param {V} [value] - The value associated with the key (if `
|
|
157
|
+
// * @param {BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRaw - The key, value, or entry to convert.
|
|
158
|
+
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRaw` is a key).
|
|
158
159
|
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
159
160
|
// */
|
|
160
|
-
// override keyValueOrEntryOrRawElementToNode(
|
|
161
|
+
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): NODE | undefined {
|
|
161
162
|
//
|
|
162
|
-
// if (
|
|
163
|
-
// if (this.isNode(
|
|
163
|
+
// if (keyOrNodeOrEntryOrRaw === null || keyOrNodeOrEntryOrRaw === undefined) return;
|
|
164
|
+
// if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
|
|
164
165
|
//
|
|
165
|
-
// if (this.
|
|
166
|
-
// const [key, entryValue] = this.
|
|
166
|
+
// if (this._toEntryFn) {
|
|
167
|
+
// const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
|
|
167
168
|
// if (this.isKey(key)) return this.createNode(key, entryValue ?? value, 'RED');
|
|
168
169
|
// }
|
|
169
170
|
//
|
|
170
|
-
// if (this.isEntry(
|
|
171
|
-
// const [key, value] =
|
|
171
|
+
// if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
172
|
+
// const [key, value] = keyOrNodeOrEntryOrRaw;
|
|
172
173
|
// if (key === undefined || key === null) return;
|
|
173
174
|
// else return this.createNode(key, value, 'RED');
|
|
174
175
|
// }
|
|
175
176
|
//
|
|
176
|
-
// if (this.isKey(
|
|
177
|
+
// if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.createNode(keyOrNodeOrEntryOrRaw, value, 'RED');
|
|
177
178
|
//
|
|
178
179
|
// return ;
|
|
179
180
|
// }
|
|
@@ -196,8 +197,8 @@ export class RedBlackTree<
|
|
|
196
197
|
*
|
|
197
198
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
198
199
|
* added.
|
|
199
|
-
* @param {
|
|
200
|
-
* `
|
|
200
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
201
|
+
* `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
201
202
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
202
203
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
203
204
|
* structure.
|
|
@@ -205,8 +206,8 @@ export class RedBlackTree<
|
|
|
205
206
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
206
207
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
207
208
|
*/
|
|
208
|
-
override add(
|
|
209
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
209
|
+
override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean {
|
|
210
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
|
|
210
211
|
if (!this.isRealNode(newNode)) return false;
|
|
211
212
|
|
|
212
213
|
const insertStatus = this._insert(newNode);
|
|
@@ -227,26 +228,23 @@ export class RedBlackTree<
|
|
|
227
228
|
* Time Complexity: O(log n)
|
|
228
229
|
* Space Complexity: O(1)
|
|
229
230
|
*
|
|
230
|
-
* The function overrides the delete method
|
|
231
|
-
*
|
|
232
|
-
* @param {
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
* @
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
* that extends the `BTNCallback
|
|
240
|
-
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
231
|
+
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
232
|
+
* a given predicate and maintain the binary search tree properties.
|
|
233
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
234
|
+
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
235
|
+
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
236
|
+
* function that determines which node(s) should be deleted.
|
|
237
|
+
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<NODE>`
|
|
238
|
+
* objects. Each object in the array contains information about the deleted node and whether
|
|
239
|
+
* balancing is needed.
|
|
241
240
|
*/
|
|
242
|
-
override delete<
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
): BinaryTreeDeleteResult<NODE>[] {
|
|
246
|
-
if (identifier === null) return [];
|
|
241
|
+
override delete(predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>): BinaryTreeDeleteResult<NODE>[] {
|
|
242
|
+
if (predicate === null) return [];
|
|
243
|
+
|
|
247
244
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
248
|
-
|
|
249
|
-
|
|
245
|
+
let nodeToDelete: OptBSTN<NODE>;
|
|
246
|
+
if (this._isPredicated(predicate)) nodeToDelete = this.getNode(predicate);
|
|
247
|
+
else nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate);
|
|
250
248
|
|
|
251
249
|
if (!nodeToDelete) {
|
|
252
250
|
return results;
|
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNKeyOrNode,
|
|
11
|
-
BTNCallback,
|
|
12
11
|
BTNKeyOrNodeOrEntry,
|
|
12
|
+
BTNPredicate,
|
|
13
13
|
IterationType,
|
|
14
|
+
OptBSTN,
|
|
14
15
|
RBTNColor,
|
|
15
16
|
TreeMultiMapNested,
|
|
16
17
|
TreeMultiMapNodeNested,
|
|
17
|
-
TreeMultiMapOptions
|
|
18
|
+
TreeMultiMapOptions,
|
|
19
|
+
BTNEntry
|
|
18
20
|
} from '../../types';
|
|
19
|
-
import { BTNEntry } from '../../types';
|
|
20
21
|
import { IBinaryTree } from '../../interfaces';
|
|
21
22
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
22
23
|
|
|
@@ -74,7 +75,7 @@ export class TreeMultiMap<
|
|
|
74
75
|
{
|
|
75
76
|
/**
|
|
76
77
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
77
|
-
* @param
|
|
78
|
+
* @param keysOrNodesOrEntriesOrRaws - The parameter `keysOrNodesOrEntriesOrRaws` is an
|
|
78
79
|
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
|
|
79
80
|
* TreeMultiMap with initial data.
|
|
80
81
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -82,11 +83,11 @@ export class TreeMultiMap<
|
|
|
82
83
|
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
83
84
|
*/
|
|
84
85
|
constructor(
|
|
85
|
-
|
|
86
|
+
keysOrNodesOrEntriesOrRaws: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
86
87
|
options?: TreeMultiMapOptions<K, V, R>
|
|
87
88
|
) {
|
|
88
89
|
super([], options);
|
|
89
|
-
if (
|
|
90
|
+
if (keysOrNodesOrEntriesOrRaws) this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
protected _count = 0;
|
|
@@ -142,6 +143,8 @@ export class TreeMultiMap<
|
|
|
142
143
|
override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE {
|
|
143
144
|
return new TreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
144
145
|
iterationType: this.iterationType,
|
|
146
|
+
comparator: this._comparator,
|
|
147
|
+
toEntryFn: this._toEntryFn,
|
|
145
148
|
...options
|
|
146
149
|
}) as TREE;
|
|
147
150
|
}
|
|
@@ -149,8 +152,8 @@ export class TreeMultiMap<
|
|
|
149
152
|
/**
|
|
150
153
|
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
|
|
151
154
|
* node based on the input.
|
|
152
|
-
* @param {
|
|
153
|
-
* `
|
|
155
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
156
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
154
157
|
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
155
158
|
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
156
159
|
* an existing node.
|
|
@@ -159,42 +162,39 @@ export class TreeMultiMap<
|
|
|
159
162
|
* @returns either a NODE object or undefined.
|
|
160
163
|
*/
|
|
161
164
|
override keyValueOrEntryOrRawElementToNode(
|
|
162
|
-
|
|
165
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
163
166
|
value?: V,
|
|
164
167
|
count = 1
|
|
165
168
|
): NODE | undefined {
|
|
166
|
-
if (
|
|
169
|
+
if (keyOrNodeOrEntryOrRaw === undefined || keyOrNodeOrEntryOrRaw === null) return;
|
|
167
170
|
|
|
168
|
-
if (this.isNode(
|
|
171
|
+
if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
|
|
169
172
|
|
|
170
|
-
if (this.isEntry(
|
|
171
|
-
const [key, entryValue] =
|
|
173
|
+
if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
174
|
+
const [key, entryValue] = keyOrNodeOrEntryOrRaw;
|
|
172
175
|
if (key === undefined || key === null) return;
|
|
173
176
|
if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'BLACK', count);
|
|
174
177
|
}
|
|
175
178
|
|
|
176
|
-
if (this.
|
|
177
|
-
const [key, entryValue] = this.
|
|
179
|
+
if (this._toEntryFn) {
|
|
180
|
+
const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
|
|
178
181
|
if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'BLACK', count);
|
|
179
182
|
}
|
|
180
183
|
|
|
181
|
-
if (this.isKey(
|
|
182
|
-
return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'BLACK', count);
|
|
184
|
+
if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.createNode(keyOrNodeOrEntryOrRaw, value, 'BLACK', count);
|
|
183
185
|
|
|
184
186
|
return;
|
|
185
187
|
}
|
|
186
188
|
|
|
187
189
|
/**
|
|
188
190
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
189
|
-
* @param {
|
|
190
|
-
* `
|
|
191
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
191
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
192
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
193
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
192
194
|
* an instance of the `TreeMultiMapNode` class.
|
|
193
195
|
*/
|
|
194
|
-
override isNode(
|
|
195
|
-
|
|
196
|
-
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
197
|
-
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
|
|
196
|
+
override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
|
|
197
|
+
return keyOrNodeOrEntryOrRaw instanceof TreeMultiMapNode;
|
|
198
198
|
}
|
|
199
199
|
|
|
200
200
|
/**
|
|
@@ -203,8 +203,8 @@ export class TreeMultiMap<
|
|
|
203
203
|
*
|
|
204
204
|
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
205
205
|
* the count and returning a boolean indicating success.
|
|
206
|
-
* @param {
|
|
207
|
-
* `
|
|
206
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
|
|
207
|
+
* `keyOrNodeOrEntryOrRaw` parameter can accept one of the following types:
|
|
208
208
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
209
209
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
210
210
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
@@ -213,8 +213,8 @@ export class TreeMultiMap<
|
|
|
213
213
|
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
214
214
|
* was successful, and false otherwise.
|
|
215
215
|
*/
|
|
216
|
-
override add(
|
|
217
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
216
|
+
override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V, count = 1): boolean {
|
|
217
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value, count);
|
|
218
218
|
const orgCount = newNode?.count || 0;
|
|
219
219
|
const isSuccessAdded = super.add(newNode);
|
|
220
220
|
|
|
@@ -230,30 +230,29 @@ export class TreeMultiMap<
|
|
|
230
230
|
* Time Complexity: O(log n)
|
|
231
231
|
* Space Complexity: O(1)
|
|
232
232
|
*
|
|
233
|
-
* The function `delete`
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
* the
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
* it. If set to false, the count of the node will be decremented by 1 before deleting it.
|
|
245
|
-
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
233
|
+
* The function `delete` in TypeScript overrides the deletion operation in a binary tree data
|
|
234
|
+
* structure, handling cases where nodes have children and maintaining balance in the tree.
|
|
235
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
236
|
+
* parameter in the `delete` method is used to specify the condition or key based on which a node
|
|
237
|
+
* should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
238
|
+
* function.
|
|
239
|
+
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
240
|
+
* boolean flag that determines whether to ignore the count of nodes when performing deletion. If
|
|
241
|
+
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
|
|
242
|
+
* `ignoreCount` is `false
|
|
243
|
+
* @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
|
|
246
244
|
*/
|
|
247
|
-
override delete
|
|
248
|
-
|
|
249
|
-
callback: C = this._DEFAULT_CALLBACK as C,
|
|
245
|
+
override delete(
|
|
246
|
+
predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
250
247
|
ignoreCount = false
|
|
251
248
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
252
|
-
if (
|
|
249
|
+
if (predicate === null) return [];
|
|
250
|
+
|
|
253
251
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
254
|
-
callback = this._ensureCallback(identifier, callback);
|
|
255
252
|
|
|
256
|
-
|
|
253
|
+
let nodeToDelete: OptBSTN<NODE>;
|
|
254
|
+
if (this._isPredicated(predicate)) nodeToDelete = this.getNode(predicate);
|
|
255
|
+
else nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate);
|
|
257
256
|
|
|
258
257
|
if (!nodeToDelete) {
|
|
259
258
|
return results;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { DijkstraResult, EntryCallback, VertexKey } from '../../types';
|
|
@@ -564,7 +564,7 @@ export abstract class AbstractGraph<
|
|
|
564
564
|
}
|
|
565
565
|
}
|
|
566
566
|
|
|
567
|
-
getMinDist
|
|
567
|
+
if (getMinDist)
|
|
568
568
|
distMap.forEach((d, v) => {
|
|
569
569
|
if (v !== srcVertex) {
|
|
570
570
|
if (d < minDist) {
|
|
@@ -574,7 +574,7 @@ export abstract class AbstractGraph<
|
|
|
574
574
|
}
|
|
575
575
|
});
|
|
576
576
|
|
|
577
|
-
genPaths
|
|
577
|
+
if (genPaths) getPaths(minDest);
|
|
578
578
|
|
|
579
579
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
580
580
|
}
|
|
@@ -761,7 +761,7 @@ export abstract class AbstractGraph<
|
|
|
761
761
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
762
762
|
if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
|
|
763
763
|
distMap.set(d, sWeight + weight);
|
|
764
|
-
genPath
|
|
764
|
+
if (genPath) preMap.set(d, s);
|
|
765
765
|
}
|
|
766
766
|
}
|
|
767
767
|
}
|
|
@@ -900,7 +900,7 @@ export abstract class AbstractGraph<
|
|
|
900
900
|
currentPath.push(vertex.key);
|
|
901
901
|
|
|
902
902
|
for (const neighbor of this.getNeighbors(vertex)) {
|
|
903
|
-
neighbor
|
|
903
|
+
if (neighbor) dfs(neighbor, currentPath, visited);
|
|
904
904
|
}
|
|
905
905
|
|
|
906
906
|
visited.delete(vertex);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { TopologicalStatus, VertexKey } from '../../types';
|
|
@@ -260,8 +260,8 @@ export class DirectedGraph<
|
|
|
260
260
|
const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
|
|
261
261
|
const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
|
|
262
262
|
|
|
263
|
-
v1ToV2
|
|
264
|
-
v2ToV1
|
|
263
|
+
if (v1ToV2) removed.push(v1ToV2);
|
|
264
|
+
if (v2ToV1) removed.push(v2ToV1);
|
|
265
265
|
}
|
|
266
266
|
|
|
267
267
|
return removed;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { VertexKey } from '../../types';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
@@ -111,7 +111,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
111
111
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
112
112
|
*/
|
|
113
113
|
isEmpty(): boolean {
|
|
114
|
-
return this.
|
|
114
|
+
return this._size === 0;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
/**
|
|
@@ -626,7 +626,7 @@ export class FibonacciHeap<E> {
|
|
|
626
626
|
* @returns The top element or undefined if the heap is empty.
|
|
627
627
|
*/
|
|
628
628
|
pop(): E | undefined {
|
|
629
|
-
if (this.
|
|
629
|
+
if (this._size === 0) return undefined;
|
|
630
630
|
|
|
631
631
|
const z = this.min!;
|
|
632
632
|
if (z.child) {
|
|
@@ -770,7 +770,7 @@ export class FibonacciHeap<E> {
|
|
|
770
770
|
* @protected
|
|
771
771
|
*/
|
|
772
772
|
protected _consolidate(): void {
|
|
773
|
-
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.
|
|
773
|
+
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this._size);
|
|
774
774
|
const elements = this.consumeLinkedList(this.root);
|
|
775
775
|
let x: FibonacciHeapNode<E> | undefined,
|
|
776
776
|
y: FibonacciHeapNode<E> | undefined,
|
|
@@ -798,7 +798,7 @@ export class FibonacciHeap<E> {
|
|
|
798
798
|
A[d] = x;
|
|
799
799
|
}
|
|
800
800
|
|
|
801
|
-
for (let i = 0; i < this.
|
|
801
|
+
for (let i = 0; i < this._size; i++) {
|
|
802
802
|
if (A[i] && this.comparator(A[i]!.element, this.min!.element) <= 0) {
|
|
803
803
|
this._min = A[i]!;
|
|
804
804
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { DoublyLinkedListOptions, ElementCallback } from '../../types';
|
|
@@ -267,7 +267,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
267
267
|
* or the linked list is empty, it will return undefined.
|
|
268
268
|
*/
|
|
269
269
|
at(index: number): E | undefined {
|
|
270
|
-
if (index < 0 || index >= this.
|
|
270
|
+
if (index < 0 || index >= this._size) return undefined;
|
|
271
271
|
let current = this.head;
|
|
272
272
|
for (let i = 0; i < index; i++) {
|
|
273
273
|
current = current!.next;
|
|
@@ -287,7 +287,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
287
287
|
* valid range of the linked list, otherwise it returns `undefined`.
|
|
288
288
|
*/
|
|
289
289
|
getNodeAt(index: number): DoublyLinkedListNode<E> | undefined {
|
|
290
|
-
if (index < 0 || index >= this.
|
|
290
|
+
if (index < 0 || index >= this._size) return undefined;
|
|
291
291
|
let current = this.head;
|
|
292
292
|
for (let i = 0; i < index; i++) {
|
|
293
293
|
current = current!.next;
|
|
@@ -331,12 +331,12 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
331
331
|
* if the index is out of bounds.
|
|
332
332
|
*/
|
|
333
333
|
addAt(index: number, value: E): boolean {
|
|
334
|
-
if (index < 0 || index > this.
|
|
334
|
+
if (index < 0 || index > this._size) return false;
|
|
335
335
|
if (index === 0) {
|
|
336
336
|
this.unshift(value);
|
|
337
337
|
return true;
|
|
338
338
|
}
|
|
339
|
-
if (index === this.
|
|
339
|
+
if (index === this._size) {
|
|
340
340
|
this.push(value);
|
|
341
341
|
return true;
|
|
342
342
|
}
|
|
@@ -442,12 +442,12 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
442
442
|
* bounds.
|
|
443
443
|
*/
|
|
444
444
|
deleteAt(index: number): boolean {
|
|
445
|
-
if (index < 0 || index >= this.
|
|
445
|
+
if (index < 0 || index >= this._size) return false;
|
|
446
446
|
if (index === 0) {
|
|
447
447
|
this.shift();
|
|
448
448
|
return true;
|
|
449
449
|
}
|
|
450
|
-
if (index === this.
|
|
450
|
+
if (index === this._size - 1) {
|
|
451
451
|
this.pop();
|
|
452
452
|
return true;
|
|
453
453
|
}
|
|
@@ -505,7 +505,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
505
505
|
* @returns A boolean value is being returned.
|
|
506
506
|
*/
|
|
507
507
|
isEmpty(): boolean {
|
|
508
|
-
return this.
|
|
508
|
+
return this._size === 0;
|
|
509
509
|
}
|
|
510
510
|
|
|
511
511
|
/**
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
|
|
@@ -237,7 +237,7 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
237
237
|
* `undefined` if the index is out of bounds.
|
|
238
238
|
*/
|
|
239
239
|
at(index: number): E | undefined {
|
|
240
|
-
if (index < 0 || index >= this.
|
|
240
|
+
if (index < 0 || index >= this._size) return undefined;
|
|
241
241
|
let current = this.head;
|
|
242
242
|
for (let i = 0; i < index; i++) {
|
|
243
243
|
current = current!.next;
|
|
@@ -274,12 +274,12 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
274
274
|
* bounds.
|
|
275
275
|
*/
|
|
276
276
|
deleteAt(index: number): boolean {
|
|
277
|
-
if (index < 0 || index >= this.
|
|
277
|
+
if (index < 0 || index >= this._size) return false;
|
|
278
278
|
if (index === 0) {
|
|
279
279
|
this.shift();
|
|
280
280
|
return true;
|
|
281
281
|
}
|
|
282
|
-
if (index === this.
|
|
282
|
+
if (index === this._size - 1) {
|
|
283
283
|
this.pop();
|
|
284
284
|
return true;
|
|
285
285
|
}
|
|
@@ -348,12 +348,12 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
348
348
|
* if the index is out of bounds.
|
|
349
349
|
*/
|
|
350
350
|
addAt(index: number, value: E): boolean {
|
|
351
|
-
if (index < 0 || index > this.
|
|
351
|
+
if (index < 0 || index > this._size) return false;
|
|
352
352
|
if (index === 0) {
|
|
353
353
|
this.unshift(value);
|
|
354
354
|
return true;
|
|
355
355
|
}
|
|
356
|
-
if (index === this.
|
|
356
|
+
if (index === this._size) {
|
|
357
357
|
this.push(value);
|
|
358
358
|
return true;
|
|
359
359
|
}
|
|
@@ -372,7 +372,7 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
372
372
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
373
373
|
*/
|
|
374
374
|
isEmpty(): boolean {
|
|
375
|
-
return this.
|
|
375
|
+
return this._size === 0;
|
|
376
376
|
}
|
|
377
377
|
|
|
378
378
|
/**
|