binary-tree-typed 2.4.5 → 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.
Files changed (94) hide show
  1. package/README.md +0 -84
  2. package/dist/cjs/index.cjs +1476 -404
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +1473 -401
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +1476 -404
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +1473 -401
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/base/index.d.ts +1 -0
  11. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  12. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  13. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  14. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
  15. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
  16. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
  17. package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
  18. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
  19. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
  20. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
  21. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
  22. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
  23. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
  24. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  25. package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
  26. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  27. package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
  28. package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
  29. package/dist/types/data-structures/heap/heap.d.ts +581 -99
  30. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  31. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  32. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
  33. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
  34. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
  35. package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
  36. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  37. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  39. package/dist/types/data-structures/queue/deque.d.ts +593 -71
  40. package/dist/types/data-structures/queue/queue.d.ts +463 -42
  41. package/dist/types/data-structures/stack/stack.d.ts +384 -32
  42. package/dist/types/data-structures/trie/trie.d.ts +470 -48
  43. package/dist/types/interfaces/graph.d.ts +1 -1
  44. package/dist/types/types/common.d.ts +2 -2
  45. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  46. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  47. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  48. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  49. package/dist/types/types/utils/validate-type.d.ts +4 -4
  50. package/dist/umd/binary-tree-typed.js +1469 -397
  51. package/dist/umd/binary-tree-typed.js.map +1 -1
  52. package/dist/umd/binary-tree-typed.min.js +5 -5
  53. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  54. package/package.json +2 -2
  55. package/src/data-structures/base/index.ts +1 -0
  56. package/src/data-structures/base/iterable-element-base.ts +4 -5
  57. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  58. package/src/data-structures/base/linear-base.ts +3 -3
  59. package/src/data-structures/binary-tree/avl-tree.ts +386 -51
  60. package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
  61. package/src/data-structures/binary-tree/binary-tree.ts +956 -81
  62. package/src/data-structures/binary-tree/bst.ts +840 -35
  63. package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
  64. package/src/data-structures/binary-tree/segment-tree.ts +498 -249
  65. package/src/data-structures/binary-tree/tree-map.ts +3784 -7
  66. package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
  67. package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
  68. package/src/data-structures/binary-tree/tree-set.ts +3531 -10
  69. package/src/data-structures/graph/abstract-graph.ts +4 -4
  70. package/src/data-structures/graph/directed-graph.ts +429 -47
  71. package/src/data-structures/graph/map-graph.ts +59 -1
  72. package/src/data-structures/graph/undirected-graph.ts +393 -59
  73. package/src/data-structures/hash/hash-map.ts +476 -92
  74. package/src/data-structures/heap/heap.ts +581 -99
  75. package/src/data-structures/heap/max-heap.ts +46 -0
  76. package/src/data-structures/heap/min-heap.ts +59 -0
  77. package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
  78. package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
  79. package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
  80. package/src/data-structures/matrix/matrix.ts +584 -12
  81. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  82. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  83. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  84. package/src/data-structures/queue/deque.ts +592 -70
  85. package/src/data-structures/queue/queue.ts +463 -42
  86. package/src/data-structures/stack/stack.ts +384 -32
  87. package/src/data-structures/trie/trie.ts +470 -48
  88. package/src/interfaces/graph.ts +1 -1
  89. package/src/types/common.ts +2 -2
  90. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  91. package/src/types/data-structures/heap/heap.ts +1 -0
  92. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  93. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  94. package/src/types/utils/validate-type.ts +4 -4
