tree-multimap-typed 1.42.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/.dependency-cruiser.js +449 -0
- package/.eslintrc.js +65 -0
- package/.prettierignore +6 -0
- package/.prettierrc.js +16 -0
- package/LICENSE +21 -0
- package/README.md +527 -0
- package/coverage/clover.xml +14 -0
- package/coverage/coverage-final.json +96 -0
- package/coverage/coverage-summary.json +60 -0
- package/coverage/lcov-report/base.css +403 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +120 -0
- package/coverage/lcov-report/index.ts.html +110 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +206 -0
- package/coverage/lcov.info +14 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +106 -0
- package/dist/data-structures/binary-tree/avl-tree.js +349 -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 +322 -0
- package/dist/data-structures/binary-tree/binary-tree.js +1305 -0
- package/dist/data-structures/binary-tree/bst.d.ts +175 -0
- package/dist/data-structures/binary-tree/bst.js +546 -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 +93 -0
- package/dist/data-structures/binary-tree/rb-tree.js +434 -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-multimap.d.ts +126 -0
- package/dist/data-structures/binary-tree/tree-multimap.js +358 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +347 -0
- package/dist/data-structures/graph/abstract-graph.js +936 -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 +10 -0
- package/dist/index.js +27 -0
- 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-multimap.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/tree-multimap.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/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +50 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1383 -0
- package/docs/classes/TreeMultiset.html +2376 -0
- package/docs/classes/TreeMultisetNode.html +313 -0
- package/docs/index.html +523 -0
- package/docs/modules.html +45 -0
- package/jest.config.js +8 -0
- package/package.json +184 -0
- package/src/data-structures/binary-tree/avl-tree.ts +352 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1558 -0
- package/src/data-structures/binary-tree/bst.ts +564 -0
- package/src/data-structures/binary-tree/index.ts +7 -0
- package/src/data-structures/binary-tree/rb-tree.ts +486 -0
- package/src/data-structures/binary-tree/segment-tree.ts +190 -0
- package/src/data-structures/binary-tree/tree-multimap.ts +381 -0
- package/src/data-structures/graph/abstract-graph.ts +1056 -0
- package/src/data-structures/graph/directed-graph.ts +450 -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 +260 -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 +1 -0
- package/src/data-structures/hash/tree-set.ts +1 -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 +314 -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 +281 -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 +11 -0
- 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-multimap.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/test/index.test.ts +461 -0
- package/tsconfig.json +38 -0
|
@@ -0,0 +1,486 @@
|
|
|
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 {
|
|
10
|
+
BinaryTreeDeletedResult,
|
|
11
|
+
BTNCallback,
|
|
12
|
+
BTNKey,
|
|
13
|
+
IterationType,
|
|
14
|
+
RBTNColor,
|
|
15
|
+
RBTreeNodeNested,
|
|
16
|
+
RBTreeOptions
|
|
17
|
+
} from '../../types';
|
|
18
|
+
import {BST, BSTNode} from "./bst";
|
|
19
|
+
import {IBinaryTree} from "../../interfaces";
|
|
20
|
+
import {BinaryTreeNode} from "./binary-tree";
|
|
21
|
+
|
|
22
|
+
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
23
|
+
color: RBTNColor;
|
|
24
|
+
constructor(key: BTNKey, value?: V, color: RBTNColor = RBTNColor.BLACK) {
|
|
25
|
+
super(key, value);
|
|
26
|
+
this.color = color;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 1. Each node is either red or black.
|
|
32
|
+
* 2. The root node is always black.
|
|
33
|
+
* 3. Leaf nodes are typically NIL nodes and are considered black.
|
|
34
|
+
* 4. Red nodes must have black children.
|
|
35
|
+
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
36
|
+
*/
|
|
37
|
+
export class RedBlackTree<V = any, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
|
|
38
|
+
extends BST<V, N>
|
|
39
|
+
implements IBinaryTree<V, N>
|
|
40
|
+
{
|
|
41
|
+
|
|
42
|
+
constructor(options?: RBTreeOptions) {
|
|
43
|
+
super(options);
|
|
44
|
+
this._root = this.NIL;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected _root: N;
|
|
48
|
+
|
|
49
|
+
get root(): N {
|
|
50
|
+
return this._root;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected _size: number = 0;
|
|
54
|
+
|
|
55
|
+
get size(): number {
|
|
56
|
+
return this._size;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
NIL: N = new RBTreeNode<V>(NaN) as unknown as N;
|
|
60
|
+
|
|
61
|
+
override add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined {
|
|
62
|
+
let node: N;
|
|
63
|
+
if (typeof keyOrNode === 'number') {
|
|
64
|
+
node = this.createNode(keyOrNode, value, RBTNColor.RED);
|
|
65
|
+
} else if(keyOrNode instanceof RBTreeNode) {
|
|
66
|
+
node = keyOrNode;
|
|
67
|
+
} else if (keyOrNode === null) {
|
|
68
|
+
return;
|
|
69
|
+
} else if (keyOrNode === undefined) {
|
|
70
|
+
return;
|
|
71
|
+
} else {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
node.left = this.NIL;
|
|
76
|
+
node.right = this.NIL;
|
|
77
|
+
|
|
78
|
+
let y: N | undefined = undefined;
|
|
79
|
+
let x: N | undefined = this.root;
|
|
80
|
+
|
|
81
|
+
while (x !== this.NIL) {
|
|
82
|
+
y = x;
|
|
83
|
+
if (x && node.key < x.key) {
|
|
84
|
+
x = x.left;
|
|
85
|
+
} else {
|
|
86
|
+
x = x?.right;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
node.parent = y;
|
|
91
|
+
if (y === undefined) {
|
|
92
|
+
this._setRoot(node);
|
|
93
|
+
} else if (node.key < y.key) {
|
|
94
|
+
y.left = node;
|
|
95
|
+
} else {
|
|
96
|
+
y.right = node;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (node.parent === undefined) {
|
|
100
|
+
node.color = RBTNColor.BLACK;
|
|
101
|
+
this._size++;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (node.parent.parent === undefined) {
|
|
106
|
+
this._size++;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
this._fixInsert(node);
|
|
111
|
+
this._size++;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
override createNode(key: BTNKey, value?: V, color: RBTNColor = RBTNColor.BLACK): N {
|
|
115
|
+
return new RBTreeNode<V, N>(key, value, color) as N;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
delete<C extends BTNCallback<N>>(
|
|
120
|
+
identifier: ReturnType<C> | null | undefined,
|
|
121
|
+
callback: C = this.defaultOneParamCallback as C
|
|
122
|
+
): BinaryTreeDeletedResult<N>[] {
|
|
123
|
+
const ans: BinaryTreeDeletedResult<N>[] = [];
|
|
124
|
+
if (identifier === null) return ans;
|
|
125
|
+
const helper = (node: N | undefined): void => {
|
|
126
|
+
let z: N = this.NIL;
|
|
127
|
+
let x: N | undefined, y: N;
|
|
128
|
+
while (node !== this.NIL) {
|
|
129
|
+
if (node && callback(node) === identifier) {
|
|
130
|
+
z = node;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (node && identifier && callback(node) <= identifier) {
|
|
134
|
+
node = node.right;
|
|
135
|
+
} else {
|
|
136
|
+
node = node?.left;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (z === this.NIL) {
|
|
141
|
+
this._size--;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
y = z;
|
|
146
|
+
let yOriginalColor: number = y.color;
|
|
147
|
+
if (z.left === this.NIL) {
|
|
148
|
+
x = z.right;
|
|
149
|
+
this._rbTransplant(z, z.right!);
|
|
150
|
+
} else if (z.right === this.NIL) {
|
|
151
|
+
x = z.left;
|
|
152
|
+
this._rbTransplant(z, z.left!);
|
|
153
|
+
} else {
|
|
154
|
+
y = this.getLeftMost(z.right);
|
|
155
|
+
yOriginalColor = y.color;
|
|
156
|
+
x = y.right;
|
|
157
|
+
if (y.parent === z) {
|
|
158
|
+
x!.parent = y;
|
|
159
|
+
} else {
|
|
160
|
+
this._rbTransplant(y, y.right!);
|
|
161
|
+
y.right = z.right;
|
|
162
|
+
y.right!.parent = y;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this._rbTransplant(z, y);
|
|
166
|
+
y.left = z.left;
|
|
167
|
+
y.left!.parent = y;
|
|
168
|
+
y.color = z.color;
|
|
169
|
+
}
|
|
170
|
+
if (yOriginalColor === RBTNColor.BLACK) {
|
|
171
|
+
this._fixDelete(x!);
|
|
172
|
+
}
|
|
173
|
+
this._size--;
|
|
174
|
+
};
|
|
175
|
+
helper(this.root);
|
|
176
|
+
// TODO
|
|
177
|
+
return ans;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
isNode(node: N | undefined): node is N {
|
|
181
|
+
return node !== this.NIL && node !== undefined;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
getNode<C extends BTNCallback<N, BTNKey>>(
|
|
185
|
+
identifier: BTNKey,
|
|
186
|
+
callback?: C,
|
|
187
|
+
beginRoot?: N | undefined,
|
|
188
|
+
iterationType?: IterationType
|
|
189
|
+
): N | undefined;
|
|
190
|
+
|
|
191
|
+
getNode<C extends BTNCallback<N, N>>(
|
|
192
|
+
identifier: N | undefined,
|
|
193
|
+
callback?: C,
|
|
194
|
+
beginRoot?: N | undefined,
|
|
195
|
+
iterationType?: IterationType
|
|
196
|
+
): N | undefined;
|
|
197
|
+
|
|
198
|
+
getNode<C extends BTNCallback<N>>(
|
|
199
|
+
identifier: ReturnType<C>,
|
|
200
|
+
callback: C,
|
|
201
|
+
beginRoot?: N | undefined,
|
|
202
|
+
iterationType?: IterationType
|
|
203
|
+
): N | undefined;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
207
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
208
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
209
|
+
* type.
|
|
210
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
211
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
212
|
+
* whether the node matches the criteria or not. The default callback function
|
|
213
|
+
* (`this.defaultOneParamCallback`) is used if no callback function is
|
|
214
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
215
|
+
* the root node from which the search should begin.
|
|
216
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
217
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
218
|
+
* @returns either the found node (of type N) or null if no node is found.
|
|
219
|
+
*/
|
|
220
|
+
getNode<C extends BTNCallback<N>>(
|
|
221
|
+
identifier: ReturnType<C> | undefined,
|
|
222
|
+
callback: C = this.defaultOneParamCallback as C,
|
|
223
|
+
beginRoot = this.root,
|
|
224
|
+
iterationType = this.iterationType
|
|
225
|
+
): N | null | undefined {
|
|
226
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
227
|
+
|
|
228
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* The function returns the leftmost node in a red-black tree.
|
|
233
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
234
|
+
* a Red-Black Tree.
|
|
235
|
+
* @returns The leftmost node in the given RBTreeNode.
|
|
236
|
+
*/
|
|
237
|
+
getLeftMost(node: N = this.root): N {
|
|
238
|
+
while (node.left !== undefined && node.left !== this.NIL) {
|
|
239
|
+
node = node.left;
|
|
240
|
+
}
|
|
241
|
+
return node;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* The function returns the rightmost node in a red-black tree.
|
|
246
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
247
|
+
* @returns the rightmost node in a red-black tree.
|
|
248
|
+
*/
|
|
249
|
+
getRightMost(node: N): N {
|
|
250
|
+
while (node.right !== undefined && node.right !== this.NIL) {
|
|
251
|
+
node = node.right;
|
|
252
|
+
}
|
|
253
|
+
return node;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* The function returns the successor of a given node in a red-black tree.
|
|
258
|
+
* @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
|
|
259
|
+
* @returns the successor of the given RBTreeNode.
|
|
260
|
+
*/
|
|
261
|
+
getSuccessor(x: N): N | undefined {
|
|
262
|
+
if (x.right !== this.NIL) {
|
|
263
|
+
return this.getLeftMost(x.right);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
let y: N | undefined = x.parent;
|
|
267
|
+
while (y !== this.NIL && y !== undefined && x === y.right) {
|
|
268
|
+
x = y;
|
|
269
|
+
y = y.parent;
|
|
270
|
+
}
|
|
271
|
+
return y;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The function returns the predecessor of a given node in a red-black tree.
|
|
276
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
277
|
+
* Red-Black Tree.
|
|
278
|
+
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
279
|
+
*/
|
|
280
|
+
getPredecessor(x: N): N {
|
|
281
|
+
if (x.left !== this.NIL) {
|
|
282
|
+
return this.getRightMost(x.left!);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
let y: N | undefined = x.parent;
|
|
286
|
+
while (y !== this.NIL && x === y!.left) {
|
|
287
|
+
x = y!;
|
|
288
|
+
y = y!.parent;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return y!;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
override clear() {
|
|
295
|
+
this._root = this.NIL;
|
|
296
|
+
this._size = 0;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
protected override _setRoot(v: N) {
|
|
300
|
+
if (v) {
|
|
301
|
+
v.parent = undefined;
|
|
302
|
+
}
|
|
303
|
+
this._root = v;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The function performs a left rotation on a red-black tree node.
|
|
308
|
+
* @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
|
|
309
|
+
*/
|
|
310
|
+
protected _leftRotate(x: N): void {
|
|
311
|
+
if (x.right) {
|
|
312
|
+
const y: N = x.right;
|
|
313
|
+
x.right = y.left;
|
|
314
|
+
if (y.left !== this.NIL) {
|
|
315
|
+
if (y.left) y.left.parent = x;
|
|
316
|
+
}
|
|
317
|
+
y.parent = x.parent;
|
|
318
|
+
if (x.parent === undefined) {
|
|
319
|
+
this._setRoot(y);
|
|
320
|
+
} else if (x === x.parent.left) {
|
|
321
|
+
x.parent.left = y;
|
|
322
|
+
} else {
|
|
323
|
+
x.parent.right = y;
|
|
324
|
+
}
|
|
325
|
+
y.left = x;
|
|
326
|
+
x.parent = y;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The function performs a right rotation on a red-black tree node.
|
|
332
|
+
* @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
|
|
333
|
+
* rotated.
|
|
334
|
+
*/
|
|
335
|
+
protected _rightRotate(x: N): void {
|
|
336
|
+
if (x.left) {
|
|
337
|
+
const y: N = x.left;
|
|
338
|
+
x.left = y.right;
|
|
339
|
+
if (y.right !== this.NIL) {
|
|
340
|
+
if (y.right) y.right.parent = x;
|
|
341
|
+
}
|
|
342
|
+
y.parent = x.parent;
|
|
343
|
+
if (x.parent === undefined) {
|
|
344
|
+
this._setRoot(y);
|
|
345
|
+
} else if (x === x.parent.right) {
|
|
346
|
+
x.parent.right = y;
|
|
347
|
+
} else {
|
|
348
|
+
x.parent.left = y;
|
|
349
|
+
}
|
|
350
|
+
y.right = x;
|
|
351
|
+
x.parent = y;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
|
|
357
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
358
|
+
* red-black tree.
|
|
359
|
+
*/
|
|
360
|
+
protected _fixDelete(x: N): void {
|
|
361
|
+
let s: N | undefined;
|
|
362
|
+
while (x !== this.root && x.color === RBTNColor.BLACK) {
|
|
363
|
+
if (x.parent && x === x.parent.left) {
|
|
364
|
+
s = x.parent.right!;
|
|
365
|
+
if (s.color === 1) {
|
|
366
|
+
s.color = RBTNColor.BLACK;
|
|
367
|
+
x.parent.color = RBTNColor.RED;
|
|
368
|
+
this._leftRotate(x.parent);
|
|
369
|
+
s = x.parent.right!;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (s.left !== undefined && s.left.color === RBTNColor.BLACK && s.right && s.right.color === RBTNColor.BLACK) {
|
|
373
|
+
s.color = RBTNColor.RED;
|
|
374
|
+
x = x.parent;
|
|
375
|
+
} else {
|
|
376
|
+
if (s.right && s.right.color === RBTNColor.BLACK) {
|
|
377
|
+
if (s.left) s.left.color = RBTNColor.BLACK;
|
|
378
|
+
s.color = RBTNColor.RED;
|
|
379
|
+
this._rightRotate(s);
|
|
380
|
+
s = x.parent.right;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (s) s.color = x.parent.color;
|
|
384
|
+
x.parent.color = RBTNColor.BLACK;
|
|
385
|
+
if (s && s.right) s.right.color = RBTNColor.BLACK;
|
|
386
|
+
this._leftRotate(x.parent);
|
|
387
|
+
x = this.root;
|
|
388
|
+
}
|
|
389
|
+
} else {
|
|
390
|
+
s = x.parent!.left!;
|
|
391
|
+
if (s.color === 1) {
|
|
392
|
+
s.color = RBTNColor.BLACK;
|
|
393
|
+
x.parent!.color = RBTNColor.RED;
|
|
394
|
+
this._rightRotate(x.parent!);
|
|
395
|
+
s = x.parent!.left;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (s && s.right && s.right.color === RBTNColor.BLACK && s.right.color === RBTNColor.BLACK) {
|
|
399
|
+
s.color = RBTNColor.RED;
|
|
400
|
+
x = x.parent!;
|
|
401
|
+
} else {
|
|
402
|
+
if (s && s.left && s.left.color === RBTNColor.BLACK) {
|
|
403
|
+
if (s.right) s.right.color = RBTNColor.BLACK;
|
|
404
|
+
s.color = RBTNColor.RED;
|
|
405
|
+
this._leftRotate(s);
|
|
406
|
+
s = x.parent!.left;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if (s) s.color = x.parent!.color;
|
|
410
|
+
x.parent!.color = RBTNColor.BLACK;
|
|
411
|
+
if (s && s.left) s.left.color = RBTNColor.BLACK;
|
|
412
|
+
this._rightRotate(x.parent!);
|
|
413
|
+
x = this.root;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
x.color = RBTNColor.BLACK;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
422
|
+
* @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
|
|
423
|
+
* @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
|
|
424
|
+
*/
|
|
425
|
+
protected _rbTransplant(u: N, v: N): void {
|
|
426
|
+
if (u.parent === undefined) {
|
|
427
|
+
this._setRoot(v);
|
|
428
|
+
} else if (u === u.parent.left) {
|
|
429
|
+
u.parent.left = v;
|
|
430
|
+
} else {
|
|
431
|
+
u.parent.right = v;
|
|
432
|
+
}
|
|
433
|
+
v.parent = u.parent;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
438
|
+
* @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
|
|
439
|
+
* red-black tree.
|
|
440
|
+
*/
|
|
441
|
+
protected _fixInsert(k: N): void {
|
|
442
|
+
let u: N | undefined;
|
|
443
|
+
while (k.parent && k.parent.color === 1) {
|
|
444
|
+
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
445
|
+
u = k.parent.parent.left;
|
|
446
|
+
if (u && u.color === 1) {
|
|
447
|
+
u.color = RBTNColor.BLACK;
|
|
448
|
+
k.parent.color = RBTNColor.BLACK;
|
|
449
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
450
|
+
k = k.parent.parent;
|
|
451
|
+
} else {
|
|
452
|
+
if (k === k.parent.left) {
|
|
453
|
+
k = k.parent;
|
|
454
|
+
this._rightRotate(k);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
k.parent!.color = RBTNColor.BLACK;
|
|
458
|
+
k.parent!.parent!.color = RBTNColor.RED;
|
|
459
|
+
this._leftRotate(k.parent!.parent!);
|
|
460
|
+
}
|
|
461
|
+
} else {
|
|
462
|
+
u = k.parent.parent!.right;
|
|
463
|
+
|
|
464
|
+
if (u && u.color === 1) {
|
|
465
|
+
u.color = RBTNColor.BLACK;
|
|
466
|
+
k.parent.color = RBTNColor.BLACK;
|
|
467
|
+
k.parent.parent!.color = RBTNColor.RED;
|
|
468
|
+
k = k.parent.parent!;
|
|
469
|
+
} else {
|
|
470
|
+
if (k === k.parent.right) {
|
|
471
|
+
k = k.parent;
|
|
472
|
+
this._leftRotate(k);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
k.parent!.color = RBTNColor.BLACK;
|
|
476
|
+
k.parent!.parent!.color = RBTNColor.RED;
|
|
477
|
+
this._rightRotate(k.parent!.parent!);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (k === this.root) {
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
this.root.color = RBTNColor.BLACK;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
@@ -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
|
+
}
|