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

Jessica #3

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

Jessica #3

wants to merge 3 commits into from

Conversation

jessicacaley
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? They are both binary trees, but are organized differently: a 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, but it would be much different efficiency because you can't access the nodes by index but also things like removing from the beginning are more efficient in a linked list.
Why is adding a node to a heap an O(log n) operation? Because you only have to hit each level of the tree once and the tree is guaranteed to be balanced so more nodes on the tree increase the complexity logarithmically instead of linearly.
Were the heap_up & heap_down methods useful? Why? Yes, very! It helped me understand the steps of what was happening and why for adding and removing nodes, and allowed for recursion to simpify the implementation.

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.

Excellent work! You hit all the learning goals here. Well done!

@@ -14,18 +14,23 @@ def initialize
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n(log(n))) where n is the number of nodes in the heap

Choose a reason for hiding this comment

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

Since you are making at most log n swaps to add something to a heap, the time complexity is O(log n)

return if @store[current_index].key > @store[parent_index].key

swap(current_index, parent_index)
heap_up(parent_index)
end

# This helper method takes an index and
# moves it up 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.

Nicely done!

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