max-priority-queue-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.
Files changed (76) hide show
  1. package/README.md +63 -0
  2. package/dist/cjs/index.cjs +400 -119
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +399 -118
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +400 -119
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +399 -118
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
  22. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  23. package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
  24. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  25. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  26. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  27. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  28. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  29. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  30. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  31. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  32. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  33. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  34. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  35. package/dist/types/data-structures/queue/deque.d.ts +272 -65
  36. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  37. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  38. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  39. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  40. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  41. package/dist/umd/max-priority-queue-typed.js +397 -116
  42. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  43. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  44. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  45. package/package.json +2 -2
  46. package/src/data-structures/base/iterable-element-base.ts +4 -5
  47. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  48. package/src/data-structures/binary-tree/binary-indexed-tree.ts +302 -247
  49. package/src/data-structures/binary-tree/binary-tree.ts +429 -79
  50. package/src/data-structures/binary-tree/bst.ts +335 -34
  51. package/src/data-structures/binary-tree/red-black-tree.ts +290 -97
  52. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  53. package/src/data-structures/binary-tree/tree-map.ts +1284 -6
  54. package/src/data-structures/binary-tree/tree-multi-map.ts +1094 -211
  55. package/src/data-structures/binary-tree/tree-multi-set.ts +858 -65
  56. package/src/data-structures/binary-tree/tree-set.ts +1136 -9
  57. package/src/data-structures/graph/directed-graph.ts +219 -47
  58. package/src/data-structures/graph/map-graph.ts +59 -1
  59. package/src/data-structures/graph/undirected-graph.ts +204 -59
  60. package/src/data-structures/hash/hash-map.ts +230 -77
  61. package/src/data-structures/heap/heap.ts +287 -99
  62. package/src/data-structures/heap/max-heap.ts +46 -0
  63. package/src/data-structures/heap/min-heap.ts +59 -0
  64. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  65. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  66. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  67. package/src/data-structures/matrix/matrix.ts +416 -12
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  70. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  71. package/src/data-structures/queue/deque.ts +272 -65
  72. package/src/data-structures/queue/queue.ts +211 -42
  73. package/src/data-structures/stack/stack.ts +174 -32
  74. package/src/data-structures/trie/trie.ts +213 -43
  75. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  76. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
@@ -11,6 +11,89 @@ import { ERR } from '../../common';
11
11
  /**
12
12
  *
13
13
  */
