-
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 - Dominique #23
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.
Really nice work. You hit all the major learning goals. Very well done with the in-place heapsort. Take a look at my comments and let me know if you have any questions.
while i > 0 do | ||
heapify(root_array, i, size) | ||
i -= 1 | ||
end | ||
|
||
while size > 1 do | ||
root_array[1], root_array[size] = root_array[size], root_array[1] | ||
size -= 1 | ||
heapify(root_array, 1, size) | ||
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 really clever.
# O(n) | ||
# Space Complexity: O(1) | ||
def heap_sort(array) |
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 great implementation of the in-place heapsort. Nice!
# Time Complexity: (logn) because in the worst case an element inserted at the bottom has to be swapped at | ||
# every level from bottom to top up to the root node to maintain the heap property. So essentially you are only traversing half the number of the heap. | ||
# Space Complexity: O(1) because there will always be one space of memory allocated for a new heap node | ||
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: (logn) because after the swapping of the root node and the last node, the last node may have to traverse the tree height (heap down) | ||
# to satisfy the heap | ||
# Space Complexity: O(1) because only one space in memory will be created as the algorithm scales. | ||
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) because the algorithm is only checking to see if there are elements in the array. This | ||
# will stay as the problem scales | ||
# Space complexity: O(1) because no new memory will be created as the algorithm scales | ||
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[0] | ||
return | ||
else | ||
return false | ||
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.
if @store[0] | |
return | |
else | |
return false | |
end | |
return @store.empty? |
# Time complexity: ? | ||
# Space complexity: ? | ||
def heap_up(index) | ||
|
||
def heap_up(current_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.
👍 , but no estimates in time/space complexity?
|
||
while @store[left_child_index] || @store[right_child_index] | ||
# having trouble figuring out when to end the while loop | ||
if @store[left_child_index].key < @store[right_child_index].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_index]
is nil
?
end | ||
|
||
# This helper method takes an index and | ||
# moves it up the heap if it's smaller | ||
# than it's parent node. | ||
def heap_down(index) | ||
raise NotImplementedError, "Method not implemented yet..." | ||
def heap_down(current_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.
👍
# heap.add(3, "Pasta") | ||
# print heap.add(2, "Soup") | ||
heap.add(3, "Pasta") | ||
heap.add(6, "Soup") | ||
heap.add(1, "Pizza") | ||
heap.add(0, "Donuts") | ||
heap.add(16, "Cookies") | ||
heap.add(57, "Cake") | ||
|
||
print heap |
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 can be removed prior to submission.
heap = MinHeap.new | |
# heap.add(3, "Pasta") | |
# print heap.add(2, "Soup") | |
heap.add(3, "Pasta") | |
heap.add(6, "Soup") | |
heap.add(1, "Pizza") | |
heap.add(0, "Donuts") | |
heap.add(16, "Cookies") | |
heap.add(57, "Cake") | |
print heap |
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?