Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Faiza1987
Copy link

@Faiza1987 Faiza1987 commented Oct 1, 2019

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A bst is sorted (elements < root go to the left and elements > root go to right). A heap is not sorted and just follows the rule that it must be filled from left to right and the smallest element in the tree, if it is a min-heap, must be the root or the largest element must be the root in the case of a max-heap.
Could you build a heap with linked nodes? Yes, if you use an array as the underlying structure.
Why is adding a node to a heap an O(log n) operation? Because the heap property must be satisfied so whenever a node is added to a heap, it sorts the heap until it satisfies the heap property again.
Were the heap_up & heap_down methods useful? Why? Yes, they resorted the heaps to satisfy the heap property for both min and max heaps when a node was added or removed.

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.

Overall not bad, but there are small issues. Take a look at my comments and see if they make sense. Let me know any questions you have.

swap(0, @store.length - 1)
removed_node = @store.pop
heap_down(0)
return removed_node.value

Choose a reason for hiding this comment

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

Getting a NoMethodError here in the "can sort a 5-element array" test.

def heapsort(list)
return list if list.empty? || list.length == 1

minHeap = MinHeap.new()

Choose a reason for hiding this comment

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

Just a note that coding style for minHeap should be min_heap

Comment on lines +55 to +57
if @store.empty?
return
end

Choose a reason for hiding this comment

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

You're returning nil if @store.empty? I would instead return @store.empty?

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