min-heap-typed 1.40.0-rc → 1.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dependency-cruiser.js +422 -422
- package/.eslintrc.js +59 -59
- package/.prettierrc.js +14 -14
- package/README.md +20 -3
- package/coverage/clover.xml +11 -7
- package/coverage/coverage-final.json +95 -1
- package/coverage/coverage-summary.json +59 -2
- package/coverage/lcov-report/base.css +278 -99
- package/coverage/lcov-report/index.html +69 -65
- package/coverage/lcov-report/index.ts.html +36 -35
- package/coverage/lcov-report/sorter.js +15 -5
- package/dist/data-structures/binary-tree/avl-tree.d.ts +106 -0
- package/dist/data-structures/binary-tree/avl-tree.js +347 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +355 -0
- package/dist/data-structures/binary-tree/binary-tree.js +1115 -0
- package/dist/data-structures/binary-tree/bst.d.ts +167 -0
- package/dist/data-structures/binary-tree/bst.js +512 -0
- package/dist/data-structures/binary-tree/index.d.ts +7 -0
- package/dist/data-structures/binary-tree/index.js +23 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -0
- package/dist/data-structures/binary-tree/rb-tree.js +21 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
- package/dist/data-structures/binary-tree/segment-tree.js +180 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +126 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +355 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +313 -0
- package/dist/data-structures/graph/abstract-graph.js +884 -0
- package/dist/data-structures/graph/directed-graph.d.ts +194 -0
- package/dist/data-structures/graph/directed-graph.js +404 -0
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/index.js +20 -0
- package/dist/data-structures/graph/map-graph.d.ts +73 -0
- package/dist/data-structures/graph/map-graph.js +93 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +120 -0
- package/dist/data-structures/graph/undirected-graph.js +239 -0
- package/dist/data-structures/hash/coordinate-map.d.ts +44 -0
- package/dist/data-structures/hash/coordinate-map.js +62 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +36 -0
- package/dist/data-structures/hash/coordinate-set.js +52 -0
- package/dist/data-structures/hash/hash-map.d.ts +50 -0
- package/dist/data-structures/hash/hash-map.js +153 -0
- package/dist/data-structures/hash/hash-table.d.ts +103 -0
- package/dist/data-structures/hash/hash-table.js +236 -0
- package/dist/data-structures/hash/index.d.ts +6 -0
- package/dist/data-structures/hash/index.js +22 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.js +6 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.js +6 -0
- package/dist/data-structures/heap/heap.d.ts +235 -0
- package/dist/data-structures/heap/heap.js +515 -0
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.d.ts +15 -0
- package/dist/data-structures/heap/max-heap.js +26 -0
- package/dist/data-structures/heap/min-heap.d.ts +15 -0
- package/dist/data-structures/heap/min-heap.js +26 -0
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/index.js +27 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +253 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +569 -0
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/index.js +19 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +232 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +533 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +80 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +187 -0
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/index.js +20 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +28 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
- package/dist/data-structures/matrix/matrix2d.js +199 -0
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +106 -0
- package/dist/data-structures/matrix/vector2d.d.ts +200 -0
- package/dist/data-structures/matrix/vector2d.js +290 -0
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/priority-queue.js +17 -0
- package/dist/data-structures/queue/deque.d.ts +161 -0
- package/dist/data-structures/queue/deque.js +264 -0
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/index.js +18 -0
- package/dist/data-structures/queue/queue.d.ts +122 -0
- package/dist/data-structures/queue/queue.js +187 -0
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.d.ts +64 -0
- package/dist/data-structures/stack/stack.js +94 -0
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +8 -0
- package/dist/data-structures/tree/tree.js +40 -0
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.d.ts +79 -0
- package/dist/data-structures/trie/trie.js +251 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +18 -4
- package/dist/interfaces/binary-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/graph.d.ts +5 -0
- package/dist/interfaces/graph.js +2 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +8 -0
- package/dist/interfaces/index.js +24 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
- package/dist/types/data-structures/binary-tree/bst.js +2 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/index.js +22 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
- package/dist/types/data-structures/graph/abstract-graph.js +2 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/graph/directed-graph.js +9 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/index.js +19 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/map-graph.js +2 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/undirected-graph.js +2 -0
- package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-map.js +2 -0
- package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-set.js +2 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-map.js +2 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-table.js +2 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/hash/index.js +2 -0
- package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-map.js +2 -0
- package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-set.js +2 -0
- package/dist/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.js +2 -0
- package/dist/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/data-structures/heap/index.js +17 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/max-heap.js +2 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/min-heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +11 -0
- package/dist/types/data-structures/index.js +27 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/index.js +18 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +17 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix.js +2 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix2d.js +2 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/data-structures/matrix/navigator.js +2 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/vector2d.js +2 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/index.js +19 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/deque.js +2 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/index.js +18 -0
- package/dist/types/data-structures/queue/queue.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.js +2 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/index.js +17 -0
- package/dist/types/data-structures/stack/stack.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.js +2 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/index.js +17 -0
- package/dist/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/data-structures/tree/tree.js +2 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/index.js +17 -0
- package/dist/types/data-structures/trie/trie.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.js +2 -0
- package/dist/types/helpers.d.ts +8 -0
- package/dist/types/helpers.js +9 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +20 -0
- package/dist/utils/utils.js +73 -0
- package/jest.config.js +6 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +350 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1262 -0
- package/src/data-structures/binary-tree/bst.ts +522 -0
- package/src/data-structures/binary-tree/index.ts +7 -0
- package/src/data-structures/binary-tree/rb-tree.ts +358 -0
- package/src/data-structures/binary-tree/segment-tree.ts +190 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +379 -0
- package/src/data-structures/graph/abstract-graph.ts +1000 -0
- package/src/data-structures/graph/directed-graph.ts +449 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +106 -0
- package/src/data-structures/graph/undirected-graph.ts +259 -0
- package/src/data-structures/hash/coordinate-map.ts +63 -0
- package/src/data-structures/hash/coordinate-set.ts +52 -0
- package/src/data-structures/hash/hash-map.ts +185 -0
- package/src/data-structures/hash/hash-table.ts +268 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/tree-map.ts +2 -0
- package/src/data-structures/hash/tree-set.ts +2 -0
- package/src/data-structures/heap/heap.ts +589 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +26 -0
- package/src/data-structures/heap/min-heap.ts +26 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +605 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +576 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +221 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +211 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +315 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/priority-queue.ts +16 -0
- package/src/data-structures/queue/deque.ts +282 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +209 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +102 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +41 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +262 -0
- package/src/index.ts +4 -1
- package/src/interfaces/binary-tree.ts +10 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/graph.ts +7 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +8 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
- package/src/types/data-structures/binary-tree/bst.ts +11 -0
- package/src/types/data-structures/binary-tree/index.ts +6 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
- package/src/types/data-structures/graph/abstract-graph.ts +11 -0
- package/src/types/data-structures/graph/directed-graph.ts +8 -0
- package/src/types/data-structures/graph/index.ts +3 -0
- package/src/types/data-structures/graph/map-graph.ts +1 -0
- package/src/types/data-structures/graph/undirected-graph.ts +1 -0
- package/src/types/data-structures/hash/coordinate-map.ts +1 -0
- package/src/types/data-structures/hash/coordinate-set.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +1 -0
- package/src/types/data-structures/hash/hash-table.ts +1 -0
- package/src/types/data-structures/hash/index.ts +1 -0
- package/src/types/data-structures/hash/tree-map.ts +1 -0
- package/src/types/data-structures/hash/tree-set.ts +1 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/heap/index.ts +1 -0
- package/src/types/data-structures/heap/max-heap.ts +1 -0
- package/src/types/data-structures/heap/min-heap.ts +1 -0
- package/src/types/data-structures/index.ts +11 -0
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/index.ts +2 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +1 -0
- package/src/types/data-structures/matrix/matrix2d.ts +1 -0
- package/src/types/data-structures/matrix/navigator.ts +14 -0
- package/src/types/data-structures/matrix/vector2d.ts +1 -0
- package/src/types/data-structures/priority-queue/index.ts +3 -0
- package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/data-structures/queue/deque.ts +1 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +1 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +1 -0
- package/src/types/data-structures/tree/index.ts +1 -0
- package/src/types/data-structures/tree/tree.ts +1 -0
- package/src/types/data-structures/trie/index.ts +1 -0
- package/src/types/data-structures/trie/trie.ts +1 -0
- package/src/types/helpers.ts +11 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +86 -0
- package/tsconfig.json +1 -2
|
@@ -0,0 +1,1262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey} from '../../types';
|
|
10
|
+
import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
|
|
11
|
+
import {IBinaryTree} from '../../interfaces';
|
|
12
|
+
import {trampoline} from '../../utils';
|
|
13
|
+
import {Queue} from '../queue';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Represents a node in a binary tree.
|
|
17
|
+
* @template V - The type of data stored in the node.
|
|
18
|
+
* @template N - The type of the family relationship in the binary tree.
|
|
19
|
+
*/
|
|
20
|
+
export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> {
|
|
21
|
+
/**
|
|
22
|
+
* The key associated with the node.
|
|
23
|
+
*/
|
|
24
|
+
key: BTNKey;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The value stored in the node.
|
|
28
|
+
*/
|
|
29
|
+
value: V | undefined;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The parent node of the current node.
|
|
33
|
+
*/
|
|
34
|
+
parent: N | null | undefined;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new instance of BinaryTreeNode.
|
|
38
|
+
* @param {BTNKey} key - The key associated with the node.
|
|
39
|
+
* @param {V} value - The value stored in the node.
|
|
40
|
+
*/
|
|
41
|
+
constructor(key: BTNKey, value?: V) {
|
|
42
|
+
this.key = key;
|
|
43
|
+
this.value = value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
protected _left: N | null | undefined;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Get the left child node.
|
|
50
|
+
*/
|
|
51
|
+
get left(): N | null | undefined {
|
|
52
|
+
return this._left;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Set the left child node.
|
|
57
|
+
* @param {N | null | undefined} v - The left child node.
|
|
58
|
+
*/
|
|
59
|
+
set left(v: N | null | undefined) {
|
|
60
|
+
if (v) {
|
|
61
|
+
v.parent = this as unknown as N;
|
|
62
|
+
}
|
|
63
|
+
this._left = v;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected _right: N | null | undefined;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get the right child node.
|
|
70
|
+
*/
|
|
71
|
+
get right(): N | null | undefined {
|
|
72
|
+
return this._right;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Set the right child node.
|
|
77
|
+
* @param {N | null | undefined} v - The right child node.
|
|
78
|
+
*/
|
|
79
|
+
set right(v: N | null | undefined) {
|
|
80
|
+
if (v) {
|
|
81
|
+
v.parent = this as unknown as N;
|
|
82
|
+
}
|
|
83
|
+
this._right = v;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Get the position of the node within its family.
|
|
88
|
+
* @returns {FamilyPosition} - The family position of the node.
|
|
89
|
+
*/
|
|
90
|
+
get familyPosition(): FamilyPosition {
|
|
91
|
+
const that = this as unknown as N;
|
|
92
|
+
if (!this.parent) {
|
|
93
|
+
return this.left || this.right ? FamilyPosition.ROOT : FamilyPosition.ISOLATED;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (this.parent.left === that) {
|
|
97
|
+
return this.left || this.right ? FamilyPosition.ROOT_LEFT : FamilyPosition.LEFT;
|
|
98
|
+
} else if (this.parent.right === that) {
|
|
99
|
+
return this.left || this.right ? FamilyPosition.ROOT_RIGHT : FamilyPosition.RIGHT;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return FamilyPosition.MAL_NODE;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Represents a binary tree data structure.
|
|
108
|
+
* @template N - The type of the binary tree's nodes.
|
|
109
|
+
*/
|
|
110
|
+
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
+
implements IBinaryTree<V, N> {
|
|
112
|
+
iterationType: IterationType = IterationType.ITERATIVE;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Creates a new instance of BinaryTree.
|
|
116
|
+
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
117
|
+
*/
|
|
118
|
+
constructor(options?: BinaryTreeOptions) {
|
|
119
|
+
if (options !== undefined) {
|
|
120
|
+
const {iterationType = IterationType.ITERATIVE} = options;
|
|
121
|
+
this.iterationType = iterationType;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
protected _root: N | null = null;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Get the root node of the binary tree.
|
|
129
|
+
*/
|
|
130
|
+
get root(): N | null {
|
|
131
|
+
return this._root;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
protected _size = 0;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Get the number of nodes in the binary tree.
|
|
138
|
+
*/
|
|
139
|
+
get size(): number {
|
|
140
|
+
return this._size;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
145
|
+
* @param {BTNKey} key - The key for the new node.
|
|
146
|
+
* @param {V} value - The value for the new node.
|
|
147
|
+
* @returns {N} - The newly created BinaryTreeNode.
|
|
148
|
+
*/
|
|
149
|
+
createNode(key: BTNKey, value?: V): N {
|
|
150
|
+
return new BinaryTreeNode<V, N>(key, value) as N;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Clear the binary tree, removing all nodes.
|
|
155
|
+
*/
|
|
156
|
+
clear() {
|
|
157
|
+
this._setRoot(null);
|
|
158
|
+
this._size = 0;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Check if the binary tree is empty.
|
|
163
|
+
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
164
|
+
*/
|
|
165
|
+
isEmpty(): boolean {
|
|
166
|
+
return this.size === 0;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Add a node with the given key and value to the binary tree.
|
|
171
|
+
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
172
|
+
* @param {V} value - The value for the new node (optional).
|
|
173
|
+
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
174
|
+
*/
|
|
175
|
+
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
176
|
+
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
177
|
+
const queue = new Queue<N | null>([root]);
|
|
178
|
+
while (queue.size > 0) {
|
|
179
|
+
const cur = queue.shift();
|
|
180
|
+
if (cur) {
|
|
181
|
+
if (newNode && cur.key === newNode.key) return;
|
|
182
|
+
const inserted = this._addTo(newNode, cur);
|
|
183
|
+
if (inserted !== undefined) return inserted;
|
|
184
|
+
if (cur.left) queue.push(cur.left);
|
|
185
|
+
if (cur.right) queue.push(cur.right);
|
|
186
|
+
} else return;
|
|
187
|
+
}
|
|
188
|
+
return;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
let inserted: N | null | undefined, needInsert: N | null;
|
|
192
|
+
|
|
193
|
+
if (keyOrNode === null) {
|
|
194
|
+
needInsert = null;
|
|
195
|
+
} else if (typeof keyOrNode === 'number') {
|
|
196
|
+
needInsert = this.createNode(keyOrNode, value);
|
|
197
|
+
} else if (keyOrNode instanceof BinaryTreeNode) {
|
|
198
|
+
needInsert = keyOrNode;
|
|
199
|
+
} else {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
204
|
+
const existNode = key !== undefined ? this.get(key, (node: N) => node.key) : undefined;
|
|
205
|
+
|
|
206
|
+
if (this.root) {
|
|
207
|
+
if (existNode) {
|
|
208
|
+
existNode.value = value;
|
|
209
|
+
inserted = existNode;
|
|
210
|
+
} else {
|
|
211
|
+
inserted = _bfs(this.root, needInsert);
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
this._setRoot(needInsert);
|
|
215
|
+
if (needInsert !== null) {
|
|
216
|
+
this._size = 1;
|
|
217
|
+
} else {
|
|
218
|
+
this._size = 0;
|
|
219
|
+
}
|
|
220
|
+
inserted = this.root;
|
|
221
|
+
}
|
|
222
|
+
return inserted;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
227
|
+
* values, and adds them to the binary tree.
|
|
228
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of BTNKey or BinaryTreeNode
|
|
229
|
+
* objects, or null values.
|
|
230
|
+
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
231
|
+
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
232
|
+
* the value of the nodes will be `undefined`.
|
|
233
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
234
|
+
*/
|
|
235
|
+
addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[] {
|
|
236
|
+
// TODO not sure addMany not be run multi times
|
|
237
|
+
return keysOrNodes.map((keyOrNode, i) => {
|
|
238
|
+
if (keyOrNode instanceof BinaryTreeNode) {
|
|
239
|
+
return this.add(keyOrNode.key, keyOrNode.value);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (keyOrNode === null) {
|
|
243
|
+
return this.add(null);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const value = values?.[i];
|
|
247
|
+
return this.add(keyOrNode, value);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
253
|
+
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
254
|
+
* `BTNKey` or `N` values.
|
|
255
|
+
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
256
|
+
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
257
|
+
* array. Each value in the `data` array will be assigned to the
|
|
258
|
+
* @returns The method is returning a boolean value.
|
|
259
|
+
*/
|
|
260
|
+
refill(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: Array<V>): boolean {
|
|
261
|
+
this.clear();
|
|
262
|
+
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
delete<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
266
|
+
|
|
267
|
+
delete<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
268
|
+
|
|
269
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BinaryTreeDeletedResult<N>[];
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
273
|
+
* with the parent node that needs to be balanced.
|
|
274
|
+
* a key (`BTNKey`). If it is a key, the function will find the corresponding node in the
|
|
275
|
+
* binary tree.
|
|
276
|
+
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
277
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
278
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
279
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
280
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
281
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
282
|
+
* included in the result. The `callback` parameter has a default value of
|
|
283
|
+
* `((node: N) => node.key)`, which
|
|
284
|
+
*/
|
|
285
|
+
delete<C extends BTNCallback<N>>(
|
|
286
|
+
identifier: ReturnType<C> | null,
|
|
287
|
+
callback: C = ((node: N) => node.key) as C
|
|
288
|
+
): BinaryTreeDeletedResult<N>[] {
|
|
289
|
+
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
290
|
+
if (!this.root) return bstDeletedResult;
|
|
291
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
292
|
+
|
|
293
|
+
const curr = this.get(identifier, callback);
|
|
294
|
+
if (!curr) return bstDeletedResult;
|
|
295
|
+
|
|
296
|
+
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
297
|
+
let needBalanced: N | null = null,
|
|
298
|
+
orgCurrent = curr;
|
|
299
|
+
|
|
300
|
+
if (!curr.left) {
|
|
301
|
+
if (!parent) {
|
|
302
|
+
// Handle the case when there's only one root node
|
|
303
|
+
this._setRoot(null);
|
|
304
|
+
} else {
|
|
305
|
+
const {familyPosition: fp} = curr;
|
|
306
|
+
if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
|
|
307
|
+
parent.left = curr.right;
|
|
308
|
+
} else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
|
|
309
|
+
parent.right = curr.right;
|
|
310
|
+
}
|
|
311
|
+
needBalanced = parent;
|
|
312
|
+
}
|
|
313
|
+
} else {
|
|
314
|
+
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
315
|
+
if (leftSubTreeRightMost) {
|
|
316
|
+
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
317
|
+
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
318
|
+
if (parentOfLeftSubTreeMax) {
|
|
319
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
320
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
321
|
+
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
322
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
this._size = this.size - 1;
|
|
327
|
+
|
|
328
|
+
bstDeletedResult.push({deleted: orgCurrent, needBalanced});
|
|
329
|
+
return bstDeletedResult;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
334
|
+
* specified root node.
|
|
335
|
+
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
336
|
+
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
337
|
+
* of the node (`BTNKey`), or `null`.
|
|
338
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
339
|
+
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
340
|
+
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
341
|
+
* node of the binary tree.
|
|
342
|
+
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
343
|
+
*/
|
|
344
|
+
getDepth(distNode: BTNKey | N | null, beginRoot: BTNKey | N | null = this.root): number {
|
|
345
|
+
if (typeof distNode === 'number') distNode = this.get(distNode);
|
|
346
|
+
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
347
|
+
let depth = 0;
|
|
348
|
+
while (distNode?.parent) {
|
|
349
|
+
if (distNode === beginRoot) {
|
|
350
|
+
return depth;
|
|
351
|
+
}
|
|
352
|
+
depth++;
|
|
353
|
+
distNode = distNode.parent;
|
|
354
|
+
}
|
|
355
|
+
return depth;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
360
|
+
* iterative approach.
|
|
361
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
362
|
+
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
363
|
+
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
364
|
+
* node is specified. If `
|
|
365
|
+
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
366
|
+
* height of the binary tree using a recursive approach or an iterative approach. It can have two
|
|
367
|
+
* possible values:
|
|
368
|
+
* @returns the height of the binary tree.
|
|
369
|
+
*/
|
|
370
|
+
getHeight(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): number {
|
|
371
|
+
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
372
|
+
if (!beginRoot) return -1;
|
|
373
|
+
|
|
374
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
375
|
+
const _getMaxHeight = (cur: N | null | undefined): number => {
|
|
376
|
+
if (!cur) return -1;
|
|
377
|
+
const leftHeight = _getMaxHeight(cur.left);
|
|
378
|
+
const rightHeight = _getMaxHeight(cur.right);
|
|
379
|
+
return Math.max(leftHeight, rightHeight) + 1;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
return _getMaxHeight(beginRoot);
|
|
383
|
+
} else {
|
|
384
|
+
if (!beginRoot) {
|
|
385
|
+
return -1;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
|
|
389
|
+
let maxHeight = 0;
|
|
390
|
+
|
|
391
|
+
while (stack.length > 0) {
|
|
392
|
+
const {node, depth} = stack.pop()!;
|
|
393
|
+
|
|
394
|
+
if (node.left) {
|
|
395
|
+
stack.push({node: node.left, depth: depth + 1});
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (node.right) {
|
|
399
|
+
stack.push({node: node.right, depth: depth + 1});
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
maxHeight = Math.max(maxHeight, depth);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return maxHeight;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
411
|
+
* recursive or iterative approach.
|
|
412
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
413
|
+
* calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
|
|
414
|
+
* not provided.
|
|
415
|
+
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
416
|
+
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
417
|
+
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
418
|
+
*/
|
|
419
|
+
getMinHeight(beginRoot: N | null = this.root, iterationType = this.iterationType): number {
|
|
420
|
+
if (!beginRoot) return -1;
|
|
421
|
+
|
|
422
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
423
|
+
const _getMinHeight = (cur: N | null | undefined): number => {
|
|
424
|
+
if (!cur) return 0;
|
|
425
|
+
if (!cur.left && !cur.right) return 0;
|
|
426
|
+
const leftMinHeight = _getMinHeight(cur.left);
|
|
427
|
+
const rightMinHeight = _getMinHeight(cur.right);
|
|
428
|
+
return Math.min(leftMinHeight, rightMinHeight) + 1;
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
return _getMinHeight(beginRoot);
|
|
432
|
+
} else {
|
|
433
|
+
const stack: N[] = [];
|
|
434
|
+
let node: N | null | undefined = beginRoot,
|
|
435
|
+
last: N | null = null;
|
|
436
|
+
const depths: Map<N, number> = new Map();
|
|
437
|
+
|
|
438
|
+
while (stack.length > 0 || node) {
|
|
439
|
+
if (node) {
|
|
440
|
+
stack.push(node);
|
|
441
|
+
node = node.left;
|
|
442
|
+
} else {
|
|
443
|
+
node = stack[stack.length - 1];
|
|
444
|
+
if (!node.right || last === node.right) {
|
|
445
|
+
node = stack.pop();
|
|
446
|
+
if (node) {
|
|
447
|
+
const leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
|
|
448
|
+
const rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
|
|
449
|
+
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
450
|
+
last = node;
|
|
451
|
+
node = null;
|
|
452
|
+
}
|
|
453
|
+
} else node = node.right;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return depths.get(beginRoot) ?? -1;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
463
|
+
* height of the tree.
|
|
464
|
+
* @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
|
|
465
|
+
* either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
|
|
466
|
+
* @returns The method is returning a boolean value.
|
|
467
|
+
*/
|
|
468
|
+
isPerfectlyBalanced(beginRoot: N | null = this.root): boolean {
|
|
469
|
+
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
getNodes<C extends BTNCallback<N, BTNKey>>(
|
|
473
|
+
identifier: BTNKey,
|
|
474
|
+
callback?: C,
|
|
475
|
+
onlyOne?: boolean,
|
|
476
|
+
beginRoot?: N | null,
|
|
477
|
+
iterationType?: IterationType
|
|
478
|
+
): N[];
|
|
479
|
+
|
|
480
|
+
getNodes<C extends BTNCallback<N, N>>(
|
|
481
|
+
identifier: N | null,
|
|
482
|
+
callback?: C,
|
|
483
|
+
onlyOne?: boolean,
|
|
484
|
+
beginRoot?: N | null,
|
|
485
|
+
iterationType?: IterationType
|
|
486
|
+
): N[];
|
|
487
|
+
|
|
488
|
+
getNodes<C extends BTNCallback<N>>(
|
|
489
|
+
identifier: ReturnType<C>,
|
|
490
|
+
callback: C,
|
|
491
|
+
onlyOne?: boolean,
|
|
492
|
+
beginRoot?: N | null,
|
|
493
|
+
iterationType?: IterationType
|
|
494
|
+
): N[];
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
498
|
+
* recursive or iterative traversal.
|
|
499
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
500
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
501
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
502
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
503
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
504
|
+
* included in the result. The `callback` parameter has a default value of
|
|
505
|
+
* `((node: N) => node.key)`, which
|
|
506
|
+
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
507
|
+
* first node that matches the identifier. If set to true, the function will return an array with
|
|
508
|
+
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
509
|
+
* function will continue searching for all
|
|
510
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
511
|
+
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary
|
|
512
|
+
* tree.
|
|
513
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
514
|
+
* traverse the binary tree. It can have two possible values:
|
|
515
|
+
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
516
|
+
*/
|
|
517
|
+
getNodes<C extends BTNCallback<N>>(
|
|
518
|
+
identifier: ReturnType<C> | null,
|
|
519
|
+
callback: C = ((node: N) => node.key) as C,
|
|
520
|
+
onlyOne = false,
|
|
521
|
+
beginRoot: N | null = this.root,
|
|
522
|
+
iterationType = this.iterationType
|
|
523
|
+
): N[] {
|
|
524
|
+
if (!beginRoot) return [];
|
|
525
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
526
|
+
const ans: N[] = [];
|
|
527
|
+
|
|
528
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
529
|
+
const _traverse = (cur: N) => {
|
|
530
|
+
if (callback(cur) === identifier) {
|
|
531
|
+
ans.push(cur);
|
|
532
|
+
if (onlyOne) return;
|
|
533
|
+
}
|
|
534
|
+
if (!cur.left && !cur.right) return;
|
|
535
|
+
cur.left && _traverse(cur.left);
|
|
536
|
+
cur.right && _traverse(cur.right);
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
_traverse(beginRoot);
|
|
540
|
+
} else {
|
|
541
|
+
const queue = new Queue<N>([beginRoot]);
|
|
542
|
+
while (queue.size > 0) {
|
|
543
|
+
const cur = queue.shift();
|
|
544
|
+
if (cur) {
|
|
545
|
+
if (callback(cur) === identifier) {
|
|
546
|
+
ans.push(cur);
|
|
547
|
+
if (onlyOne) return ans;
|
|
548
|
+
}
|
|
549
|
+
cur.left && queue.push(cur.left);
|
|
550
|
+
cur.right && queue.push(cur.right);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
return ans;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
has<C extends BTNCallback<N, BTNKey>>(
|
|
559
|
+
identifier: BTNKey,
|
|
560
|
+
callback?: C,
|
|
561
|
+
beginRoot?: N,
|
|
562
|
+
iterationType?: IterationType
|
|
563
|
+
): boolean;
|
|
564
|
+
|
|
565
|
+
has<C extends BTNCallback<N, N>>(
|
|
566
|
+
identifier: N | null,
|
|
567
|
+
callback?: C,
|
|
568
|
+
beginRoot?: N,
|
|
569
|
+
iterationType?: IterationType
|
|
570
|
+
): boolean;
|
|
571
|
+
|
|
572
|
+
has<C extends BTNCallback<N>>(
|
|
573
|
+
identifier: ReturnType<C> | null,
|
|
574
|
+
callback: C,
|
|
575
|
+
beginRoot?: N,
|
|
576
|
+
iterationType?: IterationType
|
|
577
|
+
): boolean;
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* The function checks if a binary tree has a node with a given property or key.
|
|
581
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
582
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or a
|
|
583
|
+
* generic type `N`.
|
|
584
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
585
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
586
|
+
* whether the node matches the criteria or not. The default callback function
|
|
587
|
+
* `((node: N) => node.key)` is used if no callback function is
|
|
588
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
589
|
+
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
590
|
+
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
591
|
+
* as
|
|
592
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
593
|
+
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
594
|
+
* @returns a boolean value.
|
|
595
|
+
*/
|
|
596
|
+
has<C extends BTNCallback<N>>(
|
|
597
|
+
identifier: ReturnType<C> | null,
|
|
598
|
+
callback: C = ((node: N) => node.key) as C,
|
|
599
|
+
beginRoot = this.root,
|
|
600
|
+
iterationType = this.iterationType
|
|
601
|
+
): boolean {
|
|
602
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
603
|
+
// TODO may support finding node by value equal
|
|
604
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
get<C extends BTNCallback<N, BTNKey>>(
|
|
608
|
+
identifier: BTNKey,
|
|
609
|
+
callback?: C,
|
|
610
|
+
beginRoot?: N,
|
|
611
|
+
iterationType?: IterationType
|
|
612
|
+
): N | null;
|
|
613
|
+
|
|
614
|
+
get<C extends BTNCallback<N, N>>(
|
|
615
|
+
identifier: N | null,
|
|
616
|
+
callback?: C,
|
|
617
|
+
beginRoot?: N,
|
|
618
|
+
iterationType?: IterationType
|
|
619
|
+
): N | null;
|
|
620
|
+
|
|
621
|
+
get<C extends BTNCallback<N>>(
|
|
622
|
+
identifier: ReturnType<C>,
|
|
623
|
+
callback: C,
|
|
624
|
+
beginRoot?: N,
|
|
625
|
+
iterationType?: IterationType
|
|
626
|
+
): N | null;
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
630
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
631
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
632
|
+
* type.
|
|
633
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
634
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
635
|
+
* whether the node matches the criteria or not. The default callback function
|
|
636
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
637
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
638
|
+
* the root node from which the search should begin.
|
|
639
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
640
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
641
|
+
* @returns either the found node (of type N) or null if no node is found.
|
|
642
|
+
*/
|
|
643
|
+
get<C extends BTNCallback<N>>(
|
|
644
|
+
identifier: ReturnType<C> | null,
|
|
645
|
+
callback: C = ((node: N) => node.key) as C,
|
|
646
|
+
beginRoot = this.root,
|
|
647
|
+
iterationType = this.iterationType
|
|
648
|
+
): N | null {
|
|
649
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
650
|
+
// TODO may support finding node by value equal
|
|
651
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
656
|
+
* up to the root node, with the option to reverse the order of the nodes.
|
|
657
|
+
* @param {N} beginRoot - The `beginRoot` parameter represents the starting node from which you want
|
|
658
|
+
* to find the path to the root node.
|
|
659
|
+
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
660
|
+
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
661
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
662
|
+
* @returns The function `getPathToRoot` returns an array of type `N[]`.
|
|
663
|
+
*/
|
|
664
|
+
getPathToRoot(beginRoot: N, isReverse = true): N[] {
|
|
665
|
+
// TODO to support get path through passing key
|
|
666
|
+
const result: N[] = [];
|
|
667
|
+
while (beginRoot.parent) {
|
|
668
|
+
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
669
|
+
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
670
|
+
result.push(beginRoot);
|
|
671
|
+
beginRoot = beginRoot.parent;
|
|
672
|
+
}
|
|
673
|
+
result.push(beginRoot);
|
|
674
|
+
return isReverse ? result.reverse() : result;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
679
|
+
* iterative traversal.
|
|
680
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
681
|
+
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
682
|
+
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
683
|
+
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
684
|
+
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
685
|
+
* @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
|
|
686
|
+
* no leftmost node, it returns `null`.
|
|
687
|
+
*/
|
|
688
|
+
getLeftMost(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): N | null {
|
|
689
|
+
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
690
|
+
|
|
691
|
+
if (!beginRoot) return beginRoot;
|
|
692
|
+
|
|
693
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
694
|
+
const _traverse = (cur: N): N => {
|
|
695
|
+
if (!cur.left) return cur;
|
|
696
|
+
return _traverse(cur.left);
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
return _traverse(beginRoot);
|
|
700
|
+
} else {
|
|
701
|
+
// Indirect implementation of iteration using tail recursion optimization
|
|
702
|
+
const _traverse = trampoline((cur: N) => {
|
|
703
|
+
if (!cur.left) return cur;
|
|
704
|
+
return _traverse.cont(cur.left);
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
return _traverse(beginRoot);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
713
|
+
* iteratively.
|
|
714
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
715
|
+
* find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
|
|
716
|
+
* or `null`. If it is `null`, it means there is no starting node
|
|
717
|
+
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
718
|
+
* be performed when finding the rightmost node in a binary tree. It can have two possible values:
|
|
719
|
+
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the
|
|
720
|
+
* `beginRoot` parameter is `null`, it returns `null`.
|
|
721
|
+
*/
|
|
722
|
+
getRightMost(beginRoot: N | null = this.root, iterationType = this.iterationType): N | null {
|
|
723
|
+
// TODO support get right most by passing key in
|
|
724
|
+
if (!beginRoot) return beginRoot;
|
|
725
|
+
|
|
726
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
727
|
+
const _traverse = (cur: N): N => {
|
|
728
|
+
if (!cur.right) return cur;
|
|
729
|
+
return _traverse(cur.right);
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
return _traverse(beginRoot);
|
|
733
|
+
} else {
|
|
734
|
+
// Indirect implementation of iteration using tail recursion optimization
|
|
735
|
+
const _traverse = trampoline((cur: N) => {
|
|
736
|
+
if (!cur.right) return cur;
|
|
737
|
+
return _traverse.cont(cur.right);
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
return _traverse(beginRoot);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
|
|
746
|
+
* @param {N} beginRoot - The `beginRoot` parameter is the root node of the binary tree that you want
|
|
747
|
+
* to check if it is a binary search tree (BST) subtree.
|
|
748
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
749
|
+
* type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
|
|
750
|
+
* possible values:
|
|
751
|
+
* @returns The function `isSubtreeBST` returns a boolean value.
|
|
752
|
+
*/
|
|
753
|
+
isSubtreeBST(beginRoot: N | null, iterationType = this.iterationType): boolean {
|
|
754
|
+
// TODO there is a bug
|
|
755
|
+
if (!beginRoot) return true;
|
|
756
|
+
|
|
757
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
758
|
+
const dfs = (cur: N | null | undefined, min: BTNKey, max: BTNKey): boolean => {
|
|
759
|
+
if (!cur) return true;
|
|
760
|
+
if (cur.key <= min || cur.key >= max) return false;
|
|
761
|
+
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
762
|
+
};
|
|
763
|
+
|
|
764
|
+
return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
765
|
+
} else {
|
|
766
|
+
const stack = [];
|
|
767
|
+
let prev = Number.MIN_SAFE_INTEGER,
|
|
768
|
+
curr: N | null | undefined = beginRoot;
|
|
769
|
+
while (curr || stack.length > 0) {
|
|
770
|
+
while (curr) {
|
|
771
|
+
stack.push(curr);
|
|
772
|
+
curr = curr.left;
|
|
773
|
+
}
|
|
774
|
+
curr = stack.pop()!;
|
|
775
|
+
if (!curr || prev >= curr.key) return false;
|
|
776
|
+
prev = curr.key;
|
|
777
|
+
curr = curr.right;
|
|
778
|
+
}
|
|
779
|
+
return true;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* The function checks if a binary tree is a binary search tree.
|
|
785
|
+
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
786
|
+
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
787
|
+
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is not
|
|
788
|
+
* provided in
|
|
789
|
+
* @returns a boolean value.
|
|
790
|
+
*/
|
|
791
|
+
isBST(iterationType = this.iterationType): boolean {
|
|
792
|
+
if (this.root === null) return true;
|
|
793
|
+
return this.isSubtreeBST(this.root, iterationType);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
798
|
+
* node, either recursively or iteratively.
|
|
799
|
+
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
800
|
+
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
801
|
+
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
802
|
+
* an array.
|
|
803
|
+
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
804
|
+
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
805
|
+
* start from the root of the tree.
|
|
806
|
+
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
807
|
+
* performed on the binary tree. It can have two possible values:
|
|
808
|
+
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
809
|
+
*/
|
|
810
|
+
subTreeTraverse<C extends BTNCallback<N>>(
|
|
811
|
+
callback: C = ((node: N) => node.key) as C,
|
|
812
|
+
beginRoot: BTNKey | N | null = this.root,
|
|
813
|
+
iterationType = this.iterationType
|
|
814
|
+
): ReturnType<C>[] {
|
|
815
|
+
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
816
|
+
|
|
817
|
+
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
818
|
+
if (!beginRoot) return ans;
|
|
819
|
+
|
|
820
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
821
|
+
const _traverse = (cur: N) => {
|
|
822
|
+
ans.push(callback(cur));
|
|
823
|
+
cur.left && _traverse(cur.left);
|
|
824
|
+
cur.right && _traverse(cur.right);
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
_traverse(beginRoot);
|
|
828
|
+
} else {
|
|
829
|
+
const stack: N[] = [beginRoot];
|
|
830
|
+
|
|
831
|
+
while (stack.length > 0) {
|
|
832
|
+
const cur = stack.pop()!;
|
|
833
|
+
|
|
834
|
+
ans.push(callback(cur));
|
|
835
|
+
cur.right && stack.push(cur.right);
|
|
836
|
+
cur.left && stack.push(cur.left);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
return ans;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
844
|
+
* function on each node according to a specified order pattern.
|
|
845
|
+
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
846
|
+
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
847
|
+
* is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
|
|
848
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
849
|
+
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
850
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
851
|
+
* search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
|
|
852
|
+
* is `null`, an empty array will be returned.
|
|
853
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
854
|
+
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
855
|
+
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
856
|
+
*/
|
|
857
|
+
dfs<C extends BTNCallback<N>>(
|
|
858
|
+
callback: C = ((node: N) => node.key) as C,
|
|
859
|
+
pattern: DFSOrderPattern = 'in',
|
|
860
|
+
beginRoot: N | null = this.root,
|
|
861
|
+
iterationType: IterationType = IterationType.ITERATIVE
|
|
862
|
+
): ReturnType<C>[] {
|
|
863
|
+
if (!beginRoot) return [];
|
|
864
|
+
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
865
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
866
|
+
const _traverse = (node: N) => {
|
|
867
|
+
switch (pattern) {
|
|
868
|
+
case 'in':
|
|
869
|
+
if (node.left) _traverse(node.left);
|
|
870
|
+
ans.push(callback(node));
|
|
871
|
+
if (node.right) _traverse(node.right);
|
|
872
|
+
break;
|
|
873
|
+
case 'pre':
|
|
874
|
+
ans.push(callback(node));
|
|
875
|
+
|
|
876
|
+
if (node.left) _traverse(node.left);
|
|
877
|
+
if (node.right) _traverse(node.right);
|
|
878
|
+
break;
|
|
879
|
+
case 'post':
|
|
880
|
+
if (node.left) _traverse(node.left);
|
|
881
|
+
if (node.right) _traverse(node.right);
|
|
882
|
+
ans.push(callback(node));
|
|
883
|
+
|
|
884
|
+
break;
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
_traverse(beginRoot);
|
|
889
|
+
} else {
|
|
890
|
+
// 0: visit, 1: print
|
|
891
|
+
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
|
|
892
|
+
|
|
893
|
+
while (stack.length > 0) {
|
|
894
|
+
const cur = stack.pop();
|
|
895
|
+
if (!cur || !cur.node) continue;
|
|
896
|
+
if (cur.opt === 1) {
|
|
897
|
+
ans.push(callback(cur.node));
|
|
898
|
+
} else {
|
|
899
|
+
switch (pattern) {
|
|
900
|
+
case 'in':
|
|
901
|
+
stack.push({opt: 0, node: cur.node.right});
|
|
902
|
+
stack.push({opt: 1, node: cur.node});
|
|
903
|
+
stack.push({opt: 0, node: cur.node.left});
|
|
904
|
+
break;
|
|
905
|
+
case 'pre':
|
|
906
|
+
stack.push({opt: 0, node: cur.node.right});
|
|
907
|
+
stack.push({opt: 0, node: cur.node.left});
|
|
908
|
+
stack.push({opt: 1, node: cur.node});
|
|
909
|
+
break;
|
|
910
|
+
case 'post':
|
|
911
|
+
stack.push({opt: 1, node: cur.node});
|
|
912
|
+
stack.push({opt: 0, node: cur.node.right});
|
|
913
|
+
stack.push({opt: 0, node: cur.node.left});
|
|
914
|
+
break;
|
|
915
|
+
default:
|
|
916
|
+
stack.push({opt: 0, node: cur.node.right});
|
|
917
|
+
stack.push({opt: 1, node: cur.node});
|
|
918
|
+
stack.push({opt: 0, node: cur.node.left});
|
|
919
|
+
break;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
return ans;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
|
|
930
|
+
* function on each node.
|
|
931
|
+
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
932
|
+
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
933
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
|
|
934
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
935
|
+
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
936
|
+
* will not be performed and an empty array will be returned.
|
|
937
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
938
|
+
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
939
|
+
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
|
|
940
|
+
*/
|
|
941
|
+
bfs<C extends BTNCallback<N>>(
|
|
942
|
+
callback: C = ((node: N) => node.key) as C,
|
|
943
|
+
beginRoot: N | null = this.root,
|
|
944
|
+
iterationType = this.iterationType
|
|
945
|
+
): ReturnType<C>[] {
|
|
946
|
+
if (!beginRoot) return [];
|
|
947
|
+
|
|
948
|
+
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
949
|
+
|
|
950
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
951
|
+
const queue = new Queue<N>([beginRoot]);
|
|
952
|
+
|
|
953
|
+
function traverse(level: number) {
|
|
954
|
+
if (queue.size === 0) return;
|
|
955
|
+
|
|
956
|
+
const current = queue.shift()!;
|
|
957
|
+
ans.push(callback(current));
|
|
958
|
+
|
|
959
|
+
if (current.left) queue.push(current.left);
|
|
960
|
+
if (current.right) queue.push(current.right);
|
|
961
|
+
|
|
962
|
+
traverse(level + 1);
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
traverse(0);
|
|
966
|
+
} else {
|
|
967
|
+
const queue = new Queue<N>([beginRoot]);
|
|
968
|
+
while (queue.size > 0) {
|
|
969
|
+
const levelSize = queue.size;
|
|
970
|
+
|
|
971
|
+
for (let i = 0; i < levelSize; i++) {
|
|
972
|
+
const current = queue.shift()!;
|
|
973
|
+
ans.push(callback(current));
|
|
974
|
+
|
|
975
|
+
if (current.left) queue.push(current.left);
|
|
976
|
+
if (current.right) queue.push(current.right);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
return ans;
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
/**
|
|
984
|
+
* The `listLevels` function takes a binary tree node and a callback function, and returns an array
|
|
985
|
+
* of arrays representing the levels of the tree.
|
|
986
|
+
* @param {C} callback - The `callback` parameter is a function that will be called on each node in
|
|
987
|
+
* the tree. It takes a node as input and returns a value. The return type of the callback function
|
|
988
|
+
* is determined by the generic type `C`.
|
|
989
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
|
|
990
|
+
* traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
|
|
991
|
+
* from the root node of the binary tree.
|
|
992
|
+
* @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
|
|
993
|
+
* recursively or iteratively. It can have two possible values:
|
|
994
|
+
* @returns The function `listLevels` returns an array of arrays, where each inner array represents a
|
|
995
|
+
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
996
|
+
* function `C` applied to the nodes at that level.
|
|
997
|
+
*/
|
|
998
|
+
listLevels<C extends BTNCallback<N>>(
|
|
999
|
+
callback: C = ((node: N) => node.key) as C,
|
|
1000
|
+
beginRoot: N | null = this.root,
|
|
1001
|
+
iterationType = this.iterationType
|
|
1002
|
+
): ReturnType<C>[][] {
|
|
1003
|
+
if (!beginRoot) return [];
|
|
1004
|
+
const levelsNodes: ReturnType<C>[][] = [];
|
|
1005
|
+
|
|
1006
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
1007
|
+
const _recursive = (node: N, level: number) => {
|
|
1008
|
+
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
1009
|
+
levelsNodes[level].push(callback(node));
|
|
1010
|
+
if (node.left) _recursive(node.left, level + 1);
|
|
1011
|
+
if (node.right) _recursive(node.right, level + 1);
|
|
1012
|
+
};
|
|
1013
|
+
|
|
1014
|
+
_recursive(beginRoot, 0);
|
|
1015
|
+
} else {
|
|
1016
|
+
const stack: [N, number][] = [[beginRoot, 0]];
|
|
1017
|
+
|
|
1018
|
+
while (stack.length > 0) {
|
|
1019
|
+
const head = stack.pop()!;
|
|
1020
|
+
const [node, level] = head;
|
|
1021
|
+
|
|
1022
|
+
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
1023
|
+
levelsNodes[level].push(callback(node));
|
|
1024
|
+
if (node.right) stack.push([node.right, level + 1]);
|
|
1025
|
+
if (node.left) stack.push([node.left, level + 1]);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
return levelsNodes;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
/**
|
|
1033
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
1034
|
+
* @param {N} node - The parameter "node" represents a node in a binary tree.
|
|
1035
|
+
* @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
|
|
1036
|
+
*/
|
|
1037
|
+
getPredecessor(node: N): N {
|
|
1038
|
+
if (node.left) {
|
|
1039
|
+
let predecessor: N | null | undefined = node.left;
|
|
1040
|
+
while (!predecessor || (predecessor.right && predecessor.right !== node)) {
|
|
1041
|
+
if (predecessor) {
|
|
1042
|
+
predecessor = predecessor.right;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
return predecessor;
|
|
1046
|
+
} else {
|
|
1047
|
+
return node;
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
// --- start additional methods ---
|
|
1052
|
+
|
|
1053
|
+
/**
|
|
1054
|
+
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
1055
|
+
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
1056
|
+
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
1057
|
+
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
|
|
1058
|
+
* default value for this parameter is `((node: N) => node.key)`.
|
|
1059
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1060
|
+
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
1061
|
+
* following values:
|
|
1062
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
1063
|
+
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
1064
|
+
* `beginRoot` is `null`, an empty array will be returned.
|
|
1065
|
+
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
1066
|
+
*/
|
|
1067
|
+
morris<C extends BTNCallback<N>>(
|
|
1068
|
+
callback: C = ((node: N) => node.key) as C,
|
|
1069
|
+
pattern: DFSOrderPattern = 'in',
|
|
1070
|
+
beginRoot: N | null = this.root
|
|
1071
|
+
): ReturnType<C>[] {
|
|
1072
|
+
if (beginRoot === null) return [];
|
|
1073
|
+
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
1074
|
+
|
|
1075
|
+
let cur: N | null | undefined = beginRoot;
|
|
1076
|
+
const _reverseEdge = (node: N | null | undefined) => {
|
|
1077
|
+
let pre: N | null | undefined = null;
|
|
1078
|
+
let next: N | null | undefined = null;
|
|
1079
|
+
while (node) {
|
|
1080
|
+
next = node.right;
|
|
1081
|
+
node.right = pre;
|
|
1082
|
+
pre = node;
|
|
1083
|
+
node = next;
|
|
1084
|
+
}
|
|
1085
|
+
return pre;
|
|
1086
|
+
};
|
|
1087
|
+
const _printEdge = (node: N | null) => {
|
|
1088
|
+
const tail: N | null | undefined = _reverseEdge(node);
|
|
1089
|
+
let cur: N | null | undefined = tail;
|
|
1090
|
+
while (cur) {
|
|
1091
|
+
ans.push(callback(cur));
|
|
1092
|
+
cur = cur.right;
|
|
1093
|
+
}
|
|
1094
|
+
_reverseEdge(tail);
|
|
1095
|
+
};
|
|
1096
|
+
switch (pattern) {
|
|
1097
|
+
case 'in':
|
|
1098
|
+
while (cur) {
|
|
1099
|
+
if (cur.left) {
|
|
1100
|
+
const predecessor = this.getPredecessor(cur);
|
|
1101
|
+
if (!predecessor.right) {
|
|
1102
|
+
predecessor.right = cur;
|
|
1103
|
+
cur = cur.left;
|
|
1104
|
+
continue;
|
|
1105
|
+
} else {
|
|
1106
|
+
predecessor.right = null;
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
ans.push(callback(cur));
|
|
1110
|
+
cur = cur.right;
|
|
1111
|
+
}
|
|
1112
|
+
break;
|
|
1113
|
+
case 'pre':
|
|
1114
|
+
while (cur) {
|
|
1115
|
+
if (cur.left) {
|
|
1116
|
+
const predecessor = this.getPredecessor(cur);
|
|
1117
|
+
if (!predecessor.right) {
|
|
1118
|
+
predecessor.right = cur;
|
|
1119
|
+
ans.push(callback(cur));
|
|
1120
|
+
cur = cur.left;
|
|
1121
|
+
continue;
|
|
1122
|
+
} else {
|
|
1123
|
+
predecessor.right = null;
|
|
1124
|
+
}
|
|
1125
|
+
} else {
|
|
1126
|
+
ans.push(callback(cur));
|
|
1127
|
+
}
|
|
1128
|
+
cur = cur.right;
|
|
1129
|
+
}
|
|
1130
|
+
break;
|
|
1131
|
+
case 'post':
|
|
1132
|
+
while (cur) {
|
|
1133
|
+
if (cur.left) {
|
|
1134
|
+
const predecessor = this.getPredecessor(cur);
|
|
1135
|
+
if (predecessor.right === null) {
|
|
1136
|
+
predecessor.right = cur;
|
|
1137
|
+
cur = cur.left;
|
|
1138
|
+
continue;
|
|
1139
|
+
} else {
|
|
1140
|
+
predecessor.right = null;
|
|
1141
|
+
_printEdge(cur.left);
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
cur = cur.right;
|
|
1145
|
+
}
|
|
1146
|
+
_printEdge(beginRoot);
|
|
1147
|
+
break;
|
|
1148
|
+
}
|
|
1149
|
+
return ans;
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
/**
|
|
1153
|
+
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1154
|
+
* either an iterative or recursive manner.
|
|
1155
|
+
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
1156
|
+
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
1157
|
+
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
1158
|
+
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1159
|
+
* binary tree nodes in a specific order.
|
|
1160
|
+
*/
|
|
1161
|
+
* [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1162
|
+
if (!node) {
|
|
1163
|
+
return;
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1167
|
+
const stack: (N | null | undefined)[] = [];
|
|
1168
|
+
let current: N | null | undefined = node;
|
|
1169
|
+
|
|
1170
|
+
while (current || stack.length > 0) {
|
|
1171
|
+
while (current) {
|
|
1172
|
+
stack.push(current);
|
|
1173
|
+
current = current.left;
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
current = stack.pop();
|
|
1177
|
+
|
|
1178
|
+
if (current) yield current.key;
|
|
1179
|
+
if (current) current = current.right;
|
|
1180
|
+
}
|
|
1181
|
+
} else {
|
|
1182
|
+
if (node.left) {
|
|
1183
|
+
yield* this[Symbol.iterator](node.left);
|
|
1184
|
+
}
|
|
1185
|
+
yield node.key;
|
|
1186
|
+
if (node.right) {
|
|
1187
|
+
yield* this[Symbol.iterator](node.right);
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
/**
|
|
1193
|
+
* Swap the data of two nodes in the binary tree.
|
|
1194
|
+
* @param {N} srcNode - The source node to swap.
|
|
1195
|
+
* @param {N} destNode - The destination node to swap.
|
|
1196
|
+
* @returns {N} - The destination node after the swap.
|
|
1197
|
+
*/
|
|
1198
|
+
protected _swap(srcNode: N, destNode: N): N {
|
|
1199
|
+
const {key, value} = destNode;
|
|
1200
|
+
const tempNode = this.createNode(key, value);
|
|
1201
|
+
|
|
1202
|
+
if (tempNode) {
|
|
1203
|
+
destNode.key = srcNode.key;
|
|
1204
|
+
destNode.value = srcNode.value;
|
|
1205
|
+
|
|
1206
|
+
srcNode.key = tempNode.key;
|
|
1207
|
+
srcNode.value = tempNode.value;
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
return destNode;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
/**
|
|
1214
|
+
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1215
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
1216
|
+
* the binary tree. It can be either a node object or `null`.
|
|
1217
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
1218
|
+
* be added as a child.
|
|
1219
|
+
* @returns either the left or right child node of the parent node, depending on which child is
|
|
1220
|
+
* available for adding the new node. If a new node is added, the function also updates the size of
|
|
1221
|
+
* the binary tree. If neither the left nor right child is available, the function returns undefined.
|
|
1222
|
+
* If the parent node is null, the function also returns undefined.
|
|
1223
|
+
*/
|
|
1224
|
+
protected _addTo(newNode: N | null, parent: N): N | null | undefined {
|
|
1225
|
+
if (parent) {
|
|
1226
|
+
// When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
|
|
1227
|
+
// In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
|
|
1228
|
+
if (parent.left === undefined) {
|
|
1229
|
+
parent.left = newNode;
|
|
1230
|
+
if (newNode) {
|
|
1231
|
+
this._size = this.size + 1;
|
|
1232
|
+
}
|
|
1233
|
+
return parent.left;
|
|
1234
|
+
} else if (parent.right === undefined) {
|
|
1235
|
+
parent.right = newNode;
|
|
1236
|
+
if (newNode) {
|
|
1237
|
+
this._size = this.size + 1;
|
|
1238
|
+
}
|
|
1239
|
+
return parent.right;
|
|
1240
|
+
} else {
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
} else {
|
|
1244
|
+
return;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
/**
|
|
1249
|
+
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
1250
|
+
* it also sets the parent property of the value to undefined.
|
|
1251
|
+
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
1252
|
+
* type `N` or `null`.
|
|
1253
|
+
*/
|
|
1254
|
+
protected _setRoot(v: N | null) {
|
|
1255
|
+
if (v) {
|
|
1256
|
+
v.parent = undefined;
|
|
1257
|
+
}
|
|
1258
|
+
this._root = v;
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
// --- end additional methods ---
|
|
1262
|
+
}
|