-
Notifications
You must be signed in to change notification settings - Fork 51
Spruce: Kaitlyn #33
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?
Spruce: Kaitlyn #33
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,22 @@ | ||
| 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) | ||
| Space Complexity: O(n) | ||
| """ | ||
| pass | ||
| if len(list) <= 1: | ||
| return list | ||
|
|
||
| heap = MinHeap() | ||
|
|
||
| for item in list: | ||
| heap.add(item) | ||
|
|
||
| return_list = [] | ||
|
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. Your approach looks good and should work if you complete the |
||
| while not heap.empty(): | ||
| return_list.append(heap.remove()) | ||
|
|
||
| return return_list | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| from cgitb import small | ||
|
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. 👀 Stray import (likely from VSCode autocomplete) |
||
|
|
||
|
|
||
| class HeapNode: | ||
|
|
||
| def __init__(self, key, value): | ||
|
|
@@ -19,35 +22,47 @@ 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) | ||
| """ | ||
| pass | ||
| if value == None: | ||
| value = key | ||
| item = HeapNode(key, value) | ||
| self.store.append(item) | ||
| 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) | ||
| """ | ||
| 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 | ||
|
|
||
| self.swap(0, -1) | ||
|
|
||
| removed_value = self.store.pop() | ||
|
|
||
| self.heap_down(0) | ||
|
|
||
| return removed_value.value | ||
|
|
||
|
|
||
| def __str__(self): | ||
| """ This method lets you print the heap, when you're testing your app. | ||
| """ | ||
| if len(self.store) == 0: | ||
| 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 improvement to the provided str method. |
||
| return "[]" | ||
| return f"[{', '.join([str(element) for element in self.store])}]" | ||
|
|
||
|
|
||
| def empty(self): | ||
| """ This method returns true if the heap is empty | ||
| Time complexity: ? | ||
| Space complexity: ? | ||
| Time complexity: O(1) | ||
| Space complexity: O(1) | ||
| """ | ||
| pass | ||
| return not self.store | ||
|
|
||
|
|
||
| def heap_up(self, index): | ||
|
|
@@ -57,19 +72,63 @@ def heap_up(self, index): | |
| property is reestablished. | ||
|
|
||
| This could be **very** helpful for the add method. | ||
| Time complexity: ? | ||
| Space complexity: ? | ||
| Time complexity: O(log n) | ||
| Space complexity: O(log n) | ||
| """ | ||
| pass | ||
| if self.empty(): | ||
| return | ||
|
Comment on lines
+78
to
+79
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. We wouldn't really expect this function to be called when the heap is empty (since it's only called as part of |
||
|
|
||
| if index != 0: | ||
| compare_index = (index - 1) // 2 | ||
| if self.store[index].key < self.store[compare_index].key: | ||
| self.swap(index, compare_index) | ||
| self.heap_up(compare_index) | ||
|
|
||
| return | ||
|
|
||
| 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 | ||
| if self.empty(): | ||
| return | ||
|
Comment on lines
+95
to
+96
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 incomplete. We would need essentially the "reverse" logic of what was done for |
||
|
|
||
| left_child_index = index * 2 + 1 | ||
| right_child_index = index * 2 + 2 | ||
|
|
||
| if left_child_index < len(self.store): | ||
| if right_child_index < len(self.store): | ||
| if self.is_left_index_smaller(left_child_index, right_child_index): | ||
| # smaller_value = self.find_smaller_key(right_child_index, left_child_index) | ||
| # if smaller_value == left_child_index: | ||
| self.swap(index, left_child_index) | ||
| self.heap_down(left_child_index) | ||
| else: | ||
| if self.store[index].key > self.store[right_child_index].key: | ||
| self.swap(index, right_child_index) | ||
| self.heap_down(right_child_index) | ||
| else: | ||
| if self.is_left_index_smaller(left_child_index, index): | ||
| self.swap(index, left_child_index) | ||
| self.heap_down(left_child_index) | ||
|
Comment on lines
+114
to
+115
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. 👀 Notice that each time we decide which child to swap, we swap the child, then keep heaping from that newly swapped location (following the larger value down). We could reduce the repetition somewhat by figuring out which index (if any) will be swapped, then at the end, actually do the swap and heap follow. |
||
|
|
||
| return | ||
|
|
||
| # I started with this helper method, but then switched to the other one | ||
| # def find_smaller_key(self, index_1, index_2): | ||
| # if self.store[index_1].key < self.store[index_2].key: | ||
| # return index_1 | ||
| # else: | ||
| # return index_2 | ||
|
|
||
| # I know this isn't that helpful, but it helped me make more sense of what was happening | ||
| # in the heap_down method | ||
| def is_left_index_smaller(self, index_1, index_2): | ||
|
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. ✨ Adding helpers to wrap your head around some noisy logic is a great strategy. It can be much nicer to read off the name of a function that literally says what it's checking rather than a complicated comparison expression and then keep reminding yourself what the intent of that comparison was. |
||
| if self.store[index_1].key < self.store[index_2].key: | ||
| return True | ||
| return False | ||
|
|
||
| def swap(self, index_1, index_2): | ||
| """ Swaps two elements in self.store | ||
|
|
||
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.
This feels like a workaround due to not completing the
heap_downmethod.