-
Notifications
You must be signed in to change notification settings - Fork 5
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
Branches -- Paige #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
const { MinHeap } = require('../lib/minheap'); | ||
|
||
// This method uses a heap to sort an array. | ||
// Time Complexity: ? | ||
// Space Complexity: ? | ||
// Time Complexity: O(n log n) - first iterate over list (O(n)) | ||
// and run .add() for each item (O(log n)) | ||
// Space Complexity: O(1) - uses original list param to store sorted list | ||
function heapsort(list) { | ||
throw new Error('Method not implemented yet...'); | ||
}; | ||
if (list.length <= 1) return list; | ||
const heap = new MinHeap(); | ||
const length = list.length; | ||
|
||
for (let i = 0; i < length; i++) { | ||
heap.add(list.pop()); | ||
} | ||
|
||
while (!heap.isEmpty()) { | ||
list.push(heap.remove()); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
module.exports = heapsort; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,18 +11,31 @@ class MinHeap { | |
} | ||
|
||
// This method adds a HeapNode instance to the heap | ||
// Time Complexity: ? | ||
// Space Complexity: ? | ||
// Time Complexity: O(log n) | ||
// Space Complexity: O(log n) | ||
add(key, value = key) { | ||
Comment on lines
+14
to
16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method works, and you have the space complexity right because you're doing recursion. |
||
throw new Error("Method not implemented yet..."); | ||
const newNode = new HeapNode(key, value); | ||
this.store.push(newNode); | ||
|
||
if (this.store.length === 1) { | ||
return; | ||
} else { | ||
return this.heapUp(this.store.length - 1); | ||
} | ||
} | ||
|
||
// 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(log n) | ||
remove() { | ||
throw new Error("Method not implemented yet..."); | ||
if (this.isEmpty()) return; | ||
|
||
this.swap(0, this.store.length - 1); | ||
const result = this.store.pop(); | ||
if (!this.isEmpty()) this.heapDown(0); | ||
|
||
return result.value; | ||
} | ||
|
||
|
||
|
@@ -38,26 +51,46 @@ class MinHeap { | |
} | ||
|
||
// This method returns true if the heap is empty | ||
// Time complexity: ? | ||
// Space complexity: ? | ||
// Time complexity: O(1) | ||
// Space complexity: O(1) | ||
isEmpty() { | ||
Comment on lines
+54
to
56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
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(log n) | ||
// Space complexity: O(log n) | ||
heapUp(index) { | ||
throw new Error("Method not implemented yet..."); | ||
let parent = Math.floor((index - 1)/2); | ||
if (parent < 1) parent = 0; | ||
|
||
if (this.store[parent].key > this.store[index].key) { | ||
this.swap(parent, index); | ||
return this.heapUp(parent); | ||
} | ||
} | ||
|
||
// This helper method takes an index and | ||
// moves it up the heap if it's smaller | ||
// than it's parent node. | ||
heapDown(index) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method works in most cases, but I think you're missing an edge-case here. |
||
throw new Error("Method not implemented yet..."); | ||
const leftChild = 2 * index + 1; | ||
const rightChild = 2 * index + 2; | ||
|
||
if (leftChild >= this.store.length || rightChild >= this.store.length) return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the left child is smaller than the length and the left child is smaller than it's parent, and at the same time the right child is equal to the length? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CheezItMan can you give me an example? I'm having a hard time thinking of how a child index would be smaller than its parent index. |
||
|
||
if (this.store[leftChild].key > this.store[index].key | ||
&& this.store[rightChild].key > this.store[index].key) { | ||
return; | ||
} else if (this.store[leftChild].key < this.store[rightChild].key) { | ||
this.swap(leftChild, index); | ||
return this.heapDown(leftChild); | ||
} else { | ||
this.swap(rightChild, index); | ||
return this.heapDown(rightChild); | ||
} | ||
} | ||
|
||
// If you want a swap method... you're welcome | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 , except that you're building a heap, so the space complexity is O(n).