max-priority-queue-typed 2.3.0 → 2.4.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 (43) hide show
  1. package/dist/types/data-structures/base/linear-base.d.ts +6 -6
  2. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -6
  3. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
  4. package/dist/types/data-structures/binary-tree/index.d.ts +3 -3
  5. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +150 -20
  6. package/dist/types/data-structures/binary-tree/tree-map.d.ts +188 -0
  7. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +238 -147
  8. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +270 -0
  9. package/dist/types/data-structures/binary-tree/tree-set.d.ts +181 -0
  10. package/dist/types/interfaces/binary-tree.d.ts +2 -2
  11. package/dist/types/types/data-structures/binary-tree/index.d.ts +3 -3
  12. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +33 -0
  13. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +16 -0
  14. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +33 -0
  15. package/package.json +2 -2
  16. package/src/data-structures/base/linear-base.ts +2 -12
  17. package/src/data-structures/binary-tree/avl-tree.ts +1 -1
  18. package/src/data-structures/binary-tree/binary-tree.ts +45 -21
  19. package/src/data-structures/binary-tree/bst.ts +85 -10
  20. package/src/data-structures/binary-tree/index.ts +3 -3
  21. package/src/data-structures/binary-tree/red-black-tree.ts +568 -76
  22. package/src/data-structures/binary-tree/tree-map.ts +439 -0
  23. package/src/data-structures/binary-tree/tree-multi-map.ts +488 -325
  24. package/src/data-structures/binary-tree/tree-multi-set.ts +502 -0
  25. package/src/data-structures/binary-tree/tree-set.ts +407 -0
  26. package/src/data-structures/queue/deque.ts +10 -0
  27. package/src/interfaces/binary-tree.ts +2 -2
  28. package/src/types/data-structures/binary-tree/index.ts +3 -3
  29. package/src/types/data-structures/binary-tree/tree-map.ts +45 -0
  30. package/src/types/data-structures/binary-tree/tree-multi-set.ts +19 -0
  31. package/src/types/data-structures/binary-tree/tree-set.ts +39 -0
  32. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -236
  33. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -197
  34. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +0 -243
  35. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
  36. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
  37. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
  38. package/src/data-structures/binary-tree/avl-tree-counter.ts +0 -539
  39. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +0 -438
  40. package/src/data-structures/binary-tree/tree-counter.ts +0 -575
  41. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +0 -3
  42. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +0 -3
  43. package/src/types/data-structures/binary-tree/tree-counter.ts +0 -3
