Skip to content

Conversation

@JansenMartin
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heaps guarantee elements on higher levels are either greater or lower than elements at lower levels. Binary Search Trees guarantee order from left to right. Heaps are better for finding min/max values, while Binary Search Trees are better for sorting elements.
Could you build a heap with linked nodes? Yes, but it's much easier to implement a heap with an array. Why? Because indexes can be used to determine the parent or children of a given element.
Why is adding a node to a heap an O(log n) operation? Because the heap must satisfy the "heap-order" property. That means, when adding a node, you must go through the heap to ensure parents are either greater (max-heap) or less than (min-heap) their children. n stands for the number of levels within the tree.
Were the heap_up & heap_down methods useful? Why? Yes. They preserved the heap-order of the min-heap when adding/removing nodes to the heap.

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, except for the space complexity you hit the heap class well.

You don't have heapsort here. So... that's missing.

lib/min_heap.rb Outdated
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(log n), where n is the number of levels in the heap
# Space Complexity: O(1), because there are no extra variables that change with input size

Choose a reason for hiding this comment

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

Since you have a recursive heap_up method, you have to consider the space complexity of that method. So... this is actually O(log n).

The same applies for all your recursive methods.

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