Conversation
kyra-patton
left a comment
There was a problem hiding this comment.
✨💫 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) |
There was a problem hiding this comment.
✨ 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) |
| the heap property is reestablished. | ||
| """ | ||
| pass | ||
| left = index * 2 + 1 |
|
|
||
| i = 0 | ||
| while not heap.empty(): | ||
| arr[i] = heap.remove() |
There was a problem hiding this comment.
👀 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: |
There was a problem hiding this comment.
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
| If value == None the new node's value should be set to key | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: o(n) |
There was a problem hiding this comment.
✨ However space complexity is O(log n). See comment below ⬇️
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: o(n) | ||
| Space Complexity: o(1) |
There was a problem hiding this comment.
✨ 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) |
There was a problem hiding this comment.
✨ 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.
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up&heap_downmethods useful? Why?heap_upwas good for checking if a node should be moved up and swapping if that was the case; the same in reverse,heap_downwas good for removing a node at the root and then choosing the replacement child and sliding the respective children upward