tree-set-typed 2.3.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/.eslintrc.js +61 -0
- package/.prettierignore +6 -0
- package/.prettierrc.js +16 -0
- package/LICENSE +21 -0
- package/README.md +482 -0
- package/coverage/clover.xml +13 -0
- package/coverage/coverage-final.json +96 -0
- package/coverage/coverage-summary.json +60 -0
- package/coverage/lcov-report/base.css +403 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +119 -0
- package/coverage/lcov-report/index.ts.html +109 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +206 -0
- package/coverage/lcov.info +14 -0
- package/dist/cjs/index.cjs +12 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs-legacy/index.cjs +12 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +3 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm-legacy/index.mjs +3 -0
- package/dist/esm-legacy/index.mjs.map +1 -0
- package/dist/types/common/index.d.ts +12 -0
- package/dist/types/constants/index.d.ts +4 -0
- package/dist/types/data-structures/base/index.d.ts +2 -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 +150 -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 +236 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +197 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +440 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +807 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +645 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +10 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +312 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -0
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +243 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +333 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +332 -0
- package/dist/types/data-structures/graph/index.d.ts +4 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +347 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +428 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.d.ts +552 -0
- package/dist/types/data-structures/heap/index.d.ts +3 -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/index.d.ts +12 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +437 -0
- package/dist/types/data-structures/linked-list/index.d.ts +3 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +567 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -0
- package/dist/types/data-structures/matrix/index.d.ts +2 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +55 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -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 +459 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/queue.d.ts +364 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.d.ts +324 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/tree.d.ts +62 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.d.ts +412 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/interfaces/binary-tree.d.ts +60 -0
- package/dist/types/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/types/interfaces/graph.d.ts +21 -0
- package/dist/types/interfaces/heap.d.ts +1 -0
- package/dist/types/interfaces/index.d.ts +8 -0
- package/dist/types/interfaces/navigator.d.ts +1 -0
- package/dist/types/interfaces/priority-queue.d.ts +1 -0
- package/dist/types/interfaces/segment-tree.d.ts +1 -0
- package/dist/types/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/types/types/common.d.ts +15 -0
- package/dist/types/types/data-structures/base/base.d.ts +13 -0
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/types/types/data-structures/binary-tree/index.d.ts +9 -0
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/index.d.ts +12 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/index.d.ts +3 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/index.d.ts +2 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/number.d.ts +14 -0
- package/dist/types/utils/utils.d.ts +209 -0
- package/dist/umd/red-black-tree-typed.js +14578 -0
- package/dist/umd/red-black-tree-typed.js.map +1 -0
- package/dist/umd/red-black-tree-typed.min.js +44 -0
- package/dist/umd/red-black-tree-typed.min.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1383 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +263 -0
- package/docs/index.html +523 -0
- package/docs/modules.html +45 -0
- package/jest.config.js +8 -0
- package/package.json +113 -0
- package/src/common/index.ts +23 -0
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +352 -0
- package/src/data-structures/base/iterable-entry-base.ts +246 -0
- package/src/data-structures/base/linear-base.ts +643 -0
- package/src/data-structures/binary-tree/avl-tree-counter.ts +539 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +438 -0
- package/src/data-structures/binary-tree/avl-tree.ts +840 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +331 -0
- package/src/data-structures/binary-tree/binary-tree.ts +2492 -0
- package/src/data-structures/binary-tree/bst.ts +2024 -0
- package/src/data-structures/binary-tree/index.ts +10 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +767 -0
- package/src/data-structures/binary-tree/segment-tree.ts +324 -0
- package/src/data-structures/binary-tree/tree-counter.ts +575 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +549 -0
- package/src/data-structures/graph/abstract-graph.ts +1081 -0
- package/src/data-structures/graph/directed-graph.ts +715 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +132 -0
- package/src/data-structures/graph/undirected-graph.ts +626 -0
- package/src/data-structures/hash/hash-map.ts +813 -0
- package/src/data-structures/hash/index.ts +1 -0
- package/src/data-structures/heap/heap.ts +1020 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +47 -0
- package/src/data-structures/heap/min-heap.ts +36 -0
- package/src/data-structures/index.ts +12 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +876 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +1050 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +173 -0
- package/src/data-structures/matrix/index.ts +2 -0
- package/src/data-structures/matrix/matrix.ts +491 -0
- package/src/data-structures/matrix/navigator.ts +124 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +42 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +29 -0
- package/src/data-structures/priority-queue/priority-queue.ts +19 -0
- package/src/data-structures/queue/deque.ts +1001 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +592 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +469 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +115 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +756 -0
- package/src/index.ts +24 -0
- package/src/interfaces/binary-tree.ts +252 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/graph.ts +44 -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/common.ts +25 -0
- package/src/types/data-structures/base/base.ts +34 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -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 +19 -0
- package/src/types/data-structures/binary-tree/index.ts +9 -0
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -0
- package/src/types/data-structures/graph/abstract-graph.ts +18 -0
- package/src/types/data-structures/graph/directed-graph.ts +2 -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/hash-map.ts +19 -0
- package/src/types/data-structures/hash/index.ts +3 -0
- package/src/types/data-structures/heap/heap.ts +6 -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 +12 -0
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -0
- package/src/types/data-structures/linked-list/index.ts +3 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +2 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -0
- package/src/types/data-structures/matrix/navigator.ts +14 -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 +3 -0
- package/src/types/data-structures/queue/deque.ts +5 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +5 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +3 -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 +3 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +33 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/number.ts +22 -0
- package/src/utils/utils.ts +350 -0
- package/test/index.test.ts +111 -0
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +12 -0
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +71 -0
|
@@ -0,0 +1,840 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { BST } from './bst';
|
|
10
|
+
import type {
|
|
11
|
+
AVLTreeOptions,
|
|
12
|
+
BinaryTreeDeleteResult,
|
|
13
|
+
BinaryTreeOptions,
|
|
14
|
+
BSTNOptKeyOrNode,
|
|
15
|
+
EntryCallback,
|
|
16
|
+
FamilyPosition,
|
|
17
|
+
IterationType,
|
|
18
|
+
RBTNColor
|
|
19
|
+
} from '../../types';
|
|
20
|
+
import { IBinaryTree } from '../../interfaces';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Represents a Node in an AVL (Adelson-Velsky and Landis) Tree.
|
|
24
|
+
* It extends a BSTNode and ensures the 'height' property is maintained.
|
|
25
|
+
*
|
|
26
|
+
* @template K - The type of the key.
|
|
27
|
+
* @template V - The type of the value.
|
|
28
|
+
*/
|
|
29
|
+
export class AVLTreeNode<K = any, V = any> {
|
|
30
|
+
key: K;
|
|
31
|
+
value?: V;
|
|
32
|
+
parent?: AVLTreeNode<K, V> = undefined;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Creates an instance of AVLTreeNode.
|
|
36
|
+
* @remarks Time O(1), Space O(1)
|
|
37
|
+
*
|
|
38
|
+
* @param key - The key of the node.
|
|
39
|
+
* @param [value] - The value associated with the key.
|
|
40
|
+
*/
|
|
41
|
+
constructor(key: K, value?: V) {
|
|
42
|
+
this.key = key;
|
|
43
|
+
this.value = value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
_left?: AVLTreeNode<K, V> | null | undefined = undefined;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Gets the left child of the node.
|
|
50
|
+
* @remarks Time O(1), Space O(1)
|
|
51
|
+
*
|
|
52
|
+
* @returns The left child.
|
|
53
|
+
*/
|
|
54
|
+
get left(): AVLTreeNode<K, V> | null | undefined {
|
|
55
|
+
return this._left;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Sets the left child of the node and updates its parent reference.
|
|
60
|
+
* @remarks Time O(1), Space O(1)
|
|
61
|
+
*
|
|
62
|
+
* @param v - The node to set as the left child.
|
|
63
|
+
*/
|
|
64
|
+
set left(v: AVLTreeNode<K, V> | null | undefined) {
|
|
65
|
+
if (v) {
|
|
66
|
+
v.parent = this;
|
|
67
|
+
}
|
|
68
|
+
this._left = v;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
_right?: AVLTreeNode<K, V> | null | undefined = undefined;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Gets the right child of the node.
|
|
75
|
+
* @remarks Time O(1), Space O(1)
|
|
76
|
+
*
|
|
77
|
+
* @returns The right child.
|
|
78
|
+
*/
|
|
79
|
+
get right(): AVLTreeNode<K, V> | null | undefined {
|
|
80
|
+
return this._right;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Sets the right child of the node and updates its parent reference.
|
|
85
|
+
* @remarks Time O(1), Space O(1)
|
|
86
|
+
*
|
|
87
|
+
* @param v - The node to set as the right child.
|
|
88
|
+
*/
|
|
89
|
+
set right(v: AVLTreeNode<K, V> | null | undefined) {
|
|
90
|
+
if (v) {
|
|
91
|
+
v.parent = this;
|
|
92
|
+
}
|
|
93
|
+
this._right = v;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
_height: number = 0;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
100
|
+
* @remarks Time O(1), Space O(1)
|
|
101
|
+
*
|
|
102
|
+
* @returns The height.
|
|
103
|
+
*/
|
|
104
|
+
get height(): number {
|
|
105
|
+
return this._height;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Sets the height of the node.
|
|
110
|
+
* @remarks Time O(1), Space O(1)
|
|
111
|
+
*
|
|
112
|
+
* @param value - The new height.
|
|
113
|
+
*/
|
|
114
|
+
set height(value: number) {
|
|
115
|
+
this._height = value;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
_color: RBTNColor = 'BLACK';
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
122
|
+
* @remarks Time O(1), Space O(1)
|
|
123
|
+
*
|
|
124
|
+
* @returns The node's color.
|
|
125
|
+
*/
|
|
126
|
+
get color(): RBTNColor {
|
|
127
|
+
return this._color;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Sets the color of the node.
|
|
132
|
+
* @remarks Time O(1), Space O(1)
|
|
133
|
+
*
|
|
134
|
+
* @param value - The new color.
|
|
135
|
+
*/
|
|
136
|
+
set color(value: RBTNColor) {
|
|
137
|
+
this._color = value;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
_count: number = 1;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
144
|
+
* @remarks Time O(1), Space O(1)
|
|
145
|
+
*
|
|
146
|
+
* @returns The subtree node count.
|
|
147
|
+
*/
|
|
148
|
+
get count(): number {
|
|
149
|
+
return this._count;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Sets the count of nodes in the subtree.
|
|
154
|
+
* @remarks Time O(1), Space O(1)
|
|
155
|
+
*
|
|
156
|
+
* @param value - The new count.
|
|
157
|
+
*/
|
|
158
|
+
set count(value: number) {
|
|
159
|
+
this._count = value;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Gets the position of the node relative to its parent.
|
|
164
|
+
* @remarks Time O(1), Space O(1)
|
|
165
|
+
*
|
|
166
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
167
|
+
*/
|
|
168
|
+
get familyPosition(): FamilyPosition {
|
|
169
|
+
if (!this.parent) {
|
|
170
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (this.parent.left === this) {
|
|
174
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
175
|
+
} else if (this.parent.right === this) {
|
|
176
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return 'MAL_NODE';
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree.
|
|
185
|
+
* This tree extends BST and performs rotations on set/delete to maintain balance.
|
|
186
|
+
*
|
|
187
|
+
* @template K - The type of the key.
|
|
188
|
+
* @template V - The type of the value.
|
|
189
|
+
* @template R - The type of the raw data object (if using `toEntryFn`).
|
|
190
|
+
*
|
|
191
|
+
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
192
|
+
* 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
|
|
193
|
+
* 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
|
|
194
|
+
* 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
|
|
195
|
+
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
196
|
+
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
197
|
+
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* // basic AVLTree creation and add operation
|
|
201
|
+
* // Create a simple AVLTree with initial values
|
|
202
|
+
* const tree = new AVLTree([5, 2, 8, 1, 9]);
|
|
203
|
+
*
|
|
204
|
+
* tree.print();
|
|
205
|
+
* // _2___
|
|
206
|
+
* // / \
|
|
207
|
+
* // 1 _8_
|
|
208
|
+
* // / \
|
|
209
|
+
* // 5 9
|
|
210
|
+
*
|
|
211
|
+
* // Verify the tree maintains sorted order
|
|
212
|
+
* console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
|
|
213
|
+
*
|
|
214
|
+
* // Check size
|
|
215
|
+
* console.log(tree.size); // 5;
|
|
216
|
+
*
|
|
217
|
+
* // Add a new element
|
|
218
|
+
* tree.set(3);
|
|
219
|
+
* console.log(tree.size); // 6;
|
|
220
|
+
* console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
|
|
221
|
+
* @example
|
|
222
|
+
* // AVLTree has and get operations
|
|
223
|
+
* const tree = new AVLTree<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
224
|
+
*
|
|
225
|
+
* // Check if element exists
|
|
226
|
+
* console.log(tree.has(6)); // true;
|
|
227
|
+
* console.log(tree.has(99)); // false;
|
|
228
|
+
*
|
|
229
|
+
* // Get node by key
|
|
230
|
+
* const node = tree.getNode(6);
|
|
231
|
+
* console.log(node?.key); // 6;
|
|
232
|
+
*
|
|
233
|
+
* // Verify tree is balanced
|
|
234
|
+
* console.log(tree.isAVLBalanced()); // true;
|
|
235
|
+
* @example
|
|
236
|
+
* // AVLTree delete and balance verification
|
|
237
|
+
* const tree = new AVLTree([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
238
|
+
*
|
|
239
|
+
* // Delete an element
|
|
240
|
+
* tree.delete(10);
|
|
241
|
+
* console.log(tree.has(10)); // false;
|
|
242
|
+
*
|
|
243
|
+
* // Tree should remain balanced after deletion
|
|
244
|
+
* console.log(tree.isAVLBalanced()); // true;
|
|
245
|
+
*
|
|
246
|
+
* // Size decreased
|
|
247
|
+
* console.log(tree.size); // 15;
|
|
248
|
+
*
|
|
249
|
+
* // Remaining elements are still sorted
|
|
250
|
+
* const keys = [...tree.keys()];
|
|
251
|
+
* console.log(keys); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16];
|
|
252
|
+
* @example
|
|
253
|
+
* // AVLTree for university ranking system with strict balance
|
|
254
|
+
* interface University {
|
|
255
|
+
* name: string;
|
|
256
|
+
* rank: number;
|
|
257
|
+
* students: number;
|
|
258
|
+
* }
|
|
259
|
+
*
|
|
260
|
+
* // AVLTree provides highest search efficiency with strict balance
|
|
261
|
+
* // (every node's left/right subtrees differ by at most 1 in height)
|
|
262
|
+
* const universityTree = new AVLTree<number, University>([
|
|
263
|
+
* [1, { name: 'MIT', rank: 1, students: 1200 }],
|
|
264
|
+
* [5, { name: 'Stanford', rank: 5, students: 1800 }],
|
|
265
|
+
* [3, { name: 'Harvard', rank: 3, students: 2300 }],
|
|
266
|
+
* [2, { name: 'Caltech', rank: 2, students: 400 }],
|
|
267
|
+
* [4, { name: 'CMU', rank: 4, students: 1500 }]
|
|
268
|
+
* ]);
|
|
269
|
+
*
|
|
270
|
+
* // Quick lookup by rank
|
|
271
|
+
* const mit = universityTree.get(1);
|
|
272
|
+
* console.log(mit?.name); // 'MIT';
|
|
273
|
+
*
|
|
274
|
+
* const cmulevel = universityTree.getHeight(4);
|
|
275
|
+
* console.log(typeof cmulevel); // 'number';
|
|
276
|
+
*
|
|
277
|
+
* // Tree maintains strict balance during insertions and deletions
|
|
278
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
279
|
+
*
|
|
280
|
+
* // Add more universities
|
|
281
|
+
* universityTree.set(6, { name: 'Oxford', rank: 6, students: 2000 });
|
|
282
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
283
|
+
*
|
|
284
|
+
* // Delete and verify balance is maintained
|
|
285
|
+
* universityTree.delete(2);
|
|
286
|
+
* console.log(universityTree.has(2)); // false;
|
|
287
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
288
|
+
*
|
|
289
|
+
* // Get all remaining universities in rank order
|
|
290
|
+
* const remainingRanks = [...universityTree.keys()];
|
|
291
|
+
* console.log(remainingRanks); // [1, 3, 4, 5, 6];
|
|
292
|
+
* console.log(universityTree.size); // 5;
|
|
293
|
+
* @example
|
|
294
|
+
* // Find elements in a range
|
|
295
|
+
* // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
|
|
296
|
+
* type Datum = { timestamp: Date; temperature: number };
|
|
297
|
+
* // Fixed dataset of CPU temperature readings
|
|
298
|
+
* const cpuData: Datum[] = [
|
|
299
|
+
* { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 },
|
|
300
|
+
* { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 },
|
|
301
|
+
* { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 },
|
|
302
|
+
* { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 },
|
|
303
|
+
* { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 },
|
|
304
|
+
* { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 },
|
|
305
|
+
* { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 },
|
|
306
|
+
* { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 },
|
|
307
|
+
* { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 },
|
|
308
|
+
* { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 },
|
|
309
|
+
* { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 },
|
|
310
|
+
* { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 },
|
|
311
|
+
* { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 },
|
|
312
|
+
* { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 },
|
|
313
|
+
* { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 },
|
|
314
|
+
* { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 },
|
|
315
|
+
* { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 },
|
|
316
|
+
* { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 },
|
|
317
|
+
* { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 },
|
|
318
|
+
* { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 },
|
|
319
|
+
* { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 },
|
|
320
|
+
* { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 },
|
|
321
|
+
* { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 },
|
|
322
|
+
* { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 },
|
|
323
|
+
* { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 },
|
|
324
|
+
* { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 },
|
|
325
|
+
* { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 },
|
|
326
|
+
* { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 },
|
|
327
|
+
* { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 },
|
|
328
|
+
* { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 }
|
|
329
|
+
* ];
|
|
330
|
+
*
|
|
331
|
+
* // Create an AVL tree to store CPU temperature data
|
|
332
|
+
* const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, {
|
|
333
|
+
* toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature]
|
|
334
|
+
* });
|
|
335
|
+
*
|
|
336
|
+
* // Query a specific time range (e.g., from 00:05 to 00:15)
|
|
337
|
+
* const rangeStart = new Date('2024-12-02T00:05:00');
|
|
338
|
+
* const rangeEnd = new Date('2024-12-02T00:15:00');
|
|
339
|
+
* const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({
|
|
340
|
+
* minute: node ? node.key.getMinutes() : 0,
|
|
341
|
+
* temperature: cpuTemperatureTree.get(node ? node.key : undefined)
|
|
342
|
+
* }));
|
|
343
|
+
*
|
|
344
|
+
* console.log(rangeResults); // [
|
|
345
|
+
* // { minute: 5, temperature: 59.4 },
|
|
346
|
+
* // { minute: 6, temperature: 60.1 },
|
|
347
|
+
* // { minute: 7, temperature: 61.3 },
|
|
348
|
+
* // { minute: 8, temperature: 62 },
|
|
349
|
+
* // { minute: 9, temperature: 63.5 },
|
|
350
|
+
* // { minute: 10, temperature: 64 },
|
|
351
|
+
* // { minute: 11, temperature: 62.8 },
|
|
352
|
+
* // { minute: 12, temperature: 61.5 },
|
|
353
|
+
* // { minute: 13, temperature: 60.2 },
|
|
354
|
+
* // { minute: 14, temperature: 59.8 },
|
|
355
|
+
* // { minute: 15, temperature: 58.6 }
|
|
356
|
+
* // ];
|
|
357
|
+
*/
|
|
358
|
+
export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
359
|
+
/**
|
|
360
|
+
* Creates an instance of AVLTree.
|
|
361
|
+
* @remarks Time O(N log N) (from `setMany` with balanced set). Space O(N).
|
|
362
|
+
*
|
|
363
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
364
|
+
* @param [options] - Configuration options for the AVL tree.
|
|
365
|
+
*/
|
|
366
|
+
constructor(
|
|
367
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
368
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
369
|
+
> = [],
|
|
370
|
+
options?: AVLTreeOptions<K, V, R>
|
|
371
|
+
) {
|
|
372
|
+
super([], options);
|
|
373
|
+
// Note: super.setMany is called, which in BST defaults to balanced set.
|
|
374
|
+
if (keysNodesEntriesOrRaws) super.setMany(keysNodesEntriesOrRaws);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* (Protected) Creates a new AVL tree node.
|
|
379
|
+
* @remarks Time O(1), Space O(1)
|
|
380
|
+
*
|
|
381
|
+
* @param key - The key for the new node.
|
|
382
|
+
* @param [value] - The value for the new node.
|
|
383
|
+
* @returns The newly created AVLTreeNode.
|
|
384
|
+
*/
|
|
385
|
+
override createNode(key: K, value?: V): AVLTreeNode<K, V> {
|
|
386
|
+
return new AVLTreeNode<K, V>(key, this._isMapMode ? undefined : value) as AVLTreeNode<K, V>;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Checks if the given item is an `AVLTreeNode` instance.
|
|
391
|
+
* @remarks Time O(1), Space O(1)
|
|
392
|
+
*
|
|
393
|
+
* @param keyNodeOrEntry - The item to check.
|
|
394
|
+
* @returns True if it's an AVLTreeNode, false otherwise.
|
|
395
|
+
*/
|
|
396
|
+
override isNode(
|
|
397
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
398
|
+
): keyNodeOrEntry is AVLTreeNode<K, V> {
|
|
399
|
+
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Sets a new node to the AVL tree and balances the tree path.
|
|
404
|
+
* @remarks Time O(log N) (O(H) for BST set + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
405
|
+
*
|
|
406
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
407
|
+
* @param [value] - The value, if providing just a key.
|
|
408
|
+
* @returns True if the addition was successful, false otherwise.
|
|
409
|
+
*/
|
|
410
|
+
override set(
|
|
411
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
412
|
+
value?: V
|
|
413
|
+
): boolean {
|
|
414
|
+
if (keyNodeOrEntry === null) return false;
|
|
415
|
+
const inserted = super.set(keyNodeOrEntry, value);
|
|
416
|
+
// If insertion was successful, balance the path from the new node up to the root.
|
|
417
|
+
if (inserted) this._balancePath(keyNodeOrEntry);
|
|
418
|
+
return inserted;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Deletes a node from the AVL tree and re-balances the tree.
|
|
423
|
+
* @remarks Time O(log N) (O(H) for BST delete + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
424
|
+
*
|
|
425
|
+
* @param keyNodeOrEntry - The node to delete.
|
|
426
|
+
* @returns An array containing deletion results.
|
|
427
|
+
*/
|
|
428
|
+
override delete(
|
|
429
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
430
|
+
): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
|
|
431
|
+
const deletedResults = super.delete(keyNodeOrEntry);
|
|
432
|
+
// After deletion, balance the path from the parent of the *physically deleted* node.
|
|
433
|
+
for (const { needBalanced } of deletedResults) {
|
|
434
|
+
if (needBalanced) {
|
|
435
|
+
this._balancePath(needBalanced);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return deletedResults;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Rebuilds the tree to be perfectly balanced.
|
|
443
|
+
* @remarks AVL trees are already height-balanced, but this makes them *perfectly* balanced (minimal height and all leaves at N or N-1).
|
|
444
|
+
* Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
445
|
+
*
|
|
446
|
+
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
447
|
+
* @returns True if successful, false if the tree was empty.
|
|
448
|
+
*/
|
|
449
|
+
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
450
|
+
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
451
|
+
const n = nodes.length;
|
|
452
|
+
if (n === 0) return false;
|
|
453
|
+
|
|
454
|
+
this._clearNodes();
|
|
455
|
+
|
|
456
|
+
// Build balanced tree from sorted array
|
|
457
|
+
const build = (l: number, r: number, parent?: AVLTreeNode<K, V>): AVLTreeNode<K, V> | undefined => {
|
|
458
|
+
if (l > r) return undefined;
|
|
459
|
+
const m = l + ((r - l) >> 1);
|
|
460
|
+
const root = nodes[m]!;
|
|
461
|
+
root.left = build(l, m - 1, root);
|
|
462
|
+
root.right = build(m + 1, r, root);
|
|
463
|
+
root.parent = parent;
|
|
464
|
+
|
|
465
|
+
// Update height during the build
|
|
466
|
+
const lh = root.left ? (root.left as AVLTreeNode<K, V>).height : -1;
|
|
467
|
+
const rh = root.right ? (root.right as AVLTreeNode<K, V>).height : -1;
|
|
468
|
+
root.height = Math.max(lh, rh) + 1;
|
|
469
|
+
return root;
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
const newRoot = build(0, n - 1, undefined);
|
|
473
|
+
this._setRoot(newRoot);
|
|
474
|
+
this._size = n;
|
|
475
|
+
return true;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
480
|
+
* @remarks Time O(N log N) (O(N) iteration + O(log M) `set` for each item into the new tree). Space O(N) for the new tree.
|
|
481
|
+
*
|
|
482
|
+
* @template MK - New key type.
|
|
483
|
+
* @template MV - New value type.
|
|
484
|
+
* @template MR - New raw type.
|
|
485
|
+
* @param callback - A function to map each [key, value] pair.
|
|
486
|
+
* @param [options] - Options for the new AVLTree.
|
|
487
|
+
* @param [thisArg] - `this` context for the callback.
|
|
488
|
+
* @returns A new, mapped AVLTree.
|
|
489
|
+
*/
|
|
490
|
+
override map<MK = K, MV = V, MR = any>(
|
|
491
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
492
|
+
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
493
|
+
thisArg?: unknown
|
|
494
|
+
): AVLTree<MK, MV, MR> {
|
|
495
|
+
const out = this._createLike<MK, MV, MR>([], options);
|
|
496
|
+
|
|
497
|
+
let index = 0;
|
|
498
|
+
// Iterates in-order
|
|
499
|
+
for (const [key, value] of this) {
|
|
500
|
+
// `set` on the new tree will be O(log N) and will self-balance.
|
|
501
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
502
|
+
}
|
|
503
|
+
return out;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* (Protected) Creates a new, empty instance of the same AVLTree constructor.
|
|
508
|
+
* @remarks Time O(1)
|
|
509
|
+
*
|
|
510
|
+
* @template TK, TV, TR - Generic types for the new instance.
|
|
511
|
+
* @param [options] - Options for the new tree.
|
|
512
|
+
* @returns A new, empty tree.
|
|
513
|
+
*/
|
|
514
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this {
|
|
515
|
+
const Ctor = this.constructor as unknown as new (
|
|
516
|
+
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
517
|
+
opts?: AVLTreeOptions<TK, TV, TR>
|
|
518
|
+
) => this;
|
|
519
|
+
return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
524
|
+
* @remarks Time O(N log N) (from constructor) due to processing the iterable.
|
|
525
|
+
*
|
|
526
|
+
* @template TK, TV, TR - Generic types for the new instance.
|
|
527
|
+
* @param [iter=[]] - An iterable to populate the new tree.
|
|
528
|
+
* @param [options] - Options for the new tree.
|
|
529
|
+
* @returns A new AVLTree.
|
|
530
|
+
*/
|
|
531
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
532
|
+
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
533
|
+
options?: Partial<AVLTreeOptions<TK, TV, TR>>
|
|
534
|
+
): AVLTree<TK, TV, TR> {
|
|
535
|
+
const Ctor = this.constructor as unknown as new (
|
|
536
|
+
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
537
|
+
opts?: AVLTreeOptions<TK, TV, TR>
|
|
538
|
+
) => AVLTree<TK, TV, TR>;
|
|
539
|
+
return new Ctor(iter, { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) });
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* (Protected) Swaps properties of two nodes, including height.
|
|
544
|
+
* @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
|
|
545
|
+
*
|
|
546
|
+
* @param srcNode - The source node.
|
|
547
|
+
* @param destNode - The destination node.
|
|
548
|
+
* @returns The `destNode` (now holding `srcNode`'s properties).
|
|
549
|
+
*/
|
|
550
|
+
protected override _swapProperties(
|
|
551
|
+
srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>,
|
|
552
|
+
destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>
|
|
553
|
+
): AVLTreeNode<K, V> | undefined {
|
|
554
|
+
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
555
|
+
const destNodeEnsured = this.ensureNode(destNode);
|
|
556
|
+
|
|
557
|
+
if (srcNodeEnsured && destNodeEnsured) {
|
|
558
|
+
const { key, value, height } = destNodeEnsured;
|
|
559
|
+
const tempNode = this.createNode(key, value);
|
|
560
|
+
|
|
561
|
+
if (tempNode) {
|
|
562
|
+
tempNode.height = height;
|
|
563
|
+
|
|
564
|
+
// Copy src to dest
|
|
565
|
+
destNodeEnsured.key = srcNodeEnsured.key;
|
|
566
|
+
if (!this._isMapMode) destNodeEnsured.value = srcNodeEnsured.value;
|
|
567
|
+
destNodeEnsured.height = srcNodeEnsured.height;
|
|
568
|
+
|
|
569
|
+
// Copy temp (original dest) to src
|
|
570
|
+
srcNodeEnsured.key = tempNode.key;
|
|
571
|
+
if (!this._isMapMode) srcNodeEnsured.value = tempNode.value;
|
|
572
|
+
srcNodeEnsured.height = tempNode.height;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
return destNodeEnsured;
|
|
576
|
+
}
|
|
577
|
+
return undefined;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* (Protected) Calculates the balance factor (height(right) - height(left)).
|
|
582
|
+
* @remarks Time O(1) (assumes heights are stored).
|
|
583
|
+
*
|
|
584
|
+
* @param node - The node to check.
|
|
585
|
+
* @returns The balance factor (positive if right-heavy, negative if left-heavy).
|
|
586
|
+
*/
|
|
587
|
+
protected _balanceFactor(node: AVLTreeNode<K, V>): number {
|
|
588
|
+
const left = node.left ? (node.left as AVLTreeNode<K, V>).height : -1;
|
|
589
|
+
const right = node.right ? (node.right as AVLTreeNode<K, V>).height : -1;
|
|
590
|
+
return right - left;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* (Protected) Recalculates and updates the height of a node based on its children's heights.
|
|
595
|
+
* @remarks Time O(1) (assumes children's heights are correct).
|
|
596
|
+
*
|
|
597
|
+
* @param node - The node to update.
|
|
598
|
+
*/
|
|
599
|
+
protected _updateHeight(node: AVLTreeNode<K, V>): void {
|
|
600
|
+
const leftHeight = node.left ? (node.left as AVLTreeNode<K, V>).height : -1;
|
|
601
|
+
const rightHeight = node.right ? (node.right as AVLTreeNode<K, V>).height : -1;
|
|
602
|
+
node.height = 1 + Math.max(leftHeight, rightHeight);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* (Protected) Performs a Left-Left (LL) rotation (a single right rotation).
|
|
607
|
+
* @remarks Time O(1), Space O(1)
|
|
608
|
+
*
|
|
609
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
610
|
+
*/
|
|
611
|
+
protected _balanceLL(A: AVLTreeNode<K, V>): void {
|
|
612
|
+
const parentOfA = A.parent;
|
|
613
|
+
const B = A.left; // The left child
|
|
614
|
+
if (B !== null) A.parent = B;
|
|
615
|
+
if (B && B.right) {
|
|
616
|
+
B.right.parent = A;
|
|
617
|
+
}
|
|
618
|
+
if (B) B.parent = parentOfA;
|
|
619
|
+
|
|
620
|
+
// Update parent's child pointer
|
|
621
|
+
if (A === this.root) {
|
|
622
|
+
if (B) this._setRoot(B);
|
|
623
|
+
} else {
|
|
624
|
+
if (parentOfA?.left === A) {
|
|
625
|
+
parentOfA.left = B;
|
|
626
|
+
} else {
|
|
627
|
+
if (parentOfA) parentOfA.right = B;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// Perform rotation
|
|
632
|
+
if (B) {
|
|
633
|
+
A.left = B.right;
|
|
634
|
+
B.right = A;
|
|
635
|
+
}
|
|
636
|
+
this._updateHeight(A);
|
|
637
|
+
if (B) this._updateHeight(B);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* (Protected) Performs a Left-Right (LR) double rotation.
|
|
642
|
+
* @remarks Time O(1), Space O(1)
|
|
643
|
+
*
|
|
644
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
645
|
+
*/
|
|
646
|
+
protected _balanceLR(A: AVLTreeNode<K, V>): void {
|
|
647
|
+
const parentOfA = A.parent;
|
|
648
|
+
const B = A.left;
|
|
649
|
+
let C = undefined;
|
|
650
|
+
if (B) {
|
|
651
|
+
C = B.right; // The "middle" node
|
|
652
|
+
}
|
|
653
|
+
if (A && C !== null) A.parent = C;
|
|
654
|
+
if (B && C !== null) B.parent = C;
|
|
655
|
+
|
|
656
|
+
if (C) {
|
|
657
|
+
if (C.left) {
|
|
658
|
+
if (B !== null) C.left.parent = B;
|
|
659
|
+
}
|
|
660
|
+
if (C.right) {
|
|
661
|
+
C.right.parent = A;
|
|
662
|
+
}
|
|
663
|
+
C.parent = parentOfA;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// Update parent's child pointer
|
|
667
|
+
if (A === this.root) {
|
|
668
|
+
if (C) this._setRoot(C);
|
|
669
|
+
} else {
|
|
670
|
+
if (parentOfA) {
|
|
671
|
+
if (parentOfA.left === A) {
|
|
672
|
+
parentOfA.left = C;
|
|
673
|
+
} else {
|
|
674
|
+
parentOfA.right = C;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
// Perform rotation
|
|
680
|
+
if (C) {
|
|
681
|
+
A.left = C.right;
|
|
682
|
+
if (B) B.right = C.left;
|
|
683
|
+
C.left = B;
|
|
684
|
+
C.right = A;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
this._updateHeight(A);
|
|
688
|
+
if (B) this._updateHeight(B);
|
|
689
|
+
if (C) this._updateHeight(C);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
|
|
694
|
+
* @remarks Time O(1), Space O(1)
|
|
695
|
+
*
|
|
696
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
697
|
+
*/
|
|
698
|
+
protected _balanceRR(A: AVLTreeNode<K, V>): void {
|
|
699
|
+
const parentOfA = A.parent;
|
|
700
|
+
const B = A.right; // The right child
|
|
701
|
+
if (B !== null) A.parent = B;
|
|
702
|
+
if (B) {
|
|
703
|
+
if (B.left) {
|
|
704
|
+
B.left.parent = A;
|
|
705
|
+
}
|
|
706
|
+
B.parent = parentOfA;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
// Update parent's child pointer
|
|
710
|
+
if (A === this.root) {
|
|
711
|
+
if (B) this._setRoot(B);
|
|
712
|
+
} else {
|
|
713
|
+
if (parentOfA) {
|
|
714
|
+
if (parentOfA.left === A) {
|
|
715
|
+
parentOfA.left = B;
|
|
716
|
+
} else {
|
|
717
|
+
parentOfA.right = B;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// Perform rotation
|
|
723
|
+
if (B) {
|
|
724
|
+
A.right = B.left;
|
|
725
|
+
B.left = A;
|
|
726
|
+
}
|
|
727
|
+
this._updateHeight(A);
|
|
728
|
+
if (B) this._updateHeight(B);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* (Protected) Performs a Right-Left (RL) double rotation.
|
|
733
|
+
* @remarks Time O(1), Space O(1)
|
|
734
|
+
*
|
|
735
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
736
|
+
*/
|
|
737
|
+
protected _balanceRL(A: AVLTreeNode<K, V>): void {
|
|
738
|
+
const parentOfA = A.parent;
|
|
739
|
+
const B = A.right;
|
|
740
|
+
let C = undefined;
|
|
741
|
+
if (B) {
|
|
742
|
+
C = B.left; // The "middle" node
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
if (C !== null) A.parent = C;
|
|
746
|
+
if (B && C !== null) B.parent = C;
|
|
747
|
+
|
|
748
|
+
if (C) {
|
|
749
|
+
if (C.left) {
|
|
750
|
+
C.left.parent = A;
|
|
751
|
+
}
|
|
752
|
+
if (C.right) {
|
|
753
|
+
if (B !== null) C.right.parent = B;
|
|
754
|
+
}
|
|
755
|
+
C.parent = parentOfA;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// Update parent's child pointer
|
|
759
|
+
if (A === this.root) {
|
|
760
|
+
if (C) this._setRoot(C);
|
|
761
|
+
} else {
|
|
762
|
+
if (parentOfA) {
|
|
763
|
+
if (parentOfA.left === A) {
|
|
764
|
+
parentOfA.left = C;
|
|
765
|
+
} else {
|
|
766
|
+
parentOfA.right = C;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// Perform rotation
|
|
772
|
+
if (C) A.right = C.left;
|
|
773
|
+
if (B && C) B.left = C.right;
|
|
774
|
+
if (C) C.left = A;
|
|
775
|
+
if (C) C.right = B;
|
|
776
|
+
|
|
777
|
+
this._updateHeight(A);
|
|
778
|
+
if (B) this._updateHeight(B);
|
|
779
|
+
if (C) this._updateHeight(C);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
|
|
784
|
+
* @remarks Time O(log N) (O(H)), as it traverses the path to root. Space O(H) for the path array.
|
|
785
|
+
*
|
|
786
|
+
* @param node - The node to start balancing from (e.g., the newly inserted node or parent of the deleted node).
|
|
787
|
+
*/
|
|
788
|
+
protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void {
|
|
789
|
+
// Get the path from the node to the root.
|
|
790
|
+
node = this.ensureNode(node);
|
|
791
|
+
const path = this.getPathToRoot(node, node => node, false);
|
|
792
|
+
|
|
793
|
+
// Iterate up the path (from node to root)
|
|
794
|
+
for (let i = 0; i < path.length; i++) {
|
|
795
|
+
const A = path[i];
|
|
796
|
+
if (A) {
|
|
797
|
+
this._updateHeight(A);
|
|
798
|
+
|
|
799
|
+
// Check the balance factor
|
|
800
|
+
switch (this._balanceFactor(A)) {
|
|
801
|
+
case -2: // Left-heavy
|
|
802
|
+
if (A && A.left) {
|
|
803
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
804
|
+
// Left-Left case
|
|
805
|
+
this._balanceLL(A);
|
|
806
|
+
} else {
|
|
807
|
+
// Left-Right case
|
|
808
|
+
this._balanceLR(A);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
break;
|
|
812
|
+
case +2: // Right-heavy
|
|
813
|
+
if (A && A.right) {
|
|
814
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
815
|
+
// Right-Right case
|
|
816
|
+
this._balanceRR(A);
|
|
817
|
+
} else {
|
|
818
|
+
// Right-Left case
|
|
819
|
+
this._balanceRL(A);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* (Protected) Replaces a node, ensuring height is copied.
|
|
829
|
+
* @remarks Time O(1)
|
|
830
|
+
*
|
|
831
|
+
* @param oldNode - The node to be replaced.
|
|
832
|
+
* @param newNode - The node to insert.
|
|
833
|
+
* @returns The `newNode`.
|
|
834
|
+
*/
|
|
835
|
+
protected override _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V> {
|
|
836
|
+
// When replacing a node (e.g., on duplicate key), preserve the height.
|
|
837
|
+
newNode.height = oldNode.height;
|
|
838
|
+
return super._replaceNode(oldNode, newNode);
|
|
839
|
+
}
|
|
840
|
+
}
|