Open
Conversation
CheezItMan
reviewed
May 3, 2021
There was a problem hiding this comment.
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`
Comment on lines
+4
to
6
| # 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.
This turns the array into a heap, it doesn't "sort" the array using a heap. So it's not quite the exercise.
Comment on lines
+17
to
19
| # 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) |
Comment on lines
+27
to
29
| # Time Complexity: O(log n) where n is the number of nodes in the heap | ||
| # Space Complexity: O(2) == O(1) | ||
| def remove() |
Comment on lines
+52
to
54
| # 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? |
Comment on lines
+63
to
+65
| # Time complexity: O(log n) where n is the number of nodes in the heap | ||
| # Space complexity: O(1) | ||
| def heap_up(current) |
| 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.
What happens if @store[left_child] is nil? I think this could cause an error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up&heap_downmethods useful? Why?