-
Notifications
You must be signed in to change notification settings - Fork 51
Mac P. #20
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
base: master
Are you sure you want to change the base?
Mac P. #20
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, Mac! I left a few comments on complexity below. Also technically you could create a heap with a linked list, it's just, as you said, much more convoluted and therefore not advisable.
🟢
@@ -1,8 +1,19 @@ | |||
|
|||
from heapq import heappush, heappop | |||
|
|||
def heap_sort(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.
✨ Time and space complexity? It would also be great to see you implement heap_sort
using the MinHeap
you implement below!
larger than either of its children and continues until | ||
the heap property is reestablished. | ||
""" | ||
pass | ||
left_index = (index * 2) + 1 |
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.
✨
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: O(1) | ||
Space complexity: O(n) |
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.
✨ However time and space complexity are O(log n) here because of the recursive call. heap_up
will be called (log n) times because each time you are halving the index
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: O(1) | ||
Space complexity: O(1) |
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.
✨
|
||
self.store.append(node) | ||
|
||
self.heap_up(len(self.store) - 1) | ||
|
||
def remove(self): |
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.
✨ However space complexity will be O(log n) because of the recursive call stack of heap_down
""" Adds a HeapNode instance to the heap | ||
If value == None the new node's value is set to key | ||
Time Complexity: O(log n) | ||
Space Complexity: O(n) |
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.
✨ However space complexity will just be O(log n) here because of the recursive call stack of heap_up
. You aren't creating any other significant data structures.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?