-
Notifications
You must be signed in to change notification settings - Fork 40
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
Leaves - Hallie Johnson #17
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 major learning goals here. Do remember that when you do recursion, you incur space complexity due to the system stack!
# then again to remove the sorted elements and output them as a list O(2n), drop the constant. | ||
# Space Complexity: O(1); I think this is constant, because the heap is created as the array is | ||
# reduced so there is not a growth in memory space (except that a heap may take more space?). | ||
def 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.
Good insight on space complexity, but the array doesn't actually shrink when you do .pop
just the amount of spaces used inside it. Otherwise this works.
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def 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.
👍
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) It only stores the result variable, which is not altered by | ||
# the size of the input. | ||
def 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.
👍
# Time complexity: O(1) - arrays keep track of their length so that this can be contant time retrieval. | ||
# Space complexity: O(1) | ||
def empty? |
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: O(log n); it compares with parent nodes, not all nodes. | ||
# Space complexity: O(1); swaps are happening in-place. | ||
def heap_up(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.
Since this is a recursive method the space complexity is O(log n) because of the system stack!
# This helper method takes an index and | ||
# moves it up the heap if it's smaller | ||
# than it's parent node. | ||
# ===> Aren't we taking the new root and comparing it to its child and moving it DOWN? | ||
def heap_down(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.
👍 Well done!
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?