-
Notifications
You must be signed in to change notification settings - Fork 51
Areeba R - Pine #21
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?
Areeba R - Pine #21
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,19 @@ | ||
| from heapq import heappush, heappop | ||
|
|
||
|
|
||
| def heap_sort(list): | ||
| def heap_sort(unsorted): | ||
| """ This method uses a heap to sort an array. | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: n log n | ||
| Space Complexity: n | ||
| """ | ||
| pass | ||
| heap = [] | ||
|
|
||
| for item in unsorted: | ||
| heappush(heap, item) | ||
|
|
||
| ordered = [] | ||
|
|
||
| while heap: | ||
| value = heappop(heap) | ||
| ordered.append(value) | ||
|
|
||
| return ordered | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| class HeapNode: | ||
|
|
||
| def __init__(self, key, value): | ||
| self.key = key | ||
| self.value = value | ||
|
|
@@ -19,21 +18,40 @@ 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: log n | ||
| Space Complexity: 1 | ||
|
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. ✨ Because of the recursive call stack of |
||
| """ | ||
| pass | ||
| if value == None: | ||
| value = key | ||
|
|
||
| #make a new node | ||
| new_node = HeapNode(key, value) | ||
| #add in the new node | ||
| self.store.append(new_node) | ||
| #figure out what the very last index is because that is where we added to | ||
| index = len(self.store) - 1 | ||
| #shift and restore the heap | ||
| self.heap_up(index) | ||
|
|
||
|
|
||
| def remove(self): | ||
| """ This method removes and returns an element from the heap | ||
| maintaining the heap structure | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: log n | ||
| Space Complexity: 1 | ||
|
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, however because of the recursive call stack of |
||
| """ | ||
| pass | ||
| if len(self.store) == 0: | ||
| return None | ||
|
|
||
| #put the minimum value at the very end | ||
| self.swap(0, len(self.store) - 1) | ||
| #pop it off | ||
| min = self.store.pop() | ||
| #bring down the value we put at the very top | ||
| self.heap_down(0) | ||
|
|
||
| return min.value | ||
|
|
||
|
|
||
| def __str__(self): | ||
| """ This method lets you print the heap, when you're testing your app. | ||
| """ | ||
|
|
@@ -44,11 +62,10 @@ def __str__(self): | |
|
|
||
| def empty(self): | ||
| """ This method returns true if the heap is empty | ||
| Time complexity: ? | ||
| Space complexity: ? | ||
| Time complexity: 1 | ||
| Space complexity: 1 | ||
|
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 | ||
|
|
||
| def heap_up(self, index): | ||
| """ This helper method takes an index and | ||
|
|
@@ -57,19 +74,54 @@ def heap_up(self, index): | |
| property is reestablished. | ||
|
|
||
| This could be **very** helpful for the add method. | ||
| Time complexity: ? | ||
| Space complexity: ? | ||
| Time complexity: log n | ||
| Space complexity: 1 | ||
|
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, however because of the recursive call stack, space complexity is O(log n) here |
||
| """ | ||
| pass | ||
| #base case | ||
| if index == 0: | ||
| return | ||
|
|
||
| #parent node | ||
| parent = (index - 1)//2 | ||
| #list | ||
| array = self.store | ||
|
|
||
| #if the parent is greater than the child | ||
| if array[parent].key > array[index].key: | ||
| #swap | ||
| self.swap(parent, index) | ||
| #you would need to keep calling this until you have finally restored the heap | ||
| self.heap_up(parent) | ||
|
|
||
| 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. | ||
|
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 job! Love the comments |
||
| """ | ||
| pass | ||
| left = index * 2 + 1 | ||
| right = index * 2 + 2 | ||
|
Comment on lines
+102
to
+103
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. 🤔 Why not use your helpers here? |
||
| array = self.store | ||
|
|
||
| #is there a left child | ||
| if left < len(array): | ||
| #is there a right child | ||
| if right < len(array): | ||
| #which is smaller | ||
| if array[left].key < array[right].key: | ||
| child_to_swap = left | ||
| else: | ||
| child_to_swap = right | ||
| #there is no right child | ||
| else: | ||
| child_to_swap = left | ||
|
|
||
| #if the child is smaller | ||
| if array[index].key > array[child_to_swap].key: | ||
| #swap them | ||
| self.swap(index, child_to_swap) | ||
| #keep going until the heap is restored | ||
| self.heap_down(child_to_swap) | ||
|
|
||
| def swap(self, index_1, index_2): | ||
| """ Swaps two elements in self.store | ||
|
|
@@ -79,3 +131,12 @@ def swap(self, index_1, index_2): | |
| temp = self.store[index_1] | ||
| self.store[index_1] = self.store[index_2] | ||
| self.store[index_2] = temp | ||
|
|
||
| def parent_idx(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 helpers! |
||
| return index // 2 | ||
|
|
||
| def left_child_idx(self, index): | ||
| return index * 2 | ||
|
|
||
| def right_child_idx(self, index): | ||
| return index * 2 + 1 | ||
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.
✨ Nice! How might you implement this using the
MinHeapyou created below?