Skip to content

Conversation

@theomoondev
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap maintains a complete or balanced binary tree, in which every level is full except the last. A binary search tree could be imbalanced, meaning not every level is full. Also, in a heap, each parent node has a specific order-relationship with it's children.
Could you build a heap with linked nodes? Yes, but it's much easier to implement using an array.
Why is adding a node to a heap an O(log n) operation? Adding a node will, at most, require traversing each level, and because a heap is built on a complete binary tree, it has log n levels.
Were the heap_up & heap_down methods useful? Why? Yes, they allowed me to leverage the system stack to recursively compare each parent with its children and swap to maintain the heap order.

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.

Nice work Lee, you hit the learning goals here. Well done.

Comment on lines +4 to 6
# Time Complexity: O(n log n), where n is the number of nodes and because this method adds the elements to a heap, then places them back in the array in order
# Space Complexity: O(log n), due to recursive calls of the heap_up and heap_down methods within the add and remove methods
def heapsort(list)

Choose a reason for hiding this comment

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

👍 Since you are building a heap, the space complexity is O(n) (a heap of n elements).

Comment on lines +17 to 19
# Time Complexity: O(log n), where n is the number of nodes in a heap (a complete binary tree has log n levels)
# Space Complexity: O(log n), due to recursive calls of the heap_up method
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 +26 to 28
# Time Complexity: O(log n), where n is the number of nodes in a heap (a complete binary tree has log n levels)
# Space Complexity: O(log n), due to recursive calls of the heap_down method
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 +54 to 56
# Time complexity: O(n), where n is the number of nodes
# 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.

👍 , but the time complexity is O(1)

# It could be **very** helpful for the add method.
# Time complexity: ?
# Space complexity: ?
def heap_up(index)

Choose a reason for hiding this comment

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

👍

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

Nice, very clever way to handle checking if the right and left children are beyond the array.

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