directed-graph-typed 2.1.1 → 2.1.2
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/dist/cjs/index.cjs +2798 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +2789 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +13 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- 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/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- 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/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- 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/linked-list/doubly-linked-list.d.ts +2 -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/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/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/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.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/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/directed-graph-typed.js +2791 -0
- package/dist/umd/directed-graph-typed.js.map +1 -0
- package/dist/umd/directed-graph-typed.min.js +9 -0
- package/dist/umd/directed-graph-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -14
- package/dist/index.js +0 -30
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -1,611 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Pablo Zeng
|
|
6
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
11
|
-
const bst_1 = require("./bst");
|
|
12
|
-
/**
|
|
13
|
-
* Represents a Node in an AVL (Adelson-Velsky and Landis) Tree.
|
|
14
|
-
* It extends a BSTNode and ensures the 'height' property is maintained.
|
|
15
|
-
*
|
|
16
|
-
* @template K - The type of the key.
|
|
17
|
-
* @template V - The type of the value.
|
|
18
|
-
*/
|
|
19
|
-
class AVLTreeNode extends bst_1.BSTNode {
|
|
20
|
-
/**
|
|
21
|
-
* Creates an instance of AVLTreeNode.
|
|
22
|
-
* @remarks Time O(1), Space O(1)
|
|
23
|
-
*
|
|
24
|
-
* @param key - The key of the node.
|
|
25
|
-
* @param [value] - The value associated with the key.
|
|
26
|
-
*/
|
|
27
|
-
constructor(key, value) {
|
|
28
|
-
super(key, value);
|
|
29
|
-
this.parent = undefined;
|
|
30
|
-
this._left = undefined;
|
|
31
|
-
this._right = undefined;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Gets the left child of the node.
|
|
35
|
-
* @remarks Time O(1), Space O(1)
|
|
36
|
-
*
|
|
37
|
-
* @returns The left child.
|
|
38
|
-
*/
|
|
39
|
-
get left() {
|
|
40
|
-
return this._left;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Sets the left child of the node and updates its parent reference.
|
|
44
|
-
* @remarks Time O(1), Space O(1)
|
|
45
|
-
*
|
|
46
|
-
* @param v - The node to set as the left child.
|
|
47
|
-
*/
|
|
48
|
-
set left(v) {
|
|
49
|
-
if (v) {
|
|
50
|
-
v.parent = this;
|
|
51
|
-
}
|
|
52
|
-
this._left = v;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Gets the right child of the node.
|
|
56
|
-
* @remarks Time O(1), Space O(1)
|
|
57
|
-
*
|
|
58
|
-
* @returns The right child.
|
|
59
|
-
*/
|
|
60
|
-
get right() {
|
|
61
|
-
return this._right;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Sets the right child of the node and updates its parent reference.
|
|
65
|
-
* @remarks Time O(1), Space O(1)
|
|
66
|
-
*
|
|
67
|
-
* @param v - The node to set as the right child.
|
|
68
|
-
*/
|
|
69
|
-
set right(v) {
|
|
70
|
-
if (v) {
|
|
71
|
-
v.parent = this;
|
|
72
|
-
}
|
|
73
|
-
this._right = v;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
exports.AVLTreeNode = AVLTreeNode;
|
|
77
|
-
/**
|
|
78
|
-
* Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree.
|
|
79
|
-
* This tree extends BST and performs rotations on add/delete to maintain balance.
|
|
80
|
-
*
|
|
81
|
-
* @template K - The type of the key.
|
|
82
|
-
* @template V - The type of the value.
|
|
83
|
-
* @template R - The type of the raw data object (if using `toEntryFn`).
|
|
84
|
-
*
|
|
85
|
-
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
86
|
-
* 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
|
|
87
|
-
* 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
|
|
88
|
-
* 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
|
|
89
|
-
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
90
|
-
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
91
|
-
* 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.@example
|
|
92
|
-
* // Find elements in a range
|
|
93
|
-
* // 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.
|
|
94
|
-
* type Datum = { timestamp: Date; temperature: number };
|
|
95
|
-
* // Fixed dataset of CPU temperature readings
|
|
96
|
-
* const cpuData: Datum[] = [
|
|
97
|
-
* { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 },
|
|
98
|
-
* { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 },
|
|
99
|
-
* { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 },
|
|
100
|
-
* { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 },
|
|
101
|
-
* { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 },
|
|
102
|
-
* { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 },
|
|
103
|
-
* { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 },
|
|
104
|
-
* { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 },
|
|
105
|
-
* { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 },
|
|
106
|
-
* { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 },
|
|
107
|
-
* { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 },
|
|
108
|
-
* { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 },
|
|
109
|
-
* { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 },
|
|
110
|
-
* { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 },
|
|
111
|
-
* { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 },
|
|
112
|
-
* { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 },
|
|
113
|
-
* { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 },
|
|
114
|
-
* { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 },
|
|
115
|
-
* { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 },
|
|
116
|
-
* { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 },
|
|
117
|
-
* { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 },
|
|
118
|
-
* { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 },
|
|
119
|
-
* { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 },
|
|
120
|
-
* { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 },
|
|
121
|
-
* { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 },
|
|
122
|
-
* { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 },
|
|
123
|
-
* { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 },
|
|
124
|
-
* { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 },
|
|
125
|
-
* { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 },
|
|
126
|
-
* { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 }
|
|
127
|
-
* ];
|
|
128
|
-
*
|
|
129
|
-
* // Create an AVL tree to store CPU temperature data
|
|
130
|
-
* const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, {
|
|
131
|
-
* toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature]
|
|
132
|
-
* });
|
|
133
|
-
*
|
|
134
|
-
* // Query a specific time range (e.g., from 00:05 to 00:15)
|
|
135
|
-
* const rangeStart = new Date('2024-12-02T00:05:00');
|
|
136
|
-
* const rangeEnd = new Date('2024-12-02T00:15:00');
|
|
137
|
-
* const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({
|
|
138
|
-
* minute: node ? node.key.getMinutes() : 0,
|
|
139
|
-
* temperature: cpuTemperatureTree.get(node ? node.key : undefined)
|
|
140
|
-
* }));
|
|
141
|
-
*
|
|
142
|
-
* console.log(rangeResults); // [
|
|
143
|
-
* // { minute: 5, temperature: 59.4 },
|
|
144
|
-
* // { minute: 6, temperature: 60.1 },
|
|
145
|
-
* // { minute: 7, temperature: 61.3 },
|
|
146
|
-
* // { minute: 8, temperature: 62 },
|
|
147
|
-
* // { minute: 9, temperature: 63.5 },
|
|
148
|
-
* // { minute: 10, temperature: 64 },
|
|
149
|
-
* // { minute: 11, temperature: 62.8 },
|
|
150
|
-
* // { minute: 12, temperature: 61.5 },
|
|
151
|
-
* // { minute: 13, temperature: 60.2 },
|
|
152
|
-
* // { minute: 14, temperature: 59.8 },
|
|
153
|
-
* // { minute: 15, temperature: 58.6 }
|
|
154
|
-
* // ]
|
|
155
|
-
*/
|
|
156
|
-
class AVLTree extends bst_1.BST {
|
|
157
|
-
/**
|
|
158
|
-
* Creates an instance of AVLTree.
|
|
159
|
-
* @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
|
|
160
|
-
*
|
|
161
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
|
|
162
|
-
* @param [options] - Configuration options for the AVL tree.
|
|
163
|
-
*/
|
|
164
|
-
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
165
|
-
super([], options);
|
|
166
|
-
// Note: super.addMany is called, which in BST defaults to balanced add.
|
|
167
|
-
if (keysNodesEntriesOrRaws)
|
|
168
|
-
super.addMany(keysNodesEntriesOrRaws);
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* (Protected) Creates a new AVL tree node.
|
|
172
|
-
* @remarks Time O(1), Space O(1)
|
|
173
|
-
*
|
|
174
|
-
* @param key - The key for the new node.
|
|
175
|
-
* @param [value] - The value for the new node.
|
|
176
|
-
* @returns The newly created AVLTreeNode.
|
|
177
|
-
*/
|
|
178
|
-
_createNode(key, value) {
|
|
179
|
-
return new AVLTreeNode(key, this._isMapMode ? undefined : value);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Checks if the given item is an `AVLTreeNode` instance.
|
|
183
|
-
* @remarks Time O(1), Space O(1)
|
|
184
|
-
*
|
|
185
|
-
* @param keyNodeOrEntry - The item to check.
|
|
186
|
-
* @returns True if it's an AVLTreeNode, false otherwise.
|
|
187
|
-
*/
|
|
188
|
-
isNode(keyNodeOrEntry) {
|
|
189
|
-
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Adds a new node to the AVL tree and balances the tree path.
|
|
193
|
-
* @remarks Time O(log N) (O(H) for BST add + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
194
|
-
*
|
|
195
|
-
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
196
|
-
* @param [value] - The value, if providing just a key.
|
|
197
|
-
* @returns True if the addition was successful, false otherwise.
|
|
198
|
-
*/
|
|
199
|
-
add(keyNodeOrEntry, value) {
|
|
200
|
-
if (keyNodeOrEntry === null)
|
|
201
|
-
return false;
|
|
202
|
-
const inserted = super.add(keyNodeOrEntry, value);
|
|
203
|
-
// If insertion was successful, balance the path from the new node up to the root.
|
|
204
|
-
if (inserted)
|
|
205
|
-
this._balancePath(keyNodeOrEntry);
|
|
206
|
-
return inserted;
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Deletes a node from the AVL tree and re-balances the tree.
|
|
210
|
-
* @remarks Time O(log N) (O(H) for BST delete + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
211
|
-
*
|
|
212
|
-
* @param keyNodeOrEntry - The node to delete.
|
|
213
|
-
* @returns An array containing deletion results.
|
|
214
|
-
*/
|
|
215
|
-
delete(keyNodeOrEntry) {
|
|
216
|
-
const deletedResults = super.delete(keyNodeOrEntry);
|
|
217
|
-
// After deletion, balance the path from the parent of the *physically deleted* node.
|
|
218
|
-
for (const { needBalanced } of deletedResults) {
|
|
219
|
-
if (needBalanced) {
|
|
220
|
-
this._balancePath(needBalanced);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
return deletedResults;
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Rebuilds the tree to be perfectly balanced.
|
|
227
|
-
* @remarks AVL trees are already height-balanced, but this makes them *perfectly* balanced (minimal height and all leaves at N or N-1).
|
|
228
|
-
* Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
229
|
-
*
|
|
230
|
-
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
231
|
-
* @returns True if successful, false if the tree was empty.
|
|
232
|
-
*/
|
|
233
|
-
perfectlyBalance(iterationType = this.iterationType) {
|
|
234
|
-
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
235
|
-
const n = nodes.length;
|
|
236
|
-
if (n === 0)
|
|
237
|
-
return false;
|
|
238
|
-
this._clearNodes();
|
|
239
|
-
// Build balanced tree from sorted array
|
|
240
|
-
const build = (l, r, parent) => {
|
|
241
|
-
if (l > r)
|
|
242
|
-
return undefined;
|
|
243
|
-
const m = l + ((r - l) >> 1);
|
|
244
|
-
const root = nodes[m];
|
|
245
|
-
root.left = build(l, m - 1, root);
|
|
246
|
-
root.right = build(m + 1, r, root);
|
|
247
|
-
root.parent = parent;
|
|
248
|
-
// Update height during the build
|
|
249
|
-
const lh = root.left ? root.left.height : -1;
|
|
250
|
-
const rh = root.right ? root.right.height : -1;
|
|
251
|
-
root.height = Math.max(lh, rh) + 1;
|
|
252
|
-
return root;
|
|
253
|
-
};
|
|
254
|
-
const newRoot = build(0, n - 1, undefined);
|
|
255
|
-
this._setRoot(newRoot);
|
|
256
|
-
this._size = n;
|
|
257
|
-
return true;
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
261
|
-
* @remarks Time O(N log N) (O(N) iteration + O(log M) `add` for each item into the new tree). Space O(N) for the new tree.
|
|
262
|
-
*
|
|
263
|
-
* @template MK - New key type.
|
|
264
|
-
* @template MV - New value type.
|
|
265
|
-
* @template MR - New raw type.
|
|
266
|
-
* @param callback - A function to map each [key, value] pair.
|
|
267
|
-
* @param [options] - Options for the new AVLTree.
|
|
268
|
-
* @param [thisArg] - `this` context for the callback.
|
|
269
|
-
* @returns A new, mapped AVLTree.
|
|
270
|
-
*/
|
|
271
|
-
map(callback, options, thisArg) {
|
|
272
|
-
const out = this._createLike([], options);
|
|
273
|
-
let index = 0;
|
|
274
|
-
// Iterates in-order
|
|
275
|
-
for (const [key, value] of this) {
|
|
276
|
-
// `add` on the new tree will be O(log N) and will self-balance.
|
|
277
|
-
out.add(callback.call(thisArg, key, value, index++, this));
|
|
278
|
-
}
|
|
279
|
-
return out;
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* (Protected) Creates a new, empty instance of the same AVLTree constructor.
|
|
283
|
-
* @remarks Time O(1)
|
|
284
|
-
*
|
|
285
|
-
* @template TK, TV, TR - Generic types for the new instance.
|
|
286
|
-
* @param [options] - Options for the new tree.
|
|
287
|
-
* @returns A new, empty tree.
|
|
288
|
-
*/
|
|
289
|
-
_createInstance(options) {
|
|
290
|
-
const Ctor = this.constructor;
|
|
291
|
-
return new Ctor([], Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
295
|
-
* @remarks Time O(N log N) (from constructor) due to processing the iterable.
|
|
296
|
-
*
|
|
297
|
-
* @template TK, TV, TR - Generic types for the new instance.
|
|
298
|
-
* @param [iter=[]] - An iterable to populate the new tree.
|
|
299
|
-
* @param [options] - Options for the new tree.
|
|
300
|
-
* @returns A new AVLTree.
|
|
301
|
-
*/
|
|
302
|
-
_createLike(iter = [], options) {
|
|
303
|
-
const Ctor = this.constructor;
|
|
304
|
-
return new Ctor(iter, Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* (Protected) Swaps properties of two nodes, including height.
|
|
308
|
-
* @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
|
|
309
|
-
*
|
|
310
|
-
* @param srcNode - The source node.
|
|
311
|
-
* @param destNode - The destination node.
|
|
312
|
-
* @returns The `destNode` (now holding `srcNode`'s properties).
|
|
313
|
-
*/
|
|
314
|
-
_swapProperties(srcNode, destNode) {
|
|
315
|
-
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
316
|
-
const destNodeEnsured = this.ensureNode(destNode);
|
|
317
|
-
if (srcNodeEnsured && destNodeEnsured) {
|
|
318
|
-
const { key, value, height } = destNodeEnsured;
|
|
319
|
-
const tempNode = this._createNode(key, value);
|
|
320
|
-
if (tempNode) {
|
|
321
|
-
tempNode.height = height;
|
|
322
|
-
// Copy src to dest
|
|
323
|
-
destNodeEnsured.key = srcNodeEnsured.key;
|
|
324
|
-
if (!this._isMapMode)
|
|
325
|
-
destNodeEnsured.value = srcNodeEnsured.value;
|
|
326
|
-
destNodeEnsured.height = srcNodeEnsured.height;
|
|
327
|
-
// Copy temp (original dest) to src
|
|
328
|
-
srcNodeEnsured.key = tempNode.key;
|
|
329
|
-
if (!this._isMapMode)
|
|
330
|
-
srcNodeEnsured.value = tempNode.value;
|
|
331
|
-
srcNodeEnsured.height = tempNode.height;
|
|
332
|
-
}
|
|
333
|
-
return destNodeEnsured;
|
|
334
|
-
}
|
|
335
|
-
return undefined;
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* (Protected) Calculates the balance factor (height(right) - height(left)).
|
|
339
|
-
* @remarks Time O(1) (assumes heights are stored).
|
|
340
|
-
*
|
|
341
|
-
* @param node - The node to check.
|
|
342
|
-
* @returns The balance factor (positive if right-heavy, negative if left-heavy).
|
|
343
|
-
*/
|
|
344
|
-
_balanceFactor(node) {
|
|
345
|
-
const left = node.left ? node.left.height : -1;
|
|
346
|
-
const right = node.right ? node.right.height : -1;
|
|
347
|
-
return right - left;
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* (Protected) Recalculates and updates the height of a node based on its children's heights.
|
|
351
|
-
* @remarks Time O(1) (assumes children's heights are correct).
|
|
352
|
-
*
|
|
353
|
-
* @param node - The node to update.
|
|
354
|
-
*/
|
|
355
|
-
_updateHeight(node) {
|
|
356
|
-
const leftHeight = node.left ? node.left.height : -1;
|
|
357
|
-
const rightHeight = node.right ? node.right.height : -1;
|
|
358
|
-
node.height = 1 + Math.max(leftHeight, rightHeight);
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* (Protected) Performs a Left-Left (LL) rotation (a single right rotation).
|
|
362
|
-
* @remarks Time O(1), Space O(1)
|
|
363
|
-
*
|
|
364
|
-
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
365
|
-
*/
|
|
366
|
-
_balanceLL(A) {
|
|
367
|
-
const parentOfA = A.parent;
|
|
368
|
-
const B = A.left; // The left child
|
|
369
|
-
if (B !== null)
|
|
370
|
-
A.parent = B;
|
|
371
|
-
if (B && B.right) {
|
|
372
|
-
B.right.parent = A;
|
|
373
|
-
}
|
|
374
|
-
if (B)
|
|
375
|
-
B.parent = parentOfA;
|
|
376
|
-
// Update parent's child pointer
|
|
377
|
-
if (A === this.root) {
|
|
378
|
-
if (B)
|
|
379
|
-
this._setRoot(B);
|
|
380
|
-
}
|
|
381
|
-
else {
|
|
382
|
-
if ((parentOfA === null || parentOfA === void 0 ? void 0 : parentOfA.left) === A) {
|
|
383
|
-
parentOfA.left = B;
|
|
384
|
-
}
|
|
385
|
-
else {
|
|
386
|
-
if (parentOfA)
|
|
387
|
-
parentOfA.right = B;
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
// Perform rotation
|
|
391
|
-
if (B) {
|
|
392
|
-
A.left = B.right;
|
|
393
|
-
B.right = A;
|
|
394
|
-
}
|
|
395
|
-
this._updateHeight(A);
|
|
396
|
-
if (B)
|
|
397
|
-
this._updateHeight(B);
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* (Protected) Performs a Left-Right (LR) double rotation.
|
|
401
|
-
* @remarks Time O(1), Space O(1)
|
|
402
|
-
*
|
|
403
|
-
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
404
|
-
*/
|
|
405
|
-
_balanceLR(A) {
|
|
406
|
-
const parentOfA = A.parent;
|
|
407
|
-
const B = A.left;
|
|
408
|
-
let C = undefined;
|
|
409
|
-
if (B) {
|
|
410
|
-
C = B.right; // The "middle" node
|
|
411
|
-
}
|
|
412
|
-
if (A && C !== null)
|
|
413
|
-
A.parent = C;
|
|
414
|
-
if (B && C !== null)
|
|
415
|
-
B.parent = C;
|
|
416
|
-
if (C) {
|
|
417
|
-
if (C.left) {
|
|
418
|
-
if (B !== null)
|
|
419
|
-
C.left.parent = B;
|
|
420
|
-
}
|
|
421
|
-
if (C.right) {
|
|
422
|
-
C.right.parent = A;
|
|
423
|
-
}
|
|
424
|
-
C.parent = parentOfA;
|
|
425
|
-
}
|
|
426
|
-
// Update parent's child pointer
|
|
427
|
-
if (A === this.root) {
|
|
428
|
-
if (C)
|
|
429
|
-
this._setRoot(C);
|
|
430
|
-
}
|
|
431
|
-
else {
|
|
432
|
-
if (parentOfA) {
|
|
433
|
-
if (parentOfA.left === A) {
|
|
434
|
-
parentOfA.left = C;
|
|
435
|
-
}
|
|
436
|
-
else {
|
|
437
|
-
parentOfA.right = C;
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
// Perform rotation
|
|
442
|
-
if (C) {
|
|
443
|
-
A.left = C.right;
|
|
444
|
-
if (B)
|
|
445
|
-
B.right = C.left;
|
|
446
|
-
C.left = B;
|
|
447
|
-
C.right = A;
|
|
448
|
-
}
|
|
449
|
-
this._updateHeight(A);
|
|
450
|
-
if (B)
|
|
451
|
-
this._updateHeight(B);
|
|
452
|
-
if (C)
|
|
453
|
-
this._updateHeight(C);
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
|
|
457
|
-
* @remarks Time O(1), Space O(1)
|
|
458
|
-
*
|
|
459
|
-
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
460
|
-
*/
|
|
461
|
-
_balanceRR(A) {
|
|
462
|
-
const parentOfA = A.parent;
|
|
463
|
-
const B = A.right; // The right child
|
|
464
|
-
if (B !== null)
|
|
465
|
-
A.parent = B;
|
|
466
|
-
if (B) {
|
|
467
|
-
if (B.left) {
|
|
468
|
-
B.left.parent = A;
|
|
469
|
-
}
|
|
470
|
-
B.parent = parentOfA;
|
|
471
|
-
}
|
|
472
|
-
// Update parent's child pointer
|
|
473
|
-
if (A === this.root) {
|
|
474
|
-
if (B)
|
|
475
|
-
this._setRoot(B);
|
|
476
|
-
}
|
|
477
|
-
else {
|
|
478
|
-
if (parentOfA) {
|
|
479
|
-
if (parentOfA.left === A) {
|
|
480
|
-
parentOfA.left = B;
|
|
481
|
-
}
|
|
482
|
-
else {
|
|
483
|
-
parentOfA.right = B;
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
// Perform rotation
|
|
488
|
-
if (B) {
|
|
489
|
-
A.right = B.left;
|
|
490
|
-
B.left = A;
|
|
491
|
-
}
|
|
492
|
-
this._updateHeight(A);
|
|
493
|
-
if (B)
|
|
494
|
-
this._updateHeight(B);
|
|
495
|
-
}
|
|
496
|
-
/**
|
|
497
|
-
* (Protected) Performs a Right-Left (RL) double rotation.
|
|
498
|
-
* @remarks Time O(1), Space O(1)
|
|
499
|
-
*
|
|
500
|
-
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
501
|
-
*/
|
|
502
|
-
_balanceRL(A) {
|
|
503
|
-
const parentOfA = A.parent;
|
|
504
|
-
const B = A.right;
|
|
505
|
-
let C = undefined;
|
|
506
|
-
if (B) {
|
|
507
|
-
C = B.left; // The "middle" node
|
|
508
|
-
}
|
|
509
|
-
if (C !== null)
|
|
510
|
-
A.parent = C;
|
|
511
|
-
if (B && C !== null)
|
|
512
|
-
B.parent = C;
|
|
513
|
-
if (C) {
|
|
514
|
-
if (C.left) {
|
|
515
|
-
C.left.parent = A;
|
|
516
|
-
}
|
|
517
|
-
if (C.right) {
|
|
518
|
-
if (B !== null)
|
|
519
|
-
C.right.parent = B;
|
|
520
|
-
}
|
|
521
|
-
C.parent = parentOfA;
|
|
522
|
-
}
|
|
523
|
-
// Update parent's child pointer
|
|
524
|
-
if (A === this.root) {
|
|
525
|
-
if (C)
|
|
526
|
-
this._setRoot(C);
|
|
527
|
-
}
|
|
528
|
-
else {
|
|
529
|
-
if (parentOfA) {
|
|
530
|
-
if (parentOfA.left === A) {
|
|
531
|
-
parentOfA.left = C;
|
|
532
|
-
}
|
|
533
|
-
else {
|
|
534
|
-
parentOfA.right = C;
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
// Perform rotation
|
|
539
|
-
if (C)
|
|
540
|
-
A.right = C.left;
|
|
541
|
-
if (B && C)
|
|
542
|
-
B.left = C.right;
|
|
543
|
-
if (C)
|
|
544
|
-
C.left = A;
|
|
545
|
-
if (C)
|
|
546
|
-
C.right = B;
|
|
547
|
-
this._updateHeight(A);
|
|
548
|
-
if (B)
|
|
549
|
-
this._updateHeight(B);
|
|
550
|
-
if (C)
|
|
551
|
-
this._updateHeight(C);
|
|
552
|
-
}
|
|
553
|
-
/**
|
|
554
|
-
* (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
|
|
555
|
-
* @remarks Time O(log N) (O(H)), as it traverses the path to root. Space O(H) for the path array.
|
|
556
|
-
*
|
|
557
|
-
* @param node - The node to start balancing from (e.g., the newly inserted node or parent of the deleted node).
|
|
558
|
-
*/
|
|
559
|
-
_balancePath(node) {
|
|
560
|
-
// Get the path from the node to the root.
|
|
561
|
-
node = this.ensureNode(node);
|
|
562
|
-
const path = this.getPathToRoot(node, node => node, false);
|
|
563
|
-
// Iterate up the path (from node to root)
|
|
564
|
-
for (let i = 0; i < path.length; i++) {
|
|
565
|
-
const A = path[i];
|
|
566
|
-
if (A) {
|
|
567
|
-
this._updateHeight(A);
|
|
568
|
-
// Check the balance factor
|
|
569
|
-
switch (this._balanceFactor(A)) {
|
|
570
|
-
case -2: // Left-heavy
|
|
571
|
-
if (A && A.left) {
|
|
572
|
-
if (this._balanceFactor(A.left) <= 0) {
|
|
573
|
-
// Left-Left case
|
|
574
|
-
this._balanceLL(A);
|
|
575
|
-
}
|
|
576
|
-
else {
|
|
577
|
-
// Left-Right case
|
|
578
|
-
this._balanceLR(A);
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
break;
|
|
582
|
-
case +2: // Right-heavy
|
|
583
|
-
if (A && A.right) {
|
|
584
|
-
if (this._balanceFactor(A.right) >= 0) {
|
|
585
|
-
// Right-Right case
|
|
586
|
-
this._balanceRR(A);
|
|
587
|
-
}
|
|
588
|
-
else {
|
|
589
|
-
// Right-Left case
|
|
590
|
-
this._balanceRL(A);
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
/**
|
|
598
|
-
* (Protected) Replaces a node, ensuring height is copied.
|
|
599
|
-
* @remarks Time O(1)
|
|
600
|
-
*
|
|
601
|
-
* @param oldNode - The node to be replaced.
|
|
602
|
-
* @param newNode - The node to insert.
|
|
603
|
-
* @returns The `newNode`.
|
|
604
|
-
*/
|
|
605
|
-
_replaceNode(oldNode, newNode) {
|
|
606
|
-
// When replacing a node (e.g., on duplicate key), preserve the height.
|
|
607
|
-
newNode.height = oldNode.height;
|
|
608
|
-
return super._replaceNode(oldNode, newNode);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
exports.AVLTree = AVLTree;
|