-
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?
Conversation
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.
Nice work, you hit the basic learning goals here. I do think you missed one edge-case on heapDown
, but otherwise very well done.
// 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) { |
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).
// Time Complexity: O(log n) | ||
// Space Complexity: O(log n) | ||
add(key, value = key) { |
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.
The method works, and you have the space complexity right because you're doing recursion.
// Time complexity: O(1) | ||
// Space complexity: O(1) | ||
isEmpty() { |
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.
👍
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 comment
The 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 comment
The 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[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 comment
The 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.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?