diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 3b834a5..fdfbf4f 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -1,8 +1,28 @@ +def heapify(arr, n, i): + largest = i + l = 2 * i + 1 + r = 2 * i + 2 + if l < n and arr[l] > arr[largest]: + largest = l + if r < n and arr[r] > arr[largest]: + largest = r + if largest != i: + arr[i], arr[largest] = arr[largest], arr[i] + heapify(arr, n, largest) -def heap_sort(list): +def heap_sort(arr): """ This method uses a heap to sort an array. - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(n) """ - pass \ No newline at end of file + n = len(arr) + for i in range(n, -1, -1): + heapify(arr, n, i) + for i in range(n - 1, 0, -1): + arr[i], arr[0] = arr[0], arr[i] + heapify(arr, i, 0) + return arr + +# time complexity: O(nlog(n)) +# space complexity: O(1) \ No newline at end of file diff --git a/heaps/min_heap.py b/heaps/min_heap.py index f6fe4e0..abec46a 100644 --- a/heaps/min_heap.py +++ b/heaps/min_heap.py @@ -19,20 +19,34 @@ 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 + + self.store.append(HeapNode(key, value)) + self.heap_up(len(self.store) - 1) + # this method removes heap node with minimum key 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 len(self.store) == 0: + return None + + first_node = 0 + last_node = len(self.store) - 1 + + self.swap(first_node, last_node) + result = self.store.pop() + self.heap_down(first_node) + return result.value def __str__(self): """ This method lets you print the heap, when you're testing your app. @@ -44,11 +58,10 @@ 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) """ - pass - + return len(self.store) == 0 def heap_up(self, index): """ This helper method takes an index and @@ -57,18 +70,40 @@ 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 + temporary = index + while temporary > 0: + parent = (temporary - 1) // 2 + if self.store[temporary].key < self.store[parent].key: + self.swap(temporary, parent) + temporary = parent + else: + break + + # method to move heap down 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 + temporary = index + while temporary < len(self.store) - 1: + left_child = 2 * temporary + 1 + right_child = 2 * temporary + 2 + if left_child < len(self.store) and self.store[temporary].key > self.store[left_child].key: + temporary = left_child + if right_child < len(self.store) and self.store[temporary].key > self.store[right_child].key: + temporary = right_child + if temporary == index: + break + self.swap(temporary, index) + index = temporary + + def swap(self, index_1, index_2):