Skip to content

Conversation

@kdow
Copy link

@kdow kdow commented Sep 29, 2019

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 will have elements in order, left to right. Heaps are binary trees; they only care that the parent node is greater than its child nodes (max heap) or less than its child nodes (min heap) - nodes at the same level do not have to be in order.
Could you build a heap with linked nodes? Yes
Why is adding a node to a heap an O(log n) operation? Since they are binary trees, the worst case of adding a node would be O(log n). This is because heaps are always assumed to be balanced, so when an element added at the bottom, the number of nodes is halved each time it heaps up.
Were the heap_up & heap_down methods useful? Why? Yes, they help with the add and remove methods.

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. You hit the learning goals here. Check out my comments and let me know if you have any questions.

raise NotImplementedError, "Method not implemented yet..."
end
# Time Complexity: O(nlog n)
# 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 you are building a heap here, the space complexity is O(n)

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log n)
# 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_up is recursive, you have to account for space used on the call stack. So this is O(log n) space complexity.

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