Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions heaps/heap_sort.py
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice! How might you implement this using the MinHeap you created below?

"""
pass
heap = []

for item in unsorted:
heappush(heap, item)

ordered = []

while heap:
value = heappop(heap)
ordered.append(value)

return ordered
93 changes: 77 additions & 16 deletions heaps/min_heap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class HeapNode:

def __init__(self, key, value):
self.key = key
self.value = value
Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Because of the recursive call stack of heap_up space complexity is O(log n) here

"""
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice, however because of the recursive call stack of heap_down, space complexity is also O(log n)

"""
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.
"""
Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""
pass

return len(self.store) == 0

def heap_up(self, index):
""" This helper method takes an index and
Expand All @@ -57,19 +74,54 @@ 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Nice, however because of the recursive call stack, space complexity is O(log n) here

"""
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
moves the corresponding element down the heap if it's
larger than either of its children and continues until
the heap property is reestablished.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Great job! Love the comments

"""
pass
left = index * 2 + 1
right = index * 2 + 2
Comment on lines +102 to +103

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Why not use your helpers here?

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
Expand All @@ -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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💫 Nice helpers!

return index // 2

def left_child_idx(self, index):
return index * 2

def right_child_idx(self, index):
return index * 2 + 1