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

Mariah: Pine #19

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

Mariah: Pine #19

wants to merge 3 commits into from

Conversation

mrosman7
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 is organized at the different levels by priority
Could you build a heap with linked nodes? yes, I think you could but heaps are generally used with arrays
Why is adding a node to a heap an O(log n) operation? because you have to also heap up the element you added
Were the heap_up & heap_down methods useful? Why? yes, they made it easier to keep our heap sorted by priority

Copy link

@kyra-patton kyra-patton 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 Mariah! I just had a couple comments below. Let me know what questions you have.

🟢



def heap_sort(list):
""" This method uses a heap to sort an array.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(log n)

Choose a reason for hiding this comment

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

✨ Would love to see how you could write heap_sort with the MinHeap class you implemented below as well. Additionally, time complexity is O(n logn) because you are doing heappush/heappop which is a log(n) operation n times.

while parent >= 0 and self.store[parent].key > self.store[temp].key:
self.swap(temp, parent)
temp = parent
parent = (temp - 1) // 2

def heap_down(self, index):
""" This helper method takes an index and
moves the corresponding element down the heap if it's
larger than either of its children and continues until
the heap property is reestablished.
"""

Choose a reason for hiding this comment

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

Time complexity: ?
Space complexity: ?
Time complexity: O(log n)
Space complexity: O(1)

Choose a reason for hiding this comment

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

✨ Nice iterative solution! The only thing I would say is style wise, I might stick to recursion or iteration across heap_up and heap_down for consistency.

Time complexity: ?
Space complexity: ?
Time complexity: O(1)
Space complexity: O(1)

Choose a reason for hiding this comment

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

Time Complexity: ?
Space Complexity: ?
Time Complexity: O(log n)
Space Complexity: O(1)

Choose a reason for hiding this comment

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

✨ Space complexity is O(log n) here because of the recursive call stack of heap_down

Time Complexity: ?
Space Complexity: ?
Time Complexity: O(log n)
Space Complexity: O(1)

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
2 participants