From e2131b4fc96088e408e9681a565d325f4dfe0038 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Wed, 20 Jul 2022 18:03:57 -0600 Subject: [PATCH 1/6] first two heap_sort tests pass --- heaps/heap_sort.py | 3 ++- heaps/min_heap.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 3b834a5..62c7e8c 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -5,4 +5,5 @@ def heap_sort(list): Time Complexity: ? Space Complexity: ? """ - pass \ No newline at end of file + if len(list) <= 1: # returns list of empty or only has one node + return list diff --git a/heaps/min_heap.py b/heaps/min_heap.py index f6fe4e0..dbb32f0 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -1,5 +1,5 @@ class HeapNode: - + def __init__(self, key, value): self.key = key self.value = value From 046ab23c1eeacecb9b8b80e83984454cc8db5ca8 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Wed, 20 Jul 2022 18:39:03 -0600 Subject: [PATCH 2/6] add, test passes --- heaps/min_heap.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/heaps/min_heap.py b/heaps/min_heap.py index dbb32f0..bb31c39 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -22,7 +22,10 @@ def add(self, key, value = None): Time Complexity: ? Space Complexity: ? """ - pass + if value == None: + value = key + + self.store.append(HeapNode(key,value)) def remove(self): """ This method removes and returns an element from the heap From e66014f5839d57707bca3f024700a6e4e177ee61 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Wed, 20 Jul 2022 19:55:04 -0600 Subject: [PATCH 3/6] passing all min_heap tests --- heaps/min_heap.py | 105 ++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 41 deletions(-) diff --git a/heaps/min_heap.py b/heaps/min_heap.py index bb31c39..0927bad 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -1,5 +1,4 @@ class HeapNode: - def __init__(self, key, value): self.key = key self.value = value @@ -10,74 +9,98 @@ def __str__(self): def __repr__(self): return str(self.value) -class MinHeap: +class MinHeap: def __init__(self): self.store = [] - - 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: ? + 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: ? """ if value == None: value = key - self.store.append(HeapNode(key,value)) + 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: ? + """This method removes and returns an element from the heap + maintaining the heap structure + Time Complexity: ? + Space Complexity: ? """ - pass + if len(self.store) == 0: + return None + + self.swap(0, len(self.store) - 1) # swap first w/ last + removed = self.store.pop() # remove the last node + self.heap_down(0) # restructure the heap + return removed.value - def __str__(self): - """ This method lets you print the heap, when you're testing your app. - """ + """This method lets you print the heap, when you're testing your app.""" if len(self.store) == 0: 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: ? + """This method returns true if the heap is empty + Time complexity: ? + Space complexity: ? """ - pass - + return self.store == [] def heap_up(self, index): - """ This helper method takes an index and - moves the corresponding element up the heap, if - it is less than it's parent node until the Heap - property is reestablished. - - This could be **very** helpful for the add method. - Time complexity: ? - Space complexity: ? + """This helper method takes an index and + moves the corresponding element up the heap, if + it is less than it's parent node until the Heap + property is reestablished. + + This could be **very** helpful for the add method. + Time complexity: ? + Space complexity: ? """ - pass + if self.empty(): + return + + if index != 0: + check = (index - 1) // 2 + if self.store[index].key < self.store[check].key: + self.swap(index, check) + + self.heap_up(check) + 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. + """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 * 2 + 1 + right = index * 2 + 2 + store = self.store + if left < len(self.store): + if right < len(self.store): + if store[left].key < store[right].key: + less = left + else: + less = right + else: + less = left + + if store[index].key > store[less].key: + self.swap(index, less) + self.heap_down(less) - def swap(self, index_1, index_2): - """ Swaps two elements in self.store - at index_1 and index_2 - used for heap_up & heap_down + """Swaps two elements in self.store + at index_1 and index_2 + used for heap_up & heap_down """ temp = self.store[index_1] self.store[index_1] = self.store[index_2] From 2ff87c3d1b706690159000b44d5d771daf3932f1 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Wed, 20 Jul 2022 20:01:32 -0600 Subject: [PATCH 4/6] Passing all Tests! --- heaps/heap_sort.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 62c7e8c..971d5d6 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -1,4 +1,4 @@ - +from heaps.min_heap import MinHeap def heap_sort(list): """ This method uses a heap to sort an array. @@ -7,3 +7,15 @@ def heap_sort(list): """ if len(list) <= 1: # returns list of empty or only has one node return list + + heap = MinHeap() + + for value in list: + heap.add(value) + + index = 0 + while not heap.empty(): + list[index] = heap.remove() + index += 1 + + return list From 6d1065a39e0f25efb3609bcf33f7988e9f3d563d Mon Sep 17 00:00:00 2001 From: sjolivas Date: Wed, 20 Jul 2022 20:07:19 -0600 Subject: [PATCH 5/6] Added Big O Notation --- heaps/min_heap.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/heaps/min_heap.py b/heaps/min_heap.py index 0927bad..212f692 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -17,8 +17,8 @@ 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(logn) + Space Complexity: O(n) """ if value == None: value = key @@ -29,8 +29,8 @@ def add(self, key, value=None): def remove(self): """This method removes and returns an element from the heap maintaining the heap structure - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(logn) + Space Complexity: O(1) """ if len(self.store) == 0: return None @@ -49,8 +49,8 @@ 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) """ return self.store == [] @@ -61,8 +61,8 @@ def heap_up(self, index): property is reestablished. This could be **very** helpful for the add method. - Time complexity: ? - Space complexity: ? + Time complexity: O(logn) + Space complexity: O(1) """ if self.empty(): return From 184f43678ae04d3f8daaac4dbf32c32abbe37461 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Fri, 22 Jul 2022 21:12:35 -0600 Subject: [PATCH 6/6] made corrections --- heaps/heap_sort.py | 7 +++---- heaps/min_heap.py | 29 +++++++++++++---------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 971d5d6..340e386 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -13,9 +13,8 @@ def heap_sort(list): for value in list: heap.add(value) - index = 0 + result = [] # unpacking the heap into a new result list to avoid side effects while not heap.empty(): - list[index] = heap.remove() - index += 1 + result.append(heap.remove()) - return list + return result diff --git a/heaps/min_heap.py b/heaps/min_heap.py index 212f692..b26d80d 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -18,9 +18,9 @@ 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: O(logn) - Space Complexity: O(n) + Space Complexity: O(logn) - heap_up is recursive, stack soace being consumed """ - if value == None: + if value is None: value = key self.store.append(HeapNode(key, value)) @@ -30,9 +30,9 @@ def remove(self): """This method removes and returns an element from the heap maintaining the heap structure Time Complexity: O(logn) - Space Complexity: O(1) + Space Complexity: O(logn) - heap_down is recursive, stack soace being consumed """ - if len(self.store) == 0: + if self.empty(): return None self.swap(0, len(self.store) - 1) # swap first w/ last @@ -52,7 +52,7 @@ def empty(self): Time complexity: O(1) Space complexity: O(1) """ - return self.store == [] + return not self.store def heap_up(self, index): """This helper method takes an index and @@ -62,18 +62,15 @@ def heap_up(self, index): This could be **very** helpful for the add method. Time complexity: O(logn) - Space complexity: O(1) + Space complexity: O(logn) """ - if self.empty(): + if index == 0: return - if index != 0: - check = (index - 1) // 2 - if self.store[index].key < self.store[check].key: - self.swap(index, check) - + check = (index - 1) // 2 + if self.store[index].key < self.store[check].key: + self.swap(index, check) self.heap_up(check) - return def heap_down(self, index): """This helper method takes an index and @@ -83,17 +80,17 @@ def heap_down(self, index): """ left = index * 2 + 1 right = index * 2 + 2 - store = self.store + if left < len(self.store): if right < len(self.store): - if store[left].key < store[right].key: + if self.store[left].key < self.store[right].key: less = left else: less = right else: less = left - if store[index].key > store[less].key: + if self.store[index].key > self.store[less].key: self.swap(index, less) self.heap_down(less)