-
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
Branches - Monick #12
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.
Overall pretty well done, some errors on BigO and one issue with heap_down. See my inline comments and let me know what questions you have. You did hit the main learning goals here.
list.size.times do |node| | ||
last_node = heap.remove() | ||
if sorted_list.empty? | ||
sorted_list.push(last_node) | ||
elsif last_node < sorted_list[-1] | ||
temp = sorted_list[-1] | ||
sorted_list[-1] = last_node | ||
sorted_list.push(temp) | ||
else | ||
sorted_list.push(last_node) | ||
end | ||
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 overly complicated. Remember you always remove the smallest element of a heap.
list.size.times do |node| | |
last_node = heap.remove() | |
if sorted_list.empty? | |
sorted_list.push(last_node) | |
elsif last_node < sorted_list[-1] | |
temp = sorted_list[-1] | |
sorted_list[-1] = last_node | |
sorted_list.push(temp) | |
else | |
sorted_list.push(last_node) | |
end | |
end | |
until heap.empty? | |
sorted_list << heap.remove() | |
end |
# 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.
👍
# 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.
👍
# Time complexity: O(n) | ||
# 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.
It works, but how is this O(n) time complexity? Why not O(1)?
# 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.
Note, you're doing recursion so this is O(log n) space complexity. You gotta remember the system stack!
right_child = 2*index+2 | ||
|
||
# if either is nil, can use, so return | ||
return if @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 it IS smaller than the parent? Small edge-case the test missed here.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?