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

Time - Ross #13

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

Time - Ross #13

wants to merge 2 commits into from

Conversation

syrosalynyu
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Binary Search Tree guarantees the left child will always be smaller than the right child. On the other hand, Heap can only guarantee the children will always be smaller than the parent (for MaxHeap).
Could you build a heap with linked nodes? Yes, you can. But implemented using the array is easier.
Why is adding a node to a heap an O(log n) operation? B/c once you added the new node to the end of the heap, it will only bubble up to where it belongs on one side of the tree. So it's a logn operation.
Were the heap_up & heap_down methods useful? Why? heap_up is useful when adding a new node, and heap-down is useful to reorganize the heap when removing the root node.

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.

Well done Ross, you hit the learning goals here. Nice work!

Comment on lines 4 to +6
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
def heapsort(list)

Choose a reason for hiding this comment

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

👍 But time and space complexity?

Comment on lines +54 to 56
# 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 +65 to 67
# Time complexity: o(log n)
# Space complexity: o(1)
def heap_up(index)

Choose a reason for hiding this comment

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

Due to the recursion, the space complexity is O(log n)

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.

The space complexity is O(log n) due to recursion.


# find the min_child
min_i = left_i
if right_i < @store.length

Choose a reason for hiding this comment

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

Nice good catch checking to see if the right index is past the end of the array.

Comment on lines 80 to 83
# 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.

👍

Comment on lines +29 to 31
# Time Complexity: o(log n)
# Space Complexity: o(1)
def remove()

Choose a reason for hiding this comment

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

👍

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