@gabrielrufino/cube
Version:
Data structures made in Typescript
102 lines (101 loc) • 4.02 kB
JavaScript
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var MaxHeap = /** @class */ (function () {
function MaxHeap(_a) {
var _b = _a === void 0 ? {} : _a, greaterThanOrEqualTo = _b.greaterThanOrEqualTo, _c = _b.inputs, inputs = _c === void 0 ? [] : _c;
this._data = [];
this._greaterThanOrEqualTo = function (value1, value2) { return value1 >= value2; };
this._getLeftIndex = function (index) { return (2 * index) + 1; };
this._getRightIndex = function (index) { return (2 * index) + 2; };
this._getParentIndex = function (index) { return Math.floor((index - 1) / 2); };
if (greaterThanOrEqualTo) {
this._greaterThanOrEqualTo = greaterThanOrEqualTo;
}
for (var _i = 0, inputs_1 = inputs; _i < inputs_1.length; _i++) {
var input = inputs_1[_i];
this.insert(input);
}
}
Object.defineProperty(MaxHeap.prototype, "data", {
get: function () {
return __spreadArray([], this._data, true);
},
enumerable: false,
configurable: true
});
Object.defineProperty(MaxHeap.prototype, "size", {
get: function () {
return this.data.length;
},
enumerable: false,
configurable: true
});
Object.defineProperty(MaxHeap.prototype, "isEmpty", {
get: function () {
return this.size === 0;
},
enumerable: false,
configurable: true
});
Object.defineProperty(MaxHeap.prototype, "max", {
get: function () {
return this.data[0] || null;
},
enumerable: false,
configurable: true
});
MaxHeap.prototype.insert = function (value) {
this._data = __spreadArray(__spreadArray([], this.data, true), [value], false);
this._siftUp(this.size - 1);
return value;
};
MaxHeap.prototype.extract = function () {
if (!this.isEmpty) {
var _a = this.data, min = _a[0], rest = _a.slice(1);
this._data = __spreadArray([rest[rest.length - 1]], rest.slice(0, rest.length - 1), true).filter(function (value) { return value !== undefined; });
this._siftDown(0);
return min;
}
return null;
};
MaxHeap.prototype._siftUp = function (index) {
var _a;
var parent = this._getParentIndex(index);
if (index > 0 && this._greaterThanOrEqualTo(this.data[index], this.data[parent])) {
_a = [this.data[index], this.data[parent]], this._data[parent] = _a[0], this._data[index] = _a[1];
this._siftUp(parent);
}
};
MaxHeap.prototype._siftDown = function (index) {
var _a, _b;
var left = this._getLeftIndex(index);
var right = this._getRightIndex(index);
if (left < this.size && this._greaterThanOrEqualTo(this.data[left], this.data[index])) {
_a = [this.data[index], this.data[left]], this._data[left] = _a[0], this._data[index] = _a[1];
this._siftDown(left);
}
if (right < this.size && this._greaterThanOrEqualTo(this.data[right], this.data[index])) {
_b = [this.data[index], this.data[right]], this._data[right] = _b[0], this._data[index] = _b[1];
this._siftDown(right);
}
};
MaxHeap.prototype[Symbol.toPrimitive] = function (type) {
var primitives = {
default: true,
number: this.size,
string: this.data.join(', '),
};
return primitives[type];
};
return MaxHeap;
}());
exports.default = MaxHeap;