min-heap-typed 1.45.1 → 1.45.3

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.
@@ -271,19 +271,28 @@ class Heap {
271
271
  * @param index - The index of the newly added element.
272
272
  */
273
273
  bubbleUp(index) {
274
- const element = this.nodes[index];
274
+ // const element = this.nodes[index];
275
+ // while (index > 0) {
276
+ // const parentIndex = (index - 1) >> 1;
277
+ // const parent = this.nodes[parentIndex];
278
+ // if (this.comparator(element, parent) < 0) {
279
+ // this.nodes[index] = parent;
280
+ // this.nodes[parentIndex] = element;
281
+ // index = parentIndex;
282
+ // } else {
283
+ // break;
284
+ // }
285
+ // }
286
+ const item = this.nodes[index];
275
287
  while (index > 0) {
276
- const parentIndex = Math.floor((index - 1) / 2);
277
- const parent = this.nodes[parentIndex];
278
- if (this.comparator(element, parent) < 0) {
279
- this.nodes[index] = parent;
280
- this.nodes[parentIndex] = element;
281
- index = parentIndex;
282
- }
283
- else {
288
+ const parent = (index - 1) >> 1;
289
+ const parentItem = this.nodes[parent];
290
+ if (this.comparator(parentItem, item) <= 0)
284
291
  break;
285
- }
292
+ this.nodes[index] = parentItem;
293
+ index = parent;
286
294
  }
295
+ this.nodes[index] = item;
287
296
  }
288
297
  /**
289
298
  * Time Complexity: O(log n)
@@ -297,8 +306,8 @@ class Heap {
297
306
  * @param index - The index from which to start sinking.
298
307
  */
299
308
  sinkDown(index) {
300
- const leftChildIndex = 2 * index + 1;
301
- const rightChildIndex = 2 * index + 2;
309
+ const leftChildIndex = index << 1 | 1;
310
+ const rightChildIndex = leftChildIndex + 1;
302
311
  const length = this.nodes.length;
303
312
  let targetIndex = index;
304
313
  if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.45.1",
3
+ "version": "1.45.3",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -132,6 +132,6 @@
132
132
  "typescript": "^4.9.5"
133
133
  },
134
134
  "dependencies": {
135
- "data-structure-typed": "^1.45.0"
135
+ "data-structure-typed": "^1.45.3"
136
136
  }
137
137
  }
@@ -305,18 +305,28 @@ export class Heap<E = any> {
305
305
  * @param index - The index of the newly added element.
306
306
  */
307
307
  protected bubbleUp(index: number): void {
308
- const element = this.nodes[index];
308
+ // const element = this.nodes[index];
309
+ // while (index > 0) {
310
+ // const parentIndex = (index - 1) >> 1;
311
+ // const parent = this.nodes[parentIndex];
312
+ // if (this.comparator(element, parent) < 0) {
313
+ // this.nodes[index] = parent;
314
+ // this.nodes[parentIndex] = element;
315
+ // index = parentIndex;
316
+ // } else {
317
+ // break;
318
+ // }
319
+ // }
320
+
321
+ const item = this.nodes[index];
309
322
  while (index > 0) {
310
- const parentIndex = Math.floor((index - 1) / 2);
311
- const parent = this.nodes[parentIndex];
312
- if (this.comparator(element, parent) < 0) {
313
- this.nodes[index] = parent;
314
- this.nodes[parentIndex] = element;
315
- index = parentIndex;
316
- } else {
317
- break;
318
- }
323
+ const parent = (index - 1) >> 1;
324
+ const parentItem = this.nodes[parent];
325
+ if (this.comparator(parentItem, item) <= 0) break;
326
+ this.nodes[index] = parentItem;
327
+ index = parent;
319
328
  }
329
+ this.nodes[index] = item;
320
330
  }
321
331
 
322
332
  /**
@@ -332,8 +342,8 @@ export class Heap<E = any> {
332
342
  * @param index - The index from which to start sinking.
333
343
  */
334
344
  protected sinkDown(index: number): void {
335
- const leftChildIndex = 2 * index + 1;
336
- const rightChildIndex = 2 * index + 2;
345
+ const leftChildIndex = index << 1 | 1;
346
+ const rightChildIndex = leftChildIndex + 1;
337
347
  const length = this.nodes.length;
338
348
  let targetIndex = index;
339
349