JSFiddle - React, Tailwind, and code Playground
by dance2die
JavaScript 1.7
var Heap = function() {
this.storage = [];
};
Heap.prototype.insert = function(value) {
// Push to storage array
this.storage.push(value);
var that = this;
// Recursive function to handle swaps, input index
var reheapify = function(index) {
// Get parent index
var parentInd = Math.ceil(index/2-1);
// Base Case : value < parent or parent is null
if (parentInd < 0 || that.storage[index] <= that.storage[parentInd]) {
return 'value added to index '+index;
}
// Recursive Case: swap with parent and make recursive call
var temp = that.storage[index];
that.storage[index] = that.storage[parentInd];
that.storage[parentInd] = temp;
return reheapify(parentInd);
};
return reheapify(that.storage.length-1);
};
// Heap remove max method on prototype
// Remove the max value from a heap, reorder the heap, and return the max value
Heap.prototype.removeMax = function() {
// Check if heap is currently empty
if (this.storage.length === 0) {
// If nothing to remove then return null
return null;
} else if (this.storage.length === 1) {
// If heap only has one element in it then pop off the lone element in the storage array and return it
var removed = this.storage.pop();
return removed;
}
// Handle all other cases where heap has more than one node
// Preserve the max value in order to return it
var maxValue = this.storage[0];
// Replace the root node with the last node of the heap and remove the last node
this.storage[0] = this.storage.pop();
// Preserve context for inner recursive helper function
var that = this;
// Recursive function to restore the heap property of the heap
var reheapify = function(index) {
// Set index of max value to current node's index
var maxIndex = index;
// Check first child node's value against current node
if ((2*index + 1 < that.storage.length) && (that.storage[2*index + 1] > that.storage[index])) {
// If greater then set...