data-structure-typed 1.17.4 → 1.18.5
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 +193 -66
- 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 +3 -0
- package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +288 -0
- package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
- package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +1502 -0
- package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +503 -0
- 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 +11 -0
- package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +110 -0
- package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +243 -0
- package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +55 -0
- package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/backup/recursive-type/src/data-structures/diagrams/README.md +5 -0
- package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +985 -0
- 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 +478 -0
- package/backup/recursive-type/src/data-structures/graph/index.ts +3 -0
- package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +293 -0
- package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +67 -0
- package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +56 -0
- package/backup/recursive-type/src/data-structures/hash/hash-table.ts +3 -0
- package/backup/recursive-type/src/data-structures/hash/index.ts +6 -0
- package/backup/recursive-type/src/data-structures/hash/pair.ts +3 -0
- package/backup/recursive-type/src/data-structures/hash/tree-map.ts +3 -0
- package/backup/recursive-type/src/data-structures/hash/tree-set.ts +3 -0
- package/backup/recursive-type/src/data-structures/heap/heap.ts +176 -0
- package/backup/recursive-type/src/data-structures/heap/index.ts +3 -0
- package/backup/recursive-type/src/data-structures/heap/max-heap.ts +31 -0
- package/backup/recursive-type/src/data-structures/heap/min-heap.ts +34 -0
- package/backup/recursive-type/src/data-structures/index.ts +15 -0
- package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +42 -0
- package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +56 -0
- package/backup/recursive-type/src/data-structures/interfaces/bst.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +15 -0
- package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/heap.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/index.ts +13 -0
- package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +1 -0
- package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +3 -0
- package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
- package/backup/recursive-type/src/data-structures/linked-list/index.ts +3 -0
- package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +490 -0
- package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/backup/recursive-type/src/data-structures/matrix/index.ts +4 -0
- package/backup/recursive-type/src/data-structures/matrix/matrix.ts +27 -0
- package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +208 -0
- package/backup/recursive-type/src/data-structures/matrix/navigator.ts +122 -0
- package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +316 -0
- package/backup/recursive-type/src/data-structures/priority-queue/index.ts +3 -0
- package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
- package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
- package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +354 -0
- package/backup/recursive-type/src/data-structures/queue/deque.ts +251 -0
- package/backup/recursive-type/src/data-structures/queue/index.ts +2 -0
- package/backup/recursive-type/src/data-structures/queue/queue.ts +120 -0
- package/backup/recursive-type/src/data-structures/stack/index.ts +1 -0
- package/backup/recursive-type/src/data-structures/stack/stack.ts +98 -0
- package/backup/recursive-type/src/data-structures/tree/index.ts +1 -0
- package/backup/recursive-type/src/data-structures/tree/tree.ts +80 -0
- package/backup/recursive-type/src/data-structures/trie/index.ts +1 -0
- package/backup/recursive-type/src/data-structures/trie/trie.ts +227 -0
- package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +5 -0
- package/backup/recursive-type/src/data-structures/types/avl-tree.ts +8 -0
- package/backup/recursive-type/src/data-structures/types/binary-tree.ts +10 -0
- package/backup/recursive-type/src/data-structures/types/bst.ts +6 -0
- package/backup/recursive-type/src/data-structures/types/directed-graph.ts +8 -0
- package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/backup/recursive-type/src/data-structures/types/heap.ts +5 -0
- package/backup/recursive-type/src/data-structures/types/index.ts +12 -0
- package/backup/recursive-type/src/data-structures/types/navigator.ts +13 -0
- package/backup/recursive-type/src/data-structures/types/priority-queue.ts +9 -0
- package/backup/recursive-type/src/data-structures/types/segment-tree.ts +1 -0
- package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +1 -0
- package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +1 -0
- package/backup/recursive-type/src/index.ts +1 -0
- package/backup/recursive-type/src/utils/index.ts +2 -0
- package/backup/recursive-type/src/utils/types/index.ts +1 -0
- package/backup/recursive-type/src/utils/types/utils.ts +6 -0
- package/backup/recursive-type/src/utils/utils.ts +78 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +19 -25
- package/dist/data-structures/binary-tree/avl-tree.js +12 -20
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +10 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +135 -161
- package/dist/data-structures/binary-tree/binary-tree.js +192 -164
- package/dist/data-structures/binary-tree/bst.d.ts +21 -21
- package/dist/data-structures/binary-tree/bst.js +33 -36
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -2
- package/dist/data-structures/binary-tree/rb-tree.js +68 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +17 -39
- package/dist/data-structures/binary-tree/segment-tree.js +34 -47
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -8
- package/dist/data-structures/binary-tree/tree-multiset.js +7 -6
- package/dist/data-structures/graph/abstract-graph.d.ts +56 -71
- package/dist/data-structures/graph/abstract-graph.js +87 -92
- package/dist/data-structures/graph/directed-graph.d.ts +128 -105
- package/dist/data-structures/graph/directed-graph.js +161 -121
- package/dist/data-structures/graph/undirected-graph.d.ts +81 -62
- package/dist/data-structures/graph/undirected-graph.js +99 -78
- package/dist/data-structures/hash/coordinate-map.d.ts +1 -5
- package/dist/data-structures/hash/coordinate-map.js +3 -9
- package/dist/data-structures/hash/coordinate-set.d.ts +2 -6
- package/dist/data-structures/hash/coordinate-set.js +3 -9
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +7 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +7 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +7 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +7 -0
- package/dist/data-structures/heap/heap.d.ts +0 -14
- package/dist/data-structures/heap/heap.js +3 -27
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/interfaces/abstract-graph.d.ts +22 -0
- package/dist/data-structures/interfaces/abstract-graph.js +2 -0
- package/dist/data-structures/interfaces/avl-tree.d.ts +1 -0
- package/dist/data-structures/interfaces/avl-tree.js +2 -0
- package/dist/data-structures/interfaces/binary-tree.d.ts +27 -0
- package/dist/data-structures/interfaces/binary-tree.js +2 -0
- package/dist/data-structures/interfaces/bst.d.ts +1 -0
- package/dist/data-structures/interfaces/bst.js +2 -0
- package/dist/data-structures/interfaces/directed-graph.d.ts +9 -0
- package/dist/data-structures/interfaces/directed-graph.js +2 -0
- package/dist/data-structures/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/interfaces/doubly-linked-list.js +2 -0
- package/dist/data-structures/interfaces/heap.d.ts +1 -0
- package/dist/data-structures/interfaces/heap.js +2 -0
- package/dist/data-structures/interfaces/index.d.ts +13 -0
- package/dist/data-structures/interfaces/index.js +29 -0
- package/dist/data-structures/interfaces/navigator.d.ts +1 -0
- package/dist/data-structures/interfaces/navigator.js +2 -0
- package/dist/data-structures/interfaces/priority-queue.d.ts +1 -0
- package/dist/data-structures/interfaces/priority-queue.js +2 -0
- package/dist/data-structures/interfaces/segment-tree.d.ts +1 -0
- package/dist/data-structures/interfaces/segment-tree.js +2 -0
- package/dist/data-structures/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/data-structures/interfaces/singly-linked-list.js +2 -0
- package/dist/data-structures/interfaces/tree-multiset.d.ts +1 -0
- package/dist/data-structures/interfaces/tree-multiset.js +2 -0
- package/dist/data-structures/interfaces/undirected-graph.d.ts +2 -0
- package/dist/data-structures/interfaces/undirected-graph.js +2 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -3
- package/dist/data-structures/linked-list/doubly-linked-list.js +12 -18
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +12 -15
- package/dist/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +4 -4
- package/dist/data-structures/queue/deque.d.ts +5 -5
- package/dist/data-structures/queue/deque.js +6 -6
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/stack/stack.d.ts +1 -1
- package/dist/data-structures/tree/tree.d.ts +12 -4
- package/dist/data-structures/tree/tree.js +44 -12
- package/dist/data-structures/trie/trie.d.ts +3 -3
- package/dist/data-structures/trie/trie.js +10 -10
- package/dist/data-structures/types/abstract-graph.d.ts +1 -20
- package/dist/data-structures/types/avl-tree.d.ts +5 -4
- package/dist/data-structures/types/binary-tree.d.ts +6 -5
- package/dist/data-structures/types/bst.d.ts +4 -3
- package/dist/data-structures/types/directed-graph.d.ts +5 -9
- package/dist/data-structures/types/directed-graph.js +7 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -1
- package/dist/data-structures/types/heap.d.ts +2 -2
- package/dist/data-structures/types/index.d.ts +0 -1
- package/dist/data-structures/types/index.js +0 -1
- package/dist/data-structures/types/navigator.d.ts +2 -2
- package/dist/data-structures/types/priority-queue.d.ts +2 -2
- package/dist/data-structures/types/tree-multiset.d.ts +3 -4
- package/docs/assets/search.js +1 -1
- package/docs/classes/AVLTree.html +552 -405
- package/docs/classes/AVLTreeNode.html +107 -242
- package/docs/classes/AaTree.html +18 -13
- package/docs/classes/AbstractEdge.html +77 -68
- package/docs/classes/AbstractGraph.html +223 -115
- package/docs/classes/AbstractVertex.html +71 -45
- package/docs/classes/ArrayDeque.html +31 -26
- package/docs/classes/BST.html +543 -391
- package/docs/classes/BSTNode.html +107 -236
- package/docs/classes/BTree.html +18 -13
- package/docs/classes/BinaryIndexedTree.html +56 -21
- package/docs/classes/BinaryTree.html +564 -355
- package/docs/classes/BinaryTreeNode.html +146 -199
- package/docs/classes/Character.html +21 -16
- package/docs/classes/CoordinateMap.html +49 -52
- package/docs/classes/CoordinateSet.html +50 -53
- package/docs/classes/Deque.html +51 -68
- package/docs/classes/DirectedEdge.html +98 -103
- package/docs/classes/DirectedGraph.html +449 -210
- package/docs/classes/DirectedVertex.html +63 -52
- package/docs/classes/DoublyLinkedList.html +56 -71
- package/docs/classes/DoublyLinkedListNode.html +28 -23
- package/docs/classes/HashTable.html +155 -0
- package/docs/classes/Heap.html +33 -109
- package/docs/classes/HeapItem.html +25 -20
- package/docs/classes/Matrix2D.html +33 -28
- package/docs/classes/MatrixNTI2D.html +21 -16
- package/docs/classes/MaxHeap.html +33 -114
- package/docs/classes/MaxPriorityQueue.html +71 -61
- package/docs/classes/MinHeap.html +33 -114
- package/docs/classes/MinPriorityQueue.html +71 -61
- package/docs/classes/Navigator.html +28 -23
- package/docs/classes/ObjectDeque.html +66 -51
- package/docs/classes/{RBTree.html → Pair.html} +25 -20
- package/docs/classes/PriorityQueue.html +66 -56
- package/docs/classes/Queue.html +33 -28
- package/docs/classes/SegmentTree.html +129 -57
- package/docs/classes/SegmentTreeNode.html +62 -140
- package/docs/classes/SinglyLinkedList.html +53 -58
- package/docs/classes/SinglyLinkedListNode.html +25 -20
- package/docs/classes/SkipLinkedList.html +18 -13
- package/docs/classes/SplayTree.html +18 -13
- package/docs/classes/Stack.html +31 -26
- package/docs/classes/TreeMap.html +155 -0
- package/docs/classes/TreeMultiSet.html +541 -388
- package/docs/classes/TreeNode.html +125 -35
- package/docs/classes/TreeSet.html +155 -0
- package/docs/classes/Trie.html +30 -25
- package/docs/classes/TrieNode.html +33 -28
- package/docs/classes/TwoThreeTree.html +18 -13
- package/docs/classes/UndirectedEdge.html +87 -80
- package/docs/classes/UndirectedGraph.html +356 -178
- package/docs/classes/UndirectedVertex.html +63 -52
- package/docs/classes/Vector2D.html +45 -40
- package/docs/enums/CP.html +21 -16
- package/docs/enums/FamilyPosition.html +21 -16
- package/docs/enums/LoopType.html +20 -15
- package/docs/{interfaces/AVLTreeDeleted.html → enums/TopologicalProperty.html} +47 -44
- package/docs/index.html +203 -69
- package/docs/interfaces/{PriorityQueueOptions.html → IBinaryTree.html} +49 -40
- package/docs/interfaces/IBinaryTreeNode.html +383 -0
- package/docs/interfaces/IDirectedGraph.html +24 -19
- package/docs/interfaces/IGraph.html +122 -89
- package/docs/interfaces/{HeapOptions.html → IUNDirectedGraph.html} +26 -53
- package/docs/modules.html +33 -23
- package/docs/types/{ToThunkFn.html → AVLTreeDeleted.html} +31 -22
- package/docs/types/BSTComparator.html +18 -13
- package/docs/types/BSTDeletedResult.html +23 -18
- package/docs/types/BinaryTreeDeleted.html +23 -18
- package/docs/types/BinaryTreeNodeId.html +18 -13
- package/docs/types/BinaryTreeNodePropertyName.html +18 -13
- package/docs/types/DFSOrderPattern.html +18 -13
- package/docs/types/DijkstraResult.html +18 -13
- package/docs/types/Direction.html +18 -13
- package/docs/types/{DoublyLinkedListGetBy.html → EdgeId.html} +22 -17
- package/docs/types/{TrlFn.html → HeapOptions.html} +33 -20
- package/docs/types/{TrlAsyncFn.html → NavigatorParams.html} +46 -20
- package/docs/types/NodeOrPropertyName.html +18 -13
- package/docs/types/PriorityQueueComparator.html +18 -13
- package/docs/types/PriorityQueueDFSOrderPattern.html +18 -13
- package/docs/types/{SpecifyOptional.html → PriorityQueueOptions.html} +32 -20
- package/docs/types/RecursiveAVLTreeNode.html +135 -0
- package/docs/types/RecursiveBSTNode.html +135 -0
- package/docs/types/RecursiveBinaryTreeNode.html +135 -0
- package/docs/types/ResultByProperty.html +21 -16
- package/docs/types/ResultsByProperty.html +21 -16
- package/docs/types/SegmentTreeNodeVal.html +18 -13
- package/docs/types/TopologicalStatus.html +18 -13
- package/docs/types/TreeMultiSetDeletedResult.html +23 -18
- package/docs/types/Turning.html +18 -13
- package/docs/types/VertexId.html +18 -13
- package/notes/note.md +12 -1
- package/package.json +11 -3
- package/tsconfig.json +2 -2
- package/docs/interfaces/NavigatorParams.html +0 -197
- package/docs/types/Thunk.html +0 -133
|
@@ -0,0 +1,227 @@
|
|
|
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
|
+
|
|
10
|
+
constructor(v: string) {
|
|
11
|
+
this._val = v;
|
|
12
|
+
this._isEnd = false;
|
|
13
|
+
this._children = new Map<string, TrieNode>();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private _val;
|
|
17
|
+
|
|
18
|
+
get val(): string {
|
|
19
|
+
return this._val;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
set val(v: string) {
|
|
23
|
+
this._val = v;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected _children: Map<string, TrieNode>;
|
|
27
|
+
|
|
28
|
+
get children(): Map<string, TrieNode> {
|
|
29
|
+
return this._children;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
set children(v: Map<string, TrieNode>) {
|
|
33
|
+
this._children = v;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
protected _isEnd: boolean;
|
|
37
|
+
|
|
38
|
+
get isEnd(): boolean {
|
|
39
|
+
return this._isEnd;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
set isEnd(v: boolean) {
|
|
43
|
+
this._isEnd = v;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class Trie {
|
|
49
|
+
constructor(words?: string[]) {
|
|
50
|
+
this._root = new TrieNode('');
|
|
51
|
+
if (words) {
|
|
52
|
+
for (const i of words) {
|
|
53
|
+
this.add(i);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
protected _root: TrieNode;
|
|
59
|
+
|
|
60
|
+
get root() {
|
|
61
|
+
return this._root;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
set root(v: TrieNode) {
|
|
65
|
+
this._root = v;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
add(word: string): boolean {
|
|
69
|
+
let cur = this._root;
|
|
70
|
+
for (const c of word) {
|
|
71
|
+
let nodeC = cur.children.get(c);
|
|
72
|
+
if (!nodeC) {
|
|
73
|
+
nodeC = new TrieNode(c);
|
|
74
|
+
cur.children.set(c, nodeC);
|
|
75
|
+
}
|
|
76
|
+
cur = nodeC;
|
|
77
|
+
}
|
|
78
|
+
cur.isEnd = true;
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
has(input: string): boolean {
|
|
83
|
+
let cur = this._root;
|
|
84
|
+
for (const c of input) {
|
|
85
|
+
const nodeC = cur.children.get(c);
|
|
86
|
+
if (!nodeC) return false;
|
|
87
|
+
cur = nodeC;
|
|
88
|
+
}
|
|
89
|
+
return cur.isEnd;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
remove(word: string) {
|
|
93
|
+
let isDeleted = false;
|
|
94
|
+
const dfs = (cur: TrieNode, i: number): boolean => {
|
|
95
|
+
const char = word[i];
|
|
96
|
+
const child = cur.children.get(char);
|
|
97
|
+
if (child) {
|
|
98
|
+
if (i === word.length - 1) {
|
|
99
|
+
if (child.isEnd) {
|
|
100
|
+
if (child.children.size > 0) {
|
|
101
|
+
child.isEnd = false;
|
|
102
|
+
} else {
|
|
103
|
+
cur.children.delete(char);
|
|
104
|
+
}
|
|
105
|
+
isDeleted = true;
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
const res = dfs(child, i + 1);
|
|
111
|
+
if (res && !cur.isEnd && child.children.size === 0) {
|
|
112
|
+
cur.children.delete(char);
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
dfs(this.root, 0);
|
|
121
|
+
return isDeleted;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// --- start additional methods ---
|
|
125
|
+
/**
|
|
126
|
+
* 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
|
|
127
|
+
* @param {string} input - The input parameter is a string that represents the input value for the function.
|
|
128
|
+
* @returns a boolean value.
|
|
129
|
+
*/
|
|
130
|
+
isAbsPrefix(input: string): boolean {
|
|
131
|
+
let cur = this._root;
|
|
132
|
+
for (const c of input) {
|
|
133
|
+
const nodeC = cur.children.get(c);
|
|
134
|
+
if (!nodeC) return false;
|
|
135
|
+
cur = nodeC;
|
|
136
|
+
}
|
|
137
|
+
return !cur.isEnd;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 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
|
|
142
|
+
* @param {string} input - The input parameter is a string that represents the prefix we want to check.
|
|
143
|
+
* @returns a boolean value.
|
|
144
|
+
*/
|
|
145
|
+
isPrefix(input: string): boolean {
|
|
146
|
+
let cur = this._root;
|
|
147
|
+
for (const c of input) {
|
|
148
|
+
const nodeC = cur.children.get(c);
|
|
149
|
+
if (!nodeC) return false;
|
|
150
|
+
cur = nodeC;
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 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
|
|
157
|
+
* @param {string} input - The input parameter is a string that represents the common prefix that we want to check for
|
|
158
|
+
* in the Trie data structure.
|
|
159
|
+
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
160
|
+
*/
|
|
161
|
+
isCommonPrefix(input: string): boolean {
|
|
162
|
+
let commonPre = '';
|
|
163
|
+
const dfs = (cur: TrieNode) => {
|
|
164
|
+
commonPre += cur.val;
|
|
165
|
+
if (commonPre === input) return;
|
|
166
|
+
if (cur.isEnd) return;
|
|
167
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
168
|
+
else return;
|
|
169
|
+
}
|
|
170
|
+
dfs(this._root);
|
|
171
|
+
return commonPre === input;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The function `getLongestCommonPrefix` returns the longest common prefix among all the words stored in a Trie data
|
|
176
|
+
* structure.
|
|
177
|
+
* @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
|
|
178
|
+
* Trie.
|
|
179
|
+
*/
|
|
180
|
+
getLongestCommonPrefix(): string {
|
|
181
|
+
let commonPre = '';
|
|
182
|
+
const dfs = (cur: TrieNode) => {
|
|
183
|
+
commonPre += cur.val;
|
|
184
|
+
if (cur.isEnd) return;
|
|
185
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
186
|
+
else return;
|
|
187
|
+
}
|
|
188
|
+
dfs(this._root);
|
|
189
|
+
return commonPre;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
194
|
+
* @param [prefix] - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
195
|
+
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
196
|
+
* @returns an array of strings.
|
|
197
|
+
*/
|
|
198
|
+
getAll(prefix = ''): string[] {
|
|
199
|
+
const words: string[] = [];
|
|
200
|
+
|
|
201
|
+
function dfs(node: TrieNode, word: string) {
|
|
202
|
+
for (const char of node.children.keys()) {
|
|
203
|
+
const charNode = node.children.get(char);
|
|
204
|
+
if (charNode !== undefined) {
|
|
205
|
+
dfs(charNode, word.concat(char));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (node.isEnd) {
|
|
209
|
+
words.push(word);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
let startNode = this._root;
|
|
214
|
+
|
|
215
|
+
if (prefix) {
|
|
216
|
+
for (const c of prefix) {
|
|
217
|
+
const nodeC = startNode.children.get(c);
|
|
218
|
+
if (nodeC) startNode = nodeC;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
dfs(startNode, prefix);
|
|
223
|
+
return words;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// --- end additional methods ---
|
|
227
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import {AVLTreeNode} from '../binary-tree';
|
|
2
|
+
|
|
3
|
+
export type AVLTreeDeleted<N> = {
|
|
4
|
+
deleted: N | null;
|
|
5
|
+
needBalanced: N | null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type RecursiveAVLTreeNode<T> = AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T,AVLTreeNode<T,AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
@@ -0,0 +1,10 @@
|
|
|
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<N> = { deleted: N | null | undefined, needBalanced: N | null };
|
|
8
|
+
export type ResultByProperty<N extends BinaryTreeNode<N['val'], N>> = N['val'] | N | number | BinaryTreeNodeId;
|
|
9
|
+
export type ResultsByProperty<N extends BinaryTreeNode<N['val'], N>> = ResultByProperty<N>[];
|
|
10
|
+
export type RecursiveBinaryTreeNode<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T,BinaryTreeNode<T,BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
@@ -0,0 +1,6 @@
|
|
|
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<N extends BSTNode<N['val'], N>> = { deleted: N | null, needBalanced: N | null };
|
|
6
|
+
export type RecursiveBSTNode<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T,BSTNode<T,BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1,12 @@
|
|
|
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';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type Direction = 'up' | 'right' | 'down' | 'left';
|
|
2
|
+
export type Turning = { [key in Direction]: Direction };
|
|
3
|
+
|
|
4
|
+
export type 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 type TreeMultiSetDeletedResult<N> = { deleted: N | null, needBalanced: N | null };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './data-structures';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
2
|
+
export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: Symbol };
|
|
3
|
+
export type TrlFn = (...args: any[]) => any;
|
|
4
|
+
export type TrlAsyncFn = (...args: any[]) => any;
|
|
5
|
+
|
|
6
|
+
export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export const uuidV4 = function () {
|
|
2
|
+
return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
|
|
3
|
+
const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
4
|
+
return v.toString(16);
|
|
5
|
+
});
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const arrayRemove = function <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): T[] {
|
|
9
|
+
let i = -1, len = array ? array.length : 0;
|
|
10
|
+
const result = [];
|
|
11
|
+
|
|
12
|
+
while (++i < len) {
|
|
13
|
+
const value = array[i];
|
|
14
|
+
if (predicate(value, i, array)) {
|
|
15
|
+
result.push(value);
|
|
16
|
+
Array.prototype.splice.call(array, i--, 1);
|
|
17
|
+
len--;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* data-structure-typed
|
|
27
|
+
*
|
|
28
|
+
* @author Tyler Zeng
|
|
29
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
30
|
+
* @license MIT License
|
|
31
|
+
*/
|
|
32
|
+
import type {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types';
|
|
33
|
+
|
|
34
|
+
export const THUNK_SYMBOL = Symbol('thunk')
|
|
35
|
+
|
|
36
|
+
export const isThunk = (fnOrValue: any) => {
|
|
37
|
+
return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const toThunk = (fn: ToThunkFn): Thunk => {
|
|
41
|
+
const thunk = () => fn()
|
|
42
|
+
thunk.__THUNK__ = THUNK_SYMBOL
|
|
43
|
+
return thunk
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const trampoline = (fn: TrlFn) => {
|
|
47
|
+
const cont = (...args: [...Parameters<TrlFn>]) => toThunk(() => fn(...args))
|
|
48
|
+
|
|
49
|
+
return Object.assign(
|
|
50
|
+
(...args: [...Parameters<TrlFn>]) => {
|
|
51
|
+
let result = fn(...args)
|
|
52
|
+
|
|
53
|
+
while (isThunk(result) && typeof result === 'function') {
|
|
54
|
+
result = result()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return result
|
|
58
|
+
},
|
|
59
|
+
{cont}
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const trampolineAsync = (fn: TrlAsyncFn) => {
|
|
64
|
+
const cont = (...args: [...Parameters<TrlAsyncFn>]) => toThunk(() => fn(...args))
|
|
65
|
+
|
|
66
|
+
return Object.assign(
|
|
67
|
+
async (...args: [...Parameters<TrlAsyncFn>]) => {
|
|
68
|
+
let result = await fn(...args)
|
|
69
|
+
|
|
70
|
+
while (isThunk(result) && typeof result === 'function') {
|
|
71
|
+
result = await result()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return result
|
|
75
|
+
},
|
|
76
|
+
{cont}
|
|
77
|
+
)
|
|
78
|
+
}
|
|
@@ -6,31 +6,25 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeDeleted, BinaryTreeNodeId } from '../types';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* The function overrides the clone method of the AVLTreeNode class to create a new AVLTreeNode object with the same
|
|
13
|
-
* id, value, and count.
|
|
14
|
-
* @returns The method is returning a new instance of the AVLTreeNode class with the same id, val, and count values as
|
|
15
|
-
* the current instance.
|
|
16
|
-
*/
|
|
17
|
-
clone(): AVLTreeNode<T>;
|
|
9
|
+
import type { AVLTreeDeleted, BinaryTreeNodeId, RecursiveAVLTreeNode } from '../types';
|
|
10
|
+
import { IBinaryTreeNode } from '../interfaces';
|
|
11
|
+
export declare class AVLTreeNode<T, FAMILY extends AVLTreeNode<T, FAMILY> = RecursiveAVLTreeNode<T>> extends BSTNode<T, FAMILY> implements IBinaryTreeNode<T, FAMILY> {
|
|
18
12
|
}
|
|
19
|
-
export declare class AVLTree<
|
|
20
|
-
|
|
13
|
+
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode<number>> extends BST<N> {
|
|
14
|
+
_createNode(id: BinaryTreeNodeId, val: N['val'], count?: number): N;
|
|
21
15
|
/**
|
|
22
16
|
* The function overrides the add method of a Binary Search Tree to insert a node with a given id and value, and then
|
|
23
17
|
* balances the tree.
|
|
24
18
|
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add or
|
|
25
19
|
* update in the AVL tree.
|
|
26
|
-
* @param {
|
|
27
|
-
* `id`. It can be of type `
|
|
20
|
+
* @param {N | null} val - The `val` parameter represents the value that you want to assign to the node with the given
|
|
21
|
+
* `id`. It can be of type `N` (the generic type) or `null`.
|
|
28
22
|
* @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
|
|
29
23
|
* of times the value `val` should be inserted into the AVL tree. If the `count` parameter is not provided, it defaults
|
|
30
24
|
* to `1`, indicating that the value should be inserted once.
|
|
31
|
-
* @returns The method is returning either an
|
|
25
|
+
* @returns The method is returning either an N object or null.
|
|
32
26
|
*/
|
|
33
|
-
add(id: BinaryTreeNodeId, val:
|
|
27
|
+
add(id: BinaryTreeNodeId, val: N['val'] | null, count?: number): N | null;
|
|
34
28
|
/**
|
|
35
29
|
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
36
30
|
* then balances the tree if necessary.
|
|
@@ -39,45 +33,45 @@ export declare class AVLTree<T> extends BST<T> {
|
|
|
39
33
|
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
40
34
|
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
41
35
|
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
42
|
-
* @returns The method is returning an array of `AVLTreeDeleted<
|
|
36
|
+
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
43
37
|
*/
|
|
44
|
-
remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): AVLTreeDeleted<
|
|
38
|
+
remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): AVLTreeDeleted<N>[];
|
|
45
39
|
/**
|
|
46
40
|
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
47
41
|
* height of its right subtree.
|
|
48
|
-
* @param node - The parameter "node" is of type
|
|
42
|
+
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
49
43
|
* @returns The balance factor of the given AVL tree node.
|
|
50
44
|
*/
|
|
51
|
-
balanceFactor(node:
|
|
45
|
+
balanceFactor(node: N): number;
|
|
52
46
|
/**
|
|
53
47
|
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
54
48
|
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
55
49
|
*/
|
|
56
|
-
updateHeight(node:
|
|
50
|
+
updateHeight(node: N): void;
|
|
57
51
|
/**
|
|
58
52
|
* The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
59
53
|
* each node in the path from the given node to the root.
|
|
60
54
|
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
61
55
|
*/
|
|
62
|
-
balancePath(node:
|
|
56
|
+
balancePath(node: N): void;
|
|
63
57
|
/**
|
|
64
58
|
* The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
65
59
|
* @param A - The parameter A is an AVLTreeNode object.
|
|
66
60
|
*/
|
|
67
|
-
balanceLL(A:
|
|
61
|
+
balanceLL(A: N): void;
|
|
68
62
|
/**
|
|
69
63
|
* The `balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
70
64
|
* @param A - A is an AVLTreeNode object.
|
|
71
65
|
*/
|
|
72
|
-
balanceLR(A:
|
|
66
|
+
balanceLR(A: N): void;
|
|
73
67
|
/**
|
|
74
68
|
* The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
75
69
|
* @param A - The parameter A is an AVLTreeNode object.
|
|
76
70
|
*/
|
|
77
|
-
balanceRR(A:
|
|
71
|
+
balanceRR(A: N): void;
|
|
78
72
|
/**
|
|
79
73
|
* The `balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
80
74
|
* @param A - A is an AVLTreeNode object.
|
|
81
75
|
*/
|
|
82
|
-
balanceRL(A:
|
|
76
|
+
balanceRL(A: N): void;
|
|
83
77
|
}
|
|
@@ -40,15 +40,6 @@ var AVLTreeNode = /** @class */ (function (_super) {
|
|
|
40
40
|
function AVLTreeNode() {
|
|
41
41
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
42
42
|
}
|
|
43
|
-
/**
|
|
44
|
-
* The function overrides the clone method of the AVLTreeNode class to create a new AVLTreeNode object with the same
|
|
45
|
-
* id, value, and count.
|
|
46
|
-
* @returns The method is returning a new instance of the AVLTreeNode class with the same id, val, and count values as
|
|
47
|
-
* the current instance.
|
|
48
|
-
*/
|
|
49
|
-
AVLTreeNode.prototype.clone = function () {
|
|
50
|
-
return new AVLTreeNode(this.id, this.val, this.count);
|
|
51
|
-
};
|
|
52
43
|
return AVLTreeNode;
|
|
53
44
|
}(bst_1.BSTNode));
|
|
54
45
|
exports.AVLTreeNode = AVLTreeNode;
|
|
@@ -57,20 +48,21 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
57
48
|
function AVLTree() {
|
|
58
49
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
59
50
|
}
|
|
60
|
-
AVLTree.prototype.
|
|
61
|
-
|
|
51
|
+
AVLTree.prototype._createNode = function (id, val, count) {
|
|
52
|
+
var node = new AVLTreeNode(id, val, count);
|
|
53
|
+
return node;
|
|
62
54
|
};
|
|
63
55
|
/**
|
|
64
56
|
* The function overrides the add method of a Binary Search Tree to insert a node with a given id and value, and then
|
|
65
57
|
* balances the tree.
|
|
66
58
|
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add or
|
|
67
59
|
* update in the AVL tree.
|
|
68
|
-
* @param {
|
|
69
|
-
* `id`. It can be of type `
|
|
60
|
+
* @param {N | null} val - The `val` parameter represents the value that you want to assign to the node with the given
|
|
61
|
+
* `id`. It can be of type `N` (the generic type) or `null`.
|
|
70
62
|
* @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
|
|
71
63
|
* of times the value `val` should be inserted into the AVL tree. If the `count` parameter is not provided, it defaults
|
|
72
64
|
* to `1`, indicating that the value should be inserted once.
|
|
73
|
-
* @returns The method is returning either an
|
|
65
|
+
* @returns The method is returning either an N object or null.
|
|
74
66
|
*/
|
|
75
67
|
AVLTree.prototype.add = function (id, val, count) {
|
|
76
68
|
var inserted = _super.prototype.add.call(this, id, val, count);
|
|
@@ -86,7 +78,7 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
86
78
|
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
87
79
|
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
88
80
|
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
89
|
-
* @returns The method is returning an array of `AVLTreeDeleted<
|
|
81
|
+
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
90
82
|
*/
|
|
91
83
|
AVLTree.prototype.remove = function (id, isUpdateAllLeftSum) {
|
|
92
84
|
var e_1, _a;
|
|
@@ -111,7 +103,7 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
111
103
|
/**
|
|
112
104
|
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
113
105
|
* height of its right subtree.
|
|
114
|
-
* @param node - The parameter "node" is of type
|
|
106
|
+
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
115
107
|
* @returns The balance factor of the given AVL tree node.
|
|
116
108
|
*/
|
|
117
109
|
AVLTree.prototype.balanceFactor = function (node) {
|
|
@@ -187,7 +179,7 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
187
179
|
B.parent = parentOfA;
|
|
188
180
|
if (A === this.root) {
|
|
189
181
|
if (B)
|
|
190
|
-
this.
|
|
182
|
+
this._setRoot(B);
|
|
191
183
|
}
|
|
192
184
|
else {
|
|
193
185
|
if ((parentOfA === null || parentOfA === void 0 ? void 0 : parentOfA.left) === A) {
|
|
@@ -232,7 +224,7 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
232
224
|
}
|
|
233
225
|
if (A === this.root) {
|
|
234
226
|
if (C)
|
|
235
|
-
this.
|
|
227
|
+
this._setRoot(C);
|
|
236
228
|
}
|
|
237
229
|
else {
|
|
238
230
|
if (parentOfA) {
|
|
@@ -271,7 +263,7 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
271
263
|
}
|
|
272
264
|
if (A === this.root) {
|
|
273
265
|
if (B)
|
|
274
|
-
this.
|
|
266
|
+
this._setRoot(B);
|
|
275
267
|
}
|
|
276
268
|
else {
|
|
277
269
|
if (parentOfA) {
|
|
@@ -315,7 +307,7 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
315
307
|
}
|
|
316
308
|
if (A === this.root) {
|
|
317
309
|
if (C)
|
|
318
|
-
this.
|
|
310
|
+
this._setRoot(C);
|
|
319
311
|
}
|
|
320
312
|
else {
|
|
321
313
|
if (parentOfA) {
|
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export declare class BinaryIndexedTree {
|
|
9
|
-
private readonly _sumTree;
|
|
10
9
|
/**
|
|
11
10
|
* The constructor initializes an array with a specified length and fills it with zeros.
|
|
12
11
|
* @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
|
|
@@ -14,6 +13,8 @@ export declare class BinaryIndexedTree {
|
|
|
14
13
|
* The size of the sum tree array is `n + 1` because
|
|
15
14
|
*/
|
|
16
15
|
constructor(n: number);
|
|
16
|
+
private _sumTree;
|
|
17
|
+
get sumTree(): number[];
|
|
17
18
|
static lowBit(x: number): number;
|
|
18
19
|
/**
|
|
19
20
|
* The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
|
|
@@ -41,4 +42,5 @@ export declare class BinaryIndexedTree {
|
|
|
41
42
|
* @returns the sum of the elements in the range specified by the start and end indices.
|
|
42
43
|
*/
|
|
43
44
|
getRangeSum(start: number, end: number): number;
|
|
45
|
+
protected _setSumTree(value: number[]): void;
|
|
44
46
|
}
|