data-structure-typed 2.0.5 → 2.1.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 +5 -1
- package/COMMANDS.md +27 -0
- package/README.md +5 -34
- package/benchmark/report.html +13 -77
- package/benchmark/report.json +152 -184
- package/dist/cjs/index.cjs +13062 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +12984 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/index.cjs +13091 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +13013 -0
- package/dist/index.js.map +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +219 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +144 -0
- package/dist/types/data-structures/base/linear-base.d.ts +335 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -0
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -0
- package/dist/{esm → types}/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -0
- package/dist/types/data-structures/heap/heap.d.ts +503 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -0
- package/dist/types/data-structures/queue/deque.d.ts +431 -0
- package/dist/types/data-structures/queue/queue.d.ts +308 -0
- package/dist/{cjs → types}/data-structures/stack/stack.d.ts +124 -102
- package/dist/types/data-structures/trie/trie.d.ts +350 -0
- package/dist/types/interfaces/binary-tree.d.ts +59 -0
- package/dist/types/interfaces/graph.d.ts +21 -0
- package/dist/{cjs → types}/types/data-structures/base/base.d.ts +1 -1
- package/dist/{esm → types}/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/{cjs → types}/types/utils/utils.d.ts +1 -0
- package/dist/{esm → types}/utils/utils.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +4693 -6484
- package/dist/umd/data-structure-typed.js.map +1 -0
- package/dist/umd/data-structure-typed.min.js +8 -6
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/jest.integration.config.js +3 -0
- package/package.json +13 -12
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
- package/src/data-structures/binary-tree/avl-tree.ts +237 -206
- package/src/data-structures/binary-tree/binary-tree.ts +665 -896
- package/src/data-structures/binary-tree/bst.ts +565 -572
- package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
- package/src/data-structures/binary-tree/tree-counter.ts +195 -219
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +2 -0
- package/src/utils/utils.ts +9 -14
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/index.html +1 -1
- package/test/performance/benchmark-runner.ts +528 -0
- package/test/performance/data-structures/comparison/comparison.test.ts +27 -57
- package/test/performance/reportor.mjs +43 -43
- package/test/performance/runner-config.json +39 -0
- package/test/performance/single-suite-runner.ts +69 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +350 -90
- package/test/unit/data-structures/binary-tree/bst.test.ts +12 -9
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +25 -24
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +112 -4
- package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/heap/heap.test.ts +14 -21
- package/test/unit/data-structures/heap/max-heap.test.ts +5 -9
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -4
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -7
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +8 -11
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -4
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +1 -4
- package/test/unit/data-structures/queue/queue.test.ts +4 -5
- package/test/unit/utils/utils.test.ts +0 -1
- package/tsconfig-base.json +20 -20
- package/tsconfig-types.json +17 -0
- package/tsconfig.test.json +8 -0
- package/tsup.config.js +11 -22
- package/tsup.node.config.ts +37 -0
- package/dist/cjs/common/index.js +0 -29
- package/dist/cjs/common/index.js.map +0 -1
- package/dist/cjs/data-structures/base/index.js +0 -19
- package/dist/cjs/data-structures/base/index.js.map +0 -1
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +0 -116
- package/dist/cjs/data-structures/base/iterable-element-base.js +0 -202
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +0 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +0 -168
- package/dist/cjs/data-structures/base/iterable-entry-base.js +0 -241
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +0 -1
- package/dist/cjs/data-structures/base/linear-base.d.ts +0 -277
- package/dist/cjs/data-structures/base/linear-base.js +0 -553
- package/dist/cjs/data-structures/base/linear-base.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +0 -214
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +0 -409
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -104
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +0 -203
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -302
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -599
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +0 -295
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +0 -910
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +0 -2197
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +0 -461
- package/dist/cjs/data-structures/binary-tree/bst.js +0 -880
- package/dist/cjs/data-structures/binary-tree/bst.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/index.js +0 -27
- package/dist/cjs/data-structures/binary-tree/index.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +0 -280
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +0 -642
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +0 -298
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +0 -212
- package/dist/cjs/data-structures/binary-tree/tree-counter.js +0 -445
- package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +0 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +0 -267
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +0 -365
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +0 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +0 -335
- package/dist/cjs/data-structures/graph/abstract-graph.js +0 -867
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +0 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +0 -323
- package/dist/cjs/data-structures/graph/directed-graph.js +0 -613
- package/dist/cjs/data-structures/graph/directed-graph.js.map +0 -1
- package/dist/cjs/data-structures/graph/index.js +0 -21
- package/dist/cjs/data-structures/graph/index.js.map +0 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +0 -84
- package/dist/cjs/data-structures/graph/map-graph.js +0 -111
- package/dist/cjs/data-structures/graph/map-graph.js.map +0 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +0 -231
- package/dist/cjs/data-structures/graph/undirected-graph.js +0 -445
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +0 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +0 -519
- package/dist/cjs/data-structures/hash/hash-map.js +0 -880
- package/dist/cjs/data-structures/hash/hash-map.js.map +0 -1
- package/dist/cjs/data-structures/hash/index.js +0 -18
- package/dist/cjs/data-structures/hash/index.js.map +0 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +0 -578
- package/dist/cjs/data-structures/heap/heap.js +0 -911
- package/dist/cjs/data-structures/heap/heap.js.map +0 -1
- package/dist/cjs/data-structures/heap/index.js +0 -20
- package/dist/cjs/data-structures/heap/index.js.map +0 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +0 -68
- package/dist/cjs/data-structures/heap/max-heap.js +0 -96
- package/dist/cjs/data-structures/heap/max-heap.js.map +0 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +0 -68
- package/dist/cjs/data-structures/heap/min-heap.js +0 -87
- package/dist/cjs/data-structures/heap/min-heap.js.map +0 -1
- package/dist/cjs/data-structures/index.js +0 -29
- package/dist/cjs/data-structures/index.js.map +0 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +0 -885
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +0 -1238
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +0 -1
- package/dist/cjs/data-structures/linked-list/index.js +0 -20
- package/dist/cjs/data-structures/linked-list/index.js.map +0 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +0 -500
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +0 -870
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +0 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +0 -134
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +0 -245
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +0 -1
- package/dist/cjs/data-structures/matrix/index.js +0 -19
- package/dist/cjs/data-structures/matrix/index.js.map +0 -1
- package/dist/cjs/data-structures/matrix/matrix.js +0 -449
- package/dist/cjs/data-structures/matrix/matrix.js.map +0 -1
- package/dist/cjs/data-structures/matrix/navigator.js +0 -112
- package/dist/cjs/data-structures/matrix/navigator.js.map +0 -1
- package/dist/cjs/data-structures/priority-queue/index.js +0 -20
- package/dist/cjs/data-structures/priority-queue/index.js.map +0 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +0 -71
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +0 -102
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +0 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +0 -72
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +0 -94
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +0 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +0 -74
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +0 -96
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +0 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +0 -458
- package/dist/cjs/data-structures/queue/deque.js +0 -919
- package/dist/cjs/data-structures/queue/deque.js.map +0 -1
- package/dist/cjs/data-structures/queue/index.js +0 -19
- package/dist/cjs/data-structures/queue/index.js.map +0 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +0 -329
- package/dist/cjs/data-structures/queue/queue.js +0 -457
- package/dist/cjs/data-structures/queue/queue.js.map +0 -1
- package/dist/cjs/data-structures/stack/index.js +0 -18
- package/dist/cjs/data-structures/stack/index.js.map +0 -1
- package/dist/cjs/data-structures/stack/stack.js +0 -346
- package/dist/cjs/data-structures/stack/stack.js.map +0 -1
- package/dist/cjs/data-structures/tree/index.js +0 -18
- package/dist/cjs/data-structures/tree/index.js.map +0 -1
- package/dist/cjs/data-structures/tree/tree.js +0 -108
- package/dist/cjs/data-structures/tree/tree.js.map +0 -1
- package/dist/cjs/data-structures/trie/index.js +0 -18
- package/dist/cjs/data-structures/trie/index.js.map +0 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +0 -351
- package/dist/cjs/data-structures/trie/trie.js +0 -594
- package/dist/cjs/data-structures/trie/trie.js.map +0 -1
- package/dist/cjs/index.js +0 -22
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +0 -9
- package/dist/cjs/interfaces/binary-tree.js +0 -3
- package/dist/cjs/interfaces/binary-tree.js.map +0 -1
- package/dist/cjs/interfaces/doubly-linked-list.js +0 -3
- package/dist/cjs/interfaces/doubly-linked-list.js.map +0 -1
- package/dist/cjs/interfaces/graph.d.ts +0 -5
- package/dist/cjs/interfaces/graph.js +0 -3
- package/dist/cjs/interfaces/graph.js.map +0 -1
- package/dist/cjs/interfaces/heap.js +0 -3
- package/dist/cjs/interfaces/heap.js.map +0 -1
- package/dist/cjs/interfaces/index.js +0 -25
- package/dist/cjs/interfaces/index.js.map +0 -1
- package/dist/cjs/interfaces/navigator.js +0 -3
- package/dist/cjs/interfaces/navigator.js.map +0 -1
- package/dist/cjs/interfaces/priority-queue.js +0 -3
- package/dist/cjs/interfaces/priority-queue.js.map +0 -1
- package/dist/cjs/interfaces/segment-tree.js +0 -3
- package/dist/cjs/interfaces/segment-tree.js.map +0 -1
- package/dist/cjs/interfaces/singly-linked-list.js +0 -3
- package/dist/cjs/interfaces/singly-linked-list.js.map +0 -1
- package/dist/cjs/types/common.js +0 -3
- package/dist/cjs/types/common.js.map +0 -1
- package/dist/cjs/types/data-structures/base/base.js +0 -3
- package/dist/cjs/types/data-structures/base/base.js.map +0 -1
- package/dist/cjs/types/data-structures/base/index.js +0 -18
- package/dist/cjs/types/data-structures/base/index.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.js +0 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.js +0 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-indexed-tree.js +0 -3
- package/dist/cjs/types/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -3
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.js +0 -3
- package/dist/cjs/types/data-structures/binary-tree/bst.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/index.js +0 -26
- package/dist/cjs/types/data-structures/binary-tree/index.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/red-black-tree.js +0 -3
- package/dist/cjs/types/data-structures/binary-tree/red-black-tree.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/segment-tree.js +0 -3
- package/dist/cjs/types/data-structures/binary-tree/segment-tree.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-counter.js +0 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-counter.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js +0 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js.map +0 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +0 -10
- package/dist/cjs/types/data-structures/graph/abstract-graph.js +0 -3
- package/dist/cjs/types/data-structures/graph/abstract-graph.js.map +0 -1
- package/dist/cjs/types/data-structures/graph/directed-graph.js +0 -3
- package/dist/cjs/types/data-structures/graph/directed-graph.js.map +0 -1
- package/dist/cjs/types/data-structures/graph/index.js +0 -20
- package/dist/cjs/types/data-structures/graph/index.js.map +0 -1
- package/dist/cjs/types/data-structures/graph/map-graph.js +0 -3
- package/dist/cjs/types/data-structures/graph/map-graph.js.map +0 -1
- package/dist/cjs/types/data-structures/graph/undirected-graph.js +0 -3
- package/dist/cjs/types/data-structures/graph/undirected-graph.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/hash-map.js +0 -3
- package/dist/cjs/types/data-structures/hash/hash-map.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/index.js +0 -18
- package/dist/cjs/types/data-structures/hash/index.js.map +0 -1
- package/dist/cjs/types/data-structures/heap/heap.js +0 -3
- package/dist/cjs/types/data-structures/heap/heap.js.map +0 -1
- package/dist/cjs/types/data-structures/heap/index.js +0 -18
- package/dist/cjs/types/data-structures/heap/index.js.map +0 -1
- package/dist/cjs/types/data-structures/heap/max-heap.js +0 -3
- package/dist/cjs/types/data-structures/heap/max-heap.js.map +0 -1
- package/dist/cjs/types/data-structures/heap/min-heap.js +0 -3
- package/dist/cjs/types/data-structures/heap/min-heap.js.map +0 -1
- package/dist/cjs/types/data-structures/index.js +0 -29
- package/dist/cjs/types/data-structures/index.js.map +0 -1
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.js +0 -3
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.js.map +0 -1
- package/dist/cjs/types/data-structures/linked-list/index.js +0 -20
- package/dist/cjs/types/data-structures/linked-list/index.js.map +0 -1
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.js +0 -3
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.js.map +0 -1
- package/dist/cjs/types/data-structures/linked-list/skip-linked-list.js +0 -3
- package/dist/cjs/types/data-structures/linked-list/skip-linked-list.js.map +0 -1
- package/dist/cjs/types/data-structures/matrix/index.js +0 -19
- package/dist/cjs/types/data-structures/matrix/index.js.map +0 -1
- package/dist/cjs/types/data-structures/matrix/matrix.js +0 -3
- package/dist/cjs/types/data-structures/matrix/matrix.js.map +0 -1
- package/dist/cjs/types/data-structures/matrix/navigator.js +0 -3
- package/dist/cjs/types/data-structures/matrix/navigator.js.map +0 -1
- package/dist/cjs/types/data-structures/priority-queue/index.js +0 -20
- package/dist/cjs/types/data-structures/priority-queue/index.js.map +0 -1
- package/dist/cjs/types/data-structures/priority-queue/max-priority-queue.js +0 -3
- package/dist/cjs/types/data-structures/priority-queue/max-priority-queue.js.map +0 -1
- package/dist/cjs/types/data-structures/priority-queue/min-priority-queue.js +0 -3
- package/dist/cjs/types/data-structures/priority-queue/min-priority-queue.js.map +0 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.js +0 -3
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.js.map +0 -1
- package/dist/cjs/types/data-structures/queue/deque.js +0 -3
- package/dist/cjs/types/data-structures/queue/deque.js.map +0 -1
- package/dist/cjs/types/data-structures/queue/index.js +0 -19
- package/dist/cjs/types/data-structures/queue/index.js.map +0 -1
- package/dist/cjs/types/data-structures/queue/queue.js +0 -3
- package/dist/cjs/types/data-structures/queue/queue.js.map +0 -1
- package/dist/cjs/types/data-structures/stack/index.js +0 -18
- package/dist/cjs/types/data-structures/stack/index.js.map +0 -1
- package/dist/cjs/types/data-structures/stack/stack.js +0 -3
- package/dist/cjs/types/data-structures/stack/stack.js.map +0 -1
- package/dist/cjs/types/data-structures/tree/index.js +0 -18
- package/dist/cjs/types/data-structures/tree/index.js.map +0 -1
- package/dist/cjs/types/data-structures/tree/tree.js +0 -3
- package/dist/cjs/types/data-structures/tree/tree.js.map +0 -1
- package/dist/cjs/types/data-structures/trie/index.js +0 -18
- package/dist/cjs/types/data-structures/trie/index.js.map +0 -1
- package/dist/cjs/types/data-structures/trie/trie.js +0 -3
- package/dist/cjs/types/data-structures/trie/trie.js.map +0 -1
- package/dist/cjs/types/index.js +0 -20
- package/dist/cjs/types/index.js.map +0 -1
- package/dist/cjs/types/utils/index.js +0 -19
- package/dist/cjs/types/utils/index.js.map +0 -1
- package/dist/cjs/types/utils/utils.js +0 -3
- package/dist/cjs/types/utils/utils.js.map +0 -1
- package/dist/cjs/types/utils/validate-type.js +0 -3
- package/dist/cjs/types/utils/validate-type.js.map +0 -1
- package/dist/cjs/utils/index.js +0 -19
- package/dist/cjs/utils/index.js.map +0 -1
- package/dist/cjs/utils/number.js +0 -24
- package/dist/cjs/utils/number.js.map +0 -1
- package/dist/cjs/utils/utils.d.ts +0 -209
- package/dist/cjs/utils/utils.js +0 -353
- package/dist/cjs/utils/utils.js.map +0 -1
- package/dist/esm/common/index.d.ts +0 -12
- package/dist/esm/common/index.js +0 -29
- package/dist/esm/common/index.js.map +0 -1
- package/dist/esm/data-structures/base/index.d.ts +0 -2
- package/dist/esm/data-structures/base/index.js +0 -3
- package/dist/esm/data-structures/base/index.js.map +0 -1
- package/dist/esm/data-structures/base/iterable-element-base.d.ts +0 -116
- package/dist/esm/data-structures/base/iterable-element-base.js +0 -199
- package/dist/esm/data-structures/base/iterable-element-base.js.map +0 -1
- package/dist/esm/data-structures/base/iterable-entry-base.d.ts +0 -168
- package/dist/esm/data-structures/base/iterable-entry-base.js +0 -237
- package/dist/esm/data-structures/base/iterable-entry-base.js.map +0 -1
- package/dist/esm/data-structures/base/linear-base.d.ts +0 -277
- package/dist/esm/data-structures/base/linear-base.js +0 -549
- package/dist/esm/data-structures/base/linear-base.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +0 -214
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +0 -410
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -104
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +0 -205
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +0 -302
- package/dist/esm/data-structures/binary-tree/avl-tree.js +0 -601
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/esm/data-structures/binary-tree/binary-indexed-tree.js +0 -296
- package/dist/esm/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +0 -910
- package/dist/esm/data-structures/binary-tree/binary-tree.js +0 -2198
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +0 -461
- package/dist/esm/data-structures/binary-tree/bst.js +0 -881
- package/dist/esm/data-structures/binary-tree/bst.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/index.d.ts +0 -10
- package/dist/esm/data-structures/binary-tree/index.js +0 -11
- package/dist/esm/data-structures/binary-tree/index.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +0 -280
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +0 -641
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/esm/data-structures/binary-tree/segment-tree.js +0 -295
- package/dist/esm/data-structures/binary-tree/segment-tree.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +0 -212
- package/dist/esm/data-structures/binary-tree/tree-counter.js +0 -446
- package/dist/esm/data-structures/binary-tree/tree-counter.js.map +0 -1
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +0 -367
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +0 -1
- package/dist/esm/data-structures/graph/abstract-graph.d.ts +0 -335
- package/dist/esm/data-structures/graph/abstract-graph.js +0 -862
- package/dist/esm/data-structures/graph/abstract-graph.js.map +0 -1
- package/dist/esm/data-structures/graph/directed-graph.d.ts +0 -323
- package/dist/esm/data-structures/graph/directed-graph.js +0 -609
- package/dist/esm/data-structures/graph/directed-graph.js.map +0 -1
- package/dist/esm/data-structures/graph/index.d.ts +0 -4
- package/dist/esm/data-structures/graph/index.js +0 -5
- package/dist/esm/data-structures/graph/index.js.map +0 -1
- package/dist/esm/data-structures/graph/map-graph.d.ts +0 -84
- package/dist/esm/data-structures/graph/map-graph.js +0 -108
- package/dist/esm/data-structures/graph/map-graph.js.map +0 -1
- package/dist/esm/data-structures/graph/undirected-graph.d.ts +0 -231
- package/dist/esm/data-structures/graph/undirected-graph.js +0 -439
- package/dist/esm/data-structures/graph/undirected-graph.js.map +0 -1
- package/dist/esm/data-structures/hash/hash-map.d.ts +0 -519
- package/dist/esm/data-structures/hash/hash-map.js +0 -878
- package/dist/esm/data-structures/hash/hash-map.js.map +0 -1
- package/dist/esm/data-structures/hash/index.d.ts +0 -1
- package/dist/esm/data-structures/hash/index.js +0 -2
- package/dist/esm/data-structures/hash/index.js.map +0 -1
- package/dist/esm/data-structures/heap/heap.d.ts +0 -578
- package/dist/esm/data-structures/heap/heap.js +0 -914
- package/dist/esm/data-structures/heap/heap.js.map +0 -1
- package/dist/esm/data-structures/heap/index.d.ts +0 -3
- package/dist/esm/data-structures/heap/index.js +0 -4
- package/dist/esm/data-structures/heap/index.js.map +0 -1
- package/dist/esm/data-structures/heap/max-heap.d.ts +0 -68
- package/dist/esm/data-structures/heap/max-heap.js +0 -95
- package/dist/esm/data-structures/heap/max-heap.js.map +0 -1
- package/dist/esm/data-structures/heap/min-heap.d.ts +0 -68
- package/dist/esm/data-structures/heap/min-heap.js +0 -83
- package/dist/esm/data-structures/heap/min-heap.js.map +0 -1
- package/dist/esm/data-structures/index.d.ts +0 -12
- package/dist/esm/data-structures/index.js +0 -13
- package/dist/esm/data-structures/index.js.map +0 -1
- package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +0 -885
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js +0 -1236
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +0 -1
- package/dist/esm/data-structures/linked-list/index.d.ts +0 -3
- package/dist/esm/data-structures/linked-list/index.js +0 -4
- package/dist/esm/data-structures/linked-list/index.js.map +0 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +0 -500
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +0 -866
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +0 -1
- package/dist/esm/data-structures/linked-list/skip-linked-list.d.ts +0 -134
- package/dist/esm/data-structures/linked-list/skip-linked-list.js +0 -243
- package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +0 -1
- package/dist/esm/data-structures/matrix/index.d.ts +0 -2
- package/dist/esm/data-structures/matrix/index.js +0 -3
- package/dist/esm/data-structures/matrix/index.js.map +0 -1
- package/dist/esm/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/esm/data-structures/matrix/matrix.js +0 -444
- package/dist/esm/data-structures/matrix/matrix.js.map +0 -1
- package/dist/esm/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/esm/data-structures/matrix/navigator.js +0 -114
- package/dist/esm/data-structures/matrix/navigator.js.map +0 -1
- package/dist/esm/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/esm/data-structures/priority-queue/index.js +0 -4
- package/dist/esm/data-structures/priority-queue/index.js.map +0 -1
- package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +0 -71
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js +0 -101
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +0 -1
- package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +0 -72
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js +0 -90
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +0 -1
- package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +0 -74
- package/dist/esm/data-structures/priority-queue/priority-queue.js +0 -92
- package/dist/esm/data-structures/priority-queue/priority-queue.js.map +0 -1
- package/dist/esm/data-structures/queue/deque.d.ts +0 -458
- package/dist/esm/data-structures/queue/deque.js +0 -915
- package/dist/esm/data-structures/queue/deque.js.map +0 -1
- package/dist/esm/data-structures/queue/index.d.ts +0 -2
- package/dist/esm/data-structures/queue/index.js +0 -3
- package/dist/esm/data-structures/queue/index.js.map +0 -1
- package/dist/esm/data-structures/queue/queue.d.ts +0 -329
- package/dist/esm/data-structures/queue/queue.js +0 -452
- package/dist/esm/data-structures/queue/queue.js.map +0 -1
- package/dist/esm/data-structures/stack/index.d.ts +0 -1
- package/dist/esm/data-structures/stack/index.js +0 -2
- package/dist/esm/data-structures/stack/index.js.map +0 -1
- package/dist/esm/data-structures/stack/stack.d.ts +0 -284
- package/dist/esm/data-structures/stack/stack.js +0 -342
- package/dist/esm/data-structures/stack/stack.js.map +0 -1
- package/dist/esm/data-structures/tree/index.d.ts +0 -1
- package/dist/esm/data-structures/tree/index.js +0 -2
- package/dist/esm/data-structures/tree/index.js.map +0 -1
- package/dist/esm/data-structures/tree/tree.d.ts +0 -62
- package/dist/esm/data-structures/tree/tree.js +0 -107
- package/dist/esm/data-structures/tree/tree.js.map +0 -1
- package/dist/esm/data-structures/trie/index.d.ts +0 -1
- package/dist/esm/data-structures/trie/index.js +0 -2
- package/dist/esm/data-structures/trie/index.js.map +0 -1
- package/dist/esm/data-structures/trie/trie.d.ts +0 -351
- package/dist/esm/data-structures/trie/trie.js +0 -592
- package/dist/esm/data-structures/trie/trie.js.map +0 -1
- package/dist/esm/index.d.ts +0 -5
- package/dist/esm/index.js +0 -6
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/interfaces/binary-tree.d.ts +0 -9
- package/dist/esm/interfaces/binary-tree.js +0 -2
- package/dist/esm/interfaces/binary-tree.js.map +0 -1
- package/dist/esm/interfaces/doubly-linked-list.d.ts +0 -1
- package/dist/esm/interfaces/doubly-linked-list.js +0 -2
- package/dist/esm/interfaces/doubly-linked-list.js.map +0 -1
- package/dist/esm/interfaces/graph.d.ts +0 -5
- package/dist/esm/interfaces/graph.js +0 -2
- package/dist/esm/interfaces/graph.js.map +0 -1
- package/dist/esm/interfaces/heap.d.ts +0 -1
- package/dist/esm/interfaces/heap.js +0 -2
- package/dist/esm/interfaces/heap.js.map +0 -1
- package/dist/esm/interfaces/index.d.ts +0 -8
- package/dist/esm/interfaces/index.js +0 -9
- package/dist/esm/interfaces/index.js.map +0 -1
- package/dist/esm/interfaces/navigator.d.ts +0 -1
- package/dist/esm/interfaces/navigator.js +0 -2
- package/dist/esm/interfaces/navigator.js.map +0 -1
- package/dist/esm/interfaces/priority-queue.d.ts +0 -1
- package/dist/esm/interfaces/priority-queue.js +0 -2
- package/dist/esm/interfaces/priority-queue.js.map +0 -1
- package/dist/esm/interfaces/segment-tree.d.ts +0 -1
- package/dist/esm/interfaces/segment-tree.js +0 -2
- package/dist/esm/interfaces/segment-tree.js.map +0 -1
- package/dist/esm/interfaces/singly-linked-list.d.ts +0 -1
- package/dist/esm/interfaces/singly-linked-list.js +0 -2
- package/dist/esm/interfaces/singly-linked-list.js.map +0 -1
- package/dist/esm/types/common.d.ts +0 -15
- package/dist/esm/types/common.js +0 -2
- package/dist/esm/types/common.js.map +0 -1
- package/dist/esm/types/data-structures/base/base.d.ts +0 -13
- package/dist/esm/types/data-structures/base/base.js +0 -2
- package/dist/esm/types/data-structures/base/base.js.map +0 -1
- package/dist/esm/types/data-structures/base/index.d.ts +0 -1
- package/dist/esm/types/data-structures/base/index.js +0 -2
- package/dist/esm/types/data-structures/base/index.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
- package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/esm/types/data-structures/binary-tree/avl-tree-counter.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
- package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
- package/dist/esm/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/esm/types/data-structures/binary-tree/avl-tree.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -1
- package/dist/esm/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/esm/types/data-structures/binary-tree/binary-indexed-tree.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/binary-tree.d.ts +0 -29
- package/dist/esm/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/esm/types/data-structures/binary-tree/binary-tree.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/bst.d.ts +0 -12
- package/dist/esm/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/esm/types/data-structures/binary-tree/bst.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/index.d.ts +0 -9
- package/dist/esm/types/data-structures/binary-tree/index.js +0 -10
- package/dist/esm/types/data-structures/binary-tree/index.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/red-black-tree.d.ts +0 -3
- package/dist/esm/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/esm/types/data-structures/binary-tree/red-black-tree.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/segment-tree.d.ts +0 -1
- package/dist/esm/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/esm/types/data-structures/binary-tree/segment-tree.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
- package/dist/esm/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/esm/types/data-structures/binary-tree/tree-counter.js.map +0 -1
- package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +0 -2
- package/dist/esm/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/esm/types/data-structures/binary-tree/tree-multi-map.js.map +0 -1
- package/dist/esm/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/esm/types/data-structures/graph/abstract-graph.js.map +0 -1
- package/dist/esm/types/data-structures/graph/directed-graph.d.ts +0 -1
- package/dist/esm/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/esm/types/data-structures/graph/directed-graph.js.map +0 -1
- package/dist/esm/types/data-structures/graph/index.d.ts +0 -3
- package/dist/esm/types/data-structures/graph/index.js +0 -4
- package/dist/esm/types/data-structures/graph/index.js.map +0 -1
- package/dist/esm/types/data-structures/graph/map-graph.d.ts +0 -1
- package/dist/esm/types/data-structures/graph/map-graph.js +0 -2
- package/dist/esm/types/data-structures/graph/map-graph.js.map +0 -1
- package/dist/esm/types/data-structures/graph/undirected-graph.d.ts +0 -1
- package/dist/esm/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/esm/types/data-structures/graph/undirected-graph.js.map +0 -1
- package/dist/esm/types/data-structures/hash/hash-map.d.ts +0 -19
- package/dist/esm/types/data-structures/hash/hash-map.js +0 -2
- package/dist/esm/types/data-structures/hash/hash-map.js.map +0 -1
- package/dist/esm/types/data-structures/hash/index.d.ts +0 -2
- package/dist/esm/types/data-structures/hash/index.js +0 -2
- package/dist/esm/types/data-structures/hash/index.js.map +0 -1
- package/dist/esm/types/data-structures/heap/heap.d.ts +0 -5
- package/dist/esm/types/data-structures/heap/heap.js +0 -2
- package/dist/esm/types/data-structures/heap/heap.js.map +0 -1
- package/dist/esm/types/data-structures/heap/index.d.ts +0 -1
- package/dist/esm/types/data-structures/heap/index.js +0 -2
- package/dist/esm/types/data-structures/heap/index.js.map +0 -1
- package/dist/esm/types/data-structures/heap/max-heap.d.ts +0 -1
- package/dist/esm/types/data-structures/heap/max-heap.js +0 -2
- package/dist/esm/types/data-structures/heap/max-heap.js.map +0 -1
- package/dist/esm/types/data-structures/heap/min-heap.d.ts +0 -1
- package/dist/esm/types/data-structures/heap/min-heap.js +0 -2
- package/dist/esm/types/data-structures/heap/min-heap.js.map +0 -1
- package/dist/esm/types/data-structures/index.d.ts +0 -12
- package/dist/esm/types/data-structures/index.js +0 -13
- package/dist/esm/types/data-structures/index.js.map +0 -1
- package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -2
- package/dist/esm/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/esm/types/data-structures/linked-list/doubly-linked-list.js.map +0 -1
- package/dist/esm/types/data-structures/linked-list/index.d.ts +0 -3
- package/dist/esm/types/data-structures/linked-list/index.js +0 -4
- package/dist/esm/types/data-structures/linked-list/index.js.map +0 -1
- package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +0 -2
- package/dist/esm/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/esm/types/data-structures/linked-list/singly-linked-list.js.map +0 -1
- package/dist/esm/types/data-structures/linked-list/skip-linked-list.d.ts +0 -4
- package/dist/esm/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/esm/types/data-structures/linked-list/skip-linked-list.js.map +0 -1
- package/dist/esm/types/data-structures/matrix/index.d.ts +0 -2
- package/dist/esm/types/data-structures/matrix/index.js +0 -3
- package/dist/esm/types/data-structures/matrix/index.js.map +0 -1
- package/dist/esm/types/data-structures/matrix/matrix.d.ts +0 -7
- package/dist/esm/types/data-structures/matrix/matrix.js +0 -2
- package/dist/esm/types/data-structures/matrix/matrix.js.map +0 -1
- package/dist/esm/types/data-structures/matrix/navigator.d.ts +0 -14
- package/dist/esm/types/data-structures/matrix/navigator.js +0 -2
- package/dist/esm/types/data-structures/matrix/navigator.js.map +0 -1
- package/dist/esm/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/esm/types/data-structures/priority-queue/index.js +0 -4
- package/dist/esm/types/data-structures/priority-queue/index.js.map +0 -1
- package/dist/esm/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -1
- package/dist/esm/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/esm/types/data-structures/priority-queue/max-priority-queue.js.map +0 -1
- package/dist/esm/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -1
- package/dist/esm/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/esm/types/data-structures/priority-queue/min-priority-queue.js.map +0 -1
- package/dist/esm/types/data-structures/priority-queue/priority-queue.d.ts +0 -2
- package/dist/esm/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/esm/types/data-structures/priority-queue/priority-queue.js.map +0 -1
- package/dist/esm/types/data-structures/queue/deque.d.ts +0 -4
- package/dist/esm/types/data-structures/queue/deque.js +0 -2
- package/dist/esm/types/data-structures/queue/deque.js.map +0 -1
- package/dist/esm/types/data-structures/queue/index.d.ts +0 -2
- package/dist/esm/types/data-structures/queue/index.js +0 -3
- package/dist/esm/types/data-structures/queue/index.js.map +0 -1
- package/dist/esm/types/data-structures/queue/queue.d.ts +0 -4
- package/dist/esm/types/data-structures/queue/queue.js +0 -2
- package/dist/esm/types/data-structures/queue/queue.js.map +0 -1
- package/dist/esm/types/data-structures/stack/index.d.ts +0 -1
- package/dist/esm/types/data-structures/stack/index.js +0 -2
- package/dist/esm/types/data-structures/stack/index.js.map +0 -1
- package/dist/esm/types/data-structures/stack/stack.d.ts +0 -2
- package/dist/esm/types/data-structures/stack/stack.js +0 -2
- package/dist/esm/types/data-structures/stack/stack.js.map +0 -1
- package/dist/esm/types/data-structures/tree/index.d.ts +0 -1
- package/dist/esm/types/data-structures/tree/index.js +0 -2
- package/dist/esm/types/data-structures/tree/index.js.map +0 -1
- package/dist/esm/types/data-structures/tree/tree.d.ts +0 -1
- package/dist/esm/types/data-structures/tree/tree.js +0 -2
- package/dist/esm/types/data-structures/tree/tree.js.map +0 -1
- package/dist/esm/types/data-structures/trie/index.d.ts +0 -1
- package/dist/esm/types/data-structures/trie/index.js +0 -2
- package/dist/esm/types/data-structures/trie/index.js.map +0 -1
- package/dist/esm/types/data-structures/trie/trie.d.ts +0 -4
- package/dist/esm/types/data-structures/trie/trie.js +0 -2
- package/dist/esm/types/data-structures/trie/trie.js.map +0 -1
- package/dist/esm/types/index.d.ts +0 -3
- package/dist/esm/types/index.js +0 -4
- package/dist/esm/types/index.js.map +0 -1
- package/dist/esm/types/utils/index.d.ts +0 -2
- package/dist/esm/types/utils/index.js +0 -3
- package/dist/esm/types/utils/index.js.map +0 -1
- package/dist/esm/types/utils/utils.d.ts +0 -21
- package/dist/esm/types/utils/utils.js +0 -2
- package/dist/esm/types/utils/utils.js.map +0 -1
- package/dist/esm/types/utils/validate-type.d.ts +0 -19
- package/dist/esm/types/utils/validate-type.js +0 -2
- package/dist/esm/types/utils/validate-type.js.map +0 -1
- package/dist/esm/utils/index.d.ts +0 -2
- package/dist/esm/utils/index.js +0 -3
- package/dist/esm/utils/index.js.map +0 -1
- package/dist/esm/utils/number.d.ts +0 -14
- package/dist/esm/utils/number.js +0 -21
- package/dist/esm/utils/number.js.map +0 -1
- package/dist/esm/utils/utils.js +0 -324
- package/dist/esm/utils/utils.js.map +0 -1
- package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +0 -71
- package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +0 -81
- package/tsconfig-cjs.json +0 -14
- package/tsconfig-esm.json +0 -14
- /package/dist/{cjs → types}/common/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/base/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/binary-tree/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/binary-tree/segment-tree.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/graph/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/hash/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/heap/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/linked-list/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/matrix/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/matrix/matrix.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/matrix/navigator.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/priority-queue/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/queue/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/stack/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/tree/index.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/tree/tree.d.ts +0 -0
- /package/dist/{cjs → types}/data-structures/trie/index.d.ts +0 -0
- /package/dist/{cjs → types}/index.d.ts +0 -0
- /package/dist/{cjs → types}/interfaces/doubly-linked-list.d.ts +0 -0
- /package/dist/{cjs → types}/interfaces/heap.d.ts +0 -0
- /package/dist/{cjs → types}/interfaces/index.d.ts +0 -0
- /package/dist/{cjs → types}/interfaces/navigator.d.ts +0 -0
- /package/dist/{cjs → types}/interfaces/priority-queue.d.ts +0 -0
- /package/dist/{cjs → types}/interfaces/segment-tree.d.ts +0 -0
- /package/dist/{cjs → types}/interfaces/singly-linked-list.d.ts +0 -0
- /package/dist/{cjs → types}/types/common.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/base/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/avl-tree.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/binary-tree.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/bst.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/red-black-tree.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/segment-tree.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/tree-counter.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/binary-tree/tree-multi-map.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/graph/directed-graph.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/graph/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/graph/map-graph.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/graph/undirected-graph.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/hash/hash-map.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/hash/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/heap/heap.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/heap/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/heap/max-heap.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/heap/min-heap.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/linked-list/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/linked-list/singly-linked-list.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/linked-list/skip-linked-list.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/matrix/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/matrix/matrix.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/matrix/navigator.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/priority-queue/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/priority-queue/priority-queue.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/queue/deque.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/queue/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/queue/queue.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/stack/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/stack/stack.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/tree/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/tree/tree.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/trie/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/data-structures/trie/trie.d.ts +0 -0
- /package/dist/{cjs → types}/types/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/utils/index.d.ts +0 -0
- /package/dist/{cjs → types}/types/utils/validate-type.d.ts +0 -0
- /package/dist/{cjs → types}/utils/index.d.ts +0 -0
- /package/dist/{cjs → types}/utils/number.d.ts +0 -0
|
@@ -1,2197 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Pablo Zeng
|
|
6
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.BinaryTree = exports.BinaryTreeNode = void 0;
|
|
11
|
-
const utils_1 = require("../../utils");
|
|
12
|
-
const queue_1 = require("../queue");
|
|
13
|
-
const base_1 = require("../base");
|
|
14
|
-
const common_1 = require("../../common");
|
|
15
|
-
/**
|
|
16
|
-
* Represents a node in a binary tree.
|
|
17
|
-
* @template V - The type of data stored in the node.
|
|
18
|
-
* @template BinaryTreeNode<K, V> - The type of the family relationship in the binary tree.
|
|
19
|
-
*/
|
|
20
|
-
class BinaryTreeNode {
|
|
21
|
-
/**
|
|
22
|
-
* The constructor function initializes an object with a key and an optional value in TypeScript.
|
|
23
|
-
* @param {K} key - The `key` parameter in the constructor function is used to store the key value
|
|
24
|
-
* for the key-value pair.
|
|
25
|
-
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
|
|
26
|
-
* have to be provided when creating an instance of the class. If a `value` is not provided, it will
|
|
27
|
-
* default to `undefined`.
|
|
28
|
-
*/
|
|
29
|
-
constructor(key, value) {
|
|
30
|
-
this.parent = undefined;
|
|
31
|
-
this._left = undefined;
|
|
32
|
-
this._right = undefined;
|
|
33
|
-
this._height = 0;
|
|
34
|
-
this._color = 'BLACK';
|
|
35
|
-
this._count = 1;
|
|
36
|
-
this.key = key;
|
|
37
|
-
this.value = value;
|
|
38
|
-
}
|
|
39
|
-
get left() {
|
|
40
|
-
return this._left;
|
|
41
|
-
}
|
|
42
|
-
set left(v) {
|
|
43
|
-
if (v) {
|
|
44
|
-
v.parent = this;
|
|
45
|
-
}
|
|
46
|
-
this._left = v;
|
|
47
|
-
}
|
|
48
|
-
get right() {
|
|
49
|
-
return this._right;
|
|
50
|
-
}
|
|
51
|
-
set right(v) {
|
|
52
|
-
if (v) {
|
|
53
|
-
v.parent = this;
|
|
54
|
-
}
|
|
55
|
-
this._right = v;
|
|
56
|
-
}
|
|
57
|
-
get height() {
|
|
58
|
-
return this._height;
|
|
59
|
-
}
|
|
60
|
-
set height(value) {
|
|
61
|
-
this._height = value;
|
|
62
|
-
}
|
|
63
|
-
get color() {
|
|
64
|
-
return this._color;
|
|
65
|
-
}
|
|
66
|
-
set color(value) {
|
|
67
|
-
this._color = value;
|
|
68
|
-
}
|
|
69
|
-
get count() {
|
|
70
|
-
return this._count;
|
|
71
|
-
}
|
|
72
|
-
set count(value) {
|
|
73
|
-
this._count = value;
|
|
74
|
-
}
|
|
75
|
-
get familyPosition() {
|
|
76
|
-
if (!this.parent) {
|
|
77
|
-
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
78
|
-
}
|
|
79
|
-
if (this.parent.left === this) {
|
|
80
|
-
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
81
|
-
}
|
|
82
|
-
else if (this.parent.right === this) {
|
|
83
|
-
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
84
|
-
}
|
|
85
|
-
return 'MAL_NODE';
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
exports.BinaryTreeNode = BinaryTreeNode;
|
|
89
|
-
/**
|
|
90
|
-
* 1. Two Children Maximum: Each node has at most two children.
|
|
91
|
-
* 2. Left and Right Children: Nodes have distinct left and right children.
|
|
92
|
-
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
93
|
-
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
94
|
-
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
95
|
-
* @example
|
|
96
|
-
* // determine loan approval using a decision tree
|
|
97
|
-
* // Decision tree structure
|
|
98
|
-
* const loanDecisionTree = new BinaryTree<string>(
|
|
99
|
-
* ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
|
|
100
|
-
* { isDuplicate: true }
|
|
101
|
-
* );
|
|
102
|
-
*
|
|
103
|
-
* function determineLoanApproval(
|
|
104
|
-
* node?: BinaryTreeNode<string> | null,
|
|
105
|
-
* conditions?: { [key: string]: boolean }
|
|
106
|
-
* ): string {
|
|
107
|
-
* if (!node) throw new Error('Invalid node');
|
|
108
|
-
*
|
|
109
|
-
* // If it's a leaf node, return the decision result
|
|
110
|
-
* if (!node.left && !node.right) return node.key;
|
|
111
|
-
*
|
|
112
|
-
* // Check if a valid condition exists for the current node's key
|
|
113
|
-
* return conditions?.[node.key]
|
|
114
|
-
* ? determineLoanApproval(node.left, conditions)
|
|
115
|
-
* : determineLoanApproval(node.right, conditions);
|
|
116
|
-
* }
|
|
117
|
-
*
|
|
118
|
-
* // Test case 1: Stable income and good credit score
|
|
119
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved'
|
|
120
|
-
*
|
|
121
|
-
* // Test case 2: Stable income but poor credit score
|
|
122
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected'
|
|
123
|
-
*
|
|
124
|
-
* // Test case 3: No stable income
|
|
125
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected'
|
|
126
|
-
*
|
|
127
|
-
* // Test case 4: No stable income and poor credit score
|
|
128
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected'
|
|
129
|
-
* @example
|
|
130
|
-
* // evaluate the arithmetic expression represented by the binary tree
|
|
131
|
-
* const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
|
|
132
|
-
*
|
|
133
|
-
* function evaluate(node?: BinaryTreeNode<number | string> | null): number {
|
|
134
|
-
* if (!node) return 0;
|
|
135
|
-
*
|
|
136
|
-
* if (typeof node.key === 'number') return node.key;
|
|
137
|
-
*
|
|
138
|
-
* const leftValue = evaluate(node.left); // Evaluate the left subtree
|
|
139
|
-
* const rightValue = evaluate(node.right); // Evaluate the right subtree
|
|
140
|
-
*
|
|
141
|
-
* // Perform the operation based on the current node's operator
|
|
142
|
-
* switch (node.key) {
|
|
143
|
-
* case '+':
|
|
144
|
-
* return leftValue + rightValue;
|
|
145
|
-
* case '-':
|
|
146
|
-
* return leftValue - rightValue;
|
|
147
|
-
* case '*':
|
|
148
|
-
* return leftValue * rightValue;
|
|
149
|
-
* case '/':
|
|
150
|
-
* return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero
|
|
151
|
-
* default:
|
|
152
|
-
* throw new Error(`Unsupported operator: ${node.key}`);
|
|
153
|
-
* }
|
|
154
|
-
* }
|
|
155
|
-
*
|
|
156
|
-
* console.log(evaluate(expressionTree.root)); // -27
|
|
157
|
-
*/
|
|
158
|
-
class BinaryTree extends base_1.IterableEntryBase {
|
|
159
|
-
/**
|
|
160
|
-
* This TypeScript constructor function initializes a binary tree with optional options and adds
|
|
161
|
-
* elements based on the provided input.
|
|
162
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
163
|
-
* iterable that can contain either objects of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
|
|
164
|
-
* is used to initialize the binary tree with keys, nodes, entries, or raw data.
|
|
165
|
-
* @param [options] - The `options` parameter in the constructor is an optional object that can
|
|
166
|
-
* contain the following properties:
|
|
167
|
-
*/
|
|
168
|
-
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
169
|
-
super();
|
|
170
|
-
this.iterationType = 'ITERATIVE';
|
|
171
|
-
this._isMapMode = true;
|
|
172
|
-
this._isDuplicate = false;
|
|
173
|
-
this._store = new Map();
|
|
174
|
-
this._size = 0;
|
|
175
|
-
this._NIL = new BinaryTreeNode(NaN);
|
|
176
|
-
this._DEFAULT_NODE_CALLBACK = (node) => (node ? node.key : undefined);
|
|
177
|
-
if (options) {
|
|
178
|
-
const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
|
|
179
|
-
if (iterationType)
|
|
180
|
-
this.iterationType = iterationType;
|
|
181
|
-
if (isMapMode !== undefined)
|
|
182
|
-
this._isMapMode = isMapMode;
|
|
183
|
-
if (isDuplicate !== undefined)
|
|
184
|
-
this._isDuplicate = isDuplicate;
|
|
185
|
-
if (typeof toEntryFn === 'function')
|
|
186
|
-
this._toEntryFn = toEntryFn;
|
|
187
|
-
else if (toEntryFn)
|
|
188
|
-
throw TypeError('toEntryFn must be a function type');
|
|
189
|
-
}
|
|
190
|
-
if (keysNodesEntriesOrRaws)
|
|
191
|
-
this.addMany(keysNodesEntriesOrRaws);
|
|
192
|
-
}
|
|
193
|
-
get isMapMode() {
|
|
194
|
-
return this._isMapMode;
|
|
195
|
-
}
|
|
196
|
-
get isDuplicate() {
|
|
197
|
-
return this._isDuplicate;
|
|
198
|
-
}
|
|
199
|
-
get store() {
|
|
200
|
-
return this._store;
|
|
201
|
-
}
|
|
202
|
-
get root() {
|
|
203
|
-
return this._root;
|
|
204
|
-
}
|
|
205
|
-
get size() {
|
|
206
|
-
return this._size;
|
|
207
|
-
}
|
|
208
|
-
get NIL() {
|
|
209
|
-
return this._NIL;
|
|
210
|
-
}
|
|
211
|
-
get toEntryFn() {
|
|
212
|
-
return this._toEntryFn;
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Time Complexity: O(1)
|
|
216
|
-
* Space Complexity: O(1)
|
|
217
|
-
*
|
|
218
|
-
* The function creates a new binary tree node with a specified key and optional value.
|
|
219
|
-
* @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
|
|
220
|
-
* @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
|
|
221
|
-
* not required to be provided when calling the function. If a `value` is provided, it should be of
|
|
222
|
-
* type `V`, which is the type of the value associated with the node.
|
|
223
|
-
* @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
|
|
224
|
-
* as BinaryTreeNode<K, V>.
|
|
225
|
-
*/
|
|
226
|
-
createNode(key, value) {
|
|
227
|
-
return new BinaryTreeNode(key, this._isMapMode ? undefined : value);
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Time Complexity: O(1)
|
|
231
|
-
* Space Complexity: O(1)
|
|
232
|
-
*
|
|
233
|
-
* The function creates a binary tree with the specified options.
|
|
234
|
-
* @param [options] - The `options` parameter in the `createTree` function is an optional parameter
|
|
235
|
-
* that allows you to provide partial configuration options for creating a binary tree. It is of type
|
|
236
|
-
* `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
|
|
237
|
-
* of properties
|
|
238
|
-
* @returns A new instance of a binary tree with the specified options is being returned.
|
|
239
|
-
*/
|
|
240
|
-
createTree(options) {
|
|
241
|
-
return new BinaryTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, toEntryFn: this._toEntryFn }, options));
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Time Complexity: O(n)
|
|
245
|
-
* Space Complexity: O(log n)
|
|
246
|
-
*
|
|
247
|
-
* The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
|
|
248
|
-
* value and returns the corresponding node or null.
|
|
249
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
250
|
-
* parameter in the `ensureNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
|
|
251
|
-
* is used to determine whether the input is a key, node, entry, or raw data. The
|
|
252
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
|
|
253
|
-
* is used to specify the type of iteration to be performed. It has a default value of
|
|
254
|
-
* `this.iterationType` if not explicitly provided.
|
|
255
|
-
* @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
|
|
256
|
-
* conditions specified in the code snippet.
|
|
257
|
-
*/
|
|
258
|
-
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
259
|
-
if (keyNodeOrEntry === null)
|
|
260
|
-
return null;
|
|
261
|
-
if (keyNodeOrEntry === undefined)
|
|
262
|
-
return;
|
|
263
|
-
if (keyNodeOrEntry === this._NIL)
|
|
264
|
-
return;
|
|
265
|
-
if (this.isNode(keyNodeOrEntry))
|
|
266
|
-
return keyNodeOrEntry;
|
|
267
|
-
if (this.isEntry(keyNodeOrEntry)) {
|
|
268
|
-
const key = keyNodeOrEntry[0];
|
|
269
|
-
if (key === null)
|
|
270
|
-
return null;
|
|
271
|
-
if (key === undefined)
|
|
272
|
-
return;
|
|
273
|
-
return this.getNode(key, this._root, iterationType);
|
|
274
|
-
}
|
|
275
|
-
return this.getNode(keyNodeOrEntry, this._root, iterationType);
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Time Complexity: O(1)
|
|
279
|
-
* Space Complexity: O(1)
|
|
280
|
-
*
|
|
281
|
-
* The function isNode checks if the input is an instance of BinaryTreeNode.
|
|
282
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
283
|
-
* `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is
|
|
284
|
-
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
|
|
285
|
-
* accordingly.
|
|
286
|
-
* @returns The function `isNode` is checking if the input `keyNodeOrEntry` is an instance of
|
|
287
|
-
* `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
|
|
288
|
-
* it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
|
|
289
|
-
* is not a node.
|
|
290
|
-
*/
|
|
291
|
-
isNode(keyNodeOrEntry) {
|
|
292
|
-
return keyNodeOrEntry instanceof BinaryTreeNode;
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* Time Complexity: O(1)
|
|
296
|
-
* Space Complexity: O(1)
|
|
297
|
-
*
|
|
298
|
-
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
299
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R} keyNodeEntryOrRaw - K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
300
|
-
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
301
|
-
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
302
|
-
* indicating that it is of type `R`.
|
|
303
|
-
*/
|
|
304
|
-
isRaw(keyNodeEntryOrRaw) {
|
|
305
|
-
return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Time Complexity: O(1)
|
|
309
|
-
* Space Complexity: O(1)
|
|
310
|
-
*
|
|
311
|
-
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
312
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
313
|
-
* parameter in the `isRealNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
|
|
314
|
-
* The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal
|
|
315
|
-
* @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid
|
|
316
|
-
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
|
|
317
|
-
* values, it then calls the `isNode` method to further determine if the input is a node. The
|
|
318
|
-
* function will return a boolean value indicating whether the
|
|
319
|
-
*/
|
|
320
|
-
isRealNode(keyNodeOrEntry) {
|
|
321
|
-
if (keyNodeOrEntry === this._NIL || keyNodeOrEntry === null || keyNodeOrEntry === undefined)
|
|
322
|
-
return false;
|
|
323
|
-
return this.isNode(keyNodeOrEntry);
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Time Complexity: O(1)
|
|
327
|
-
* Space Complexity: O(1)
|
|
328
|
-
*
|
|
329
|
-
* The function checks if a given input is a valid node or null.
|
|
330
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
331
|
-
* `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
|
|
332
|
-
* V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or
|
|
333
|
-
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
|
|
334
|
-
* `keyNodeOrEntry` is either `null` or a real node, and returns `true` if it is a node or
|
|
335
|
-
* `null`, and `false` otherwise.
|
|
336
|
-
*/
|
|
337
|
-
isRealNodeOrNull(keyNodeOrEntry) {
|
|
338
|
-
return keyNodeOrEntry === null || this.isRealNode(keyNodeOrEntry);
|
|
339
|
-
}
|
|
340
|
-
/**
|
|
341
|
-
* Time Complexity: O(1)
|
|
342
|
-
* Space Complexity: O(1)
|
|
343
|
-
*
|
|
344
|
-
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
|
|
345
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - BTNRep<K, V,
|
|
346
|
-
* BinaryTreeNode<K, V>>
|
|
347
|
-
* @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL`
|
|
348
|
-
* property of the current object and returning a boolean value based on that comparison.
|
|
349
|
-
*/
|
|
350
|
-
isNIL(keyNodeOrEntry) {
|
|
351
|
-
return keyNodeOrEntry === this._NIL;
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* Time Complexity: O(1)
|
|
355
|
-
* Space Complexity: O(1)
|
|
356
|
-
*
|
|
357
|
-
* The function `isRange` checks if the input parameter is an instance of the `Range` class.
|
|
358
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>} keyNodeEntryOrPredicate
|
|
359
|
-
* - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
|
|
360
|
-
* of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `NodePredicate<BinaryTreeNode<K, V>>`, or
|
|
361
|
-
* `Range<K>`. The function checks if the `keyNodeEntry
|
|
362
|
-
* @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an
|
|
363
|
-
* instance of the `Range` class. If it is an instance of `Range`, the function will return `true`,
|
|
364
|
-
* indicating that the parameter is a `Range<K>`. If it is not an instance of `Range`, the function
|
|
365
|
-
* will return `false`.
|
|
366
|
-
*/
|
|
367
|
-
isRange(keyNodeEntryOrPredicate) {
|
|
368
|
-
return keyNodeEntryOrPredicate instanceof common_1.Range;
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
* Time Complexity: O(1)
|
|
372
|
-
* Space Complexity: O(1)
|
|
373
|
-
*
|
|
374
|
-
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
375
|
-
* tree.
|
|
376
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
377
|
-
* `keyNodeOrEntry` can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It represents a
|
|
378
|
-
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
|
|
379
|
-
* provided
|
|
380
|
-
* @returns The function `isLeaf` returns a boolean value indicating whether the input
|
|
381
|
-
* `keyNodeOrEntry` is a leaf node in a binary tree.
|
|
382
|
-
*/
|
|
383
|
-
isLeaf(keyNodeOrEntry) {
|
|
384
|
-
keyNodeOrEntry = this.ensureNode(keyNodeOrEntry);
|
|
385
|
-
if (keyNodeOrEntry === undefined)
|
|
386
|
-
return false;
|
|
387
|
-
if (keyNodeOrEntry === null)
|
|
388
|
-
return true;
|
|
389
|
-
return !this.isRealNode(keyNodeOrEntry.left) && !this.isRealNode(keyNodeOrEntry.right);
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* Time Complexity: O(1)
|
|
393
|
-
* Space Complexity: O(1)
|
|
394
|
-
*
|
|
395
|
-
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
|
|
396
|
-
* with a length of 2.
|
|
397
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
398
|
-
* parameter in the `isEntry` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or type `R`.
|
|
399
|
-
* The function checks if the provided `keyNodeOrEntry` is of type `BTN
|
|
400
|
-
* @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array
|
|
401
|
-
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
|
|
402
|
-
* `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
|
|
403
|
-
*/
|
|
404
|
-
isEntry(keyNodeOrEntry) {
|
|
405
|
-
return Array.isArray(keyNodeOrEntry) && keyNodeOrEntry.length === 2;
|
|
406
|
-
}
|
|
407
|
-
/**
|
|
408
|
-
* Time Complexity O(1)
|
|
409
|
-
* Space Complexity O(1)
|
|
410
|
-
*
|
|
411
|
-
* The function `isValidKey` checks if a given key is comparable.
|
|
412
|
-
* @param {any} key - The `key` parameter is of type `any`, which means it can be any data type in
|
|
413
|
-
* TypeScript.
|
|
414
|
-
* @returns The function `isValidKey` is checking if the `key` parameter is `null` or if it is comparable.
|
|
415
|
-
* If the `key` is `null`, the function returns `true`. Otherwise, it returns the result of the
|
|
416
|
-
* `isComparable` function, which is not provided in the code snippet.
|
|
417
|
-
*/
|
|
418
|
-
isValidKey(key) {
|
|
419
|
-
if (key === null)
|
|
420
|
-
return true;
|
|
421
|
-
return (0, utils_1.isComparable)(key);
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* Time Complexity O(n)
|
|
425
|
-
* Space Complexity O(1)
|
|
426
|
-
*
|
|
427
|
-
* The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
|
|
428
|
-
* and finding the correct insertion position.
|
|
429
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `add` method you provided
|
|
430
|
-
* seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry`
|
|
431
|
-
* parameter in the method can accept different types of values:
|
|
432
|
-
* @param {V} [value] - The `value` parameter in the `add` method represents the value associated
|
|
433
|
-
* with the key that you want to add to the binary tree. When adding a key-value pair to the binary
|
|
434
|
-
* tree, you provide the key and its corresponding value. The `add` method then creates a new node
|
|
435
|
-
* with this
|
|
436
|
-
* @returns The `add` method returns a boolean value. It returns `true` if the insertion of the new
|
|
437
|
-
* node was successful, and `false` if the insertion position could not be found or if a duplicate
|
|
438
|
-
* key was found and the node was replaced instead of inserted.
|
|
439
|
-
*/
|
|
440
|
-
add(keyNodeOrEntry, value) {
|
|
441
|
-
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
442
|
-
if (newNode === undefined)
|
|
443
|
-
return false;
|
|
444
|
-
// If the tree is empty, directly set the new node as the root node
|
|
445
|
-
if (!this._root) {
|
|
446
|
-
this._setRoot(newNode);
|
|
447
|
-
if (this._isMapMode)
|
|
448
|
-
this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
|
|
449
|
-
this._size = 1;
|
|
450
|
-
return true;
|
|
451
|
-
}
|
|
452
|
-
const queue = new queue_1.Queue([this._root]);
|
|
453
|
-
let potentialParent; // Record the parent node of the potential insertion location
|
|
454
|
-
while (queue.length > 0) {
|
|
455
|
-
const cur = queue.shift();
|
|
456
|
-
if (!cur)
|
|
457
|
-
continue;
|
|
458
|
-
if (!this._isDuplicate) {
|
|
459
|
-
// Check for duplicate keys when newNode is not null
|
|
460
|
-
if (newNode !== null && cur.key === newNode.key) {
|
|
461
|
-
this._replaceNode(cur, newNode);
|
|
462
|
-
if (this._isMapMode)
|
|
463
|
-
this._setValue(cur.key, newValue);
|
|
464
|
-
return true; // If duplicate keys are found, no insertion is performed
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
// Record the first possible insertion location found
|
|
468
|
-
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
469
|
-
potentialParent = cur;
|
|
470
|
-
}
|
|
471
|
-
// Continue traversing the left and right subtrees
|
|
472
|
-
if (cur.left !== null) {
|
|
473
|
-
if (cur.left)
|
|
474
|
-
queue.push(cur.left);
|
|
475
|
-
}
|
|
476
|
-
if (cur.right !== null) {
|
|
477
|
-
if (cur.right)
|
|
478
|
-
queue.push(cur.right);
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
// At the end of the traversal, if the insertion position is found, insert
|
|
482
|
-
if (potentialParent) {
|
|
483
|
-
if (potentialParent.left === undefined) {
|
|
484
|
-
potentialParent.left = newNode;
|
|
485
|
-
}
|
|
486
|
-
else if (potentialParent.right === undefined) {
|
|
487
|
-
potentialParent.right = newNode;
|
|
488
|
-
}
|
|
489
|
-
if (this._isMapMode)
|
|
490
|
-
this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
|
|
491
|
-
this._size++;
|
|
492
|
-
return true;
|
|
493
|
-
}
|
|
494
|
-
return false; // If the insertion position cannot be found, return undefined
|
|
495
|
-
}
|
|
496
|
-
/**
|
|
497
|
-
* Time Complexity: O(k * n)
|
|
498
|
-
* Space Complexity: O(k)
|
|
499
|
-
*
|
|
500
|
-
* The `addMany` function takes in multiple keys or nodes or entries or raw values along with
|
|
501
|
-
* optional values, and adds them to a data structure while returning an array indicating whether
|
|
502
|
-
* each insertion was successful.
|
|
503
|
-
* @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
|
|
504
|
-
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
|
|
505
|
-
* `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
|
|
506
|
-
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
|
|
507
|
-
* accepts an iterable of values. These values correspond to the keys or nodes being added in the
|
|
508
|
-
* `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
|
|
509
|
-
* assign them
|
|
510
|
-
* @returns The `addMany` method returns an array of boolean values indicating whether each key,
|
|
511
|
-
* node, entry, or raw value was successfully added to the data structure. Each boolean value
|
|
512
|
-
* corresponds to the success of adding the corresponding key or value in the input iterable.
|
|
513
|
-
*/
|
|
514
|
-
addMany(keysNodesEntriesOrRaws, values) {
|
|
515
|
-
// TODO not sure addMany not be run multi times
|
|
516
|
-
const inserted = [];
|
|
517
|
-
let valuesIterator;
|
|
518
|
-
if (values) {
|
|
519
|
-
valuesIterator = values[Symbol.iterator]();
|
|
520
|
-
}
|
|
521
|
-
for (let keyNodeEntryOrRaw of keysNodesEntriesOrRaws) {
|
|
522
|
-
let value = undefined;
|
|
523
|
-
if (valuesIterator) {
|
|
524
|
-
const valueResult = valuesIterator.next();
|
|
525
|
-
if (!valueResult.done) {
|
|
526
|
-
value = valueResult.value;
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
if (this.isRaw(keyNodeEntryOrRaw))
|
|
530
|
-
keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
531
|
-
inserted.push(this.add(keyNodeEntryOrRaw, value));
|
|
532
|
-
}
|
|
533
|
-
return inserted;
|
|
534
|
-
}
|
|
535
|
-
/**
|
|
536
|
-
* Time Complexity: O(k * n)
|
|
537
|
-
* Space Complexity: O(1)
|
|
538
|
-
*
|
|
539
|
-
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
540
|
-
* elements from the other tree.
|
|
541
|
-
* @param anotherTree - BinaryTree<K, V, R, MK, MV, MR>
|
|
542
|
-
*/
|
|
543
|
-
merge(anotherTree) {
|
|
544
|
-
this.addMany(anotherTree, []);
|
|
545
|
-
}
|
|
546
|
-
/**
|
|
547
|
-
* Time Complexity: O(k * n)
|
|
548
|
-
* Space Complexity: O(1)
|
|
549
|
-
*
|
|
550
|
-
* The `refill` function clears the existing data structure and then adds new key-value pairs based
|
|
551
|
-
* on the provided input.
|
|
552
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
|
|
553
|
-
* method can accept an iterable containing a mix of `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R`
|
|
554
|
-
* objects.
|
|
555
|
-
* @param [values] - The `values` parameter in the `refill` method is an optional parameter that
|
|
556
|
-
* accepts an iterable of values of type `V` or `undefined`.
|
|
557
|
-
*/
|
|
558
|
-
refill(keysNodesEntriesOrRaws, values) {
|
|
559
|
-
this.clear();
|
|
560
|
-
this.addMany(keysNodesEntriesOrRaws, values);
|
|
561
|
-
}
|
|
562
|
-
/**
|
|
563
|
-
* Time Complexity: O(n)
|
|
564
|
-
* Space Complexity: O(1)
|
|
565
|
-
*
|
|
566
|
-
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
567
|
-
* the deleted node along with information for tree balancing.
|
|
568
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry
|
|
569
|
-
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
570
|
-
* node, entry or raw data. The method returns an array of
|
|
571
|
-
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
572
|
-
* balancing is needed.
|
|
573
|
-
* @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
|
|
574
|
-
* the array contains information about the node that was deleted (`deleted`) and the node that may
|
|
575
|
-
* need to be balanced (`needBalanced`).
|
|
576
|
-
*/
|
|
577
|
-
delete(keyNodeOrEntry) {
|
|
578
|
-
const deletedResult = [];
|
|
579
|
-
if (!this._root)
|
|
580
|
-
return deletedResult;
|
|
581
|
-
const curr = this.getNode(keyNodeOrEntry);
|
|
582
|
-
if (!curr)
|
|
583
|
-
return deletedResult;
|
|
584
|
-
const parent = curr === null || curr === void 0 ? void 0 : curr.parent;
|
|
585
|
-
let needBalanced;
|
|
586
|
-
let orgCurrent = curr;
|
|
587
|
-
if (!curr.left && !curr.right && !parent) {
|
|
588
|
-
this._setRoot(undefined);
|
|
589
|
-
}
|
|
590
|
-
else if (curr.left) {
|
|
591
|
-
const leftSubTreeRightMost = this.getRightMost(node => node, curr.left);
|
|
592
|
-
if (leftSubTreeRightMost) {
|
|
593
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
594
|
-
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
595
|
-
if (parentOfLeftSubTreeMax) {
|
|
596
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
597
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
598
|
-
else
|
|
599
|
-
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
600
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
else if (parent) {
|
|
605
|
-
const { familyPosition: fp } = curr;
|
|
606
|
-
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
607
|
-
parent.left = curr.right;
|
|
608
|
-
}
|
|
609
|
-
else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
|
|
610
|
-
parent.right = curr.right;
|
|
611
|
-
}
|
|
612
|
-
needBalanced = parent;
|
|
613
|
-
}
|
|
614
|
-
else {
|
|
615
|
-
this._setRoot(curr.right);
|
|
616
|
-
curr.right = undefined;
|
|
617
|
-
}
|
|
618
|
-
this._size = this._size - 1;
|
|
619
|
-
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
620
|
-
if (this._isMapMode && orgCurrent)
|
|
621
|
-
this._store.delete(orgCurrent.key);
|
|
622
|
-
return deletedResult;
|
|
623
|
-
}
|
|
624
|
-
/**
|
|
625
|
-
* Time Complexity: O(n)
|
|
626
|
-
* Space Complexity: O(k + log n)
|
|
627
|
-
*
|
|
628
|
-
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
629
|
-
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
630
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
631
|
-
* `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values:
|
|
632
|
-
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
633
|
-
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
634
|
-
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
635
|
-
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
636
|
-
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
637
|
-
* extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
638
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `search` function is
|
|
639
|
-
* used to specify the node from which the search operation should begin. It represents the starting
|
|
640
|
-
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
641
|
-
* provided, the search operation will start from the root
|
|
642
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
|
|
643
|
-
* specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
|
|
644
|
-
* two possible values:
|
|
645
|
-
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
646
|
-
* on the search algorithm implemented within the function.
|
|
647
|
-
*/
|
|
648
|
-
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
649
|
-
if (keyNodeEntryOrPredicate === undefined)
|
|
650
|
-
return [];
|
|
651
|
-
if (keyNodeEntryOrPredicate === null)
|
|
652
|
-
return [];
|
|
653
|
-
startNode = this.ensureNode(startNode);
|
|
654
|
-
if (!startNode)
|
|
655
|
-
return [];
|
|
656
|
-
const predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
657
|
-
const ans = [];
|
|
658
|
-
if (iterationType === 'RECURSIVE') {
|
|
659
|
-
const dfs = (cur) => {
|
|
660
|
-
if (predicate(cur)) {
|
|
661
|
-
ans.push(callback(cur));
|
|
662
|
-
if (onlyOne)
|
|
663
|
-
return;
|
|
664
|
-
}
|
|
665
|
-
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
666
|
-
return;
|
|
667
|
-
if (this.isRealNode(cur.left))
|
|
668
|
-
dfs(cur.left);
|
|
669
|
-
if (this.isRealNode(cur.right))
|
|
670
|
-
dfs(cur.right);
|
|
671
|
-
};
|
|
672
|
-
dfs(startNode);
|
|
673
|
-
}
|
|
674
|
-
else {
|
|
675
|
-
const stack = [startNode];
|
|
676
|
-
while (stack.length > 0) {
|
|
677
|
-
const cur = stack.pop();
|
|
678
|
-
if (this.isRealNode(cur)) {
|
|
679
|
-
if (predicate(cur)) {
|
|
680
|
-
ans.push(callback(cur));
|
|
681
|
-
if (onlyOne)
|
|
682
|
-
return ans;
|
|
683
|
-
}
|
|
684
|
-
if (this.isRealNode(cur.left))
|
|
685
|
-
stack.push(cur.left);
|
|
686
|
-
if (this.isRealNode(cur.right))
|
|
687
|
-
stack.push(cur.right);
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
return ans;
|
|
692
|
-
}
|
|
693
|
-
/**
|
|
694
|
-
* Time Complexity: O(n)
|
|
695
|
-
* Space Complexity: O(k + log n)
|
|
696
|
-
*
|
|
697
|
-
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
698
|
-
* or predicate, with options for recursive or iterative traversal.
|
|
699
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
700
|
-
* - The `getNodes` function you provided takes several parameters:
|
|
701
|
-
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
702
|
-
* determines whether to return only the first node that matches the criteria specified by the
|
|
703
|
-
* `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
704
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
705
|
-
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
706
|
-
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
707
|
-
* not provided, the default value is set to `this._root
|
|
708
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
709
|
-
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
710
|
-
* can have two possible values:
|
|
711
|
-
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
712
|
-
* based on the input parameters and the iteration type specified.
|
|
713
|
-
*/
|
|
714
|
-
getNodes(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
715
|
-
return this.search(keyNodeEntryOrPredicate, onlyOne, node => node, startNode, iterationType);
|
|
716
|
-
}
|
|
717
|
-
/**
|
|
718
|
-
* Time Complexity: O(n)
|
|
719
|
-
* Space Complexity: O(log n)
|
|
720
|
-
*
|
|
721
|
-
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
722
|
-
* predicate.
|
|
723
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
724
|
-
* - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key,
|
|
725
|
-
* node, entry, raw data, or a predicate function.
|
|
726
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
727
|
-
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
728
|
-
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
729
|
-
* is typically the root node of the binary tree.
|
|
730
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is
|
|
731
|
-
* used to specify the type of iteration to be performed when searching for a node. It has a default
|
|
732
|
-
* value of `this.iterationType`, which means it will use the iteration type defined in the current
|
|
733
|
-
* context if no specific value is provided
|
|
734
|
-
* @returns The `getNode` function is returning the first node that matches the specified criteria,
|
|
735
|
-
* or `null` if no matching node is found.
|
|
736
|
-
*/
|
|
737
|
-
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
738
|
-
return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType)[0];
|
|
739
|
-
}
|
|
740
|
-
/**
|
|
741
|
-
* Time Complexity: O(n)
|
|
742
|
-
* Space Complexity: O(log n)
|
|
743
|
-
*
|
|
744
|
-
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
745
|
-
* node, entry, raw data, or predicate in a data structure.
|
|
746
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
747
|
-
* - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the
|
|
748
|
-
* following types:
|
|
749
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `get`
|
|
750
|
-
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
751
|
-
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
752
|
-
* tree (`this._root`).
|
|
753
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `get` method is used
|
|
754
|
-
* to specify the type of iteration to be performed when searching for a key in the binary tree. It
|
|
755
|
-
* is an optional parameter with a default value of `this.iterationType`, which means it will use the
|
|
756
|
-
* iteration type defined in the
|
|
757
|
-
* @returns The `get` method is returning the value associated with the specified key, node, entry,
|
|
758
|
-
* raw data, or predicate in the binary tree map. If the specified key or node is found in the tree,
|
|
759
|
-
* the method returns the corresponding value. If the key or node is not found, it returns
|
|
760
|
-
* `undefined`.
|
|
761
|
-
*/
|
|
762
|
-
get(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
763
|
-
var _a;
|
|
764
|
-
if (this._isMapMode) {
|
|
765
|
-
const key = this._extractKey(keyNodeEntryOrPredicate);
|
|
766
|
-
if (key === null || key === undefined)
|
|
767
|
-
return;
|
|
768
|
-
return this._store.get(key);
|
|
769
|
-
}
|
|
770
|
-
return (_a = this.getNode(keyNodeEntryOrPredicate, startNode, iterationType)) === null || _a === void 0 ? void 0 : _a.value;
|
|
771
|
-
}
|
|
772
|
-
/**
|
|
773
|
-
* Time Complexity: O(n)
|
|
774
|
-
* Space Complexity: O(log n)
|
|
775
|
-
*
|
|
776
|
-
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
777
|
-
* exists in the data structure.
|
|
778
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
|
|
779
|
-
* - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of
|
|
780
|
-
* the following types:
|
|
781
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
782
|
-
* `override` method is used to specify the starting point for the search operation within the data
|
|
783
|
-
* structure. It defaults to `this._root` if not provided explicitly.
|
|
784
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
785
|
-
* is used to specify the type of iteration to be performed. It has a default value of
|
|
786
|
-
* `this.iterationType`, which means it will use the iteration type defined in the current context if
|
|
787
|
-
* no value is provided when calling the method.
|
|
788
|
-
* @returns The `override has` method is returning a boolean value. It checks if there are any nodes
|
|
789
|
-
* that match the provided key, node, entry, raw data, or predicate in the tree structure. If there
|
|
790
|
-
* are matching nodes, it returns `true`, indicating that the tree contains the specified element.
|
|
791
|
-
* Otherwise, it returns `false`.
|
|
792
|
-
*/
|
|
793
|
-
has(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
794
|
-
return this.search(keyNodeEntryOrPredicate, true, node => node, startNode, iterationType).length > 0;
|
|
795
|
-
}
|
|
796
|
-
/**
|
|
797
|
-
* Time Complexity: O(1)
|
|
798
|
-
* Space Complexity: O(1)
|
|
799
|
-
*
|
|
800
|
-
* The clear function removes nodes and values in map mode.
|
|
801
|
-
*/
|
|
802
|
-
clear() {
|
|
803
|
-
this._clearNodes();
|
|
804
|
-
if (this._isMapMode)
|
|
805
|
-
this._clearValues();
|
|
806
|
-
}
|
|
807
|
-
/**
|
|
808
|
-
* Time Complexity: O(1)
|
|
809
|
-
* Space Complexity: O(1)
|
|
810
|
-
*
|
|
811
|
-
* The `isEmpty` function in TypeScript checks if a data structure has no elements and returns a
|
|
812
|
-
* boolean value.
|
|
813
|
-
* @returns The `isEmpty()` method is returning a boolean value, specifically `true` if the `_size`
|
|
814
|
-
* property is equal to 0, indicating that the data structure is empty, and `false` otherwise.
|
|
815
|
-
*/
|
|
816
|
-
isEmpty() {
|
|
817
|
-
return this._size === 0;
|
|
818
|
-
}
|
|
819
|
-
/**
|
|
820
|
-
* Time Complexity: O(n)
|
|
821
|
-
* Space Complexity: O(log n)
|
|
822
|
-
*
|
|
823
|
-
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
824
|
-
* its height.
|
|
825
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
|
|
826
|
-
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
827
|
-
* binary tree or a specific node from which the balance check should begin.
|
|
828
|
-
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
829
|
-
* the tree starting from the `startNode` node is perfectly balanced or not. The return value is
|
|
830
|
-
* determined by comparing the minimum height of the tree with the height of the tree. If the minimum
|
|
831
|
-
* height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
|
|
832
|
-
* balanced and
|
|
833
|
-
*/
|
|
834
|
-
isPerfectlyBalanced(startNode = this._root) {
|
|
835
|
-
return this.getMinHeight(startNode) + 1 >= this.getHeight(startNode);
|
|
836
|
-
}
|
|
837
|
-
/**
|
|
838
|
-
* Time Complexity: O(n)
|
|
839
|
-
* Space Complexity: O(log n)
|
|
840
|
-
*
|
|
841
|
-
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
842
|
-
* or iterative methods.
|
|
843
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `isBST`
|
|
844
|
-
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
845
|
-
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
846
|
-
* provided, the function will default to
|
|
847
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `isBST` function
|
|
848
|
-
* determines whether the function should use a recursive approach or an iterative approach to check
|
|
849
|
-
* if the binary search tree (BST) is valid.
|
|
850
|
-
* @returns The `isBST` method is returning a boolean value, which indicates whether the binary
|
|
851
|
-
* search tree (BST) represented by the given root node is a valid BST or not. The method checks if
|
|
852
|
-
* the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
|
|
853
|
-
* less than the node's key, and all nodes in its right subtree have keys greater than the node's
|
|
854
|
-
*/
|
|
855
|
-
isBST(startNode = this._root, iterationType = this.iterationType) {
|
|
856
|
-
// TODO there is a bug
|
|
857
|
-
startNode = this.ensureNode(startNode);
|
|
858
|
-
if (!startNode)
|
|
859
|
-
return true;
|
|
860
|
-
if (iterationType === 'RECURSIVE') {
|
|
861
|
-
const dfs = (cur, min, max) => {
|
|
862
|
-
if (!this.isRealNode(cur))
|
|
863
|
-
return true;
|
|
864
|
-
const numKey = Number(cur.key);
|
|
865
|
-
if (numKey <= min || numKey >= max)
|
|
866
|
-
return false;
|
|
867
|
-
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
868
|
-
};
|
|
869
|
-
const isStandardBST = dfs(startNode, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
870
|
-
const isInverseBST = dfs(startNode, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
871
|
-
return isStandardBST || isInverseBST;
|
|
872
|
-
}
|
|
873
|
-
else {
|
|
874
|
-
const checkBST = (checkMax = false) => {
|
|
875
|
-
const stack = [];
|
|
876
|
-
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
877
|
-
// @ts-ignore
|
|
878
|
-
let curr = startNode;
|
|
879
|
-
while (this.isRealNode(curr) || stack.length > 0) {
|
|
880
|
-
while (this.isRealNode(curr)) {
|
|
881
|
-
stack.push(curr);
|
|
882
|
-
curr = curr.left;
|
|
883
|
-
}
|
|
884
|
-
curr = stack.pop();
|
|
885
|
-
const numKey = Number(curr.key);
|
|
886
|
-
if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
|
|
887
|
-
return false;
|
|
888
|
-
prev = numKey;
|
|
889
|
-
curr = curr.right;
|
|
890
|
-
}
|
|
891
|
-
return true;
|
|
892
|
-
};
|
|
893
|
-
const isStandardBST = checkBST(false), isInverseBST = checkBST(true);
|
|
894
|
-
return isStandardBST || isInverseBST;
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
/**
|
|
898
|
-
* Time Complexity: O(n)
|
|
899
|
-
* Space Complexity: O(log n)
|
|
900
|
-
*
|
|
901
|
-
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
902
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } dist - The `dist` parameter in the `getDepth`
|
|
903
|
-
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
904
|
-
* It is the target node for which you want to calculate the depth from the `startNode` node.
|
|
905
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
906
|
-
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
907
|
-
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
908
|
-
* for `startNode` is set to the root of the binary
|
|
909
|
-
* @returns The `getDepth` method returns the depth of a given node `dist` relative to the
|
|
910
|
-
* `startNode` node in a binary tree. If the `dist` node is not found in the path to the `startNode`
|
|
911
|
-
* node, it returns the depth of the `dist` node from the root of the tree.
|
|
912
|
-
*/
|
|
913
|
-
getDepth(dist, startNode = this._root) {
|
|
914
|
-
let distEnsured = this.ensureNode(dist);
|
|
915
|
-
const beginRootEnsured = this.ensureNode(startNode);
|
|
916
|
-
let depth = 0;
|
|
917
|
-
while (distEnsured === null || distEnsured === void 0 ? void 0 : distEnsured.parent) {
|
|
918
|
-
if (distEnsured === beginRootEnsured) {
|
|
919
|
-
return depth;
|
|
920
|
-
}
|
|
921
|
-
depth++;
|
|
922
|
-
distEnsured = distEnsured.parent;
|
|
923
|
-
}
|
|
924
|
-
return depth;
|
|
925
|
-
}
|
|
926
|
-
/**
|
|
927
|
-
* Time Complexity: O(n)
|
|
928
|
-
* Space Complexity: O(log n)
|
|
929
|
-
*
|
|
930
|
-
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
931
|
-
* or iterative approach in TypeScript.
|
|
932
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
|
|
933
|
-
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
934
|
-
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
935
|
-
* binary tree data structure.
|
|
936
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
937
|
-
* of iteration to be performed while calculating the height of the binary tree. It can have two
|
|
938
|
-
* possible values:
|
|
939
|
-
* @returns The `getHeight` method returns the height of the binary tree starting from the specified
|
|
940
|
-
* root node. The height is calculated based on the maximum depth of the tree, considering either a
|
|
941
|
-
* recursive approach or an iterative approach depending on the `iterationType` parameter.
|
|
942
|
-
*/
|
|
943
|
-
getHeight(startNode = this._root, iterationType = this.iterationType) {
|
|
944
|
-
startNode = this.ensureNode(startNode);
|
|
945
|
-
if (!this.isRealNode(startNode))
|
|
946
|
-
return -1;
|
|
947
|
-
if (iterationType === 'RECURSIVE') {
|
|
948
|
-
const _getMaxHeight = (cur) => {
|
|
949
|
-
if (!this.isRealNode(cur))
|
|
950
|
-
return -1;
|
|
951
|
-
const leftHeight = _getMaxHeight(cur.left);
|
|
952
|
-
const rightHeight = _getMaxHeight(cur.right);
|
|
953
|
-
return Math.max(leftHeight, rightHeight) + 1;
|
|
954
|
-
};
|
|
955
|
-
return _getMaxHeight(startNode);
|
|
956
|
-
}
|
|
957
|
-
else {
|
|
958
|
-
const stack = [{ node: startNode, depth: 0 }];
|
|
959
|
-
let maxHeight = 0;
|
|
960
|
-
while (stack.length > 0) {
|
|
961
|
-
const { node, depth } = stack.pop();
|
|
962
|
-
if (this.isRealNode(node.left))
|
|
963
|
-
stack.push({ node: node.left, depth: depth + 1 });
|
|
964
|
-
if (this.isRealNode(node.right))
|
|
965
|
-
stack.push({ node: node.right, depth: depth + 1 });
|
|
966
|
-
maxHeight = Math.max(maxHeight, depth);
|
|
967
|
-
}
|
|
968
|
-
return maxHeight;
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
/**
|
|
972
|
-
* Time Complexity: O(n)
|
|
973
|
-
* Space Complexity: O(log n)
|
|
974
|
-
*
|
|
975
|
-
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
976
|
-
* recursive or iterative approach in TypeScript.
|
|
977
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
978
|
-
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
979
|
-
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
980
|
-
* tree. If not provided, the default value is the root
|
|
981
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `getMinHeight` method
|
|
982
|
-
* specifies the type of iteration to use when calculating the minimum height of a binary tree. It
|
|
983
|
-
* can have two possible values:
|
|
984
|
-
* @returns The `getMinHeight` method returns the minimum height of the binary tree starting from the
|
|
985
|
-
* specified root node. The height is calculated based on the shortest path from the root node to a
|
|
986
|
-
* leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
|
|
987
|
-
* a stack) based on the `iterationType` parameter.
|
|
988
|
-
*/
|
|
989
|
-
getMinHeight(startNode = this._root, iterationType = this.iterationType) {
|
|
990
|
-
startNode = this.ensureNode(startNode);
|
|
991
|
-
if (!startNode)
|
|
992
|
-
return -1;
|
|
993
|
-
if (iterationType === 'RECURSIVE') {
|
|
994
|
-
const _getMinHeight = (cur) => {
|
|
995
|
-
if (!this.isRealNode(cur))
|
|
996
|
-
return 0;
|
|
997
|
-
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
998
|
-
return 0;
|
|
999
|
-
const leftMinHeight = _getMinHeight(cur.left);
|
|
1000
|
-
const rightMinHeight = _getMinHeight(cur.right);
|
|
1001
|
-
return Math.min(leftMinHeight, rightMinHeight) + 1;
|
|
1002
|
-
};
|
|
1003
|
-
return _getMinHeight(startNode);
|
|
1004
|
-
}
|
|
1005
|
-
else {
|
|
1006
|
-
const stack = [];
|
|
1007
|
-
let node = startNode, last = null;
|
|
1008
|
-
const depths = new Map();
|
|
1009
|
-
while (stack.length > 0 || node) {
|
|
1010
|
-
if (this.isRealNode(node)) {
|
|
1011
|
-
stack.push(node);
|
|
1012
|
-
node = node.left;
|
|
1013
|
-
}
|
|
1014
|
-
else {
|
|
1015
|
-
node = stack[stack.length - 1];
|
|
1016
|
-
if (!this.isRealNode(node.right) || last === node.right) {
|
|
1017
|
-
node = stack.pop();
|
|
1018
|
-
if (this.isRealNode(node)) {
|
|
1019
|
-
const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) : -1;
|
|
1020
|
-
const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) : -1;
|
|
1021
|
-
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
1022
|
-
last = node;
|
|
1023
|
-
node = null;
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
else
|
|
1027
|
-
node = node.right;
|
|
1028
|
-
}
|
|
1029
|
-
}
|
|
1030
|
-
return depths.get(startNode);
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
/**
|
|
1034
|
-
* Time Complexity: O(log n)
|
|
1035
|
-
* Space Complexity: O(log n)
|
|
1036
|
-
*
|
|
1037
|
-
* The function `getPathToRoot` in TypeScript retrieves the path from a given node to the root of a
|
|
1038
|
-
* tree structure, applying a specified callback function along the way.
|
|
1039
|
-
* @param {C} callback - The `callback` parameter is a function that is used to process each node in
|
|
1040
|
-
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
1041
|
-
* a value based on that node. The return type of the callback function is determined by the generic
|
|
1042
|
-
* type `C
|
|
1043
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } beginNode - The `beginNode` parameter in the
|
|
1044
|
-
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
1045
|
-
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
1046
|
-
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
1047
|
-
* not. If `isReverse` is set to `true`, the path will be reversed before being returned. If `is
|
|
1048
|
-
* @returns The function `getPathToRoot` returns an array of the return values of the callback
|
|
1049
|
-
* function `callback` applied to each node in the path from the `beginNode` to the root node. The
|
|
1050
|
-
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
1051
|
-
* parameter.
|
|
1052
|
-
*/
|
|
1053
|
-
getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
|
|
1054
|
-
const result = [];
|
|
1055
|
-
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
1056
|
-
if (!beginNodeEnsured)
|
|
1057
|
-
return result;
|
|
1058
|
-
while (beginNodeEnsured.parent) {
|
|
1059
|
-
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
1060
|
-
result.push(callback(beginNodeEnsured));
|
|
1061
|
-
beginNodeEnsured = beginNodeEnsured.parent;
|
|
1062
|
-
}
|
|
1063
|
-
result.push(callback(beginNodeEnsured));
|
|
1064
|
-
return isReverse ? result.reverse() : result;
|
|
1065
|
-
}
|
|
1066
|
-
/**
|
|
1067
|
-
* Time Complexity: O(log n)
|
|
1068
|
-
* Space Complexity: O(log n)
|
|
1069
|
-
*
|
|
1070
|
-
* The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
|
|
1071
|
-
* tail-recursive iteration.
|
|
1072
|
-
* @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
|
|
1073
|
-
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
1074
|
-
* value of `_DEFAULT_NODE_CALLBACK` if not specified.
|
|
1075
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1076
|
-
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
1077
|
-
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1078
|
-
* starting point is provided, the function will default
|
|
1079
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `getLeftMost` function
|
|
1080
|
-
* specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
|
|
1081
|
-
* possible values:
|
|
1082
|
-
* @returns The `getLeftMost` function returns the result of the callback function `C` applied to the
|
|
1083
|
-
* leftmost node in the binary tree starting from the `startNode` node. If the `startNode` node is
|
|
1084
|
-
* `NIL`, it returns the result of the callback function applied to `undefined`. If the `startNode`
|
|
1085
|
-
* node is not a real node, it returns the result of the callback
|
|
1086
|
-
*/
|
|
1087
|
-
getLeftMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
1088
|
-
if (this.isNIL(startNode))
|
|
1089
|
-
return callback(undefined);
|
|
1090
|
-
startNode = this.ensureNode(startNode);
|
|
1091
|
-
if (!this.isRealNode(startNode))
|
|
1092
|
-
return callback(startNode);
|
|
1093
|
-
if (iterationType === 'RECURSIVE') {
|
|
1094
|
-
const dfs = (cur) => {
|
|
1095
|
-
const { left } = cur;
|
|
1096
|
-
if (!this.isRealNode(left))
|
|
1097
|
-
return cur;
|
|
1098
|
-
return dfs(left);
|
|
1099
|
-
};
|
|
1100
|
-
return callback(dfs(startNode));
|
|
1101
|
-
}
|
|
1102
|
-
else {
|
|
1103
|
-
// Indirect implementation of iteration using tail recursion optimization
|
|
1104
|
-
const dfs = (0, utils_1.makeTrampoline)((cur) => {
|
|
1105
|
-
const { left } = cur;
|
|
1106
|
-
if (!this.isRealNode(left))
|
|
1107
|
-
return cur;
|
|
1108
|
-
return (0, utils_1.makeTrampolineThunk)(() => dfs(left));
|
|
1109
|
-
});
|
|
1110
|
-
return callback(dfs(startNode));
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
/**
|
|
1114
|
-
* Time Complexity: O(log n)
|
|
1115
|
-
* Space Complexity: O(log n)
|
|
1116
|
-
*
|
|
1117
|
-
* The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
|
|
1118
|
-
* or iterative traversal methods.
|
|
1119
|
-
* @param {C} callback - The `callback` parameter is a function that will be called with the result
|
|
1120
|
-
* of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`,
|
|
1121
|
-
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
1122
|
-
* as
|
|
1123
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1124
|
-
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
1125
|
-
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
1126
|
-
* starting point is provided, the function will default
|
|
1127
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `getRightMost`
|
|
1128
|
-
* function specifies the type of iteration to be used when traversing the binary tree nodes. It can
|
|
1129
|
-
* have two possible values:
|
|
1130
|
-
* @returns The `getRightMost` function returns the result of the callback function `C`, which is
|
|
1131
|
-
* passed as a parameter to the function. The callback function is called with the rightmost node in
|
|
1132
|
-
* the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
|
|
1133
|
-
* other).
|
|
1134
|
-
*/
|
|
1135
|
-
getRightMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
1136
|
-
if (this.isNIL(startNode))
|
|
1137
|
-
return callback(undefined);
|
|
1138
|
-
startNode = this.ensureNode(startNode);
|
|
1139
|
-
if (!startNode)
|
|
1140
|
-
return callback(startNode);
|
|
1141
|
-
if (iterationType === 'RECURSIVE') {
|
|
1142
|
-
const dfs = (cur) => {
|
|
1143
|
-
const { right } = cur;
|
|
1144
|
-
if (!this.isRealNode(right))
|
|
1145
|
-
return cur;
|
|
1146
|
-
return dfs(right);
|
|
1147
|
-
};
|
|
1148
|
-
return callback(dfs(startNode));
|
|
1149
|
-
}
|
|
1150
|
-
else {
|
|
1151
|
-
// Indirect implementation of iteration using tail recursion optimization
|
|
1152
|
-
const dfs = (0, utils_1.makeTrampoline)((cur) => {
|
|
1153
|
-
const { right } = cur;
|
|
1154
|
-
if (!this.isRealNode(right))
|
|
1155
|
-
return cur;
|
|
1156
|
-
return (0, utils_1.makeTrampolineThunk)(() => dfs(right));
|
|
1157
|
-
});
|
|
1158
|
-
return callback(dfs(startNode));
|
|
1159
|
-
}
|
|
1160
|
-
}
|
|
1161
|
-
/**
|
|
1162
|
-
* Time Complexity: O(log n)
|
|
1163
|
-
* Space Complexity: O(log n)
|
|
1164
|
-
*
|
|
1165
|
-
* The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
|
|
1166
|
-
* binary tree.
|
|
1167
|
-
* @param {BinaryTreeNode<K, V>} node - The `getPredecessor` function you provided seems to be attempting to find the
|
|
1168
|
-
* predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
|
|
1169
|
-
* while loop condition that might cause an infinite loop.
|
|
1170
|
-
* @returns The `getPredecessor` function returns the predecessor node of the input `BinaryTreeNode<K, V>` parameter.
|
|
1171
|
-
* If the left child of the input node exists, it traverses to the rightmost node of the left subtree
|
|
1172
|
-
* to find the predecessor. If the left child does not exist, it returns the input node itself.
|
|
1173
|
-
*/
|
|
1174
|
-
getPredecessor(node) {
|
|
1175
|
-
if (this.isRealNode(node.left)) {
|
|
1176
|
-
let predecessor = node.left;
|
|
1177
|
-
while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
|
|
1178
|
-
if (this.isRealNode(predecessor)) {
|
|
1179
|
-
predecessor = predecessor.right;
|
|
1180
|
-
}
|
|
1181
|
-
}
|
|
1182
|
-
return predecessor;
|
|
1183
|
-
}
|
|
1184
|
-
else {
|
|
1185
|
-
return node;
|
|
1186
|
-
}
|
|
1187
|
-
}
|
|
1188
|
-
/**
|
|
1189
|
-
* Time Complexity: O(log n)
|
|
1190
|
-
* Space Complexity: O(log n)
|
|
1191
|
-
*
|
|
1192
|
-
* The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
|
|
1193
|
-
* binary tree.
|
|
1194
|
-
* @param {K | BinaryTreeNode<K, V> | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
|
|
1195
|
-
* type `K`, `BinaryTreeNode<K, V>`, or `null`.
|
|
1196
|
-
* @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
|
|
1197
|
-
* a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
|
|
1198
|
-
* have a right child, the function traverses up the parent nodes until it finds a node that is not
|
|
1199
|
-
* the right child of its parent, and returns that node
|
|
1200
|
-
*/
|
|
1201
|
-
getSuccessor(x) {
|
|
1202
|
-
x = this.ensureNode(x);
|
|
1203
|
-
if (!this.isRealNode(x))
|
|
1204
|
-
return undefined;
|
|
1205
|
-
if (this.isRealNode(x.right)) {
|
|
1206
|
-
return this.getLeftMost(node => node, x.right);
|
|
1207
|
-
}
|
|
1208
|
-
let y = x.parent;
|
|
1209
|
-
while (this.isRealNode(y) && x === y.right) {
|
|
1210
|
-
x = y;
|
|
1211
|
-
y = y.parent;
|
|
1212
|
-
}
|
|
1213
|
-
return y;
|
|
1214
|
-
}
|
|
1215
|
-
/**
|
|
1216
|
-
* Time complexity: O(n)
|
|
1217
|
-
* Space complexity: O(n)
|
|
1218
|
-
*
|
|
1219
|
-
* The function performs a depth-first search on a binary tree structure based on the specified
|
|
1220
|
-
* parameters.
|
|
1221
|
-
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
1222
|
-
* visited during the depth-first search. It should accept a `BinaryTreeNode` as an argument and
|
|
1223
|
-
* return an optional node or null. The default value for this parameter is `_DEFAULT_NODE_CALLBACK`.
|
|
1224
|
-
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` function specifies
|
|
1225
|
-
* the order in which the nodes are visited during a depth-first search traversal. The possible
|
|
1226
|
-
* values for the `pattern` parameter are:
|
|
1227
|
-
* @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `dfs` function is a boolean flag
|
|
1228
|
-
* that determines whether the depth-first search should stop after finding the first matching node
|
|
1229
|
-
* or continue searching for all matching nodes. If `onlyOne` is set to `true`, the search will stop
|
|
1230
|
-
* after finding the first matching node
|
|
1231
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
|
|
1232
|
-
* startNode - The `startNode` parameter in the `dfs` function can be one of the following types:
|
|
1233
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` function
|
|
1234
|
-
* specifies the type of iteration to be performed during the Depth-First Search traversal. It is
|
|
1235
|
-
* used to determine the order in which nodes are visited during the traversal. The possible values
|
|
1236
|
-
* for `iterationType` are typically defined as an enum or a
|
|
1237
|
-
* @param [includeNull=false] - The `includeNull` parameter in the `dfs` function determines whether
|
|
1238
|
-
* null nodes should be included in the depth-first search traversal. If `includeNull` is set to
|
|
1239
|
-
* `true`, null nodes will be included in the traversal process. If it is set to `false`, null nodes
|
|
1240
|
-
* will be skipped
|
|
1241
|
-
* @returns The `dfs` method is returning an array of the return type of the callback function `C`.
|
|
1242
|
-
*/
|
|
1243
|
-
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1244
|
-
startNode = this.ensureNode(startNode);
|
|
1245
|
-
if (!startNode)
|
|
1246
|
-
return [];
|
|
1247
|
-
return this._dfs(callback, pattern, onlyOne, startNode, iterationType, includeNull);
|
|
1248
|
-
}
|
|
1249
|
-
/**
|
|
1250
|
-
* Time complexity: O(n)
|
|
1251
|
-
* Space complexity: O(n)
|
|
1252
|
-
*
|
|
1253
|
-
* The `bfs` function performs a breadth-first search traversal on a binary tree or binary search
|
|
1254
|
-
* tree, executing a specified callback function on each node visited.
|
|
1255
|
-
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
1256
|
-
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
1257
|
-
* that extends the `NodeCallback` type, which takes a parameter of type `BinaryTreeNode<K, V>` or `null`.
|
|
1258
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `bfs`
|
|
1259
|
-
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
1260
|
-
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
1261
|
-
* default value is the root node of the binary
|
|
1262
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `bfs` function
|
|
1263
|
-
* determines the type of iteration to be performed on the binary tree nodes. It can have two
|
|
1264
|
-
* possible values:
|
|
1265
|
-
* @param [includeNull=false] - The `includeNull` parameter in the `bfs` function determines whether
|
|
1266
|
-
* to include `null` values in the breadth-first search traversal of a binary tree. If `includeNull`
|
|
1267
|
-
* is set to `true`, the traversal will include `null` values for nodes that do not have children
|
|
1268
|
-
* (left
|
|
1269
|
-
* @returns The `bfs` function returns an array of values that are the result of applying the
|
|
1270
|
-
* provided callback function to each node in the binary tree in a breadth-first search manner.
|
|
1271
|
-
*/
|
|
1272
|
-
bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1273
|
-
startNode = this.ensureNode(startNode);
|
|
1274
|
-
if (!startNode)
|
|
1275
|
-
return [];
|
|
1276
|
-
const ans = [];
|
|
1277
|
-
if (iterationType === 'RECURSIVE') {
|
|
1278
|
-
const queue = new queue_1.Queue([
|
|
1279
|
-
startNode
|
|
1280
|
-
]);
|
|
1281
|
-
const dfs = (level) => {
|
|
1282
|
-
if (queue.length === 0)
|
|
1283
|
-
return;
|
|
1284
|
-
const current = queue.shift();
|
|
1285
|
-
ans.push(callback(current));
|
|
1286
|
-
if (includeNull) {
|
|
1287
|
-
if (current && this.isRealNodeOrNull(current.left))
|
|
1288
|
-
queue.push(current.left);
|
|
1289
|
-
if (current && this.isRealNodeOrNull(current.right))
|
|
1290
|
-
queue.push(current.right);
|
|
1291
|
-
}
|
|
1292
|
-
else {
|
|
1293
|
-
if (this.isRealNode(current.left))
|
|
1294
|
-
queue.push(current.left);
|
|
1295
|
-
if (this.isRealNode(current.right))
|
|
1296
|
-
queue.push(current.right);
|
|
1297
|
-
}
|
|
1298
|
-
dfs(level + 1);
|
|
1299
|
-
};
|
|
1300
|
-
dfs(0);
|
|
1301
|
-
}
|
|
1302
|
-
else {
|
|
1303
|
-
const queue = new queue_1.Queue([startNode]);
|
|
1304
|
-
while (queue.length > 0) {
|
|
1305
|
-
const levelSize = queue.length;
|
|
1306
|
-
for (let i = 0; i < levelSize; i++) {
|
|
1307
|
-
const current = queue.shift();
|
|
1308
|
-
ans.push(callback(current));
|
|
1309
|
-
if (includeNull) {
|
|
1310
|
-
if (current && this.isRealNodeOrNull(current.left))
|
|
1311
|
-
queue.push(current.left);
|
|
1312
|
-
if (current && this.isRealNodeOrNull(current.right))
|
|
1313
|
-
queue.push(current.right);
|
|
1314
|
-
}
|
|
1315
|
-
else {
|
|
1316
|
-
if (this.isRealNode(current.left))
|
|
1317
|
-
queue.push(current.left);
|
|
1318
|
-
if (this.isRealNode(current.right))
|
|
1319
|
-
queue.push(current.right);
|
|
1320
|
-
}
|
|
1321
|
-
}
|
|
1322
|
-
}
|
|
1323
|
-
}
|
|
1324
|
-
return ans;
|
|
1325
|
-
}
|
|
1326
|
-
/**
|
|
1327
|
-
* Time complexity: O(n)
|
|
1328
|
-
* Space complexity: O(n)
|
|
1329
|
-
*
|
|
1330
|
-
* The `leaves` function in TypeScript returns an array of values from leaf nodes in a binary tree
|
|
1331
|
-
* structure based on a specified callback and iteration type.
|
|
1332
|
-
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
|
|
1333
|
-
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
1334
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `leaves`
|
|
1335
|
-
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
1336
|
-
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
1337
|
-
* explicitly provided, the default value
|
|
1338
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
|
|
1339
|
-
* specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
|
|
1340
|
-
* can have two possible values:
|
|
1341
|
-
* @returns The `leaves` method returns an array of values that are the result of applying the
|
|
1342
|
-
* provided callback function to each leaf node in the binary tree.
|
|
1343
|
-
*/
|
|
1344
|
-
leaves(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
1345
|
-
startNode = this.ensureNode(startNode);
|
|
1346
|
-
const leaves = [];
|
|
1347
|
-
if (!this.isRealNode(startNode))
|
|
1348
|
-
return [];
|
|
1349
|
-
if (iterationType === 'RECURSIVE') {
|
|
1350
|
-
const dfs = (cur) => {
|
|
1351
|
-
if (this.isLeaf(cur)) {
|
|
1352
|
-
leaves.push(callback(cur));
|
|
1353
|
-
}
|
|
1354
|
-
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
1355
|
-
return;
|
|
1356
|
-
if (this.isRealNode(cur.left))
|
|
1357
|
-
dfs(cur.left);
|
|
1358
|
-
if (this.isRealNode(cur.right))
|
|
1359
|
-
dfs(cur.right);
|
|
1360
|
-
};
|
|
1361
|
-
dfs(startNode);
|
|
1362
|
-
}
|
|
1363
|
-
else {
|
|
1364
|
-
const queue = new queue_1.Queue([startNode]);
|
|
1365
|
-
while (queue.length > 0) {
|
|
1366
|
-
const cur = queue.shift();
|
|
1367
|
-
if (this.isRealNode(cur)) {
|
|
1368
|
-
if (this.isLeaf(cur)) {
|
|
1369
|
-
leaves.push(callback(cur));
|
|
1370
|
-
}
|
|
1371
|
-
if (this.isRealNode(cur.left))
|
|
1372
|
-
queue.push(cur.left);
|
|
1373
|
-
if (this.isRealNode(cur.right))
|
|
1374
|
-
queue.push(cur.right);
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
}
|
|
1378
|
-
return leaves;
|
|
1379
|
-
}
|
|
1380
|
-
/**
|
|
1381
|
-
* Time complexity: O(n)
|
|
1382
|
-
* Space complexity: O(n)
|
|
1383
|
-
*
|
|
1384
|
-
* The `listLevels` function in TypeScript generates a list of nodes at each level of a binary tree,
|
|
1385
|
-
* using either recursive or iterative traversal based on the specified iteration type.
|
|
1386
|
-
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
1387
|
-
* the binary tree during the traversal. It is used to process each node and determine what
|
|
1388
|
-
* information to include in the output for each level of the tree.
|
|
1389
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1390
|
-
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
1391
|
-
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
1392
|
-
* root of the binary tree.
|
|
1393
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `listLevels` function
|
|
1394
|
-
* determines the type of iteration to be performed on the binary tree nodes. It can have two
|
|
1395
|
-
* possible values:
|
|
1396
|
-
* @param [includeNull=false] - The `includeNull` parameter in the `listLevels` method determines
|
|
1397
|
-
* whether or not to include null nodes in the traversal of the binary tree. If `includeNull` is set
|
|
1398
|
-
* to `true`, the traversal will include null nodes in the levels of the tree. If set to `false`,
|
|
1399
|
-
* null
|
|
1400
|
-
* @returns The `listLevels` method returns an array of arrays, where each inner array represents a
|
|
1401
|
-
* level in a binary tree. Each inner array contains the return value of the provided callback
|
|
1402
|
-
* function applied to the nodes at that level.
|
|
1403
|
-
*/
|
|
1404
|
-
listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1405
|
-
startNode = this.ensureNode(startNode);
|
|
1406
|
-
const levelsNodes = [];
|
|
1407
|
-
if (!startNode)
|
|
1408
|
-
return levelsNodes;
|
|
1409
|
-
if (iterationType === 'RECURSIVE') {
|
|
1410
|
-
const _recursive = (node, level) => {
|
|
1411
|
-
if (!levelsNodes[level])
|
|
1412
|
-
levelsNodes[level] = [];
|
|
1413
|
-
levelsNodes[level].push(callback(node));
|
|
1414
|
-
if (includeNull) {
|
|
1415
|
-
if (node && this.isRealNodeOrNull(node.left))
|
|
1416
|
-
_recursive(node.left, level + 1);
|
|
1417
|
-
if (node && this.isRealNodeOrNull(node.right))
|
|
1418
|
-
_recursive(node.right, level + 1);
|
|
1419
|
-
}
|
|
1420
|
-
else {
|
|
1421
|
-
if (node && node.left)
|
|
1422
|
-
_recursive(node.left, level + 1);
|
|
1423
|
-
if (node && node.right)
|
|
1424
|
-
_recursive(node.right, level + 1);
|
|
1425
|
-
}
|
|
1426
|
-
};
|
|
1427
|
-
_recursive(startNode, 0);
|
|
1428
|
-
}
|
|
1429
|
-
else {
|
|
1430
|
-
const stack = [[startNode, 0]];
|
|
1431
|
-
while (stack.length > 0) {
|
|
1432
|
-
const head = stack.pop();
|
|
1433
|
-
const [node, level] = head;
|
|
1434
|
-
if (!levelsNodes[level])
|
|
1435
|
-
levelsNodes[level] = [];
|
|
1436
|
-
levelsNodes[level].push(callback(node));
|
|
1437
|
-
if (includeNull) {
|
|
1438
|
-
if (node && this.isRealNodeOrNull(node.right))
|
|
1439
|
-
stack.push([node.right, level + 1]);
|
|
1440
|
-
if (node && this.isRealNodeOrNull(node.left))
|
|
1441
|
-
stack.push([node.left, level + 1]);
|
|
1442
|
-
}
|
|
1443
|
-
else {
|
|
1444
|
-
if (node && node.right)
|
|
1445
|
-
stack.push([node.right, level + 1]);
|
|
1446
|
-
if (node && node.left)
|
|
1447
|
-
stack.push([node.left, level + 1]);
|
|
1448
|
-
}
|
|
1449
|
-
}
|
|
1450
|
-
}
|
|
1451
|
-
return levelsNodes;
|
|
1452
|
-
}
|
|
1453
|
-
/**
|
|
1454
|
-
* Time complexity: O(n)
|
|
1455
|
-
* Space complexity: O(n)
|
|
1456
|
-
*
|
|
1457
|
-
* The `morris` function in TypeScript performs a Depth-First Search traversal on a binary tree using
|
|
1458
|
-
* Morris Traversal algorithm with different order patterns.
|
|
1459
|
-
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
1460
|
-
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
1461
|
-
* `NodeCallback<BinaryTreeNode<K, V> | null>` type. The default value for `callback` is `this._DEFAULT
|
|
1462
|
-
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
1463
|
-
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
1464
|
-
* values for the `pattern` parameter are:
|
|
1465
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `morris`
|
|
1466
|
-
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
1467
|
-
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
1468
|
-
* key, a node, an entry, or a reference
|
|
1469
|
-
* @returns The `morris` function is returning an array of values that are the result of applying the
|
|
1470
|
-
* provided callback function to each node in the binary tree in the specified order pattern (IN,
|
|
1471
|
-
* PRE, or POST).
|
|
1472
|
-
*/
|
|
1473
|
-
morris(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root) {
|
|
1474
|
-
startNode = this.ensureNode(startNode);
|
|
1475
|
-
if (!startNode)
|
|
1476
|
-
return [];
|
|
1477
|
-
const ans = [];
|
|
1478
|
-
let cur = startNode;
|
|
1479
|
-
const _reverseEdge = (node) => {
|
|
1480
|
-
let pre = null;
|
|
1481
|
-
let next = null;
|
|
1482
|
-
while (node) {
|
|
1483
|
-
next = node.right;
|
|
1484
|
-
node.right = pre;
|
|
1485
|
-
pre = node;
|
|
1486
|
-
node = next;
|
|
1487
|
-
}
|
|
1488
|
-
return pre;
|
|
1489
|
-
};
|
|
1490
|
-
const _printEdge = (node) => {
|
|
1491
|
-
const tail = _reverseEdge(node);
|
|
1492
|
-
let cur = tail;
|
|
1493
|
-
while (cur) {
|
|
1494
|
-
ans.push(callback(cur));
|
|
1495
|
-
cur = cur.right;
|
|
1496
|
-
}
|
|
1497
|
-
_reverseEdge(tail);
|
|
1498
|
-
};
|
|
1499
|
-
switch (pattern) {
|
|
1500
|
-
case 'IN':
|
|
1501
|
-
while (cur) {
|
|
1502
|
-
if (cur.left) {
|
|
1503
|
-
const predecessor = this.getPredecessor(cur);
|
|
1504
|
-
if (!predecessor.right) {
|
|
1505
|
-
predecessor.right = cur;
|
|
1506
|
-
cur = cur.left;
|
|
1507
|
-
continue;
|
|
1508
|
-
}
|
|
1509
|
-
else {
|
|
1510
|
-
predecessor.right = null;
|
|
1511
|
-
}
|
|
1512
|
-
}
|
|
1513
|
-
ans.push(callback(cur));
|
|
1514
|
-
cur = cur.right;
|
|
1515
|
-
}
|
|
1516
|
-
break;
|
|
1517
|
-
case 'PRE':
|
|
1518
|
-
while (cur) {
|
|
1519
|
-
if (cur.left) {
|
|
1520
|
-
const predecessor = this.getPredecessor(cur);
|
|
1521
|
-
if (!predecessor.right) {
|
|
1522
|
-
predecessor.right = cur;
|
|
1523
|
-
ans.push(callback(cur));
|
|
1524
|
-
cur = cur.left;
|
|
1525
|
-
continue;
|
|
1526
|
-
}
|
|
1527
|
-
else {
|
|
1528
|
-
predecessor.right = null;
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
else {
|
|
1532
|
-
ans.push(callback(cur));
|
|
1533
|
-
}
|
|
1534
|
-
cur = cur.right;
|
|
1535
|
-
}
|
|
1536
|
-
break;
|
|
1537
|
-
case 'POST':
|
|
1538
|
-
while (cur) {
|
|
1539
|
-
if (cur.left) {
|
|
1540
|
-
const predecessor = this.getPredecessor(cur);
|
|
1541
|
-
if (predecessor.right === null) {
|
|
1542
|
-
predecessor.right = cur;
|
|
1543
|
-
cur = cur.left;
|
|
1544
|
-
continue;
|
|
1545
|
-
}
|
|
1546
|
-
else {
|
|
1547
|
-
predecessor.right = null;
|
|
1548
|
-
_printEdge(cur.left);
|
|
1549
|
-
}
|
|
1550
|
-
}
|
|
1551
|
-
cur = cur.right;
|
|
1552
|
-
}
|
|
1553
|
-
_printEdge(startNode);
|
|
1554
|
-
break;
|
|
1555
|
-
}
|
|
1556
|
-
return ans;
|
|
1557
|
-
}
|
|
1558
|
-
/**
|
|
1559
|
-
* Time complexity: O(n)
|
|
1560
|
-
* Space complexity: O(n)
|
|
1561
|
-
*
|
|
1562
|
-
* The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first
|
|
1563
|
-
* search.
|
|
1564
|
-
* @returns The `clone()` method is returning a cloned copy of the tree with the same structure and
|
|
1565
|
-
* values as the original tree. The method creates a new tree, iterates over the nodes of the
|
|
1566
|
-
* original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
|
|
1567
|
-
* the original tree is null, a null node is added to the cloned tree. If a node
|
|
1568
|
-
*/
|
|
1569
|
-
clone() {
|
|
1570
|
-
const cloned = this.createTree();
|
|
1571
|
-
this._clone(cloned);
|
|
1572
|
-
return cloned;
|
|
1573
|
-
}
|
|
1574
|
-
/**
|
|
1575
|
-
* Time Complexity: O(n)
|
|
1576
|
-
* Space Complexity: O(n)
|
|
1577
|
-
*
|
|
1578
|
-
* The `filter` function iterates over key-value pairs in a tree data structure and creates a new
|
|
1579
|
-
* tree with elements that satisfy a given predicate.
|
|
1580
|
-
* @param predicate - The `predicate` parameter in the `filter` method is a function that will be
|
|
1581
|
-
* called with four arguments: the `value` of the current entry, the `key` of the current entry, the
|
|
1582
|
-
* `index` of the current entry in the iteration, and the reference to the tree itself (`
|
|
1583
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `filter` method allows you to specify the
|
|
1584
|
-
* value of `this` that should be used when executing the `predicate` function. This is useful when
|
|
1585
|
-
* the `predicate` function relies on the context of a specific object or value. By providing a
|
|
1586
|
-
* `thisArg
|
|
1587
|
-
* @returns The `filter` method is returning a new tree that contains entries that pass the provided
|
|
1588
|
-
* predicate function.
|
|
1589
|
-
*/
|
|
1590
|
-
filter(predicate, thisArg) {
|
|
1591
|
-
const newTree = this.createTree();
|
|
1592
|
-
let index = 0;
|
|
1593
|
-
for (const [key, value] of this) {
|
|
1594
|
-
if (predicate.call(thisArg, key, value, index++, this)) {
|
|
1595
|
-
newTree.add([key, value]);
|
|
1596
|
-
}
|
|
1597
|
-
}
|
|
1598
|
-
return newTree;
|
|
1599
|
-
}
|
|
1600
|
-
/**
|
|
1601
|
-
* Time Complexity: O(n)
|
|
1602
|
-
* Space Complexity: O(n)
|
|
1603
|
-
*
|
|
1604
|
-
* The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
|
|
1605
|
-
* entry in the original BinaryTree.
|
|
1606
|
-
* @param callback - A function that will be called for each entry in the current binary tree. It
|
|
1607
|
-
* takes the key, value (which can be undefined), and an array containing the mapped key and value as
|
|
1608
|
-
* arguments.
|
|
1609
|
-
* @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
|
|
1610
|
-
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
|
|
1611
|
-
* tree being created during the mapping process. These options could include things like custom
|
|
1612
|
-
* comparators, initial
|
|
1613
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
|
|
1614
|
-
* of `this` when executing the `callback` function. It allows you to set the context (value of
|
|
1615
|
-
* `this`) within the callback function. If `thisArg` is provided, it will be passed
|
|
1616
|
-
* @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
|
|
1617
|
-
* the result of applying the provided `callback` function to each entry in the original tree.
|
|
1618
|
-
*/
|
|
1619
|
-
map(callback, options, thisArg) {
|
|
1620
|
-
const newTree = new BinaryTree([], options);
|
|
1621
|
-
let index = 0;
|
|
1622
|
-
for (const [key, value] of this) {
|
|
1623
|
-
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
1624
|
-
}
|
|
1625
|
-
return newTree;
|
|
1626
|
-
}
|
|
1627
|
-
/**
|
|
1628
|
-
* Time Complexity: O(n)
|
|
1629
|
-
* Space Complexity: O(n)
|
|
1630
|
-
*
|
|
1631
|
-
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
1632
|
-
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
1633
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1634
|
-
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
1635
|
-
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
1636
|
-
* the default is set to the root
|
|
1637
|
-
* @param {BinaryTreePrintOptions} [options] - The `options` parameter in the `toVisual` method is an
|
|
1638
|
-
* object that contains the following properties:
|
|
1639
|
-
* @returns The `override toVisual` method returns a string that represents the visual display of the
|
|
1640
|
-
* binary tree based on the provided options for showing undefined, null, and Red-Black NIL nodes.
|
|
1641
|
-
* The method constructs the visual representation by calling the `_displayAux` method and appending
|
|
1642
|
-
* the lines to the output string. The final output string contains the visual representation of the
|
|
1643
|
-
* binary tree with the specified options.
|
|
1644
|
-
*/
|
|
1645
|
-
toVisual(startNode = this._root, options) {
|
|
1646
|
-
const opts = Object.assign({ isShowUndefined: false, isShowNull: true, isShowRedBlackNIL: false }, options);
|
|
1647
|
-
startNode = this.ensureNode(startNode);
|
|
1648
|
-
let output = '';
|
|
1649
|
-
if (!startNode)
|
|
1650
|
-
return output;
|
|
1651
|
-
if (opts.isShowUndefined)
|
|
1652
|
-
output += `U for undefined\n`;
|
|
1653
|
-
if (opts.isShowNull)
|
|
1654
|
-
output += `N for null\n`;
|
|
1655
|
-
if (opts.isShowRedBlackNIL)
|
|
1656
|
-
output += `S for Sentinel Node(NIL)\n`;
|
|
1657
|
-
const display = (root) => {
|
|
1658
|
-
const [lines] = this._displayAux(root, opts);
|
|
1659
|
-
let paragraph = '';
|
|
1660
|
-
for (const line of lines) {
|
|
1661
|
-
paragraph += line + '\n';
|
|
1662
|
-
}
|
|
1663
|
-
output += paragraph;
|
|
1664
|
-
};
|
|
1665
|
-
display(startNode);
|
|
1666
|
-
return output;
|
|
1667
|
-
}
|
|
1668
|
-
/**
|
|
1669
|
-
* Time Complexity: O(n)
|
|
1670
|
-
* Space Complexity: O(n)
|
|
1671
|
-
*
|
|
1672
|
-
* The function `print` in TypeScript overrides the default print behavior to log a visual
|
|
1673
|
-
* representation of the binary tree to the console.
|
|
1674
|
-
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is used to specify the
|
|
1675
|
-
* printing options for the binary tree. It is an optional parameter that allows you to customize how
|
|
1676
|
-
* the binary tree is printed, such as choosing between different traversal orders or formatting
|
|
1677
|
-
* options.
|
|
1678
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
|
|
1679
|
-
* `override print` method is used to specify the starting point for printing the binary tree. It can
|
|
1680
|
-
* be either a key, a node, an entry, or the root of the tree. If no specific starting point is
|
|
1681
|
-
* provided, the default value is set to
|
|
1682
|
-
*/
|
|
1683
|
-
print(options, startNode = this._root) {
|
|
1684
|
-
console.log(this.toVisual(startNode, options));
|
|
1685
|
-
}
|
|
1686
|
-
_clone(cloned) {
|
|
1687
|
-
this.bfs(node => {
|
|
1688
|
-
if (node === null)
|
|
1689
|
-
cloned.add(null);
|
|
1690
|
-
else {
|
|
1691
|
-
if (this._isMapMode)
|
|
1692
|
-
cloned.add([node.key, this._store.get(node.key)]);
|
|
1693
|
-
else
|
|
1694
|
-
cloned.add([node.key, node.value]);
|
|
1695
|
-
}
|
|
1696
|
-
}, this._root, this.iterationType, true);
|
|
1697
|
-
if (this._isMapMode)
|
|
1698
|
-
cloned._store = this._store;
|
|
1699
|
-
}
|
|
1700
|
-
/**
|
|
1701
|
-
* Time Complexity: O(1)
|
|
1702
|
-
* Space Complexity: O(1)
|
|
1703
|
-
*
|
|
1704
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
1705
|
-
* or returns null.
|
|
1706
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The
|
|
1707
|
-
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which
|
|
1708
|
-
* can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. This parameter represents either a key, a
|
|
1709
|
-
* node, an entry
|
|
1710
|
-
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
1711
|
-
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
1712
|
-
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
1713
|
-
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
1714
|
-
* (`BinaryTreeNode<K, V> | null | undefined`) based on the input parameters provided. The function checks the type of the
|
|
1715
|
-
* input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null
|
|
1716
|
-
* value.
|
|
1717
|
-
*/
|
|
1718
|
-
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
1719
|
-
if (keyNodeOrEntry === undefined)
|
|
1720
|
-
return [undefined, undefined];
|
|
1721
|
-
if (keyNodeOrEntry === null)
|
|
1722
|
-
return [null, undefined];
|
|
1723
|
-
if (this.isNode(keyNodeOrEntry))
|
|
1724
|
-
return [keyNodeOrEntry, value];
|
|
1725
|
-
if (this.isEntry(keyNodeOrEntry)) {
|
|
1726
|
-
const [key, entryValue] = keyNodeOrEntry;
|
|
1727
|
-
if (key === undefined)
|
|
1728
|
-
return [undefined, undefined];
|
|
1729
|
-
else if (key === null)
|
|
1730
|
-
return [null, undefined];
|
|
1731
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
1732
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
1733
|
-
}
|
|
1734
|
-
return [this.createNode(keyNodeOrEntry, value), value];
|
|
1735
|
-
}
|
|
1736
|
-
/**
|
|
1737
|
-
* Time complexity: O(n)
|
|
1738
|
-
* Space complexity: O(n)
|
|
1739
|
-
*
|
|
1740
|
-
* The `_dfs` function performs a depth-first search traversal on a binary tree, with customizable
|
|
1741
|
-
* options for traversal order and node processing.
|
|
1742
|
-
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
1743
|
-
* called on each node visited during the depth-first search traversal. It is a generic type `C` that
|
|
1744
|
-
* extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback`
|
|
1745
|
-
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
1746
|
-
* order in which the nodes are visited during a depth-first search traversal. It can have one of the
|
|
1747
|
-
* following values:
|
|
1748
|
-
* @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `_dfs` method is a boolean flag
|
|
1749
|
-
* that determines whether the traversal should stop after processing a single node. If `onlyOne` is
|
|
1750
|
-
* set to `true`, the traversal will return as soon as a single node is processed. If it is set to
|
|
1751
|
-
* `false
|
|
1752
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
|
|
1753
|
-
* startNode - The `startNode` parameter in the `_dfs` method is used to specify the starting node
|
|
1754
|
-
* for the depth-first search traversal. It can be provided in different forms:
|
|
1755
|
-
* @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
|
|
1756
|
-
* specifies whether the traversal should be done recursively or iteratively. It can have two
|
|
1757
|
-
* possible values:
|
|
1758
|
-
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method determines whether
|
|
1759
|
-
* null nodes should be included in the traversal process. If `includeNull` is set to `true`, the
|
|
1760
|
-
* method will consider null nodes as valid nodes to visit or process. If `includeNull` is set to
|
|
1761
|
-
* `false`,
|
|
1762
|
-
* @param shouldVisitLeft - The `shouldVisitLeft` parameter in the `_dfs` method is a function that
|
|
1763
|
-
* determines whether the left child of a node should be visited during the Depth-First Search
|
|
1764
|
-
* traversal. By default, it checks if the node is not null or undefined before visiting the left
|
|
1765
|
-
* child. You can customize this behavior
|
|
1766
|
-
* @param shouldVisitRight - The `shouldVisitRight` parameter in the `_dfs` method is a function that
|
|
1767
|
-
* determines whether to visit the right child node of the current node during a depth-first search
|
|
1768
|
-
* traversal. The default implementation of this function checks if the node is not null or undefined
|
|
1769
|
-
* before deciding to visit it.
|
|
1770
|
-
* @param shouldVisitRoot - The `shouldVisitRoot` parameter in the `_dfs` method is a function that
|
|
1771
|
-
* determines whether a given node should be visited during the depth-first search traversal. The
|
|
1772
|
-
* function takes a node as an argument and returns a boolean value indicating whether the node
|
|
1773
|
-
* should be visited.
|
|
1774
|
-
* @param shouldProcessRoot - The `shouldProcessRoot` parameter in the `_dfs` method is a function
|
|
1775
|
-
* that determines whether the root node should be processed during the Depth-First Search traversal.
|
|
1776
|
-
* It takes a node (BinaryTreeNode<K, V> | null | undefined) as input and returns a boolean value. If
|
|
1777
|
-
* the function
|
|
1778
|
-
* @returns The `_dfs` method returns an array of the return type of the provided callback function
|
|
1779
|
-
* `C`.
|
|
1780
|
-
*/
|
|
1781
|
-
_dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
|
|
1782
|
-
if (includeNull)
|
|
1783
|
-
return this.isRealNodeOrNull(node);
|
|
1784
|
-
return this.isRealNode(node);
|
|
1785
|
-
}, shouldProcessRoot = node => this.isRealNodeOrNull(node)) {
|
|
1786
|
-
startNode = this.ensureNode(startNode);
|
|
1787
|
-
if (!startNode)
|
|
1788
|
-
return [];
|
|
1789
|
-
const ans = [];
|
|
1790
|
-
if (iterationType === 'RECURSIVE') {
|
|
1791
|
-
const dfs = (node) => {
|
|
1792
|
-
if (!shouldVisitRoot(node))
|
|
1793
|
-
return;
|
|
1794
|
-
const visitLeft = () => {
|
|
1795
|
-
if (shouldVisitLeft(node) && (node === null || node === void 0 ? void 0 : node.left) !== undefined)
|
|
1796
|
-
dfs(node === null || node === void 0 ? void 0 : node.left);
|
|
1797
|
-
};
|
|
1798
|
-
const visitRight = () => {
|
|
1799
|
-
if (shouldVisitRight(node) && (node === null || node === void 0 ? void 0 : node.right) !== undefined)
|
|
1800
|
-
dfs(node === null || node === void 0 ? void 0 : node.right);
|
|
1801
|
-
};
|
|
1802
|
-
switch (pattern) {
|
|
1803
|
-
case 'IN':
|
|
1804
|
-
visitLeft();
|
|
1805
|
-
if (shouldProcessRoot(node)) {
|
|
1806
|
-
ans.push(callback(node));
|
|
1807
|
-
if (onlyOne)
|
|
1808
|
-
return;
|
|
1809
|
-
}
|
|
1810
|
-
visitRight();
|
|
1811
|
-
break;
|
|
1812
|
-
case 'PRE':
|
|
1813
|
-
if (shouldProcessRoot(node)) {
|
|
1814
|
-
ans.push(callback(node));
|
|
1815
|
-
if (onlyOne)
|
|
1816
|
-
return;
|
|
1817
|
-
}
|
|
1818
|
-
visitLeft();
|
|
1819
|
-
visitRight();
|
|
1820
|
-
break;
|
|
1821
|
-
case 'POST':
|
|
1822
|
-
visitLeft();
|
|
1823
|
-
visitRight();
|
|
1824
|
-
if (shouldProcessRoot(node)) {
|
|
1825
|
-
ans.push(callback(node));
|
|
1826
|
-
if (onlyOne)
|
|
1827
|
-
return;
|
|
1828
|
-
}
|
|
1829
|
-
break;
|
|
1830
|
-
}
|
|
1831
|
-
};
|
|
1832
|
-
dfs(startNode);
|
|
1833
|
-
}
|
|
1834
|
-
else {
|
|
1835
|
-
const stack = [{ opt: common_1.DFSOperation.VISIT, node: startNode }];
|
|
1836
|
-
const pushLeft = (cur) => {
|
|
1837
|
-
var _a;
|
|
1838
|
-
if (shouldVisitLeft(cur.node))
|
|
1839
|
-
stack.push({ opt: common_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.left });
|
|
1840
|
-
};
|
|
1841
|
-
const pushRight = (cur) => {
|
|
1842
|
-
var _a;
|
|
1843
|
-
if (shouldVisitRight(cur.node))
|
|
1844
|
-
stack.push({ opt: common_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.right });
|
|
1845
|
-
};
|
|
1846
|
-
const pushRoot = (cur) => {
|
|
1847
|
-
if (shouldVisitRoot(cur.node))
|
|
1848
|
-
stack.push({ opt: common_1.DFSOperation.PROCESS, node: cur.node });
|
|
1849
|
-
};
|
|
1850
|
-
while (stack.length > 0) {
|
|
1851
|
-
const cur = stack.pop();
|
|
1852
|
-
if (cur === undefined)
|
|
1853
|
-
continue;
|
|
1854
|
-
if (!shouldVisitRoot(cur.node))
|
|
1855
|
-
continue;
|
|
1856
|
-
if (cur.opt === common_1.DFSOperation.PROCESS) {
|
|
1857
|
-
if (shouldProcessRoot(cur.node) && cur.node !== undefined) {
|
|
1858
|
-
ans.push(callback(cur.node));
|
|
1859
|
-
if (onlyOne)
|
|
1860
|
-
return ans;
|
|
1861
|
-
}
|
|
1862
|
-
}
|
|
1863
|
-
else {
|
|
1864
|
-
switch (pattern) {
|
|
1865
|
-
case 'IN':
|
|
1866
|
-
pushRight(cur);
|
|
1867
|
-
pushRoot(cur);
|
|
1868
|
-
pushLeft(cur);
|
|
1869
|
-
break;
|
|
1870
|
-
case 'PRE':
|
|
1871
|
-
pushRight(cur);
|
|
1872
|
-
pushLeft(cur);
|
|
1873
|
-
pushRoot(cur);
|
|
1874
|
-
break;
|
|
1875
|
-
case 'POST':
|
|
1876
|
-
pushRoot(cur);
|
|
1877
|
-
pushRight(cur);
|
|
1878
|
-
pushLeft(cur);
|
|
1879
|
-
break;
|
|
1880
|
-
}
|
|
1881
|
-
}
|
|
1882
|
-
}
|
|
1883
|
-
}
|
|
1884
|
-
return ans;
|
|
1885
|
-
}
|
|
1886
|
-
/**
|
|
1887
|
-
* Time Complexity: O(1)
|
|
1888
|
-
* Space Complexity: O(1)
|
|
1889
|
-
*
|
|
1890
|
-
* The function `_getIterator` returns an iterable iterator for a binary tree data structure, either
|
|
1891
|
-
* using an iterative approach or a recursive approach based on the specified iteration type.
|
|
1892
|
-
* @param node - The `node` parameter in the `_getIterator` method represents the current node being
|
|
1893
|
-
* processed during iteration. It is initially set to the root node of the data structure (or the
|
|
1894
|
-
* node passed as an argument), and then it is traversed through the data structure based on the
|
|
1895
|
-
* iteration type specified (`ITER
|
|
1896
|
-
* @returns The `_getIterator` method returns an IterableIterator containing key-value pairs of nodes
|
|
1897
|
-
* in a binary tree structure. The method uses an iterative approach to traverse the tree based on
|
|
1898
|
-
* the `iterationType` property. If the `iterationType` is set to 'ITERATIVE', the method uses a
|
|
1899
|
-
* stack to perform an in-order traversal of the tree. If the `iterationType` is not 'ITERATIVE
|
|
1900
|
-
*/
|
|
1901
|
-
*_getIterator(node = this._root) {
|
|
1902
|
-
if (!node)
|
|
1903
|
-
return;
|
|
1904
|
-
if (this.iterationType === 'ITERATIVE') {
|
|
1905
|
-
const stack = [];
|
|
1906
|
-
let current = node;
|
|
1907
|
-
while (current || stack.length > 0) {
|
|
1908
|
-
while (this.isRealNode(current)) {
|
|
1909
|
-
stack.push(current);
|
|
1910
|
-
current = current.left;
|
|
1911
|
-
}
|
|
1912
|
-
current = stack.pop();
|
|
1913
|
-
if (this.isRealNode(current)) {
|
|
1914
|
-
if (this._isMapMode)
|
|
1915
|
-
yield [current.key, this._store.get(current.key)];
|
|
1916
|
-
else
|
|
1917
|
-
yield [current.key, current.value];
|
|
1918
|
-
current = current.right;
|
|
1919
|
-
}
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
else {
|
|
1923
|
-
if (node.left && this.isRealNode(node)) {
|
|
1924
|
-
yield* this[Symbol.iterator](node.left);
|
|
1925
|
-
}
|
|
1926
|
-
if (this._isMapMode)
|
|
1927
|
-
yield [node.key, this._store.get(node.key)];
|
|
1928
|
-
else
|
|
1929
|
-
yield [node.key, node.value];
|
|
1930
|
-
if (node.right && this.isRealNode(node)) {
|
|
1931
|
-
yield* this[Symbol.iterator](node.right);
|
|
1932
|
-
}
|
|
1933
|
-
}
|
|
1934
|
-
}
|
|
1935
|
-
/**
|
|
1936
|
-
* Time Complexity: O(n)
|
|
1937
|
-
* Space Complexity: O(n)
|
|
1938
|
-
*
|
|
1939
|
-
* The function `_displayAux` in TypeScript is responsible for generating the display layout of nodes
|
|
1940
|
-
* in a binary tree based on specified options.
|
|
1941
|
-
* @param node - The `node` parameter in the `_displayAux` function represents a node in a binary
|
|
1942
|
-
* tree. It can be either a valid node containing a key or a special type of node like null,
|
|
1943
|
-
* undefined, or a Red-Black tree NIL node. The function checks the type of the node and its
|
|
1944
|
-
* @param {BinaryTreePrintOptions} options - The `options` parameter in the `_displayAux` function
|
|
1945
|
-
* contains the following properties:
|
|
1946
|
-
* @returns The `_displayAux` function returns a `NodeDisplayLayout`, which is an array containing
|
|
1947
|
-
* information about how to display a node in a binary tree. The `NodeDisplayLayout` consists of four
|
|
1948
|
-
* elements:
|
|
1949
|
-
*/
|
|
1950
|
-
_displayAux(node, options) {
|
|
1951
|
-
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1952
|
-
const emptyDisplayLayout = [['─'], 1, 0, 0];
|
|
1953
|
-
// Check if node is null or undefined or key is NaN
|
|
1954
|
-
if (node === null && !isShowNull) {
|
|
1955
|
-
return emptyDisplayLayout;
|
|
1956
|
-
}
|
|
1957
|
-
else if (node === undefined && !isShowUndefined) {
|
|
1958
|
-
return emptyDisplayLayout;
|
|
1959
|
-
}
|
|
1960
|
-
else if (this.isNIL(node) && !isShowRedBlackNIL) {
|
|
1961
|
-
return emptyDisplayLayout;
|
|
1962
|
-
}
|
|
1963
|
-
else if (node !== null && node !== undefined) {
|
|
1964
|
-
// Display logic of normal nodes
|
|
1965
|
-
const key = node.key, line = this.isNIL(node) ? 'S' : String(key), width = line.length;
|
|
1966
|
-
return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
|
|
1967
|
-
}
|
|
1968
|
-
else {
|
|
1969
|
-
// For cases where none of the conditions are met, null, undefined, and NaN nodes are not displayed
|
|
1970
|
-
const line = node === undefined ? 'U' : 'N', width = line.length;
|
|
1971
|
-
return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);
|
|
1972
|
-
}
|
|
1973
|
-
function _buildNodeDisplay(line, width, left, right) {
|
|
1974
|
-
const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
|
|
1975
|
-
const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
|
|
1976
|
-
const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1)) +
|
|
1977
|
-
'_'.repeat(Math.max(0, leftWidth - leftMiddle - 1)) +
|
|
1978
|
-
line +
|
|
1979
|
-
'_'.repeat(Math.max(0, rightMiddle)) +
|
|
1980
|
-
' '.repeat(Math.max(0, rightWidth - rightMiddle));
|
|
1981
|
-
const secondLine = (leftHeight > 0
|
|
1982
|
-
? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1)
|
|
1983
|
-
: ' '.repeat(leftWidth)) +
|
|
1984
|
-
' '.repeat(width) +
|
|
1985
|
-
(rightHeight > 0
|
|
1986
|
-
? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1)
|
|
1987
|
-
: ' '.repeat(rightWidth));
|
|
1988
|
-
const mergedLines = [firstLine, secondLine];
|
|
1989
|
-
for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
|
|
1990
|
-
const leftLine = i < leftHeight ? leftLines[i] : ' '.repeat(leftWidth);
|
|
1991
|
-
const rightLine = i < rightHeight ? rightLines[i] : ' '.repeat(rightWidth);
|
|
1992
|
-
mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
|
|
1993
|
-
}
|
|
1994
|
-
return [
|
|
1995
|
-
mergedLines,
|
|
1996
|
-
leftWidth + width + rightWidth,
|
|
1997
|
-
Math.max(leftHeight, rightHeight) + 2,
|
|
1998
|
-
leftWidth + Math.floor(width / 2)
|
|
1999
|
-
];
|
|
2000
|
-
}
|
|
2001
|
-
}
|
|
2002
|
-
/**
|
|
2003
|
-
* Time Complexity: O(1)
|
|
2004
|
-
* Space Complexity: O(1)
|
|
2005
|
-
*
|
|
2006
|
-
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
2007
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } srcNode - The `srcNode` parameter in the
|
|
2008
|
-
* `_swapProperties` method can be either a BTNRep object containing key and value
|
|
2009
|
-
* properties, or it can be of type R.
|
|
2010
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } destNode - The `destNode` parameter in the
|
|
2011
|
-
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
2012
|
-
* the `srcNode`. It can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. The method ensures that
|
|
2013
|
-
* both `srcNode
|
|
2014
|
-
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
2015
|
-
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
2016
|
-
*/
|
|
2017
|
-
_swapProperties(srcNode, destNode) {
|
|
2018
|
-
srcNode = this.ensureNode(srcNode);
|
|
2019
|
-
destNode = this.ensureNode(destNode);
|
|
2020
|
-
if (srcNode && destNode) {
|
|
2021
|
-
const { key, value } = destNode;
|
|
2022
|
-
const tempNode = this.createNode(key, value);
|
|
2023
|
-
if (tempNode) {
|
|
2024
|
-
destNode.key = srcNode.key;
|
|
2025
|
-
if (!this._isMapMode)
|
|
2026
|
-
destNode.value = srcNode.value;
|
|
2027
|
-
srcNode.key = tempNode.key;
|
|
2028
|
-
if (!this._isMapMode)
|
|
2029
|
-
srcNode.value = tempNode.value;
|
|
2030
|
-
}
|
|
2031
|
-
return destNode;
|
|
2032
|
-
}
|
|
2033
|
-
return undefined;
|
|
2034
|
-
}
|
|
2035
|
-
/**
|
|
2036
|
-
* Time Complexity: O(1)
|
|
2037
|
-
* Space Complexity: O(1)
|
|
2038
|
-
*
|
|
2039
|
-
* The _replaceNode function replaces an old node with a new node in a binary tree structure.
|
|
2040
|
-
* @param {BinaryTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that you want to replace in a
|
|
2041
|
-
* tree data structure.
|
|
2042
|
-
* @param {BinaryTreeNode<K, V>} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
|
|
2043
|
-
* that will replace the `oldNode` in a tree data structure. This function is responsible for
|
|
2044
|
-
* updating the parent, left child, right child, and root (if necessary) references when replacing a
|
|
2045
|
-
* node in the tree.
|
|
2046
|
-
* @returns The method `_replaceNode` is returning the `newNode` that was passed as a parameter after
|
|
2047
|
-
* replacing the `oldNode` with it in the binary tree structure.
|
|
2048
|
-
*/
|
|
2049
|
-
_replaceNode(oldNode, newNode) {
|
|
2050
|
-
if (oldNode.parent) {
|
|
2051
|
-
if (oldNode.parent.left === oldNode) {
|
|
2052
|
-
oldNode.parent.left = newNode;
|
|
2053
|
-
}
|
|
2054
|
-
else if (oldNode.parent.right === oldNode) {
|
|
2055
|
-
oldNode.parent.right = newNode;
|
|
2056
|
-
}
|
|
2057
|
-
}
|
|
2058
|
-
newNode.left = oldNode.left;
|
|
2059
|
-
newNode.right = oldNode.right;
|
|
2060
|
-
newNode.parent = oldNode.parent;
|
|
2061
|
-
if (this._root === oldNode) {
|
|
2062
|
-
this._setRoot(newNode);
|
|
2063
|
-
}
|
|
2064
|
-
return newNode;
|
|
2065
|
-
}
|
|
2066
|
-
/**
|
|
2067
|
-
* Time Complexity: O(1)
|
|
2068
|
-
* Space Complexity: O(1)
|
|
2069
|
-
*
|
|
2070
|
-
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
2071
|
-
* of the previous root node.
|
|
2072
|
-
* @param v - The parameter `v` in the `_setRoot` method is of type `BinaryTreeNode<K, V> | null | undefined`, which means
|
|
2073
|
-
* it can either be an optional `BinaryTreeNode<K, V>` type or `null`.
|
|
2074
|
-
*/
|
|
2075
|
-
_setRoot(v) {
|
|
2076
|
-
if (v) {
|
|
2077
|
-
v.parent = undefined;
|
|
2078
|
-
}
|
|
2079
|
-
this._root = v;
|
|
2080
|
-
}
|
|
2081
|
-
/**
|
|
2082
|
-
* Time Complexity: O(1)
|
|
2083
|
-
* Space Complexity: O(1)
|
|
2084
|
-
*
|
|
2085
|
-
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
2086
|
-
* predicate function for a binary tree node.
|
|
2087
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
2088
|
-
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
2089
|
-
* parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be
|
|
2090
|
-
* used for filtering nodes in a binary tree.
|
|
2091
|
-
* @returns A NodePredicate<BinaryTreeNode<K, V>> function is being returned.
|
|
2092
|
-
*/
|
|
2093
|
-
_ensurePredicate(keyNodeEntryOrPredicate) {
|
|
2094
|
-
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined)
|
|
2095
|
-
return (node) => (node ? false : false);
|
|
2096
|
-
if (this._isPredicate(keyNodeEntryOrPredicate))
|
|
2097
|
-
return keyNodeEntryOrPredicate;
|
|
2098
|
-
if (this.isRealNode(keyNodeEntryOrPredicate))
|
|
2099
|
-
return (node) => node === keyNodeEntryOrPredicate;
|
|
2100
|
-
if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
2101
|
-
const [key] = keyNodeEntryOrPredicate;
|
|
2102
|
-
return (node) => {
|
|
2103
|
-
if (!node)
|
|
2104
|
-
return false;
|
|
2105
|
-
return node.key === key;
|
|
2106
|
-
};
|
|
2107
|
-
}
|
|
2108
|
-
return (node) => {
|
|
2109
|
-
if (!node)
|
|
2110
|
-
return false;
|
|
2111
|
-
return node.key === keyNodeEntryOrPredicate;
|
|
2112
|
-
};
|
|
2113
|
-
}
|
|
2114
|
-
/**
|
|
2115
|
-
* Time Complexity: O(1)
|
|
2116
|
-
* Space Complexity: O(1)
|
|
2117
|
-
*
|
|
2118
|
-
* The function `_isPredicate` checks if a given parameter is a function.
|
|
2119
|
-
* @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
|
|
2120
|
-
* of value. In this context, the function `_isPredicate` is checking if `p` is a function that
|
|
2121
|
-
* satisfies the type `NodePredicate<BinaryTreeNode<K, V>>`.
|
|
2122
|
-
* @returns The function is checking if the input `p` is a function and returning a boolean value
|
|
2123
|
-
* based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
|
|
2124
|
-
* predicate function for a binary tree node. If `p` is not a function, it will return `false`.
|
|
2125
|
-
*/
|
|
2126
|
-
_isPredicate(p) {
|
|
2127
|
-
return typeof p === 'function';
|
|
2128
|
-
}
|
|
2129
|
-
/**
|
|
2130
|
-
* Time Complexity: O(1)
|
|
2131
|
-
* Space Complexity: O(1)
|
|
2132
|
-
*
|
|
2133
|
-
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
2134
|
-
* entry, raw data, or null/undefined.
|
|
2135
|
-
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `_extractKey` method you provided is a
|
|
2136
|
-
* TypeScript method that takes in a parameter `keyNodeOrEntry` of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `,
|
|
2137
|
-
* where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and `
|
|
2138
|
-
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry`
|
|
2139
|
-
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
2140
|
-
* the conditions checked in the method.
|
|
2141
|
-
*/
|
|
2142
|
-
_extractKey(keyNodeOrEntry) {
|
|
2143
|
-
if (keyNodeOrEntry === null)
|
|
2144
|
-
return null;
|
|
2145
|
-
if (keyNodeOrEntry === undefined)
|
|
2146
|
-
return;
|
|
2147
|
-
if (keyNodeOrEntry === this._NIL)
|
|
2148
|
-
return;
|
|
2149
|
-
if (this.isNode(keyNodeOrEntry))
|
|
2150
|
-
return keyNodeOrEntry.key;
|
|
2151
|
-
if (this.isEntry(keyNodeOrEntry))
|
|
2152
|
-
return keyNodeOrEntry[0];
|
|
2153
|
-
return keyNodeOrEntry;
|
|
2154
|
-
}
|
|
2155
|
-
/**
|
|
2156
|
-
* Time Complexity: O(1)
|
|
2157
|
-
* Space Complexity: O(1)
|
|
2158
|
-
*
|
|
2159
|
-
* The function `_setValue` sets a value in a store based on a key, handling cases where the key or
|
|
2160
|
-
* value is null or undefined.
|
|
2161
|
-
* @param {K | null | undefined} key - The `key` parameter can be of type `K`, `null`, or
|
|
2162
|
-
* `undefined`.
|
|
2163
|
-
* @param {V | undefined} value - The `value` parameter in the `_setValue` method can be of type `V`
|
|
2164
|
-
* or `undefined`.
|
|
2165
|
-
* @returns The method `_setValue` returns `false` if either the `key` is `null` or `undefined`, or
|
|
2166
|
-
* if the `value` is `undefined`. Otherwise, it returns the result of calling the `set` method on the
|
|
2167
|
-
* `_store` object with the `key` and `value` arguments.
|
|
2168
|
-
*/
|
|
2169
|
-
_setValue(key, value) {
|
|
2170
|
-
if (key === null || key === undefined)
|
|
2171
|
-
return false;
|
|
2172
|
-
if (value === undefined)
|
|
2173
|
-
return false;
|
|
2174
|
-
return this._store.set(key, value);
|
|
2175
|
-
}
|
|
2176
|
-
/**
|
|
2177
|
-
* Time Complexity: O(1)
|
|
2178
|
-
* Space Complexity: O(1)
|
|
2179
|
-
*
|
|
2180
|
-
* The _clearNodes function sets the root node to undefined and resets the size to 0.
|
|
2181
|
-
*/
|
|
2182
|
-
_clearNodes() {
|
|
2183
|
-
this._setRoot(undefined);
|
|
2184
|
-
this._size = 0;
|
|
2185
|
-
}
|
|
2186
|
-
/**
|
|
2187
|
-
* Time Complexity: O(1)
|
|
2188
|
-
* Space Complexity: O(1)
|
|
2189
|
-
*
|
|
2190
|
-
* The _clearValues function clears all values stored in the _store object.
|
|
2191
|
-
*/
|
|
2192
|
-
_clearValues() {
|
|
2193
|
-
this._store.clear();
|
|
2194
|
-
}
|
|
2195
|
-
}
|
|
2196
|
-
exports.BinaryTree = BinaryTree;
|
|
2197
|
-
//# sourceMappingURL=binary-tree.js.map
|