min-heap-typed 1.49.1 → 1.49.2

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 (31) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +11 -0
  2. package/dist/data-structures/base/iterable-base.js +21 -0
  3. package/dist/data-structures/hash/hash-map.d.ts +9 -9
  4. package/dist/data-structures/hash/hash-map.js +16 -15
  5. package/dist/data-structures/heap/heap.d.ts +6 -35
  6. package/dist/data-structures/heap/heap.js +10 -42
  7. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +87 -93
  8. package/dist/data-structures/linked-list/doubly-linked-list.js +126 -129
  9. package/dist/data-structures/linked-list/singly-linked-list.d.ts +16 -21
  10. package/dist/data-structures/linked-list/singly-linked-list.js +42 -42
  11. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  12. package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
  13. package/dist/data-structures/queue/deque.d.ts +70 -75
  14. package/dist/data-structures/queue/deque.js +100 -110
  15. package/dist/data-structures/queue/queue.d.ts +13 -14
  16. package/dist/data-structures/queue/queue.js +15 -18
  17. package/dist/data-structures/stack/stack.d.ts +2 -3
  18. package/dist/data-structures/stack/stack.js +2 -5
  19. package/dist/data-structures/trie/trie.d.ts +1 -2
  20. package/dist/data-structures/trie/trie.js +2 -5
  21. package/package.json +1 -1
  22. package/src/data-structures/base/iterable-base.ts +24 -0
  23. package/src/data-structures/hash/hash-map.ts +27 -28
  24. package/src/data-structures/heap/heap.ts +19 -57
  25. package/src/data-structures/linked-list/doubly-linked-list.ts +138 -142
  26. package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
  27. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  28. package/src/data-structures/queue/deque.ts +122 -135
  29. package/src/data-structures/queue/queue.ts +19 -23
  30. package/src/data-structures/stack/stack.ts +4 -8
  31. package/src/data-structures/trie/trie.ts +5 -9
@@ -126,6 +126,12 @@ export declare abstract class IterableEntryBase<K = any, V = any> {
126
126
  * all the elements in the collection.
127
127
  */
128
128
  reduce<U>(callbackfn: ReduceEntryCallback<K, V, U>, initialValue: U): U;
129
+ hasValue(value: V): boolean;
130
+ /**
131
+ * Time Complexity: O(n)
132
+ * Space Complexity: O(n)
133
+ */
134
+ print(): void;
129
135
  protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
130
136
  }
131
137
  export declare abstract class IterableElementBase<V> {
@@ -228,5 +234,10 @@ export declare abstract class IterableElementBase<V> {
228
234
  * all the elements in the array and applying the callback function to each element.
229
235
  */
230
236
  reduce<U>(callbackfn: ReduceElementCallback<V, U>, initialValue: U): U;
237
+ /**
238
+ * Time Complexity: O(n)
239
+ * Space Complexity: O(n)
240
+ */
241
+ print(): void;
231
242
  protected abstract _getIterator(...args: any[]): IterableIterator<V>;
232
243
  }
@@ -172,6 +172,20 @@ class IterableEntryBase {
172
172
  }
173
173
  return accumulator;
174
174
  }
175
+ hasValue(value) {
176
+ for (const [, elementValue] of this) {
177
+ if (elementValue === value)
178
+ return true;
179
+ }
180
+ return false;
181
+ }
182
+ /**
183
+ * Time Complexity: O(n)
184
+ * Space Complexity: O(n)
185
+ */
186
+ print() {
187
+ console.log([...this]);
188
+ }
175
189
  }
176
190
  exports.IterableEntryBase = IterableEntryBase;
177
191
  class IterableElementBase {
@@ -308,5 +322,12 @@ class IterableElementBase {
308
322
  }
309
323
  return accumulator;
310
324
  }
325
+ /**
326
+ * Time Complexity: O(n)
327
+ * Space Complexity: O(n)
328
+ */
329
+ print() {
330
+ console.log([...this]);
331
+ }
311
332
  }
312
333
  exports.IterableElementBase = IterableElementBase;
@@ -42,13 +42,13 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
42
42
  * @param {V} value - The value parameter represents the value that you want to associate with the
43
43
  * key in the data structure.
44
44
  */
45
- set(key: K, value: V): void;
45
+ set(key: K, value: V): boolean;
46
46
  /**
47
47
  * The function "setMany" sets multiple key-value pairs in a map.
48
48
  * @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
49
49
  * key-value pair is represented as an array with two elements: the key and the value.
50
50
  */
