Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abigail C. - Rock #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
19 changes: 15 additions & 4 deletions heaps/heap_sort.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@


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(1)
Comment on lines 3 to +6

Choose a reason for hiding this comment

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

👍 However the space complexity is O(n) because you are building up a heap of n nodes as the function runs.

"""
pass
heap = MinHeap()
if list == []:
return list

for num in list:
heap.add(num)

index = 0
while not heap.empty():
list[index] = heap.remove()
index += 1
return list
48 changes: 40 additions & 8 deletions heaps/min_heap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
class HeapNode:

def __init__(self, key, value):
Expand All @@ -24,16 +25,27 @@ def add(self, key, value = None):
Time Complexity: ?
Space Complexity: ?
"""
pass
if value == None:
value = key

node = HeapNode(key,value)
self.store.append(node)
self.heap_up(len(self.store) -1)
Comment on lines +28 to +33

Choose a reason for hiding this comment

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

👍 Space/time complexity?


def remove(self):
""" This method removes and returns an element from the heap
maintaining the heap structure
Time Complexity: ?
Time Complexity: O(log n)
Space Complexity: ?
"""
pass
if len(self.store) == 0:
return None

self.swap(0, len(self.store) -1)
min = self.store.pop()
self.heap_down(0)
Comment on lines 35 to +46

Choose a reason for hiding this comment

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

👍 Space complexity?


return min.value


def __str__(self):
Expand All @@ -49,7 +61,7 @@ def empty(self):
Time complexity: ?
Space complexity: ?
"""
pass
return self.store == []

Choose a reason for hiding this comment

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

👍



def heap_up(self, index):

Choose a reason for hiding this comment

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

👍 Space/time complexity?

Expand All @@ -62,16 +74,36 @@ def heap_up(self, index):
Time complexity: ?
Space complexity: ?
"""
pass

current= self.store[index].key
index_parent = math.floor((index-1)/2)
current_parent= self.store[index_parent].key

while current < current_parent:
if index_parent < 0:
break
self.swap(index, index_parent)
index = index_parent
index_parent = math.floor(((index_parent-1)/2))
current_parent = self.store[index_parent].key
return self.store

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.
"""
Comment on lines 90 to 95

Choose a reason for hiding this comment

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

👍 Nice work, very compact

pass

left_child = index * 2 + 1
right_child = index * 2 + 2

if len(self.store) > (left_child) and self.store[index].key > self.store[left_child].key:
self.swap(index, (left_child))
self.heap_down((left_child))
self.heap_down(index)
elif len(self.store) > (right_child) and self.store[index].key > self.store[right_child].key:
self.swap(index, (right_child))
self.heap_down((right_child))
self.heap_down(index)

def swap(self, index_1, index_2):
""" Swaps two elements in self.store
Expand Down