min-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
@@ -1,8 +1,19 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Pablo Zeng
6
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.Stack = void 0;
4
11
  const base_1 = require("../base");
5
12
  /**
13
+ * LIFO stack with array storage and optional record→element conversion.
14
+ * @remarks Time O(1), Space O(1)
15
+ * @template E
16
+ * @template R
6
17
  * 1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
7
18
  * 2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
8
19
  * 3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
@@ -132,214 +143,259 @@ const base_1 = require("../base");
132
143
  * console.log(spans); // [1, 1, 1, 2, 1, 4, 6]
133
144
  */
134
145
  class Stack extends base_1.IterableElementBase {
146
+ /**
147
+ * Create a Stack and optionally bulk-push elements.
148
+ * @remarks Time O(N), Space O(N)
149
+ * @param [elements] - Iterable of elements (or raw records if toElementFn is set).
150
+ * @param [options] - Options such as toElementFn and equality function.
151
+ * @returns New Stack instance.
152
+ */
135
153
  constructor(elements = [], options) {
136
154
  super(options);
155
+ this._equals = Object.is;
137
156
  this._elements = [];
138
157
  this.pushMany(elements);
139
158
  }
140
159
  /**
141
- * The elements function returns the elements of this set.
142
- * @return An array of elements
160
+ * Get the backing array of elements.
161
+ * @remarks Time O(1), Space O(1)
162
+ * @returns Internal elements array.
143
163
  */
144
164
  get elements() {
145
165
  return this._elements;
146
166
  }
147
167
  /**
148
- * The size() function returns the number of elements in an array.
149
- * @returns The size of the elements array.
168
+ * Get the number of stored elements.
169
+ * @remarks Time O(1), Space O(1)
170
+ * @returns Current size.
150
171
  */
151
172
  get size() {
152
173
  return this.elements.length;
153
174
  }
154
175
  /**
155
- * Time Complexity: O(n)
156
- * Space Complexity: O(n)
157
- *
158
- * The function "fromArray" creates a new Stack object from an array of elements.
159
- * @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
160
- * @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
161
- * array.
176
+ * Create a stack from an array of elements.
177
+ * @remarks Time O(N), Space O(N)
178
+ * @template E
179
+ * @template R
180
+ * @param this - The constructor (subclass) to instantiate.
181
+ * @param elements - Array of elements to push in order.
182
+ * @param [options] - Options forwarded to the constructor.
183
+ * @returns A new Stack populated from the array.
162
184
  */
163
- static fromArray(elements) {
164
- return new Stack(elements);
185
+ static fromArray(elements, options) {
186
+ return new this(elements, options);
165
187
  }
166
188
  /**
167
- * Time Complexity: O(1)
168
- * Space Complexity: O(1)
169
- *
170
- * The function checks if an array is empty and returns a boolean value.
171
- * @returns A boolean value indicating whether the `_elements` array is empty or not.
189
+ * Check whether the stack is empty.
190
+ * @remarks Time O(1), Space O(1)
191
+ * @returns True if size is 0.
172
192
  */
173
193
  isEmpty() {
174
194
  return this.elements.length === 0;
175
195
  }
176
196
  /**
177
- * Time Complexity: O(1)
178
- * Space Complexity: O(1)
179
- *
180
- * The `peek` function returns the last element of an array, or undefined if the array is empty.
181
- * @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
197
+ * Get the top element without removing it.
198
+ * @remarks Time O(1), Space O(1)
199
+ * @returns Top element or undefined.
182
200
  */
183
201
  peek() {
184
- if (this.isEmpty())
185
- return undefined;
186
- return this.elements[this.elements.length - 1];
202
+ return this.isEmpty() ? undefined : this.elements[this.elements.length - 1];
187
203
  }
188
204
  /**
189
- * Time Complexity: O(1)
190
- * Space Complexity: O(1)
191
- *
192
- * The push function adds an element to the stack and returns the updated stack.
193
- * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
194
- * @returns The `push` method is returning the updated `Stack<E>` object.
205
+ * Push one element onto the top.
206
+ * @remarks Time O(1), Space O(1)
207
+ * @param element - Element to push.
208
+ * @returns True when pushed.
195
209
  */
196
210
  push(element) {
197
211
  this.elements.push(element);
198
212
  return true;
199
213
  }
200
214
  /**
201
- * Time Complexity: O(1)
202
- * Space Complexity: O(1)
203
- *
204
- * The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
205
- * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
206
- * array is empty, it returns `undefined`.
215
+ * Pop and return the top element.
216
+ * @remarks Time O(1), Space O(1)
217
+ * @returns Removed element or undefined.
207
218
  */
208
219
  pop() {
209
- if (this.isEmpty())
210
- return;
211
- return this.elements.pop();
220
+ return this.isEmpty() ? undefined : this.elements.pop();
212
221
  }
213
222
  /**
214
- * Time Complexity: O(k)
215
- * Space Complexity: O(1)
216
- *
217
- * The function `pushMany` iterates over elements and pushes them into an array after applying a
218
- * transformation function if provided.
219
- * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
220
- * is an iterable containing elements of type `E` or `R`. The function iterates over each element in
221
- * the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
222
- * provided, it is used to
223
- * @returns The `pushMany` function is returning an array of boolean values indicating whether each
224
- * element was successfully pushed into the data structure.
223
+ * Push many elements from an iterable.
224
+ * @remarks Time O(N), Space O(1)
225
+ * @param elements - Iterable of elements (or raw records if toElementFn is set).
226
+ * @returns Array of per-element success flags.
225
227
  */
226
228
  pushMany(elements) {
227
229
  const ans = [];
228
230
  for (const el of elements) {
229
- if (this.toElementFn) {
231
+ if (this.toElementFn)
230
232
  ans.push(this.push(this.toElementFn(el)));
231
- }
232
- else {
233
+ else
233
234
  ans.push(this.push(el));
234
- }
235
235
  }
236
236
  return ans;
237
237
  }
238
238
  /**
239
- * Time Complexity: O(n)
240
- * Space Complexity: O(1)
241
- *
242
- * The toArray function returns a copy of the elements in an array.
243
- * @returns An array of type E.
239
+ * Delete the first occurrence of a specific element.
240
+ * @remarks Time O(N), Space O(1)
241
+ * @param element - Element to remove (using the configured equality).
242
+ * @returns True if an element was removed.
244
243
  */
245
244
  delete(element) {
246
- const index = this.elements.indexOf(element);
247
- return this.deleteAt(index);
245
+ const idx = this._indexOfByEquals(element);
246
+ return this.deleteAt(idx);
248
247
  }
249
248
  /**
250
- * Time Complexity: O(n)
251
- * Space Complexity: O(1)
252
- *
253
- * The toArray function returns a copy of the elements in an array.
254
- * @returns An array of type E.
249
+ * Delete the element at an index.
250
+ * @remarks Time O(N), Space O(1)
251
+ * @param index - Zero-based index from the bottom.
252
+ * @returns True if removed.
255
253
  */
256
254
  deleteAt(index) {
255
+ if (index < 0 || index >= this.elements.length)
256
+ return false;
257
257
  const spliced = this.elements.splice(index, 1);
258
258
  return spliced.length === 1;
259
259
  }
260
260
  /**
261
- * Time Complexity: O(1)
262
- * Space Complexity: O(1)
263
- *
264
- * The clear function clears the elements array.
261
+ * Delete the first element that satisfies a predicate.
262
+ * @remarks Time O(N), Space O(1)
263
+ * @param predicate - Function (value, index, stack) → boolean to decide deletion.
264
+ * @returns True if a match was removed.
265
+ */
266
+ deleteWhere(predicate) {
267
+ for (let i = 0; i < this.elements.length; i++) {
268
+ if (predicate(this.elements[i], i, this)) {
269
+ this.elements.splice(i, 1);
270
+ return true;
271
+ }
272
+ }
273
+ return false;
274
+ }
275
+ /**
276
+ * Remove all elements and reset storage.
277
+ * @remarks Time O(1), Space O(1)
278
+ * @returns void
265
279
  */
266
280
  clear() {
267
281
  this._elements = [];
268
282
  }
269
283
  /**
270
- * Time Complexity: O(n)
271
- * Space Complexity: O(n)
272
- *
273
- * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
274
- * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
284
+ * Deep clone this stack.
285
+ * @remarks Time O(N), Space O(N)
286
+ * @returns A new stack with the same content.
275
287
  */
276
288
  clone() {
277
- return new Stack(this, { toElementFn: this.toElementFn });
289
+ const out = this._createInstance({ toElementFn: this.toElementFn });
290
+ for (const v of this)
291
+ out.push(v);
292
+ return out;
278
293
  }
279
294
  /**
280
- * Time Complexity: O(n)
281
- * Space Complexity: O(n)
282
- *
283
- * The `filter` function creates a new stack containing elements from the original stack that satisfy
284
- * a given predicate function.
285
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
286
- * the current element being iterated over, the index of the current element, and the stack itself.
287
- * It should return a boolean value indicating whether the element should be included in the filtered
288
- * stack or not.
289
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
290
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
291
- * passed as the `this` value to the `predicate` function. If `thisArg` is
292
- * @returns The `filter` method is returning a new `Stack` object that contains the elements that
293
- * satisfy the given predicate function.
295
+ * Filter elements into a new stack of the same class.
296
+ * @remarks Time O(N), Space O(N)
297
+ * @param predicate - Predicate (value, index, stack) → boolean to keep value.
298
+ * @param [thisArg] - Value for `this` inside the predicate.
299
+ * @returns A new stack with kept values.
294
300
  */
295
301
  filter(predicate, thisArg) {
296
- const newStack = new Stack([], { toElementFn: this.toElementFn });
302
+ const out = this._createInstance({ toElementFn: this.toElementFn });
297
303
  let index = 0;
298
- for (const el of this) {
299
- if (predicate.call(thisArg, el, index, this)) {
300
- newStack.push(el);
301
- }
304
+ for (const v of this) {
305
+ if (predicate.call(thisArg, v, index, this))
306
+ out.push(v);
302
307
  index++;
303
308
  }
304
- return newStack;
309
+ return out;
305
310
  }
306
311
  /**
307
- * Time Complexity: O(n)
308
- * Space Complexity: O(n)
309
- *
310
- * The `map` function takes a callback function and applies it to each element in the stack,
311
- * returning a new stack with the results.
312
- * @param callback - The callback parameter is a function that will be called for each element in the
313
- * stack. It takes three arguments: the current element, the index of the element, and the stack
314
- * itself. It should return a new value that will be added to the new stack.
315
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
316
- * transform the raw element (`RM`) into a new element (`EM`) before pushing it into the new stack.
317
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
318
- * specify the value of `this` within the callback function. It is used to set the context or scope
319
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
320
- * value of
321
- * @returns a new Stack object with elements of type EM and raw elements of type RM.
312
+ * Map values into a new stack of the same element type.
313
+ * @remarks Time O(N), Space O(N)
314
+ * @param callback - Mapping function (value, index, stack) → newValue.
315
+ * @param [thisArg] - Value for `this` inside the callback.
316
+ * @returns A new stack with mapped values.
322
317
  */
323
- map(callback, toElementFn, thisArg) {
324
- const newStack = new Stack([], { toElementFn });
318
+ mapSame(callback, thisArg) {
319
+ const out = this._createInstance({ toElementFn: this.toElementFn });
325
320
  let index = 0;
326
- for (const el of this) {
327
- newStack.push(callback.call(thisArg, el, index, this));
321
+ for (const v of this) {
322
+ const mv = thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
323
+ out.push(mv);
324
+ }
325
+ return out;
326
+ }
327
+ /**
328
+ * Map values into a new stack (possibly different element type).
329
+ * @remarks Time O(N), Space O(N)
330
+ * @template EM
331
+ * @template RM
332
+ * @param callback - Mapping function (value, index, stack) → newElement.
333
+ * @param [options] - Options for the output stack (e.g., toElementFn).
334
+ * @param [thisArg] - Value for `this` inside the callback.
335
+ * @returns A new Stack with mapped elements.
336
+ */
337
+ map(callback, options, thisArg) {
338
+ const out = this._createLike([], Object.assign({}, (options !== null && options !== void 0 ? options : {})));
339
+ let index = 0;
340
+ for (const v of this) {
341
+ out.push(thisArg === undefined ? callback(v, index, this) : callback.call(thisArg, v, index, this));
328
342
  index++;
329
343
  }
330
- return newStack;
344
+ return out;
345
+ }
346
+ /**
347
+ * Set the equality comparator used by delete/search operations.
348
+ * @remarks Time O(1), Space O(1)
349
+ * @param equals - Equality predicate (a, b) → boolean.
350
+ * @returns This stack.
351
+ */
352
+ setEquality(equals) {
353
+ this._equals = equals;
354
+ return this;
355
+ }
356
+ /**
357
+ * (Protected) Find the index of a target element using the equality function.
358
+ * @remarks Time O(N), Space O(1)
359
+ * @param target - Element to search for.
360
+ * @returns Index or -1 if not found.
361
+ */
362
+ _indexOfByEquals(target) {
363
+ for (let i = 0; i < this.elements.length; i++)
364
+ if (this._equals(this.elements[i], target))
365
+ return i;
366
+ return -1;
331
367
  }
332
368
  /**
333
- * Time Complexity: O(n)
334
- * Space Complexity: O(n)
335
- *
336
- * Custom iterator for the Stack class.
337
- * @returns An iterator object.
369
+ * (Protected) Create an empty instance of the same concrete class.
370
+ * @remarks Time O(1), Space O(1)
371
+ * @param [options] - Options forwarded to the constructor.
372
+ * @returns An empty like-kind stack instance.
373
+ */
374
+ _createInstance(options) {
375
+ const Ctor = this.constructor;
376
+ return new Ctor([], options);
377
+ }
378
+ /**
379
+ * (Protected) Create a like-kind stack and seed it from an iterable.
380
+ * @remarks Time O(N), Space O(N)
381
+ * @template T
382
+ * @template RR
383
+ * @param [elements] - Iterable used to seed the new stack.
384
+ * @param [options] - Options forwarded to the constructor.
385
+ * @returns A like-kind Stack instance.
386
+ */
387
+ _createLike(elements = [], options) {
388
+ const Ctor = this.constructor;
389
+ return new Ctor(elements, options);
390
+ }
391
+ /**
392
+ * (Protected) Iterate elements from bottom to top.
393
+ * @remarks Time O(N), Space O(1)
394
+ * @returns Iterator of elements.
338
395
  */
339
396
  *_getIterator() {
340
- for (let i = 0; i < this.elements.length; i++) {
397
+ for (let i = 0; i < this.elements.length; i++)
341
398
  yield this.elements[i];
342
- }
343
399
  }
344
400
  }
345
401
  exports.Stack = Stack;