|
1 | 1 | (function (exports) {
|
| 2 | + 'use strict'; |
2 | 3 |
|
3 | 4 | /**
|
4 | 5 | * Constructor function of minimum heap
|
|
20 | 21 | /**
|
21 | 22 | * Exchange indexes with start index given as argument
|
22 | 23 | * to turn the tree into a valid heap. On a single call
|
23 |
| - * this method maintains only a single "branch" of the heap. Complexity O(log n) |
| 24 | + * this method maintains only a single "branch" of the heap. |
| 25 | + * Complexity O(log n) |
24 | 26 | *
|
25 | 27 | * @private
|
26 | 28 | * @param {number} index The parent
|
|
31 | 33 | right = 2 * index + 2,
|
32 | 34 | temp;
|
33 | 35 |
|
34 |
| - if (left < this._heap.length && this._cmp(this._heap[left], this._heap[index]) > 0) |
| 36 | + if (left < this._heap.length && |
| 37 | + this._cmp(this._heap[left], this._heap[index]) > 0) { |
35 | 38 | extr = left;
|
| 39 | + } |
36 | 40 |
|
37 |
| - if (right < this._heap.length && this._cmp(this._heap[right], this._heap[index]) > 0) |
| 41 | + if (right < this._heap.length && |
| 42 | + this._cmp(this._heap[right], this._heap[index]) > 0) { |
38 | 43 | extr = right;
|
| 44 | + } |
39 | 45 |
|
40 | 46 | if (index !== extr) {
|
41 | 47 | temp = this._heap[index];
|
|
54 | 60 | * @returns {number} parent The new position of the element
|
55 | 61 | */
|
56 | 62 | Heap.prototype.changeKey = function (index, value) {
|
| 63 | + this._heap[index] = value; |
57 | 64 | var elem = this._heap[index],
|
58 | 65 | parent = Math.floor(index / 2),
|
59 | 66 | temp;
|
|
92 | 99 | };
|
93 | 100 |
|
94 | 101 | /**
|
95 |
| - * Removes and returns the current extremum value which is on the top of the heap. |
| 102 | + * Removes and returns the current extremum value |
| 103 | + * which is on the top of the heap. |
96 | 104 | * Complexity O(log n).
|
97 | 105 | *
|
98 | 106 | * @public
|
99 | 107 | * @returns {number} The extremum value
|
100 | 108 | */
|
101 | 109 | Heap.prototype.extract = function () {
|
102 |
| - if (!this._heap.length) |
| 110 | + if (!this._heap.length) { |
103 | 111 | throw 'The heap is already empty!';
|
| 112 | + } |
104 | 113 |
|
105 | 114 | var extr = this._heap.shift();
|
106 | 115 | this._heapify(0);
|
107 | 116 | return extr;
|
108 | 117 | };
|
109 | 118 |
|
| 119 | + Heap.prototype.getCollection = function () { |
| 120 | + return this._heap; |
| 121 | + }; |
| 122 | + |
110 | 123 | Heap.prototype.isEmpty = function () {
|
111 | 124 | return !this._heap.length;
|
112 | 125 | };
|
|
0 commit comments