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

Cedar - Kristin L #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Cedar - Kristin L #30

wants to merge 1 commit into from

Conversation

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

💫 Overall, a nice implementation, Kristin. However, the implementation of heap_down appears to have come from the solution. As such, I can't evaluate that implementation. Also, the comprehension questions are missing, so I'm giving an overall evaluation of yellow.

Please let me know if you'd like to discuss this further, or feel free to submit a new implementation.

🟡

"""
pass
if value == None:

Choose a reason for hiding this comment

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

👀 Prefer using is to compare to None

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 +35 to +36
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. 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 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!

Comment on lines +58 to +59
Time complexity: O(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.

👀 Getting the length of a list is a constant time operation, so the overall time complexity will be constant time as well.

Comment on lines +61 to +62
if len(self.store) == 0:
return True

Choose a reason for hiding this comment

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

👀 This will return True when the list is empty, but what would happens if the list were not empty?

Python would fall off the end of the function, with the default None return value. But since we're returning a boolean from one path, we should be consistent everywhere.

        if len(self.store) == 0:
            return True
        else:
            return False

which simplifies to

        return len(self.store) == 0

We could also remember that empty lists are falsy, and write this as

        return not self.store

Comment on lines +75 to +78
check my value vs parent's value
if parent is larger
swap with parent
recursively call heap_up from new location

Choose a reason for hiding this comment

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

✨ Nice explanation of the approach

Comment on lines +72 to +73
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.

👀 This function is the main source of time and space complexity for add, so refer back to that note.


def heap_down(self, index):
""" This helper method takes an index and
moves the corresponding element down the heap if it's
larger than either of its children and continues until
the heap property is reestablished.
"""
pass
left_child = index * 2 + 1

Choose a reason for hiding this comment

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

👀 This is identical to the instructor solution and has no additions that would indicate to me that time was spent time understanding this approach.

Comment on lines +6 to +7
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 +15 to +22
while not heap.empty():
# puts sorted nums back in list using index
# remove is O(log n)
list[index] = heap.remove()
index += 1

return list
Copy link

@anselrognlie anselrognlie Jul 21, 2022

Choose a reason for hiding this comment

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

Note the since this isn't a fully in-place solution (the MinHeap has a O(n) internal store), we don't necessarily need to modify the passed in list. The tests are written to check the return value, so we could unpack the heap into a new result list to avoid mutating the input.

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

    return result

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