min-heap-typed 1.40.0-rc → 1.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dependency-cruiser.js +422 -422
- package/.eslintrc.js +59 -59
- package/.prettierrc.js +14 -14
- package/README.md +20 -3
- package/coverage/clover.xml +11 -7
- package/coverage/coverage-final.json +95 -1
- package/coverage/coverage-summary.json +59 -2
- package/coverage/lcov-report/base.css +278 -99
- package/coverage/lcov-report/index.html +69 -65
- package/coverage/lcov-report/index.ts.html +36 -35
- package/coverage/lcov-report/sorter.js +15 -5
- package/dist/data-structures/binary-tree/avl-tree.d.ts +106 -0
- package/dist/data-structures/binary-tree/avl-tree.js +347 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +355 -0
- package/dist/data-structures/binary-tree/binary-tree.js +1115 -0
- package/dist/data-structures/binary-tree/bst.d.ts +167 -0
- package/dist/data-structures/binary-tree/bst.js +512 -0
- package/dist/data-structures/binary-tree/index.d.ts +7 -0
- package/dist/data-structures/binary-tree/index.js +23 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -0
- package/dist/data-structures/binary-tree/rb-tree.js +21 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
- package/dist/data-structures/binary-tree/segment-tree.js +180 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +126 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +355 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +313 -0
- package/dist/data-structures/graph/abstract-graph.js +884 -0
- package/dist/data-structures/graph/directed-graph.d.ts +194 -0
- package/dist/data-structures/graph/directed-graph.js +404 -0
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/index.js +20 -0
- package/dist/data-structures/graph/map-graph.d.ts +73 -0
- package/dist/data-structures/graph/map-graph.js +93 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +120 -0
- package/dist/data-structures/graph/undirected-graph.js +239 -0
- package/dist/data-structures/hash/coordinate-map.d.ts +44 -0
- package/dist/data-structures/hash/coordinate-map.js +62 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +36 -0
- package/dist/data-structures/hash/coordinate-set.js +52 -0
- package/dist/data-structures/hash/hash-map.d.ts +50 -0
- package/dist/data-structures/hash/hash-map.js +153 -0
- package/dist/data-structures/hash/hash-table.d.ts +103 -0
- package/dist/data-structures/hash/hash-table.js +236 -0
- package/dist/data-structures/hash/index.d.ts +6 -0
- package/dist/data-structures/hash/index.js +22 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.js +6 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.js +6 -0
- package/dist/data-structures/heap/heap.d.ts +235 -0
- package/dist/data-structures/heap/heap.js +515 -0
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.d.ts +15 -0
- package/dist/data-structures/heap/max-heap.js +26 -0
- package/dist/data-structures/heap/min-heap.d.ts +15 -0
- package/dist/data-structures/heap/min-heap.js +26 -0
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/index.js +27 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +253 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +569 -0
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/index.js +19 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +232 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +533 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +80 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +187 -0
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/index.js +20 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +28 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
- package/dist/data-structures/matrix/matrix2d.js +199 -0
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +106 -0
- package/dist/data-structures/matrix/vector2d.d.ts +200 -0
- package/dist/data-structures/matrix/vector2d.js +290 -0
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/priority-queue.js +17 -0
- package/dist/data-structures/queue/deque.d.ts +161 -0
- package/dist/data-structures/queue/deque.js +264 -0
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/index.js +18 -0
- package/dist/data-structures/queue/queue.d.ts +122 -0
- package/dist/data-structures/queue/queue.js +187 -0
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.d.ts +64 -0
- package/dist/data-structures/stack/stack.js +94 -0
- 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 +8 -0
- package/dist/data-structures/tree/tree.js +40 -0
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.d.ts +79 -0
- package/dist/data-structures/trie/trie.js +251 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +18 -4
- package/dist/interfaces/binary-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/graph.d.ts +5 -0
- package/dist/interfaces/graph.js +2 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +8 -0
- package/dist/interfaces/index.js +24 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
- package/dist/types/data-structures/binary-tree/bst.js +2 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/index.js +22 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
- package/dist/types/data-structures/graph/abstract-graph.js +2 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/graph/directed-graph.js +9 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/index.js +19 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/map-graph.js +2 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/undirected-graph.js +2 -0
- package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-map.js +2 -0
- package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-set.js +2 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-map.js +2 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-table.js +2 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/hash/index.js +2 -0
- package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-map.js +2 -0
- package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-set.js +2 -0
- package/dist/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.js +2 -0
- package/dist/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/data-structures/heap/index.js +17 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/max-heap.js +2 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/min-heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +11 -0
- package/dist/types/data-structures/index.js +27 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/index.js +18 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +17 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix.js +2 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix2d.js +2 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/data-structures/matrix/navigator.js +2 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/vector2d.js +2 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/index.js +19 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/deque.js +2 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/index.js +18 -0
- package/dist/types/data-structures/queue/queue.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.js +2 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/index.js +17 -0
- package/dist/types/data-structures/stack/stack.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.js +2 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/index.js +17 -0
- package/dist/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/data-structures/tree/tree.js +2 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/index.js +17 -0
- package/dist/types/data-structures/trie/trie.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.js +2 -0
- package/dist/types/helpers.d.ts +8 -0
- package/dist/types/helpers.js +9 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +20 -0
- package/dist/utils/utils.js +73 -0
- package/jest.config.js +6 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +350 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1262 -0
- package/src/data-structures/binary-tree/bst.ts +522 -0
- package/src/data-structures/binary-tree/index.ts +7 -0
- package/src/data-structures/binary-tree/rb-tree.ts +358 -0
- package/src/data-structures/binary-tree/segment-tree.ts +190 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +379 -0
- package/src/data-structures/graph/abstract-graph.ts +1000 -0
- package/src/data-structures/graph/directed-graph.ts +449 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +106 -0
- package/src/data-structures/graph/undirected-graph.ts +259 -0
- package/src/data-structures/hash/coordinate-map.ts +63 -0
- package/src/data-structures/hash/coordinate-set.ts +52 -0
- package/src/data-structures/hash/hash-map.ts +185 -0
- package/src/data-structures/hash/hash-table.ts +268 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/tree-map.ts +2 -0
- package/src/data-structures/hash/tree-set.ts +2 -0
- package/src/data-structures/heap/heap.ts +589 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +26 -0
- package/src/data-structures/heap/min-heap.ts +26 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +605 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +576 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +221 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +211 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +315 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/priority-queue.ts +16 -0
- package/src/data-structures/queue/deque.ts +282 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +209 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +102 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +41 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +262 -0
- package/src/index.ts +4 -1
- package/src/interfaces/binary-tree.ts +10 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/graph.ts +7 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +8 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
- package/src/types/data-structures/binary-tree/bst.ts +11 -0
- package/src/types/data-structures/binary-tree/index.ts +6 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
- package/src/types/data-structures/graph/abstract-graph.ts +11 -0
- package/src/types/data-structures/graph/directed-graph.ts +8 -0
- package/src/types/data-structures/graph/index.ts +3 -0
- package/src/types/data-structures/graph/map-graph.ts +1 -0
- package/src/types/data-structures/graph/undirected-graph.ts +1 -0
- package/src/types/data-structures/hash/coordinate-map.ts +1 -0
- package/src/types/data-structures/hash/coordinate-set.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +1 -0
- package/src/types/data-structures/hash/hash-table.ts +1 -0
- package/src/types/data-structures/hash/index.ts +1 -0
- package/src/types/data-structures/hash/tree-map.ts +1 -0
- package/src/types/data-structures/hash/tree-set.ts +1 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/heap/index.ts +1 -0
- package/src/types/data-structures/heap/max-heap.ts +1 -0
- package/src/types/data-structures/heap/min-heap.ts +1 -0
- package/src/types/data-structures/index.ts +11 -0
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/index.ts +2 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +1 -0
- package/src/types/data-structures/matrix/matrix2d.ts +1 -0
- package/src/types/data-structures/matrix/navigator.ts +14 -0
- package/src/types/data-structures/matrix/vector2d.ts +1 -0
- package/src/types/data-structures/priority-queue/index.ts +3 -0
- package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/data-structures/queue/deque.ts +1 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +1 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +1 -0
- package/src/types/data-structures/tree/index.ts +1 -0
- package/src/types/data-structures/tree/tree.ts +1 -0
- package/src/types/data-structures/trie/index.ts +1 -0
- package/src/types/data-structures/trie/trie.ts +1 -0
- package/src/types/helpers.ts +11 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +86 -0
- package/tsconfig.json +1 -2
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import {BTNKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types';
|
|
2
|
+
import {IBinaryTree} from '../../interfaces';
|
|
3
|
+
import {BST, BSTNode} from './bst';
|
|
4
|
+
|
|
5
|
+
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
6
|
+
constructor(key: BTNKey, value?: V) {
|
|
7
|
+
super(key, value);
|
|
8
|
+
this.color = RBColor.RED;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
color: RBColor;
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
|
|
16
|
+
extends BST<V, N>
|
|
17
|
+
implements IBinaryTree<V, N> {
|
|
18
|
+
constructor(options?: RBTreeOptions) {
|
|
19
|
+
super(options);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
override createNode(key: BTNKey, value?: V): N {
|
|
23
|
+
return new RBTreeNode(key, value) as N;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
27
|
+
// const inserted = super.add(keyOrNode, value);
|
|
28
|
+
// if (inserted) this._fixInsertViolation(inserted);
|
|
29
|
+
// return inserted;
|
|
30
|
+
// }
|
|
31
|
+
//
|
|
32
|
+
// // Method for fixing insert violations in a red-black tree
|
|
33
|
+
// protected _fixInsertViolation(node: N) {
|
|
34
|
+
// while (node !== this.root! && node.color === RBColor.RED && node.parent!.color === RBColor.RED) {
|
|
35
|
+
// const parent = node.parent!;
|
|
36
|
+
// const grandparent = parent.parent!;
|
|
37
|
+
// let uncle: N | null | undefined = null;
|
|
38
|
+
//
|
|
39
|
+
// if (parent === grandparent.left) {
|
|
40
|
+
// uncle = grandparent.right;
|
|
41
|
+
//
|
|
42
|
+
// // Case 1: The uncle node is red
|
|
43
|
+
// if (uncle && uncle.color === RBColor.RED) {
|
|
44
|
+
// grandparent.color = RBColor.RED;
|
|
45
|
+
// parent.color = RBColor.BLACK;
|
|
46
|
+
// uncle.color = RBColor.BLACK;
|
|
47
|
+
// node = grandparent;
|
|
48
|
+
// } else {
|
|
49
|
+
// // Case 2: The uncle node is black, and the current node is a right child
|
|
50
|
+
// if (node === parent.right) {
|
|
51
|
+
// this._rotateLeft(parent);
|
|
52
|
+
// node = parent;
|
|
53
|
+
// // Update parent reference
|
|
54
|
+
// node.parent = grandparent;
|
|
55
|
+
// parent.parent = node;
|
|
56
|
+
// }
|
|
57
|
+
//
|
|
58
|
+
// // Case 3: The uncle node is black, and the current node is a left child
|
|
59
|
+
// parent.color = RBColor.BLACK;
|
|
60
|
+
// grandparent.color = RBColor.RED;
|
|
61
|
+
// this._rotateRight(grandparent);
|
|
62
|
+
// }
|
|
63
|
+
// } else {
|
|
64
|
+
// // Symmetric case: The parent is the right child of the grandparent
|
|
65
|
+
// uncle = grandparent.left;
|
|
66
|
+
//
|
|
67
|
+
// // Case 1: The uncle node is red
|
|
68
|
+
// if (uncle && uncle.color === RBColor.RED) {
|
|
69
|
+
// grandparent.color = RBColor.RED;
|
|
70
|
+
// parent.color = RBColor.BLACK;
|
|
71
|
+
// uncle.color = RBColor.BLACK;
|
|
72
|
+
// node = grandparent;
|
|
73
|
+
// } else {
|
|
74
|
+
// // Case 2: The uncle node is black, and the current node is a left child
|
|
75
|
+
// if (node === parent.left) {
|
|
76
|
+
// this._rotateRight(parent);
|
|
77
|
+
// node = parent;
|
|
78
|
+
// // Update parent reference
|
|
79
|
+
// node.parent = grandparent;
|
|
80
|
+
// parent.parent = node;
|
|
81
|
+
// }
|
|
82
|
+
//
|
|
83
|
+
// // Case 3: The uncle node is black, and the current node is a right child
|
|
84
|
+
// parent.color = RBColor.BLACK;
|
|
85
|
+
// grandparent.color = RBColor.RED;
|
|
86
|
+
// this._rotateLeft(grandparent);
|
|
87
|
+
// }
|
|
88
|
+
// }
|
|
89
|
+
// }
|
|
90
|
+
//
|
|
91
|
+
// // The root node is always black
|
|
92
|
+
// this.root!.color = RBColor.BLACK;
|
|
93
|
+
// }
|
|
94
|
+
//
|
|
95
|
+
// // Left rotation operation
|
|
96
|
+
// protected _rotateLeft(node: N) {
|
|
97
|
+
// const rightChild = node.right;
|
|
98
|
+
// node.right = rightChild!.left;
|
|
99
|
+
//
|
|
100
|
+
// if (rightChild!.left) {
|
|
101
|
+
// rightChild!.left.parent = node;
|
|
102
|
+
// }
|
|
103
|
+
//
|
|
104
|
+
// rightChild!.parent = node.parent;
|
|
105
|
+
//
|
|
106
|
+
// if (node === this.root) {
|
|
107
|
+
// // @ts-ignore
|
|
108
|
+
// this._setRoot(rightChild);
|
|
109
|
+
// } else if (node === node.parent!.left) {
|
|
110
|
+
// node.parent!.left = rightChild;
|
|
111
|
+
// } else {
|
|
112
|
+
// node.parent!.right = rightChild;
|
|
113
|
+
// }
|
|
114
|
+
//
|
|
115
|
+
// rightChild!.left = node;
|
|
116
|
+
// node.parent = rightChild;
|
|
117
|
+
// }
|
|
118
|
+
//
|
|
119
|
+
// // Right rotation operation
|
|
120
|
+
// protected _rotateRight(node: N) {
|
|
121
|
+
// const leftChild = node.left;
|
|
122
|
+
// node.left = leftChild!.right;
|
|
123
|
+
//
|
|
124
|
+
// if (leftChild!.right) {
|
|
125
|
+
// leftChild!.right.parent = node;
|
|
126
|
+
// }
|
|
127
|
+
//
|
|
128
|
+
// leftChild!.parent = node.parent;
|
|
129
|
+
//
|
|
130
|
+
// if (node === this.root) {
|
|
131
|
+
// // @ts-ignore
|
|
132
|
+
// this._setRoot(leftChild);
|
|
133
|
+
// } else if (node === node.parent!.right) {
|
|
134
|
+
// node.parent!.right = leftChild;
|
|
135
|
+
// } else {
|
|
136
|
+
// node.parent!.left = leftChild;
|
|
137
|
+
// }
|
|
138
|
+
//
|
|
139
|
+
// leftChild!.right = node;
|
|
140
|
+
// node.parent = leftChild;
|
|
141
|
+
// }
|
|
142
|
+
//
|
|
143
|
+
// protected _isNodeRed(node: N | null | undefined): boolean {
|
|
144
|
+
// return node ? node.color === RBColor.RED : false;
|
|
145
|
+
// }
|
|
146
|
+
//
|
|
147
|
+
// // Find the sibling node
|
|
148
|
+
// protected _findSibling(node: N): N | null | undefined {
|
|
149
|
+
// if (!node.parent) {
|
|
150
|
+
// return undefined;
|
|
151
|
+
// }
|
|
152
|
+
//
|
|
153
|
+
// return node === node.parent.left ? node.parent.right : node.parent.left;
|
|
154
|
+
// }
|
|
155
|
+
//
|
|
156
|
+
// // Remove a node
|
|
157
|
+
// protected _removeNode(node: N, replacement: N | null | undefined): void {
|
|
158
|
+
// if (node === this.root && !replacement) {
|
|
159
|
+
// // If there's only the root node and no replacement, simply delete the root node
|
|
160
|
+
// this._setRoot(null);
|
|
161
|
+
// } else if (node === this.root || this._isNodeRed(node)) {
|
|
162
|
+
// // If the node is the root or a red node, delete it directly
|
|
163
|
+
// if (node.parent!.left === node) {
|
|
164
|
+
// node.parent!.left = replacement;
|
|
165
|
+
// } else {
|
|
166
|
+
// node.parent!.right = replacement;
|
|
167
|
+
// }
|
|
168
|
+
//
|
|
169
|
+
// if (replacement) {
|
|
170
|
+
// replacement.parent = node.parent!;
|
|
171
|
+
// replacement.color = RBColor.BLACK; // Set the replacement node's color to black
|
|
172
|
+
// }
|
|
173
|
+
// } else {
|
|
174
|
+
// // If the node is a black node, perform removal and repair
|
|
175
|
+
// const sibling = this._findSibling(node);
|
|
176
|
+
//
|
|
177
|
+
// if (node.parent!.left === node) {
|
|
178
|
+
// node.parent!.left = replacement;
|
|
179
|
+
// } else {
|
|
180
|
+
// node.parent!.right = replacement;
|
|
181
|
+
// }
|
|
182
|
+
//
|
|
183
|
+
// if (replacement) {
|
|
184
|
+
// replacement.parent = node.parent;
|
|
185
|
+
// }
|
|
186
|
+
//
|
|
187
|
+
// if (!this._isNodeRed(sibling)) {
|
|
188
|
+
// // If the sibling node is black, perform repair
|
|
189
|
+
// this._fixDeleteViolation(replacement || node);
|
|
190
|
+
// }
|
|
191
|
+
// }
|
|
192
|
+
//
|
|
193
|
+
// if (node.parent) {
|
|
194
|
+
// node.parent = null;
|
|
195
|
+
// }
|
|
196
|
+
// node.left = null;
|
|
197
|
+
// node.right = null;
|
|
198
|
+
// }
|
|
199
|
+
//
|
|
200
|
+
// override delete(keyOrNode: BTNKey | N): BinaryTreeDeletedResult<N>[] {
|
|
201
|
+
// const node = this.get(keyOrNode);
|
|
202
|
+
// const result: BinaryTreeDeletedResult<N>[] = [{deleted: undefined, needBalanced: null}];
|
|
203
|
+
// if (!node) return result; // Node does not exist
|
|
204
|
+
//
|
|
205
|
+
// const replacement = this._getReplacementNode(node);
|
|
206
|
+
//
|
|
207
|
+
// const isRed = this._isNodeRed(node);
|
|
208
|
+
// const isRedReplacement = this._isNodeRed(replacement);
|
|
209
|
+
//
|
|
210
|
+
// // Remove the node
|
|
211
|
+
// this._removeNode(node, replacement);
|
|
212
|
+
//
|
|
213
|
+
// if (isRed || isRedReplacement) {
|
|
214
|
+
// // If the removed node is red or the replacement node is red, no repair is needed
|
|
215
|
+
// return result;
|
|
216
|
+
// }
|
|
217
|
+
//
|
|
218
|
+
// // Repair any violation introduced by the removal
|
|
219
|
+
// this._fixDeleteViolation(replacement);
|
|
220
|
+
//
|
|
221
|
+
// return result;
|
|
222
|
+
// }
|
|
223
|
+
//
|
|
224
|
+
// // Repair operation after node deletion
|
|
225
|
+
// protected _fixDeleteViolation(node: N | null | undefined) {
|
|
226
|
+
// let sibling;
|
|
227
|
+
//
|
|
228
|
+
// while (node && node !== this.root && !this._isNodeRed(node)) {
|
|
229
|
+
// if (node === node.parent!.left) {
|
|
230
|
+
// sibling = node.parent!.right;
|
|
231
|
+
//
|
|
232
|
+
// if (sibling && this._isNodeRed(sibling)) {
|
|
233
|
+
// // Case 1: The sibling node is red
|
|
234
|
+
// sibling.color = RBColor.BLACK;
|
|
235
|
+
// node.parent!.color = RBColor.RED;
|
|
236
|
+
// this._rotateLeft(node.parent!);
|
|
237
|
+
// sibling = node.parent!.right;
|
|
238
|
+
// }
|
|
239
|
+
//
|
|
240
|
+
// if (!sibling) return;
|
|
241
|
+
//
|
|
242
|
+
// if (
|
|
243
|
+
// (!sibling.left || sibling.left.color === RBColor.BLACK) &&
|
|
244
|
+
// (!sibling.right || sibling.right.color === RBColor.BLACK)
|
|
245
|
+
// ) {
|
|
246
|
+
// // Case 2: The sibling node and its children are all black
|
|
247
|
+
// sibling.color = RBColor.RED;
|
|
248
|
+
// node = node.parent!;
|
|
249
|
+
// } else {
|
|
250
|
+
// if (!(sibling.right && this._isNodeRed(sibling.right))) {
|
|
251
|
+
// // Case 3: The sibling node is black, and the left child is red, the right child is black
|
|
252
|
+
// sibling.left!.color = RBColor.BLACK;
|
|
253
|
+
// sibling.color = RBColor.RED;
|
|
254
|
+
// this._rotateRight(sibling);
|
|
255
|
+
// sibling = node.parent!.right;
|
|
256
|
+
// }
|
|
257
|
+
//
|
|
258
|
+
// // Case 4: The sibling node is black, and the right child is red
|
|
259
|
+
// if (sibling) {
|
|
260
|
+
// sibling.color = node.parent!.color;
|
|
261
|
+
// }
|
|
262
|
+
// if (node.parent) {
|
|
263
|
+
// node.parent.color = RBColor.BLACK;
|
|
264
|
+
// }
|
|
265
|
+
// if (sibling!.right) {
|
|
266
|
+
// sibling!.right.color = RBColor.BLACK;
|
|
267
|
+
// }
|
|
268
|
+
// this._rotateLeft(node.parent!);
|
|
269
|
+
// node = this.root;
|
|
270
|
+
// }
|
|
271
|
+
// } else {
|
|
272
|
+
// // Symmetric case: The parent is the right child of the grandparent
|
|
273
|
+
// sibling = node.parent!.left;
|
|
274
|
+
//
|
|
275
|
+
// if (sibling && this._isNodeRed(sibling)) {
|
|
276
|
+
// // Case 1: The sibling node is red
|
|
277
|
+
// sibling.color = RBColor.BLACK;
|
|
278
|
+
// node.parent!.color = RBColor.RED;
|
|
279
|
+
// this._rotateRight(node.parent!);
|
|
280
|
+
// sibling = node.parent!.left;
|
|
281
|
+
// }
|
|
282
|
+
//
|
|
283
|
+
// if (!sibling) return;
|
|
284
|
+
//
|
|
285
|
+
// if (
|
|
286
|
+
// (!sibling.left || sibling.left.color === RBColor.BLACK) &&
|
|
287
|
+
// (!sibling.right || sibling.right.color === RBColor.BLACK)
|
|
288
|
+
// ) {
|
|
289
|
+
// // Case 2: The sibling node and its children are all black
|
|
290
|
+
// sibling.color = RBColor.RED;
|
|
291
|
+
// node = node.parent!;
|
|
292
|
+
// } else {
|
|
293
|
+
// if (!(sibling.left && this._isNodeRed(sibling.left))) {
|
|
294
|
+
// // Case 3: The sibling node is black, and the right child is red, the left child is black
|
|
295
|
+
// sibling.right!.color = RBColor.BLACK;
|
|
296
|
+
// sibling.color = RBColor.RED;
|
|
297
|
+
// this._rotateLeft(sibling);
|
|
298
|
+
// sibling = node.parent!.left;
|
|
299
|
+
// }
|
|
300
|
+
//
|
|
301
|
+
// // Case 4: The sibling node is black, and the left child is red
|
|
302
|
+
// if (sibling) {
|
|
303
|
+
// sibling.color = node.parent!.color;
|
|
304
|
+
// }
|
|
305
|
+
// if (node.parent) {
|
|
306
|
+
// node.parent.color = RBColor.BLACK;
|
|
307
|
+
// }
|
|
308
|
+
// if (sibling!.left) {
|
|
309
|
+
// sibling!.left.color = RBColor.BLACK;
|
|
310
|
+
// }
|
|
311
|
+
// this._rotateRight(node.parent!);
|
|
312
|
+
// node = this.root;
|
|
313
|
+
// }
|
|
314
|
+
// }
|
|
315
|
+
// }
|
|
316
|
+
//
|
|
317
|
+
// if (node) {
|
|
318
|
+
// node.color = RBColor.BLACK;
|
|
319
|
+
// }
|
|
320
|
+
// }
|
|
321
|
+
//
|
|
322
|
+
// protected _findMin(node: N): N {
|
|
323
|
+
// while (node.left) {
|
|
324
|
+
// node = node.left;
|
|
325
|
+
// }
|
|
326
|
+
// return node;
|
|
327
|
+
// }
|
|
328
|
+
//
|
|
329
|
+
// // Get the replacement node
|
|
330
|
+
// protected _getReplacementNode(node: N): N | null | undefined {
|
|
331
|
+
// if (node.left && node.right) {
|
|
332
|
+
// return this._findSuccessor(node);
|
|
333
|
+
// }
|
|
334
|
+
//
|
|
335
|
+
// if (!node.left && !node.right) {
|
|
336
|
+
// return null; // Return a fake node with color black
|
|
337
|
+
// }
|
|
338
|
+
//
|
|
339
|
+
// return node.left || node.right;
|
|
340
|
+
// }
|
|
341
|
+
//
|
|
342
|
+
// // Find the successor node
|
|
343
|
+
// protected _findSuccessor(node: N): N | null | undefined {
|
|
344
|
+
// if (node.right) {
|
|
345
|
+
// // If the node has a right child, find the minimum node in the right subtree as the successor
|
|
346
|
+
// return this._findMin(node.right);
|
|
347
|
+
// }
|
|
348
|
+
//
|
|
349
|
+
// // Otherwise, traverse upward until finding the first parent whose left child is the current node
|
|
350
|
+
// let parent = node.parent;
|
|
351
|
+
// while (parent && node === parent.right) {
|
|
352
|
+
// node = parent;
|
|
353
|
+
// parent = parent.parent;
|
|
354
|
+
// }
|
|
355
|
+
//
|
|
356
|
+
// return parent;
|
|
357
|
+
// }
|
|
358
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
start = 0;
|
|
13
|
+
end = 0;
|
|
14
|
+
value: SegmentTreeNodeVal | null = null;
|
|
15
|
+
sum = 0;
|
|
16
|
+
left: SegmentTreeNode | null = null;
|
|
17
|
+
right: SegmentTreeNode | null = null;
|
|
18
|
+
|
|
19
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | null) {
|
|
20
|
+
this.start = start;
|
|
21
|
+
this.end = end;
|
|
22
|
+
this.sum = sum;
|
|
23
|
+
this.value = value || null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class SegmentTree {
|
|
28
|
+
/**
|
|
29
|
+
* The constructor initializes the values, start, end, and root properties of an object.
|
|
30
|
+
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
|
|
31
|
+
* @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
|
|
32
|
+
* be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
|
|
33
|
+
* the beginning of the array.
|
|
34
|
+
* @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
|
|
35
|
+
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
36
|
+
*/
|
|
37
|
+
constructor(values: number[], start?: number, end?: number) {
|
|
38
|
+
start = start || 0;
|
|
39
|
+
end = end || values.length - 1;
|
|
40
|
+
this._values = values;
|
|
41
|
+
this._start = start;
|
|
42
|
+
this._end = end;
|
|
43
|
+
|
|
44
|
+
if (values.length > 0) {
|
|
45
|
+
this._root = this.build(start, end);
|
|
46
|
+
} else {
|
|
47
|
+
this._root = null;
|
|
48
|
+
this._values = [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected _values: number[] = [];
|
|
53
|
+
|
|
54
|
+
get values(): number[] {
|
|
55
|
+
return this._values;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
protected _start = 0;
|
|
59
|
+
|
|
60
|
+
get start(): number {
|
|
61
|
+
return this._start;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
protected _end: number;
|
|
65
|
+
|
|
66
|
+
get end(): number {
|
|
67
|
+
return this._end;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected _root: SegmentTreeNode | null;
|
|
71
|
+
|
|
72
|
+
get root(): SegmentTreeNode | null {
|
|
73
|
+
return this._root;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|
|
78
|
+
* the sum of values to each segment.
|
|
79
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
|
|
80
|
+
* building the segment tree.
|
|
81
|
+
* @param {number} end - The "end" parameter represents the ending index of the segment or range for which we want to
|
|
82
|
+
* build a segment tree.
|
|
83
|
+
* @returns a SegmentTreeNode object.
|
|
84
|
+
*/
|
|
85
|
+
build(start: number, end: number): SegmentTreeNode {
|
|
86
|
+
if (start > end) {
|
|
87
|
+
return new SegmentTreeNode(start, end, 0);
|
|
88
|
+
}
|
|
89
|
+
if (start === end) return new SegmentTreeNode(start, end, this._values[start]);
|
|
90
|
+
|
|
91
|
+
const mid = start + Math.floor((end - start) / 2);
|
|
92
|
+
const left = this.build(start, mid);
|
|
93
|
+
const right = this.build(mid + 1, end);
|
|
94
|
+
const cur = new SegmentTreeNode(start, end, left.sum + right.sum);
|
|
95
|
+
cur.left = left;
|
|
96
|
+
cur.right = right;
|
|
97
|
+
return cur;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
|
|
102
|
+
* @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
|
|
103
|
+
* updated.
|
|
104
|
+
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
105
|
+
* the `SegmentTreeNode` at the specified `index`.
|
|
106
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
107
|
+
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
108
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
109
|
+
* @returns The function does not return anything.
|
|
110
|
+
*/
|
|
111
|
+
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal) {
|
|
112
|
+
const root = this.root || null;
|
|
113
|
+
if (!root) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const dfs = (cur: SegmentTreeNode, index: number, sum: number, value?: SegmentTreeNodeVal) => {
|
|
117
|
+
if (cur.start === cur.end && cur.start === index) {
|
|
118
|
+
cur.sum = sum;
|
|
119
|
+
if (value !== undefined) cur.value = value;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
123
|
+
if (index <= mid) {
|
|
124
|
+
if (cur.left) {
|
|
125
|
+
dfs(cur.left, index, sum, value);
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
if (cur.right) {
|
|
129
|
+
dfs(cur.right, index, sum, value);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (cur.left && cur.right) {
|
|
133
|
+
cur.sum = cur.left.sum + cur.right.sum;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
dfs(root, index, sum, value);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
142
|
+
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
143
|
+
* @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
|
|
144
|
+
* calculate the sum.
|
|
145
|
+
* @returns The function `querySumByRange` returns a number.
|
|
146
|
+
*/
|
|
147
|
+
querySumByRange(indexA: number, indexB: number): number {
|
|
148
|
+
const root = this.root || null;
|
|
149
|
+
if (!root) {
|
|
150
|
+
return 0;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (indexA < 0 || indexB >= this.values.length || indexA > indexB) {
|
|
154
|
+
return NaN;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const dfs = (cur: SegmentTreeNode, i: number, j: number): number => {
|
|
158
|
+
if (i <= cur.start && j >= cur.end) {
|
|
159
|
+
// The range [i, j] completely covers the current node's range [cur.start, cur.end]
|
|
160
|
+
return cur.sum;
|
|
161
|
+
}
|
|
162
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
163
|
+
if (j <= mid) {
|
|
164
|
+
if (cur.left) {
|
|
165
|
+
return dfs(cur.left, i, j);
|
|
166
|
+
} else {
|
|
167
|
+
return NaN;
|
|
168
|
+
}
|
|
169
|
+
} else if (i > mid) {
|
|
170
|
+
if (cur.right) {
|
|
171
|
+
return dfs(cur.right, i, j);
|
|
172
|
+
} else {
|
|
173
|
+
return NaN;
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
// Query both left and right subtrees
|
|
177
|
+
let leftSum = 0;
|
|
178
|
+
let rightSum = 0;
|
|
179
|
+
if (cur.left) {
|
|
180
|
+
leftSum = dfs(cur.left, i, mid);
|
|
181
|
+
}
|
|
182
|
+
if (cur.right) {
|
|
183
|
+
rightSum = dfs(cur.right, mid + 1, j);
|
|
184
|
+
}
|
|
185
|
+
return leftSum + rightSum;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
return dfs(root, indexA, indexB);
|
|
189
|
+
}
|
|
190
|
+
}
|