data-structure-typed 1.15.1 → 1.16.1
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 +398 -27
- package/dist/data-structures/binary-tree/binary-tree.d.ts +30 -30
- package/dist/data-structures/binary-tree/binary-tree.js +55 -55
- package/dist/data-structures/binary-tree/segment-tree.d.ts +17 -17
- package/dist/data-structures/binary-tree/segment-tree.js +30 -30
- package/dist/data-structures/graph/abstract-graph.d.ts +6 -6
- package/dist/data-structures/graph/abstract-graph.js +6 -6
- package/dist/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/data-structures/graph/directed-graph.js +6 -6
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/data-structures/hash/coordinate-map.d.ts +2 -2
- package/dist/data-structures/hash/coordinate-set.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +40 -25
- package/dist/data-structures/heap/heap.js +72 -27
- package/dist/data-structures/heap/max-heap.d.ts +3 -3
- package/dist/data-structures/heap/min-heap.d.ts +3 -3
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +9 -9
- package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +7 -7
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
- package/dist/data-structures/priority-queue/priority-queue.d.ts +20 -9
- package/dist/data-structures/priority-queue/priority-queue.js +34 -28
- package/dist/data-structures/queue/deque.d.ts +1 -1
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +9 -0
- package/dist/data-structures/tree/tree.js +52 -0
- package/dist/data-structures/types/heap.d.ts +0 -4
- package/dist/utils/types/utils.d.ts +1 -3
- package/dist/utils/types/utils.js +0 -14
- package/dist/utils/utils.js +0 -197
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -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 +2192 -0
- package/docs/classes/AVLTreeNode.html +574 -0
- package/docs/classes/AaTree.html +150 -0
- package/docs/classes/AbstractEdge.html +269 -0
- package/docs/classes/AbstractGraph.html +927 -0
- package/docs/classes/AbstractVertex.html +215 -0
- package/docs/classes/ArrayDeque.html +417 -0
- package/docs/classes/BST.html +2038 -0
- package/docs/classes/BSTNode.html +570 -0
- package/docs/classes/BTree.html +150 -0
- package/docs/classes/BinaryIndexedTree.html +289 -0
- package/docs/classes/BinaryTree.html +1827 -0
- package/docs/classes/BinaryTreeNode.html +533 -0
- package/docs/classes/Character.html +198 -0
- package/docs/classes/CoordinateMap.html +469 -0
- package/docs/classes/CoordinateSet.html +430 -0
- package/docs/classes/Deque.html +767 -0
- package/docs/classes/DirectedEdge.html +354 -0
- package/docs/classes/DirectedGraph.html +1243 -0
- package/docs/classes/DirectedVertex.html +226 -0
- package/docs/classes/DoublyLinkedList.html +733 -0
- package/docs/classes/DoublyLinkedListNode.html +258 -0
- package/docs/classes/Heap.html +482 -0
- package/docs/classes/HeapItem.html +233 -0
- package/docs/classes/Matrix2D.html +480 -0
- package/docs/classes/MatrixNTI2D.html +218 -0
- package/docs/classes/MaxHeap.html +500 -0
- package/docs/classes/MaxPriorityQueue.html +809 -0
- package/docs/classes/MinHeap.html +501 -0
- package/docs/classes/MinPriorityQueue.html +811 -0
- package/docs/classes/Navigator.html +291 -0
- package/docs/classes/ObjectDeque.html +423 -0
- package/docs/classes/PriorityQueue.html +733 -0
- package/docs/classes/Queue.html +370 -0
- package/docs/classes/RBTree.html +150 -0
- package/docs/classes/SegmentTree.html +345 -0
- package/docs/classes/SegmentTreeNode.html +418 -0
- package/docs/classes/SinglyLinkedList.html +1105 -0
- package/docs/classes/SinglyLinkedListNode.html +375 -0
- package/docs/classes/SplayTree.html +150 -0
- package/docs/classes/Stack.html +346 -0
- package/docs/classes/TreeMultiSet.html +2036 -0
- package/docs/classes/TreeNode.html +236 -0
- package/docs/classes/Trie.html +350 -0
- package/docs/classes/TrieNode.html +258 -0
- package/docs/classes/TwoThreeTree.html +150 -0
- package/docs/classes/UndirectedEdge.html +313 -0
- package/docs/classes/UndirectedGraph.html +1080 -0
- package/docs/classes/UndirectedVertex.html +226 -0
- package/docs/classes/Vector2D.html +783 -0
- package/docs/enums/CP.html +159 -0
- package/docs/enums/FamilyPosition.html +159 -0
- package/docs/enums/LoopType.html +160 -0
- package/docs/index.html +494 -0
- package/docs/interfaces/AVLTreeDeleted.html +161 -0
- package/docs/interfaces/BinaryTreeNodeObj.html +168 -0
- package/docs/interfaces/HeapOptions.html +167 -0
- package/docs/interfaces/IDirectedGraph.html +243 -0
- package/docs/interfaces/IGraph.html +427 -0
- package/docs/interfaces/NavigatorParams.html +197 -0
- package/docs/interfaces/PriorityQueueOptions.html +168 -0
- package/docs/modules.html +218 -0
- package/docs/types/BSTComparator.html +140 -0
- package/docs/types/BSTDeletedResult.html +137 -0
- package/docs/types/BinaryTreeDeleted.html +137 -0
- package/docs/types/BinaryTreeNodeId.html +125 -0
- package/docs/types/BinaryTreeNodePropertyName.html +125 -0
- package/docs/types/DFSOrderPattern.html +125 -0
- package/docs/types/DijkstraResult.html +145 -0
- package/docs/types/Direction.html +125 -0
- package/docs/types/DoublyLinkedListGetBy.html +125 -0
- package/docs/types/NodeOrPropertyName.html +125 -0
- package/docs/types/PriorityQueueComparator.html +145 -0
- package/docs/types/PriorityQueueDFSOrderPattern.html +125 -0
- package/docs/types/ResultByProperty.html +130 -0
- package/docs/types/ResultsByProperty.html +130 -0
- package/docs/types/SegmentTreeNodeVal.html +125 -0
- package/docs/types/SpecifyOptional.html +132 -0
- package/docs/types/Thunk.html +133 -0
- package/docs/types/ToThunkFn.html +133 -0
- package/docs/types/TopologicalStatus.html +125 -0
- package/docs/types/TreeMultiSetDeletedResult.html +137 -0
- package/docs/types/TrlAsyncFn.html +138 -0
- package/docs/types/TrlFn.html +138 -0
- package/docs/types/Turning.html +125 -0
- package/docs/types/VertexId.html +125 -0
- package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +11 -15
- package/notes/note.md +23 -0
- package/package.json +2 -4
- package/.idea/data-structure-typed.iml +0 -14
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -293
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1485
- package/src/data-structures/binary-tree/bst.ts +0 -497
- 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/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -248
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -5
- package/src/data-structures/graph/abstract-graph.ts +0 -956
- 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 +0 -424
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -254
- package/src/data-structures/hash/coordinate-map.ts +0 -71
- package/src/data-structures/hash/coordinate-set.ts +0 -60
- package/src/data-structures/hash/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/hash/pair.ts +0 -1
- package/src/data-structures/hash/tree-map.ts +0 -1
- package/src/data-structures/hash/tree-set.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -156
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -34
- package/src/data-structures/index.ts +0 -13
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -351
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -748
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -208
- package/src/data-structures/matrix/navigator.ts +0 -122
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/priority-queue.ts +0 -346
- package/src/data-structures/queue/deque.ts +0 -241
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -120
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -225
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/index.ts +0 -13
- package/src/data-structures/types/navigator.ts +0 -13
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -1
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -2
- package/src/utils/types/index.ts +0 -1
- package/src/utils/types/utils.ts +0 -176
- package/src/utils/utils.ts +0 -290
- package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -495
- package/tests/unit/data-structures/graph/index.ts +0 -3
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license MIT
|
|
3
|
-
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
|
-
* @class
|
|
5
|
-
*/
|
|
6
|
-
export class Queue<T> {
|
|
7
|
-
protected _nodes: T[];
|
|
8
|
-
protected _offset: number;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
12
|
-
* @param {T[]} [elements] - The `elements` parameter is an optional array of elements of type `T`. If provided, it
|
|
13
|
-
* will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
|
|
14
|
-
* initialized as an empty array.
|
|
15
|
-
*/
|
|
16
|
-
constructor(elements?: T[]) {
|
|
17
|
-
this._nodes = elements || [];
|
|
18
|
-
this._offset = 0;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
23
|
-
* @public
|
|
24
|
-
* @static
|
|
25
|
-
* @param {T[]} elements - The "elements" parameter is an array of elements of type T.
|
|
26
|
-
* @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
|
|
27
|
-
* array.
|
|
28
|
-
*/
|
|
29
|
-
static fromArray<T>(elements: T[]): Queue<T> {
|
|
30
|
-
return new Queue(elements);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The add function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
|
|
35
|
-
* @param {T} element - The `element` parameter represents the element that you want to add to the queue.
|
|
36
|
-
* @returns The `add` method is returning a `Queue<T>` object.
|
|
37
|
-
*/
|
|
38
|
-
add(element: T): Queue<T> {
|
|
39
|
-
this._nodes.push(element);
|
|
40
|
-
return this;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* The `poll` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
45
|
-
* necessary to optimize performance.
|
|
46
|
-
* @returns The function `poll()` returns either the first element in the queue or `null` if the queue is empty.
|
|
47
|
-
*/
|
|
48
|
-
poll(): T | null {
|
|
49
|
-
if (this.size() === 0) return null;
|
|
50
|
-
|
|
51
|
-
const first = this.peek();
|
|
52
|
-
this._offset += 1;
|
|
53
|
-
|
|
54
|
-
if (this._offset * 2 < this._nodes.length) return first;
|
|
55
|
-
|
|
56
|
-
// only remove dequeued elements when reaching half size
|
|
57
|
-
// to decrease latency of shifting elements.
|
|
58
|
-
this._nodes = this._nodes.slice(this._offset);
|
|
59
|
-
this._offset = 0;
|
|
60
|
-
return first;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
65
|
-
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
66
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
67
|
-
*/
|
|
68
|
-
peek(): T | null {
|
|
69
|
-
return this.size() > 0 ? this._nodes[this._offset] : null;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
74
|
-
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
75
|
-
* array is empty, it returns `null`.
|
|
76
|
-
*/
|
|
77
|
-
peekLast(): T | null {
|
|
78
|
-
return this.size() > 0 ? this._nodes[this._nodes.length - 1] : null;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* The size function returns the number of elements in an array.
|
|
83
|
-
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
84
|
-
*/
|
|
85
|
-
size(): number {
|
|
86
|
-
return this._nodes.length - this._offset;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* The function checks if a data structure is empty by comparing its size to zero.
|
|
91
|
-
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
|
|
92
|
-
*/
|
|
93
|
-
isEmpty(): boolean {
|
|
94
|
-
return this.size() === 0;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
|
|
99
|
-
* @returns An array of type T is being returned.
|
|
100
|
-
*/
|
|
101
|
-
toArray(): T[] {
|
|
102
|
-
return this._nodes.slice(this._offset);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* The clear function resets the nodes array and offset to their initial values.
|
|
107
|
-
*/
|
|
108
|
-
clear(): void {
|
|
109
|
-
this._nodes = [];
|
|
110
|
-
this._offset = 0;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
115
|
-
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
116
|
-
*/
|
|
117
|
-
clone(): Queue<T> {
|
|
118
|
-
return new Queue(this._nodes.slice(this._offset));
|
|
119
|
-
}
|
|
120
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './stack';
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license MIT
|
|
3
|
-
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
|
-
* @class
|
|
5
|
-
*/
|
|
6
|
-
export class Stack<T> {
|
|
7
|
-
protected _elements: T[];
|
|
8
|
-
|
|
9
|
-
/**
|
|
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
|
|
14
|
-
*/
|
|
15
|
-
constructor(elements?: T[]) {
|
|
16
|
-
this._elements = Array.isArray(elements) ? elements : [];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
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.
|
|
32
|
-
*/
|
|
33
|
-
isEmpty(): boolean {
|
|
34
|
-
return this._elements.length === 0;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* The size() function returns the number of elements in an array.
|
|
39
|
-
* @returns The size of the elements array.
|
|
40
|
-
*/
|
|
41
|
-
size(): number {
|
|
42
|
-
return this._elements.length;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
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.
|
|
48
|
-
*/
|
|
49
|
-
peek(): T | null {
|
|
50
|
-
if (this.isEmpty()) return null;
|
|
51
|
-
|
|
52
|
-
return this._elements[this._elements.length - 1];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
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.
|
|
59
|
-
*/
|
|
60
|
-
push(element: T): Stack<T> {
|
|
61
|
-
this._elements.push(element);
|
|
62
|
-
return this;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
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`.
|
|
69
|
-
*/
|
|
70
|
-
pop(): T | null {
|
|
71
|
-
if (this.isEmpty()) return null;
|
|
72
|
-
|
|
73
|
-
return this._elements.pop() || null;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* The toArray function returns a copy of the elements in an array.
|
|
78
|
-
* @returns An array of type T.
|
|
79
|
-
*/
|
|
80
|
-
toArray(): T[] {
|
|
81
|
-
return this._elements.slice();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* The clear function clears the elements array.
|
|
86
|
-
*/
|
|
87
|
-
clear(): void {
|
|
88
|
-
this._elements = [];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
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.
|
|
94
|
-
*/
|
|
95
|
-
clone(): Stack<T> {
|
|
96
|
-
return new Stack(this._elements.slice());
|
|
97
|
-
}
|
|
98
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './trie';
|
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
export class TrieNode {
|
|
9
|
-
protected _value;
|
|
10
|
-
|
|
11
|
-
constructor(v: string) {
|
|
12
|
-
this._value = v;
|
|
13
|
-
this._isEnd = false;
|
|
14
|
-
this._children = new Map<string, TrieNode>();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
protected _children: Map<string, TrieNode>;
|
|
18
|
-
|
|
19
|
-
get children(): Map<string, TrieNode> {
|
|
20
|
-
return this._children;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
set children(v: Map<string, TrieNode>) {
|
|
24
|
-
this._children = v;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
protected _isEnd: boolean;
|
|
28
|
-
|
|
29
|
-
get isEnd(): boolean {
|
|
30
|
-
return this._isEnd;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
set isEnd(v: boolean) {
|
|
34
|
-
this._isEnd = v;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get val(): string {
|
|
38
|
-
return this._value;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
set val(v: string) {
|
|
42
|
-
this._value = v;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export class Trie {
|
|
47
|
-
constructor(words?: string[]) {
|
|
48
|
-
this._root = new TrieNode('');
|
|
49
|
-
if (words) {
|
|
50
|
-
for (const i of words) {
|
|
51
|
-
this.add(i);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
protected _root: TrieNode;
|
|
57
|
-
|
|
58
|
-
get root() {
|
|
59
|
-
return this._root;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
set root(v: TrieNode) {
|
|
63
|
-
this._root = v;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
add(word: string): boolean {
|
|
67
|
-
let cur = this._root;
|
|
68
|
-
for (const c of word) {
|
|
69
|
-
let nodeC = cur.children.get(c);
|
|
70
|
-
if (!nodeC) {
|
|
71
|
-
nodeC = new TrieNode(c);
|
|
72
|
-
cur.children.set(c, nodeC);
|
|
73
|
-
}
|
|
74
|
-
cur = nodeC;
|
|
75
|
-
}
|
|
76
|
-
cur.isEnd = true;
|
|
77
|
-
return true;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
has(input: string): boolean {
|
|
81
|
-
let cur = this._root;
|
|
82
|
-
for (const c of input) {
|
|
83
|
-
const nodeC = cur.children.get(c);
|
|
84
|
-
if (!nodeC) return false;
|
|
85
|
-
cur = nodeC;
|
|
86
|
-
}
|
|
87
|
-
return cur.isEnd;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
remove(word: string) {
|
|
91
|
-
let isDeleted = false;
|
|
92
|
-
const dfs = (cur: TrieNode, i: number): boolean => {
|
|
93
|
-
const char = word[i];
|
|
94
|
-
const child = cur.children.get(char);
|
|
95
|
-
if (child) {
|
|
96
|
-
if (i === word.length - 1) {
|
|
97
|
-
if (child.isEnd) {
|
|
98
|
-
if (child.children.size > 0) {
|
|
99
|
-
child.isEnd = false;
|
|
100
|
-
} else {
|
|
101
|
-
cur.children.delete(char);
|
|
102
|
-
}
|
|
103
|
-
isDeleted = true;
|
|
104
|
-
return true;
|
|
105
|
-
}
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
const res = dfs(child, i + 1);
|
|
109
|
-
if (res && !cur.isEnd && child.children.size === 0) {
|
|
110
|
-
cur.children.delete(char);
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
dfs(this.root, 0);
|
|
119
|
-
return isDeleted;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// --- start additional methods ---
|
|
123
|
-
/**
|
|
124
|
-
* 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
|
|
125
|
-
* @param {string} input - The input parameter is a string that represents the input value for the function.
|
|
126
|
-
* @returns a boolean value.
|
|
127
|
-
*/
|
|
128
|
-
isAbsPrefix(input: string): boolean {
|
|
129
|
-
let cur = this._root;
|
|
130
|
-
for (const c of input) {
|
|
131
|
-
const nodeC = cur.children.get(c);
|
|
132
|
-
if (!nodeC) return false;
|
|
133
|
-
cur = nodeC;
|
|
134
|
-
}
|
|
135
|
-
return !cur.isEnd;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* 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
|
|
140
|
-
* @param {string} input - The input parameter is a string that represents the prefix we want to check.
|
|
141
|
-
* @returns a boolean value.
|
|
142
|
-
*/
|
|
143
|
-
isPrefix(input: string): boolean {
|
|
144
|
-
let cur = this._root;
|
|
145
|
-
for (const c of input) {
|
|
146
|
-
const nodeC = cur.children.get(c);
|
|
147
|
-
if (!nodeC) return false;
|
|
148
|
-
cur = nodeC;
|
|
149
|
-
}
|
|
150
|
-
return true;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* 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
|
|
155
|
-
* @param {string} input - The input parameter is a string that represents the common prefix that we want to check for
|
|
156
|
-
* in the Trie data structure.
|
|
157
|
-
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
158
|
-
*/
|
|
159
|
-
isCommonPrefix(input: string): boolean {
|
|
160
|
-
let commonPre = '';
|
|
161
|
-
const dfs = (cur: TrieNode) => {
|
|
162
|
-
commonPre += cur.val;
|
|
163
|
-
if (commonPre === input) return;
|
|
164
|
-
if (cur.isEnd) return;
|
|
165
|
-
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
166
|
-
else return;
|
|
167
|
-
}
|
|
168
|
-
dfs(this._root);
|
|
169
|
-
return commonPre === input;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* The function `getLongestCommonPrefix` returns the longest common prefix among all the words stored in a Trie data
|
|
174
|
-
* structure.
|
|
175
|
-
* @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
|
|
176
|
-
* Trie.
|
|
177
|
-
*/
|
|
178
|
-
getLongestCommonPrefix(): string {
|
|
179
|
-
let commonPre = '';
|
|
180
|
-
const dfs = (cur: TrieNode) => {
|
|
181
|
-
commonPre += cur.val;
|
|
182
|
-
if (cur.isEnd) return;
|
|
183
|
-
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
184
|
-
else return;
|
|
185
|
-
}
|
|
186
|
-
dfs(this._root);
|
|
187
|
-
return commonPre;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
192
|
-
* @param [prefix] - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
193
|
-
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
194
|
-
* @returns an array of strings.
|
|
195
|
-
*/
|
|
196
|
-
getAll(prefix = ''): string[] {
|
|
197
|
-
const words: string[] = [];
|
|
198
|
-
|
|
199
|
-
function dfs(node: TrieNode, word: string) {
|
|
200
|
-
for (const char of node.children.keys()) {
|
|
201
|
-
const charNode = node.children.get(char);
|
|
202
|
-
if (charNode !== undefined) {
|
|
203
|
-
dfs(charNode, word.concat(char));
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
if (node.isEnd) {
|
|
207
|
-
words.push(word);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
let startNode = this._root;
|
|
212
|
-
|
|
213
|
-
if (prefix) {
|
|
214
|
-
for (const c of prefix) {
|
|
215
|
-
const nodeC = startNode.children.get(c);
|
|
216
|
-
if (nodeC) startNode = nodeC;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
dfs(startNode, prefix);
|
|
221
|
-
return words;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// --- end additional methods ---
|
|
225
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
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
|
-
hasVertex(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
|
-
hasEdge(src: V | VertexId, dest: V | VertexId): boolean;
|
|
27
|
-
|
|
28
|
-
// hasEdge(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
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
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 };
|
|
@@ -1,18 +0,0 @@
|
|
|
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;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type DoublyLinkedListGetBy = 'node' | 'val';
|
|
@@ -1,13 +0,0 @@
|
|
|
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';
|
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type SegmentTreeNodeVal = number;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {}
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './data-structures';
|
package/src/utils/index.ts
DELETED
package/src/utils/types/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './utils';
|