heap-typed 2.0.5 → 2.1.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 (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +602 -873
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +1 -0
  64. package/dist/utils/utils.d.ts +1 -1
  65. package/dist/utils/utils.js +2 -1
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
  72. package/src/data-structures/binary-tree/avl-tree.ts +237 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +665 -896
  74. package/src/data-structures/binary-tree/bst.ts +565 -572
  75. package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
  76. package/src/data-structures/binary-tree/tree-counter.ts +195 -219
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +2 -0
  101. package/src/utils/utils.ts +9 -14
@@ -3,91 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MinPriorityQueue = void 0;
4
4
  const priority_queue_1 = require("./priority-queue");
5
5
  /**
6
- *
6
+ * Min-oriented priority queue (min-heap) built on {@link PriorityQueue}.
7
+ * The queue removes the smallest element first under the provided comparator.
8
+ * Provide a custom comparator if you store non-primitive objects.
9
+ * @template E Element type stored in the queue.
10
+ * @template R Extra record/metadata associated with each element.
11
+ * @example
7
12
  */
8
13
  class MinPriorityQueue extends priority_queue_1.PriorityQueue {
9
14
  /**
10
- * The constructor initializes a PriorityQueue with optional elements and options, including a
11
- * comparator function.
12
- * @param elements - The `elements` parameter is an iterable object that contains the initial
13
- * elements to be added to the priority queue. It is optional and defaults to an empty array if not
14
- * provided.
15
- * @param options - The `options` parameter is an object that contains additional configuration
16
- * options for the priority queue. In this case, it has a property called `comparator,` which is a
17
- * function used to compare elements in the priority queue. The `comparator` function takes two
18
- * parameters `a` and `b`
15
+ * Creates a min-priority queue.
16
+ * @param elements Optional initial elements to insert.
17
+ * @param options Optional configuration (e.g., `comparator`, `toElementFn`).
18
+ * @remarks Complexity Time: O(n log n) when inserting n elements incrementally; Space: O(n).
19
19
  */
20
20
  constructor(elements = [], options) {
21
21
  super(elements, options);
22
22
  }
23
- /**
24
- * The `clone` function returns a new instance of the `MinPriorityQueue` class with the same
25
- * comparator and toElementFn as the original instance.
26
- * @returns The method is returning a new instance of the `MinPriorityQueue` class with the same
27
- * properties as the current instance.
28
- */
29
- clone() {
30
- return new MinPriorityQueue(this, { comparator: this.comparator, toElementFn: this.toElementFn });
31
- }
32
- /**
33
- * Time Complexity: O(n)
34
- * Space Complexity: O(n)
35
- *
36
- * The `filter` function creates a new MinPriorityQueue object containing elements that pass a given callback
37
- * function.
38
- * @param callback - The `callback` parameter is a function that will be called for each element in
39
- * the heap. It takes three arguments: the current element, the index of the current element, and the
40
- * heap itself. The callback function should return a boolean value indicating whether the current
41
- * element should be included in the filtered list
42
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
43
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
44
- * passed as the `this` value to the `callback` function. If `thisArg` is
45
- * @returns The `filter` method is returning a new `MinPriorityQueue` object that contains the elements that pass
46
- * the filter condition specified by the `callback` function.
47
- */
48
- filter(callback, thisArg) {
49
- const filteredPriorityQueue = new MinPriorityQueue([], {
50
- toElementFn: this.toElementFn,
51
- comparator: this.comparator
52
- });
53
- let index = 0;
54
- for (const current of this) {
55
- if (callback.call(thisArg, current, index, this)) {
56
- filteredPriorityQueue.add(current);
57
- }
58
- index++;
59
- }
60
- return filteredPriorityQueue;
61
- }
62
- /**
63
- * Time Complexity: O(n log n)
64
- * Space Complexity: O(n)
65
- *
66
- * The `map` function creates a new heap by applying a callback function to each element of the
67
- * original heap.
68
- * @param callback - The `callback` parameter is a function that will be called for each element in
69
- * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
70
- * element), and `this` (the heap itself). The callback function should return a value of
71
- * @param comparator - The `comparator` parameter is a function that defines the order of the
72
- * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
73
- * if `a` should be placed before `b`, a positive number if `a` should be placed after
74
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
75
- * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
76
- * returns a value of type `T`. This function is used to transform the elements of the original
77
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
78
- * specify the value of `this` within the callback function. It is used to set the context or scope
79
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
80
- * value of
81
- * @returns a new instance of the `MinPriorityQueue` class with the mapped elements.
82
- */
83
- map(callback, comparator, toElementFn, thisArg) {
84
- const mappedPriorityQueue = new MinPriorityQueue([], { comparator, toElementFn });
85
- let index = 0;
86
- for (const el of this) {
87
- mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
88
- index++;
89
- }
90
- return mappedPriorityQueue;
91
- }
92
23
  }
93
24
  exports.MinPriorityQueue = MinPriorityQueue;
@@ -5,70 +5,11 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
8
+ import type { PriorityQueueOptions } from '../../types';
9
9
  import { Heap } from '../heap';
10
10
  /**
11
- * 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
12
- * 2. Heap-Based Implementation: PriorityQueue is typically implemented using a binary heap, allowing both insertion and removal operations to be completed in O(log n) time, where n is the number of elements in the queue.
13
- * 3. Task Scheduling: In systems where tasks need to be processed based on the urgency of tasks rather than the order of arrival.
14
- * 4. Dijkstra's Algorithm: In shortest path algorithms for graphs, used to select the next shortest edge to visit.
15
- * 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
16
- * 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
11
+ * @example
17
12
  */
18
13
  export declare class PriorityQueue<E = any, R = any> extends Heap<E, R> {
19
- /**
20
- * The constructor initializes a priority queue with optional elements and options.
21
- * @param elements - The `elements` parameter is an iterable object that contains the initial
22
- * elements to be added to the priority queue. It is an optional parameter, and if not provided, the
23
- * priority queue will be initialized as empty.
24
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
25
- * behavior of the priority queue. It can contain the following properties:
26
- */
27
14
  constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
28
- /**
29
- * The `clone` function returns a new instance of the `PriorityQueue` class with the same comparator
30
- * and toElementFn as the original instance.
31
- * @returns The method is returning a new instance of the `PriorityQueue` class with the same
32
- * elements and properties as the current instance.
33
- */
34
- clone(): PriorityQueue<E, R>;
35
- /**
36
- * Time Complexity: O(n)
37
- * Space Complexity: O(n)
38
- *
39
- * The `filter` function creates a new PriorityQueue object containing elements that pass a given callback
40
- * function.
41
- * @param callback - The `callback` parameter is a function that will be called for each element in
42
- * the heap. It takes three arguments: the current element, the index of the current element, and the
43
- * heap itself. The callback function should return a boolean value indicating whether the current
44
- * element should be included in the filtered list
45
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
46
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
47
- * passed as the `this` value to the `callback` function. If `thisArg` is
48
- * @returns The `filter` method is returning a new `PriorityQueue` object that contains the elements that pass
49
- * the filter condition specified by the `callback` function.
50
- */
51
- filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): PriorityQueue<E, R>;
52
- /**
53
- * Time Complexity: O(n log n)
54
- * Space Complexity: O(n)
55
- *
56
- * The `map` function creates a new heap by applying a callback function to each element of the
57
- * original heap.
58
- * @param callback - The `callback` parameter is a function that will be called for each element in
59
- * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
60
- * element), and `this` (the heap itself). The callback function should return a value of
61
- * @param comparator - The `comparator` parameter is a function that defines the order of the
62
- * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
63
- * if `a` should be placed before `b`, a positive number if `a` should be placed after
64
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
65
- * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
66
- * returns a value of type `T`. This function is used to transform the elements of the original
67
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
68
- * specify the value of `this` within the callback function. It is used to set the context or scope
69
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
70
- * value of
71
- * @returns a new instance of the `PriorityQueue` class with the mapped elements.
72
- */
73
- map<EM, RM>(callback: ElementCallback<E, R, EM>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): PriorityQueue<EM, RM>;
74
15
  }
