data-structure-typed 0.9.16 → 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 +665 -172
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- 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 -14
- package/dist/data-structures/binary-tree/avl-tree.js +142 -116
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
- package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
- package/dist/data-structures/binary-tree/bst.d.ts +117 -23
- package/dist/data-structures/binary-tree/bst.js +233 -240
- 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 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
- package/dist/data-structures/binary-tree/segment-tree.js +126 -93
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
- package/dist/data-structures/graph/abstract-graph.js +610 -572
- package/dist/data-structures/graph/directed-graph.d.ts +173 -16
- package/dist/data-structures/graph/directed-graph.js +345 -313
- 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 -9
- package/dist/data-structures/graph/undirected-graph.js +203 -178
- package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
- package/dist/data-structures/hash/coordinate-map.js +59 -36
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +49 -33
- 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/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 -45
- package/dist/data-structures/heap/heap.js +124 -86
- package/dist/data-structures/heap/max-heap.d.ts +13 -5
- package/dist/data-structures/heap/max-heap.js +18 -28
- package/dist/data-structures/heap/min-heap.d.ts +14 -5
- package/dist/data-structures/heap/min-heap.js +19 -28
- package/dist/data-structures/index.d.ts +1 -1
- package/dist/data-structures/index.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
- 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 -315
- package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
- 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 +21 -8
- package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
- package/dist/data-structures/matrix/matrix2d.js +146 -80
- package/dist/data-structures/matrix/navigator.d.ts +36 -1
- package/dist/data-structures/matrix/navigator.js +46 -37
- package/dist/data-structures/matrix/vector2d.d.ts +142 -15
- package/dist/data-structures/matrix/vector2d.js +215 -109
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
- package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
- package/dist/data-structures/priority-queue/priority-queue.js +244 -143
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +200 -82
- package/dist/data-structures/queue/queue.d.ts +65 -38
- package/dist/data-structures/queue/queue.js +110 -66
- package/dist/data-structures/stack/stack.d.ts +27 -32
- package/dist/data-structures/stack/stack.js +47 -53
- 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/data-structures/tree/tree.js +60 -0
- package/dist/data-structures/trie/trie.d.ts +33 -10
- package/dist/data-structures/trie/trie.js +123 -208
- 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-graph.d.ts +17 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
- package/dist/interfaces/index.js +31 -0
- package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -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.d.ts +1 -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/{data-structures/types → types/data-structures}/index.d.ts +3 -1
- package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
- 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.js +2 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -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 +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +17 -103
- package/dist/utils/utils.js +40 -625
- package/package.json +96 -23
- 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 -16
- package/dist/data-structures/trampoline.js +0 -130
- package/dist/data-structures/types/abstract-graph.d.ts +0 -29
- package/dist/data-structures/types/avl-tree.d.ts +0 -5
- package/dist/data-structures/types/binary-tree.d.ts +0 -16
- package/dist/data-structures/types/bst.d.ts +0 -7
- package/dist/data-structures/types/directed-graph.d.ts +0 -10
- package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/types/heap.d.ts +0 -7
- package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
- package/dist/data-structures/types/tree-multiset.d.ts +0 -5
- package/dist/data-structures/types/utils.d.ts +0 -52
- package/dist/data-structures/types/utils.js +0 -54
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -227
- 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 -1133
- package/src/data-structures/binary-tree/bst.ts +0 -395
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- 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 -172
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -7
- package/src/data-structures/graph/abstract-graph.ts +0 -753
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/directed-graph.ts +0 -306
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -155
- 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/index.ts +0 -6
- package/src/data-structures/heap/heap.ts +0 -127
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -23
- package/src/data-structures/heap/min-heap.ts +0 -25
- package/src/data-structures/index.ts +0 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
- 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 -87
- 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 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/priority-queue.ts +0 -200
- 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 -122
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -103
- package/src/data-structures/trampoline.ts +0 -51
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -203
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/navigator.ts +0 -12
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -15
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/data-structures/types/utils.ts +0 -173
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -505
- package/tsconfig.json +0 -56
- /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
- /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
- /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
- /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
- /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
- /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
- /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
- /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
- /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
- /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
- /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
- /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
|
@@ -1,17 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
1
8
|
import { BST, BSTNode } from './bst';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
9
|
+
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId } from '../../types';
|
|
10
|
+
import { IAVLTree, IAVLTreeNode } from '../../interfaces';
|
|
11
|
+
export declare class AVLTreeNode<T = any, NEIGHBOR extends AVLTreeNode<T, NEIGHBOR> = AVLTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IAVLTreeNode<T, NEIGHBOR> {
|
|
12
|
+
constructor(id: BinaryTreeNodeId, val?: T);
|
|
5
13
|
}
|
|
6
|
-
export declare class AVLTree<
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
|
|
15
|
+
/**
|
|
16
|
+
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
17
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
18
|
+
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
19
|
+
* options.
|
|
20
|
+
*/
|
|
21
|
+
constructor(options?: AVLTreeOptions);
|
|
22
|
+
/**
|
|
23
|
+
* The function creates a new AVL tree node with the given id and value.
|
|
24
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
25
|
+
* identify each node in the tree.
|
|
26
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
27
|
+
* that will be stored in the node.
|
|
28
|
+
* @returns a new AVLTreeNode object with the specified id and value.
|
|
29
|
+
*/
|
|
30
|
+
createNode(id: BinaryTreeNodeId, val?: N['val']): N;
|
|
31
|
+
/**
|
|
32
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
33
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
34
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
35
|
+
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
36
|
+
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
37
|
+
*/
|
|
38
|
+
add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
41
|
+
* then balances the tree if necessary.
|
|
42
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
43
|
+
* removed from the AVL tree.
|
|
44
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
45
|
+
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
46
|
+
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
47
|
+
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
48
|
+
*/
|
|
49
|
+
remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): BinaryTreeDeletedResult<N>[];
|
|
50
|
+
/**
|
|
51
|
+
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
52
|
+
* height of its right subtree.
|
|
53
|
+
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
54
|
+
* @returns The balance factor of the given AVL tree node.
|
|
55
|
+
*/
|
|
56
|
+
protected _balanceFactor(node: N): number;
|
|
57
|
+
/**
|
|
58
|
+
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
59
|
+
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
60
|
+
*/
|
|
61
|
+
protected _updateHeight(node: N): void;
|
|
62
|
+
/**
|
|
63
|
+
* The `_balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
64
|
+
* each node in the path from the given node to the root.
|
|
65
|
+
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
66
|
+
*/
|
|
67
|
+
protected _balancePath(node: N): void;
|
|
68
|
+
/**
|
|
69
|
+
* The `_balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
70
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
71
|
+
*/
|
|
72
|
+
protected _balanceLL(A: N): void;
|
|
73
|
+
/**
|
|
74
|
+
* The `_balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
75
|
+
* @param A - A is an AVLTreeNode object.
|
|
76
|
+
*/
|
|
77
|
+
protected _balanceLR(A: N): void;
|
|
78
|
+
/**
|
|
79
|
+
* The `_balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
80
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
81
|
+
*/
|
|
82
|
+
protected _balanceRR(A: N): void;
|
|
83
|
+
/**
|
|
84
|
+
* The `_balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
85
|
+
* @param A - A is an AVLTreeNode object.
|
|
86
|
+
*/
|
|
87
|
+
protected _balanceRL(A: N): void;
|
|
17
88
|
}
|
|
@@ -1,130 +1,145 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __values = (this && this.__values) || function(o) {
|
|
18
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
19
|
-
if (m) return m.call(o);
|
|
20
|
-
if (o && typeof o.length === "number") return {
|
|
21
|
-
next: function () {
|
|
22
|
-
if (o && i >= o.length) o = void 0;
|
|
23
|
-
return { value: o && o[i++], done: !o };
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
27
|
-
};
|
|
28
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
3
|
exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
11
|
+
const bst_1 = require("./bst");
|
|
12
|
+
class AVLTreeNode extends bst_1.BSTNode {
|
|
13
|
+
constructor(id, val) {
|
|
14
|
+
super(id, val);
|
|
35
15
|
}
|
|
36
|
-
|
|
37
|
-
return new AVLTreeNode(this.id, this.val, this.count);
|
|
38
|
-
};
|
|
39
|
-
return AVLTreeNode;
|
|
40
|
-
}(bst_1.BSTNode));
|
|
16
|
+
}
|
|
41
17
|
exports.AVLTreeNode = AVLTreeNode;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
18
|
+
class AVLTree extends bst_1.BST {
|
|
19
|
+
/**
|
|
20
|
+
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
21
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
22
|
+
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
23
|
+
* options.
|
|
24
|
+
*/
|
|
25
|
+
constructor(options) {
|
|
26
|
+
super(options);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The function creates a new AVL tree node with the given id and value.
|
|
30
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
31
|
+
* identify each node in the tree.
|
|
32
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
33
|
+
* that will be stored in the node.
|
|
34
|
+
* @returns a new AVLTreeNode object with the specified id and value.
|
|
35
|
+
*/
|
|
36
|
+
createNode(id, val) {
|
|
37
|
+
return new AVLTreeNode(id, val);
|
|
46
38
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
39
|
+
/**
|
|
40
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
41
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
42
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
43
|
+
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
44
|
+
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
45
|
+
*/
|
|
46
|
+
add(id, val) {
|
|
47
|
+
// TODO support node as a param
|
|
48
|
+
const inserted = super.add(id, val);
|
|
52
49
|
if (inserted)
|
|
53
|
-
this.
|
|
50
|
+
this._balancePath(inserted);
|
|
54
51
|
return inserted;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
55
|
+
* then balances the tree if necessary.
|
|
56
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
57
|
+
* removed from the AVL tree.
|
|
58
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
59
|
+
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
60
|
+
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
61
|
+
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
62
|
+
*/
|
|
63
|
+
remove(id, isUpdateAllLeftSum) {
|
|
64
|
+
const deletedResults = super.remove(id, isUpdateAllLeftSum);
|
|
65
|
+
for (const { needBalanced } of deletedResults) {
|
|
66
|
+
if (needBalanced) {
|
|
67
|
+
this._balancePath(needBalanced);
|
|
71
68
|
}
|
|
72
|
-
finally { if (e_1) throw e_1.error; }
|
|
73
69
|
}
|
|
74
70
|
return deletedResults;
|
|
75
|
-
}
|
|
76
|
-
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
74
|
+
* height of its right subtree.
|
|
75
|
+
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
76
|
+
* @returns The balance factor of the given AVL tree node.
|
|
77
|
+
*/
|
|
78
|
+
_balanceFactor(node) {
|
|
77
79
|
if (!node.right) // node has no right subtree
|
|
78
80
|
return -node.height;
|
|
79
81
|
else if (!node.left) // node has no left subtree
|
|
80
82
|
return +node.height;
|
|
81
83
|
else
|
|
82
84
|
return node.right.height - node.left.height;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
88
|
+
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
89
|
+
*/
|
|
90
|
+
_updateHeight(node) {
|
|
85
91
|
if (!node.left && !node.right) // node is a leaf
|
|
86
92
|
node.height = 0;
|
|
87
93
|
else if (!node.left) {
|
|
88
94
|
// node has no left subtree
|
|
89
|
-
|
|
95
|
+
const rightHeight = node.right ? node.right.height : 0;
|
|
90
96
|
node.height = 1 + rightHeight;
|
|
91
97
|
}
|
|
92
98
|
else if (!node.right) // node has no right subtree
|
|
93
99
|
node.height = 1 + node.left.height;
|
|
94
100
|
else
|
|
95
101
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The `_balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
105
|
+
* each node in the path from the given node to the root.
|
|
106
|
+
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
107
|
+
*/
|
|
108
|
+
_balancePath(node) {
|
|
109
|
+
const path = this.getPathToRoot(node);
|
|
110
|
+
for (let i = path.length - 1; i >= 0; i--) {
|
|
111
|
+
const A = path[i];
|
|
112
|
+
this._updateHeight(A);
|
|
113
|
+
switch (this._balanceFactor(A)) {
|
|
103
114
|
case -2:
|
|
104
115
|
if (A && A.left) {
|
|
105
|
-
if (this.
|
|
106
|
-
this.
|
|
116
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
117
|
+
this._balanceLL(A); // Perform LL rotation
|
|
107
118
|
}
|
|
108
119
|
else {
|
|
109
|
-
this.
|
|
120
|
+
this._balanceLR(A); // Perform LR rotation
|
|
110
121
|
}
|
|
111
122
|
}
|
|
112
123
|
break;
|
|
113
124
|
case +2:
|
|
114
125
|
if (A && A.right) {
|
|
115
|
-
if (this.
|
|
116
|
-
this.
|
|
126
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
127
|
+
this._balanceRR(A); // Perform RR rotation
|
|
117
128
|
}
|
|
118
129
|
else {
|
|
119
|
-
this.
|
|
130
|
+
this._balanceRL(A); // Perform RL rotation
|
|
120
131
|
}
|
|
121
132
|
}
|
|
122
133
|
}
|
|
123
134
|
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The `_balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
138
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
139
|
+
*/
|
|
140
|
+
_balanceLL(A) {
|
|
141
|
+
const parentOfA = A.parent;
|
|
142
|
+
const B = A.left; // A is left-heavy and B is left-heavy
|
|
128
143
|
A.parent = B;
|
|
129
144
|
if (B && B.right) {
|
|
130
145
|
B.right.parent = A;
|
|
@@ -133,7 +148,7 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
133
148
|
B.parent = parentOfA;
|
|
134
149
|
if (A === this.root) {
|
|
135
150
|
if (B)
|
|
136
|
-
this.
|
|
151
|
+
this._setRoot(B);
|
|
137
152
|
}
|
|
138
153
|
else {
|
|
139
154
|
if ((parentOfA === null || parentOfA === void 0 ? void 0 : parentOfA.left) === A) {
|
|
@@ -148,14 +163,18 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
148
163
|
A.left = B.right; // Make T2 the left subtree of A
|
|
149
164
|
B.right = A; // Make A the left child of B
|
|
150
165
|
}
|
|
151
|
-
this.
|
|
166
|
+
this._updateHeight(A);
|
|
152
167
|
if (B)
|
|
153
|
-
this.
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
168
|
+
this._updateHeight(B);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The `_balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
172
|
+
* @param A - A is an AVLTreeNode object.
|
|
173
|
+
*/
|
|
174
|
+
_balanceLR(A) {
|
|
175
|
+
const parentOfA = A.parent;
|
|
176
|
+
const B = A.left; // A is left-heavy
|
|
177
|
+
let C = null;
|
|
159
178
|
if (B) {
|
|
160
179
|
C = B.right; // B is right-heavy
|
|
161
180
|
}
|
|
@@ -174,7 +193,7 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
174
193
|
}
|
|
175
194
|
if (A === this.root) {
|
|
176
195
|
if (C)
|
|
177
|
-
this.
|
|
196
|
+
this._setRoot(C);
|
|
178
197
|
}
|
|
179
198
|
else {
|
|
180
199
|
if (parentOfA) {
|
|
@@ -193,13 +212,17 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
193
212
|
C.left = B;
|
|
194
213
|
C.right = A;
|
|
195
214
|
}
|
|
196
|
-
this.
|
|
197
|
-
B && this.
|
|
198
|
-
C && this.
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
215
|
+
this._updateHeight(A); // Adjust heights
|
|
216
|
+
B && this._updateHeight(B);
|
|
217
|
+
C && this._updateHeight(C);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* The `_balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
221
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
222
|
+
*/
|
|
223
|
+
_balanceRR(A) {
|
|
224
|
+
const parentOfA = A.parent;
|
|
225
|
+
const B = A.right; // A is right-heavy and B is right-heavy
|
|
203
226
|
A.parent = B;
|
|
204
227
|
if (B) {
|
|
205
228
|
if (B.left) {
|
|
@@ -209,7 +232,7 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
209
232
|
}
|
|
210
233
|
if (A === this.root) {
|
|
211
234
|
if (B)
|
|
212
|
-
this.
|
|
235
|
+
this._setRoot(B);
|
|
213
236
|
}
|
|
214
237
|
else {
|
|
215
238
|
if (parentOfA) {
|
|
@@ -225,13 +248,17 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
225
248
|
A.right = B.left; // Make T2 the right subtree of A
|
|
226
249
|
B.left = A;
|
|
227
250
|
}
|
|
228
|
-
this.
|
|
229
|
-
B && this.
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
251
|
+
this._updateHeight(A);
|
|
252
|
+
B && this._updateHeight(B);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* The `_balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
256
|
+
* @param A - A is an AVLTreeNode object.
|
|
257
|
+
*/
|
|
258
|
+
_balanceRL(A) {
|
|
259
|
+
const parentOfA = A.parent;
|
|
260
|
+
const B = A.right; // A is right-heavy
|
|
261
|
+
let C = null;
|
|
235
262
|
if (B) {
|
|
236
263
|
C = B.left; // B is left-heavy
|
|
237
264
|
}
|
|
@@ -249,7 +276,7 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
249
276
|
}
|
|
250
277
|
if (A === this.root) {
|
|
251
278
|
if (C)
|
|
252
|
-
this.
|
|
279
|
+
this._setRoot(C);
|
|
253
280
|
}
|
|
254
281
|
else {
|
|
255
282
|
if (parentOfA) {
|
|
@@ -269,10 +296,9 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
269
296
|
C.left = A;
|
|
270
297
|
if (C)
|
|
271
298
|
C.right = B;
|
|
272
|
-
this.
|
|
273
|
-
B && this.
|
|
274
|
-
C && this.
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
}(bst_1.BST));
|
|
299
|
+
this._updateHeight(A); // Adjust heights
|
|
300
|
+
B && this._updateHeight(B);
|
|
301
|
+
C && this._updateHeight(C);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
278
304
|
exports.AVLTree = AVLTree;
|
|
@@ -1,8 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
1
8
|
export declare class BinaryIndexedTree {
|
|
2
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The constructor initializes an array with a specified length and fills it with zeros.
|
|
11
|
+
* @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
|
|
12
|
+
* sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
|
|
13
|
+
* The size of the sum tree array is `n + 1` because
|
|
14
|
+
*/
|
|
3
15
|
constructor(n: number);
|
|
16
|
+
private _sumTree;
|
|
17
|
+
get sumTree(): number[];
|
|
4
18
|
static lowBit(x: number): number;
|
|
19
|
+
/**
|
|
20
|
+
* The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
|
|
21
|
+
* its ancestors.
|
|
22
|
+
* @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
|
|
23
|
+
* updated.
|
|
24
|
+
* @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
|
|
25
|
+
* at index "i" in the "_sumTree" array.
|
|
26
|
+
*/
|
|
5
27
|
update(i: number, delta: number): void;
|
|
28
|
+
/**
|
|
29
|
+
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
30
|
+
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
31
|
+
* array for which we want to calculate the prefix sum.
|
|
32
|
+
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
33
|
+
* `i`.
|
|
34
|
+
*/
|
|
6
35
|
getPrefixSum(i: number): number;
|
|
36
|
+
/**
|
|
37
|
+
* The function `getRangeSum` calculates the sum of a range of numbers in an array.
|
|
38
|
+
* @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
|
|
39
|
+
* sum.
|
|
40
|
+
* @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
|
|
41
|
+
* the sum.
|
|
42
|
+
* @returns the sum of the elements in the range specified by the start and end indices.
|
|
43
|
+
*/
|
|
7
44
|
getRangeSum(start: number, end: number): number;
|
|
45
|
+
protected _setSumTree(value: number[]): void;
|
|
8
46
|
}
|
|
@@ -1,32 +1,73 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BinaryIndexedTree = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
11
|
+
class BinaryIndexedTree {
|
|
12
|
+
/**
|
|
13
|
+
* The constructor initializes an array with a specified length and fills it with zeros.
|
|
14
|
+
* @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
|
|
15
|
+
* sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
|
|
16
|
+
* The size of the sum tree array is `n + 1` because
|
|
17
|
+
*/
|
|
18
|
+
constructor(n) {
|
|
6
19
|
this._sumTree = new Array(n + 1).fill(0);
|
|
7
20
|
}
|
|
8
|
-
|
|
21
|
+
get sumTree() {
|
|
22
|
+
return this._sumTree;
|
|
23
|
+
}
|
|
24
|
+
static lowBit(x) {
|
|
9
25
|
return x & (-x);
|
|
10
|
-
}
|
|
11
|
-
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
|
|
29
|
+
* its ancestors.
|
|
30
|
+
* @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
|
|
31
|
+
* updated.
|
|
32
|
+
* @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
|
|
33
|
+
* at index "i" in the "_sumTree" array.
|
|
34
|
+
*/
|
|
35
|
+
update(i, delta) {
|
|
12
36
|
while (i < this._sumTree.length) {
|
|
13
37
|
this._sumTree[i] += delta;
|
|
14
38
|
i += BinaryIndexedTree.lowBit(i);
|
|
15
39
|
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
43
|
+
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
44
|
+
* array for which we want to calculate the prefix sum.
|
|
45
|
+
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
46
|
+
* `i`.
|
|
47
|
+
*/
|
|
48
|
+
getPrefixSum(i) {
|
|
49
|
+
let sum = 0;
|
|
19
50
|
while (i > 0) {
|
|
20
51
|
sum += this._sumTree[i];
|
|
21
52
|
i -= BinaryIndexedTree.lowBit(i);
|
|
22
53
|
}
|
|
23
54
|
return sum;
|
|
24
|
-
}
|
|
25
|
-
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The function `getRangeSum` calculates the sum of a range of numbers in an array.
|
|
58
|
+
* @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
|
|
59
|
+
* sum.
|
|
60
|
+
* @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
|
|
61
|
+
* the sum.
|
|
62
|
+
* @returns the sum of the elements in the range specified by the start and end indices.
|
|
63
|
+
*/
|
|
64
|
+
getRangeSum(start, end) {
|
|
26
65
|
if (!(0 <= start && start <= end && end <= this._sumTree.length))
|
|
27
66
|
throw 'Index out of bounds';
|
|
28
67
|
return this.getPrefixSum(end) - this.getPrefixSum(start);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
68
|
+
}
|
|
69
|
+
_setSumTree(value) {
|
|
70
|
+
this._sumTree = value;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
32
73
|
exports.BinaryIndexedTree = BinaryIndexedTree;
|