51
- setMany(elements: Iterable<[K, V]>): void;
51
+ setMany(elements: Iterable<[K, V]>): boolean[];
52
52
  /**
53
53
  * The `get` function retrieves a value from a map based on a given key, either from an object map or
54
54
  * a string map.
@@ -114,6 +114,7 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
114
114
  */
115
115
  filter(predicate: EntryCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
116
116
  print(): void;
117
+ put(key: K, value: V): boolean;
117
118
  /**
118
119
  * The function returns an iterator that yields key-value pairs from both an object store and an
119
120
  * object map.
@@ -178,10 +179,9 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
178
179
  * value associated with the key being set in the data structure.
179
180
  * @returns the size of the data structure after the key-value pair has been set.
180
181
  */
181
- set(key: K, value?: V): number;
182
+ set(key: K, value?: V): boolean;
182
183
  has(key: K): boolean;
183
- hasValue(value: V): boolean;
184
- setMany(entries: Iterable<[K, V]>): void;
184
+ setMany(entries: Iterable<[K, V]>): boolean[];
185
185
  /**
186
186
  * Time Complexity: O(1)
187
187
  * Space Complexity: O(1)
@@ -207,7 +207,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
207
207
  * the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
208
208
  * where `K` is the key and `V` is the value.
209
209
  */
210
- getAt(index: number): [K, V];
210
+ getAt(index: number): V | undefined;
211
211
  /**
212
212
  * Time Complexity: O(1)
213
213
  * Space Complexity: O(1)
@@ -228,7 +228,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
228
228
  * deleted in the linked list.
229
229
  * @returns The size of the list after deleting the element at the specified index.
230
230
  */
231
- deleteAt(index: number): number;
231
+ deleteAt(index: number): boolean;
232
232
  /**
233
233
  * Time Complexity: O(1)
234
234
  * Space Complexity: O(1)
@@ -288,7 +288,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
288
288
  * function.
289
289
  */
290
290
  map<NV>(callback: EntryCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
291
- print(): void;
291
+ put(key: K, value: V): boolean;
292
292
  /**
293
293
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
294
294
  * Space Complexity: O(1)
@@ -306,5 +306,5 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
306
306
  * represents a node in a linked list. It contains a key-value pair and references to the previous
307
307
  * and next nodes in the list.
308
308
  */
309
- protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): void;
309
+ protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): boolean;
310
310
  }
@@ -68,6 +68,7 @@ class HashMap extends base_1.IterableEntryBase {
68
68
  }
69
69
  this._store[strKey] = { key, value };
70
70
  }
71
+ return true;
71
72
  }
72
73
  /**
73
74
  * The function "setMany" sets multiple key-value pairs in a map.
@@ -75,8 +76,10 @@ class HashMap extends base_1.IterableEntryBase {
75
76
  * key-value pair is represented as an array with two elements: the key and the value.
76
77
  */
77
78
  setMany(elements) {
79
+ const results = [];
78
80
  for (const [key, value] of elements)
79
- this.set(key, value);
81
+ results.push(this.set(key, value));
82
+ return results;
80
83
  }
81
84
  /**
82
85
  * The `get` function retrieves a value from a map based on a given key, either from an object map or
@@ -194,6 +197,9 @@ class HashMap extends base_1.IterableEntryBase {
194
197
  print() {
195
198
  console.log([...this.entries()]);
196
199
  }
200
+ put(key, value) {
201
+ return this.set(key, value);
202
+ }
197
203
  /**
198
204
  * The function returns an iterator that yields key-value pairs from both an object store and an
199
205
  * object map.
@@ -357,7 +363,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
357
363
  this._sentinel.prev = node;
358
364
  this._size++;
359
365
  }
360
- return this._size;
366
+ return true;
361
367
  }
362
368
  has(key) {
363
369
  if ((0, utils_1.isWeakKey)(key)) {
@@ -369,18 +375,13 @@ class LinkedHashMap extends base_1.IterableEntryBase {
369
375
  return hash in this._noObjMap;
370
376
  }
371
377
  }
372
- hasValue(value) {
373
- for (const [, elementValue] of this) {
374
- if (elementValue === value)
375
- return true;
376
- }
377
- return false;
378
- }
379
378
  setMany(entries) {
379
+ const results = [];
380
380
  for (const entry of entries) {
381
381
  const [key, value] = entry;
382
- this.set(key, value);
382
+ results.push(this.set(key, value));
383
383
  }
384
+ return results;
384
385
  }
385
386
  /**
386
387
  * Time Complexity: O(1)
@@ -424,7 +425,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
424
425
  while (index--) {
425
426
  node = node.next;
426
427
  }
427
- return [node.key, node.value];
428
+ return node.value;
428
429
  }
429
430
  /**
430
431
  * Time Complexity: O(1)
@@ -477,8 +478,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
477
478
  while (index--) {
478
479
  node = node.next;
479
480
  }
480
- this._deleteNode(node);
481
- return this._size;
481
+ return this._deleteNode(node);
482
482
  }
483
483
  /**
484
484
  * Time Complexity: O(1)
@@ -571,8 +571,8 @@ class LinkedHashMap extends base_1.IterableEntryBase {
571
571
  }
572
572
  return mappedMap;
573
573
  }
574
- print() {
575
- console.log([...this]);
574
+ put(key, value) {
575
+ return this.set(key, value);
576
576
  }
577
577
  /**
578
578
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
@@ -608,6 +608,7 @@ class LinkedHashMap extends base_1.IterableEntryBase {
608
608
  this._tail = prev;
609
609
  }
610
610
  this._size -= 1;
611
+ return true;
611
612
  }
612
613
  }
613
614
  exports.LinkedHashMap = LinkedHashMap;
@@ -52,19 +52,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
52
52
  * Insert an element into the heap and maintain the heap properties.
53
53
  * @param element - The element to be inserted.
54
54
  */
