-
Notifications
You must be signed in to change notification settings - Fork 7
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
Space - Shonda #5
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.
Overall pretty well done, you hit most of the learning goals.
Take a look at my inline comments especially on remove
. You lose some of the advantages of a heap because your remove
method is O(n) in time complexity.
@@ -6,7 +6,7 @@ Congratulations! You're submitting your assignment! | |||
|
|||
Question | Answer | |||
:------------- | :------------- | |||
How is a Heap different from a Binary Search Tree? | | |||
How is a Heap different from a Binary Search Tree? | heaps can be ordered by max and min and for |
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.
No other answers for comprehension questions?
// This method uses a heap to sort an array. | ||
// Time Complexity: ? | ||
// Space Complexity: ? | ||
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.
👍 Space & Time Complexity?
// Time Complexity: O(n) | ||
// Space Complexity: O(1) | ||
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.
👍 , This works, but the time complexity is O(log n)
heap = new MinHeap() | ||
heap.add(6, 'Pasta'); | ||
heap.add(12, 'Soup'); | ||
heap.add(20, 'Pizza'); | ||
heap.add(25, 'Donuts'); | ||
heap.add(13, 'Cookies'); | ||
|
||
console.log(heap.remove()) | ||
|
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.
heap = new MinHeap() | |
heap.add(6, 'Pasta'); | |
heap.add(12, 'Soup'); | |
heap.add(20, 'Pizza'); | |
heap.add(25, 'Donuts'); | |
heap.add(13, 'Cookies'); | |
console.log(heap.remove()) |
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
heapUp(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 has a time complexity of O(1), and it's really more of a swap with parent method.
// 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.
👍
let last = this.store.pop() | ||
this.store.unshift(last) |
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 unshift
is an O(n) operation, but all you really need to do is:
- Swap the 1st and last element
- Pop the last element (to return later).
Then you can heap_down as an O(log n) operation.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?