Skip to content

Conversation

@katemyer
Copy link

@katemyer katemyer commented Oct 5, 2020

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heaps guarantee that the elements on higher levels are greater for max heaps or lower for min heaps. But, the BST guarantees that there is order from left to right, this is useful for searching elements that are sorted.
Could you build a heap with linked nodes? No because heaps are binary trees. You can store the heap into an array since it's easier to manage memory.
Why is adding a node to a heap an O(log n) operation? Because the smallest possible height for a heap with N nodes is o log n.
Were the heap_up & heap_down methods useful? Why? Heap up methods are useful in adding elements to a heap, by comparing the new node to its parent and swapping them if they are out of order, and updating a lcoation. The heap down is useful to remove an element by swapping the last leaf with the root and done until there are more swaps left.

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.

Not bad Kate, one minor note on heal_down, and some notes on space/time complexity due to recursion. Otherwise well done.

Comment on lines +4 to 6
# Time Complexity: ? 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 +28 to 30
# Time Complexity: O(logn)
# Space Complexity: O(1)
def remove()

Choose a reason for hiding this comment

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

👍 However since you are doing recursion the space complexity is O(log n) due to the call stack.

Comment on lines +17 to 19
# Time Complexity: ? O(logn)
# Space Complexity: ? O(1)
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.

👍 However since you are doing recursion the space complexity is O(log n) due to the call stack.

Comment on lines +95 to +97
elsif right_child_index >= @store.length
return
end

Choose a reason for hiding this comment

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

What if the left child index is less than the length and the value at the left child index is less than the current parent!

Suggested change
elsif right_child_index >= @store.length
return
end
elsif right_child_index >= @store.length && @store[left_child_index].key >= @store[index].key
return
end

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