-
Notifications
You must be signed in to change notification settings - Fork 51
Cedar - Lux #27
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
base: master
Are you sure you want to change the base?
Cedar - Lux #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
|
|
||
| from heaps.min_heap import MinHeap | ||
|
|
||
| def heap_sort(list): | ||
| """ This method uses a heap to sort an array. | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n log n) where n is the number of items in the list | ||
| Space Complexity: O(n) where n is the number of items in the list | ||
| """ | ||
| pass | ||
| heap = MinHeap() | ||
| for item in list: | ||
| heap.add(item) | ||
| for i in range(len(list)): | ||
| list[i] = heap.remove() | ||
| return list | ||
|
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note the since this isn't a fully in-place solution (the MinHeap has a O(n) internal store), we don't necessarily need to modify the passed in list. The tests are written to check the return value, so we could unpack the heap into a new result list to avoid mutating the input. Also, in this situation, since we built the heap, we also "know" the number of items in the heap. So it's OK to iterate a fixed number of times. But if we were pulling things out of a heap more generally, we would want to make use of the result = []
while not heap.empty():
result.append(heap.remove())
return result |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,18 +19,29 @@ 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(log n) | ||
| Space Complexity: O(log n) due to recursive stack calls | ||
|
Comment on lines
+22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ Great. You're exactly right that it's due to the recursive call in |
||
| """ | ||
| pass | ||
| if value is None: | ||
| value = key | ||
| self.store.append(HeapNode(key, value)) | ||
| self.heap_up(len(self.store) - 1) | ||
|
|
||
| def remove(self): | ||
| """ This method removes and returns an element from the heap | ||
| maintaining the heap structure | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(log n) | ||
| Space Complexity: O(log n) due to recursive stack calls | ||
|
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ Nice. Just as for |
||
| """ | ||
| pass | ||
| if self.empty(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ Nice use of your own helper method! |
||
| return None | ||
| last_index = len(self.store) - 1 | ||
| root_index = 0 | ||
| self.swap(last_index, root_index) | ||
| removed = self.store.pop() | ||
| self.heap_down(root_index) | ||
| return removed.value | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
@@ -44,10 +55,10 @@ def __str__(self): | |
|
|
||
| def empty(self): | ||
| """ This method returns true if the heap is empty | ||
| Time complexity: ? | ||
| Space complexity: ? | ||
| Time complexity: O(1) | ||
| Space complexity: O(1) | ||
|
Comment on lines
+58
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
| """ | ||
| pass | ||
| return len(self.store) == 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember that an empty list is falsy return not self.store |
||
|
|
||
|
|
||
| def heap_up(self, index): | ||
|
|
@@ -57,18 +68,32 @@ def heap_up(self, index): | |
| property is reestablished. | ||
|
|
||
| This could be **very** helpful for the add method. | ||
| Time complexity: ? | ||
| Space complexity: ? | ||
| Time complexity: O(1) | ||
| Space complexity: O(1) | ||
|
Comment on lines
+71
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 This is where the O(log n) time and space complexity in |
||
| """ | ||
| pass | ||
| parent_index = (index - 1) // 2 | ||
| if parent_index >= 0 and self.store[parent_index].key > self.store[index].key: | ||
| self.swap(parent_index, index) | ||
| self.heap_up(parent_index) | ||
|
|
||
| def heap_down(self, index): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ Nice set of conditionals to narrow in on where to swap. Notice there's a little duplication since we call Though not prompted, like |
||
| """ 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 = (index * 2) + 1 | ||
| right_index = (index * 2) + 2 | ||
| if left_index < len(self.store) and right_index < len(self.store): | ||
| smallest = left_index | ||
| if self.store[left_index].key > self.store[right_index].key: | ||
| smallest = right_index | ||
| if self.store[smallest].key < self.store[index].key: | ||
| self.swap(smallest, index) | ||
| self.heap_down(smallest) | ||
| elif left_index < len(self.store) and self.store[left_index].key < self.store[index].key: | ||
| self.swap(left_index, index) | ||
| self.heap_down(left_index) | ||
|
|
||
|
|
||
| def swap(self, index_1, index_2): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨ Great. Since sorting using a heap reduces down to building up a heap of n items one-by-one (each taking O(log n)), then pulling them back out again (again taking O(log n) for each of n items), we end up with a time complexity of O(2n log n) → O(n log n). While for the space, we do need to worry about the O(log n) space consume during each add and remove, but they aren't cumulative (each is consumed only during the call to add or remove). However, the internal store for the
MinHeapdoes grow with the size of the input list. So the maximum space would be O(n + log n) → O(n), since n is a larger term than log n.Note that a fully in-place solution (O(1) space complexity) would require both avoiding the recursive calls, as well as working directly with the originally provided list (no internal store).