@@ -1,539 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
-
9
- import type {
10
- AVLTreeCounterOptions,
11
- BinaryTreeDeleteResult,
12
- BSTNOptKeyOrNode,
13
- EntryCallback,
14
- FamilyPosition,
15
- IterationType,
16
- RBTNColor
17
- } from '../../types';
18
- import { IBinaryTree } from '../../interfaces';
19
- import { AVLTree } from './avl-tree';
20
-
21
- /**
22
- * AVL node with an extra 'count' field; keeps parent/child links.
23
- * @remarks Time O(1), Space O(1)
24
- * @template K
25
- * @template V
26
- */
27
- export class AVLTreeCounterNode<K = any, V = any> {
28
- key: K;
29
- value?: V;
30
- parent?: AVLTreeCounterNode<K, V> = undefined;
31
-
32
- /**
33
- * Create an AVL counter node.
34
- * @remarks Time O(1), Space O(1)
35
- * @param key - Key of the node.
36
- * @param [value] - Associated value (ignored in map mode).
37
- * @param [count] - Initial count for this node (default 1).
38
- * @returns New AVLTreeCounterNode instance.
39
- */
40
- constructor(key: K, value?: V, count = 1) {
41
- this.key = key;
42
- this.value = value;
43
- this.count = count;
44
- }
45
-
46
- _left?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
47
-
48
- /**
49
- * Get the left child pointer.
50
- * @remarks Time O(1), Space O(1)
51
- * @returns Left child node, or null/undefined.
52
- */
53
- get left(): AVLTreeCounterNode<K, V> | null | undefined {
54
- return this._left;
55
- }
56
-
57
- /**
58
- * Set the left child and update its parent pointer.
59
- * @remarks Time O(1), Space O(1)
60
- * @param v - New left child node, or null/undefined.
61
- * @returns void
62
- */
63
- set left(v: AVLTreeCounterNode<K, V> | null | undefined) {
64
- if (v) {
65
- v.parent = this;
66
- }
67
- this._left = v;
68
- }
69
-
70
- _right?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
71
-
72
- /**
73
- * Get the right child pointer.
74
- * @remarks Time O(1), Space O(1)
75
- * @returns Right child node, or null/undefined.
76
- */
77
- get right(): AVLTreeCounterNode<K, V> | null | undefined {
78
- return this._right;
79
- }
80
-
81
- /**
82
- * Set the right child and update its parent pointer.
83
- * @remarks Time O(1), Space O(1)
84
- * @param v - New right child node, or null/undefined.
85
- * @returns void
86
- */
87
- set right(v: AVLTreeCounterNode<K, V> | null | undefined) {
88
- if (v) {
89
- v.parent = this;
90
- }
91
- this._right = v;
92
- }
93
-
94
- _height: number = 0;
95
-
96
- /**
97
- * Gets the height of the node (used in self-balancing trees).
98
- * @remarks Time O(1), Space O(1)
99
- *
100
- * @returns The height.
101
- */
102
- get height(): number {
103
- return this._height;
104
- }
105
-
106
- /**
107
- * Sets the height of the node.
108
- * @remarks Time O(1), Space O(1)
109
- *
110
- * @param value - The new height.
111
- */
112
- set height(value: number) {
113
- this._height = value;
114
- }
115
-
116
- _color: RBTNColor = 'BLACK';
117
-
118
- /**
119
- * Gets the color of the node (used in Red-Black trees).
120
- * @remarks Time O(1), Space O(1)
121
- *
122
- * @returns The node's color.
123
- */
124
- get color(): RBTNColor {
125
- return this._color;
126
- }
127
-
128
- /**
129
- * Sets the color of the node.
130
- * @remarks Time O(1), Space O(1)
131
- *
132
- * @param value - The new color.
133
- */
134
- set color(value: RBTNColor) {
135
- this._color = value;
136
- }
137
-
138
- _count: number = 1;
139
-
140
- /**
141
- * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
142
- * @remarks Time O(1), Space O(1)
143
- *
144
- * @returns The subtree node count.
145
- */
146
- get count(): number {
147
- return this._count;
148
- }
149
-
150
- /**
151
- * Sets the count of nodes in the subtree.
152
- * @remarks Time O(1), Space O(1)
153
- *
154
- * @param value - The new count.
155
- */
156
- set count(value: number) {
157
- this._count = value;
158
- }
159
-
160
- /**
161
- * Gets the position of the node relative to its parent.
162
- * @remarks Time O(1), Space O(1)
163
- *
164
- * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
165
- */
166
- get familyPosition(): FamilyPosition {
167
- if (!this.parent) {
168
- return this.left || this.right ? 'ROOT' : 'ISOLATED';
169
- }
170
-
171
- if (this.parent.left === this) {
172
- return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
173
- } else if (this.parent.right === this) {
174
- return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
175
- }
176
-
177
- return 'MAL_NODE';
178
- }
179
- }
180
-
181
- /**
182
- * AVL tree that tracks an aggregate 'count' across nodes; supports balanced insert/delete and map-like mode.
183
- * @remarks Time O(1), Space O(1)
184
- * @template K
185
- * @template V
186
- * @template R
187
- */
188
- export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R> implements IBinaryTree<K, V, R> {
189
- /**
190
- * Create a AVLTreeCounter instance
191
- * @remarks Time O(n), Space O(n)
192
- * @param keysNodesEntriesOrRaws
193
- * @param options
194
- * @returns New AVLTreeCounterNode instance.
195
- */
196
- constructor(
197
- keysNodesEntriesOrRaws: Iterable<
198
- K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
199
- > = [],
200
- options?: AVLTreeCounterOptions<K, V, R>
201
- ) {
202
- super([], options);
203
- if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
204
- }
205
-
206
- protected _count = 0;
207
-
208
- get count(): number {
209
- return this._count;
210
- }
211
-
212
- /**
213
- * Compute the total count by traversing the tree (sums node.count).
214
- * @remarks Time O(N), Space O(H)
215
- * @returns Total count recomputed from nodes.
216
- */
217
- getComputedCount(): number {
218
- let sum = 0;
219
- this.dfs(node => (sum += node.count));
220
- return sum;
221
- }
222
-
223
- override createNode(key: K, value?: V, count?: number): AVLTreeCounterNode<K, V> {
224
- return new AVLTreeCounterNode(key, this._isMapMode ? undefined : value, count) as AVLTreeCounterNode<K, V>;
225
- }
226
-
227
- /**
228
- * Type guard: check whether the input is an AVLTreeCounterNode.
229
- * @remarks Time O(1), Space O(1)
230
- * @returns True if the value is an AVLTreeCounterNode.
231
- */
232
- override isNode(
233
- keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
234
- ): keyNodeOrEntry is AVLTreeCounterNode<K, V> {
235
- return keyNodeOrEntry instanceof AVLTreeCounterNode;
236
- }
237
-
238
- /**
239
- * Insert or increment a node and update aggregate count.
240
- * @remarks Time O(log N), Space O(1)
241
- * @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
242
- * @param [value] - Value when a bare key is provided (ignored in map mode).
243
- * @param [count] - How much to increase the node's count (default 1).
244
- * @returns True if inserted/updated; false if ignored.
245
- */
246
- override set(
247
- keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
248
- value?: V,
249
- count = 1
250
- ): boolean {
251
- const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
252
- if (newNode === undefined) return false;
253
-
254
- const orgNodeCount = newNode?.count || 0;
255
- const inserted = super.set(newNode, newValue);
256
- if (inserted) {
257
- this._count += orgNodeCount;
258
- }
259
- return true;
260
- }
261
-
262
- /**
263
- * Delete a node (or decrement its count) and rebalance if needed.
264
- * @remarks Time O(log N), Space O(1)
265
- * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
266
- * @param [ignoreCount] - If true, remove the node regardless of its count.
267
- * @returns Array of deletion results including deleted node and a rebalance hint when present.
268
- */
269
- override delete(
270
- keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
271
- ignoreCount = false
272
- ): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] {
273
- const deletedResult: BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] = [];
274
- if (!this.root) return deletedResult;
275
-
276
- const curr: AVLTreeCounterNode<K, V> | undefined = this.getNode(keyNodeOrEntry) ?? undefined;
277
- if (!curr) return deletedResult;
278
-
279
- const parent: AVLTreeCounterNode<K, V> | undefined = curr?.parent ? curr.parent : undefined;
280
- let needBalanced: AVLTreeCounterNode<K, V> | undefined = undefined,
281
- orgCurrent: AVLTreeCounterNode<K, V> | undefined = curr;
282
-
283
- if (curr.count > 1 && !ignoreCount) {
284
- curr.count--;
285
- this._count--;
286
- } else {
287
- if (!curr.left) {
288
- if (!parent) {
289
- if (curr.right !== undefined && curr.right !== null) this._setRoot(curr.right);
290
- } else {
291
- const { familyPosition: fp } = curr;
292
- if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
293
- parent.left = curr.right;
294
- } else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
295
- parent.right = curr.right;
296
- }
297
- needBalanced = parent;
298
- }
299
- } else {
300
- const leftSubTreeRightMost = curr.left ? this.getRightMost(node => node, curr.left) : undefined;
301
- if (leftSubTreeRightMost) {
302
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
303
- orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
304
- if (parentOfLeftSubTreeMax) {
305
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
306
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
307
- } else {
308
- parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
309
- }
310
- needBalanced = parentOfLeftSubTreeMax;
311
- }
312
- }
313
- }
314
- this._size = this._size - 1;
315
-
316
- if (orgCurrent) this._count -= orgCurrent.count;
317
- }
318
-
319
- deletedResult.push({ deleted: orgCurrent, needBalanced });
320
-
321
- if (needBalanced) {
322
- this._balancePath(needBalanced);
323
- }
324
-
325
- return deletedResult;
326
- }
327
-
328
- /**
329
- * Remove all nodes and reset aggregate counters.
330
- * @remarks Time O(N), Space O(1)
331
- * @returns void
332
- */
333
- override clear() {
334
- super.clear();
335
- this._count = 0;
336
- }
337
-
338
- /**
339
- * Rebuild the tree into a perfectly balanced form using in-order nodes.
340
- * @remarks Time O(N), Space O(N)
341
- * @param [iterationType] - Traversal style to use when constructing the balanced tree.
342
- * @returns True if rebalancing succeeded (tree not empty).
343
- */
344
- override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
345
- const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
346
- const n = nodes.length;
347
- if (n === 0) return false;
348
-
349
- let total = 0;
350
- for (const nd of nodes) total += nd ? nd.count : 0;
351
-
352
- this._clearNodes();
353
-
354
- const build = (l: number, r: number, parent?: any): any => {
355
- if (l > r) return undefined;
356
- const m = l + ((r - l) >> 1);
357
- const root = nodes[m];
358
- root.left = build(l, m - 1, root);
359
- root.right = build(m + 1, r, root);
360
- root.parent = parent;
361
- const lh = root.left ? root.left.height : -1;
362
- const rh = root.right ? root.right.height : -1;
363
- root.height = Math.max(lh, rh) + 1;
364
- return root;
365
- };
366
-
367
- const newRoot = build(0, n - 1, undefined);
368
- this._setRoot(newRoot);
369
- this._size = n;
370
- this._count = total;
371
- return true;
372
- }
373
-
374
- /**
375
- * Deep copy this tree, preserving map mode and aggregate counts.
376
- * @remarks Time O(N), Space O(N)
377
- * @returns A deep copy of this tree.
378
- */
379
- override clone(): this {
380
- const out = this._createInstance();
381
-
382
- if (this._isMapMode) {
383
- this.bfs(node => out.set(node.key, undefined, node.count));
384
- } else {
385
- this.bfs(node => out.set(node.key, node.value, node.count));
386
- }
387
-
388
- if (this._isMapMode) out._store = this._store;
389
-
390
- return out as this;
391
- }
392
-
393
- /**
394
- * Create a new AVLTreeCounter by mapping each [key, value] entry.
395
- * @remarks Time O(N log N), Space O(N)
396
- * @template MK
397
- * @template MV
398
- * @template MR
399
- * @param callback - Function mapping (key, value, index, tree) → [newKey, newValue].
400
- * @param [options] - Options for the output tree.
401
- * @param [thisArg] - Value for `this` inside the callback.
402
- * @returns A new AVLTreeCounter with mapped entries.
403
- */
404
- override map<MK = K, MV = V, MR = any>(
405
- callback: EntryCallback<K, V | undefined, [MK, MV]>,
406
- options?: Partial<AVLTreeCounterOptions<MK, MV, MR>>,
407
- thisArg?: unknown
408
- ): AVLTreeCounter<MK, MV, MR> {
409
- const out = this._createLike<MK, MV, MR>([], options);
410
-
411
- let index = 0;
412
- for (const [key, value] of this) {
413
- out.set(callback.call(thisArg, value, key, index++, this));
414
- }
415
- return out;
416
- }
417
-
418
- /**
419
- * (Protected) Create an empty instance of the same concrete class.
420
- * @remarks Time O(1), Space O(1)
421
- * @template TK
422
- * @template TV
423
- * @template TR
424
- * @param [options] - Optional constructor options for the like-kind instance.
425
- * @returns An empty like-kind instance.
426
- */
427
- protected override _createInstance<TK = K, TV = V, TR = R>(
428
- options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>
429
- ): this {
430
- const Ctor = this.constructor as unknown as new (
431
- iter?: Iterable<
432
- TK | AVLTreeCounterNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
433
- >,
434
- opts?: AVLTreeCounterOptions<TK, TV, TR>
435
- ) => AVLTreeCounter<TK, TV, TR>;
436
- return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
437
- }
438
-
439
- /**
440
- * (Protected) Create a like-kind instance and seed it from an iterable.
441
- * @remarks Time O(N log N), Space O(N)
442
- * @template TK
443
- * @template TV
444
- * @template TR
445
- * @param iter - Iterable used to seed the new tree.
446
- * @param [options] - Options merged with the current snapshot.
447
- * @returns A like-kind AVLTreeCounter built from the iterable.
448
- */
449
- protected override _createLike<TK = K, TV = V, TR = R>(
450
- iter: Iterable<
451
- TK | AVLTreeCounterNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
452
- > = [],
453
- options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>
454
- ): AVLTreeCounter<TK, TV, TR> {
455
- const Ctor = this.constructor as unknown as new (
456
- iter?: Iterable<
457
- TK | AVLTreeCounterNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
458
- >,
459
- opts?: AVLTreeCounterOptions<TK, TV, TR>
460
- ) => AVLTreeCounter<TK, TV, TR>;
461
- return new Ctor(iter, { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) });
462
- }
463
-
464
- /**
465
- * (Protected) Normalize input into a node plus its effective value and count.
466
- * @remarks Time O(1), Space O(1)
467
- * @param keyNodeOrEntry - Key, node, or [key, value] entry.
468
- * @param [value] - Value used when a bare key is provided.
469
- * @param [count] - Count increment to apply (default 1).
470
- * @returns Tuple [node, value] where node may be undefined.
471
- */
472
- protected override _keyValueNodeOrEntryToNodeAndValue(
473
- keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
474
- value?: V,
475
- count = 1
476
- ): [AVLTreeCounterNode<K, V> | undefined, V | undefined] {
477
- if (keyNodeOrEntry === undefined || keyNodeOrEntry === null) return [undefined, undefined];
478
- if (this.isNode(keyNodeOrEntry)) return [keyNodeOrEntry, value];
479
-
480
- if (this.isEntry(keyNodeOrEntry)) {
481
- const [key, entryValue] = keyNodeOrEntry;
482
- if (key === undefined || key === null) return [undefined, undefined];
483
- const finalValue = value ?? entryValue;
484
- return [this.createNode(key, finalValue, count), finalValue];
485
- }
486
-
487
- return [this.createNode(keyNodeOrEntry, value, count), value];
488
- }
489
-
490
- /**
491
- * (Protected) Swap keys/values/counters between the source and destination nodes.
492
- * @remarks Time O(1), Space O(1)
493
- * @param srcNode - Source node (or key) whose properties will be moved.
494
- * @param destNode - Destination node (or key) to receive properties.
495
- * @returns Destination node after swap, or undefined.
496
- */
497
- protected override _swapProperties(
498
- srcNode: BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>,
499
- destNode: BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>
500
- ): AVLTreeCounterNode<K, V> | undefined {
501
- srcNode = this.ensureNode(srcNode);
502
- destNode = this.ensureNode(destNode);
503
- if (srcNode && destNode) {
504
- const { key, value, count, height } = destNode;
505
- const tempNode = this.createNode(key, value, count);
506
- if (tempNode) {
507
- tempNode.height = height;
508
-
509
- destNode.key = srcNode.key;
510
- if (!this._isMapMode) destNode.value = srcNode.value;
511
- destNode.count = srcNode.count;
512
- destNode.height = srcNode.height;
513
-
514
- srcNode.key = tempNode.key;
515
- if (!this._isMapMode) srcNode.value = tempNode.value;
516
- srcNode.count = tempNode.count;
517
- srcNode.height = tempNode.height;
518
- }
519
-
520
- return destNode;
521
- }
522
- return undefined;
523
- }
524
-
525
- /**
526
- * (Protected) Replace one node by another and adjust counters accordingly.
527
- * @remarks Time O(1), Space O(1)
528
- * @param oldNode - Node being replaced.
529
- * @param newNode - Replacement node.
530
- * @returns The new node after replacement.
531
- */
532
- protected override _replaceNode(
533
- oldNode: AVLTreeCounterNode<K, V>,
534
- newNode: AVLTreeCounterNode<K, V>
535
- ): AVLTreeCounterNode<K, V> {
536
- newNode.count = oldNode.count + newNode.count;
537
- return super._replaceNode(oldNode, newNode);
538
- }
539
- }