Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@aphunk
Copy link

@aphunk aphunk commented Oct 1, 2019

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Binary search trees guarantee an order based on the values of the nodes while heaps only guarantee that the parent is larger (max-heap) or that the child is larger (min-heap).
Could you build a heap with linked nodes? Yes
Why is adding a node to a heap an O(log n) operation? The maximum number of operations is determined by the height of the tree, and not the number of nodes.
Were the heap_up & heap_down methods useful? Why? Yes, they were very useful in the add and remove methods--keeping heap up and down private made the add/remove methods more readable.

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.

Definitely some issues here. Take a look at my comments and let me know if you have questions.

end

sorted = []
heap.each do |elem|

Choose a reason for hiding this comment

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

Heaps don't have a .each method.

Suggested change
heap.each do |elem|
while !heap.empty?


sorted = []
heap.each do |elem|
sorted << heap.remove(elem)

Choose a reason for hiding this comment

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

remove doesn't take an argument.

Comment on lines +17 to +18
# Time Complexity: O(log n), where n is the number of nodes in heap
# Space Complexity: O(log n), where n is the number of nodes in heap

Choose a reason for hiding this comment

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

👍

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log n), where n is the number of nodes in heap
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

Since heap_down is recursive, you will be using the call stack, so the space complexity is O(log n)

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