data-structure-typed 2.5.0 → 2.5.1
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/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
- package/CHANGELOG.md +3 -1
- package/README.md +63 -26
- package/dist/cjs/binary-tree.cjs +23698 -0
- package/dist/cjs/graph.cjs +5236 -0
- package/dist/cjs/hash.cjs +1262 -0
- package/dist/cjs/heap.cjs +1540 -0
- package/dist/cjs/index.cjs +28244 -14795
- package/dist/cjs/linked-list.cjs +4370 -0
- package/dist/cjs/matrix.cjs +1042 -0
- package/dist/cjs/priority-queue.cjs +1314 -0
- package/dist/cjs/queue.cjs +4090 -0
- package/dist/cjs/stack.cjs +861 -0
- package/dist/cjs/trie.cjs +1173 -0
- package/dist/cjs-legacy/binary-tree.cjs +23730 -0
- package/dist/cjs-legacy/graph.cjs +5234 -0
- package/dist/cjs-legacy/hash.cjs +1262 -0
- package/dist/cjs-legacy/heap.cjs +1537 -0
- package/dist/cjs-legacy/index.cjs +28239 -14790
- package/dist/cjs-legacy/linked-list.cjs +4376 -0
- package/dist/cjs-legacy/matrix.cjs +1045 -0
- package/dist/cjs-legacy/priority-queue.cjs +1312 -0
- package/dist/cjs-legacy/queue.cjs +4088 -0
- package/dist/cjs-legacy/stack.cjs +861 -0
- package/dist/cjs-legacy/trie.cjs +1172 -0
- package/dist/esm/binary-tree.mjs +23683 -0
- package/dist/esm/graph.mjs +5223 -0
- package/dist/esm/hash.mjs +1259 -0
- package/dist/esm/heap.mjs +1534 -0
- package/dist/esm/index.mjs +28242 -14796
- package/dist/esm/linked-list.mjs +4363 -0
- package/dist/esm/matrix.mjs +1038 -0
- package/dist/esm/priority-queue.mjs +1310 -0
- package/dist/esm/queue.mjs +4086 -0
- package/dist/esm/stack.mjs +859 -0
- package/dist/esm/trie.mjs +1170 -0
- package/dist/esm-legacy/binary-tree.mjs +23715 -0
- package/dist/esm-legacy/graph.mjs +5221 -0
- package/dist/esm-legacy/hash.mjs +1259 -0
- package/dist/esm-legacy/heap.mjs +1531 -0
- package/dist/esm-legacy/index.mjs +28237 -14791
- package/dist/esm-legacy/linked-list.mjs +4369 -0
- package/dist/esm-legacy/matrix.mjs +1041 -0
- package/dist/esm-legacy/priority-queue.mjs +1308 -0
- package/dist/esm-legacy/queue.mjs +4084 -0
- package/dist/esm-legacy/stack.mjs +859 -0
- package/dist/esm-legacy/trie.mjs +1169 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +240 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +280 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +502 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +481 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +380 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +120 -1
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +2764 -384
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2753 -353
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2241 -321
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +2554 -274
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +200 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +180 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -10
- package/dist/types/data-structures/heap/heap.d.ts +280 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +343 -3
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +303 -3
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +362 -2
- package/dist/types/data-structures/matrix/matrix.d.ts +160 -0
- package/dist/types/data-structures/queue/deque.d.ts +304 -4
- package/dist/types/data-structures/queue/queue.d.ts +240 -0
- package/dist/types/data-structures/stack/stack.d.ts +200 -0
- package/dist/types/data-structures/trie/trie.d.ts +244 -4
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +28022 -14573
- package/dist/umd/data-structure-typed.min.js +9 -3
- package/docs-site-docusaurus/README.md +41 -0
- package/docs-site-docusaurus/docs/api/README.md +52 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6130 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
- package/docs-site-docusaurus/docs/api/classes/BST.md +5831 -0
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
- package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
- package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
- package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6374 -0
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
- package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1257 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1475 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1117 -0
- package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
- package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/architecture.md +613 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +420 -0
- package/docs-site-docusaurus/docs/guide/guides.md +611 -0
- package/docs-site-docusaurus/docs/guide/installation.md +60 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +823 -0
- package/docs-site-docusaurus/docs/guide/overview.md +638 -0
- package/docs-site-docusaurus/docs/guide/performance.md +833 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +73 -0
- package/docs-site-docusaurus/docusaurus.config.ts +159 -0
- package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
- package/docs-site-docusaurus/package-lock.json +18667 -0
- package/docs-site-docusaurus/package.json +50 -0
- package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
- package/docs-site-docusaurus/sidebars.ts +23 -0
- package/docs-site-docusaurus/sort-protected.mjs +87 -0
- package/docs-site-docusaurus/src/css/custom.css +96 -0
- package/docs-site-docusaurus/src/pages/index.module.css +13 -0
- package/docs-site-docusaurus/src/pages/index.tsx +71 -0
- package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
- package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
- package/docs-site-docusaurus/static/.nojekyll +0 -0
- package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
- package/docs-site-docusaurus/static/img/favicon.ico +0 -0
- package/docs-site-docusaurus/static/img/favicon.png +0 -0
- package/docs-site-docusaurus/static/img/logo-180.png +0 -0
- package/docs-site-docusaurus/static/img/logo.jpg +0 -0
- package/docs-site-docusaurus/static/img/logo.png +0 -0
- package/docs-site-docusaurus/static/img/logo.svg +1 -0
- package/docs-site-docusaurus/static/img/og-image.png +0 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docs-site-docusaurus/static/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/package.json +106 -10
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +240 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +281 -1
- package/src/data-structures/binary-tree/binary-tree.ts +502 -2
- package/src/data-structures/binary-tree/bst.ts +481 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +380 -0
- package/src/data-structures/binary-tree/segment-tree.ts +121 -2
- package/src/data-structures/binary-tree/tree-map.ts +2862 -482
- package/src/data-structures/binary-tree/tree-multi-map.ts +2963 -563
- package/src/data-structures/binary-tree/tree-multi-set.ts +2399 -479
- package/src/data-structures/binary-tree/tree-set.ts +2685 -405
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +200 -0
- package/src/data-structures/graph/undirected-graph.ts +180 -0
- package/src/data-structures/hash/hash-map.ts +235 -15
- package/src/data-structures/heap/heap.ts +280 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +343 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +303 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +362 -2
- package/src/data-structures/matrix/matrix.ts +161 -1
- package/src/data-structures/queue/deque.ts +305 -5
- package/src/data-structures/queue/queue.ts +240 -0
- package/src/data-structures/stack/stack.ts +200 -0
- package/src/data-structures/trie/trie.ts +245 -5
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
- package/vercel.json +6 -0
- package/dist/leetcode/avl-tree-counter.mjs +0 -2957
- package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
- package/dist/leetcode/avl-tree.mjs +0 -2720
- package/dist/leetcode/binary-tree.mjs +0 -1594
- package/dist/leetcode/bst.mjs +0 -2398
- package/dist/leetcode/deque.mjs +0 -683
- package/dist/leetcode/directed-graph.mjs +0 -1733
- package/dist/leetcode/doubly-linked-list.mjs +0 -709
- package/dist/leetcode/hash-map.mjs +0 -493
- package/dist/leetcode/heap.mjs +0 -542
- package/dist/leetcode/max-heap.mjs +0 -375
- package/dist/leetcode/max-priority-queue.mjs +0 -383
- package/dist/leetcode/min-heap.mjs +0 -363
- package/dist/leetcode/min-priority-queue.mjs +0 -371
- package/dist/leetcode/priority-queue.mjs +0 -363
- package/dist/leetcode/queue.mjs +0 -943
- package/dist/leetcode/red-black-tree.mjs +0 -2765
- package/dist/leetcode/singly-linked-list.mjs +0 -754
- package/dist/leetcode/stack.mjs +0 -217
- package/dist/leetcode/tree-counter.mjs +0 -3039
- package/dist/leetcode/tree-multi-map.mjs +0 -2913
- package/dist/leetcode/trie.mjs +0 -413
- package/dist/leetcode/undirected-graph.mjs +0 -1650
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
[**data-structure-typed**](../README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[data-structure-typed](../README.md) / BinaryIndexedTree
|
|
6
|
+
|
|
7
|
+
# Class: BinaryIndexedTree
|
|
8
|
+
|
|
9
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:30](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L30)
|
|
10
|
+
|
|
11
|
+
Binary Indexed Tree (Fenwick Tree).
|
|
12
|
+
|
|
13
|
+
Efficient prefix sums and point updates in O(log n).
|
|
14
|
+
Standard array-based implementation per C++ competitive programming conventions.
|
|
15
|
+
|
|
16
|
+
All indices are 0-based externally; internally converted to 1-based for BIT arithmetic.
|
|
17
|
+
|
|
18
|
+
## Example
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
const bit = new BinaryIndexedTree(6);
|
|
22
|
+
bit.update(0, 3); // index 0 += 3
|
|
23
|
+
bit.update(1, 2); // index 1 += 2
|
|
24
|
+
bit.update(2, 7); // index 2 += 7
|
|
25
|
+
|
|
26
|
+
bit.query(2); // prefix sum [0..2] = 12
|
|
27
|
+
bit.queryRange(1, 2); // range sum [1..2] = 9
|
|
28
|
+
bit.get(1); // point value at index 1 = 2
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Implements
|
|
32
|
+
|
|
33
|
+
- `Iterable`\<`number`\>
|
|
34
|
+
|
|
35
|
+
## Constructors
|
|
36
|
+
|
|
37
|
+
### Constructor
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
new BinaryIndexedTree(sizeOrElements): BinaryIndexedTree;
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:38](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L38)
|
|
44
|
+
|
|
45
|
+
Construct a BIT of given size (all zeros), or from an initial values array.
|
|
46
|
+
|
|
47
|
+
#### Parameters
|
|
48
|
+
|
|
49
|
+
##### sizeOrElements
|
|
50
|
+
|
|
51
|
+
`number` \| `number`[]
|
|
52
|
+
|
|
53
|
+
number of elements, or an array of initial values
|
|
54
|
+
|
|
55
|
+
#### Returns
|
|
56
|
+
|
|
57
|
+
`BinaryIndexedTree`
|
|
58
|
+
|
|
59
|
+
## Methods
|
|
60
|
+
|
|
61
|
+
### \[iterator\]()
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
iterator: IterableIterator<number>;
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:550](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L550)
|
|
68
|
+
|
|
69
|
+
Iterate over point values in index order.
|
|
70
|
+
|
|
71
|
+
#### Returns
|
|
72
|
+
|
|
73
|
+
`IterableIterator`\<`number`\>
|
|
74
|
+
|
|
75
|
+
#### Implementation of
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
Iterable.[iterator]
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
***
|
|
82
|
+
|
|
83
|
+
### get()
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
get(index): number;
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:239](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L239)
|
|
90
|
+
|
|
91
|
+
Get the point value at index (0-based).
|
|
92
|
+
Time: O(log n)
|
|
93
|
+
|
|
94
|
+
*
|
|
95
|
+
|
|
96
|
+
#### Parameters
|
|
97
|
+
|
|
98
|
+
##### index
|
|
99
|
+
|
|
100
|
+
`number`
|
|
101
|
+
|
|
102
|
+
#### Returns
|
|
103
|
+
|
|
104
|
+
`number`
|
|
105
|
+
|
|
106
|
+
#### Example
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
// Get value at index
|
|
110
|
+
const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
111
|
+
console.log(bit.get(0)); // 1;
|
|
112
|
+
console.log(bit.get(2)); // 3;
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
***
|
|
116
|
+
|
|
117
|
+
### lowerBound()
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
lowerBound(sum): number;
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:432](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L432)
|
|
124
|
+
|
|
125
|
+
Find the smallest index i such that prefix sum [0..i] >= sum.
|
|
126
|
+
Requires all values to be non-negative (behavior undefined otherwise).
|
|
127
|
+
Returns size if no such index exists.
|
|
128
|
+
Time: O(log n)
|
|
129
|
+
|
|
130
|
+
*
|
|
131
|
+
|
|
132
|
+
#### Parameters
|
|
133
|
+
|
|
134
|
+
##### sum
|
|
135
|
+
|
|
136
|
+
`number`
|
|
137
|
+
|
|
138
|
+
#### Returns
|
|
139
|
+
|
|
140
|
+
`number`
|
|
141
|
+
|
|
142
|
+
#### Example
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
// Find index with prefix sum ≥ target
|
|
146
|
+
const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
147
|
+
const idx = bit.lowerBound(4);
|
|
148
|
+
console.log(idx); // >= 0;
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
***
|
|
152
|
+
|
|
153
|
+
### query()
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
query(index): number;
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:301](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L301)
|
|
160
|
+
|
|
161
|
+
Prefix sum query: returns sum of elements [0..index] (inclusive, 0-based).
|
|
162
|
+
Time: O(log n)
|
|
163
|
+
|
|
164
|
+
*
|
|
165
|
+
|
|
166
|
+
#### Parameters
|
|
167
|
+
|
|
168
|
+
##### index
|
|
169
|
+
|
|
170
|
+
`number`
|
|
171
|
+
|
|
172
|
+
#### Returns
|
|
173
|
+
|
|
174
|
+
`number`
|
|
175
|
+
|
|
176
|
+
#### Example
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
// Prefix sum
|
|
180
|
+
const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
181
|
+
console.log(bit.query(2)); // 6;
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
***
|
|
185
|
+
|
|
186
|
+
### queryRange()
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
queryRange(start, end): number;
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:362](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L362)
|
|
193
|
+
|
|
194
|
+
Range sum query: returns sum of elements [start..end] (inclusive, 0-based).
|
|
195
|
+
Time: O(log n)
|
|
196
|
+
|
|
197
|
+
*
|
|
198
|
+
|
|
199
|
+
#### Parameters
|
|
200
|
+
|
|
201
|
+
##### start
|
|
202
|
+
|
|
203
|
+
`number`
|
|
204
|
+
|
|
205
|
+
##### end
|
|
206
|
+
|
|
207
|
+
`number`
|
|
208
|
+
|
|
209
|
+
#### Returns
|
|
210
|
+
|
|
211
|
+
`number`
|
|
212
|
+
|
|
213
|
+
#### Example
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
// Range sum
|
|
217
|
+
const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
218
|
+
console.log(bit.queryRange(1, 2)); // 5;
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
***
|
|
222
|
+
|
|
223
|
+
### set()
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
set(index, value): void;
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:176](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L176)
|
|
230
|
+
|
|
231
|
+
Point set: set the value at index to an absolute value (0-based).
|
|
232
|
+
Time: O(log n)
|
|
233
|
+
|
|
234
|
+
*
|
|
235
|
+
|
|
236
|
+
#### Parameters
|
|
237
|
+
|
|
238
|
+
##### index
|
|
239
|
+
|
|
240
|
+
`number`
|
|
241
|
+
|
|
242
|
+
##### value
|
|
243
|
+
|
|
244
|
+
`number`
|
|
245
|
+
|
|
246
|
+
#### Returns
|
|
247
|
+
|
|
248
|
+
`void`
|
|
249
|
+
|
|
250
|
+
#### Example
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
// Set value at index
|
|
254
|
+
const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
255
|
+
bit.set(1, 10);
|
|
256
|
+
console.log(bit.get(1)); // 10;
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
***
|
|
260
|
+
|
|
261
|
+
### toArray()
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
toArray(): number[];
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:539](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L539)
|
|
268
|
+
|
|
269
|
+
Returns the point values as a plain array.
|
|
270
|
+
Time: O(n log n)
|
|
271
|
+
|
|
272
|
+
*
|
|
273
|
+
|
|
274
|
+
#### Returns
|
|
275
|
+
|
|
276
|
+
`number`[]
|
|
277
|
+
|
|
278
|
+
#### Example
|
|
279
|
+
|
|
280
|
+
```ts
|
|
281
|
+
// Convert to array
|
|
282
|
+
const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
283
|
+
console.log(bit.toArray()); // [1, 2, 3];
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
***
|
|
287
|
+
|
|
288
|
+
### update()
|
|
289
|
+
|
|
290
|
+
```ts
|
|
291
|
+
update(index, delta): void;
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:113](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L113)
|
|
295
|
+
|
|
296
|
+
Point update: add delta to the value at index (0-based).
|
|
297
|
+
Time: O(log n)
|
|
298
|
+
|
|
299
|
+
*
|
|
300
|
+
|
|
301
|
+
#### Parameters
|
|
302
|
+
|
|
303
|
+
##### index
|
|
304
|
+
|
|
305
|
+
`number`
|
|
306
|
+
|
|
307
|
+
##### delta
|
|
308
|
+
|
|
309
|
+
`number`
|
|
310
|
+
|
|
311
|
+
#### Returns
|
|
312
|
+
|
|
313
|
+
`void`
|
|
314
|
+
|
|
315
|
+
#### Example
|
|
316
|
+
|
|
317
|
+
```ts
|
|
318
|
+
// Add delta at index
|
|
319
|
+
const bit = new BinaryIndexedTree([1, 2, 3, 4, 5]);
|
|
320
|
+
bit.update(2, 7);
|
|
321
|
+
console.log(bit.get(2)); // 10;
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
***
|
|
325
|
+
|
|
326
|
+
### upperBound()
|
|
327
|
+
|
|
328
|
+
```ts
|
|
329
|
+
upperBound(sum): number;
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:478](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L478)
|
|
333
|
+
|
|
334
|
+
Find the smallest index i such that prefix sum [0..i] > sum.
|
|
335
|
+
Requires all values to be non-negative (behavior undefined otherwise).
|
|
336
|
+
Returns size if no such index exists.
|
|
337
|
+
Time: O(log n)
|
|
338
|
+
|
|
339
|
+
*
|
|
340
|
+
|
|
341
|
+
#### Parameters
|
|
342
|
+
|
|
343
|
+
##### sum
|
|
344
|
+
|
|
345
|
+
`number`
|
|
346
|
+
|
|
347
|
+
#### Returns
|
|
348
|
+
|
|
349
|
+
`number`
|
|
350
|
+
|
|
351
|
+
#### Example
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
// Find index with prefix sum > target
|
|
355
|
+
const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
356
|
+
const idx = bit.upperBound(4);
|
|
357
|
+
console.log(idx); // >= 0;
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
---
|
|
362
|
+
|
|
363
|
+
## Protected Members
|
|
364
|
+
|
|
365
|
+
### \_highBit()
|
|
366
|
+
|
|
367
|
+
```ts
|
|
368
|
+
protected _highBit(n): number;
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:620](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L620)
|
|
372
|
+
|
|
373
|
+
Returns highest power of 2 <= n.
|
|
374
|
+
|
|
375
|
+
#### Parameters
|
|
376
|
+
|
|
377
|
+
##### n
|
|
378
|
+
|
|
379
|
+
`number`
|
|
380
|
+
|
|
381
|
+
#### Returns
|
|
382
|
+
|
|
383
|
+
`number`
|
|
384
|
+
|
|
385
|
+
***
|
|
386
|
+
|
|
387
|
+
### \_pointQuery()
|
|
388
|
+
|
|
389
|
+
```ts
|
|
390
|
+
protected _pointQuery(pos): number;
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:599](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L599)
|
|
394
|
+
|
|
395
|
+
1-based point query: get exact value at pos.
|
|
396
|
+
|
|
397
|
+
#### Parameters
|
|
398
|
+
|
|
399
|
+
##### pos
|
|
400
|
+
|
|
401
|
+
`number`
|
|
402
|
+
|
|
403
|
+
#### Returns
|
|
404
|
+
|
|
405
|
+
`number`
|
|
406
|
+
|
|
407
|
+
***
|
|
408
|
+
|
|
409
|
+
### \_pointUpdate()
|
|
410
|
+
|
|
411
|
+
```ts
|
|
412
|
+
protected _pointUpdate(pos, delta): void;
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:591](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L591)
|
|
416
|
+
|
|
417
|
+
1-based point update: add delta to position pos.
|
|
418
|
+
|
|
419
|
+
#### Parameters
|
|
420
|
+
|
|
421
|
+
##### pos
|
|
422
|
+
|
|
423
|
+
`number`
|
|
424
|
+
|
|
425
|
+
##### delta
|
|
426
|
+
|
|
427
|
+
`number`
|
|
428
|
+
|
|
429
|
+
#### Returns
|
|
430
|
+
|
|
431
|
+
`void`
|
|
432
|
+
|
|
433
|
+
***
|
|
434
|
+
|
|
435
|
+
### \_prefixSum()
|
|
436
|
+
|
|
437
|
+
```ts
|
|
438
|
+
protected _prefixSum(pos): number;
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
Defined in: [data-structures/binary-tree/binary-indexed-tree.ts:581](https://github.com/zrwusa/data-structure-typed/blob/2f6ceb3aee852228efc88b111e19c0809753e805/src/data-structures/binary-tree/binary-indexed-tree.ts#L581)
|
|
442
|
+
|
|
443
|
+
1-based prefix sum up to pos (inclusive).
|
|
444
|
+
|
|
445
|
+
#### Parameters
|
|
446
|
+
|
|
447
|
+
##### pos
|
|
448
|
+
|
|
449
|
+
`number`
|
|
450
|
+
|
|
451
|
+
#### Returns
|
|
452
|
+
|
|
453
|
+
`number`
|
|
454
|
+
|
|
455
|
+
***
|