data-structure-typed 1.18.7 → 1.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +208 -419
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +198 -167
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +413 -398
- package/dist/data-structures/binary-tree/avl-tree.d.ts +24 -16
- package/dist/data-structures/binary-tree/avl-tree.js +23 -17
- package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -26
- package/dist/data-structures/binary-tree/binary-tree.js +12 -27
- package/dist/data-structures/binary-tree/bst.d.ts +66 -90
- package/dist/data-structures/binary-tree/bst.js +85 -163
- package/dist/data-structures/binary-tree/rb-tree.d.ts +6 -17
- package/dist/data-structures/binary-tree/rb-tree.js +5 -17
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +187 -18
- package/dist/data-structures/binary-tree/tree-multiset.js +716 -39
- package/dist/data-structures/graph/abstract-graph.d.ts +112 -52
- package/dist/data-structures/graph/abstract-graph.js +138 -72
- package/dist/data-structures/graph/directed-graph.d.ts +104 -101
- package/dist/data-structures/graph/directed-graph.js +134 -129
- package/dist/data-structures/graph/undirected-graph.d.ts +66 -65
- package/dist/data-structures/graph/undirected-graph.js +107 -106
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +12 -18
- package/dist/data-structures/interfaces/abstract-graph.d.ts +2 -4
- package/dist/data-structures/interfaces/avl-tree.d.ts +2 -2
- package/dist/data-structures/interfaces/binary-tree.d.ts +1 -1
- package/dist/data-structures/interfaces/bst.d.ts +4 -5
- package/dist/data-structures/interfaces/rb-tree.d.ts +2 -3
- 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 +10 -6
- 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 +5 -5
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/types/index.d.ts +1 -0
- package/dist/utils/types/index.js +1 -0
- package/dist/utils/types/validate-type.d.ts +19 -0
- package/dist/utils/types/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +2 -2
- package/dist/utils/utils.js +0 -62
- package/dist/utils/validate-type.d.ts +45 -0
- package/dist/utils/validate-type.js +58 -0
- package/package.json +5 -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
package/README.md
CHANGED
|
@@ -3,8 +3,190 @@
|
|
|
3
3
|
## Brief
|
|
4
4
|
Javascript & TypeScript Data Structure Library.
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
Binary Tree, Binary Search Tree (BST), AVL Tree, Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Linked List, Singly Linked List, Doubly Linked List, Queue, Object Deque, Array Deque, Stack, Hash, Coordinate Set, Coordinate Map, Heap, Priority Queue, Max Priority Queue, Min Priority Queue, Trie
|
|
7
|
+
|
|
8
|
+
## Algorithms list only a few out, you can discover more in API docs
|
|
9
|
+
|
|
10
|
+
DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm
|
|
11
|
+
|
|
12
|
+
## Code design
|
|
13
|
+
By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design.
|
|
14
|
+
|
|
15
|
+
# How
|
|
16
|
+
|
|
17
|
+
## install
|
|
18
|
+
### yarn
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
yarn add data-structure-typed
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### npm
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install data-structure-typed
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Binary Search Tree (BST) snippet
|
|
31
|
+
|
|
32
|
+
#### TS
|
|
33
|
+
```typescript
|
|
34
|
+
import {BST, BSTNode} from 'data-structure-typed';
|
|
35
|
+
|
|
36
|
+
const bst = new BST();
|
|
37
|
+
bst.add(11);
|
|
38
|
+
bst.add(3);
|
|
39
|
+
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
40
|
+
bst.size === 16; // true
|
|
41
|
+
bst.has(6); // true
|
|
42
|
+
const node6 = bst.get(6);
|
|
43
|
+
bst.getHeight(6) === 2; // true
|
|
44
|
+
bst.getHeight() === 5; // true
|
|
45
|
+
bst.getDepth(6) === 3; // true
|
|
46
|
+
const leftMost = bst.getLeftMost();
|
|
47
|
+
leftMost?.id === 1; // true
|
|
48
|
+
expect(leftMost?.id).toBe(1);
|
|
49
|
+
bst.remove(6);
|
|
50
|
+
bst.get(6); // null
|
|
51
|
+
bst.isAVLBalanced(); // true or false
|
|
52
|
+
const bfsIDs = bst.BFS();
|
|
53
|
+
bfsIDs[0] === 11; // true
|
|
54
|
+
expect(bfsIDs[0]).toBe(11);
|
|
55
|
+
|
|
56
|
+
const objBST = new BST<BSTNode<{ id: number, keyA: number }>>();
|
|
57
|
+
objBST.add(11, {id: 11, keyA: 11});
|
|
58
|
+
objBST.add(3, {id: 3, keyA: 3});
|
|
59
|
+
|
|
60
|
+
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
61
|
+
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
62
|
+
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
63
|
+
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
64
|
+
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
65
|
+
|
|
66
|
+
objBST.remove(11);
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
const avlTree = new AVLTree();
|
|
70
|
+
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
71
|
+
avlTree.isAVLBalanced(); // true
|
|
72
|
+
avlTree.remove(10);
|
|
73
|
+
avlTree.isAVLBalanced(); // true
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
#### JS
|
|
77
|
+
```javascript
|
|
78
|
+
const {BST, BSTNode} = require('data-structure-typed');
|
|
79
|
+
|
|
80
|
+
const bst = new BST();
|
|
81
|
+
bst.add(11);
|
|
82
|
+
bst.add(3);
|
|
83
|
+
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
84
|
+
bst.size === 16; // true
|
|
85
|
+
bst.has(6); // true
|
|
86
|
+
const node6 = bst.get(6);
|
|
87
|
+
bst.getHeight(6) === 2; // true
|
|
88
|
+
bst.getHeight() === 5; // true
|
|
89
|
+
bst.getDepth(6) === 3; // true
|
|
90
|
+
const leftMost = bst.getLeftMost();
|
|
91
|
+
leftMost?.id === 1; // true
|
|
92
|
+
expect(leftMost?.id).toBe(1);
|
|
93
|
+
bst.remove(6);
|
|
94
|
+
bst.get(6); // null
|
|
95
|
+
bst.isAVLBalanced(); // true or false
|
|
96
|
+
const bfsIDs = bst.BFS();
|
|
97
|
+
bfsIDs[0] === 11; // true
|
|
98
|
+
expect(bfsIDs[0]).toBe(11);
|
|
99
|
+
|
|
100
|
+
const objBST = new BST();
|
|
101
|
+
objBST.add(11, {id: 11, keyA: 11});
|
|
102
|
+
objBST.add(3, {id: 3, keyA: 3});
|
|
103
|
+
|
|
104
|
+
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
105
|
+
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
106
|
+
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
107
|
+
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
108
|
+
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
109
|
+
|
|
110
|
+
objBST.remove(11);
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
const avlTree = new AVLTree();
|
|
114
|
+
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
115
|
+
avlTree.isAVLBalanced(); // true
|
|
116
|
+
avlTree.remove(10);
|
|
117
|
+
avlTree.isAVLBalanced(); // true
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Directed Graph simple snippet
|
|
122
|
+
|
|
123
|
+
#### TS or JS
|
|
124
|
+
```typescript
|
|
125
|
+
import {DirectedGraph} from 'data-structure-typed';
|
|
126
|
+
|
|
127
|
+
const graph = new DirectedGraph();
|
|
128
|
+
|
|
129
|
+
graph.addVertex('A');
|
|
130
|
+
graph.addVertex('B');
|
|
131
|
+
|
|
132
|
+
graph.hasVertex('A'); // true
|
|
133
|
+
graph.hasVertex('B'); // true
|
|
134
|
+
graph.hasVertex('C'); // false
|
|
135
|
+
|
|
136
|
+
graph.addEdge('A', 'B');
|
|
137
|
+
graph.hasEdge('A', 'B'); // true
|
|
138
|
+
graph.hasEdge('B', 'A'); // false
|
|
139
|
+
|
|
140
|
+
graph.removeEdgeSrcToDest('A', 'B');
|
|
141
|
+
graph.hasEdge('A', 'B'); // false
|
|
142
|
+
|
|
143
|
+
graph.addVertex('C');
|
|
144
|
+
|
|
145
|
+
graph.addEdge('A', 'B');
|
|
146
|
+
graph.addEdge('B', 'C');
|
|
147
|
+
|
|
148
|
+
const topologicalOrderIds = graph.topologicalSort(); // ['A', 'B', 'C']
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Undirected Graph snippet
|
|
152
|
+
|
|
153
|
+
#### TS or JS
|
|
154
|
+
```typescript
|
|
155
|
+
import {UndirectedGraph} from 'data-structure-typed';
|
|
156
|
+
|
|
157
|
+
const graph = new UndirectedGraph();
|
|
158
|
+
graph.addVertex('A');
|
|
159
|
+
graph.addVertex('B');
|
|
160
|
+
graph.addVertex('C');
|
|
161
|
+
graph.addVertex('D');
|
|
162
|
+
graph.removeVertex('C');
|
|
163
|
+
graph.addEdge('A', 'B');
|
|
164
|
+
graph.addEdge('B', 'D');
|
|
165
|
+
|
|
166
|
+
const dijkstraResult = graph.dijkstra('A');
|
|
167
|
+
Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D']
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+

