From 29469ca379e44f09f4fdc8528a641719500ec16d Mon Sep 17 00:00:00 2001 From: Drew Cuthbertson Date: Mon, 26 Jan 2015 19:01:37 -0800 Subject: [PATCH] Removed extraneous heap code --- Heap/src/heap.js | 54 +----------------------------------------------- 1 file changed, 1 insertion(+), 53 deletions(-) diff --git a/Heap/src/heap.js b/Heap/src/heap.js index 36447eb..6bef385 100644 --- a/Heap/src/heap.js +++ b/Heap/src/heap.js @@ -29,66 +29,14 @@ Heap.prototype.show = function(noLog) { // For you to complete: Heap.prototype.peek = function() { - return this._data[0]; + }; Heap.prototype.insert = function(element) { - this._data.push(element); - var current = this._data.length - 1; - var parent = this._parentOf(current); - var working = true; - - while (working) { - if (this._data[parent] > this._data[current]) { - this._swap(current, parent); - current = parent; - parent = this._parentOf(current); - } else { - working = false; - } - } }; Heap.prototype.extract = function() { - if (this._data.length === 1) { - return this._data.pop(); - } - - var toReturn = this._data[0]; - this._data[0] = this._data.pop(); - var current = 0; - var left = 1; - var right = 2; - var working = true; - - while (working) { - if (this._hasElementAt(left) || this._hasElementAt(right)) { - // Find which of the two children is smaller. - var minChild; - if (!this._hasElementAt(left)) { - minChild = right; - } else if (!this._hasElementAt(right)) { - minChild = left; - } else if (this._data[right] < this._data[left]) { - minChild = right; - } else { - minChild = left; - } - - if (this._data[current] > this._data[minChild]) { - this._swap(current, minChild); - current = minChild; - left = this._leftChildOf(current); - right = this._rightChildOf(current); - } else { - working = false; - } - } else { - working = false; - } - } - return toReturn; };