Skip to content

Fire - Jing#20

Open
Jing-321 wants to merge 3 commits intoAda-C14:masterfrom
Jing-321:master
Open

Fire - Jing#20
Jing-321 wants to merge 3 commits intoAda-C14:masterfrom
Jing-321:master

Conversation

@Jing-321
Copy link

@Jing-321 Jing-321 commented May 6, 2021

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree?
Could you build a heap with linked nodes?
Why is adding a node to a heap an O(log n) operation?
Were the heap_up & heap_down methods useful? Why?

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.

It passes all the tests, but it doesn't do what it's supposed to do. Take a look at my comments and let me know what questions you have.

There is some good code here, but a lot is just... rough, maybe make a 2nd stab at this.

def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
end No newline at end of file
def heapsort(list)

Choose a reason for hiding this comment

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

⚠️ , Space & time complexity?

Also you add things into a heap, but in the internal structure the items are not sorted. They're maintained in heap order with the parent smaller than it's children. You have to REMOVE elements from the heap 1-by-1 to be guaranteed to get it into order.

Comment on lines +18 to 20
# 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.

👍

lib/min_heap.rb Outdated
Comment on lines 21 to 22
if @store.empty?
@store << HeapNode.new(key, value)

Choose a reason for hiding this comment

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

Do you need this if block?

lib/min_heap.rb Outdated
# Space Complexity: O(1)
def remove()
raise NotImplementedError, "Method not implemented yet..."
min = @store.min_by {|node| node.key}

Choose a reason for hiding this comment

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

This is a linear search, you're totally losing the advantage of a Heap here!

Comment on lines +62 to 64
# Time complexity: O(0)
# Space complexity: O(0)
def empty?

Choose a reason for hiding this comment

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

👍

Comment on lines +73 to 75
# Time complexity: O(logn)
# Space complexity: O(0)
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 83 to 86
# 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.

This isn't working because you've got to:

  1. Check the both left and right child
    1. And swap with the smaller of the two, if needed
  2. Consider the case where there is a left child and not a right child

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!

Comment on lines +83 to +84
swap(index, index * 2 + 1)
swap(index, index * 2 + 2) if @store[index * 2 + 2] != nil && @store[index].key > @store[index * 2 + 2].key

Choose a reason for hiding this comment

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

Fixed!

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