Skip to content

Commit

Permalink
Removed extraneous heap code
Browse files Browse the repository at this point in the history
  • Loading branch information
Syeoryn committed Jan 27, 2015
1 parent 140c194 commit 29469ca
Showing 1 changed file with 1 addition and 53 deletions.
54 changes: 1 addition & 53 deletions Heap/src/heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit 29469ca

Please sign in to comment.