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
20 changes: 17 additions & 3 deletions heaps/heap_sort.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
from heaps.min_heap import MinHeap


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(n)
"""
pass
if len(list) <= 1:
return list
Comment on lines +9 to +10

Choose a reason for hiding this comment

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

This feels like a workaround due to not completing the heap_down method.


heap = MinHeap()

for item in list:
heap.add(item)

return_list = []

Choose a reason for hiding this comment

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

Your approach looks good and should work if you complete the heap_down method.

while not heap.empty():
return_list.append(heap.remove())

return return_list

87 changes: 73 additions & 14 deletions heaps/min_heap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from cgitb import small

Choose a reason for hiding this comment

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

👀 Stray import (likely from VSCode autocomplete)



class HeapNode:

def __init__(self, key, value):
Expand All @@ -19,35 +22,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(log n)
"""
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(log n)
"""
pass
if self.empty():

Choose a reason for hiding this comment

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

✨ Nice use of your own helper method!

return None

self.swap(0, -1)

removed_value = self.store.pop()

self.heap_down(0)

return removed_value.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():

Choose a reason for hiding this comment

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

✨ Nice improvement to the provided str method.

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 not self.store


def heap_up(self, index):
Expand All @@ -57,19 +72,63 @@ 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(log n)
"""
pass
if self.empty():
return
Comment on lines +78 to +79

Choose a reason for hiding this comment

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

We wouldn't really expect this function to be called when the heap is empty (since it's only called as part of add). We could even make it more clear to callers that this is not intended to be called arbitrarily if we named the function _heap_up or __heap_up.


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
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
if self.empty():
return
Comment on lines +95 to +96

Choose a reason for hiding this comment

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

👀 This is incomplete. We would need essentially the "reverse" logic of what was done for heap_up.


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)
Comment on lines +114 to +115

Choose a reason for hiding this comment

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

👀 Notice that each time we decide which child to swap, we swap the child, then keep heaping from that newly swapped location (following the larger value down). We could reduce the repetition somewhat by figuring out which index (if any) will be swapped, then at the end, actually do the swap and heap follow.


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):

Choose a reason for hiding this comment

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

✨ Adding helpers to wrap your head around some noisy logic is a great strategy. It can be much nicer to read off the name of a function that literally says what it's checking rather than a complicated comparison expression and then keep reminding yourself what the intent of that comparison was.

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
Expand Down