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

C16 - Afina Walton #1

Open
wants to merge 4 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
Binary file added .DS_Store
Binary file not shown.
Binary file added lib/.DS_Store
Binary file not shown.
19 changes: 16 additions & 3 deletions lib/heapsort.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
const { MinHeap } = require('./minheap');
// This method uses a heap to sort an array.
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(n log n)
// Space Complexity: O(n)
function heapsort(list) {

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 (list.length === 0) return list;

const heap = new MinHeap();

for (const num of list) {
heap.add(num);
}

for (let i = 0; i < list.length; i++) {
list[i] = heap.remove();
}

return list;
};

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

// This method adds a HeapNode instance to the heap
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log n)
// Space Complexity: O(1)
add(key, value = key) {

Choose a reason for hiding this comment

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

✨ However space complexity will be O(log n) here because of the recursive call stack of heap_up

throw new Error("Method not implemented yet...");
// create new HeapNode
const newNode = new HeapNode(key, value);

// add the node to the end
this.store.push(newNode);

// perform heap-up where parent must be less than children
const index = this.store.length - 1;
this.heapUp(index);
}

// This method removes and returns an element from the heap
// maintaining the heap structure
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log n)
// Space Complexity: O(1)
remove() {

Choose a reason for hiding this comment

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

✨ However space complexity will be O(log n) here because of the recursive call stack of heap_down

throw new Error("Method not implemented yet...");
// if MinHeap is empty
if (this.store.length === 0) return;

// if MinHeap is not empty
this.swap(0, this.store.length - 1);
// remove last
const removed = this.store.pop();
// take the root and do a heap down
this.heapDown(0);

return removed.value;
}


Expand All @@ -38,26 +56,72 @@ class MinHeap {
}

// This method returns true if the heap is empty
// Time complexity: ?
// Space complexity: ?
// Time complexity: O(1)
// Space complexity: O(1)
isEmpty() {

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 === 0) return true

return false;
}

// 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(log n)
// Space complexity: O(1)

Choose a reason for hiding this comment

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

✨ However, because of the recursive call stack space complexity will be O(log n)

heapUp(index) {
throw new Error("Method not implemented yet...");
// update position in heap/array
const parentIndex = Math.floor((index - 1) / 2);

const element = this.store[index];
const parent = this.store[parentIndex];

// base case
if (index === 0 || parent.key < element.key) {
// pass

Choose a reason for hiding this comment

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

This works however, it's generally considered bad style to leave the body of an if statement empty

Suggested change
// pass
return

} else if (parent.key > element.key) {
this.swap(parentIndex, index);
index = parentIndex;
this.heapUp(index);
}

return;
}

// 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 element = this.store[index];

const rightChildIndex = (index * 2) + 2;
const leftChildIndex = (index * 2) + 1;
let leftChild, rightChild;
let smallestItemIndex = index;

if (leftChildIndex < this.store.length) {
leftChild = this.store[leftChildIndex];
if (leftChild.key < element.key) {
smallestItemIndex = leftChildIndex;
}
}

if (rightChildIndex < this.store.length) {
rightChild = this.store[rightChildIndex];

if (rightChild.key < this.store[smallestItemIndex].key) {
smallestItemIndex = rightChildIndex;
}
}

// base case
if (smallestItemIndex === index) {
return;
}
// swap
// perform heapDown
this.swap(index, smallestItemIndex);
this.heapDown(smallestItemIndex);
}

// If you want a swap method... you're welcome
Expand All @@ -68,5 +132,6 @@ class MinHeap {
}

module.exports = {
MinHeap
MinHeap,
HeapNode
};
3 changes: 2 additions & 1 deletion test/heapsort.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const expect = require('chai').expect;
const heapsort = require('../lib/heapsort');

describe.skip("heapsort", function() {
describe("heapsort", function() {
it("sorts an empty array", function() {
// Arrange
const list = [];
Expand Down
Loading