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
15 changes: 11 additions & 4 deletions heaps/heap_sort.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@

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(nlogn)
Space Complexity: O(n)
Comment on lines +5 to +6

Choose a reason for hiding this comment

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

✨ Great. Since sorting using a heap reduces down to building up a heap of n items one-by-one (each taking O(log n)), then pulling them back out again (again taking O(log n) for each of n items), we end up with a time complexity of O(2n log n) → O(n log n). While for the space, we do need to worry about the O(log n) space consume during each add and remove, but they aren't cumulative (each is consumed only during the call to add or remove). However, the internal store for the MinHeap does grow with the size of the input list. So the maximum space would be O(n + log n) → O(n), since n is a larger term than log n.

Note that a fully in-place solution (O(1) space complexity) would require both avoiding the recursive calls, as well as working directly with the originally provided list (no internal store).

"""
pass
heap = MinHeap()
sorted = []
for item in list:
heap.add(item)
for i in range(len(list)):

Choose a reason for hiding this comment

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

In this situation, since we built the heap, we also "know" the number of items in the heap. So it works to iterate using knowledge about the list. But if we were pulling things out of a heap more generally, we would want to make use of the empty helper as follows:

    sorted = []
    while not heap.empty():
        sorted.append(heap.remove())

    return sorted

sorted.append(heap.remove())
return sorted

66 changes: 53 additions & 13 deletions heaps/min_heap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import operator

class HeapNode:

def __init__(self, key, value):
Expand All @@ -19,18 +21,28 @@ 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(logn)
Space complexity: O(logn) because of the call stack
Comment on lines +24 to +25

Choose a reason for hiding this comment

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

✨ Great. You're exactly right that it's due to the recursive call in heap_up that the space complexity is O(log n). If heap_up were implemented iteratively, this would only require O(1) space complexity since the stack size wouldn't depend on the heap depth.

"""
pass
self.store.append(HeapNode(key, value or key))

Choose a reason for hiding this comment

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

👀 To explicitly handle the case where the value is absent, prefer an explicit check with None

        if value is None:
            value = key

Using or would treat any falsy value as being missing, so we could not store False as the value, for example.

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(logn)
Space complexity: O(logn) because of the call stack
Comment on lines +33 to +34

Choose a reason for hiding this comment

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

✨ Nice. Just as for add, you're right that the log space complexity remove is due to the recursive heap_down implementation. We could achieve O(1) space complexity if we used an iterative approach.

"""
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
if len(self.store) == 1:
return self.store.pop().value
minimum = self.store[0]
self.store[0] = self.store.pop()
Comment on lines +38 to +41

Choose a reason for hiding this comment

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

We could avoid this special case by swapping the first element with the last (moves the minimum to the end, and a larger value to the head), then popping from the end. If there were only one thing left, it would be swapped with itself, then removed.

self.heap_down(0)

return minimum.value




Expand All @@ -44,10 +56,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)
Comment on lines +59 to +60

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

Choose a reason for hiding this comment

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

Remember that an empty list is falsy

        return not self.store



def heap_up(self, index):
Expand All @@ -57,18 +69,46 @@ def heap_up(self, index):
property is reestablished.

This could be **very** helpful for the add method.
Time complexity: ?
Space complexity: ?
Time complexity: O(logn)
Space complexity: O(logn) because of the call stack
Comment on lines +72 to +73

Choose a reason for hiding this comment

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

✨ Yes, this function is where the complexity in add comes from.

"""
pass
if index == 0:
return
parent = (index-1)//2
if self.store[parent].key > self.store[index].key:
self.swap(parent, index)
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.
"""
pass
def get_valid_index_or_none(index):
if index < len(self.store):
return index
else:
return None

def get_key_or_none(index):
if not index:
return None
return self.store[index].key
Comment on lines +89 to +98

Choose a reason for hiding this comment

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

✨ Nice helpers. They don't really need to be defined locally to the heap_down function, but that does prevent them from being called from anywhere else. On the one hand I like the protection, on the other hand, local functions can be confusing since they make the reader think about why the function as declared locally. Not really a right or wrong way here, just be sure to follow the style in use by the rest of your team.


left_index = get_valid_index_or_none(index * 2 + 1)
right_index = get_valid_index_or_none(index * 2 + 2)

left_key = get_key_or_none(left_index)
right_key = get_key_or_none(right_index)

index_to_swap = min((node for node in [(left_index, left_key), (right_index, right_key)] if node[1] is not None ), key=operator.itemgetter(1), default=None)

Choose a reason for hiding this comment

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

This is a really concise set of steps to filter out the invalid children, then select the minimum! However, it's a bit dense, which makes it a little tough to understand. Consider adding a comment about what it does, break it up into several lines with the intermediate products given descriptive names, or move into a helper function with a descriptive name.

An example of splitting across multiple lines

        candidate_children = [(left_index, left_key), (right_index, right_key)]  # we could use a tuple rather than a list
        valid_children = (node for node in candidate_children if node[1] is not None)  # this actually makes a generator function, not a tuple
        index_to_swap = min(valid_children, key=operator.itemgetter(1), default=None) 


if index_to_swap and self.store[index].key > index_to_swap[1]:
self.swap(index, index_to_swap[0])
self.heap_down(index_to_swap[0])



def swap(self, index_1, index_2):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_min_heap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import pytest
from heaps.min_heap import MinHeap

Expand Down Expand Up @@ -69,12 +70,15 @@ def test_it_can_remove_nodes_in_proper_order(heap):
heap.add(0, "Donuts")
heap.add(16, "Cookies")

print(str(heap))

# Act
returned_items = ["Donuts", "Pizza", "Pasta", "Soup", "Cookies", "Cake"]


for item in returned_items:
assert heap.remove() == item
print(str(heap))

def test_removing_a_node_from_an_empty_heap_is_none(heap):
assert heap.remove() == None