Skip to content

Conversation

@jasyl
Copy link

@jasyl jasyl commented Apr 29, 2021

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap has different rules. A heap can be a max heap where the parent node is greater than its children or a min heap where its children are greater than the parent. The heap should be a complete tree, this means that all levels except the bottom are full and the bottom row is filled left to right. Also a heap is only partially ordered while a BST must be sorted.
Could you build a heap with linked nodes? I think you can if you're using binary tree linked nodes. Though my understanding in using an Array is that it's more efficient because you can utilize the formula to access children or parent nodes and make swaps.
Why is adding a node to a heap an O(log n) operation? You can push a node to the end, this would be O(1) but then you have to check if this value satisfies the rules for this heap (max or min). If not, you'll need to swap the parent with the current node and repeat this until conditions are satisfied. This is worst case O(log n)
Were the heap_up & heap_down methods useful? Why? Very helpful, these allowed us to add elements to the heap and reorder the tree accordingly.

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 Jasmine, you hit the learning goals here. Well done.

Comment on lines +4 to +7
# Time Complexity: O(n * log(n)) where n is the number of elements.
# Space Complexity: O(log(n)) worsecase because of recursion inside of heapify?
# list = [5, 27, 3, 16, -50]
def heapsort(list)

Choose a reason for hiding this comment

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

👍 Really nice. However you're using a lot of one letter variable names.

Comment on lines +17 to 19
# Time Complexity: O(log(n))
# Space Complexity: O(log(n) due to the stack call in heap_up, could be O(1) using a loop
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(log(n)) - where n is the number of elements and log(n) reps the levels
# Space Complexity: O(log(n) due to the stack call in heap_down, could be O(1) using a loop
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 +56 to 58
# 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 +67 to 69
# Time complexity: O(log(n)) - n for number of elements. log(n) represents the number of levels.
# Space complexity: O(log(n)) - the stack for the resursive calls could be as tall as the number of levels.
def heap_up(index)

Choose a reason for hiding this comment

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

👍

Comment on lines 78 to 81
# 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.

👍

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