-
Notifications
You must be signed in to change notification settings - Fork 37
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
Water - Christabel #15
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.
It looks pretty good Christabel, I like the iterative methods, very efficient. However I think there are some hidden bugs in heap_down. Take a look at my comments and let me know if you have questions.
Consider:
heap.add(3, 3)
heap.add(6, 6)
heap.add(1, 1)
Then if you try to remove things you'll get NoMethodError: undefined method
key' for nil:NilClass`
# Time Complexity: O(n log n) where n is the number of elements in the given array | ||
# 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.
This turns the array into a heap, it doesn't "sort" the array using a heap. So it's not quite the exercise.
# Time Complexity: O(log n) where n is the number of nodes in the heap | ||
# Space Complexity: O(2) == 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.
👍 , nice iterative heap_up
# Time Complexity: O(log n) where n is the number of nodes in the heap | ||
# Space Complexity: O(2) == 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.
👍
# Time complexity: O(1) ? I think the length of arrays is somehow cached or something in Ruby? If not, then O(n) where n is the number of elements in the array | ||
# 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) where n is the number of nodes in the heap | ||
# Space complexity: O(1) | ||
def heap_up(current) |
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.
👍 , very cleanly written
right_child = current * 2 + 2 | ||
child = @store[left_child].key < @store[right_child].key ? left_child : right_child | ||
|
||
while @store[current].key > @store[child].key do |
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 happens if @store[left_child]
is nil
? I think this could cause an error.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?