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 - Emily Ball #13

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

Branches - Emily Ball #13

wants to merge 3 commits into from

Conversation

eaball35
Copy link

@eaball35 eaball35 commented Mar 15, 2020

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heaps have children that are either all larger or smaller than their parents whereas BSTs have all right children larger than left or vice versa.
Could you build a heap with linked nodes? Yes
Why is adding a node to a heap an O(log n) operation? When adding a node to a heap, you first add it to end of the tree and then heap up. When heaping you, you may traverse the entire height of the tree which is up to log n possible swaps.
Were the heap_up & heap_down methods useful? Why? Yes, they are necessary for reestablishing the heap property after adding or removing elements.

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.

Very well done. You hit all the learning goals here. Awesome work. Do take a look at my minor comment and let me know if you have questions.

Comment on lines +4 to 6
# Time Complexity: O(n log n)
# Space Complexity: O(log n) - since using recurisve heap down
def heap_sort(list)

Choose a reason for hiding this comment

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

This works but since you're building a heap with n nodes, the space complexity is O(n).

@@ -0,0 +1,91 @@
require_relative "heap_node"

class MaxHeap

Choose a reason for hiding this comment

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

Interesting that you built a max heap as well.

Comment on lines +9 to 11
# 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 +20 to 23
# This method removes and returns an element from the heap maintaining the heap structure
# 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 +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 +103 to +111
if @store[left_child] && @store[left_child].key < @store[index].key
swap(index, left_child)
heap_down_helper(left_child)
end

if @store[right_child] && @store[right_child].key < @store[index].key
swap(index, right_child)
heap_down_helper(right_child)
end

Choose a reason for hiding this comment

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

Doing it this way will work, but might lead to extra swaps if the right child is initially less than the left.

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