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

Maple - Karishma Johnson #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

karishmacarterjohnson
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? heaps are unordered
Could you build a heap with linked nodes? no
Why is adding a node to a heap an O(log n) operation? it goes pairwise up/down the tree thus halving the operations each iteration
Were the heap_up & heap_down methods useful? Why? heap_up was good for checking if a node should be moved up and swapping if that was the case; the same in reverse, heap_down was good for removing a node at the root and then choosing the replacement child and sliding the respective children upward

@karishmacarterjohnson karishmacarterjohnson changed the title all tests pass Maple - Karishma Johnson May 12, 2022
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 job Karishma. I left some comments on your implementation below.

In response to your comprehension questions, note that heaps are _semi_ordered not unordered, and that a heap can be implemented using a linked list it's just not the ideal structure as linked lists are quite expensive to traverse.

Let me know what questions you have!

🟢

""" This method uses a heap to sort an array.
Time Complexity: ?
Space Complexity: ?
Time Complexity: o(n^2)

Choose a reason for hiding this comment

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

✨ However time complexity is actually O(n log n) because remove and add are O(log n) operations instead of O(n). Because you are creating a heap of size n, space complexity will be O(n)

Time complexity: ?
Space complexity: ?
Time complexity: o(1)
Space complexity: o(0-1)

Choose a reason for hiding this comment

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

✨ Space complexity would be O(1)


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.
"""
pass
left = index * 2 + 1

Choose a reason for hiding this comment

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

i = 0
while not heap.empty():
arr[i] = heap.remove()

Choose a reason for hiding this comment

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

👀 remove is actually an O(log n) operation - see additional comments in your remove implementation

self.store.append(node)

index = len(self.store) -1
while index != None and index != 0:

Choose a reason for hiding this comment

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

Interesting to do this iteratively! Because you are halving the index with each call to heap_up, this loop should actually run O(log n) times

@@ -19,18 +19,36 @@ def __init__(self):
def add(self, key, value = None):
""" This method adds a HeapNode instance to the heap
If value == None the new node's value should be set to key
Time Complexity: ?
Space Complexity: ?
Time Complexity: o(n)

Choose a reason for hiding this comment

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

✨ However space complexity is O(log n). See comment below ⬇️

Time Complexity: ?
Space Complexity: ?
Time Complexity: o(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.

✨ Because of the recursive heap_down operation, space and time complexity will actually be O(log n) here.

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.

✨ Creative to use this in conjunction with iteration in your add function. For consistency style wise, I would recommend sticking to either an iterative or recursive solution between heap_up and heap_down. Technically the specification does say heap_up should perform its operation "until the Heap property is reestablished" which indicates recursion, but I'm not too concerned with that.

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