Skip to content

Conversation

@hn4ever
Copy link

@hn4ever hn4ever commented May 20, 2021

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heap is a binary tree but it's not a binary search tree as it doesn't have the rule of 'left must be smaller and right must be greater'.
Could you build a heap with linked nodes? yes but it's easier to do it with an array.
Why is adding a node to a heap an O(log n) operation? Being a binary tree, adding a node will take half of the time (as the nodes are balanced and ordered, so it's easy to find where the node should be placed).
Were the heap_up & heap_down methods useful? Why? these methods are helpful for adding and removing nodes in 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.

Great work Noor, you hit the learning goals here. Well done.

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(log 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.

👍

lib/min_heap.rb Outdated
Comment on lines 26 to 28
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def remove()

Choose a reason for hiding this comment

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

👍

Comment on lines +56 to 58
# 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.

👍

lib/min_heap.rb Outdated
Comment on lines 67 to 69
# Time complexity: ?
# Space complexity: ?
def heap_up(index)

Choose a reason for hiding this comment

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

👍 Time & space?

# 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.

👍 Nicely done!

@hn4ever
Copy link
Author

hn4ever commented Jul 4, 2021

Thank you Chris

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