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,434 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.RedBlackTree = exports.RBTreeNode = void 0;
|
|
11
|
+
const types_1 = require("../../types");
|
|
12
|
+
const bst_1 = require("./bst");
|
|
13
|
+
const binary_tree_1 = require("./binary-tree");
|
|
14
|
+
class RBTreeNode extends bst_1.BSTNode {
|
|
15
|
+
constructor(key, value, color = types_1.RBTNColor.BLACK) {
|
|
16
|
+
super(key, value);
|
|
17
|
+
this.color = color;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.RBTreeNode = RBTreeNode;
|
|
21
|
+
/**
|
|
22
|
+
* 1. Each node is either red or black.
|
|
23
|
+
* 2. The root node is always black.
|
|
24
|
+
* 3. Leaf nodes are typically NIL nodes and are considered black.
|
|
25
|
+
* 4. Red nodes must have black children.
|
|
26
|
+
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
27
|
+
*/
|
|
28
|
+
class RedBlackTree extends bst_1.BST {
|
|
29
|
+
constructor(options) {
|
|
30
|
+
super(options);
|
|
31
|
+
this._size = 0;
|
|
32
|
+
this.NIL = new RBTreeNode(NaN);
|
|
33
|
+
this._root = this.NIL;
|
|
34
|
+
}
|
|
35
|
+
get root() {
|
|
36
|
+
return this._root;
|
|
37
|
+
}
|
|
38
|
+
get size() {
|
|
39
|
+
return this._size;
|
|
40
|
+
}
|
|
41
|
+
add(keyOrNode, value) {
|
|
42
|
+
let node;
|
|
43
|
+
if (typeof keyOrNode === 'number') {
|
|
44
|
+
node = this.createNode(keyOrNode, value, types_1.RBTNColor.RED);
|
|
45
|
+
}
|
|
46
|
+
else if (keyOrNode instanceof RBTreeNode) {
|
|
47
|
+
node = keyOrNode;
|
|
48
|
+
}
|
|
49
|
+
else if (keyOrNode === null) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
else if (keyOrNode === undefined) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
node.left = this.NIL;
|
|
59
|
+
node.right = this.NIL;
|
|
60
|
+
let y = undefined;
|
|
61
|
+
let x = this.root;
|
|
62
|
+
while (x !== this.NIL) {
|
|
63
|
+
y = x;
|
|
64
|
+
if (x && node.key < x.key) {
|
|
65
|
+
x = x.left;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
x = x === null || x === void 0 ? void 0 : x.right;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
node.parent = y;
|
|
72
|
+
if (y === undefined) {
|
|
73
|
+
this._setRoot(node);
|
|
74
|
+
}
|
|
75
|
+
else if (node.key < y.key) {
|
|
76
|
+
y.left = node;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
y.right = node;
|
|
80
|
+
}
|
|
81
|
+
if (node.parent === undefined) {
|
|
82
|
+
node.color = types_1.RBTNColor.BLACK;
|
|
83
|
+
this._size++;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (node.parent.parent === undefined) {
|
|
87
|
+
this._size++;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this._fixInsert(node);
|
|
91
|
+
this._size++;
|
|
92
|
+
}
|
|
93
|
+
createNode(key, value, color = types_1.RBTNColor.BLACK) {
|
|
94
|
+
return new RBTreeNode(key, value, color);
|
|
95
|
+
}
|
|
96
|
+
delete(identifier, callback = this.defaultOneParamCallback) {
|
|
97
|
+
const ans = [];
|
|
98
|
+
if (identifier === null)
|
|
99
|
+
return ans;
|
|
100
|
+
const helper = (node) => {
|
|
101
|
+
let z = this.NIL;
|
|
102
|
+
let x, y;
|
|
103
|
+
while (node !== this.NIL) {
|
|
104
|
+
if (node && callback(node) === identifier) {
|
|
105
|
+
z = node;
|
|
106
|
+
}
|
|
107
|
+
if (node && identifier && callback(node) <= identifier) {
|
|
108
|
+
node = node.right;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
node = node === null || node === void 0 ? void 0 : node.left;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (z === this.NIL) {
|
|
115
|
+
this._size--;
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
y = z;
|
|
119
|
+
let yOriginalColor = y.color;
|
|
120
|
+
if (z.left === this.NIL) {
|
|
121
|
+
x = z.right;
|
|
122
|
+
this._rbTransplant(z, z.right);
|
|
123
|
+
}
|
|
124
|
+
else if (z.right === this.NIL) {
|
|
125
|
+
x = z.left;
|
|
126
|
+
this._rbTransplant(z, z.left);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
y = this.getLeftMost(z.right);
|
|
130
|
+
yOriginalColor = y.color;
|
|
131
|
+
x = y.right;
|
|
132
|
+
if (y.parent === z) {
|
|
133
|
+
x.parent = y;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
this._rbTransplant(y, y.right);
|
|
137
|
+
y.right = z.right;
|
|
138
|
+
y.right.parent = y;
|
|
139
|
+
}
|
|
140
|
+
this._rbTransplant(z, y);
|
|
141
|
+
y.left = z.left;
|
|
142
|
+
y.left.parent = y;
|
|
143
|
+
y.color = z.color;
|
|
144
|
+
}
|
|
145
|
+
if (yOriginalColor === types_1.RBTNColor.BLACK) {
|
|
146
|
+
this._fixDelete(x);
|
|
147
|
+
}
|
|
148
|
+
this._size--;
|
|
149
|
+
};
|
|
150
|
+
helper(this.root);
|
|
151
|
+
// TODO
|
|
152
|
+
return ans;
|
|
153
|
+
}
|
|
154
|
+
isNode(node) {
|
|
155
|
+
return node !== this.NIL && node !== undefined;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
159
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
160
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
161
|
+
* type.
|
|
162
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
163
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
164
|
+
* whether the node matches the criteria or not. The default callback function
|
|
165
|
+
* (`this.defaultOneParamCallback`) is used if no callback function is
|
|
166
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
167
|
+
* the root node from which the search should begin.
|
|
168
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
169
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
170
|
+
* @returns either the found node (of type N) or null if no node is found.
|
|
171
|
+
*/
|
|
172
|
+
getNode(identifier, callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
173
|
+
var _a;
|
|
174
|
+
if (identifier instanceof binary_tree_1.BinaryTreeNode)
|
|
175
|
+
callback = (node => node);
|
|
176
|
+
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* The function returns the leftmost node in a red-black tree.
|
|
180
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
181
|
+
* a Red-Black Tree.
|
|
182
|
+
* @returns The leftmost node in the given RBTreeNode.
|
|
183
|
+
*/
|
|
184
|
+
getLeftMost(node = this.root) {
|
|
185
|
+
while (node.left !== undefined && node.left !== this.NIL) {
|
|
186
|
+
node = node.left;
|
|
187
|
+
}
|
|
188
|
+
return node;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The function returns the rightmost node in a red-black tree.
|
|
192
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
193
|
+
* @returns the rightmost node in a red-black tree.
|
|
194
|
+
*/
|
|
195
|
+
getRightMost(node) {
|
|
196
|
+
while (node.right !== undefined && node.right !== this.NIL) {
|
|
197
|
+
node = node.right;
|
|
198
|
+
}
|
|
199
|
+
return node;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The function returns the successor of a given node in a red-black tree.
|
|
203
|
+
* @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
|
|
204
|
+
* @returns the successor of the given RBTreeNode.
|
|
205
|
+
*/
|
|
206
|
+
getSuccessor(x) {
|
|
207
|
+
if (x.right !== this.NIL) {
|
|
208
|
+
return this.getLeftMost(x.right);
|
|
209
|
+
}
|
|
210
|
+
let y = x.parent;
|
|
211
|
+
while (y !== this.NIL && y !== undefined && x === y.right) {
|
|
212
|
+
x = y;
|
|
213
|
+
y = y.parent;
|
|
214
|
+
}
|
|
215
|
+
return y;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* The function returns the predecessor of a given node in a red-black tree.
|
|
219
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
220
|
+
* Red-Black Tree.
|
|
221
|
+
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
222
|
+
*/
|
|
223
|
+
getPredecessor(x) {
|
|
224
|
+
if (x.left !== this.NIL) {
|
|
225
|
+
return this.getRightMost(x.left);
|
|
226
|
+
}
|
|
227
|
+
let y = x.parent;
|
|
228
|
+
while (y !== this.NIL && x === y.left) {
|
|
229
|
+
x = y;
|
|
230
|
+
y = y.parent;
|
|
231
|
+
}
|
|
232
|
+
return y;
|
|
233
|
+
}
|
|
234
|
+
clear() {
|
|
235
|
+
this._root = this.NIL;
|
|
236
|
+
this._size = 0;
|
|
237
|
+
}
|
|
238
|
+
_setRoot(v) {
|
|
239
|
+
if (v) {
|
|
240
|
+
v.parent = undefined;
|
|
241
|
+
}
|
|
242
|
+
this._root = v;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* The function performs a left rotation on a red-black tree node.
|
|
246
|
+
* @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
|
|
247
|
+
*/
|
|
248
|
+
_leftRotate(x) {
|
|
249
|
+
if (x.right) {
|
|
250
|
+
const y = x.right;
|
|
251
|
+
x.right = y.left;
|
|
252
|
+
if (y.left !== this.NIL) {
|
|
253
|
+
if (y.left)
|
|
254
|
+
y.left.parent = x;
|
|
255
|
+
}
|
|
256
|
+
y.parent = x.parent;
|
|
257
|
+
if (x.parent === undefined) {
|
|
258
|
+
this._setRoot(y);
|
|
259
|
+
}
|
|
260
|
+
else if (x === x.parent.left) {
|
|
261
|
+
x.parent.left = y;
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
x.parent.right = y;
|
|
265
|
+
}
|
|
266
|
+
y.left = x;
|
|
267
|
+
x.parent = y;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* The function performs a right rotation on a red-black tree node.
|
|
272
|
+
* @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
|
|
273
|
+
* rotated.
|
|
274
|
+
*/
|
|
275
|
+
_rightRotate(x) {
|
|
276
|
+
if (x.left) {
|
|
277
|
+
const y = x.left;
|
|
278
|
+
x.left = y.right;
|
|
279
|
+
if (y.right !== this.NIL) {
|
|
280
|
+
if (y.right)
|
|
281
|
+
y.right.parent = x;
|
|
282
|
+
}
|
|
283
|
+
y.parent = x.parent;
|
|
284
|
+
if (x.parent === undefined) {
|
|
285
|
+
this._setRoot(y);
|
|
286
|
+
}
|
|
287
|
+
else if (x === x.parent.right) {
|
|
288
|
+
x.parent.right = y;
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
x.parent.left = y;
|
|
292
|
+
}
|
|
293
|
+
y.right = x;
|
|
294
|
+
x.parent = y;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
|
|
299
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
300
|
+
* red-black tree.
|
|
301
|
+
*/
|
|
302
|
+
_fixDelete(x) {
|
|
303
|
+
let s;
|
|
304
|
+
while (x !== this.root && x.color === types_1.RBTNColor.BLACK) {
|
|
305
|
+
if (x.parent && x === x.parent.left) {
|
|
306
|
+
s = x.parent.right;
|
|
307
|
+
if (s.color === 1) {
|
|
308
|
+
s.color = types_1.RBTNColor.BLACK;
|
|
309
|
+
x.parent.color = types_1.RBTNColor.RED;
|
|
310
|
+
this._leftRotate(x.parent);
|
|
311
|
+
s = x.parent.right;
|
|
312
|
+
}
|
|
313
|
+
if (s.left !== undefined && s.left.color === types_1.RBTNColor.BLACK && s.right && s.right.color === types_1.RBTNColor.BLACK) {
|
|
314
|
+
s.color = types_1.RBTNColor.RED;
|
|
315
|
+
x = x.parent;
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
if (s.right && s.right.color === types_1.RBTNColor.BLACK) {
|
|
319
|
+
if (s.left)
|
|
320
|
+
s.left.color = types_1.RBTNColor.BLACK;
|
|
321
|
+
s.color = types_1.RBTNColor.RED;
|
|
322
|
+
this._rightRotate(s);
|
|
323
|
+
s = x.parent.right;
|
|
324
|
+
}
|
|
325
|
+
if (s)
|
|
326
|
+
s.color = x.parent.color;
|
|
327
|
+
x.parent.color = types_1.RBTNColor.BLACK;
|
|
328
|
+
if (s && s.right)
|
|
329
|
+
s.right.color = types_1.RBTNColor.BLACK;
|
|
330
|
+
this._leftRotate(x.parent);
|
|
331
|
+
x = this.root;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
s = x.parent.left;
|
|
336
|
+
if (s.color === 1) {
|
|
337
|
+
s.color = types_1.RBTNColor.BLACK;
|
|
338
|
+
x.parent.color = types_1.RBTNColor.RED;
|
|
339
|
+
this._rightRotate(x.parent);
|
|
340
|
+
s = x.parent.left;
|
|
341
|
+
}
|
|
342
|
+
if (s && s.right && s.right.color === types_1.RBTNColor.BLACK && s.right.color === types_1.RBTNColor.BLACK) {
|
|
343
|
+
s.color = types_1.RBTNColor.RED;
|
|
344
|
+
x = x.parent;
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
if (s && s.left && s.left.color === types_1.RBTNColor.BLACK) {
|
|
348
|
+
if (s.right)
|
|
349
|
+
s.right.color = types_1.RBTNColor.BLACK;
|
|
350
|
+
s.color = types_1.RBTNColor.RED;
|
|
351
|
+
this._leftRotate(s);
|
|
352
|
+
s = x.parent.left;
|
|
353
|
+
}
|
|
354
|
+
if (s)
|
|
355
|
+
s.color = x.parent.color;
|
|
356
|
+
x.parent.color = types_1.RBTNColor.BLACK;
|
|
357
|
+
if (s && s.left)
|
|
358
|
+
s.left.color = types_1.RBTNColor.BLACK;
|
|
359
|
+
this._rightRotate(x.parent);
|
|
360
|
+
x = this.root;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
x.color = types_1.RBTNColor.BLACK;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
368
|
+
* @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
|
|
369
|
+
* @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
|
|
370
|
+
*/
|
|
371
|
+
_rbTransplant(u, v) {
|
|
372
|
+
if (u.parent === undefined) {
|
|
373
|
+
this._setRoot(v);
|
|
374
|
+
}
|
|
375
|
+
else if (u === u.parent.left) {
|
|
376
|
+
u.parent.left = v;
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
u.parent.right = v;
|
|
380
|
+
}
|
|
381
|
+
v.parent = u.parent;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
385
|
+
* @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
|
|
386
|
+
* red-black tree.
|
|
387
|
+
*/
|
|
388
|
+
_fixInsert(k) {
|
|
389
|
+
let u;
|
|
390
|
+
while (k.parent && k.parent.color === 1) {
|
|
391
|
+
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
392
|
+
u = k.parent.parent.left;
|
|
393
|
+
if (u && u.color === 1) {
|
|
394
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
395
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
396
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
397
|
+
k = k.parent.parent;
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
if (k === k.parent.left) {
|
|
401
|
+
k = k.parent;
|
|
402
|
+
this._rightRotate(k);
|
|
403
|
+
}
|
|
404
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
405
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
406
|
+
this._leftRotate(k.parent.parent);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
u = k.parent.parent.right;
|
|
411
|
+
if (u && u.color === 1) {
|
|
412
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
413
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
414
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
415
|
+
k = k.parent.parent;
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
if (k === k.parent.right) {
|
|
419
|
+
k = k.parent;
|
|
420
|
+
this._leftRotate(k);
|
|
421
|
+
}
|
|
422
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
423
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
424
|
+
this._rightRotate(k.parent.parent);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (k === this.root) {
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
this.root.color = types_1.RBTNColor.BLACK;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
exports.RedBlackTree = RedBlackTree;
|
|
@@ -0,0 +1,67 @@
|
|
|
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 type { SegmentTreeNodeVal } from '../../types';
|
|
9
|
+
export declare class SegmentTreeNode {
|
|
10
|
+
start: number;
|
|
11
|
+
end: number;
|
|
12
|
+
value: SegmentTreeNodeVal | null;
|
|
13
|
+
sum: number;
|
|
14
|
+
left: SegmentTreeNode | null;
|
|
15
|
+
right: SegmentTreeNode | null;
|
|
16
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | null);
|
|
17
|
+
}
|
|
18
|
+
export declare class SegmentTree {
|
|
19
|
+
/**
|
|
20
|
+
* The constructor initializes the values, start, end, and root properties of an object.
|
|
21
|
+
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
|
|
22
|
+
* @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
|
|
23
|
+
* be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
|
|
24
|
+
* the beginning of the array.
|
|
25
|
+
* @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
|
|
26
|
+
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
27
|
+
*/
|
|
28
|
+
constructor(values: number[], start?: number, end?: number);
|
|
29
|
+
protected _values: number[];
|
|
30
|
+
get values(): number[];
|
|
31
|
+
protected _start: number;
|
|
32
|
+
get start(): number;
|
|
33
|
+
protected _end: number;
|
|
34
|
+
get end(): number;
|
|
35
|
+
protected _root: SegmentTreeNode | null;
|
|
36
|
+
get root(): SegmentTreeNode | null;
|
|
37
|
+
/**
|
|
38
|
+
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|
|
39
|
+
* the sum of values to each segment.
|
|
40
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
|
|
41
|
+
* building the segment tree.
|
|
42
|
+
* @param {number} end - The "end" parameter represents the ending index of the segment or range for which we want to
|
|
43
|
+
* build a segment tree.
|
|
44
|
+
* @returns a SegmentTreeNode object.
|
|
45
|
+
*/
|
|
46
|
+
build(start: number, end: number): SegmentTreeNode;
|
|
47
|
+
/**
|
|
48
|
+
* The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
|
|
49
|
+
* @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
|
|
50
|
+
* updated.
|
|
51
|
+
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
52
|
+
* the `SegmentTreeNode` at the specified `index`.
|
|
53
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
54
|
+
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
55
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
56
|
+
* @returns The function does not return anything.
|
|
57
|
+
*/
|
|
58
|
+
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal): void;
|
|
59
|
+
/**
|
|
60
|
+
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
61
|
+
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
62
|
+
* @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
|
|
63
|
+
* calculate the sum.
|
|
64
|
+
* @returns The function `querySumByRange` returns a number.
|
|
65
|
+
*/
|
|
66
|
+
querySumByRange(indexA: number, indexB: number): number;
|
|
67
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.SegmentTree = exports.SegmentTreeNode = void 0;
|
|
11
|
+
class SegmentTreeNode {
|
|
12
|
+
constructor(start, end, sum, value) {
|
|
13
|
+
this.start = 0;
|
|
14
|
+
this.end = 0;
|
|
15
|
+
this.value = null;
|
|
16
|
+
this.sum = 0;
|
|
17
|
+
this.left = null;
|
|
18
|
+
this.right = null;
|
|
19
|
+
this.start = start;
|
|
20
|
+
this.end = end;
|
|
21
|
+
this.sum = sum;
|
|
22
|
+
this.value = value || null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.SegmentTreeNode = SegmentTreeNode;
|
|
26
|
+
class SegmentTree {
|
|
27
|
+
/**
|
|
28
|
+
* The constructor initializes the values, start, end, and root properties of an object.
|
|
29
|
+
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
|
|
30
|
+
* @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
|
|
31
|
+
* be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
|
|
32
|
+
* the beginning of the array.
|
|
33
|
+
* @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
|
|
34
|
+
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
35
|
+
*/
|
|
36
|
+
constructor(values, start, end) {
|
|
37
|
+
this._values = [];
|
|
38
|
+
this._start = 0;
|
|
39
|
+
start = start || 0;
|
|
40
|
+
end = end || values.length - 1;
|
|
41
|
+
this._values = values;
|
|
42
|
+
this._start = start;
|
|
43
|
+
this._end = end;
|
|
44
|
+
if (values.length > 0) {
|
|
45
|
+
this._root = this.build(start, end);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this._root = null;
|
|
49
|
+
this._values = [];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
get values() {
|
|
53
|
+
return this._values;
|
|
54
|
+
}
|
|
55
|
+
get start() {
|
|
56
|
+
return this._start;
|
|
57
|
+
}
|
|
58
|
+
get end() {
|
|
59
|
+
return this._end;
|
|
60
|
+
}
|
|
61
|
+
get root() {
|
|
62
|
+
return this._root;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|
|
66
|
+
* the sum of values to each segment.
|
|
67
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
|
|
68
|
+
* building the segment tree.
|
|
69
|
+
* @param {number} end - The "end" parameter represents the ending index of the segment or range for which we want to
|
|
70
|
+
* build a segment tree.
|
|
71
|
+
* @returns a SegmentTreeNode object.
|
|
72
|
+
*/
|
|
73
|
+
build(start, end) {
|
|
74
|
+
if (start > end) {
|
|
75
|
+
return new SegmentTreeNode(start, end, 0);
|
|
76
|
+
}
|
|
77
|
+
if (start === end)
|
|
78
|
+
return new SegmentTreeNode(start, end, this._values[start]);
|
|
79
|
+
const mid = start + Math.floor((end - start) / 2);
|
|
80
|
+
const left = this.build(start, mid);
|
|
81
|
+
const right = this.build(mid + 1, end);
|
|
82
|
+
const cur = new SegmentTreeNode(start, end, left.sum + right.sum);
|
|
83
|
+
cur.left = left;
|
|
84
|
+
cur.right = right;
|
|
85
|
+
return cur;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
|
|
89
|
+
* @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
|
|
90
|
+
* updated.
|
|
91
|
+
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
92
|
+
* the `SegmentTreeNode` at the specified `index`.
|
|
93
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
94
|
+
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
95
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
96
|
+
* @returns The function does not return anything.
|
|
97
|
+
*/
|
|
98
|
+
updateNode(index, sum, value) {
|
|
99
|
+
const root = this.root || null;
|
|
100
|
+
if (!root) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const dfs = (cur, index, sum, value) => {
|
|
104
|
+
if (cur.start === cur.end && cur.start === index) {
|
|
105
|
+
cur.sum = sum;
|
|
106
|
+
if (value !== undefined)
|
|
107
|
+
cur.value = value;
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
111
|
+
if (index <= mid) {
|
|
112
|
+
if (cur.left) {
|
|
113
|
+
dfs(cur.left, index, sum, value);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
if (cur.right) {
|
|
118
|
+
dfs(cur.right, index, sum, value);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (cur.left && cur.right) {
|
|
122
|
+
cur.sum = cur.left.sum + cur.right.sum;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
dfs(root, index, sum, value);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
129
|
+
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
130
|
+
* @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
|
|
131
|
+
* calculate the sum.
|
|
132
|
+
* @returns The function `querySumByRange` returns a number.
|
|
133
|
+
*/
|
|
134
|
+
querySumByRange(indexA, indexB) {
|
|
135
|
+
const root = this.root || null;
|
|
136
|
+
if (!root) {
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
if (indexA < 0 || indexB >= this.values.length || indexA > indexB) {
|
|
140
|
+
return NaN;
|
|
141
|
+
}
|
|
142
|
+
const dfs = (cur, i, j) => {
|
|
143
|
+
if (i <= cur.start && j >= cur.end) {
|
|
144
|
+
// The range [i, j] completely covers the current node's range [cur.start, cur.end]
|
|
145
|
+
return cur.sum;
|
|
146
|
+
}
|
|
147
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
148
|
+
if (j <= mid) {
|
|
149
|
+
if (cur.left) {
|
|
150
|
+
return dfs(cur.left, i, j);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
return NaN;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
else if (i > mid) {
|
|
157
|
+
if (cur.right) {
|
|
158
|
+
return dfs(cur.right, i, j);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
return NaN;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
// Query both left and right subtrees
|
|
166
|
+
let leftSum = 0;
|
|
167
|
+
let rightSum = 0;
|
|
168
|
+
if (cur.left) {
|
|
169
|
+
leftSum = dfs(cur.left, i, mid);
|
|
170
|
+
}
|
|
171
|
+
if (cur.right) {
|
|
172
|
+
rightSum = dfs(cur.right, mid + 1, j);
|
|
173
|
+
}
|
|
174
|
+
return leftSum + rightSum;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
return dfs(root, indexA, indexB);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.SegmentTree = SegmentTree;
|