diff --git a/heaps/heap_sort.py b/heaps/heap_sort.py index 3b834a5..eff4020 100644 --- a/heaps/heap_sort.py +++ b/heaps/heap_sort.py @@ -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 \ No newline at end of file + heap = [] + + for item in unsorted: + heappush(heap, item) + + ordered = [] + + while heap: + value = heappop(heap) + ordered.append(value) + + return ordered diff --git a/heaps/min_heap.py b/heaps/min_heap.py index f6fe4e0..92013a7 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 @@ -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 """ - 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 """ - 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 """ - pass - + return len(self.store) == 0 def heap_up(self, index): """ This helper method takes an index and @@ -57,10 +74,24 @@ 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 """ - 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 @@ -68,8 +99,29 @@ def heap_down(self, index): larger than either of its children and continues until the heap property is reestablished. """ - pass + left = index * 2 + 1 + right = index * 2 + 2 + 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): + return index // 2 + + def left_child_idx(self, index): + return index * 2 + + def right_child_idx(self, index): + return index * 2 + 1