tree-multimap-typed 2.4.5 → 2.5.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/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/tree-multimap-typed.js +10290 -2125
- package/dist/umd/tree-multimap-typed.js.map +1 -1
- package/dist/umd/tree-multimap-typed.min.js +4 -4
- package/dist/umd/tree-multimap-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
- package/src/data-structures/binary-tree/binary-tree.ts +429 -79
- package/src/data-structures/binary-tree/bst.ts +335 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1284 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
- package/src/data-structures/binary-tree/tree-set.ts +1136 -9
- package/src/data-structures/graph/directed-graph.ts +219 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +204 -59
- package/src/data-structures/hash/hash-map.ts +230 -77
- package/src/data-structures/heap/heap.ts +287 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +416 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +272 -65
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +213 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -27,7 +27,7 @@ export declare abstract class IterableElementBase<E, R> implements Iterable<E> {
|
|
|
27
27
|
* @remarks
|
|
28
28
|
* Time O(1), Space O(1).
|
|
29
29
|
*/
|
|
30
|
-
protected
|
|
30
|
+
protected _toElementFn?: (rawElement: R) => E;
|
|
31
31
|
/**
|
|
32
32
|
* Exposes the current `toElementFn`, if configured.
|
|
33
33
|
*
|
|
@@ -80,12 +80,6 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
80
80
|
* @returns The node's color.
|
|
81
81
|
*/
|
|
82
82
|
get color(): RBTNColor;
|
|
83
|
-
/**
|
|
84
|
-
* Sets the color of the node.
|
|
85
|
-
* @remarks Time O(1), Space O(1)
|
|
86
|
-
*
|
|
87
|
-
* @param value - The new color.
|
|
88
|
-
*/
|
|
89
83
|
set color(value: RBTNColor);
|
|
90
84
|
_count: number;
|
|
91
85
|
/**
|
|
@@ -95,12 +89,6 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
95
89
|
* @returns The subtree node count.
|
|
96
90
|
*/
|
|
97
91
|
get count(): number;
|
|
98
|
-
/**
|
|
99
|
-
* Sets the count of nodes in the subtree.
|
|
100
|
-
* @remarks Time O(1), Space O(1)
|
|
101
|
-
*
|
|
102
|
-
* @param value - The new count.
|
|
103
|
-
*/
|
|
104
92
|
set count(value: number);
|
|
105
93
|
/**
|
|
106
94
|
* Gets the position of the node relative to its parent.
|
|
@@ -127,28 +115,6 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
127
115
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
128
116
|
*
|
|
129
117
|
* @example
|
|
130
|
-
* // basic AVLTree creation and add operation
|
|
131
|
-
* // Create a simple AVLTree with initial values
|
|
132
|
-
* const tree = new AVLTree([5, 2, 8, 1, 9]);
|
|
133
|
-
*
|
|
134
|
-
* tree.print();
|
|
135
|
-
* // _2___
|
|
136
|
-
* // / \
|
|
137
|
-
* // 1 _8_
|
|
138
|
-
* // / \
|
|
139
|
-
* // 5 9
|
|
140
|
-
*
|
|
141
|
-
* // Verify the tree maintains sorted order
|
|
142
|
-
* console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
|
|
143
|
-
*
|
|
144
|
-
* // Check size
|
|
145
|
-
* console.log(tree.size); // 5;
|
|
146
|
-
*
|
|
147
|
-
* // Add a new element
|
|
148
|
-
* tree.set(3);
|
|
149
|
-
* console.log(tree.size); // 6;
|
|
150
|
-
* console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
|
|
151
|
-
* @example
|
|
152
118
|
* // AVLTree has and get operations
|
|
153
119
|
* const tree = new AVLTree<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
154
120
|
*
|
|
@@ -163,23 +129,6 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
163
129
|
* // Verify tree is balanced
|
|
164
130
|
* console.log(tree.isAVLBalanced()); // true;
|
|
165
131
|
* @example
|
|
166
|
-
* // AVLTree delete and balance verification
|
|
167
|
-
* const tree = new AVLTree([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
168
|
-
*
|
|
169
|
-
* // Delete an element
|
|
170
|
-
* tree.delete(10);
|
|
171
|
-
* console.log(tree.has(10)); // false;
|
|
172
|
-
*
|
|
173
|
-
* // Tree should remain balanced after deletion
|
|
174
|
-
* console.log(tree.isAVLBalanced()); // true;
|
|
175
|
-
*
|
|
176
|
-
* // Size decreased
|
|
177
|
-
* console.log(tree.size); // 15;
|
|
178
|
-
*
|
|
179
|
-
* // Remaining elements are still sorted
|
|
180
|
-
* const keys = [...tree.keys()];
|
|
181
|
-
* console.log(keys); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16];
|
|
182
|
-
* @example
|
|
183
132
|
* // AVLTree for university ranking system with strict balance
|
|
184
133
|
* interface University {
|
|
185
134
|
* name: string;
|
|
@@ -318,6 +267,48 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
318
267
|
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
319
268
|
* @param [value] - The value, if providing just a key.
|
|
320
269
|
* @returns True if the addition was successful, false otherwise.
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
* @example
|
|
307
|
+
* // Set a key-value pair
|
|
308
|
+
* const avl = new AVLTree<number, string>();
|
|
309
|
+
* avl.set(1, 'one');
|
|
310
|
+
* avl.set(2, 'two');
|
|
311
|
+
* console.log(avl.get(1)); // 'one';
|
|
321
312
|
*/
|
|
322
313
|
set(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
323
314
|
/**
|
|
@@ -326,6 +317,45 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
326
317
|
*
|
|
327
318
|
* @param keyNodeOrEntry - The node to delete.
|
|
328
319
|
* @returns An array containing deletion results.
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
* @example
|
|
354
|
+
* // Remove nodes and verify structure
|
|
355
|
+
* const avl = new AVLTree<number>([5, 3, 7, 1, 4, 6, 8]);
|
|
356
|
+
* avl.delete(3);
|
|
357
|
+
* console.log(avl.has(3)); // false;
|
|
358
|
+
* console.log(avl.size); // 6;
|
|
329
359
|
*/
|
|
330
360
|
delete(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[];
|
|
331
361
|
/**
|
|
@@ -335,6 +365,26 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
335
365
|
*
|
|
336
366
|
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
337
367
|
* @returns True if successful, false if the tree was empty.
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
* @example
|
|
381
|
+
* // Rebalance the tree
|
|
382
|
+
* const avl = new AVLTree<number>();
|
|
383
|
+
* // Insert in sorted order (worst case for BST)
|
|
384
|
+
* for (let i = 1; i <= 7; i++) avl.add(i);
|
|
385
|
+
* console.log(avl.isAVLBalanced()); // false;
|
|
386
|
+
* avl.perfectlyBalance();
|
|
387
|
+
* console.log(avl.isAVLBalanced()); // true;
|
|
338
388
|
*/
|
|
339
389
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
340
390
|
/**
|
|
@@ -348,6 +398,33 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
348
398
|
* @param [options] - Options for the new AVLTree.
|
|
349
399
|
* @param [thisArg] - `this` context for the callback.
|
|
350
400
|
* @returns A new, mapped AVLTree.
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
* @example
|
|
424
|
+
* // Transform to new tree
|
|
425
|
+
* const avl = new AVLTree<number, number>([[1, 10], [2, 20], [3, 30]]);
|
|
426
|
+
* const doubled = avl.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
|
|
427
|
+
* console.log([...doubled.values()]); // [20, 40, 60];
|
|
351
428
|
*/
|
|
352
429
|
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
|
|
353
430
|
/**
|
|
@@ -1,174 +1,220 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Binary Indexed Tree (Fenwick Tree).
|
|
2
3
|
*
|
|
4
|
+
* Efficient prefix sums and point updates in O(log n).
|
|
5
|
+
* Standard array-based implementation per C++ competitive programming conventions.
|
|
6
|
+
*
|
|
7
|
+
* All indices are 0-based externally; internally converted to 1-based for BIT arithmetic.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const bit = new BinaryIndexedTree(6);
|
|
12
|
+
* bit.update(0, 3); // index 0 += 3
|
|
13
|
+
* bit.update(1, 2); // index 1 += 2
|
|
14
|
+
* bit.update(2, 7); // index 2 += 7
|
|
15
|
+
*
|
|
16
|
+
* bit.query(2); // prefix sum [0..2] = 12
|
|
17
|
+
* bit.queryRange(1, 2); // range sum [1..2] = 9
|
|
18
|
+
* bit.get(1); // point value at index 1 = 2
|
|
19
|
+
* ```
|
|
3
20
|
*/
|
|
4
|
-
export declare class BinaryIndexedTree {
|
|
5
|
-
protected readonly
|
|
6
|
-
protected
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
21
|
+
export declare class BinaryIndexedTree implements Iterable<number> {
|
|
22
|
+
protected readonly _size: number;
|
|
23
|
+
protected _tree: number[];
|
|
24
|
+
/**
|
|
25
|
+
* Construct a BIT of given size (all zeros), or from an initial values array.
|
|
26
|
+
* @param sizeOrElements - number of elements, or an array of initial values
|
|
27
|
+
*/
|
|
28
|
+
constructor(sizeOrElements: number | number[]);
|
|
29
|
+
/**
|
|
30
|
+
* Point update: add delta to the value at index (0-based).
|
|
31
|
+
* Time: O(log n)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
* @example
|
|
47
|
+
* // Add delta at index
|
|
48
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4, 5]);
|
|
49
|
+
* bit.update(2, 7);
|
|
50
|
+
* console.log(bit.get(2)); // 10;
|
|
51
|
+
*/
|
|
52
|
+
update(index: number, delta: number): void;
|
|
53
|
+
/**
|
|
54
|
+
* Point set: set the value at index to an absolute value (0-based).
|
|
55
|
+
* Time: O(log n)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
* @example
|
|
72
|
+
* // Set value at index
|
|
73
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
74
|
+
* bit.set(1, 10);
|
|
75
|
+
* console.log(bit.get(1)); // 10;
|
|
76
|
+
*/
|
|
77
|
+
set(index: number, value: number): void;
|
|
78
|
+
/**
|
|
79
|
+
* Get the point value at index (0-based).
|
|
80
|
+
* Time: O(log n)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
* @example
|
|
96
|
+
* // Get value at index
|
|
97
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
98
|
+
* console.log(bit.get(0)); // 1;
|
|
99
|
+
* console.log(bit.get(2)); // 3;
|
|
100
|
+
*/
|
|
101
|
+
get(index: number): number;
|
|
102
|
+
/**
|
|
103
|
+
* Prefix sum query: returns sum of elements [0..index] (inclusive, 0-based).
|
|
104
|
+
* Time: O(log n)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
* @example
|
|
121
|
+
* // Prefix sum
|
|
122
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
123
|
+
* console.log(bit.query(2)); // 6;
|
|
124
|
+
*/
|
|
125
|
+
query(index: number): number;
|
|
126
|
+
/**
|
|
127
|
+
* Range sum query: returns sum of elements [start..end] (inclusive, 0-based).
|
|
128
|
+
* Time: O(log n)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
* @example
|
|
144
|
+
* // Range sum
|
|
145
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
146
|
+
* console.log(bit.queryRange(1, 2)); // 5;
|
|
147
|
+
*/
|
|
148
|
+
queryRange(start: number, end: number): number;
|
|
149
|
+
/**
|
|
150
|
+
* Find the smallest index i such that prefix sum [0..i] >= sum.
|
|
151
|
+
* Requires all values to be non-negative (behavior undefined otherwise).
|
|
152
|
+
* Returns size if no such index exists.
|
|
153
|
+
* Time: O(log n)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
* @example
|
|
170
|
+
* // Find index with prefix sum ≥ target
|
|
171
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
172
|
+
* const idx = bit.lowerBound(4);
|
|
173
|
+
* console.log(idx); // >= 0;
|
|
84
174
|
*/
|
|
85
175
|
lowerBound(sum: number): number;
|
|
86
176
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
|
|
177
|
+
* Find the smallest index i such that prefix sum [0..i] > sum.
|
|
178
|
+
* Requires all values to be non-negative (behavior undefined otherwise).
|
|
179
|
+
* Returns size if no such index exists.
|
|
180
|
+
* Time: O(log n)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
* @example
|
|
184
|
+
* // Find index with prefix sum > target
|
|
185
|
+
* const bit = new BinaryIndexedTree([1, 2, 3, 4]);
|
|
186
|
+
* const idx = bit.upperBound(4);
|
|
187
|
+
* console.log(idx); // >= 0;
|
|
92
188
|
*/
|
|
93
189
|
upperBound(sum: number): number;
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
*
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
* that needs to be checked.
|
|
122
|
-
*/
|
|
190
|
+
get size(): number;
|
|
191
|
+
isEmpty(): boolean;
|
|
192
|
+
clear(): void;
|
|
193
|
+
clone(): BinaryIndexedTree;
|
|
194
|
+
/**
|
|
195
|
+
* Returns the point values as a plain array.
|
|
196
|
+
* Time: O(n log n)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
* @example
|
|
200
|
+
* // Convert to array
|
|
201
|
+
* const bit = new BinaryIndexedTree([1, 2, 3]);
|
|
202
|
+
* console.log(bit.toArray()); // [1, 2, 3];
|
|
203
|
+
*/
|
|
204
|
+
toArray(): number[];
|
|
205
|
+
/**
|
|
206
|
+
* Iterate over point values in index order.
|
|
207
|
+
*/
|
|
208
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
209
|
+
forEach(callback: (value: number, index: number) => void): void;
|
|
210
|
+
print(): void;
|
|
211
|
+
/** 1-based prefix sum up to pos (inclusive). */
|
|
212
|
+
protected _prefixSum(pos: number): number;
|
|
213
|
+
/** 1-based point update: add delta to position pos. */
|
|
214
|
+
protected _pointUpdate(pos: number, delta: number): void;
|
|
215
|
+
/** 1-based point query: get exact value at pos. */
|
|
216
|
+
protected _pointQuery(pos: number): number;
|
|
123
217
|
protected _checkIndex(index: number): void;
|
|
124
|
-
/**
|
|
125
|
-
|
|
126
|
-
* freqMap.
|
|
127
|
-
* @param {number} index - The `index` parameter is a number that represents the index of an element in a
|
|
128
|
-
* data structure.
|
|
129
|
-
* @returns a number.
|
|
130
|
-
*/
|
|
131
|
-
protected _readSingle(index: number): number;
|
|
132
|
-
/**
|
|
133
|
-
* The function `_updateNegativeCount` updates a counter based on changes in frequency values.
|
|
134
|
-
* @param {number} freqCur - The current frequency value.
|
|
135
|
-
* @param {number} freqNew - The freqNew parameter represents the new frequency value.
|
|
136
|
-
*/
|
|
137
|
-
protected _updateNegativeCount(freqCur: number, freqNew: number): void;
|
|
138
|
-
/**
|
|
139
|
-
* The `_update` function updates the values in a binary indexed freqMap starting from a given index and
|
|
140
|
-
* propagating the changes to its parent nodes.
|
|
141
|
-
* @param {number} index - The `index` parameter is a number that represents the index of the element in
|
|
142
|
-
* the data structure that needs to be updated.
|
|
143
|
-
* @param {number} delta - The `delta` parameter represents the change in value that needs to be
|
|
144
|
-
* applied to the elements in the data structure.
|
|
145
|
-
*/
|
|
146
|
-
protected _update(index: number, delta: number): void;
|
|
147
|
-
/**
|
|
148
|
-
* The `_writeSingle` function updates the frequency at a specific index and triggers a callback if
|
|
149
|
-
* the frequency has changed.
|
|
150
|
-
* @param {number} index - The `index` parameter is a number that represents the index of the element
|
|
151
|
-
* being modified or accessed.
|
|
152
|
-
* @param {number} freq - The `freq` parameter represents the new frequency value that needs to be
|
|
153
|
-
* written to the specified index `index`.
|
|
154
|
-
*/
|
|
155
|
-
protected _writeSingle(index: number, freq: number): void;
|
|
156
|
-
/**
|
|
157
|
-
* The `_read` function calculates the sum of values in a binary freqMap up to a given count.
|
|
158
|
-
* @param {number} count - The `count` parameter is a number that represents the number of elements
|
|
159
|
-
* to read from the freqMap.
|
|
160
|
-
* @returns the sum of the values obtained from calling the `_getFrequency` method for each index in the
|
|
161
|
-
* range from `count` to 1.
|
|
162
|
-
*/
|
|
163
|
-
protected _read(count: number): number;
|
|
164
|
-
/**
|
|
165
|
-
* The function `_binarySearch` performs a binary search to find the largest number that satisfies a given
|
|
166
|
-
* condition.
|
|
167
|
-
* @param {number} sum - The sum parameter is a number that represents the target sum value.
|
|
168
|
-
* @param before - The `before` parameter is a function that takes two numbers `x` and `y` as
|
|
169
|
-
* arguments and returns a boolean value. It is used to determine if `x` is less than or equal to
|
|
170
|
-
* `y`. The purpose of this function is to compare two numbers and determine their order.
|
|
171
|
-
* @returns the value of the variable "left".
|
|
172
|
-
*/
|
|
173
|
-
protected _binarySearch(sum: number, before: (x: number, y: number) => boolean): number;
|
|
218
|
+
/** Returns highest power of 2 <= n. */
|
|
219
|
+
protected _highBit(n: number): number;
|
|
174
220
|
}
|