55
- add(element: E): Heap<E>;
56
- /**
57
- * Time Complexity: O(log n), where n is the number of elements in the heap.
58
- * Space Complexity: O(1)
59
- */
60
- /**
61
- * Time Complexity: O(log n), where n is the number of elements in the heap.
62
- * Space Complexity: O(1)
63
- *
64
- * Insert an element into the heap and maintain the heap properties.
65
- * @param element - The element to be inserted.
66
- */
67
- push(element: E): Heap<E>;
55
+ add(element: E): boolean;
68
56
  /**
69
57
  * Time Complexity: O(log n), where n is the number of elements in the heap.
70
58
  * Space Complexity: O(1)
@@ -77,18 +65,6 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
77
65
  * @returns The top element or undefined if the heap is empty.
78
66
  */
79
67
  poll(): E | undefined;
80
- /**
81
- * Time Complexity: O(log n), where n is the number of elements in the heap.
82
- * Space Complexity: O(1)
83
- */
84
- /**
85
- * Time Complexity: O(log n), where n is the number of elements in the heap.
86
- * Space Complexity: O(1)
87
- *
88
- * Remove and return the top element (smallest or largest element) from the heap.
89
- * @returns The top element or undefined if the heap is empty.
90
- */
91
- pop(): E | undefined;
92
68
  /**
93
69
  * Peek at the top element of the heap without removing it.
94
70
  * @returns The top element or undefined if the heap is empty.
@@ -114,7 +90,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
114
90
  * Clear and add elements of the heap
115
91
  * @param elements
116
92
  */
117
- refill(elements: E[]): void;
93
+ refill(elements: E[]): boolean[];
118
94
  /**
119
95
  * Time Complexity: O(n), where n is the number of elements in the heap.
120
96
  * Space Complexity: O(1)
@@ -203,7 +179,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
203
179
  *
204
180
  * Fix the entire heap to maintain heap properties.
205
181
  */
206
- fix(): void;
182
+ fix(): boolean[];
207
183
  /**
208
184
  * Time Complexity: O(n)
209
185
  * Space Complexity: O(n)
@@ -250,12 +226,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
250
226
  * original Heap.
251
227
  */
252
228
  map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T>;
253
- /**
254
- * Time Complexity: O(log n)
255
- * Space Complexity: O(1)
256
- */
257
- print(): void;
258
- protected _getIterator(): Generator<E, void, unknown>;
229
+ protected _getIterator(): IterableIterator<E>;
259
230
  /**
260
231
  * Time Complexity: O(n)
261
232
  * Space Complexity: O(1)
@@ -267,7 +238,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
267
238
  * Float operation to maintain heap properties after adding an element.
268
239
  * @param index - The index of the newly added element.
269
240
  */
270
- protected _bubbleUp(index: number): void;
241
+ protected _bubbleUp(index: number): boolean;
271
242
  /**
272
243
  * Time Complexity: O(log n)
273
244
  * Space Complexity: O(1)
@@ -276,7 +247,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
276
247
  * @param index - The index from which to start sinking.
277
248
  * @param halfLength
278
249
  */
279
- protected _sinkDown(index: number, halfLength: number): void;
250
+ protected _sinkDown(index: number, halfLength: number): boolean;
280
251
  }
