data-structure-typed 1.18.0 → 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 +8 -16
- package/dist/data-structures/binary-tree/binary-tree.d.ts +99 -98
- package/dist/data-structures/binary-tree/binary-tree.js +70 -65
- package/dist/data-structures/binary-tree/bst.d.ts +21 -21
- package/dist/data-structures/binary-tree/bst.js +15 -17
- 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/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 -58
- package/dist/data-structures/graph/abstract-graph.js +84 -68
- package/dist/data-structures/graph/directed-graph.d.ts +127 -96
- package/dist/data-structures/graph/directed-graph.js +161 -109
- package/dist/data-structures/graph/undirected-graph.d.ts +82 -59
- package/dist/data-structures/graph/undirected-graph.js +99 -72
- package/dist/data-structures/hash/coordinate-set.d.ts +1 -1
- 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 -1
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- 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/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/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 +288 -287
- package/docs/classes/AVLTreeNode.html +106 -63
- package/docs/classes/AaTree.html +14 -12
- package/docs/classes/AbstractEdge.html +68 -34
- package/docs/classes/AbstractGraph.html +219 -114
- package/docs/classes/AbstractVertex.html +71 -30
- package/docs/classes/ArrayDeque.html +27 -25
- package/docs/classes/BST.html +279 -273
- package/docs/classes/BSTNode.html +106 -57
- package/docs/classes/BTree.html +14 -12
- package/docs/classes/BinaryIndexedTree.html +22 -20
- package/docs/classes/BinaryTree.html +283 -277
- package/docs/classes/BinaryTreeNode.html +111 -63
- package/docs/classes/Character.html +17 -15
- package/docs/classes/CoordinateMap.html +22 -20
- package/docs/classes/CoordinateSet.html +23 -21
- package/docs/classes/Deque.html +47 -45
- package/docs/classes/DirectedEdge.html +74 -41
- package/docs/classes/DirectedGraph.html +444 -208
- package/docs/classes/DirectedVertex.html +63 -36
- package/docs/classes/DoublyLinkedList.html +52 -50
- package/docs/classes/DoublyLinkedListNode.html +24 -22
- package/docs/classes/HashTable.html +14 -12
- package/docs/classes/Heap.html +29 -27
- package/docs/classes/HeapItem.html +21 -19
- package/docs/classes/Matrix2D.html +29 -27
- package/docs/classes/MatrixNTI2D.html +17 -15
- package/docs/classes/MaxHeap.html +29 -27
- package/docs/classes/MaxPriorityQueue.html +67 -60
- package/docs/classes/MinHeap.html +29 -27
- package/docs/classes/MinPriorityQueue.html +67 -60
- package/docs/classes/Navigator.html +24 -22
- package/docs/classes/ObjectDeque.html +62 -50
- package/docs/classes/Pair.html +14 -12
- package/docs/classes/PriorityQueue.html +62 -55
- package/docs/classes/Queue.html +29 -27
- package/docs/classes/SegmentTree.html +30 -28
- package/docs/classes/SegmentTreeNode.html +33 -31
- package/docs/classes/SinglyLinkedList.html +49 -47
- package/docs/classes/SinglyLinkedListNode.html +21 -19
- package/docs/classes/SkipLinkedList.html +14 -12
- package/docs/classes/SplayTree.html +14 -12
- package/docs/classes/Stack.html +27 -25
- package/docs/classes/TreeMap.html +14 -12
- package/docs/classes/TreeMultiSet.html +277 -270
- package/docs/classes/TreeNode.html +29 -27
- package/docs/classes/TreeSet.html +14 -12
- package/docs/classes/Trie.html +26 -24
- package/docs/classes/TrieNode.html +24 -22
- package/docs/classes/TwoThreeTree.html +14 -12
- package/docs/classes/UndirectedEdge.html +70 -51
- package/docs/classes/UndirectedGraph.html +344 -161
- package/docs/classes/UndirectedVertex.html +63 -36
- package/docs/classes/Vector2D.html +41 -39
- package/docs/enums/CP.html +17 -15
- package/docs/enums/FamilyPosition.html +17 -15
- package/docs/enums/LoopType.html +16 -14
- package/docs/{interfaces/AVLTreeDeleted.html → enums/TopologicalProperty.html} +43 -43
- package/docs/index.html +195 -68
- package/docs/interfaces/{HeapOptions.html → IBinaryTree.html} +39 -32
- package/docs/interfaces/IBinaryTreeNode.html +383 -0
- package/docs/interfaces/IDirectedGraph.html +20 -18
- package/docs/interfaces/IGraph.html +118 -88
- package/docs/interfaces/{PriorityQueueOptions.html → IUNDirectedGraph.html} +22 -53
- package/docs/modules.html +25 -21
- package/docs/types/{ToThunkFn.html → AVLTreeDeleted.html} +27 -21
- package/docs/types/BSTComparator.html +14 -12
- package/docs/types/BSTDeletedResult.html +19 -17
- package/docs/types/BinaryTreeDeleted.html +19 -17
- package/docs/types/BinaryTreeNodeId.html +14 -12
- package/docs/types/BinaryTreeNodePropertyName.html +14 -12
- package/docs/types/DFSOrderPattern.html +14 -12
- package/docs/types/DijkstraResult.html +14 -12
- package/docs/types/Direction.html +14 -12
- package/docs/types/{TrlFn.html → EdgeId.html} +18 -29
- package/docs/types/{TrlAsyncFn.html → HeapOptions.html} +29 -19
- package/docs/types/NavigatorParams.html +164 -0
- package/docs/types/NodeOrPropertyName.html +14 -12
- package/docs/types/PriorityQueueComparator.html +14 -12
- package/docs/types/PriorityQueueDFSOrderPattern.html +14 -12
- package/docs/types/{SpecifyOptional.html → PriorityQueueOptions.html} +28 -19
- package/docs/types/RecursiveAVLTreeNode.html +135 -0
- package/docs/types/{Thunk.html → RecursiveBSTNode.html} +23 -24
- package/docs/types/RecursiveBinaryTreeNode.html +135 -0
- package/docs/types/ResultByProperty.html +17 -15
- package/docs/types/ResultsByProperty.html +17 -15
- package/docs/types/SegmentTreeNodeVal.html +14 -12
- package/docs/types/TopologicalStatus.html +14 -12
- package/docs/types/TreeMultiSetDeletedResult.html +19 -17
- package/docs/types/Turning.html +14 -12
- package/docs/types/VertexId.html +14 -12
- package/notes/note.md +8 -1
- package/package.json +10 -2
- package/docs/classes/RBTree.html +0 -153
- package/docs/interfaces/NavigatorParams.html +0 -200
|
@@ -0,0 +1,243 @@
|
|
|
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
|
+
|
|
9
|
+
import type {SegmentTreeNodeVal} from '../types';
|
|
10
|
+
|
|
11
|
+
export class SegmentTreeNode {
|
|
12
|
+
constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null) {
|
|
13
|
+
this._start = start;
|
|
14
|
+
this._end = end;
|
|
15
|
+
this._sum = sum;
|
|
16
|
+
this._val = val || null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private _start = 0;
|
|
20
|
+
get start(): number {
|
|
21
|
+
return this._start;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
set start(v: number) {
|
|
25
|
+
this._start = v;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private _end = 0;
|
|
29
|
+
|
|
30
|
+
get end(): number {
|
|
31
|
+
return this._end;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
set end(v: number) {
|
|
35
|
+
this._end = v;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private _val: SegmentTreeNodeVal | null = null;
|
|
39
|
+
|
|
40
|
+
get val(): SegmentTreeNodeVal | null {
|
|
41
|
+
return this._val;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
set val(v: SegmentTreeNodeVal | null) {
|
|
45
|
+
this._val = v;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private _sum = 0;
|
|
49
|
+
|
|
50
|
+
get sum(): number {
|
|
51
|
+
return this._sum;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
set sum(v: number) {
|
|
55
|
+
this._sum = v;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private _left: SegmentTreeNode | null = null;
|
|
59
|
+
|
|
60
|
+
get left(): SegmentTreeNode | null {
|
|
61
|
+
return this._left;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
set left(v: SegmentTreeNode | null) {
|
|
65
|
+
this._left = v;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private _right: SegmentTreeNode | null = null;
|
|
69
|
+
|
|
70
|
+
get right(): SegmentTreeNode | null {
|
|
71
|
+
return this._right;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
set right(v: SegmentTreeNode | null) {
|
|
75
|
+
this._right = v;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export class SegmentTree {
|
|
80
|
+
/**
|
|
81
|
+
* The constructor initializes the values, start, end, and root properties of an object.
|
|
82
|
+
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
|
|
83
|
+
* @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
|
|
84
|
+
* be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
|
|
85
|
+
* the beginning of the array.
|
|
86
|
+
* @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
|
|
87
|
+
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
88
|
+
*/
|
|
89
|
+
constructor(values: number[], start?: number, end?: number) {
|
|
90
|
+
start = start || 0;
|
|
91
|
+
end = end || values.length - 1;
|
|
92
|
+
this._values = values;
|
|
93
|
+
this._start = start;
|
|
94
|
+
this._end = end;
|
|
95
|
+
this._root = this.build(start, end);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private _values: number[] = [];
|
|
99
|
+
|
|
100
|
+
get values(): number[] {
|
|
101
|
+
return this._values;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private _start = 0;
|
|
105
|
+
get start(): number {
|
|
106
|
+
return this._start;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private _end: number;
|
|
110
|
+
|
|
111
|
+
get end(): number {
|
|
112
|
+
return this._end;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private _root: SegmentTreeNode | null;
|
|
116
|
+
|
|
117
|
+
get root(): SegmentTreeNode | null {
|
|
118
|
+
return this._root;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The function builds a segment tree by recursively dividing the given range into smaller segments and creating nodes
|
|
123
|
+
* for each segment.
|
|
124
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
|
|
125
|
+
* building the segment tree.
|
|
126
|
+
* @param {number} end - The `end` parameter represents the ending index of the segment or range for which we are
|
|
127
|
+
* building the segment tree.
|
|
128
|
+
* @returns a SegmentTreeNode object.
|
|
129
|
+
*/
|
|
130
|
+
build(start: number, end: number): SegmentTreeNode {
|
|
131
|
+
if (start === end) return new SegmentTreeNode(start, end, this._values[start]);
|
|
132
|
+
|
|
133
|
+
const mid = start + Math.floor((end - start) / 2);
|
|
134
|
+
const left = this.build(start, mid);
|
|
135
|
+
const right = this.build(mid + 1, end);
|
|
136
|
+
const cur = new SegmentTreeNode(start, end, left.sum + right.sum);
|
|
137
|
+
cur.left = left;
|
|
138
|
+
cur.right = right;
|
|
139
|
+
return cur;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
|
|
144
|
+
* @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
|
|
145
|
+
* updated.
|
|
146
|
+
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
147
|
+
* the `SegmentTreeNode` at the specified `index`.
|
|
148
|
+
* @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
|
|
149
|
+
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
150
|
+
* cur.val = val;` and pass a value for `val` in the
|
|
151
|
+
* @returns The function does not return anything.
|
|
152
|
+
*/
|
|
153
|
+
updateNode(index: number, sum: number, val?: SegmentTreeNodeVal) {
|
|
154
|
+
const root = this.root || null;
|
|
155
|
+
if (!root) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const dfs = (cur: SegmentTreeNode, index: number, sum: number, val?: SegmentTreeNodeVal) => {
|
|
159
|
+
if (cur.start === cur.end && cur.start === index) {
|
|
160
|
+
cur.sum = sum;
|
|
161
|
+
// cur.val = val;
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
165
|
+
if (index <= mid) {
|
|
166
|
+
if (cur.left) {
|
|
167
|
+
dfs(cur.left, index, sum, val);
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
if (cur.right) {
|
|
171
|
+
dfs(cur.right, index, sum, val);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (cur.left && cur.right) {
|
|
175
|
+
cur.sum = cur.left.sum + cur.right.sum;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
dfs(root, index, sum);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
184
|
+
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
185
|
+
* @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
|
|
186
|
+
* calculate the sum.
|
|
187
|
+
* @returns The function `querySumByRange` returns a number.
|
|
188
|
+
*/
|
|
189
|
+
querySumByRange(indexA: number, indexB: number): number {
|
|
190
|
+
const root = this.root || null;
|
|
191
|
+
if (!root) {
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const dfs = (cur: SegmentTreeNode, i: number, j: number): number => {
|
|
196
|
+
if (cur.start === i && cur.end === j) {
|
|
197
|
+
return cur.sum;
|
|
198
|
+
}
|
|
199
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
200
|
+
if (j <= mid) {
|
|
201
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
202
|
+
if (cur.left) {
|
|
203
|
+
return dfs(cur.left, i, j);
|
|
204
|
+
} else {
|
|
205
|
+
return NaN;
|
|
206
|
+
}
|
|
207
|
+
} else if (i > mid) {
|
|
208
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
209
|
+
if (cur.right) {
|
|
210
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
211
|
+
return dfs(cur.right, i, j);
|
|
212
|
+
|
|
213
|
+
} else {
|
|
214
|
+
return NaN;
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
218
|
+
if (cur.left && cur.right) {
|
|
219
|
+
return dfs(cur.left, i, mid) + dfs(cur.right, mid + 1, j);
|
|
220
|
+
} else {
|
|
221
|
+
return NaN;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
return dfs(root, indexA, indexB);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
protected _setValues(value: number[]) {
|
|
229
|
+
this._values = value;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
protected _setStart(value: number) {
|
|
233
|
+
this._start = value;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
protected _setEnd(value: number) {
|
|
237
|
+
this._end = value;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
protected _setRoot(v: SegmentTreeNode | null) {
|
|
241
|
+
this._root = v;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
import {BST, BSTNode} from './bst';
|
|
9
|
+
import type {BinaryTreeNodeId, TreeMultiSetDeletedResult} from '../types';
|
|
10
|
+
import {IBinaryTree} from '../interfaces';
|
|
11
|
+
|
|
12
|
+
export class TreeMultiSet<N extends BSTNode<N['val'], N>> extends BST<N> implements IBinaryTree<N> {
|
|
13
|
+
/**
|
|
14
|
+
* The function creates a new BSTNode with the given id, value, and count.
|
|
15
|
+
* @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
|
|
16
|
+
* distinguish one node from another in the tree.
|
|
17
|
+
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
18
|
+
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
19
|
+
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
20
|
+
* @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
|
|
21
|
+
*/
|
|
22
|
+
override _createNode(id: BinaryTreeNodeId, val: N['val'], count?: number): N {
|
|
23
|
+
const node = new BSTNode<N['val'], N>(id, val, count);
|
|
24
|
+
return node as N;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The function overrides the add method of the BinarySearchTree class in TypeScript.
|
|
29
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to add.
|
|
30
|
+
* @param {N | null} val - The `val` parameter represents the value that you want to add to the binary search tree. It
|
|
31
|
+
* can be of type `N` (the generic type) or `null`.
|
|
32
|
+
* @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
|
|
33
|
+
* of times the value should be added to the binary search tree. If not provided, the default value is `undefined`.
|
|
34
|
+
* @returns The `add` method is returning a `BSTNode<N>` object or `null`.
|
|
35
|
+
*/
|
|
36
|
+
override add(id: BinaryTreeNodeId, val: N | null, count?: number): N | null {
|
|
37
|
+
return super.add(id, val, count);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The function overrides the remove method of the superclass and returns the result of calling the superclass's remove
|
|
42
|
+
* method.
|
|
43
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
44
|
+
* removed from the tree.
|
|
45
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean value that
|
|
46
|
+
* determines whether to update the left sum of all nodes in the tree after removing a node. If `isUpdateAllLeftSum` is
|
|
47
|
+
* set to `true`, the left sum of all nodes will be recalculated. If it
|
|
48
|
+
* @returns The method is returning an array of TreeMultiSetDeletedResult objects.
|
|
49
|
+
*/
|
|
50
|
+
override remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): TreeMultiSetDeletedResult<N>[] {
|
|
51
|
+
return super.remove(id, isUpdateAllLeftSum);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// 操作 常见名称 Ada Java JavaScript C++ Python Perl PHP Ruby // 尾部插入 inject, snoc Append offerLast push push_back append push
|
|
2
|
+
array_push push // 头部插入 push, cons Prepend offerFirst unshift push_front appendleft unshift array_unshift unshift //
|
|
3
|
+
尾部删除 eject Delete_Last pollLast pop pop_back pop pop array_pop pop // 头部删除 pop Delete_First pollFirst shift pop_front
|
|
4
|
+
popleft shift array_shift shift // 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end
|
|
5
|
+
last // 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first
|