data-structure-typed 1.48.0 → 1.48.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +24 -18
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +188 -308
- package/dist/cjs/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/data-structures/base/index.js +18 -0
- package/dist/cjs/data-structures/base/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/cjs/data-structures/base/iterable-base.js +313 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +45 -40
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +91 -88
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +16 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/cjs/data-structures/hash/hash-map.js +314 -82
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/cjs/data-structures/heap/heap.js +60 -30
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/index.d.ts +1 -0
- package/dist/cjs/data-structures/index.js +1 -0
- package/dist/cjs/data-structures/index.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/cjs/data-structures/queue/deque.js +36 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/cjs/data-structures/queue/queue.js +69 -82
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/cjs/data-structures/stack/stack.js +50 -31
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/cjs/data-structures/trie/trie.js +53 -32
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/cjs/types/data-structures/base/base.js +3 -0
- package/dist/cjs/types/data-structures/base/base.js.map +1 -0
- package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/base/index.js +18 -0
- package/dist/cjs/types/data-structures/base/index.js.map +1 -0
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/cjs/types/data-structures/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/index.js +1 -0
- package/dist/cjs/types/data-structures/index.js.map +1 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/data-structures/base/index.js +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/mjs/data-structures/base/iterable-base.js +307 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +45 -40
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +91 -88
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +16 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
- package/dist/mjs/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/mjs/data-structures/hash/hash-map.js +310 -80
- package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/mjs/data-structures/heap/heap.js +60 -30
- package/dist/mjs/data-structures/index.d.ts +1 -0
- package/dist/mjs/data-structures/index.js +1 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/mjs/data-structures/queue/deque.js +36 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/mjs/data-structures/queue/queue.js +66 -79
- package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/mjs/data-structures/stack/stack.js +50 -31
- package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/mjs/data-structures/trie/trie.js +53 -32
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/mjs/types/data-structures/base/base.js +1 -0
- package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/base/index.js +1 -0
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/mjs/types/data-structures/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/index.js +1 -0
- package/dist/umd/data-structure-typed.js +1104 -575
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/binary-tree.ts +98 -93
- package/src/data-structures/binary-tree/tree-multimap.ts +18 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +334 -83
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/src/types/data-structures/index.ts +1 -0
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +21 -25
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +17 -12
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -8
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +24 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +24 -8
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +82 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +312 -18
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
- package/test/unit/data-structures/queue/deque.test.ts +25 -0
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { FamilyPosition, IterationType } from '../../types';
|
|
9
9
|
import { trampoline } from '../../utils';
|
|
10
10
|
import { Queue } from '../queue';
|
|
11
|
+
import { IterablePairBase } from "../base";
|
|
11
12
|
/**
|
|
12
13
|
* Represents a node in a binary tree.
|
|
13
14
|
* @template V - The type of data stored in the node.
|
|
@@ -70,7 +71,7 @@ export class BinaryTreeNode {
|
|
|
70
71
|
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
71
72
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
72
73
|
*/
|
|
73
|
-
export class BinaryTree {
|
|
74
|
+
export class BinaryTree extends IterablePairBase {
|
|
74
75
|
iterationType = IterationType.ITERATIVE;
|
|
75
76
|
/**
|
|
76
77
|
* The constructor function initializes a binary tree object with optional elements and options.
|
|
@@ -82,6 +83,7 @@ export class BinaryTree {
|
|
|
82
83
|
* required.
|
|
83
84
|
*/
|
|
84
85
|
constructor(elements, options) {
|
|
86
|
+
super();
|
|
85
87
|
if (options) {
|
|
86
88
|
const { iterationType } = options;
|
|
87
89
|
if (iterationType) {
|
|
@@ -1405,113 +1407,86 @@ export class BinaryTree {
|
|
|
1405
1407
|
}
|
|
1406
1408
|
/**
|
|
1407
1409
|
* Time complexity: O(n)
|
|
1408
|
-
* Space complexity: O(
|
|
1410
|
+
* Space complexity: O(n)
|
|
1409
1411
|
*/
|
|
1410
1412
|
/**
|
|
1411
|
-
*
|
|
1412
|
-
*
|
|
1413
|
-
*
|
|
1414
|
-
*
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1413
|
+
* Time complexity: O(n)
|
|
1414
|
+
* Space complexity: O(n)
|
|
1415
|
+
*
|
|
1416
|
+
* The `clone` function creates a new tree object and copies all the nodes from the original tree to
|
|
1417
|
+
* the new tree.
|
|
1418
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
1419
|
+
*/
|
|
1420
|
+
clone() {
|
|
1421
|
+
const cloned = this.createTree();
|
|
1422
|
+
this.bfs(node => cloned.add([node.key, node.value]));
|
|
1423
|
+
return cloned;
|
|
1420
1424
|
}
|
|
1421
1425
|
/**
|
|
1422
|
-
*
|
|
1423
|
-
*
|
|
1424
|
-
* @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
|
|
1425
|
-
* `tree`.
|
|
1426
|
-
* @returns The `filter` method is returning a new tree object that contains only the entries that
|
|
1427
|
-
* satisfy the given predicate function.
|
|
1426
|
+
* Time Complexity: O(n)
|
|
1427
|
+
* Space Complexity: O(n)
|
|
1428
1428
|
*/
|
|
1429
|
-
|
|
1429
|
+
/**
|
|
1430
|
+
* Time Complexity: O(n)
|
|
1431
|
+
* Space Complexity: O(n)
|
|
1432
|
+
*
|
|
1433
|
+
* The `filter` function creates a new tree by iterating over the elements of the current tree and
|
|
1434
|
+
* adding only the elements that satisfy the given predicate function.
|
|
1435
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
1436
|
+
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
1437
|
+
* included in the filtered tree or not.
|
|
1438
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1439
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
1440
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
1441
|
+
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that
|
|
1442
|
+
* pass the given predicate function.
|
|
1443
|
+
*/
|
|
1444
|
+
filter(predicate, thisArg) {
|
|
1430
1445
|
const newTree = this.createTree();
|
|
1446
|
+
let index = 0;
|
|
1431
1447
|
for (const [key, value] of this) {
|
|
1432
|
-
if (predicate(
|
|
1448
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
1433
1449
|
newTree.add([key, value]);
|
|
1434
1450
|
}
|
|
1435
1451
|
}
|
|
1436
1452
|
return newTree;
|
|
1437
1453
|
}
|
|
1438
1454
|
/**
|
|
1439
|
-
*
|
|
1440
|
-
*
|
|
1441
|
-
|
|
1455
|
+
* Time Complexity: O(n)
|
|
1456
|
+
* Space Complexity: O(n)
|
|
1457
|
+
*/
|
|
1458
|
+
/**
|
|
1459
|
+
* Time Complexity: O(n)
|
|
1460
|
+
* Space Complexity: O(n)
|
|
1461
|
+
*
|
|
1462
|
+
* The `map` function creates a new tree by applying a callback function to each key-value pair in
|
|
1463
|
+
* the original tree.
|
|
1464
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
1465
|
+
* in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
|
|
1466
|
+
* the index of the current pair, and a reference to the tree itself. The callback function should
|
|
1467
|
+
* return a new
|
|
1468
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1469
|
+
* specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
|
|
1470
|
+
* will be used as the `this` value when the callback function is called. If you don't pass a value
|
|
1442
1471
|
* @returns The `map` method is returning a new tree object.
|
|
1443
1472
|
*/
|
|
1444
|
-
map(callback) {
|
|
1473
|
+
map(callback, thisArg) {
|
|
1445
1474
|
const newTree = this.createTree();
|
|
1475
|
+
let index = 0;
|
|
1446
1476
|
for (const [key, value] of this) {
|
|
1447
|
-
newTree.add([key, callback(
|
|
1477
|
+
newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
|
|
1448
1478
|
}
|
|
1449
1479
|
return newTree;
|
|
1450
1480
|
}
|
|
1451
|
-
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1452
|
-
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
1453
|
-
// const newTree = this.createTree();
|
|
1454
|
-
// for (const [key, value] of this) {
|
|
1455
|
-
// newTree.add(key, callback([key, value], this));
|
|
1456
|
-
// }
|
|
1457
|
-
// return newTree;
|
|
1458
|
-
// }
|
|
1459
|
-
|
|
1460
|
-
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
1461
|
-
* entry, accumulating a single value.
|
|
1462
|
-
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
1463
|
-
* entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
|
|
1464
|
-
* based on the logic defined in the callback function.
|
|
1465
|
-
* @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
1466
|
-
* is the value that will be passed as the first argument to the callback function when reducing the
|
|
1467
|
-
* elements of the tree.
|
|
1468
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
1469
|
-
* all the entries in the tree and applying the callback function to each entry.
|
|
1470
|
-
*/
|
|
1471
|
-
reduce(callback, initialValue) {
|
|
1472
|
-
let accumulator = initialValue;
|
|
1473
|
-
for (const [key, value] of this) {
|
|
1474
|
-
accumulator = callback(accumulator, [key, value], this);
|
|
1475
|
-
}
|
|
1476
|
-
return accumulator;
|
|
1477
|
-
}
|
|
1478
|
-
/**
|
|
1479
|
-
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1480
|
-
* either an iterative or recursive manner.
|
|
1481
|
-
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
1482
|
-
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
1483
|
-
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
1484
|
-
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1485
|
-
* binary tree nodes in a specific order.
|
|
1486
|
-
*/
|
|
1487
|
-
*[Symbol.iterator](node = this.root) {
|
|
1488
|
-
if (!node)
|
|
1489
|
-
return;
|
|
1490
|
-
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1491
|
-
const stack = [];
|
|
1492
|
-
let current = node;
|
|
1493
|
-
while (current || stack.length > 0) {
|
|
1494
|
-
while (current && !isNaN(current.key)) {
|
|
1495
|
-
stack.push(current);
|
|
1496
|
-
current = current.left;
|
|
1497
|
-
}
|
|
1498
|
-
current = stack.pop();
|
|
1499
|
-
if (current && !isNaN(current.key)) {
|
|
1500
|
-
yield [current.key, current.value];
|
|
1501
|
-
current = current.right;
|
|
1502
|
-
}
|
|
1503
|
-
}
|
|
1504
|
-
}
|
|
1505
|
-
else {
|
|
1506
|
-
if (node.left && !isNaN(node.key)) {
|
|
1507
|
-
yield* this[Symbol.iterator](node.left);
|
|
1508
|
-
}
|
|
1509
|
-
yield [node.key, node.value];
|
|
1510
|
-
if (node.right && !isNaN(node.key)) {
|
|
1511
|
-
yield* this[Symbol.iterator](node.right);
|
|
1512
|
-
}
|
|
1513
|
-
}
|
|
1514
|
-
}
|
|
1481
|
+
// // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1482
|
+
// // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
1483
|
+
// // const newTree = this.createTree();
|
|
1484
|
+
// // for (const [key, value] of this) {
|
|
1485
|
+
// // newTree.add(key, callback([key, value], this));
|
|
1486
|
+
// // }
|
|
1487
|
+
// // return newTree;
|
|
1488
|
+
// // }
|
|
1489
|
+
//
|
|
1515
1490
|
/**
|
|
1516
1491
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1517
1492
|
* @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
|
|
@@ -1541,6 +1516,34 @@ export class BinaryTree {
|
|
|
1541
1516
|
};
|
|
1542
1517
|
display(beginRoot);
|
|
1543
1518
|
}
|
|
1519
|
+
*_getIterator(node = this.root) {
|
|
1520
|
+
if (!node)
|
|
1521
|
+
return;
|
|
1522
|
+
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1523
|
+
const stack = [];
|
|
1524
|
+
let current = node;
|
|
1525
|
+
while (current || stack.length > 0) {
|
|
1526
|
+
while (current && !isNaN(current.key)) {
|
|
1527
|
+
stack.push(current);
|
|
1528
|
+
current = current.left;
|
|
1529
|
+
}
|
|
1530
|
+
current = stack.pop();
|
|
1531
|
+
if (current && !isNaN(current.key)) {
|
|
1532
|
+
yield [current.key, current.value];
|
|
1533
|
+
current = current.right;
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
else {
|
|
1538
|
+
if (node.left && !isNaN(node.key)) {
|
|
1539
|
+
yield* this[Symbol.iterator](node.left);
|
|
1540
|
+
}
|
|
1541
|
+
yield [node.key, node.value];
|
|
1542
|
+
if (node.right && !isNaN(node.key)) {
|
|
1543
|
+
yield* this[Symbol.iterator](node.right);
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1544
1547
|
_displayAux(node, options) {
|
|
1545
1548
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1546
1549
|
const emptyDisplayLayout = [['─'], 1, 0, 0];
|
|
@@ -137,6 +137,18 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
137
137
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
138
138
|
*/
|
|
139
139
|
clear(): void;
|
|
140
|
+
/**
|
|
141
|
+
* Time complexity: O(n)
|
|
142
|
+
* Space complexity: O(n)
|
|
143
|
+
*/
|
|
144
|
+
/**
|
|
145
|
+
* Time complexity: O(n)
|
|
146
|
+
* Space complexity: O(n)
|
|
147
|
+
*
|
|
148
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
149
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
150
|
+
*/
|
|
151
|
+
clone(): TREE;
|
|
140
152
|
/**
|
|
141
153
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
142
154
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -279,6 +279,22 @@ export class TreeMultimap extends AVLTree {
|
|
|
279
279
|
super.clear();
|
|
280
280
|
this._count = 0;
|
|
281
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Time complexity: O(n)
|
|
284
|
+
* Space complexity: O(n)
|
|
285
|
+
*/
|
|
286
|
+
/**
|
|
287
|
+
* Time complexity: O(n)
|
|
288
|
+
* Space complexity: O(n)
|
|
289
|
+
*
|
|
290
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
291
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
292
|
+
*/
|
|
293
|
+
clone() {
|
|
294
|
+
const cloned = this.createTree();
|
|
295
|
+
this.bfs(node => cloned.add([node.key, node.value], node.count));
|
|
296
|
+
return cloned;
|
|
297
|
+
}
|
|
282
298
|
/**
|
|
283
299
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
284
300
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { DijkstraResult, VertexKey } from '../../types';
|
|
2
|
+
import { PairCallback } from "../../types";
|
|
2
3
|
import { IGraph } from '../../interfaces';
|
|
4
|
+
import { IterablePairBase } from "../base";
|
|
3
5
|
export declare abstract class AbstractVertex<V = any> {
|
|
4
6
|
key: VertexKey;
|
|
5
7
|
value: V | undefined;
|
|
@@ -28,7 +30,8 @@ export declare abstract class AbstractEdge<E = any> {
|
|
|
28
30
|
protected _hashCode: string;
|
|
29
31
|
get hashCode(): string;
|
|
30
32
|
}
|
|
31
|
-
export declare abstract class AbstractGraph<V = any, E = any, VO extends AbstractVertex<V> = AbstractVertex<V>, EO extends AbstractEdge<E> = AbstractEdge<E>> implements IGraph<V, E, VO, EO> {
|
|
33
|
+
export declare abstract class AbstractGraph<V = any, E = any, VO extends AbstractVertex<V> = AbstractVertex<V>, EO extends AbstractEdge<E> = AbstractEdge<E>> extends IterablePairBase<VertexKey, V | undefined> implements IGraph<V, E, VO, EO> {
|
|
34
|
+
constructor();
|
|
32
35
|
protected _vertices: Map<VertexKey, VO>;
|
|
33
36
|
get vertices(): Map<VertexKey, VO>;
|
|
34
37
|
/**
|
|
@@ -443,11 +446,46 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
443
446
|
* @returns the bridges found using the Tarjan algorithm.
|
|
444
447
|
*/
|
|
445
448
|
getBridges(): EO[];
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
449
|
+
/**
|
|
450
|
+
* Time Complexity: O(n)
|
|
451
|
+
* Space Complexity: O(n)
|
|
452
|
+
*/
|
|
453
|
+
/**
|
|
454
|
+
* Time Complexity: O(n)
|
|
455
|
+
* Space Complexity: O(n)
|
|
456
|
+
*
|
|
457
|
+
* The `filter` function iterates over key-value pairs in a data structure and returns an array of
|
|
458
|
+
* pairs that satisfy a given predicate.
|
|
459
|
+
* @param predicate - The `predicate` parameter is a callback function that takes four arguments:
|
|
460
|
+
* `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
|
|
461
|
+
* in the filtered array. The callback function should return `true` if the element should be
|
|
462
|
+
* included, and `
|
|
463
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
464
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
465
|
+
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
466
|
+
* @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
|
|
467
|
+
* that satisfy the given predicate function.
|
|
468
|
+
*/
|
|
469
|
+
filter(predicate: PairCallback<VertexKey, V | undefined, boolean>, thisArg?: any): [VertexKey, V | undefined][];
|
|
470
|
+
/**
|
|
471
|
+
* Time Complexity: O(n)
|
|
472
|
+
* Space Complexity: O(n)
|
|
473
|
+
*/
|
|
474
|
+
/**
|
|
475
|
+
* Time Complexity: O(n)
|
|
476
|
+
* Space Complexity: O(n)
|
|
477
|
+
*
|
|
478
|
+
* The `map` function iterates over the elements of a collection and applies a callback function to
|
|
479
|
+
* each element, returning an array of the results.
|
|
480
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
481
|
+
* map. It takes four arguments:
|
|
482
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
483
|
+
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
484
|
+
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
485
|
+
* @returns The `map` function is returning an array of type `T[]`.
|
|
486
|
+
*/
|
|
487
|
+
map<T>(callback: PairCallback<VertexKey, V | undefined, T>, thisArg?: any): T[];
|
|
488
|
+
protected _getIterator(): IterableIterator<[VertexKey, V | undefined]>;
|
|
451
489
|
protected abstract _addEdgeOnly(edge: EO): boolean;
|
|
452
490
|
protected _addVertexOnly(newVertex: VO): boolean;
|
|
453
491
|
protected _getVertex(vertexOrKey: VertexKey | VO): VO | undefined;
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { uuidV4 } from '../../utils';
|
|
9
9
|
import { PriorityQueue } from '../priority-queue';
|
|
10
10
|
import { Queue } from '../queue';
|
|
11
|
+
import { IterablePairBase } from "../base";
|
|
11
12
|
export class AbstractVertex {
|
|
12
13
|
key;
|
|
13
14
|
value;
|
|
@@ -45,7 +46,10 @@ export class AbstractEdge {
|
|
|
45
46
|
return this._hashCode;
|
|
46
47
|
}
|
|
47
48
|
}
|
|
48
|
-
export class AbstractGraph {
|
|
49
|
+
export class AbstractGraph extends IterablePairBase {
|
|
50
|
+
constructor() {
|
|
51
|
+
super();
|
|
52
|
+
}
|
|
49
53
|
_vertices = new Map();
|
|
50
54
|
get vertices() {
|
|
51
55
|
return this._vertices;
|
|
@@ -1021,46 +1025,67 @@ export class AbstractGraph {
|
|
|
1021
1025
|
getBridges() {
|
|
1022
1026
|
return this.tarjan(false, true, false, false).bridges;
|
|
1023
1027
|
}
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1028
|
+
/**
|
|
1029
|
+
* Time Complexity: O(n)
|
|
1030
|
+
* Space Complexity: O(n)
|
|
1031
|
+
*/
|
|
1032
|
+
/**
|
|
1033
|
+
* Time Complexity: O(n)
|
|
1034
|
+
* Space Complexity: O(n)
|
|
1035
|
+
*
|
|
1036
|
+
* The `filter` function iterates over key-value pairs in a data structure and returns an array of
|
|
1037
|
+
* pairs that satisfy a given predicate.
|
|
1038
|
+
* @param predicate - The `predicate` parameter is a callback function that takes four arguments:
|
|
1039
|
+
* `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
|
|
1040
|
+
* in the filtered array. The callback function should return `true` if the element should be
|
|
1041
|
+
* included, and `
|
|
1042
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1043
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
1044
|
+
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
1045
|
+
* @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
|
|
1046
|
+
* that satisfy the given predicate function.
|
|
1047
|
+
*/
|
|
1048
|
+
filter(predicate, thisArg) {
|
|
1037
1049
|
const filtered = [];
|
|
1038
1050
|
let index = 0;
|
|
1039
|
-
for (const
|
|
1040
|
-
if (predicate(
|
|
1041
|
-
filtered.push(
|
|
1051
|
+
for (const [key, value] of this) {
|
|
1052
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
1053
|
+
filtered.push([key, value]);
|
|
1042
1054
|
}
|
|
1043
1055
|
index++;
|
|
1044
1056
|
}
|
|
1045
1057
|
return filtered;
|
|
1046
1058
|
}
|
|
1047
|
-
|
|
1059
|
+
/**
|
|
1060
|
+
* Time Complexity: O(n)
|
|
1061
|
+
* Space Complexity: O(n)
|
|
1062
|
+
*/
|
|
1063
|
+
/**
|
|
1064
|
+
* Time Complexity: O(n)
|
|
1065
|
+
* Space Complexity: O(n)
|
|
1066
|
+
*
|
|
1067
|
+
* The `map` function iterates over the elements of a collection and applies a callback function to
|
|
1068
|
+
* each element, returning an array of the results.
|
|
1069
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
1070
|
+
* map. It takes four arguments:
|
|
1071
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1072
|
+
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
1073
|
+
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
1074
|
+
* @returns The `map` function is returning an array of type `T[]`.
|
|
1075
|
+
*/
|
|
1076
|
+
map(callback, thisArg) {
|
|
1048
1077
|
const mapped = [];
|
|
1049
1078
|
let index = 0;
|
|
1050
|
-
for (const
|
|
1051
|
-
mapped.push(callback(
|
|
1079
|
+
for (const [key, value] of this) {
|
|
1080
|
+
mapped.push(callback.call(thisArg, value, key, index, this));
|
|
1052
1081
|
index++;
|
|
1053
1082
|
}
|
|
1054
1083
|
return mapped;
|
|
1055
1084
|
}
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
for (const entry of this) {
|
|
1060
|
-
accumulator = callback(accumulator, entry, index, this._vertices);
|
|
1061
|
-
index++;
|
|
1085
|
+
*_getIterator() {
|
|
1086
|
+
for (const vertex of this._vertices.values()) {
|
|
1087
|
+
yield [vertex.key, vertex.value];
|
|
1062
1088
|
}
|
|
1063
|
-
return accumulator;
|
|
1064
1089
|
}
|
|
1065
1090
|
_addVertexOnly(newVertex) {
|
|
1066
1091
|
if (this.hasVertex(newVertex)) {
|