281
252
  export declare class FibonacciHeapNode<E> {
282
253
  element: E;
@@ -42,7 +42,7 @@ class Heap extends base_1.IterableElementBase {
42
42
  }
43
43
  if (elements) {
44
44
  for (const el of elements) {
45
- this.push(el);
45
+ this.add(el);
46
46
  }
47
47
  // this.fix();
48
48
  }
@@ -85,23 +85,8 @@ class Heap extends base_1.IterableElementBase {
85
85
  * @param element - The element to be inserted.
86
86
  */
87
87
  add(element) {
88
- return this.push(element);
89
- }
90
- /**
91
- * Time Complexity: O(log n), where n is the number of elements in the heap.
92
- * Space Complexity: O(1)
93
- */
94
- /**
95
- * Time Complexity: O(log n), where n is the number of elements in the heap.
96
- * Space Complexity: O(1)
97
- *
98
- * Insert an element into the heap and maintain the heap properties.
99
- * @param element - The element to be inserted.
100
- */
101
- push(element) {
102
88
  this._elements.push(element);
103
- this._bubbleUp(this.elements.length - 1);
104
- return this;
89
+ return this._bubbleUp(this.elements.length - 1);
105
90
  }
106
91
  /**
107
92
  * Time Complexity: O(log n), where n is the number of elements in the heap.
@@ -125,20 +110,6 @@ class Heap extends base_1.IterableElementBase {
125
110
  }
126
111
  return value;
127
112
  }
128
- /**
129
- * Time Complexity: O(log n), where n is the number of elements in the heap.
130
- * Space Complexity: O(1)
131
- */
132
- /**
133
- * Time Complexity: O(log n), where n is the number of elements in the heap.
134
- * Space Complexity: O(1)
135
- *
136
- * Remove and return the top element (smallest or largest element) from the heap.
137
- * @returns The top element or undefined if the heap is empty.
138
- */
139
- pop() {
140
- return this.poll();
141
- }
142
113
  /**
143
114
  * Peek at the top element of the heap without removing it.
144
115
  * @returns The top element or undefined if the heap is empty.
@@ -172,7 +143,7 @@ class Heap extends base_1.IterableElementBase {
172
143
  */
173
144
  refill(elements) {
174
145
  this._elements = elements;
175
- this.fix();
146
+ return this.fix();
176
147
  }
177
148
  /**
178
149
  * Time Complexity: O(n), where n is the number of elements in the heap.
@@ -209,7 +180,7 @@ class Heap extends base_1.IterableElementBase {
209
180
  if (index < 0)
210
181
  return false;
211
182
  if (index === 0) {
212
- this.pop();
183
+ this.poll();
213
184
  }
214
185
  else if (index === this.elements.length - 1) {
215
186
  this.elements.pop();
@@ -321,8 +292,10 @@ class Heap extends base_1.IterableElementBase {
321
292
  * Fix the entire heap to maintain heap properties.
322
293
  */
323
294
  fix() {
295
+ const results = [];
324
296
  for (let i = Math.floor(this.size / 2); i >= 0; i--)
325
- this._sinkDown(i, this.elements.length >> 1);
297
+ results.push(this._sinkDown(i, this.elements.length >> 1));
298
+ return results;
326
299
  }
327
300
  /**
328
301
  * Time Complexity: O(n)
@@ -349,7 +322,7 @@ class Heap extends base_1.IterableElementBase {
349
322
  let index = 0;
350
323
  for (const current of this) {
351
324
  if (callback.call(thisArg, current, index, this)) {
352
- filteredList.push(current);
325
+ filteredList.add(current);
353
326
  }
354
327
  index++;
355
328
  }
@@ -388,13 +361,6 @@ class Heap extends base_1.IterableElementBase {
388
361
  }
389
362
  return mappedHeap;
390
363
  }
391
- /**
392
- * Time Complexity: O(log n)
393
- * Space Complexity: O(1)
394
- */
395
- print() {
396
- console.log([...this]);
397
- }
398
364
  *_getIterator() {
399
365
  for (const element of this.elements) {
400
366
  yield element;
@@ -422,6 +388,7 @@ class Heap extends base_1.IterableElementBase {
422
388
  index = parent;
423
389
  }
424
390
  this.elements[index] = element;
391
+ return true;
425
392
  }
426
393
  /**
427
394
  * Time Complexity: O(log n)
@@ -448,6 +415,7 @@ class Heap extends base_1.IterableElementBase {
448
415
  index = left;
449
416
  }
450
417
  this.elements[index] = element;
418
+ return true;
451
419
  }
452
420
  }
453
421
  exports.Heap = Heap;