@@ -1,95 +1,20 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Kirk Qi
6
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.PriorityQueue = void 0;
4
11
  const heap_1 = require("../heap");
5
12
  /**
6
- * 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
7
- * 2. Heap-Based Implementation: PriorityQueue is typically implemented using a binary heap, allowing both insertion and removal operations to be completed in O(log n) time, where n is the number of elements in the queue.
8
- * 3. Task Scheduling: In systems where tasks need to be processed based on the urgency of tasks rather than the order of arrival.
9
- * 4. Dijkstra's Algorithm: In shortest path algorithms for graphs, used to select the next shortest edge to visit.
10
- * 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
11
- * 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
13
+ * @example
12
14
  */
13
15
  class PriorityQueue extends heap_1.Heap {
14
- /**
15
- * The constructor initializes a priority queue with optional elements and options.
16
- * @param elements - The `elements` parameter is an iterable object that contains the initial
17
- * elements to be added to the priority queue. It is an optional parameter, and if not provided, the
18
- * priority queue will be initialized as empty.
19
- * @param [options] - The `options` parameter is an optional object that can be used to customize the
20
- * behavior of the priority queue. It can contain the following properties:
21
- */
22
16
  constructor(elements = [], options) {
23
17
  super(elements, options);
24
18
  }
25
- /**
26
- * The `clone` function returns a new instance of the `PriorityQueue` class with the same comparator
27
- * and toElementFn as the original instance.
28
- * @returns The method is returning a new instance of the `PriorityQueue` class with the same
29
- * elements and properties as the current instance.
30
- */
31
- clone() {
32
- return new PriorityQueue(this, { comparator: this.comparator, toElementFn: this.toElementFn });
33
- }
34
- /**
35
- * Time Complexity: O(n)
36
- * Space Complexity: O(n)
37
- *
38
- * The `filter` function creates a new PriorityQueue object containing elements that pass a given callback
39
- * function.
40
- * @param callback - The `callback` parameter is a function that will be called for each element in
41
- * the heap. It takes three arguments: the current element, the index of the current element, and the
42
- * heap itself. The callback function should return a boolean value indicating whether the current
43
- * element should be included in the filtered list
44
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
45
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
46
- * passed as the `this` value to the `callback` function. If `thisArg` is
47
- * @returns The `filter` method is returning a new `PriorityQueue` object that contains the elements that pass
48
- * the filter condition specified by the `callback` function.
49
- */
50
- filter(callback, thisArg) {
51
- const filteredPriorityQueue = new PriorityQueue([], {
52
- toElementFn: this.toElementFn,
53
- comparator: this.comparator
54
- });
55
- let index = 0;
56
- for (const current of this) {
57
- if (callback.call(thisArg, current, index, this)) {
58
- filteredPriorityQueue.add(current);
59
- }
60
- index++;
61
- }
62
- return filteredPriorityQueue;
63
- }
64
- /**
65
- * Time Complexity: O(n log n)
66
- * Space Complexity: O(n)
67
- *
68
- * The `map` function creates a new heap by applying a callback function to each element of the
69
- * original heap.
70
- * @param callback - The `callback` parameter is a function that will be called for each element in
71
- * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
72
- * element), and `this` (the heap itself). The callback function should return a value of
73
- * @param comparator - The `comparator` parameter is a function that defines the order of the
74
- * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
75
- * if `a` should be placed before `b`, a positive number if `a` should be placed after
76
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
77
- * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
78
- * returns a value of type `T`. This function is used to transform the elements of the original
79
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
80
- * specify the value of `this` within the callback function. It is used to set the context or scope
81
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
82
- * value of
83
- * @returns a new instance of the `PriorityQueue` class with the mapped elements.
84
- */
85
- map(callback, comparator, toElementFn, thisArg) {
86
- const mappedPriorityQueue = new PriorityQueue([], { comparator, toElementFn });
87
- let index = 0;
88
- for (const el of this) {
89
- mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
90
- index++;
91
- }
92
- return mappedPriorityQueue;
93
- }
94
19
  }
95
20
  exports.PriorityQueue = PriorityQueue;