Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Janice H. #20

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Janice H. #20

wants to merge 6 commits into from

Conversation

jaitch
Copy link

@jaitch jaitch commented Mar 17, 2020

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? It is full; the root is the max/min, not middle
Could you build a heap with linked nodes? yes
Why is adding a node to a heap an O(log n) operation? Because you have to traverse the height of the heap
Were the heap_up & heap_down methods useful? Why? Yes-- because you have to heap up when you add a node, and heap down when you remove a node.

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.

Some serious issues here. You tried to do the in-place Heapsort and it's not working. See my inline notes there. You also have some errors in time/space complexity and your heap_down method is not working in a heap-like manner. See my notes.

Comment on lines +4 to +10
# Time Complexity: O(2nlogn)?
# Space Complexity: O(1)
# GAH I cannot get those last two numbers
# sorted properly for some reason and it's
# time to turn this in. I know they are in
# the correct order at the end but then flip
def heapsort(list)

Choose a reason for hiding this comment

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

It's neat that you were trying to do the in-place heapsort. I'd suggest doing it the simple way first, using the minheap you created in this assignment and adding all the elements of the list to the heap and then extracting them one by one and shoveling them into the resulting list. It's O(n) space complexity but it works. Then try for this one if/when you have time.

return list
end

def heap_up(list, index)

Choose a reason for hiding this comment

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

To do this in-place, I would suggest making a Max_heap instead of a min-heap and then as you remove a node adding it to the rear of the array working your way from the last node until index 0. That's a lot easier than using a min-heap like you are doing.

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

This works, but why would adding one value to the heap be O(n log n) time complexity and why would it take O(n) space complexity?

I suggest it's O(log n) for space (the recursive stack) and O(log n) for time.

lib/min_heap.rb Outdated Show resolved Hide resolved
Comment on lines +27 to 29
# Time Complexity: O(nlogn)
# 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.

See my notes above about space/time complexity.

def heap_up(index)

# for right children
if index % 2 == 0

Choose a reason for hiding this comment

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

These if statements could be condensed a bit.

Comment on lines 98 to 106
def heap_down(index)
raise NotImplementedError, "Method not implemented yet..."
if !@store[index+1].nil? && @store[index].key > @store[index+1].key
swap(index, index+1)
heap_down(index+1)
elsif !@store[index+2].nil? && @store[index].key > @store[index+2].key
swap(index, index+2)
heap_down(index+2)
end
end

Choose a reason for hiding this comment

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

This method is NOT doing head_down. Instead it's comparing a node to the nodes next to it. Remember the formula to find a node's left child is:

left_child_index = index * 2 + 1
and the right child
right_child_index = index * 2 + 2

Because you're not doing the correct heap_down method this method is just using a sorted array which is O(n)

Comment on lines +70 to 72
# Time complexity: O(nlogn)
# Space complexity: O(1)
def heap_up(index)

Choose a reason for hiding this comment

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

How is the time complexity to heap up 1 element O(n log n), you don't, or shouldn't look at every node in the list for this method.

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.

3 participants