-
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 - Mariya #30
base: master
Are you sure you want to change the base?
Leaves - Mariya #30
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 nice work, you hit the major learning goals here. Well done. Take a look at my feedback and let me know if you have any questions. Most of the feedback is centered around BigO and extra recursive calls on heap_up and heap_down.
# Time Complexity: O (n log (n)) | ||
# Space Complexity: O(n) | ||
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 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.
Since heap_up
is recursive the space complexity is O(log n)
# Time Complexity: O(log(n)) | ||
# Space Complexity: O(1) | ||
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.
👍 However like add
the space complexity is O(log n)
if @store.length == 0 | ||
return true | ||
else | ||
return false | ||
end |
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.
if @store.length == 0 | |
return true | |
else | |
return false | |
end | |
return @store.length == 0 |
# Time complexity: O(1) | ||
# 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.
👍
while parent > 0 | ||
return heap_up(parent) | ||
end |
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.
Note you're making extra recursive calls here. You only want to heap_up
if you made a swap, and since you're returning here you don't need a while
loop.
# Time complexity: O(log (n)) | ||
# Space complexity: O(1) | ||
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, it's going to be O(log n) space complexity.
swap(index, smallest_child) | ||
end | ||
|
||
return heap_down(smallest_child) |
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.
Similar to the heap_up method you're making extra recursive calls. You only need to continue to heap_down
if you make a swap.
end | ||
|
||
|
||
# This helper method takes an index and | ||
# moves it up the heap if it's smaller | ||
# than it's parent node. | ||
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.
This works, but it has issues with extra swaps.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?