avl-tree-typed 1.48.1 → 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/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +36 -69
- package/dist/data-structures/binary-tree/binary-tree.js +78 -129
- package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/data-structures/graph/abstract-graph.js +50 -27
- package/dist/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/data-structures/hash/hash-map.js +69 -173
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/data-structures/queue/deque.d.ts +29 -51
- package/dist/data-structures/queue/deque.js +36 -71
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- 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 +82 -138
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +76 -185
- 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/index.ts +1 -0
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey, BTNodeEntry, BTNodeExemplar, BTNodeKeyOrNode } from '../../types';
|
|
9
|
-
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout } from '../../types';
|
|
9
|
+
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout, PairCallback } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
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.
|
|
@@ -41,7 +42,7 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
41
42
|
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
42
43
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
43
44
|
*/
|
|
44
|
-
export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> implements IBinaryTree<V, N, TREE> {
|
|
45
|
+
export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> extends IterablePairBase<BTNKey, V | undefined> implements IBinaryTree<V, N, TREE> {
|
|
45
46
|
iterationType: IterationType;
|
|
46
47
|
/**
|
|
47
48
|
* The constructor function initializes a binary tree object with optional elements and options.
|
|
@@ -460,31 +461,6 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
460
461
|
* by the return type of the `callback` function.
|
|
461
462
|
*/
|
|
462
463
|
morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNodeKeyOrNode<N>): ReturnType<C>[];
|
|
463
|
-
/**
|
|
464
|
-
* Time complexity: O(n)
|
|
465
|
-
* Space complexity: O(n)
|
|
466
|
-
*/
|
|
467
|
-
/**
|
|
468
|
-
* Time complexity: O(n)
|
|
469
|
-
* Space complexity: O(n)
|
|
470
|
-
*
|
|
471
|
-
* The function "keys" returns an array of keys from a given object.
|
|
472
|
-
* @returns an array of BTNKey objects.
|
|
473
|
-
*/
|
|
474
|
-
keys(): BTNKey[];
|
|
475
|
-
/**
|
|
476
|
-
* Time complexity: O(n)
|
|
477
|
-
* Space complexity: O(n)
|
|
478
|
-
*/
|
|
479
|
-
/**
|
|
480
|
-
* Time complexity: O(n)
|
|
481
|
-
* Space complexity: O(n)
|
|
482
|
-
*
|
|
483
|
-
* The function "values" returns an array of values from a map-like object.
|
|
484
|
-
* @returns The `values()` method is returning an array of values (`V`) from the entries in the
|
|
485
|
-
* object.
|
|
486
|
-
*/
|
|
487
|
-
values(): (V | undefined)[];
|
|
488
464
|
/**
|
|
489
465
|
* Time complexity: O(n)
|
|
490
466
|
* Space complexity: O(n)
|
|
@@ -499,55 +475,45 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
499
475
|
*/
|
|
500
476
|
clone(): TREE;
|
|
501
477
|
/**
|
|
502
|
-
* Time
|
|
503
|
-
* Space
|
|
478
|
+
* Time Complexity: O(n)
|
|
479
|
+
* Space Complexity: O(n)
|
|
504
480
|
*/
|
|
505
481
|
/**
|
|
506
|
-
*
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
* tree
|
|
510
|
-
|
|
511
|
-
|
|
482
|
+
* Time Complexity: O(n)
|
|
483
|
+
* Space Complexity: O(n)
|
|
484
|
+
*
|
|
485
|
+
* The `filter` function creates a new tree by iterating over the elements of the current tree and
|
|
486
|
+
* adding only the elements that satisfy the given predicate function.
|
|
487
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
488
|
+
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
489
|
+
* included in the filtered tree or not.
|
|
490
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
491
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
492
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
493
|
+
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that
|
|
494
|
+
* pass the given predicate function.
|
|
495
|
+
*/
|
|
496
|
+
filter(predicate: PairCallback<BTNKey, V | undefined, boolean>, thisArg?: any): TREE;
|
|
512
497
|
/**
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
* @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
|
|
516
|
-
* `tree`.
|
|
517
|
-
* @returns The `filter` method is returning a new tree object that contains only the entries that
|
|
518
|
-
* satisfy the given predicate function.
|
|
498
|
+
* Time Complexity: O(n)
|
|
499
|
+
* Space Complexity: O(n)
|
|
519
500
|
*/
|
|
520
|
-
filter(predicate: (entry: [BTNKey, V | undefined], tree: this) => boolean): TREE;
|
|
521
501
|
/**
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
*
|
|
502
|
+
* Time Complexity: O(n)
|
|
503
|
+
* Space Complexity: O(n)
|
|
504
|
+
*
|
|
505
|
+
* The `map` function creates a new tree by applying a callback function to each key-value pair in
|
|
506
|
+
* the original tree.
|
|
507
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
508
|
+
* in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
|
|
509
|
+
* the index of the current pair, and a reference to the tree itself. The callback function should
|
|
510
|
+
* return a new
|
|
511
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
512
|
+
* specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
|
|
513
|
+
* will be used as the `this` value when the callback function is called. If you don't pass a value
|
|
525
514
|
* @returns The `map` method is returning a new tree object.
|
|
526
515
|
*/
|
|
527
|
-
map(callback:
|
|
528
|
-
/**
|
|
529
|
-
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
530
|
-
* entry, accumulating a single value.
|
|
531
|
-
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
532
|
-
* entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
|
|
533
|
-
* based on the logic defined in the callback function.
|
|
534
|
-
* @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
535
|
-
* is the value that will be passed as the first argument to the callback function when reducing the
|
|
536
|
-
* elements of the tree.
|
|
537
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
538
|
-
* all the entries in the tree and applying the callback function to each entry.
|
|
539
|
-
*/
|
|
540
|
-
reduce<T>(callback: (accumulator: T, entry: [BTNKey, V | undefined], tree: this) => T, initialValue: T): T;
|
|
541
|
-
/**
|
|
542
|
-
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
543
|
-
* either an iterative or recursive manner.
|
|
544
|
-
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
545
|
-
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
546
|
-
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
547
|
-
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
548
|
-
* binary tree nodes in a specific order.
|
|
549
|
-
*/
|
|
550
|
-
[Symbol.iterator](node?: N | null | undefined): Generator<[BTNKey, V | undefined], void, undefined>;
|
|
516
|
+
map(callback: PairCallback<BTNKey, V | undefined, V>, thisArg?: any): TREE;
|
|
551
517
|
/**
|
|
552
518
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
553
519
|
* @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
|
|
@@ -556,6 +522,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
556
522
|
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
557
523
|
*/
|
|
558
524
|
print(beginRoot?: BTNodeKeyOrNode<N>, options?: BinaryTreePrintOptions): void;
|
|
525
|
+
protected _getIterator(node?: N | null | undefined): IterableIterator<[BTNKey, V | undefined]>;
|
|
559
526
|
protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
|
|
560
527
|
protected _defaultOneParamCallback: (node: N) => number;
|
|
561
528
|
/**
|
|
@@ -11,6 +11,7 @@ exports.BinaryTree = exports.BinaryTreeNode = void 0;
|
|
|
11
11
|
const types_1 = require("../../types");
|
|
12
12
|
const utils_1 = require("../../utils");
|
|
13
13
|
const queue_1 = require("../queue");
|
|
14
|
+
const base_1 = require("../base");
|
|
14
15
|
/**
|
|
15
16
|
* Represents a node in a binary tree.
|
|
16
17
|
* @template V - The type of data stored in the node.
|
|
@@ -69,7 +70,7 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
69
70
|
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
70
71
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
71
72
|
*/
|
|
72
|
-
class BinaryTree {
|
|
73
|
+
class BinaryTree extends base_1.IterablePairBase {
|
|
73
74
|
/**
|
|
74
75
|
* The constructor function initializes a binary tree object with optional elements and options.
|
|
75
76
|
* @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
|
|
@@ -80,6 +81,7 @@ class BinaryTree {
|
|
|
80
81
|
* required.
|
|
81
82
|
*/
|
|
82
83
|
constructor(elements, options) {
|
|
84
|
+
super();
|
|
83
85
|
this.iterationType = types_1.IterationType.ITERATIVE;
|
|
84
86
|
this._defaultOneParamCallback = (node) => node.key;
|
|
85
87
|
if (options) {
|
|
@@ -1404,43 +1406,6 @@ class BinaryTree {
|
|
|
1404
1406
|
}
|
|
1405
1407
|
return ans;
|
|
1406
1408
|
}
|
|
1407
|
-
/**
|
|
1408
|
-
* Time complexity: O(n)
|
|
1409
|
-
* Space complexity: O(n)
|
|
1410
|
-
*/
|
|
1411
|
-
/**
|
|
1412
|
-
* Time complexity: O(n)
|
|
1413
|
-
* Space complexity: O(n)
|
|
1414
|
-
*
|
|
1415
|
-
* The function "keys" returns an array of keys from a given object.
|
|
1416
|
-
* @returns an array of BTNKey objects.
|
|
1417
|
-
*/
|
|
1418
|
-
keys() {
|
|
1419
|
-
const keys = [];
|
|
1420
|
-
for (const entry of this) {
|
|
1421
|
-
keys.push(entry[0]);
|
|
1422
|
-
}
|
|
1423
|
-
return keys;
|
|
1424
|
-
}
|
|
1425
|
-
/**
|
|
1426
|
-
* Time complexity: O(n)
|
|
1427
|
-
* Space complexity: O(n)
|
|
1428
|
-
*/
|
|
1429
|
-
/**
|
|
1430
|
-
* Time complexity: O(n)
|
|
1431
|
-
* Space complexity: O(n)
|
|
1432
|
-
*
|
|
1433
|
-
* The function "values" returns an array of values from a map-like object.
|
|
1434
|
-
* @returns The `values()` method is returning an array of values (`V`) from the entries in the
|
|
1435
|
-
* object.
|
|
1436
|
-
*/
|
|
1437
|
-
values() {
|
|
1438
|
-
const values = [];
|
|
1439
|
-
for (const entry of this) {
|
|
1440
|
-
values.push(entry[1]);
|
|
1441
|
-
}
|
|
1442
|
-
return values;
|
|
1443
|
-
}
|
|
1444
1409
|
/**
|
|
1445
1410
|
* Time complexity: O(n)
|
|
1446
1411
|
* Space complexity: O(n)
|
|
@@ -1459,114 +1424,70 @@ class BinaryTree {
|
|
|
1459
1424
|
return cloned;
|
|
1460
1425
|
}
|
|
1461
1426
|
/**
|
|
1462
|
-
* Time
|
|
1463
|
-
* Space
|
|
1464
|
-
*/
|
|
1465
|
-
/**
|
|
1466
|
-
* The `forEach` function iterates over each entry in a tree and calls a callback function with the
|
|
1467
|
-
* entry and the tree as arguments.
|
|
1468
|
-
* @param callback - The callback parameter is a function that will be called for each entry in the
|
|
1469
|
-
* tree. It takes two parameters: entry and tree.
|
|
1427
|
+
* Time Complexity: O(n)
|
|
1428
|
+
* Space Complexity: O(n)
|
|
1470
1429
|
*/
|
|
1471
|
-
forEach(callback) {
|
|
1472
|
-
for (const entry of this) {
|
|
1473
|
-
callback(entry, this);
|
|
1474
|
-
}
|
|
1475
|
-
}
|
|
1476
1430
|
/**
|
|
1477
|
-
*
|
|
1478
|
-
*
|
|
1479
|
-
*
|
|
1480
|
-
* `tree
|
|
1481
|
-
*
|
|
1482
|
-
*
|
|
1483
|
-
|
|
1484
|
-
|
|
1431
|
+
* Time Complexity: O(n)
|
|
1432
|
+
* Space Complexity: O(n)
|
|
1433
|
+
*
|
|
1434
|
+
* The `filter` function creates a new tree by iterating over the elements of the current tree and
|
|
1435
|
+
* adding only the elements that satisfy the given predicate function.
|
|
1436
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
1437
|
+
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
1438
|
+
* included in the filtered tree or not.
|
|
1439
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1440
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
1441
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
1442
|
+
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that
|
|
1443
|
+
* pass the given predicate function.
|
|
1444
|
+
*/
|
|
1445
|
+
filter(predicate, thisArg) {
|
|
1485
1446
|
const newTree = this.createTree();
|
|
1447
|
+
let index = 0;
|
|
1486
1448
|
for (const [key, value] of this) {
|
|
1487
|
-
if (predicate(
|
|
1449
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
1488
1450
|
newTree.add([key, value]);
|
|
1489
1451
|
}
|
|
1490
1452
|
}
|
|
1491
1453
|
return newTree;
|
|
1492
1454
|
}
|
|
1493
1455
|
/**
|
|
1494
|
-
*
|
|
1495
|
-
*
|
|
1496
|
-
|
|
1456
|
+
* Time Complexity: O(n)
|
|
1457
|
+
* Space Complexity: O(n)
|
|
1458
|
+
*/
|
|
1459
|
+
/**
|
|
1460
|
+
* Time Complexity: O(n)
|
|
1461
|
+
* Space Complexity: O(n)
|
|
1462
|
+
*
|
|
1463
|
+
* The `map` function creates a new tree by applying a callback function to each key-value pair in
|
|
1464
|
+
* the original tree.
|
|
1465
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
1466
|
+
* in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
|
|
1467
|
+
* the index of the current pair, and a reference to the tree itself. The callback function should
|
|
1468
|
+
* return a new
|
|
1469
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1470
|
+
* specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
|
|
1471
|
+
* will be used as the `this` value when the callback function is called. If you don't pass a value
|
|
1497
1472
|
* @returns The `map` method is returning a new tree object.
|
|
1498
1473
|
*/
|
|
1499
|
-
map(callback) {
|
|
1474
|
+
map(callback, thisArg) {
|
|
1500
1475
|
const newTree = this.createTree();
|
|
1476
|
+
let index = 0;
|
|
1501
1477
|
for (const [key, value] of this) {
|
|
1502
|
-
newTree.add([key, callback(
|
|
1478
|
+
newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
|
|
1503
1479
|
}
|
|
1504
1480
|
return newTree;
|
|
1505
1481
|
}
|
|
1506
|
-
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1507
|
-
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
1508
|
-
// const newTree = this.createTree();
|
|
1509
|
-
// for (const [key, value] of this) {
|
|
1510
|
-
// newTree.add(key, callback([key, value], this));
|
|
1511
|
-
// }
|
|
1512
|
-
// return newTree;
|
|
1513
|
-
// }
|
|
1514
|
-
|
|
1515
|
-
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
1516
|
-
* entry, accumulating a single value.
|
|
1517
|
-
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
1518
|
-
* entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
|
|
1519
|
-
* based on the logic defined in the callback function.
|
|
1520
|
-
* @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
1521
|
-
* is the value that will be passed as the first argument to the callback function when reducing the
|
|
1522
|
-
* elements of the tree.
|
|
1523
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
1524
|
-
* all the entries in the tree and applying the callback function to each entry.
|
|
1525
|
-
*/
|
|
1526
|
-
reduce(callback, initialValue) {
|
|
1527
|
-
let accumulator = initialValue;
|
|
1528
|
-
for (const [key, value] of this) {
|
|
1529
|
-
accumulator = callback(accumulator, [key, value], this);
|
|
1530
|
-
}
|
|
1531
|
-
return accumulator;
|
|
1532
|
-
}
|
|
1533
|
-
/**
|
|
1534
|
-
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1535
|
-
* either an iterative or recursive manner.
|
|
1536
|
-
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
1537
|
-
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
1538
|
-
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
1539
|
-
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1540
|
-
* binary tree nodes in a specific order.
|
|
1541
|
-
*/
|
|
1542
|
-
*[Symbol.iterator](node = this.root) {
|
|
1543
|
-
if (!node)
|
|
1544
|
-
return;
|
|
1545
|
-
if (this.iterationType === types_1.IterationType.ITERATIVE) {
|
|
1546
|
-
const stack = [];
|
|
1547
|
-
let current = node;
|
|
1548
|
-
while (current || stack.length > 0) {
|
|
1549
|
-
while (current && !isNaN(current.key)) {
|
|
1550
|
-
stack.push(current);
|
|
1551
|
-
current = current.left;
|
|
1552
|
-
}
|
|
1553
|
-
current = stack.pop();
|
|
1554
|
-
if (current && !isNaN(current.key)) {
|
|
1555
|
-
yield [current.key, current.value];
|
|
1556
|
-
current = current.right;
|
|
1557
|
-
}
|
|
1558
|
-
}
|
|
1559
|
-
}
|
|
1560
|
-
else {
|
|
1561
|
-
if (node.left && !isNaN(node.key)) {
|
|
1562
|
-
yield* this[Symbol.iterator](node.left);
|
|
1563
|
-
}
|
|
1564
|
-
yield [node.key, node.value];
|
|
1565
|
-
if (node.right && !isNaN(node.key)) {
|
|
1566
|
-
yield* this[Symbol.iterator](node.right);
|
|
1567
|
-
}
|
|
1568
|
-
}
|
|
1569
|
-
}
|
|
1482
|
+
// // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1483
|
+
// // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
1484
|
+
// // const newTree = this.createTree();
|
|
1485
|
+
// // for (const [key, value] of this) {
|
|
1486
|
+
// // newTree.add(key, callback([key, value], this));
|
|
1487
|
+
// // }
|
|
1488
|
+
// // return newTree;
|
|
1489
|
+
// // }
|
|
1490
|
+
//
|
|
1570
1491
|
/**
|
|
1571
1492
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1572
1493
|
* @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
|
|
@@ -1596,6 +1517,34 @@ class BinaryTree {
|
|
|
1596
1517
|
};
|
|
1597
1518
|
display(beginRoot);
|
|
1598
1519
|
}
|
|
1520
|
+
*_getIterator(node = this.root) {
|
|
1521
|
+
if (!node)
|
|
1522
|
+
return;
|
|
1523
|
+
if (this.iterationType === types_1.IterationType.ITERATIVE) {
|
|
1524
|
+
const stack = [];
|
|
1525
|
+
let current = node;
|
|
1526
|
+
while (current || stack.length > 0) {
|
|
1527
|
+
while (current && !isNaN(current.key)) {
|
|
1528
|
+
stack.push(current);
|
|
1529
|
+
current = current.left;
|
|
1530
|
+
}
|
|
1531
|
+
current = stack.pop();
|
|
1532
|
+
if (current && !isNaN(current.key)) {
|
|
1533
|
+
yield [current.key, current.value];
|
|
1534
|
+
current = current.right;
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
else {
|
|
1539
|
+
if (node.left && !isNaN(node.key)) {
|
|
1540
|
+
yield* this[Symbol.iterator](node.left);
|
|
1541
|
+
}
|
|
1542
|
+
yield [node.key, node.value];
|
|
1543
|
+
if (node.right && !isNaN(node.key)) {
|
|
1544
|
+
yield* this[Symbol.iterator](node.right);
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1599
1548
|
_displayAux(node, options) {
|
|
1600
1549
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1601
1550
|
const emptyDisplayLayout = [['─'], 1, 0, 0];
|
|
@@ -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;
|
|
@@ -11,6 +11,7 @@ exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
|
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
12
|
const priority_queue_1 = require("../priority-queue");
|
|
13
13
|
const queue_1 = require("../queue");
|
|
14
|
+
const base_1 = require("../base");
|
|
14
15
|
class AbstractVertex {
|
|
15
16
|
/**
|
|
16
17
|
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
@@ -45,8 +46,9 @@ class AbstractEdge {
|
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
48
|
exports.AbstractEdge = AbstractEdge;
|
|
48
|
-
class AbstractGraph {
|
|
49
|
+
class AbstractGraph extends base_1.IterablePairBase {
|
|
49
50
|
constructor() {
|
|
51
|
+
super();
|
|
50
52
|
this._vertices = new Map();
|
|
51
53
|
}
|
|
52
54
|
get vertices() {
|
|
@@ -1028,46 +1030,67 @@ class AbstractGraph {
|
|
|
1028
1030
|
getBridges() {
|
|
1029
1031
|
return this.tarjan(false, true, false, false).bridges;
|
|
1030
1032
|
}
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1033
|
+
/**
|
|
1034
|
+
* Time Complexity: O(n)
|
|
1035
|
+
* Space Complexity: O(n)
|
|
1036
|
+
*/
|
|
1037
|
+
/**
|
|
1038
|
+
* Time Complexity: O(n)
|
|
1039
|
+
* Space Complexity: O(n)
|
|
1040
|
+
*
|
|
1041
|
+
* The `filter` function iterates over key-value pairs in a data structure and returns an array of
|
|
1042
|
+
* pairs that satisfy a given predicate.
|
|
1043
|
+
* @param predicate - The `predicate` parameter is a callback function that takes four arguments:
|
|
1044
|
+
* `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
|
|
1045
|
+
* in the filtered array. The callback function should return `true` if the element should be
|
|
1046
|
+
* included, and `
|
|
1047
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1048
|
+
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
1049
|
+
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
1050
|
+
* @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
|
|
1051
|
+
* that satisfy the given predicate function.
|
|
1052
|
+
*/
|
|
1053
|
+
filter(predicate, thisArg) {
|
|
1044
1054
|
const filtered = [];
|
|
1045
1055
|
let index = 0;
|
|
1046
|
-
for (const
|
|
1047
|
-
if (predicate(
|
|
1048
|
-
filtered.push(
|
|
1056
|
+
for (const [key, value] of this) {
|
|
1057
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
1058
|
+
filtered.push([key, value]);
|
|
1049
1059
|
}
|
|
1050
1060
|
index++;
|
|
1051
1061
|
}
|
|
1052
1062
|
return filtered;
|
|
1053
1063
|
}
|
|
1054
|
-
|
|
1064
|
+
/**
|
|
1065
|
+
* Time Complexity: O(n)
|
|
1066
|
+
* Space Complexity: O(n)
|
|
1067
|
+
*/
|
|
1068
|
+
/**
|
|
1069
|
+
* Time Complexity: O(n)
|
|
1070
|
+
* Space Complexity: O(n)
|
|
1071
|
+
*
|
|
1072
|
+
* The `map` function iterates over the elements of a collection and applies a callback function to
|
|
1073
|
+
* each element, returning an array of the results.
|
|
1074
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
1075
|
+
* map. It takes four arguments:
|
|
1076
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1077
|
+
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
1078
|
+
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
1079
|
+
* @returns The `map` function is returning an array of type `T[]`.
|
|
1080
|
+
*/
|
|
1081
|
+
map(callback, thisArg) {
|
|
1055
1082
|
const mapped = [];
|
|
1056
1083
|
let index = 0;
|
|
1057
|
-
for (const
|
|
1058
|
-
mapped.push(callback(
|
|
1084
|
+
for (const [key, value] of this) {
|
|
1085
|
+
mapped.push(callback.call(thisArg, value, key, index, this));
|
|
1059
1086
|
index++;
|
|
1060
1087
|
}
|
|
1061
1088
|
return mapped;
|
|
1062
1089
|
}
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
for (const entry of this) {
|
|
1067
|
-
accumulator = callback(accumulator, entry, index, this._vertices);
|
|
1068
|
-
index++;
|
|
1090
|
+
*_getIterator() {
|
|
1091
|
+
for (const vertex of this._vertices.values()) {
|
|
1092
|
+
yield [vertex.key, vertex.value];
|
|
1069
1093
|
}
|
|
1070
|
-
return accumulator;
|
|
1071
1094
|
}
|
|
1072
1095
|
_addVertexOnly(newVertex) {
|
|
1073
1096
|
if (this.hasVertex(newVertex)) {
|