14
+ /**
15
+ * Matrix — a numeric matrix with standard linear algebra operations.
16
+ *
17
+ * @example
18
+ * // Basic matrix arithmetic
19
+ * const a = new Matrix([
20
+ * [1, 2],
21
+ * [3, 4]
22
+ * ]);
23
+ * const b = new Matrix([
24
+ * [5, 6],
25
+ * [7, 8]
26
+ * ]);
27
+ *
28
+ * const sum = a.add(b);
29
+ * console.log(sum?.data); // [
30
+ * // [6, 8],
31
+ * // [10, 12]
32
+ * // ];
33
+ *
34
+ * const diff = b.subtract(a);
35
+ * console.log(diff?.data); // [
36
+ * // [4, 4],
37
+ * // [4, 4]
38
+ * // ];
39
+ * @example
40
+ * // Matrix multiplication for transformations
41
+ * // 2x3 matrix * 3x2 matrix = 2x2 matrix
42
+ * const a = new Matrix([
43
+ * [1, 2, 3],
44
+ * [4, 5, 6]
45
+ * ]);
46
+ * const b = new Matrix([
47
+ * [7, 8],
48
+ * [9, 10],
49
+ * [11, 12]
50
+ * ]);
51
+ *
52
+ * const product = a.multiply(b);
53
+ * console.log(product?.rows); // 2;
54
+ * console.log(product?.cols); // 2;
55
+ * // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
56
+ * // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
57
+ * console.log(product?.data); // [
58
+ * // [58, 64],
59
+ * // [139, 154]
60
+ * // ];
61
+ * @example
62
+ * // Matrix transpose (square matrix)
63
+ * const m = new Matrix([
64
+ * [1, 2, 3],
65
+ * [4, 5, 6],
66
+ * [7, 8, 9]
67
+ * ]);
68
+ *
69
+ * const transposed = m.transpose();
70
+ * console.log(transposed.rows); // 3;
71
+ * console.log(transposed.cols); // 3;
72
+ * console.log(transposed.data); // [
73
+ * // [1, 4, 7],
74
+ * // [2, 5, 8],
75
+ * // [3, 6, 9]
76
+ * // ];
77
+ *
78
+ * // Transpose of transpose = original
79
+ * console.log(transposed.transpose().data); // m.data;
80
+ * @example
81
+ * // Get and set individual cells
82
+ * const m = new Matrix([
83
+ * [0, 0, 0],
84
+ * [0, 0, 0]
85
+ * ]);
86
+ *
87
+ * m.set(0, 1, 42);
88
+ * m.set(1, 2, 99);
89
+ *
90
+ * console.log(m.get(0, 1)); // 42;
91
+ * console.log(m.get(1, 2)); // 99;
92
+ * console.log(m.get(0, 0)); // 0;
93
+ *
94
+ * // Out of bounds returns undefined
95
+ * console.log(m.get(5, 5)); // undefined;
96
+ */
14
97
  export class Matrix {
15
98
  /**
16
99
  * The constructor function initializes a matrix object with the provided data and options, or with
@@ -106,6 +189,33 @@ export class Matrix {
106
189
  * retrieve from the data array.
107
190
  * @returns The `get` function returns a number if the provided row and column indices are valid.
108
191
  * Otherwise, it returns `undefined`.
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+ * @example
204
+ * // Get and set individual cells
205
+ * const m = new Matrix([
206
+ * [0, 0, 0],
207
+ * [0, 0, 0]
208
+ * ]);
209
+ *
210
+ * m.set(0, 1, 42);
211
+ * m.set(1, 2, 99);
212
+ *
213
+ * console.log(m.get(0, 1)); // 42;
214
+ * console.log(m.get(1, 2)); // 99;
215
+ * console.log(m.get(0, 0)); // 0;
216
+ *
217
+ * // Out of bounds returns undefined
218
+ * console.log(m.get(5, 5)); // undefined;
109
219
  */
110
220
  get(row: number, col: number): number | undefined {
111
221
  if (this.isValidIndex(row, col)) {
@@ -124,6 +234,24 @@ export class Matrix {
124
234
  * @returns a boolean value. It returns true if the index (row, col) is valid and the value is
125
235
  * successfully set in the data array. It returns false if the index is invalid and the value is not
126
236
  * set.
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+ * @example
249
+ * // Modify individual cells
250
+ * const m = Matrix.zeros(2, 2);
251
+ * console.log(m.set(0, 0, 5)); // true;
252
+ * console.log(m.set(1, 1, 10)); // true;
253
+ * console.log(m.get(0, 0)); // 5;
254
+ * console.log(m.get(1, 1)); // 10;
127
255
  */
128
256
  set(row: number, col: number, value: number): boolean {
129
257
  if (this.isValidIndex(row, col)) {
@@ -148,6 +276,39 @@ export class Matrix {
148
276
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
149
277
  * @returns The `add` method returns a new `Matrix` object that represents the result of adding the
150
278
  * current matrix with the provided `matrix` parameter.
279
+
280
+
281
+
282
+
283
+
284
+
285
+
286
+
287
+
288
+
289
+
290
+ * @example
291
+ * // Basic matrix arithmetic
292
+ * const a = new Matrix([
293
+ * [1, 2],
294
+ * [3, 4]
295
+ * ]);
296
+ * const b = new Matrix([
297
+ * [5, 6],
298
+ * [7, 8]
299
+ * ]);
300
+ *
301
+ * const sum = a.add(b);
302
+ * console.log(sum?.data); // [
303
+ * // [6, 8],
304
+ * // [10, 12]
305
+ * // ];
306
+ *
307
+ * const diff = b.subtract(a);
308
+ * console.log(diff?.data); // [
309
+ * // [4, 4],
310
+ * // [4, 4]
311
+ * // ];
151
312
  */
152
313
  add(matrix: Matrix): Matrix | undefined {
153
314
  if (!this.isMatchForCalculate(matrix)) {
@@ -161,10 +322,7 @@ export class Matrix {
161
322
  const a = this.get(i, j),
162
323
  b = matrix.get(i, j);
163
324
  if (a !== undefined && b !== undefined) {
164
- const added = this._addFn(a, b);
165
- if (added) {
166
- resultData[i][j] = added;
167
- }
325
+ resultData[i][j] = this._addFn(a, b) ?? 0;
168
326
  }
169
327
  }
170
328
  }
@@ -184,6 +342,23 @@ export class Matrix {
184
342
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
185
343
  * represents the matrix that you want to subtract from the current matrix.
186
344
  * @returns a new Matrix object with the result of the subtraction operation.
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+
354
+
355
+
356
+ * @example
357
+ * // Element-wise subtraction
358
+ * const a = Matrix.from([[5, 6], [7, 8]]);
359
+ * const b = Matrix.from([[1, 2], [3, 4]]);
360
+ * const result = a.subtract(b);
361
+ * console.log(result?.toArray()); // [[4, 4], [4, 4]];
187
362
  */
188
363
  subtract(matrix: Matrix): Matrix | undefined {
189
364
  if (!this.isMatchForCalculate(matrix)) {
@@ -197,10 +372,7 @@ export class Matrix {
197
372
  const a = this.get(i, j),
198
373
  b = matrix.get(i, j);
199
374
  if (a !== undefined && b !== undefined) {
200
- const subtracted = this._subtractFn(a, b);
201
- if (subtracted) {
202
- resultData[i][j] = subtracted;
203
- }
375
+ resultData[i][j] = this._subtractFn(a, b) ?? 0;
204
376
  }
205
377
  }
206
378
  }
@@ -219,6 +391,39 @@ export class Matrix {
219
391
  * as a new matrix.
220
392
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
221
393
  * @returns a new Matrix object.
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+
404
+
405
+ * @example
406
+ * // Matrix multiplication for transformations
407
+ * // 2x3 matrix * 3x2 matrix = 2x2 matrix
408
+ * const a = new Matrix([
409
+ * [1, 2, 3],
410
+ * [4, 5, 6]
411
+ * ]);
412
+ * const b = new Matrix([
413
+ * [7, 8],
414
+ * [9, 10],
415
+ * [11, 12]
416
+ * ]);
417
+ *
418
+ * const product = a.multiply(b);
419
+ * console.log(product?.rows); // 2;
420
+ * console.log(product?.cols); // 2;
421
+ * // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
422
+ * // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
423
+ * console.log(product?.data); // [
424
+ * // [58, 64],
425
+ * // [139, 154]
426
+ * // ];
222
427
  */
223
428
  multiply(matrix: Matrix): Matrix | undefined {
224
429
  if (this.cols !== matrix.rows) {
@@ -257,9 +462,39 @@ export class Matrix {
257
462
  * The transpose function takes a matrix and returns a new matrix that is the transpose of the
258
463
  * original matrix.
259
464
  * @returns The transpose() function returns a new Matrix object with the transposed data.
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
+
475
+
476
+ * @example
477
+ * // Matrix transpose (square matrix)
478
+ * const m = new Matrix([
479
+ * [1, 2, 3],
480
+ * [4, 5, 6],
481
+ * [7, 8, 9]
482
+ * ]);
483
+ *
484
+ * const transposed = m.transpose();
485
+ * console.log(transposed.rows); // 3;
486
+ * console.log(transposed.cols); // 3;
487
+ * console.log(transposed.data); // [
488
+ * // [1, 4, 7],
489
+ * // [2, 5, 8],
490
+ * // [3, 6, 9]
491
+ * // ];
492
+ *
493
+ * // Transpose of transpose = original
494
+ * console.log(transposed.transpose().data); // m.data;
260
495
  */
261
496
  transpose(): Matrix {
262
- if (this.data.some(row => row.length !== this.rows)) {
497
+ if (this.data.some(row => row.length !== this.cols)) {
263
498
  throw new Error(ERR.matrixNotRectangular());
264
499
  }
265
500
 
@@ -285,6 +520,28 @@ export class Matrix {
285
520
  /**
286
521
  * The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
287
522
  * @returns a Matrix object, which represents the inverse of the original matrix.
523
+
524
+
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+
534
+ * @example
535
+ * // Compute the inverse of a 2x2 matrix
536
+ * const m = Matrix.from([[4, 7], [2, 6]]);
537
+ * const inv = m.inverse();
538
+ * console.log(inv); // defined;
539
+ * // A * A^-1 should ≈ Identity
540
+ * const product = m.multiply(inv!);
541
+ * console.log(product?.get(0, 0)); // toBeCloseTo;
542
+ * console.log(product?.get(0, 1)); // toBeCloseTo;
543
+ * console.log(product?.get(1, 0)); // toBeCloseTo;
544
+ * console.log(product?.get(1, 1)); // toBeCloseTo;
288
545
  */
289
546
  inverse(): Matrix | undefined {
290
547
  // Check if the matrix is square
@@ -365,6 +622,23 @@ export class Matrix {
365
622
  * The dot function calculates the dot product of two matrices and returns a new matrix.
366
623
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
367
624
  * @returns a new Matrix object.
625
+
626
+
627
+
628
+
629
+
630
+
631
+
632
+
633
+
634
+
635
+
636
+ * @example
637
+ * // Dot product of two matrices
638
+ * const a = Matrix.from([[1, 2], [3, 4]]);
639
+ * const b = Matrix.from([[5, 6], [7, 8]]);
640
+ * const result = a.dot(b);
641
+ * console.log(result?.toArray()); // [[19, 22], [43, 50]];
368
642
  */
369
643
  dot(matrix: Matrix): Matrix | undefined {
370
644
  if (this.cols !== matrix.rows) {
@@ -418,15 +692,145 @@ export class Matrix {
418
692
  * and properties as the current instance.
419
693
  */
420
694
  clone(): Matrix {
421
- return new Matrix(this.data, {
422
- rows: this.rows,
423
- cols: this.cols,
695
+ return new Matrix(
696
+ this._data.map(row => [...row]),
697
+ {
698
+ rows: this.rows,
699
+ cols: this.cols,
700
+ addFn: this.addFn,
701
+ subtractFn: this.subtractFn,
702
+ multiplyFn: this.multiplyFn
703
+ }
704
+ );
705
+ }
706
+
707
+ // ─── Standard interface ─────────────────────────────────────
708
+
709
+ /**
710
+ * Returns [rows, cols] dimensions tuple.
711
+ */
712
+ get size(): [number, number] {
713
+ return [this._rows, this._cols];
714
+ }
715
+
716
+ isEmpty(): boolean {
717
+ return this._rows === 0 || this._cols === 0;
718
+ }
719
+
720
+ /**
721
+ * Returns a deep copy of the data as a plain 2D array.
722
+ */
723
+ toArray(): number[][] {
724
+ return this._data.map(row => [...row]);
725
+ }
726
+
727
+ /**
728
+ * Returns a flat row-major array.
729
+ */
730
+ flatten(): number[] {
731
+ const result: number[] = [];
732
+ for (const row of this._data) {
733
+ for (const v of row) result.push(v);
734
+ }
735
+ return result;
736
+ }
737
+
738
+ /**
739
+ * Iterates over rows.
740
+ */
741
+ [Symbol.iterator](): IterableIterator<number[]> {
742
+ const data = this._data;
743
+ let i = 0;
744
+ return {
745
+ [Symbol.iterator]() {
746
+ return this;
747
+ },
748
+ next(): IteratorResult<number[]> {
749
+ if (i < data.length) {
750
+ return { value: [...data[i++]], done: false };
751
+ }
752
+ return { value: undefined as any, done: true };
753
+ }
754
+ };
755
+ }
756
+
757
+ /**
758
+ * Visits each element with its row and column index.
759
+ */
760
+ forEach(callback: (value: number, row: number, col: number) => void): void {
761
+ for (let i = 0; i < this._rows; i++) {
762
+ for (let j = 0; j < this._cols; j++) {
763
+ callback(this._data[i][j], i, j);
764
+ }
765
+ }
766
+ }
767
+
768
+ /**
769
+ * Maps each element (number → number) and returns a new Matrix.
770
+ */
771
+ map(callback: (value: number, row: number, col: number) => number): Matrix {
772
+ const resultData: number[][] = [];
773
+ for (let i = 0; i < this._rows; i++) {
774
+ resultData[i] = [];
775
+ for (let j = 0; j < this._cols; j++) {
776
+ resultData[i][j] = callback(this._data[i][j], i, j);
777
+ }
778
+ }
779
+ return new Matrix(resultData, {
780
+ rows: this._rows,
781
+ cols: this._cols,
424
782
  addFn: this.addFn,
425
783
  subtractFn: this.subtractFn,
426
784
  multiplyFn: this.multiplyFn
427
785
  });
428
786
  }
429
787
 
788
+ print(): void {
789
+ for (const row of this._data) {
790
+ console.log(row.join('\t'));
791
+ }
792
+ }
793
+
794
+ // ─── Factory methods ────────────────────────────────────────
795
+
796
+ /**
797
+ * Creates a rows×cols zero matrix.
798
+ * @example
799
+ * ```ts
800
+ * const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
801
+ * ```
802
+ */
803
+ static zeros(rows: number, cols: number): Matrix {
804
+ const data: number[][] = Array.from({ length: rows }, () => new Array(cols).fill(0));
805
+ return new Matrix(data);
806
+ }
807
+
808
+ /**
809
+ * Creates an n×n identity matrix.
810
+ * @example
811
+ * ```ts
812
+ * const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
813
+ * ```
814
+ */
815
+ static identity(n: number): Matrix {
816
+ const data: number[][] = Array.from({ length: n }, (_, i) =>
817
+ Array.from({ length: n }, (_, j) => (i === j ? 1 : 0))
818
+ );
819
+ return new Matrix(data);
820
+ }
821
+
822
+ /**
823
+ * Creates a Matrix from a plain 2D array (deep copy).
824
+ * @example
825
+ * ```ts
826
+ * const m = Matrix.from([[1, 2], [3, 4]]);
827
+ * m.get(0, 1); // 2
828
+ * ```
829
+ */
830
+ static from(data: number[][]): Matrix {
831
+ return new Matrix(data.map(row => [...row]));
832
+ }
833
+
430
834
  protected _addFn(a: number | undefined, b: number): number | undefined {
431
835
  if (a === undefined) return b;
432
836
  return a + b;
@@ -16,6 +16,63 @@ import { ERR } from '../../common';
16
16
  * @template E Element type stored in the queue.
17
17
  * @template R Extra record/metadata associated with each element.
18
18
  * @example
19
+ * // Job scheduling by priority
20
+ * const jobs = new MaxPriorityQueue<number>();
21
+ *
22
+ * jobs.add(3); // low priority
23
+ * jobs.add(7); // high priority
24
+ * jobs.add(5); // medium priority
25
+ * jobs.add(10); // critical
26
+ *
27
+ * // Highest priority job first
28
+ * console.log(jobs.poll()); // 10;
29
+ * console.log(jobs.poll()); // 7;
30
+ * console.log(jobs.poll()); // 5;
31
+ * console.log(jobs.poll()); // 3;
32
+ * @example
33
+ * // Auction system with highest bid tracking
34
+ * interface Bid {
35
+ * bidder: string;
36
+ * amount: number;
37
+ * }
38
+ *
39
+ * const auction = new MaxPriorityQueue<Bid>([], {
40
+ * comparator: (a, b) => b.amount - a.amount
41
+ * });
42
+ *
43
+ * auction.add({ bidder: 'Alice', amount: 100 });
44
+ * auction.add({ bidder: 'Bob', amount: 250 });
45
+ * auction.add({ bidder: 'Charlie', amount: 175 });
46
+ *
47
+ * // Current highest bid
48
+ * console.log(auction.peek()?.bidder); // 'Bob';
49
+ * console.log(auction.peek()?.amount); // 250;
50
+ *
51
+ * // Process winning bid
52
+ * const winner = auction.poll()!;
53
+ * console.log(winner.bidder); // 'Bob';
54
+ * console.log(auction.peek()?.bidder); // 'Charlie';
55
+ * @example
56
+ * // CPU process scheduling
57
+ * const cpuQueue = new MaxPriorityQueue<[number, string]>([], {
58
+ * comparator: (a, b) => b[0] - a[0]
59
+ * });
60
+ *
61
+ * cpuQueue.add([5, 'System process']);
62
+ * cpuQueue.add([1, 'Background task']);
63
+ * cpuQueue.add([8, 'User interaction']);
64
+ * cpuQueue.add([3, 'Network sync']);
65
+ *
66
+ * const order = [];
67
+ * while (cpuQueue.size > 0) {
68
+ * order.push(cpuQueue.poll()![1]);
69
+ * }
70
+ * console.log(order); // [
71
+ * // 'User interaction',
72
+ * // 'System process',
73
+ * // 'Network sync',
74
+ * // 'Background task'
75
+ * // ];
19
76
  */
20
77
  export class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
21
78
  /**
@@ -15,6 +15,66 @@ import { PriorityQueue } from './priority-queue';
15
15
  * @template E Element type stored in the queue.
16
16
  * @template R Extra record/metadata associated with each element.
17
17
  * @example
18
+ * // Shortest job first scheduling
19
+ * const jobs = new MinPriorityQueue<number>();
20
+ *
21
+ * jobs.add(8); // 8 seconds
22
+ * jobs.add(2); // 2 seconds
23
+ * jobs.add(5); // 5 seconds
24
+ * jobs.add(1); // 1 second
25
+ *
26
+ * // Shortest job first
27
+ * console.log(jobs.poll()); // 1;
28
+ * console.log(jobs.poll()); // 2;
29
+ * console.log(jobs.poll()); // 5;
30
+ * console.log(jobs.poll()); // 8;
31
+ * @example
32
+ * // Event-driven simulation with timestamps
33
+ * interface Event {
34
+ * time: number;
35
+ * action: string;
36
+ * }
37
+ *
38
+ * const timeline = new MinPriorityQueue<Event>([], {
39
+ * comparator: (a, b) => a.time - b.time
40
+ * });
41
+ *
42
+ * timeline.add({ time: 300, action: 'Timeout' });
43
+ * timeline.add({ time: 100, action: 'Request received' });
44
+ * timeline.add({ time: 200, action: 'Processing done' });
45
+ * timeline.add({ time: 150, action: 'Cache hit' });
46
+ *
47
+ * const order = [];
48
+ * while (timeline.size > 0) {
49
+ * order.push(timeline.poll()!.action);
50
+ * }
51
+ * console.log(order); // [
52
+ * // 'Request received',
53
+ * // 'Cache hit',
54
+ * // 'Processing done',
55
+ * // 'Timeout'
56
+ * // ];
57
+ * @example
58
+ * // Huffman coding frequency selection
59
+ * // Character frequencies for Huffman tree building
60
+ * const freq = new MinPriorityQueue<[number, string]>([], {
61
+ * comparator: (a, b) => a[0] - b[0]
62
+ * });
63
+ *
64
+ * freq.add([5, 'a']);
65
+ * freq.add([9, 'b']);
66
+ * freq.add([12, 'c']);
67
+ * freq.add([2, 'd']);
68
+ *
69
+ * // Always pick two lowest frequencies
70
+ * const first = freq.poll()!;
71
+ * const second = freq.poll()!;
72
+ * console.log(first[1]); // 'd'; // freq 2
73
+ * console.log(second[1]); // 'a'; // freq 5
74
+ *
75
+ * // Combined node goes back
76
+ * freq.add([first[0] + second[0], first[1] + second[1]]);
77
+ * console.log(freq.peek()![0]); // 7;
18
78
  */
19
79
  export class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
20
80
  /**
@@ -11,6 +11,66 @@ import { Heap } from '../heap';
11
11
 
12
12
  /**
13
13
  * @example
14
+ * // Hospital emergency room triage
15
+ * interface Patient {
16
+ * name: string;
17
+ * severity: number; // 1 = critical, 5 = minor
18
+ * }
19
+ *
20
+ * const er = new PriorityQueue<Patient>([], {
21
+ * comparator: (a, b) => a.severity - b.severity
22
+ * });
23
+ *
24
+ * er.add({ name: 'Flu symptoms', severity: 4 });
25
+ * er.add({ name: 'Heart attack', severity: 1 });
26
+ * er.add({ name: 'Broken arm', severity: 3 });
27
+ * er.add({ name: 'Stroke', severity: 1 });
28
+ *
29
+ * // Most critical patients first
30
+ * console.log(er.poll()?.severity); // 1;
31
+ * console.log(er.poll()?.severity); // 1;
32
+ * console.log(er.poll()?.severity); // 3;
33
+ * console.log(er.poll()?.severity); // 4;
34
+ * @example
35
+ * // Task scheduler with deadlines
36
+ * interface Task {
37
+ * name: string;
38
+ * deadline: number; // hours until due
39
+ * }
40
+ *
41
+ * const scheduler = new PriorityQueue<Task>([], {
42
+ * comparator: (a, b) => a.deadline - b.deadline
43
+ * });
44
+ *
45
+ * scheduler.add({ name: 'Report', deadline: 24 });
46
+ * scheduler.add({ name: 'Email', deadline: 2 });
47
+ * scheduler.add({ name: 'Meeting prep', deadline: 4 });
48
+ * scheduler.add({ name: 'Code review', deadline: 8 });
49
+ *
50
+ * // Process most urgent first
51
+ * console.log(scheduler.peek()?.name); // 'Email';
52
+ * console.log(scheduler.size); // 4;
53
+ *
54
+ * const order = [];
55
+ * while (scheduler.size > 0) {
56
+ * order.push(scheduler.poll()!.name);
57
+ * }
58
+ * console.log(order); // ['Email', 'Meeting prep', 'Code review', 'Report'];
59
+ * @example
60
+ * // Bandwidth allocation with priorities
61
+ * const bandwidth = new PriorityQueue<[number, string]>([], {
62
+ * comparator: (a, b) => a[0] - b[0]
63
+ * });
64
+ *
65
+ * bandwidth.add([1, 'Video call']); // highest priority
66
+ * bandwidth.add([3, 'File download']);
67
+ * bandwidth.add([2, 'Web browsing']);
68
+ * bandwidth.add([1, 'Voice call']);
69
+ *
70
+ * // Allocate bandwidth to highest priority first
71
+ * console.log(bandwidth.poll()?.[1]); // 'Video call';
72
+ * console.log(bandwidth.poll()?.[1]); // 'Voice call';
73
+ * console.log(bandwidth.size); // 2;
14
74
  */
15
75
  export class PriorityQueue<E = any, R = any> extends Heap<E, R> {
16
76
  constructor(elements: Iterable<E> | Iterable<R> = [], options?: PriorityQueueOptions<E, R>) {