Skip to content

Conversation

fan-gela
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree?
Could you build a heap with linked nodes?
Why is adding a node to a heap an O(log n) operation?
Were the heap_up & heap_down methods useful? Why?

@anselrognlie anselrognlie self-requested a review July 18, 2022 15:38
Copy link

@anselrognlie anselrognlie left a comment

Choose a reason for hiding this comment

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

✨💫 Nice job on your implementation, Angela. I left some comments below.

You left the comprehension questions blank, and had a test problem, so I'm grading this as a yellow for now. But please feel free to resubmit if you'd like me to take another look.

🟡

Comment on lines +22 to +23
Time Complexity: O(log n)
Space Complexity: O(log n)

Choose a reason for hiding this comment

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

✨ Great! In the worst case, the new value we're inserting is the new root of the heap, meaning it would need to move up the full height of the heap (which is log n levels deep) leading to O(log n) time complexity. Your implementation of the heap_up helper is recursive, meaning that for each recursive call (up to log n of them) there is stack space being consumed. So the space complexity is also be O(log n). If heap_up were implemented iteratively, this could be reduced to O(1) space complexity.

Comment on lines +35 to +36
Time Complexity: O(log n)
Space Complexity: O(log n)

Choose a reason for hiding this comment

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

✨ Great! In the worst case, the value that got swapped to the top will need to move all the way back down to a leaf, meaning it would need to move down the full height of the heap (which is log n levels deep) leading to O(log n) time complexity. Your implementation of the heap_down helper is recursive, meaning that for each recursive call (up to log n of them) there is stack space being consumed. So the space complexity is also be O(log n). If heap_down were implemented iteratively, this could be reduced to O(1) space complexity.

removed_element = self.store.pop()
# Reheapify the heap
self.heap_down(0)
return removed_element

Choose a reason for hiding this comment

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

👀 We shouldn't return the internal node that we made for the heap. Instead, just return the value here. Returning the entire node was part of what was causing a test failure.

        return removed_element.value

"""
pass
return len(self.store) == 0

Choose a reason for hiding this comment

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

Remember that an empty list is falsy

        return not self.store

Comment on lines +73 to +74
Time complexity: O(log n)
Space complexity: O(log n)

Choose a reason for hiding this comment

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

✨ Yes, this function is where the complexity in add comes from.

pass
if index == 0:
return
if self.store[index].key < self.store[(index - 1) // 2].key:

Choose a reason for hiding this comment

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

Notice that you repeated the (index - 1) // 2 calculation a few times. Consider performing that once and storing it in a local variable.

if index * 2 + 1 < len(self.store):
if index * 2 + 2 < len(self.store):
if self.store[index * 2 + 1].key > self.store[index * 2 + 2].key:
if self.store[index * 2 + 1].key < self.store[index].key:

Choose a reason for hiding this comment

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

👀 The one we want to swap is the smaller of the two children. This will swap the larger of the two children. This is the other part causing the test failure for the remove ordering.

Comment on lines +105 to +106
self.swap(index, index * 2 + 1)
self.heap_down(index * 2 + 1)

Choose a reason for hiding this comment

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

👀 Notice that the branches that do anything all culminate in swapping with an index and the making the recursive heap call on that other index.

Consider restructuring so that we calculate what the other index is, and then we could write the swap and heap calls just once.

Comment on lines +5 to +6
Time Complexity: O(n log n)
Space Complexity: O(n)

Choose a reason for hiding this comment

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

✨ Great. Since sorting using a heap reduces down to building up a heap of n items one-by-one (each taking O(log n)), then pulling them back out again (again taking O(log n) for each of n items), we end up with a time complexity of O(2n log n) → O(n log n). While for the space, we do need to worry about the O(log n) space consume during each add and remove, but they aren't cumulative (each is consumed only during the call to add or remove). However, the internal store for the MinHeap does grow with the size of the input list. So the maximum space would be O(n + log n) → O(n), since n is a larger term than log n.

Note that a fully in-place solution (O(1) space complexity) would require both avoiding the recursive calls, as well as working directly with the originally provided list (no internal store).

Comment on lines +16 to +18
while len(heap.store) > 0:
sorted_list.append(heap.remove().value)

Choose a reason for hiding this comment

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

We should treat the internal store as an implementation detail and not check its length directly. Instead, we can make use of the empty helper method. Also, note that the value should be returned by the remove function, not the whole node.

    result = []
    while not heap.empty():
        result.append(heap.remove())

    return result

sorted_list.append(heap.remove().value)

return sorted(sorted_list)

Choose a reason for hiding this comment

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

👀 You already sorted the list by adding and removing with the heap. No need for the sorted call here.

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