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

Raisah #24

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

Raisah #24

wants to merge 3 commits into from

Conversation

raisahv
Copy link

@raisahv raisahv commented Mar 18, 2020

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? A heap is not ordered in the same way as a BST. Each node in a BST has to have a left child that is lower than and a right child that is higher than the current node. A min heap just has to have a current node that is lower than both it's children.
Could you build a heap with linked nodes? Yes, this would be similar to building a heap with an array, but a linked list would take more time to reach a specific index, so it might not be ideal for heap_up and heap_down.
Why is adding a node to a heap an O(log n) operation? Because you add a node to the end of the heap array, at worse, there will have to be logn swaps to move the node to the top of the heap.
Were the heap_up & heap_down methods useful? Why? Yes, because they made the add and remove functions easier to implement

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. You hit all the learning goals here. Nice work.

Comment on lines +4 to +6
# Time Complexity: O(nlogn + nlogn) The heapsort will iterate n times through the list to add elements to the heap, taking logn times at worst each time and then the heapsort will iterate n times through the heap to remove elements, taking logn times at worst to remove elements. This reduces to O(nlogn)
# Space Complexity: O(n) the heapsort function creates a heap of n size when sorting.
def heapsort(list)

Choose a reason for hiding this comment

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

You can reduce the time complexity to O(n log n). Otherwise this works well.

Comment on lines +17 to 19
# Time Complexity: O(logn) At worse, the heap_up function will have to iterate logn times to add a heapnode from the leaf to the root of the heap
# Space Complexity: O(logn) the heap_up function doesn't store additional variables, but does make logn recursive calls to the memory stack in worst case
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(logn) At worse, the heap_down function will have to iterate logn times to remove a heapnode
# Space Complexity: O(logn) the remove function doesn't store additional variables, but does make logn recursive calls to the memory stack in worst case
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(n) checks the length of @store, which varies linearly with the length of the heap
# Space complexity: O(1) variables stay constant regardless of length of heap
def empty?

Choose a reason for hiding this comment

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

Why would this be O(n) time complexity ?

Comment on lines +67 to +69
# Time Complexity: O(logn) At worse, the heap_up function will have to iterate logn times to add a heapnode from the leaf to the root of the heap
# Space Complexity: O(logn) the heap_up function doesn't store additional variables, but does make logn recursive calls to the memory stack in worst case
def heap_up(current_index)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Method not implemented yet..."
# moves it down the heap if it's larger
# than it's children nodes.
def heap_down(current_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