Skip to content

Conversation

@steve-messing
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 are not ordered in relation to their sibling nodes
Could you build a heap with linked nodes? Yes but less time efficient
Why is adding a node to a heap an O(log n) operation?
Were the heap_up & heap_down methods useful? Why? yes

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 Sophie, this hits the learning goals here. Well done.

Comment on lines +4 to 6
# Time Complexity: n log(n)
# Space Complexity: n
def heap_sort(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: log n
# Space Complexity: 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.

👍

Comment on lines +26 to 28
# Time Complexity: log n
# Space Complexity: 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 +52 to 54
# Time complexity: 1
# Space complexity: 1
def empty?

Choose a reason for hiding this comment

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

👍

Suggested change
# Time complexity: 1
# Space complexity: 1
def empty?
# Time complexity: O(1)
# Space complexity: O(1)
def empty?

Comment on lines +63 to 65
# Time complexity: log n
# Space complexity: log n
def heap_up(index)

Choose a reason for hiding this comment

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

👍

Comment on lines +86 to +95
if @store[left].key < @store[index].key
# recursive case - left node
swap(left, index)
heap_down(left)
end
if right < @store.length && @store[right].key < @store[index].key
# recursive case - right node
swap(right, index)
heap_down(right)
end

Choose a reason for hiding this comment

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

This works, but you might engage in extra swaps if the sequential if statements.

Comment on lines +77 to 79
# Time complexity: log n
# Space complexity: log n
def heap_down(index)

Choose a reason for hiding this comment

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

👍

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