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 - Yasmin #14

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

Leaves - Yasmin #14

wants to merge 5 commits into from

Conversation

YasminM11
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? In binary search tree, for every node except the leaf node, the left child has a less key value and right child has a greater key value. In heap, for every node other than the root, the key value of the parent node is greater or equal to the key value of the child node.
Could you build a heap with linked nodes? Yes
Why is adding a node to a heap an O(log n) operation? Because you are adding into the end, then Compare the added element with its parent, and swapping them if they are out of order.
Were the heap_up & heap_down methods useful? Why? Yes, Because they are helping in the adding and removing methods.

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.

Well heap_down isn't implemented and that's the hardest method here. The rest work as advertised with the exception of heap_sort (see my note on that). See if you can implement heap_down as it's a good exercise. If you want me to take another look just ping me on Slack.

heap.add(list.pop)
end
sorted_list = []
list.length.times do

Choose a reason for hiding this comment

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

Remember at this stage the list is empty!

Suggested change
list.length.times do
until heap.empty?

Comment on lines +4 to +6
# Time Complexity: o(n log(n))
# Space Complexity: o(n)
def heapsort(list)

Choose a reason for hiding this comment

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

One showstopper of a bug preventing this from working. See my suggestion below.

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

👍

Comment on lines +28 to 30
# Time Complexity: o(log n)
# Space Complexity: o(log n)
def remove()

Choose a reason for hiding this comment

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

👍

Comment on lines +58 to 60
# 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.

👍

Comment on lines +70 to 72
# Time complexity: o(log n)
# Space complexity: o(log n)
def heap_up(index)

Choose a reason for hiding this comment

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

👍

lib/min_heap.rb Show resolved Hide resolved
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.

You have heap_down mostly right, I do see one bug there.

right_child = (index * 2) + 2
return if @store[left_child].nil?

if @store[left_child].key < @store[right_child].key

Choose a reason for hiding this comment

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

What if @store[right_child] is nil?

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