Skip to content

Mira #3

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

Open
wants to merge 3 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
18 changes: 15 additions & 3 deletions lib/heapsort.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
const { MinHeap } = require('./minheap');

// This method uses a heap to sort an array.
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(logn)
// Space Complexity: O(n)
function heapsort(list) {
throw new Error('Method not implemented yet...');
const heap = new MinHeap();
const out = [];
let i = 0;

list.forEach(x => heap.add(x));

while (i < list.length) {
out[i] = heap.remove();
i++;
}
return out;
};

module.exports = heapsort;
65 changes: 52 additions & 13 deletions lib/minheap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@ class MinHeap {
}

// This method adds a HeapNode instance to the heap
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(logn)
// Space Complexity: O(1)
add(key, value = key) {
throw new Error("Method not implemented yet...");
const node = new HeapNode(key, value);
const i = this.store.push(node) - 1;
this.heapUp(i);
// console.log(this.store);
}

// This method removes and returns an element from the heap
// maintaining the heap structure
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(logn)
// Space Complexity: O(1)
remove() {
throw new Error("Method not implemented yet...");
if (this.isEmpty()) return;
this.swap(0, this.store.length - 1);
const { value } = this.store.pop();
if (!this.isEmpty()) this.heapDown(0);
return value;
}


Expand All @@ -38,26 +45,58 @@ class MinHeap {
}

// This method returns true if the heap is empty
// Time complexity: ?
// Space complexity: ?
// Time complexity: O(1)
// Space complexity: O(1)
isEmpty() {
throw new Error("Method not implemented yet...");
return this.store.length === 0;
}

// This helper method takes an index and
// moves it up the heap, if it is less than it's parent node.
// It could be **very** helpful for the add method.
// Time complexity: ?
// Space complexity: ?
// Time complexity: O(logn)
// Space complexity: O(1)
heapUp(index) {
throw new Error("Method not implemented yet...");
if (index <= 0) return;

const parentIndex = this.parentIndex(index);
if (this.store[index].key < this.store[parentIndex].key) {
this.swap(index, parentIndex);
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) {
throw new Error("Method not implemented yet...");
const [leftIndex, rightIndex] = this.children(index);

if (!leftIndex && !rightIndex) return;

let min;
rightIndex ? min = rightIndex : min = leftIndex;
if ((leftIndex && rightIndex) &&
this.store[leftIndex].key < this.store[rightIndex].key) {
min = leftIndex;
}

if (this.store[index].key > this.store[min].key) {
this.swap(index, min);
this.heapDown(min);
}
}

children(index) {
let i = index * 2 + 1, j;
if (i >= this.store.length) i = null;
if (i && i < this.store.length - 1) j = i + 1;
return [i, j];
}

parentIndex(index) {
const i = Math.floor((index - 1) / 2);
return i < 0 ? 0 : i;
}

// If you want a swap method... you're welcome
Expand Down