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

Leaves_Ga-Young #32

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

Leaves_Ga-Young #32

wants to merge 4 commits into from

Conversation

gyjin
Copy link

@gyjin gyjin commented Mar 29, 2020

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap sorts elements from one level to another, a parent is always less than or more than its children (depending on min or max heap). A binary search tree sorts the children of the parent.
Could you build a heap with linked nodes? One could but an array is preferred.
Why is adding a node to a heap an O(log n) operation? The node must be compared to its parent node until it is seated in the correct position. At its worst, that node will be compared all the way up to the root node.
Were the heap_up & heap_down methods useful? Why? Yes, it isolated the logic of comparing nodes to determine if a swap was necessary, and could be called recursively.

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.

I apologize for taking so long to get to this. I did find the small bug in your heap_down code which was getting caught in heapsort. Take a look and let me know if I need to clarify.

lib/heap_sort.rb Outdated
end

# I'm having trouble figuring out why the "can sort a 5-element array" test will not pass for this. I have created a new "can sort a different 5-element array" test and that passes. I am thinking it has to do with the order of numbers - some exact pattern of numbers is causing the heap to not sort properly, but all my tests from the min_heap are passing. Could I get some help?

Choose a reason for hiding this comment

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

I think it's a bug with your heap.

lib/min_heap.rb Outdated
Comment on lines 17 to 19
# Time Complexity: O(log n) where n is number of nodes
# Space Complexity: O(1)
def add(key, value)

Choose a reason for hiding this comment

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

Since heap_up is a recursive method and it will make log n recursive calls. This method has O(log n) space complexity.

lib/min_heap.rb Outdated
Comment on lines 27 to 29
# Time Complexity: O(log n) where n is number of nodes
# 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.

  1. Similar issue to above on space complexity.
  2. What if the heap is empty? maybe a check is appropriate.

Comment on lines +56 to 58
# Time complexity: O(1)
# Space complexity: O(1)
def empty?

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 75 to 77
else
return
end

Choose a reason for hiding this comment

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

This is a bit redundant.

Suggested change
else
return
end

lib/min_heap.rb Outdated
left_child = (index * 2 + 1)
right_child = (index * 2 + 2)

until @store[left_child] == nil || @store[right_child] == nil

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 is not nil but the right child is, and yet if the parent is still bigger than the left child.

Comment on lines -65 to 85
# moves it up the heap if it's smaller
# moves it down 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.

Ok this one has a small bug that heapsort caught. Take a look at it, you can get into the circumstance that the left child is not nil and smaller than the parent and the right child is nil.

@gyjin
Copy link
Author

gyjin commented Apr 24, 2020

Hi Chris, I think I fixed that bug. Could you take another look?

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