@@ -1,174 +1,514 @@
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 _freq: number;
6
- protected readonly _max: number;
7
- /**
8
- * The constructor initializes the properties of an object, including default frequency, maximum
9
- * value, a freqMap data structure, the most significant bit, and the count of negative frequencies.
10
- * @param - - `frequency`: The default frequency value. It is optional and has a default
11
- * value of 0.
12
- */
13
- constructor({ frequency, max }: {
14
- frequency?: number;
15
- max: number;
16
- });
17
- protected _freqMap: Record<number, number>;
18
- /**
19
- * The function returns the frequency map of numbers.
20
- * @returns The `_freqMap` property, which is a record with number keys and number values, is being
21
- * returned.
22
- */
23
- get freqMap(): Record<number, number>;
24
- protected _msb: number;
21
+ export declare class BinaryIndexedTree implements Iterable<number> {
22
+ protected readonly _size: number;
23
+ protected _tree: number[];
25
24
  /**
26
- * The function returns the value of the _msb property.
27
- * @returns The `_msb` property of the object.
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
28
27
  */
29
- get msb(): number;
30
- protected _negativeCount: number;
28
+ constructor(sizeOrElements: number | number[]);
31
29
  /**
32
- * The function returns the value of the _negativeCount property.
33
- * @returns The method is returning the value of the variable `_negativeCount`, which is of type
34
- * `number`.
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
+
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
+
84
+
85
+
86
+
87
+
88
+ * @example
89
+ * // Add delta at index
90
+ * const bit = new BinaryIndexedTree([1, 2, 3, 4, 5]);
91
+ * bit.update(2, 7);
92
+ * console.log(bit.get(2)); // 10;
35
93
  */
36
- get negativeCount(): number;
94
+ update(index: number, delta: number): void;
37
95
  /**
38
- * The above function returns the value of the protected variable `_freq`.
39
- * @returns The frequency value stored in the protected variable `_freq`.
96
+ * Point set: set the value at index to an absolute value (0-based).
97
+ * Time: O(log n)
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+ * @example
156
+ * // Set value at index
157
+ * const bit = new BinaryIndexedTree([1, 2, 3]);
158
+ * bit.set(1, 10);
159
+ * console.log(bit.get(1)); // 10;
40
160
  */
41
- get freq(): number;
161
+ set(index: number, value: number): void;
42
162
  /**
43
- * The above function returns the maximum value.
44
- * @returns The maximum value stored in the variable `_max`.
163
+ * Get the point value at index (0-based).
164
+ * Time: O(log n)
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+ * @example
222
+ * // Get value at index
223
+ * const bit = new BinaryIndexedTree([1, 2, 3]);
224
+ * console.log(bit.get(0)); // 1;
225
+ * console.log(bit.get(2)); // 3;
45
226
  */
46
- get max(): number;
227
+ get(index: number): number;
47
228
  /**
48
- * The function "readSingle" reads a single number from a specified index.
49
- * @param {number} index - The `index` parameter is a number that represents the index of an element in a
50
- * collection or array.
51
- * @returns a number.
229
+ * Prefix sum query: returns sum of elements [0..index] (inclusive, 0-based).
230
+ * Time: O(log n)
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+
270
+
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+
284
+
285
+
286
+
287
+
288
+ * @example
289
+ * // Prefix sum
290
+ * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
291
+ * console.log(bit.query(2)); // 6;
52
292
  */
53
- readSingle(index: number): number;
293
+ query(index: number): number;
54
294
  /**
55
- * The "update" function updates the value at a given index by adding a delta and triggers a callback
56
- * to notify of the change.
57
- * @param {number} position - The `index` parameter represents the index of the element that needs to be
58
- * updated in the data structure.
59
- * @param {number} change - The "delta" parameter represents the change in value that needs to be
60
- * applied to the frequency at the specified index.
295
+ * Range sum query: returns sum of elements [start..end] (inclusive, 0-based).
296
+ * Time: O(log n)
297
+
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+
316
+
317
+
318
+
319
+
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
+ * // Range sum
355
+ * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
356
+ * console.log(bit.queryRange(1, 2)); // 5;
61
357
  */
62
- update(position: number, change: number): void;
358
+ queryRange(start: number, end: number): number;
63
359
  /**
64
- * The function "writeSingle" checks the index and writes a single value with a given frequency.
65
- * @param {number} index - The `index` parameter is a number that represents the index of an element. It
66
- * is used to identify the specific element that needs to be written.
67
- * @param {number} freq - The `freq` parameter represents the frequency value that needs to be
68
- * written.
69
- */
70
- writeSingle(index: number, freq: number): void;
71
- /**
72
- * The read function takes a count parameter, checks if it is an integer, and returns the result of
73
- * calling the _read function with the count parameter clamped between 0 and the maximum value.
74
- * @param {number} count - The `count` parameter is a number that represents the number of items to
75
- * read.
76
- * @returns a number.
77
- */
78
- read(count: number): number;
79
- /**
80
- * The function returns the lower bound of a non-descending sequence that sums up to a given number.
81
- * @param {number} sum - The `sum` parameter is a number that represents the target sum that we want
82
- * to find in the sequence.
83
- * @returns The lowerBound function is returning a number.
360
+ * Find the smallest index i such that prefix sum [0..i] >= sum.
361
+ * Requires all values to be non-negative (behavior undefined otherwise).
362
+ * Returns size if no such index exists.
363
+ * Time: O(log n)
364
+
365
+
366
+
367
+
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+
420
+
421
+ * @example
422
+ * // Find index with prefix sum ≥ target
423
+ * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
424
+ * const idx = bit.lowerBound(4);
425
+ * console.log(idx); // >= 0;
84
426
  */
85
427
  lowerBound(sum: number): number;
86
428
  /**
87
- * The upperBound function returns the index of the first element in a sequence that is greater than
88
- * or equal to a given sum.
89
- * @param {number} sum - The "sum" parameter is a number that represents the target sum that we want
90
- * to find in the sequence.
91
- * @returns The upperBound function is returning a number.
429
+ * Find the smallest index i such that prefix sum [0..i] > sum.
430
+ * Requires all values to be non-negative (behavior undefined otherwise).
431
+ * Returns size if no such index exists.
432
+ * Time: O(log n)
433
+
434
+
435
+
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+
454
+
455
+
456
+ * @example
457
+ * // Find index with prefix sum > target
458
+ * const bit = new BinaryIndexedTree([1, 2, 3, 4]);
459
+ * const idx = bit.upperBound(4);
460
+ * console.log(idx); // >= 0;
92
461
  */
93
462
  upperBound(sum: number): number;
463
+ get size(): number;
464
+ isEmpty(): boolean;
465
+ clear(): void;
466
+ clone(): BinaryIndexedTree;
94
467
  /**
95
- * The function calculates the prefix sum of an array using a binary indexed tree.
96
- * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
97
- * array for which we want to calculate the prefix sum.
98
- * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
99
- * `i`.
468
+ * Returns the point values as a plain array.
469
+ * Time: O(n log n)
470
+
471
+
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+
484
+
485
+
486
+
487
+
488
+
489
+
490
+
491
+
492
+
493
+ * @example
494
+ * // Convert to array
495
+ * const bit = new BinaryIndexedTree([1, 2, 3]);
496
+ * console.log(bit.toArray()); // [1, 2, 3];
100
497
  */
101
- getPrefixSum(i: number): number;
498
+ toArray(): number[];
102
499
  /**
103
- * The function returns the value of a specific index in a freqMap data structure, or a default value if
104
- * the index is not found.
105
- * @param {number} index - The `index` parameter is a number that represents the index of a node in a
106
- * freqMap data structure.
107
- * @returns a number.
108
- */
109
- protected _getFrequency(index: number): number;
110
- /**
111
- * The function _updateFrequency adds a delta value to the element at the specified index in the freqMap array.
112
- * @param {number} index - The index parameter is a number that represents the index of the freqMap
113
- * element that needs to be updated.
114
- * @param {number} delta - The `delta` parameter represents the change in value that needs to be
115
- * added to the freqMap at the specified `index`.
116
- */
117
- protected _updateFrequency(index: number, delta: number): void;
118
- /**
119
- * The function checks if the given index is valid and within the range.
120
- * @param {number} index - The parameter "index" is of type number and represents the index value
121
- * that needs to be checked.
500
+ * Iterate over point values in index order.
122
501
  */
502
+ [Symbol.iterator](): IterableIterator<number>;
503
+ forEach(callback: (value: number, index: number) => void): void;
504
+ print(): void;
505
+ /** 1-based prefix sum up to pos (inclusive). */
506
+ protected _prefixSum(pos: number): number;
507
+ /** 1-based point update: add delta to position pos. */
508
+ protected _pointUpdate(pos: number, delta: number): void;
509
+ /** 1-based point query: get exact value at pos. */
510
+ protected _pointQuery(pos: number): number;
123
511
  protected _checkIndex(index: number): void;
124
- /**
125
- * The function calculates the sum of elements in an array up to a given index using a binary indexed
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;
512
+ /** Returns highest power of 2 <= n. */
513
+ protected _highBit(n: number): number;
174
514
  }