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

Sara - Branches #28

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

Sara - Branches #28

wants to merge 2 commits into from

Conversation

sarashahbaig
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 node's children are always smaller than a node, and a binary tree node's children have one smaller and one larger node as children.
Could you build a heap with linked nodes? Yes.
Why is adding a node to a heap an O(log n) operation? because we are looking at each level of the heap.
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, you hit the main learning goals here. There are some issues with time/space complexity and with heap_down, but otherwise well done. Take a look at my comments and let me know what questions you have.

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

Choose a reason for hiding this comment

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

👍

Comment on lines +17 to 19
# Time Complexity: o(logn)
# Space Complexity: o(logn)
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(logn)
# Space Complexity: o(1) i think
def remove()

Choose a reason for hiding this comment

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

Since heap_down is recursive this is O(log n) for time complexity just like add

Comment on lines +53 to 55
# 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 +65 to 67
# Time complexity: o(logn)
# 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.

See add for notes on time complexity, otherwise 👍

return
end

if @store[left_index].key < @store[right_index].key

Choose a reason for hiding this comment

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

You also need to compare the child to the current element.

Suggested change
if @store[left_index].key < @store[right_index].key
if @store[left_index].key < @store[right_index].key && @store[left_index].key < @store[index].key

if @store[left_index].key < @store[right_index].key
swap(left_index, index)
heap_down(left_index)
else

Choose a reason for hiding this comment

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

Suggested change
else
elsif @store[right_index].key < @store[index].key

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