binary-tree-typed 2.4.4 → 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.
Files changed (85) hide show
  1. package/README.md +0 -84
  2. package/dist/cjs/index.cjs +965 -420
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +962 -417
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +965 -421
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +962 -418
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/binary-tree-typed.js +959 -414
  46. package/dist/umd/binary-tree-typed.js.map +1 -1
  47. package/dist/umd/binary-tree-typed.min.js +3 -3
  48. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
@@ -18,71 +18,6 @@ import { LinearBase } from '../base/linear-base';
18
18
  * 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
19
19
  * 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
20
20
  * @example
21
- * // basic Deque creation and push/pop operations
22
- * // Create a simple Deque with initial values
23
- * const deque = new Deque([1, 2, 3, 4, 5]);
24
- *
25
- * // Verify the deque maintains insertion order
26
- * console.log([...deque]); // [1, 2, 3, 4, 5];
27
- *
28
- * // Check length
29
- * console.log(deque.length); // 5;
30
- *
31
- * // Push to the end
32
- * deque.push(6);
33
- * console.log(deque.length); // 6;
34
- *
35
- * // Pop from the end
36
- * const last = deque.pop();
37
- * console.log(last); // 6;
38
- * @example
39
- * // Deque shift and unshift operations
40
- * const deque = new Deque<number>([20, 30, 40]);
41
- *
42
- * // Unshift adds to the front
43
- * deque.unshift(10);
44
- * console.log([...deque]); // [10, 20, 30, 40];
45
- *
46
- * // Shift removes from the front (O(1) complexity!)
47
- * const first = deque.shift();
48
- * console.log(first); // 10;
49
- *
50
- * // Verify remaining elements
51
- * console.log([...deque]); // [20, 30, 40];
52
- * console.log(deque.length); // 3;
53
- * @example
54
- * // Deque peek at both ends
55
- * const deque = new Deque<number>([10, 20, 30, 40, 50]);
56
- *
57
- * // Get first element without removing
58
- * const first = deque.at(0);
59
- * console.log(first); // 10;
60
- *
61
- * // Get last element without removing
62
- * const last = deque.at(deque.length - 1);
63
- * console.log(last); // 50;
64
- *
65
- * // Length unchanged
66
- * console.log(deque.length); // 5;
67
- * @example
68
- * // Deque for...of iteration and reverse
69
- * const deque = new Deque<string>(['A', 'B', 'C', 'D']);
70
- *
71
- * // Iterate forward
72
- * const forward: string[] = [];
73
- * for (const item of deque) {
74
- * forward.push(item);
75
- * }
76
- * console.log(forward); // ['A', 'B', 'C', 'D'];
77
- *
78
- * // Reverse the deque
79
- * deque.reverse();
80
- * const backward: string[] = [];
81
- * for (const item of deque) {
82
- * backward.push(item);
83
- * }
84
- * console.log(backward); // ['D', 'C', 'B', 'A'];
85
- * @example
86
21
  * // Deque as sliding window for stream processing
