Skip to content

Conversation

@OlgaSe
Copy link

@OlgaSe OlgaSe commented May 6, 2021

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Compare to Binary search tree children follow different rule: they both less or bigger than parent, in the Binary search tree left child should be smaller that parent and parent should be greater than right child.
Could you build a heap with linked nodes? Yes, heap can be implemented with linked nodes, but easier to implement with array.
Why is adding a node to a heap an O(log n) operation? Because log n is the number of levels in the heap and the maximum number of swaps to add a node is equal to the number of levels.
Were the heap_up & heap_down methods useful? Why? Yes, as a helper methods they made the add and remove methods short and easy to read.

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 Olga, I had one comment on heap_down and some on space complexity, but otherwise very nice work.

Comment on lines +4 to +6
# Time Complexity: O(n log n)
# Space Complexity: O(n)
def heapsort(list)

Choose a reason for hiding this comment

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

👍

Comment on lines +17 to 19
# Time Complexity: O(log n)
# Space Complexity: O(n)
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.

👍 Why O(n) for space complexity, you add 1 element with this method.

Comment on lines +26 to 28
# 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.

👍 However for space complexity it's O(log n) because of the call stack.

Comment on lines +51 to 53
# 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 +62 to 64
# 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.

👍 , but your space complexity is off due to recursion

left_child = (index * 2) + 1
right_child = (index * 2) + 2

return if @store[left_child].nil? || @store[right_child].nil?

Choose a reason for hiding this comment

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

What about if the left child is not nil and the right child is?

Could you end up not making a swap you needed to? This is a rare but possible scenario.

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