-
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 - Emily Ball #13
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.
Very well done. You hit all the learning goals here. Awesome work. Do take a look at my minor comment and let me know if you have questions.
# Time Complexity: O(n log n) | ||
# Space Complexity: O(log n) - since using recurisve heap down | ||
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 works but since you're building a heap with n nodes, the space complexity is O(n).
@@ -0,0 +1,91 @@ | |||
require_relative "heap_node" | |||
|
|||
class MaxHeap |
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.
Interesting that you built a max heap as well.
# 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.
👍
# This method removes and returns an element from the heap maintaining the heap structure | ||
# 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.
👍
if @store[left_child] && @store[left_child].key < @store[index].key | ||
swap(index, left_child) | ||
heap_down_helper(left_child) | ||
end | ||
|
||
if @store[right_child] && @store[right_child].key < @store[index].key | ||
swap(index, right_child) | ||
heap_down_helper(right_child) | ||
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.
Doing it this way will work, but might lead to extra swaps if the right child is initially less than the left.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?