Skip to content

Conversation

@nerdyistrendy
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Heap is a complete tree and in a different order(depends on min or max, parent will always be smaller or greater than child)
Could you build a heap with linked nodes? No as they are not indexed
Why is adding a node to a heap an O(log n) operation? In worst case it will traverse from the bottom to the top, which is O(log n) times of swap
Were the heap_up & heap_down methods useful? Why? yes, that's how we keep the heap in 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.

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

Comment on lines +4 to +6
# Time Complexity: O(nlogn): add O(nlogn) + remove O(nlogn)
# Space Complexity: O(n) for creating @store to store all the nodes
def heapsort(list)

Choose a reason for hiding this comment

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

👍

Comment on lines +17 to 19
# Time Complexity: O(log n): push O(1) + heap_up O(log n)
# Space Complexity: O(log n) : heap_up 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 +26 to 28
# Time Complexity: O(log n): pop, swap O(1) + heap_down O(log n)
# Space Complexity: O(log n) from heap_down call stacks
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(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 +65 to 68
# Time complexity: O(log n): swap - O(1), and we will do it up to O(log n) times
# Space complexity: O(log n) for call stacks
# min heap, the smallest node goes up to root
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 +81 to 84
# swap parent with the smaller child
# Time complexity: O(log n): find_smaller_childs_index - O(1), swap - O(1), and we will do it up to O(log n) times
# Space complexity: O(log n) for call stacks
def heap_down(index)

Choose a reason for hiding this comment

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

👍 , I like the helper method!

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