Skip to content

Maple - Karishma Johnson#17

Open
karishmacarterjohnson wants to merge 1 commit intoAda-C16:masterfrom
karishmacarterjohnson:master
Open

Maple - Karishma Johnson#17
karishmacarterjohnson wants to merge 1 commit intoAda-C16:masterfrom
karishmacarterjohnson:master

Conversation

@karishmacarterjohnson
Copy link
Copy Markdown

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
Copy Markdown

@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!

🟢

Comment thread heaps/heap_sort.py
""" This method uses a heap to sort an array.
Time Complexity: ?
Space Complexity: ?
Time Complexity: o(n^2)
Copy link
Copy Markdown

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)

Comment thread heaps/min_heap.py
Time complexity: ?
Space complexity: ?
Time complexity: o(1)
Space complexity: o(0-1)
Copy link
Copy Markdown

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)

Comment thread heaps/min_heap.py
the heap property is reestablished.
"""
pass
left = index * 2 + 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread heaps/heap_sort.py

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

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

Comment thread heaps/min_heap.py
self.store.append(node)

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

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

Comment thread heaps/min_heap.py
If value == None the new node's value should be set to key
Time Complexity: ?
Space Complexity: ?
Time Complexity: o(n)
Copy link
Copy Markdown

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 ⬇️

Comment thread heaps/min_heap.py
Time Complexity: ?
Space Complexity: ?
Time Complexity: o(n)
Space Complexity: o(1)
Copy link
Copy Markdown

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.

Comment thread heaps/min_heap.py
Time complexity: ?
Space complexity: ?
Time complexity: o(1)
Space complexity: o(1)
Copy link
Copy Markdown

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