-
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
Angele Z. #29
base: master
Are you sure you want to change the base?
Angele Z. #29
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.
Well you didn't get heapsort implemented, but otherwise this works. Take a look at my comments and let me know what questions you have.
lib/min_heap.rb
Outdated
# Time Complexity: O(h) - height of the heap | ||
# Space Complexity: O(h) | ||
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.
👍 note h = log n
lib/min_heap.rb
Outdated
# Time Complexity: O(h) | ||
# Space Complexity: O(h) | ||
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.
👍
lib/min_heap.rb
Outdated
# Time complexity: o(1) | ||
# 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.
O(1)?? How? This is a recursive method so it's going to have time/space complexity.
lib/min_heap.rb
Outdated
end | ||
swap(index, index_with_smaller_key) | ||
end | ||
heap_down(index_with_smaller_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.
This might lead you to heaping down, even when you didn't do a swap... It won't cause bugs, but might lead to extra recursive calls.
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.
Really well done, this hits all the learning goals. The code is well-written and executed. Awesome!
# Time Complexity: o((n * log n) + (n * log n)) => o(n log n) | it depends on the length of the list, but adding and removing from the list takes log(n), and the removal of each takes log(n) as well, and has to happen n times (the length of the list) | ||
# Space Complexity: also o(n log n) | ||
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.
The method here works, great! However the space complexity is O(n) because you're making a heap of n elements, not n log n elements.
# Time Complexity: O(log n) - height of the heap is a logarithmic relationship to the number of nodes | ||
# Space Complexity: O(log n) | ||
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(log n) | ||
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) - the access time complexity of an array | ||
# Space complexity: o(1) - the access space complexity of an array | ||
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.
👍
def heap_up(index) | ||
|
||
return if index == 0 | ||
parent_index = (index % 2 != 0) ? (index / 2) : (index / 2 - 1) |
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.
Could this just be:
parent_index = (index % 2 != 0) ? (index / 2) : (index / 2 - 1) | |
parent_index =(index - 1) / 2 |
# Time complexity: o(h) - the time it takes to run depends on the height of the heap; in the worst case scenario it will touch each level of the heap | ||
# Space complexity: o(h) - the space complexity of heap_up depends on the number of recursive calls that will be held in memory, up to the height of the heap. | ||
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.
👍 And h = log n, where n is the number of nodes.
# moves it down the heap if it's bigger | ||
# than its child 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.
👍 Nicely done
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?