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

Branches - Monick #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Branches - Monick #12

wants to merge 1 commit into from

Conversation

mfunkemomo
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heap will ensure nodes are organized by least to greatest or greatest to least
Could you build a heap with linked nodes? Yes?
Why is adding a node to a heap an O(log n) operation? Because you're adding it to only half the tree
Were the heap_up & heap_down methods useful? Why? Yes

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 pretty well done, some errors on BigO and one issue with heap_down. See my inline comments and let me know what questions you have. You did hit the main learning goals here.

Comment on lines +20 to +31
list.size.times do |node|
last_node = heap.remove()
if sorted_list.empty?
sorted_list.push(last_node)
elsif last_node < sorted_list[-1]
temp = sorted_list[-1]
sorted_list[-1] = last_node
sorted_list.push(temp)
else
sorted_list.push(last_node)
end
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 overly complicated. Remember you always remove the smallest element of a heap.

Suggested change
list.size.times do |node|
last_node = heap.remove()
if sorted_list.empty?
sorted_list.push(last_node)
elsif last_node < sorted_list[-1]
temp = sorted_list[-1]
sorted_list[-1] = last_node
sorted_list.push(temp)
else
sorted_list.push(last_node)
end
end
until heap.empty?
sorted_list << heap.remove()
end

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

👍

Comment on lines +27 to 29
# Time Complexity: O(log n)
# 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.

👍

Comment on lines +56 to 58
# Time complexity: O(n)
# 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.

It works, but how is this O(n) time complexity? Why not O(1)?

Comment on lines +67 to 69
# Time complexity: O(log n)
# 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.

Note, you're doing recursion so this is O(log n) space complexity. You gotta remember the system stack!

right_child = 2*index+2

# if either is nil, can use, so return
return if @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 it IS smaller than the parent? Small edge-case the test missed here.

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