Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aeron - Rock #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/heapsort.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import MinHeap from "./minheap";

// This method uses a heap to sort an array.
// Time Complexity: ?
// Space Complexity: ?
function heapsort(list) {
throw new Error('Method not implemented yet...');
let heap = new MinHeap;

while(!heap.isEmpty()) {
list.push(heap.remove());
}
Comment on lines +7 to +11

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems 1/2 completed?

};

module.exports = heapsort;
57 changes: 46 additions & 11 deletions lib/minheap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@ class MinHeap {
constructor() {
this.store = [];
}

// This method adds a HeapNode instance to the heap
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: generally Log N
// Space Complexity: equal to the nodes in the heap - 1

add(key, value = key) {
Comment on lines +13 to 16

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

throw new Error("Method not implemented yet...");

const node = new HeapNode(key, value);
this.store.push(node);
if (this.store.length > 1) {
this.heapUp(this.store.length - 1);
}
}

// This method removes and returns an element from the heap
// maintaining the heap structure
// Time Complexity: ?
// Space Complexity: ?
remove() {
Comment on lines 27 to 29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

throw new Error("Method not implemented yet...");
if (!this.store.length) return;

this.swap(0, this.store.length - 1);
const removed = this.store.pop();
this.heapDown(0);

return removed.value;
}


Expand All @@ -41,7 +52,7 @@ class MinHeap {
// Time complexity: ?
// Space complexity: ?
isEmpty() {
throw new Error("Method not implemented yet...");
return !this.store
}

// This helper method takes an index and
Expand All @@ -50,23 +61,47 @@ class MinHeap {
// Time complexity: ?
// Space complexity: ?
heapUp(index) {
Comment on lines 61 to 63

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Time/space complexity.

throw new Error("Method not implemented yet...");

const parentIndex = Math.floor((index - 1) / 2);
const parentNode = this.store[parentIndex];
const newNode = this.store[index];

if (newNode.key < parentNode.key) {
this.swap(index, parentIndex);
if (parentIndex > 0) {
this.heapUp(parentIndex)
}
}
}

// This helper method takes an index and
// moves it up the heap if it's smaller
// than it's parent node.
heapDown(index) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

throw new Error("Method not implemented yet...");
const left = 2 * index + 1
const right = 2 * index + 2
let smallest = index;

if((left < this.store.length) && (this.store[left].key < this.store[smallest].key)) {
smallest = left;
}

if ((right < this.store.length) && (this.store[right].key < this.store[smallest].key)) {
smallest = right;
}

if (index !== smallest){
this.swap(index, smallest);
this.heapDown(smallest);
}
}


// If you want a swap method... you're welcome
swap(index1, index2) {
const s = this.store;
[s[index1], s[index2]] = [s[index2], s[index1]];
}
}

module.exports = {
MinHeap
};
module.exports = { MinHeap };