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

Spruce: Kaitlyn #33

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

Spruce: Kaitlyn #33

wants to merge 5 commits into from

Conversation

kaitlyngore
Copy link

@kaitlyngore kaitlyngore commented Jul 19, 2022

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap maintains its balance and is a complete binary tree.
Could you build a heap with linked nodes? You could but it would increase the time complexity
Why is adding a node to a heap an O(log n) operation? Because a heap is balanced, there will be at most 1 swap per level of the heap and there are log n levels to the heap.
Were the heap_up & heap_down methods useful? Why? Yes because they put the nodes back into the order and keep the heap balanced.

@anselrognlie anselrognlie self-requested a review July 19, 2022 04:03
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.

Thanks for resubmitting. heap_down looks good and passes all the tests. I'll revise this to a green project.

🟢

Good job on the comprehension question, Kaitlyn. A key point to remember is that heaps are semi-ordered, so we can perform fewer checks when inserting. It's not a true binary search tree since it doesn't have the left/right guarantees that we have from a BST (we only know that a node is guaranteed to be bigger or smaller, depending on the style of heap, than its children). Because of the weaker ordering, maintaining the heap property in a balanced fashion is less costly than for a BST.

However, in your implementation, the remove (heap_down) is incomplete. This constitutes a significant portion of functionality missing. As such I need to evaluate this as a red submission.

Please resubmit with an implementation for heap_down.

🔴

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

Choose a reason for hiding this comment

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

👀 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 would also be O(log n). If heap_up were implemented iteratively, this could be reduced to O(1) space complexity.

Comment on lines 34 to 35
Time Complexity: O(log n)
Space Complexity: O(1)

Choose a reason for hiding this comment

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

👀 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.

You're currently missing a full implementation of the heap_down helper, but if you handled it recursively as you did for heap_up, the space complexity would also be O(log n). If heap_down were implemented iteratively, this could be reduced to O(1) space complexity.

"""
pass
if self.empty():

Choose a reason for hiding this comment

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

✨ Nice use of your own helper method!


self.heap_down(0)

return removed_value

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_value.value



def __str__(self):
""" This method lets you print the heap, when you're testing your app.
"""
if len(self.store) == 0:
if self.empty():

Choose a reason for hiding this comment

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

✨ Nice improvement to the provided str method.

Comment on lines +93 to +94
if self.empty():
return

Choose a reason for hiding this comment

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

👀 This is incomplete. We would need essentially the "reverse" logic of what was done for heap_up.

Comment on lines 6 to 7
Time Complexity: O(n * 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.

👀 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). You have an extra n term in your time complexity.

For the space, we do need to worry about the O(log n) space consume during each add and remove (if completed), 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 +9 to +10
return list

Choose a reason for hiding this comment

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

This feels like a workaround due to not completing the heap_down method.


return_list = []
while heap.empty() != True:

Choose a reason for hiding this comment

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

👀 Prefer not explicitly comparing to a boolean.

    while not heap.empty():

heap.add(item)

return_list = []

Choose a reason for hiding this comment

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

Your approach looks good and should work if you complete the heap_down method.

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.

Thanks for resubmitting. heap_down looks good and passes all the tests. I'll revise this to a green project.

🟢


# I know this isn't that helpful, but it helped me make more sense of what was happening
# in the heap_down method
def is_left_index_smaller(self, index_1, index_2):

Choose a reason for hiding this comment

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

✨ Adding helpers to wrap your head around some noisy logic is a great strategy. It can be much nicer to read off the name of a function that literally says what it's checking rather than a complicated comparison expression and then keep reminding yourself what the intent of that comparison was.

Comment on lines +114 to +115
self.swap(index, left_child_index)
self.heap_down(left_child_index)

Choose a reason for hiding this comment

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

👀 Notice that each time we decide which child to swap, we swap the child, then keep heaping from that newly swapped location (following the larger value down). We could reduce the repetition somewhat by figuring out which index (if any) will be swapped, then at the end, actually do the swap and heap follow.

@@ -1,3 +1,6 @@
from cgitb import small

Choose a reason for hiding this comment

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

👀 Stray import (likely from VSCode autocomplete)

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