-
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 - Yasmin #14
base: master
Are you sure you want to change the base?
Leaves - Yasmin #14
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 heap_down isn't implemented and that's the hardest method here. The rest work as advertised with the exception of heap_sort (see my note on that). See if you can implement heap_down
as it's a good exercise. If you want me to take another look just ping me on Slack.
heap.add(list.pop) | ||
end | ||
sorted_list = [] | ||
list.length.times 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.
Remember at this stage the list is empty!
list.length.times do | |
until heap.empty? |
# Time Complexity: o(n log(n)) | ||
# Space Complexity: o(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.
One showstopper of a bug preventing this from working. See my suggestion below.
# Time Complexity: o(log n) | ||
# 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) | ||
# 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) | ||
# Space complexity: o(log n) | ||
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.
👍
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.
You have heap_down
mostly right, I do see one bug there.
right_child = (index * 2) + 2 | ||
return if @store[left_child].nil? | ||
|
||
if @store[left_child].key < @store[right_child].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.
What if @store[right_child]
is nil
?
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?