-
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_Ga-Young #32
base: master
Are you sure you want to change the base?
Leaves_Ga-Young #32
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.
I apologize for taking so long to get to this. I did find the small bug in your heap_down
code which was getting caught in heapsort
. Take a look and let me know if I need to clarify.
lib/heap_sort.rb
Outdated
end | ||
|
||
# I'm having trouble figuring out why the "can sort a 5-element array" test will not pass for this. I have created a new "can sort a different 5-element array" test and that passes. I am thinking it has to do with the order of numbers - some exact pattern of numbers is causing the heap to not sort properly, but all my tests from the min_heap are passing. Could I get some help? |
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.
I think it's a bug with your heap.
lib/min_heap.rb
Outdated
# Time Complexity: O(log n) where n is number of nodes | ||
# Space Complexity: O(1) | ||
def add(key, value) |
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 a recursive method and it will make log n recursive calls. This method has O(log n) space complexity.
lib/min_heap.rb
Outdated
# Time Complexity: O(log n) where n is number of nodes | ||
# 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.
- Similar issue to above on space complexity.
- What if the heap is empty? maybe a check is appropriate.
# 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.
👍
lib/min_heap.rb
Outdated
else | ||
return | ||
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.
This is a bit redundant.
else | |
return | |
end |
lib/min_heap.rb
Outdated
left_child = (index * 2 + 1) | ||
right_child = (index * 2 + 2) | ||
|
||
until @store[left_child] == nil || @store[right_child] == nil |
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 not nil
but the right child is, and yet if the parent is still bigger than the left child.
# moves it up the heap if it's smaller | ||
# moves it down 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.
Ok this one has a small bug that heapsort caught. Take a look at it, you can get into the circumstance that the left child is not nil and smaller than the parent and the right child is nil.
Hi Chris, I think I fixed that bug. Could you take another look? |
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?