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

Water - Kareha #17

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

Water - Kareha #17

wants to merge 5 commits into from

Conversation

agesak
Copy link

@agesak agesak commented May 2, 2021

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heaps have to fulfill heap property and don't have rule that what's to the left of a node has to be less than node, and what's to the right has to be greater.
Could you build a heap with linked nodes? Yes but we use arrays because of constant look up time for nodes
Why is adding a node to a heap an O(log n) operation? Because to add a node, you have to check at every level that the parent is larger than its children (max heap) or smaller than its children (min heap), which requires a traversal equal to the depth of the tree, which is O(logn)
Were the heap_up & heap_down methods useful? Why? Yes, they were helpful in adding/removing nodes and doing heapsort.

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.

Great work Kareha! You hit the learning goals here. Well done! Really nicely written code here.

Comment on lines +7 to +12
# for max heap
# this is helpful https://www.youtube.com/watch?v=2DmK_H7IdTo
# this is also helpful: https://www.programiz.com/dsa/heap-sort
# time: o(logn)
# space: o(logn) - because of call stack?
def heap_down(list, i, length)

Choose a reason for hiding this comment

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

👍 Nice work for in-place heapsort!

Comment on lines +17 to 19
# Time Complexity: O(logn)
# Space Complexity: O(logn)
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 +27 to 29
# Time Complexity: O(logn)
# Space Complexity: O(logn)
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 +53 to 55
# Time complexity: O(1)
# 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 +64 to 66
# Time complexity: O(logn)
# Space complexity: O(logn)
def heap_up(index)

Choose a reason for hiding this comment

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

👍 nice!

Comment on lines 79 to 82
# 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)

Choose a reason for hiding this comment

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

👍 , Nicely 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