From 6bdeb91f05aa426ea7434ee1f27594d3fcc969ee Mon Sep 17 00:00:00 2001 From: letypl12 <86847701+letypl12@users.noreply.github.com> Date: Wed, 20 Jul 2022 13:58:32 -0700 Subject: [PATCH 1/2] Heaps --- heaps/heap_sort.py | 14 ++++++++++++-- heaps/min_heap.py | 48 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 3b834a5..53bc9ff 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -1,8 +1,18 @@ - +from heaps.min_heap import MinHeap def heap_sort(list): """ This method uses a heap to sort an array. Time Complexity: ? Space Complexity: ? """ - pass \ No newline at end of file + heap = MinHeap() + for num in list: + heap.add(num) + + index = 0 + while not heap.empty(): + list[index]=heap.remove() + index +=1 + return list + + diff --git a/heaps/min_heap.py b/heaps/min_heap.py index f6fe4e0..eb9b77b 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -22,7 +22,13 @@ def add(self, key, value = None): Time Complexity: ? Space Complexity: ? """ - pass + if value == None: + value = key + + node = HeapNode(key, value) + self.store.append(node) + self.heap_up(len(self.store)-1) + def remove(self): """ This method removes and returns an element from the heap @@ -30,10 +36,14 @@ def remove(self): Time Complexity: ? Space Complexity: ? """ - pass + if len(self.store)== 0: + return None - - + self.swap(0, len(self.store)-1) + min = self.store.pop() + self.heap_down(0) + return min.value + def __str__(self): """ This method lets you print the heap, when you're testing your app. """ @@ -47,8 +57,8 @@ def empty(self): Time complexity: ? Space complexity: ? """ - pass - + if len(self.store)== 0: + return True def heap_up(self, index): """ This helper method takes an index and @@ -60,7 +70,15 @@ def heap_up(self, index): Time complexity: ? Space complexity: ? """ - pass + if index == 0: + return + + parent_index =(index-1)//2 + store = self.store + + if store[parent_index].key > store[index].key: + self.swap(parent_index, index) + self.heap_up(parent_index) def heap_down(self, index): """ This helper method takes an index and @@ -68,7 +86,21 @@ def heap_down(self, index): larger than either of its children and continues until the heap property is reestablished. """ - pass + left_child = index * 2 + 1 + right_child = index * 2 + 2 + store = self.store + if left_child < len(self.store): + if right_child < len(self.store): + if store[left_child].key < self.store[right_child].key: + smaller = left_child + else: + smaller = right_child + else: + smaller = left_child + + if store[index].key > store[smaller].key: + self.swap(index, smaller) + self.heap_down(smaller) def swap(self, index_1, index_2): From f9b4a45730bae3dfd17c73db2f9b3a60c9ae4e43 Mon Sep 17 00:00:00 2001 From: letypl12 <86847701+letypl12@users.noreply.github.com> Date: Wed, 20 Jul 2022 14:09:56 -0700 Subject: [PATCH 2/2] Added some time and space complexity --- heaps/heap_sort.py | 4 ++-- heaps/min_heap.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 53bc9ff..d79cf63 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -2,8 +2,8 @@ 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 (1) """ heap = MinHeap() for num in list: diff --git a/heaps/min_heap.py b/heaps/min_heap.py index eb9b77b..d542e7a 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -19,8 +19,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(1) + Space Complexity: O(n) """ if value == None: value = key @@ -33,8 +33,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(1) + Space Complexity: O(1) """ if len(self.store)== 0: return None