data-structure-typed 1.47.8 → 1.48.0
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/CHANGELOG.md +1 -1
- package/README.md +33 -32
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +8 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +14 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +52 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +13 -0
- package/dist/cjs/data-structures/binary-tree/bst.js +41 -21
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +54 -31
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +44 -21
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +5 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js +41 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +8 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +14 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +52 -28
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +13 -0
- package/dist/mjs/data-structures/binary-tree/bst.js +41 -21
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +16 -0
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +54 -31
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -0
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +44 -21
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +5 -0
- package/dist/mjs/data-structures/graph/abstract-graph.js +41 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +1 -1
- package/dist/umd/data-structure-typed.js +228 -90
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +9 -0
- package/src/data-structures/binary-tree/binary-tree.ts +47 -23
- package/src/data-structures/binary-tree/bst.ts +38 -19
- package/src/data-structures/binary-tree/rb-tree.ts +55 -31
- package/src/data-structures/binary-tree/tree-multimap.ts +42 -17
- package/src/data-structures/graph/abstract-graph.ts +46 -0
- package/src/data-structures/graph/directed-graph.ts +1 -1
- package/test/integration/index.html +2 -2
- package/test/unit/data-structures/graph/directed-graph.test.ts +41 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.48.0",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, RedBlack Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -82,6 +82,15 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
82
82
|
}) as TREE;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
/**
|
|
86
|
+
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
87
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
88
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
89
|
+
*/
|
|
90
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
91
|
+
return exemplar instanceof AVLTreeNode;
|
|
92
|
+
}
|
|
93
|
+
|
|
85
94
|
/**
|
|
86
95
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
87
96
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -163,6 +163,47 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
163
163
|
return new BinaryTree<V, N, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
+
/**
|
|
167
|
+
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
|
168
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
169
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
170
|
+
*/
|
|
171
|
+
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
172
|
+
return exemplar instanceof BinaryTreeNode;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
|
|
177
|
+
* object.
|
|
178
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
|
|
179
|
+
* function. It can be any type.
|
|
180
|
+
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
|
|
181
|
+
*/
|
|
182
|
+
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | null | undefined {
|
|
183
|
+
if (exemplar === undefined) return;
|
|
184
|
+
|
|
185
|
+
let node: N | null | undefined;
|
|
186
|
+
if (exemplar === null) {
|
|
187
|
+
node = null;
|
|
188
|
+
} else if (this.isEntry(exemplar)) {
|
|
189
|
+
const [key, value] = exemplar;
|
|
190
|
+
if (key === undefined) {
|
|
191
|
+
return;
|
|
192
|
+
} else if (key === null) {
|
|
193
|
+
node = null;
|
|
194
|
+
} else {
|
|
195
|
+
node = this.createNode(key, value);
|
|
196
|
+
}
|
|
197
|
+
} else if (this.isNode(exemplar)) {
|
|
198
|
+
node = exemplar;
|
|
199
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
200
|
+
node = this.createNode(exemplar);
|
|
201
|
+
} else {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
return node;
|
|
205
|
+
}
|
|
206
|
+
|
|
166
207
|
/**
|
|
167
208
|
* The function checks if a given value is an entry in a binary tree node.
|
|
168
209
|
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
@@ -188,7 +229,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
188
229
|
*/
|
|
189
230
|
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | null | undefined {
|
|
190
231
|
|
|
191
|
-
let inserted: N | null | undefined
|
|
232
|
+
let inserted: N | null | undefined;
|
|
233
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
234
|
+
if (newNode === undefined) return;
|
|
192
235
|
|
|
193
236
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
194
237
|
const queue = new Queue<N>([root]);
|
|
@@ -205,30 +248,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
205
248
|
}
|
|
206
249
|
};
|
|
207
250
|
|
|
208
|
-
if (keyOrNodeOrEntry === null) {
|
|
209
|
-
needInsert = null;
|
|
210
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
211
|
-
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
212
|
-
} else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
213
|
-
needInsert = keyOrNodeOrEntry;
|
|
214
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
215
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
216
|
-
if (key === undefined) {
|
|
217
|
-
return;
|
|
218
|
-
} else if (key === null) {
|
|
219
|
-
needInsert = null;
|
|
220
|
-
} else {
|
|
221
|
-
needInsert = this.createNode(key, value);
|
|
222
|
-
}
|
|
223
|
-
} else {
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
251
|
if (this.root) {
|
|
228
|
-
inserted = _bfs(this.root,
|
|
252
|
+
inserted = _bfs(this.root, newNode);
|
|
229
253
|
} else {
|
|
230
|
-
this._setRoot(
|
|
231
|
-
if (
|
|
254
|
+
this._setRoot(newNode);
|
|
255
|
+
if (newNode) {
|
|
232
256
|
this._size = 1;
|
|
233
257
|
} else {
|
|
234
258
|
this._size = 0;
|
|
@@ -143,6 +143,42 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
143
143
|
}) as TREE;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
/**
|
|
147
|
+
* The function checks if an exemplar is an instance of BSTNode.
|
|
148
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
149
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
150
|
+
*/
|
|
151
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
152
|
+
return exemplar instanceof BSTNode;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
157
|
+
* is valid, otherwise it returns undefined.
|
|
158
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
159
|
+
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
160
|
+
*/
|
|
161
|
+
override exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined {
|
|
162
|
+
let node: N | undefined;
|
|
163
|
+
if (exemplar === null || exemplar === undefined) {
|
|
164
|
+
return;
|
|
165
|
+
} else if (this.isNode(exemplar)) {
|
|
166
|
+
node = exemplar;
|
|
167
|
+
} else if (this.isEntry(exemplar)) {
|
|
168
|
+
const [key, value] = exemplar;
|
|
169
|
+
if (key === undefined || key === null) {
|
|
170
|
+
return;
|
|
171
|
+
} else {
|
|
172
|
+
node = this.createNode(key, value);
|
|
173
|
+
}
|
|
174
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
175
|
+
node = this.createNode(exemplar);
|
|
176
|
+
} else {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
return node;
|
|
180
|
+
}
|
|
181
|
+
|
|
146
182
|
/**
|
|
147
183
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
148
184
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -159,25 +195,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
159
195
|
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
160
196
|
*/
|
|
161
197
|
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
let newNode: N | undefined;
|
|
167
|
-
if (keyOrNodeOrEntry instanceof BSTNode) {
|
|
168
|
-
newNode = keyOrNodeOrEntry;
|
|
169
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
170
|
-
newNode = this.createNode(keyOrNodeOrEntry);
|
|
171
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
172
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
173
|
-
if (key === undefined || key === null) {
|
|
174
|
-
return;
|
|
175
|
-
} else {
|
|
176
|
-
newNode = this.createNode(key, value);
|
|
177
|
-
}
|
|
178
|
-
} else {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
198
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
199
|
+
if (newNode === undefined) return;
|
|
181
200
|
|
|
182
201
|
if (this.root === undefined) {
|
|
183
202
|
this._setRoot(newNode);
|
|
@@ -107,38 +107,62 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
110
|
+
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
111
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
112
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
113
|
+
* class.
|
|
112
114
|
*/
|
|
113
|
-
|
|
115
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
116
|
+
return exemplar instanceof RedBlackTreeNode;
|
|
117
|
+
}
|
|
114
118
|
|
|
115
119
|
/**
|
|
116
|
-
* The function
|
|
117
|
-
*
|
|
118
|
-
* @
|
|
119
|
-
*
|
|
120
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
121
|
+
* otherwise it returns undefined.
|
|
122
|
+
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
123
|
+
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
124
|
+
* that is not a valid exemplar.
|
|
125
|
+
* @returns a variable `node` which is of type `N | undefined`.
|
|
120
126
|
*/
|
|
121
|
-
override
|
|
122
|
-
let node: N;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
} else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
126
|
-
node = keyOrNodeOrEntry;
|
|
127
|
-
} else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
127
|
+
override exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined {
|
|
128
|
+
let node: N | undefined;
|
|
129
|
+
|
|
130
|
+
if (exemplar === null || exemplar === undefined) {
|
|
128
131
|
return;
|
|
129
|
-
} else if (this.
|
|
130
|
-
|
|
132
|
+
} else if (this.isNode(exemplar)) {
|
|
133
|
+
node = exemplar;
|
|
134
|
+
} else if (this.isEntry(exemplar)) {
|
|
135
|
+
const [key, value] = exemplar;
|
|
131
136
|
if (key === undefined || key === null) {
|
|
132
137
|
return;
|
|
133
138
|
} else {
|
|
134
139
|
node = this.createNode(key, value, RBTNColor.RED);
|
|
135
140
|
}
|
|
141
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
142
|
+
node = this.createNode(exemplar, undefined, RBTNColor.RED);
|
|
136
143
|
} else {
|
|
137
144
|
return;
|
|
138
145
|
}
|
|
146
|
+
return node;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
151
|
+
* Space Complexity: O(1)
|
|
152
|
+
*/
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
156
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
157
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
158
|
+
* `undefined`.
|
|
159
|
+
*/
|
|
160
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
|
|
161
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
162
|
+
if (newNode === undefined) return;
|
|
139
163
|
|
|
140
|
-
|
|
141
|
-
|
|
164
|
+
newNode.left = this.Sentinel;
|
|
165
|
+
newNode.right = this.Sentinel;
|
|
142
166
|
|
|
143
167
|
let y: N | undefined = undefined;
|
|
144
168
|
let x: N | undefined = this.root;
|
|
@@ -146,13 +170,13 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
146
170
|
while (x !== this.Sentinel) {
|
|
147
171
|
y = x;
|
|
148
172
|
if (x) {
|
|
149
|
-
if (
|
|
173
|
+
if (newNode.key < x.key) {
|
|
150
174
|
x = x.left;
|
|
151
|
-
} else if (
|
|
175
|
+
} else if (newNode.key > x.key) {
|
|
152
176
|
x = x?.right;
|
|
153
177
|
} else {
|
|
154
|
-
if (
|
|
155
|
-
this._replaceNode(x,
|
|
178
|
+
if (newNode !== x) {
|
|
179
|
+
this._replaceNode(x, newNode)
|
|
156
180
|
}
|
|
157
181
|
return;
|
|
158
182
|
}
|
|
@@ -160,27 +184,27 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
160
184
|
|
|
161
185
|
}
|
|
162
186
|
|
|
163
|
-
|
|
187
|
+
newNode.parent = y;
|
|
164
188
|
if (y === undefined) {
|
|
165
|
-
this._setRoot(
|
|
166
|
-
} else if (
|
|
167
|
-
y.left =
|
|
189
|
+
this._setRoot(newNode);
|
|
190
|
+
} else if (newNode.key < y.key) {
|
|
191
|
+
y.left = newNode;
|
|
168
192
|
} else {
|
|
169
|
-
y.right =
|
|
193
|
+
y.right = newNode;
|
|
170
194
|
}
|
|
171
195
|
|
|
172
|
-
if (
|
|
173
|
-
|
|
196
|
+
if (newNode.parent === undefined) {
|
|
197
|
+
newNode.color = RBTNColor.BLACK;
|
|
174
198
|
this._size++;
|
|
175
199
|
return;
|
|
176
200
|
}
|
|
177
201
|
|
|
178
|
-
if (
|
|
202
|
+
if (newNode.parent.parent === undefined) {
|
|
179
203
|
this._size++;
|
|
180
204
|
return;
|
|
181
205
|
}
|
|
182
206
|
|
|
183
|
-
this._fixInsert(
|
|
207
|
+
this._fixInsert(newNode);
|
|
184
208
|
this._size++;
|
|
185
209
|
}
|
|
186
210
|
|
|
@@ -80,6 +80,45 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
80
80
|
}) as TREE;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
85
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
86
|
+
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
87
|
+
* class.
|
|
88
|
+
*/
|
|
89
|
+
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
90
|
+
return exemplar instanceof TreeMultimapNode;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
95
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents
|
|
96
|
+
* the value type and `N` represents the node type.
|
|
97
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
98
|
+
* times the node should be created. If not provided, it defaults to 1.
|
|
99
|
+
* @returns a value of type `N` (the generic type parameter) or `undefined`.
|
|
100
|
+
*/
|
|
101
|
+
override exemplarToNode(exemplar: BTNodeExemplar<V, N>, count = 1): N | undefined {
|
|
102
|
+
let node: N | undefined;
|
|
103
|
+
if (exemplar === undefined || exemplar === null) {
|
|
104
|
+
return;
|
|
105
|
+
} else if (this.isNode(exemplar)) {
|
|
106
|
+
node = exemplar;
|
|
107
|
+
} else if (this.isEntry(exemplar)) {
|
|
108
|
+
const [key, value] = exemplar;
|
|
109
|
+
if (key === undefined || key === null) {
|
|
110
|
+
return;
|
|
111
|
+
} else {
|
|
112
|
+
node = this.createNode(key, value, count);
|
|
113
|
+
}
|
|
114
|
+
} else if (this.isNodeKey(exemplar)) {
|
|
115
|
+
node = this.createNode(exemplar, undefined, count);
|
|
116
|
+
} else {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
return node;
|
|
120
|
+
}
|
|
121
|
+
|
|
83
122
|
/**
|
|
84
123
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
85
124
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -98,23 +137,9 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
|
|
|
98
137
|
* @returns either a node (`N`) or `undefined`.
|
|
99
138
|
*/
|
|
100
139
|
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count = 1): N | undefined {
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
} else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
105
|
-
newNode = keyOrNodeOrEntry;
|
|
106
|
-
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
107
|
-
newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
|
|
108
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
109
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
110
|
-
if (key === undefined || key === null) {
|
|
111
|
-
return;
|
|
112
|
-
} else {
|
|
113
|
-
newNode = this.createNode(key, value, count);
|
|
114
|
-
}
|
|
115
|
-
} else {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
140
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
|
|
141
|
+
if (newNode === undefined) return;
|
|
142
|
+
|
|
118
143
|
const orgNodeCount = newNode?.count || 0;
|
|
119
144
|
const inserted = super.add(newNode);
|
|
120
145
|
if (inserted) {
|
|
@@ -1159,6 +1159,52 @@ export abstract class AbstractGraph<
|
|
|
1159
1159
|
return this.tarjan(false, true, false, false).bridges;
|
|
1160
1160
|
}
|
|
1161
1161
|
|
|
1162
|
+
* [Symbol.iterator](): Iterator<[VertexKey, V | undefined]> {
|
|
1163
|
+
for (const vertex of this._vertices.values()) {
|
|
1164
|
+
yield [vertex.key, vertex.value];
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
forEach(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => void): void {
|
|
1169
|
+
let index = 0;
|
|
1170
|
+
for (const vertex of this) {
|
|
1171
|
+
callback(vertex, index, this._vertices);
|
|
1172
|
+
index++;
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
filter(predicate: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => boolean): [VertexKey, V | undefined][] {
|
|
1177
|
+
const filtered: [VertexKey, V | undefined][] = [];
|
|
1178
|
+
let index = 0;
|
|
1179
|
+
for (const entry of this) {
|
|
1180
|
+
if (predicate(entry, index, this._vertices)) {
|
|
1181
|
+
filtered.push(entry);
|
|
1182
|
+
}
|
|
1183
|
+
index++;
|
|
1184
|
+
}
|
|
1185
|
+
return filtered;
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
map<T>(callback: (entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T): T[] {
|
|
1189
|
+
const mapped: T[] = [];
|
|
1190
|
+
let index = 0;
|
|
1191
|
+
for (const entry of this) {
|
|
1192
|
+
mapped.push(callback(entry, index, this._vertices));
|
|
1193
|
+
index++;
|
|
1194
|
+
}
|
|
1195
|
+
return mapped;
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
reduce<T>(callback: (accumulator: T, entry: [VertexKey, V | undefined], index: number, map: Map<VertexKey, VO>) => T, initialValue: T): T {
|
|
1199
|
+
let accumulator: T = initialValue;
|
|
1200
|
+
let index = 0;
|
|
1201
|
+
for (const entry of this) {
|
|
1202
|
+
accumulator = callback(accumulator, entry, index, this._vertices);
|
|
1203
|
+
index++;
|
|
1204
|
+
}
|
|
1205
|
+
return accumulator;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1162
1208
|
protected abstract _addEdgeOnly(edge: EO): boolean;
|
|
1163
1209
|
|
|
1164
1210
|
protected _addVertexOnly(newVertex: VO): boolean {
|
|
@@ -87,7 +87,7 @@ export class DirectedGraph<
|
|
|
87
87
|
* @returns a new instance of a DirectedVertex object, casted as type VO.
|
|
88
88
|
*/
|
|
89
89
|
createVertex(key: VertexKey, value?: V): VO {
|
|
90
|
-
return new DirectedVertex(key, value
|
|
90
|
+
return new DirectedVertex(key, value) as VO;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
|
@@ -349,7 +349,7 @@
|
|
|
349
349
|
// 1 _4 6 _9
|
|
350
350
|
// / /
|
|
351
351
|
// 3 8
|
|
352
|
-
|
|
352
|
+
|
|
353
353
|
|
|
354
354
|
const trie2 = new Trie(orgStrArr);
|
|
355
355
|
trie2.print(); // ['trie', 'trial', 'triangle', 'trick', 'trip', 'tree', 'trend', 'track', 'trace', 'transmit']
|
|
@@ -367,7 +367,7 @@
|
|
|
367
367
|
// 0 2 _5_ 8_
|
|
368
368
|
// / \ \
|
|
369
369
|
// 4 6 9
|
|
370
|
-
|
|
370
|
+
|
|
371
371
|
} catch (e) {
|
|
372
372
|
console.error(e);
|
|
373
373
|
}
|
|
@@ -595,3 +595,44 @@ describe('cycles, strongly connected components, bridges, articular points in Di
|
|
|
595
595
|
expect(dfnMap.size).toBe(8);
|
|
596
596
|
expect(lowMap.size).toBe(8);
|
|
597
597
|
});
|
|
598
|
+
|
|
599
|
+
describe('DirectedGraph iterative Methods', () => {
|
|
600
|
+
let graph: DirectedGraph<string>;
|
|
601
|
+
let vertices: string[];
|
|
602
|
+
|
|
603
|
+
beforeEach(() => {
|
|
604
|
+
graph = new DirectedGraph();
|
|
605
|
+
vertices = ['A', 'B', 'C', 'D'];
|
|
606
|
+
vertices.forEach(vertex => graph.addVertex(vertex));
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
test('[Symbol.iterator] should iterate over all vertices', () => {
|
|
610
|
+
const iteratedVertices = [];
|
|
611
|
+
for (const vertex of graph) {
|
|
612
|
+
iteratedVertices.push(vertex[0]);
|
|
613
|
+
}
|
|
614
|
+
expect(iteratedVertices).toEqual(vertices);
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
test('forEach should apply a function to each vertex', () => {
|
|
618
|
+
const result: VertexKey[] = [];
|
|
619
|
+
graph.forEach(vertex => result.push(vertex[0]));
|
|
620
|
+
expect(result).toEqual(vertices);
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
test('filter should return vertices that satisfy the condition', () => {
|
|
624
|
+
const filtered = graph.filter(vertex => vertex[0] === 'A' || vertex[0] === 'B');
|
|
625
|
+
expect(filtered).toEqual([["A", undefined], ["B", undefined]]);
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
test('map should apply a function to each vertex and return a new array', () => {
|
|
629
|
+
const mapped = graph.map(vertex => vertex[0] + '_mapped');
|
|
630
|
+
expect(mapped).toEqual(vertices.map(v => v + '_mapped'));
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
test('reduce should accumulate a value based on each vertex', () => {
|
|
634
|
+
const concatenated = graph.reduce((acc, vertex) => acc + vertex[0], '');
|
|
635
|
+
expect(concatenated).toBe(vertices.join(''));
|
|
636
|
+
});
|
|
637
|
+
});
|
|
638
|
+
|