UNPKG

tstruct

Version:

Data structures & basic algorithms library

149 lines (148 loc) 5.28 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.MinHeap = exports.MaxHeap = exports.Heap = void 0; var CompareFunction_1 = require("../CompareFunction"); var Heap = (function () { function Heap(compareFunction) { if (compareFunction === void 0) { compareFunction = CompareFunction_1.descendingCompareFunction; } this.compareFunction = compareFunction; this._data = []; } Object.defineProperty(Heap.prototype, "size", { get: function () { return this._data.length; }, enumerable: false, configurable: true }); Heap.prototype.getRoot = function () { return this._data[0]; }; Heap.prototype.extractRoot = function () { var root = this._data[0]; if (root != undefined) { this.remove(root); } return root; }; Heap.prototype.add = function (value) { this._data.push(value); if (this.size == 1) return; this._rearrange(this.size - 1); }; Heap.prototype.rearrange = function () { for (var i = 0; i < this.size; i++) { this._rearrange(i); } }; Heap.prototype._rearrange = function (index) { this._checkOnParent(index); this._checkOnChildren(index); }; Heap.prototype._checkOnParent = function (index) { var parentId = this.getParentIndex(index); while (this.compareFunction(this._data[parentId], this._data[index]) < 0 && index > 0) { var tmp = this._data[parentId]; this._data[parentId] = this._data[index]; this._data[index] = tmp; index = parentId; parentId = this.getParentIndex(index); } return index; }; Heap.prototype._checkOnChildren = function (index) { var _this = this; while (index < this.size) { var children = this.getChildrenIndex(index); if (children[0] >= this.size && children[1] >= this.size) { break; } var childrenValues = children.map(function (i) { return _this._data[i]; }); if (this.compareFunction(childrenValues[0], this._data[index]) < 0 && this.compareFunction(childrenValues[1], this._data[index]) < 0) { break; } if (this.compareFunction(childrenValues[0], childrenValues[1]) > 0 || childrenValues[1] == undefined) { this._data[children[0]] = this._data[index]; this._data[index] = childrenValues[0]; index = children[0]; } else { this._data[children[1]] = this._data[index]; this._data[index] = childrenValues[1]; index = children[1]; } } return index; }; Heap.prototype.remove = function (value) { var index = this._data.findIndex(function (v) { return v == value; }); if (index < 0) return; var elem = this._data.pop(); if (index >= this.size) return; this._data[index] = elem; this._checkOnChildren(index); }; Heap.prototype.toArray = function () { return this._data; }; Heap.prototype.getParentIndex = function (index) { return Math.floor((index - 1) / 2); }; Heap.prototype.getChildrenIndex = function (index) { return [index * 2 + 1, index * 2 + 2]; }; Heap.prototype[Symbol.iterator] = function () { return this._data[Symbol.iterator](); }; return Heap; }()); exports.Heap = Heap; var MaxHeap = (function (_super) { __extends(MaxHeap, _super); function MaxHeap() { return _super.call(this, CompareFunction_1.descendingCompareFunction) || this; } MaxHeap.prototype.extractMax = function () { return this.extractRoot(); }; MaxHeap.prototype.getMax = function () { return this.getRoot(); }; return MaxHeap; }(Heap)); exports.MaxHeap = MaxHeap; var MinHeap = (function (_super) { __extends(MinHeap, _super); function MinHeap() { return _super.call(this, CompareFunction_1.ascendingCompareFunction) || this; } MinHeap.prototype.extractMin = function () { return this.extractRoot(); }; MinHeap.prototype.getMin = function () { return this.getRoot(); }; return MinHeap; }(Heap)); exports.MinHeap = MinHeap;