Conversation
There was a problem hiding this comment.
✨💫 Nice work! I left a few comments below.
For the comprehension questions,
How is a heap different from a BST? --> Your answer is correct, but what is the max/min heap property?
Were the heap_up & heap_down methods useful? Why? --> You do use recursion in heap_up and heap_down but why is recursion useful here?
Let me know what questions you have.
🟢
| """ This method uses a heap to sort an array. | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(log n) |
There was a problem hiding this comment.
👀 Time complexity is going to be O(nlogn) as you iterate through n items in list twice, and within each loop perform add or remove which are both O(log n) operations
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(log n) | ||
| Space Complexity: O(1) |
There was a problem hiding this comment.
✨ Nice however space complexity is going to be O(log n) here because of the recursive call stack of heap_up
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(log n) | ||
| Space Complexity: O(1) |
There was a problem hiding this comment.
✨ However space complexity here will be O(log n) because of the recursive call stack of heap_down
| Time complexity: ? | ||
| Space complexity: ? | ||
| Time complexity: O(1) | ||
| Space complexity: O(1) |
| Time complexity: ? | ||
| Space complexity: ? | ||
| Time complexity: O(log n) | ||
| Space complexity: O(log n) |
| if len(self.store) > (left_child) and self.store[index].key > self.store[left_child].key: | ||
| self.swap(index, (left_child)) | ||
| self.heap_down((left_child)) | ||
| self.heap_down(index) | ||
| elif len(self.store) > (right_child) and self.store[index].key > self.store[right_child].key: | ||
| self.swap(index, (right_child)) | ||
| self.heap_down((right_child)) | ||
| self.heap_down(index) |
There was a problem hiding this comment.
You can reduce your recursive calls to heap_down here. Instead of choosing to swap with the left child automatically if it exists, choose to swap index with whichever child is smaller.
If you swap with the smaller of the two children, that child (as the new parent of index and the other child) will automatically be smaller than both of it's new children and you won't have to call heap_down on it again.
| """ 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. |
There was a problem hiding this comment.
This works, but review my comment below to see how you might improve upon this solution ⬇️
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up&heap_downmethods useful? Why?