|
|
171
|
+
|
|
172
|
+

|
|
173
|
+
|
|
174
|
+

|
|
175
|
+
|
|
176
|
+

|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
## API docs & Examples
|
|
180
|
+
|
|
181
|
+
[API Docs](https://data-structure-typed-docs.vercel.app)
|
|
182
|
+
|
|
183
|
+
[Live Examples](https://data-structure-typed-examples.vercel.app)
|
|
184
|
+
|
|
185
|
+
<a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
|
|
186
|
+
|
|
187
|
+
[//]: # ([Examples Repository](https://github.com/zrwusa/data-structure-typed-examples))
|
|
188
|
+
|
|
189
|
+
<a href="https://github.com/zrwusa/data-structure-typed-examples" target="_blank">Examples Repository</a>
|
|
8
190
|
|
|
9
191
|
## Data Structures
|
|
10
192
|
|
|
@@ -46,7 +228,7 @@ wide range of data structures
|
|
|
46
228
|
<td>Tree Multiset</td>
|
|
47
229
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
48
230
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
49
|
-
<td><a href="https://data-structure-typed-docs.vercel.app/classes/
|
|
231
|
+
<td><a href="https://data-structure-typed-docs.vercel.app/classes/TreeMultiset.html"><span>TreeMultiset</span></a></td>
|
|
50
232
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
51
233
|
</tr>
|
|
52
234
|
<tr>
|
|
@@ -199,410 +381,6 @@ wide range of data structures
|
|
|
199
381
|
</tbody>
|
|
200
382
|
</table>
|
|
201
383
|
|
|
202
|
-
## Algorithms list only a few out, you can discover more in API docs
|
|
203
|
-
|
|
204
|
-
DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm
|
|
205
|
-
|
|
206
|
-
# How
|
|
207
|
-
|
|
208
|
-
[API Docs](https://data-structure-typed-docs.vercel.app)
|
|
209
|
-
|
|
210
|
-
[Live Examples](https://data-structure-typed-examples.vercel.app)
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-

|
|
214
|
-
|
|
215
|
-

|
|
216
|
-
|
|
217
|
-

|
|
218
|
-
|
|
219
|
-

|
|
220
|
-
|
|
221
|
-
<a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
|
|
222
|
-
|
|
223
|
-
## install
|
|
224
|
-
|
|
225
|
-
### yarn
|
|
226
|
-
|
|
227
|
-
```bash
|
|
228
|
-
yarn add data-structure-typed
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
### npm
|
|
232
|
-
|
|
233
|
-
```bash
|
|
234
|
-
npm install data-structure-typed
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### Binary Search Tree (BST) snippet
|
|
238
|
-
|
|
239
|
-
```typescript
|
|
240
|
-
import {BST, BSTNode} from 'data-structure-typed';
|
|
241
|
-
|
|
242
|
-
const tree = new BST();
|
|
243
|
-
expect(tree).toBeInstanceOf(BST);
|
|
244
|
-
|
|
245
|
-
const ids = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
246
|
-
tree.addMany(ids);
|
|
247
|
-
expect(tree.root).toBeInstanceOf(BSTNode);
|
|
248
|
-
if (tree.root) expect(tree.root.id).toBe(11);
|
|
249
|
-
expect(tree.count).toBe(16);
|
|
250
|
-
expect(tree.has(6)).toBe(true);
|
|
251
|
-
|
|
252
|
-
const node6 = tree.get(6);
|
|
253
|
-
expect(node6 && tree.getHeight(node6)).toBe(2);
|
|
254
|
-
expect(node6 && tree.getDepth(node6)).toBe(3);
|
|
255
|
-
|
|
256
|
-
const nodeId10 = tree.get(10, 'id');
|
|
257
|
-
expect(nodeId10?.id).toBe(10);
|
|
258
|
-
|
|
259
|
-
const nodeVal9 = tree.get(9, 'val');
|
|
260
|
-
expect(nodeVal9?.id).toBe(9);
|
|
261
|
-
|
|
262
|
-
const nodesByCount1 = tree.getNodes(1, 'count');
|
|
263
|
-
expect(nodesByCount1.length).toBe(16);
|
|
264
|
-
|
|
265
|
-
const leftMost = tree.getLeftMost();
|
|
266
|
-
expect(leftMost?.id).toBe(1);
|
|
267
|
-
|
|
268
|
-
const node15 = tree.get(15);
|
|
269
|
-
const minNodeBySpecificNode = node15 && tree.getLeftMost(node15);
|
|
270
|
-
expect(minNodeBySpecificNode?.id).toBe(12);
|
|
271
|
-
|
|
272
|
-
const subTreeSum = node15 && tree.subTreeSum(node15);
|
|
273
|
-
expect(subTreeSum).toBe(70);
|
|
274
|
-
|
|
275
|
-
const lesserSum = tree.lesserSum(10);
|
|
276
|
-
expect(lesserSum).toBe(45);
|
|
277
|
-
|
|
278
|
-
expect(node15).toBeInstanceOf(BSTNode);
|
|
279
|
-
if (node15 instanceof BSTNode) {
|
|
280
|
-
const subTreeAdd = tree.subTreeAdd(node15, 1, 'count');
|
|
281
|
-
expect(subTreeAdd).toBeDefined();
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const node11 = tree.get(11);
|
|
285
|
-
expect(node11).toBeInstanceOf(BSTNode);
|
|
286
|
-
if (node11 instanceof BSTNode) {
|
|
287
|
-
const allGreaterNodesAdded = tree.allGreaterNodesAdd(node11, 2, 'count');
|
|
288
|
-
expect(allGreaterNodesAdded).toBeDefined();
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const dfsInorderNodes = tree.DFS('in', 'node');
|
|
292
|
-
expect(dfsInorderNodes[0].id).toBe(1);
|
|
293
|
-
expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16);
|
|
294
|
-
|
|
295
|
-
tree.balance();
|
|
296
|
-
expect(tree.isBalanced()).toBe(true);
|
|
297
|
-
|
|
298
|
-
const bfsNodesAfterBalanced = tree.BFS('node');
|
|
299
|
-
expect(bfsNodesAfterBalanced[0].id).toBe(8);
|
|
300
|
-
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16);
|
|
301
|
-
|
|
302
|
-
const removed11 = tree.remove(11, true);
|
|
303
|
-
expect(removed11).toBeInstanceOf(Array);
|
|
304
|
-
expect(removed11[0]).toBeDefined();
|
|
305
|
-
expect(removed11[0].deleted).toBeDefined();
|
|
306
|
-
|
|
307
|
-
if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11);
|
|
308
|
-
|
|
309
|
-
expect(tree.isAVLBalanced()).toBe(true);
|
|
310
|
-
|
|
311
|
-
expect(node15 && tree.getHeight(node15)).toBe(2);
|
|
312
|
-
|
|
313
|
-
const removed1 = tree.remove(1, true);
|
|
314
|
-
expect(removed1).toBeInstanceOf(Array);
|
|
315
|
-
expect(removed1[0]).toBeDefined();
|
|
316
|
-
expect(removed1[0].deleted).toBeDefined();
|
|
317
|
-
if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1);
|
|
318
|
-
|
|
319
|
-
expect(tree.isAVLBalanced()).toBe(true);
|
|
320
|
-
|
|
321
|
-
expect(tree.getHeight()).toBe(4);
|
|
322
|
-
|
|
323
|
-
// The code for removing these nodes (4, 10, 15, 5, 13, 3, 8, 6, 7, 9, 14) in sequence has been omitted.
|
|
324
|
-
|
|
325
|
-
expect(tree.isAVLBalanced()).toBe(false);
|
|
326
|
-
|
|
327
|
-
const bfsIDs = tree.BFS();
|
|
328
|
-
expect(bfsIDs[0]).toBe(2);
|
|
329
|
-
expect(bfsIDs[1]).toBe(12);
|
|
330
|
-
expect(bfsIDs[2]).toBe(16);
|
|
331
|
-
|
|
332
|
-
const bfsNodes = tree.BFS('node');
|
|
333
|
-
expect(bfsNodes[0].id).toBe(2);
|
|
334
|
-
expect(bfsNodes[1].id).toBe(12);
|
|
335
|
-
expect(bfsNodes[2].id).toBe(16);
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
### Directed Graph simple snippet
|
|
339
|
-
|
|
340
|
-
```typescript
|
|
341
|
-
import {DirectedGraph, DirectedVertex, DirectedEdge, VertexId} from 'data-structure-typed';
|
|
342
|
-
|
|
343
|
-
let graph: DirectedGraph<DirectedVertex, DirectedEdge>;
|
|
344
|
-
|
|
345
|
-
beforeEach(() => {
|
|
346
|
-
graph = new DirectedGraph();
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
it('should add vertices', () => {
|
|
351
|
-
const vertex1 = new DirectedVertex('A');
|
|
352
|
-
const vertex2 = new DirectedVertex('B');
|
|
353
|
-
|
|
354
|
-
graph.addVertex(vertex1);
|
|
355
|
-
graph.addVertex(vertex2);
|
|
356
|
-
|
|
357
|
-
expect(graph.hasVertex(vertex1)).toBe(true);
|
|
358
|
-
expect(graph.hasVertex(vertex2)).toBe(true);
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
it('should add edges', () => {
|
|
362
|
-
const vertex1 = new DirectedVertex('A');
|
|
363
|
-
const vertex2 = new DirectedVertex('B');
|
|
364
|
-
const edge = new DirectedEdge('A', 'B');
|
|
365
|
-
|
|
366
|
-
graph.addVertex(vertex1);
|
|
367
|
-
graph.addVertex(vertex2);
|
|
368
|
-
graph.addEdge(edge);
|
|
369
|
-
|
|
370
|
-
expect(graph.hasEdge('A', 'B')).toBe(true);
|
|
371
|
-
expect(graph.hasEdge('B', 'A')).toBe(false);
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
it('should remove edges', () => {
|
|
375
|
-
const vertex1 = new DirectedVertex('A');
|
|
376
|
-
const vertex2 = new DirectedVertex('B');
|
|
377
|
-
const edge = new DirectedEdge('A', 'B');
|
|
378
|
-
|
|
379
|
-
graph.addVertex(vertex1);
|
|
380
|
-
graph.addVertex(vertex2);
|
|
381
|
-
graph.addEdge(edge);
|
|
382
|
-
|
|
383
|
-
expect(graph.removeEdge(edge)).toBe(edge);
|
|
384
|
-
expect(graph.hasEdge('A', 'B')).toBe(false);
|
|
385
|
-
});
|
|
386
|
-
|
|
387
|
-
it('should perform topological sort', () => {
|
|
388
|
-
const vertexA = new DirectedVertex('A');
|
|
389
|
-
const vertexB = new DirectedVertex('B');
|
|
390
|
-
const vertexC = new DirectedVertex('C');
|
|
391
|
-
const edgeAB = new DirectedEdge('A', 'B');
|
|
392
|
-
const edgeBC = new DirectedEdge('B', 'C');
|
|
393
|
-
|
|
394
|
-
graph.addVertex(vertexA);
|
|
395
|
-
graph.addVertex(vertexB);
|
|
396
|
-
graph.addVertex(vertexC);
|
|
397
|
-
graph.addEdge(edgeAB);
|
|
398
|
-
graph.addEdge(edgeBC);
|
|
399
|
-
|
|
400
|
-
const topologicalOrder = graph.topologicalSort();
|
|
401
|
-
if (topologicalOrder) expect(topologicalOrder.map(v => v.id)).toEqual(['A', 'B', 'C']);
|
|
402
|
-
});
|
|
403
|
-
```
|
|
404
|
-
|
|
405
|
-
### Directed Graph complex snippet
|
|
406
|
-
|
|
407
|
-
```typescript
|
|
408
|
-
import {DirectedGraph, DirectedVertex, DirectedEdge, VertexId} from 'data-structure-typed';
|
|
409
|
-
|
|
410
|
-
class MyVertex extends DirectedVertex {
|
|
411
|
-
private _data: string;
|
|
412
|
-
get data(): string {
|
|
413
|
-
return this._data;
|
|
414
|
-
}
|
|
415
|
-
set data(value: string) {
|
|
416
|
-
this._data = value;
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
constructor(id: VertexId, data: string) {
|
|
420
|
-
super(id);
|
|
421
|
-
this._data = data;
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
class MyEdge extends DirectedEdge {
|
|
426
|
-
private _data: string;
|
|
427
|
-
get data(): string {
|
|
428
|
-
return this._data;
|
|
429
|
-
}
|
|
430
|
-
set data(value: string) {
|
|
431
|
-
this._data = value;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
constructor(v1: VertexId, v2: VertexId, weight: number, data: string) {
|
|
435
|
-
super(v1, v2, weight);
|
|
436
|
-
this._data = data;
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
describe('DirectedGraph Test3', () => {
|
|
441
|
-
const myGraph = new DirectedGraph<MyVertex, MyEdge>();
|
|
442
|
-
|
|
443
|
-
it('should test graph operations', () => {
|
|
444
|
-
const vertex1 = new MyVertex(1, 'data1');
|
|
445
|
-
const vertex2 = new MyVertex(2, 'data2');
|
|
446
|
-
const vertex3 = new MyVertex(3, 'data3');
|
|
447
|
-
const vertex4 = new MyVertex(4, 'data4');
|
|
448
|
-
const vertex5 = new MyVertex(5, 'data5');
|
|
449
|
-
const vertex6 = new MyVertex(6, 'data6');
|
|
450
|
-
const vertex7 = new MyVertex(7, 'data7');
|
|
451
|
-
const vertex8 = new MyVertex(8, 'data8');
|
|
452
|
-
const vertex9 = new MyVertex(9, 'data9');
|
|
453
|
-
myGraph.addVertex(vertex1);
|
|
454
|
-
myGraph.addVertex(vertex2);
|
|
455
|
-
myGraph.addVertex(vertex3);
|
|
456
|
-
myGraph.addVertex(vertex4);
|
|
457
|
-
myGraph.addVertex(vertex5);
|
|
458
|
-
myGraph.addVertex(vertex6);
|
|
459
|
-
myGraph.addVertex(vertex7);
|
|
460
|
-
myGraph.addVertex(vertex8);
|
|
461
|
-
myGraph.addVertex(vertex9);
|
|
462
|
-
|
|
463
|
-
myGraph.addEdge(new MyEdge(1, 2, 10, 'edge-data1-2'));
|
|
464
|
-
myGraph.addEdge(new MyEdge(2, 1, 20, 'edge-data2-1'));
|
|
465
|
-
|
|
466
|
-
expect(myGraph.getEdge(1, 2)).toBeTruthy();
|
|
467
|
-
expect(myGraph.getEdge(2, 1)).toBeTruthy();
|
|
468
|
-
expect(myGraph.getEdge(1, '100')).toBeFalsy();
|
|
469
|
-
|
|
470
|
-
myGraph.removeEdgeBetween(1, 2);
|
|
471
|
-
expect(myGraph.getEdge(1, 2)).toBeFalsy();
|
|
472
|
-
|
|
473
|
-
myGraph.addEdge(new MyEdge(3, 1, 3, 'edge-data-3-1'));
|
|
474
|
-
myGraph.addEdge(new MyEdge(1, 9, 19, 'edge-data1-9'));
|
|
475
|
-
myGraph.addEdge(new MyEdge(9, 7, 97, 'edge-data9-7'));
|
|
476
|
-
myGraph.addEdge(new MyEdge(7, 9, 79, 'edge-data7-9'));
|
|
477
|
-
myGraph.addEdge(new MyEdge(1, 4, 14, 'edge-data1-4'));
|
|
478
|
-
myGraph.addEdge(new MyEdge(4, 7, 47, 'edge-data4-7'));
|
|
479
|
-
myGraph.addEdge(new MyEdge(1, 2, 12, 'edge-data1-2'));
|
|
480
|
-
myGraph.addEdge(new MyEdge(2, 3, 23, 'edge-data2-3'));
|
|
481
|
-
myGraph.addEdge(new MyEdge(3, 5, 35, 'edge-data3-5'));
|
|
482
|
-
myGraph.addEdge(new MyEdge(5, 7, 57, 'edge-data5-7'));
|
|
483
|
-
myGraph.addEdge(new MyEdge(7, 3, 73, 'edge-data7-3'));
|
|
484
|
-
|
|
485
|
-
const topologicalSorted = myGraph.topologicalSort();
|
|
486
|
-
expect(topologicalSorted).toBeNull();
|
|
487
|
-
|
|
488
|
-
const minPath1to7 = myGraph.getMinPathBetween(1, 7);
|
|
489
|
-
expect(minPath1to7).toBeInstanceOf(Array);
|
|
490
|
-
if (minPath1to7 && minPath1to7.length > 0) {
|
|
491
|
-
expect(minPath1to7).toHaveLength(3);
|
|
492
|
-
expect(minPath1to7[0]).toBeInstanceOf(MyVertex);
|
|
493
|
-
expect(minPath1to7[0].id).toBe(1);
|
|
494
|
-
expect(minPath1to7[1].id).toBe(9);
|
|
495
|
-
expect(minPath1to7[2].id).toBe(7);
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
const fordResult1 = myGraph.bellmanFord(1);
|
|
499
|
-
expect(fordResult1).toBeTruthy();
|
|
500
|
-
expect(fordResult1.hasNegativeCycle).toBeUndefined();
|
|
501
|
-
const {distMap, preMap, paths, min, minPath} = fordResult1;
|
|
502
|
-
expect(distMap).toBeInstanceOf(Map);
|
|
503
|
-
expect(distMap.size).toBe(9);
|
|
504
|
-
expect(distMap.get(vertex1)).toBe(0);
|
|
505
|
-
expect(distMap.get(vertex2)).toBe(12);
|
|
506
|
-
expect(distMap.get(vertex3)).toBe(35);
|
|
507
|
-
expect(distMap.get(vertex4)).toBe(14);
|
|
508
|
-
expect(distMap.get(vertex5)).toBe(70);
|
|
509
|
-
expect(distMap.get(vertex6)).toBe(Infinity);
|
|
510
|
-
expect(distMap.get(vertex7)).toBe(61);
|
|
511
|
-
expect(distMap.get(vertex8)).toBe(Infinity);
|
|
512
|
-
expect(distMap.get(vertex9)).toBe(19);
|
|
513
|
-
|
|
514
|
-
expect(preMap).toBeInstanceOf(Map);
|
|
515
|
-
expect(preMap.size).toBe(0);
|
|
516
|
-
|
|
517
|
-
expect(paths).toBeInstanceOf(Array);
|
|
518
|
-
expect(paths.length).toBe(0);
|
|
519
|
-
expect(min).toBe(Infinity);
|
|
520
|
-
expect(minPath).toBeInstanceOf(Array);
|
|
521
|
-
|
|
522
|
-
const floydResult = myGraph.floyd();
|
|
523
|
-
expect(floydResult).toBeTruthy();
|
|
524
|
-
if (floydResult) {
|
|
525
|
-
const {costs, predecessor} = floydResult;
|
|
526
|
-
expect(costs).toBeInstanceOf(Array);
|
|
527
|
-
expect(costs.length).toBe(9);
|
|
528
|
-
expect(costs[0]).toEqual([32, 12, 35, 14, 70, Infinity, 61, Infinity, 19]);
|
|
529
|
-
expect(costs[1]).toEqual([20, 32, 23, 34, 58, Infinity, 81, Infinity, 39]);
|
|
530
|
-
expect(costs[2]).toEqual([3, 15, 38, 17, 35, Infinity, 64, Infinity, 22]);
|
|
531
|
-
expect(costs[3]).toEqual([123, 135, 120, 137, 155, Infinity, 47, Infinity, 126]);
|
|
532
|
-
expect(costs[4]).toEqual([133, 145, 130, 147, 165, Infinity, 57, Infinity, 136]);
|
|
533
|
-
expect(costs[5]).toEqual([Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity]);
|
|
534
|
-
expect(costs[6]).toEqual([76, 88, 73, 90, 108, Infinity, 137, Infinity, 79]);
|
|
535
|
-
expect(costs[7]).toEqual([Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity]);
|
|
536
|
-
expect(costs[8]).toEqual([173, 185, 170, 187, 205, Infinity, 97, Infinity, 176]);
|
|
537
|
-
|
|
538
|
-
expect(predecessor).toBeInstanceOf(Array);
|
|
539
|
-
expect(predecessor.length).toBe(9);
|
|
540
|
-
expect(predecessor[0]).toEqual([vertex2, null, vertex2, null, vertex3, null, vertex4, null, null]);
|
|
541
|
-
expect(predecessor[1]).toEqual([null, vertex1, null, vertex1, vertex3, null, vertex4, null, vertex1]);
|
|
542
|
-
expect(predecessor[5]).toEqual([null, null, null, null, null, null, null, null, null]);
|
|
543
|
-
expect(predecessor[7]).toEqual([null, null, null, null, null, null, null, null, null]);
|
|
544
|
-
expect(predecessor[8]).toEqual([vertex7, vertex7, vertex7, vertex7, vertex7, null, null, null, vertex7]);
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
const dijkstraRes12tt = myGraph.dijkstra(1, 2, true, true);
|
|
548
|
-
expect(dijkstraRes12tt).toBeTruthy();
|
|
549
|
-
if (dijkstraRes12tt) {
|
|
550
|
-
const {distMap, minDist, minPath, paths, preMap, seen} = dijkstraRes12tt;
|
|
551
|
-
expect(distMap).toBeInstanceOf(Map);
|
|
552
|
-
expect(distMap.size).toBe(9);
|
|
553
|
-
expect(distMap.get(vertex1)).toBe(0);
|
|
554
|
-
expect(distMap.get(vertex2)).toBe(12);
|
|
555
|
-
expect(distMap.get(vertex3)).toBe(Infinity);
|
|
556
|
-
expect(distMap.get(vertex4)).toBe(14);
|
|
557
|
-
expect(distMap.get(vertex5)).toBe(Infinity);
|
|
558
|
-
expect(distMap.get(vertex6)).toBe(Infinity);
|
|
559
|
-
expect(distMap.get(vertex7)).toBe(Infinity);
|
|
560
|
-
expect(distMap.get(vertex8)).toBe(Infinity);
|
|
561
|
-
expect(distMap.get(vertex9)).toBe(19);
|
|
562
|
-
|
|
563
|
-
expect(minDist).toBe(12);
|
|
564
|
-
expect(minPath).toBeInstanceOf(Array);
|
|
565
|
-
expect(minPath.length).toBe(2);
|
|
566
|
-
expect(minPath[0]).toBe(vertex1);
|
|
567
|
-
expect(minPath[1]).toBe(vertex2);
|
|
568
|
-
|
|
569
|
-
expect(paths).toBeInstanceOf(Array);
|
|
570
|
-
expect(paths.length).toBe(9);
|
|
571
|
-
expect(paths[0]).toBeInstanceOf(Array);
|
|
572
|
-
expect(paths[0][0]).toBe(vertex1);
|
|
573
|
-
|
|
574
|
-
expect(paths[1]).toBeInstanceOf(Array);
|
|
575
|
-
expect(paths[1][0]).toBe(vertex1);
|
|
576
|
-
expect(paths[1][1]).toBe(vertex2);
|
|
577
|
-
|
|
578
|
-
expect(paths[2]).toBeInstanceOf(Array);
|
|
579
|
-
expect(paths[2][0]).toBe(vertex3);
|
|
580
|
-
expect(paths[3]).toBeInstanceOf(Array);
|
|
581
|
-
expect(paths[3][0]).toBe(vertex1);
|
|
582
|
-
expect(paths[3][1]).toBe(vertex4);
|
|
583
|
-
expect(paths[4]).toBeInstanceOf(Array);
|
|
584
|
-
expect(paths[4][0]).toBe(vertex5);
|
|
585
|
-
|
|
586
|
-
expect(paths[5]).toBeInstanceOf(Array);
|
|
587
|
-
expect(paths[5][0]).toBe(vertex6);
|
|
588
|
-
expect(paths[6]).toBeInstanceOf(Array);
|
|
589
|
-
expect(paths[6][0]).toBe(vertex7);
|
|
590
|
-
expect(paths[7]).toBeInstanceOf(Array);
|
|
591
|
-
expect(paths[7][0]).toBe(vertex8);
|
|
592
|
-
expect(paths[8]).toBeInstanceOf(Array);
|
|
593
|
-
expect(paths[8][0]).toBe(vertex1);
|
|
594
|
-
expect(paths[8][1]).toBe(vertex9);
|
|
595
|
-
}
|
|
596
|
-
});
|
|
597
|
-
});
|
|
598
|
-
```
|
|
599
|
-
|
|
600
|
-
## API docs
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
[//]: # ([Examples Repository](https://github.com/zrwusa/data-structure-typed-examples))
|
|
604
|
-
|
|
605
|
-
<a href="https://github.com/zrwusa/data-structure-typed-examples" target="_blank">Examples Repository</a>
|
|
606
384
|
|
|
607
385
|
# Why
|
|
608
386
|
|
|
@@ -875,29 +653,40 @@ describe('DirectedGraph Test3', () => {
|
|
|
875
653
|
|
|
876
654
|

|
|
877
655
|
|
|
878
|
-
![]
|
|
656
|
+
[//]: # ()
|
|
879
657
|
|
|
880
|
-
|
|
658
|
+
[//]: # ()
|
|
659
|
+
[//]: # ()
|
|
881
660
|
|
|
882
|
-
|
|
661
|
+
[//]: # ()
|
|
662
|
+
[//]: # ()
|
|
883
663
|
|
|
884
|
-
|
|
664
|
+
[//]: # ()
|
|
665
|
+
[//]: # ()
|
|
885
666
|
|
|
886
|
-
|
|
667
|
+
[//]: # ()
|
|
668
|
+
[//]: # ()
|
|
887
669
|
|
|
888
|
-
|
|
670
|
+
[//]: # ()
|
|
671
|
+
[//]: # ()
|
|
889
672
|
|
|
890
|
-
|
|
673
|
+
[//]: # ()
|
|
674
|
+
[//]: # ()
|
|
891
675
|
|
|
892
|
-
|
|
676
|
+
[//]: # ()
|
|
677
|
+
[//]: # ()
|
|
893
678
|
|
|
894
|
-
|
|
679
|
+
[//]: # ()
|
|
680
|
+
[//]: # ()
|
|
895
681
|
|
|
896
|
-
|
|
682
|
+
[//]: # ()
|
|
683
|
+
[//]: # ()
|
|
897
684
|
|
|
898
|
-
|
|
685
|
+
[//]: # ()
|
|
686
|
+
[//]: # ()
|
|
899
687
|
|
|
900
|
-
|
|
688
|
+
[//]: # ()
|
|
689
|
+
[//]: # ()
|
|
901
690
|
|
|
902
691
|
[//]: # ()
|
|
903
692
|
|