Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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!

# 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)

# 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