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 - Hallie Johnson #17

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

Leaves - Hallie Johnson #17

wants to merge 2 commits into from

Conversation

idhallie
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A binary search tree has to follow the pattern where the left node is smaller than the parent and the right node is bigger than the parent. This is not the case with a heap. Also, a heap stays in a complete formation, whereas a binary tree can be askew.
Could you build a heap with linked nodes? Sure, but it works with an array, and arrays are easy, so that's what I would prefer.
Why is adding a node to a heap an O(log n) operation? It's O(log n) because you add the node to the end of the array and then you compare to its parent until it is in the correct formation. This comparison to the parent share of all nodes equates to O(log n).
Were the heap_up & heap_down methods useful? Why? Yes. Heap up was useful to add a node and put it in its rightful place and heap_down was useful when removing a node where the last item in the tree becomes the root and needs to be pushed down into its new spot.

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.

Nice work, you hit the major learning goals here. Do remember that when you do recursion, you incur space complexity due to the system stack!

Comment on lines +4 to +8
# then again to remove the sorted elements and output them as a list O(2n), drop the constant.
# Space Complexity: O(1); I think this is constant, because the heap is created as the array is
# reduced so there is not a growth in memory space (except that a heap may take more space?).
def heapsort(list)

Choose a reason for hiding this comment

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

Good insight on space complexity, but the array doesn't actually shrink when you do .pop just the amount of spaces used inside it. Otherwise this works.

Comment on lines +17 to 19
# Time Complexity: O(log n)
# Space Complexity: O(1)
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 +29 to 32
# Time Complexity: O(log n)
# Space Complexity: O(1) It only stores the result variable, which is not altered by
# the size of the input.
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 +60 to 62
# Time complexity: O(1) - arrays keep track of their length so that this can be contant time retrieval.
# Space complexity: O(1)
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 +71 to +73
# Time complexity: O(log n); it compares with parent nodes, not all nodes.
# Space complexity: O(1); swaps are happening in-place.
def heap_up(index)

Choose a reason for hiding this comment

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

Since this is a recursive method the space complexity is O(log n) because of the system stack!

# This helper method takes an index and
# moves it up the heap if it's smaller
# than it's parent node.
# ===> Aren't we taking the new root and comparing it to its child and moving it DOWN?
def heap_down(index)

Choose a reason for hiding this comment

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

👍 Well done!

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