data-structure-typed 0.8.18 → 1.12.9
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/.dependency-cruiser.js +449 -0
- package/.idea/data-structure-typed.iml +2 -0
- package/.idea/modules.xml +1 -1
- package/README.md +298 -2
- package/dist/data-structures/binary-tree/aa-tree.js +5 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +58 -5
- package/dist/data-structures/binary-tree/avl-tree.js +150 -46
- package/dist/data-structures/binary-tree/b-tree.js +5 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +28 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +41 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +230 -36
- package/dist/data-structures/binary-tree/binary-tree.js +747 -369
- package/dist/data-structures/binary-tree/bst.d.ts +20 -8
- package/dist/data-structures/binary-tree/bst.js +164 -107
- package/dist/data-structures/binary-tree/rb-tree.js +5 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -3
- package/dist/data-structures/binary-tree/segment-tree.js +95 -61
- package/dist/data-structures/binary-tree/splay-tree.js +5 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +5 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +35 -11
- package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +168 -46
- package/dist/data-structures/graph/abstract-graph.js +712 -323
- package/dist/data-structures/graph/directed-graph.d.ts +114 -12
- package/dist/data-structures/graph/directed-graph.js +372 -128
- package/dist/data-structures/graph/undirected-graph.d.ts +67 -3
- package/dist/data-structures/graph/undirected-graph.js +233 -81
- package/dist/data-structures/hash/coordinate-map.d.ts +33 -1
- package/dist/data-structures/hash/coordinate-map.js +70 -20
- package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
- package/dist/data-structures/hash/coordinate-set.js +58 -15
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/heap/heap.d.ts +26 -37
- package/dist/data-structures/heap/heap.js +56 -60
- package/dist/data-structures/heap/max-heap.d.ts +8 -2
- package/dist/data-structures/heap/max-heap.js +32 -9
- package/dist/data-structures/heap/min-heap.d.ts +9 -2
- package/dist/data-structures/heap/min-heap.js +33 -9
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -7
- package/dist/data-structures/linked-list/doubly-linked-list.js +101 -61
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -19
- package/dist/data-structures/linked-list/singly-linked-list.js +312 -174
- package/dist/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/data-structures/matrix/matrix.js +19 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +84 -4
- package/dist/data-structures/matrix/matrix2d.js +158 -61
- package/dist/data-structures/matrix/navigator.d.ts +34 -16
- package/dist/data-structures/matrix/navigator.js +65 -18
- package/dist/data-structures/matrix/vector2d.d.ts +153 -29
- package/dist/data-structures/matrix/vector2d.js +249 -102
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/priority-queue.d.ts +145 -21
- package/dist/data-structures/priority-queue/priority-queue.js +285 -116
- package/dist/data-structures/queue/deque.d.ts +69 -0
- package/dist/data-structures/queue/deque.js +151 -56
- package/dist/data-structures/queue/queue.d.ts +34 -37
- package/dist/data-structures/queue/queue.js +59 -61
- package/dist/data-structures/stack/stack.d.ts +29 -35
- package/dist/data-structures/stack/stack.js +51 -56
- package/dist/data-structures/trie/trie.d.ts +36 -6
- package/dist/data-structures/trie/trie.js +256 -83
- package/dist/data-structures/types/abstract-graph.d.ts +29 -0
- package/dist/data-structures/types/abstract-graph.js +2 -0
- package/dist/data-structures/types/avl-tree.d.ts +5 -0
- package/dist/data-structures/types/avl-tree.js +2 -0
- package/dist/data-structures/types/binary-tree.d.ts +16 -0
- package/dist/data-structures/types/binary-tree.js +2 -0
- package/dist/data-structures/types/bst.d.ts +7 -0
- package/dist/data-structures/types/bst.js +2 -0
- package/dist/data-structures/types/directed-graph.d.ts +10 -0
- package/dist/data-structures/types/directed-graph.js +2 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/types/doubly-linked-list.js +2 -0
- package/dist/data-structures/types/heap.d.ts +7 -0
- package/dist/data-structures/types/heap.js +2 -0
- package/dist/data-structures/types/index.d.ts +13 -0
- package/dist/data-structures/types/index.js +29 -0
- package/dist/data-structures/types/navigator.d.ts +14 -0
- package/dist/data-structures/types/navigator.js +2 -0
- package/dist/data-structures/types/priority-queue.d.ts +7 -0
- package/dist/data-structures/types/priority-queue.js +2 -0
- package/dist/data-structures/types/segment-tree.d.ts +1 -0
- package/dist/data-structures/types/segment-tree.js +2 -0
- package/dist/data-structures/types/singly-linked-list.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -0
- package/dist/data-structures/types/tree-multiset.js +2 -0
- package/dist/utils/trampoline.d.ts +14 -0
- package/dist/utils/trampoline.js +130 -0
- package/dist/utils/types/index.js +17 -0
- package/dist/{types → utils}/types/utils.d.ts +15 -1
- package/dist/{types → utils/types}/utils.js +21 -19
- package/dist/{utils.d.ts → utils/utils.d.ts} +5 -22
- package/dist/utils/utils.js +651 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +85 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +423 -0
- package/docs/classes/AaTree.html +117 -0
- package/docs/classes/AbstractEdge.html +198 -0
- package/docs/classes/AbstractGraph.html +891 -0
- package/docs/classes/AbstractVertex.html +164 -0
- package/docs/classes/ArrayDeque.html +384 -0
- package/docs/classes/BST.html +1893 -0
- package/docs/classes/BSTNode.html +425 -0
- package/docs/classes/BTree.html +117 -0
- package/docs/classes/BinaryIndexedTree.html +244 -0
- package/docs/classes/BinaryTree.html +1754 -0
- package/docs/classes/BinaryTreeNode.html +396 -0
- package/docs/classes/Character.html +165 -0
- package/docs/classes/CoordinateMap.html +394 -0
- package/docs/classes/CoordinateSet.html +355 -0
- package/docs/classes/Deque.html +617 -0
- package/docs/classes/DirectedEdge.html +247 -0
- package/docs/classes/DirectedGraph.html +1207 -0
- package/docs/classes/DirectedVertex.html +154 -0
- package/docs/classes/DoublyLinkedList.html +619 -0
- package/docs/classes/DoublyLinkedListNode.html +160 -0
- package/docs/classes/Heap.html +315 -0
- package/docs/classes/Matrix2D.html +447 -0
- package/docs/classes/MatrixNTI2D.html +181 -0
- package/docs/classes/MaxHeap.html +325 -0
- package/docs/classes/MaxPriorityQueue.html +668 -0
- package/docs/classes/MinHeap.html +326 -0
- package/docs/classes/MinPriorityQueue.html +668 -0
- package/docs/classes/Navigator.html +285 -0
- package/docs/classes/ObjectDeque.html +289 -0
- package/docs/classes/PriorityQueue.html +643 -0
- package/docs/classes/Queue.html +337 -0
- package/docs/classes/RBTree.html +117 -0
- package/docs/classes/SegmentTree.html +234 -0
- package/docs/classes/SegmentTreeNode.html +302 -0
- package/docs/classes/SinglyLinkedList.html +1035 -0
- package/docs/classes/SinglyLinkedListNode.html +304 -0
- package/docs/classes/SplayTree.html +117 -0
- package/docs/classes/Stack.html +313 -0
- package/docs/classes/TreeMultiSet.html +1897 -0
- package/docs/classes/Trie.html +317 -0
- package/docs/classes/TrieNode.html +221 -0
- package/docs/classes/TwoThreeTree.html +117 -0
- package/docs/classes/UndirectedEdge.html +220 -0
- package/docs/classes/UndirectedGraph.html +1006 -0
- package/docs/classes/UndirectedVertex.html +154 -0
- package/docs/classes/Vector2D.html +746 -0
- package/docs/enums/CP.html +126 -0
- package/docs/enums/FamilyPosition.html +126 -0
- package/docs/enums/LoopType.html +119 -0
- package/docs/index.html +288 -0
- package/docs/modules.html +146 -0
- package/jest.config.js +5 -0
- package/package.json +33 -47
- package/rename_clear_files.sh +29 -0
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/data-structures/binary-tree/avl-tree.ts +58 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +31 -4
- package/src/data-structures/binary-tree/binary-tree.ts +460 -145
- package/src/data-structures/binary-tree/bst.ts +31 -25
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/segment-tree.ts +25 -12
- package/src/data-structures/binary-tree/tree-multiset.ts +5 -4
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +224 -108
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +132 -26
- package/src/data-structures/graph/undirected-graph.ts +78 -11
- package/src/data-structures/hash/coordinate-map.ts +33 -1
- package/src/data-structures/hash/coordinate-set.ts +25 -0
- package/src/data-structures/hash/index.ts +5 -0
- package/src/data-structures/heap/heap.ts +27 -41
- package/src/data-structures/heap/max-heap.ts +8 -2
- package/src/data-structures/heap/min-heap.ts +9 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -17
- package/src/data-structures/linked-list/singly-linked-list.ts +56 -39
- package/src/data-structures/matrix/matrix.ts +11 -0
- package/src/data-structures/matrix/matrix2d.ts +90 -10
- package/src/data-structures/matrix/navigator.ts +34 -14
- package/src/data-structures/matrix/vector2d.ts +187 -63
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/priority-queue.ts +200 -78
- package/src/data-structures/queue/deque.ts +71 -2
- package/src/data-structures/queue/queue.ts +37 -40
- package/src/data-structures/stack/stack.ts +32 -38
- package/src/data-structures/trie/trie.ts +83 -14
- package/src/data-structures/types/abstract-graph.ts +51 -0
- package/src/data-structures/types/avl-tree.ts +6 -0
- package/src/data-structures/types/binary-tree.ts +15 -0
- package/src/data-structures/types/bst.ts +5 -0
- package/src/data-structures/types/directed-graph.ts +18 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +8 -0
- package/src/data-structures/types/index.ts +13 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/trampoline.ts +51 -0
- package/src/utils/types/index.ts +1 -0
- package/src/{types → utils/types}/utils.ts +27 -5
- package/src/{utils.ts → utils/utils.ts} +41 -131
- package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
- package/{dist/types/data-structures/graph/index.d.ts → tests/unit/data-structures/graph/index.ts} +1 -1
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
- package/tsconfig.json +9 -6
- package/dist/data-structures/trampoline.d.ts +0 -25
- package/dist/data-structures/trampoline.js +0 -52
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/pair.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/index.d.ts +0 -9
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/utils.d.ts +0 -46
- package/dist/utils.js +0 -569
- package/src/data-structures/trampoline.ts +0 -91
- package/src/types/index.ts +0 -1
- /package/dist/{types/data-structures/hash/hash-table.d.ts → data-structures/types/singly-linked-list.d.ts} +0 -0
- /package/dist/{types → utils}/index.d.ts +0 -0
- /package/dist/{types → utils}/index.js +0 -0
- /package/dist/{types → utils}/types/index.d.ts +0 -0
- /package/{src/types/patches/index.d.ts → tests/unit/data-structures/graph/abstract-graph.ts} +0 -0
|
@@ -1,42 +1,50 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license MIT
|
|
3
|
-
* @copyright
|
|
4
|
-
*
|
|
3
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
5
4
|
* @class
|
|
6
5
|
*/
|
|
7
6
|
export class Stack<T> {
|
|
8
7
|
protected _elements: T[];
|
|
9
8
|
|
|
10
9
|
/**
|
|
11
|
-
*
|
|
12
|
-
* @param {
|
|
10
|
+
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
11
|
+
* @param {T[]} [elements] - The `elements` parameter is an optional parameter of type `T[]`, which represents an array
|
|
12
|
+
* of elements of type `T`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
13
|
+
* is provided and is an array, it is assigned to the `_elements
|
|
13
14
|
*/
|
|
14
15
|
constructor(elements?: T[]) {
|
|
15
16
|
this._elements = Array.isArray(elements) ? elements : [];
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
|
-
*
|
|
20
|
-
* @
|
|
21
|
-
* @returns {
|
|
20
|
+
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
21
|
+
* @param {T[]} elements - The `elements` parameter is an array of elements of type `T`.
|
|
22
|
+
* @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
|
|
23
|
+
* array.
|
|
24
|
+
*/
|
|
25
|
+
static fromArray<T>(elements: T[]): Stack<T> {
|
|
26
|
+
return new Stack(elements);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The function checks if an array is empty and returns a boolean value.
|
|
31
|
+
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
22
32
|
*/
|
|
23
33
|
isEmpty(): boolean {
|
|
24
34
|
return this._elements.length === 0;
|
|
25
35
|
}
|
|
26
36
|
|
|
27
37
|
/**
|
|
28
|
-
*
|
|
29
|
-
* @
|
|
30
|
-
* @returns {number}
|
|
38
|
+
* The size() function returns the number of elements in an array.
|
|
39
|
+
* @returns The size of the elements array.
|
|
31
40
|
*/
|
|
32
41
|
size(): number {
|
|
33
42
|
return this._elements.length;
|
|
34
43
|
}
|
|
35
44
|
|
|
36
45
|
/**
|
|
37
|
-
*
|
|
38
|
-
* @
|
|
39
|
-
* @returns {object}
|
|
46
|
+
* The `peek` function returns the last element of an array, or null if the array is empty.
|
|
47
|
+
* @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
|
|
40
48
|
*/
|
|
41
49
|
peek(): T | null {
|
|
42
50
|
if (this.isEmpty()) return null;
|
|
@@ -45,9 +53,9 @@ export class Stack<T> {
|
|
|
45
53
|
}
|
|
46
54
|
|
|
47
55
|
/**
|
|
48
|
-
*
|
|
49
|
-
* @
|
|
50
|
-
* @
|
|
56
|
+
* The push function adds an element to the stack and returns the updated stack.
|
|
57
|
+
* @param {T} element - The parameter "element" is of type T, which means it can be any data type.
|
|
58
|
+
* @returns The `push` method is returning the updated `Stack<T>` object.
|
|
51
59
|
*/
|
|
52
60
|
push(element: T): Stack<T> {
|
|
53
61
|
this._elements.push(element);
|
|
@@ -55,9 +63,9 @@ export class Stack<T> {
|
|
|
55
63
|
}
|
|
56
64
|
|
|
57
65
|
/**
|
|
58
|
-
*
|
|
59
|
-
* @
|
|
60
|
-
*
|
|
66
|
+
* The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
|
|
67
|
+
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
68
|
+
* array is empty, it returns `null`.
|
|
61
69
|
*/
|
|
62
70
|
pop(): T | null {
|
|
63
71
|
if (this.isEmpty()) return null;
|
|
@@ -66,39 +74,25 @@ export class Stack<T> {
|
|
|
66
74
|
}
|
|
67
75
|
|
|
68
76
|
/**
|
|
69
|
-
*
|
|
70
|
-
* @
|
|
71
|
-
* @returns {array}
|
|
77
|
+
* The toArray function returns a copy of the elements in an array.
|
|
78
|
+
* @returns An array of type T.
|
|
72
79
|
*/
|
|
73
80
|
toArray(): T[] {
|
|
74
81
|
return this._elements.slice();
|
|
75
82
|
}
|
|
76
83
|
|
|
77
84
|
/**
|
|
78
|
-
*
|
|
79
|
-
* @public
|
|
85
|
+
* The clear function clears the elements array.
|
|
80
86
|
*/
|
|
81
87
|
clear(): void {
|
|
82
88
|
this._elements = [];
|
|
83
89
|
}
|
|
84
90
|
|
|
85
91
|
/**
|
|
86
|
-
*
|
|
87
|
-
* @
|
|
88
|
-
* @return {Stack}
|
|
92
|
+
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
93
|
+
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
89
94
|
*/
|
|
90
95
|
clone(): Stack<T> {
|
|
91
96
|
return new Stack(this._elements.slice());
|
|
92
97
|
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Creates a stack from an existing array
|
|
96
|
-
* @public
|
|
97
|
-
* @static
|
|
98
|
-
* @param {array} [elements]
|
|
99
|
-
* @return {Stack}
|
|
100
|
-
*/
|
|
101
|
-
static fromArray<T>(elements: T[]): Stack<T> {
|
|
102
|
-
return new Stack(elements);
|
|
103
|
-
}
|
|
104
98
|
}
|
|
@@ -1,5 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
export class TrieNode {
|
|
2
|
-
protected
|
|
6
|
+
protected _value;
|
|
7
|
+
|
|
8
|
+
constructor(v: string) {
|
|
9
|
+
this._value = v;
|
|
10
|
+
this._isEnd = false;
|
|
11
|
+
this._children = new Map<string, TrieNode>();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected _children: Map<string, TrieNode>;
|
|
3
15
|
|
|
4
16
|
get children(): Map<string, TrieNode> {
|
|
5
17
|
return this._children;
|
|
@@ -9,7 +21,7 @@ export class TrieNode {
|
|
|
9
21
|
this._children = v;
|
|
10
22
|
}
|
|
11
23
|
|
|
12
|
-
protected _isEnd
|
|
24
|
+
protected _isEnd: boolean;
|
|
13
25
|
|
|
14
26
|
get isEnd(): boolean {
|
|
15
27
|
return this._isEnd;
|
|
@@ -18,10 +30,28 @@ export class TrieNode {
|
|
|
18
30
|
set isEnd(v: boolean) {
|
|
19
31
|
this._isEnd = v;
|
|
20
32
|
}
|
|
33
|
+
|
|
34
|
+
get val(): string {
|
|
35
|
+
return this._value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
set val(v: string) {
|
|
39
|
+
this._value = v;
|
|
40
|
+
}
|
|
21
41
|
}
|
|
22
42
|
|
|
23
43
|
export class Trie {
|
|
44
|
+
constructor(words?: string[]) {
|
|
45
|
+
this._root = new TrieNode('');
|
|
46
|
+
if (words) {
|
|
47
|
+
for (const i of words) {
|
|
48
|
+
this.put(i);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
24
53
|
protected _root: TrieNode;
|
|
54
|
+
|
|
25
55
|
get root() {
|
|
26
56
|
return this._root;
|
|
27
57
|
}
|
|
@@ -30,16 +60,12 @@ export class Trie {
|
|
|
30
60
|
this._root = v;
|
|
31
61
|
}
|
|
32
62
|
|
|
33
|
-
|
|
34
|
-
this._root = new TrieNode();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
put(input: string): boolean {
|
|
63
|
+
put(word: string): boolean {
|
|
38
64
|
let cur = this._root;
|
|
39
|
-
for (const c of
|
|
65
|
+
for (const c of word) {
|
|
40
66
|
let nodeC = cur.children.get(c);
|
|
41
67
|
if (!nodeC) {
|
|
42
|
-
nodeC = new TrieNode();
|
|
68
|
+
nodeC = new TrieNode(c);
|
|
43
69
|
cur.children.set(c, nodeC);
|
|
44
70
|
}
|
|
45
71
|
cur = nodeC;
|
|
@@ -48,7 +74,6 @@ export class Trie {
|
|
|
48
74
|
return true;
|
|
49
75
|
}
|
|
50
76
|
|
|
51
|
-
|
|
52
77
|
has(input: string): boolean {
|
|
53
78
|
let cur = this._root;
|
|
54
79
|
for (const c of input) {
|
|
@@ -93,8 +118,9 @@ export class Trie {
|
|
|
93
118
|
|
|
94
119
|
// --- start additional methods ---
|
|
95
120
|
/**
|
|
96
|
-
* Only can present as a prefix, not a word
|
|
97
|
-
* @param input
|
|
121
|
+
* The function checks if a given input string has an absolute prefix in a tree data structure.Only can present as a prefix, not a word
|
|
122
|
+
* @param {string} input - The input parameter is a string that represents the input value for the function.
|
|
123
|
+
* @returns a boolean value.
|
|
98
124
|
*/
|
|
99
125
|
isAbsPrefix(input: string): boolean {
|
|
100
126
|
let cur = this._root;
|
|
@@ -107,8 +133,9 @@ export class Trie {
|
|
|
107
133
|
}
|
|
108
134
|
|
|
109
135
|
/**
|
|
110
|
-
* Can present as a prefix or word
|
|
111
|
-
* @param input
|
|
136
|
+
* The function checks if a given input string is a prefix of any existing string in a tree structure.Can present as a abs prefix or word
|
|
137
|
+
* @param {string} input - The input parameter is a string that represents the prefix we want to check.
|
|
138
|
+
* @returns a boolean value.
|
|
112
139
|
*/
|
|
113
140
|
isPrefix(input: string): boolean {
|
|
114
141
|
let cur = this._root;
|
|
@@ -120,7 +147,49 @@ export class Trie {
|
|
|
120
147
|
return true;
|
|
121
148
|
}
|
|
122
149
|
|
|
150
|
+
/**
|
|
151
|
+
* The function checks if the input string is a common prefix in a Trie data structure.Check if the input string is the common prefix of all the words
|
|
152
|
+
* @param {string} input - The input parameter is a string that represents the common prefix that we want to check for
|
|
153
|
+
* in the Trie data structure.
|
|
154
|
+
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
155
|
+
*/
|
|
156
|
+
isCommonPrefix(input: string): boolean {
|
|
157
|
+
let commonPre = '';
|
|
158
|
+
const dfs = (cur: TrieNode) => {
|
|
159
|
+
commonPre += cur.val;
|
|
160
|
+
if (commonPre === input) return;
|
|
161
|
+
if (cur.isEnd) return;
|
|
162
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
163
|
+
else return;
|
|
164
|
+
}
|
|
165
|
+
dfs(this._root);
|
|
166
|
+
return commonPre === input;
|
|
167
|
+
}
|
|
123
168
|
|
|
169
|
+
/**
|
|
170
|
+
* The function `getLongestCommonPrefix` returns the longest common prefix among all the words stored in a Trie data
|
|
171
|
+
* structure.
|
|
172
|
+
* @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
|
|
173
|
+
* Trie.
|
|
174
|
+
*/
|
|
175
|
+
getLongestCommonPrefix(): string {
|
|
176
|
+
let commonPre = '';
|
|
177
|
+
const dfs = (cur: TrieNode) => {
|
|
178
|
+
commonPre += cur.val;
|
|
179
|
+
if (cur.isEnd) return;
|
|
180
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
181
|
+
else return;
|
|
182
|
+
}
|
|
183
|
+
dfs(this._root);
|
|
184
|
+
return commonPre;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
189
|
+
* @param [prefix] - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
190
|
+
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
191
|
+
* @returns an array of strings.
|
|
192
|
+
*/
|
|
124
193
|
getAll(prefix = ''): string[] {
|
|
125
194
|
const words: string[] = [];
|
|
126
195
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export type VertexId = string | number;
|
|
2
|
+
export type DijkstraResult<V> =
|
|
3
|
+
{ distMap: Map<V, number>, preMap: Map<V, V | null>, seen: Set<V>, paths: V[][], minDist: number, minPath: V[] }
|
|
4
|
+
| null;
|
|
5
|
+
|
|
6
|
+
export interface IGraph<V, E> {
|
|
7
|
+
|
|
8
|
+
containsVertex(vertexOrId: V | VertexId): boolean;
|
|
9
|
+
|
|
10
|
+
getVertex(vertexOrId: VertexId | V): V | null;
|
|
11
|
+
|
|
12
|
+
getVertexId(vertexOrId: V | VertexId): VertexId;
|
|
13
|
+
|
|
14
|
+
vertexSet(): Map<VertexId, V>;
|
|
15
|
+
|
|
16
|
+
addVertex(v: V): boolean;
|
|
17
|
+
|
|
18
|
+
removeVertex(vertexOrId: V | VertexId): boolean;
|
|
19
|
+
|
|
20
|
+
removeAllVertices(vertices: V[] | VertexId[]): boolean;
|
|
21
|
+
|
|
22
|
+
degreeOf(vertexOrId: V | VertexId): number;
|
|
23
|
+
|
|
24
|
+
edgesOf(vertexOrId: V | VertexId): E[];
|
|
25
|
+
|
|
26
|
+
containsEdge(src: V | VertexId, dest: V | VertexId): boolean;
|
|
27
|
+
|
|
28
|
+
// containsEdge(e: E): boolean;
|
|
29
|
+
|
|
30
|
+
getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
31
|
+
|
|
32
|
+
// getAllEdges(src: V, dest: V): E[];
|
|
33
|
+
|
|
34
|
+
edgeSet(): E[];
|
|
35
|
+
|
|
36
|
+
addEdge(edge: E): boolean;
|
|
37
|
+
|
|
38
|
+
removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
|
|
39
|
+
|
|
40
|
+
removeEdge(edge: E): E | null;
|
|
41
|
+
|
|
42
|
+
// removeAllEdges(v1: VertexId | V, v2: VertexId | V): (E | null)[];
|
|
43
|
+
|
|
44
|
+
// removeAllEdges(edges: E[] | [VertexId, VertexId]): boolean;
|
|
45
|
+
|
|
46
|
+
setEdgeWeight(srcOrId: V | VertexId, destOrId: V | VertexId, weight: number): boolean;
|
|
47
|
+
|
|
48
|
+
getMinPathBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): V[] | null;
|
|
49
|
+
|
|
50
|
+
getNeighbors(vertexOrId: V | VertexId): V[];
|
|
51
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {BinaryTreeNode} from '../binary-tree';
|
|
2
|
+
|
|
3
|
+
export type BinaryTreeNodePropertyName = 'id' | 'val' | 'count';
|
|
4
|
+
export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
|
|
5
|
+
export type DFSOrderPattern = 'in' | 'pre' | 'post';
|
|
6
|
+
export type BinaryTreeNodeId = number;
|
|
7
|
+
export type BinaryTreeDeleted<T> = { deleted: BinaryTreeNode<T> | null | undefined, needBalanced: BinaryTreeNode<T> | null };
|
|
8
|
+
export type ResultByProperty<T> = T | BinaryTreeNode<T> | number | BinaryTreeNodeId;
|
|
9
|
+
export type ResultsByProperty<T> = ResultByProperty<T>[];
|
|
10
|
+
|
|
11
|
+
export interface BinaryTreeNodeObj<T> {
|
|
12
|
+
id: BinaryTreeNodeId;
|
|
13
|
+
val: T;
|
|
14
|
+
count?: number;
|
|
15
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import {BSTNode} from '../binary-tree';
|
|
2
|
+
import type {BinaryTreeNodeId} from './binary-tree';
|
|
3
|
+
|
|
4
|
+
export type BSTComparator = (a: BinaryTreeNodeId, b: BinaryTreeNodeId) => number;
|
|
5
|
+
export type BSTDeletedResult<T> = { deleted: BSTNode<T> | null, needBalanced: BSTNode<T> | null };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {VertexId} from './abstract-graph';
|
|
2
|
+
|
|
3
|
+
export interface IDirectedGraph<V, E> {
|
|
4
|
+
incomingEdgesOf(vertex: V): E[];
|
|
5
|
+
|
|
6
|
+
outgoingEdgesOf(vertex: V): E[];
|
|
7
|
+
|
|
8
|
+
inDegreeOf(vertexOrId: V | VertexId): number;
|
|
9
|
+
|
|
10
|
+
outDegreeOf(vertexOrId: V | VertexId): number;
|
|
11
|
+
|
|
12
|
+
getEdgeSrc(e: E): V | null;
|
|
13
|
+
|
|
14
|
+
getEdgeDest(e: E): V | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 0 means unknown, 1 means visiting, 2 means visited;
|
|
18
|
+
export type TopologicalStatus = 0 | 1 | 2;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type DoublyLinkedListGetBy = 'node' | 'val';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './binary-tree';
|
|
2
|
+
export * from './bst';
|
|
3
|
+
export * from './avl-tree';
|
|
4
|
+
export * from './segment-tree';
|
|
5
|
+
export * from './tree-multiset';
|
|
6
|
+
export * from './abstract-graph';
|
|
7
|
+
export * from './directed-graph';
|
|
8
|
+
export * from './priority-queue';
|
|
9
|
+
export * from './heap';
|
|
10
|
+
export * from './singly-linked-list';
|
|
11
|
+
export * from './doubly-linked-list';
|
|
12
|
+
export * from './navigator';
|
|
13
|
+
export * from '../../utils/types/utils';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type Direction = 'up' | 'right' | 'down' | 'left';
|
|
2
|
+
export type Turning = { [key in Direction]: Direction };
|
|
3
|
+
|
|
4
|
+
export interface NavigatorParams<T> {
|
|
5
|
+
matrix: T[][],
|
|
6
|
+
turning: Turning,
|
|
7
|
+
onMove: (cur: [number, number]) => void
|
|
8
|
+
init: {
|
|
9
|
+
cur: [number, number],
|
|
10
|
+
charDir: Direction,
|
|
11
|
+
VISITED: T,
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type SegmentTreeNodeVal = number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
import {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types';
|
|
6
|
+
|
|
7
|
+
export const THUNK_SYMBOL = Symbol('thunk')
|
|
8
|
+
|
|
9
|
+
export const isThunk = (fnOrValue: any) => {
|
|
10
|
+
return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const toThunk = (fn: ToThunkFn): Thunk => {
|
|
14
|
+
const thunk = () => fn()
|
|
15
|
+
thunk.__THUNK__ = THUNK_SYMBOL
|
|
16
|
+
return thunk
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const trampoline = (fn: TrlFn) => {
|
|
20
|
+
const cont = (...args: [...Parameters<TrlFn>]) => toThunk(() => fn(...args))
|
|
21
|
+
|
|
22
|
+
return Object.assign(
|
|
23
|
+
(...args: [...Parameters<TrlFn>]) => {
|
|
24
|
+
let result = fn(...args)
|
|
25
|
+
|
|
26
|
+
while (isThunk(result) && typeof result === 'function') {
|
|
27
|
+
result = result()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return result
|
|
31
|
+
},
|
|
32
|
+
{cont}
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const trampolineAsync = (fn: TrlAsyncFn) => {
|
|
37
|
+
const cont = (...args: [...Parameters<TrlAsyncFn>]) => toThunk(() => fn(...args))
|
|
38
|
+
|
|
39
|
+
return Object.assign(
|
|
40
|
+
async (...args: [...Parameters<TrlAsyncFn>]) => {
|
|
41
|
+
let result = await fn(...args)
|
|
42
|
+
|
|
43
|
+
while (isThunk(result) && typeof result === 'function') {
|
|
44
|
+
result = await result()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return result
|
|
48
|
+
},
|
|
49
|
+
{cont}
|
|
50
|
+
)
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils';
|
|
@@ -74,9 +74,9 @@ export type DebounceOptions = {
|
|
|
74
74
|
};
|
|
75
75
|
|
|
76
76
|
export interface DebouncedFunction<F extends Procedure> {
|
|
77
|
-
(this: ThisParameterType<F>, ...args: Parameters<F>): void;
|
|
78
|
-
|
|
79
77
|
cancel: () => void;
|
|
78
|
+
|
|
79
|
+
(this: ThisParameterType<F>, ...args: [...Parameters<F>]): void;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
export type MonthKey =
|
|
@@ -123,10 +123,10 @@ export class TreeNode<T> {
|
|
|
123
123
|
if (!this.children) {
|
|
124
124
|
this.children = [];
|
|
125
125
|
}
|
|
126
|
-
if (children instanceof
|
|
127
|
-
this.children = this.children.concat(children);
|
|
128
|
-
} else {
|
|
126
|
+
if (children instanceof TreeNode) {
|
|
129
127
|
this.children.push(children);
|
|
128
|
+
} else {
|
|
129
|
+
this.children = this.children.concat(children);
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
|
|
@@ -155,4 +155,26 @@ export class TreeNode<T> {
|
|
|
155
155
|
|
|
156
156
|
export type OrderType = 'InOrder' | 'PreOrder' | 'PostOrder'
|
|
157
157
|
|
|
158
|
+
export type DeepProxy<T> = T extends (...args: any[]) => infer R
|
|
159
|
+
? (...args: [...Parameters<T>]) => DeepProxy<R>
|
|
160
|
+
: T extends object
|
|
161
|
+
? { [K in keyof T]: DeepProxy<T[K]> }
|
|
162
|
+
: T;
|
|
163
|
+
|
|
164
|
+
export type DeepProxyOnChange = (target: any, property: string | symbol, value: any, receiver: any, descriptor: any, result: any) => void;
|
|
165
|
+
|
|
166
|
+
export type DeepProxyOnGet = (target: any, property: string | symbol, value: any, receiver: any, descriptor: any, result: any) => void;
|
|
167
|
+
|
|
168
|
+
export type CurryFunc<T> = T extends (...args: infer Args) => infer R
|
|
169
|
+
? Args extends [infer Arg, ...infer RestArgs]
|
|
170
|
+
? (arg: Arg) => CurryFunc<(...args: RestArgs) => R>
|
|
171
|
+
: R
|
|
172
|
+
: T;
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
176
|
+
const THUNK_SYMBOL = Symbol('thunk')
|
|
177
|
+
export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: typeof THUNK_SYMBOL };
|
|
178
|
+
export type TrlFn = (...args: any[]) => any;
|
|
179
|
+
export type TrlAsyncFn = (...args: any[]) => any;
|
|
158
180
|
|