Skip to content

Conversation

@nidhiparixitpatel
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? binary search tree maintain a certain order (post, pre, in order) in sorting, heaps are sorted with the the top most root node either being the max or min and children being less than their parents.
Could you build a heap with linked nodes? yes
Why is adding a node to a heap an O(log n) operation? it is only O(log n) because when you add a node, you can use a method like heap up to sort the heap and it only needs to touch half the nodes (all the parent nodes) and compare instead of having to compare to each element in the heap
Were the heap_up & heap_down methods useful? Why? Yes, heap up and heap down maintain the order of 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.

Ok so some issues here. Take a look at my comments on time and space complexity and look at my heap-down method. You need to compare the current index with both of it's children, not just the left-child.

Comment on lines 17 to 18
# Time Complexity: ?
# Space Complexity: ?

Choose a reason for hiding this comment

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

Since you're doing recursion it should be O(log n) for both.

Comment on lines +64 to +67
def find_parent(index)
parent_index = (index - 1)/2
return parent_index
end

Choose a reason for hiding this comment

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

I like this helper method.

if @store[parent_index].key <= @store[index].key || index == 0
return
end
if @store[parent_index].key > @store[index].key

Choose a reason for hiding this comment

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

Since your prior if statement returns you don't need an if statement here.

raise NotImplementedError, "Method not implemented yet..."
puts 'heap down'
puts to_s
child_index = 2*index + 1

Choose a reason for hiding this comment

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

You are only finding the left child here. You are not finding the right child.

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