data-structure-typed 1.18.7 → 1.18.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/README.md +154 -366
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +29 -32
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +179 -136
- package/dist/data-structures/binary-tree/avl-tree.d.ts +4 -4
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/binary-tree.js +4 -4
- package/dist/data-structures/binary-tree/bst.d.ts +13 -25
- package/dist/data-structures/binary-tree/bst.js +20 -74
- package/dist/data-structures/binary-tree/rb-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +7 -14
- package/dist/data-structures/graph/abstract-graph.d.ts +11 -9
- package/dist/data-structures/graph/abstract-graph.js +39 -22
- package/dist/data-structures/graph/directed-graph.d.ts +17 -15
- package/dist/data-structures/graph/directed-graph.js +47 -42
- package/dist/data-structures/graph/undirected-graph.d.ts +7 -7
- package/dist/data-structures/graph/undirected-graph.js +48 -48
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +2 -3
- package/dist/data-structures/interfaces/abstract-graph.d.ts +2 -4
- package/dist/data-structures/interfaces/avl-tree.d.ts +1 -1
- package/dist/data-structures/interfaces/bst.d.ts +2 -2
- package/dist/data-structures/interfaces/rb-tree.d.ts +2 -2
- package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
- package/dist/data-structures/tree/tree.d.ts +1 -4
- package/dist/data-structures/tree/tree.js +1 -12
- package/dist/data-structures/types/abstract-binary-tree.d.ts +9 -5
- package/dist/data-structures/types/abstract-binary-tree.js +9 -5
- package/dist/data-structures/types/avl-tree.d.ts +1 -1
- package/dist/data-structures/types/binary-tree.d.ts +1 -1
- package/dist/data-structures/types/bst.d.ts +1 -1
- package/dist/data-structures/types/bst.js +1 -1
- package/dist/data-structures/types/directed-graph.js +1 -1
- package/dist/data-structures/types/helpers.d.ts +1 -8
- package/dist/data-structures/types/rb-tree.d.ts +2 -0
- package/dist/data-structures/types/rb-tree.js +1 -1
- package/dist/data-structures/types/tree-multiset.d.ts +4 -4
- package/dist/utils/types/utils.d.ts +18 -0
- package/dist/utils/utils.d.ts +6 -1
- package/dist/utils/utils.js +21 -1
- package/package.json +2 -2
- package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
- package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
- package/backup/recursive-type/src/assets/logo.png +0 -0
- package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
- package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
- package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
- package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
- package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
- package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
- package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
- package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
- package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
- package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
- package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
- package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
- package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
- package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
- package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
- package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
- package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
- package/backup/recursive-type/src/data-structures/index.ts +0 -15
- package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
- package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
- package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
- package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
- package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
- package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
- package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
- package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
- package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
- package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
- package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
- package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
- package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
- package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
- package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
- package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
- package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
- package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
- package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
- package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
- package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
- package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
- package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
- package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
- package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
- package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
- package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
- package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
- package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
- package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
- package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
- package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
- package/backup/recursive-type/src/index.ts +0 -1
- package/backup/recursive-type/src/utils/index.ts +0 -2
- package/backup/recursive-type/src/utils/types/index.ts +0 -1
- package/backup/recursive-type/src/utils/types/utils.ts +0 -6
- package/backup/recursive-type/src/utils/utils.ts +0 -78
- package/docs/.nojekyll +0 -1
- package/docs/assets/highlight.css +0 -92
- package/docs/assets/main.js +0 -58
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1367
- package/docs/classes/AVLTree.html +0 -2451
- package/docs/classes/AVLTreeNode.html +0 -499
- package/docs/classes/AaTree.html +0 -172
- package/docs/classes/AbstractBinaryTree.html +0 -2118
- package/docs/classes/AbstractBinaryTreeNode.html +0 -524
- package/docs/classes/AbstractEdge.html +0 -295
- package/docs/classes/AbstractGraph.html +0 -1043
- package/docs/classes/AbstractVertex.html +0 -258
- package/docs/classes/ArrayDeque.html +0 -439
- package/docs/classes/BST.html +0 -2297
- package/docs/classes/BSTNode.html +0 -503
- package/docs/classes/BTree.html +0 -172
- package/docs/classes/BinaryIndexedTree.html +0 -341
- package/docs/classes/BinaryTree.html +0 -2133
- package/docs/classes/BinaryTreeNode.html +0 -501
- package/docs/classes/Character.html +0 -220
- package/docs/classes/CoordinateMap.html +0 -483
- package/docs/classes/CoordinateSet.html +0 -444
- package/docs/classes/Deque.html +0 -975
- package/docs/classes/DirectedEdge.html +0 -366
- package/docs/classes/DirectedGraph.html +0 -1443
- package/docs/classes/DirectedVertex.html +0 -254
- package/docs/classes/DoublyLinkedList.html +0 -968
- package/docs/classes/DoublyLinkedListNode.html +0 -297
- package/docs/classes/HashTable.html +0 -172
- package/docs/classes/Heap.html +0 -423
- package/docs/classes/HeapItem.html +0 -255
- package/docs/classes/Matrix2D.html +0 -502
- package/docs/classes/MatrixNTI2D.html +0 -240
- package/docs/classes/MaxHeap.html +0 -436
- package/docs/classes/MaxPriorityQueue.html +0 -836
- package/docs/classes/MinHeap.html +0 -437
- package/docs/classes/MinPriorityQueue.html +0 -838
- package/docs/classes/Navigator.html +0 -313
- package/docs/classes/ObjectDeque.html +0 -455
- package/docs/classes/Pair.html +0 -172
- package/docs/classes/PriorityQueue.html +0 -760
- package/docs/classes/Queue.html +0 -392
- package/docs/classes/RBTree.html +0 -2388
- package/docs/classes/RBTreeNode.html +0 -516
- package/docs/classes/SegmentTree.html +0 -434
- package/docs/classes/SegmentTreeNode.html +0 -357
- package/docs/classes/SinglyLinkedList.html +0 -788
- package/docs/classes/SinglyLinkedListNode.html +0 -270
- package/docs/classes/SkipLinkedList.html +0 -172
- package/docs/classes/SplayTree.html +0 -172
- package/docs/classes/Stack.html +0 -368
- package/docs/classes/TreeMap.html +0 -172
- package/docs/classes/TreeMultiSet.html +0 -2297
- package/docs/classes/TreeMultiSetNode.html +0 -499
- package/docs/classes/TreeNode.html +0 -343
- package/docs/classes/TreeSet.html +0 -172
- package/docs/classes/Trie.html +0 -372
- package/docs/classes/TrieNode.html +0 -280
- package/docs/classes/TwoThreeTree.html +0 -172
- package/docs/classes/UndirectedEdge.html +0 -337
- package/docs/classes/UndirectedGraph.html +0 -1210
- package/docs/classes/UndirectedVertex.html +0 -254
- package/docs/classes/Vector2D.html +0 -805
- package/docs/enums/CP.html +0 -181
- package/docs/enums/FamilyPosition.html +0 -181
- package/docs/enums/LoopType.html +0 -182
- package/docs/enums/RBColor.html +0 -174
- package/docs/enums/TopologicalProperty.html +0 -181
- package/docs/index.html +0 -645
- package/docs/interfaces/IAVLTree.html +0 -1378
- package/docs/interfaces/IAVLTreeNode.html +0 -405
- package/docs/interfaces/IAbstractBinaryTree.html +0 -1124
- package/docs/interfaces/IAbstractBinaryTreeNode.html +0 -384
- package/docs/interfaces/IAbstractGraph.html +0 -433
- package/docs/interfaces/IBST.html +0 -1271
- package/docs/interfaces/IBSTNode.html +0 -408
- package/docs/interfaces/IDirectedGraph.html +0 -572
- package/docs/interfaces/IUNDirectedGraph.html +0 -465
- package/docs/modules.html +0 -262
- package/docs/types/AVLTreeOptions.html +0 -150
- package/docs/types/AbstractBinaryTreeNodeProperties.html +0 -152
- package/docs/types/AbstractBinaryTreeNodeProperty.html +0 -152
- package/docs/types/AbstractBinaryTreeOptions.html +0 -156
- package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -152
- package/docs/types/BSTComparator.html +0 -162
- package/docs/types/BSTOptions.html +0 -152
- package/docs/types/BinaryTreeDeletedResult.html +0 -159
- package/docs/types/BinaryTreeNodeId.html +0 -147
- package/docs/types/BinaryTreeNodePropertyName.html +0 -147
- package/docs/types/BinaryTreeOptions.html +0 -150
- package/docs/types/DFSOrderPattern.html +0 -147
- package/docs/types/DijkstraResult.html +0 -167
- package/docs/types/Direction.html +0 -147
- package/docs/types/EdgeId.html +0 -147
- package/docs/types/HeapOptions.html +0 -168
- package/docs/types/IdObject.html +0 -157
- package/docs/types/KeyValObject.html +0 -152
- package/docs/types/NavigatorParams.html +0 -181
- package/docs/types/NodeOrPropertyName.html +0 -147
- package/docs/types/PriorityQueueComparator.html +0 -167
- package/docs/types/PriorityQueueDFSOrderPattern.html +0 -147
- package/docs/types/PriorityQueueOptions.html +0 -161
- package/docs/types/RBTreeOptions.html +0 -150
- package/docs/types/RecursiveAVLTreeNode.html +0 -152
- package/docs/types/RecursiveBSTNode.html +0 -152
- package/docs/types/RecursiveBinaryTreeNode.html +0 -152
- package/docs/types/RecursiveTreeMultiSetNode.html +0 -152
- package/docs/types/SegmentTreeNodeVal.html +0 -147
- package/docs/types/TopologicalStatus.html +0 -147
- package/docs/types/TreeMultiSetOptions.html +0 -152
- package/docs/types/Turning.html +0 -147
- package/docs/types/VertexId.html +0 -147
- package/notes/bst.test.ts +0 -181
- package/notes/note.md +0 -34
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator,
|
|
9
|
-
import {
|
|
8
|
+
import type { BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTNodeNested } from '../types';
|
|
9
|
+
import { BSTOptions, CP } from '../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBST, IBSTNode } from '../interfaces';
|
|
12
|
-
export declare class BSTNode<T, FAMILY extends BSTNode<T, FAMILY> =
|
|
12
|
+
export declare class BSTNode<T = any, FAMILY extends BSTNode<T, FAMILY> = BSTNodeNested<T>> extends BinaryTreeNode<T, FAMILY> implements IBSTNode<T, FAMILY> {
|
|
13
13
|
/**
|
|
14
14
|
* The function creates a new binary search tree node with the specified id, value, and count.
|
|
15
15
|
* @param {BinaryTreeNodeId} id - The id parameter is the identifier for the binary tree node. It is used to uniquely
|
|
@@ -22,7 +22,7 @@ export declare class BSTNode<T, FAMILY extends BSTNode<T, FAMILY> = RecursiveBST
|
|
|
22
22
|
*/
|
|
23
23
|
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
24
24
|
}
|
|
25
|
-
export declare class BST<N extends BSTNode<N['val'], N> = BSTNode
|
|
25
|
+
export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
|
|
26
26
|
/**
|
|
27
27
|
* The constructor function accepts an optional options object and sets the comparator property if provided.
|
|
28
28
|
* @param [options] - An optional object that can contain the following properties:
|
|
@@ -51,7 +51,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode<number>> exten
|
|
|
51
51
|
* inserted once.
|
|
52
52
|
* @returns The method `add` returns a `N` object or `null`.
|
|
53
53
|
*/
|
|
54
|
-
add(id: BinaryTreeNodeId, val
|
|
54
|
+
add(id: BinaryTreeNodeId, val?: N['val'], count?: number): N | null;
|
|
55
55
|
/**
|
|
56
56
|
* The `get` function returns the first node in a binary search tree that matches the given property value or name.
|
|
57
57
|
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
@@ -71,17 +71,6 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode<number>> exten
|
|
|
71
71
|
* there are no nodes in
|
|
72
72
|
*/
|
|
73
73
|
lastKey(): BinaryTreeNodeId;
|
|
74
|
-
/**
|
|
75
|
-
* The `remove` function in this TypeScript code removes a node from a binary search tree and returns information about
|
|
76
|
-
* the deleted node and any nodes that need to be balanced.
|
|
77
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that needs to be removed
|
|
78
|
-
* from the binary search tree.
|
|
79
|
-
* @param {boolean} [ignoreCount] - A boolean flag indicating whether to ignore the count of the node being removed. If
|
|
80
|
-
* set to true, the count of the node will not be considered and the node will be removed regardless of its count. If
|
|
81
|
-
* set to false or not provided, the count of the node will be taken into account and the
|
|
82
|
-
* @returns an array of `BSTDeletedResult<N>` objects.
|
|
83
|
-
*/
|
|
84
|
-
remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
85
74
|
/**
|
|
86
75
|
* The function `getNodes` returns an array of binary search tree nodes that match a given property value, with the
|
|
87
76
|
* option to specify the property name and whether to return only one node.
|
|
@@ -97,16 +86,15 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode<number>> exten
|
|
|
97
86
|
*/
|
|
98
87
|
getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[];
|
|
99
88
|
/**
|
|
100
|
-
* The `lesserSum` function calculates the sum of
|
|
101
|
-
* a
|
|
102
|
-
* @param {BinaryTreeNodeId}
|
|
103
|
-
* calculate the lesser sum.
|
|
89
|
+
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a lesser value
|
|
90
|
+
* than a given node.
|
|
91
|
+
* @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
104
92
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
105
|
-
* specifies the property
|
|
106
|
-
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in
|
|
107
|
-
*
|
|
93
|
+
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
|
|
94
|
+
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in a binary
|
|
95
|
+
* tree that have a lesser value than the specified `beginNode` based on the specified `propertyName`.
|
|
108
96
|
*/
|
|
109
|
-
lesserSum(
|
|
97
|
+
lesserSum(beginNode: N | BinaryTreeNodeId | null, propertyName?: BinaryTreeNodePropertyName): number;
|
|
110
98
|
/**
|
|
111
99
|
* The function `allGreaterNodesAdd` updates the value of a specified property for all nodes in a binary search tree
|
|
112
100
|
* that have a greater value than a given node.
|
|
@@ -119,7 +107,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode<number>> exten
|
|
|
119
107
|
* defaults to 'id'.
|
|
120
108
|
* @returns a boolean value.
|
|
121
109
|
*/
|
|
122
|
-
allGreaterNodesAdd(node: N, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
|
|
110
|
+
allGreaterNodesAdd(node: N | BinaryTreeNodeId | null, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
|
|
123
111
|
/**
|
|
124
112
|
* The `balance` function takes a sorted array of nodes and builds a balanced binary search tree using either a
|
|
125
113
|
* recursive or iterative approach.
|
|
@@ -50,7 +50,7 @@ var BSTNode = /** @class */ (function (_super) {
|
|
|
50
50
|
* @returns The method is returning a new instance of the BSTNode class, casted as the FAMILY type.
|
|
51
51
|
*/
|
|
52
52
|
BSTNode.prototype.createNode = function (id, val, count) {
|
|
53
|
-
return new BSTNode(id,
|
|
53
|
+
return new BSTNode(id, val, count);
|
|
54
54
|
};
|
|
55
55
|
return BSTNode;
|
|
56
56
|
}(binary_tree_1.BinaryTreeNode));
|
|
@@ -83,7 +83,7 @@ var BST = /** @class */ (function (_super) {
|
|
|
83
83
|
* @returns a new instance of the BSTNode class, casted as type N.
|
|
84
84
|
*/
|
|
85
85
|
BST.prototype.createNode = function (id, val, count) {
|
|
86
|
-
return new BSTNode(id, val
|
|
86
|
+
return new BSTNode(id, val, count);
|
|
87
87
|
};
|
|
88
88
|
/**
|
|
89
89
|
* The `add` function inserts a new node into a binary search tree, updating the count and value of an existing node if
|
|
@@ -127,7 +127,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
127
127
|
if (cur.left === undefined) {
|
|
128
128
|
if (newNode) {
|
|
129
129
|
newNode.parent = cur;
|
|
130
|
-
newNode.familyPosition = types_1.FamilyPosition.LEFT;
|
|
131
130
|
}
|
|
132
131
|
//Add to the left of the current node
|
|
133
132
|
cur.left = newNode;
|
|
@@ -147,7 +146,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
147
146
|
if (cur.right === undefined) {
|
|
148
147
|
if (newNode) {
|
|
149
148
|
newNode.parent = cur;
|
|
150
|
-
newNode.familyPosition = types_1.FamilyPosition.RIGHT;
|
|
151
149
|
}
|
|
152
150
|
//Add to the right of the current node
|
|
153
151
|
cur.right = newNode;
|
|
@@ -201,67 +199,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
201
199
|
else
|
|
202
200
|
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
|
|
203
201
|
};
|
|
204
|
-
/**
|
|
205
|
-
* The `remove` function in this TypeScript code removes a node from a binary search tree and returns information about
|
|
206
|
-
* the deleted node and any nodes that need to be balanced.
|
|
207
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that needs to be removed
|
|
208
|
-
* from the binary search tree.
|
|
209
|
-
* @param {boolean} [ignoreCount] - A boolean flag indicating whether to ignore the count of the node being removed. If
|
|
210
|
-
* set to true, the count of the node will not be considered and the node will be removed regardless of its count. If
|
|
211
|
-
* set to false or not provided, the count of the node will be taken into account and the
|
|
212
|
-
* @returns an array of `BSTDeletedResult<N>` objects.
|
|
213
|
-
*/
|
|
214
|
-
BST.prototype.remove = function (id, ignoreCount) {
|
|
215
|
-
var bstDeletedResult = [];
|
|
216
|
-
if (!this.root)
|
|
217
|
-
return bstDeletedResult;
|
|
218
|
-
var curr = this.get(id);
|
|
219
|
-
if (!curr)
|
|
220
|
-
return bstDeletedResult;
|
|
221
|
-
var parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
222
|
-
var needBalanced = null, orgCurrent = curr;
|
|
223
|
-
if (curr.count > 1 && !ignoreCount) {
|
|
224
|
-
curr.count--;
|
|
225
|
-
this._setCount(this.count - 1);
|
|
226
|
-
}
|
|
227
|
-
else {
|
|
228
|
-
if (!curr.left) {
|
|
229
|
-
if (!parent) {
|
|
230
|
-
if (curr.right !== undefined)
|
|
231
|
-
this._setRoot(curr.right);
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
switch (curr.familyPosition) {
|
|
235
|
-
case types_1.FamilyPosition.LEFT:
|
|
236
|
-
parent.left = curr.right;
|
|
237
|
-
break;
|
|
238
|
-
case types_1.FamilyPosition.RIGHT:
|
|
239
|
-
parent.right = curr.right;
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
needBalanced = parent;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
else {
|
|
246
|
-
var leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
|
|
247
|
-
if (leftSubTreeMax) {
|
|
248
|
-
var parentOfLeftSubTreeMax = leftSubTreeMax.parent;
|
|
249
|
-
orgCurrent = curr.swapLocation(leftSubTreeMax);
|
|
250
|
-
if (parentOfLeftSubTreeMax) {
|
|
251
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeMax)
|
|
252
|
-
parentOfLeftSubTreeMax.right = leftSubTreeMax.left;
|
|
253
|
-
else
|
|
254
|
-
parentOfLeftSubTreeMax.left = leftSubTreeMax.left;
|
|
255
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
this._setSize(this.size - 1);
|
|
260
|
-
this._setCount(this.count - curr.count);
|
|
261
|
-
}
|
|
262
|
-
bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
|
|
263
|
-
return bstDeletedResult;
|
|
264
|
-
};
|
|
265
202
|
/**
|
|
266
203
|
* The function `getNodes` returns an array of binary search tree nodes that match a given property value, with the
|
|
267
204
|
* option to specify the property name and whether to return only one node.
|
|
@@ -324,20 +261,24 @@ var BST = /** @class */ (function (_super) {
|
|
|
324
261
|
};
|
|
325
262
|
// --- start additional functions
|
|
326
263
|
/**
|
|
327
|
-
* The `lesserSum` function calculates the sum of
|
|
328
|
-
* a
|
|
329
|
-
* @param {BinaryTreeNodeId}
|
|
330
|
-
* calculate the lesser sum.
|
|
264
|
+
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a lesser value
|
|
265
|
+
* than a given node.
|
|
266
|
+
* @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
331
267
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
332
|
-
* specifies the property
|
|
333
|
-
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in
|
|
334
|
-
*
|
|
268
|
+
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
|
|
269
|
+
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in a binary
|
|
270
|
+
* tree that have a lesser value than the specified `beginNode` based on the specified `propertyName`.
|
|
335
271
|
*/
|
|
336
|
-
BST.prototype.lesserSum = function (
|
|
272
|
+
BST.prototype.lesserSum = function (beginNode, propertyName) {
|
|
337
273
|
var _this = this;
|
|
338
274
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
275
|
+
if (typeof beginNode === 'number')
|
|
276
|
+
beginNode = this.get(beginNode, 'id');
|
|
277
|
+
if (!beginNode)
|
|
278
|
+
return 0;
|
|
339
279
|
if (!this.root)
|
|
340
280
|
return 0;
|
|
281
|
+
var id = beginNode.id;
|
|
341
282
|
var getSumByPropertyName = function (cur) {
|
|
342
283
|
var needSum;
|
|
343
284
|
switch (propertyName) {
|
|
@@ -426,6 +367,11 @@ var BST = /** @class */ (function (_super) {
|
|
|
426
367
|
BST.prototype.allGreaterNodesAdd = function (node, delta, propertyName) {
|
|
427
368
|
var _this = this;
|
|
428
369
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
370
|
+
if (typeof node === 'number')
|
|
371
|
+
node = this.get(node, 'id');
|
|
372
|
+
if (!node)
|
|
373
|
+
return false;
|
|
374
|
+
var id = node.id;
|
|
429
375
|
if (!this.root)
|
|
430
376
|
return false;
|
|
431
377
|
var _sumByPropertyName = function (cur) {
|
|
@@ -443,7 +389,7 @@ var BST = /** @class */ (function (_super) {
|
|
|
443
389
|
};
|
|
444
390
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
445
391
|
var _traverse_3 = function (cur) {
|
|
446
|
-
var compared = _this._compare(cur.id,
|
|
392
|
+
var compared = _this._compare(cur.id, id);
|
|
447
393
|
_sumByPropertyName(cur);
|
|
448
394
|
if (!cur.left && !cur.right)
|
|
449
395
|
return;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { BinaryTreeNodeId, RBColor, RBTreeOptions } from '../types';
|
|
1
|
+
import { BinaryTreeNodeId, RBColor, RBTreeNodeNested, RBTreeOptions } from '../types';
|
|
2
2
|
import { IRBTree, IRBTreeNode } from '../interfaces/rb-tree';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
|
-
export declare class RBTreeNode<T, FAMILY extends RBTreeNode<T, FAMILY>> extends BSTNode<T, FAMILY> implements IRBTreeNode<T, FAMILY> {
|
|
5
|
-
constructor(id:
|
|
4
|
+
export declare class RBTreeNode<T = any, FAMILY extends RBTreeNode<T, FAMILY> = RBTreeNodeNested<T>> extends BSTNode<T, FAMILY> implements IRBTreeNode<T, FAMILY> {
|
|
5
|
+
constructor(id: BinaryTreeNodeId, val?: T, count?: number);
|
|
6
6
|
private _color;
|
|
7
7
|
get color(): RBColor;
|
|
8
8
|
set color(value: RBColor);
|
|
@@ -16,12 +16,12 @@ export declare class RBTreeNode<T, FAMILY extends RBTreeNode<T, FAMILY>> extends
|
|
|
16
16
|
* node.
|
|
17
17
|
* @returns The method is returning a new instance of the RBTreeNode class, casted as a FAMILY type.
|
|
18
18
|
*/
|
|
19
|
-
createNode(id: BinaryTreeNodeId, val?: T
|
|
19
|
+
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
20
20
|
}
|
|
21
|
-
export declare class RBTree<N extends RBTreeNode<N['val'], N
|
|
21
|
+
export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
|
|
22
22
|
constructor(options?: RBTreeOptions);
|
|
23
23
|
createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N;
|
|
24
|
-
insert(id: number, val
|
|
24
|
+
insert(id: number, val?: N | null): void;
|
|
25
25
|
private leftRotate;
|
|
26
26
|
private rightRotate;
|
|
27
27
|
private insertFixup;
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
11
|
-
export declare class TreeMultiSetNode<T, FAMILY extends TreeMultiSetNode<T, FAMILY> =
|
|
8
|
+
import type { BinaryTreeNodeId, TreeMultiSetNodeNested, TreeMultiSetOptions } from '../types';
|
|
9
|
+
import { ITreeMultiSet, ITreeMultiSetNode } from '../interfaces';
|
|
10
|
+
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
11
|
+
export declare class TreeMultiSetNode<T = any, FAMILY extends TreeMultiSetNode<T, FAMILY> = TreeMultiSetNodeNested<T>> extends AVLTreeNode<T, FAMILY> implements ITreeMultiSetNode<T, FAMILY> {
|
|
12
12
|
/**
|
|
13
13
|
* The function creates a new node in a binary tree with an optional value and count.
|
|
14
14
|
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
@@ -22,9 +22,9 @@ export declare class TreeMultiSetNode<T, FAMILY extends TreeMultiSetNode<T, FAMI
|
|
|
22
22
|
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
|
-
* The only distinction between a TreeMultiSet and a
|
|
25
|
+
* The only distinction between a TreeMultiSet and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
26
26
|
*/
|
|
27
|
-
export declare class TreeMultiSet<N extends
|
|
27
|
+
export declare class TreeMultiSet<N extends TreeMultiSetNode<N['val'], N> = TreeMultiSetNode> extends AVLTree<N> implements ITreeMultiSet<N> {
|
|
28
28
|
constructor(options?: TreeMultiSetOptions);
|
|
29
29
|
/**
|
|
30
30
|
* The function creates a new BSTNode with the given id, value, and count.
|
|
@@ -27,14 +27,7 @@ var __assign = (this && this.__assign) || function () {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.TreeMultiSet = exports.TreeMultiSetNode = void 0;
|
|
30
|
-
|
|
31
|
-
* data-structure-typed
|
|
32
|
-
*
|
|
33
|
-
* @author Tyler Zeng
|
|
34
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
35
|
-
* @license MIT License
|
|
36
|
-
*/
|
|
37
|
-
var bst_1 = require("./bst");
|
|
30
|
+
var avl_tree_1 = require("./avl-tree");
|
|
38
31
|
var TreeMultiSetNode = /** @class */ (function (_super) {
|
|
39
32
|
__extends(TreeMultiSetNode, _super);
|
|
40
33
|
function TreeMultiSetNode() {
|
|
@@ -51,18 +44,18 @@ var TreeMultiSetNode = /** @class */ (function (_super) {
|
|
|
51
44
|
* @returns The method is returning a new instance of the TreeMultiSetNode class, casted as the FAMILY type.
|
|
52
45
|
*/
|
|
53
46
|
TreeMultiSetNode.prototype.createNode = function (id, val, count) {
|
|
54
|
-
return new TreeMultiSetNode(id,
|
|
47
|
+
return new TreeMultiSetNode(id, val, count);
|
|
55
48
|
};
|
|
56
49
|
return TreeMultiSetNode;
|
|
57
|
-
}(
|
|
50
|
+
}(avl_tree_1.AVLTreeNode));
|
|
58
51
|
exports.TreeMultiSetNode = TreeMultiSetNode;
|
|
59
52
|
/**
|
|
60
|
-
* The only distinction between a TreeMultiSet and a
|
|
53
|
+
* The only distinction between a TreeMultiSet and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
61
54
|
*/
|
|
62
55
|
var TreeMultiSet = /** @class */ (function (_super) {
|
|
63
56
|
__extends(TreeMultiSet, _super);
|
|
64
57
|
function TreeMultiSet(options) {
|
|
65
|
-
return _super.call(this, __assign(__assign({}, options), {
|
|
58
|
+
return _super.call(this, __assign(__assign({}, options), { isMergeDuplicatedVal: true })) || this;
|
|
66
59
|
}
|
|
67
60
|
/**
|
|
68
61
|
* The function creates a new BSTNode with the given id, value, and count.
|
|
@@ -74,8 +67,8 @@ var TreeMultiSet = /** @class */ (function (_super) {
|
|
|
74
67
|
* @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
|
|
75
68
|
*/
|
|
76
69
|
TreeMultiSet.prototype.createNode = function (id, val, count) {
|
|
77
|
-
return new TreeMultiSetNode(id, val
|
|
70
|
+
return new TreeMultiSetNode(id, val, count);
|
|
78
71
|
};
|
|
79
72
|
return TreeMultiSet;
|
|
80
|
-
}(
|
|
73
|
+
}(avl_tree_1.AVLTree));
|
|
81
74
|
exports.TreeMultiSet = TreeMultiSet;
|
|
@@ -50,8 +50,8 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
50
50
|
*/
|
|
51
51
|
hasVertex(vertexOrId: V | VertexId): boolean;
|
|
52
52
|
abstract getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
53
|
-
|
|
54
|
-
addVertex(
|
|
53
|
+
addVertex(vertex: V): boolean;
|
|
54
|
+
addVertex(id: VertexId, val?: V['val']): boolean;
|
|
55
55
|
/**
|
|
56
56
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
57
57
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
@@ -80,8 +80,8 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
80
80
|
* vertices `v1` and `v2`, and `false` otherwise.
|
|
81
81
|
*/
|
|
82
82
|
hasEdge(v1: VertexId | V, v2: VertexId | V): boolean;
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
addEdge(edge: E): boolean;
|
|
84
|
+
addEdge(src: V | VertexId, dest: V | VertexId, weight?: number, val?: E['val']): boolean;
|
|
85
85
|
/**
|
|
86
86
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
87
87
|
* @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
|
|
@@ -205,11 +205,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
205
205
|
min: number;
|
|
206
206
|
minPath: V[];
|
|
207
207
|
};
|
|
208
|
-
/**
|
|
209
|
-
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
210
|
-
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
211
|
-
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
212
|
-
*/
|
|
213
208
|
/**
|
|
214
209
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
215
210
|
* all pairs
|
|
@@ -254,6 +249,13 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
|
|
|
254
249
|
SCCs: Map<number, V[]>;
|
|
255
250
|
cycles: Map<number, V[]>;
|
|
256
251
|
};
|
|
252
|
+
/**
|
|
253
|
+
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
254
|
+
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
255
|
+
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
256
|
+
*/
|
|
257
|
+
protected _addVertexOnly(newVertex: V): boolean;
|
|
258
|
+
protected abstract _addEdgeOnly(edge: E): boolean;
|
|
257
259
|
/**
|
|
258
260
|
* BellmanFord time:O(VE) space:O(V)
|
|
259
261
|
* one to rest pairs
|
|
@@ -149,17 +149,14 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
149
149
|
AbstractGraph.prototype.hasVertex = function (vertexOrId) {
|
|
150
150
|
return this._vertices.has(this._getVertexId(vertexOrId));
|
|
151
151
|
};
|
|
152
|
-
AbstractGraph.prototype.
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
return
|
|
159
|
-
// throw (new Error('Duplicated vertex id is not allowed'));
|
|
152
|
+
AbstractGraph.prototype.addVertex = function (idOrVertex, val) {
|
|
153
|
+
if (idOrVertex instanceof AbstractVertex) {
|
|
154
|
+
return this._addVertexOnly(idOrVertex);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
var newVertex = this.createVertex(idOrVertex, val);
|
|
158
|
+
return this._addVertexOnly(newVertex);
|
|
160
159
|
}
|
|
161
|
-
this._vertices.set(newVertex.id, newVertex);
|
|
162
|
-
return true;
|
|
163
160
|
};
|
|
164
161
|
/**
|
|
165
162
|
* The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
|
|
@@ -209,13 +206,25 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
209
206
|
var edge = this.getEdge(v1, v2);
|
|
210
207
|
return !!edge;
|
|
211
208
|
};
|
|
212
|
-
AbstractGraph.prototype.
|
|
213
|
-
if (
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
209
|
+
AbstractGraph.prototype.addEdge = function (srcOrEdge, dest, weight, val) {
|
|
210
|
+
if (srcOrEdge instanceof AbstractEdge) {
|
|
211
|
+
return this._addEdgeOnly(srcOrEdge);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
|
|
215
|
+
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest)))
|
|
216
|
+
return false;
|
|
217
|
+
if (srcOrEdge instanceof AbstractVertex)
|
|
218
|
+
srcOrEdge = srcOrEdge.id;
|
|
219
|
+
if (dest instanceof AbstractVertex)
|
|
220
|
+
dest = dest.id;
|
|
221
|
+
var newEdge = this.createEdge(srcOrEdge, dest, weight, val);
|
|
222
|
+
return this._addEdgeOnly(newEdge);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
throw new Error('dest must be a Vertex or vertex id while srcOrEdge is an Edge');
|
|
226
|
+
}
|
|
227
|
+
}
|
|
219
228
|
};
|
|
220
229
|
/**
|
|
221
230
|
* The function sets the weight of an edge between two vertices in a graph.
|
|
@@ -884,11 +893,6 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
884
893
|
}
|
|
885
894
|
return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
|
|
886
895
|
};
|
|
887
|
-
/**
|
|
888
|
-
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
889
|
-
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
890
|
-
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
891
|
-
*/
|
|
892
896
|
/**
|
|
893
897
|
* Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
|
|
894
898
|
* all pairs
|
|
@@ -1058,6 +1062,19 @@ var AbstractGraph = /** @class */ (function () {
|
|
|
1058
1062
|
}
|
|
1059
1063
|
return { dfnMap: dfnMap, lowMap: lowMap, bridges: bridges, articulationPoints: articulationPoints, SCCs: SCCs, cycles: cycles };
|
|
1060
1064
|
};
|
|
1065
|
+
/**
|
|
1066
|
+
* Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
|
|
1067
|
+
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
|
|
1068
|
+
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
1069
|
+
*/
|
|
1070
|
+
AbstractGraph.prototype._addVertexOnly = function (newVertex) {
|
|
1071
|
+
if (this.hasVertex(newVertex)) {
|
|
1072
|
+
return false;
|
|
1073
|
+
// throw (new Error('Duplicated vertex id is not allowed'));
|
|
1074
|
+
}
|
|
1075
|
+
this._vertices.set(newVertex.id, newVertex);
|
|
1076
|
+
return true;
|
|
1077
|
+
};
|
|
1061
1078
|
/**
|
|
1062
1079
|
* BellmanFord time:O(VE) space:O(V)
|
|
1063
1080
|
* one to rest pairs
|
|
@@ -63,14 +63,6 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
63
63
|
* @returns a E object or null.
|
|
64
64
|
*/
|
|
65
65
|
getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null;
|
|
66
|
-
/**
|
|
67
|
-
* The `addEdge` function adds a directed edge to a graph if the source and destination vertices exist.
|
|
68
|
-
* @param edge - The parameter `edge` is of type `E`, which represents a directed edge in a graph. It
|
|
69
|
-
* contains two properties:
|
|
70
|
-
* @returns The method `addEdge` returns a boolean value. It returns `true` if the edge was successfully added to the
|
|
71
|
-
* graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
|
|
72
|
-
*/
|
|
73
|
-
addEdge(edge: E): boolean;
|
|
74
66
|
/**
|
|
75
67
|
* The `removeEdgeBetween` function removes an edge between two vertices in a directed graph and returns the removed
|
|
76
68
|
* edge, or null if the edge was not found.
|
|
@@ -164,18 +156,19 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
164
156
|
*/
|
|
165
157
|
getDestinations(vertex: V | VertexId | null): V[];
|
|
166
158
|
/**
|
|
167
|
-
* The `topologicalSort` function performs a topological sort on a
|
|
168
|
-
*
|
|
169
|
-
* @
|
|
170
|
-
*
|
|
159
|
+
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
160
|
+
* in the sorted order, or null if the graph contains a cycle.
|
|
161
|
+
* @param {'vertex' | 'id'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
162
|
+
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'id'. If 'vertex' is
|
|
163
|
+
* specified, the vertices themselves will be used for sorting. If 'id' is specified, the ids of
|
|
164
|
+
* @returns an array of vertices or vertex IDs in topological order, or null if there is a cycle in the graph.
|
|
171
165
|
*/
|
|
172
|
-
topologicalSort(): Array<V | VertexId> | null;
|
|
166
|
+
topologicalSort(propertyName?: 'vertex' | 'id'): Array<V | VertexId> | null;
|
|
173
167
|
/**
|
|
174
168
|
* The `edgeSet` function returns an array of all directed edges in the graph.
|
|
175
169
|
* @returns The `edgeSet()` method returns an array of `E` objects.
|
|
176
170
|
*/
|
|
177
171
|
edgeSet(): E[];
|
|
178
|
-
/**--- start find cycles --- */
|
|
179
172
|
/**
|
|
180
173
|
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex in a directed graph.
|
|
181
174
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a `V`
|
|
@@ -183,7 +176,7 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
183
176
|
* @returns an array of DirectedVertex objects.
|
|
184
177
|
*/
|
|
185
178
|
getNeighbors(vertexOrId: V | VertexId): V[];
|
|
186
|
-
/**---
|
|
179
|
+
/**--- start find cycles --- */
|
|
187
180
|
/**
|
|
188
181
|
* The function "getEndsOfEdge" returns the source and destination vertices of a directed edge if it exists in the
|
|
189
182
|
* graph, otherwise it returns null.
|
|
@@ -192,6 +185,15 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
|
|
|
192
185
|
* not exist in the graph.
|
|
193
186
|
*/
|
|
194
187
|
getEndsOfEdge(edge: E): [V, V] | null;
|
|
188
|
+
/**--- end find cycles --- */
|
|
189
|
+
/**
|
|
190
|
+
* The `_addEdgeOnly` function adds a directed edge to a graph if the source and destination vertices exist.
|
|
191
|
+
* @param edge - The parameter `edge` is of type `E`, which represents a directed edge in a graph. It
|
|
192
|
+
* contains two properties:
|
|
193
|
+
* @returns The method `_addEdgeOnly` returns a boolean value. It returns `true` if the edge was successfully added to the
|
|
194
|
+
* graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
|
|
195
|
+
*/
|
|
196
|
+
protected _addEdgeOnly(edge: E): boolean;
|
|
195
197
|
protected _setOutEdgeMap(value: Map<V, E[]>): void;
|
|
196
198
|
protected _setInEdgeMap(value: Map<V, E[]>): void;
|
|
197
199
|
}
|