Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Leaves - Dominique #23

wants to merge 2 commits into from

Conversation

dtaylor73
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heap guarantees that elements on higher levels are greater or smaller than elements on lower levels, whereas BST guarantees order from left to right. BST is good to find sorted elements, Heaps are good for finding min/max.
Could you build a heap with linked nodes? Yes, but it is more difficult
Why is adding a node to a heap an O(log n) operation? because in the worst case, you may have to traverse the height of the tree to the first treenode in order to satisfy the heap rule
Were the heap_up & heap_down methods useful? Why? yes it made since to separate that logic since its whole purpose is to traverse the tree and swap nodes

Copy link

@CheezItMan CheezItMan left a 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.

Comment on lines +12 to +22
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really clever.

Comment on lines +4 to +7
# O(n)
# Space Complexity: O(1)
def heap_sort(array)

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!

Comment on lines +17 to 20
# 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +33 to 36
# 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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +63 to 66
# 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?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +67 to +71
if @store[0]
return
else
return false
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if @store[0]
return
else
return false
end
return @store.empty?

Comment on lines 79 to +81
# Time complexity: ?
# Space complexity: ?
def heap_up(index)

def heap_up(current_index)

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

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +129 to +139
# 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

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.

Suggested change
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants