Skip to content

Conversation

@seaweeddol
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap is always a complete tree, and each parent has a specific order-relationship with its children
Could you build a heap with linked nodes? Yes but using an array is easier
Why is adding a node to a heap an O(log n) operation? Because it will be sorted into the heap, which means checking each level of the heap but not every node - therefore O(logn)
Were the heap_up & heap_down methods useful? Why? Yes, they made it easy to navigate through each level of the heap and swap values.

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 Jessica, you hit the learning goals here. Well done.

Comment on lines +4 to 6
# Time Complexity: O(nlogn) - loops n times based on length of list, and then logn adding or removing
# Space Complexity: O(nlogn)
def heapsort(list)

Choose a reason for hiding this comment

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

👍 However your space complexity is O(n) because you add n elements into the heap.

Comment on lines +17 to +29
def find_parent(index)
return (index - 1) / 2
end

# helper method to find left child
def left_child(index)
return 2 * index + 1
end

# helper method to find right child
def right_child(index)
return 2 * index + 2
end

Choose a reason for hiding this comment

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

Good set of helper methods.

Comment on lines +32 to 34
# Time Complexity: O(logn) depending on the height of the heap
# Space Complexity: O(logn) to hold the recursive stack
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 +41 to +43
# Time Complexity: O(logn) depending on the height of the heap
# Space Complexity: O(logn) to hold the recursive stack
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 +81 to 83
# Time complexity: O(logn)
# Space complexity: O(logn)
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 +105 to +106
if !@store[left]
smallest = right

Choose a reason for hiding this comment

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

Just a note, that if left is not in the array, then right is not either.

Comment on lines 93 to 96
# 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.

👍

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