data-structure-typed 1.53.9 → 1.54.1
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/CHANGELOG.md +1 -1
- package/README.md +1 -0
- package/benchmark/report.html +1 -28
- package/benchmark/report.json +17 -326
- package/dist/{mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts → cjs/data-structures/binary-tree/avl-tree-counter.d.ts} +54 -59
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +408 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -189
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +133 -357
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +108 -78
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +126 -79
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +243 -190
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +273 -229
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +141 -122
- package/dist/cjs/data-structures/binary-tree/bst.js +170 -134
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/cjs/data-structures/binary-tree/index.js +2 -0
- package/dist/cjs/data-structures/binary-tree/index.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +84 -80
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +101 -79
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/{mjs/data-structures/binary-tree/tree-multi-map.d.ts → cjs/data-structures/binary-tree/tree-counter.d.ts} +54 -56
- package/dist/cjs/data-structures/binary-tree/tree-counter.js +445 -0
- package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +78 -186
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +140 -388
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +3 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/cjs/data-structures/graph/map-graph.js +3 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js +3 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +3 -0
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +3 -0
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/cjs/data-structures/matrix/matrix.js +3 -0
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/cjs/data-structures/matrix/navigator.js +3 -0
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +0 -4
- package/dist/cjs/data-structures/trie/trie.js +0 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +7 -6
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.js +3 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.js.map +1 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/cjs/types/data-structures/binary-tree/index.js +2 -0
- package/dist/cjs/types/data-structures/binary-tree/index.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/cjs/types/data-structures/binary-tree/tree-counter.js +3 -0
- package/dist/cjs/types/data-structures/binary-tree/tree-counter.js.map +1 -0
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -3
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/{mjs/data-structures/binary-tree/avl-tree-multi-map.js → esm/data-structures/binary-tree/avl-tree-counter.js} +72 -78
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -0
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -0
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +191 -0
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +234 -0
- package/dist/{mjs → esm}/data-structures/binary-tree/avl-tree.js +126 -80
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/{mjs → esm}/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/esm/data-structures/binary-tree/binary-indexed-tree.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/binary-tree/binary-tree.d.ts +243 -190
- package/dist/{mjs → esm}/data-structures/binary-tree/binary-tree.js +276 -234
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/binary-tree/bst.d.ts +141 -122
- package/dist/{mjs → esm}/data-structures/binary-tree/bst.js +171 -140
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/{mjs → esm}/data-structures/binary-tree/index.js +2 -0
- package/dist/{mjs → esm}/data-structures/binary-tree/index.js.map +1 -1
- package/dist/{mjs → esm}/data-structures/binary-tree/red-black-tree.d.ts +84 -80
- package/dist/{mjs → esm}/data-structures/binary-tree/red-black-tree.js +100 -79
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -0
- package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/{mjs/data-structures/binary-tree/tree-multi-map.js → esm/data-structures/binary-tree/tree-counter.js} +88 -86
- package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -0
- package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +106 -0
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +196 -0
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/{mjs → esm}/data-structures/graph/directed-graph.js +3 -0
- package/dist/esm/data-structures/graph/directed-graph.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/{mjs → esm}/data-structures/graph/map-graph.js +3 -0
- package/dist/esm/data-structures/graph/map-graph.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/{mjs → esm}/data-structures/graph/undirected-graph.js +3 -0
- package/dist/esm/data-structures/graph/undirected-graph.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/linked-list/singly-linked-list.d.ts +3 -0
- package/dist/{mjs → esm}/data-structures/linked-list/singly-linked-list.js +3 -0
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/{mjs → esm}/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/{mjs → esm}/data-structures/matrix/matrix.js +3 -0
- package/dist/esm/data-structures/matrix/matrix.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/{mjs → esm}/data-structures/matrix/navigator.js +3 -0
- package/dist/esm/data-structures/matrix/navigator.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/{mjs → esm}/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/{mjs → esm}/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -0
- package/dist/{mjs → esm}/data-structures/trie/trie.d.ts +0 -4
- package/dist/{mjs → esm}/data-structures/trie/trie.js +0 -4
- package/dist/esm/data-structures/trie/trie.js.map +1 -0
- package/dist/esm/interfaces/binary-tree.d.ts +9 -0
- package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
- package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.js.map +1 -0
- package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/esm/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/{mjs → esm}/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/{mjs → esm}/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/{mjs → esm}/types/data-structures/binary-tree/index.js +2 -0
- package/dist/{mjs → esm}/types/data-structures/binary-tree/index.js.map +1 -1
- package/dist/esm/types/data-structures/binary-tree/rb-tree.d.ts +3 -0
- package/dist/esm/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/esm/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/esm/types/data-structures/binary-tree/tree-counter.js.map +1 -0
- package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/umd/data-structure-typed.js +1407 -913
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +14 -14
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +148 -394
- package/src/data-structures/binary-tree/avl-tree.ts +152 -112
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +446 -379
- package/src/data-structures/binary-tree/bst.ts +224 -201
- package/src/data-structures/binary-tree/index.ts +2 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +138 -114
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +156 -428
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/trie/trie.ts +0 -4
- package/src/interfaces/binary-tree.ts +10 -11
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +5 -3
- package/src/types/data-structures/binary-tree/index.ts +2 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -4
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -4
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/avl-tree.test.ts +1 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/integration/compile.js +144 -0
- package/test/integration/compile.ts +171 -0
- package/test/integration/index.html +48 -48
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -4
- package/test/performance/data-structures/comparison/comparison.test.ts +2 -2
- package/test/performance/reportor.ts +5 -3
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +877 -0
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +459 -761
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +223 -223
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +474 -492
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/overall.test.ts +0 -3
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +87 -90
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +975 -0
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +729 -857
- package/test/unit/data-structures/hash/hash-map.test.ts +1 -1
- package/{tsconfig-mjs.json → tsconfig-esm.json} +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -204
- package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +0 -1
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +0 -1
- package/dist/mjs/data-structures/binary-tree/bst.js.map +0 -1
- package/dist/mjs/data-structures/binary-tree/red-black-tree.js.map +0 -1
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +0 -1
- package/dist/mjs/data-structures/graph/directed-graph.js.map +0 -1
- package/dist/mjs/data-structures/graph/map-graph.js.map +0 -1
- package/dist/mjs/data-structures/graph/undirected-graph.js.map +0 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +0 -1
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js.map +0 -1
- package/dist/mjs/data-structures/matrix/matrix.js.map +0 -1
- package/dist/mjs/data-structures/matrix/navigator.js.map +0 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js.map +0 -1
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js.map +0 -1
- package/dist/mjs/data-structures/trie/trie.js.map +0 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +0 -8
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -4
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +0 -4
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +0 -5
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +0 -4
- /package/dist/{mjs → esm}/common/index.d.ts +0 -0
- /package/dist/{mjs → esm}/common/index.js +0 -0
- /package/dist/{mjs → esm}/common/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/base/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/base/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/base/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/base/iterable-element-base.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/base/iterable-element-base.js +0 -0
- /package/dist/{mjs → esm}/data-structures/base/iterable-element-base.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/base/iterable-entry-base.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/base/iterable-entry-base.js +0 -0
- /package/dist/{mjs → esm}/data-structures/base/iterable-entry-base.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/binary-tree/segment-tree.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/binary-tree/segment-tree.js +0 -0
- /package/dist/{mjs → esm}/data-structures/binary-tree/segment-tree.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/graph/abstract-graph.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/graph/abstract-graph.js +0 -0
- /package/dist/{mjs → esm}/data-structures/graph/abstract-graph.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/graph/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/graph/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/graph/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/hash/hash-map.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/hash/hash-map.js +0 -0
- /package/dist/{mjs → esm}/data-structures/hash/hash-map.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/hash/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/hash/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/hash/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/heap.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/heap.js +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/heap.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/max-heap.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/max-heap.js +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/max-heap.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/min-heap.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/min-heap.js +0 -0
- /package/dist/{mjs → esm}/data-structures/heap/min-heap.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/linked-list/doubly-linked-list.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/linked-list/doubly-linked-list.js +0 -0
- /package/dist/{mjs → esm}/data-structures/linked-list/doubly-linked-list.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/linked-list/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/linked-list/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/linked-list/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/matrix/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/matrix/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/matrix/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/priority-queue/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/priority-queue/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/priority-queue/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/priority-queue/priority-queue.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/priority-queue/priority-queue.js +0 -0
- /package/dist/{mjs → esm}/data-structures/priority-queue/priority-queue.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/queue/deque.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/queue/deque.js +0 -0
- /package/dist/{mjs → esm}/data-structures/queue/deque.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/queue/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/queue/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/queue/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/queue/queue.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/queue/queue.js +0 -0
- /package/dist/{mjs → esm}/data-structures/queue/queue.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/stack/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/stack/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/stack/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/stack/stack.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/stack/stack.js +0 -0
- /package/dist/{mjs → esm}/data-structures/stack/stack.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/tree/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/tree/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/tree/index.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/tree/tree.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/tree/tree.js +0 -0
- /package/dist/{mjs → esm}/data-structures/tree/tree.js.map +0 -0
- /package/dist/{mjs → esm}/data-structures/trie/index.d.ts +0 -0
- /package/dist/{mjs → esm}/data-structures/trie/index.js +0 -0
- /package/dist/{mjs → esm}/data-structures/trie/index.js.map +0 -0
- /package/dist/{mjs → esm}/index.d.ts +0 -0
- /package/dist/{mjs → esm}/index.js +0 -0
- /package/dist/{mjs → esm}/index.js.map +0 -0
- /package/dist/{mjs → esm}/interfaces/binary-tree.js +0 -0
- /package/dist/{mjs → esm}/interfaces/binary-tree.js.map +0 -0
- /package/dist/{mjs → esm}/interfaces/doubly-linked-list.d.ts +0 -0
- /package/dist/{mjs → esm}/interfaces/doubly-linked-list.js +0 -0
- /package/dist/{mjs → esm}/interfaces/doubly-linked-list.js.map +0 -0
- /package/dist/{mjs → esm}/interfaces/graph.d.ts +0 -0
- /package/dist/{mjs → esm}/interfaces/graph.js +0 -0
- /package/dist/{mjs → esm}/interfaces/graph.js.map +0 -0
- /package/dist/{mjs → esm}/interfaces/heap.d.ts +0 -0
- /package/dist/{mjs → esm}/interfaces/heap.js +0 -0
- /package/dist/{mjs → esm}/interfaces/heap.js.map +0 -0
- /package/dist/{mjs → esm}/interfaces/index.d.ts +0 -0
- /package/dist/{mjs → esm}/interfaces/index.js +0 -0
- /package/dist/{mjs → esm}/interfaces/index.js.map +0 -0
- /package/dist/{mjs → esm}/interfaces/navigator.d.ts +0 -0
- /package/dist/{mjs → esm}/interfaces/navigator.js +0 -0
- /package/dist/{mjs → esm}/interfaces/navigator.js.map +0 -0
- /package/dist/{mjs → esm}/interfaces/priority-queue.d.ts +0 -0
- /package/dist/{mjs → esm}/interfaces/priority-queue.js +0 -0
- /package/dist/{mjs → esm}/interfaces/priority-queue.js.map +0 -0
- /package/dist/{mjs → esm}/interfaces/segment-tree.d.ts +0 -0
- /package/dist/{mjs → esm}/interfaces/segment-tree.js +0 -0
- /package/dist/{mjs → esm}/interfaces/segment-tree.js.map +0 -0
- /package/dist/{mjs → esm}/interfaces/singly-linked-list.d.ts +0 -0
- /package/dist/{mjs → esm}/interfaces/singly-linked-list.js +0 -0
- /package/dist/{mjs → esm}/interfaces/singly-linked-list.js.map +0 -0
- /package/dist/{mjs → esm}/types/common.d.ts +0 -0
- /package/dist/{mjs → esm}/types/common.js +0 -0
- /package/dist/{mjs → esm}/types/common.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/base/base.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/base/base.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/base/base.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/base/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/base/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/base/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/avl-tree.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/avl-tree.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-indexed-tree.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-indexed-tree.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-tree.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/binary-tree.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/bst.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/bst.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/rb-tree.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/rb-tree.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/segment-tree.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/segment-tree.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/segment-tree.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/tree-multi-map.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/binary-tree/tree-multi-map.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/abstract-graph.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/abstract-graph.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/abstract-graph.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/directed-graph.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/directed-graph.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/directed-graph.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/map-graph.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/map-graph.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/map-graph.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/undirected-graph.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/undirected-graph.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/graph/undirected-graph.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/hash/hash-map.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/hash/hash-map.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/hash/hash-map.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/hash/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/hash/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/hash/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/heap.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/heap.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/heap.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/max-heap.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/max-heap.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/max-heap.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/min-heap.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/min-heap.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/heap/min-heap.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/doubly-linked-list.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/doubly-linked-list.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/singly-linked-list.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/singly-linked-list.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/singly-linked-list.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/skip-linked-list.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/skip-linked-list.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/linked-list/skip-linked-list.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/matrix/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/matrix/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/matrix/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/matrix/matrix.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/matrix/matrix.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/matrix/matrix.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/matrix/navigator.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/matrix/navigator.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/matrix/navigator.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/max-priority-queue.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/max-priority-queue.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/min-priority-queue.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/min-priority-queue.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/priority-queue.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/priority-queue.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/priority-queue/priority-queue.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/queue/deque.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/queue/deque.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/queue/deque.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/queue/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/queue/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/queue/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/queue/queue.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/queue/queue.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/queue/queue.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/stack/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/stack/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/stack/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/stack/stack.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/stack/stack.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/stack/stack.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/tree/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/tree/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/tree/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/tree/tree.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/tree/tree.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/tree/tree.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/trie/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/trie/index.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/trie/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/data-structures/trie/trie.d.ts +0 -0
- /package/dist/{mjs → esm}/types/data-structures/trie/trie.js +0 -0
- /package/dist/{mjs → esm}/types/data-structures/trie/trie.js.map +0 -0
- /package/dist/{mjs → esm}/types/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/index.js +0 -0
- /package/dist/{mjs → esm}/types/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/utils/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types/utils/index.js +0 -0
- /package/dist/{mjs → esm}/types/utils/index.js.map +0 -0
- /package/dist/{mjs → esm}/types/utils/utils.d.ts +0 -0
- /package/dist/{mjs → esm}/types/utils/utils.js +0 -0
- /package/dist/{mjs → esm}/types/utils/utils.js.map +0 -0
- /package/dist/{mjs → esm}/types/utils/validate-type.d.ts +0 -0
- /package/dist/{mjs → esm}/types/utils/validate-type.js +0 -0
- /package/dist/{mjs → esm}/types/utils/validate-type.js.map +0 -0
- /package/dist/{mjs → esm}/utils/index.d.ts +0 -0
- /package/dist/{mjs → esm}/utils/index.js +0 -0
- /package/dist/{mjs → esm}/utils/index.js.map +0 -0
- /package/dist/{mjs → esm}/utils/number.d.ts +0 -0
- /package/dist/{mjs → esm}/utils/number.js +0 -0
- /package/dist/{mjs → esm}/utils/number.js.map +0 -0
- /package/dist/{mjs → esm}/utils/utils.d.ts +0 -0
- /package/dist/{mjs → esm}/utils/utils.js +0 -0
- /package/dist/{mjs → esm}/utils/utils.js.map +0 -0
|
@@ -1,711 +1,165 @@
|
|
|
1
|
-
import { BinaryTreeNode, BSTNode,
|
|
2
|
-
import { isDebugTest } from '../../../config';
|
|
1
|
+
import { BinaryTreeNode, BSTNode, Range, TreeMultiMap, TreeMultiMapNode } from '../../../../src';
|
|
3
2
|
import { getRandomInt } from '../../../utils';
|
|
4
3
|
|
|
4
|
+
import { isDebugTest } from '../../../config';
|
|
5
|
+
import { costOfLiving } from './data/cost-of-living-by-country';
|
|
6
|
+
|
|
5
7
|
const isDebug = isDebugTest;
|
|
6
8
|
// const isDebug = true;
|
|
7
9
|
|
|
8
|
-
describe('TreeMultiMap
|
|
10
|
+
describe('TreeMultiMap 1', () => {
|
|
9
11
|
let tmm: TreeMultiMap<number>;
|
|
12
|
+
|
|
10
13
|
beforeEach(() => {
|
|
11
14
|
tmm = new TreeMultiMap<number>();
|
|
12
15
|
});
|
|
13
16
|
|
|
14
|
-
it('Should
|
|
15
|
-
tmm.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
tmm.
|
|
24
|
-
expect(tmm.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
describe('TreeMultiMap operations test1', () => {
|
|
42
|
-
it('should height ', () => {
|
|
43
|
-
const tmm = new TreeMultiMap();
|
|
44
|
-
expect(tmm.getHeight()).toBe(-1);
|
|
45
|
-
expect(tmm.getMinHeight()).toBe(-1);
|
|
46
|
-
|
|
47
|
-
tmm.addMany([1, 6, 7, 2, 3, 4, 9, 11, 8, 5, 10, 12, 16, 14, 13, 15]);
|
|
48
|
-
// tmm.print()
|
|
49
|
-
expect(tmm.getHeight()).toBe(5);
|
|
50
|
-
expect(tmm.getMinHeight()).toBe(2);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should size and count', () => {
|
|
54
|
-
const tmm = new TreeMultiMap();
|
|
55
|
-
|
|
56
|
-
expect(tmm instanceof TreeMultiMap);
|
|
57
|
-
|
|
58
|
-
tmm.add([11, 11]);
|
|
59
|
-
tmm.add([3, 3]);
|
|
60
|
-
expect(tmm.count).toBe(2);
|
|
61
|
-
expect(tmm.getComputedCount()).toBe(2);
|
|
62
|
-
expect(tmm.size).toBe(2);
|
|
63
|
-
|
|
64
|
-
const keyValuePairs: [number, number][] = [
|
|
65
|
-
[11, 11],
|
|
66
|
-
[3, 3],
|
|
67
|
-
[15, 15],
|
|
68
|
-
[1, 1],
|
|
69
|
-
[8, 8],
|
|
70
|
-
[13, 13],
|
|
71
|
-
[16, 16],
|
|
72
|
-
[2, 2],
|
|
73
|
-
[6, 6],
|
|
74
|
-
[9, 9],
|
|
75
|
-
[12, 12],
|
|
76
|
-
[14, 14],
|
|
77
|
-
[4, 4],
|
|
78
|
-
[7, 7],
|
|
79
|
-
[10, 10],
|
|
80
|
-
[5, 5]
|
|
81
|
-
];
|
|
82
|
-
|
|
83
|
-
tmm.addMany(keyValuePairs);
|
|
84
|
-
expect(tmm.size).toBe(16);
|
|
85
|
-
expect(tmm.count).toBe(18);
|
|
86
|
-
expect(tmm.getComputedCount()).toBe(18);
|
|
87
|
-
tmm.delete(11);
|
|
88
|
-
expect(tmm.count).toBe(17);
|
|
89
|
-
expect(tmm.getComputedCount()).toBe(17);
|
|
90
|
-
tmm.delete(3, true);
|
|
91
|
-
expect(tmm.count).toBe(15);
|
|
92
|
-
expect(tmm.getComputedCount()).toBe(15);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should perform various operations on a Binary Search Tree with numeric values1', () => {
|
|
96
|
-
const tmm = new TreeMultiMap<number, number>();
|
|
97
|
-
|
|
98
|
-
expect(tmm instanceof TreeMultiMap);
|
|
99
|
-
|
|
100
|
-
tmm.add([11, 11]);
|
|
101
|
-
tmm.add([3, 3]);
|
|
102
|
-
const idAndValues: [number, number][] = [
|
|
103
|
-
[11, 11],
|
|
104
|
-
[3, 3],
|
|
105
|
-
[15, 15],
|
|
106
|
-
[1, 1],
|
|
107
|
-
[8, 8],
|
|
108
|
-
[13, 13],
|
|
109
|
-
[16, 16],
|
|
110
|
-
[2, 2],
|
|
111
|
-
[6, 6],
|
|
112
|
-
[9, 9],
|
|
113
|
-
[12, 12],
|
|
114
|
-
[14, 14],
|
|
115
|
-
[4, 4],
|
|
116
|
-
[7, 7],
|
|
117
|
-
[10, 10],
|
|
118
|
-
[5, 5]
|
|
119
|
-
];
|
|
120
|
-
tmm.addMany(idAndValues);
|
|
121
|
-
expect(tmm.root instanceof TreeMultiMapNode);
|
|
122
|
-
|
|
123
|
-
if (tmm.root) expect(tmm.root.key == 11);
|
|
124
|
-
|
|
125
|
-
expect(tmm.size).toBe(16);
|
|
126
|
-
expect(tmm.count).toBe(18);
|
|
127
|
-
expect(tmm.getComputedCount()).toBe(18);
|
|
128
|
-
|
|
129
|
-
expect(tmm.has(6));
|
|
130
|
-
if (isDebug) tmm.print();
|
|
131
|
-
expect(tmm.getHeight(6)).toBe(1);
|
|
132
|
-
expect(tmm.getDepth(6)).toBe(3);
|
|
133
|
-
const nodeId10 = tmm.getNode(10);
|
|
134
|
-
expect(nodeId10?.key).toBe(10);
|
|
135
|
-
|
|
136
|
-
const nodeVal9 = tmm.getNode(node => node.key === 9);
|
|
137
|
-
expect(nodeVal9?.key).toBe(9);
|
|
138
|
-
|
|
139
|
-
const nodesByCount1 = tmm.getNodes(node => node.count === 1);
|
|
140
|
-
expect(nodesByCount1.length).toBe(14);
|
|
141
|
-
|
|
142
|
-
const nodesByCount2 = tmm.getNodes(node => node.count === 2);
|
|
143
|
-
expect(nodesByCount2.length).toBe(2);
|
|
144
|
-
const leftMost = tmm.getLeftMost();
|
|
145
|
-
expect(leftMost).toBe(1);
|
|
146
|
-
|
|
147
|
-
const node15 = tmm.getNode(15);
|
|
148
|
-
const minNodeBySpecificNode = node15 && tmm.getLeftMost(node => node, node15);
|
|
149
|
-
expect(minNodeBySpecificNode?.key).toBe(14);
|
|
150
|
-
|
|
151
|
-
let subTreeSum = 0;
|
|
152
|
-
if (node15) tmm.dfs(node => (subTreeSum += node.key), 'PRE', 15);
|
|
153
|
-
expect(subTreeSum).toBe(45);
|
|
154
|
-
let lesserSum = 0;
|
|
155
|
-
tmm.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
|
156
|
-
expect(lesserSum).toBe(45);
|
|
157
|
-
|
|
158
|
-
expect(node15 instanceof TreeMultiMapNode);
|
|
159
|
-
if (node15 instanceof TreeMultiMapNode) {
|
|
160
|
-
const subTreeAdd = tmm.dfs(node => (node.count += 1), 'PRE', 15);
|
|
161
|
-
expect(subTreeAdd);
|
|
162
|
-
}
|
|
163
|
-
const node11 = tmm.getNode(11);
|
|
164
|
-
expect(node11 instanceof TreeMultiMapNode);
|
|
165
|
-
if (node11 instanceof TreeMultiMapNode) {
|
|
166
|
-
const allGreaterNodesAdded = tmm.lesserOrGreaterTraverse(node => (node.count += 2), 1, 11);
|
|
167
|
-
expect(allGreaterNodesAdded);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const dfsInorderNodes = tmm.dfs(node => node, 'IN');
|
|
171
|
-
expect(dfsInorderNodes[0].key).toBe(1);
|
|
172
|
-
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
|
173
|
-
expect(tmm.isPerfectlyBalanced()).toBe(false);
|
|
174
|
-
tmm.perfectlyBalance();
|
|
175
|
-
expect(tmm.isPerfectlyBalanced()).toBe(false);
|
|
176
|
-
|
|
177
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
178
|
-
|
|
179
|
-
const bfsNodesAfterBalanced = tmm.bfs(node => node);
|
|
180
|
-
expect(bfsNodesAfterBalanced[0].key).toBe(6);
|
|
181
|
-
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
|
182
|
-
|
|
183
|
-
const removed11 = tmm.delete(11, true);
|
|
184
|
-
expect(removed11 instanceof Array);
|
|
185
|
-
expect(removed11[0]);
|
|
186
|
-
expect(removed11[0].deleted);
|
|
187
|
-
|
|
188
|
-
if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
|
|
189
|
-
|
|
190
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
191
|
-
|
|
192
|
-
expect(tmm.getHeight(15)).toBe(1);
|
|
193
|
-
|
|
194
|
-
const removed1 = tmm.delete(1, true);
|
|
195
|
-
expect(removed1 instanceof Array);
|
|
196
|
-
expect(removed1[0]);
|
|
197
|
-
expect(removed1[0].deleted);
|
|
198
|
-
if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
|
|
199
|
-
|
|
200
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
201
|
-
|
|
202
|
-
expect(tmm.getHeight()).toBe(5);
|
|
203
|
-
|
|
204
|
-
const removed4 = tmm.delete(4, true);
|
|
205
|
-
expect(removed4 instanceof Array);
|
|
206
|
-
expect(removed4[0]);
|
|
207
|
-
expect(removed4[0].deleted);
|
|
208
|
-
if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
|
|
209
|
-
|
|
210
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
211
|
-
expect(tmm.getHeight()).toBe(5);
|
|
212
|
-
|
|
213
|
-
const removed10 = tmm.delete(10, true);
|
|
214
|
-
expect(removed10 instanceof Array);
|
|
215
|
-
expect(removed10[0]);
|
|
216
|
-
expect(removed10[0].deleted);
|
|
217
|
-
if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
|
|
218
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
219
|
-
|
|
220
|
-
expect(tmm.getHeight()).toBe(4);
|
|
221
|
-
|
|
222
|
-
const removed15 = tmm.delete(15, true);
|
|
223
|
-
expect(removed15 instanceof Array);
|
|
224
|
-
expect(removed15[0]);
|
|
225
|
-
expect(removed15[0].deleted);
|
|
226
|
-
if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
|
|
227
|
-
|
|
228
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
229
|
-
expect(tmm.getHeight()).toBe(3);
|
|
230
|
-
|
|
231
|
-
const removed5 = tmm.delete(5, true);
|
|
232
|
-
expect(removed5 instanceof Array);
|
|
233
|
-
expect(removed5[0]);
|
|
234
|
-
expect(removed5[0].deleted);
|
|
235
|
-
if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
|
|
236
|
-
|
|
237
|
-
expect(tmm.isAVLBalanced()).toBe(true);
|
|
238
|
-
expect(tmm.getHeight()).toBe(3);
|
|
239
|
-
|
|
240
|
-
const removed13 = tmm.delete(13, true);
|
|
241
|
-
expect(removed13 instanceof Array);
|
|
242
|
-
expect(removed13[0]);
|
|
243
|
-
expect(removed13[0].deleted);
|
|
244
|
-
if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
|
|
245
|
-
expect(tmm.isAVLBalanced()).toBe(true);
|
|
246
|
-
expect(tmm.getHeight()).toBe(3);
|
|
247
|
-
|
|
248
|
-
const removed3 = tmm.delete(3, true);
|
|
249
|
-
expect(removed3 instanceof Array);
|
|
250
|
-
expect(removed3[0]);
|
|
251
|
-
expect(removed3[0].deleted);
|
|
252
|
-
if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
|
|
253
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
254
|
-
expect(tmm.getHeight()).toBe(3);
|
|
255
|
-
|
|
256
|
-
const removed8 = tmm.delete(8, true);
|
|
257
|
-
expect(removed8 instanceof Array);
|
|
258
|
-
expect(removed8[0]);
|
|
259
|
-
expect(removed8[0].deleted);
|
|
260
|
-
if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
|
|
261
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
262
|
-
expect(tmm.getHeight()).toBe(3);
|
|
263
|
-
|
|
264
|
-
const removed6 = tmm.delete(6, true);
|
|
265
|
-
expect(removed6 instanceof Array);
|
|
266
|
-
expect(removed6[0]);
|
|
267
|
-
expect(removed6[0].deleted);
|
|
268
|
-
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
|
269
|
-
expect(tmm.delete(6, true).length).toBe(0);
|
|
270
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
271
|
-
|
|
272
|
-
expect(tmm.getHeight()).toBe(3);
|
|
273
|
-
|
|
274
|
-
const removed7 = tmm.delete(7, true);
|
|
275
|
-
expect(removed7 instanceof Array);
|
|
276
|
-
expect(removed7[0]);
|
|
277
|
-
expect(removed7[0].deleted);
|
|
278
|
-
if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
|
|
279
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
280
|
-
expect(tmm.getHeight()).toBe(3);
|
|
281
|
-
|
|
282
|
-
const removed9 = tmm.delete(9, true);
|
|
283
|
-
expect(removed9 instanceof Array);
|
|
284
|
-
expect(removed9[0]);
|
|
285
|
-
expect(removed9[0].deleted);
|
|
286
|
-
if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
|
|
287
|
-
expect(tmm.isAVLBalanced()).toBe(true);
|
|
288
|
-
expect(tmm.getHeight()).toBe(2);
|
|
289
|
-
|
|
290
|
-
const removed14 = tmm.delete(14, true);
|
|
291
|
-
expect(removed14 instanceof Array);
|
|
292
|
-
expect(removed14[0]);
|
|
293
|
-
expect(removed14[0].deleted);
|
|
294
|
-
if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
|
|
295
|
-
expect(tmm.isAVLBalanced()).toBe(true);
|
|
296
|
-
expect(tmm.getHeight()).toBe(1);
|
|
297
|
-
|
|
298
|
-
expect(tmm.isAVLBalanced()).toBe(true);
|
|
299
|
-
|
|
300
|
-
const bfsIDs = tmm.bfs(node => node.key);
|
|
301
|
-
|
|
302
|
-
expect(bfsIDs[0]).toBe(12);
|
|
303
|
-
expect(bfsIDs[1]).toBe(2);
|
|
304
|
-
expect(bfsIDs[2]).toBe(16);
|
|
305
|
-
|
|
306
|
-
const bfsNodes = tmm.bfs(node => node);
|
|
307
|
-
|
|
308
|
-
expect(bfsNodes[0].key).toBe(12);
|
|
309
|
-
expect(bfsNodes[1].key).toBe(2);
|
|
310
|
-
expect(bfsNodes[2].key).toBe(16);
|
|
311
|
-
|
|
312
|
-
expect(tmm.count).toBe(6);
|
|
313
|
-
expect(tmm.getComputedCount()).toBe(8);
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
317
|
-
const objTreeMultiMap = new TreeMultiMap<number, { key: number; keyA: number }>();
|
|
318
|
-
expect(objTreeMultiMap).toBeInstanceOf(TreeMultiMap);
|
|
319
|
-
objTreeMultiMap.add([11, { key: 11, keyA: 11 }]);
|
|
320
|
-
objTreeMultiMap.add([3, { key: 3, keyA: 3 }]);
|
|
321
|
-
const values: [number, { key: number; keyA: number }][] = [
|
|
322
|
-
[15, { key: 15, keyA: 15 }],
|
|
323
|
-
[1, { key: 1, keyA: 1 }],
|
|
324
|
-
[8, { key: 8, keyA: 8 }],
|
|
325
|
-
[13, { key: 13, keyA: 13 }],
|
|
326
|
-
[16, { key: 16, keyA: 16 }],
|
|
327
|
-
[2, { key: 2, keyA: 2 }],
|
|
328
|
-
[6, { key: 6, keyA: 6 }],
|
|
329
|
-
[9, { key: 9, keyA: 9 }],
|
|
330
|
-
[12, { key: 12, keyA: 12 }],
|
|
331
|
-
[14, { key: 14, keyA: 14 }],
|
|
332
|
-
[4, { key: 4, keyA: 4 }],
|
|
333
|
-
[7, { key: 7, keyA: 7 }],
|
|
334
|
-
[10, { key: 10, keyA: 10 }],
|
|
335
|
-
[5, { key: 5, keyA: 5 }]
|
|
336
|
-
];
|
|
337
|
-
|
|
338
|
-
objTreeMultiMap.addMany(values);
|
|
339
|
-
|
|
340
|
-
expect(objTreeMultiMap.root).toBeInstanceOf(TreeMultiMapNode);
|
|
341
|
-
|
|
342
|
-
if (objTreeMultiMap.root) expect(objTreeMultiMap.root.key).toBe(5);
|
|
343
|
-
|
|
344
|
-
expect(objTreeMultiMap.count).toBe(16);
|
|
345
|
-
expect(objTreeMultiMap.getComputedCount()).toBe(16);
|
|
346
|
-
|
|
347
|
-
expect(objTreeMultiMap.has(6)).toBe(true);
|
|
348
|
-
});
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
describe('TreeMultiMap operations test recursively1', () => {
|
|
352
|
-
it('should perform various operations on a Binary Search Tree with numeric values1', () => {
|
|
353
|
-
const tmm = new TreeMultiMap<number>([], {
|
|
354
|
-
iterationType: 'RECURSIVE'
|
|
17
|
+
it('Should add and delete values', () => {
|
|
18
|
+
tmm.add(3, 3);
|
|
19
|
+
tmm.add(3, 33);
|
|
20
|
+
tmm.add(3, 333);
|
|
21
|
+
expect(tmm.get(3)).toEqual([3, 33, 333]);
|
|
22
|
+
tmm.deleteValue(3, 33);
|
|
23
|
+
expect(tmm.get(3)).toEqual([3, 333]);
|
|
24
|
+
tmm.deleteValue(3, 3);
|
|
25
|
+
expect(tmm.get(3)).toEqual([333]);
|
|
26
|
+
tmm.deleteValue(3, 333);
|
|
27
|
+
expect(tmm.get(3)).toBe(undefined);
|
|
28
|
+
tmm.add(3, 3);
|
|
29
|
+
tmm.add([3, [3333, 33333]]);
|
|
30
|
+
expect(tmm.get(3)).toEqual([3, 3333, 33333]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('add and getNode', () => {
|
|
34
|
+
it('should add and find a node in the tmm', () => {
|
|
35
|
+
tmm.add(10);
|
|
36
|
+
tmm.add(20);
|
|
37
|
+
tmm.add(5);
|
|
38
|
+
|
|
39
|
+
expect(tmm.getNode(10)).toBeInstanceOf(TreeMultiMapNode);
|
|
40
|
+
expect(tmm.getNode(20)).toBeInstanceOf(TreeMultiMapNode);
|
|
41
|
+
expect(tmm.getNode(5)).toBeInstanceOf(TreeMultiMapNode);
|
|
42
|
+
expect(tmm.getNode(15)).toBe(undefined);
|
|
355
43
|
});
|
|
356
44
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
const idAndValues: [number, number][] = [
|
|
361
|
-
[11, 11],
|
|
362
|
-
[3, 3],
|
|
363
|
-
[15, 15],
|
|
364
|
-
[1, 1],
|
|
365
|
-
[8, 8],
|
|
366
|
-
[13, 13],
|
|
367
|
-
[16, 16],
|
|
368
|
-
[2, 2],
|
|
369
|
-
[6, 6],
|
|
370
|
-
[9, 9],
|
|
371
|
-
[12, 12],
|
|
372
|
-
[14, 14],
|
|
373
|
-
[4, 4],
|
|
374
|
-
[7, 7],
|
|
375
|
-
[10, 10],
|
|
376
|
-
[5, 5]
|
|
377
|
-
];
|
|
378
|
-
tmm.addMany(idAndValues);
|
|
379
|
-
expect(tmm.root).toBeInstanceOf(TreeMultiMapNode);
|
|
380
|
-
|
|
381
|
-
if (tmm.root) expect(tmm.root.key).toBe(5);
|
|
382
|
-
|
|
383
|
-
expect(tmm.size).toBe(16);
|
|
384
|
-
expect(tmm.count).toBe(18);
|
|
385
|
-
expect(tmm.getComputedCount()).toBe(18);
|
|
386
|
-
|
|
387
|
-
expect(tmm.has(6));
|
|
388
|
-
|
|
389
|
-
expect(tmm.getHeight(6)).toBe(1);
|
|
390
|
-
expect(tmm.getDepth(6)).toBe(3);
|
|
391
|
-
const nodeId10 = tmm.getNode(10);
|
|
392
|
-
expect(nodeId10?.key).toBe(10);
|
|
393
|
-
|
|
394
|
-
const nodeVal9 = tmm.getNode(node => node.key === 9);
|
|
395
|
-
expect(nodeVal9?.key).toBe(9);
|
|
396
|
-
|
|
397
|
-
const nodesByCount1 = tmm.getNodes(node => node.count === 1);
|
|
398
|
-
expect(nodesByCount1.length).toBe(14);
|
|
399
|
-
|
|
400
|
-
const nodesByCount2 = tmm.getNodes(node => node.count === 2);
|
|
401
|
-
expect(nodesByCount2.length).toBe(2);
|
|
402
|
-
const leftMost = tmm.getLeftMost();
|
|
403
|
-
expect(leftMost).toBe(1);
|
|
404
|
-
|
|
405
|
-
const node15 = tmm.getNode(15);
|
|
406
|
-
const minNodeBySpecificNode = node15 && tmm.getLeftMost(node => node, node15);
|
|
407
|
-
expect(minNodeBySpecificNode?.key).toBe(14);
|
|
408
|
-
|
|
409
|
-
let subTreeSum = 0;
|
|
410
|
-
if (node15) tmm.dfs(node => (subTreeSum += node.key), 'PRE', 15);
|
|
411
|
-
expect(subTreeSum).toBe(45);
|
|
412
|
-
let lesserSum = 0;
|
|
413
|
-
expect(tmm.has(9)).toBe(true);
|
|
414
|
-
tmm.lesserOrGreaterTraverse(
|
|
415
|
-
node => {
|
|
416
|
-
lesserSum += node.key;
|
|
417
|
-
return node.key;
|
|
418
|
-
},
|
|
419
|
-
-1,
|
|
420
|
-
10
|
|
421
|
-
);
|
|
422
|
-
expect(lesserSum).toBe(45);
|
|
423
|
-
|
|
424
|
-
expect(node15 instanceof TreeMultiMapNode);
|
|
425
|
-
if (node15 instanceof TreeMultiMapNode) {
|
|
426
|
-
const subTreeAdd = tmm.dfs(node => (node.count += 1), 'PRE', 15);
|
|
427
|
-
expect(subTreeAdd);
|
|
428
|
-
}
|
|
429
|
-
const node11 = tmm.getNode(11);
|
|
430
|
-
expect(node11 instanceof TreeMultiMapNode);
|
|
431
|
-
if (node11 instanceof TreeMultiMapNode) {
|
|
432
|
-
const allGreaterNodesAdded = tmm.lesserOrGreaterTraverse(node => (node.count += 2), 1, 11);
|
|
433
|
-
expect(allGreaterNodesAdded);
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
const dfsInorderNodes = tmm.dfs(node => node, 'IN');
|
|
437
|
-
expect(dfsInorderNodes[0].key).toBe(1);
|
|
438
|
-
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
|
439
|
-
expect(tmm.isPerfectlyBalanced()).toBe(false);
|
|
440
|
-
|
|
441
|
-
tmm.perfectlyBalance();
|
|
442
|
-
|
|
443
|
-
expect(tmm.isPerfectlyBalanced()).toBe(false);
|
|
444
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
445
|
-
|
|
446
|
-
const bfsNodesAfterBalanced = tmm.bfs(node => node);
|
|
447
|
-
expect(bfsNodesAfterBalanced[0].key).toBe(6);
|
|
448
|
-
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
|
449
|
-
|
|
450
|
-
const removed11 = tmm.delete(11, true);
|
|
451
|
-
expect(removed11 instanceof Array);
|
|
452
|
-
expect(removed11[0]);
|
|
453
|
-
expect(removed11[0].deleted);
|
|
454
|
-
|
|
455
|
-
if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
|
|
456
|
-
|
|
457
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
458
|
-
|
|
459
|
-
expect(tmm.getHeight(15)).toBe(1);
|
|
460
|
-
|
|
461
|
-
const removed1 = tmm.delete(1, true);
|
|
462
|
-
expect(removed1 instanceof Array);
|
|
463
|
-
expect(removed1[0]);
|
|
464
|
-
expect(removed1[0].deleted);
|
|
465
|
-
if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
|
|
466
|
-
|
|
467
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
468
|
-
|
|
469
|
-
expect(tmm.getHeight()).toBe(5);
|
|
470
|
-
|
|
471
|
-
const removed4 = tmm.delete(4, true);
|
|
472
|
-
expect(removed4 instanceof Array);
|
|
473
|
-
expect(removed4[0]);
|
|
474
|
-
expect(removed4[0].deleted);
|
|
475
|
-
if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
|
|
476
|
-
|
|
477
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
478
|
-
expect(tmm.getHeight()).toBe(5);
|
|
479
|
-
|
|
480
|
-
const removed10 = tmm.delete(10, true);
|
|
481
|
-
expect(removed10 instanceof Array);
|
|
482
|
-
expect(removed10[0]);
|
|
483
|
-
expect(removed10[0].deleted);
|
|
484
|
-
if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
|
|
485
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
486
|
-
|
|
487
|
-
expect(tmm.getHeight()).toBe(4);
|
|
488
|
-
|
|
489
|
-
const removed15 = tmm.delete(15, true);
|
|
490
|
-
expect(removed15 instanceof Array);
|
|
491
|
-
expect(removed15[0]);
|
|
492
|
-
expect(removed15[0].deleted);
|
|
493
|
-
if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
|
|
494
|
-
|
|
495
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
496
|
-
expect(tmm.getHeight()).toBe(3);
|
|
497
|
-
|
|
498
|
-
const removed5 = tmm.delete(5, true);
|
|
499
|
-
expect(removed5 instanceof Array);
|
|
500
|
-
expect(removed5[0]);
|
|
501
|
-
expect(removed5[0].deleted);
|
|
502
|
-
if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
|
|
503
|
-
|
|
504
|
-
expect(tmm.isAVLBalanced()).toBe(true);
|
|
505
|
-
expect(tmm.getHeight()).toBe(3);
|
|
506
|
-
|
|
507
|
-
const removed13 = tmm.delete(13, true);
|
|
508
|
-
expect(removed13 instanceof Array);
|
|
509
|
-
expect(removed13[0]);
|
|
510
|
-
expect(removed13[0].deleted);
|
|
511
|
-
if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
|
|
512
|
-
expect(tmm.isAVLBalanced()).toBe(true);
|
|
513
|
-
expect(tmm.getHeight()).toBe(3);
|
|
514
|
-
|
|
515
|
-
const removed3 = tmm.delete(3, true);
|
|
516
|
-
expect(removed3 instanceof Array);
|
|
517
|
-
expect(removed3[0]);
|
|
518
|
-
expect(removed3[0].deleted);
|
|
519
|
-
if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
|
|
520
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
521
|
-
expect(tmm.getHeight()).toBe(3);
|
|
522
|
-
|
|
523
|
-
const removed8 = tmm.delete(8, true);
|
|
524
|
-
expect(removed8 instanceof Array);
|
|
525
|
-
expect(removed8[0]);
|
|
526
|
-
expect(removed8[0].deleted);
|
|
527
|
-
if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
|
|
528
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
529
|
-
expect(tmm.getHeight()).toBe(3);
|
|
530
|
-
|
|
531
|
-
const removed6 = tmm.delete(6, true);
|
|
532
|
-
expect(removed6 instanceof Array);
|
|
533
|
-
expect(removed6[0]);
|
|
534
|
-
expect(removed6[0].deleted);
|
|
535
|
-
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
|
536
|
-
expect(tmm.delete(6, true).length).toBe(0);
|
|
537
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
538
|
-
|
|
539
|
-
expect(tmm.getHeight()).toBe(3);
|
|
540
|
-
|
|
541
|
-
const removed7 = tmm.delete(7, true);
|
|
542
|
-
expect(removed7 instanceof Array);
|
|
543
|
-
expect(removed7[0]);
|
|
544
|
-
expect(removed7[0].deleted);
|
|
545
|
-
if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
|
|
546
|
-
expect(tmm.isAVLBalanced()).toBe(false);
|
|
547
|
-
expect(tmm.getHeight()).toBe(3);
|
|
548
|
-
|
|
549
|
-
const removed9 = tmm.delete(9, true);
|
|
550
|
-
expect(removed9 instanceof Array);
|
|
551
|
-
expect(removed9[0]);
|
|
552
|
-
expect(removed9[0].deleted);
|
|
553
|
-
if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
|
|
554
|
-
expect(tmm.isAVLBalanced()).toBe(true);
|
|
555
|
-
expect(tmm.getHeight()).toBe(2);
|
|
556
|
-
|
|
557
|
-
const removed14 = tmm.delete(14, true);
|
|
558
|
-
expect(removed14 instanceof Array);
|
|
559
|
-
expect(removed14[0]);
|
|
560
|
-
expect(removed14[0].deleted);
|
|
561
|
-
if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
|
|
562
|
-
expect(tmm.isAVLBalanced()).toBe(true);
|
|
563
|
-
expect(tmm.getHeight()).toBe(1);
|
|
564
|
-
|
|
565
|
-
expect(tmm.isAVLBalanced()).toBe(true);
|
|
566
|
-
|
|
567
|
-
const bfsIDs = tmm.bfs(node => node.key);
|
|
568
|
-
|
|
569
|
-
expect(bfsIDs[0]).toBe(12);
|
|
570
|
-
expect(bfsIDs[1]).toBe(2);
|
|
571
|
-
expect(bfsIDs[2]).toBe(16);
|
|
572
|
-
|
|
573
|
-
const bfsNodes = tmm.bfs(node => node);
|
|
574
|
-
|
|
575
|
-
expect(bfsNodes[0].key).toBe(12);
|
|
576
|
-
expect(bfsNodes[1].key).toBe(2);
|
|
577
|
-
expect(bfsNodes[2].key).toBe(16);
|
|
45
|
+
it('should add and find nodes with negative keys', () => {
|
|
46
|
+
tmm.add(-10);
|
|
47
|
+
tmm.add(-20);
|
|
578
48
|
|
|
579
|
-
|
|
580
|
-
|
|
49
|
+
expect(tmm.getNode(-10)).toBeInstanceOf(TreeMultiMapNode);
|
|
50
|
+
expect(tmm.getNode(-20)).toBeInstanceOf(TreeMultiMapNode);
|
|
51
|
+
});
|
|
581
52
|
});
|
|
582
53
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
[15, { key: 15, keyA: 15 }],
|
|
590
|
-
[1, { key: 1, keyA: 1 }],
|
|
591
|
-
[8, { key: 8, keyA: 8 }],
|
|
592
|
-
[13, { key: 13, keyA: 13 }],
|
|
593
|
-
[16, { key: 16, keyA: 16 }],
|
|
594
|
-
[2, { key: 2, keyA: 2 }],
|
|
595
|
-
[6, { key: 6, keyA: 6 }],
|
|
596
|
-
[9, { key: 9, keyA: 9 }],
|
|
597
|
-
[12, { key: 12, keyA: 12 }],
|
|
598
|
-
[14, { key: 14, keyA: 14 }],
|
|
599
|
-
[4, { key: 4, keyA: 4 }],
|
|
600
|
-
[7, { key: 7, keyA: 7 }],
|
|
601
|
-
[10, { key: 10, keyA: 10 }],
|
|
602
|
-
[5, { key: 5, keyA: 5 }]
|
|
603
|
-
];
|
|
604
|
-
|
|
605
|
-
objTreeMultiMap.addMany(values);
|
|
54
|
+
describe('deleteNode', () => {
|
|
55
|
+
it('should delete a node from the tmm', () => {
|
|
56
|
+
tmm.add(10);
|
|
57
|
+
tmm.add(20);
|
|
58
|
+
tmm.add(5);
|
|
59
|
+
tmm.delete(20);
|
|
606
60
|
|
|
607
|
-
|
|
61
|
+
expect(tmm.getNode(20)).toBe(undefined);
|
|
62
|
+
});
|
|
608
63
|
|
|
609
|
-
|
|
64
|
+
it('should handle deleting a non-existent node', () => {
|
|
65
|
+
tmm.add(10);
|
|
66
|
+
tmm.add(20);
|
|
67
|
+
tmm.add(5);
|
|
68
|
+
tmm.delete(15);
|
|
610
69
|
|
|
611
|
-
|
|
612
|
-
|
|
70
|
+
expect(tmm.getNode(15)).toBe(undefined);
|
|
71
|
+
});
|
|
613
72
|
|
|
614
|
-
|
|
73
|
+
it('should getNode performance O(log n)', () => {
|
|
74
|
+
for (let i = 0; i < 10; i++) tmm.add(i);
|
|
75
|
+
tmm.getNode(6);
|
|
76
|
+
});
|
|
615
77
|
});
|
|
616
|
-
});
|
|
617
78
|
|
|
618
|
-
describe('
|
|
619
|
-
|
|
620
|
-
|
|
79
|
+
describe('minimum', () => {
|
|
80
|
+
it('should find the minimum node in the tmm', () => {
|
|
81
|
+
tmm.add(10);
|
|
82
|
+
tmm.add(20);
|
|
83
|
+
tmm.add(5);
|
|
84
|
+
tmm.add(15);
|
|
85
|
+
tmm.add(3);
|
|
621
86
|
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
87
|
+
const minNode = tmm.getLeftMost(node => node, tmm.root);
|
|
88
|
+
expect(minNode?.key).toBe(3);
|
|
89
|
+
});
|
|
625
90
|
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
91
|
+
it('should handle an empty tmm', () => {
|
|
92
|
+
const minNode = tmm.getLeftMost(node => node, tmm.root);
|
|
93
|
+
expect(minNode).toBe(undefined);
|
|
94
|
+
});
|
|
630
95
|
});
|
|
631
96
|
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
tmm.add(
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
for (let i = 0; i < inputSize; i++) {
|
|
640
|
-
const num = getRandomInt(0, inputSize - 1);
|
|
641
|
-
tmm.delete(num);
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
let nilCount = 0;
|
|
645
|
-
const dfs = (cur: TreeMultiMapNode<number>) => {
|
|
646
|
-
if (isNaN(cur.key)) nilCount++;
|
|
647
|
-
if (cur.left) dfs(cur.left);
|
|
648
|
-
if (cur.right) dfs(cur.right);
|
|
649
|
-
};
|
|
650
|
-
if (tmm.root) dfs(tmm.root);
|
|
97
|
+
describe('getRightMost', () => {
|
|
98
|
+
it('should find the getRightMost node in the tmm', () => {
|
|
99
|
+
tmm.add(10);
|
|
100
|
+
tmm.add(20);
|
|
101
|
+
tmm.add(5);
|
|
102
|
+
tmm.add(15);
|
|
103
|
+
tmm.add(25);
|
|
651
104
|
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
105
|
+
const maxNode = tmm.getRightMost(node => node, tmm.root);
|
|
106
|
+
expect(maxNode?.key).toBe(25);
|
|
107
|
+
});
|
|
655
108
|
|
|
656
|
-
|
|
109
|
+
it('should handle an empty tmm', () => {
|
|
110
|
+
const maxNode = tmm.getRightMost(node => node, tmm.root);
|
|
111
|
+
expect(maxNode).toBe(undefined);
|
|
112
|
+
});
|
|
657
113
|
});
|
|
658
114
|
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
tmm.add(
|
|
664
|
-
|
|
115
|
+
describe('getSuccessor', () => {
|
|
116
|
+
it('should find the getSuccessor of a node', () => {
|
|
117
|
+
tmm.add(10);
|
|
118
|
+
tmm.add(20);
|
|
119
|
+
tmm.add(5);
|
|
120
|
+
tmm.add(15);
|
|
121
|
+
tmm.add(25);
|
|
665
122
|
|
|
666
|
-
|
|
667
|
-
tmm.
|
|
668
|
-
}
|
|
123
|
+
const node = tmm.getNode(15);
|
|
124
|
+
const successorNode = tmm.getSuccessor(node!);
|
|
669
125
|
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
if (isNaN(cur.key)) nilCount++;
|
|
673
|
-
if (cur.left) dfs(cur.left);
|
|
674
|
-
if (cur.right) dfs(cur.right);
|
|
675
|
-
};
|
|
676
|
-
if (tmm.root) dfs(tmm.root);
|
|
126
|
+
expect(successorNode?.key).toBe(20);
|
|
127
|
+
});
|
|
677
128
|
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
129
|
+
it('should handle a node with no getSuccessor', () => {
|
|
130
|
+
tmm.add(10);
|
|
131
|
+
tmm.add(5);
|
|
681
132
|
|
|
682
|
-
|
|
133
|
+
const node = tmm.getNode(10);
|
|
134
|
+
const successorNode = tmm.getSuccessor(node!);
|
|
135
|
+
// TODO not sure if it should be undefined or tmm.NIL
|
|
136
|
+
expect(successorNode).toBe(undefined);
|
|
137
|
+
});
|
|
683
138
|
});
|
|
684
139
|
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
tmm.add(
|
|
690
|
-
|
|
140
|
+
describe('getPredecessor', () => {
|
|
141
|
+
it('should find the getPredecessor of a node', () => {
|
|
142
|
+
tmm.add(10);
|
|
143
|
+
tmm.add(20);
|
|
144
|
+
tmm.add(5);
|
|
145
|
+
tmm.add(15);
|
|
146
|
+
tmm.add(25);
|
|
691
147
|
|
|
692
|
-
|
|
693
|
-
tmm.
|
|
694
|
-
}
|
|
148
|
+
const node = tmm.getNode(20);
|
|
149
|
+
const predecessorNode = tmm.getPredecessor(node!);
|
|
695
150
|
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
if (isNaN(cur.key)) nanCount++;
|
|
699
|
-
if (cur.left) dfs(cur.left);
|
|
700
|
-
if (cur.right) dfs(cur.right);
|
|
701
|
-
};
|
|
702
|
-
if (tmm.root) dfs(tmm.root);
|
|
151
|
+
expect(predecessorNode?.key).toBe(15);
|
|
152
|
+
});
|
|
703
153
|
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
154
|
+
it('should handle a node with no getPredecessor', () => {
|
|
155
|
+
tmm.add(10);
|
|
156
|
+
tmm.add(20);
|
|
707
157
|
|
|
708
|
-
|
|
158
|
+
const node = tmm.getNode(20);
|
|
159
|
+
const predecessorNode = tmm.getPredecessor(node!);
|
|
160
|
+
// TODO not sure if it should be tmm.NIL or something else.
|
|
161
|
+
expect(predecessorNode).toBe(tmm.getNode(20));
|
|
162
|
+
});
|
|
709
163
|
});
|
|
710
164
|
|
|
711
165
|
it('should the clone method', () => {
|
|
@@ -734,7 +188,7 @@ describe('TreeMultiMap delete test', function () {
|
|
|
734
188
|
expect(tmm.root?.left?.left?.key).toBe(NaN);
|
|
735
189
|
expect(tmm.root?.left?.right?.key).toBe(NaN);
|
|
736
190
|
expect(tmm.root?.right?.key).toBe('4');
|
|
737
|
-
expect(tmm.root?.right?.left?.key).toBe(
|
|
191
|
+
expect(tmm.root?.right?.left?.key).toBe('3');
|
|
738
192
|
expect(tmm.root?.right?.right?.key).toBe('5');
|
|
739
193
|
tmm.delete('3');
|
|
740
194
|
checkTreeStructure(tmm);
|
|
@@ -744,232 +198,650 @@ describe('TreeMultiMap delete test', function () {
|
|
|
744
198
|
expect(tmm.size).toBe(4);
|
|
745
199
|
expect(cloned.size).toBe(3);
|
|
746
200
|
});
|
|
201
|
+
|
|
202
|
+
it('should add value', () => {
|
|
203
|
+
const tmm = new TreeMultiMap<number, string>([4, 5, [1, ['1']], 2, 3]);
|
|
204
|
+
expect(tmm.get(1)).toEqual(['1']);
|
|
205
|
+
expect(tmm.getNode(1)?.value).toEqual([]);
|
|
206
|
+
tmm.add(1, 'a');
|
|
207
|
+
expect(tmm.get(1)).toEqual(['1', 'a']);
|
|
208
|
+
tmm.add([1, ['b']]);
|
|
209
|
+
expect(tmm.getNode(1)?.value).toEqual([]);
|
|
210
|
+
expect(tmm.get(1)).toEqual(['1', 'a', 'b']);
|
|
211
|
+
const tmmMapped = new TreeMultiMap<number>([4, 5, [1, ['1']], 2, 3]);
|
|
212
|
+
expect(tmmMapped.get(1)).toEqual(['1']);
|
|
213
|
+
expect(tmmMapped.getNode(1)?.value).toEqual([]);
|
|
214
|
+
tmmMapped.add(1, 'a');
|
|
215
|
+
expect(tmmMapped.get(1)).toEqual(['1', 'a']);
|
|
216
|
+
tmmMapped.add([1, ['b']]);
|
|
217
|
+
expect(tmmMapped.getNode(1)?.value).toEqual([]);
|
|
218
|
+
expect(tmmMapped.get(1)).toEqual(['1', 'a', 'b']);
|
|
219
|
+
});
|
|
747
220
|
});
|
|
748
221
|
|
|
749
|
-
describe('TreeMultiMap
|
|
750
|
-
let
|
|
222
|
+
describe('TreeMultiMap 2', () => {
|
|
223
|
+
let tmm: TreeMultiMap<number>;
|
|
224
|
+
|
|
751
225
|
beforeEach(() => {
|
|
752
|
-
|
|
753
|
-
treeMM.add(1, 'a', 10);
|
|
754
|
-
treeMM.add([2, 'b'], undefined, 10);
|
|
755
|
-
treeMM.add([3, 'c'], undefined, 1);
|
|
226
|
+
tmm = new TreeMultiMap<number>();
|
|
756
227
|
});
|
|
757
228
|
|
|
758
|
-
it('
|
|
759
|
-
|
|
760
|
-
expect(
|
|
761
|
-
|
|
762
|
-
expect(
|
|
763
|
-
|
|
229
|
+
it('should add nodes into the tmm', () => {
|
|
230
|
+
tmm.add(10);
|
|
231
|
+
expect(tmm.getNode(10)).toBeDefined();
|
|
232
|
+
tmm.add(20);
|
|
233
|
+
expect(tmm.getNode(20)).toBeDefined();
|
|
234
|
+
tmm.add(5);
|
|
235
|
+
expect(tmm.getNode(5)).toBeDefined();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('should delete nodes from the tmm', () => {
|
|
239
|
+
tmm.add(10);
|
|
240
|
+
tmm.add(20);
|
|
241
|
+
tmm.add(5);
|
|
242
|
+
tmm.delete(20);
|
|
243
|
+
expect(tmm.getNode(20)).toBe(undefined);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('should get the successor of a node', () => {
|
|
247
|
+
tmm.add(10);
|
|
248
|
+
tmm.add(20);
|
|
249
|
+
const node = tmm.getNode(10);
|
|
250
|
+
const successor = tmm.getSuccessor(node!);
|
|
251
|
+
expect(successor?.key).toBe(20);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('should get the predecessor of a node', () => {
|
|
255
|
+
tmm.add(10);
|
|
256
|
+
tmm.add(20);
|
|
257
|
+
const node = tmm.getNode(20);
|
|
258
|
+
const predecessor = tmm.getPredecessor(node!);
|
|
259
|
+
expect(predecessor?.key).toBe(20);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('should rotate nodes to the left', () => {
|
|
263
|
+
tmm.add(10);
|
|
264
|
+
tmm.add(20);
|
|
265
|
+
tmm.add(5);
|
|
266
|
+
const node = tmm.getNode(10);
|
|
267
|
+
tmm.add(15);
|
|
268
|
+
// Verify that rotation has occurred
|
|
269
|
+
expect(node?.left?.key).toBe(5);
|
|
270
|
+
expect(node?.right?.key).toBe(20);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('should rotate nodes to the right', () => {
|
|
274
|
+
tmm.add(10);
|
|
275
|
+
tmm.add(20);
|
|
276
|
+
tmm.add(5);
|
|
277
|
+
const node = tmm.getNode(20);
|
|
278
|
+
tmm.add(25);
|
|
279
|
+
// Verify that rotation has occurred
|
|
280
|
+
expect(node?.left?.key).toBeNaN();
|
|
281
|
+
expect(node?.right?.key).toBe(25);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('should all node attributes fully conform to the red-black tmm standards.', () => {
|
|
285
|
+
tmm.add(10);
|
|
286
|
+
tmm.add(20);
|
|
287
|
+
tmm.add(5);
|
|
288
|
+
tmm.add(15);
|
|
289
|
+
tmm.add(21);
|
|
290
|
+
tmm.add(6);
|
|
291
|
+
tmm.add(2);
|
|
292
|
+
|
|
293
|
+
let node10F = tmm.getNode(10);
|
|
294
|
+
let node20F = tmm.getNode(20);
|
|
295
|
+
let node5F = tmm.getNode(5);
|
|
296
|
+
let node15F = tmm.getNode(15);
|
|
297
|
+
let node21F = tmm.getNode(21);
|
|
298
|
+
let node6F = tmm.getNode(6);
|
|
299
|
+
let node2F = tmm.getNode(2);
|
|
300
|
+
expect(node10F?.key).toBe(10);
|
|
301
|
+
expect(node10F?.color).toBe('BLACK');
|
|
302
|
+
expect(node10F?.left).toBe(node5F);
|
|
303
|
+
expect(node10F?.right).toBe(node20F);
|
|
304
|
+
expect(node10F?.parent).toBe(undefined);
|
|
305
|
+
expect(node20F?.key).toBe(20);
|
|
306
|
+
expect(node20F?.color).toBe('BLACK');
|
|
307
|
+
expect(node20F?.left).toBe(node15F);
|
|
308
|
+
expect(node20F?.right).toBe(node21F);
|
|
309
|
+
expect(node20F?.parent).toBe(node10F);
|
|
310
|
+
expect(node5F?.key).toBe(5);
|
|
311
|
+
expect(node5F?.color).toBe('BLACK');
|
|
312
|
+
expect(node5F?.left).toBe(node2F);
|
|
313
|
+
expect(node5F?.right).toBe(node6F);
|
|
314
|
+
expect(node5F?.parent).toBe(node10F);
|
|
315
|
+
expect(node15F?.key).toBe(15);
|
|
316
|
+
expect(node15F?.color).toBe('RED');
|
|
317
|
+
expect(node15F?.left).toBe(tmm.NIL);
|
|
318
|
+
expect(node15F?.right).toBe(tmm.NIL);
|
|
319
|
+
expect(node15F?.parent).toBe(node20F);
|
|
320
|
+
expect(node21F?.key).toBe(21);
|
|
321
|
+
expect(node21F?.color).toBe('RED');
|
|
322
|
+
expect(node21F?.left).toBe(tmm.NIL);
|
|
323
|
+
expect(node21F?.right).toBe(tmm.NIL);
|
|
324
|
+
expect(node21F?.parent).toBe(node20F);
|
|
325
|
+
expect(node6F?.key).toBe(6);
|
|
326
|
+
expect(node6F?.color).toBe('RED');
|
|
327
|
+
expect(node6F?.left).toBe(tmm.NIL);
|
|
328
|
+
expect(node6F?.right).toBe(tmm.NIL);
|
|
329
|
+
expect(node6F?.parent).toBe(node5F);
|
|
330
|
+
expect(node2F?.key).toBe(2);
|
|
331
|
+
expect(node2F?.color).toBe('RED');
|
|
332
|
+
expect(node2F?.left).toBe(tmm.NIL);
|
|
333
|
+
expect(node2F?.right).toBe(tmm.NIL);
|
|
334
|
+
expect(node2F?.parent).toBe(node5F);
|
|
335
|
+
expect(node15F?.key).toBe(15);
|
|
336
|
+
expect(node15F?.color).toBe('RED');
|
|
337
|
+
expect(node15F?.left).toBe(tmm.NIL);
|
|
338
|
+
expect(node15F?.right).toBe(tmm.NIL);
|
|
339
|
+
expect(node15F?.parent).toBe(node20F);
|
|
340
|
+
tmm.delete(5);
|
|
341
|
+
node10F = tmm.getNode(10);
|
|
342
|
+
node20F = tmm.getNode(20);
|
|
343
|
+
node5F = tmm.getNode(5);
|
|
344
|
+
node15F = tmm.getNode(15);
|
|
345
|
+
node21F = tmm.getNode(21);
|
|
346
|
+
node6F = tmm.getNode(6);
|
|
347
|
+
node2F = tmm.getNode(2);
|
|
348
|
+
expect(node10F?.key).toBe(10);
|
|
349
|
+
expect(node10F?.color).toBe('BLACK');
|
|
350
|
+
expect(node10F?.left).toBe(node6F);
|
|
351
|
+
expect(node10F?.right).toBe(node20F);
|
|
352
|
+
expect(node10F?.parent).toBe(undefined);
|
|
353
|
+
expect(node20F?.key).toBe(20);
|
|
354
|
+
expect(node20F?.color).toBe('BLACK');
|
|
355
|
+
expect(node20F?.left).toBe(node15F);
|
|
356
|
+
expect(node20F?.right).toBe(node21F);
|
|
357
|
+
expect(node20F?.parent).toBe(node10F);
|
|
358
|
+
expect(node5F).toBe(undefined);
|
|
359
|
+
expect(node15F?.key).toBe(15);
|
|
360
|
+
expect(node15F?.color).toBe('RED');
|
|
361
|
+
expect(node15F?.left).toBe(tmm.NIL);
|
|
362
|
+
expect(node15F?.right).toBe(tmm.NIL);
|
|
363
|
+
expect(node15F?.parent).toBe(node20F);
|
|
364
|
+
expect(node21F?.key).toBe(21);
|
|
365
|
+
expect(node21F?.color).toBe('RED');
|
|
366
|
+
expect(node21F?.left).toBe(tmm.NIL);
|
|
367
|
+
expect(node21F?.right).toBe(tmm.NIL);
|
|
368
|
+
expect(node21F?.parent).toBe(node20F);
|
|
369
|
+
expect(node6F?.key).toBe(6);
|
|
370
|
+
expect(node6F?.color).toBe('BLACK');
|
|
371
|
+
expect(node6F?.left).toBe(node2F);
|
|
372
|
+
expect(node6F?.right).toBe(tmm.NIL);
|
|
373
|
+
expect(node6F?.parent).toBe(node10F);
|
|
374
|
+
expect(node2F?.key).toBe(2);
|
|
375
|
+
expect(node2F?.color).toBe('RED');
|
|
376
|
+
expect(node2F?.left).toBe(tmm.NIL);
|
|
377
|
+
expect(node2F?.right).toBe(tmm.NIL);
|
|
378
|
+
expect(node2F?.parent).toBe(node6F);
|
|
379
|
+
expect(node15F?.key).toBe(15);
|
|
380
|
+
expect(node15F?.color).toBe('RED');
|
|
381
|
+
expect(node15F?.left).toBe(tmm.NIL);
|
|
382
|
+
expect(node15F?.right).toBe(tmm.NIL);
|
|
383
|
+
expect(node15F?.parent).toBe(node20F);
|
|
384
|
+
tmm.delete(20);
|
|
385
|
+
node10F = tmm.getNode(10);
|
|
386
|
+
node20F = tmm.getNode(20);
|
|
387
|
+
node5F = tmm.getNode(5);
|
|
388
|
+
node15F = tmm.getNode(15);
|
|
389
|
+
node21F = tmm.getNode(21);
|
|
390
|
+
node6F = tmm.getNode(6);
|
|
391
|
+
node2F = tmm.getNode(2);
|
|
392
|
+
expect(node10F?.key).toBe(10);
|
|
393
|
+
expect(node10F?.color).toBe('BLACK');
|
|
394
|
+
expect(node10F?.left).toBe(node6F);
|
|
395
|
+
expect(node10F?.right).toBe(node21F);
|
|
396
|
+
expect(node10F?.parent).toBe(undefined);
|
|
397
|
+
expect(node20F).toBe(undefined);
|
|
398
|
+
expect(node5F).toBe(undefined);
|
|
399
|
+
expect(node15F?.key).toBe(15);
|
|
400
|
+
expect(node15F?.color).toBe('RED');
|
|
401
|
+
expect(node15F?.left).toBe(tmm.NIL);
|
|
402
|
+
expect(node15F?.right).toBe(tmm.NIL);
|
|
403
|
+
expect(node15F?.parent).toBe(node21F);
|
|
404
|
+
expect(node21F?.key).toBe(21);
|
|
405
|
+
expect(node21F?.color).toBe('BLACK');
|
|
406
|
+
expect(node21F?.left).toBe(node15F);
|
|
407
|
+
expect(node21F?.right).toBe(tmm.NIL);
|
|
408
|
+
expect(node21F?.parent).toBe(node10F);
|
|
409
|
+
expect(node6F?.key).toBe(6);
|
|
410
|
+
expect(node6F?.color).toBe('BLACK');
|
|
411
|
+
expect(node6F?.left).toBe(node2F);
|
|
412
|
+
expect(node6F?.right).toBe(tmm.NIL);
|
|
413
|
+
expect(node6F?.parent).toBe(node10F);
|
|
414
|
+
expect(node2F?.key).toBe(2);
|
|
415
|
+
expect(node2F?.color).toBe('RED');
|
|
416
|
+
expect(node2F?.left).toBe(tmm.NIL);
|
|
417
|
+
expect(node2F?.right).toBe(tmm.NIL);
|
|
418
|
+
expect(node2F?.parent).toBe(node6F);
|
|
419
|
+
expect(node15F?.key).toBe(15);
|
|
420
|
+
expect(node15F?.color).toBe('RED');
|
|
421
|
+
expect(node15F?.left).toBe(tmm.NIL);
|
|
422
|
+
expect(node15F?.right).toBe(tmm.NIL);
|
|
423
|
+
expect(node15F?.parent).toBe(node21F);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it('should fix the tmm after insertion', () => {
|
|
427
|
+
tmm.add(1);
|
|
428
|
+
tmm.add(2);
|
|
429
|
+
tmm.add(5);
|
|
430
|
+
tmm.add(15);
|
|
431
|
+
const node15F = tmm.getNode(15);
|
|
432
|
+
expect(node15F?.left).toBe(tmm.NIL);
|
|
433
|
+
expect(node15F?.right).toBe(tmm.NIL);
|
|
434
|
+
expect(node15F?.parent).toBe(tmm.getNode(5));
|
|
435
|
+
|
|
436
|
+
tmm.add(25);
|
|
437
|
+
tmm.add(10);
|
|
438
|
+
tmm.add(8);
|
|
439
|
+
tmm.add(28);
|
|
440
|
+
tmm.add(111);
|
|
441
|
+
tmm.add(12);
|
|
442
|
+
tmm.delete(2);
|
|
443
|
+
tmm.add(22);
|
|
444
|
+
tmm.add(50);
|
|
445
|
+
tmm.add(155);
|
|
446
|
+
tmm.add(225);
|
|
447
|
+
const node225F = tmm.getNode(225);
|
|
448
|
+
expect(node225F?.left).toBe(tmm.NIL);
|
|
449
|
+
expect(node225F?.right).toBe(tmm.NIL);
|
|
450
|
+
expect(node225F?.parent?.key).toBe(155);
|
|
451
|
+
tmm.add(7);
|
|
452
|
+
if (isDebug) tmm.print();
|
|
764
453
|
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
454
|
+
const node15S = tmm.getNode(15);
|
|
455
|
+
expect(node15S?.left?.key).toBe(10);
|
|
456
|
+
expect(node15S?.right?.key).toBe(25);
|
|
457
|
+
expect(tmm.root).toBe(tmm.getNode(8));
|
|
458
|
+
expect(node15S?.parent?.key).toBe(28);
|
|
459
|
+
tmm.delete(15);
|
|
460
|
+
expect(tmm.root?.key).toBe(8);
|
|
461
|
+
expect(tmm.root?.parent).toBe(undefined);
|
|
462
|
+
|
|
463
|
+
const node15T = tmm.getNode(15);
|
|
464
|
+
expect(node15T).toBe(undefined);
|
|
465
|
+
|
|
466
|
+
tmm.add(23);
|
|
467
|
+
tmm.add(33);
|
|
468
|
+
tmm.add(15);
|
|
469
|
+
|
|
470
|
+
const nodeLM = tmm.getLeftMost();
|
|
471
|
+
expect(nodeLM).toBe(1);
|
|
472
|
+
|
|
473
|
+
const node50 = tmm.getNode(50);
|
|
474
|
+
expect(node50?.key).toBe(50);
|
|
475
|
+
expect(node50?.left?.key).toBe(33);
|
|
476
|
+
expect(node50?.right).toBe(tmm.NIL);
|
|
477
|
+
const node15Fo = tmm.getNode(15);
|
|
478
|
+
|
|
479
|
+
expect(node15Fo?.key).toBe(15);
|
|
480
|
+
expect(node15Fo?.left).toBe(tmm.NIL);
|
|
481
|
+
const node225S = tmm.getNode(225);
|
|
482
|
+
expect(node225S?.left).toBe(tmm.NIL);
|
|
483
|
+
expect(node225S?.right).toBe(tmm.NIL);
|
|
484
|
+
expect(node225S?.parent?.key).toBe(155);
|
|
485
|
+
// TODO
|
|
486
|
+
// expect(tmm.getNode(0)).toBe(undefined);
|
|
487
|
+
tmm.add(2);
|
|
488
|
+
tmm.add(3);
|
|
489
|
+
tmm.add(4);
|
|
490
|
+
tmm.add(6);
|
|
491
|
+
tmm.add(9);
|
|
492
|
+
tmm.add(11);
|
|
493
|
+
tmm.add(13);
|
|
494
|
+
tmm.add(14);
|
|
495
|
+
tmm.add(16);
|
|
496
|
+
tmm.add(17);
|
|
497
|
+
tmm.add(18);
|
|
498
|
+
tmm.add(19);
|
|
499
|
+
tmm.add(110);
|
|
770
500
|
|
|
771
|
-
|
|
772
|
-
expect(mockCallback.mock.calls[0]).toEqual([1, 'a']);
|
|
773
|
-
expect(mockCallback.mock.calls[1]).toEqual([2, 'b']);
|
|
774
|
-
expect(mockCallback.mock.calls[2]).toEqual([3, 'c']);
|
|
775
|
-
});
|
|
501
|
+
if (isDebug) tmm.print();
|
|
776
502
|
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
expect(filteredTree.size).toBe(2);
|
|
780
|
-
expect([...filteredTree]).toEqual([
|
|
781
|
-
[2, 'b'],
|
|
782
|
-
[3, 'c']
|
|
503
|
+
expect(tmm.dfs()).toEqual([
|
|
504
|
+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22, 23, 25, 28, 33, 50, 110, 111, 155, 225
|
|
783
505
|
]);
|
|
506
|
+
|
|
507
|
+
expect(tmm.isBST()).toBe(true);
|
|
784
508
|
});
|
|
785
509
|
|
|
786
|
-
it('
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
510
|
+
it('should fix the tmm after insertion and deletion', () => {
|
|
511
|
+
for (let i = 0; i < 100; i++) {
|
|
512
|
+
tmm.add(i);
|
|
513
|
+
}
|
|
514
|
+
for (let i = 0; i < 49; i++) {
|
|
515
|
+
tmm.delete(i);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
expect(tmm.size).toBe(51);
|
|
519
|
+
expect(tmm.isBST()).toBe(true);
|
|
520
|
+
expect(tmm.isBST(tmm.root, 'RECURSIVE')).toBe(true);
|
|
521
|
+
|
|
522
|
+
expect(tmm.dfs(n => n.key, 'IN', tmm.root, 'ITERATIVE')).toEqual([
|
|
523
|
+
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
|
|
524
|
+
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
|
|
525
|
+
]);
|
|
526
|
+
expect(tmm.dfs(n => n.key, 'IN', tmm.root, 'RECURSIVE')).toEqual([
|
|
527
|
+
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
|
|
528
|
+
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
|
|
793
529
|
]);
|
|
794
530
|
});
|
|
795
531
|
|
|
796
|
-
it('
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
532
|
+
it('should fix the tmm after large scale insertion and deletion', () => {
|
|
533
|
+
for (let i = 0; i < 10000; i++) {
|
|
534
|
+
tmm.add(i);
|
|
535
|
+
}
|
|
536
|
+
for (let i = 0; i < 10000; i++) {
|
|
537
|
+
tmm.delete(i);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
expect(tmm.size).toBe(0);
|
|
541
|
+
expect(tmm.isBST()).toBe(true);
|
|
542
|
+
expect(tmm.dfs(n => n.key, 'IN', tmm.root, 'ITERATIVE')).toEqual([]);
|
|
800
543
|
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
544
|
+
tmm.clear();
|
|
545
|
+
for (let i = 0; i < 1000; i++) {
|
|
546
|
+
tmm.add(getRandomInt(-100, 1000));
|
|
547
|
+
tmm.delete(getRandomInt(-100, 1000));
|
|
805
548
|
}
|
|
806
549
|
|
|
807
|
-
|
|
808
|
-
expect(
|
|
809
|
-
[1, 'a'],
|
|
810
|
-
[2, 'b'],
|
|
811
|
-
[3, 'c']
|
|
812
|
-
]);
|
|
550
|
+
// TODO there is a bug when dfs the tmm with NIL node
|
|
551
|
+
// expect(tmm.isBST()).toBe(true);
|
|
813
552
|
});
|
|
814
553
|
|
|
815
|
-
it('
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
expect(
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
554
|
+
it('duplicates', () => {
|
|
555
|
+
tmm.addMany([9, 8, 7, 8, 8, 8, 2, 3, 6, 5, 5, 4]);
|
|
556
|
+
if (isDebug) tmm.print();
|
|
557
|
+
|
|
558
|
+
expect(tmm.size).toBe(8);
|
|
559
|
+
expect(tmm.isBST()).toBe(true);
|
|
560
|
+
expect(tmm.isAVLBalanced()).toBe(true);
|
|
561
|
+
tmm.addMany([10, 5, 2, 11]);
|
|
562
|
+
expect(tmm.size).toBe(10);
|
|
563
|
+
expect(tmm.isBST()).toBe(true);
|
|
564
|
+
expect(tmm.isAVLBalanced()).toBe(true);
|
|
823
565
|
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
expect(
|
|
566
|
+
tmm.clear();
|
|
567
|
+
tmm.addMany([10, 20, 30, 40, 50, 60]);
|
|
568
|
+
expect(tmm.isAVLBalanced()).toBe(false);
|
|
827
569
|
});
|
|
828
570
|
|
|
829
|
-
|
|
830
|
-
const
|
|
831
|
-
|
|
571
|
+
describe('TreeMultiMap delete test', function () {
|
|
572
|
+
const tmm = new TreeMultiMap<number, number>();
|
|
573
|
+
const inputSize = 100; // Adjust input sizes as needed
|
|
574
|
+
|
|
575
|
+
beforeEach(() => {
|
|
576
|
+
tmm.clear();
|
|
577
|
+
});
|
|
578
|
+
it('The structure remains normal after random deletion', function () {
|
|
579
|
+
for (let i = 0; i < inputSize; i++) {
|
|
580
|
+
tmm.add(i);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
for (let i = 0; i < inputSize; i++) {
|
|
584
|
+
const num = getRandomInt(0, inputSize - 1);
|
|
585
|
+
tmm.delete(num);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
let nanCount = 0;
|
|
589
|
+
const dfs = (cur: TreeMultiMapNode<number>) => {
|
|
590
|
+
if (isNaN(cur.key)) nanCount++;
|
|
591
|
+
if (cur.left) dfs(cur.left);
|
|
592
|
+
if (cur.right) dfs(cur.right);
|
|
593
|
+
};
|
|
594
|
+
if (tmm.root) dfs(tmm.root);
|
|
595
|
+
|
|
596
|
+
expect(tmm.size).toBeLessThanOrEqual(inputSize);
|
|
597
|
+
expect(tmm.getHeight()).toBeLessThan(Math.log2(inputSize) * 2);
|
|
598
|
+
|
|
599
|
+
expect(nanCount).toBeLessThanOrEqual(inputSize);
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
it(`Random additions, complete deletions of structures are normal`, function () {
|
|
603
|
+
for (let i = 0; i < inputSize; i++) {
|
|
604
|
+
const num = getRandomInt(0, inputSize - 1);
|
|
605
|
+
if (i === 0 && isDebug) console.log(`first:`, num);
|
|
606
|
+
tmm.add(num);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
for (let i = 0; i < inputSize; i++) {
|
|
610
|
+
tmm.delete(i);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
let nanCount = 0;
|
|
614
|
+
const dfs = (cur: TreeMultiMapNode<number>) => {
|
|
615
|
+
if (isNaN(cur.key)) nanCount++;
|
|
616
|
+
if (cur.left) dfs(cur.left);
|
|
617
|
+
if (cur.right) dfs(cur.right);
|
|
618
|
+
};
|
|
619
|
+
if (tmm.root) dfs(tmm.root);
|
|
620
|
+
|
|
621
|
+
expect(tmm.size).toBe(0);
|
|
622
|
+
expect(tmm.getHeight()).toBe(-1);
|
|
623
|
+
expect(nanCount).toBeLessThanOrEqual(inputSize);
|
|
624
|
+
|
|
625
|
+
if (isDebug) tmm.print();
|
|
626
|
+
});
|
|
832
627
|
});
|
|
833
628
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
629
|
+
describe('TreeMultiMap iterative methods test', () => {
|
|
630
|
+
let tmm: TreeMultiMap<number, string, object, string, string, object>;
|
|
631
|
+
beforeEach(() => {
|
|
632
|
+
tmm = new TreeMultiMap();
|
|
633
|
+
tmm.add([1, ['a']]);
|
|
634
|
+
tmm.add(2, 'b');
|
|
635
|
+
tmm.add([3, ['c']]);
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
it('The node obtained by get Node should match the node type', () => {
|
|
639
|
+
const node3 = tmm.getNode(3);
|
|
640
|
+
expect(node3).toBeInstanceOf(BinaryTreeNode);
|
|
641
|
+
expect(node3).toBeInstanceOf(BSTNode);
|
|
642
|
+
expect(node3).toBeInstanceOf(TreeMultiMapNode);
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
it('forEach should iterate over all elements', () => {
|
|
646
|
+
const mockCallback = jest.fn();
|
|
647
|
+
tmm.forEach((key, value) => {
|
|
648
|
+
mockCallback(key, value);
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
expect(mockCallback.mock.calls.length).toBe(3);
|
|
652
|
+
expect(mockCallback.mock.calls[0]).toEqual([1, ['a']]);
|
|
653
|
+
expect(mockCallback.mock.calls[1]).toEqual([2, ['b']]);
|
|
654
|
+
expect(mockCallback.mock.calls[2]).toEqual([3, ['c']]);
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
it('filter should return a new tmm with filtered elements', () => {
|
|
658
|
+
const filteredTree = tmm.filter(key => key > 1);
|
|
659
|
+
expect(filteredTree.size).toBe(2);
|
|
660
|
+
expect([...filteredTree]).toEqual([
|
|
661
|
+
[2, ['b']],
|
|
662
|
+
[3, ['c']]
|
|
663
|
+
]);
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
it('map should return a new tmm with modified elements', () => {
|
|
667
|
+
const tmmMapped = tmm.map((key, value) => [(key * 2).toString(), value ? value :[]]);
|
|
668
|
+
expect(tmmMapped.size).toBe(3);
|
|
669
|
+
expect([...tmmMapped]).toEqual([
|
|
670
|
+
['2', ['a']],
|
|
671
|
+
['4', ['b']],
|
|
672
|
+
['6', ['c']]
|
|
673
|
+
]);
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
it('reduce should accumulate values', () => {
|
|
677
|
+
const sum = tmm.reduce((acc, value, key) => acc + key, 0);
|
|
678
|
+
expect(sum).toBe(6);
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
it('[Symbol.iterator] should provide an iterator', () => {
|
|
682
|
+
const entries = [];
|
|
683
|
+
for (const entry of tmm) {
|
|
684
|
+
entries.push(entry);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
expect(entries.length).toBe(3);
|
|
688
|
+
expect(entries).toEqual([
|
|
689
|
+
[1, ['a']],
|
|
690
|
+
[2, ['b']],
|
|
691
|
+
[3, ['c']]
|
|
692
|
+
]);
|
|
693
|
+
});
|
|
837
694
|
});
|
|
838
695
|
});
|
|
839
696
|
|
|
840
|
-
describe('TreeMultiMap
|
|
841
|
-
let tmm: TreeMultiMap<number>;
|
|
697
|
+
describe('TreeMultiMap - _deleteFixup', () => {
|
|
698
|
+
let tmm: TreeMultiMap<number, number>;
|
|
699
|
+
|
|
842
700
|
beforeEach(() => {
|
|
843
|
-
tmm = new TreeMultiMap
|
|
701
|
+
tmm = new TreeMultiMap();
|
|
844
702
|
});
|
|
845
703
|
|
|
846
|
-
it('
|
|
847
|
-
tmm.
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
]);
|
|
854
|
-
const newNode = new TreeMultiMapNode(3, undefined, 10);
|
|
855
|
-
tmm.add(newNode, 33, 20);
|
|
856
|
-
// TODO expect(tmm.count).toBe(25);
|
|
857
|
-
expect(tmm.count).toBe(15);
|
|
858
|
-
expect(tmm.getComputedCount()).toBe(15);
|
|
859
|
-
expect(tmm.getNode(3)?.count).toBe(11);
|
|
704
|
+
it('should handle deleting a red leaf node', () => {
|
|
705
|
+
tmm.add(10, 10);
|
|
706
|
+
tmm.add(5, 5); // Red leaf
|
|
707
|
+
tmm.add(20, 20);
|
|
708
|
+
|
|
709
|
+
expect(tmm.delete(5)).toHaveLength(1); // Delete red leaf
|
|
710
|
+
expect(tmm.root?.left).toBe(tmm.NIL); // Left child should be NIL
|
|
860
711
|
});
|
|
861
|
-
});
|
|
862
712
|
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
tmm.add([11, 11]);
|
|
870
|
-
tmm.add([3, 3]);
|
|
871
|
-
const idAndValues: [number, number][] = [
|
|
872
|
-
[11, 11],
|
|
873
|
-
[3, 3],
|
|
874
|
-
[15, 15],
|
|
875
|
-
[1, 1],
|
|
876
|
-
[8, 8],
|
|
877
|
-
[13, 13],
|
|
878
|
-
[16, 16],
|
|
879
|
-
[2, 2],
|
|
880
|
-
[6, 6],
|
|
881
|
-
[9, 9],
|
|
882
|
-
[12, 12],
|
|
883
|
-
[14, 14],
|
|
884
|
-
[4, 4],
|
|
885
|
-
[7, 7],
|
|
886
|
-
[10, 10],
|
|
887
|
-
[5, 5]
|
|
888
|
-
];
|
|
889
|
-
tmm.addMany(idAndValues);
|
|
890
|
-
expect(tmm.root instanceof TreeMultiMapNode);
|
|
891
|
-
|
|
892
|
-
if (tmm.root) expect(tmm.root.key == 11);
|
|
893
|
-
|
|
894
|
-
expect(tmm.size).toBe(16);
|
|
895
|
-
expect(tmm.count).toBe(18);
|
|
896
|
-
expect(tmm.getComputedCount()).toBe(18);
|
|
897
|
-
|
|
898
|
-
expect(tmm.has(6));
|
|
899
|
-
if (isDebug) tmm.print();
|
|
900
|
-
expect(tmm.getHeight(6)).toBe(1);
|
|
901
|
-
expect(tmm.getDepth(6)).toBe(3);
|
|
902
|
-
const nodeId10 = tmm.getNode(10);
|
|
903
|
-
expect(nodeId10?.key).toBe(10);
|
|
713
|
+
it('should handle deleting a black leaf node', () => {
|
|
714
|
+
tmm.add(10, 10);
|
|
715
|
+
tmm.add(5, 5); // Black node
|
|
716
|
+
tmm.add(20, 20);
|
|
717
|
+
tmm.add(1, 1); // Black leaf node
|
|
904
718
|
|
|
905
|
-
|
|
906
|
-
expect(
|
|
719
|
+
expect(tmm.delete(1)).toHaveLength(1); // Delete black leaf
|
|
720
|
+
expect(tmm.root?.left?.left).toBe(tmm.NIL);
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
it('should handle deleting black node with red sibling', () => {
|
|
724
|
+
tmm.add(10, 10);
|
|
725
|
+
tmm.add(5, 5); // Black node
|
|
726
|
+
tmm.add(20, 20); // Red sibling
|
|
727
|
+
tmm.add(25, 25); // Force the sibling to be red
|
|
728
|
+
|
|
729
|
+
expect(tmm.delete(5)).toHaveLength(1); // Delete black node
|
|
730
|
+
expect(tmm.root?.right?.color).toBe('BLACK'); // Ensure sibling color is black after fixup
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
it('should handle deleting black node with black sibling', () => {
|
|
734
|
+
tmm.add(10, 10);
|
|
735
|
+
tmm.add(5, 5); // Black node
|
|
736
|
+
tmm.add(20, 20); // Black sibling
|
|
737
|
+
|
|
738
|
+
expect(tmm.delete(5)).toHaveLength(1); // Delete black node
|
|
739
|
+
expect(tmm.root?.left).toBe(tmm.NIL);
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
it('should handle deleting the root node', () => {
|
|
743
|
+
tmm.add(10, 10); // Root node
|
|
744
|
+
tmm.add(5, 5);
|
|
745
|
+
tmm.add(20, 20);
|
|
746
|
+
|
|
747
|
+
expect(tmm.delete(10)).toHaveLength(1); // Delete root node
|
|
748
|
+
expect(tmm.root?.key).toBe(20); // New root should be 20
|
|
749
|
+
});
|
|
750
|
+
|
|
751
|
+
it('should handle complex case with multiple rotations', () => {
|
|
752
|
+
tmm.add(10, 10);
|
|
753
|
+
tmm.add(5, 5);
|
|
754
|
+
tmm.add(15, 15);
|
|
755
|
+
tmm.add(12, 12);
|
|
756
|
+
tmm.add(18, 18);
|
|
757
|
+
tmm.add(16, 16);
|
|
758
|
+
|
|
759
|
+
// Delete a node that will cause rotations and color changes
|
|
760
|
+
expect(tmm.delete(5)).toHaveLength(1);
|
|
761
|
+
|
|
762
|
+
// Verify the color and structure after fixup
|
|
763
|
+
expect(tmm.root?.color).toBe('BLACK');
|
|
764
|
+
expect(tmm.root?.left).toBe(tmm.NIL);
|
|
765
|
+
expect(tmm.root?.right?.left?.color).toBe('BLACK');
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
it('should handle complex delete fixup scenarios', () => {
|
|
769
|
+
const tmm = new TreeMultiMap<number, number>();
|
|
770
|
+
|
|
771
|
+
// Build a tmm that will require complex fixup
|
|
772
|
+
tmm.add(20, 20);
|
|
773
|
+
tmm.add(10, 10);
|
|
774
|
+
tmm.add(30, 30);
|
|
775
|
+
tmm.add(5, 5);
|
|
776
|
+
tmm.add(15, 15);
|
|
777
|
+
tmm.add(25, 25);
|
|
778
|
+
tmm.add(35, 35);
|
|
779
|
+
tmm.add(2, 2);
|
|
780
|
+
tmm.add(8, 8);
|
|
781
|
+
|
|
782
|
+
// This deletion should trigger a complex fixup
|
|
783
|
+
tmm.delete(2);
|
|
784
|
+
// tmm.print(tmm.root, { isShowNull: true, isShowRedBlackNIL: true, isShowUndefined: false });
|
|
785
|
+
|
|
786
|
+
expect(tmm.isLeaf(2)).toBe(false);
|
|
787
|
+
expect(tmm.isLeaf(8)).toBe(true);
|
|
788
|
+
expect(tmm.isLeaf(15)).toBe(true);
|
|
789
|
+
expect(tmm.isLeaf(25)).toBe(true);
|
|
790
|
+
expect(tmm.isLeaf(35)).toBe(true);
|
|
791
|
+
expect(tmm.isLeaf(20)).toBe(false);
|
|
792
|
+
expect(tmm.isLeaf(30)).toBe(false);
|
|
793
|
+
// Verify tmm structure and colors after fixup
|
|
794
|
+
expect(tmm.root?.color).toBe('BLACK');
|
|
795
|
+
expect(tmm.root?.key).toBe(20);
|
|
796
|
+
expect(tmm.root?.left?.color).toBe('RED');
|
|
797
|
+
expect(tmm.root?.left?.key).toBe(10);
|
|
798
|
+
expect(tmm.root?.right?.color).toBe('BLACK');
|
|
799
|
+
expect(tmm.root?.right?.key).toBe(30);
|
|
800
|
+
expect(tmm.root?.left?.left?.color).toBe('BLACK');
|
|
801
|
+
expect(tmm.root?.left?.left?.key).toBe(5);
|
|
802
|
+
expect(tmm.root?.left?.right?.color).toBe('BLACK');
|
|
803
|
+
expect(tmm.root?.left?.right?.key).toBe(15);
|
|
804
|
+
expect(tmm.leaves(node => (node === null ? '' : `${node.key} ${node.color}`), tmm.root, 'RECURSIVE')).toEqual([
|
|
805
|
+
'8 RED',
|
|
806
|
+
'15 BLACK',
|
|
807
|
+
'25 RED',
|
|
808
|
+
'35 RED'
|
|
809
|
+
]);
|
|
810
|
+
expect(tmm.listLevels(node => (node === tmm.NIL ? 'NIL' : `${node.key} ${node.color}`))).toEqual([
|
|
811
|
+
['20 BLACK'],
|
|
812
|
+
['10 RED', '30 BLACK'],
|
|
813
|
+
['5 BLACK', '15 BLACK', '25 RED', '35 RED'],
|
|
814
|
+
['NIL', '8 RED', 'NIL', 'NIL', 'NIL', 'NIL', 'NIL', 'NIL'],
|
|
815
|
+
['NIL', 'NIL']
|
|
816
|
+
]);
|
|
907
817
|
});
|
|
908
818
|
});
|
|
909
819
|
|
|
910
|
-
describe('
|
|
911
|
-
it('
|
|
912
|
-
const
|
|
913
|
-
|
|
914
|
-
|
|
820
|
+
describe('real world data', () => {
|
|
821
|
+
it('cost of living', () => {
|
|
822
|
+
const indexedByRank = new TreeMultiMap(costOfLiving, {
|
|
823
|
+
specifyComparable: node => node.rank,
|
|
824
|
+
toEntryFn: raw => [raw, undefined]
|
|
915
825
|
});
|
|
916
|
-
|
|
917
|
-
expect(
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
[13, 13],
|
|
927
|
-
[16, 16],
|
|
928
|
-
[2, 2],
|
|
929
|
-
[6, 6],
|
|
930
|
-
[9, 9],
|
|
931
|
-
[12, 12],
|
|
932
|
-
[14, 14],
|
|
933
|
-
[4, 4],
|
|
934
|
-
[7, 7],
|
|
935
|
-
[10, 10],
|
|
936
|
-
[5, 5]
|
|
937
|
-
];
|
|
938
|
-
tmm.addMany(idAndValues);
|
|
939
|
-
expect(tmm.root).toBeInstanceOf(TreeMultiMapNode);
|
|
940
|
-
|
|
941
|
-
if (tmm.root) expect(tmm.root.key).toBe(5);
|
|
942
|
-
|
|
943
|
-
expect(tmm.size).toBe(16);
|
|
944
|
-
expect(tmm.count).toBe(18);
|
|
945
|
-
expect(tmm.getComputedCount()).toBe(18);
|
|
946
|
-
|
|
947
|
-
expect(tmm.has(6));
|
|
948
|
-
|
|
949
|
-
expect(tmm.getHeight(6)).toBe(1);
|
|
950
|
-
expect(tmm.getDepth(6)).toBe(3);
|
|
951
|
-
const nodeId10 = tmm.getNode(10);
|
|
952
|
-
expect(nodeId10?.key).toBe(10);
|
|
953
|
-
|
|
954
|
-
const nodeVal9 = tmm.getNode(node => node.key === 9);
|
|
955
|
-
expect(nodeVal9?.key).toBe(9);
|
|
826
|
+
expect(indexedByRank.size).toBe(7);
|
|
827
|
+
expect(indexedByRank.dfs(node => node?.key?.country)).toEqual([
|
|
828
|
+
'Switzerland',
|
|
829
|
+
'New Zealand',
|
|
830
|
+
'Mexico',
|
|
831
|
+
'South Africa',
|
|
832
|
+
'Japan',
|
|
833
|
+
'Brazil',
|
|
834
|
+
'Taiwan'
|
|
835
|
+
]);
|
|
956
836
|
});
|
|
957
837
|
});
|
|
958
838
|
|
|
959
|
-
describe('
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
});
|
|
967
|
-
|
|
968
|
-
it('should clone work well', () => {
|
|
969
|
-
expect(treeMM.count).toBe(21);
|
|
970
|
-
expect(treeMM.getComputedCount()).toBe(21);
|
|
971
|
-
const cloned = treeMM.clone();
|
|
972
|
-
expect(cloned.root?.left?.key).toBe(1);
|
|
973
|
-
expect(cloned.get(cloned.root?.right)).toBe(undefined);
|
|
839
|
+
describe('classic use', () => {
|
|
840
|
+
// Test case for finding elements in a given range
|
|
841
|
+
it('@example Find elements in a range', () => {
|
|
842
|
+
const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
843
|
+
expect(tmm.search(new Range(5, 10))).toEqual([5, 10, 7]);
|
|
844
|
+
expect(tmm.search(new Range(4, 12))).toEqual([5, 10, 12, 7]);
|
|
845
|
+
expect(tmm.search(new Range(15, 20))).toEqual([15, 18]);
|
|
974
846
|
});
|
|
975
847
|
});
|