data-structure-typed 0.8.18 → 1.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/LICENSE +21 -0
- package/README.md +690 -2
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -18
- package/dist/data-structures/binary-tree/avl-tree.js +110 -37
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +40 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +44 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -138
- package/dist/data-structures/binary-tree/binary-tree.js +27 -979
- package/dist/data-structures/binary-tree/bst.d.ts +118 -28
- package/dist/data-structures/binary-tree/bst.js +162 -124
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +63 -13
- package/dist/data-structures/binary-tree/segment-tree.js +80 -17
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -10
- package/dist/data-structures/binary-tree/tree-multiset.js +682 -9
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -64
- package/dist/data-structures/graph/abstract-graph.js +365 -92
- package/dist/data-structures/graph/directed-graph.d.ts +175 -26
- package/dist/data-structures/graph/directed-graph.js +249 -95
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -8
- package/dist/data-structures/graph/undirected-graph.js +154 -44
- package/dist/data-structures/hash/coordinate-map.d.ts +39 -2
- package/dist/data-structures/hash/coordinate-map.js +44 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +34 -0
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -51
- package/dist/data-structures/heap/heap.js +106 -63
- package/dist/data-structures/heap/max-heap.d.ts +13 -4
- package/dist/data-structures/heap/max-heap.js +10 -2
- package/dist/data-structures/heap/min-heap.d.ts +14 -4
- package/dist/data-structures/heap/min-heap.js +11 -2
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -57
- package/dist/data-structures/linked-list/doubly-linked-list.js +461 -194
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -319
- package/dist/data-structures/linked-list/singly-linked-list.js +338 -557
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +14 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +87 -4
- package/dist/data-structures/matrix/matrix2d.js +91 -8
- package/dist/data-structures/matrix/navigator.d.ts +37 -16
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/vector2d.d.ts +156 -29
- package/dist/data-structures/matrix/vector2d.js +184 -55
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +28 -4
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +29 -4
- package/dist/data-structures/priority-queue/priority-queue.d.ts +166 -22
- package/dist/data-structures/priority-queue/priority-queue.js +219 -75
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +151 -7
- package/dist/data-structures/queue/queue.d.ts +68 -42
- package/dist/data-structures/queue/queue.js +95 -51
- package/dist/data-structures/stack/stack.d.ts +30 -36
- package/dist/data-structures/stack/stack.js +31 -37
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/{types/utils.js → data-structures/tree/tree.js} +26 -19
- package/dist/data-structures/trie/trie.d.ts +39 -6
- package/dist/data-structures/trie/trie.js +81 -12
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-binary-tree.js +2 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/abstract-graph.js +2 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/avl-tree.js +2 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/bst.js +2 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/dist/interfaces/directed-graph.js +2 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/index.js +31 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/rb-tree.js +2 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +13 -7
- package/dist/types/data-structures/index.js +31 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +19 -0
- package/dist/{data-structures/trampoline.js → utils/utils.js} +26 -12
- package/package.json +106 -55
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/index.d.ts +0 -3
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/types/utils.d.ts +0 -46
- package/dist/types/utils.d.ts +0 -46
- package/dist/utils.d.ts +0 -122
- package/dist/utils.js +0 -569
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -232
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1088
- package/src/data-structures/binary-tree/bst.ts +0 -404
- package/src/data-structures/binary-tree/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -164
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -21
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/graph/abstract-graph.ts +0 -789
- package/src/data-structures/graph/directed-graph.ts +0 -322
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -154
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -136
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -22
- package/src/data-structures/heap/min-heap.ts +0 -24
- package/src/data-structures/index.ts +0 -11
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -258
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -750
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -99
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -12
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -12
- package/src/data-structures/priority-queue/priority-queue.ts +0 -208
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -123
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -104
- package/src/data-structures/trampoline.ts +0 -91
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -153
- package/src/index.ts +0 -1
- package/src/types/index.ts +0 -1
- package/src/types/patches/index.d.ts +0 -0
- package/src/types/utils.ts +0 -158
- package/src/utils.ts +0 -605
- package/tsconfig.json +0 -53
- /package/dist/{types/data-structures/hash/hash-table.d.ts → interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/pair.d.ts → interfaces/heap.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/tree-map.d.ts → interfaces/navigator.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/tree-set.d.ts → interfaces/priority-queue.d.ts} +0 -0
- /package/dist/{types/data-structures/linked-list/skip-linked-list.d.ts → interfaces/segment-tree.d.ts} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/singly-linked-list.d.ts} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/types/data-structures/doubly-linked-list.d.ts} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/types/data-structures/singly-linked-list.d.ts} +0 -0
- /package/dist/{types/types → utils}/index.d.ts +0 -0
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
import {BST, BSTNode} from './bst';
|
|
2
|
-
import {BinaryTreeNodeId} from './binary-tree';
|
|
3
|
-
|
|
4
|
-
export interface AVLTreeDeleted<T> {
|
|
5
|
-
deleted: AVLTreeNode<T> | null;
|
|
6
|
-
needBalanced: AVLTreeNode<T> | null;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class AVLTreeNode<T> extends BSTNode<T> {
|
|
10
|
-
override clone(): AVLTreeNode<T> {
|
|
11
|
-
return new AVLTreeNode<T>(this.id, this.val, this.count);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class AVLTree<T> extends BST<T> {
|
|
16
|
-
|
|
17
|
-
override createNode(id: BinaryTreeNodeId, val: T, count?: number): AVLTreeNode<T> {
|
|
18
|
-
return new AVLTreeNode<T>(id, val, count);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
override put(id: BinaryTreeNodeId, val: T | null, count?: number): AVLTreeNode<T> | null {
|
|
22
|
-
const inserted = super.put(id, val, count);
|
|
23
|
-
if (inserted) this.balancePath(inserted);
|
|
24
|
-
return inserted;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
override remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): AVLTreeDeleted<T>[] {
|
|
28
|
-
const deletedResults = super.remove(id, isUpdateAllLeftSum);
|
|
29
|
-
for (const {needBalanced} of deletedResults) {
|
|
30
|
-
if (needBalanced) {
|
|
31
|
-
this.balancePath(needBalanced);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return deletedResults;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
balanceFactor(node: AVLTreeNode<T>): number {
|
|
38
|
-
if (!node.right) // node has no right subtree
|
|
39
|
-
return -node.height;
|
|
40
|
-
else if (!node.left) // node has no left subtree
|
|
41
|
-
return +node.height;
|
|
42
|
-
else
|
|
43
|
-
return node.right.height - node.left.height;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
updateHeight(node: AVLTreeNode<T>): void {
|
|
47
|
-
if (!node.left && !node.right) // node is a leaf
|
|
48
|
-
node.height = 0;
|
|
49
|
-
else if (!node.left) {
|
|
50
|
-
// node has no left subtree
|
|
51
|
-
const rightHeight = node.right ? node.right.height : 0;
|
|
52
|
-
node.height = 1 + rightHeight;
|
|
53
|
-
} else if (!node.right) // node has no right subtree
|
|
54
|
-
node.height = 1 + node.left.height;
|
|
55
|
-
else
|
|
56
|
-
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
balancePath(node: AVLTreeNode<T>): void {
|
|
60
|
-
const path = this.getPathToRoot(node);
|
|
61
|
-
for (let i = path.length - 1; i >= 0; i--) {
|
|
62
|
-
const A = path[i];
|
|
63
|
-
this.updateHeight(A);
|
|
64
|
-
switch (this.balanceFactor(A)) {
|
|
65
|
-
case -2:
|
|
66
|
-
if (A && A.left) {
|
|
67
|
-
if (this.balanceFactor(A.left) <= 0) {
|
|
68
|
-
this.balanceLL(A); // Perform LL rotation
|
|
69
|
-
} else {
|
|
70
|
-
this.balanceLR(A); // Perform LR rotation
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
break;
|
|
74
|
-
case +2:
|
|
75
|
-
if (A && A.right) {
|
|
76
|
-
if (this.balanceFactor(A.right) >= 0) {
|
|
77
|
-
this.balanceRR(A); // Perform RR rotation
|
|
78
|
-
} else {
|
|
79
|
-
this.balanceRL(A); // Perform RL rotation
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
balanceLL(A: AVLTreeNode<T>): void {
|
|
87
|
-
const parentOfA = A.parent;
|
|
88
|
-
const B = A.left; // A is left-heavy and B is left-heavy
|
|
89
|
-
A.parent = B;
|
|
90
|
-
if (B && B.right) {
|
|
91
|
-
B.right.parent = A;
|
|
92
|
-
}
|
|
93
|
-
if (B) B.parent = parentOfA;
|
|
94
|
-
if (A === this.root) {
|
|
95
|
-
if (B) this.root = B;
|
|
96
|
-
} else {
|
|
97
|
-
if (parentOfA?.left === A) {
|
|
98
|
-
parentOfA.left = B;
|
|
99
|
-
} else {
|
|
100
|
-
if (parentOfA) parentOfA.right = B;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (B) {
|
|
105
|
-
A.left = B.right; // Make T2 the left subtree of A
|
|
106
|
-
B.right = A; // Make A the left child of B
|
|
107
|
-
}
|
|
108
|
-
this.updateHeight(A);
|
|
109
|
-
if (B) this.updateHeight(B);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
balanceLR(A: AVLTreeNode<T>): void {
|
|
113
|
-
const parentOfA = A.parent;
|
|
114
|
-
const B = A.left; // A is left-heavy
|
|
115
|
-
let C = null;
|
|
116
|
-
if (B) {
|
|
117
|
-
C = B.right;// B is right-heavy
|
|
118
|
-
}
|
|
119
|
-
if (A) A.parent = C;
|
|
120
|
-
if (B) B.parent = C;
|
|
121
|
-
|
|
122
|
-
if (C) {
|
|
123
|
-
if (C.left) {
|
|
124
|
-
C.left.parent = B;
|
|
125
|
-
}
|
|
126
|
-
if (C.right) {
|
|
127
|
-
C.right.parent = A;
|
|
128
|
-
}
|
|
129
|
-
C.parent = parentOfA;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (A === this.root) {
|
|
133
|
-
if (C) this.root = C;
|
|
134
|
-
} else {
|
|
135
|
-
if (parentOfA) {
|
|
136
|
-
if (parentOfA.left === A) {
|
|
137
|
-
parentOfA.left = C;
|
|
138
|
-
} else {
|
|
139
|
-
parentOfA.right = C;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (C) {
|
|
145
|
-
A.left = C.right; // Make T3 the left subtree of A
|
|
146
|
-
if (B) B.right = C.left; // Make T2 the right subtree of B
|
|
147
|
-
C.left = B;
|
|
148
|
-
C.right = A;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
this.updateHeight(A); // Adjust heights
|
|
152
|
-
B && this.updateHeight(B);
|
|
153
|
-
C && this.updateHeight(C);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
balanceRR(A: AVLTreeNode<T>): void {
|
|
157
|
-
const parentOfA = A.parent;
|
|
158
|
-
const B = A.right; // A is right-heavy and B is right-heavy
|
|
159
|
-
A.parent = B;
|
|
160
|
-
if (B) {
|
|
161
|
-
if (B.left) {
|
|
162
|
-
B.left.parent = A;
|
|
163
|
-
}
|
|
164
|
-
B.parent = parentOfA;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (A === this.root) {
|
|
168
|
-
if (B) this.root = B;
|
|
169
|
-
} else {
|
|
170
|
-
if (parentOfA) {
|
|
171
|
-
if (parentOfA.left === A) {
|
|
172
|
-
parentOfA.left = B;
|
|
173
|
-
} else {
|
|
174
|
-
parentOfA.right = B;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (B) {
|
|
180
|
-
A.right = B.left; // Make T2 the right subtree of A
|
|
181
|
-
B.left = A;
|
|
182
|
-
}
|
|
183
|
-
this.updateHeight(A);
|
|
184
|
-
B && this.updateHeight(B);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
balanceRL(A: AVLTreeNode<T>): void {
|
|
188
|
-
const parentOfA = A.parent;
|
|
189
|
-
const B = A.right; // A is right-heavy
|
|
190
|
-
let C = null;
|
|
191
|
-
if (B) {
|
|
192
|
-
C = B.left; // B is left-heavy
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
A.parent = C;
|
|
196
|
-
if (B) B.parent = C;
|
|
197
|
-
|
|
198
|
-
if (C) {
|
|
199
|
-
if (C.left) {
|
|
200
|
-
C.left.parent = A;
|
|
201
|
-
}
|
|
202
|
-
if (C.right) {
|
|
203
|
-
C.right.parent = B;
|
|
204
|
-
}
|
|
205
|
-
C.parent = parentOfA;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
if (A === this.root) {
|
|
210
|
-
if (C) this.root = C;
|
|
211
|
-
} else {
|
|
212
|
-
if (parentOfA) {
|
|
213
|
-
if (parentOfA.left === A) {
|
|
214
|
-
parentOfA.left = C;
|
|
215
|
-
} else {
|
|
216
|
-
parentOfA.right = C;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (C) A.right = C.left; // Make T2 the right subtree of A
|
|
222
|
-
if (B && C) B.left = C.right; // Make T3 the left subtree of B
|
|
223
|
-
if (C) C.left = A;
|
|
224
|
-
if (C) C.right = B;
|
|
225
|
-
|
|
226
|
-
this.updateHeight(A); // Adjust heights
|
|
227
|
-
B && this.updateHeight(B);
|
|
228
|
-
C && this.updateHeight(C);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
export class BinaryIndexedTree {
|
|
2
|
-
private readonly _sumTree: number[];
|
|
3
|
-
|
|
4
|
-
constructor(n: number) {
|
|
5
|
-
this._sumTree = new Array<number>(n + 1).fill(0);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
update(i: number, delta: number) {
|
|
9
|
-
while (i < this._sumTree.length) {
|
|
10
|
-
this._sumTree[i] += delta;
|
|
11
|
-
i += BinaryIndexedTree.lowBit(i);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
getPrefixSum(i: number) {
|
|
16
|
-
let sum = 0;
|
|
17
|
-
while (i > 0) {
|
|
18
|
-
sum += this._sumTree[i];
|
|
19
|
-
i -= BinaryIndexedTree.lowBit(i);
|
|
20
|
-
}
|
|
21
|
-
return sum;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public getRangeSum(start: number, end: number): number {
|
|
25
|
-
if (!(0 <= start && start <= end && end <= this._sumTree.length))
|
|
26
|
-
throw 'Index out of bounds';
|
|
27
|
-
return this.getPrefixSum(end) - this.getPrefixSum(start);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
static lowBit(x: number) {
|
|
31
|
-
return x & (-x);
|
|
32
|
-
}
|
|
33
|
-
}
|