87
22
  * interface DataPoint {
88
23
  * timestamp: number;
@@ -133,6 +68,10 @@ import { LinearBase } from '../base/linear-base';
133
68
  * console.log(windowResults[2].windowSize); // 3; // Windows are at max size from 3rd onwards
134
69
  * console.log(windowResults[4].windowSize); // 3; // Last window still has 3 elements
135
70
  * console.log(dataWindow.length); // 3;
71
+ * @example
72
+ * // Convert deque to array
73
+ * const dq = new Deque<number>([10, 20, 30]);
74
+ * console.log(dq.toArray()); // [10, 20, 30];
136
75
  */
137
76
  export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
138
77
  protected _equals: (a: E, b: E) => boolean;
@@ -143,7 +82,10 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
143
82
  * @param [options] - Options such as bucketSize, toElementFn, and maxLen.
144
83
  * @returns New Deque instance.
145
84
  */
146
- constructor(elements?: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>, options?: DequeOptions<E, R>);
85
+ constructor(elements?: IterableWithSizeOrLength<E>, options?: DequeOptions<E, R>);
86
+ constructor(elements: IterableWithSizeOrLength<R>, options: DequeOptions<E, R> & {
87
+ toElementFn: (rawElement: R) => E;
88
+ });
147
89
  protected _bucketSize: number;
148
90
  /**
149
91
  * Get the current bucket size.
@@ -151,6 +93,26 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
151
93
  * @returns Bucket capacity per bucket.
152
94
  */
153
95
  get bucketSize(): number;
96
+ protected _autoCompactRatio: number;
97
+ /**
98
+ * Get the auto-compaction ratio.
99
+ * When `elements / (bucketCount * bucketSize)` drops below this ratio after
100
+ * enough shift/pop operations, the deque auto-compacts.
101
+ * @remarks Time O(1), Space O(1)
102
+ * @returns Current ratio threshold. 0 means auto-compact is disabled.
103
+ */
104
+ get autoCompactRatio(): number;
105
+ /**
106
+ * Set the auto-compaction ratio.
107
+ * @remarks Time O(1), Space O(1)
108
+ * @param value - Ratio in [0,1]. 0 disables auto-compact.
109
+ */
110
+ set autoCompactRatio(value: number);
111
+ /**
112
+ * Counter for shift/pop operations since last compaction check.
113
+ * Only checks ratio every `_bucketSize` operations to minimize overhead.
114
+ */
115
+ protected _compactCounter: number;
154
116
  protected _bucketFirst: number;
155
117
  /**
156
118
  * Get the index of the first bucket in use.
@@ -204,12 +166,53 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
204
166
  * Get the first element without removing it.
205
167
  * @remarks Time O(1), Space O(1)
206
168
  * @returns First element or undefined.
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+ * @example
181
+ * // Deque peek at both ends
182
+ * const deque = new Deque<number>([10, 20, 30, 40, 50]);
183
+ *
184
+ * // Get first element without removing
185
+ * const first = deque.at(0);
186
+ * console.log(first); // 10;
187
+ *
188
+ * // Get last element without removing
189
+ * const last = deque.at(deque.length - 1);
190
+ * console.log(last); // 50;
191
+ *
192
+ * // Length unchanged
193
+ * console.log(deque.length); // 5;
207
194
  */
208
195
  get first(): E | undefined;
209
196
  /**
210
197
  * Get the last element without removing it.
211
198
  * @remarks Time O(1), Space O(1)
212
199
  * @returns Last element or undefined.
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+ * @example
212
+ * // Peek at the back element
213
+ * const dq = new Deque<string>(['a', 'b', 'c']);
214
+ * console.log(dq.last); // 'c';
215
+ * console.log(dq.first); // 'a';
213
216
  */
214
217
  get last(): E | undefined;
215
218
  /**
@@ -228,18 +231,79 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
228
231
  * @remarks Time O(1) amortized, Space O(1)
229
232
  * @param element - Element to append.
230
233
  * @returns True when appended.
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+ * @example
246
+ * // basic Deque creation and push/pop operations
247
+ * // Create a simple Deque with initial values
248
+ * const deque = new Deque([1, 2, 3, 4, 5]);
249
+ *
250
+ * // Verify the deque maintains insertion order
251
+ * console.log([...deque]); // [1, 2, 3, 4, 5];
252
+ *
253
+ * // Check length
254
+ * console.log(deque.length); // 5;
255
+ *
256
+ * // Push to the end
257
+ * deque.push(6);
258
+ * console.log(deque.length); // 6;
259
+ *
260
+ * // Pop from the end
261
+ * const last = deque.pop();
262
+ * console.log(last); // 6;
231
263
  */
232
264
  push(element: E): boolean;
233
265
  /**
234
266
  * Remove and return the last element.
235
267
  * @remarks Time O(1), Space O(1)
236
268
  * @returns Removed element or undefined.
269
+
270
+
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+
280
+ * @example
281
+ * // Remove from the back
282
+ * const dq = new Deque<number>([1, 2, 3]);
283
+ * console.log(dq.pop()); // 3;
284
+ * console.log(dq.length); // 2;
237
285
  */
238
286
  pop(): E | undefined;
239
287
  /**
240
288
  * Remove and return the first element.
241
289
  * @remarks Time O(1) amortized, Space O(1)
242
290
  * @returns Removed element or undefined.
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+ * @example
303
+ * // Remove from the front
304
+ * const dq = new Deque<number>([1, 2, 3]);
305
+ * console.log(dq.shift()); // 1;
306
+ * console.log(dq.length); // 2;
243
307
  */
244
308
  shift(): E | undefined;
245
309
  /**
@@ -247,6 +311,32 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
247
311
  * @remarks Time O(1) amortized, Space O(1)
248
312
  * @param element - Element to prepend.
249
313
  * @returns True when prepended.
314
+
315
+
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+
324
+
325
+ * @example
326
+ * // Deque shift and unshift operations
327
+ * const deque = new Deque<number>([20, 30, 40]);
328
+ *
329
+ * // Unshift adds to the front
330
+ * deque.unshift(10);
331
+ * console.log([...deque]); // [10, 20, 30, 40];
332
+ *
333
+ * // Shift removes from the front (O(1) complexity!)
334
+ * const first = deque.shift();
335
+ * console.log(first); // 10;
336
+ *
337
+ * // Verify remaining elements
338
+ * console.log([...deque]); // [20, 30, 40];
339
+ * console.log(deque.length); // 3;
250
340
  */
251
341
  unshift(element: E): boolean;
252
342
  /**
@@ -267,12 +357,39 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
267
357
  * Check whether the deque is empty.
268
358
  * @remarks Time O(1), Space O(1)
269
359
  * @returns True if length is 0.
360
+
361
+
362
+
363
+
364
+
365
+
366
+
367
+
368
+
369
+ * @example
370
+ * // Check if empty
371
+ * const dq = new Deque();
372
+ * console.log(dq.isEmpty()); // true;
270
373
  */
271
374
  isEmpty(): boolean;
272
375
  /**
273
376
  * Remove all elements and reset structure.
274
377
  * @remarks Time O(1), Space O(1)
275
378
  * @returns void
379
+
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+
388
+ * @example
389
+ * // Remove all elements
390
+ * const dq = new Deque<number>([1, 2, 3]);
391
+ * dq.clear();
392
+ * console.log(dq.length); // 0;
276
393
  */
277
394
  clear(): void;
278
395
  /**
@@ -280,6 +397,19 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
280
397
  * @remarks Time O(1), Space O(1)
281
398
  * @param pos - Zero-based position from the front.
282
399
  * @returns Element or undefined.
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+
408
+ * @example
409
+ * // Access by index
410
+ * const dq = new Deque<string>(['a', 'b', 'c']);
411
+ * console.log(dq.at(0)); // 'a';
412
+ * console.log(dq.at(2)); // 'c';
283
413
  */
284
414
  at(pos: number): E | undefined;
285
415
  /**
@@ -336,6 +466,19 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
336
466
  * @remarks Time O(N), Space O(1)
337
467
  * @param element - Element to remove (using the configured equality).
338
468
  * @returns True if an element was removed.
469
+
470
+
471
+
472
+
473
+
474
+
475
+
476
+
477
+ * @example
478
+ * // Remove element
479
+ * const dq = new Deque<number>([1, 2, 3]);
480
+ * dq.delete(2);
481
+ * console.log(dq.length); // 2;
339
482
  */
340
483
  delete(element: E): boolean;
341
484
  /**
@@ -356,6 +499,35 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
356
499
  * Reverse the deque by reversing buckets and pointers.
357
500
  * @remarks Time O(N), Space O(N)
358
501
  * @returns This deque.
502
+
503
+
504
+
505
+
506
+
507
+
508
+
509
+
510
+
511
+
512
+
513
+ * @example
514
+ * // Deque for...of iteration and reverse
515
+ * const deque = new Deque<string>(['A', 'B', 'C', 'D']);
516
+ *
517
+ * // Iterate forward
518
+ * const forward: string[] = [];
519
+ * for (const item of deque) {
520
+ * forward.push(item);
521
+ * }
522
+ * console.log(forward); // ['A', 'B', 'C', 'D'];
523
+ *
524
+ * // Reverse the deque
525
+ * deque.reverse();
526
+ * const backward: string[] = [];
527
+ * for (const item of deque) {
528
+ * backward.push(item);
529
+ * }
530
+ * console.log(backward); // ['D', 'C', 'B', 'A'];
359
531
  */
360
532
  reverse(): this;
361
533
  /**
@@ -369,11 +541,59 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
369
541
  * @remarks Time O(N), Space O(1)
370
542
  * @returns void
371
543
  */
544
+ /**
545
+ * (Protected) Trigger auto-compaction if space utilization drops below threshold.
546
+ * Only checks every `_bucketSize` operations to minimize hot-path overhead.
547
+ * Uses element-based ratio: `elements / (bucketCount * bucketSize)`.
548
+ */
549
+ protected _autoCompact(): void;
550
+ /**
551
+ * Compact the deque by removing unused buckets.
552
+ * @remarks Time O(N), Space O(1)
553
+ * @returns True if compaction was performed (bucket count reduced).
554
+ */
555
+ /**
556
+ * Compact the deque by removing unused buckets.
557
+ * @remarks Time O(N), Space O(1)
558
+ * @returns True if compaction was performed (bucket count reduced).
559
+
560
+
561
+
562
+
563
+
564
+
565
+
566
+
567
+ * @example
568
+ * // Reclaim memory
569
+ * const dq = new Deque<number>([1, 2, 3, 4, 5]);
570
+ * dq.shift();
571
+ * dq.shift();
572
+ * dq.compact();
573
+ * console.log(dq.length); // 3;
574
+ */
575
+ compact(): boolean;
372
576
  shrinkToFit(): void;
373
577
  /**
374
578
  * Deep clone this deque, preserving options.
375
579
  * @remarks Time O(N), Space O(N)
376
580
  * @returns A new deque with the same content and options.
581
+
582
+
583
+
584
+
585
+
586
+
587
+
588
+
589
+
590
+ * @example
591
+ * // Create independent copy
592
+ * const dq = new Deque<number>([1, 2, 3]);
593
+ * const copy = dq.clone();
594
+ * copy.pop();
595
+ * console.log(dq.length); // 3;
596
+ * console.log(copy.length); // 2;
377
597
  */
378
598
  clone(): this;
379
599
  /**
@@ -382,6 +602,20 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
382
602
  * @param predicate - Predicate (value, index, deque) → boolean to keep element.
383
603
  * @param [thisArg] - Value for `this` inside the predicate.
384
604
  * @returns A new deque with kept elements.
605
+
606
+
607
+
608
+
609
+
610
+
611
+
612
+
613
+
614
+ * @example
615
+ * // Filter elements
616
+ * const dq = new Deque<number>([1, 2, 3, 4]);
617
+ * const result = dq.filter(x => x > 2);
618
+ * console.log(result.length); // 2;
385
619
  */
386
620
  filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): this;
387
621
  /**
@@ -401,6 +635,19 @@ export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
401
635
  * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
402
636
  * @param [thisArg] - Value for `this` inside the callback.
403
637
  * @returns A new Deque with mapped elements.
638
+
639
+
640
+
641
+
642
+
643
+
644
+
645
+
646
+ * @example
647
+ * // Transform elements
648
+ * const dq = new Deque<number>([1, 2, 3]);
649
+ * const result = dq.map(x => x * 10);
650
+ * console.log(result.toArray()); // [10, 20, 30];
404
651
  */
405
652
  map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: IterableElementBaseOptions<EM, RM>, thisArg?: any): Deque<EM, RM>;
406
653
  /**