From 9f93d032890fc415e11186fdd6e23f8dece19e65 Mon Sep 17 00:00:00 2001 From: kaitlyngore Date: Mon, 18 Jul 2022 13:06:10 -0400 Subject: [PATCH 1/5] min_heap methods --- heaps/min_heap.py | 51 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/heaps/min_heap.py b/heaps/min_heap.py index f6fe4e0..4c691ae 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -19,35 +19,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(1) """ - 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(1) """ - pass + if self.empty(): + return None + + self.swap(0, self.store[-1]) + removed_value = self.store.pop() + + self.heap_down() + + return removed_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(): 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 self.store == [] def heap_up(self, index): @@ -57,10 +69,20 @@ 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(1) """ - pass + if self.empty(): + return + + 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 @@ -68,7 +90,8 @@ def heap_down(self, index): larger than either of its children and continues until the heap property is reestablished. """ - pass + if self.empty(): + return def swap(self, index_1, index_2): From a090f5f942b8e933edea6c3f59dad839e9162f34 Mon Sep 17 00:00:00 2001 From: kaitlyngore Date: Mon, 18 Jul 2022 13:26:30 -0400 Subject: [PATCH 2/5] all but 2 tests passing --- heaps/heap_sort.py | 14 +++++++++++++- heaps/min_heap.py | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 3b834a5..a1373bf 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -1,3 +1,4 @@ +from heaps.min_heap import MinHeap def heap_sort(list): @@ -5,4 +6,15 @@ def heap_sort(list): Time Complexity: ? Space Complexity: ? """ - pass \ No newline at end of file + if len(list) <= 1: + return list + + heap = MinHeap() + + for item in list: + heap.add(item) + + return_list = [] + while not heap.empty(): + return_list.append(heap.remove()) + diff --git a/heaps/min_heap.py b/heaps/min_heap.py index 4c691ae..c65c86f 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -37,11 +37,11 @@ def remove(self): if self.empty(): return None - self.swap(0, self.store[-1]) + self.swap(0, -1) removed_value = self.store.pop() - self.heap_down() + self.heap_down(0) return removed_value From 36b79063037a61ae8d0b46c4c6f1431ac9e6088c Mon Sep 17 00:00:00 2001 From: kaitlyngore Date: Mon, 18 Jul 2022 13:27:17 -0400 Subject: [PATCH 3/5] small change --- heaps/heap_sort.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index a1373bf..cca8583 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -8,13 +8,15 @@ def heap_sort(list): """ if len(list) <= 1: return list - + heap = MinHeap() for item in list: heap.add(item) return_list = [] - while not heap.empty(): + while heap.empty() != True: return_list.append(heap.remove()) + return return_list + From b5df6a87c5f4acd17470ff4fa04c552392bdb7ac Mon Sep 17 00:00:00 2001 From: kaitlyngore Date: Mon, 18 Jul 2022 13:28:13 -0400 Subject: [PATCH 4/5] add time space complexity --- heaps/heap_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index cca8583..c801c5a 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -3,8 +3,8 @@ def heap_sort(list): """ This method uses a heap to sort an array. - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n * n log n) + Space Complexity: O(n) """ if len(list) <= 1: return list From 68d4ea28deb1eabb8a2bf363292f8e4fb7e3c7a3 Mon Sep 17 00:00:00 2001 From: kaitlyngore Date: Thu, 21 Jul 2022 01:25:02 -0400 Subject: [PATCH 5/5] complete heap_down method --- heaps/heap_sort.py | 4 ++-- heaps/min_heap.py | 50 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index c801c5a..ce5d51f 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -3,7 +3,7 @@ def heap_sort(list): """ This method uses a heap to sort an array. - Time Complexity: O(n * n log n) + Time Complexity: O(n log n) Space Complexity: O(n) """ if len(list) <= 1: @@ -15,7 +15,7 @@ def heap_sort(list): heap.add(item) return_list = [] - while heap.empty() != True: + while not heap.empty(): return_list.append(heap.remove()) return return_list diff --git a/heaps/min_heap.py b/heaps/min_heap.py index c65c86f..5d01032 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -1,3 +1,6 @@ +from cgitb import small + + class HeapNode: def __init__(self, key, value): @@ -20,7 +23,7 @@ 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(log n) - Space Complexity: O(1) + Space Complexity: O(log n) """ if value == None: value = key @@ -32,7 +35,7 @@ def remove(self): """ This method removes and returns an element from the heap maintaining the heap structure Time Complexity: O(log n) - Space Complexity: O(1) + Space Complexity: O(log n) """ if self.empty(): return None @@ -43,7 +46,7 @@ def remove(self): self.heap_down(0) - return removed_value + return removed_value.value def __str__(self): @@ -59,7 +62,7 @@ def empty(self): Time complexity: O(1) Space complexity: O(1) """ - return self.store == [] + return not self.store def heap_up(self, index): @@ -70,7 +73,7 @@ def heap_up(self, index): This could be **very** helpful for the add method. Time complexity: O(log n) - Space complexity: O(1) + Space complexity: O(log n) """ if self.empty(): return @@ -79,8 +82,7 @@ def heap_up(self, index): 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) + self.heap_up(compare_index) return @@ -93,6 +95,40 @@ def heap_down(self, index): if self.empty(): return + 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) + + 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): + 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